chiark / gitweb /
core: add %v specifier
[elogind.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 "macro.h"
26 #include "util.h"
27 #include "specifier.h"
28
29 /*
30  * Generic infrastructure for replacing %x style specifiers in
31  * strings. Will call a callback for each replacement.
32  *
33  */
34
35 char *specifier_printf(const char *text, const Specifier table[], void *userdata) {
36         char *r, *t;
37         const char *f;
38         bool percent = false;
39         size_t l;
40
41         assert(text);
42         assert(table);
43
44         l = strlen(text);
45         r = new(char, l+1);
46         if (!r)
47                 return NULL;
48
49         t = r;
50
51         for (f = text; *f; f++, l--) {
52
53                 if (percent) {
54                         if (*f == '%')
55                                 *(t++) = '%';
56                         else {
57                                 const Specifier *i;
58
59                                 for (i = table; i->specifier; i++)
60                                         if (i->specifier == *f)
61                                                 break;
62
63                                 if (i->lookup) {
64                                         char *n, *w;
65                                         size_t k, j;
66
67                                         w = i->lookup(i->specifier, i->data, userdata);
68                                         if (!w) {
69                                                 free(r);
70                                                 return NULL;
71                                         }
72
73                                         j = t - r;
74                                         k = strlen(w);
75
76                                         n = new(char, j + k + l + 1);
77                                         if (!n) {
78                                                 free(r);
79                                                 free(w);
80                                                 return NULL;
81                                         }
82
83                                         memcpy(n, r, j);
84                                         memcpy(n + j, w, k);
85
86                                         free(r);
87                                         free(w);
88
89                                         r = n;
90                                         t = n + j + k;
91                                 } else {
92                                         *(t++) = '%';
93                                         *(t++) = *f;
94                                 }
95                         }
96
97                         percent = false;
98                 } else if (*f == '%')
99                         percent = true;
100                 else
101                         *(t++) = *f;
102         }
103
104         *t = 0;
105         return r;
106 }
107
108 /* Generic handler for simple string replacements */
109
110 char* specifier_string(char specifier, void *data, void *userdata) {
111         return strdup(strempty(data));
112 }
113
114 char *specifier_machine_id(char specifier, void *data, void *userdata) {
115         sd_id128_t id;
116         char *buf;
117         int r;
118
119         r = sd_id128_get_machine(&id);
120         if (r < 0)
121                 return NULL;
122
123         buf = new(char, 33);
124         if (!buf)
125                 return NULL;
126
127         return sd_id128_to_string(id, buf);
128 }
129
130 char *specifier_boot_id(char specifier, void *data, void *userdata) {
131         sd_id128_t id;
132         char *buf;
133         int r;
134
135         r = sd_id128_get_boot(&id);
136         if (r < 0)
137                 return NULL;
138
139         buf = new(char, 33);
140         if (!buf)
141                 return NULL;
142
143         return sd_id128_to_string(id, buf);
144 }
145
146 char *specifier_host_name(char specifier, void *data, void *userdata) {
147         return gethostname_malloc();
148 }
149
150 char *specifier_kernel_release(char specifier, void *data, void *userdata) {
151         struct utsname uts;
152         int r;
153
154         r = uname(&uts);
155         if (r < 0)
156                 return NULL;
157
158         return strdup(uts.release);
159 }