blob: dcb5648e2651058eeb3e60deb58bf1d370b98d82 [file] [log] [blame]
Patrick Georgi53ea1d42019-11-22 16:55:58 +01001/* SPDX-License-Identifier: GPL-2.0 */
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 <glob.h>
12#include <limits.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include "lkc.h"
18#include "parser.tab.h"
19
20#define YY_DECL static int yylex1(void)
21
22#define START_STRSIZE 16
23
24static struct {
25 struct file *file;
26 int lineno;
27} current_pos;
28
29static int prev_prev_token = T_EOL;
30static int prev_token = T_EOL;
31static char *text;
32static int text_size, text_asize;
33
34struct buffer {
35 struct buffer *parent;
36 YY_BUFFER_STATE state;
37};
38
39static struct buffer *current_buf;
40
41static int last_ts, first_ts;
42
43static char *expand_token(const char *in, size_t n);
44static void append_expanded_string(const char *in);
45static void zconf_endhelp(void);
46static void zconf_endfile(void);
47
48static void new_string(void)
49{
50 text = xmalloc(START_STRSIZE);
51 text_asize = START_STRSIZE;
52 text_size = 0;
53 *text = 0;
54}
55
56static void append_string(const char *str, int size)
57{
58 int new_size = text_size + size + 1;
59 if (new_size > text_asize) {
60 new_size += START_STRSIZE - 1;
61 new_size &= -START_STRSIZE;
62 text = xrealloc(text, new_size);
63 text_asize = new_size;
64 }
65 memcpy(text + text_size, str, size);
66 text_size += size;
67 text[text_size] = 0;
68}
69
70static void alloc_string(const char *str, int size)
71{
72 text = xmalloc(size + 1);
73 memcpy(text, str, size);
74 text[size] = 0;
75}
76
77static void warn_ignored_character(char chr)
78{
79 fprintf(stderr,
80 "%s:%d:warning: ignoring unsupported character '%c'\n",
81 current_file->name, yylineno, chr);
82}
83%}
84
85n [A-Za-z0-9_-]
86
87%%
88 int str = 0;
89 int ts, i;
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"select" return T_SELECT;
120"source" return T_SOURCE;
121"string" return T_STRING;
122"tristate" return T_TRISTATE;
123"visible" return T_VISIBLE;
124"||" return T_OR;
125"&&" return T_AND;
126"=" return T_EQUAL;
127"!=" return T_UNEQUAL;
128"<" return T_LESS;
129"<=" return T_LESS_EQUAL;
130">" return T_GREATER;
131">=" return T_GREATER_EQUAL;
132"!" return T_NOT;
133"(" return T_OPEN_PAREN;
134")" return T_CLOSE_PAREN;
135":=" return T_COLON_EQUAL;
136"+=" return T_PLUS_EQUAL;
137\"|\' {
138 str = yytext[0];
139 new_string();
140 BEGIN(STRING);
141 }
142{n}+ {
143 alloc_string(yytext, yyleng);
144 yylval.string = text;
145 return T_WORD;
146 }
147({n}|$)+ {
148 /* this token includes at least one '$' */
149 yylval.string = expand_token(yytext, yyleng);
150 if (strlen(yylval.string))
151 return T_WORD;
152 free(yylval.string);
153 }
154. warn_ignored_character(*yytext);
155
156<ASSIGN_VAL>{
157 [^[:blank:]\n]+.* {
158 alloc_string(yytext, yyleng);
159 yylval.string = text;
160 return T_ASSIGN_VAL;
161 }
162 \n { BEGIN(INITIAL); return T_EOL; }
163 .
164}
165
166<STRING>{
167 "$".* append_expanded_string(yytext);
168 [^$'"\\\n]+ {
169 append_string(yytext, yyleng);
170 }
171 \\.? {
172 append_string(yytext + 1, yyleng - 1);
173 }
174 \'|\" {
175 if (str == yytext[0]) {
176 BEGIN(INITIAL);
177 yylval.string = text;
178 return T_WORD_QUOTE;
179 } else
180 append_string(yytext, 1);
181 }
182 \n {
183 fprintf(stderr,
184 "%s:%d:warning: multi-line strings not supported\n",
185 zconf_curname(), zconf_lineno());
186 unput('\n');
187 BEGIN(INITIAL);
188 yylval.string = text;
189 return T_WORD_QUOTE;
190 }
191 <<EOF>> {
192 BEGIN(INITIAL);
193 yylval.string = text;
194 return T_WORD_QUOTE;
195 }
196}
197
198<HELP>{
199 [ \t]+ {
200 ts = 0;
201 for (i = 0; i < yyleng; i++) {
202 if (yytext[i] == '\t')
203 ts = (ts & ~7) + 8;
204 else
205 ts++;
206 }
207 last_ts = ts;
208 if (first_ts) {
209 if (ts < first_ts) {
210 zconf_endhelp();
211 return T_HELPTEXT;
212 }
213 ts -= first_ts;
214 while (ts > 8) {
215 append_string(" ", 8);
216 ts -= 8;
217 }
218 append_string(" ", ts);
219 }
220 }
221 [ \t]*\n/[^ \t\n] {
222 zconf_endhelp();
223 return T_HELPTEXT;
224 }
225 [ \t]*\n {
226 append_string("\n", 1);
227 }
228 [^ \t\n].* {
229 while (yyleng) {
230 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
231 break;
232 yyleng--;
233 }
234 append_string(yytext, yyleng);
235 if (!first_ts)
236 first_ts = last_ts;
237 }
238 <<EOF>> {
239 zconf_endhelp();
240 return T_HELPTEXT;
241 }
242}
243
244<<EOF>> {
245 BEGIN(INITIAL);
246
247 if (prev_token != T_EOL && prev_token != T_HELPTEXT)
248 fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
249 current_file->name, yylineno);
250
251 if (current_file) {
252 zconf_endfile();
253 return T_EOL;
254 }
255 fclose(yyin);
256 yyterminate();
257}
258
259%%
260
261/* second stage lexer */
262int yylex(void)
263{
264 int token;
265
266repeat:
267 token = yylex1();
268
269 if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
270 if (token == T_EOL) {
271 /* Do not pass unneeded T_EOL to the parser. */
272 goto repeat;
273 } else {
274 /*
275 * For the parser, update file/lineno at the first token
276 * of each statement. Generally, \n is a statement
277 * terminator in Kconfig, but it is not always true
278 * because \n could be escaped by a backslash.
279 */
280 current_pos.file = current_file;
281 current_pos.lineno = yylineno;
282 }
283 }
284
285 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
286 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
287 BEGIN(ASSIGN_VAL);
288
289 prev_prev_token = prev_token;
290 prev_token = token;
291
292 return token;
293}
294
295static char *expand_token(const char *in, size_t n)
296{
297 char *out;
298 int c;
299 char c2;
300 const char *rest, *end;
301
302 new_string();
303 append_string(in, n);
304
305 /* get the whole line because we do not know the end of token. */
306 while ((c = input()) != EOF) {
307 if (c == '\n') {
308 unput(c);
309 break;
310 }
311 c2 = c;
312 append_string(&c2, 1);
313 }
314
315 rest = text;
316 out = expand_one_token(&rest);
317
318 /* push back unused characters to the input stream */
319 end = rest + strlen(rest);
320 while (end > rest)
321 unput(*--end);
322
323 free(text);
324
325 return out;
326}
327
328static void append_expanded_string(const char *str)
329{
330 const char *end;
331 char *res;
332
333 str++;
334
335 res = expand_dollar(&str);
336
337 /* push back unused characters to the input stream */
338 end = str + strlen(str);
339 while (end > str)
340 unput(*--end);
341
342 append_string(res, strlen(res));
343
344 free(res);
345}
346
347void zconf_starthelp(void)
348{
349 new_string();
350 last_ts = first_ts = 0;
351 BEGIN(HELP);
352}
353
354static void zconf_endhelp(void)
355{
356 yylval.string = text;
357 BEGIN(INITIAL);
358}
359
360
361/*
362 * Try to open specified file with following names:
363 * ./name
364 * $(srctree)/name
365 * The latter is used when srctree is separate from objtree
366 * when compiling the kernel.
367 * Return NULL if file is not found.
368 */
369FILE *zconf_fopen(const char *name)
370{
371 char *env, fullname[PATH_MAX+1];
372 FILE *f;
373
374 f = fopen(name, "r");
375 if (!f && name != NULL && name[0] != '/') {
376 env = getenv(SRCTREE);
377 if (env) {
378 snprintf(fullname, sizeof(fullname),
379 "%s/%s", env, name);
380 f = fopen(fullname, "r");
381 }
382 }
383 return f;
384}
385
386void 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
401void 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
442void zconf_nextfiles(const char *wildcard)
443{
444 glob_t g;
445 char **w;
446 int i;
447
448 if (glob(wildcard, 0, NULL, &g) != 0) {
449 return;
450 }
451 if (g.gl_pathv == NULL) {
452 globfree(&g);
453 return;
454 }
455
456 /* working through files backwards, since
457 * we're first pushing them on a stack
458 * before actually handling them.
459 */
460 for (i = g.gl_pathc; i > 0; i--) {
461 w = &g.gl_pathv[i - 1];
462 zconf_nextfile(*w);
463 }
464
465 globfree(&g);
466}
467
468static void zconf_endfile(void)
469{
470 struct buffer *parent;
471
472 current_file = current_file->parent;
473 if (current_file)
474 yylineno = current_file->lineno;
475
476 parent = current_buf->parent;
477 if (parent) {
478 fclose(yyin);
479 yy_delete_buffer(YY_CURRENT_BUFFER);
480 yy_switch_to_buffer(parent->state);
481 }
482 free(current_buf);
483 current_buf = parent;
484}
485
486int zconf_lineno(void)
487{
488 return current_pos.lineno;
489}
490
491const char *zconf_curname(void)
492{
493 return current_pos.file ? current_pos.file->name : "<none>";
494}