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
18 #include "parser.tab.h"
20 #define YY_DECL static int yylex1(void)
22 #define START_STRSIZE 16
29 static int prev_prev_token = T_EOL;
30 static int prev_token = T_EOL;
32 static int text_size, text_asize;
35 struct buffer *parent;
36 YY_BUFFER_STATE state;
39 struct buffer *current_buf;
41 static int last_ts, first_ts;
43 static char *expand_token(const char *in, size_t n);
44 static void append_expanded_string(const char *in);
45 static void zconf_endhelp(void);
46 static void zconf_endfile(void);
48 static void new_string(void)
50 text = xmalloc(START_STRSIZE);
51 text_asize = START_STRSIZE;
56 static void append_string(const char *str, int size)
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;
65 memcpy(text + text_size, str, size);
70 static void alloc_string(const char *str, int size)
72 text = xmalloc(size + 1);
73 memcpy(text, str, size);
77 static void warn_ignored_character(char chr)
80 "%s:%d:warning: ignoring unsupported character '%c'\n",
81 current_file->name, yylineno, chr);
91 #.* /* ignore comment */
92 [ \t]* /* whitespaces */
93 \\\n /* escaped new line */
95 "allnoconfig_y" return T_ALLNOCONFIG_Y;
97 "choice" return T_CHOICE;
98 "comment" return T_COMMENT;
99 "config" return T_CONFIG;
100 "def_bool" return T_DEF_BOOL;
101 "def_tristate" return T_DEF_TRISTATE;
102 "default" return T_DEFAULT;
103 "defconfig_list" return T_DEFCONFIG_LIST;
104 "depends" return T_DEPENDS;
105 "endchoice" return T_ENDCHOICE;
106 "endif" return T_ENDIF;
107 "endmenu" return T_ENDMENU;
108 "help"|"---help---" return T_HELP;
111 "imply" return T_IMPLY;
113 "mainmenu" return T_MAINMENU;
114 "menu" return T_MENU;
115 "menuconfig" return T_MENUCONFIG;
116 "modules" return T_MODULES;
118 "option" return T_OPTION;
119 "optional" return T_OPTIONAL;
120 "prompt" return T_PROMPT;
121 "range" return T_RANGE;
122 "select" return T_SELECT;
123 "source" return T_SOURCE;
124 "string" return T_STRING;
125 "tristate" return T_TRISTATE;
126 "visible" return T_VISIBLE;
130 "!=" return T_UNEQUAL;
132 "<=" return T_LESS_EQUAL;
133 ">" return T_GREATER;
134 ">=" return T_GREATER_EQUAL;
136 "(" return T_OPEN_PAREN;
137 ")" return T_CLOSE_PAREN;
138 ":=" return T_COLON_EQUAL;
139 "+=" return T_PLUS_EQUAL;
146 alloc_string(yytext, yyleng);
147 yylval.string = text;
151 /* this token includes at least one '$' */
152 yylval.string = expand_token(yytext, yyleng);
153 if (strlen(yylval.string))
157 . warn_ignored_character(*yytext);
161 alloc_string(yytext, yyleng);
162 yylval.string = text;
165 \n { BEGIN(INITIAL); return T_EOL; }
170 "$".* append_expanded_string(yytext);
172 append_string(yytext, yyleng);
175 append_string(yytext + 1, yyleng - 1);
178 if (str == yytext[0]) {
180 yylval.string = text;
183 append_string(yytext, 1);
187 "%s:%d:warning: multi-line strings not supported\n",
188 zconf_curname(), zconf_lineno());
191 yylval.string = text;
196 yylval.string = text;
204 for (i = 0; i < yyleng; i++) {
205 if (yytext[i] == '\t')
218 append_string(" ", 8);
221 append_string(" ", ts);
229 append_string("\n", 1);
233 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
237 append_string(yytext, yyleng);
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);
264 /* second stage lexer */
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. */
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.
283 current_pos.file = current_file;
284 current_pos.lineno = yylineno;
288 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
289 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
292 prev_prev_token = prev_token;
298 static char *expand_token(const char *in, size_t n)
303 const char *rest, *end;
306 append_string(in, n);
308 /* get the whole line because we do not know the end of token. */
309 while ((c = input()) != EOF) {
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>";