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 */
94 "allnoconfig_y" return T_ALLNOCONFIG_Y;
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 "defconfig_list" return T_DEFCONFIG_LIST;
103 "depends" return T_DEPENDS;
104 "endchoice" return T_ENDCHOICE;
105 "endif" return T_ENDIF;
106 "endmenu" return T_ENDMENU;
107 "help" return T_HELP;
110 "imply" return T_IMPLY;
112 "mainmenu" return T_MAINMENU;
113 "menu" return T_MENU;
114 "menuconfig" return T_MENUCONFIG;
115 "modules" return T_MODULES;
117 "option" return T_OPTION;
118 "optional" return T_OPTIONAL;
119 "prompt" return T_PROMPT;
120 "range" return T_RANGE;
121 "select" return T_SELECT;
122 "source" return T_SOURCE;
123 "string" return T_STRING;
124 "tristate" return T_TRISTATE;
125 "visible" return T_VISIBLE;
129 "!=" return T_UNEQUAL;
131 "<=" return T_LESS_EQUAL;
132 ">" return T_GREATER;
133 ">=" return T_GREATER_EQUAL;
135 "(" return T_OPEN_PAREN;
136 ")" return T_CLOSE_PAREN;
137 ":=" return T_COLON_EQUAL;
138 "+=" return T_PLUS_EQUAL;
145 alloc_string(yytext, yyleng);
146 yylval.string = text;
150 /* this token includes at least one '$' */
151 yylval.string = expand_token(yytext, yyleng);
152 if (strlen(yylval.string))
156 . warn_ignored_character(*yytext);
160 alloc_string(yytext, yyleng);
161 yylval.string = text;
164 \n { BEGIN(INITIAL); return T_EOL; }
169 "$".* append_expanded_string(yytext);
171 append_string(yytext, yyleng);
174 append_string(yytext + 1, yyleng - 1);
177 if (str == yytext[0]) {
179 yylval.string = text;
182 append_string(yytext, 1);
186 "%s:%d:warning: multi-line strings not supported\n",
187 zconf_curname(), zconf_lineno());
190 yylval.string = text;
195 yylval.string = text;
203 for (i = 0; i < yyleng; i++) {
204 if (yytext[i] == '\t')
217 append_string(" ", 8);
220 append_string(" ", ts);
228 append_string("\n", 1);
232 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
236 append_string(yytext, yyleng);
249 if (prev_token != T_EOL && prev_token != T_HELPTEXT)
250 fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
251 current_file->name, yylineno);
263 /* second stage lexer */
271 if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
272 if (token == T_EOL) {
273 /* Do not pass unneeded T_EOL to the parser. */
277 * For the parser, update file/lineno at the first token
278 * of each statement. Generally, \n is a statement
279 * terminator in Kconfig, but it is not always true
280 * because \n could be escaped by a backslash.
282 current_pos.file = current_file;
283 current_pos.lineno = yylineno;
287 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
288 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
291 prev_prev_token = prev_token;
297 static char *expand_token(const char *in, size_t n)
302 const char *rest, *end;
305 append_string(in, n);
307 /* get the whole line because we do not know the end of token. */
308 while ((c = input()) != EOF) {
314 append_string(&c2, 1);
318 out = expand_one_token(&rest);
320 /* push back unused characters to the input stream */
321 end = rest + strlen(rest);
330 static void append_expanded_string(const char *str)
337 res = expand_dollar(&str);
339 /* push back unused characters to the input stream */
340 end = str + strlen(str);
344 append_string(res, strlen(res));
349 void zconf_starthelp(void)
352 last_ts = first_ts = 0;
356 static void zconf_endhelp(void)
358 yylval.string = text;
364 * Try to open specified file with following names:
367 * The latter is used when srctree is separate from objtree
368 * when compiling the firmware.
369 * Return NULL if file is not found.
371 FILE *zconf_fopen(const char *name)
373 char *env, fullname[PATH_MAX+1];
376 f = fopen(name, "r");
377 if (!f && name != NULL && name[0] != '/') {
378 env = getenv(SRCTREE);
380 snprintf(fullname, sizeof(fullname),
382 f = fopen(fullname, "r");
388 void zconf_initscan(const char *name)
390 yyin = zconf_fopen(name);
392 fprintf(stderr, "can't find file %s\n", name);
396 current_buf = xmalloc(sizeof(*current_buf));
397 memset(current_buf, 0, sizeof(*current_buf));
399 current_file = file_lookup(name);
403 void zconf_nextfile(const char *name)
406 struct file *file = file_lookup(name);
407 struct buffer *buf = xmalloc(sizeof(*buf));
408 memset(buf, 0, sizeof(*buf));
410 current_buf->state = YY_CURRENT_BUFFER;
411 yyin = zconf_fopen(file->name);
413 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
414 zconf_curname(), zconf_lineno(), file->name);
417 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
418 buf->parent = current_buf;
421 current_file->lineno = yylineno;
422 file->parent = current_file;
424 for (iter = current_file; iter; iter = iter->parent) {
425 if (!strcmp(iter->name, file->name)) {
427 "Recursive inclusion detected.\n"
429 " current file : %s\n", file->name);
433 fprintf(stderr, " included from: %s:%d\n",
434 iter->name, iter->lineno - 1);
435 } while (strcmp(iter->name, file->name));
444 static void zconf_endfile(void)
446 struct buffer *parent;
448 current_file = current_file->parent;
450 yylineno = current_file->lineno;
452 parent = current_buf->parent;
455 yy_delete_buffer(YY_CURRENT_BUFFER);
456 yy_switch_to_buffer(parent->state);
459 current_buf = parent;
462 int zconf_lineno(void)
464 return current_pos.lineno;
467 const char *zconf_curname(void)
469 return current_pos.file ? current_pos.file->name : "<none>";