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