1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2018 Masahiro Yamada <yamada.masahiro@socionext.com>
13 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
15 static char *expand_string_with_args(const char *in, int argc, char *argv[]);
17 static void __attribute__((noreturn)) pperror(const char *format, ...)
21 fprintf(stderr, "%s:%d: ", current_file->name, yylineno);
23 vfprintf(stderr, format, ap);
25 fprintf(stderr, "\n");
31 * Environment variables
33 static LIST_HEAD(env_list);
38 struct list_head node;
41 static void env_add(const char *name, const char *value)
45 e = xmalloc(sizeof(*e));
46 e->name = xstrdup(name);
47 e->value = xstrdup(value);
49 list_add_tail(&e->node, &env_list);
52 static void env_del(struct env *e)
60 /* The returned pointer must be freed when done */
61 static char *env_expand(const char *name)
69 list_for_each_entry(e, &env_list, node) {
70 if (!strcmp(name, e->name))
71 return xstrdup(e->value);
79 * We need to remember all referenced environment variables.
80 * They will be written out to include/config/auto.conf.cmd
84 return xstrdup(value);
87 void env_write_dep(FILE *f, const char *autoconfig_name)
91 list_for_each_entry_safe(e, tmp, &env_list, node) {
92 fprintf(f, "ifneq \"$(%s)\" \"%s\"\n", e->name, e->value);
93 fprintf(f, "%s: FORCE\n", autoconfig_name);
94 fprintf(f, "endif\n");
104 unsigned int min_args;
105 unsigned int max_args;
106 char *(*func)(int argc, char *argv[]);
109 static char *do_shell(int argc, char *argv[])
125 nread = fread(buf, 1, sizeof(buf), p);
126 if (nread == sizeof(buf))
129 /* remove trailing new lines */
130 while (buf[nread - 1] == '\n')
135 /* replace a new line with a space */
136 for (i = 0; i < nread; i++) {
141 if (pclose(p) == -1) {
149 static const struct function function_table[] = {
150 /* Name MIN MAX Function */
151 { "shell", 1, 1, do_shell },
154 #define FUNCTION_MAX_ARGS 16
156 static char *function_expand(const char *name, int argc, char *argv[])
158 const struct function *f;
161 for (i = 0; i < ARRAY_SIZE(function_table); i++) {
162 f = &function_table[i];
163 if (strcmp(f->name, name))
166 if (argc < f->min_args)
167 pperror("too few function arguments passed to '%s'",
170 if (argc > f->max_args)
171 pperror("too many function arguments passed to '%s'",
174 return f->func(argc, argv);
181 * Variables (and user-defined functions)
183 static LIST_HEAD(variable_list);
188 struct list_head node;
191 static struct variable *variable_lookup(const char *name)
195 list_for_each_entry(v, &variable_list, node) {
196 if (!strcmp(name, v->name))
203 static char *variable_expand(const char *name, int argc, char *argv[])
207 v = variable_lookup(name);
211 return expand_string_with_args(v->value, argc, argv);
214 void variable_add(const char *name, const char *value)
218 v = variable_lookup(name);
222 v = xmalloc(sizeof(*v));
223 v->name = xstrdup(name);
224 list_add_tail(&v->node, &variable_list);
227 v->value = xstrdup(value);
230 static void variable_del(struct variable *v)
238 void variable_all_del(void)
240 struct variable *v, *tmp;
242 list_for_each_entry_safe(v, tmp, &variable_list, node)
247 * Evaluate a clause with arguments. argc/argv are arguments from the upper
250 * Returned string must be freed when done
252 static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
254 char *tmp, *name, *res, *endptr, *prev, *p;
256 char *new_argv[FUNCTION_MAX_ARGS];
261 tmp = xstrndup(str, len);
264 * If variable name is '1', '2', etc. It is generally an argument
265 * from a user-function call (i.e. local-scope variable). If not
266 * available, then look-up global-scope variables.
268 n = strtoul(tmp, &endptr, 10);
269 if (!*endptr && n > 0 && n <= argc) {
270 res = xstrdup(argv[n - 1]);
278 * The function name and arguments are separated by a comma.
279 * For example, if the function call is like this:
282 * The input string for this helper should be:
286 * new_argv[0] = 'foo'
287 * new_argv[1] = '$(x)'
288 * new_argv[2] = '$(y)'
291 if (nest == 0 && *p == ',') {
293 if (new_argc >= FUNCTION_MAX_ARGS)
294 pperror("too many function arguments");
295 new_argv[new_argc++] = prev;
297 } else if (*p == '(') {
299 } else if (*p == ')') {
305 new_argv[new_argc++] = prev;
309 * new_argv[0] represents a function name or a variable name. Put it
310 * into 'name', then shift the rest of the arguments. This simplifies
313 name = expand_string_with_args(new_argv[0], argc, argv);
315 for (i = 0; i < new_argc; i++)
316 new_argv[i] = expand_string_with_args(new_argv[i + 1],
319 /* Search for variables */
320 res = variable_expand(name, new_argc, new_argv);
324 /* Look for built-in functions */
325 res = function_expand(name, new_argc, new_argv);
329 /* Last, try environment variable */
331 res = env_expand(name);
338 for (i = 0; i < new_argc; i++)
348 * Expand a string that follows '$'
350 * For example, if the input string is
351 * ($(FOO)$($(BAR)))$(BAZ)
352 * this helper evaluates
354 * and returns a new string containing the expansion (note that the string is
355 * recursively expanded), also advancing 'str' to point to the next character
356 * after the corresponding closing parenthesis, in this case, *str will be
359 static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
361 const char *p = *str;
366 * In Kconfig, variable/function references always start with "$(".
367 * Neither single-letter variables as in $A nor curly braces as in ${CC}
368 * are supported. '$' not followed by '(' loses its special meaning.
380 } else if (*q == ')') {
388 pperror("unterminated reference to '%s': missing ')'", p);
390 /* Advance 'str' to after the expanded initial portion of the string */
393 return eval_clause(p, q - p, argc, argv);
396 char *expand_dollar(const char **str)
398 return expand_dollar_with_args(str, 0, NULL);
401 static char *__expand_string(const char **str, bool (*is_end)(char c),
402 int argc, char *argv[])
405 char *expansion, *out;
406 size_t in_len, out_len;
418 expansion = expand_dollar_with_args(&p, argc, argv);
419 out_len += in_len + strlen(expansion);
420 out = xrealloc(out, out_len);
421 strncat(out, in, in_len);
422 strcat(out, expansion);
436 out = xrealloc(out, out_len);
437 strncat(out, in, in_len);
439 /* Advance 'str' to the end character */
445 static bool is_end_of_str(char c)
451 * Expand variables and functions in the given string. Undefined variables
452 * expand to an empty string.
453 * The returned string must be freed when done.
455 static char *expand_string_with_args(const char *in, int argc, char *argv[])
457 return __expand_string(&in, is_end_of_str, argc, argv);
460 char *expand_string(const char *in)
462 return expand_string_with_args(in, 0, NULL);
465 static bool is_end_of_token(char c)
467 /* Why are '.' and '/' valid characters for symbols? */
468 return !(isalnum(c) || c == '_' || c == '-' || c == '.' || c == '/');
472 * Expand variables in a token. The parsing stops when a token separater
473 * (in most cases, it is a whitespace) is encountered. 'str' is updated to
474 * point to the next character.
476 * The returned string must be freed when done.
478 char *expand_one_token(const char **str)
480 return __expand_string(str, is_end_of_token, 0, NULL);