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 static 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);
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;
137 open_quote = yytext[0];
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 (open_quote == 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;
202 for (i = 0; i < yyleng; i++) {
203 if (yytext[i] == '\t')
216 append_string(" ", 8);
219 append_string(" ", ts);
227 append_string("\n", 1);
231 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
235 append_string(yytext, yyleng);
248 if (prev_token != T_EOL && prev_token != T_HELPTEXT)
249 fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
250 current_file->name, yylineno);
262 /* second stage lexer */
270 if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
271 if (token == T_EOL) {
272 /* Do not pass unneeded T_EOL to the parser. */
276 * For the parser, update file/lineno at the first token
277 * of each statement. Generally, \n is a statement
278 * terminator in Kconfig, but it is not always true
279 * because \n could be escaped by a backslash.
281 current_pos.file = current_file;
282 current_pos.lineno = yylineno;
286 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
287 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
290 prev_prev_token = prev_token;
296 static char *expand_token(const char *in, size_t n)
301 const char *rest, *end;
304 append_string(in, n);
306 /* get the whole line because we do not know the end of token. */
307 while ((c = input()) != EOF) {
313 append_string(&c2, 1);
317 out = expand_one_token(&rest);
319 /* push back unused characters to the input stream */
320 end = rest + strlen(rest);
329 static void append_expanded_string(const char *str)
336 res = expand_dollar(&str);
338 /* push back unused characters to the input stream */
339 end = str + strlen(str);
343 append_string(res, strlen(res));
348 void zconf_starthelp(void)
351 last_ts = first_ts = 0;
355 static void zconf_endhelp(void)
357 yylval.string = text;
363 * Try to open specified file with following names:
366 * The latter is used when srctree is separate from objtree
367 * when compiling the kernel.
368 * Return NULL if file is not found.
370 FILE *zconf_fopen(const char *name)
372 char *env, fullname[PATH_MAX+1];
375 f = fopen(name, "r");
376 if (!f && name != NULL && name[0] != '/') {
377 env = getenv(SRCTREE);
379 snprintf(fullname, sizeof(fullname),
381 f = fopen(fullname, "r");
387 void zconf_initscan(const char *name)
389 yyin = zconf_fopen(name);
391 fprintf(stderr, "can't find file %s\n", name);
395 current_buf = xmalloc(sizeof(*current_buf));
396 memset(current_buf, 0, sizeof(*current_buf));
398 current_file = file_lookup(name);
402 void zconf_nextfile(const char *name)
405 struct file *file = file_lookup(name);
406 struct buffer *buf = xmalloc(sizeof(*buf));
407 memset(buf, 0, sizeof(*buf));
409 current_buf->state = YY_CURRENT_BUFFER;
410 yyin = zconf_fopen(file->name);
412 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
413 zconf_curname(), zconf_lineno(), file->name);
416 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
417 buf->parent = current_buf;
420 current_file->lineno = yylineno;
421 file->parent = current_file;
423 for (iter = current_file; iter; iter = iter->parent) {
424 if (!strcmp(iter->name, file->name)) {
426 "Recursive inclusion detected.\n"
428 " current file : %s\n", file->name);
432 fprintf(stderr, " included from: %s:%d\n",
433 iter->name, iter->lineno - 1);
434 } while (strcmp(iter->name, file->name));
443 void zconf_nextfiles(const char *wildcard)
449 if (glob(wildcard, 0, NULL, &g) != 0) {
452 if (g.gl_pathv == NULL) {
457 /* working through files backwards, since
458 * we're first pushing them on a stack
459 * before actually handling them.
461 for (i = g.gl_pathc; i > 0; i--) {
462 w = &g.gl_pathv[i - 1];
469 static void zconf_endfile(void)
471 struct buffer *parent;
473 current_file = current_file->parent;
475 yylineno = current_file->lineno;
477 parent = current_buf->parent;
480 yy_delete_buffer(YY_CURRENT_BUFFER);
481 yy_switch_to_buffer(parent->state);
484 current_buf = parent;
487 int zconf_lineno(void)
489 return current_pos.lineno;
492 const char *zconf_curname(void)
494 return current_pos.file ? current_pos.file->name : "<none>";