blob: 22fceb264cf51b6972c7d09357265d416c4157a5 [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%}
Masahiro Yamada56869d42018-08-09 15:47:06 +090034%expect 30
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;
Masahiro Yamada1175c022018-05-28 18:21:50 +090044 enum variable_flavor flavor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Roman Zippel3370f9f2005-11-08 21:34:52 -080047%token <id>T_MAINMENU
48%token <id>T_MENU
49%token <id>T_ENDMENU
50%token <id>T_SOURCE
51%token <id>T_CHOICE
52%token <id>T_ENDCHOICE
53%token <id>T_COMMENT
54%token <id>T_CONFIG
55%token <id>T_MENUCONFIG
56%token <id>T_HELP
Linus Torvalds1da177e2005-04-16 15:20:36 -070057%token <string> T_HELPTEXT
Roman Zippel3370f9f2005-11-08 21:34:52 -080058%token <id>T_IF
59%token <id>T_ENDIF
60%token <id>T_DEPENDS
Roman Zippel3370f9f2005-11-08 21:34:52 -080061%token <id>T_OPTIONAL
62%token <id>T_PROMPT
63%token <id>T_TYPE
64%token <id>T_DEFAULT
65%token <id>T_SELECT
Nicolas Pitre237e3ad2016-11-11 00:10:05 -050066%token <id>T_IMPLY
Roman Zippel3370f9f2005-11-08 21:34:52 -080067%token <id>T_RANGE
Arnaud Lacombe86e187f2010-11-06 18:30:23 -030068%token <id>T_VISIBLE
Roman Zippelf6a88aa2006-06-08 22:12:44 -070069%token <id>T_OPTION
Roman Zippel3370f9f2005-11-08 21:34:52 -080070%token <id>T_ON
Linus Torvalds1da177e2005-04-16 15:20:36 -070071%token <string> T_WORD
72%token <string> T_WORD_QUOTE
73%token T_UNEQUAL
Jan Beulich31847b62015-06-15 13:00:21 +010074%token T_LESS
75%token T_LESS_EQUAL
76%token T_GREATER
77%token T_GREATER_EQUAL
Linus Torvalds1da177e2005-04-16 15:20:36 -070078%token T_CLOSE_PAREN
79%token T_OPEN_PAREN
Roman Zippel3370f9f2005-11-08 21:34:52 -080080%token T_EOL
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +090081%token <string> T_VARIABLE
Masahiro Yamada1175c022018-05-28 18:21:50 +090082%token <flavor> T_ASSIGN
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
91%type <string> prompt
Ulf Magnusson26e47a32017-10-08 19:11:18 +020092%type <symbol> nonconst_symbol
Linus Torvalds1da177e2005-04-16 15:20:36 -070093%type <symbol> symbol
94%type <expr> expr
95%type <expr> if_expr
Roman Zippela02f0572005-11-08 21:34:53 -080096%type <id> end
97%type <id> option_name
98%type <menu> if_entry menu_entry choice_entry
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +090099%type <string> symbol_option_arg word_opt assign_val
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
Josh Triplett1456edb2009-10-15 11:03:20 -0700108%{
Ulf Magnussonc8734432017-10-05 05:06:48 +0200109/* Include kconf_id.c here so it can see the token constants. */
Linus Torvaldsbb3290d2017-08-19 10:17:02 -0700110#include "kconf_id.c"
Josh Triplett1456edb2009-10-15 11:03:20 -0700111%}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113%%
Arnaud Lacombe8ea13e22010-08-16 22:55:31 -0400114input: nl start | start;
115
Masahiro Yamada96d8e482018-05-28 18:21:42 +0900116start: mainmenu_stmt stmt_list | stmt_list;
Ulf Magnusson0724a7c2017-10-08 19:11:21 +0200117
118/* mainmenu entry */
119
Masahiro Yamada56869d42018-08-09 15:47:06 +0900120mainmenu_stmt: T_MAINMENU prompt T_EOL
Ulf Magnusson0724a7c2017-10-08 19:11:21 +0200121{
122 menu_add_prompt(P_MENU, $2, NULL);
123};
124
Roman Zippela02f0572005-11-08 21:34:53 -0800125stmt_list:
126 /* empty */
127 | stmt_list common_stmt
128 | stmt_list choice_stmt
129 | stmt_list menu_stmt
Roman Zippela02f0572005-11-08 21:34:53 -0800130 | stmt_list end { zconf_error("unexpected end statement"); }
131 | stmt_list T_WORD error T_EOL { zconf_error("unknown statement \"%s\"", $2); }
132 | stmt_list option_name error T_EOL
133{
Linus Torvaldsbb3290d2017-08-19 10:17:02 -0700134 zconf_error("unexpected option \"%s\"", $2->name);
Roman Zippela02f0572005-11-08 21:34:53 -0800135}
136 | stmt_list error T_EOL { zconf_error("invalid statement"); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137;
138
Roman Zippela02f0572005-11-08 21:34:53 -0800139option_name:
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500140 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 -0700141;
142
Roman Zippela02f0572005-11-08 21:34:53 -0800143common_stmt:
144 T_EOL
145 | if_stmt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 | comment_stmt
147 | config_stmt
148 | menuconfig_stmt
149 | source_stmt
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900150 | assignment_stmt
Roman Zippela02f0572005-11-08 21:34:53 -0800151;
152
153option_error:
154 T_WORD error T_EOL { zconf_error("unknown option \"%s\"", $1); }
155 | error T_EOL { zconf_error("invalid option"); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156;
157
158
159/* config/menuconfig entry */
160
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200161config_entry_start: T_CONFIG nonconst_symbol T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200163 $2->flags |= SYMBOL_OPTIONAL;
164 menu_add_entry($2);
165 printd(DEBUG_PARSE, "%s:%d:config %s\n", zconf_curname(), zconf_lineno(), $2->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
167
168config_stmt: config_entry_start config_option_list
169{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
171};
172
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200173menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200175 $2->flags |= SYMBOL_OPTIONAL;
176 menu_add_entry($2);
177 printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", zconf_curname(), zconf_lineno(), $2->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
180menuconfig_stmt: menuconfig_entry_start config_option_list
181{
182 if (current_entry->prompt)
183 current_entry->prompt->type = P_MENU;
184 else
185 zconfprint("warning: menuconfig statement without prompt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
187};
188
189config_option_list:
190 /* empty */
191 | config_option_list config_option
Roman Zippelf6a88aa2006-06-08 22:12:44 -0700192 | config_option_list symbol_option
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 | config_option_list depends
194 | config_option_list help
Roman Zippela02f0572005-11-08 21:34:53 -0800195 | config_option_list option_error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 | config_option_list T_EOL
197;
198
Roman Zippel3370f9f2005-11-08 21:34:52 -0800199config_option: T_TYPE prompt_stmt_opt T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Roman Zippel3370f9f2005-11-08 21:34:52 -0800201 menu_set_type($1->stype);
202 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
203 zconf_curname(), zconf_lineno(),
204 $1->stype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205};
206
207config_option: T_PROMPT prompt if_expr T_EOL
208{
209 menu_add_prompt(P_PROMPT, $2, $3);
210 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
211};
212
213config_option: T_DEFAULT expr if_expr T_EOL
214{
215 menu_add_expr(P_DEFAULT, $2, $3);
Roman Zippel3370f9f2005-11-08 21:34:52 -0800216 if ($1->stype != S_UNKNOWN)
217 menu_set_type($1->stype);
218 printd(DEBUG_PARSE, "%s:%d:default(%u)\n",
219 zconf_curname(), zconf_lineno(),
220 $1->stype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221};
222
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200223config_option: T_SELECT nonconst_symbol if_expr T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200225 menu_add_symbol(P_SELECT, $2, $3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 printd(DEBUG_PARSE, "%s:%d:select\n", zconf_curname(), zconf_lineno());
227};
228
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200229config_option: T_IMPLY nonconst_symbol if_expr T_EOL
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500230{
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200231 menu_add_symbol(P_IMPLY, $2, $3);
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500232 printd(DEBUG_PARSE, "%s:%d:imply\n", zconf_curname(), zconf_lineno());
233};
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235config_option: T_RANGE symbol symbol if_expr T_EOL
236{
237 menu_add_expr(P_RANGE, expr_alloc_comp(E_RANGE,$2, $3), $4);
238 printd(DEBUG_PARSE, "%s:%d:range\n", zconf_curname(), zconf_lineno());
239};
240
Roman Zippelf6a88aa2006-06-08 22:12:44 -0700241symbol_option: T_OPTION symbol_option_list T_EOL
242;
243
244symbol_option_list:
245 /* empty */
246 | symbol_option_list T_WORD symbol_option_arg
247{
Arnaud Lacombe61f956f2011-05-04 21:14:44 -0400248 const struct kconf_id *id = kconf_id_lookup($2, strlen($2));
Ulf Magnussonbc28fe12017-10-08 19:11:20 +0200249 if (id && id->flags & TF_OPTION) {
Roman Zippelf6a88aa2006-06-08 22:12:44 -0700250 menu_add_option(id->token, $3);
Ulf Magnussonbc28fe12017-10-08 19:11:20 +0200251 free($3);
252 }
Roman Zippelf6a88aa2006-06-08 22:12:44 -0700253 else
254 zconfprint("warning: ignoring unknown option %s", $2);
255 free($2);
256};
257
258symbol_option_arg:
259 /* empty */ { $$ = NULL; }
260 | T_EQUAL prompt { $$ = $2; }
261;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/* choice entry */
264
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100265choice: T_CHOICE word_opt T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100267 struct symbol *sym = sym_lookup($2, SYMBOL_CHOICE);
Dirk Gouders693359f2018-07-03 14:43:31 +0200268 sym->flags |= SYMBOL_NO_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 menu_add_entry(sym);
270 menu_add_expr(P_CHOICE, NULL, NULL);
Masahiro Yamadabf0bbdc2018-02-20 20:40:29 +0900271 free($2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 printd(DEBUG_PARSE, "%s:%d:choice\n", zconf_curname(), zconf_lineno());
273};
274
275choice_entry: choice choice_option_list
276{
Roman Zippela02f0572005-11-08 21:34:53 -0800277 $$ = menu_add_menu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278};
279
280choice_end: end
281{
282 if (zconf_endtoken($1, T_CHOICE, T_ENDCHOICE)) {
283 menu_end_menu();
284 printd(DEBUG_PARSE, "%s:%d:endchoice\n", zconf_curname(), zconf_lineno());
285 }
286};
287
Roman Zippela02f0572005-11-08 21:34:53 -0800288choice_stmt: choice_entry choice_block choice_end
289;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291choice_option_list:
292 /* empty */
293 | choice_option_list choice_option
294 | choice_option_list depends
295 | choice_option_list help
296 | choice_option_list T_EOL
Roman Zippela02f0572005-11-08 21:34:53 -0800297 | choice_option_list option_error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298;
299
300choice_option: T_PROMPT prompt if_expr T_EOL
301{
302 menu_add_prompt(P_PROMPT, $2, $3);
303 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
304};
305
Roman Zippel3370f9f2005-11-08 21:34:52 -0800306choice_option: T_TYPE prompt_stmt_opt T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Roman Zippel3370f9f2005-11-08 21:34:52 -0800308 if ($1->stype == S_BOOLEAN || $1->stype == S_TRISTATE) {
309 menu_set_type($1->stype);
310 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
311 zconf_curname(), zconf_lineno(),
312 $1->stype);
313 } else
314 YYERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};
316
317choice_option: T_OPTIONAL T_EOL
318{
319 current_entry->sym->flags |= SYMBOL_OPTIONAL;
320 printd(DEBUG_PARSE, "%s:%d:optional\n", zconf_curname(), zconf_lineno());
321};
322
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200323choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Roman Zippel3370f9f2005-11-08 21:34:52 -0800325 if ($1->stype == S_UNKNOWN) {
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200326 menu_add_symbol(P_DEFAULT, $2, $3);
Roman Zippel3370f9f2005-11-08 21:34:52 -0800327 printd(DEBUG_PARSE, "%s:%d:default\n",
328 zconf_curname(), zconf_lineno());
329 } else
330 YYERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331};
332
333choice_block:
334 /* empty */
Roman Zippela02f0572005-11-08 21:34:53 -0800335 | choice_block common_stmt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336;
337
338/* if entry */
339
Dirk Goudersb2d00d72018-06-21 15:30:54 +0200340if_entry: T_IF expr T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 printd(DEBUG_PARSE, "%s:%d:if\n", zconf_curname(), zconf_lineno());
343 menu_add_entry(NULL);
344 menu_add_dep($2);
Roman Zippela02f0572005-11-08 21:34:53 -0800345 $$ = menu_add_menu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346};
347
348if_end: end
349{
350 if (zconf_endtoken($1, T_IF, T_ENDIF)) {
351 menu_end_menu();
352 printd(DEBUG_PARSE, "%s:%d:endif\n", zconf_curname(), zconf_lineno());
353 }
354};
355
Roman Zippela02f0572005-11-08 21:34:53 -0800356if_stmt: if_entry if_block if_end
357;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359if_block:
360 /* empty */
Roman Zippela02f0572005-11-08 21:34:53 -0800361 | if_block common_stmt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 | if_block menu_stmt
363 | if_block choice_stmt
364;
365
366/* menu entry */
367
368menu: T_MENU prompt T_EOL
369{
370 menu_add_entry(NULL);
blaisorblade@yahoo.itfb7f6ff2005-07-28 17:56:25 +0200371 menu_add_prompt(P_MENU, $2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 printd(DEBUG_PARSE, "%s:%d:menu\n", zconf_curname(), zconf_lineno());
373};
374
Arnaud Lacombe86e187f2010-11-06 18:30:23 -0300375menu_entry: menu visibility_list depends_list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Roman Zippela02f0572005-11-08 21:34:53 -0800377 $$ = menu_add_menu();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
379
380menu_end: end
381{
382 if (zconf_endtoken($1, T_MENU, T_ENDMENU)) {
383 menu_end_menu();
384 printd(DEBUG_PARSE, "%s:%d:endmenu\n", zconf_curname(), zconf_lineno());
385 }
386};
387
Roman Zippela02f0572005-11-08 21:34:53 -0800388menu_stmt: menu_entry menu_block menu_end
389;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391menu_block:
392 /* empty */
Roman Zippela02f0572005-11-08 21:34:53 -0800393 | menu_block common_stmt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 | menu_block menu_stmt
395 | menu_block choice_stmt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396;
397
Roman Zippela02f0572005-11-08 21:34:53 -0800398source_stmt: T_SOURCE prompt T_EOL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 printd(DEBUG_PARSE, "%s:%d:source %s\n", zconf_curname(), zconf_lineno(), $2);
Roman Zippela02f0572005-11-08 21:34:53 -0800401 zconf_nextfile($2);
Ulf Magnusson24161a62017-10-08 19:11:19 +0200402 free($2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403};
404
405/* comment entry */
406
407comment: T_COMMENT prompt T_EOL
408{
409 menu_add_entry(NULL);
blaisorblade@yahoo.itfb7f6ff2005-07-28 17:56:25 +0200410 menu_add_prompt(P_COMMENT, $2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 printd(DEBUG_PARSE, "%s:%d:comment\n", zconf_curname(), zconf_lineno());
412};
413
414comment_stmt: comment depends_list
Ulf Magnussondf60f4b2017-10-09 00:14:48 +0200415;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417/* help option */
418
419help_start: T_HELP T_EOL
420{
421 printd(DEBUG_PARSE, "%s:%d:help\n", zconf_curname(), zconf_lineno());
422 zconf_starthelp();
423};
424
425help: help_start T_HELPTEXT
426{
Ulf Magnusson6479f322018-01-12 07:47:47 +0100427 if (current_entry->help) {
428 free(current_entry->help);
429 zconfprint("warning: '%s' defined with more than one help text -- only the last one will be used",
430 current_entry->sym->name ?: "<choice>");
431 }
Ulf Magnusson1b9eda22018-01-31 10:34:30 +0100432
433 /* Is the help text empty or all whitespace? */
434 if ($2[strspn($2, " \f\n\r\t\v")] == '\0')
435 zconfprint("warning: '%s' defined with blank help text",
436 current_entry->sym->name ?: "<choice>");
437
Sam Ravnborg03d29122007-07-21 00:00:36 +0200438 current_entry->help = $2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439};
440
441/* depends option */
442
Roman Zippela02f0572005-11-08 21:34:53 -0800443depends_list:
444 /* empty */
445 | depends_list depends
446 | depends_list T_EOL
447 | depends_list option_error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448;
449
450depends: T_DEPENDS T_ON expr T_EOL
451{
452 menu_add_dep($3);
453 printd(DEBUG_PARSE, "%s:%d:depends on\n", zconf_curname(), zconf_lineno());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454};
455
Arnaud Lacombe86e187f2010-11-06 18:30:23 -0300456/* visibility option */
457
458visibility_list:
459 /* empty */
460 | visibility_list visible
461 | visibility_list T_EOL
462;
463
464visible: T_VISIBLE if_expr
465{
466 menu_add_visibility($2);
467};
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/* prompt statement */
470
471prompt_stmt_opt:
472 /* empty */
473 | prompt if_expr
474{
blaisorblade@yahoo.itfb7f6ff2005-07-28 17:56:25 +0200475 menu_add_prompt(P_PROMPT, $1, $2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476};
477
478prompt: T_WORD
479 | T_WORD_QUOTE
480;
481
Roman Zippela02f0572005-11-08 21:34:53 -0800482end: T_ENDMENU T_EOL { $$ = $1; }
483 | T_ENDCHOICE T_EOL { $$ = $1; }
484 | T_ENDIF T_EOL { $$ = $1; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485;
486
Roman Zippela02f0572005-11-08 21:34:53 -0800487nl:
488 T_EOL
489 | nl T_EOL
490;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492if_expr: /* empty */ { $$ = NULL; }
493 | T_IF expr { $$ = $2; }
494;
495
496expr: symbol { $$ = expr_alloc_symbol($1); }
Jan Beulich31847b62015-06-15 13:00:21 +0100497 | symbol T_LESS symbol { $$ = expr_alloc_comp(E_LTH, $1, $3); }
498 | symbol T_LESS_EQUAL symbol { $$ = expr_alloc_comp(E_LEQ, $1, $3); }
499 | symbol T_GREATER symbol { $$ = expr_alloc_comp(E_GTH, $1, $3); }
500 | symbol T_GREATER_EQUAL symbol { $$ = expr_alloc_comp(E_GEQ, $1, $3); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 | symbol T_EQUAL symbol { $$ = expr_alloc_comp(E_EQUAL, $1, $3); }
502 | symbol T_UNEQUAL symbol { $$ = expr_alloc_comp(E_UNEQUAL, $1, $3); }
503 | T_OPEN_PAREN expr T_CLOSE_PAREN { $$ = $2; }
504 | T_NOT expr { $$ = expr_alloc_one(E_NOT, $2); }
505 | expr T_OR expr { $$ = expr_alloc_two(E_OR, $1, $3); }
506 | expr T_AND expr { $$ = expr_alloc_two(E_AND, $1, $3); }
507;
508
Ulf Magnusson26e47a32017-10-08 19:11:18 +0200509/* For symbol definitions, selects, etc., where quotes are not accepted */
510nonconst_symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); };
511
512symbol: nonconst_symbol
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100513 | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514;
515
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100516word_opt: /* empty */ { $$ = NULL; }
517 | T_WORD
518
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900519/* assignment statement */
520
Masahiro Yamada1175c022018-05-28 18:21:50 +0900521assignment_stmt: T_VARIABLE T_ASSIGN assign_val T_EOL { variable_add($1, $3, $2); free($1); free($3); }
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900522
523assign_val:
524 /* empty */ { $$ = xstrdup(""); };
525 | T_ASSIGN_VAL
526;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528%%
529
530void conf_parse(const char *name)
531{
532 struct symbol *sym;
533 int i;
534
535 zconf_initscan(name);
536
nir.tzachar@gmail.com692d97c2009-11-25 12:28:43 +0200537 _menu_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Roman Zippela02f0572005-11-08 21:34:53 -0800539 if (getenv("ZCONF_DEBUG"))
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900540 yydebug = 1;
541 yyparse();
Masahiro Yamada9ced3bd2018-05-28 18:21:49 +0900542
543 /* Variables are expanded in the parse phase. We can free them here. */
544 variable_all_del();
545
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900546 if (yynerrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 exit(1);
Yann E. MORIN6902dcc2013-09-03 17:07:18 +0200548 if (!modules_sym)
549 modules_sym = sym_find( "n" );
Arnaud Lacombef6ce00b2010-09-09 21:17:26 -0400550
Masahiro Yamada96d8e482018-05-28 18:21:42 +0900551 if (!menu_has_prompt(&rootmenu)) {
552 current_entry = &rootmenu;
Masahiro Yamada137c0112018-05-28 18:21:44 +0900553 menu_add_prompt(P_MENU, "Main menu", NULL);
Masahiro Yamada96d8e482018-05-28 18:21:42 +0900554 }
Arnaud Lacombef6ce00b2010-09-09 21:17:26 -0400555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 menu_finalize(&rootmenu);
557 for_all_symbols(i, sym) {
Sam Ravnborg5447d342007-05-06 09:20:10 +0200558 if (sym_check_deps(sym))
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900559 yynerrs++;
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900560 }
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900561 if (yynerrs)
Sam Ravnborg5447d342007-05-06 09:20:10 +0200562 exit(1);
Karsten Wiesebfc10002006-12-13 00:34:07 -0800563 sym_set_change_count(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Josh Triplett65166572009-10-15 12:13:36 -0700566static const char *zconf_tokenname(int token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 switch (token) {
569 case T_MENU: return "menu";
570 case T_ENDMENU: return "endmenu";
571 case T_CHOICE: return "choice";
572 case T_ENDCHOICE: return "endchoice";
573 case T_IF: return "if";
574 case T_ENDIF: return "endif";
Roman Zippela02f0572005-11-08 21:34:53 -0800575 case T_DEPENDS: return "depends";
Arnaud Lacombe86e187f2010-11-06 18:30:23 -0300576 case T_VISIBLE: return "visible";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 return "<token>";
579}
580
Arnaud Lacombe61f956f2011-05-04 21:14:44 -0400581static bool zconf_endtoken(const struct kconf_id *id, int starttoken, int endtoken)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Roman Zippela02f0572005-11-08 21:34:53 -0800583 if (id->token != endtoken) {
584 zconf_error("unexpected '%s' within %s block",
Linus Torvaldsbb3290d2017-08-19 10:17:02 -0700585 id->name, zconf_tokenname(starttoken));
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900586 yynerrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return false;
588 }
589 if (current_menu->file != current_file) {
Roman Zippela02f0572005-11-08 21:34:53 -0800590 zconf_error("'%s' in different file than '%s'",
Linus Torvaldsbb3290d2017-08-19 10:17:02 -0700591 id->name, zconf_tokenname(starttoken));
Roman Zippela02f0572005-11-08 21:34:53 -0800592 fprintf(stderr, "%s:%d: location of the '%s'\n",
593 current_menu->file->name, current_menu->lineno,
594 zconf_tokenname(starttoken));
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900595 yynerrs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return false;
597 }
598 return true;
599}
600
601static void zconfprint(const char *err, ...)
602{
603 va_list ap;
604
Roman Zippela02f0572005-11-08 21:34:53 -0800605 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
606 va_start(ap, err);
607 vfprintf(stderr, err, ap);
608 va_end(ap);
609 fprintf(stderr, "\n");
610}
611
612static void zconf_error(const char *err, ...)
613{
614 va_list ap;
615
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900616 yynerrs++;
Roman Zippela02f0572005-11-08 21:34:53 -0800617 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 va_start(ap, err);
619 vfprintf(stderr, err, ap);
620 va_end(ap);
621 fprintf(stderr, "\n");
622}
623
Masahiro Yamada765f4cd2018-01-12 00:50:50 +0900624static void yyerror(const char *err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Josh Triplett65166572009-10-15 12:13:36 -0700629static void print_quoted_string(FILE *out, const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 const char *p;
632 int len;
633
634 putc('"', out);
635 while ((p = strchr(str, '"'))) {
636 len = p - str;
637 if (len)
638 fprintf(out, "%.*s", len, str);
639 fputs("\\\"", out);
640 str = p + 1;
641 }
642 fputs(str, out);
643 putc('"', out);
644}
645
Josh Triplett65166572009-10-15 12:13:36 -0700646static void print_symbol(FILE *out, struct menu *menu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct symbol *sym = menu->sym;
649 struct property *prop;
650
651 if (sym_is_choice(sym))
Li Zefanc6ccc302010-04-14 11:44:20 +0800652 fprintf(out, "\nchoice\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 else
Li Zefanc6ccc302010-04-14 11:44:20 +0800654 fprintf(out, "\nconfig %s\n", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 switch (sym->type) {
656 case S_BOOLEAN:
Masahiro Yamadab92d8042017-12-16 00:38:02 +0900657 fputs(" bool\n", out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659 case S_TRISTATE:
660 fputs(" tristate\n", out);
661 break;
662 case S_STRING:
663 fputs(" string\n", out);
664 break;
665 case S_INT:
666 fputs(" integer\n", out);
667 break;
668 case S_HEX:
669 fputs(" hex\n", out);
670 break;
671 default:
672 fputs(" ???\n", out);
673 break;
674 }
675 for (prop = sym->prop; prop; prop = prop->next) {
676 if (prop->menu != menu)
677 continue;
678 switch (prop->type) {
679 case P_PROMPT:
680 fputs(" prompt ", out);
681 print_quoted_string(out, prop->text);
682 if (!expr_is_yes(prop->visible.expr)) {
683 fputs(" if ", out);
684 expr_fprint(prop->visible.expr, out);
685 }
686 fputc('\n', out);
687 break;
688 case P_DEFAULT:
689 fputs( " default ", out);
690 expr_fprint(prop->expr, out);
691 if (!expr_is_yes(prop->visible.expr)) {
692 fputs(" if ", out);
693 expr_fprint(prop->visible.expr, out);
694 }
695 fputc('\n', out);
696 break;
697 case P_CHOICE:
698 fputs(" #choice value\n", out);
699 break;
Li Zefanc6ccc302010-04-14 11:44:20 +0800700 case P_SELECT:
701 fputs( " select ", out);
702 expr_fprint(prop->expr, out);
703 fputc('\n', out);
704 break;
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500705 case P_IMPLY:
706 fputs( " imply ", out);
707 expr_fprint(prop->expr, out);
708 fputc('\n', out);
709 break;
Li Zefanc6ccc302010-04-14 11:44:20 +0800710 case P_RANGE:
711 fputs( " range ", out);
712 expr_fprint(prop->expr, out);
713 fputc('\n', out);
714 break;
715 case P_MENU:
716 fputs( " menu ", out);
717 print_quoted_string(out, prop->text);
718 fputc('\n', out);
719 break;
Dirk Goudersecd53ac2018-06-22 21:27:38 +0200720 case P_SYMBOL:
721 fputs( " symbol ", out);
722 fprintf(out, "%s\n", prop->sym->name);
723 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 default:
725 fprintf(out, " unknown prop %d!\n", prop->type);
726 break;
727 }
728 }
Sam Ravnborg03d29122007-07-21 00:00:36 +0200729 if (menu->help) {
730 int len = strlen(menu->help);
731 while (menu->help[--len] == '\n')
732 menu->help[len] = 0;
733 fprintf(out, " help\n%s\n", menu->help);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737void zconfdump(FILE *out)
738{
739 struct property *prop;
740 struct symbol *sym;
741 struct menu *menu;
742
743 menu = rootmenu.list;
744 while (menu) {
745 if ((sym = menu->sym))
746 print_symbol(out, menu);
747 else if ((prop = menu->prompt)) {
748 switch (prop->type) {
749 case P_COMMENT:
750 fputs("\ncomment ", out);
751 print_quoted_string(out, prop->text);
752 fputs("\n", out);
753 break;
754 case P_MENU:
755 fputs("\nmenu ", out);
756 print_quoted_string(out, prop->text);
757 fputs("\n", out);
758 break;
759 default:
760 ;
761 }
762 if (!expr_is_yes(prop->visible.expr)) {
763 fputs(" depends ", out);
764 expr_fprint(prop->visible.expr, out);
765 fputc('\n', out);
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769 if (menu->list)
770 menu = menu->list;
771 else if (menu->next)
772 menu = menu->next;
773 else while ((menu = menu->parent)) {
774 if (menu->prompt && menu->prompt->type == P_MENU)
775 fputs("\nendmenu\n", out);
776 if (menu->next) {
777 menu = menu->next;
778 break;
779 }
780 }
781 }
782}
783
Arnaud Lacombe378dbb22011-05-23 02:08:52 -0400784#include "zconf.lex.c"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785#include "util.c"
786#include "confdata.c"
787#include "expr.c"
788#include "symbol.c"
789#include "menu.c"
Masahiro Yamada104daea2018-05-28 18:21:40 +0900790#include "preprocess.c"