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