1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
17 #define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt)
20 #define DEBUG_PARSE 0x0002
24 static void yyerror(const char *err);
25 static void zconfprint(const char *err, ...);
26 static void zconf_error(const char *err, ...);
27 static bool zconf_endtoken(const char *tokenname,
28 const char *expected_tokenname);
30 struct symbol *symbol_hash[SYMBOL_HASHSIZE];
32 struct menu *current_menu, *current_entry;
39 struct symbol *symbol;
42 enum symbol_type type;
43 enum variable_flavor flavor;
46 %token <string> T_HELPTEXT
47 %token <string> T_WORD
48 %token <string> T_WORD_QUOTE
83 %token <string> T_ASSIGN_VAL
87 %left T_EQUAL T_UNEQUAL
88 %left T_LESS T_LESS_EQUAL T_GREATER T_GREATER_EQUAL
91 %type <symbol> nonconst_symbol
93 %type <type> type logic_type default
97 %type <menu> if_entry menu_entry choice_entry
98 %type <string> word_opt assign_val
99 %type <flavor> assign_op
102 fprintf(stderr, "%s:%d: missing end statement for this entry\n",
103 $$->file->name, $$->lineno);
104 if (current_menu == $$)
106 } if_entry menu_entry choice_entry
109 input: mainmenu_stmt stmt_list | stmt_list;
113 mainmenu_stmt: T_MAINMENU T_WORD_QUOTE T_EOL
115 menu_add_prompt(P_MENU, $2, NULL);
120 | stmt_list assignment_stmt
121 | stmt_list choice_stmt
122 | stmt_list comment_stmt
123 | stmt_list config_stmt
125 | stmt_list menu_stmt
126 | stmt_list menuconfig_stmt
127 | stmt_list source_stmt
128 | stmt_list T_WORD error T_EOL { zconf_error("unknown statement \"%s\"", $2); }
129 | stmt_list error T_EOL { zconf_error("invalid statement"); }
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"); }
140 /* config/menuconfig entry */
142 config_entry_start: T_CONFIG nonconst_symbol T_EOL
144 $2->flags |= SYMBOL_OPTIONAL;
146 printd(DEBUG_PARSE, "%s:%d:config %s\n", zconf_curname(), zconf_lineno(), $2->name);
149 config_stmt: config_entry_start config_option_list
151 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
154 menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL
156 $2->flags |= SYMBOL_OPTIONAL;
158 printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", zconf_curname(), zconf_lineno(), $2->name);
161 menuconfig_stmt: menuconfig_entry_start config_option_list
163 if (current_entry->prompt)
164 current_entry->prompt->type = P_MENU;
166 zconfprint("warning: menuconfig statement without prompt");
167 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
172 | config_option_list config_option
173 | config_option_list depends
174 | config_option_list help
177 config_option: type prompt_stmt_opt T_EOL
180 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
181 zconf_curname(), zconf_lineno(),
185 config_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
187 menu_add_prompt(P_PROMPT, $2, $3);
188 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
191 config_option: default expr if_expr T_EOL
193 menu_add_expr(P_DEFAULT, $2, $3);
196 printd(DEBUG_PARSE, "%s:%d:default(%u)\n",
197 zconf_curname(), zconf_lineno(),
201 config_option: T_SELECT nonconst_symbol if_expr T_EOL
203 menu_add_symbol(P_SELECT, $2, $3);
204 printd(DEBUG_PARSE, "%s:%d:select\n", zconf_curname(), zconf_lineno());
207 config_option: T_IMPLY nonconst_symbol if_expr T_EOL
209 menu_add_symbol(P_IMPLY, $2, $3);
210 printd(DEBUG_PARSE, "%s:%d:imply\n", zconf_curname(), zconf_lineno());
213 config_option: T_RANGE symbol symbol if_expr T_EOL
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());
219 config_option: T_MODULES T_EOL
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;
229 choice: T_CHOICE word_opt T_EOL
231 struct symbol *sym = sym_lookup($2, SYMBOL_CHOICE);
232 sym->flags |= SYMBOL_NO_WRITE;
234 menu_add_expr(P_CHOICE, NULL, NULL);
236 printd(DEBUG_PARSE, "%s:%d:choice\n", zconf_curname(), zconf_lineno());
239 choice_entry: choice choice_option_list
241 $$ = menu_add_menu();
246 if (zconf_endtoken($1, "choice")) {
248 printd(DEBUG_PARSE, "%s:%d:endchoice\n", zconf_curname(), zconf_lineno());
252 choice_stmt: choice_entry stmt_list_in_choice choice_end
257 | choice_option_list choice_option
258 | choice_option_list depends
259 | choice_option_list help
262 choice_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
264 menu_add_prompt(P_PROMPT, $2, $3);
265 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
268 choice_option: logic_type prompt_stmt_opt T_EOL
271 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
272 zconf_curname(), zconf_lineno(), $1);
275 choice_option: T_OPTIONAL T_EOL
277 current_entry->sym->flags |= SYMBOL_OPTIONAL;
278 printd(DEBUG_PARSE, "%s:%d:optional\n", zconf_curname(), zconf_lineno());
281 choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL
283 menu_add_symbol(P_DEFAULT, $2, $3);
284 printd(DEBUG_PARSE, "%s:%d:default\n",
285 zconf_curname(), zconf_lineno());
290 | T_INT { $$ = S_INT; }
291 | T_HEX { $$ = S_HEX; }
292 | T_STRING { $$ = S_STRING; }
295 T_BOOL { $$ = S_BOOLEAN; }
296 | T_TRISTATE { $$ = S_TRISTATE; }
299 T_DEFAULT { $$ = S_UNKNOWN; }
300 | T_DEF_BOOL { $$ = S_BOOLEAN; }
301 | T_DEF_TRISTATE { $$ = S_TRISTATE; }
305 if_entry: T_IF expr T_EOL
307 printd(DEBUG_PARSE, "%s:%d:if\n", zconf_curname(), zconf_lineno());
308 menu_add_entry(NULL);
310 $$ = menu_add_menu();
315 if (zconf_endtoken($1, "if")) {
317 printd(DEBUG_PARSE, "%s:%d:endif\n", zconf_curname(), zconf_lineno());
321 if_stmt: if_entry stmt_list if_end
324 if_stmt_in_choice: if_entry stmt_list_in_choice if_end
329 menu: T_MENU T_WORD_QUOTE T_EOL
331 menu_add_entry(NULL);
332 menu_add_prompt(P_MENU, $2, NULL);
333 printd(DEBUG_PARSE, "%s:%d:menu\n", zconf_curname(), zconf_lineno());
336 menu_entry: menu menu_option_list
338 $$ = menu_add_menu();
343 if (zconf_endtoken($1, "menu")) {
345 printd(DEBUG_PARSE, "%s:%d:endmenu\n", zconf_curname(), zconf_lineno());
349 menu_stmt: menu_entry stmt_list menu_end
354 | menu_option_list visible
355 | menu_option_list depends
358 source_stmt: T_SOURCE T_WORD_QUOTE T_EOL
360 printd(DEBUG_PARSE, "%s:%d:source %s\n", zconf_curname(), zconf_lineno(), $2);
367 comment: T_COMMENT T_WORD_QUOTE T_EOL
369 menu_add_entry(NULL);
370 menu_add_prompt(P_COMMENT, $2, NULL);
371 printd(DEBUG_PARSE, "%s:%d:comment\n", zconf_curname(), zconf_lineno());
374 comment_stmt: comment comment_option_list
379 | comment_option_list depends
384 help_start: T_HELP T_EOL
386 printd(DEBUG_PARSE, "%s:%d:help\n", zconf_curname(), zconf_lineno());
390 help: help_start T_HELPTEXT
392 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>");
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>");
403 current_entry->help = $2;
408 depends: T_DEPENDS T_ON expr T_EOL
411 printd(DEBUG_PARSE, "%s:%d:depends on\n", zconf_curname(), zconf_lineno());
414 /* visibility option */
415 visible: T_VISIBLE if_expr T_EOL
417 menu_add_visibility($2);
420 /* prompt statement */
424 | T_WORD_QUOTE if_expr
426 menu_add_prompt(P_PROMPT, $1, $2);
429 end: T_ENDMENU T_EOL { $$ = "menu"; }
430 | T_ENDCHOICE T_EOL { $$ = "choice"; }
431 | T_ENDIF T_EOL { $$ = "if"; }
434 if_expr: /* empty */ { $$ = NULL; }
435 | T_IF expr { $$ = $2; }
438 expr: symbol { $$ = expr_alloc_symbol($1); }
439 | 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); }
443 | 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); }
451 /* For symbol definitions, selects, etc., where quotes are not accepted */
452 nonconst_symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); };
454 symbol: nonconst_symbol
455 | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); }
458 word_opt: /* empty */ { $$ = NULL; }
461 /* assignment statement */
463 assignment_stmt: T_WORD assign_op assign_val T_EOL { variable_add($1, $3, $2); free($1); free($3); }
466 T_EQUAL { $$ = VAR_RECURSIVE; }
467 | T_COLON_EQUAL { $$ = VAR_SIMPLE; }
468 | T_PLUS_EQUAL { $$ = VAR_APPEND; }
472 /* empty */ { $$ = xstrdup(""); };
478 void conf_parse(const char *name)
483 zconf_initscan(name);
487 if (getenv("ZCONF_DEBUG"))
491 /* Variables are expanded in the parse phase. We can free them here. */
497 modules_sym = sym_find( "n" );
499 if (!menu_has_prompt(&rootmenu)) {
500 current_entry = &rootmenu;
501 menu_add_prompt(P_MENU, "Main menu", NULL);
504 menu_finalize(&rootmenu);
505 for_all_symbols(i, sym) {
506 if (sym_check_deps(sym))
511 conf_set_changed(true);
514 static bool zconf_endtoken(const char *tokenname,
515 const char *expected_tokenname)
517 if (strcmp(tokenname, expected_tokenname)) {
518 zconf_error("unexpected '%s' within %s block",
519 tokenname, expected_tokenname);
523 if (current_menu->file != current_file) {
524 zconf_error("'%s' in different file than '%s'",
525 tokenname, expected_tokenname);
526 fprintf(stderr, "%s:%d: location of the '%s'\n",
527 current_menu->file->name, current_menu->lineno,
535 static void zconfprint(const char *err, ...)
539 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
541 vfprintf(stderr, err, ap);
543 fprintf(stderr, "\n");
546 static void zconf_error(const char *err, ...)
551 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
553 vfprintf(stderr, err, ap);
555 fprintf(stderr, "\n");
558 static void yyerror(const char *err)
560 fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err);
563 static void print_quoted_string(FILE *out, const char *str)
569 while ((p = strchr(str, '"'))) {
572 fprintf(out, "%.*s", len, str);
580 static void print_symbol(FILE *out, struct menu *menu)
582 struct symbol *sym = menu->sym;
583 struct property *prop;
585 if (sym_is_choice(sym))
586 fprintf(out, "\nchoice\n");
588 fprintf(out, "\nconfig %s\n", sym->name);
591 fputs(" bool\n", out);
594 fputs(" tristate\n", out);
597 fputs(" string\n", out);
600 fputs(" integer\n", out);
603 fputs(" hex\n", out);
606 fputs(" ???\n", out);
609 for (prop = sym->prop; prop; prop = prop->next) {
610 if (prop->menu != menu)
612 switch (prop->type) {
614 fputs(" prompt ", out);
615 print_quoted_string(out, prop->text);
616 if (!expr_is_yes(prop->visible.expr)) {
618 expr_fprint(prop->visible.expr, out);
623 fputs( " default ", out);
624 expr_fprint(prop->expr, out);
625 if (!expr_is_yes(prop->visible.expr)) {
627 expr_fprint(prop->visible.expr, out);
632 fputs(" #choice value\n", out);
635 fputs( " select ", out);
636 expr_fprint(prop->expr, out);
640 fputs( " imply ", out);
641 expr_fprint(prop->expr, out);
645 fputs( " range ", out);
646 expr_fprint(prop->expr, out);
650 fputs( " menu ", out);
651 print_quoted_string(out, prop->text);
655 fputs( " symbol ", out);
656 fprintf(out, "%s\n", prop->menu->sym->name);
659 fprintf(out, " unknown prop %d!\n", prop->type);
664 int len = strlen(menu->help);
665 while (menu->help[--len] == '\n')
667 fprintf(out, " help\n%s\n", menu->help);
671 void zconfdump(FILE *out)
673 struct property *prop;
677 menu = rootmenu.list;
679 if ((sym = menu->sym))
680 print_symbol(out, menu);
681 else if ((prop = menu->prompt)) {
682 switch (prop->type) {
684 fputs("\ncomment ", out);
685 print_quoted_string(out, prop->text);
689 fputs("\nmenu ", out);
690 print_quoted_string(out, prop->text);
696 if (!expr_is_yes(prop->visible.expr)) {
697 fputs(" depends ", out);
698 expr_fprint(prop->visible.expr, out);
707 else while ((menu = menu->parent)) {
708 if (menu->prompt && menu->prompt->type == P_MENU)
709 fputs("\nendmenu\n", out);