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