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
18 #include "preprocess.h"
20 #include "parser.tab.h"
22 #define YY_DECL static int yylex1(void)
24 #define START_STRSIZE 16
26 /* The Kconfig file currently being parsed. */
27 const char *cur_filename;
30 * The line number of the current statement. This does not match yylineno.
31 * yylineno is used by the lexer, while cur_lineno is used by the parser.
35 static int prev_prev_token = T_EOL;
36 static int prev_token = T_EOL;
38 static int text_size, text_asize;
41 struct buffer *parent;
42 YY_BUFFER_STATE state;
48 static struct buffer *current_buf;
50 static int last_ts, first_ts;
52 static char *expand_token(const char *in, size_t n);
53 static void append_expanded_string(const char *in);
54 static void zconf_endhelp(void);
55 static void zconf_endfile(void);
57 static void new_string(void)
59 text = xmalloc(START_STRSIZE);
60 text_asize = START_STRSIZE;
65 static void append_string(const char *str, int size)
67 int new_size = text_size + size + 1;
68 if (new_size > text_asize) {
69 new_size += START_STRSIZE - 1;
70 new_size &= -START_STRSIZE;
71 text = xrealloc(text, new_size);
72 text_asize = new_size;
74 memcpy(text + text_size, str, size);
79 static void alloc_string(const char *str, int size)
81 text = xmalloc(size + 1);
82 memcpy(text, str, size);
86 static void warn_ignored_character(char chr)
89 "%s:%d:warning: ignoring unsupported character '%c'\n",
90 cur_filename, yylineno, chr);
99 #.* /* ignore comment */
100 [ \t]* /* whitespaces */
101 \\\n /* escaped new line */
103 "bool" return T_BOOL;
104 "choice" return T_CHOICE;
105 "comment" return T_COMMENT;
106 "config" return T_CONFIG;
107 "def_bool" return T_DEF_BOOL;
108 "def_tristate" return T_DEF_TRISTATE;
109 "default" return T_DEFAULT;
110 "depends" return T_DEPENDS;
111 "endchoice" return T_ENDCHOICE;
112 "endif" return T_ENDIF;
113 "endmenu" return T_ENDMENU;
114 "help" return T_HELP;
117 "imply" return T_IMPLY;
119 "mainmenu" return T_MAINMENU;
120 "menu" return T_MENU;
121 "menuconfig" return T_MENUCONFIG;
122 "modules" return T_MODULES;
124 "prompt" return T_PROMPT;
125 "range" return T_RANGE;
126 "select" return T_SELECT;
127 "source" return T_SOURCE;
128 "string" return T_STRING;
129 "tristate" return T_TRISTATE;
130 "visible" return T_VISIBLE;
134 "!=" return T_UNEQUAL;
136 "<=" return T_LESS_EQUAL;
137 ">" return T_GREATER;
138 ">=" return T_GREATER_EQUAL;
140 "(" return T_OPEN_PAREN;
141 ")" return T_CLOSE_PAREN;
142 ":=" return T_COLON_EQUAL;
143 "+=" return T_PLUS_EQUAL;
145 open_quote = yytext[0];
150 alloc_string(yytext, yyleng);
151 yylval.string = text;
155 /* this token includes at least one '$' */
156 yylval.string = expand_token(yytext, yyleng);
157 if (strlen(yylval.string))
161 . warn_ignored_character(*yytext);
165 alloc_string(yytext, yyleng);
166 yylval.string = text;
169 \n { BEGIN(INITIAL); return T_EOL; }
174 "$".* append_expanded_string(yytext);
176 append_string(yytext, yyleng);
179 append_string(yytext + 1, yyleng - 1);
182 if (open_quote == yytext[0]) {
184 yylval.string = text;
187 append_string(yytext, 1);
191 "%s:%d:warning: multi-line strings not supported\n",
192 cur_filename, cur_lineno);
195 yylval.string = text;
200 yylval.string = text;
210 for (i = 0; i < yyleng; i++) {
211 if (yytext[i] == '\t')
224 append_string(" ", 8);
227 append_string(" ", ts);
235 append_string("\n", 1);
239 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
243 append_string(yytext, yyleng);
256 if (prev_token != T_EOL && prev_token != T_HELPTEXT)
257 fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
258 cur_filename, yylineno);
270 /* second stage lexer */
278 if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
280 /* Do not pass unneeded T_EOL to the parser. */
284 * For the parser, update lineno at the first token
285 * of each statement. Generally, \n is a statement
286 * terminator in Kconfig, but it is not always true
287 * because \n could be escaped by a backslash.
289 cur_lineno = yylineno;
292 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
293 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
296 prev_prev_token = prev_token;
302 static char *expand_token(const char *in, size_t n)
307 const char *rest, *end;
310 append_string(in, n);
313 * get the whole line because we do not know the end of token.
314 * input() returns 0 (not EOF!) when it reachs the end of file.
316 while ((c = input()) != 0) {
322 append_string(&c2, 1);
326 out = expand_one_token(&rest);
328 /* push back unused characters to the input stream */
329 end = rest + strlen(rest);
338 static void append_expanded_string(const char *str)
345 res = expand_dollar(&str);
347 /* push back unused characters to the input stream */
348 end = str + strlen(str);
352 append_string(res, strlen(res));
357 void zconf_starthelp(void)
360 last_ts = first_ts = 0;
364 static void zconf_endhelp(void)
366 yylval.string = text;
372 * Try to open specified file with following names:
375 * The latter is used when srctree is separate from objtree
376 * when compiling the kernel.
377 * Return NULL if file is not found.
379 FILE *zconf_fopen(const char *name)
381 char *env, fullname[PATH_MAX+1];
384 f = fopen(name, "r");
385 if (!f && name != NULL && name[0] != '/') {
386 env = getenv(SRCTREE);
388 snprintf(fullname, sizeof(fullname),
390 f = fopen(fullname, "r");
396 void zconf_initscan(const char *name)
398 yyin = zconf_fopen(name);
400 fprintf(stderr, "can't find file %s\n", name);
404 cur_filename = file_lookup(name);
408 void zconf_nextfile(const char *name)
410 struct buffer *buf = xmalloc(sizeof(*buf));
411 bool recur_include = false;
413 buf->state = YY_CURRENT_BUFFER;
414 buf->yylineno = yylineno;
415 buf->filename = cur_filename;
416 buf->source_lineno = cur_lineno;
417 buf->parent = current_buf;
419 yyin = zconf_fopen(name);
421 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
422 cur_filename, cur_lineno, name);
425 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
427 for (buf = current_buf; buf; buf = buf->parent) {
428 if (!strcmp(buf->filename, name))
429 recur_include = true;
434 "Recursive inclusion detected.\n"
436 " current file : %s\n", name);
438 for (buf = current_buf; buf; buf = buf->parent)
439 fprintf(stderr, " included from: %s:%d\n",
440 buf->filename, buf->source_lineno);
445 cur_filename = file_lookup(name);
448 static void zconf_endfile(void)
453 yy_delete_buffer(YY_CURRENT_BUFFER);
454 yy_switch_to_buffer(current_buf->state);
455 yylineno = current_buf->yylineno;
456 cur_filename = current_buf->filename;
458 current_buf = current_buf->parent;