kconfig: support user-defined function and recursively expanded variable
[carl9170fw.git] / config / zconf.l
1 %option nostdinit noyywrap never-interactive full ecs
2 %option 8bit nodefault yylineno
3 %x COMMAND HELP STRING PARAM ASSIGN_VAL
4 %{
5 /*
6  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
7  * Released under the terms of the GNU GPL v2.0.
8  */
9
10 #include <assert.h>
11 #include <limits.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16
17 #include "lkc.h"
18
19 #define START_STRSIZE   16
20
21 static struct {
22         struct file *file;
23         int lineno;
24 } current_pos;
25
26 static char *text;
27 static int text_size, text_asize;
28
29 struct buffer {
30         struct buffer *parent;
31         YY_BUFFER_STATE state;
32 };
33
34 struct buffer *current_buf;
35
36 static int last_ts, first_ts;
37
38 static char *expand_token(const char *in, size_t n);
39 static void append_expanded_string(const char *in);
40 static void zconf_endhelp(void);
41 static void zconf_endfile(void);
42
43 static void new_string(void)
44 {
45         text = xmalloc(START_STRSIZE);
46         text_asize = START_STRSIZE;
47         text_size = 0;
48         *text = 0;
49 }
50
51 static void append_string(const char *str, int size)
52 {
53         int new_size = text_size + size + 1;
54         if (new_size > text_asize) {
55                 new_size += START_STRSIZE - 1;
56                 new_size &= -START_STRSIZE;
57                 text = xrealloc(text, new_size);
58                 text_asize = new_size;
59         }
60         memcpy(text + text_size, str, size);
61         text_size += size;
62         text[text_size] = 0;
63 }
64
65 static void alloc_string(const char *str, int size)
66 {
67         text = xmalloc(size + 1);
68         memcpy(text, str, size);
69         text[size] = 0;
70 }
71
72 static void warn_ignored_character(char chr)
73 {
74         fprintf(stderr,
75                 "%s:%d:warning: ignoring unsupported character '%c'\n",
76                 zconf_curname(), zconf_lineno(), chr);
77 }
78 %}
79
80 n       [A-Za-z0-9_-]
81
82 %%
83         int str = 0;
84         int ts, i;
85
86 [ \t]*#.*\n     |
87 [ \t]*\n        {
88         return T_EOL;
89 }
90 [ \t]*#.*
91
92
93 [ \t]+  {
94         BEGIN(COMMAND);
95 }
96
97 .       {
98         unput(yytext[0]);
99         BEGIN(COMMAND);
100 }
101
102
103 <COMMAND>{
104         {n}+    {
105                 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
106                 current_pos.file = current_file;
107                 current_pos.lineno = yylineno;
108                 if (id && id->flags & TF_COMMAND) {
109                         BEGIN(PARAM);
110                         yylval.id = id;
111                         return id->token;
112                 }
113                 alloc_string(yytext, yyleng);
114                 yylval.string = text;
115                 return T_VARIABLE;
116         }
117         "="     { BEGIN(ASSIGN_VAL); return T_ASSIGN; }
118         [[:blank:]]+
119         .       warn_ignored_character(*yytext);
120         \n      {
121                 BEGIN(INITIAL);
122                 return T_EOL;
123         }
124 }
125
126 <ASSIGN_VAL>{
127         [^[:blank:]\n]+.*       {
128                 alloc_string(yytext, yyleng);
129                 yylval.string = text;
130                 return T_ASSIGN_VAL;
131         }
132         \n      { BEGIN(INITIAL); return T_EOL; }
133         .
134 }
135
136 <PARAM>{
137         "&&"    return T_AND;
138         "||"    return T_OR;
139         "("     return T_OPEN_PAREN;
140         ")"     return T_CLOSE_PAREN;
141         "!"     return T_NOT;
142         "="     return T_EQUAL;
143         "!="    return T_UNEQUAL;
144         "<="    return T_LESS_EQUAL;
145         ">="    return T_GREATER_EQUAL;
146         "<"     return T_LESS;
147         ">"     return T_GREATER;
148         \"|\'   {
149                 str = yytext[0];
150                 new_string();
151                 BEGIN(STRING);
152         }
153         \n      BEGIN(INITIAL); return T_EOL;
154         ({n}|[/.])+     {
155                 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
156                 if (id && id->flags & TF_PARAM) {
157                         yylval.id = id;
158                         return id->token;
159                 }
160                 alloc_string(yytext, yyleng);
161                 yylval.string = text;
162                 return T_WORD;
163         }
164         ({n}|[/.$])+    {
165                 /* this token includes at least one '$' */
166                 yylval.string = expand_token(yytext, yyleng);
167                 if (strlen(yylval.string))
168                         return T_WORD;
169                 free(yylval.string);
170         }
171         #.*     /* comment */
172         \\\n    ;
173         [[:blank:]]+
174         .       warn_ignored_character(*yytext);
175         <<EOF>> {
176                 BEGIN(INITIAL);
177         }
178 }
179
180 <STRING>{
181         "$".*   append_expanded_string(yytext);
182         [^$'"\\\n]+/\n  {
183                 append_string(yytext, yyleng);
184                 yylval.string = text;
185                 return T_WORD_QUOTE;
186         }
187         [^$'"\\\n]+     {
188                 append_string(yytext, yyleng);
189         }
190         \\.?/\n {
191                 append_string(yytext + 1, yyleng - 1);
192                 yylval.string = text;
193                 return T_WORD_QUOTE;
194         }
195         \\.?    {
196                 append_string(yytext + 1, yyleng - 1);
197         }
198         \'|\"   {
199                 if (str == yytext[0]) {
200                         BEGIN(PARAM);
201                         yylval.string = text;
202                         return T_WORD_QUOTE;
203                 } else
204                         append_string(yytext, 1);
205         }
206         \n      {
207                 fprintf(stderr,
208                         "%s:%d:warning: multi-line strings not supported\n",
209                         zconf_curname(), zconf_lineno());
210                 BEGIN(INITIAL);
211                 return T_EOL;
212         }
213         <<EOF>> {
214                 BEGIN(INITIAL);
215         }
216 }
217
218 <HELP>{
219         [ \t]+  {
220                 ts = 0;
221                 for (i = 0; i < yyleng; i++) {
222                         if (yytext[i] == '\t')
223                                 ts = (ts & ~7) + 8;
224                         else
225                                 ts++;
226                 }
227                 last_ts = ts;
228                 if (first_ts) {
229                         if (ts < first_ts) {
230                                 zconf_endhelp();
231                                 return T_HELPTEXT;
232                         }
233                         ts -= first_ts;
234                         while (ts > 8) {
235                                 append_string("        ", 8);
236                                 ts -= 8;
237                         }
238                         append_string("        ", ts);
239                 }
240         }
241         [ \t]*\n/[^ \t\n] {
242                 zconf_endhelp();
243                 return T_HELPTEXT;
244         }
245         [ \t]*\n        {
246                 append_string("\n", 1);
247         }
248         [^ \t\n].* {
249                 while (yyleng) {
250                         if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
251                                 break;
252                         yyleng--;
253                 }
254                 append_string(yytext, yyleng);
255                 if (!first_ts)
256                         first_ts = last_ts;
257         }
258         <<EOF>> {
259                 zconf_endhelp();
260                 return T_HELPTEXT;
261         }
262 }
263
264 <<EOF>> {
265         if (current_file) {
266                 zconf_endfile();
267                 return T_EOL;
268         }
269         fclose(yyin);
270         yyterminate();
271 }
272
273 %%
274 static char *expand_token(const char *in, size_t n)
275 {
276         char *out;
277         int c;
278         char c2;
279         const char *rest, *end;
280
281         new_string();
282         append_string(in, n);
283
284         /* get the whole line because we do not know the end of token. */
285         while ((c = input()) != EOF) {
286                 if (c == '\n') {
287                         unput(c);
288                         break;
289                 }
290                 c2 = c;
291                 append_string(&c2, 1);
292         }
293
294         rest = text;
295         out = expand_one_token(&rest);
296
297         /* push back unused characters to the input stream */
298         end = rest + strlen(rest);
299         while (end > rest)
300                 unput(*--end);
301
302         free(text);
303
304         return out;
305 }
306
307 static void append_expanded_string(const char *str)
308 {
309         const char *end;
310         char *res;
311
312         str++;
313
314         res = expand_dollar(&str);
315
316         /* push back unused characters to the input stream */
317         end = str + strlen(str);
318         while (end > str)
319                 unput(*--end);
320
321         append_string(res, strlen(res));
322
323         free(res);
324 }
325
326 void zconf_starthelp(void)
327 {
328         new_string();
329         last_ts = first_ts = 0;
330         BEGIN(HELP);
331 }
332
333 static void zconf_endhelp(void)
334 {
335         yylval.string = text;
336         BEGIN(INITIAL);
337 }
338
339
340 /*
341  * Try to open specified file with following names:
342  * ./name
343  * $(srctree)/name
344  * The latter is used when srctree is separate from objtree
345  * when compiling the firmware.
346  * Return NULL if file is not found.
347  */
348 FILE *zconf_fopen(const char *name)
349 {
350         char *env, fullname[PATH_MAX+1];
351         FILE *f;
352
353         f = fopen(name, "r");
354         if (!f && name != NULL && name[0] != '/') {
355                 env = getenv(SRCTREE);
356                 if (env) {
357                         sprintf(fullname, "%s/%s", env, name);
358                         f = fopen(fullname, "r");
359                 }
360         }
361         return f;
362 }
363
364 void zconf_initscan(const char *name)
365 {
366         yyin = zconf_fopen(name);
367         if (!yyin) {
368                 fprintf(stderr, "can't find file %s\n", name);
369                 exit(1);
370         }
371
372         current_buf = xmalloc(sizeof(*current_buf));
373         memset(current_buf, 0, sizeof(*current_buf));
374
375         current_file = file_lookup(name);
376         yylineno = 1;
377 }
378
379 void zconf_nextfile(const char *name)
380 {
381         struct file *iter;
382         struct file *file = file_lookup(name);
383         struct buffer *buf = xmalloc(sizeof(*buf));
384         memset(buf, 0, sizeof(*buf));
385
386         current_buf->state = YY_CURRENT_BUFFER;
387         yyin = zconf_fopen(file->name);
388         if (!yyin) {
389                 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
390                         zconf_curname(), zconf_lineno(), file->name);
391                 exit(1);
392         }
393         yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
394         buf->parent = current_buf;
395         current_buf = buf;
396
397         current_file->lineno = yylineno;
398         file->parent = current_file;
399
400         for (iter = current_file; iter; iter = iter->parent) {
401                 if (!strcmp(iter->name, file->name)) {
402                         fprintf(stderr,
403                                 "Recursive inclusion detected.\n"
404                                 "Inclusion path:\n"
405                                 "  current file : %s\n", file->name);
406                         iter = file;
407                         do {
408                                 iter = iter->parent;
409                                 fprintf(stderr, "  included from: %s:%d\n",
410                                         iter->name, iter->lineno - 1);
411                         } while (strcmp(iter->name, file->name));
412                         exit(1);
413                 }
414         }
415
416         yylineno = 1;
417         current_file = file;
418 }
419
420 static void zconf_endfile(void)
421 {
422         struct buffer *parent;
423
424         current_file = current_file->parent;
425         if (current_file)
426                 yylineno = current_file->lineno;
427
428         parent = current_buf->parent;
429         if (parent) {
430                 fclose(yyin);
431                 yy_delete_buffer(YY_CURRENT_BUFFER);
432                 yy_switch_to_buffer(parent->state);
433         }
434         free(current_buf);
435         current_buf = parent;
436 }
437
438 int zconf_lineno(void)
439 {
440         return current_pos.lineno;
441 }
442
443 const char *zconf_curname(void)
444 {
445         return current_pos.file ? current_pos.file->name : "<none>";
446 }