blob: 2af7ce4e15315722e76113c55003beab0baba526 [file] [log] [blame]
Masahiro Yamada0c874102018-12-18 21:13:35 +09001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
Masahiro Yamada0c874102018-12-18 21:13:35 +09005%{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
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"
Masahiro Yamadaa77a05d2021-04-14 00:08:17 +090015#include "internal.h"
Roman Zippel7a884882005-11-08 21:34:51 -080016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt)
18
19#define PRINTD 0x0001
20#define DEBUG_PARSE 0x0002
21
22int cdebug = PRINTD;
23
Masahiro Yamada765f4cd2018-01-12 00:50:50 +090024static 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, ...);
Masahiro Yamadacaaebb32018-12-11 20:01:06 +090027static bool zconf_endtoken(const char *tokenname,
28 const char *expected_tokenname);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Andi Kleene66f25d2010-01-13 17:02:44 +010030struct symbol *symbol_hash[SYMBOL_HASHSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Masahiro Yamadaa77a05d2021-04-14 00:08:17 +090032struct menu *current_menu, *current_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034%}
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36%union
37{
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 char *string;
39 struct symbol *symbol;
40 struct expr *expr;
41 struct menu *menu;
Masahiro Yamada3c8f3172018-12-11 20:00:59 +090042 enum symbol_type type;
Masahiro Yamada1175c022018-05-28 18:21:50 +090043 enum variable_flavor flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046%token <string> T_HELPTEXT
Linus Torvalds1da177e2005-04-16 15:20:36 -070047%token <string> T_WORD
48%token <string> T_WORD_QUOTE
Masahiro Yamada3c8f3172018-12-11 20:00:59 +090049%token T_BOOL
Masahiro Yamadab3d1d9d2018-12-11 20:01:07 +090050%token T_CHOICE
Linus Torvalds1da177e2005-04-16 15:20:36 -070051%token T_CLOSE_PAREN
Masahiro Yamadac3d22872018-12-11 20:01:01 +090052%token T_COLON_EQUAL
Masahiro Yamadab3d1d9d2018-12-11 20:01:07 +090053%token T_COMMENT
54%token T_CONFIG
Masahiro Yamada3c8f3172018-12-11 20:00:59 +090055%token T_DEFAULT
56%token T_DEF_BOOL
57%token T_DEF_TRISTATE
Masahiro Yamadab3d1d9d2018-12-11 20:01:07 +090058%token T_DEPENDS
59%token T_ENDCHOICE
60%token T_ENDIF
61%token T_ENDMENU
62%token T_HELP
Masahiro Yamada3c8f3172018-12-11 20:00:59 +090063%token T_HEX
Masahiro Yamadab3d1d9d2018-12-11 20:01:07 +090064%token T_IF
65%token T_IMPLY
Masahiro Yamada3c8f3172018-12-11 20:00:59 +090066%token T_INT
Masahiro Yamadab3d1d9d2018-12-11 20:01:07 +090067%token T_MAINMENU
68%token T_MENU
69%token T_MENUCONFIG
Masahiro Yamadace2164a2018-12-11 20:01:00 +090070%token T_MODULES
Masahiro Yamadab3d1d9d2018-12-11 20:01:07 +090071%token T_ON
Linus Torvalds1da177e2005-04-16 15:20:36 -070072%token T_OPEN_PAREN
Masahiro Yamadab3d1d9d2018-12-11 20:01:07 +090073%token T_OPTIONAL
Masahiro Yamadac3d22872018-12-11 20:01:01 +090074%token T_PLUS_EQUAL
Masahiro Yamadab3d1d9d2018-12-11 20:01:07 +090075%token T_PROMPT
76%token T_RANGE
77%token T_SELECT
78%token T_SOURCE
Masahiro Yamada3c8f3172018-12-11 20:00:59 +090079%token T_STRING
80%token T_TRISTATE
Masahiro Yamadab3d1d9d2018-12-11 20:01:07 +090081%token T_VISIBLE
Roman Zippel3370f9f2005-11-08 21:34:52 -080082%token T_EOL
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +090083%token <string> T_ASSIGN_VAL
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85%left T_OR
86%left T_AND
87%left T_EQUAL T_UNEQUAL
Jan Beulich31847b62015-06-15 13:00:21 +010088%left T_LESS T_LESS_EQUAL T_GREATER T_GREATER_EQUAL
Linus Torvalds1da177e2005-04-16 15:20:36 -070089%nonassoc T_NOT
90
Ulf Magnusson26e47a32017-10-08 19:11:18 +020091%type <symbol> nonconst_symbol
Linus Torvalds1da177e2005-04-16 15:20:36 -070092%type <symbol> symbol
Masahiro Yamada3c8f3172018-12-11 20:00:59 +090093%type <type> type logic_type default
Linus Torvalds1da177e2005-04-16 15:20:36 -070094%type <expr> expr
95%type <expr> if_expr
Masahiro Yamadacaaebb32018-12-11 20:01:06 +090096%type <string> end
Roman Zippela02f0572005-11-08 21:34:53 -080097%type <menu> if_entry menu_entry choice_entry
Masahiro Yamadace2164a2018-12-11 20:01:00 +090098%type <string> word_opt assign_val
Masahiro Yamadac3d22872018-12-11 20:01:01 +090099%type <flavor> assign_op
Roman Zippela02f0572005-11-08 21:34:53 -0800100
101%destructor {
102 fprintf(stderr, "%s:%d: missing end statement for this entry\n",
103 $$->file->name, $$->lineno);
104 if (current_menu == $$)
105 menu_end_menu();
106} if_entry menu_entry choice_entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108%%
Masahiro Yamadacc66bca2018-12-11 20:00:49 +0900109input: mainmenu_stmt stmt_list | stmt_list;
Ulf Magnusson0724a7c2017-10-08 19:11:21 +0200110
111/* mainmenu entry */
112
Masahiro Yamada1be6e792019-12-17 13:14:19 +0900113mainmenu_stmt: T_MAINMENU T_WORD_QUOTE T_EOL
Ulf Magnusson0724a7c2017-10-08 19:11:21 +0200114{
115 menu_add_prompt(P_MENU, $2, NULL);
116};
117
Roman Zippela02f0572005-11-08 21:34:53 -0800118stmt_list:
119 /* empty */
Masahiro Yamada09d58732020-04-24 14:49:29 +0900120 | stmt_list assignment_stmt
Roman Zippela02f0572005-11-08 21:34:53 -0800121 | stmt_list choice_stmt
Masahiro Yamada09d58732020-04-24 14:49:29 +0900122 | stmt_list comment_stmt
123 | stmt_list config_stmt
124 | stmt_list if_stmt
Roman Zippela02f0572005-11-08 21:34:53 -0800125 | stmt_list menu_stmt
Masahiro Yamada09d58732020-04-24 14:49:29 +0900126 | stmt_list menuconfig_stmt
127 | stmt_list source_stmt
Roman Zippela02f0572005-11-08 21:34:53 -0800128 | stmt_list T_WORD error T_EOL { zconf_error("unknown statement \"%s\"", $2); }
Roman Zippela02f0572005-11-08 21:34:53 -0800129 | stmt_list error T_EOL { zconf_error("invalid statement"); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130;
131
Masahiro Yamada09d58732020-04-24 14:49:29 +0900132stmt_list_in_choice:
133 /* empty */
134 | stmt_list_in_choice comment_stmt
135 | stmt_list_in_choice config_stmt
136 | stmt_list_in_choice if_stmt_in_choice
137 | stmt_list_in_choice error T_EOL { zconf_error("invalid statement"); }
Roman Zippela02f0572005-11-08 21:34:53 -0800138;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* config/menuconfig entry */
141
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200142config_entry_start: T_CONFIG nonconst_symbol T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200144 $2->flags |= SYMBOL_OPTIONAL;
145 menu_add_entry($2);
146 printd(DEBUG_PARSE, "%s:%d:config %s\n", zconf_curname(), zconf_lineno(), $2->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149config_stmt: config_entry_start config_option_list
150{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
152};
153
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200154menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200156 $2->flags |= SYMBOL_OPTIONAL;
157 menu_add_entry($2);
158 printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", zconf_curname(), zconf_lineno(), $2->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
160
161menuconfig_stmt: menuconfig_entry_start config_option_list
162{
163 if (current_entry->prompt)
164 current_entry->prompt->type = P_MENU;
165 else
166 zconfprint("warning: menuconfig statement without prompt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
168};
169
170config_option_list:
171 /* empty */
172 | config_option_list config_option
173 | config_option_list depends
174 | config_option_list help
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175;
176
Masahiro Yamada3c8f3172018-12-11 20:00:59 +0900177config_option: type prompt_stmt_opt T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Masahiro Yamada3c8f3172018-12-11 20:00:59 +0900179 menu_set_type($1);
Roman Zippel3370f9f2005-11-08 21:34:52 -0800180 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
181 zconf_curname(), zconf_lineno(),
Masahiro Yamada3c8f3172018-12-11 20:00:59 +0900182 $1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183};
184
Masahiro Yamada1be6e792019-12-17 13:14:19 +0900185config_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 menu_add_prompt(P_PROMPT, $2, $3);
188 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
189};
190
Masahiro Yamada3c8f3172018-12-11 20:00:59 +0900191config_option: default expr if_expr T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 menu_add_expr(P_DEFAULT, $2, $3);
Masahiro Yamada3c8f3172018-12-11 20:00:59 +0900194 if ($1 != S_UNKNOWN)
195 menu_set_type($1);
Roman Zippel3370f9f2005-11-08 21:34:52 -0800196 printd(DEBUG_PARSE, "%s:%d:default(%u)\n",
197 zconf_curname(), zconf_lineno(),
Masahiro Yamada3c8f3172018-12-11 20:00:59 +0900198 $1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200201config_option: T_SELECT nonconst_symbol if_expr T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200203 menu_add_symbol(P_SELECT, $2, $3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 printd(DEBUG_PARSE, "%s:%d:select\n", zconf_curname(), zconf_lineno());
205};
206
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200207config_option: T_IMPLY nonconst_symbol if_expr T_EOL
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500208{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200209 menu_add_symbol(P_IMPLY, $2, $3);
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500210 printd(DEBUG_PARSE, "%s:%d:imply\n", zconf_curname(), zconf_lineno());
211};
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213config_option: T_RANGE symbol symbol if_expr T_EOL
214{
215 menu_add_expr(P_RANGE, expr_alloc_comp(E_RANGE,$2, $3), $4);
216 printd(DEBUG_PARSE, "%s:%d:range\n", zconf_curname(), zconf_lineno());
217};
218
Masahiro Yamada6dd85ff2021-03-14 04:48:36 +0900219config_option: T_MODULES T_EOL
Roman Zippelf6a88aa2006-06-08 22:12:44 -0700220{
Masahiro Yamada6dd85ff2021-03-14 04:48:36 +0900221 if (modules_sym)
222 zconf_error("symbol '%s' redefines option 'modules' already defined by symbol '%s'",
223 current_entry->sym->name, modules_sym->name);
224 modules_sym = current_entry->sym;
Roman Zippelf6a88aa2006-06-08 22:12:44 -0700225};
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227/* choice entry */
228
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100229choice: T_CHOICE word_opt T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100231 struct symbol *sym = sym_lookup($2, SYMBOL_CHOICE);
Dirk Gouders693359f2018-07-03 14:43:31 +0200232 sym->flags |= SYMBOL_NO_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 menu_add_entry(sym);
234 menu_add_expr(P_CHOICE, NULL, NULL);
Masahiro Yamadabf0bbdc2018-02-20 20:40:29 +0900235 free($2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 printd(DEBUG_PARSE, "%s:%d:choice\n", zconf_curname(), zconf_lineno());
237};
238
239choice_entry: choice choice_option_list
240{
Roman Zippela02f0572005-11-08 21:34:53 -0800241 $$ = menu_add_menu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
244choice_end: end
245{
Masahiro Yamadacaaebb32018-12-11 20:01:06 +0900246 if (zconf_endtoken($1, "choice")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 menu_end_menu();
248 printd(DEBUG_PARSE, "%s:%d:endchoice\n", zconf_curname(), zconf_lineno());
249 }
250};
251
Masahiro Yamada09d58732020-04-24 14:49:29 +0900252choice_stmt: choice_entry stmt_list_in_choice choice_end
Roman Zippela02f0572005-11-08 21:34:53 -0800253;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255choice_option_list:
256 /* empty */
257 | choice_option_list choice_option
258 | choice_option_list depends
259 | choice_option_list help
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260;
261
Masahiro Yamada1be6e792019-12-17 13:14:19 +0900262choice_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 menu_add_prompt(P_PROMPT, $2, $3);
265 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
266};
267
Masahiro Yamada3c8f3172018-12-11 20:00:59 +0900268choice_option: logic_type prompt_stmt_opt T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Masahiro Yamada3c8f3172018-12-11 20:00:59 +0900270 menu_set_type($1);
271 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
272 zconf_curname(), zconf_lineno(), $1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273};
274
275choice_option: T_OPTIONAL T_EOL
276{
277 current_entry->sym->flags |= SYMBOL_OPTIONAL;
278 printd(DEBUG_PARSE, "%s:%d:optional\n", zconf_curname(), zconf_lineno());
279};
280
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200281choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Masahiro Yamada3c8f3172018-12-11 20:00:59 +0900283 menu_add_symbol(P_DEFAULT, $2, $3);
284 printd(DEBUG_PARSE, "%s:%d:default\n",
285 zconf_curname(), zconf_lineno());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286};
287
Masahiro Yamada3c8f3172018-12-11 20:00:59 +0900288type:
289 logic_type
290 | T_INT { $$ = S_INT; }
291 | T_HEX { $$ = S_HEX; }
292 | T_STRING { $$ = S_STRING; }
293
294logic_type:
295 T_BOOL { $$ = S_BOOLEAN; }
296 | T_TRISTATE { $$ = S_TRISTATE; }
297
298default:
299 T_DEFAULT { $$ = S_UNKNOWN; }
300 | T_DEF_BOOL { $$ = S_BOOLEAN; }
301 | T_DEF_TRISTATE { $$ = S_TRISTATE; }
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/* if entry */
304
Dirk Goudersb2d00d72018-06-21 15:30:54 +0200305if_entry: T_IF expr T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 printd(DEBUG_PARSE, "%s:%d:if\n", zconf_curname(), zconf_lineno());
308 menu_add_entry(NULL);
309 menu_add_dep($2);
Roman Zippela02f0572005-11-08 21:34:53 -0800310 $$ = menu_add_menu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
313if_end: end
314{
Masahiro Yamadacaaebb32018-12-11 20:01:06 +0900315 if (zconf_endtoken($1, "if")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 menu_end_menu();
317 printd(DEBUG_PARSE, "%s:%d:endif\n", zconf_curname(), zconf_lineno());
318 }
319};
320
Masahiro Yamada48917962018-12-11 20:00:54 +0900321if_stmt: if_entry stmt_list if_end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322;
323
Masahiro Yamada09d58732020-04-24 14:49:29 +0900324if_stmt_in_choice: if_entry stmt_list_in_choice if_end
325;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/* menu entry */
328
Masahiro Yamada1be6e792019-12-17 13:14:19 +0900329menu: T_MENU T_WORD_QUOTE T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 menu_add_entry(NULL);
blaisorblade@yahoo.itfb7f6ff2005-07-28 17:56:25 +0200332 menu_add_prompt(P_MENU, $2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 printd(DEBUG_PARSE, "%s:%d:menu\n", zconf_curname(), zconf_lineno());
334};
335
Masahiro Yamada1f31be92018-12-11 20:00:56 +0900336menu_entry: menu menu_option_list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Roman Zippela02f0572005-11-08 21:34:53 -0800338 $$ = menu_add_menu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339};
340
341menu_end: end
342{
Masahiro Yamadacaaebb32018-12-11 20:01:06 +0900343 if (zconf_endtoken($1, "menu")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 menu_end_menu();
345 printd(DEBUG_PARSE, "%s:%d:endmenu\n", zconf_curname(), zconf_lineno());
346 }
347};
348
Masahiro Yamada94d4e1b2018-12-11 20:00:55 +0900349menu_stmt: menu_entry stmt_list menu_end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350;
351
Masahiro Yamada1f31be92018-12-11 20:00:56 +0900352menu_option_list:
353 /* empty */
354 | menu_option_list visible
355 | menu_option_list depends
356;
357
Masahiro Yamada1be6e792019-12-17 13:14:19 +0900358source_stmt: T_SOURCE T_WORD_QUOTE T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 printd(DEBUG_PARSE, "%s:%d:source %s\n", zconf_curname(), zconf_lineno(), $2);
Roman Zippela02f0572005-11-08 21:34:53 -0800361 zconf_nextfile($2);
Ulf Magnusson24161a62017-10-08 19:11:19 +0200362 free($2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363};
364
365/* comment entry */
366
Masahiro Yamada1be6e792019-12-17 13:14:19 +0900367comment: T_COMMENT T_WORD_QUOTE T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 menu_add_entry(NULL);
blaisorblade@yahoo.itfb7f6ff2005-07-28 17:56:25 +0200370 menu_add_prompt(P_COMMENT, $2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 printd(DEBUG_PARSE, "%s:%d:comment\n", zconf_curname(), zconf_lineno());
372};
373
Masahiro Yamada4b5ec812018-12-11 20:00:57 +0900374comment_stmt: comment comment_option_list
375;
376
377comment_option_list:
378 /* empty */
379 | comment_option_list depends
Ulf Magnussondf60f4b2017-10-09 00:14:48 +0200380;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382/* help option */
383
384help_start: T_HELP T_EOL
385{
386 printd(DEBUG_PARSE, "%s:%d:help\n", zconf_curname(), zconf_lineno());
387 zconf_starthelp();
388};
389
390help: help_start T_HELPTEXT
391{
Ulf Magnusson6479f322018-01-12 07:47:47 +0100392 if (current_entry->help) {
393 free(current_entry->help);
394 zconfprint("warning: '%s' defined with more than one help text -- only the last one will be used",
395 current_entry->sym->name ?: "<choice>");
396 }
Ulf Magnusson1b9eda22018-01-31 10:34:30 +0100397
398 /* Is the help text empty or all whitespace? */
399 if ($2[strspn($2, " \f\n\r\t\v")] == '\0')
400 zconfprint("warning: '%s' defined with blank help text",
401 current_entry->sym->name ?: "<choice>");
402
Sam Ravnborg03d29122007-07-21 00:00:36 +0200403 current_entry->help = $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
406/* depends option */
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408depends: T_DEPENDS T_ON expr T_EOL
409{
410 menu_add_dep($3);
411 printd(DEBUG_PARSE, "%s:%d:depends on\n", zconf_curname(), zconf_lineno());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412};
413
Arnaud Lacombe86e187f2010-11-06 18:30:23 -0300414/* visibility option */
Masahiro Yamada413cd192018-12-11 20:00:46 +0900415visible: T_VISIBLE if_expr T_EOL
Arnaud Lacombe86e187f2010-11-06 18:30:23 -0300416{
417 menu_add_visibility($2);
418};
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/* prompt statement */
421
422prompt_stmt_opt:
423 /* empty */
Masahiro Yamada1be6e792019-12-17 13:14:19 +0900424 | T_WORD_QUOTE if_expr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
blaisorblade@yahoo.itfb7f6ff2005-07-28 17:56:25 +0200426 menu_add_prompt(P_PROMPT, $1, $2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Masahiro Yamadacaaebb32018-12-11 20:01:06 +0900429end: T_ENDMENU T_EOL { $$ = "menu"; }
430 | T_ENDCHOICE T_EOL { $$ = "choice"; }
431 | T_ENDIF T_EOL { $$ = "if"; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434if_expr: /* empty */ { $$ = NULL; }
435 | T_IF expr { $$ = $2; }
436;
437
438expr: symbol { $$ = expr_alloc_symbol($1); }
Jan Beulich31847b62015-06-15 13:00:21 +0100439 | symbol T_LESS symbol { $$ = expr_alloc_comp(E_LTH, $1, $3); }
440 | symbol T_LESS_EQUAL symbol { $$ = expr_alloc_comp(E_LEQ, $1, $3); }
441 | symbol T_GREATER symbol { $$ = expr_alloc_comp(E_GTH, $1, $3); }
442 | symbol T_GREATER_EQUAL symbol { $$ = expr_alloc_comp(E_GEQ, $1, $3); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 | symbol T_EQUAL symbol { $$ = expr_alloc_comp(E_EQUAL, $1, $3); }
444 | symbol T_UNEQUAL symbol { $$ = expr_alloc_comp(E_UNEQUAL, $1, $3); }
445 | T_OPEN_PAREN expr T_CLOSE_PAREN { $$ = $2; }
446 | T_NOT expr { $$ = expr_alloc_one(E_NOT, $2); }
447 | expr T_OR expr { $$ = expr_alloc_two(E_OR, $1, $3); }
448 | expr T_AND expr { $$ = expr_alloc_two(E_AND, $1, $3); }
449;
450
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200451/* For symbol definitions, selects, etc., where quotes are not accepted */
452nonconst_symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); };
453
454symbol: nonconst_symbol
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100455 | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456;
457
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100458word_opt: /* empty */ { $$ = NULL; }
459 | T_WORD
460
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900461/* assignment statement */
462
Masahiro Yamada171a5152018-12-11 20:01:02 +0900463assignment_stmt: T_WORD assign_op assign_val T_EOL { variable_add($1, $3, $2); free($1); free($3); }
Masahiro Yamadac3d22872018-12-11 20:01:01 +0900464
465assign_op:
466 T_EQUAL { $$ = VAR_RECURSIVE; }
467 | T_COLON_EQUAL { $$ = VAR_SIMPLE; }
468 | T_PLUS_EQUAL { $$ = VAR_APPEND; }
469;
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900470
471assign_val:
472 /* empty */ { $$ = xstrdup(""); };
473 | T_ASSIGN_VAL
474;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476%%
477
478void conf_parse(const char *name)
479{
480 struct symbol *sym;
481 int i;
482
483 zconf_initscan(name);
484
nir.tzachar@gmail.com692d97c2009-11-25 12:28:43 +0200485 _menu_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Roman Zippela02f0572005-11-08 21:34:53 -0800487 if (getenv("ZCONF_DEBUG"))
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900488 yydebug = 1;
489 yyparse();
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900490
491 /* Variables are expanded in the parse phase. We can free them here. */
492 variable_all_del();
493
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900494 if (yynerrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 exit(1);
Yann E. MORIN6902dcc2013-09-03 17:07:18 +0200496 if (!modules_sym)
497 modules_sym = sym_find( "n" );
Arnaud Lacombef6ce00b2010-09-09 21:17:26 -0400498
Masahiro Yamada96d8e482018-05-28 18:21:42 +0900499 if (!menu_has_prompt(&rootmenu)) {
500 current_entry = &rootmenu;
Masahiro Yamada137c0112018-05-28 18:21:44 +0900501 menu_add_prompt(P_MENU, "Main menu", NULL);
Masahiro Yamada96d8e482018-05-28 18:21:42 +0900502 }
Arnaud Lacombef6ce00b2010-09-09 21:17:26 -0400503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 menu_finalize(&rootmenu);
505 for_all_symbols(i, sym) {
Sam Ravnborg5447d342007-05-06 09:20:10 +0200506 if (sym_check_deps(sym))
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900507 yynerrs++;
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900508 }
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900509 if (yynerrs)
Sam Ravnborg5447d342007-05-06 09:20:10 +0200510 exit(1);
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900511 conf_set_changed(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Masahiro Yamadacaaebb32018-12-11 20:01:06 +0900514static bool zconf_endtoken(const char *tokenname,
515 const char *expected_tokenname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Masahiro Yamadacaaebb32018-12-11 20:01:06 +0900517 if (strcmp(tokenname, expected_tokenname)) {
Roman Zippela02f0572005-11-08 21:34:53 -0800518 zconf_error("unexpected '%s' within %s block",
Masahiro Yamadacaaebb32018-12-11 20:01:06 +0900519 tokenname, expected_tokenname);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900520 yynerrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return false;
522 }
523 if (current_menu->file != current_file) {
Roman Zippela02f0572005-11-08 21:34:53 -0800524 zconf_error("'%s' in different file than '%s'",
Masahiro Yamadacaaebb32018-12-11 20:01:06 +0900525 tokenname, expected_tokenname);
Roman Zippela02f0572005-11-08 21:34:53 -0800526 fprintf(stderr, "%s:%d: location of the '%s'\n",
527 current_menu->file->name, current_menu->lineno,
Masahiro Yamadacaaebb32018-12-11 20:01:06 +0900528 expected_tokenname);
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900529 yynerrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return false;
531 }
532 return true;
533}
534
535static void zconfprint(const char *err, ...)
536{
537 va_list ap;
538
Roman Zippela02f0572005-11-08 21:34:53 -0800539 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
540 va_start(ap, err);
541 vfprintf(stderr, err, ap);
542 va_end(ap);
543 fprintf(stderr, "\n");
544}
545
546static void zconf_error(const char *err, ...)
547{
548 va_list ap;
549
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900550 yynerrs++;
Roman Zippela02f0572005-11-08 21:34:53 -0800551 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 va_start(ap, err);
553 vfprintf(stderr, err, ap);
554 va_end(ap);
555 fprintf(stderr, "\n");
556}
557
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900558static void yyerror(const char *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Josh Triplett65166572009-10-15 12:13:36 -0700563static void print_quoted_string(FILE *out, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 const char *p;
566 int len;
567
568 putc('"', out);
569 while ((p = strchr(str, '"'))) {
570 len = p - str;
571 if (len)
572 fprintf(out, "%.*s", len, str);
573 fputs("\\\"", out);
574 str = p + 1;
575 }
576 fputs(str, out);
577 putc('"', out);
578}
579
Josh Triplett65166572009-10-15 12:13:36 -0700580static void print_symbol(FILE *out, struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct symbol *sym = menu->sym;
583 struct property *prop;
584
585 if (sym_is_choice(sym))
Li Zefanc6ccc302010-04-14 11:44:20 +0800586 fprintf(out, "\nchoice\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 else
Li Zefanc6ccc302010-04-14 11:44:20 +0800588 fprintf(out, "\nconfig %s\n", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 switch (sym->type) {
590 case S_BOOLEAN:
Masahiro Yamadab92d8042017-12-16 00:38:02 +0900591 fputs(" bool\n", out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
593 case S_TRISTATE:
594 fputs(" tristate\n", out);
595 break;
596 case S_STRING:
597 fputs(" string\n", out);
598 break;
599 case S_INT:
600 fputs(" integer\n", out);
601 break;
602 case S_HEX:
603 fputs(" hex\n", out);
604 break;
605 default:
606 fputs(" ???\n", out);
607 break;
608 }
609 for (prop = sym->prop; prop; prop = prop->next) {
610 if (prop->menu != menu)
611 continue;
612 switch (prop->type) {
613 case P_PROMPT:
614 fputs(" prompt ", out);
615 print_quoted_string(out, prop->text);
616 if (!expr_is_yes(prop->visible.expr)) {
617 fputs(" if ", out);
618 expr_fprint(prop->visible.expr, out);
619 }
620 fputc('\n', out);
621 break;
622 case P_DEFAULT:
623 fputs( " default ", out);
624 expr_fprint(prop->expr, out);
625 if (!expr_is_yes(prop->visible.expr)) {
626 fputs(" if ", out);
627 expr_fprint(prop->visible.expr, out);
628 }
629 fputc('\n', out);
630 break;
631 case P_CHOICE:
632 fputs(" #choice value\n", out);
633 break;
Li Zefanc6ccc302010-04-14 11:44:20 +0800634 case P_SELECT:
635 fputs( " select ", out);
636 expr_fprint(prop->expr, out);
637 fputc('\n', out);
638 break;
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500639 case P_IMPLY:
640 fputs( " imply ", out);
641 expr_fprint(prop->expr, out);
642 fputc('\n', out);
643 break;
Li Zefanc6ccc302010-04-14 11:44:20 +0800644 case P_RANGE:
645 fputs( " range ", out);
646 expr_fprint(prop->expr, out);
647 fputc('\n', out);
648 break;
649 case P_MENU:
650 fputs( " menu ", out);
651 print_quoted_string(out, prop->text);
652 fputc('\n', out);
653 break;
Dirk Goudersecd53ac2018-06-22 21:27:38 +0200654 case P_SYMBOL:
655 fputs( " symbol ", out);
Masahiro Yamada6397d962019-12-17 13:14:22 +0900656 fprintf(out, "%s\n", prop->menu->sym->name);
Dirk Goudersecd53ac2018-06-22 21:27:38 +0200657 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 default:
659 fprintf(out, " unknown prop %d!\n", prop->type);
660 break;
661 }
662 }
Sam Ravnborg03d29122007-07-21 00:00:36 +0200663 if (menu->help) {
664 int len = strlen(menu->help);
665 while (menu->help[--len] == '\n')
666 menu->help[len] = 0;
667 fprintf(out, " help\n%s\n", menu->help);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671void zconfdump(FILE *out)
672{
673 struct property *prop;
674 struct symbol *sym;
675 struct menu *menu;
676
677 menu = rootmenu.list;
678 while (menu) {
679 if ((sym = menu->sym))
680 print_symbol(out, menu);
681 else if ((prop = menu->prompt)) {
682 switch (prop->type) {
683 case P_COMMENT:
684 fputs("\ncomment ", out);
685 print_quoted_string(out, prop->text);
686 fputs("\n", out);
687 break;
688 case P_MENU:
689 fputs("\nmenu ", out);
690 print_quoted_string(out, prop->text);
691 fputs("\n", out);
692 break;
693 default:
694 ;
695 }
696 if (!expr_is_yes(prop->visible.expr)) {
697 fputs(" depends ", out);
698 expr_fprint(prop->visible.expr, out);
699 fputc('\n', out);
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
703 if (menu->list)
704 menu = menu->list;
705 else if (menu->next)
706 menu = menu->next;
707 else while ((menu = menu->parent)) {
708 if (menu->prompt && menu->prompt->type == P_MENU)
709 fputs("\nendmenu\n", out);
710 if (menu->next) {
711 menu = menu->next;
712 break;
713 }
714 }
715 }
716}