chiark / gitweb /
util: unify SO_PEERCRED/SO_PEERSEC invocations
[elogind.git] / src / shared / replace-var.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 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 "replace-var.h"
27 #include "def.h"
28
29 /*
30  * Generic infrastructure for replacing @FOO@ style variables in
31  * strings. Will call a callback for each replacement.
32  */
33
34 static int get_variable(const char *b, char **r) {
35         size_t k;
36         char *t;
37
38         assert(b);
39         assert(r);
40
41         if (*b != '@')
42                 return 0;
43
44         k = strspn(b + 1, UPPERCASE_LETTERS "_");
45         if (k <= 0 || b[k+1] != '@')
46                 return 0;
47
48         t = strndup(b + 1, k);
49         if (!t)
50                 return -ENOMEM;
51
52         *r = t;
53         return 1;
54 }
55
56 char *replace_var(const char *text, char *(*lookup)(const char *variable, void*userdata), void *userdata) {
57         char *r, *t;
58         const char *f;
59         size_t l;
60
61         assert(text);
62         assert(lookup);
63
64         l = strlen(text);
65         r = new(char, l+1);
66         if (!r)
67                 return NULL;
68
69         f = text;
70         t = r;
71         while (*f) {
72                 _cleanup_free_ char *v = NULL, *n = NULL;
73                 char *a;
74                 int k;
75                 size_t skip, d, nl;
76
77                 k = get_variable(f, &v);
78                 if (k < 0)
79                         goto oom;
80                 if (k == 0) {
81                         *(t++) = *(f++);
82                         continue;
83                 }
84
85                 n = lookup(v, userdata);
86                 if (!n)
87                         goto oom;
88
89                 skip = strlen(v) + 2;
90
91                 d = t - r;
92                 nl = l - skip + strlen(n);
93                 a = realloc(r, nl + 1);
94                 if (!a)
95                         goto oom;
96
97                 l = nl;
98                 r = a;
99                 t = r + d;
100
101                 t = stpcpy(t, n);
102                 f += skip;
103         }
104
105         *t = 0;
106         return r;
107
108 oom:
109         free(r);
110         return NULL;
111 }