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