blob: 592dfbfa9fb30d6804043a5bad23b9f683adefd8 [file] [log] [blame]
Masahiro Yamada104daea2018-05-28 18:21:40 +09001// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (C) 2018 Masahiro Yamada <yamada.masahiro@socionext.com>
4
Masahiro Yamada558e78e2018-12-21 17:33:04 +09005#include <ctype.h>
Masahiro Yamada104daea2018-05-28 18:21:40 +09006#include <stdarg.h>
7#include <stdbool.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include "list.h"
Masahiro Yamada558e78e2018-12-21 17:33:04 +090013#include "lkc.h"
Masahiro Yamada104daea2018-05-28 18:21:40 +090014
Masahiro Yamadae298f3b2018-05-28 18:21:45 +090015#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
16
17static char *expand_string_with_args(const char *in, int argc, char *argv[]);
18
Masahiro Yamada104daea2018-05-28 18:21:40 +090019static void __attribute__((noreturn)) pperror(const char *format, ...)
20{
21 va_list ap;
22
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");
28
29 exit(1);
30}
31
32/*
33 * Environment variables
34 */
35static LIST_HEAD(env_list);
36
37struct env {
38 char *name;
39 char *value;
40 struct list_head node;
41};
42
43static void env_add(const char *name, const char *value)
44{
45 struct env *e;
46
47 e = xmalloc(sizeof(*e));
48 e->name = xstrdup(name);
49 e->value = xstrdup(value);
50
51 list_add_tail(&e->node, &env_list);
52}
53
54static void env_del(struct env *e)
55{
56 list_del(&e->node);
57 free(e->name);
58 free(e->value);
59 free(e);
60}
61
62/* The returned pointer must be freed when done */
63static char *env_expand(const char *name)
64{
65 struct env *e;
66 const char *value;
67
68 if (!*name)
69 return NULL;
70
71 list_for_each_entry(e, &env_list, node) {
72 if (!strcmp(name, e->name))
73 return xstrdup(e->value);
74 }
75
76 value = getenv(name);
77 if (!value)
78 return NULL;
79
80 /*
81 * We need to remember all referenced environment variables.
82 * They will be written out to include/config/auto.conf.cmd
83 */
84 env_add(name, value);
85
86 return xstrdup(value);
87}
88
89void env_write_dep(FILE *f, const char *autoconfig_name)
90{
91 struct env *e, *tmp;
92
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);
98 }
99}
100
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900101/*
102 * Built-in functions
103 */
104struct function {
105 const char *name;
106 unsigned int min_args;
107 unsigned int max_args;
108 char *(*func)(int argc, char *argv[]);
109};
110
Masahiro Yamada1d6272e2018-05-28 18:21:53 +0900111static char *do_error_if(int argc, char *argv[])
112{
113 if (!strcmp(argv[0], "y"))
114 pperror("%s", argv[1]);
115
116 return NULL;
117}
118
Masahiro Yamadaa702a612018-05-28 18:21:54 +0900119static char *do_filename(int argc, char *argv[])
120{
121 return xstrdup(current_file->name);
122}
123
Masahiro Yamada1d6272e2018-05-28 18:21:53 +0900124static char *do_info(int argc, char *argv[])
125{
126 printf("%s\n", argv[0]);
127
128 return xstrdup("");
129}
130
Masahiro Yamadaa702a612018-05-28 18:21:54 +0900131static char *do_lineno(int argc, char *argv[])
132{
133 char buf[16];
134
135 sprintf(buf, "%d", yylineno);
136
137 return xstrdup(buf);
138}
139
Masahiro Yamada2fd5b092018-05-28 18:21:46 +0900140static char *do_shell(int argc, char *argv[])
141{
142 FILE *p;
143 char buf[256];
144 char *cmd;
145 size_t nread;
146 int i;
147
148 cmd = argv[0];
149
150 p = popen(cmd, "r");
151 if (!p) {
152 perror(cmd);
153 exit(1);
154 }
155
156 nread = fread(buf, 1, sizeof(buf), p);
157 if (nread == sizeof(buf))
158 nread--;
159
160 /* remove trailing new lines */
Jerry James73d1c582018-06-23 22:49:04 +0200161 while (nread > 0 && buf[nread - 1] == '\n')
Masahiro Yamada2fd5b092018-05-28 18:21:46 +0900162 nread--;
163
164 buf[nread] = 0;
165
166 /* replace a new line with a space */
167 for (i = 0; i < nread; i++) {
168 if (buf[i] == '\n')
169 buf[i] = ' ';
170 }
171
172 if (pclose(p) == -1) {
173 perror(cmd);
174 exit(1);
175 }
176
177 return xstrdup(buf);
178}
179
Masahiro Yamada1d6272e2018-05-28 18:21:53 +0900180static char *do_warning_if(int argc, char *argv[])
181{
182 if (!strcmp(argv[0], "y"))
183 fprintf(stderr, "%s:%d: %s\n",
184 current_file->name, yylineno, argv[1]);
185
186 return xstrdup("");
187}
188
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900189static const struct function function_table[] = {
190 /* Name MIN MAX Function */
Masahiro Yamada1d6272e2018-05-28 18:21:53 +0900191 { "error-if", 2, 2, do_error_if },
Masahiro Yamadaa702a612018-05-28 18:21:54 +0900192 { "filename", 0, 0, do_filename },
Masahiro Yamada1d6272e2018-05-28 18:21:53 +0900193 { "info", 1, 1, do_info },
Masahiro Yamadaa702a612018-05-28 18:21:54 +0900194 { "lineno", 0, 0, do_lineno },
Masahiro Yamada2fd5b092018-05-28 18:21:46 +0900195 { "shell", 1, 1, do_shell },
Masahiro Yamada1d6272e2018-05-28 18:21:53 +0900196 { "warning-if", 2, 2, do_warning_if },
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900197};
198
199#define FUNCTION_MAX_ARGS 16
200
201static char *function_expand(const char *name, int argc, char *argv[])
Masahiro Yamada104daea2018-05-28 18:21:40 +0900202{
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900203 const struct function *f;
204 int i;
205
206 for (i = 0; i < ARRAY_SIZE(function_table); i++) {
207 f = &function_table[i];
208 if (strcmp(f->name, name))
209 continue;
210
211 if (argc < f->min_args)
212 pperror("too few function arguments passed to '%s'",
213 name);
214
215 if (argc > f->max_args)
216 pperror("too many function arguments passed to '%s'",
217 name);
218
219 return f->func(argc, argv);
220 }
221
222 return NULL;
223}
224
225/*
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900226 * Variables (and user-defined functions)
227 */
228static LIST_HEAD(variable_list);
229
230struct variable {
231 char *name;
232 char *value;
Masahiro Yamada1175c022018-05-28 18:21:50 +0900233 enum variable_flavor flavor;
Masahiro Yamada915f6492018-05-28 18:21:55 +0900234 int exp_count;
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900235 struct list_head node;
236};
237
238static struct variable *variable_lookup(const char *name)
239{
240 struct variable *v;
241
242 list_for_each_entry(v, &variable_list, node) {
243 if (!strcmp(name, v->name))
244 return v;
245 }
246
247 return NULL;
248}
249
250static char *variable_expand(const char *name, int argc, char *argv[])
251{
252 struct variable *v;
Masahiro Yamada1175c022018-05-28 18:21:50 +0900253 char *res;
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900254
255 v = variable_lookup(name);
256 if (!v)
257 return NULL;
258
Masahiro Yamada915f6492018-05-28 18:21:55 +0900259 if (argc == 0 && v->exp_count)
260 pperror("Recursive variable '%s' references itself (eventually)",
261 name);
262
263 if (v->exp_count > 1000)
264 pperror("Too deep recursive expansion");
265
266 v->exp_count++;
267
Masahiro Yamada1175c022018-05-28 18:21:50 +0900268 if (v->flavor == VAR_RECURSIVE)
269 res = expand_string_with_args(v->value, argc, argv);
270 else
271 res = xstrdup(v->value);
272
Masahiro Yamada915f6492018-05-28 18:21:55 +0900273 v->exp_count--;
274
Masahiro Yamada1175c022018-05-28 18:21:50 +0900275 return res;
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900276}
277
Masahiro Yamada1175c022018-05-28 18:21:50 +0900278void variable_add(const char *name, const char *value,
279 enum variable_flavor flavor)
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900280{
281 struct variable *v;
Masahiro Yamadaed2a22f2018-05-28 18:21:51 +0900282 char *new_value;
283 bool append = false;
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900284
285 v = variable_lookup(name);
286 if (v) {
Masahiro Yamadaed2a22f2018-05-28 18:21:51 +0900287 /* 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);
293 }
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900294 } else {
Masahiro Yamadaed2a22f2018-05-28 18:21:51 +0900295 /* For undefined variables, += assumes the recursive flavor */
296 if (flavor == VAR_APPEND)
297 flavor = VAR_RECURSIVE;
298
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900299 v = xmalloc(sizeof(*v));
300 v->name = xstrdup(name);
Masahiro Yamada915f6492018-05-28 18:21:55 +0900301 v->exp_count = 0;
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900302 list_add_tail(&v->node, &variable_list);
303 }
304
Masahiro Yamada1175c022018-05-28 18:21:50 +0900305 v->flavor = flavor;
306
307 if (flavor == VAR_SIMPLE)
Masahiro Yamadaed2a22f2018-05-28 18:21:51 +0900308 new_value = expand_string(value);
Masahiro Yamada1175c022018-05-28 18:21:50 +0900309 else
Masahiro Yamadaed2a22f2018-05-28 18:21:51 +0900310 new_value = xstrdup(value);
311
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;
320 }
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900321}
322
323static void variable_del(struct variable *v)
324{
325 list_del(&v->node);
326 free(v->name);
327 free(v->value);
328 free(v);
329}
330
331void variable_all_del(void)
332{
333 struct variable *v, *tmp;
334
335 list_for_each_entry_safe(v, tmp, &variable_list, node)
336 variable_del(v);
337}
338
339/*
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900340 * Evaluate a clause with arguments. argc/argv are arguments from the upper
341 * function call.
342 *
343 * Returned string must be freed when done
344 */
345static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
346{
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900347 char *tmp, *name, *res, *endptr, *prev, *p;
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900348 int new_argc = 0;
349 char *new_argv[FUNCTION_MAX_ARGS];
350 int nest = 0;
351 int i;
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900352 unsigned long n;
Masahiro Yamada104daea2018-05-28 18:21:40 +0900353
354 tmp = xstrndup(str, len);
355
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900356 /*
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.
360 */
361 n = strtoul(tmp, &endptr, 10);
362 if (!*endptr && n > 0 && n <= argc) {
363 res = xstrdup(argv[n - 1]);
364 goto free_tmp;
365 }
366
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900367 prev = p = tmp;
Masahiro Yamada104daea2018-05-28 18:21:40 +0900368
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900369 /*
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))
374 *
375 * The input string for this helper should be:
376 * foo,$(x),$(y)
377 *
378 * and split into:
379 * new_argv[0] = 'foo'
380 * new_argv[1] = '$(x)'
381 * new_argv[2] = '$(y)'
382 */
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--;
394 }
395
396 p++;
397 }
398 new_argv[new_argc++] = prev;
399
400 /*
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.
405 */
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);
411
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900412 /* Search for variables */
413 res = variable_expand(name, new_argc, new_argv);
414 if (res)
415 goto free;
416
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900417 /* Look for built-in functions */
418 res = function_expand(name, new_argc, new_argv);
Masahiro Yamada104daea2018-05-28 18:21:40 +0900419 if (res)
420 goto free;
421
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900422 /* Last, try environment variable */
423 if (new_argc == 0) {
424 res = env_expand(name);
425 if (res)
426 goto free;
427 }
428
Masahiro Yamada104daea2018-05-28 18:21:40 +0900429 res = xstrdup("");
430free:
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900431 for (i = 0; i < new_argc; i++)
432 free(new_argv[i]);
Masahiro Yamada104daea2018-05-28 18:21:40 +0900433 free(name);
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900434free_tmp:
Masahiro Yamada104daea2018-05-28 18:21:40 +0900435 free(tmp);
436
437 return res;
438}
439
440/*
441 * Expand a string that follows '$'
442 *
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)
451 */
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900452static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
Masahiro Yamada104daea2018-05-28 18:21:40 +0900453{
454 const char *p = *str;
455 const char *q;
456 int nest = 0;
457
458 /*
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900459 * In Kconfig, variable/function references always start with "$(".
Masahiro Yamada104daea2018-05-28 18:21:40 +0900460 * Neither single-letter variables as in $A nor curly braces as in ${CC}
461 * are supported. '$' not followed by '(' loses its special meaning.
462 */
463 if (*p != '(') {
464 *str = p;
465 return xstrdup("$");
466 }
467
468 p++;
469 q = p;
470 while (*q) {
471 if (*q == '(') {
472 nest++;
473 } else if (*q == ')') {
474 if (nest-- == 0)
475 break;
476 }
477 q++;
478 }
479
480 if (!*q)
481 pperror("unterminated reference to '%s': missing ')'", p);
482
483 /* Advance 'str' to after the expanded initial portion of the string */
484 *str = q + 1;
485
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900486 return eval_clause(p, q - p, argc, argv);
Masahiro Yamada104daea2018-05-28 18:21:40 +0900487}
488
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900489char *expand_dollar(const char **str)
490{
491 return expand_dollar_with_args(str, 0, NULL);
492}
493
494static char *__expand_string(const char **str, bool (*is_end)(char c),
495 int argc, char *argv[])
Masahiro Yamada104daea2018-05-28 18:21:40 +0900496{
497 const char *in, *p;
498 char *expansion, *out;
499 size_t in_len, out_len;
500
501 out = xmalloc(1);
502 *out = 0;
503 out_len = 1;
504
505 p = in = *str;
506
507 while (1) {
508 if (*p == '$') {
509 in_len = p - in;
510 p++;
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900511 expansion = expand_dollar_with_args(&p, argc, argv);
Masahiro Yamada104daea2018-05-28 18:21:40 +0900512 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;
519 }
520
521 if (is_end(*p))
522 break;
523
524 p++;
525 }
526
527 in_len = p - in;
528 out_len += in_len;
529 out = xrealloc(out, out_len);
530 strncat(out, in, in_len);
531
532 /* Advance 'str' to the end character */
533 *str = p;
534
535 return out;
536}
537
538static bool is_end_of_str(char c)
539{
540 return !c;
541}
542
543/*
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900544 * Expand variables and functions in the given string. Undefined variables
Masahiro Yamada104daea2018-05-28 18:21:40 +0900545 * expand to an empty string.
546 * The returned string must be freed when done.
547 */
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900548static char *expand_string_with_args(const char *in, int argc, char *argv[])
549{
550 return __expand_string(&in, is_end_of_str, argc, argv);
551}
552
Masahiro Yamada104daea2018-05-28 18:21:40 +0900553char *expand_string(const char *in)
554{
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900555 return expand_string_with_args(in, 0, NULL);
Masahiro Yamada104daea2018-05-28 18:21:40 +0900556}
557
558static bool is_end_of_token(char c)
559{
Masahiro Yamadaf5451582018-12-11 20:01:05 +0900560 return !(isalnum(c) || c == '_' || c == '-');
Masahiro Yamada104daea2018-05-28 18:21:40 +0900561}
562
563/*
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.
567 *
568 * The returned string must be freed when done.
569 */
570char *expand_one_token(const char **str)
571{
Masahiro Yamadae298f3b2018-05-28 18:21:45 +0900572 return __expand_string(str, is_end_of_token, 0, NULL);
Masahiro Yamada104daea2018-05-28 18:21:40 +0900573}