blob: 045093d827e11640c67fdb2c3ca4bb85734d2a7e [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
Adrian Bunkbe2be1d2008-07-17 02:07:59 +03003%option noinput
Linus Torvalds1da177e2005-04-16 15:20:36 -07004%x COMMAND HELP STRING PARAM
5%{
6/*
7 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
8 * Released under the terms of the GNU GPL v2.0.
9 */
10
11#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
38static void zconf_endhelp(void);
Roman Zippela02f0572005-11-08 21:34:53 -080039static void zconf_endfile(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Josh Triplett65166572009-10-15 12:13:36 -070041static void new_string(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Alan Cox177acf72012-11-06 14:32:08 +000043 text = xmalloc(START_STRSIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 text_asize = START_STRSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 text_size = 0;
Roman Zippel7a884882005-11-08 21:34:51 -080046 *text = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Josh Triplett65166572009-10-15 12:13:36 -070049static void append_string(const char *str, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 int new_size = text_size + size + 1;
52 if (new_size > text_asize) {
Roman Zippel7a884882005-11-08 21:34:51 -080053 new_size += START_STRSIZE - 1;
54 new_size &= -START_STRSIZE;
Masahiro Yamadad717f242018-02-09 01:19:07 +090055 text = xrealloc(text, new_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 text_asize = new_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
Roman Zippel7a884882005-11-08 21:34:51 -080058 memcpy(text + text_size, str, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 text_size += size;
Roman Zippel7a884882005-11-08 21:34:51 -080060 text[text_size] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Josh Triplett65166572009-10-15 12:13:36 -070063static void alloc_string(const char *str, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Alan Cox177acf72012-11-06 14:32:08 +000065 text = xmalloc(size + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 memcpy(text, str, size);
67 text[size] = 0;
68}
Andreas Ruprechtc2264562015-07-12 09:41:50 +020069
70static void warn_ignored_character(char chr)
71{
72 fprintf(stderr,
73 "%s:%d:warning: ignoring unsupported character '%c'\n",
74 zconf_curname(), zconf_lineno(), chr);
75}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076%}
77
Andreas Ruprechtc2264562015-07-12 09:41:50 +020078n [A-Za-z0-9_-]
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80%%
81 int str = 0;
82 int ts, i;
83
Roman Zippela02f0572005-11-08 21:34:53 -080084[ \t]*#.*\n |
85[ \t]*\n {
Roman Zippela02f0572005-11-08 21:34:53 -080086 return T_EOL;
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088[ \t]*#.*
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91[ \t]+ {
92 BEGIN(COMMAND);
93}
94
95. {
96 unput(yytext[0]);
97 BEGIN(COMMAND);
98}
99
100
101<COMMAND>{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 {n}+ {
Arnaud Lacombe61f956f2011-05-04 21:14:44 -0400103 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
Roman Zippela02f0572005-11-08 21:34:53 -0800104 BEGIN(PARAM);
105 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 Yamada765f4cd2018-01-12 00:50:50 +0900108 yylval.id = id;
Roman Zippel7a884882005-11-08 21:34:51 -0800109 return id->token;
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 alloc_string(yytext, yyleng);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900112 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return T_WORD;
114 }
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200115 . warn_ignored_character(*yytext);
Roman Zippela02f0572005-11-08 21:34:53 -0800116 \n {
117 BEGIN(INITIAL);
Roman Zippela02f0572005-11-08 21:34:53 -0800118 return T_EOL;
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122<PARAM>{
123 "&&" return T_AND;
124 "||" return T_OR;
125 "(" return T_OPEN_PAREN;
126 ")" return T_CLOSE_PAREN;
127 "!" return T_NOT;
128 "=" return T_EQUAL;
129 "!=" return T_UNEQUAL;
Jan Beulich31847b62015-06-15 13:00:21 +0100130 "<=" return T_LESS_EQUAL;
131 ">=" return T_GREATER_EQUAL;
132 "<" return T_LESS;
133 ">" return T_GREATER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 \"|\' {
135 str = yytext[0];
136 new_string();
137 BEGIN(STRING);
138 }
Masahiro Yamada18492682018-03-23 02:00:14 +0900139 \n BEGIN(INITIAL); return T_EOL;
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200140 ({n}|[/.])+ {
Arnaud Lacombe61f956f2011-05-04 21:14:44 -0400141 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
Roman Zippel3370f9f2005-11-08 21:34:52 -0800142 if (id && id->flags & TF_PARAM) {
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900143 yylval.id = id;
Roman Zippel7a884882005-11-08 21:34:51 -0800144 return id->token;
Roman Zippel3370f9f2005-11-08 21:34:52 -0800145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 alloc_string(yytext, yyleng);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900147 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return T_WORD;
149 }
150 #.* /* comment */
Masahiro Yamada18492682018-03-23 02:00:14 +0900151 \\\n ;
Jan Beulich2e0d7372015-01-20 12:52:48 +0000152 [[:blank:]]+
Andreas Ruprechtc2264562015-07-12 09:41:50 +0200153 . warn_ignored_character(*yytext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 <<EOF>> {
155 BEGIN(INITIAL);
156 }
157}
158
159<STRING>{
160 [^'"\\\n]+/\n {
161 append_string(yytext, yyleng);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900162 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return T_WORD_QUOTE;
164 }
165 [^'"\\\n]+ {
166 append_string(yytext, yyleng);
167 }
168 \\.?/\n {
169 append_string(yytext + 1, yyleng - 1);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900170 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return T_WORD_QUOTE;
172 }
173 \\.? {
174 append_string(yytext + 1, yyleng - 1);
175 }
176 \'|\" {
177 if (str == yytext[0]) {
178 BEGIN(PARAM);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900179 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return T_WORD_QUOTE;
181 } else
182 append_string(yytext, 1);
183 }
184 \n {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900185 fprintf(stderr,
186 "%s:%d:warning: multi-line strings not supported\n",
187 zconf_curname(), zconf_lineno());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 BEGIN(INITIAL);
189 return T_EOL;
190 }
191 <<EOF>> {
192 BEGIN(INITIAL);
193 }
194}
195
196<HELP>{
197 [ \t]+ {
198 ts = 0;
199 for (i = 0; i < yyleng; i++) {
200 if (yytext[i] == '\t')
201 ts = (ts & ~7) + 8;
202 else
203 ts++;
204 }
205 last_ts = ts;
206 if (first_ts) {
207 if (ts < first_ts) {
208 zconf_endhelp();
209 return T_HELPTEXT;
210 }
211 ts -= first_ts;
212 while (ts > 8) {
213 append_string(" ", 8);
214 ts -= 8;
215 }
216 append_string(" ", ts);
217 }
218 }
219 [ \t]*\n/[^ \t\n] {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 zconf_endhelp();
221 return T_HELPTEXT;
222 }
223 [ \t]*\n {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 append_string("\n", 1);
225 }
226 [^ \t\n].* {
EGRY Gaborf7a4b4c2008-01-11 23:55:20 +0100227 while (yyleng) {
228 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
229 break;
230 yyleng--;
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 append_string(yytext, yyleng);
233 if (!first_ts)
234 first_ts = last_ts;
235 }
236 <<EOF>> {
237 zconf_endhelp();
238 return T_HELPTEXT;
239 }
240}
241
242<<EOF>> {
Roman Zippela02f0572005-11-08 21:34:53 -0800243 if (current_file) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 zconf_endfile();
Roman Zippela02f0572005-11-08 21:34:53 -0800245 return T_EOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 fclose(yyin);
248 yyterminate();
249}
250
251%%
252void zconf_starthelp(void)
253{
254 new_string();
255 last_ts = first_ts = 0;
256 BEGIN(HELP);
257}
258
259static void zconf_endhelp(void)
260{
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900261 yylval.string = text;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 BEGIN(INITIAL);
263}
264
265
266/*
267 * Try to open specified file with following names:
268 * ./name
269 * $(srctree)/name
270 * The latter is used when srctree is separate from objtree
271 * when compiling the kernel.
272 * Return NULL if file is not found.
273 */
274FILE *zconf_fopen(const char *name)
275{
276 char *env, fullname[PATH_MAX+1];
277 FILE *f;
278
279 f = fopen(name, "r");
Marcin Garski11de39e2007-05-05 22:49:00 +0200280 if (!f && name != NULL && name[0] != '/') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 env = getenv(SRCTREE);
282 if (env) {
283 sprintf(fullname, "%s/%s", env, name);
284 f = fopen(fullname, "r");
285 }
286 }
287 return f;
288}
289
290void zconf_initscan(const char *name)
291{
292 yyin = zconf_fopen(name);
293 if (!yyin) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900294 fprintf(stderr, "can't find file %s\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 exit(1);
296 }
297
Alan Cox177acf72012-11-06 14:32:08 +0000298 current_buf = xmalloc(sizeof(*current_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 memset(current_buf, 0, sizeof(*current_buf));
300
301 current_file = file_lookup(name);
Masahiro Yamada18492682018-03-23 02:00:14 +0900302 yylineno = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305void zconf_nextfile(const char *name)
306{
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100307 struct file *iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct file *file = file_lookup(name);
Alan Cox177acf72012-11-06 14:32:08 +0000309 struct buffer *buf = xmalloc(sizeof(*buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 memset(buf, 0, sizeof(*buf));
311
312 current_buf->state = YY_CURRENT_BUFFER;
Arnaud Lacombee82dae92010-09-04 16:09:26 -0400313 yyin = zconf_fopen(file->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!yyin) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900315 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
316 zconf_curname(), zconf_lineno(), file->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 exit(1);
318 }
319 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
320 buf->parent = current_buf;
321 current_buf = buf;
322
Masahiro Yamada18492682018-03-23 02:00:14 +0900323 current_file->lineno = yylineno;
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900324 file->parent = current_file;
325
326 for (iter = current_file; iter; iter = iter->parent) {
327 if (!strcmp(iter->name, file->name)) {
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +0900328 fprintf(stderr,
Masahiro Yamada32a94b82018-03-23 02:00:12 +0900329 "Recursive inclusion detected.\n"
330 "Inclusion path:\n"
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900331 " current file : %s\n", file->name);
332 iter = file;
Masahiro Yamada5ae6fcc2018-03-02 16:05:12 +0900333 do {
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100334 iter = iter->parent;
Masahiro Yamada32a94b82018-03-23 02:00:12 +0900335 fprintf(stderr, " included from: %s:%d\n",
Masahiro Yamada5ae6fcc2018-03-02 16:05:12 +0900336 iter->name, iter->lineno - 1);
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900337 } while (strcmp(iter->name, file->name));
Yann E. MORINf094f8a2011-02-24 19:36:42 +0100338 exit(1);
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Masahiro Yamada379a8eb2018-03-23 02:00:13 +0900341
Masahiro Yamada18492682018-03-23 02:00:14 +0900342 yylineno = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 current_file = file;
344}
345
Roman Zippela02f0572005-11-08 21:34:53 -0800346static void zconf_endfile(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct buffer *parent;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 current_file = current_file->parent;
Masahiro Yamada18492682018-03-23 02:00:14 +0900351 if (current_file)
352 yylineno = current_file->lineno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 parent = current_buf->parent;
355 if (parent) {
356 fclose(yyin);
357 yy_delete_buffer(YY_CURRENT_BUFFER);
358 yy_switch_to_buffer(parent->state);
359 }
360 free(current_buf);
361 current_buf = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
364int zconf_lineno(void)
365{
Roman Zippela02f0572005-11-08 21:34:53 -0800366 return current_pos.lineno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Arnaud Lacombe2e7a0912010-09-04 16:03:30 -0400369const char *zconf_curname(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Roman Zippela02f0572005-11-08 21:34:53 -0800371 return current_pos.file ? current_pos.file->name : "<none>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}