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_info(int argc, char *argv[])
119 printf("%s\n", argv[0]);
124 static char *do_shell(int argc, char *argv[])
140 nread = fread(buf, 1, sizeof(buf), p);
141 if (nread == sizeof(buf))
144 /* remove trailing new lines */
145 while (buf[nread - 1] == '\n')
150 /* replace a new line with a space */
151 for (i = 0; i < nread; i++) {
156 if (pclose(p) == -1) {
164 static char *do_warning_if(int argc, char *argv[])
166 if (!strcmp(argv[0], "y"))
167 fprintf(stderr, "%s:%d: %s\n",
168 current_file->name, yylineno, argv[1]);
173 static const struct function function_table[] = {
174 /* Name MIN MAX Function */
175 { "error-if", 2, 2, do_error_if },
176 { "info", 1, 1, do_info },
177 { "shell", 1, 1, do_shell },
178 { "warning-if", 2, 2, do_warning_if },
181 #define FUNCTION_MAX_ARGS 16
183 static char *function_expand(const char *name, int argc, char *argv[])
185 const struct function *f;
188 for (i = 0; i < ARRAY_SIZE(function_table); i++) {
189 f = &function_table[i];
190 if (strcmp(f->name, name))
193 if (argc < f->min_args)
194 pperror("too few function arguments passed to '%s'",
197 if (argc > f->max_args)
198 pperror("too many function arguments passed to '%s'",
201 return f->func(argc, argv);
208 * Variables (and user-defined functions)
210 static LIST_HEAD(variable_list);
215 enum variable_flavor flavor;
216 struct list_head node;
219 static struct variable *variable_lookup(const char *name)
223 list_for_each_entry(v, &variable_list, node) {
224 if (!strcmp(name, v->name))
231 static char *variable_expand(const char *name, int argc, char *argv[])
236 v = variable_lookup(name);
240 if (v->flavor == VAR_RECURSIVE)
241 res = expand_string_with_args(v->value, argc, argv);
243 res = xstrdup(v->value);
248 void variable_add(const char *name, const char *value,
249 enum variable_flavor flavor)
255 v = variable_lookup(name);
257 /* For defined variables, += inherits the existing flavor */
258 if (flavor == VAR_APPEND) {
265 /* For undefined variables, += assumes the recursive flavor */
266 if (flavor == VAR_APPEND)
267 flavor = VAR_RECURSIVE;
269 v = xmalloc(sizeof(*v));
270 v->name = xstrdup(name);
271 list_add_tail(&v->node, &variable_list);
276 if (flavor == VAR_SIMPLE)
277 new_value = expand_string(value);
279 new_value = xstrdup(value);
282 v->value = xrealloc(v->value,
283 strlen(v->value) + strlen(new_value) + 2);
284 strcat(v->value, " ");
285 strcat(v->value, new_value);
288 v->value = new_value;
292 static void variable_del(struct variable *v)
300 void variable_all_del(void)
302 struct variable *v, *tmp;
304 list_for_each_entry_safe(v, tmp, &variable_list, node)
309 * Evaluate a clause with arguments. argc/argv are arguments from the upper
312 * Returned string must be freed when done
314 static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
316 char *tmp, *name, *res, *endptr, *prev, *p;
318 char *new_argv[FUNCTION_MAX_ARGS];
323 tmp = xstrndup(str, len);
326 * If variable name is '1', '2', etc. It is generally an argument
327 * from a user-function call (i.e. local-scope variable). If not
328 * available, then look-up global-scope variables.
330 n = strtoul(tmp, &endptr, 10);
331 if (!*endptr && n > 0 && n <= argc) {
332 res = xstrdup(argv[n - 1]);
340 * The function name and arguments are separated by a comma.
341 * For example, if the function call is like this:
344 * The input string for this helper should be:
348 * new_argv[0] = 'foo'
349 * new_argv[1] = '$(x)'
350 * new_argv[2] = '$(y)'
353 if (nest == 0 && *p == ',') {
355 if (new_argc >= FUNCTION_MAX_ARGS)
356 pperror("too many function arguments");
357 new_argv[new_argc++] = prev;
359 } else if (*p == '(') {
361 } else if (*p == ')') {
367 new_argv[new_argc++] = prev;
371 * new_argv[0] represents a function name or a variable name. Put it
372 * into 'name', then shift the rest of the arguments. This simplifies
375 name = expand_string_with_args(new_argv[0], argc, argv);
377 for (i = 0; i < new_argc; i++)
378 new_argv[i] = expand_string_with_args(new_argv[i + 1],
381 /* Search for variables */
382 res = variable_expand(name, new_argc, new_argv);
386 /* Look for built-in functions */
387 res = function_expand(name, new_argc, new_argv);
391 /* Last, try environment variable */
393 res = env_expand(name);
400 for (i = 0; i < new_argc; i++)
410 * Expand a string that follows '$'
412 * For example, if the input string is
413 * ($(FOO)$($(BAR)))$(BAZ)
414 * this helper evaluates
416 * and returns a new string containing the expansion (note that the string is
417 * recursively expanded), also advancing 'str' to point to the next character
418 * after the corresponding closing parenthesis, in this case, *str will be
421 static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
423 const char *p = *str;
428 * In Kconfig, variable/function references always start with "$(".
429 * Neither single-letter variables as in $A nor curly braces as in ${CC}
430 * are supported. '$' not followed by '(' loses its special meaning.
442 } else if (*q == ')') {
450 pperror("unterminated reference to '%s': missing ')'", p);
452 /* Advance 'str' to after the expanded initial portion of the string */
455 return eval_clause(p, q - p, argc, argv);
458 char *expand_dollar(const char **str)
460 return expand_dollar_with_args(str, 0, NULL);
463 static char *__expand_string(const char **str, bool (*is_end)(char c),
464 int argc, char *argv[])
467 char *expansion, *out;
468 size_t in_len, out_len;
480 expansion = expand_dollar_with_args(&p, argc, argv);
481 out_len += in_len + strlen(expansion);
482 out = xrealloc(out, out_len);
483 strncat(out, in, in_len);
484 strcat(out, expansion);
498 out = xrealloc(out, out_len);
499 strncat(out, in, in_len);
501 /* Advance 'str' to the end character */
507 static bool is_end_of_str(char c)
513 * Expand variables and functions in the given string. Undefined variables
514 * expand to an empty string.
515 * The returned string must be freed when done.
517 static char *expand_string_with_args(const char *in, int argc, char *argv[])
519 return __expand_string(&in, is_end_of_str, argc, argv);
522 char *expand_string(const char *in)
524 return expand_string_with_args(in, 0, NULL);
527 static bool is_end_of_token(char c)
529 /* Why are '.' and '/' valid characters for symbols? */
530 return !(isalnum(c) || c == '_' || c == '-' || c == '.' || c == '/');
534 * Expand variables in a token. The parsing stops when a token separater
535 * (in most cases, it is a whitespace) is encountered. 'str' is updated to
536 * point to the next character.
538 * The returned string must be freed when done.
540 char *expand_one_token(const char **str)
542 return __expand_string(str, is_end_of_token, 0, NULL);