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