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);
90 #.* /* ignore comment */
91 [ \t]* /* whitespaces */
92 \\\n /* escaped new line */
95 "choice" return T_CHOICE;
96 "comment" return T_COMMENT;
97 "config" return T_CONFIG;
98 "def_bool" return T_DEF_BOOL;
99 "def_tristate" return T_DEF_TRISTATE;
100 "default" return T_DEFAULT;
101 "depends" return T_DEPENDS;
102 "endchoice" return T_ENDCHOICE;
103 "endif" return T_ENDIF;
104 "endmenu" return T_ENDMENU;
105 "help" return T_HELP;
108 "imply" return T_IMPLY;
110 "mainmenu" return T_MAINMENU;
111 "menu" return T_MENU;
112 "menuconfig" return T_MENUCONFIG;
113 "modules" return T_MODULES;
115 "optional" return T_OPTIONAL;
116 "prompt" return T_PROMPT;
117 "range" return T_RANGE;
118 "select" return T_SELECT;
119 "source" return T_SOURCE;
120 "string" return T_STRING;
121 "tristate" return T_TRISTATE;
122 "visible" return T_VISIBLE;
126 "!=" return T_UNEQUAL;
128 "<=" return T_LESS_EQUAL;
129 ">" return T_GREATER;
130 ">=" return T_GREATER_EQUAL;
132 "(" return T_OPEN_PAREN;
133 ")" return T_CLOSE_PAREN;
134 ":=" return T_COLON_EQUAL;
135 "+=" return T_PLUS_EQUAL;
142 alloc_string(yytext, yyleng);
143 yylval.string = text;
147 /* this token includes at least one '$' */
148 yylval.string = expand_token(yytext, yyleng);
149 if (strlen(yylval.string))
153 . warn_ignored_character(*yytext);
157 alloc_string(yytext, yyleng);
158 yylval.string = text;
161 \n { BEGIN(INITIAL); return T_EOL; }
166 "$".* append_expanded_string(yytext);
168 append_string(yytext, yyleng);
171 append_string(yytext + 1, yyleng - 1);
174 if (str == yytext[0]) {
176 yylval.string = text;
179 append_string(yytext, 1);
183 "%s:%d:warning: multi-line strings not supported\n",
184 zconf_curname(), zconf_lineno());
187 yylval.string = text;
192 yylval.string = text;
200 for (i = 0; i < yyleng; i++) {
201 if (yytext[i] == '\t')
214 append_string(" ", 8);
217 append_string(" ", ts);
225 append_string("\n", 1);
229 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
233 append_string(yytext, yyleng);
246 if (prev_token != T_EOL && prev_token != T_HELPTEXT)
247 fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
248 current_file->name, yylineno);
260 /* second stage lexer */
268 if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
269 if (token == T_EOL) {
270 /* Do not pass unneeded T_EOL to the parser. */
274 * For the parser, update file/lineno at the first token
275 * of each statement. Generally, \n is a statement
276 * terminator in Kconfig, but it is not always true
277 * because \n could be escaped by a backslash.
279 current_pos.file = current_file;
280 current_pos.lineno = yylineno;
284 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
285 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
288 prev_prev_token = prev_token;
294 static char *expand_token(const char *in, size_t n)
299 const char *rest, *end;
302 append_string(in, n);
304 /* get the whole line because we do not know the end of token. */
305 while ((c = input()) != EOF) {
311 append_string(&c2, 1);
315 out = expand_one_token(&rest);
317 /* push back unused characters to the input stream */
318 end = rest + strlen(rest);
327 static void append_expanded_string(const char *str)
334 res = expand_dollar(&str);
336 /* push back unused characters to the input stream */
337 end = str + strlen(str);
341 append_string(res, strlen(res));
346 void zconf_starthelp(void)
349 last_ts = first_ts = 0;
353 static void zconf_endhelp(void)
355 yylval.string = text;
361 * Try to open specified file with following names:
364 * The latter is used when srctree is separate from objtree
365 * when compiling the kernel.
366 * Return NULL if file is not found.
368 FILE *zconf_fopen(const char *name)
370 char *env, fullname[PATH_MAX+1];
373 f = fopen(name, "r");
374 if (!f && name != NULL && name[0] != '/') {
375 env = getenv(SRCTREE);
377 snprintf(fullname, sizeof(fullname),
379 f = fopen(fullname, "r");
385 void zconf_initscan(const char *name)
387 yyin = zconf_fopen(name);
389 fprintf(stderr, "can't find file %s\n", name);
393 current_buf = xmalloc(sizeof(*current_buf));
394 memset(current_buf, 0, sizeof(*current_buf));
396 current_file = file_lookup(name);
400 void zconf_nextfile(const char *name)
403 struct file *file = file_lookup(name);
404 struct buffer *buf = xmalloc(sizeof(*buf));
405 memset(buf, 0, sizeof(*buf));
407 current_buf->state = YY_CURRENT_BUFFER;
408 yyin = zconf_fopen(file->name);
410 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
411 zconf_curname(), zconf_lineno(), file->name);
414 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
415 buf->parent = current_buf;
418 current_file->lineno = yylineno;
419 file->parent = current_file;
421 for (iter = current_file; iter; iter = iter->parent) {
422 if (!strcmp(iter->name, file->name)) {
424 "Recursive inclusion detected.\n"
426 " current file : %s\n", file->name);
430 fprintf(stderr, " included from: %s:%d\n",
431 iter->name, iter->lineno - 1);
432 } while (strcmp(iter->name, file->name));
441 static void zconf_endfile(void)
443 struct buffer *parent;
445 current_file = current_file->parent;
447 yylineno = current_file->lineno;
449 parent = current_buf->parent;
452 yy_delete_buffer(YY_CURRENT_BUFFER);
453 yy_switch_to_buffer(parent->state);
456 current_buf = parent;
459 int zconf_lineno(void)
461 return current_pos.lineno;
464 const char *zconf_curname(void)
466 return current_pos.file ? current_pos.file->name : "<none>";