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);
91 #.* /* ignore comment */
92 [ \t]* /* whitespaces */
93 \\\n /* escaped new line */
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 "depends" return T_DEPENDS;
103 "endchoice" return T_ENDCHOICE;
104 "endif" return T_ENDIF;
105 "endmenu" return T_ENDMENU;
106 "help" return T_HELP;
109 "imply" return T_IMPLY;
111 "mainmenu" return T_MAINMENU;
112 "menu" return T_MENU;
113 "menuconfig" return T_MENUCONFIG;
114 "modules" return T_MODULES;
116 "optional" return T_OPTIONAL;
117 "prompt" return T_PROMPT;
118 "range" return T_RANGE;
119 "reset" return T_RESET;
120 "select" return T_SELECT;
121 "source" return T_SOURCE;
122 "string" return T_STRING;
123 "tristate" return T_TRISTATE;
124 "visible" return T_VISIBLE;
128 "!=" return T_UNEQUAL;
130 "<=" return T_LESS_EQUAL;
131 ">" return T_GREATER;
132 ">=" return T_GREATER_EQUAL;
134 "(" return T_OPEN_PAREN;
135 ")" return T_CLOSE_PAREN;
136 ":=" return T_COLON_EQUAL;
137 "+=" return T_PLUS_EQUAL;
139 open_quote = yytext[0];
144 alloc_string(yytext, yyleng);
145 yylval.string = text;
149 /* this token includes at least one '$' */
150 yylval.string = expand_token(yytext, yyleng);
151 if (strlen(yylval.string))
155 . warn_ignored_character(*yytext);
159 alloc_string(yytext, yyleng);
160 yylval.string = text;
163 \n { BEGIN(INITIAL); return T_EOL; }
168 "$".* append_expanded_string(yytext);
170 append_string(yytext, yyleng);
173 append_string(yytext + 1, yyleng - 1);
176 if (open_quote == yytext[0]) {
178 yylval.string = text;
181 append_string(yytext, 1);
185 "%s:%d:warning: multi-line strings not supported\n",
186 zconf_curname(), zconf_lineno());
189 yylval.string = text;
194 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 static 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 void zconf_nextfile(const char *name)
450 char path[PATH_MAX], *p;
452 err = glob(name, GLOB_ERR | GLOB_MARK, NULL, &gl);
454 /* ignore wildcard patterns that return no result */
455 if (err == GLOB_NOMATCH && strchr(name, '*')) {
460 if (err == GLOB_NOMATCH) {
461 p = strdup(current_file->name);
463 snprintf(path, sizeof(path), "%s/%s", dirname(p), name);
464 err = glob(path, GLOB_ERR | GLOB_MARK, NULL, &gl);
470 const char *reason = "unknown error";
474 reason = "out of memory";
477 reason = "read error";
480 reason = "No files found";
486 printf("%s:%d: glob failed: %s \"%s\"\n", zconf_curname(), zconf_lineno(),
492 for (i = 0; i < gl.gl_pathc; i++)
493 __zconf_nextfile(gl.gl_pathv[i]);
496 static void zconf_endfile(void)
498 struct buffer *parent;
500 current_file = current_file->parent;
502 yylineno = current_file->lineno;
504 parent = current_buf->parent;
507 yy_delete_buffer(YY_CURRENT_BUFFER);
508 yy_switch_to_buffer(parent->state);
511 current_buf = parent;
514 int zconf_lineno(void)
516 return current_pos.lineno;
519 const char *zconf_curname(void)
521 return current_pos.file ? current_pos.file->name : "<none>";