blob: e15e8c7063e04a0c028578b085c67bedebe88473 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001%{
2/*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 * Released under the terms of the GNU GPL v2.0.
5 */
6
7#include <ctype.h>
8#include <stdarg.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <stdbool.h>
13
Roman Zippel7a884882005-11-08 21:34:51 -080014#include "lkc.h"
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt)
17
18#define PRINTD 0x0001
19#define DEBUG_PARSE 0x0002
20
21int cdebug = PRINTD;
22
Masahiro Yamada765f4cd2018-01-12 00:50:50 +090023int yylex(void);
24static void yyerror(const char *err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static void zconfprint(const char *err, ...);
Roman Zippela02f0572005-11-08 21:34:53 -080026static void zconf_error(const char *err, ...);
Arnaud Lacombe61f956f2011-05-04 21:14:44 -040027static bool zconf_endtoken(const struct kconf_id *id, int starttoken, int endtoken);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Andi Kleene66f25d2010-01-13 17:02:44 +010029struct symbol *symbol_hash[SYMBOL_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31static struct menu *current_menu, *current_entry;
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033%}
Nicolas Pitre237e3ad2016-11-11 00:10:05 -050034%expect 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36%union
37{
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 char *string;
Roman Zippela02f0572005-11-08 21:34:53 -080039 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct symbol *symbol;
41 struct expr *expr;
42 struct menu *menu;
Arnaud Lacombe61f956f2011-05-04 21:14:44 -040043 const struct kconf_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Roman Zippel3370f9f2005-11-08 21:34:52 -080046%token <id>T_MAINMENU
47%token <id>T_MENU
48%token <id>T_ENDMENU
49%token <id>T_SOURCE
50%token <id>T_CHOICE
51%token <id>T_ENDCHOICE
52%token <id>T_COMMENT
53%token <id>T_CONFIG
54%token <id>T_MENUCONFIG
55%token <id>T_HELP
Linus Torvalds1da177e2005-04-16 15:20:36 -070056%token <string> T_HELPTEXT
Roman Zippel3370f9f2005-11-08 21:34:52 -080057%token <id>T_IF
58%token <id>T_ENDIF
59%token <id>T_DEPENDS
Roman Zippel3370f9f2005-11-08 21:34:52 -080060%token <id>T_OPTIONAL
61%token <id>T_PROMPT
62%token <id>T_TYPE
63%token <id>T_DEFAULT
64%token <id>T_SELECT
Nicolas Pitre237e3ad2016-11-11 00:10:05 -050065%token <id>T_IMPLY
Roman Zippel3370f9f2005-11-08 21:34:52 -080066%token <id>T_RANGE
Arnaud Lacombe86e187f2010-11-06 18:30:23 -030067%token <id>T_VISIBLE
Roman Zippelf6a88aa2006-06-08 22:12:44 -070068%token <id>T_OPTION
Roman Zippel3370f9f2005-11-08 21:34:52 -080069%token <id>T_ON
Linus Torvalds1da177e2005-04-16 15:20:36 -070070%token <string> T_WORD
71%token <string> T_WORD_QUOTE
72%token T_UNEQUAL
Jan Beulich31847b62015-06-15 13:00:21 +010073%token T_LESS
74%token T_LESS_EQUAL
75%token T_GREATER
76%token T_GREATER_EQUAL
Linus Torvalds1da177e2005-04-16 15:20:36 -070077%token T_CLOSE_PAREN
78%token T_OPEN_PAREN
Roman Zippel3370f9f2005-11-08 21:34:52 -080079%token T_EOL
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +090080%token <string> T_VARIABLE
81%token T_ASSIGN
82%token <string> T_ASSIGN_VAL
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84%left T_OR
85%left T_AND
86%left T_EQUAL T_UNEQUAL
Jan Beulich31847b62015-06-15 13:00:21 +010087%left T_LESS T_LESS_EQUAL T_GREATER T_GREATER_EQUAL
Linus Torvalds1da177e2005-04-16 15:20:36 -070088%nonassoc T_NOT
89
90%type <string> prompt
Ulf Magnusson26e47a32017-10-08 19:11:18 +020091%type <symbol> nonconst_symbol
Linus Torvalds1da177e2005-04-16 15:20:36 -070092%type <symbol> symbol
93%type <expr> expr
94%type <expr> if_expr
Roman Zippela02f0572005-11-08 21:34:53 -080095%type <id> end
96%type <id> option_name
97%type <menu> if_entry menu_entry choice_entry
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +090098%type <string> symbol_option_arg word_opt assign_val
Roman Zippela02f0572005-11-08 21:34:53 -080099
100%destructor {
101 fprintf(stderr, "%s:%d: missing end statement for this entry\n",
102 $$->file->name, $$->lineno);
103 if (current_menu == $$)
104 menu_end_menu();
105} if_entry menu_entry choice_entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Josh Triplett1456edb2009-10-15 11:03:20 -0700107%{
Ulf Magnussonc8734432017-10-05 05:06:48 +0200108/* Include kconf_id.c here so it can see the token constants. */
Linus Torvaldsbb3290d2017-08-19 10:17:02 -0700109#include "kconf_id.c"
Josh Triplett1456edb2009-10-15 11:03:20 -0700110%}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112%%
Arnaud Lacombe8ea13e22010-08-16 22:55:31 -0400113input: nl start | start;
114
Masahiro Yamada96d8e482018-05-28 18:21:42 +0900115start: mainmenu_stmt stmt_list | stmt_list;
Ulf Magnusson0724a7c2017-10-08 19:11:21 +0200116
117/* mainmenu entry */
118
119mainmenu_stmt: T_MAINMENU prompt nl
120{
121 menu_add_prompt(P_MENU, $2, NULL);
122};
123
Roman Zippela02f0572005-11-08 21:34:53 -0800124stmt_list:
125 /* empty */
126 | stmt_list common_stmt
127 | stmt_list choice_stmt
128 | stmt_list menu_stmt
Roman Zippela02f0572005-11-08 21:34:53 -0800129 | stmt_list end { zconf_error("unexpected end statement"); }
130 | stmt_list T_WORD error T_EOL { zconf_error("unknown statement \"%s\"", $2); }
131 | stmt_list option_name error T_EOL
132{
Linus Torvaldsbb3290d2017-08-19 10:17:02 -0700133 zconf_error("unexpected option \"%s\"", $2->name);
Roman Zippela02f0572005-11-08 21:34:53 -0800134}
135 | stmt_list error T_EOL { zconf_error("invalid statement"); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136;
137
Roman Zippela02f0572005-11-08 21:34:53 -0800138option_name:
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500139 T_DEPENDS | T_PROMPT | T_TYPE | T_SELECT | T_IMPLY | T_OPTIONAL | T_RANGE | T_DEFAULT | T_VISIBLE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140;
141
Roman Zippela02f0572005-11-08 21:34:53 -0800142common_stmt:
143 T_EOL
144 | if_stmt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 | comment_stmt
146 | config_stmt
147 | menuconfig_stmt
148 | source_stmt
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900149 | assignment_stmt
Roman Zippela02f0572005-11-08 21:34:53 -0800150;
151
152option_error:
153 T_WORD error T_EOL { zconf_error("unknown option \"%s\"", $1); }
154 | error T_EOL { zconf_error("invalid option"); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155;
156
157
158/* config/menuconfig entry */
159
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200160config_entry_start: T_CONFIG nonconst_symbol T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200162 $2->flags |= SYMBOL_OPTIONAL;
163 menu_add_entry($2);
164 printd(DEBUG_PARSE, "%s:%d:config %s\n", zconf_curname(), zconf_lineno(), $2->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167config_stmt: config_entry_start config_option_list
168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
170};
171
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200172menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200174 $2->flags |= SYMBOL_OPTIONAL;
175 menu_add_entry($2);
176 printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", zconf_curname(), zconf_lineno(), $2->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
179menuconfig_stmt: menuconfig_entry_start config_option_list
180{
181 if (current_entry->prompt)
182 current_entry->prompt->type = P_MENU;
183 else
184 zconfprint("warning: menuconfig statement without prompt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
186};
187
188config_option_list:
189 /* empty */
190 | config_option_list config_option
Roman Zippelf6a88aa2006-06-08 22:12:44 -0700191 | config_option_list symbol_option
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 | config_option_list depends
193 | config_option_list help
Roman Zippela02f0572005-11-08 21:34:53 -0800194 | config_option_list option_error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 | config_option_list T_EOL
196;
197
Roman Zippel3370f9f2005-11-08 21:34:52 -0800198config_option: T_TYPE prompt_stmt_opt T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Roman Zippel3370f9f2005-11-08 21:34:52 -0800200 menu_set_type($1->stype);
201 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
202 zconf_curname(), zconf_lineno(),
203 $1->stype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
206config_option: T_PROMPT prompt if_expr T_EOL
207{
208 menu_add_prompt(P_PROMPT, $2, $3);
209 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
210};
211
212config_option: T_DEFAULT expr if_expr T_EOL
213{
214 menu_add_expr(P_DEFAULT, $2, $3);
Roman Zippel3370f9f2005-11-08 21:34:52 -0800215 if ($1->stype != S_UNKNOWN)
216 menu_set_type($1->stype);
217 printd(DEBUG_PARSE, "%s:%d:default(%u)\n",
218 zconf_curname(), zconf_lineno(),
219 $1->stype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200222config_option: T_SELECT nonconst_symbol if_expr T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200224 menu_add_symbol(P_SELECT, $2, $3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 printd(DEBUG_PARSE, "%s:%d:select\n", zconf_curname(), zconf_lineno());
226};
227
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200228config_option: T_IMPLY nonconst_symbol if_expr T_EOL
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500229{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200230 menu_add_symbol(P_IMPLY, $2, $3);
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500231 printd(DEBUG_PARSE, "%s:%d:imply\n", zconf_curname(), zconf_lineno());
232};
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234config_option: T_RANGE symbol symbol if_expr T_EOL
235{
236 menu_add_expr(P_RANGE, expr_alloc_comp(E_RANGE,$2, $3), $4);
237 printd(DEBUG_PARSE, "%s:%d:range\n", zconf_curname(), zconf_lineno());
238};
239
Roman Zippelf6a88aa2006-06-08 22:12:44 -0700240symbol_option: T_OPTION symbol_option_list T_EOL
241;
242
243symbol_option_list:
244 /* empty */
245 | symbol_option_list T_WORD symbol_option_arg
246{
Arnaud Lacombe61f956f2011-05-04 21:14:44 -0400247 const struct kconf_id *id = kconf_id_lookup($2, strlen($2));
Ulf Magnussonbc28fe12017-10-08 19:11:20 +0200248 if (id && id->flags & TF_OPTION) {
Roman Zippelf6a88aa2006-06-08 22:12:44 -0700249 menu_add_option(id->token, $3);
Ulf Magnussonbc28fe12017-10-08 19:11:20 +0200250 free($3);
251 }
Roman Zippelf6a88aa2006-06-08 22:12:44 -0700252 else
253 zconfprint("warning: ignoring unknown option %s", $2);
254 free($2);
255};
256
257symbol_option_arg:
258 /* empty */ { $$ = NULL; }
259 | T_EQUAL prompt { $$ = $2; }
260;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/* choice entry */
263
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100264choice: T_CHOICE word_opt T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100266 struct symbol *sym = sym_lookup($2, SYMBOL_CHOICE);
267 sym->flags |= SYMBOL_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 menu_add_entry(sym);
269 menu_add_expr(P_CHOICE, NULL, NULL);
Masahiro Yamadabf0bbdc2018-02-20 20:40:29 +0900270 free($2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 printd(DEBUG_PARSE, "%s:%d:choice\n", zconf_curname(), zconf_lineno());
272};
273
274choice_entry: choice choice_option_list
275{
Roman Zippela02f0572005-11-08 21:34:53 -0800276 $$ = menu_add_menu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277};
278
279choice_end: end
280{
281 if (zconf_endtoken($1, T_CHOICE, T_ENDCHOICE)) {
282 menu_end_menu();
283 printd(DEBUG_PARSE, "%s:%d:endchoice\n", zconf_curname(), zconf_lineno());
284 }
285};
286
Roman Zippela02f0572005-11-08 21:34:53 -0800287choice_stmt: choice_entry choice_block choice_end
288;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290choice_option_list:
291 /* empty */
292 | choice_option_list choice_option
293 | choice_option_list depends
294 | choice_option_list help
295 | choice_option_list T_EOL
Roman Zippela02f0572005-11-08 21:34:53 -0800296 | choice_option_list option_error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297;
298
299choice_option: T_PROMPT prompt if_expr T_EOL
300{
301 menu_add_prompt(P_PROMPT, $2, $3);
302 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
303};
304
Roman Zippel3370f9f2005-11-08 21:34:52 -0800305choice_option: T_TYPE prompt_stmt_opt T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Roman Zippel3370f9f2005-11-08 21:34:52 -0800307 if ($1->stype == S_BOOLEAN || $1->stype == S_TRISTATE) {
308 menu_set_type($1->stype);
309 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
310 zconf_curname(), zconf_lineno(),
311 $1->stype);
312 } else
313 YYERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314};
315
316choice_option: T_OPTIONAL T_EOL
317{
318 current_entry->sym->flags |= SYMBOL_OPTIONAL;
319 printd(DEBUG_PARSE, "%s:%d:optional\n", zconf_curname(), zconf_lineno());
320};
321
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200322choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Roman Zippel3370f9f2005-11-08 21:34:52 -0800324 if ($1->stype == S_UNKNOWN) {
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200325 menu_add_symbol(P_DEFAULT, $2, $3);
Roman Zippel3370f9f2005-11-08 21:34:52 -0800326 printd(DEBUG_PARSE, "%s:%d:default\n",
327 zconf_curname(), zconf_lineno());
328 } else
329 YYERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330};
331
332choice_block:
333 /* empty */
Roman Zippela02f0572005-11-08 21:34:53 -0800334 | choice_block common_stmt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335;
336
337/* if entry */
338
Roman Zippela02f0572005-11-08 21:34:53 -0800339if_entry: T_IF expr nl
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 printd(DEBUG_PARSE, "%s:%d:if\n", zconf_curname(), zconf_lineno());
342 menu_add_entry(NULL);
343 menu_add_dep($2);
Roman Zippela02f0572005-11-08 21:34:53 -0800344 $$ = menu_add_menu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345};
346
347if_end: end
348{
349 if (zconf_endtoken($1, T_IF, T_ENDIF)) {
350 menu_end_menu();
351 printd(DEBUG_PARSE, "%s:%d:endif\n", zconf_curname(), zconf_lineno());
352 }
353};
354
Roman Zippela02f0572005-11-08 21:34:53 -0800355if_stmt: if_entry if_block if_end
356;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358if_block:
359 /* empty */
Roman Zippela02f0572005-11-08 21:34:53 -0800360 | if_block common_stmt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 | if_block menu_stmt
362 | if_block choice_stmt
363;
364
365/* menu entry */
366
367menu: T_MENU prompt T_EOL
368{
369 menu_add_entry(NULL);
blaisorblade@yahoo.itfb7f6ff2005-07-28 17:56:25 +0200370 menu_add_prompt(P_MENU, $2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 printd(DEBUG_PARSE, "%s:%d:menu\n", zconf_curname(), zconf_lineno());
372};
373
Arnaud Lacombe86e187f2010-11-06 18:30:23 -0300374menu_entry: menu visibility_list depends_list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Roman Zippela02f0572005-11-08 21:34:53 -0800376 $$ = menu_add_menu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377};
378
379menu_end: end
380{
381 if (zconf_endtoken($1, T_MENU, T_ENDMENU)) {
382 menu_end_menu();
383 printd(DEBUG_PARSE, "%s:%d:endmenu\n", zconf_curname(), zconf_lineno());
384 }
385};
386
Roman Zippela02f0572005-11-08 21:34:53 -0800387menu_stmt: menu_entry menu_block menu_end
388;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390menu_block:
391 /* empty */
Roman Zippela02f0572005-11-08 21:34:53 -0800392 | menu_block common_stmt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 | menu_block menu_stmt
394 | menu_block choice_stmt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395;
396
Roman Zippela02f0572005-11-08 21:34:53 -0800397source_stmt: T_SOURCE prompt T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 printd(DEBUG_PARSE, "%s:%d:source %s\n", zconf_curname(), zconf_lineno(), $2);
Roman Zippela02f0572005-11-08 21:34:53 -0800400 zconf_nextfile($2);
Ulf Magnusson24161a62017-10-08 19:11:19 +0200401 free($2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402};
403
404/* comment entry */
405
406comment: T_COMMENT prompt T_EOL
407{
408 menu_add_entry(NULL);
blaisorblade@yahoo.itfb7f6ff2005-07-28 17:56:25 +0200409 menu_add_prompt(P_COMMENT, $2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 printd(DEBUG_PARSE, "%s:%d:comment\n", zconf_curname(), zconf_lineno());
411};
412
413comment_stmt: comment depends_list
Ulf Magnussondf60f4b2017-10-09 00:14:48 +0200414;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416/* help option */
417
418help_start: T_HELP T_EOL
419{
420 printd(DEBUG_PARSE, "%s:%d:help\n", zconf_curname(), zconf_lineno());
421 zconf_starthelp();
422};
423
424help: help_start T_HELPTEXT
425{
Ulf Magnusson6479f322018-01-12 07:47:47 +0100426 if (current_entry->help) {
427 free(current_entry->help);
428 zconfprint("warning: '%s' defined with more than one help text -- only the last one will be used",
429 current_entry->sym->name ?: "<choice>");
430 }
Ulf Magnusson1b9eda22018-01-31 10:34:30 +0100431
432 /* Is the help text empty or all whitespace? */
433 if ($2[strspn($2, " \f\n\r\t\v")] == '\0')
434 zconfprint("warning: '%s' defined with blank help text",
435 current_entry->sym->name ?: "<choice>");
436
Sam Ravnborg03d29122007-07-21 00:00:36 +0200437 current_entry->help = $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438};
439
440/* depends option */
441
Roman Zippela02f0572005-11-08 21:34:53 -0800442depends_list:
443 /* empty */
444 | depends_list depends
445 | depends_list T_EOL
446 | depends_list option_error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447;
448
449depends: T_DEPENDS T_ON expr T_EOL
450{
451 menu_add_dep($3);
452 printd(DEBUG_PARSE, "%s:%d:depends on\n", zconf_curname(), zconf_lineno());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453};
454
Arnaud Lacombe86e187f2010-11-06 18:30:23 -0300455/* visibility option */
456
457visibility_list:
458 /* empty */
459 | visibility_list visible
460 | visibility_list T_EOL
461;
462
463visible: T_VISIBLE if_expr
464{
465 menu_add_visibility($2);
466};
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/* prompt statement */
469
470prompt_stmt_opt:
471 /* empty */
472 | prompt if_expr
473{
blaisorblade@yahoo.itfb7f6ff2005-07-28 17:56:25 +0200474 menu_add_prompt(P_PROMPT, $1, $2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475};
476
477prompt: T_WORD
478 | T_WORD_QUOTE
479;
480
Roman Zippela02f0572005-11-08 21:34:53 -0800481end: T_ENDMENU T_EOL { $$ = $1; }
482 | T_ENDCHOICE T_EOL { $$ = $1; }
483 | T_ENDIF T_EOL { $$ = $1; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484;
485
Roman Zippela02f0572005-11-08 21:34:53 -0800486nl:
487 T_EOL
488 | nl T_EOL
489;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491if_expr: /* empty */ { $$ = NULL; }
492 | T_IF expr { $$ = $2; }
493;
494
495expr: symbol { $$ = expr_alloc_symbol($1); }
Jan Beulich31847b62015-06-15 13:00:21 +0100496 | symbol T_LESS symbol { $$ = expr_alloc_comp(E_LTH, $1, $3); }
497 | symbol T_LESS_EQUAL symbol { $$ = expr_alloc_comp(E_LEQ, $1, $3); }
498 | symbol T_GREATER symbol { $$ = expr_alloc_comp(E_GTH, $1, $3); }
499 | symbol T_GREATER_EQUAL symbol { $$ = expr_alloc_comp(E_GEQ, $1, $3); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 | symbol T_EQUAL symbol { $$ = expr_alloc_comp(E_EQUAL, $1, $3); }
501 | symbol T_UNEQUAL symbol { $$ = expr_alloc_comp(E_UNEQUAL, $1, $3); }
502 | T_OPEN_PAREN expr T_CLOSE_PAREN { $$ = $2; }
503 | T_NOT expr { $$ = expr_alloc_one(E_NOT, $2); }
504 | expr T_OR expr { $$ = expr_alloc_two(E_OR, $1, $3); }
505 | expr T_AND expr { $$ = expr_alloc_two(E_AND, $1, $3); }
506;
507
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200508/* For symbol definitions, selects, etc., where quotes are not accepted */
509nonconst_symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); };
510
511symbol: nonconst_symbol
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100512 | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513;
514
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100515word_opt: /* empty */ { $$ = NULL; }
516 | T_WORD
517
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900518/* assignment statement */
519
520assignment_stmt: T_VARIABLE T_ASSIGN assign_val T_EOL { variable_add($1, $3); free($1); free($3); }
521
522assign_val:
523 /* empty */ { $$ = xstrdup(""); };
524 | T_ASSIGN_VAL
525;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527%%
528
529void conf_parse(const char *name)
530{
531 struct symbol *sym;
532 int i;
533
534 zconf_initscan(name);
535
nir.tzachar@gmail.com692d97c2009-11-25 12:28:43 +0200536 _menu_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Roman Zippela02f0572005-11-08 21:34:53 -0800538 if (getenv("ZCONF_DEBUG"))
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900539 yydebug = 1;
540 yyparse();
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900541
542 /* Variables are expanded in the parse phase. We can free them here. */
543 variable_all_del();
544
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900545 if (yynerrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 exit(1);
Yann E. MORIN6902dcc2013-09-03 17:07:18 +0200547 if (!modules_sym)
548 modules_sym = sym_find( "n" );
Arnaud Lacombef6ce00b2010-09-09 21:17:26 -0400549
Masahiro Yamada96d8e482018-05-28 18:21:42 +0900550 if (!menu_has_prompt(&rootmenu)) {
551 current_entry = &rootmenu;
Masahiro Yamada137c0112018-05-28 18:21:44 +0900552 menu_add_prompt(P_MENU, "Main menu", NULL);
Masahiro Yamada96d8e482018-05-28 18:21:42 +0900553 }
Arnaud Lacombef6ce00b2010-09-09 21:17:26 -0400554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 menu_finalize(&rootmenu);
556 for_all_symbols(i, sym) {
Sam Ravnborg5447d342007-05-06 09:20:10 +0200557 if (sym_check_deps(sym))
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900558 yynerrs++;
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900559 }
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900560 if (yynerrs)
Sam Ravnborg5447d342007-05-06 09:20:10 +0200561 exit(1);
Karsten Wiesebfc10002006-12-13 00:34:07 -0800562 sym_set_change_count(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Josh Triplett65166572009-10-15 12:13:36 -0700565static const char *zconf_tokenname(int token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 switch (token) {
568 case T_MENU: return "menu";
569 case T_ENDMENU: return "endmenu";
570 case T_CHOICE: return "choice";
571 case T_ENDCHOICE: return "endchoice";
572 case T_IF: return "if";
573 case T_ENDIF: return "endif";
Roman Zippela02f0572005-11-08 21:34:53 -0800574 case T_DEPENDS: return "depends";
Arnaud Lacombe86e187f2010-11-06 18:30:23 -0300575 case T_VISIBLE: return "visible";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577 return "<token>";
578}
579
Arnaud Lacombe61f956f2011-05-04 21:14:44 -0400580static bool zconf_endtoken(const struct kconf_id *id, int starttoken, int endtoken)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Roman Zippela02f0572005-11-08 21:34:53 -0800582 if (id->token != endtoken) {
583 zconf_error("unexpected '%s' within %s block",
Linus Torvaldsbb3290d2017-08-19 10:17:02 -0700584 id->name, zconf_tokenname(starttoken));
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900585 yynerrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return false;
587 }
588 if (current_menu->file != current_file) {
Roman Zippela02f0572005-11-08 21:34:53 -0800589 zconf_error("'%s' in different file than '%s'",
Linus Torvaldsbb3290d2017-08-19 10:17:02 -0700590 id->name, zconf_tokenname(starttoken));
Roman Zippela02f0572005-11-08 21:34:53 -0800591 fprintf(stderr, "%s:%d: location of the '%s'\n",
592 current_menu->file->name, current_menu->lineno,
593 zconf_tokenname(starttoken));
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900594 yynerrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return false;
596 }
597 return true;
598}
599
600static void zconfprint(const char *err, ...)
601{
602 va_list ap;
603
Roman Zippela02f0572005-11-08 21:34:53 -0800604 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
605 va_start(ap, err);
606 vfprintf(stderr, err, ap);
607 va_end(ap);
608 fprintf(stderr, "\n");
609}
610
611static void zconf_error(const char *err, ...)
612{
613 va_list ap;
614
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900615 yynerrs++;
Roman Zippela02f0572005-11-08 21:34:53 -0800616 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 va_start(ap, err);
618 vfprintf(stderr, err, ap);
619 va_end(ap);
620 fprintf(stderr, "\n");
621}
622
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900623static void yyerror(const char *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Josh Triplett65166572009-10-15 12:13:36 -0700628static void print_quoted_string(FILE *out, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 const char *p;
631 int len;
632
633 putc('"', out);
634 while ((p = strchr(str, '"'))) {
635 len = p - str;
636 if (len)
637 fprintf(out, "%.*s", len, str);
638 fputs("\\\"", out);
639 str = p + 1;
640 }
641 fputs(str, out);
642 putc('"', out);
643}
644
Josh Triplett65166572009-10-15 12:13:36 -0700645static void print_symbol(FILE *out, struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct symbol *sym = menu->sym;
648 struct property *prop;
649
650 if (sym_is_choice(sym))
Li Zefanc6ccc302010-04-14 11:44:20 +0800651 fprintf(out, "\nchoice\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 else
Li Zefanc6ccc302010-04-14 11:44:20 +0800653 fprintf(out, "\nconfig %s\n", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 switch (sym->type) {
655 case S_BOOLEAN:
Masahiro Yamadab92d8042017-12-16 00:38:02 +0900656 fputs(" bool\n", out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 break;
658 case S_TRISTATE:
659 fputs(" tristate\n", out);
660 break;
661 case S_STRING:
662 fputs(" string\n", out);
663 break;
664 case S_INT:
665 fputs(" integer\n", out);
666 break;
667 case S_HEX:
668 fputs(" hex\n", out);
669 break;
670 default:
671 fputs(" ???\n", out);
672 break;
673 }
674 for (prop = sym->prop; prop; prop = prop->next) {
675 if (prop->menu != menu)
676 continue;
677 switch (prop->type) {
678 case P_PROMPT:
679 fputs(" prompt ", out);
680 print_quoted_string(out, prop->text);
681 if (!expr_is_yes(prop->visible.expr)) {
682 fputs(" if ", out);
683 expr_fprint(prop->visible.expr, out);
684 }
685 fputc('\n', out);
686 break;
687 case P_DEFAULT:
688 fputs( " default ", out);
689 expr_fprint(prop->expr, out);
690 if (!expr_is_yes(prop->visible.expr)) {
691 fputs(" if ", out);
692 expr_fprint(prop->visible.expr, out);
693 }
694 fputc('\n', out);
695 break;
696 case P_CHOICE:
697 fputs(" #choice value\n", out);
698 break;
Li Zefanc6ccc302010-04-14 11:44:20 +0800699 case P_SELECT:
700 fputs( " select ", out);
701 expr_fprint(prop->expr, out);
702 fputc('\n', out);
703 break;
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500704 case P_IMPLY:
705 fputs( " imply ", out);
706 expr_fprint(prop->expr, out);
707 fputc('\n', out);
708 break;
Li Zefanc6ccc302010-04-14 11:44:20 +0800709 case P_RANGE:
710 fputs( " range ", out);
711 expr_fprint(prop->expr, out);
712 fputc('\n', out);
713 break;
714 case P_MENU:
715 fputs( " menu ", out);
716 print_quoted_string(out, prop->text);
717 fputc('\n', out);
718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 default:
720 fprintf(out, " unknown prop %d!\n", prop->type);
721 break;
722 }
723 }
Sam Ravnborg03d29122007-07-21 00:00:36 +0200724 if (menu->help) {
725 int len = strlen(menu->help);
726 while (menu->help[--len] == '\n')
727 menu->help[len] = 0;
728 fprintf(out, " help\n%s\n", menu->help);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732void zconfdump(FILE *out)
733{
734 struct property *prop;
735 struct symbol *sym;
736 struct menu *menu;
737
738 menu = rootmenu.list;
739 while (menu) {
740 if ((sym = menu->sym))
741 print_symbol(out, menu);
742 else if ((prop = menu->prompt)) {
743 switch (prop->type) {
744 case P_COMMENT:
745 fputs("\ncomment ", out);
746 print_quoted_string(out, prop->text);
747 fputs("\n", out);
748 break;
749 case P_MENU:
750 fputs("\nmenu ", out);
751 print_quoted_string(out, prop->text);
752 fputs("\n", out);
753 break;
754 default:
755 ;
756 }
757 if (!expr_is_yes(prop->visible.expr)) {
758 fputs(" depends ", out);
759 expr_fprint(prop->visible.expr, out);
760 fputc('\n', out);
761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763
764 if (menu->list)
765 menu = menu->list;
766 else if (menu->next)
767 menu = menu->next;
768 else while ((menu = menu->parent)) {
769 if (menu->prompt && menu->prompt->type == P_MENU)
770 fputs("\nendmenu\n", out);
771 if (menu->next) {
772 menu = menu->next;
773 break;
774 }
775 }
776 }
777}
778
Arnaud Lacombe378dbb22011-05-23 02:08:52 -0400779#include "zconf.lex.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780#include "util.c"
781#include "confdata.c"
782#include "expr.c"
783#include "symbol.c"
784#include "menu.c"
Masahiro Yamada104daea2018-05-28 18:21:40 +0900785#include "preprocess.c"