kconfig: refactor pattern matching in STRING state
[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]+     {
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         if (current_file) {
263                 zconf_endfile();
264                 return T_EOL;
265         }
266         fclose(yyin);
267         yyterminate();
268 }
269
270 %%
271 static char *expand_token(const char *in, size_t n)
272 {
273         char *out;
274         int c;
275         char c2;
276         const char *rest, *end;
277
278         new_string();
279         append_string(in, n);
280
281         /* get the whole line because we do not know the end of token. */
282         while ((c = input()) != EOF) {
283                 if (c == '\n') {
284                         unput(c);
285                         break;
286                 }
287                 c2 = c;
288                 append_string(&c2, 1);
289         }
290
291         rest = text;
292         out = expand_one_token(&rest);
293
294         /* push back unused characters to the input stream */
295         end = rest + strlen(rest);
296         while (end > rest)
297                 unput(*--end);
298
299         free(text);
300
301         return out;
302 }
303
304 static void append_expanded_string(const char *str)
305 {
306         const char *end;
307         char *res;
308
309         str++;
310
311         res = expand_dollar(&str);
312
313         /* push back unused characters to the input stream */
314         end = str + strlen(str);
315         while (end > str)
316                 unput(*--end);
317
318         append_string(res, strlen(res));
319
320         free(res);
321 }
322
323 void zconf_starthelp(void)
324 {
325         new_string();
326         last_ts = first_ts = 0;
327         BEGIN(HELP);
328 }
329
330 static void zconf_endhelp(void)
331 {
332         yylval.string = text;
333         BEGIN(INITIAL);
334 }
335
336
337 /*
338  * Try to open specified file with following names:
339  * ./name
340  * $(srctree)/name
341  * The latter is used when srctree is separate from objtree
342  * when compiling the firmware.
343  * Return NULL if file is not found.
344  */
345 FILE *zconf_fopen(const char *name)
346 {
347         char *env, fullname[PATH_MAX+1];
348         FILE *f;
349
350         f = fopen(name, "r");
351         if (!f && name != NULL && name[0] != '/') {
352                 env = getenv(SRCTREE);
353                 if (env) {
354                         sprintf(fullname, "%s/%s", env, name);
355                         f = fopen(fullname, "r");
356                 }
357         }
358         return f;
359 }
360
361 void zconf_initscan(const char *name)
362 {
363         yyin = zconf_fopen(name);
364         if (!yyin) {
365                 fprintf(stderr, "can't find file %s\n", name);
366                 exit(1);
367         }
368
369         current_buf = xmalloc(sizeof(*current_buf));
370         memset(current_buf, 0, sizeof(*current_buf));
371
372         current_file = file_lookup(name);
373         yylineno = 1;
374 }
375
376 void zconf_nextfile(const char *name)
377 {
378         struct file *iter;
379         struct file *file = file_lookup(name);
380         struct buffer *buf = xmalloc(sizeof(*buf));
381         memset(buf, 0, sizeof(*buf));
382
383         current_buf->state = YY_CURRENT_BUFFER;
384         yyin = zconf_fopen(file->name);
385         if (!yyin) {
386                 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
387                         zconf_curname(), zconf_lineno(), file->name);
388                 exit(1);
389         }
390         yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
391         buf->parent = current_buf;
392         current_buf = buf;
393
394         current_file->lineno = yylineno;
395         file->parent = current_file;
396
397         for (iter = current_file; iter; iter = iter->parent) {
398                 if (!strcmp(iter->name, file->name)) {
399                         fprintf(stderr,
400                                 "Recursive inclusion detected.\n"
401                                 "Inclusion path:\n"
402                                 "  current file : %s\n", file->name);
403                         iter = file;
404                         do {
405                                 iter = iter->parent;
406                                 fprintf(stderr, "  included from: %s:%d\n",
407                                         iter->name, iter->lineno - 1);
408                         } while (strcmp(iter->name, file->name));
409                         exit(1);
410                 }
411         }
412
413         yylineno = 1;
414         current_file = file;
415 }
416
417 static void zconf_endfile(void)
418 {
419         struct buffer *parent;
420
421         current_file = current_file->parent;
422         if (current_file)
423                 yylineno = current_file->lineno;
424
425         parent = current_buf->parent;
426         if (parent) {
427                 fclose(yyin);
428                 yy_delete_buffer(YY_CURRENT_BUFFER);
429                 yy_switch_to_buffer(parent->state);
430         }
431         free(current_buf);
432         current_buf = parent;
433 }
434
435 int zconf_lineno(void)
436 {
437         return current_pos.lineno;
438 }
439
440 const char *zconf_curname(void)
441 {
442         return current_pos.file ? current_pos.file->name : "<none>";
443 }