build: scripts/config - update to kconfig-v6.6.16
[openwrt/staging/stintel.git] / scripts / config / lexer.l
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 */
5 %option nostdinit noyywrap never-interactive full ecs
6 %option 8bit nodefault yylineno
7 %x ASSIGN_VAL HELP STRING
8 %{
9
10 #include <assert.h>
11 #include <limits.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <glob.h>
16 #include <libgen.h>
17
18 #include "lkc.h"
19 #include "parser.tab.h"
20
21 #define YY_DECL static int yylex1(void)
22
23 #define START_STRSIZE 16
24
25 static struct {
26 struct file *file;
27 int lineno;
28 } current_pos;
29
30 static int prev_prev_token = T_EOL;
31 static int prev_token = T_EOL;
32 static char *text;
33 static int text_size, text_asize;
34
35 struct buffer {
36 struct buffer *parent;
37 YY_BUFFER_STATE state;
38 };
39
40 static struct buffer *current_buf;
41
42 static int last_ts, first_ts;
43
44 static char *expand_token(const char *in, size_t n);
45 static void append_expanded_string(const char *in);
46 static void zconf_endhelp(void);
47 static void zconf_endfile(void);
48
49 static void new_string(void)
50 {
51 text = xmalloc(START_STRSIZE);
52 text_asize = START_STRSIZE;
53 text_size = 0;
54 *text = 0;
55 }
56
57 static void append_string(const char *str, int size)
58 {
59 int new_size = text_size + size + 1;
60 if (new_size > text_asize) {
61 new_size += START_STRSIZE - 1;
62 new_size &= -START_STRSIZE;
63 text = xrealloc(text, new_size);
64 text_asize = new_size;
65 }
66 memcpy(text + text_size, str, size);
67 text_size += size;
68 text[text_size] = 0;
69 }
70
71 static void alloc_string(const char *str, int size)
72 {
73 text = xmalloc(size + 1);
74 memcpy(text, str, size);
75 text[size] = 0;
76 }
77
78 static void warn_ignored_character(char chr)
79 {
80 fprintf(stderr,
81 "%s:%d:warning: ignoring unsupported character '%c'\n",
82 current_file->name, yylineno, chr);
83 }
84 %}
85
86 n [A-Za-z0-9_-]
87
88 %%
89 char open_quote = 0;
90
91 #.* /* ignore comment */
92 [ \t]* /* whitespaces */
93 \\\n /* escaped new line */
94 \n return T_EOL;
95 "bool" return T_BOOL;
96 "choice" return T_CHOICE;
97 "comment" return T_COMMENT;
98 "config" return T_CONFIG;
99 "def_bool" return T_DEF_BOOL;
100 "def_tristate" return T_DEF_TRISTATE;
101 "default" return T_DEFAULT;
102 "depends" return T_DEPENDS;
103 "endchoice" return T_ENDCHOICE;
104 "endif" return T_ENDIF;
105 "endmenu" return T_ENDMENU;
106 "help" return T_HELP;
107 "hex" return T_HEX;
108 "if" return T_IF;
109 "imply" return T_IMPLY;
110 "int" return T_INT;
111 "mainmenu" return T_MAINMENU;
112 "menu" return T_MENU;
113 "menuconfig" return T_MENUCONFIG;
114 "modules" return T_MODULES;
115 "on" return T_ON;
116 "optional" return T_OPTIONAL;
117 "prompt" return T_PROMPT;
118 "range" return T_RANGE;
119 "reset" return T_RESET;
120 "select" return T_SELECT;
121 "source" return T_SOURCE;
122 "string" return T_STRING;
123 "tristate" return T_TRISTATE;
124 "visible" return T_VISIBLE;
125 "||" return T_OR;
126 "&&" return T_AND;
127 "=" return T_EQUAL;
128 "!=" return T_UNEQUAL;
129 "<" return T_LESS;
130 "<=" return T_LESS_EQUAL;
131 ">" return T_GREATER;
132 ">=" return T_GREATER_EQUAL;
133 "!" return T_NOT;
134 "(" return T_OPEN_PAREN;
135 ")" return T_CLOSE_PAREN;
136 ":=" return T_COLON_EQUAL;
137 "+=" return T_PLUS_EQUAL;
138 \"|\' {
139 open_quote = yytext[0];
140 new_string();
141 BEGIN(STRING);
142 }
143 ({n}|[/.])+ {
144 alloc_string(yytext, yyleng);
145 yylval.string = text;
146 return T_WORD;
147 }
148 ({n}|[/.$])+ {
149 /* this token includes at least one '$' */
150 yylval.string = expand_token(yytext, yyleng);
151 if (strlen(yylval.string))
152 return T_WORD;
153 free(yylval.string);
154 }
155 . warn_ignored_character(*yytext);
156
157 <ASSIGN_VAL>{
158 [^[:blank:]\n]+.* {
159 alloc_string(yytext, yyleng);
160 yylval.string = text;
161 return T_ASSIGN_VAL;
162 }
163 \n { BEGIN(INITIAL); return T_EOL; }
164 .
165 }
166
167 <STRING>{
168 "$".* append_expanded_string(yytext);
169 [^$'"\\\n]+ {
170 append_string(yytext, yyleng);
171 }
172 \\.? {
173 append_string(yytext + 1, yyleng - 1);
174 }
175 \'|\" {
176 if (open_quote == yytext[0]) {
177 BEGIN(INITIAL);
178 yylval.string = text;
179 return T_WORD_QUOTE;
180 } else
181 append_string(yytext, 1);
182 }
183 \n {
184 fprintf(stderr,
185 "%s:%d:warning: multi-line strings not supported\n",
186 zconf_curname(), zconf_lineno());
187 unput('\n');
188 BEGIN(INITIAL);
189 yylval.string = text;
190 return T_WORD_QUOTE;
191 }
192 <<EOF>> {
193 BEGIN(INITIAL);
194 yylval.string = text;
195 return T_WORD_QUOTE;
196 }
197 }
198
199 <HELP>{
200 [ \t]+ {
201 int ts, i;
202
203 ts = 0;
204 for (i = 0; i < yyleng; i++) {
205 if (yytext[i] == '\t')
206 ts = (ts & ~7) + 8;
207 else
208 ts++;
209 }
210 last_ts = ts;
211 if (first_ts) {
212 if (ts < first_ts) {
213 zconf_endhelp();
214 return T_HELPTEXT;
215 }
216 ts -= first_ts;
217 while (ts > 8) {
218 append_string(" ", 8);
219 ts -= 8;
220 }
221 append_string(" ", ts);
222 }
223 }
224 [ \t]*\n/[^ \t\n] {
225 zconf_endhelp();
226 return T_HELPTEXT;
227 }
228 [ \t]*\n {
229 append_string("\n", 1);
230 }
231 [^ \t\n].* {
232 while (yyleng) {
233 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
234 break;
235 yyleng--;
236 }
237 append_string(yytext, yyleng);
238 if (!first_ts)
239 first_ts = last_ts;
240 }
241 <<EOF>> {
242 zconf_endhelp();
243 return T_HELPTEXT;
244 }
245 }
246
247 <<EOF>> {
248 BEGIN(INITIAL);
249
250 if (prev_token != T_EOL && prev_token != T_HELPTEXT)
251 fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
252 current_file->name, yylineno);
253
254 if (current_file) {
255 zconf_endfile();
256 return T_EOL;
257 }
258 fclose(yyin);
259 yyterminate();
260 }
261
262 %%
263
264 /* second stage lexer */
265 int yylex(void)
266 {
267 int token;
268
269 repeat:
270 token = yylex1();
271
272 if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
273 if (token == T_EOL) {
274 /* Do not pass unneeded T_EOL to the parser. */
275 goto repeat;
276 } else {
277 /*
278 * For the parser, update file/lineno at the first token
279 * of each statement. Generally, \n is a statement
280 * terminator in Kconfig, but it is not always true
281 * because \n could be escaped by a backslash.
282 */
283 current_pos.file = current_file;
284 current_pos.lineno = yylineno;
285 }
286 }
287
288 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
289 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
290 BEGIN(ASSIGN_VAL);
291
292 prev_prev_token = prev_token;
293 prev_token = token;
294
295 return token;
296 }
297
298 static char *expand_token(const char *in, size_t n)
299 {
300 char *out;
301 int c;
302 char c2;
303 const char *rest, *end;
304
305 new_string();
306 append_string(in, n);
307
308 /* get the whole line because we do not know the end of token. */
309 while ((c = input()) != EOF) {
310 if (c == '\n') {
311 unput(c);
312 break;
313 }
314 c2 = c;
315 append_string(&c2, 1);
316 }
317
318 rest = text;
319 out = expand_one_token(&rest);
320
321 /* push back unused characters to the input stream */
322 end = rest + strlen(rest);
323 while (end > rest)
324 unput(*--end);
325
326 free(text);
327
328 return out;
329 }
330
331 static void append_expanded_string(const char *str)
332 {
333 const char *end;
334 char *res;
335
336 str++;
337
338 res = expand_dollar(&str);
339
340 /* push back unused characters to the input stream */
341 end = str + strlen(str);
342 while (end > str)
343 unput(*--end);
344
345 append_string(res, strlen(res));
346
347 free(res);
348 }
349
350 void zconf_starthelp(void)
351 {
352 new_string();
353 last_ts = first_ts = 0;
354 BEGIN(HELP);
355 }
356
357 static void zconf_endhelp(void)
358 {
359 yylval.string = text;
360 BEGIN(INITIAL);
361 }
362
363
364 /*
365 * Try to open specified file with following names:
366 * ./name
367 * $(srctree)/name
368 * The latter is used when srctree is separate from objtree
369 * when compiling the kernel.
370 * Return NULL if file is not found.
371 */
372 FILE *zconf_fopen(const char *name)
373 {
374 char *env, fullname[PATH_MAX+1];
375 FILE *f;
376
377 f = fopen(name, "r");
378 if (!f && name != NULL && name[0] != '/') {
379 env = getenv(SRCTREE);
380 if (env) {
381 snprintf(fullname, sizeof(fullname),
382 "%s/%s", env, name);
383 f = fopen(fullname, "r");
384 }
385 }
386 return f;
387 }
388
389 void zconf_initscan(const char *name)
390 {
391 yyin = zconf_fopen(name);
392 if (!yyin) {
393 fprintf(stderr, "can't find file %s\n", name);
394 exit(1);
395 }
396
397 current_buf = xmalloc(sizeof(*current_buf));
398 memset(current_buf, 0, sizeof(*current_buf));
399
400 current_file = file_lookup(name);
401 yylineno = 1;
402 }
403
404 static void __zconf_nextfile(const char *name)
405 {
406 struct file *iter;
407 struct file *file = file_lookup(name);
408 struct buffer *buf = xmalloc(sizeof(*buf));
409 memset(buf, 0, sizeof(*buf));
410
411 current_buf->state = YY_CURRENT_BUFFER;
412 yyin = zconf_fopen(file->name);
413 if (!yyin) {
414 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
415 zconf_curname(), zconf_lineno(), file->name);
416 exit(1);
417 }
418 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
419 buf->parent = current_buf;
420 current_buf = buf;
421
422 current_file->lineno = yylineno;
423 file->parent = current_file;
424
425 for (iter = current_file; iter; iter = iter->parent) {
426 if (!strcmp(iter->name, file->name)) {
427 fprintf(stderr,
428 "Recursive inclusion detected.\n"
429 "Inclusion path:\n"
430 " current file : %s\n", file->name);
431 iter = file;
432 do {
433 iter = iter->parent;
434 fprintf(stderr, " included from: %s:%d\n",
435 iter->name, iter->lineno - 1);
436 } while (strcmp(iter->name, file->name));
437 exit(1);
438 }
439 }
440
441 yylineno = 1;
442 current_file = file;
443 }
444
445 void zconf_nextfile(const char *name)
446 {
447 glob_t gl;
448 int err;
449 int i;
450 char path[PATH_MAX], *p;
451
452 err = glob(name, GLOB_ERR | GLOB_MARK, NULL, &gl);
453
454 /* ignore wildcard patterns that return no result */
455 if (err == GLOB_NOMATCH && strchr(name, '*')) {
456 err = 0;
457 gl.gl_pathc = 0;
458 }
459
460 if (err == GLOB_NOMATCH) {
461 p = strdup(current_file->name);
462 if (p) {
463 snprintf(path, sizeof(path), "%s/%s", dirname(p), name);
464 err = glob(path, GLOB_ERR | GLOB_MARK, NULL, &gl);
465 free(p);
466 }
467 }
468
469 if (err) {
470 const char *reason = "unknown error";
471
472 switch (err) {
473 case GLOB_NOSPACE:
474 reason = "out of memory";
475 break;
476 case GLOB_ABORTED:
477 reason = "read error";
478 break;
479 case GLOB_NOMATCH:
480 reason = "No files found";
481 break;
482 default:
483 break;
484 }
485
486 printf("%s:%d: glob failed: %s \"%s\"\n", zconf_curname(), zconf_lineno(),
487 reason, name);
488
489 exit(1);
490 }
491
492 for (i = 0; i < gl.gl_pathc; i++)
493 __zconf_nextfile(gl.gl_pathv[i]);
494 }
495
496 static void zconf_endfile(void)
497 {
498 struct buffer *parent;
499
500 current_file = current_file->parent;
501 if (current_file)
502 yylineno = current_file->lineno;
503
504 parent = current_buf->parent;
505 if (parent) {
506 fclose(yyin);
507 yy_delete_buffer(YY_CURRENT_BUFFER);
508 yy_switch_to_buffer(parent->state);
509 }
510 free(current_buf);
511 current_buf = parent;
512 }
513
514 int zconf_lineno(void)
515 {
516 return current_pos.lineno;
517 }
518
519 const char *zconf_curname(void)
520 {
521 return current_pos.file ? current_pos.file->name : "<none>";
522 }