1 /* SPDX-License-Identifier: GPL-2.0 */
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;
47 static struct buffer *current_buf;
49 static int last_ts, first_ts;
51 static char *expand_token(const char *in, size_t n);
52 static void append_expanded_string(const char *in);
53 static void zconf_endhelp(void);
54 static void zconf_endfile(void);
56 static void new_string(void)
58 text = xmalloc(START_STRSIZE);
59 text_asize = START_STRSIZE;
64 static void append_string(const char *str, int size)
66 int new_size = text_size + size + 1;
67 if (new_size > text_asize) {
68 new_size += START_STRSIZE - 1;
69 new_size &= -START_STRSIZE;
70 text = xrealloc(text, new_size);
71 text_asize = new_size;
73 memcpy(text + text_size, str, size);
78 static void alloc_string(const char *str, int size)
80 text = xmalloc(size + 1);
81 memcpy(text, str, size);
85 static void warn_ignored_character(char chr)
88 "%s:%d:warning: ignoring unsupported character '%c'\n",
89 cur_filename, yylineno, chr);
98 #.* /* ignore comment */
99 [ \t]* /* whitespaces */
100 \\\n /* escaped new line */
102 "bool" return T_BOOL;
103 "choice" return T_CHOICE;
104 "comment" return T_COMMENT;
105 "config" return T_CONFIG;
106 "def_bool" return T_DEF_BOOL;
107 "def_tristate" return T_DEF_TRISTATE;
108 "default" return T_DEFAULT;
109 "depends" return T_DEPENDS;
110 "endchoice" return T_ENDCHOICE;
111 "endif" return T_ENDIF;
112 "endmenu" return T_ENDMENU;
113 "help" return T_HELP;
116 "imply" return T_IMPLY;
118 "mainmenu" return T_MAINMENU;
119 "menu" return T_MENU;
120 "menuconfig" return T_MENUCONFIG;
121 "modules" return T_MODULES;
123 "prompt" return T_PROMPT;
124 "range" return T_RANGE;
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;
144 open_quote = yytext[0];
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 (open_quote == yytext[0]) {
183 yylval.string = text;
186 append_string(yytext, 1);
190 "%s:%d:warning: multi-line strings not supported\n",
191 cur_filename, cur_lineno);
194 yylval.string = text;
199 yylval.string = text;
209 for (i = 0; i < yyleng; i++) {
210 if (yytext[i] == '\t')
223 append_string(" ", 8);
226 append_string(" ", ts);
234 append_string("\n", 1);
238 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
242 append_string(yytext, yyleng);
255 if (prev_token != T_EOL && prev_token != T_HELPTEXT)
256 fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
257 cur_filename, yylineno);
269 /* second stage lexer */
277 if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
279 /* Do not pass unneeded T_EOL to the parser. */
283 * For the parser, update lineno at the first token
284 * of each statement. Generally, \n is a statement
285 * terminator in Kconfig, but it is not always true
286 * because \n could be escaped by a backslash.
288 cur_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);
312 * get the whole line because we do not know the end of token.
313 * input() returns 0 (not EOF!) when it reachs the end of file.
315 while ((c = input()) != 0) {
321 append_string(&c2, 1);
325 out = expand_one_token(&rest);
327 /* push back unused characters to the input stream */
328 end = rest + strlen(rest);
337 static void append_expanded_string(const char *str)
344 res = expand_dollar(&str);
346 /* push back unused characters to the input stream */
347 end = str + strlen(str);
351 append_string(res, strlen(res));
356 void zconf_starthelp(void)
359 last_ts = first_ts = 0;
363 static void zconf_endhelp(void)
365 yylval.string = text;
371 * Try to open specified file with following names:
374 * The latter is used when srctree is separate from objtree
375 * when compiling the kernel.
376 * Return NULL if file is not found.
378 FILE *zconf_fopen(const char *name)
380 char *env, fullname[PATH_MAX+1];
383 f = fopen(name, "r");
384 if (!f && name != NULL && name[0] != '/') {
385 env = getenv(SRCTREE);
387 snprintf(fullname, sizeof(fullname),
389 f = fopen(fullname, "r");
395 void zconf_initscan(const char *name)
397 yyin = zconf_fopen(name);
399 fprintf(stderr, "can't find file %s\n", name);
403 cur_filename = file_lookup(name);
407 void zconf_nextfile(const char *name)
409 struct buffer *buf = xmalloc(sizeof(*buf));
410 bool recur_include = false;
412 buf->state = YY_CURRENT_BUFFER;
413 buf->yylineno = yylineno;
414 buf->filename = cur_filename;
415 buf->source_lineno = cur_lineno;
416 buf->parent = current_buf;
418 yyin = zconf_fopen(name);
420 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
421 cur_filename, cur_lineno, name);
424 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
426 for (buf = current_buf; buf; buf = buf->parent) {
427 if (!strcmp(buf->filename, name))
428 recur_include = true;
433 "Recursive inclusion detected.\n"
435 " current file : %s\n", name);
437 for (buf = current_buf; buf; buf = buf->parent)
438 fprintf(stderr, " included from: %s:%d\n",
439 buf->filename, buf->source_lineno);
444 cur_filename = file_lookup(name);
447 static void zconf_endfile(void)
452 yy_delete_buffer(YY_CURRENT_BUFFER);
453 yy_switch_to_buffer(current_buf->state);
454 yylineno = current_buf->yylineno;
455 cur_filename = current_buf->filename;
457 current_buf = current_buf->parent;