blob: 240109f965aeb72362226845a4364294924c607a [file] [log] [blame]
Masahiro Yamada0c874102018-12-18 21:13:35 +09001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 */
Arnaud Lacombe674eed82011-06-07 13:34:05 -04005%option nostdinit noyywrap never-interactive full ecs
Masahiro Yamada18492682018-03-23 02:00:14 +09006%option 8bit nodefault yylineno
Masahiro Yamada979f2b22018-12-11 20:01:10 +09007%x ASSIGN_VAL HELP STRING
Linus Torvalds1da177e2005-04-16 15:20:36 -07008%{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +090010#include <assert.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <limits.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <unistd.h>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "lkc.h"
Masahiro Yamada769a1c02019-01-24 19:47:30 +090018#include "parser.tab.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Masahiro Yamadacc66bca2018-12-11 20:00:49 +090020#define YY_DECL static int yylex1(void)
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#define START_STRSIZE 16
23
Roman Zippela02f0572005-11-08 21:34:53 -080024static struct {
25 struct file *file;
26 int lineno;
27} current_pos;
28
Masahiro Yamada824fa3b2018-12-11 20:01:08 +090029static int prev_prev_token = T_EOL;
Masahiro Yamadacc66bca2018-12-11 20:00:49 +090030static int prev_token = T_EOL;
Roman Zippel7a884882005-11-08 21:34:51 -080031static char *text;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int text_size, text_asize;
33
34struct buffer {
Masahiro Yamadabb66fc62014-06-10 19:08:13 +090035 struct buffer *parent;
36 YY_BUFFER_STATE state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Masahiro Yamadad41809f2020-07-29 12:18:37 +090039static struct buffer *current_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static int last_ts, first_ts;
42
Masahiro Yamada104daea2018-05-28 18:21:40 +090043static char *expand_token(const char *in, size_t n);
44static void append_expanded_string(const char *in);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static void zconf_endhelp(void);
Roman Zippela02f0572005-11-08 21:34:53 -080046static void zconf_endfile(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Josh Triplett65166572009-10-15 12:13:36 -070048static void new_string(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Alan Cox177acf72012-11-06 14:32:08 +000050 text = xmalloc(START_STRSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 text_asize = START_STRSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 text_size = 0;
Roman Zippel7a884882005-11-08 21:34:51 -080053 *text = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Josh Triplett65166572009-10-15 12:13:36 -070056static void append_string(const char *str, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 int new_size = text_size + size + 1;
59 if (new_size > text_asize) {
Roman Zippel7a884882005-11-08 21:34:51 -080060 new_size += START_STRSIZE - 1;
61 new_size &= -START_STRSIZE;
Masahiro Yamadad717f242018-02-09 01:19:07 +090062 text = xrealloc(text, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 text_asize = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
Roman Zippel7a884882005-11-08 21:34:51 -080065 memcpy(text + text_size, str, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 text_size += size;
Roman Zippel7a884882005-11-08 21:34:51 -080067 text[text_size] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Josh Triplett65166572009-10-15 12:13:36 -070070static void alloc_string(const char *str, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Alan Cox177acf72012-11-06 14:32:08 +000072 text = xmalloc(size + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 memcpy(text, str, size);
74 text[size] = 0;
75}
Andreas Ruprechtc2264562015-07-12 09:41:50 +020076
77static void warn_ignored_character(char chr)
78{
79 fprintf(stderr,
80 "%s:%d:warning: ignoring unsupported character '%c'\n",
Masahiro Yamada77c1c0f2018-12-11 20:00:44 +090081 current_file->name, yylineno, chr);
Andreas Ruprechtc2264562015-07-12 09:41:50 +020082}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083%}
84
Andreas Ruprechtc2264562015-07-12 09:41:50 +020085n [A-Za-z0-9_-]
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87%%
88 int str = 0;
89 int ts, i;
90
Masahiro Yamada979f2b22018-12-11 20:01:10 +090091#.* /* ignore comment */
92[ \t]* /* whitespaces */
93\\\n /* escaped new line */
94\n return T_EOL;
95"allnoconfig_y" return T_ALLNOCONFIG_Y;
96"bool" return T_BOOL;
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"defconfig_list" return T_DEFCONFIG_LIST;
104"depends" return T_DEPENDS;
105"endchoice" return T_ENDCHOICE;
106"endif" return T_ENDIF;
107"endmenu" return T_ENDMENU;
Masahiro Yamadaf70f74d2020-06-17 12:02:19 +0900108"help" return T_HELP;
Masahiro Yamada979f2b22018-12-11 20:01:10 +0900109"hex" return T_HEX;
110"if" return T_IF;
111"imply" return T_IMPLY;
112"int" return T_INT;
113"mainmenu" return T_MAINMENU;
114"menu" return T_MENU;
115"menuconfig" return T_MENUCONFIG;
116"modules" return T_MODULES;
117"on" return T_ON;
118"option" return T_OPTION;
119"optional" return T_OPTIONAL;
120"prompt" return T_PROMPT;
121"range" return T_RANGE;
122"select" return T_SELECT;
123"source" return T_SOURCE;
124"string" return T_STRING;
125"tristate" return T_TRISTATE;
126"visible" return T_VISIBLE;
127"||" return T_OR;
128"&&" return T_AND;
129"=" return T_EQUAL;
130"!=" return T_UNEQUAL;
131"<" return T_LESS;
132"<=" return T_LESS_EQUAL;
133">" return T_GREATER;
134">=" return T_GREATER_EQUAL;
135"!" return T_NOT;
136"(" return T_OPEN_PAREN;
137")" return T_CLOSE_PAREN;
138":=" return T_COLON_EQUAL;
139"+=" return T_PLUS_EQUAL;
140\"|\' {
141 str = yytext[0];
142 new_string();
143 BEGIN(STRING);
144 }
145{n}+ {
146 alloc_string(yytext, yyleng);
147 yylval.string = text;
148 return T_WORD;
149 }
150({n}|$)+ {
151 /* this token includes at least one '$' */
152 yylval.string = expand_token(yytext, yyleng);
153 if (strlen(yylval.string))
154 return T_WORD;
155 free(yylval.string);
156 }
157. warn_ignored_character(*yytext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900159<ASSIGN_VAL>{
160 [^[:blank:]\n]+.* {
161 alloc_string(yytext, yyleng);
162 yylval.string = text;
163 return T_ASSIGN_VAL;
164 }
165 \n { BEGIN(INITIAL); return T_EOL; }
166 .
167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169<STRING>{
Masahiro Yamada104daea2018-05-28 18:21:40 +0900170 "$".* append_expanded_string(yytext);
Masahiro Yamada104daea2018-05-28 18:21:40 +0900171 [^$'"\\\n]+ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 append_string(yytext, yyleng);
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 \\.? {
175 append_string(yytext + 1, yyleng - 1);
176 }
177 \'|\" {
178 if (str == yytext[0]) {
Masahiro Yamada979f2b22018-12-11 20:01:10 +0900179 BEGIN(INITIAL);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900180 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return T_WORD_QUOTE;
182 } else
183 append_string(yytext, 1);
184 }
185 \n {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900186 fprintf(stderr,
187 "%s:%d:warning: multi-line strings not supported\n",
188 zconf_curname(), zconf_lineno());
Masahiro Yamada21c5ecf2018-12-11 20:00:48 +0900189 unput('\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 BEGIN(INITIAL);
Masahiro Yamada21c5ecf2018-12-11 20:00:48 +0900191 yylval.string = text;
192 return T_WORD_QUOTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194 <<EOF>> {
195 BEGIN(INITIAL);
Masahiro Yamadafbac5972018-12-11 20:00:45 +0900196 yylval.string = text;
197 return T_WORD_QUOTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199}
200
201<HELP>{
202 [ \t]+ {
203 ts = 0;
204 for (i = 0; i < yyleng; i++) {
205 if (yytext[i] == '\t')
206 ts = (ts & ~7) + 8;
207 else
208 ts++;
209 }
210 last_ts = ts;
211 if (first_ts) {
212 if (ts < first_ts) {
213 zconf_endhelp();
214 return T_HELPTEXT;
215 }
216 ts -= first_ts;
217 while (ts > 8) {
218 append_string(" ", 8);
219 ts -= 8;
220 }
221 append_string(" ", ts);
222 }
223 }
224 [ \t]*\n/[^ \t\n] {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 zconf_endhelp();
226 return T_HELPTEXT;
227 }
228 [ \t]*\n {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 append_string("\n", 1);
230 }
231 [^ \t\n].* {
EGRY Gaborf7a4b4c2008-01-11 23:55:20 +0100232 while (yyleng) {
233 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
234 break;
235 yyleng--;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 append_string(yytext, yyleng);
238 if (!first_ts)
239 first_ts = last_ts;
240 }
241 <<EOF>> {
242 zconf_endhelp();
243 return T_HELPTEXT;
244 }
245}
246
247<<EOF>> {
Masahiro Yamada0bcc5472018-12-11 20:00:50 +0900248 BEGIN(INITIAL);
249
Masahiro Yamada72367932018-12-11 20:00:51 +0900250 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);
253
Roman Zippela02f0572005-11-08 21:34:53 -0800254 if (current_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 zconf_endfile();
Roman Zippela02f0572005-11-08 21:34:53 -0800256 return T_EOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 fclose(yyin);
259 yyterminate();
260}
261
262%%
Masahiro Yamadacc66bca2018-12-11 20:00:49 +0900263
264/* second stage lexer */
265int yylex(void)
266{
267 int token;
268
269repeat:
270 token = yylex1();
271
Masahiro Yamada4b31a322018-12-11 20:01:09 +0900272 if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
273 if (token == T_EOL) {
274 /* Do not pass unneeded T_EOL to the parser. */
275 goto repeat;
276 } else {
277 /*
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.
282 */
283 current_pos.file = current_file;
284 current_pos.lineno = yylineno;
285 }
286 }
Masahiro Yamadacc66bca2018-12-11 20:00:49 +0900287
Masahiro Yamada824fa3b2018-12-11 20:01:08 +0900288 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
289 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
290 BEGIN(ASSIGN_VAL);
291
292 prev_prev_token = prev_token;
Masahiro Yamadacc66bca2018-12-11 20:00:49 +0900293 prev_token = token;
294
295 return token;
296}
297
Masahiro Yamada104daea2018-05-28 18:21:40 +0900298static char *expand_token(const char *in, size_t n)
299{
300 char *out;
301 int c;
302 char c2;
303 const char *rest, *end;
304
305 new_string();
306 append_string(in, n);
307
308 /* get the whole line because we do not know the end of token. */
309 while ((c = input()) != EOF) {
310 if (c == '\n') {
311 unput(c);
312 break;
313 }
314 c2 = c;
315 append_string(&c2, 1);
316 }
317
318 rest = text;
319 out = expand_one_token(&rest);
320
321 /* push back unused characters to the input stream */
322 end = rest + strlen(rest);
323 while (end > rest)
324 unput(*--end);
325
326 free(text);
327
328 return out;
329}
330
331static void append_expanded_string(const char *str)
332{
333 const char *end;
334 char *res;
335
336 str++;
337
338 res = expand_dollar(&str);
339
340 /* push back unused characters to the input stream */
341 end = str + strlen(str);
342 while (end > str)
343 unput(*--end);
344
345 append_string(res, strlen(res));
346
347 free(res);
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350void zconf_starthelp(void)
351{
352 new_string();
353 last_ts = first_ts = 0;
354 BEGIN(HELP);
355}
356
357static void zconf_endhelp(void)
358{
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900359 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 BEGIN(INITIAL);
361}
362
363
364/*
365 * Try to open specified file with following names:
366 * ./name
367 * $(srctree)/name
368 * The latter is used when srctree is separate from objtree
369 * when compiling the kernel.
370 * Return NULL if file is not found.
371 */
372FILE *zconf_fopen(const char *name)
373{
374 char *env, fullname[PATH_MAX+1];
375 FILE *f;
376
377 f = fopen(name, "r");
Marcin Garski11de39e2007-05-05 22:49:00 +0200378 if (!f && name != NULL && name[0] != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 env = getenv(SRCTREE);
380 if (env) {
Jacob Garberb9d1a8e2019-05-10 13:28:52 -0600381 snprintf(fullname, sizeof(fullname),
382 "%s/%s", env, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 f = fopen(fullname, "r");
384 }
385 }
386 return f;
387}
388
389void zconf_initscan(const char *name)
390{
391 yyin = zconf_fopen(name);
392 if (!yyin) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900393 fprintf(stderr, "can't find file %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 exit(1);
395 }
396
Alan Cox177acf72012-11-06 14:32:08 +0000397 current_buf = xmalloc(sizeof(*current_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 memset(current_buf, 0, sizeof(*current_buf));
399
400 current_file = file_lookup(name);
Masahiro Yamada18492682018-03-23 02:00:14 +0900401 yylineno = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404void zconf_nextfile(const char *name)
405{
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100406 struct file *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct file *file = file_lookup(name);
Alan Cox177acf72012-11-06 14:32:08 +0000408 struct buffer *buf = xmalloc(sizeof(*buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 memset(buf, 0, sizeof(*buf));
410
411 current_buf->state = YY_CURRENT_BUFFER;
Arnaud Lacombee82dae92010-09-04 16:09:26 -0400412 yyin = zconf_fopen(file->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (!yyin) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900414 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
415 zconf_curname(), zconf_lineno(), file->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 exit(1);
417 }
418 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
419 buf->parent = current_buf;
420 current_buf = buf;
421
Masahiro Yamada18492682018-03-23 02:00:14 +0900422 current_file->lineno = yylineno;
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900423 file->parent = current_file;
424
425 for (iter = current_file; iter; iter = iter->parent) {
426 if (!strcmp(iter->name, file->name)) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900427 fprintf(stderr,
Masahiro Yamada32a94b82018-03-23 02:00:12 +0900428 "Recursive inclusion detected.\n"
429 "Inclusion path:\n"
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900430 " current file : %s\n", file->name);
431 iter = file;
Masahiro Yamada5ae6fcc2018-03-02 16:05:12 +0900432 do {
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100433 iter = iter->parent;
Masahiro Yamada32a94b82018-03-23 02:00:12 +0900434 fprintf(stderr, " included from: %s:%d\n",
Masahiro Yamada5ae6fcc2018-03-02 16:05:12 +0900435 iter->name, iter->lineno - 1);
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900436 } while (strcmp(iter->name, file->name));
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100437 exit(1);
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900440
Masahiro Yamada18492682018-03-23 02:00:14 +0900441 yylineno = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 current_file = file;
443}
444
Roman Zippela02f0572005-11-08 21:34:53 -0800445static void zconf_endfile(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct buffer *parent;
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 current_file = current_file->parent;
Masahiro Yamada18492682018-03-23 02:00:14 +0900450 if (current_file)
451 yylineno = current_file->lineno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 parent = current_buf->parent;
454 if (parent) {
455 fclose(yyin);
456 yy_delete_buffer(YY_CURRENT_BUFFER);
457 yy_switch_to_buffer(parent->state);
458 }
459 free(current_buf);
460 current_buf = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463int zconf_lineno(void)
464{
Roman Zippela02f0572005-11-08 21:34:53 -0800465 return current_pos.lineno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Arnaud Lacombe2e7a0912010-09-04 16:03:30 -0400468const char *zconf_curname(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Roman Zippela02f0572005-11-08 21:34:53 -0800470 return current_pos.file ? current_pos.file->name : "<none>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}