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