Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // Copyright (C) 2018 Masahiro Yamada <yamada.masahiro@socionext.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "list.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "lkc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define ARRAY_SIZE(arr)		(sizeof(arr) / sizeof((arr)[0]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static char *expand_string_with_args(const char *in, int argc, char *argv[]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) static char *expand_string(const char *in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static void __attribute__((noreturn)) pperror(const char *format, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	fprintf(stderr, "%s:%d: ", current_file->name, yylineno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	va_start(ap, format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	vfprintf(stderr, format, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	fprintf(stderr, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * Environment variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static LIST_HEAD(env_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) struct env {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	char *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static void env_add(const char *name, const char *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct env *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	e = xmalloc(sizeof(*e));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	e->name = xstrdup(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	e->value = xstrdup(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	list_add_tail(&e->node, &env_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static void env_del(struct env *e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	list_del(&e->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	free(e->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	free(e->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	free(e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) /* The returned pointer must be freed when done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static char *env_expand(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct env *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	const char *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (!*name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	list_for_each_entry(e, &env_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		if (!strcmp(name, e->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			return xstrdup(e->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	value = getenv(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 * We need to remember all referenced environment variables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	 * They will be written out to include/config/auto.conf.cmd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	env_add(name, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return xstrdup(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) void env_write_dep(FILE *f, const char *autoconfig_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct env *e, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	list_for_each_entry_safe(e, tmp, &env_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		fprintf(f, "ifneq \"$(%s)\" \"%s\"\n", e->name, e->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		fprintf(f, "%s: FORCE\n", autoconfig_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		fprintf(f, "endif\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		env_del(e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * Built-in functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct function {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	unsigned int min_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	unsigned int max_args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	char *(*func)(int argc, char *argv[]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static char *do_error_if(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (!strcmp(argv[0], "y"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		pperror("%s", argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	return xstrdup("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static char *do_filename(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return xstrdup(current_file->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static char *do_info(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	printf("%s\n", argv[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return xstrdup("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static char *do_lineno(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	char buf[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	sprintf(buf, "%d", yylineno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return xstrdup(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static char *do_shell(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	FILE *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	char buf[4096];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	char *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	size_t nread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	cmd = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	p = popen(cmd, "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		perror(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	nread = fread(buf, 1, sizeof(buf), p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (nread == sizeof(buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		nread--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	/* remove trailing new lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	while (nread > 0 && buf[nread - 1] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		nread--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	buf[nread] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	/* replace a new line with a space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	for (i = 0; i < nread; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		if (buf[i] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			buf[i] = ' ';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (pclose(p) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		perror(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	return xstrdup(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static char *do_warning_if(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (!strcmp(argv[0], "y"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		fprintf(stderr, "%s:%d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			current_file->name, yylineno, argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return xstrdup("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static const struct function function_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	/* Name		MIN	MAX	Function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	{ "error-if",	2,	2,	do_error_if },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	{ "filename",	0,	0,	do_filename },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	{ "info",	1,	1,	do_info },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	{ "lineno",	0,	0,	do_lineno },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	{ "shell",	1,	1,	do_shell },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	{ "warning-if",	2,	2,	do_warning_if },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) #define FUNCTION_MAX_ARGS		16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static char *function_expand(const char *name, int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	const struct function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	for (i = 0; i < ARRAY_SIZE(function_table); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		f = &function_table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		if (strcmp(f->name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		if (argc < f->min_args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			pperror("too few function arguments passed to '%s'",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 				name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		if (argc > f->max_args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			pperror("too many function arguments passed to '%s'",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 				name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		return f->func(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  * Variables (and user-defined functions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static LIST_HEAD(variable_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct variable {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	char *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	enum variable_flavor flavor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	int exp_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static struct variable *variable_lookup(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	struct variable *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	list_for_each_entry(v, &variable_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		if (!strcmp(name, v->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static char *variable_expand(const char *name, int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	struct variable *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	char *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	v = variable_lookup(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (!v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (argc == 0 && v->exp_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		pperror("Recursive variable '%s' references itself (eventually)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (v->exp_count > 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		pperror("Too deep recursive expansion");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	v->exp_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (v->flavor == VAR_RECURSIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		res = expand_string_with_args(v->value, argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		res = xstrdup(v->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	v->exp_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) void variable_add(const char *name, const char *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		  enum variable_flavor flavor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct variable *v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	char *new_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	bool append = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	v = variable_lookup(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (v) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		/* For defined variables, += inherits the existing flavor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		if (flavor == VAR_APPEND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			flavor = v->flavor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			append = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			free(v->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		/* For undefined variables, += assumes the recursive flavor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		if (flavor == VAR_APPEND)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			flavor = VAR_RECURSIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		v = xmalloc(sizeof(*v));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		v->name = xstrdup(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		v->exp_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		list_add_tail(&v->node, &variable_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	v->flavor = flavor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (flavor == VAR_SIMPLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		new_value = expand_string(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		new_value = xstrdup(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (append) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		v->value = xrealloc(v->value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 				    strlen(v->value) + strlen(new_value) + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		strcat(v->value, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		strcat(v->value, new_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		free(new_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		v->value = new_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static void variable_del(struct variable *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	list_del(&v->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	free(v->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	free(v->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	free(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) void variable_all_del(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	struct variable *v, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	list_for_each_entry_safe(v, tmp, &variable_list, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		variable_del(v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  * Evaluate a clause with arguments.  argc/argv are arguments from the upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  * function call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * Returned string must be freed when done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	char *tmp, *name, *res, *endptr, *prev, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	int new_argc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	char *new_argv[FUNCTION_MAX_ARGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	int nest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	unsigned long n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	tmp = xstrndup(str, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	 * If variable name is '1', '2', etc.  It is generally an argument
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	 * from a user-function call (i.e. local-scope variable).  If not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	 * available, then look-up global-scope variables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	n = strtoul(tmp, &endptr, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (!*endptr && n > 0 && n <= argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		res = xstrdup(argv[n - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		goto free_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	prev = p = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	 * Split into tokens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	 * The function name and arguments are separated by a comma.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	 * For example, if the function call is like this:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	 *   $(foo,$(x),$(y))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	 * The input string for this helper should be:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	 *   foo,$(x),$(y)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	 * and split into:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	 *   new_argv[0] = 'foo'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	 *   new_argv[1] = '$(x)'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	 *   new_argv[2] = '$(y)'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		if (nest == 0 && *p == ',') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			*p = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 			if (new_argc >= FUNCTION_MAX_ARGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 				pperror("too many function arguments");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 			new_argv[new_argc++] = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			prev = p + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		} else if (*p == '(') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			nest++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		} else if (*p == ')') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			nest--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	new_argv[new_argc++] = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	 * Shift arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	 * new_argv[0] represents a function name or a variable name.  Put it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	 * into 'name', then shift the rest of the arguments.  This simplifies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	 * 'const' handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	name = expand_string_with_args(new_argv[0], argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	new_argc--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	for (i = 0; i < new_argc; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		new_argv[i] = expand_string_with_args(new_argv[i + 1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 						      argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	/* Search for variables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	res = variable_expand(name, new_argc, new_argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	/* Look for built-in functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	res = function_expand(name, new_argc, new_argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	/* Last, try environment variable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (new_argc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		res = env_expand(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	res = xstrdup("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	for (i = 0; i < new_argc; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		free(new_argv[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	free(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) free_tmp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	free(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)  * Expand a string that follows '$'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)  * For example, if the input string is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)  *     ($(FOO)$($(BAR)))$(BAZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)  * this helper evaluates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)  *     $($(FOO)$($(BAR)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)  * and returns a new string containing the expansion (note that the string is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)  * recursively expanded), also advancing 'str' to point to the next character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  * after the corresponding closing parenthesis, in this case, *str will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  *     $(BAR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	const char *p = *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	const char *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	int nest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	 * In Kconfig, variable/function references always start with "$(".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	 * Neither single-letter variables as in $A nor curly braces as in ${CC}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	 * are supported.  '$' not followed by '(' loses its special meaning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (*p != '(') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		*str = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		return xstrdup("$");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	q = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	while (*q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		if (*q == '(') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			nest++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		} else if (*q == ')') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 			if (nest-- == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		q++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	if (!*q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		pperror("unterminated reference to '%s': missing ')'", p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	/* Advance 'str' to after the expanded initial portion of the string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	*str = q + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	return eval_clause(p, q - p, argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) char *expand_dollar(const char **str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	return expand_dollar_with_args(str, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) static char *__expand_string(const char **str, bool (*is_end)(char c),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			     int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	const char *in, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	char *expansion, *out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	size_t in_len, out_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	out = xmalloc(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	*out = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	out_len = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	p = in = *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		if (*p == '$') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			in_len = p - in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 			p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 			expansion = expand_dollar_with_args(&p, argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 			out_len += in_len + strlen(expansion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 			out = xrealloc(out, out_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			strncat(out, in, in_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 			strcat(out, expansion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 			free(expansion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			in = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		if (is_end(*p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	in_len = p - in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	out_len += in_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	out = xrealloc(out, out_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	strncat(out, in, in_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	/* Advance 'str' to the end character */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	*str = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	return out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) static bool is_end_of_str(char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	return !c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)  * Expand variables and functions in the given string.  Undefined variables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)  * expand to an empty string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)  * The returned string must be freed when done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) static char *expand_string_with_args(const char *in, int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	return __expand_string(&in, is_end_of_str, argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) static char *expand_string(const char *in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	return expand_string_with_args(in, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) static bool is_end_of_token(char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	return !(isalnum(c) || c == '_' || c == '-');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)  * Expand variables in a token.  The parsing stops when a token separater
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)  * (in most cases, it is a whitespace) is encountered.  'str' is updated to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)  * point to the next character.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)  * The returned string must be freed when done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) char *expand_one_token(const char **str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	return __expand_string(str, is_end_of_token, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }