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 enum variable_flavor flavor;
189 struct list_head node;
192 static struct variable *variable_lookup(const char *name)
196 list_for_each_entry(v, &variable_list, node) {
197 if (!strcmp(name, v->name))
204 static char *variable_expand(const char *name, int argc, char *argv[])
209 v = variable_lookup(name);
213 if (v->flavor == VAR_RECURSIVE)
214 res = expand_string_with_args(v->value, argc, argv);
216 res = xstrdup(v->value);
221 void variable_add(const char *name, const char *value,
222 enum variable_flavor flavor)
226 v = variable_lookup(name);
230 v = xmalloc(sizeof(*v));
231 v->name = xstrdup(name);
232 list_add_tail(&v->node, &variable_list);
237 if (flavor == VAR_SIMPLE)
238 v->value = expand_string(value);
240 v->value = xstrdup(value);
243 static void variable_del(struct variable *v)
251 void variable_all_del(void)
253 struct variable *v, *tmp;
255 list_for_each_entry_safe(v, tmp, &variable_list, node)
260 * Evaluate a clause with arguments. argc/argv are arguments from the upper
263 * Returned string must be freed when done
265 static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
267 char *tmp, *name, *res, *endptr, *prev, *p;
269 char *new_argv[FUNCTION_MAX_ARGS];
274 tmp = xstrndup(str, len);
277 * If variable name is '1', '2', etc. It is generally an argument
278 * from a user-function call (i.e. local-scope variable). If not
279 * available, then look-up global-scope variables.
281 n = strtoul(tmp, &endptr, 10);
282 if (!*endptr && n > 0 && n <= argc) {
283 res = xstrdup(argv[n - 1]);
291 * The function name and arguments are separated by a comma.
292 * For example, if the function call is like this:
295 * The input string for this helper should be:
299 * new_argv[0] = 'foo'
300 * new_argv[1] = '$(x)'
301 * new_argv[2] = '$(y)'
304 if (nest == 0 && *p == ',') {
306 if (new_argc >= FUNCTION_MAX_ARGS)
307 pperror("too many function arguments");
308 new_argv[new_argc++] = prev;
310 } else if (*p == '(') {
312 } else if (*p == ')') {
318 new_argv[new_argc++] = prev;
322 * new_argv[0] represents a function name or a variable name. Put it
323 * into 'name', then shift the rest of the arguments. This simplifies
326 name = expand_string_with_args(new_argv[0], argc, argv);
328 for (i = 0; i < new_argc; i++)
329 new_argv[i] = expand_string_with_args(new_argv[i + 1],
332 /* Search for variables */
333 res = variable_expand(name, new_argc, new_argv);
337 /* Look for built-in functions */
338 res = function_expand(name, new_argc, new_argv);
342 /* Last, try environment variable */
344 res = env_expand(name);
351 for (i = 0; i < new_argc; i++)
361 * Expand a string that follows '$'
363 * For example, if the input string is
364 * ($(FOO)$($(BAR)))$(BAZ)
365 * this helper evaluates
367 * and returns a new string containing the expansion (note that the string is
368 * recursively expanded), also advancing 'str' to point to the next character
369 * after the corresponding closing parenthesis, in this case, *str will be
372 static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
374 const char *p = *str;
379 * In Kconfig, variable/function references always start with "$(".
380 * Neither single-letter variables as in $A nor curly braces as in ${CC}
381 * are supported. '$' not followed by '(' loses its special meaning.
393 } else if (*q == ')') {
401 pperror("unterminated reference to '%s': missing ')'", p);
403 /* Advance 'str' to after the expanded initial portion of the string */
406 return eval_clause(p, q - p, argc, argv);
409 char *expand_dollar(const char **str)
411 return expand_dollar_with_args(str, 0, NULL);
414 static char *__expand_string(const char **str, bool (*is_end)(char c),
415 int argc, char *argv[])
418 char *expansion, *out;
419 size_t in_len, out_len;
431 expansion = expand_dollar_with_args(&p, argc, argv);
432 out_len += in_len + strlen(expansion);
433 out = xrealloc(out, out_len);
434 strncat(out, in, in_len);
435 strcat(out, expansion);
449 out = xrealloc(out, out_len);
450 strncat(out, in, in_len);
452 /* Advance 'str' to the end character */
458 static bool is_end_of_str(char c)
464 * Expand variables and functions in the given string. Undefined variables
465 * expand to an empty string.
466 * The returned string must be freed when done.
468 static char *expand_string_with_args(const char *in, int argc, char *argv[])
470 return __expand_string(&in, is_end_of_str, argc, argv);
473 char *expand_string(const char *in)
475 return expand_string_with_args(in, 0, NULL);
478 static bool is_end_of_token(char c)
480 /* Why are '.' and '/' valid characters for symbols? */
481 return !(isalnum(c) || c == '_' || c == '-' || c == '.' || c == '/');
485 * Expand variables in a token. The parsing stops when a token separater
486 * (in most cases, it is a whitespace) is encountered. 'str' is updated to
487 * point to the next character.
489 * The returned string must be freed when done.
491 char *expand_one_token(const char **str)
493 return __expand_string(str, is_end_of_token, 0, NULL);