chiark / gitweb /
initctl: talk to systemd via the abstract private bus
[elogind.git] / src / specifier.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU 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         if (!(r = new(char, l+1)))
45                 return NULL;
46
47         t = r;
48
49         for (f = text; *f; f++, l--) {
50
51                 if (percent) {
52                         if (*f == '%')
53                                 *(t++) = '%';
54                         else {
55                                 const Specifier *i;
56
57                                 for (i = table; i->specifier; i++)
58                                         if (i->specifier == *f)
59                                                 break;
60
61                                 if (i->lookup) {
62                                         char *n, *w;
63                                         size_t k, j;
64
65                                         if (!(w = i->lookup(i->specifier, i->data, userdata))) {
66                                                 free(r);
67                                                 return NULL;
68                                         }
69
70                                         j = t - r;
71                                         k = strlen(w);
72
73                                         if (!(n = new(char, j + k + l + 1))) {
74                                                 free(r);
75                                                 free(w);
76                                                 return NULL;
77                                         }
78
79                                         memcpy(n, r, j);
80                                         memcpy(n + j, w, k);
81
82                                         free(r);
83                                         free(w);
84
85                                         r = n;
86                                         t = n + j + k;
87                                 } else {
88                                         *(t++) = '%';
89                                         *(t++) = *f;
90                                 }
91                         }
92
93                         percent = false;
94                 } else if (*f == '%')
95                         percent = true;
96                 else
97                         *(t++) = *f;
98         }
99
100         *t = 0;
101         return r;
102 }
103
104 /* Generic handler for simple string replacements */
105
106 char* specifier_string(char specifier, void *data, void *userdata) {
107         assert(data);
108
109         return strdup(strempty(data));
110 }