1 /* SPDX-License-Identifier: GPL-2.0-only */
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
19 #include "parser.tab.h"
21 #define YY_DECL static int yylex1(void)
23 #define START_STRSIZE 16
30 static int prev_prev_token = T_EOL;
31 static int prev_token = T_EOL;
33 static int text_size, text_asize;
36 struct buffer *parent;
37 YY_BUFFER_STATE state;
40 static struct buffer *current_buf;
42 static int last_ts, first_ts;
44 static char *expand_token(const char *in, size_t n);
45 static void append_expanded_string(const char *in);
46 static void zconf_endhelp(void);
47 static void zconf_endfile(void);
49 static void new_string(void)
51 text = xmalloc(START_STRSIZE);
52 text_asize = START_STRSIZE;
57 static void append_string(const char *str, int size)
59 int new_size = text_size + size + 1;
60 if (new_size > text_asize) {
61 new_size += START_STRSIZE - 1;
62 new_size &= -START_STRSIZE;
63 text = xrealloc(text, new_size);
64 text_asize = new_size;
66 memcpy(text + text_size, str, size);
71 static void alloc_string(const char *str, int size)
73 text = xmalloc(size + 1);
74 memcpy(text, str, size);
78 static void warn_ignored_character(char chr)
81 "%s:%d:warning: ignoring unsupported character '%c'\n",
82 current_file->name, yylineno, chr);
92 #.* /* ignore comment */
93 [ \t]* /* whitespaces */
94 \\\n /* escaped new line */
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 "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 "optional" return T_OPTIONAL;
118 "prompt" return T_PROMPT;
119 "range" return T_RANGE;
120 "reset" return T_RESET;
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 kernel.
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 static 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 void zconf_nextfile(const char *name)
449 char path[PATH_MAX], *p;
451 err = glob(name, GLOB_ERR | GLOB_MARK, NULL, &gl);
453 /* ignore wildcard patterns that return no result */
454 if (err == GLOB_NOMATCH && strchr(name, '*')) {
459 if (err == GLOB_NOMATCH) {
460 p = strdup(current_file->name);
462 snprintf(path, sizeof(path), "%s/%s", dirname(p), name);
463 err = glob(path, GLOB_ERR | GLOB_MARK, NULL, &gl);
469 const char *reason = "unknown error";
473 reason = "out of memory";
476 reason = "read error";
479 reason = "No files found";
485 printf("%s:%d: glob failed: %s \"%s\"\n", zconf_curname(), zconf_lineno(),
491 for (i = 0; i < gl.gl_pathc; i++)
492 __zconf_nextfile(gl.gl_pathv[i]);
495 static void zconf_endfile(void)
497 struct buffer *parent;
499 current_file = current_file->parent;
501 yylineno = current_file->lineno;
503 parent = current_buf->parent;
506 yy_delete_buffer(YY_CURRENT_BUFFER);
507 yy_switch_to_buffer(parent->state);
510 current_buf = parent;
513 int zconf_lineno(void)
515 return current_pos.lineno;
518 const char *zconf_curname(void)
520 return current_pos.file ? current_pos.file->name : "<none>";