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
20 #include "parser.tab.h"
22 #define YY_DECL static int yylex1(void)
24 #define START_STRSIZE 16
31 static int prev_prev_token = T_EOL;
32 static int prev_token = T_EOL;
34 static int text_size, text_asize;
37 struct buffer *parent;
38 YY_BUFFER_STATE state;
41 struct buffer *current_buf;
43 static int last_ts, first_ts;
45 static char *expand_token(const char *in, size_t n);
46 static void append_expanded_string(const char *in);
47 static void zconf_endhelp(void);
48 static void zconf_endfile(void);
50 static void new_string(void)
52 text = xmalloc(START_STRSIZE);
53 text_asize = START_STRSIZE;
58 static void append_string(const char *str, int size)
60 int new_size = text_size + size + 1;
61 if (new_size > text_asize) {
62 new_size += START_STRSIZE - 1;
63 new_size &= -START_STRSIZE;
64 text = xrealloc(text, new_size);
65 text_asize = new_size;
67 memcpy(text + text_size, str, size);
72 static void alloc_string(const char *str, int size)
74 text = xmalloc(size + 1);
75 memcpy(text, str, size);
79 static void warn_ignored_character(char chr)
82 "%s:%d:warning: ignoring unsupported character '%c'\n",
83 current_file->name, yylineno, chr);
93 #.* /* ignore comment */
94 [ \t]* /* whitespaces */
95 \\\n /* escaped new line */
97 "allnoconfig_y" return T_ALLNOCONFIG_Y;
99 "choice" return T_CHOICE;
100 "comment" return T_COMMENT;
101 "config" return T_CONFIG;
102 "def_bool" return T_DEF_BOOL;
103 "def_tristate" return T_DEF_TRISTATE;
104 "default" return T_DEFAULT;
105 "defconfig_list" return T_DEFCONFIG_LIST;
106 "depends" return T_DEPENDS;
107 "endchoice" return T_ENDCHOICE;
108 "endif" return T_ENDIF;
109 "endmenu" return T_ENDMENU;
110 "help"|"---help---" return T_HELP;
113 "imply" return T_IMPLY;
115 "mainmenu" return T_MAINMENU;
116 "menu" return T_MENU;
117 "menuconfig" return T_MENUCONFIG;
118 "modules" return T_MODULES;
120 "option" return T_OPTION;
121 "optional" return T_OPTIONAL;
122 "prompt" return T_PROMPT;
123 "range" return T_RANGE;
124 "reset" return T_RESET;
125 "select" return T_SELECT;
126 "source" return T_SOURCE;
127 "string" return T_STRING;
128 "tristate" return T_TRISTATE;
129 "visible" return T_VISIBLE;
133 "!=" return T_UNEQUAL;
135 "<=" return T_LESS_EQUAL;
136 ">" return T_GREATER;
137 ">=" return T_GREATER_EQUAL;
139 "(" return T_OPEN_PAREN;
140 ")" return T_CLOSE_PAREN;
141 ":=" return T_COLON_EQUAL;
142 "+=" return T_PLUS_EQUAL;
149 alloc_string(yytext, yyleng);
150 yylval.string = text;
154 /* this token includes at least one '$' */
155 yylval.string = expand_token(yytext, yyleng);
156 if (strlen(yylval.string))
160 . warn_ignored_character(*yytext);
164 alloc_string(yytext, yyleng);
165 yylval.string = text;
168 \n { BEGIN(INITIAL); return T_EOL; }
173 "$".* append_expanded_string(yytext);
175 append_string(yytext, yyleng);
178 append_string(yytext + 1, yyleng - 1);
181 if (str == yytext[0]) {
183 yylval.string = text;
186 append_string(yytext, 1);
190 "%s:%d:warning: multi-line strings not supported\n",
191 zconf_curname(), zconf_lineno());
194 yylval.string = text;
199 yylval.string = text;
207 for (i = 0; i < yyleng; i++) {
208 if (yytext[i] == '\t')
221 append_string(" ", 8);
224 append_string(" ", ts);
232 append_string("\n", 1);
236 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
240 append_string(yytext, yyleng);
253 if (prev_token != T_EOL && prev_token != T_HELPTEXT)
254 fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
255 current_file->name, yylineno);
267 /* second stage lexer */
275 if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
276 if (token == T_EOL) {
277 /* Do not pass unneeded T_EOL to the parser. */
281 * For the parser, update file/lineno at the first token
282 * of each statement. Generally, \n is a statement
283 * terminator in Kconfig, but it is not always true
284 * because \n could be escaped by a backslash.
286 current_pos.file = current_file;
287 current_pos.lineno = yylineno;
291 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
292 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
295 prev_prev_token = prev_token;
301 static char *expand_token(const char *in, size_t n)
306 const char *rest, *end;
309 append_string(in, n);
311 /* get the whole line because we do not know the end of token. */
312 while ((c = input()) != EOF) {
318 append_string(&c2, 1);
322 out = expand_one_token(&rest);
324 /* push back unused characters to the input stream */
325 end = rest + strlen(rest);
334 static void append_expanded_string(const char *str)
341 res = expand_dollar(&str);
343 /* push back unused characters to the input stream */
344 end = str + strlen(str);
348 append_string(res, strlen(res));
353 void zconf_starthelp(void)
356 last_ts = first_ts = 0;
360 static void zconf_endhelp(void)
362 yylval.string = text;
368 * Try to open specified file with following names:
371 * The latter is used when srctree is separate from objtree
372 * when compiling the kernel.
373 * Return NULL if file is not found.
375 FILE *zconf_fopen(const char *name)
377 char *env, fullname[PATH_MAX+1];
380 f = fopen(name, "r");
381 if (!f && name != NULL && name[0] != '/') {
382 env = getenv(SRCTREE);
384 snprintf(fullname, sizeof(fullname),
386 f = fopen(fullname, "r");
392 void zconf_initscan(const char *name)
394 yyin = zconf_fopen(name);
396 fprintf(stderr, "can't find file %s\n", name);
400 current_buf = xmalloc(sizeof(*current_buf));
401 memset(current_buf, 0, sizeof(*current_buf));
403 current_file = file_lookup(name);
407 static void __zconf_nextfile(const char *name)
410 struct file *file = file_lookup(name);
411 struct buffer *buf = xmalloc(sizeof(*buf));
412 memset(buf, 0, sizeof(*buf));
414 current_buf->state = YY_CURRENT_BUFFER;
415 yyin = zconf_fopen(file->name);
417 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
418 zconf_curname(), zconf_lineno(), file->name);
421 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
422 buf->parent = current_buf;
425 current_file->lineno = yylineno;
426 file->parent = current_file;
428 for (iter = current_file; iter; iter = iter->parent) {
429 if (!strcmp(iter->name, file->name)) {
431 "Recursive inclusion detected.\n"
433 " current file : %s\n", file->name);
437 fprintf(stderr, " included from: %s:%d\n",
438 iter->name, iter->lineno - 1);
439 } while (strcmp(iter->name, file->name));
448 void zconf_nextfile(const char *name)
453 char path[PATH_MAX], *p;
455 err = glob(name, GLOB_ERR | GLOB_MARK, NULL, &gl);
457 /* ignore wildcard patterns that return no result */
458 if (err == GLOB_NOMATCH && strchr(name, '*')) {
463 if (err == GLOB_NOMATCH) {
464 p = strdup(current_file->name);
466 snprintf(path, sizeof(path), "%s/%s", dirname(p), name);
467 err = glob(path, GLOB_ERR | GLOB_MARK, NULL, &gl);
473 const char *reason = "unknown error";
477 reason = "out of memory";
480 reason = "read error";
483 reason = "No files found";
489 printf("%s:%d: glob failed: %s \"%s\"\n", zconf_curname(), zconf_lineno(),
495 for (i = 0; i < gl.gl_pathc; i++)
496 __zconf_nextfile(gl.gl_pathv[i]);
499 static void zconf_endfile(void)
501 struct buffer *parent;
503 current_file = current_file->parent;
505 yylineno = current_file->lineno;
507 parent = current_buf->parent;
510 yy_delete_buffer(YY_CURRENT_BUFFER);
511 yy_switch_to_buffer(parent->state);
514 current_buf = parent;
517 int zconf_lineno(void)
519 return current_pos.lineno;
522 const char *zconf_curname(void)
524 return current_pos.file ? current_pos.file->name : "<none>";