staging: erofs: support IO read error injection
[linux/fpc-iii.git] / scripts / kconfig / preprocess.c
blob592dfbfa9fb30d6804043a5bad23b9f683adefd8
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (C) 2018 Masahiro Yamada <yamada.masahiro@socionext.com>
5 #include <ctype.h>
6 #include <stdarg.h>
7 #include <stdbool.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
12 #include "list.h"
13 #include "lkc.h"
15 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
17 static char *expand_string_with_args(const char *in, int argc, char *argv[]);
19 static void __attribute__((noreturn)) pperror(const char *format, ...)
21 va_list ap;
23 fprintf(stderr, "%s:%d: ", current_file->name, yylineno);
24 va_start(ap, format);
25 vfprintf(stderr, format, ap);
26 va_end(ap);
27 fprintf(stderr, "\n");
29 exit(1);
33 * Environment variables
35 static LIST_HEAD(env_list);
37 struct env {
38 char *name;
39 char *value;
40 struct list_head node;
43 static void env_add(const char *name, const char *value)
45 struct env *e;
47 e = xmalloc(sizeof(*e));
48 e->name = xstrdup(name);
49 e->value = xstrdup(value);
51 list_add_tail(&e->node, &env_list);
54 static void env_del(struct env *e)
56 list_del(&e->node);
57 free(e->name);
58 free(e->value);
59 free(e);
62 /* The returned pointer must be freed when done */
63 static char *env_expand(const char *name)
65 struct env *e;
66 const char *value;
68 if (!*name)
69 return NULL;
71 list_for_each_entry(e, &env_list, node) {
72 if (!strcmp(name, e->name))
73 return xstrdup(e->value);
76 value = getenv(name);
77 if (!value)
78 return NULL;
81 * We need to remember all referenced environment variables.
82 * They will be written out to include/config/auto.conf.cmd
84 env_add(name, value);
86 return xstrdup(value);
89 void env_write_dep(FILE *f, const char *autoconfig_name)
91 struct env *e, *tmp;
93 list_for_each_entry_safe(e, tmp, &env_list, node) {
94 fprintf(f, "ifneq \"$(%s)\" \"%s\"\n", e->name, e->value);
95 fprintf(f, "%s: FORCE\n", autoconfig_name);
96 fprintf(f, "endif\n");
97 env_del(e);
102 * Built-in functions
104 struct function {
105 const char *name;
106 unsigned int min_args;
107 unsigned int max_args;
108 char *(*func)(int argc, char *argv[]);
111 static char *do_error_if(int argc, char *argv[])
113 if (!strcmp(argv[0], "y"))
114 pperror("%s", argv[1]);
116 return NULL;
119 static char *do_filename(int argc, char *argv[])
121 return xstrdup(current_file->name);
124 static char *do_info(int argc, char *argv[])
126 printf("%s\n", argv[0]);
128 return xstrdup("");
131 static char *do_lineno(int argc, char *argv[])
133 char buf[16];
135 sprintf(buf, "%d", yylineno);
137 return xstrdup(buf);
140 static char *do_shell(int argc, char *argv[])
142 FILE *p;
143 char buf[256];
144 char *cmd;
145 size_t nread;
146 int i;
148 cmd = argv[0];
150 p = popen(cmd, "r");
151 if (!p) {
152 perror(cmd);
153 exit(1);
156 nread = fread(buf, 1, sizeof(buf), p);
157 if (nread == sizeof(buf))
158 nread--;
160 /* remove trailing new lines */
161 while (nread > 0 && buf[nread - 1] == '\n')
162 nread--;
164 buf[nread] = 0;
166 /* replace a new line with a space */
167 for (i = 0; i < nread; i++) {
168 if (buf[i] == '\n')
169 buf[i] = ' ';
172 if (pclose(p) == -1) {
173 perror(cmd);
174 exit(1);
177 return xstrdup(buf);
180 static char *do_warning_if(int argc, char *argv[])
182 if (!strcmp(argv[0], "y"))
183 fprintf(stderr, "%s:%d: %s\n",
184 current_file->name, yylineno, argv[1]);
186 return xstrdup("");
189 static const struct function function_table[] = {
190 /* Name MIN MAX Function */
191 { "error-if", 2, 2, do_error_if },
192 { "filename", 0, 0, do_filename },
193 { "info", 1, 1, do_info },
194 { "lineno", 0, 0, do_lineno },
195 { "shell", 1, 1, do_shell },
196 { "warning-if", 2, 2, do_warning_if },
199 #define FUNCTION_MAX_ARGS 16
201 static char *function_expand(const char *name, int argc, char *argv[])
203 const struct function *f;
204 int i;
206 for (i = 0; i < ARRAY_SIZE(function_table); i++) {
207 f = &function_table[i];
208 if (strcmp(f->name, name))
209 continue;
211 if (argc < f->min_args)
212 pperror("too few function arguments passed to '%s'",
213 name);
215 if (argc > f->max_args)
216 pperror("too many function arguments passed to '%s'",
217 name);
219 return f->func(argc, argv);
222 return NULL;
226 * Variables (and user-defined functions)
228 static LIST_HEAD(variable_list);
230 struct variable {
231 char *name;
232 char *value;
233 enum variable_flavor flavor;
234 int exp_count;
235 struct list_head node;
238 static struct variable *variable_lookup(const char *name)
240 struct variable *v;
242 list_for_each_entry(v, &variable_list, node) {
243 if (!strcmp(name, v->name))
244 return v;
247 return NULL;
250 static char *variable_expand(const char *name, int argc, char *argv[])
252 struct variable *v;
253 char *res;
255 v = variable_lookup(name);
256 if (!v)
257 return NULL;
259 if (argc == 0 && v->exp_count)
260 pperror("Recursive variable '%s' references itself (eventually)",
261 name);
263 if (v->exp_count > 1000)
264 pperror("Too deep recursive expansion");
266 v->exp_count++;
268 if (v->flavor == VAR_RECURSIVE)
269 res = expand_string_with_args(v->value, argc, argv);
270 else
271 res = xstrdup(v->value);
273 v->exp_count--;
275 return res;
278 void variable_add(const char *name, const char *value,
279 enum variable_flavor flavor)
281 struct variable *v;
282 char *new_value;
283 bool append = false;
285 v = variable_lookup(name);
286 if (v) {
287 /* For defined variables, += inherits the existing flavor */
288 if (flavor == VAR_APPEND) {
289 flavor = v->flavor;
290 append = true;
291 } else {
292 free(v->value);
294 } else {
295 /* For undefined variables, += assumes the recursive flavor */
296 if (flavor == VAR_APPEND)
297 flavor = VAR_RECURSIVE;
299 v = xmalloc(sizeof(*v));
300 v->name = xstrdup(name);
301 v->exp_count = 0;
302 list_add_tail(&v->node, &variable_list);
305 v->flavor = flavor;
307 if (flavor == VAR_SIMPLE)
308 new_value = expand_string(value);
309 else
310 new_value = xstrdup(value);
312 if (append) {
313 v->value = xrealloc(v->value,
314 strlen(v->value) + strlen(new_value) + 2);
315 strcat(v->value, " ");
316 strcat(v->value, new_value);
317 free(new_value);
318 } else {
319 v->value = new_value;
323 static void variable_del(struct variable *v)
325 list_del(&v->node);
326 free(v->name);
327 free(v->value);
328 free(v);
331 void variable_all_del(void)
333 struct variable *v, *tmp;
335 list_for_each_entry_safe(v, tmp, &variable_list, node)
336 variable_del(v);
340 * Evaluate a clause with arguments. argc/argv are arguments from the upper
341 * function call.
343 * Returned string must be freed when done
345 static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
347 char *tmp, *name, *res, *endptr, *prev, *p;
348 int new_argc = 0;
349 char *new_argv[FUNCTION_MAX_ARGS];
350 int nest = 0;
351 int i;
352 unsigned long n;
354 tmp = xstrndup(str, len);
357 * If variable name is '1', '2', etc. It is generally an argument
358 * from a user-function call (i.e. local-scope variable). If not
359 * available, then look-up global-scope variables.
361 n = strtoul(tmp, &endptr, 10);
362 if (!*endptr && n > 0 && n <= argc) {
363 res = xstrdup(argv[n - 1]);
364 goto free_tmp;
367 prev = p = tmp;
370 * Split into tokens
371 * The function name and arguments are separated by a comma.
372 * For example, if the function call is like this:
373 * $(foo,$(x),$(y))
375 * The input string for this helper should be:
376 * foo,$(x),$(y)
378 * and split into:
379 * new_argv[0] = 'foo'
380 * new_argv[1] = '$(x)'
381 * new_argv[2] = '$(y)'
383 while (*p) {
384 if (nest == 0 && *p == ',') {
385 *p = 0;
386 if (new_argc >= FUNCTION_MAX_ARGS)
387 pperror("too many function arguments");
388 new_argv[new_argc++] = prev;
389 prev = p + 1;
390 } else if (*p == '(') {
391 nest++;
392 } else if (*p == ')') {
393 nest--;
396 p++;
398 new_argv[new_argc++] = prev;
401 * Shift arguments
402 * new_argv[0] represents a function name or a variable name. Put it
403 * into 'name', then shift the rest of the arguments. This simplifies
404 * 'const' handling.
406 name = expand_string_with_args(new_argv[0], argc, argv);
407 new_argc--;
408 for (i = 0; i < new_argc; i++)
409 new_argv[i] = expand_string_with_args(new_argv[i + 1],
410 argc, argv);
412 /* Search for variables */
413 res = variable_expand(name, new_argc, new_argv);
414 if (res)
415 goto free;
417 /* Look for built-in functions */
418 res = function_expand(name, new_argc, new_argv);
419 if (res)
420 goto free;
422 /* Last, try environment variable */
423 if (new_argc == 0) {
424 res = env_expand(name);
425 if (res)
426 goto free;
429 res = xstrdup("");
430 free:
431 for (i = 0; i < new_argc; i++)
432 free(new_argv[i]);
433 free(name);
434 free_tmp:
435 free(tmp);
437 return res;
441 * Expand a string that follows '$'
443 * For example, if the input string is
444 * ($(FOO)$($(BAR)))$(BAZ)
445 * this helper evaluates
446 * $($(FOO)$($(BAR)))
447 * and returns a new string containing the expansion (note that the string is
448 * recursively expanded), also advancing 'str' to point to the next character
449 * after the corresponding closing parenthesis, in this case, *str will be
450 * $(BAR)
452 static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
454 const char *p = *str;
455 const char *q;
456 int nest = 0;
459 * In Kconfig, variable/function references always start with "$(".
460 * Neither single-letter variables as in $A nor curly braces as in ${CC}
461 * are supported. '$' not followed by '(' loses its special meaning.
463 if (*p != '(') {
464 *str = p;
465 return xstrdup("$");
468 p++;
469 q = p;
470 while (*q) {
471 if (*q == '(') {
472 nest++;
473 } else if (*q == ')') {
474 if (nest-- == 0)
475 break;
477 q++;
480 if (!*q)
481 pperror("unterminated reference to '%s': missing ')'", p);
483 /* Advance 'str' to after the expanded initial portion of the string */
484 *str = q + 1;
486 return eval_clause(p, q - p, argc, argv);
489 char *expand_dollar(const char **str)
491 return expand_dollar_with_args(str, 0, NULL);
494 static char *__expand_string(const char **str, bool (*is_end)(char c),
495 int argc, char *argv[])
497 const char *in, *p;
498 char *expansion, *out;
499 size_t in_len, out_len;
501 out = xmalloc(1);
502 *out = 0;
503 out_len = 1;
505 p = in = *str;
507 while (1) {
508 if (*p == '$') {
509 in_len = p - in;
510 p++;
511 expansion = expand_dollar_with_args(&p, argc, argv);
512 out_len += in_len + strlen(expansion);
513 out = xrealloc(out, out_len);
514 strncat(out, in, in_len);
515 strcat(out, expansion);
516 free(expansion);
517 in = p;
518 continue;
521 if (is_end(*p))
522 break;
524 p++;
527 in_len = p - in;
528 out_len += in_len;
529 out = xrealloc(out, out_len);
530 strncat(out, in, in_len);
532 /* Advance 'str' to the end character */
533 *str = p;
535 return out;
538 static bool is_end_of_str(char c)
540 return !c;
544 * Expand variables and functions in the given string. Undefined variables
545 * expand to an empty string.
546 * The returned string must be freed when done.
548 static char *expand_string_with_args(const char *in, int argc, char *argv[])
550 return __expand_string(&in, is_end_of_str, argc, argv);
553 char *expand_string(const char *in)
555 return expand_string_with_args(in, 0, NULL);
558 static bool is_end_of_token(char c)
560 return !(isalnum(c) || c == '_' || c == '-');
564 * Expand variables in a token. The parsing stops when a token separater
565 * (in most cases, it is a whitespace) is encountered. 'str' is updated to
566 * point to the next character.
568 * The returned string must be freed when done.
570 char *expand_one_token(const char **str)
572 return __expand_string(str, is_end_of_token, 0, NULL);