2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * All the "defined" stuff is for handling variables,
34 * such as ${OSNAME}, in maps.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
42 #include <sys/ioctl.h>
43 #include <sys/param.h>
44 #include <sys/linker.h>
45 #include <sys/mount.h>
46 #include <sys/socket.h>
49 #include <sys/utsname.h>
67 static TAILQ_HEAD(, defined_value) defined_values;
70 defined_find(const char *name)
72 struct defined_value *d;
74 TAILQ_FOREACH(d, &defined_values, d_next) {
75 if (strcmp(d->d_name, name) == 0)
83 defined_expand(const char *string)
86 char c, *expanded, *name;
87 int i, ret, before_len = 0, name_off = 0, name_len = 0, after_off = 0;
88 bool backslashed = false, bracketed = false;
90 expanded = checked_strdup(string);
92 for (i = 0; string[i] != '\0'; i++) {
94 if (c == '\\' && backslashed == false) {
107 * The 'before_len' variable contains the number
108 * of characters before the '$'.
111 assert(i + 1 < (int)strlen(string));
112 if (string[i + 1] == '{')
115 if (string[i + 1] == '\0') {
116 log_warnx("truncated variable");
126 if (string[i + 1] == '\0') {
127 log_warnx("truncated variable");
138 * The 'name_off' variable contains the number
139 * of characters before the variable name,
140 * including the "$" or "${".
144 for (; string[i] != '\0'; i++) {
147 * XXX: Decide on the set of characters that can be
148 * used in a variable name.
150 if (isalnum(c) || c == '_')
154 * End of variable name.
161 * The 'after_off' variable contains the number
162 * of characters before the rest of the string,
163 * i.e. after the variable name.
167 assert(i - 1 > name_off);
168 name_len = i - name_off;
174 assert(i > name_off);
175 name_len = i - name_off;
179 name = strndup(string + name_off, name_len);
181 log_err(1, "strndup");
182 value = defined_find(name);
184 log_warnx("undefined variable ${%s}", name);
189 * Concatenate it back.
191 ret = asprintf(&expanded, "%.*s%s%s",
192 before_len, string, value, string + after_off);
194 log_err(1, "asprintf");
196 //log_debugx("\"%s\" expanded to \"%s\"", string, expanded);
200 * Figure out where to start searching for next variable.
203 i = before_len + strlen(value);
204 backslashed = bracketed = false;
205 before_len = name_off = name_len = after_off = 0;
206 assert(i <= (int)strlen(string));
209 if (before_len != 0 || name_off != 0 || name_len != 0 || after_off != 0) {
210 log_warnx("truncated variable");
218 defined_add(const char *name, const char *value)
220 struct defined_value *d;
223 found = defined_find(name);
225 log_errx(1, "variable %s already defined", name);
227 log_debugx("defining variable %s=%s", name, value);
229 d = calloc(1, sizeof(*d));
231 log_err(1, "calloc");
232 d->d_name = checked_strdup(name);
233 d->d_value = checked_strdup(value);
235 TAILQ_INSERT_TAIL(&defined_values, d, d_next);
239 defined_parse_and_add(char *def)
244 name = strsep(&value, "=");
246 if (value == NULL || value[0] == '\0')
247 log_errx(1, "missing variable value");
248 if (name == NULL || name[0] == '\0')
249 log_errx(1, "missing variable name");
251 defined_add(name, value);
260 TAILQ_INIT(&defined_values);
262 error = uname(&name);
266 defined_add("ARCH", name.machine);
267 defined_add("CPU", name.machine);
268 defined_add("DOLLAR", "$");
269 defined_add("HOST", name.nodename);
270 defined_add("OSNAME", name.sysname);
271 defined_add("OSREL", name.release);
272 defined_add("OSVERS", name.version);