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
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;
41 static 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);
92 #.* /* ignore comment */
93 [ \t]* /* whitespaces */
94 \\\n /* escaped new line */
97 "choice" return T_CHOICE;
98 "comment" return T_COMMENT;
99 "config" return T_CONFIG;
100 "def_bool" return T_DEF_BOOL;
101 "def_tristate" return T_DEF_TRISTATE;
102 "default" return T_DEFAULT;
103 "depends" return T_DEPENDS;
104 "endchoice" return T_ENDCHOICE;
105 "endif" return T_ENDIF;
106 "endmenu" return T_ENDMENU;
107 "help" return T_HELP;
110 "imply" return T_IMPLY;
112 "mainmenu" return T_MAINMENU;
113 "menu" return T_MENU;
114 "menuconfig" return T_MENUCONFIG;
115 "modules" return T_MODULES;
117 "optional" return T_OPTIONAL;
118 "prompt" return T_PROMPT;
119 "range" return T_RANGE;
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);
309 * get the whole line because we do not know the end of token.
310 * input() returns 0 (not EOF!) when it reachs the end of file.
312 while ((c = input()) != 0) {
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_file = file_lookup(name);
404 void zconf_nextfile(const char *name)
407 struct file *file = file_lookup(name);
408 struct buffer *buf = xmalloc(sizeof(*buf));
410 buf->state = YY_CURRENT_BUFFER;
411 buf->yylineno = yylineno;
412 buf->parent = current_buf;
414 yyin = zconf_fopen(file->name);
416 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
417 zconf_curname(), zconf_lineno(), file->name);
420 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
422 current_file->lineno = zconf_lineno();
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);
436 } while (strcmp(iter->name, file->name));
445 static void zconf_endfile(void)
449 current_file = current_file->parent;
455 yy_delete_buffer(YY_CURRENT_BUFFER);
456 yy_switch_to_buffer(current_buf->state);
457 yylineno = current_buf->yylineno;
459 current_buf = current_buf->parent;
463 int zconf_lineno(void)
465 return current_pos.lineno;
468 const char *zconf_curname(void)
470 return current_pos.file ? current_pos.file->name : "<none>";