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 const struct function function_table[] = {
110 /* Name MIN MAX Function */
113 #define FUNCTION_MAX_ARGS 16
115 static char *function_expand(const char *name, int argc, char *argv[])
117 const struct function *f;
120 for (i = 0; i < ARRAY_SIZE(function_table); i++) {
121 f = &function_table[i];
122 if (strcmp(f->name, name))
125 if (argc < f->min_args)
126 pperror("too few function arguments passed to '%s'",
129 if (argc > f->max_args)
130 pperror("too many function arguments passed to '%s'",
133 return f->func(argc, argv);
140 * Evaluate a clause with arguments. argc/argv are arguments from the upper
143 * Returned string must be freed when done
145 static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
147 char *tmp, *name, *res, *prev, *p;
149 char *new_argv[FUNCTION_MAX_ARGS];
153 tmp = xstrndup(str, len);
159 * The function name and arguments are separated by a comma.
160 * For example, if the function call is like this:
163 * The input string for this helper should be:
167 * new_argv[0] = 'foo'
168 * new_argv[1] = '$(x)'
169 * new_argv[2] = '$(y)'
172 if (nest == 0 && *p == ',') {
174 if (new_argc >= FUNCTION_MAX_ARGS)
175 pperror("too many function arguments");
176 new_argv[new_argc++] = prev;
178 } else if (*p == '(') {
180 } else if (*p == ')') {
186 new_argv[new_argc++] = prev;
190 * new_argv[0] represents a function name or a variable name. Put it
191 * into 'name', then shift the rest of the arguments. This simplifies
194 name = expand_string_with_args(new_argv[0], argc, argv);
196 for (i = 0; i < new_argc; i++)
197 new_argv[i] = expand_string_with_args(new_argv[i + 1],
200 /* Look for built-in functions */
201 res = function_expand(name, new_argc, new_argv);
205 /* Last, try environment variable */
207 res = env_expand(name);
214 for (i = 0; i < new_argc; i++)
223 * Expand a string that follows '$'
225 * For example, if the input string is
226 * ($(FOO)$($(BAR)))$(BAZ)
227 * this helper evaluates
229 * and returns a new string containing the expansion (note that the string is
230 * recursively expanded), also advancing 'str' to point to the next character
231 * after the corresponding closing parenthesis, in this case, *str will be
234 static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
236 const char *p = *str;
241 * In Kconfig, variable/function references always start with "$(".
242 * Neither single-letter variables as in $A nor curly braces as in ${CC}
243 * are supported. '$' not followed by '(' loses its special meaning.
255 } else if (*q == ')') {
263 pperror("unterminated reference to '%s': missing ')'", p);
265 /* Advance 'str' to after the expanded initial portion of the string */
268 return eval_clause(p, q - p, argc, argv);
271 char *expand_dollar(const char **str)
273 return expand_dollar_with_args(str, 0, NULL);
276 static char *__expand_string(const char **str, bool (*is_end)(char c),
277 int argc, char *argv[])
280 char *expansion, *out;
281 size_t in_len, out_len;
293 expansion = expand_dollar_with_args(&p, argc, argv);
294 out_len += in_len + strlen(expansion);
295 out = xrealloc(out, out_len);
296 strncat(out, in, in_len);
297 strcat(out, expansion);
311 out = xrealloc(out, out_len);
312 strncat(out, in, in_len);
314 /* Advance 'str' to the end character */
320 static bool is_end_of_str(char c)
326 * Expand variables and functions in the given string. Undefined variables
327 * expand to an empty string.
328 * The returned string must be freed when done.
330 static char *expand_string_with_args(const char *in, int argc, char *argv[])
332 return __expand_string(&in, is_end_of_str, argc, argv);
335 char *expand_string(const char *in)
337 return expand_string_with_args(in, 0, NULL);
340 static bool is_end_of_token(char c)
342 /* Why are '.' and '/' valid characters for symbols? */
343 return !(isalnum(c) || c == '_' || c == '-' || c == '.' || c == '/');
347 * Expand variables in a token. The parsing stops when a token separater
348 * (in most cases, it is a whitespace) is encountered. 'str' is updated to
349 * point to the next character.
351 * The returned string must be freed when done.
353 char *expand_one_token(const char **str)
355 return __expand_string(str, is_end_of_token, 0, NULL);