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