]> git.proxmox.com Git - systemd.git/blob - src/shared/specifier.c
Merge tag 'upstream/229'
[systemd.git] / src / shared / specifier.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/utsname.h>
26
27 #include "sd-id128.h"
28
29 #include "alloc-util.h"
30 #include "hostname-util.h"
31 #include "macro.h"
32 #include "specifier.h"
33 #include "string-util.h"
34
35 /*
36 * Generic infrastructure for replacing %x style specifiers in
37 * strings. Will call a callback for each replacement.
38 *
39 */
40
41 int specifier_printf(const char *text, const Specifier table[], void *userdata, char **_ret) {
42 char *ret, *t;
43 const char *f;
44 bool percent = false;
45 size_t l;
46 int r;
47
48 assert(text);
49 assert(table);
50
51 l = strlen(text);
52 ret = new(char, l+1);
53 if (!ret)
54 return -ENOMEM;
55
56 t = ret;
57
58 for (f = text; *f; f++, l--) {
59
60 if (percent) {
61 if (*f == '%')
62 *(t++) = '%';
63 else {
64 const Specifier *i;
65
66 for (i = table; i->specifier; i++)
67 if (i->specifier == *f)
68 break;
69
70 if (i->lookup) {
71 _cleanup_free_ char *w = NULL;
72 char *n;
73 size_t k, j;
74
75 r = i->lookup(i->specifier, i->data, userdata, &w);
76 if (r < 0) {
77 free(ret);
78 return r;
79 }
80
81 j = t - ret;
82 k = strlen(w);
83
84 n = new(char, j + k + l + 1);
85 if (!n) {
86 free(ret);
87 return -ENOMEM;
88 }
89
90 memcpy(n, ret, j);
91 memcpy(n + j, w, k);
92
93 free(ret);
94
95 ret = n;
96 t = n + j + k;
97 } else {
98 *(t++) = '%';
99 *(t++) = *f;
100 }
101 }
102
103 percent = false;
104 } else if (*f == '%')
105 percent = true;
106 else
107 *(t++) = *f;
108 }
109
110 *t = 0;
111 *_ret = ret;
112 return 0;
113 }
114
115 /* Generic handler for simple string replacements */
116
117 int specifier_string(char specifier, void *data, void *userdata, char **ret) {
118 char *n;
119
120 n = strdup(strempty(data));
121 if (!n)
122 return -ENOMEM;
123
124 *ret = n;
125 return 0;
126 }
127
128 int specifier_machine_id(char specifier, void *data, void *userdata, char **ret) {
129 sd_id128_t id;
130 char *n;
131 int r;
132
133 r = sd_id128_get_machine(&id);
134 if (r < 0)
135 return r;
136
137 n = new(char, 33);
138 if (!n)
139 return -ENOMEM;
140
141 *ret = sd_id128_to_string(id, n);
142 return 0;
143 }
144
145 int specifier_boot_id(char specifier, void *data, void *userdata, char **ret) {
146 sd_id128_t id;
147 char *n;
148 int r;
149
150 r = sd_id128_get_boot(&id);
151 if (r < 0)
152 return r;
153
154 n = new(char, 33);
155 if (!n)
156 return -ENOMEM;
157
158 *ret = sd_id128_to_string(id, n);
159 return 0;
160 }
161
162 int specifier_host_name(char specifier, void *data, void *userdata, char **ret) {
163 char *n;
164
165 n = gethostname_malloc();
166 if (!n)
167 return -ENOMEM;
168
169 *ret = n;
170 return 0;
171 }
172
173 int specifier_kernel_release(char specifier, void *data, void *userdata, char **ret) {
174 struct utsname uts;
175 char *n;
176 int r;
177
178 r = uname(&uts);
179 if (r < 0)
180 return -errno;
181
182 n = strdup(uts.release);
183 if (!n)
184 return -ENOMEM;
185
186 *ret = n;
187 return 0;
188 }