Loading...
1// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (C) 2018 Masahiro Yamada <yamada.masahiro@socionext.com>
4
5#include <ctype.h>
6#include <stdarg.h>
7#include <stdbool.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include "list.h"
13#include "lkc.h"
14
15#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
16
17static char *expand_string_with_args(const char *in, int argc, char *argv[]);
18static char *expand_string(const char *in);
19
20static void __attribute__((noreturn)) pperror(const char *format, ...)
21{
22 va_list ap;
23
24 fprintf(stderr, "%s:%d: ", current_file->name, yylineno);
25 va_start(ap, format);
26 vfprintf(stderr, format, ap);
27 va_end(ap);
28 fprintf(stderr, "\n");
29
30 exit(1);
31}
32
33/*
34 * Environment variables
35 */
36static LIST_HEAD(env_list);
37
38struct env {
39 char *name;
40 char *value;
41 struct list_head node;
42};
43
44static void env_add(const char *name, const char *value)
45{
46 struct env *e;
47
48 e = xmalloc(sizeof(*e));
49 e->name = xstrdup(name);
50 e->value = xstrdup(value);
51
52 list_add_tail(&e->node, &env_list);
53}
54
55static void env_del(struct env *e)
56{
57 list_del(&e->node);
58 free(e->name);
59 free(e->value);
60 free(e);
61}
62
63/* The returned pointer must be freed when done */
64static char *env_expand(const char *name)
65{
66 struct env *e;
67 const char *value;
68
69 if (!*name)
70 return NULL;
71
72 list_for_each_entry(e, &env_list, node) {
73 if (!strcmp(name, e->name))
74 return xstrdup(e->value);
75 }
76
77 value = getenv(name);
78 if (!value)
79 return NULL;
80
81 /*
82 * We need to remember all referenced environment variables.
83 * They will be written out to include/config/auto.conf.cmd
84 */
85 env_add(name, value);
86
87 return xstrdup(value);
88}
89
90void env_write_dep(FILE *f, const char *autoconfig_name)
91{
92 struct env *e, *tmp;
93
94 list_for_each_entry_safe(e, tmp, &env_list, node) {
95 fprintf(f, "ifneq \"$(%s)\" \"%s\"\n", e->name, e->value);
96 fprintf(f, "%s: FORCE\n", autoconfig_name);
97 fprintf(f, "endif\n");
98 env_del(e);
99 }
100}
101
102/*
103 * Built-in functions
104 */
105struct function {
106 const char *name;
107 unsigned int min_args;
108 unsigned int max_args;
109 char *(*func)(int argc, char *argv[]);
110};
111
112static char *do_error_if(int argc, char *argv[])
113{
114 if (!strcmp(argv[0], "y"))
115 pperror("%s", argv[1]);
116
117 return xstrdup("");
118}
119
120static char *do_filename(int argc, char *argv[])
121{
122 return xstrdup(current_file->name);
123}
124
125static char *do_info(int argc, char *argv[])
126{
127 printf("%s\n", argv[0]);
128
129 return xstrdup("");
130}
131
132static char *do_lineno(int argc, char *argv[])
133{
134 char buf[16];
135
136 sprintf(buf, "%d", yylineno);
137
138 return xstrdup(buf);
139}
140
141static char *do_shell(int argc, char *argv[])
142{
143 FILE *p;
144 char buf[4096];
145 char *cmd;
146 size_t nread;
147 int i;
148
149 cmd = argv[0];
150
151 p = popen(cmd, "r");
152 if (!p) {
153 perror(cmd);
154 exit(1);
155 }
156
157 nread = fread(buf, 1, sizeof(buf), p);
158 if (nread == sizeof(buf))
159 nread--;
160
161 /* remove trailing new lines */
162 while (nread > 0 && buf[nread - 1] == '\n')
163 nread--;
164
165 buf[nread] = 0;
166
167 /* replace a new line with a space */
168 for (i = 0; i < nread; i++) {
169 if (buf[i] == '\n')
170 buf[i] = ' ';
171 }
172
173 if (pclose(p) == -1) {
174 perror(cmd);
175 exit(1);
176 }
177
178 return xstrdup(buf);
179}
180
181static char *do_warning_if(int argc, char *argv[])
182{
183 if (!strcmp(argv[0], "y"))
184 fprintf(stderr, "%s:%d: %s\n",
185 current_file->name, yylineno, argv[1]);
186
187 return xstrdup("");
188}
189
190static const struct function function_table[] = {
191 /* Name MIN MAX Function */
192 { "error-if", 2, 2, do_error_if },
193 { "filename", 0, 0, do_filename },
194 { "info", 1, 1, do_info },
195 { "lineno", 0, 0, do_lineno },
196 { "shell", 1, 1, do_shell },
197 { "warning-if", 2, 2, do_warning_if },
198};
199
200#define FUNCTION_MAX_ARGS 16
201
202static char *function_expand(const char *name, int argc, char *argv[])
203{
204 const struct function *f;
205 int i;
206
207 for (i = 0; i < ARRAY_SIZE(function_table); i++) {
208 f = &function_table[i];
209 if (strcmp(f->name, name))
210 continue;
211
212 if (argc < f->min_args)
213 pperror("too few function arguments passed to '%s'",
214 name);
215
216 if (argc > f->max_args)
217 pperror("too many function arguments passed to '%s'",
218 name);
219
220 return f->func(argc, argv);
221 }
222
223 return NULL;
224}
225
226/*
227 * Variables (and user-defined functions)
228 */
229static LIST_HEAD(variable_list);
230
231struct variable {
232 char *name;
233 char *value;
234 enum variable_flavor flavor;
235 int exp_count;
236 struct list_head node;
237};
238
239static struct variable *variable_lookup(const char *name)
240{
241 struct variable *v;
242
243 list_for_each_entry(v, &variable_list, node) {
244 if (!strcmp(name, v->name))
245 return v;
246 }
247
248 return NULL;
249}
250
251static char *variable_expand(const char *name, int argc, char *argv[])
252{
253 struct variable *v;
254 char *res;
255
256 v = variable_lookup(name);
257 if (!v)
258 return NULL;
259
260 if (argc == 0 && v->exp_count)
261 pperror("Recursive variable '%s' references itself (eventually)",
262 name);
263
264 if (v->exp_count > 1000)
265 pperror("Too deep recursive expansion");
266
267 v->exp_count++;
268
269 if (v->flavor == VAR_RECURSIVE)
270 res = expand_string_with_args(v->value, argc, argv);
271 else
272 res = xstrdup(v->value);
273
274 v->exp_count--;
275
276 return res;
277}
278
279void variable_add(const char *name, const char *value,
280 enum variable_flavor flavor)
281{
282 struct variable *v;
283 char *new_value;
284 bool append = false;
285
286 v = variable_lookup(name);
287 if (v) {
288 /* For defined variables, += inherits the existing flavor */
289 if (flavor == VAR_APPEND) {
290 flavor = v->flavor;
291 append = true;
292 } else {
293 free(v->value);
294 }
295 } else {
296 /* For undefined variables, += assumes the recursive flavor */
297 if (flavor == VAR_APPEND)
298 flavor = VAR_RECURSIVE;
299
300 v = xmalloc(sizeof(*v));
301 v->name = xstrdup(name);
302 v->exp_count = 0;
303 list_add_tail(&v->node, &variable_list);
304 }
305
306 v->flavor = flavor;
307
308 if (flavor == VAR_SIMPLE)
309 new_value = expand_string(value);
310 else
311 new_value = xstrdup(value);
312
313 if (append) {
314 v->value = xrealloc(v->value,
315 strlen(v->value) + strlen(new_value) + 2);
316 strcat(v->value, " ");
317 strcat(v->value, new_value);
318 free(new_value);
319 } else {
320 v->value = new_value;
321 }
322}
323
324static void variable_del(struct variable *v)
325{
326 list_del(&v->node);
327 free(v->name);
328 free(v->value);
329 free(v);
330}
331
332void variable_all_del(void)
333{
334 struct variable *v, *tmp;
335
336 list_for_each_entry_safe(v, tmp, &variable_list, node)
337 variable_del(v);
338}
339
340/*
341 * Evaluate a clause with arguments. argc/argv are arguments from the upper
342 * function call.
343 *
344 * Returned string must be freed when done
345 */
346static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
347{
348 char *tmp, *name, *res, *endptr, *prev, *p;
349 int new_argc = 0;
350 char *new_argv[FUNCTION_MAX_ARGS];
351 int nest = 0;
352 int i;
353 unsigned long n;
354
355 tmp = xstrndup(str, len);
356
357 /*
358 * If variable name is '1', '2', etc. It is generally an argument
359 * from a user-function call (i.e. local-scope variable). If not
360 * available, then look-up global-scope variables.
361 */
362 n = strtoul(tmp, &endptr, 10);
363 if (!*endptr && n > 0 && n <= argc) {
364 res = xstrdup(argv[n - 1]);
365 goto free_tmp;
366 }
367
368 prev = p = tmp;
369
370 /*
371 * Split into tokens
372 * The function name and arguments are separated by a comma.
373 * For example, if the function call is like this:
374 * $(foo,$(x),$(y))
375 *
376 * The input string for this helper should be:
377 * foo,$(x),$(y)
378 *
379 * and split into:
380 * new_argv[0] = 'foo'
381 * new_argv[1] = '$(x)'
382 * new_argv[2] = '$(y)'
383 */
384 while (*p) {
385 if (nest == 0 && *p == ',') {
386 *p = 0;
387 if (new_argc >= FUNCTION_MAX_ARGS)
388 pperror("too many function arguments");
389 new_argv[new_argc++] = prev;
390 prev = p + 1;
391 } else if (*p == '(') {
392 nest++;
393 } else if (*p == ')') {
394 nest--;
395 }
396
397 p++;
398 }
399
400 if (new_argc >= FUNCTION_MAX_ARGS)
401 pperror("too many function arguments");
402 new_argv[new_argc++] = prev;
403
404 /*
405 * Shift arguments
406 * new_argv[0] represents a function name or a variable name. Put it
407 * into 'name', then shift the rest of the arguments. This simplifies
408 * 'const' handling.
409 */
410 name = expand_string_with_args(new_argv[0], argc, argv);
411 new_argc--;
412 for (i = 0; i < new_argc; i++)
413 new_argv[i] = expand_string_with_args(new_argv[i + 1],
414 argc, argv);
415
416 /* Search for variables */
417 res = variable_expand(name, new_argc, new_argv);
418 if (res)
419 goto free;
420
421 /* Look for built-in functions */
422 res = function_expand(name, new_argc, new_argv);
423 if (res)
424 goto free;
425
426 /* Last, try environment variable */
427 if (new_argc == 0) {
428 res = env_expand(name);
429 if (res)
430 goto free;
431 }
432
433 res = xstrdup("");
434free:
435 for (i = 0; i < new_argc; i++)
436 free(new_argv[i]);
437 free(name);
438free_tmp:
439 free(tmp);
440
441 return res;
442}
443
444/*
445 * Expand a string that follows '$'
446 *
447 * For example, if the input string is
448 * ($(FOO)$($(BAR)))$(BAZ)
449 * this helper evaluates
450 * $($(FOO)$($(BAR)))
451 * and returns a new string containing the expansion (note that the string is
452 * recursively expanded), also advancing 'str' to point to the next character
453 * after the corresponding closing parenthesis, in this case, *str will be
454 * $(BAR)
455 */
456static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
457{
458 const char *p = *str;
459 const char *q;
460 int nest = 0;
461
462 /*
463 * In Kconfig, variable/function references always start with "$(".
464 * Neither single-letter variables as in $A nor curly braces as in ${CC}
465 * are supported. '$' not followed by '(' loses its special meaning.
466 */
467 if (*p != '(') {
468 *str = p;
469 return xstrdup("$");
470 }
471
472 p++;
473 q = p;
474 while (*q) {
475 if (*q == '(') {
476 nest++;
477 } else if (*q == ')') {
478 if (nest-- == 0)
479 break;
480 }
481 q++;
482 }
483
484 if (!*q)
485 pperror("unterminated reference to '%s': missing ')'", p);
486
487 /* Advance 'str' to after the expanded initial portion of the string */
488 *str = q + 1;
489
490 return eval_clause(p, q - p, argc, argv);
491}
492
493char *expand_dollar(const char **str)
494{
495 return expand_dollar_with_args(str, 0, NULL);
496}
497
498static char *__expand_string(const char **str, bool (*is_end)(char c),
499 int argc, char *argv[])
500{
501 const char *in, *p;
502 char *expansion, *out;
503 size_t in_len, out_len;
504
505 out = xmalloc(1);
506 *out = 0;
507 out_len = 1;
508
509 p = in = *str;
510
511 while (1) {
512 if (*p == '$') {
513 in_len = p - in;
514 p++;
515 expansion = expand_dollar_with_args(&p, argc, argv);
516 out_len += in_len + strlen(expansion);
517 out = xrealloc(out, out_len);
518 strncat(out, in, in_len);
519 strcat(out, expansion);
520 free(expansion);
521 in = p;
522 continue;
523 }
524
525 if (is_end(*p))
526 break;
527
528 p++;
529 }
530
531 in_len = p - in;
532 out_len += in_len;
533 out = xrealloc(out, out_len);
534 strncat(out, in, in_len);
535
536 /* Advance 'str' to the end character */
537 *str = p;
538
539 return out;
540}
541
542static bool is_end_of_str(char c)
543{
544 return !c;
545}
546
547/*
548 * Expand variables and functions in the given string. Undefined variables
549 * expand to an empty string.
550 * The returned string must be freed when done.
551 */
552static char *expand_string_with_args(const char *in, int argc, char *argv[])
553{
554 return __expand_string(&in, is_end_of_str, argc, argv);
555}
556
557static char *expand_string(const char *in)
558{
559 return expand_string_with_args(in, 0, NULL);
560}
561
562static bool is_end_of_token(char c)
563{
564 return !(isalnum(c) || c == '_' || c == '-');
565}
566
567/*
568 * Expand variables in a token. The parsing stops when a token separater
569 * (in most cases, it is a whitespace) is encountered. 'str' is updated to
570 * point to the next character.
571 *
572 * The returned string must be freed when done.
573 */
574char *expand_one_token(const char **str)
575{
576 return __expand_string(str, is_end_of_token, 0, NULL);
577}
1// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (C) 2018 Masahiro Yamada <yamada.masahiro@socionext.com>
4
5#include <ctype.h>
6#include <stdarg.h>
7#include <stdbool.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include <array_size.h>
13#include <list.h>
14#include <xalloc.h>
15#include "internal.h"
16#include "lkc.h"
17#include "preprocess.h"
18
19static char *expand_string_with_args(const char *in, int argc, char *argv[]);
20static char *expand_string(const char *in);
21
22static void __attribute__((noreturn)) pperror(const char *format, ...)
23{
24 va_list ap;
25
26 fprintf(stderr, "%s:%d: ", cur_filename, yylineno);
27 va_start(ap, format);
28 vfprintf(stderr, format, ap);
29 va_end(ap);
30 fprintf(stderr, "\n");
31
32 exit(1);
33}
34
35/*
36 * Environment variables
37 */
38static LIST_HEAD(env_list);
39
40struct env {
41 char *name;
42 char *value;
43 struct list_head node;
44};
45
46static void env_add(const char *name, const char *value)
47{
48 struct env *e;
49
50 e = xmalloc(sizeof(*e));
51 e->name = xstrdup(name);
52 e->value = xstrdup(value);
53
54 list_add_tail(&e->node, &env_list);
55}
56
57static void env_del(struct env *e)
58{
59 list_del(&e->node);
60 free(e->name);
61 free(e->value);
62 free(e);
63}
64
65/* The returned pointer must be freed when done */
66static char *env_expand(const char *name)
67{
68 struct env *e;
69 const char *value;
70
71 if (!*name)
72 return NULL;
73
74 list_for_each_entry(e, &env_list, node) {
75 if (!strcmp(name, e->name))
76 return xstrdup(e->value);
77 }
78
79 value = getenv(name);
80 if (!value)
81 return NULL;
82
83 /*
84 * We need to remember all referenced environment variables.
85 * They will be written out to include/config/auto.conf.cmd
86 */
87 env_add(name, value);
88
89 return xstrdup(value);
90}
91
92void env_write_dep(struct gstr *s)
93{
94 struct env *e, *tmp;
95
96 list_for_each_entry_safe(e, tmp, &env_list, node) {
97 str_printf(s,
98 "\n"
99 "ifneq \"$(%s)\" \"%s\"\n"
100 "$(autoconfig): FORCE\n"
101 "endif\n",
102 e->name, e->value);
103 env_del(e);
104 }
105}
106
107/*
108 * Built-in functions
109 */
110struct function {
111 const char *name;
112 unsigned int min_args;
113 unsigned int max_args;
114 char *(*func)(int argc, char *argv[]);
115};
116
117static char *do_error_if(int argc, char *argv[])
118{
119 if (!strcmp(argv[0], "y"))
120 pperror("%s", argv[1]);
121
122 return xstrdup("");
123}
124
125static char *do_filename(int argc, char *argv[])
126{
127 return xstrdup(cur_filename);
128}
129
130static char *do_info(int argc, char *argv[])
131{
132 printf("%s\n", argv[0]);
133
134 return xstrdup("");
135}
136
137static char *do_lineno(int argc, char *argv[])
138{
139 char buf[16];
140
141 sprintf(buf, "%d", yylineno);
142
143 return xstrdup(buf);
144}
145
146static char *do_shell(int argc, char *argv[])
147{
148 FILE *p;
149 char buf[4096];
150 char *cmd;
151 size_t nread;
152 int i;
153
154 cmd = argv[0];
155
156 p = popen(cmd, "r");
157 if (!p) {
158 perror(cmd);
159 exit(1);
160 }
161
162 nread = fread(buf, 1, sizeof(buf), p);
163 if (nread == sizeof(buf))
164 nread--;
165
166 /* remove trailing new lines */
167 while (nread > 0 && buf[nread - 1] == '\n')
168 nread--;
169
170 buf[nread] = 0;
171
172 /* replace a new line with a space */
173 for (i = 0; i < nread; i++) {
174 if (buf[i] == '\n')
175 buf[i] = ' ';
176 }
177
178 if (pclose(p) == -1) {
179 perror(cmd);
180 exit(1);
181 }
182
183 return xstrdup(buf);
184}
185
186static char *do_warning_if(int argc, char *argv[])
187{
188 if (!strcmp(argv[0], "y"))
189 fprintf(stderr, "%s:%d: %s\n", cur_filename, yylineno, argv[1]);
190
191 return xstrdup("");
192}
193
194static 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 },
202};
203
204#define FUNCTION_MAX_ARGS 16
205
206static char *function_expand(const char *name, int argc, char *argv[])
207{
208 const struct function *f;
209 int i;
210
211 for (i = 0; i < ARRAY_SIZE(function_table); i++) {
212 f = &function_table[i];
213 if (strcmp(f->name, name))
214 continue;
215
216 if (argc < f->min_args)
217 pperror("too few function arguments passed to '%s'",
218 name);
219
220 if (argc > f->max_args)
221 pperror("too many function arguments passed to '%s'",
222 name);
223
224 return f->func(argc, argv);
225 }
226
227 return NULL;
228}
229
230/*
231 * Variables (and user-defined functions)
232 */
233static LIST_HEAD(variable_list);
234
235struct variable {
236 char *name;
237 char *value;
238 enum variable_flavor flavor;
239 int exp_count;
240 struct list_head node;
241};
242
243static struct variable *variable_lookup(const char *name)
244{
245 struct variable *v;
246
247 list_for_each_entry(v, &variable_list, node) {
248 if (!strcmp(name, v->name))
249 return v;
250 }
251
252 return NULL;
253}
254
255static char *variable_expand(const char *name, int argc, char *argv[])
256{
257 struct variable *v;
258 char *res;
259
260 v = variable_lookup(name);
261 if (!v)
262 return NULL;
263
264 if (argc == 0 && v->exp_count)
265 pperror("Recursive variable '%s' references itself (eventually)",
266 name);
267
268 if (v->exp_count > 1000)
269 pperror("Too deep recursive expansion");
270
271 v->exp_count++;
272
273 if (v->flavor == VAR_RECURSIVE)
274 res = expand_string_with_args(v->value, argc, argv);
275 else
276 res = xstrdup(v->value);
277
278 v->exp_count--;
279
280 return res;
281}
282
283void variable_add(const char *name, const char *value,
284 enum variable_flavor flavor)
285{
286 struct variable *v;
287 char *new_value;
288 bool append = false;
289
290 v = variable_lookup(name);
291 if (v) {
292 /* For defined variables, += inherits the existing flavor */
293 if (flavor == VAR_APPEND) {
294 flavor = v->flavor;
295 append = true;
296 } else {
297 free(v->value);
298 }
299 } else {
300 /* For undefined variables, += assumes the recursive flavor */
301 if (flavor == VAR_APPEND)
302 flavor = VAR_RECURSIVE;
303
304 v = xmalloc(sizeof(*v));
305 v->name = xstrdup(name);
306 v->exp_count = 0;
307 list_add_tail(&v->node, &variable_list);
308 }
309
310 v->flavor = flavor;
311
312 if (flavor == VAR_SIMPLE)
313 new_value = expand_string(value);
314 else
315 new_value = xstrdup(value);
316
317 if (append) {
318 v->value = xrealloc(v->value,
319 strlen(v->value) + strlen(new_value) + 2);
320 strcat(v->value, " ");
321 strcat(v->value, new_value);
322 free(new_value);
323 } else {
324 v->value = new_value;
325 }
326}
327
328static void variable_del(struct variable *v)
329{
330 list_del(&v->node);
331 free(v->name);
332 free(v->value);
333 free(v);
334}
335
336void variable_all_del(void)
337{
338 struct variable *v, *tmp;
339
340 list_for_each_entry_safe(v, tmp, &variable_list, node)
341 variable_del(v);
342}
343
344/*
345 * Evaluate a clause with arguments. argc/argv are arguments from the upper
346 * function call.
347 *
348 * Returned string must be freed when done
349 */
350static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
351{
352 char *tmp, *name, *res, *endptr, *prev, *p;
353 int new_argc = 0;
354 char *new_argv[FUNCTION_MAX_ARGS];
355 int nest = 0;
356 int i;
357 unsigned long n;
358
359 tmp = xstrndup(str, len);
360
361 /*
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.
365 */
366 n = strtoul(tmp, &endptr, 10);
367 if (!*endptr && n > 0 && n <= argc) {
368 res = xstrdup(argv[n - 1]);
369 goto free_tmp;
370 }
371
372 prev = p = tmp;
373
374 /*
375 * Split into tokens
376 * The function name and arguments are separated by a comma.
377 * For example, if the function call is like this:
378 * $(foo,$(x),$(y))
379 *
380 * The input string for this helper should be:
381 * foo,$(x),$(y)
382 *
383 * and split into:
384 * new_argv[0] = 'foo'
385 * new_argv[1] = '$(x)'
386 * new_argv[2] = '$(y)'
387 */
388 while (*p) {
389 if (nest == 0 && *p == ',') {
390 *p = 0;
391 if (new_argc >= FUNCTION_MAX_ARGS)
392 pperror("too many function arguments");
393 new_argv[new_argc++] = prev;
394 prev = p + 1;
395 } else if (*p == '(') {
396 nest++;
397 } else if (*p == ')') {
398 nest--;
399 }
400
401 p++;
402 }
403
404 if (new_argc >= FUNCTION_MAX_ARGS)
405 pperror("too many function arguments");
406 new_argv[new_argc++] = prev;
407
408 /*
409 * Shift arguments
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
412 * 'const' handling.
413 */
414 name = expand_string_with_args(new_argv[0], argc, argv);
415 new_argc--;
416 for (i = 0; i < new_argc; i++)
417 new_argv[i] = expand_string_with_args(new_argv[i + 1],
418 argc, argv);
419
420 /* Search for variables */
421 res = variable_expand(name, new_argc, new_argv);
422 if (res)
423 goto free;
424
425 /* Look for built-in functions */
426 res = function_expand(name, new_argc, new_argv);
427 if (res)
428 goto free;
429
430 /* Last, try environment variable */
431 if (new_argc == 0) {
432 res = env_expand(name);
433 if (res)
434 goto free;
435 }
436
437 res = xstrdup("");
438free:
439 for (i = 0; i < new_argc; i++)
440 free(new_argv[i]);
441 free(name);
442free_tmp:
443 free(tmp);
444
445 return res;
446}
447
448/*
449 * Expand a string that follows '$'
450 *
451 * For example, if the input string is
452 * ($(FOO)$($(BAR)))$(BAZ)
453 * this helper evaluates
454 * $($(FOO)$($(BAR)))
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
458 * $(BAR)
459 */
460static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
461{
462 const char *p = *str;
463 const char *q;
464 int nest = 0;
465
466 /*
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.
470 */
471 if (*p != '(') {
472 *str = p;
473 return xstrdup("$");
474 }
475
476 p++;
477 q = p;
478 while (*q) {
479 if (*q == '(') {
480 nest++;
481 } else if (*q == ')') {
482 if (nest-- == 0)
483 break;
484 }
485 q++;
486 }
487
488 if (!*q)
489 pperror("unterminated reference to '%s': missing ')'", p);
490
491 /* Advance 'str' to after the expanded initial portion of the string */
492 *str = q + 1;
493
494 return eval_clause(p, q - p, argc, argv);
495}
496
497char *expand_dollar(const char **str)
498{
499 return expand_dollar_with_args(str, 0, NULL);
500}
501
502static char *__expand_string(const char **str, bool (*is_end)(char c),
503 int argc, char *argv[])
504{
505 const char *in, *p;
506 char *expansion, *out;
507 size_t in_len, out_len;
508
509 out = xmalloc(1);
510 *out = 0;
511 out_len = 1;
512
513 p = in = *str;
514
515 while (1) {
516 if (*p == '$') {
517 in_len = p - in;
518 p++;
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);
524 free(expansion);
525 in = p;
526 continue;
527 }
528
529 if (is_end(*p))
530 break;
531
532 p++;
533 }
534
535 in_len = p - in;
536 out_len += in_len;
537 out = xrealloc(out, out_len);
538 strncat(out, in, in_len);
539
540 /* Advance 'str' to the end character */
541 *str = p;
542
543 return out;
544}
545
546static bool is_end_of_str(char c)
547{
548 return !c;
549}
550
551/*
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.
555 */
556static char *expand_string_with_args(const char *in, int argc, char *argv[])
557{
558 return __expand_string(&in, is_end_of_str, argc, argv);
559}
560
561static char *expand_string(const char *in)
562{
563 return expand_string_with_args(in, 0, NULL);
564}
565
566static bool is_end_of_token(char c)
567{
568 return !(isalnum(c) || c == '_' || c == '-');
569}
570
571/*
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.
575 *
576 * The returned string must be freed when done.
577 */
578char *expand_one_token(const char **str)
579{
580 return __expand_string(str, is_end_of_token, 0, NULL);
581}