1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2018 Masahiro Yamada <yamada.masahiro@socionext.com>
12 #include <array_size.h>
17 #include "preprocess.h"
19 static char *expand_string_with_args(const char *in
, int argc
, char *argv
[]);
20 static char *expand_string(const char *in
);
22 static void __attribute__((noreturn
)) pperror(const char *format
, ...)
26 fprintf(stderr
, "%s:%d: ", cur_filename
, yylineno
);
28 vfprintf(stderr
, format
, ap
);
30 fprintf(stderr
, "\n");
36 * Environment variables
38 static LIST_HEAD(env_list
);
43 struct list_head node
;
46 static void env_add(const char *name
, const char *value
)
50 e
= xmalloc(sizeof(*e
));
51 e
->name
= xstrdup(name
);
52 e
->value
= xstrdup(value
);
54 list_add_tail(&e
->node
, &env_list
);
57 static void env_del(struct env
*e
)
65 /* The returned pointer must be freed when done */
66 static char *env_expand(const char *name
)
74 list_for_each_entry(e
, &env_list
, node
) {
75 if (!strcmp(name
, e
->name
))
76 return xstrdup(e
->value
);
84 * We need to remember all referenced environment variables.
85 * They will be written out to include/config/auto.conf.cmd
89 return xstrdup(value
);
92 void env_write_dep(struct gstr
*s
)
96 list_for_each_entry_safe(e
, tmp
, &env_list
, node
) {
99 "ifneq \"$(%s)\" \"%s\"\n"
100 "$(autoconfig): FORCE\n"
112 unsigned int min_args
;
113 unsigned int max_args
;
114 char *(*func
)(int argc
, char *argv
[]);
117 static char *do_error_if(int argc
, char *argv
[])
119 if (!strcmp(argv
[0], "y"))
120 pperror("%s", argv
[1]);
125 static char *do_filename(int argc
, char *argv
[])
127 return xstrdup(cur_filename
);
130 static char *do_info(int argc
, char *argv
[])
132 printf("%s\n", argv
[0]);
137 static char *do_lineno(int argc
, char *argv
[])
141 sprintf(buf
, "%d", yylineno
);
146 static char *do_shell(int argc
, char *argv
[])
162 nread
= fread(buf
, 1, sizeof(buf
), p
);
163 if (nread
== sizeof(buf
))
166 /* remove trailing new lines */
167 while (nread
> 0 && buf
[nread
- 1] == '\n')
172 /* replace a new line with a space */
173 for (i
= 0; i
< nread
; i
++) {
178 if (pclose(p
) == -1) {
186 static char *do_warning_if(int argc
, char *argv
[])
188 if (!strcmp(argv
[0], "y"))
189 fprintf(stderr
, "%s:%d: %s\n", cur_filename
, yylineno
, argv
[1]);
194 static const struct function function_table
[] = {
195 /* Name MIN MAX Function */
196 { "error-if", 2, 2, do_error_if
},
197 { "filename", 0, 0, do_filename
},
198 { "info", 1, 1, do_info
},
199 { "lineno", 0, 0, do_lineno
},
200 { "shell", 1, 1, do_shell
},
201 { "warning-if", 2, 2, do_warning_if
},
204 #define FUNCTION_MAX_ARGS 16
206 static char *function_expand(const char *name
, int argc
, char *argv
[])
208 const struct function
*f
;
211 for (i
= 0; i
< ARRAY_SIZE(function_table
); i
++) {
212 f
= &function_table
[i
];
213 if (strcmp(f
->name
, name
))
216 if (argc
< f
->min_args
)
217 pperror("too few function arguments passed to '%s'",
220 if (argc
> f
->max_args
)
221 pperror("too many function arguments passed to '%s'",
224 return f
->func(argc
, argv
);
231 * Variables (and user-defined functions)
233 static LIST_HEAD(variable_list
);
238 enum variable_flavor flavor
;
240 struct list_head node
;
243 static struct variable
*variable_lookup(const char *name
)
247 list_for_each_entry(v
, &variable_list
, node
) {
248 if (!strcmp(name
, v
->name
))
255 static char *variable_expand(const char *name
, int argc
, char *argv
[])
260 v
= variable_lookup(name
);
264 if (argc
== 0 && v
->exp_count
)
265 pperror("Recursive variable '%s' references itself (eventually)",
268 if (v
->exp_count
> 1000)
269 pperror("Too deep recursive expansion");
273 if (v
->flavor
== VAR_RECURSIVE
)
274 res
= expand_string_with_args(v
->value
, argc
, argv
);
276 res
= xstrdup(v
->value
);
283 void variable_add(const char *name
, const char *value
,
284 enum variable_flavor flavor
)
290 v
= variable_lookup(name
);
292 /* For defined variables, += inherits the existing flavor */
293 if (flavor
== VAR_APPEND
) {
300 /* For undefined variables, += assumes the recursive flavor */
301 if (flavor
== VAR_APPEND
)
302 flavor
= VAR_RECURSIVE
;
304 v
= xmalloc(sizeof(*v
));
305 v
->name
= xstrdup(name
);
307 list_add_tail(&v
->node
, &variable_list
);
312 if (flavor
== VAR_SIMPLE
)
313 new_value
= expand_string(value
);
315 new_value
= xstrdup(value
);
318 v
->value
= xrealloc(v
->value
,
319 strlen(v
->value
) + strlen(new_value
) + 2);
320 strcat(v
->value
, " ");
321 strcat(v
->value
, new_value
);
324 v
->value
= new_value
;
328 static void variable_del(struct variable
*v
)
336 void variable_all_del(void)
338 struct variable
*v
, *tmp
;
340 list_for_each_entry_safe(v
, tmp
, &variable_list
, node
)
345 * Evaluate a clause with arguments. argc/argv are arguments from the upper
348 * Returned string must be freed when done
350 static char *eval_clause(const char *str
, size_t len
, int argc
, char *argv
[])
352 char *tmp
, *name
, *res
, *endptr
, *prev
, *p
;
354 char *new_argv
[FUNCTION_MAX_ARGS
];
359 tmp
= xstrndup(str
, len
);
362 * If variable name is '1', '2', etc. It is generally an argument
363 * from a user-function call (i.e. local-scope variable). If not
364 * available, then look-up global-scope variables.
366 n
= strtoul(tmp
, &endptr
, 10);
367 if (!*endptr
&& n
> 0 && n
<= argc
) {
368 res
= xstrdup(argv
[n
- 1]);
376 * The function name and arguments are separated by a comma.
377 * For example, if the function call is like this:
380 * The input string for this helper should be:
384 * new_argv[0] = 'foo'
385 * new_argv[1] = '$(x)'
386 * new_argv[2] = '$(y)'
389 if (nest
== 0 && *p
== ',') {
391 if (new_argc
>= FUNCTION_MAX_ARGS
)
392 pperror("too many function arguments");
393 new_argv
[new_argc
++] = prev
;
395 } else if (*p
== '(') {
397 } else if (*p
== ')') {
404 if (new_argc
>= FUNCTION_MAX_ARGS
)
405 pperror("too many function arguments");
406 new_argv
[new_argc
++] = prev
;
410 * new_argv[0] represents a function name or a variable name. Put it
411 * into 'name', then shift the rest of the arguments. This simplifies
414 name
= expand_string_with_args(new_argv
[0], argc
, argv
);
416 for (i
= 0; i
< new_argc
; i
++)
417 new_argv
[i
] = expand_string_with_args(new_argv
[i
+ 1],
420 /* Search for variables */
421 res
= variable_expand(name
, new_argc
, new_argv
);
425 /* Look for built-in functions */
426 res
= function_expand(name
, new_argc
, new_argv
);
430 /* Last, try environment variable */
432 res
= env_expand(name
);
439 for (i
= 0; i
< new_argc
; i
++)
449 * Expand a string that follows '$'
451 * For example, if the input string is
452 * ($(FOO)$($(BAR)))$(BAZ)
453 * this helper evaluates
455 * and returns a new string containing the expansion (note that the string is
456 * recursively expanded), also advancing 'str' to point to the next character
457 * after the corresponding closing parenthesis, in this case, *str will be
460 static char *expand_dollar_with_args(const char **str
, int argc
, char *argv
[])
462 const char *p
= *str
;
467 * In Kconfig, variable/function references always start with "$(".
468 * Neither single-letter variables as in $A nor curly braces as in ${CC}
469 * are supported. '$' not followed by '(' loses its special meaning.
481 } else if (*q
== ')') {
489 pperror("unterminated reference to '%s': missing ')'", p
);
491 /* Advance 'str' to after the expanded initial portion of the string */
494 return eval_clause(p
, q
- p
, argc
, argv
);
497 char *expand_dollar(const char **str
)
499 return expand_dollar_with_args(str
, 0, NULL
);
502 static char *__expand_string(const char **str
, bool (*is_end
)(char c
),
503 int argc
, char *argv
[])
506 char *expansion
, *out
;
507 size_t in_len
, out_len
;
519 expansion
= expand_dollar_with_args(&p
, argc
, argv
);
520 out_len
+= in_len
+ strlen(expansion
);
521 out
= xrealloc(out
, out_len
);
522 strncat(out
, in
, in_len
);
523 strcat(out
, expansion
);
537 out
= xrealloc(out
, out_len
);
538 strncat(out
, in
, in_len
);
540 /* Advance 'str' to the end character */
546 static bool is_end_of_str(char c
)
552 * Expand variables and functions in the given string. Undefined variables
553 * expand to an empty string.
554 * The returned string must be freed when done.
556 static char *expand_string_with_args(const char *in
, int argc
, char *argv
[])
558 return __expand_string(&in
, is_end_of_str
, argc
, argv
);
561 static char *expand_string(const char *in
)
563 return expand_string_with_args(in
, 0, NULL
);
566 static bool is_end_of_token(char c
)
568 return !(isalnum(c
) || c
== '_' || c
== '-');
572 * Expand variables in a token. The parsing stops when a token separater
573 * (in most cases, it is a whitespace) is encountered. 'str' is updated to
574 * point to the next character.
576 * The returned string must be freed when done.
578 char *expand_one_token(const char **str
)
580 return __expand_string(str
, is_end_of_token
, 0, NULL
);