1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
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.
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.
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/>.
26 #include "replace-var.h"
29 * Generic infrastructure for replacing @FOO@ style variables in
30 * strings. Will call a callback for each replacement.
33 static int get_variable(const char *b, char **r) {
43 k = strspn(b + 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ_");
44 if (k <= 0 || b[k+1] != '@')
47 t = strndup(b + 1, k);
55 char *replace_var(const char *text, char *(*lookup)(const char *variable, void*userdata), void *userdata) {
71 _cleanup_free_ char *v = NULL, *n = NULL;
76 k = get_variable(f, &v);
84 n = lookup(v, userdata);
91 nl = l - skip + strlen(n);
92 a = realloc(r, nl + 1);