2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * All the "defined" stuff is for handling variables,
35 * such as ${OSNAME}, in maps.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/types.h>
43 #include <sys/ioctl.h>
44 #include <sys/param.h>
45 #include <sys/linker.h>
46 #include <sys/mount.h>
47 #include <sys/socket.h>
50 #include <sys/utsname.h>
68 static TAILQ_HEAD(, defined_value) defined_values;
71 defined_find(const char *name)
73 struct defined_value *d;
75 TAILQ_FOREACH(d, &defined_values, d_next) {
76 if (strcmp(d->d_name, name) == 0)
84 defined_expand(const char *string)
87 char c, *expanded, *name;
88 int i, ret, before_len = 0, name_off = 0, name_len = 0, after_off = 0;
89 bool backslashed = false, bracketed = false;
91 expanded = checked_strdup(string);
93 for (i = 0; string[i] != '\0'; i++) {
95 if (c == '\\' && backslashed == false) {
108 * The 'before_len' variable contains the number
109 * of characters before the '$'.
112 assert(i + 1 < (int)strlen(string));
113 if (string[i + 1] == '{')
116 if (string[i + 1] == '\0') {
117 log_warnx("truncated variable");
127 if (string[i + 1] == '\0') {
128 log_warnx("truncated variable");
139 * The 'name_off' variable contains the number
140 * of characters before the variable name,
141 * including the "$" or "${".
145 for (; string[i] != '\0'; i++) {
148 * XXX: Decide on the set of characters that can be
149 * used in a variable name.
151 if (isalnum(c) || c == '_')
155 * End of variable name.
162 * The 'after_off' variable contains the number
163 * of characters before the rest of the string,
164 * i.e. after the variable name.
168 assert(i - 1 > name_off);
169 name_len = i - name_off;
175 assert(i > name_off);
176 name_len = i - name_off;
180 name = strndup(string + name_off, name_len);
182 log_err(1, "strndup");
183 value = defined_find(name);
185 log_warnx("undefined variable ${%s}", name);
190 * Concatenate it back.
192 ret = asprintf(&expanded, "%.*s%s%s",
193 before_len, string, value, string + after_off);
195 log_err(1, "asprintf");
197 //log_debugx("\"%s\" expanded to \"%s\"", string, expanded);
201 * Figure out where to start searching for next variable.
204 i = before_len + strlen(value);
205 backslashed = bracketed = false;
206 before_len = name_off = name_len = after_off = 0;
207 assert(i <= (int)strlen(string));
210 if (before_len != 0 || name_off != 0 || name_len != 0 || after_off != 0) {
211 log_warnx("truncated variable");
219 defined_add(const char *name, const char *value)
221 struct defined_value *d;
224 found = defined_find(name);
226 log_errx(1, "variable %s already defined", name);
228 log_debugx("defining variable %s=%s", name, value);
230 d = calloc(1, sizeof(*d));
232 log_err(1, "calloc");
233 d->d_name = checked_strdup(name);
234 d->d_value = checked_strdup(value);
236 TAILQ_INSERT_TAIL(&defined_values, d, d_next);
240 defined_parse_and_add(char *def)
245 name = strsep(&value, "=");
247 if (value == NULL || value[0] == '\0')
248 log_errx(1, "missing variable value");
249 if (name == NULL || name[0] == '\0')
250 log_errx(1, "missing variable name");
252 defined_add(name, value);
261 TAILQ_INIT(&defined_values);
263 error = uname(&name);
267 defined_add("ARCH", name.machine);
268 defined_add("CPU", name.machine);
269 defined_add("DOLLAR", "$");
270 defined_add("HOST", name.nodename);
271 defined_add("OSNAME", name.sysname);
272 defined_add("OSREL", name.release);
273 defined_add("OSVERS", name.version);