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