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
17 #include "preprocess.h"
19 #include "parser.tab.h"
21 #define YY_DECL static int yylex1(void)
23 #define START_STRSIZE 16
25 /* The Kconfig file currently being parsed. */
26 const char *cur_filename;
29 * The line number of the current statement. This does not match yylineno.
30 * yylineno is used by the lexer, while cur_lineno is used by the parser.
34 static int prev_prev_token = T_EOL;
35 static int prev_token = T_EOL;
37 static int text_size, text_asize;
40 struct buffer *parent;
41 YY_BUFFER_STATE state;
45 static struct buffer *current_buf;
47 static int last_ts, first_ts;
49 static char *expand_token(const char *in, size_t n);
50 static void append_expanded_string(const char *in);
51 static void zconf_endhelp(void);
52 static void zconf_endfile(void);
54 static void new_string(void)
56 text = xmalloc(START_STRSIZE);
57 text_asize = START_STRSIZE;
62 static void append_string(const char *str, int size)
64 int new_size = text_size + size + 1;
65 if (new_size > text_asize) {
66 new_size += START_STRSIZE - 1;
67 new_size &= -START_STRSIZE;
68 text = xrealloc(text, new_size);
69 text_asize = new_size;
71 memcpy(text + text_size, str, size);
76 static void alloc_string(const char *str, int size)
78 text = xmalloc(size + 1);
79 memcpy(text, str, size);
83 static void warn_ignored_character(char chr)
86 "%s:%d:warning: ignoring unsupported character '%c'\n",
87 cur_filename, yylineno, chr);
96 #.* /* ignore comment */
97 [ \t]* /* whitespaces */
98 \\\n /* escaped new line */
100 "bool" return T_BOOL;
101 "choice" return T_CHOICE;
102 "comment" return T_COMMENT;
103 "config" return T_CONFIG;
104 "def_bool" return T_DEF_BOOL;
105 "def_tristate" return T_DEF_TRISTATE;
106 "default" return T_DEFAULT;
107 "depends" return T_DEPENDS;
108 "endchoice" return T_ENDCHOICE;
109 "endif" return T_ENDIF;
110 "endmenu" return T_ENDMENU;
111 "help" return T_HELP;
114 "imply" return T_IMPLY;
116 "mainmenu" return T_MAINMENU;
117 "menu" return T_MENU;
118 "menuconfig" return T_MENUCONFIG;
119 "modules" return T_MODULES;
121 "optional" return T_OPTIONAL;
122 "prompt" return T_PROMPT;
123 "range" return T_RANGE;
124 "select" return T_SELECT;
125 "source" return T_SOURCE;
126 "string" return T_STRING;
127 "tristate" return T_TRISTATE;
128 "visible" return T_VISIBLE;
132 "!=" return T_UNEQUAL;
134 "<=" return T_LESS_EQUAL;
135 ">" return T_GREATER;
136 ">=" return T_GREATER_EQUAL;
138 "(" return T_OPEN_PAREN;
139 ")" return T_CLOSE_PAREN;
140 ":=" return T_COLON_EQUAL;
141 "+=" return T_PLUS_EQUAL;
143 open_quote = yytext[0];
148 alloc_string(yytext, yyleng);
149 yylval.string = text;
153 /* this token includes at least one '$' */
154 yylval.string = expand_token(yytext, yyleng);
155 if (strlen(yylval.string))
159 . warn_ignored_character(*yytext);
163 alloc_string(yytext, yyleng);
164 yylval.string = text;
167 \n { BEGIN(INITIAL); return T_EOL; }
172 "$".* append_expanded_string(yytext);
174 append_string(yytext, yyleng);
177 append_string(yytext + 1, yyleng - 1);
180 if (open_quote == yytext[0]) {
182 yylval.string = text;
185 append_string(yytext, 1);
189 "%s:%d:warning: multi-line strings not supported\n",
190 cur_filename, cur_lineno);
193 yylval.string = text;
198 yylval.string = text;
208 for (i = 0; i < yyleng; i++) {
209 if (yytext[i] == '\t')
222 append_string(" ", 8);
225 append_string(" ", ts);
233 append_string("\n", 1);
237 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
241 append_string(yytext, yyleng);
254 if (prev_token != T_EOL && prev_token != T_HELPTEXT)
255 fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
256 cur_filename, yylineno);
268 /* second stage lexer */
276 if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
278 /* Do not pass unneeded T_EOL to the parser. */
282 * For the parser, update lineno at the first token
283 * of each statement. Generally, \n is a statement
284 * terminator in Kconfig, but it is not always true
285 * because \n could be escaped by a backslash.
287 cur_lineno = yylineno;
290 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
291 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
294 prev_prev_token = prev_token;
300 static char *expand_token(const char *in, size_t n)
305 const char *rest, *end;
308 append_string(in, n);
311 * get the whole line because we do not know the end of token.
312 * input() returns 0 (not EOF!) when it reachs the end of file.
314 while ((c = input()) != 0) {
320 append_string(&c2, 1);
324 out = expand_one_token(&rest);
326 /* push back unused characters to the input stream */
327 end = rest + strlen(rest);
336 static void append_expanded_string(const char *str)
343 res = expand_dollar(&str);
345 /* push back unused characters to the input stream */
346 end = str + strlen(str);
350 append_string(res, strlen(res));
355 void zconf_starthelp(void)
358 last_ts = first_ts = 0;
362 static void zconf_endhelp(void)
364 yylval.string = text;
370 * Try to open specified file with following names:
373 * The latter is used when srctree is separate from objtree
374 * when compiling the kernel.
375 * Return NULL if file is not found.
377 FILE *zconf_fopen(const char *name)
379 char *env, fullname[PATH_MAX+1];
382 f = fopen(name, "r");
383 if (!f && name != NULL && name[0] != '/') {
384 env = getenv(SRCTREE);
386 snprintf(fullname, sizeof(fullname),
388 f = fopen(fullname, "r");
394 void zconf_initscan(const char *name)
396 yyin = zconf_fopen(name);
398 fprintf(stderr, "can't find file %s\n", name);
402 current_file = file_lookup(name);
403 cur_filename = current_file->name;
407 void zconf_nextfile(const char *name)
410 struct file *file = file_lookup(name);
411 struct buffer *buf = xmalloc(sizeof(*buf));
413 buf->state = YY_CURRENT_BUFFER;
414 buf->yylineno = yylineno;
415 buf->parent = current_buf;
417 yyin = zconf_fopen(name);
419 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
420 cur_filename, cur_lineno, name);
423 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
425 current_file->lineno = cur_lineno;
426 file->parent = current_file;
428 for (iter = current_file; iter; iter = iter->parent) {
429 if (!strcmp(iter->name, name)) {
431 "Recursive inclusion detected.\n"
433 " current file : %s\n", name);
437 fprintf(stderr, " included from: %s:%d\n",
438 iter->name, iter->lineno);
439 } while (strcmp(iter->name, name));
445 cur_filename = file->name;
449 static void zconf_endfile(void)
453 current_file = current_file->parent;
455 cur_filename = current_file->name;
461 yy_delete_buffer(YY_CURRENT_BUFFER);
462 yy_switch_to_buffer(current_buf->state);
463 yylineno = current_buf->yylineno;
465 current_buf = current_buf->parent;