blob: 9a147977dc3f8fac0b063e1fa86e1affcb448dd1 [file] [log] [blame]
Arnaud Lacombe674eed82011-06-07 13:34:05 -04001%option nostdinit noyywrap never-interactive full ecs
Masahiro Yamada18492682018-03-23 02:00:14 +09002%option 8bit nodefault yylineno
Linus Torvalds1da177e2005-04-16 15:20:36 -07003%x COMMAND HELP STRING PARAM
4%{
5/*
6 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
7 * Released under the terms of the GNU GPL v2.0.
8 */
9
10#include <limits.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <unistd.h>
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "lkc.h"
17
18#define START_STRSIZE 16
19
Roman Zippela02f0572005-11-08 21:34:53 -080020static struct {
21 struct file *file;
22 int lineno;
23} current_pos;
24
Roman Zippel7a884882005-11-08 21:34:51 -080025static char *text;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static int text_size, text_asize;
27
28struct buffer {
Masahiro Yamadabb66fc62014-06-10 19:08:13 +090029 struct buffer *parent;
30 YY_BUFFER_STATE state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
33struct buffer *current_buf;
34
35static int last_ts, first_ts;
36
Masahiro Yamada104daea2018-05-28 18:21:40 +090037static char *expand_token(const char *in, size_t n);
38static void append_expanded_string(const char *in);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static void zconf_endhelp(void);
Roman Zippela02f0572005-11-08 21:34:53 -080040static void zconf_endfile(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Josh Triplett65166572009-10-15 12:13:36 -070042static void new_string(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Alan Cox177acf72012-11-06 14:32:08 +000044 text = xmalloc(START_STRSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 text_asize = START_STRSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 text_size = 0;
Roman Zippel7a884882005-11-08 21:34:51 -080047 *text = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Josh Triplett65166572009-10-15 12:13:36 -070050static void append_string(const char *str, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 int new_size = text_size + size + 1;
53 if (new_size > text_asize) {
Roman Zippel7a884882005-11-08 21:34:51 -080054 new_size += START_STRSIZE - 1;
55 new_size &= -START_STRSIZE;
Masahiro Yamadad717f242018-02-09 01:19:07 +090056 text = xrealloc(text, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 text_asize = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Roman Zippel7a884882005-11-08 21:34:51 -080059 memcpy(text + text_size, str, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 text_size += size;
Roman Zippel7a884882005-11-08 21:34:51 -080061 text[text_size] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
Josh Triplett65166572009-10-15 12:13:36 -070064static void alloc_string(const char *str, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Alan Cox177acf72012-11-06 14:32:08 +000066 text = xmalloc(size + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 memcpy(text, str, size);
68 text[size] = 0;
69}
Andreas Ruprechtc2264562015-07-12 09:41:50 +020070
71static void warn_ignored_character(char chr)
72{
73 fprintf(stderr,
74 "%s:%d:warning: ignoring unsupported character '%c'\n",
75 zconf_curname(), zconf_lineno(), chr);
76}
Linus Torvalds1da177e2005-04-16 15:20:36 -070077%}
78
Andreas Ruprechtc2264562015-07-12 09:41:50 +020079n [A-Za-z0-9_-]
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81%%
82 int str = 0;
83 int ts, i;
84
Roman Zippela02f0572005-11-08 21:34:53 -080085[ \t]*#.*\n |
86[ \t]*\n {
Roman Zippela02f0572005-11-08 21:34:53 -080087 return T_EOL;
88}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089[ \t]*#.*
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92[ \t]+ {
93 BEGIN(COMMAND);
94}
95
96. {
97 unput(yytext[0]);
98 BEGIN(COMMAND);
99}
100
101
102<COMMAND>{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 {n}+ {
Arnaud Lacombe61f956f2011-05-04 21:14:44 -0400104 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
Roman Zippela02f0572005-11-08 21:34:53 -0800105 current_pos.file = current_file;
Masahiro Yamada18492682018-03-23 02:00:14 +0900106 current_pos.lineno = yylineno;
Roman Zippel7a884882005-11-08 21:34:51 -0800107 if (id && id->flags & TF_COMMAND) {
Masahiro Yamada9de07152018-05-28 18:21:48 +0900108 BEGIN(PARAM);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900109 yylval.id = id;
Roman Zippel7a884882005-11-08 21:34:51 -0800110 return id->token;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 alloc_string(yytext, yyleng);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900113 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return T_WORD;
115 }
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200116 . warn_ignored_character(*yytext);
Roman Zippela02f0572005-11-08 21:34:53 -0800117 \n {
118 BEGIN(INITIAL);
Roman Zippela02f0572005-11-08 21:34:53 -0800119 return T_EOL;
120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123<PARAM>{
124 "&&" return T_AND;
125 "||" return T_OR;
126 "(" return T_OPEN_PAREN;
127 ")" return T_CLOSE_PAREN;
128 "!" return T_NOT;
129 "=" return T_EQUAL;
130 "!=" return T_UNEQUAL;
Jan Beulich31847b62015-06-15 13:00:21 +0100131 "<=" return T_LESS_EQUAL;
132 ">=" return T_GREATER_EQUAL;
133 "<" return T_LESS;
134 ">" return T_GREATER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 \"|\' {
136 str = yytext[0];
137 new_string();
138 BEGIN(STRING);
139 }
Masahiro Yamada18492682018-03-23 02:00:14 +0900140 \n BEGIN(INITIAL); return T_EOL;
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200141 ({n}|[/.])+ {
Arnaud Lacombe61f956f2011-05-04 21:14:44 -0400142 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
Roman Zippel3370f9f2005-11-08 21:34:52 -0800143 if (id && id->flags & TF_PARAM) {
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900144 yylval.id = id;
Roman Zippel7a884882005-11-08 21:34:51 -0800145 return id->token;
Roman Zippel3370f9f2005-11-08 21:34:52 -0800146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 alloc_string(yytext, yyleng);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900148 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return T_WORD;
150 }
Masahiro Yamada104daea2018-05-28 18:21:40 +0900151 ({n}|[/.$])+ {
152 /* this token includes at least one '$' */
153 yylval.string = expand_token(yytext, yyleng);
154 if (strlen(yylval.string))
155 return T_WORD;
156 free(yylval.string);
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 #.* /* comment */
Masahiro Yamada18492682018-03-23 02:00:14 +0900159 \\\n ;
Jan Beulich2e0d7372015-01-20 12:52:48 +0000160 [[:blank:]]+
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200161 . warn_ignored_character(*yytext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 <<EOF>> {
163 BEGIN(INITIAL);
164 }
165}
166
167<STRING>{
Masahiro Yamada104daea2018-05-28 18:21:40 +0900168 "$".* append_expanded_string(yytext);
169 [^$'"\\\n]+/\n {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 append_string(yytext, yyleng);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900171 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return T_WORD_QUOTE;
173 }
Masahiro Yamada104daea2018-05-28 18:21:40 +0900174 [^$'"\\\n]+ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 append_string(yytext, yyleng);
176 }
177 \\.?/\n {
178 append_string(yytext + 1, yyleng - 1);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900179 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return T_WORD_QUOTE;
181 }
182 \\.? {
183 append_string(yytext + 1, yyleng - 1);
184 }
185 \'|\" {
186 if (str == yytext[0]) {
187 BEGIN(PARAM);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900188 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return T_WORD_QUOTE;
190 } else
191 append_string(yytext, 1);
192 }
193 \n {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900194 fprintf(stderr,
195 "%s:%d:warning: multi-line strings not supported\n",
196 zconf_curname(), zconf_lineno());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 BEGIN(INITIAL);
198 return T_EOL;
199 }
200 <<EOF>> {
201 BEGIN(INITIAL);
202 }
203}
204
205<HELP>{
206 [ \t]+ {
207 ts = 0;
208 for (i = 0; i < yyleng; i++) {
209 if (yytext[i] == '\t')
210 ts = (ts & ~7) + 8;
211 else
212 ts++;
213 }
214 last_ts = ts;
215 if (first_ts) {
216 if (ts < first_ts) {
217 zconf_endhelp();
218 return T_HELPTEXT;
219 }
220 ts -= first_ts;
221 while (ts > 8) {
222 append_string(" ", 8);
223 ts -= 8;
224 }
225 append_string(" ", ts);
226 }
227 }
228 [ \t]*\n/[^ \t\n] {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 zconf_endhelp();
230 return T_HELPTEXT;
231 }
232 [ \t]*\n {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 append_string("\n", 1);
234 }
235 [^ \t\n].* {
EGRY Gaborf7a4b4c2008-01-11 23:55:20 +0100236 while (yyleng) {
237 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
238 break;
239 yyleng--;
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 append_string(yytext, yyleng);
242 if (!first_ts)
243 first_ts = last_ts;
244 }
245 <<EOF>> {
246 zconf_endhelp();
247 return T_HELPTEXT;
248 }
249}
250
251<<EOF>> {
Roman Zippela02f0572005-11-08 21:34:53 -0800252 if (current_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 zconf_endfile();
Roman Zippela02f0572005-11-08 21:34:53 -0800254 return T_EOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256 fclose(yyin);
257 yyterminate();
258}
259
260%%
Masahiro Yamada104daea2018-05-28 18:21:40 +0900261static char *expand_token(const char *in, size_t n)
262{
263 char *out;
264 int c;
265 char c2;
266 const char *rest, *end;
267
268 new_string();
269 append_string(in, n);
270
271 /* get the whole line because we do not know the end of token. */
272 while ((c = input()) != EOF) {
273 if (c == '\n') {
274 unput(c);
275 break;
276 }
277 c2 = c;
278 append_string(&c2, 1);
279 }
280
281 rest = text;
282 out = expand_one_token(&rest);
283
284 /* push back unused characters to the input stream */
285 end = rest + strlen(rest);
286 while (end > rest)
287 unput(*--end);
288
289 free(text);
290
291 return out;
292}
293
294static void append_expanded_string(const char *str)
295{
296 const char *end;
297 char *res;
298
299 str++;
300
301 res = expand_dollar(&str);
302
303 /* push back unused characters to the input stream */
304 end = str + strlen(str);
305 while (end > str)
306 unput(*--end);
307
308 append_string(res, strlen(res));
309
310 free(res);
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313void zconf_starthelp(void)
314{
315 new_string();
316 last_ts = first_ts = 0;
317 BEGIN(HELP);
318}
319
320static void zconf_endhelp(void)
321{
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900322 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 BEGIN(INITIAL);
324}
325
326
327/*
328 * Try to open specified file with following names:
329 * ./name
330 * $(srctree)/name
331 * The latter is used when srctree is separate from objtree
332 * when compiling the kernel.
333 * Return NULL if file is not found.
334 */
335FILE *zconf_fopen(const char *name)
336{
337 char *env, fullname[PATH_MAX+1];
338 FILE *f;
339
340 f = fopen(name, "r");
Marcin Garski11de39e2007-05-05 22:49:00 +0200341 if (!f && name != NULL && name[0] != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 env = getenv(SRCTREE);
343 if (env) {
344 sprintf(fullname, "%s/%s", env, name);
345 f = fopen(fullname, "r");
346 }
347 }
348 return f;
349}
350
351void zconf_initscan(const char *name)
352{
353 yyin = zconf_fopen(name);
354 if (!yyin) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900355 fprintf(stderr, "can't find file %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 exit(1);
357 }
358
Alan Cox177acf72012-11-06 14:32:08 +0000359 current_buf = xmalloc(sizeof(*current_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 memset(current_buf, 0, sizeof(*current_buf));
361
362 current_file = file_lookup(name);
Masahiro Yamada18492682018-03-23 02:00:14 +0900363 yylineno = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366void zconf_nextfile(const char *name)
367{
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100368 struct file *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct file *file = file_lookup(name);
Alan Cox177acf72012-11-06 14:32:08 +0000370 struct buffer *buf = xmalloc(sizeof(*buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 memset(buf, 0, sizeof(*buf));
372
373 current_buf->state = YY_CURRENT_BUFFER;
Arnaud Lacombee82dae92010-09-04 16:09:26 -0400374 yyin = zconf_fopen(file->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (!yyin) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900376 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
377 zconf_curname(), zconf_lineno(), file->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 exit(1);
379 }
380 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
381 buf->parent = current_buf;
382 current_buf = buf;
383
Masahiro Yamada18492682018-03-23 02:00:14 +0900384 current_file->lineno = yylineno;
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900385 file->parent = current_file;
386
387 for (iter = current_file; iter; iter = iter->parent) {
388 if (!strcmp(iter->name, file->name)) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900389 fprintf(stderr,
Masahiro Yamada32a94b82018-03-23 02:00:12 +0900390 "Recursive inclusion detected.\n"
391 "Inclusion path:\n"
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900392 " current file : %s\n", file->name);
393 iter = file;
Masahiro Yamada5ae6fcc2018-03-02 16:05:12 +0900394 do {
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100395 iter = iter->parent;
Masahiro Yamada32a94b82018-03-23 02:00:12 +0900396 fprintf(stderr, " included from: %s:%d\n",
Masahiro Yamada5ae6fcc2018-03-02 16:05:12 +0900397 iter->name, iter->lineno - 1);
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900398 } while (strcmp(iter->name, file->name));
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100399 exit(1);
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900402
Masahiro Yamada18492682018-03-23 02:00:14 +0900403 yylineno = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 current_file = file;
405}
406
Roman Zippela02f0572005-11-08 21:34:53 -0800407static void zconf_endfile(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 struct buffer *parent;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 current_file = current_file->parent;
Masahiro Yamada18492682018-03-23 02:00:14 +0900412 if (current_file)
413 yylineno = current_file->lineno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 parent = current_buf->parent;
416 if (parent) {
417 fclose(yyin);
418 yy_delete_buffer(YY_CURRENT_BUFFER);
419 yy_switch_to_buffer(parent->state);
420 }
421 free(current_buf);
422 current_buf = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425int zconf_lineno(void)
426{
Roman Zippela02f0572005-11-08 21:34:53 -0800427 return current_pos.lineno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Arnaud Lacombe2e7a0912010-09-04 16:03:30 -0400430const char *zconf_curname(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Roman Zippela02f0572005-11-08 21:34:53 -0800432 return current_pos.file ? current_pos.file->name : "<none>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}