kconfig: add built-in function support
[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 const struct function function_table[] = {
110         /* Name         MIN     MAX     Function */
111 };
112
113 #define FUNCTION_MAX_ARGS               16
114
115 static char *function_expand(const char *name, int argc, char *argv[])
116 {
117         const struct function *f;
118         int i;
119
120         for (i = 0; i < ARRAY_SIZE(function_table); i++) {
121                 f = &function_table[i];
122                 if (strcmp(f->name, name))
123                         continue;
124
125                 if (argc < f->min_args)
126                         pperror("too few function arguments passed to '%s'",
127                                 name);
128
129                 if (argc > f->max_args)
130                         pperror("too many function arguments passed to '%s'",
131                                 name);
132
133                 return f->func(argc, argv);
134         }
135
136         return NULL;
137 }
138
139 /*
140  * Evaluate a clause with arguments.  argc/argv are arguments from the upper
141  * function call.
142  *
143  * Returned string must be freed when done
144  */
145 static char *eval_clause(const char *str, size_t len, int argc, char *argv[])
146 {
147         char *tmp, *name, *res, *prev, *p;
148         int new_argc = 0;
149         char *new_argv[FUNCTION_MAX_ARGS];
150         int nest = 0;
151         int i;
152
153         tmp = xstrndup(str, len);
154
155         prev = p = tmp;
156
157         /*
158          * Split into tokens
159          * The function name and arguments are separated by a comma.
160          * For example, if the function call is like this:
161          *   $(foo,$(x),$(y))
162          *
163          * The input string for this helper should be:
164          *   foo,$(x),$(y)
165          *
166          * and split into:
167          *   new_argv[0] = 'foo'
168          *   new_argv[1] = '$(x)'
169          *   new_argv[2] = '$(y)'
170          */
171         while (*p) {
172                 if (nest == 0 && *p == ',') {
173                         *p = 0;
174                         if (new_argc >= FUNCTION_MAX_ARGS)
175                                 pperror("too many function arguments");
176                         new_argv[new_argc++] = prev;
177                         prev = p + 1;
178                 } else if (*p == '(') {
179                         nest++;
180                 } else if (*p == ')') {
181                         nest--;
182                 }
183
184                 p++;
185         }
186         new_argv[new_argc++] = prev;
187
188         /*
189          * Shift arguments
190          * new_argv[0] represents a function name or a variable name.  Put it
191          * into 'name', then shift the rest of the arguments.  This simplifies
192          * 'const' handling.
193          */
194         name = expand_string_with_args(new_argv[0], argc, argv);
195         new_argc--;
196         for (i = 0; i < new_argc; i++)
197                 new_argv[i] = expand_string_with_args(new_argv[i + 1],
198                                                       argc, argv);
199
200         /* Look for built-in functions */
201         res = function_expand(name, new_argc, new_argv);
202         if (res)
203                 goto free;
204
205         /* Last, try environment variable */
206         if (new_argc == 0) {
207                 res = env_expand(name);
208                 if (res)
209                         goto free;
210         }
211
212         res = xstrdup("");
213 free:
214         for (i = 0; i < new_argc; i++)
215                 free(new_argv[i]);
216         free(name);
217         free(tmp);
218
219         return res;
220 }
221
222 /*
223  * Expand a string that follows '$'
224  *
225  * For example, if the input string is
226  *     ($(FOO)$($(BAR)))$(BAZ)
227  * this helper evaluates
228  *     $($(FOO)$($(BAR)))
229  * and returns a new string containing the expansion (note that the string is
230  * recursively expanded), also advancing 'str' to point to the next character
231  * after the corresponding closing parenthesis, in this case, *str will be
232  *     $(BAR)
233  */
234 static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
235 {
236         const char *p = *str;
237         const char *q;
238         int nest = 0;
239
240         /*
241          * In Kconfig, variable/function references always start with "$(".
242          * Neither single-letter variables as in $A nor curly braces as in ${CC}
243          * are supported.  '$' not followed by '(' loses its special meaning.
244          */
245         if (*p != '(') {
246                 *str = p;
247                 return xstrdup("$");
248         }
249
250         p++;
251         q = p;
252         while (*q) {
253                 if (*q == '(') {
254                         nest++;
255                 } else if (*q == ')') {
256                         if (nest-- == 0)
257                                 break;
258                 }
259                 q++;
260         }
261
262         if (!*q)
263                 pperror("unterminated reference to '%s': missing ')'", p);
264
265         /* Advance 'str' to after the expanded initial portion of the string */
266         *str = q + 1;
267
268         return eval_clause(p, q - p, argc, argv);
269 }
270
271 char *expand_dollar(const char **str)
272 {
273         return expand_dollar_with_args(str, 0, NULL);
274 }
275
276 static char *__expand_string(const char **str, bool (*is_end)(char c),
277                              int argc, char *argv[])
278 {
279         const char *in, *p;
280         char *expansion, *out;
281         size_t in_len, out_len;
282
283         out = xmalloc(1);
284         *out = 0;
285         out_len = 1;
286
287         p = in = *str;
288
289         while (1) {
290                 if (*p == '$') {
291                         in_len = p - in;
292                         p++;
293                         expansion = expand_dollar_with_args(&p, argc, argv);
294                         out_len += in_len + strlen(expansion);
295                         out = xrealloc(out, out_len);
296                         strncat(out, in, in_len);
297                         strcat(out, expansion);
298                         free(expansion);
299                         in = p;
300                         continue;
301                 }
302
303                 if (is_end(*p))
304                         break;
305
306                 p++;
307         }
308
309         in_len = p - in;
310         out_len += in_len;
311         out = xrealloc(out, out_len);
312         strncat(out, in, in_len);
313
314         /* Advance 'str' to the end character */
315         *str = p;
316
317         return out;
318 }
319
320 static bool is_end_of_str(char c)
321 {
322         return !c;
323 }
324
325 /*
326  * Expand variables and functions in the given string.  Undefined variables
327  * expand to an empty string.
328  * The returned string must be freed when done.
329  */
330 static char *expand_string_with_args(const char *in, int argc, char *argv[])
331 {
332         return __expand_string(&in, is_end_of_str, argc, argv);
333 }
334
335 char *expand_string(const char *in)
336 {
337         return expand_string_with_args(in, 0, NULL);
338 }
339
340 static bool is_end_of_token(char c)
341 {
342         /* Why are '.' and '/' valid characters for symbols? */
343         return !(isalnum(c) || c == '_' || c == '-' || c == '.' || c == '/');
344 }
345
346 /*
347  * Expand variables in a token.  The parsing stops when a token separater
348  * (in most cases, it is a whitespace) is encountered.  'str' is updated to
349  * point to the next character.
350  *
351  * The returned string must be freed when done.
352  */
353 char *expand_one_token(const char **str)
354 {
355         return __expand_string(str, is_end_of_token, 0, NULL);
356 }