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)
228 v = variable_lookup(name);
230 /* For defined variables, += inherits the existing flavor */
231 if (flavor == VAR_APPEND) {
238 /* For undefined variables, += assumes the recursive flavor */
239 if (flavor == VAR_APPEND)
240 flavor = VAR_RECURSIVE;
242 v = xmalloc(sizeof(*v));
243 v->name = xstrdup(name);
244 list_add_tail(&v->node, &variable_list);
249 if (flavor == VAR_SIMPLE)
250 new_value = expand_string(value);
252 new_value = xstrdup(value);
255 v->value = xrealloc(v->value,
256 strlen(v->value) + strlen(new_value) + 2);
257 strcat(v->value, " ");
258 strcat(v->value, new_value);
261 v->value = new_value;
265 static void variable_del(struct variable *v)
273 void variable_all_del(void)
275 struct variable *v, *tmp;
277 list_for_each_entry_safe(v, tmp, &variable_list, node)
282 * Evaluate a clause with arguments. argc/argv are arguments from the upper
285 * Returned string must be freed when done
287 static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
289 char *tmp, *name, *res, *endptr, *prev, *p;
291 char *new_argv[FUNCTION_MAX_ARGS];
296 tmp = xstrndup(str, len);
299 * If variable name is '1', '2', etc. It is generally an argument
300 * from a user-function call (i.e. local-scope variable). If not
301 * available, then look-up global-scope variables.
303 n = strtoul(tmp, &endptr, 10);
304 if (!*endptr && n > 0 && n <= argc) {
305 res = xstrdup(argv[n - 1]);
313 * The function name and arguments are separated by a comma.
314 * For example, if the function call is like this:
317 * The input string for this helper should be:
321 * new_argv[0] = 'foo'
322 * new_argv[1] = '$(x)'
323 * new_argv[2] = '$(y)'
326 if (nest == 0 && *p == ',') {
328 if (new_argc >= FUNCTION_MAX_ARGS)
329 pperror("too many function arguments");
330 new_argv[new_argc++] = prev;
332 } else if (*p == '(') {
334 } else if (*p == ')') {
340 new_argv[new_argc++] = prev;
344 * new_argv[0] represents a function name or a variable name. Put it
345 * into 'name', then shift the rest of the arguments. This simplifies
348 name = expand_string_with_args(new_argv[0], argc, argv);
350 for (i = 0; i < new_argc; i++)
351 new_argv[i] = expand_string_with_args(new_argv[i + 1],
354 /* Search for variables */
355 res = variable_expand(name, new_argc, new_argv);
359 /* Look for built-in functions */
360 res = function_expand(name, new_argc, new_argv);
364 /* Last, try environment variable */
366 res = env_expand(name);
373 for (i = 0; i < new_argc; i++)
383 * Expand a string that follows '$'
385 * For example, if the input string is
386 * ($(FOO)$($(BAR)))$(BAZ)
387 * this helper evaluates
389 * and returns a new string containing the expansion (note that the string is
390 * recursively expanded), also advancing 'str' to point to the next character
391 * after the corresponding closing parenthesis, in this case, *str will be
394 static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
396 const char *p = *str;
401 * In Kconfig, variable/function references always start with "$(".
402 * Neither single-letter variables as in $A nor curly braces as in ${CC}
403 * are supported. '$' not followed by '(' loses its special meaning.
415 } else if (*q == ')') {
423 pperror("unterminated reference to '%s': missing ')'", p);
425 /* Advance 'str' to after the expanded initial portion of the string */
428 return eval_clause(p, q - p, argc, argv);
431 char *expand_dollar(const char **str)
433 return expand_dollar_with_args(str, 0, NULL);
436 static char *__expand_string(const char **str, bool (*is_end)(char c),
437 int argc, char *argv[])
440 char *expansion, *out;
441 size_t in_len, out_len;
453 expansion = expand_dollar_with_args(&p, argc, argv);
454 out_len += in_len + strlen(expansion);
455 out = xrealloc(out, out_len);
456 strncat(out, in, in_len);
457 strcat(out, expansion);
471 out = xrealloc(out, out_len);
472 strncat(out, in, in_len);
474 /* Advance 'str' to the end character */
480 static bool is_end_of_str(char c)
486 * Expand variables and functions in the given string. Undefined variables
487 * expand to an empty string.
488 * The returned string must be freed when done.
490 static char *expand_string_with_args(const char *in, int argc, char *argv[])
492 return __expand_string(&in, is_end_of_str, argc, argv);
495 char *expand_string(const char *in)
497 return expand_string_with_args(in, 0, NULL);
500 static bool is_end_of_token(char c)
502 /* Why are '.' and '/' valid characters for symbols? */
503 return !(isalnum(c) || c == '_' || c == '-' || c == '.' || c == '/');
507 * Expand variables in a token. The parsing stops when a token separater
508 * (in most cases, it is a whitespace) is encountered. 'str' is updated to
509 * point to the next character.
511 * The returned string must be freed when done.
513 char *expand_one_token(const char **str)
515 return __expand_string(str, is_end_of_token, 0, NULL);