blob: dd08f7a38ccd23475b3338b9b83deae3d0d889cf [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 Yamada9ced3bd2018-05-28 18:21:49 +0900117 "=" { BEGIN(ASSIGN_VAL); return T_ASSIGN; }
118 [[:blank:]]+
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200119 . warn_ignored_character(*yytext);
Roman Zippela02f0572005-11-08 21:34:53 -0800120 \n {
121 BEGIN(INITIAL);
Roman Zippela02f0572005-11-08 21:34:53 -0800122 return T_EOL;
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900126<ASSIGN_VAL>{
127 [^[:blank:]\n]+.* {
128 alloc_string(yytext, yyleng);
129 yylval.string = text;
130 return T_ASSIGN_VAL;
131 }
132 \n { BEGIN(INITIAL); return T_EOL; }
133 .
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136<PARAM>{
137 "&&" return T_AND;
138 "||" return T_OR;
139 "(" return T_OPEN_PAREN;
140 ")" return T_CLOSE_PAREN;
141 "!" return T_NOT;
142 "=" return T_EQUAL;
143 "!=" return T_UNEQUAL;
Jan Beulich31847b62015-06-15 13:00:21 +0100144 "<=" return T_LESS_EQUAL;
145 ">=" return T_GREATER_EQUAL;
146 "<" return T_LESS;
147 ">" return T_GREATER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 \"|\' {
149 str = yytext[0];
150 new_string();
151 BEGIN(STRING);
152 }
Masahiro Yamada18492682018-03-23 02:00:14 +0900153 \n BEGIN(INITIAL); return T_EOL;
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200154 ({n}|[/.])+ {
Arnaud Lacombe61f956f2011-05-04 21:14:44 -0400155 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
Roman Zippel3370f9f2005-11-08 21:34:52 -0800156 if (id && id->flags & TF_PARAM) {
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900157 yylval.id = id;
Roman Zippel7a884882005-11-08 21:34:51 -0800158 return id->token;
Roman Zippel3370f9f2005-11-08 21:34:52 -0800159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 alloc_string(yytext, yyleng);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900161 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return T_WORD;
163 }
Masahiro Yamada104daea2018-05-28 18:21:40 +0900164 ({n}|[/.$])+ {
165 /* this token includes at least one '$' */
166 yylval.string = expand_token(yytext, yyleng);
167 if (strlen(yylval.string))
168 return T_WORD;
169 free(yylval.string);
170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 #.* /* comment */
Masahiro Yamada18492682018-03-23 02:00:14 +0900172 \\\n ;
Jan Beulich2e0d7372015-01-20 12:52:48 +0000173 [[:blank:]]+
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200174 . warn_ignored_character(*yytext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 <<EOF>> {
176 BEGIN(INITIAL);
177 }
178}
179
180<STRING>{
Masahiro Yamada104daea2018-05-28 18:21:40 +0900181 "$".* append_expanded_string(yytext);
182 [^$'"\\\n]+/\n {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 append_string(yytext, yyleng);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900184 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return T_WORD_QUOTE;
186 }
Masahiro Yamada104daea2018-05-28 18:21:40 +0900187 [^$'"\\\n]+ {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 append_string(yytext, yyleng);
189 }
190 \\.?/\n {
191 append_string(yytext + 1, yyleng - 1);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900192 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return T_WORD_QUOTE;
194 }
195 \\.? {
196 append_string(yytext + 1, yyleng - 1);
197 }
198 \'|\" {
199 if (str == yytext[0]) {
200 BEGIN(PARAM);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900201 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return T_WORD_QUOTE;
203 } else
204 append_string(yytext, 1);
205 }
206 \n {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900207 fprintf(stderr,
208 "%s:%d:warning: multi-line strings not supported\n",
209 zconf_curname(), zconf_lineno());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 BEGIN(INITIAL);
211 return T_EOL;
212 }
213 <<EOF>> {
214 BEGIN(INITIAL);
215 }
216}
217
218<HELP>{
219 [ \t]+ {
220 ts = 0;
221 for (i = 0; i < yyleng; i++) {
222 if (yytext[i] == '\t')
223 ts = (ts & ~7) + 8;
224 else
225 ts++;
226 }
227 last_ts = ts;
228 if (first_ts) {
229 if (ts < first_ts) {
230 zconf_endhelp();
231 return T_HELPTEXT;
232 }
233 ts -= first_ts;
234 while (ts > 8) {
235 append_string(" ", 8);
236 ts -= 8;
237 }
238 append_string(" ", ts);
239 }
240 }
241 [ \t]*\n/[^ \t\n] {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 zconf_endhelp();
243 return T_HELPTEXT;
244 }
245 [ \t]*\n {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 append_string("\n", 1);
247 }
248 [^ \t\n].* {
EGRY Gaborf7a4b4c2008-01-11 23:55:20 +0100249 while (yyleng) {
250 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
251 break;
252 yyleng--;
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 append_string(yytext, yyleng);
255 if (!first_ts)
256 first_ts = last_ts;
257 }
258 <<EOF>> {
259 zconf_endhelp();
260 return T_HELPTEXT;
261 }
262}
263
264<<EOF>> {
Roman Zippela02f0572005-11-08 21:34:53 -0800265 if (current_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 zconf_endfile();
Roman Zippela02f0572005-11-08 21:34:53 -0800267 return T_EOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269 fclose(yyin);
270 yyterminate();
271}
272
273%%
Masahiro Yamada104daea2018-05-28 18:21:40 +0900274static char *expand_token(const char *in, size_t n)
275{
276 char *out;
277 int c;
278 char c2;
279 const char *rest, *end;
280
281 new_string();
282 append_string(in, n);
283
284 /* get the whole line because we do not know the end of token. */
285 while ((c = input()) != EOF) {
286 if (c == '\n') {
287 unput(c);
288 break;
289 }
290 c2 = c;
291 append_string(&c2, 1);
292 }
293
294 rest = text;
295 out = expand_one_token(&rest);
296
297 /* push back unused characters to the input stream */
298 end = rest + strlen(rest);
299 while (end > rest)
300 unput(*--end);
301
302 free(text);
303
304 return out;
305}
306
307static void append_expanded_string(const char *str)
308{
309 const char *end;
310 char *res;
311
312 str++;
313
314 res = expand_dollar(&str);
315
316 /* push back unused characters to the input stream */
317 end = str + strlen(str);
318 while (end > str)
319 unput(*--end);
320
321 append_string(res, strlen(res));
322
323 free(res);
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326void zconf_starthelp(void)
327{
328 new_string();
329 last_ts = first_ts = 0;
330 BEGIN(HELP);
331}
332
333static void zconf_endhelp(void)
334{
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900335 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 BEGIN(INITIAL);
337}
338
339
340/*
341 * Try to open specified file with following names:
342 * ./name
343 * $(srctree)/name
344 * The latter is used when srctree is separate from objtree
345 * when compiling the kernel.
346 * Return NULL if file is not found.
347 */
348FILE *zconf_fopen(const char *name)
349{
350 char *env, fullname[PATH_MAX+1];
351 FILE *f;
352
353 f = fopen(name, "r");
Marcin Garski11de39e2007-05-05 22:49:00 +0200354 if (!f && name != NULL && name[0] != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 env = getenv(SRCTREE);
356 if (env) {
357 sprintf(fullname, "%s/%s", env, name);
358 f = fopen(fullname, "r");
359 }
360 }
361 return f;
362}
363
364void zconf_initscan(const char *name)
365{
366 yyin = zconf_fopen(name);
367 if (!yyin) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900368 fprintf(stderr, "can't find file %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 exit(1);
370 }
371
Alan Cox177acf72012-11-06 14:32:08 +0000372 current_buf = xmalloc(sizeof(*current_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 memset(current_buf, 0, sizeof(*current_buf));
374
375 current_file = file_lookup(name);
Masahiro Yamada18492682018-03-23 02:00:14 +0900376 yylineno = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379void zconf_nextfile(const char *name)
380{
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100381 struct file *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct file *file = file_lookup(name);
Alan Cox177acf72012-11-06 14:32:08 +0000383 struct buffer *buf = xmalloc(sizeof(*buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 memset(buf, 0, sizeof(*buf));
385
386 current_buf->state = YY_CURRENT_BUFFER;
Arnaud Lacombee82dae92010-09-04 16:09:26 -0400387 yyin = zconf_fopen(file->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (!yyin) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900389 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
390 zconf_curname(), zconf_lineno(), file->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 exit(1);
392 }
393 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
394 buf->parent = current_buf;
395 current_buf = buf;
396
Masahiro Yamada18492682018-03-23 02:00:14 +0900397 current_file->lineno = yylineno;
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900398 file->parent = current_file;
399
400 for (iter = current_file; iter; iter = iter->parent) {
401 if (!strcmp(iter->name, file->name)) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900402 fprintf(stderr,
Masahiro Yamada32a94b82018-03-23 02:00:12 +0900403 "Recursive inclusion detected.\n"
404 "Inclusion path:\n"
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900405 " current file : %s\n", file->name);
406 iter = file;
Masahiro Yamada5ae6fcc2018-03-02 16:05:12 +0900407 do {
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100408 iter = iter->parent;
Masahiro Yamada32a94b82018-03-23 02:00:12 +0900409 fprintf(stderr, " included from: %s:%d\n",
Masahiro Yamada5ae6fcc2018-03-02 16:05:12 +0900410 iter->name, iter->lineno - 1);
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900411 } while (strcmp(iter->name, file->name));
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100412 exit(1);
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900415
Masahiro Yamada18492682018-03-23 02:00:14 +0900416 yylineno = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 current_file = file;
418}
419
Roman Zippela02f0572005-11-08 21:34:53 -0800420static void zconf_endfile(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct buffer *parent;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 current_file = current_file->parent;
Masahiro Yamada18492682018-03-23 02:00:14 +0900425 if (current_file)
426 yylineno = current_file->lineno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 parent = current_buf->parent;
429 if (parent) {
430 fclose(yyin);
431 yy_delete_buffer(YY_CURRENT_BUFFER);
432 yy_switch_to_buffer(parent->state);
433 }
434 free(current_buf);
435 current_buf = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438int zconf_lineno(void)
439{
Roman Zippela02f0572005-11-08 21:34:53 -0800440 return current_pos.lineno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Arnaud Lacombe2e7a0912010-09-04 16:03:30 -0400443const char *zconf_curname(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Roman Zippela02f0572005-11-08 21:34:53 -0800445 return current_pos.file ? current_pos.file->name : "<none>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}