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_error_if(int argc, char *argv[])
111 if (!strcmp(argv[0], "y"))
112 pperror("%s", argv[1]);
117 static char *do_filename(int argc, char *argv[])
119 return xstrdup(current_file->name);
122 static char *do_info(int argc, char *argv[])
124 printf("%s\n", argv[0]);
129 static char *do_lineno(int argc, char *argv[])
133 sprintf(buf, "%d", yylineno);
138 static char *do_shell(int argc, char *argv[])
154 nread = fread(buf, 1, sizeof(buf), p);
155 if (nread == sizeof(buf))
158 /* remove trailing new lines */
159 while (buf[nread - 1] == '\n')
164 /* replace a new line with a space */
165 for (i = 0; i < nread; i++) {
170 if (pclose(p) == -1) {
178 static char *do_warning_if(int argc, char *argv[])
180 if (!strcmp(argv[0], "y"))
181 fprintf(stderr, "%s:%d: %s\n",
182 current_file->name, yylineno, argv[1]);
187 static const struct function function_table[] = {
188 /* Name MIN MAX Function */
189 { "error-if", 2, 2, do_error_if },
190 { "filename", 0, 0, do_filename },
191 { "info", 1, 1, do_info },
192 { "lineno", 0, 0, do_lineno },
193 { "shell", 1, 1, do_shell },
194 { "warning-if", 2, 2, do_warning_if },
197 #define FUNCTION_MAX_ARGS 16
199 static char *function_expand(const char *name, int argc, char *argv[])
201 const struct function *f;
204 for (i = 0; i < ARRAY_SIZE(function_table); i++) {
205 f = &function_table[i];
206 if (strcmp(f->name, name))
209 if (argc < f->min_args)
210 pperror("too few function arguments passed to '%s'",
213 if (argc > f->max_args)
214 pperror("too many function arguments passed to '%s'",
217 return f->func(argc, argv);
224 * Variables (and user-defined functions)
226 static LIST_HEAD(variable_list);
231 enum variable_flavor flavor;
232 struct list_head node;
235 static struct variable *variable_lookup(const char *name)
239 list_for_each_entry(v, &variable_list, node) {
240 if (!strcmp(name, v->name))
247 static char *variable_expand(const char *name, int argc, char *argv[])
252 v = variable_lookup(name);
256 if (v->flavor == VAR_RECURSIVE)
257 res = expand_string_with_args(v->value, argc, argv);
259 res = xstrdup(v->value);
264 void variable_add(const char *name, const char *value,
265 enum variable_flavor flavor)
271 v = variable_lookup(name);
273 /* For defined variables, += inherits the existing flavor */
274 if (flavor == VAR_APPEND) {
281 /* For undefined variables, += assumes the recursive flavor */
282 if (flavor == VAR_APPEND)
283 flavor = VAR_RECURSIVE;
285 v = xmalloc(sizeof(*v));
286 v->name = xstrdup(name);
287 list_add_tail(&v->node, &variable_list);
292 if (flavor == VAR_SIMPLE)
293 new_value = expand_string(value);
295 new_value = xstrdup(value);
298 v->value = xrealloc(v->value,
299 strlen(v->value) + strlen(new_value) + 2);
300 strcat(v->value, " ");
301 strcat(v->value, new_value);
304 v->value = new_value;
308 static void variable_del(struct variable *v)
316 void variable_all_del(void)
318 struct variable *v, *tmp;
320 list_for_each_entry_safe(v, tmp, &variable_list, node)
325 * Evaluate a clause with arguments. argc/argv are arguments from the upper
328 * Returned string must be freed when done
330 static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
332 char *tmp, *name, *res, *endptr, *prev, *p;
334 char *new_argv[FUNCTION_MAX_ARGS];
339 tmp = xstrndup(str, len);
342 * If variable name is '1', '2', etc. It is generally an argument
343 * from a user-function call (i.e. local-scope variable). If not
344 * available, then look-up global-scope variables.
346 n = strtoul(tmp, &endptr, 10);
347 if (!*endptr && n > 0 && n <= argc) {
348 res = xstrdup(argv[n - 1]);
356 * The function name and arguments are separated by a comma.
357 * For example, if the function call is like this:
360 * The input string for this helper should be:
364 * new_argv[0] = 'foo'
365 * new_argv[1] = '$(x)'
366 * new_argv[2] = '$(y)'
369 if (nest == 0 && *p == ',') {
371 if (new_argc >= FUNCTION_MAX_ARGS)
372 pperror("too many function arguments");
373 new_argv[new_argc++] = prev;
375 } else if (*p == '(') {
377 } else if (*p == ')') {
383 new_argv[new_argc++] = prev;
387 * new_argv[0] represents a function name or a variable name. Put it
388 * into 'name', then shift the rest of the arguments. This simplifies
391 name = expand_string_with_args(new_argv[0], argc, argv);
393 for (i = 0; i < new_argc; i++)
394 new_argv[i] = expand_string_with_args(new_argv[i + 1],
397 /* Search for variables */
398 res = variable_expand(name, new_argc, new_argv);
402 /* Look for built-in functions */
403 res = function_expand(name, new_argc, new_argv);
407 /* Last, try environment variable */
409 res = env_expand(name);
416 for (i = 0; i < new_argc; i++)
426 * Expand a string that follows '$'
428 * For example, if the input string is
429 * ($(FOO)$($(BAR)))$(BAZ)
430 * this helper evaluates
432 * and returns a new string containing the expansion (note that the string is
433 * recursively expanded), also advancing 'str' to point to the next character
434 * after the corresponding closing parenthesis, in this case, *str will be
437 static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
439 const char *p = *str;
444 * In Kconfig, variable/function references always start with "$(".
445 * Neither single-letter variables as in $A nor curly braces as in ${CC}
446 * are supported. '$' not followed by '(' loses its special meaning.
458 } else if (*q == ')') {
466 pperror("unterminated reference to '%s': missing ')'", p);
468 /* Advance 'str' to after the expanded initial portion of the string */
471 return eval_clause(p, q - p, argc, argv);
474 char *expand_dollar(const char **str)
476 return expand_dollar_with_args(str, 0, NULL);
479 static char *__expand_string(const char **str, bool (*is_end)(char c),
480 int argc, char *argv[])
483 char *expansion, *out;
484 size_t in_len, out_len;
496 expansion = expand_dollar_with_args(&p, argc, argv);
497 out_len += in_len + strlen(expansion);
498 out = xrealloc(out, out_len);
499 strncat(out, in, in_len);
500 strcat(out, expansion);
514 out = xrealloc(out, out_len);
515 strncat(out, in, in_len);
517 /* Advance 'str' to the end character */
523 static bool is_end_of_str(char c)
529 * Expand variables and functions in the given string. Undefined variables
530 * expand to an empty string.
531 * The returned string must be freed when done.
533 static char *expand_string_with_args(const char *in, int argc, char *argv[])
535 return __expand_string(&in, is_end_of_str, argc, argv);
538 char *expand_string(const char *in)
540 return expand_string_with_args(in, 0, NULL);
543 static bool is_end_of_token(char c)
545 /* Why are '.' and '/' valid characters for symbols? */
546 return !(isalnum(c) || c == '_' || c == '-' || c == '.' || c == '/');
550 * Expand variables in a token. The parsing stops when a token separater
551 * (in most cases, it is a whitespace) is encountered. 'str' is updated to
552 * point to the next character.
554 * The returned string must be freed when done.
556 char *expand_one_token(const char **str)
558 return __expand_string(str, is_end_of_token, 0, NULL);