1/*	$NetBSD: defined.c,v 1.2 2019/11/16 12:26:54 tkusumi Exp $	*/
2
3/*-
4 * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 * Copyright (c) 2016 The DragonFly Project
6 * Copyright (c) 2014 The FreeBSD Foundation
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
11 *
12 * This software was developed by Edward Tomasz Napierala under sponsorship
13 * from the FreeBSD Foundation.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 */
37#include <sys/cdefs.h>
38__RCSID("$NetBSD: defined.c,v 1.2 2019/11/16 12:26:54 tkusumi Exp $");
39
40/*
41 * All the "defined" stuff is for handling variables,
42 * such as ${OSNAME}, in maps.
43 */
44
45#include <sys/types.h>
46#include <sys/utsname.h>
47#include <assert.h>
48#include <ctype.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53
54#include "common.h"
55
56static TAILQ_HEAD(, defined_value)	defined_values;
57
58static const char *
59defined_find(const char *name)
60{
61	struct defined_value *d;
62
63	TAILQ_FOREACH(d, &defined_values, d_next) {
64		if (strcmp(d->d_name, name) == 0)
65			return d->d_value;
66	}
67
68	return NULL;
69}
70
71char *
72defined_expand(const char *string)
73{
74	const char *value;
75	char c, *expanded, *name;
76	int ret;
77	size_t i, before_len = 0, name_off = 0, name_len = 0, after_off = 0;
78	bool backslashed = false, bracketed = false;
79
80	expanded = checked_strdup(string);
81
82	for (i = 0; string[i] != '\0'; i++) {
83		c = string[i];
84		if (c == '\\' && backslashed == false) {
85			backslashed = true;
86			continue;
87		}
88		if (backslashed) {
89			backslashed = false;
90			continue;
91		}
92		backslashed = false;
93		if (c != '$')
94			continue;
95
96		/*
97		 * The 'before_len' variable contains the number
98		 * of characters before the '$'.
99		 */
100		before_len = i;
101		assert(i + 1 < strlen(string));
102		if (string[i + 1] == '{')
103			bracketed = true;
104
105		if (string[i + 1] == '\0') {
106			log_warnx("truncated variable");
107			return NULL;
108		}
109
110		/*
111		 * Skip '$'.
112		 */
113		i++;
114
115		if (bracketed) {
116			if (string[i + 1] == '\0') {
117				log_warnx("truncated variable");
118				return NULL;
119			}
120
121			/*
122			 * Skip '{'.
123			 */
124			i++;
125		}
126
127		/*
128		 * The 'name_off' variable contains the number
129		 * of characters before the variable name,
130		 * including the "$" or "${".
131		 */
132		name_off = i;
133
134		for (; string[i] != '\0'; i++) {
135			c = string[i];
136			/*
137			 * XXX: Decide on the set of characters that can be
138			 *	used in a variable name.
139			 */
140			if (isalnum((unsigned char)c) || c == '_')
141				continue;
142
143			/*
144			 * End of variable name.
145			 */
146			if (bracketed) {
147				if (c != '}')
148					continue;
149
150				/*
151				 * The 'after_off' variable contains the number
152				 * of characters before the rest of the string,
153				 * i.e. after the variable name.
154				 */
155				after_off = i + 1;
156				assert(i > 1);
157				assert(i - 1 > name_off);
158				name_len = i - name_off;
159				break;
160			}
161
162			after_off = i;
163			assert(i > 1);
164			assert(i > name_off);
165			name_len = i - name_off;
166			break;
167		}
168
169		name = strndup(string + name_off, name_len);
170		if (name == NULL)
171			log_err(1, "strndup");
172		value = defined_find(name);
173		if (value == NULL) {
174			log_warnx("undefined variable ${%s}", name);
175			return NULL;
176		}
177
178		/*
179		 * Concatenate it back.
180		 */
181		ret = asprintf(&expanded, "%.*s%s%s",
182		    (int)before_len, string, value, string + after_off);
183		if (ret < 0)
184			log_err(1, "asprintf");
185
186		//log_debugx("\"%s\" expanded to \"%s\"", string, expanded);
187		free(name);
188
189		/*
190		 * Figure out where to start searching for next variable.
191		 */
192		string = expanded;
193		i = before_len + strlen(value);
194		backslashed = bracketed = false;
195		before_len = name_off = name_len = after_off = 0;
196		assert(i <= strlen(string));
197	}
198
199	if (before_len != 0 || name_off != 0 || name_len != 0
200	    || after_off != 0) {
201		log_warnx("truncated variable");
202		return NULL;
203	}
204
205	return expanded;
206}
207
208static void
209defined_add(const char *name, const char *value)
210{
211	struct defined_value *d;
212	const char *found;
213
214	found = defined_find(name);
215	if (found != NULL)
216		log_errx(1, "variable %s already defined", name);
217
218	log_debugx("defining variable %s=%s", name, value);
219
220	d = calloc(1, sizeof(*d));
221	if (d == NULL)
222		log_err(1, "calloc");
223	d->d_name = checked_strdup(name);
224	d->d_value = checked_strdup(value);
225
226	TAILQ_INSERT_TAIL(&defined_values, d, d_next);
227}
228
229void
230defined_parse_and_add(char *def)
231{
232	char *name, *value;
233
234	value = def;
235	name = strsep(&value, "=");
236
237	if (value == NULL || value[0] == '\0')
238		log_errx(1, "missing variable value");
239	if (name == NULL || name[0] == '\0')
240		log_errx(1, "missing variable name");
241
242	defined_add(name, value);
243}
244
245void
246defined_init(void)
247{
248	struct utsname name;
249	int error;
250
251	TAILQ_INIT(&defined_values);
252
253	error = uname(&name);
254	if (error != 0)
255		log_err(1, "uname");
256
257	defined_add("ARCH", name.machine);
258	defined_add("CPU", name.machine);
259	defined_add("DOLLAR", "$");
260	defined_add("HOST", name.nodename);
261	defined_add("OSNAME", name.sysname);
262	defined_add("OSREL", name.release);
263	defined_add("OSVERS", name.version);
264}
265