kconfig: use T_WORD instead of T_VARIABLE for variables
[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 YY_DECL         static int yylex1(void)
20
21 #define START_STRSIZE   16
22
23 static struct {
24         struct file *file;
25         int lineno;
26 } current_pos;
27
28 static int prev_token = T_EOL;
29 static char *text;
30 static int text_size, text_asize;
31
32 struct buffer {
33         struct buffer *parent;
34         YY_BUFFER_STATE state;
35 };
36
37 struct buffer *current_buf;
38
39 static int last_ts, first_ts;
40
41 static char *expand_token(const char *in, size_t n);
42 static void append_expanded_string(const char *in);
43 static void zconf_endhelp(void);
44 static void zconf_endfile(void);
45
46 static void new_string(void)
47 {
48         text = xmalloc(START_STRSIZE);
49         text_asize = START_STRSIZE;
50         text_size = 0;
51         *text = 0;
52 }
53
54 static void append_string(const char *str, int size)
55 {
56         int new_size = text_size + size + 1;
57         if (new_size > text_asize) {
58                 new_size += START_STRSIZE - 1;
59                 new_size &= -START_STRSIZE;
60                 text = xrealloc(text, new_size);
61                 text_asize = new_size;
62         }
63         memcpy(text + text_size, str, size);
64         text_size += size;
65         text[text_size] = 0;
66 }
67
68 static void alloc_string(const char *str, int size)
69 {
70         text = xmalloc(size + 1);
71         memcpy(text, str, size);
72         text[size] = 0;
73 }
74
75 static void warn_ignored_character(char chr)
76 {
77         fprintf(stderr,
78                 "%s:%d:warning: ignoring unsupported character '%c'\n",
79                 current_file->name, yylineno, chr);
80 }
81 %}
82
83 n       [A-Za-z0-9_-]
84
85 %%
86         int str = 0;
87         int ts, i;
88
89 [ \t]*#.*\n     |
90 [ \t]*\n        {
91         return T_EOL;
92 }
93 [ \t]*#.*
94 .       {
95         unput(yytext[0]);
96         BEGIN(COMMAND);
97 }
98
99
100 <COMMAND>{
101         {n}+    {
102                 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
103                 current_pos.file = current_file;
104                 current_pos.lineno = yylineno;
105                 if (id && id->flags & TF_COMMAND) {
106                         BEGIN(PARAM);
107                         yylval.id = id;
108                         return id->token;
109                 }
110                 alloc_string(yytext, yyleng);
111                 yylval.string = text;
112                 return T_WORD;
113         }
114         ({n}|$)+        {
115                 /* this token includes at least one '$' */
116                 yylval.string = expand_token(yytext, yyleng);
117                 if (strlen(yylval.string))
118                         return T_WORD;
119                 free(yylval.string);
120         }
121         "="     { BEGIN(ASSIGN_VAL); return T_EQUAL; }
122         ":="    { BEGIN(ASSIGN_VAL); return T_COLON_EQUAL; }
123         "+="    { BEGIN(ASSIGN_VAL); return T_PLUS_EQUAL; }
124         [[:blank:]]+
125         .       warn_ignored_character(*yytext);
126         \n      {
127                 BEGIN(INITIAL);
128                 return T_EOL;
129         }
130 }
131
132 <ASSIGN_VAL>{
133         [^[:blank:]\n]+.*       {
134                 alloc_string(yytext, yyleng);
135                 yylval.string = text;
136                 return T_ASSIGN_VAL;
137         }
138         \n      { BEGIN(INITIAL); return T_EOL; }
139         .
140 }
141
142 <PARAM>{
143         "modules"               return T_MODULES;
144         "defconfig_list"        return T_DEFCONFIG_LIST;
145         "allnoconfig_y"         return T_ALLNOCONFIG_Y;
146         "&&"    return T_AND;
147         "||"    return T_OR;
148         "("     return T_OPEN_PAREN;
149         ")"     return T_CLOSE_PAREN;
150         "!"     return T_NOT;
151         "="     return T_EQUAL;
152         "!="    return T_UNEQUAL;
153         "<="    return T_LESS_EQUAL;
154         ">="    return T_GREATER_EQUAL;
155         "<"     return T_LESS;
156         ">"     return T_GREATER;
157         \"|\'   {
158                 str = yytext[0];
159                 new_string();
160                 BEGIN(STRING);
161         }
162         \n      BEGIN(INITIAL); return T_EOL;
163         ({n}|[/.])+     {
164                 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
165                 if (id && id->flags & TF_PARAM) {
166                         yylval.id = id;
167                         return id->token;
168                 }
169                 alloc_string(yytext, yyleng);
170                 yylval.string = text;
171                 return T_WORD;
172         }
173         ({n}|[/.$])+    {
174                 /* this token includes at least one '$' */
175                 yylval.string = expand_token(yytext, yyleng);
176                 if (strlen(yylval.string))
177                         return T_WORD;
178                 free(yylval.string);
179         }
180         #.*     /* comment */
181         \\\n    ;
182         [[:blank:]]+
183         .       warn_ignored_character(*yytext);
184 }
185
186 <STRING>{
187         "$".*   append_expanded_string(yytext);
188         [^$'"\\\n]+     {
189                 append_string(yytext, yyleng);
190         }
191         \\.?    {
192                 append_string(yytext + 1, yyleng - 1);
193         }
194         \'|\"   {
195                 if (str == yytext[0]) {
196                         BEGIN(PARAM);
197                         yylval.string = text;
198                         return T_WORD_QUOTE;
199                 } else
200                         append_string(yytext, 1);
201         }
202         \n      {
203                 fprintf(stderr,
204                         "%s:%d:warning: multi-line strings not supported\n",
205                         zconf_curname(), zconf_lineno());
206                 unput('\n');
207                 BEGIN(INITIAL);
208                 yylval.string = text;
209                 return T_WORD_QUOTE;
210         }
211         <<EOF>> {
212                 BEGIN(INITIAL);
213                 yylval.string = text;
214                 return T_WORD_QUOTE;
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         BEGIN(INITIAL);
266
267         if (prev_token != T_EOL && prev_token != T_HELPTEXT)
268                 fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
269                         current_file->name, yylineno);
270
271         if (current_file) {
272                 zconf_endfile();
273                 return T_EOL;
274         }
275         fclose(yyin);
276         yyterminate();
277 }
278
279 %%
280
281 /* second stage lexer */
282 int yylex(void)
283 {
284         int token;
285
286 repeat:
287         token = yylex1();
288
289         /* Do not pass unneeded T_EOL to the parser. */
290         if ((prev_token == T_EOL || prev_token == T_HELPTEXT) && token == T_EOL)
291                 goto repeat;
292
293         prev_token = token;
294
295         return token;
296 }
297
298 static char *expand_token(const char *in, size_t n)
299 {
300         char *out;
301         int c;
302         char c2;
303         const char *rest, *end;
304
305         new_string();
306         append_string(in, n);
307
308         /* get the whole line because we do not know the end of token. */
309         while ((c = input()) != EOF) {
310                 if (c == '\n') {
311                         unput(c);
312                         break;
313                 }
314                 c2 = c;
315                 append_string(&c2, 1);
316         }
317
318         rest = text;
319         out = expand_one_token(&rest);
320
321         /* push back unused characters to the input stream */
322         end = rest + strlen(rest);
323         while (end > rest)
324                 unput(*--end);
325
326         free(text);
327
328         return out;
329 }
330
331 static void append_expanded_string(const char *str)
332 {
333         const char *end;
334         char *res;
335
336         str++;
337
338         res = expand_dollar(&str);
339
340         /* push back unused characters to the input stream */
341         end = str + strlen(str);
342         while (end > str)
343                 unput(*--end);
344
345         append_string(res, strlen(res));
346
347         free(res);
348 }
349
350 void zconf_starthelp(void)
351 {
352         new_string();
353         last_ts = first_ts = 0;
354         BEGIN(HELP);
355 }
356
357 static void zconf_endhelp(void)
358 {
359         yylval.string = text;
360         BEGIN(INITIAL);
361 }
362
363
364 /*
365  * Try to open specified file with following names:
366  * ./name
367  * $(srctree)/name
368  * The latter is used when srctree is separate from objtree
369  * when compiling the firmware.
370  * Return NULL if file is not found.
371  */
372 FILE *zconf_fopen(const char *name)
373 {
374         char *env, fullname[PATH_MAX+1];
375         FILE *f;
376
377         f = fopen(name, "r");
378         if (!f && name != NULL && name[0] != '/') {
379                 env = getenv(SRCTREE);
380                 if (env) {
381                         sprintf(fullname, "%s/%s", env, name);
382                         f = fopen(fullname, "r");
383                 }
384         }
385         return f;
386 }
387
388 void zconf_initscan(const char *name)
389 {
390         yyin = zconf_fopen(name);
391         if (!yyin) {
392                 fprintf(stderr, "can't find file %s\n", name);
393                 exit(1);
394         }
395
396         current_buf = xmalloc(sizeof(*current_buf));
397         memset(current_buf, 0, sizeof(*current_buf));
398
399         current_file = file_lookup(name);
400         yylineno = 1;
401 }
402
403 void zconf_nextfile(const char *name)
404 {
405         struct file *iter;
406         struct file *file = file_lookup(name);
407         struct buffer *buf = xmalloc(sizeof(*buf));
408         memset(buf, 0, sizeof(*buf));
409
410         current_buf->state = YY_CURRENT_BUFFER;
411         yyin = zconf_fopen(file->name);
412         if (!yyin) {
413                 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
414                         zconf_curname(), zconf_lineno(), file->name);
415                 exit(1);
416         }
417         yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
418         buf->parent = current_buf;
419         current_buf = buf;
420
421         current_file->lineno = yylineno;
422         file->parent = current_file;
423
424         for (iter = current_file; iter; iter = iter->parent) {
425                 if (!strcmp(iter->name, file->name)) {
426                         fprintf(stderr,
427                                 "Recursive inclusion detected.\n"
428                                 "Inclusion path:\n"
429                                 "  current file : %s\n", file->name);
430                         iter = file;
431                         do {
432                                 iter = iter->parent;
433                                 fprintf(stderr, "  included from: %s:%d\n",
434                                         iter->name, iter->lineno - 1);
435                         } while (strcmp(iter->name, file->name));
436                         exit(1);
437                 }
438         }
439
440         yylineno = 1;
441         current_file = file;
442 }
443
444 static void zconf_endfile(void)
445 {
446         struct buffer *parent;
447
448         current_file = current_file->parent;
449         if (current_file)
450                 yylineno = current_file->lineno;
451
452         parent = current_buf->parent;
453         if (parent) {
454                 fclose(yyin);
455                 yy_delete_buffer(YY_CURRENT_BUFFER);
456                 yy_switch_to_buffer(parent->state);
457         }
458         free(current_buf);
459         current_buf = parent;
460 }
461
462 int zconf_lineno(void)
463 {
464         return current_pos.lineno;
465 }
466
467 const char *zconf_curname(void)
468 {
469         return current_pos.file ? current_pos.file->name : "<none>";
470 }