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 * Evaluate a clause with arguments. argc/argv are arguments from the upper
184 * Returned string must be freed when done
186 static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
188 char *tmp, *name, *res, *prev, *p;
190 char *new_argv[FUNCTION_MAX_ARGS];
194 tmp = xstrndup(str, len);
200 * The function name and arguments are separated by a comma.
201 * For example, if the function call is like this:
204 * The input string for this helper should be:
208 * new_argv[0] = 'foo'
209 * new_argv[1] = '$(x)'
210 * new_argv[2] = '$(y)'
213 if (nest == 0 && *p == ',') {
215 if (new_argc >= FUNCTION_MAX_ARGS)
216 pperror("too many function arguments");
217 new_argv[new_argc++] = prev;
219 } else if (*p == '(') {
221 } else if (*p == ')') {
227 new_argv[new_argc++] = prev;
231 * new_argv[0] represents a function name or a variable name. Put it
232 * into 'name', then shift the rest of the arguments. This simplifies
235 name = expand_string_with_args(new_argv[0], argc, argv);
237 for (i = 0; i < new_argc; i++)
238 new_argv[i] = expand_string_with_args(new_argv[i + 1],
241 /* Look for built-in functions */
242 res = function_expand(name, new_argc, new_argv);
246 /* Last, try environment variable */
248 res = env_expand(name);
255 for (i = 0; i < new_argc; i++)
264 * Expand a string that follows '$'
266 * For example, if the input string is
267 * ($(FOO)$($(BAR)))$(BAZ)
268 * this helper evaluates
270 * and returns a new string containing the expansion (note that the string is
271 * recursively expanded), also advancing 'str' to point to the next character
272 * after the corresponding closing parenthesis, in this case, *str will be
275 static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
277 const char *p = *str;
282 * In Kconfig, variable/function references always start with "$(".
283 * Neither single-letter variables as in $A nor curly braces as in ${CC}
284 * are supported. '$' not followed by '(' loses its special meaning.
296 } else if (*q == ')') {
304 pperror("unterminated reference to '%s': missing ')'", p);
306 /* Advance 'str' to after the expanded initial portion of the string */
309 return eval_clause(p, q - p, argc, argv);
312 char *expand_dollar(const char **str)
314 return expand_dollar_with_args(str, 0, NULL);
317 static char *__expand_string(const char **str, bool (*is_end)(char c),
318 int argc, char *argv[])
321 char *expansion, *out;
322 size_t in_len, out_len;
334 expansion = expand_dollar_with_args(&p, argc, argv);
335 out_len += in_len + strlen(expansion);
336 out = xrealloc(out, out_len);
337 strncat(out, in, in_len);
338 strcat(out, expansion);
352 out = xrealloc(out, out_len);
353 strncat(out, in, in_len);
355 /* Advance 'str' to the end character */
361 static bool is_end_of_str(char c)
367 * Expand variables and functions in the given string. Undefined variables
368 * expand to an empty string.
369 * The returned string must be freed when done.
371 static char *expand_string_with_args(const char *in, int argc, char *argv[])
373 return __expand_string(&in, is_end_of_str, argc, argv);
376 char *expand_string(const char *in)
378 return expand_string_with_args(in, 0, NULL);
381 static bool is_end_of_token(char c)
383 /* Why are '.' and '/' valid characters for symbols? */
384 return !(isalnum(c) || c == '_' || c == '-' || c == '.' || c == '/');
388 * Expand variables in a token. The parsing stops when a token separater
389 * (in most cases, it is a whitespace) is encountered. 'str' is updated to
390 * point to the next character.
392 * The returned string must be freed when done.
394 char *expand_one_token(const char **str)
396 return __expand_string(str, is_end_of_token, 0, NULL);