]>
git.proxmox.com Git - systemd.git/blob - src/basic/replace-var.c
2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "alloc-util.h"
27 #include "replace-var.h"
28 #include "string-util.h"
31 * Generic infrastructure for replacing @FOO@ style variables in
32 * strings. Will call a callback for each replacement.
35 static int get_variable(const char *b
, char **r
) {
45 k
= strspn(b
+ 1, UPPERCASE_LETTERS
"_");
46 if (k
<= 0 || b
[k
+1] != '@')
49 t
= strndup(b
+ 1, k
);
57 char *replace_var(const char *text
, char *(*lookup
)(const char *variable
, void*userdata
), void *userdata
) {
73 _cleanup_free_
char *v
= NULL
, *n
= NULL
;
78 k
= get_variable(f
, &v
);
86 n
= lookup(v
, userdata
);
93 nl
= l
- skip
+ strlen(n
);
94 a
= realloc(r
, nl
+ 1);