1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
5 %option nostdinit noyywrap never-interactive full ecs
6 %option 8bit nodefault yylineno
7 %x ASSIGN_VAL HELP STRING
17 #include "parser.tab.h"
19 #define YY_DECL static int yylex1(void)
21 #define START_STRSIZE 16
28 static int prev_prev_token = T_EOL;
29 static int prev_token = T_EOL;
31 static int text_size, text_asize;
34 struct buffer *parent;
35 YY_BUFFER_STATE state;
38 static struct buffer *current_buf;
40 static int last_ts, first_ts;
42 static char *expand_token(const char *in, size_t n);
43 static void append_expanded_string(const char *in);
44 static void zconf_endhelp(void);
45 static void zconf_endfile(void);
47 static void new_string(void)
49 text = xmalloc(START_STRSIZE);
50 text_asize = START_STRSIZE;
55 static void append_string(const char *str, int size)
57 int new_size = text_size + size + 1;
58 if (new_size > text_asize) {
59 new_size += START_STRSIZE - 1;
60 new_size &= -START_STRSIZE;
61 text = xrealloc(text, new_size);
62 text_asize = new_size;
64 memcpy(text + text_size, str, size);
69 static void alloc_string(const char *str, int size)
71 text = xmalloc(size + 1);
72 memcpy(text, str, size);
76 static void warn_ignored_character(char chr)
79 "%s:%d:warning: ignoring unsupported character '%c'\n",
80 current_file->name, yylineno, chr);
89 #.* /* ignore comment */
90 [ \t]* /* whitespaces */
91 \\\n /* escaped new line */
94 "choice" return T_CHOICE;
95 "comment" return T_COMMENT;
96 "config" return T_CONFIG;
97 "def_bool" return T_DEF_BOOL;
98 "def_tristate" return T_DEF_TRISTATE;
99 "default" return T_DEFAULT;
100 "depends" return T_DEPENDS;
101 "endchoice" return T_ENDCHOICE;
102 "endif" return T_ENDIF;
103 "endmenu" return T_ENDMENU;
104 "help" return T_HELP;
107 "imply" return T_IMPLY;
109 "mainmenu" return T_MAINMENU;
110 "menu" return T_MENU;
111 "menuconfig" return T_MENUCONFIG;
112 "modules" return T_MODULES;
114 "optional" return T_OPTIONAL;
115 "prompt" return T_PROMPT;
116 "range" return T_RANGE;
117 "select" return T_SELECT;
118 "source" return T_SOURCE;
119 "string" return T_STRING;
120 "tristate" return T_TRISTATE;
121 "visible" return T_VISIBLE;
125 "!=" return T_UNEQUAL;
127 "<=" return T_LESS_EQUAL;
128 ">" return T_GREATER;
129 ">=" return T_GREATER_EQUAL;
131 "(" return T_OPEN_PAREN;
132 ")" return T_CLOSE_PAREN;
133 ":=" return T_COLON_EQUAL;
134 "+=" return T_PLUS_EQUAL;
136 open_quote = yytext[0];
141 alloc_string(yytext, yyleng);
142 yylval.string = text;
146 /* this token includes at least one '$' */
147 yylval.string = expand_token(yytext, yyleng);
148 if (strlen(yylval.string))
152 . warn_ignored_character(*yytext);
156 alloc_string(yytext, yyleng);
157 yylval.string = text;
160 \n { BEGIN(INITIAL); return T_EOL; }
165 "$".* append_expanded_string(yytext);
167 append_string(yytext, yyleng);
170 append_string(yytext + 1, yyleng - 1);
173 if (open_quote == yytext[0]) {
175 yylval.string = text;
178 append_string(yytext, 1);
182 "%s:%d:warning: multi-line strings not supported\n",
183 zconf_curname(), zconf_lineno());
186 yylval.string = text;
191 yylval.string = text;
201 for (i = 0; i < yyleng; i++) {
202 if (yytext[i] == '\t')
215 append_string(" ", 8);
218 append_string(" ", ts);
226 append_string("\n", 1);
230 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
234 append_string(yytext, yyleng);
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);
261 /* second stage lexer */
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. */
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.
280 current_pos.file = current_file;
281 current_pos.lineno = yylineno;
285 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
286 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
289 prev_prev_token = prev_token;
295 static char *expand_token(const char *in, size_t n)
300 const char *rest, *end;
303 append_string(in, n);
306 * get the whole line because we do not know the end of token.
307 * input() returns 0 (not EOF!) when it reachs the end of file.
309 while ((c = input()) != 0) {
315 append_string(&c2, 1);
319 out = expand_one_token(&rest);
321 /* push back unused characters to the input stream */
322 end = rest + strlen(rest);
331 static void append_expanded_string(const char *str)
338 res = expand_dollar(&str);
340 /* push back unused characters to the input stream */
341 end = str + strlen(str);
345 append_string(res, strlen(res));
350 void zconf_starthelp(void)
353 last_ts = first_ts = 0;
357 static void zconf_endhelp(void)
359 yylval.string = text;
365 * Try to open specified file with following names:
368 * The latter is used when srctree is separate from objtree
369 * when compiling the kernel.
370 * Return NULL if file is not found.
372 FILE *zconf_fopen(const char *name)
374 char *env, fullname[PATH_MAX+1];
377 f = fopen(name, "r");
378 if (!f && name != NULL && name[0] != '/') {
379 env = getenv(SRCTREE);
381 snprintf(fullname, sizeof(fullname),
383 f = fopen(fullname, "r");
389 void zconf_initscan(const char *name)
391 yyin = zconf_fopen(name);
393 fprintf(stderr, "can't find file %s\n", name);
397 current_buf = xmalloc(sizeof(*current_buf));
398 memset(current_buf, 0, sizeof(*current_buf));
400 current_file = file_lookup(name);
404 void zconf_nextfile(const char *name)
407 struct file *file = file_lookup(name);
408 struct buffer *buf = xmalloc(sizeof(*buf));
409 memset(buf, 0, sizeof(*buf));
411 current_buf->state = YY_CURRENT_BUFFER;
412 yyin = zconf_fopen(file->name);
414 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
415 zconf_curname(), zconf_lineno(), file->name);
418 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
419 buf->parent = current_buf;
422 current_file->lineno = yylineno;
423 file->parent = current_file;
425 for (iter = current_file; iter; iter = iter->parent) {
426 if (!strcmp(iter->name, file->name)) {
428 "Recursive inclusion detected.\n"
430 " current file : %s\n", file->name);
434 fprintf(stderr, " included from: %s:%d\n",
435 iter->name, iter->lineno - 1);
436 } while (strcmp(iter->name, file->name));
445 static void zconf_endfile(void)
447 struct buffer *parent;
449 current_file = current_file->parent;
451 yylineno = current_file->lineno;
453 parent = current_buf->parent;
456 yy_delete_buffer(YY_CURRENT_BUFFER);
457 yy_switch_to_buffer(parent->state);
460 current_buf = parent;
463 int zconf_lineno(void)
465 return current_pos.lineno;
468 const char *zconf_curname(void)
470 return current_pos.file ? current_pos.file->name : "<none>";