blob: 25bd2b89fe3f3b8d588cad2c72b3cc9b3ebae2cb [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
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +09003%x COMMAND HELP STRING PARAM ASSIGN_VAL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004%{
5/*
6 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
7 * Released under the terms of the GNU GPL v2.0.
8 */
9
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"
18
19#define START_STRSIZE 16
20
Roman Zippela02f0572005-11-08 21:34:53 -080021static struct {
22 struct file *file;
23 int lineno;
24} current_pos;
25
Roman Zippel7a884882005-11-08 21:34:51 -080026static char *text;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int text_size, text_asize;
28
29struct buffer {
Masahiro Yamadabb66fc62014-06-10 19:08:13 +090030 struct buffer *parent;
31 YY_BUFFER_STATE state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
34struct buffer *current_buf;
35
36static int last_ts, first_ts;
37
Masahiro Yamada104daea2018-05-28 18:21:40 +090038static char *expand_token(const char *in, size_t n);
39static void append_expanded_string(const char *in);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static void zconf_endhelp(void);
Roman Zippela02f0572005-11-08 21:34:53 -080041static void zconf_endfile(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Josh Triplett65166572009-10-15 12:13:36 -070043static void new_string(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Alan Cox177acf72012-11-06 14:32:08 +000045 text = xmalloc(START_STRSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 text_asize = START_STRSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 text_size = 0;
Roman Zippel7a884882005-11-08 21:34:51 -080048 *text = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Josh Triplett65166572009-10-15 12:13:36 -070051static void append_string(const char *str, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 int new_size = text_size + size + 1;
54 if (new_size > text_asize) {
Roman Zippel7a884882005-11-08 21:34:51 -080055 new_size += START_STRSIZE - 1;
56 new_size &= -START_STRSIZE;
Masahiro Yamadad717f242018-02-09 01:19:07 +090057 text = xrealloc(text, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 text_asize = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
Roman Zippel7a884882005-11-08 21:34:51 -080060 memcpy(text + text_size, str, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 text_size += size;
Roman Zippel7a884882005-11-08 21:34:51 -080062 text[text_size] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Josh Triplett65166572009-10-15 12:13:36 -070065static void alloc_string(const char *str, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Alan Cox177acf72012-11-06 14:32:08 +000067 text = xmalloc(size + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 memcpy(text, str, size);
69 text[size] = 0;
70}
Andreas Ruprechtc2264562015-07-12 09:41:50 +020071
72static void warn_ignored_character(char chr)
73{
74 fprintf(stderr,
75 "%s:%d:warning: ignoring unsupported character '%c'\n",
76 zconf_curname(), zconf_lineno(), chr);
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078%}
79
Andreas Ruprechtc2264562015-07-12 09:41:50 +020080n [A-Za-z0-9_-]
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82%%
83 int str = 0;
84 int ts, i;
85
Roman Zippela02f0572005-11-08 21:34:53 -080086[ \t]*#.*\n |
87[ \t]*\n {
Roman Zippela02f0572005-11-08 21:34:53 -080088 return T_EOL;
89}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090[ \t]*#.*
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93[ \t]+ {
94 BEGIN(COMMAND);
95}
96
97. {
98 unput(yytext[0]);
99 BEGIN(COMMAND);
100}
101
102
103<COMMAND>{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 {n}+ {
Arnaud Lacombe61f956f2011-05-04 21:14:44 -0400105 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
Roman Zippela02f0572005-11-08 21:34:53 -0800106 current_pos.file = current_file;
Masahiro Yamada18492682018-03-23 02:00:14 +0900107 current_pos.lineno = yylineno;
Roman Zippel7a884882005-11-08 21:34:51 -0800108 if (id && id->flags & TF_COMMAND) {
Masahiro Yamada9de07152018-05-28 18:21:48 +0900109 BEGIN(PARAM);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900110 yylval.id = id;
Roman Zippel7a884882005-11-08 21:34:51 -0800111 return id->token;
112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 alloc_string(yytext, yyleng);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900114 yylval.string = text;
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900115 return T_VARIABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
Masahiro Yamada82bc8bd2018-05-28 18:21:52 +0900117 ({n}|$)+ {
118 /* this token includes at least one '$' */
119 yylval.string = expand_token(yytext, yyleng);
120 if (strlen(yylval.string))
121 return T_VARIABLE;
122 free(yylval.string);
123 }
Masahiro Yamada1175c022018-05-28 18:21:50 +0900124 "=" { BEGIN(ASSIGN_VAL); yylval.flavor = VAR_RECURSIVE; return T_ASSIGN; }
125 ":=" { BEGIN(ASSIGN_VAL); yylval.flavor = VAR_SIMPLE; return T_ASSIGN; }
Masahiro Yamadaed2a22f2018-05-28 18:21:51 +0900126 "+=" { BEGIN(ASSIGN_VAL); yylval.flavor = VAR_APPEND; return T_ASSIGN; }
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900127 [[:blank:]]+
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200128 . warn_ignored_character(*yytext);
Roman Zippela02f0572005-11-08 21:34:53 -0800129 \n {
130 BEGIN(INITIAL);
Roman Zippela02f0572005-11-08 21:34:53 -0800131 return T_EOL;
132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900135<ASSIGN_VAL>{
136 [^[:blank:]\n]+.* {
137 alloc_string(yytext, yyleng);
138 yylval.string = text;
139 return T_ASSIGN_VAL;
140 }
141 \n { BEGIN(INITIAL); return T_EOL; }
142 .
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145<PARAM>{
146 "&&" return T_AND;
147 "||" return T_OR;
148 "(" return T_OPEN_PAREN;
149 ")" return T_CLOSE_PAREN;
150 "!" return T_NOT;
151 "=" return T_EQUAL;
152 "!=" return T_UNEQUAL;
Jan Beulich31847b62015-06-15 13:00:21 +0100153 "<=" return T_LESS_EQUAL;
154 ">=" return T_GREATER_EQUAL;
155 "<" return T_LESS;
156 ">" return T_GREATER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 \"|\' {
158 str = yytext[0];
159 new_string();
160 BEGIN(STRING);
161 }
Masahiro Yamada18492682018-03-23 02:00:14 +0900162 \n BEGIN(INITIAL); return T_EOL;
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200163 ({n}|[/.])+ {
Arnaud Lacombe61f956f2011-05-04 21:14:44 -0400164 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
Roman Zippel3370f9f2005-11-08 21:34:52 -0800165 if (id && id->flags & TF_PARAM) {
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900166 yylval.id = id;
Roman Zippel7a884882005-11-08 21:34:51 -0800167 return id->token;
Roman Zippel3370f9f2005-11-08 21:34:52 -0800168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 alloc_string(yytext, yyleng);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900170 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return T_WORD;
172 }
Masahiro Yamada104daea2018-05-28 18:21:40 +0900173 ({n}|[/.$])+ {
174 /* this token includes at least one '$' */
175 yylval.string = expand_token(yytext, yyleng);
176 if (strlen(yylval.string))
177 return T_WORD;
178 free(yylval.string);
179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 #.* /* comment */
Masahiro Yamada18492682018-03-23 02:00:14 +0900181 \\\n ;
Jan Beulich2e0d7372015-01-20 12:52:48 +0000182 [[:blank:]]+
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200183 . warn_ignored_character(*yytext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 <<EOF>> {
185 BEGIN(INITIAL);
186 }
187}
188
189<STRING>{
Masahiro Yamada104daea2018-05-28 18:21:40 +0900190 "$".* append_expanded_string(yytext);
191 [^$'"\\\n]+/\n {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 append_string(yytext, yyleng);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900193 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return T_WORD_QUOTE;
195 }
Masahiro Yamada104daea2018-05-28 18:21:40 +0900196 [^$'"\\\n]+ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 append_string(yytext, yyleng);
198 }
199 \\.?/\n {
200 append_string(yytext + 1, yyleng - 1);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900201 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return T_WORD_QUOTE;
203 }
204 \\.? {
205 append_string(yytext + 1, yyleng - 1);
206 }
207 \'|\" {
208 if (str == yytext[0]) {
209 BEGIN(PARAM);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900210 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return T_WORD_QUOTE;
212 } else
213 append_string(yytext, 1);
214 }
215 \n {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900216 fprintf(stderr,
217 "%s:%d:warning: multi-line strings not supported\n",
218 zconf_curname(), zconf_lineno());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 BEGIN(INITIAL);
220 return T_EOL;
221 }
222 <<EOF>> {
223 BEGIN(INITIAL);
224 }
225}
226
227<HELP>{
228 [ \t]+ {
229 ts = 0;
230 for (i = 0; i < yyleng; i++) {
231 if (yytext[i] == '\t')
232 ts = (ts & ~7) + 8;
233 else
234 ts++;
235 }
236 last_ts = ts;
237 if (first_ts) {
238 if (ts < first_ts) {
239 zconf_endhelp();
240 return T_HELPTEXT;
241 }
242 ts -= first_ts;
243 while (ts > 8) {
244 append_string(" ", 8);
245 ts -= 8;
246 }
247 append_string(" ", ts);
248 }
249 }
250 [ \t]*\n/[^ \t\n] {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 zconf_endhelp();
252 return T_HELPTEXT;
253 }
254 [ \t]*\n {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 append_string("\n", 1);
256 }
257 [^ \t\n].* {
EGRY Gaborf7a4b4c2008-01-11 23:55:20 +0100258 while (yyleng) {
259 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
260 break;
261 yyleng--;
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 append_string(yytext, yyleng);
264 if (!first_ts)
265 first_ts = last_ts;
266 }
267 <<EOF>> {
268 zconf_endhelp();
269 return T_HELPTEXT;
270 }
271}
272
273<<EOF>> {
Roman Zippela02f0572005-11-08 21:34:53 -0800274 if (current_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 zconf_endfile();
Roman Zippela02f0572005-11-08 21:34:53 -0800276 return T_EOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 fclose(yyin);
279 yyterminate();
280}
281
282%%
Masahiro Yamada104daea2018-05-28 18:21:40 +0900283static char *expand_token(const char *in, size_t n)
284{
285 char *out;
286 int c;
287 char c2;
288 const char *rest, *end;
289
290 new_string();
291 append_string(in, n);
292
293 /* get the whole line because we do not know the end of token. */
294 while ((c = input()) != EOF) {
295 if (c == '\n') {
296 unput(c);
297 break;
298 }
299 c2 = c;
300 append_string(&c2, 1);
301 }
302
303 rest = text;
304 out = expand_one_token(&rest);
305
306 /* push back unused characters to the input stream */
307 end = rest + strlen(rest);
308 while (end > rest)
309 unput(*--end);
310
311 free(text);
312
313 return out;
314}
315
316static void append_expanded_string(const char *str)
317{
318 const char *end;
319 char *res;
320
321 str++;
322
323 res = expand_dollar(&str);
324
325 /* push back unused characters to the input stream */
326 end = str + strlen(str);
327 while (end > str)
328 unput(*--end);
329
330 append_string(res, strlen(res));
331
332 free(res);
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335void zconf_starthelp(void)
336{
337 new_string();
338 last_ts = first_ts = 0;
339 BEGIN(HELP);
340}
341
342static void zconf_endhelp(void)
343{
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900344 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 BEGIN(INITIAL);
346}
347
348
349/*
350 * Try to open specified file with following names:
351 * ./name
352 * $(srctree)/name
353 * The latter is used when srctree is separate from objtree
354 * when compiling the kernel.
355 * Return NULL if file is not found.
356 */
357FILE *zconf_fopen(const char *name)
358{
359 char *env, fullname[PATH_MAX+1];
360 FILE *f;
361
362 f = fopen(name, "r");
Marcin Garski11de39e2007-05-05 22:49:00 +0200363 if (!f && name != NULL && name[0] != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 env = getenv(SRCTREE);
365 if (env) {
366 sprintf(fullname, "%s/%s", env, name);
367 f = fopen(fullname, "r");
368 }
369 }
370 return f;
371}
372
373void zconf_initscan(const char *name)
374{
375 yyin = zconf_fopen(name);
376 if (!yyin) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900377 fprintf(stderr, "can't find file %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 exit(1);
379 }
380
Alan Cox177acf72012-11-06 14:32:08 +0000381 current_buf = xmalloc(sizeof(*current_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 memset(current_buf, 0, sizeof(*current_buf));
383
384 current_file = file_lookup(name);
Masahiro Yamada18492682018-03-23 02:00:14 +0900385 yylineno = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388void zconf_nextfile(const char *name)
389{
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100390 struct file *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct file *file = file_lookup(name);
Alan Cox177acf72012-11-06 14:32:08 +0000392 struct buffer *buf = xmalloc(sizeof(*buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 memset(buf, 0, sizeof(*buf));
394
395 current_buf->state = YY_CURRENT_BUFFER;
Arnaud Lacombee82dae92010-09-04 16:09:26 -0400396 yyin = zconf_fopen(file->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!yyin) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900398 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
399 zconf_curname(), zconf_lineno(), file->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 exit(1);
401 }
402 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
403 buf->parent = current_buf;
404 current_buf = buf;
405
Masahiro Yamada18492682018-03-23 02:00:14 +0900406 current_file->lineno = yylineno;
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900407 file->parent = current_file;
408
409 for (iter = current_file; iter; iter = iter->parent) {
410 if (!strcmp(iter->name, file->name)) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900411 fprintf(stderr,
Masahiro Yamada32a94b82018-03-23 02:00:12 +0900412 "Recursive inclusion detected.\n"
413 "Inclusion path:\n"
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900414 " current file : %s\n", file->name);
415 iter = file;
Masahiro Yamada5ae6fcc2018-03-02 16:05:12 +0900416 do {
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100417 iter = iter->parent;
Masahiro Yamada32a94b82018-03-23 02:00:12 +0900418 fprintf(stderr, " included from: %s:%d\n",
Masahiro Yamada5ae6fcc2018-03-02 16:05:12 +0900419 iter->name, iter->lineno - 1);
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900420 } while (strcmp(iter->name, file->name));
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100421 exit(1);
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900424
Masahiro Yamada18492682018-03-23 02:00:14 +0900425 yylineno = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 current_file = file;
427}
428
Roman Zippela02f0572005-11-08 21:34:53 -0800429static void zconf_endfile(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct buffer *parent;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 current_file = current_file->parent;
Masahiro Yamada18492682018-03-23 02:00:14 +0900434 if (current_file)
435 yylineno = current_file->lineno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 parent = current_buf->parent;
438 if (parent) {
439 fclose(yyin);
440 yy_delete_buffer(YY_CURRENT_BUFFER);
441 yy_switch_to_buffer(parent->state);
442 }
443 free(current_buf);
444 current_buf = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447int zconf_lineno(void)
448{
Roman Zippela02f0572005-11-08 21:34:53 -0800449 return current_pos.lineno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Arnaud Lacombe2e7a0912010-09-04 16:03:30 -0400452const char *zconf_curname(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Roman Zippela02f0572005-11-08 21:34:53 -0800454 return current_pos.file ? current_pos.file->name : "<none>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}