1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
16 #define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt)
19 #define DEBUG_PARSE 0x0002
23 static void yyerror(const char *err);
24 static void zconfprint(const char *err, ...);
25 static void zconf_error(const char *err, ...);
26 static bool zconf_endtoken(const char *tokenname,
27 const char *expected_tokenname);
29 struct symbol *symbol_hash[SYMBOL_HASHSIZE];
31 static struct menu *current_menu, *current_entry;
38 struct symbol *symbol;
41 enum symbol_type type;
42 enum variable_flavor flavor;
45 %token <string> T_HELPTEXT
46 %token <string> T_WORD
47 %token <string> T_WORD_QUOTE
48 %token T_ALLNOCONFIG_Y
56 %token T_DEFCONFIG_LIST
85 %token <string> T_ASSIGN_VAL
89 %left T_EQUAL T_UNEQUAL
90 %left T_LESS T_LESS_EQUAL T_GREATER T_GREATER_EQUAL
93 %type <symbol> nonconst_symbol
95 %type <type> type logic_type default
99 %type <menu> if_entry menu_entry choice_entry
100 %type <string> word_opt assign_val
101 %type <flavor> assign_op
104 fprintf(stderr, "%s:%d: missing end statement for this entry\n",
105 $$->file->name, $$->lineno);
106 if (current_menu == $$)
108 } if_entry menu_entry choice_entry
111 input: mainmenu_stmt stmt_list | stmt_list;
115 mainmenu_stmt: T_MAINMENU T_WORD_QUOTE T_EOL
117 menu_add_prompt(P_MENU, $2, NULL);
122 | stmt_list assignment_stmt
123 | stmt_list choice_stmt
124 | stmt_list comment_stmt
125 | stmt_list config_stmt
127 | stmt_list menu_stmt
128 | stmt_list menuconfig_stmt
129 | stmt_list source_stmt
130 | stmt_list T_WORD error T_EOL { zconf_error("unknown statement \"%s\"", $2); }
131 | stmt_list error T_EOL { zconf_error("invalid statement"); }
136 | stmt_list_in_choice comment_stmt
137 | stmt_list_in_choice config_stmt
138 | stmt_list_in_choice if_stmt_in_choice
139 | stmt_list_in_choice error T_EOL { zconf_error("invalid statement"); }
142 /* config/menuconfig entry */
144 config_entry_start: T_CONFIG nonconst_symbol T_EOL
146 $2->flags |= SYMBOL_OPTIONAL;
148 printd(DEBUG_PARSE, "%s:%d:config %s\n", zconf_curname(), zconf_lineno(), $2->name);
151 config_stmt: config_entry_start config_option_list
153 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
156 menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL
158 $2->flags |= SYMBOL_OPTIONAL;
160 printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", zconf_curname(), zconf_lineno(), $2->name);
163 menuconfig_stmt: menuconfig_entry_start config_option_list
165 if (current_entry->prompt)
166 current_entry->prompt->type = P_MENU;
168 zconfprint("warning: menuconfig statement without prompt");
169 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
174 | config_option_list config_option
175 | config_option_list depends
176 | config_option_list help
179 config_option: type prompt_stmt_opt T_EOL
182 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
183 zconf_curname(), zconf_lineno(),
187 config_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
189 menu_add_prompt(P_PROMPT, $2, $3);
190 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
193 config_option: default expr if_expr T_EOL
195 menu_add_expr(P_DEFAULT, $2, $3);
198 printd(DEBUG_PARSE, "%s:%d:default(%u)\n",
199 zconf_curname(), zconf_lineno(),
203 config_option: T_SELECT nonconst_symbol if_expr T_EOL
205 menu_add_symbol(P_SELECT, $2, $3);
206 printd(DEBUG_PARSE, "%s:%d:select\n", zconf_curname(), zconf_lineno());
209 config_option: T_IMPLY nonconst_symbol if_expr T_EOL
211 menu_add_symbol(P_IMPLY, $2, $3);
212 printd(DEBUG_PARSE, "%s:%d:imply\n", zconf_curname(), zconf_lineno());
215 config_option: T_RANGE symbol symbol if_expr T_EOL
217 menu_add_expr(P_RANGE, expr_alloc_comp(E_RANGE,$2, $3), $4);
218 printd(DEBUG_PARSE, "%s:%d:range\n", zconf_curname(), zconf_lineno());
221 config_option: T_OPTION T_MODULES T_EOL
223 menu_add_option_modules();
226 config_option: T_OPTION T_DEFCONFIG_LIST T_EOL
228 menu_add_option_defconfig_list();
231 config_option: T_OPTION T_ALLNOCONFIG_Y T_EOL
233 menu_add_option_allnoconfig_y();
238 choice: T_CHOICE word_opt T_EOL
240 struct symbol *sym = sym_lookup($2, SYMBOL_CHOICE);
241 sym->flags |= SYMBOL_NO_WRITE;
243 menu_add_expr(P_CHOICE, NULL, NULL);
245 printd(DEBUG_PARSE, "%s:%d:choice\n", zconf_curname(), zconf_lineno());
248 choice_entry: choice choice_option_list
250 $$ = menu_add_menu();
255 if (zconf_endtoken($1, "choice")) {
257 printd(DEBUG_PARSE, "%s:%d:endchoice\n", zconf_curname(), zconf_lineno());
261 choice_stmt: choice_entry stmt_list_in_choice choice_end
266 | choice_option_list choice_option
267 | choice_option_list depends
268 | choice_option_list help
271 choice_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
273 menu_add_prompt(P_PROMPT, $2, $3);
274 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
277 choice_option: logic_type prompt_stmt_opt T_EOL
280 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
281 zconf_curname(), zconf_lineno(), $1);
284 choice_option: T_OPTIONAL T_EOL
286 current_entry->sym->flags |= SYMBOL_OPTIONAL;
287 printd(DEBUG_PARSE, "%s:%d:optional\n", zconf_curname(), zconf_lineno());
290 choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL
292 menu_add_symbol(P_DEFAULT, $2, $3);
293 printd(DEBUG_PARSE, "%s:%d:default\n",
294 zconf_curname(), zconf_lineno());
299 | T_INT { $$ = S_INT; }
300 | T_HEX { $$ = S_HEX; }
301 | T_STRING { $$ = S_STRING; }
304 T_BOOL { $$ = S_BOOLEAN; }
305 | T_TRISTATE { $$ = S_TRISTATE; }
308 T_DEFAULT { $$ = S_UNKNOWN; }
309 | T_DEF_BOOL { $$ = S_BOOLEAN; }
310 | T_DEF_TRISTATE { $$ = S_TRISTATE; }
314 if_entry: T_IF expr T_EOL
316 printd(DEBUG_PARSE, "%s:%d:if\n", zconf_curname(), zconf_lineno());
317 menu_add_entry(NULL);
319 $$ = menu_add_menu();
324 if (zconf_endtoken($1, "if")) {
326 printd(DEBUG_PARSE, "%s:%d:endif\n", zconf_curname(), zconf_lineno());
330 if_stmt: if_entry stmt_list if_end
333 if_stmt_in_choice: if_entry stmt_list_in_choice if_end
338 menu: T_MENU T_WORD_QUOTE T_EOL
340 menu_add_entry(NULL);
341 menu_add_prompt(P_MENU, $2, NULL);
342 printd(DEBUG_PARSE, "%s:%d:menu\n", zconf_curname(), zconf_lineno());
345 menu_entry: menu menu_option_list
347 $$ = menu_add_menu();
352 if (zconf_endtoken($1, "menu")) {
354 printd(DEBUG_PARSE, "%s:%d:endmenu\n", zconf_curname(), zconf_lineno());
358 menu_stmt: menu_entry stmt_list menu_end
363 | menu_option_list visible
364 | menu_option_list depends
367 source_stmt: T_SOURCE T_WORD_QUOTE T_EOL
369 printd(DEBUG_PARSE, "%s:%d:source %s\n", zconf_curname(), zconf_lineno(), $2);
376 comment: T_COMMENT T_WORD_QUOTE T_EOL
378 menu_add_entry(NULL);
379 menu_add_prompt(P_COMMENT, $2, NULL);
380 printd(DEBUG_PARSE, "%s:%d:comment\n", zconf_curname(), zconf_lineno());
383 comment_stmt: comment comment_option_list
388 | comment_option_list depends
393 help_start: T_HELP T_EOL
395 printd(DEBUG_PARSE, "%s:%d:help\n", zconf_curname(), zconf_lineno());
399 help: help_start T_HELPTEXT
401 if (current_entry->help) {
402 free(current_entry->help);
403 zconfprint("warning: '%s' defined with more than one help text -- only the last one will be used",
404 current_entry->sym->name ?: "<choice>");
407 /* Is the help text empty or all whitespace? */
408 if ($2[strspn($2, " \f\n\r\t\v")] == '\0')
409 zconfprint("warning: '%s' defined with blank help text",
410 current_entry->sym->name ?: "<choice>");
412 current_entry->help = $2;
417 depends: T_DEPENDS T_ON expr T_EOL
420 printd(DEBUG_PARSE, "%s:%d:depends on\n", zconf_curname(), zconf_lineno());
423 /* visibility option */
424 visible: T_VISIBLE if_expr T_EOL
426 menu_add_visibility($2);
429 /* prompt statement */
433 | T_WORD_QUOTE if_expr
435 menu_add_prompt(P_PROMPT, $1, $2);
438 end: T_ENDMENU T_EOL { $$ = "menu"; }
439 | T_ENDCHOICE T_EOL { $$ = "choice"; }
440 | T_ENDIF T_EOL { $$ = "if"; }
443 if_expr: /* empty */ { $$ = NULL; }
444 | T_IF expr { $$ = $2; }
447 expr: symbol { $$ = expr_alloc_symbol($1); }
448 | symbol T_LESS symbol { $$ = expr_alloc_comp(E_LTH, $1, $3); }
449 | symbol T_LESS_EQUAL symbol { $$ = expr_alloc_comp(E_LEQ, $1, $3); }
450 | symbol T_GREATER symbol { $$ = expr_alloc_comp(E_GTH, $1, $3); }
451 | symbol T_GREATER_EQUAL symbol { $$ = expr_alloc_comp(E_GEQ, $1, $3); }
452 | symbol T_EQUAL symbol { $$ = expr_alloc_comp(E_EQUAL, $1, $3); }
453 | symbol T_UNEQUAL symbol { $$ = expr_alloc_comp(E_UNEQUAL, $1, $3); }
454 | T_OPEN_PAREN expr T_CLOSE_PAREN { $$ = $2; }
455 | T_NOT expr { $$ = expr_alloc_one(E_NOT, $2); }
456 | expr T_OR expr { $$ = expr_alloc_two(E_OR, $1, $3); }
457 | expr T_AND expr { $$ = expr_alloc_two(E_AND, $1, $3); }
460 /* For symbol definitions, selects, etc., where quotes are not accepted */
461 nonconst_symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); };
463 symbol: nonconst_symbol
464 | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); }
467 word_opt: /* empty */ { $$ = NULL; }
470 /* assignment statement */
472 assignment_stmt: T_WORD assign_op assign_val T_EOL { variable_add($1, $3, $2); free($1); free($3); }
475 T_EQUAL { $$ = VAR_RECURSIVE; }
476 | T_COLON_EQUAL { $$ = VAR_SIMPLE; }
477 | T_PLUS_EQUAL { $$ = VAR_APPEND; }
481 /* empty */ { $$ = xstrdup(""); };
487 void conf_parse(const char *name)
492 zconf_initscan(name);
496 if (getenv("ZCONF_DEBUG"))
500 /* Variables are expanded in the parse phase. We can free them here. */
506 modules_sym = sym_find( "n" );
508 if (!menu_has_prompt(&rootmenu)) {
509 current_entry = &rootmenu;
510 menu_add_prompt(P_MENU, "Main menu", NULL);
513 menu_finalize(&rootmenu);
514 for_all_symbols(i, sym) {
515 if (sym_check_deps(sym))
520 sym_set_change_count(1);
523 static bool zconf_endtoken(const char *tokenname,
524 const char *expected_tokenname)
526 if (strcmp(tokenname, expected_tokenname)) {
527 zconf_error("unexpected '%s' within %s block",
528 tokenname, expected_tokenname);
532 if (current_menu->file != current_file) {
533 zconf_error("'%s' in different file than '%s'",
534 tokenname, expected_tokenname);
535 fprintf(stderr, "%s:%d: location of the '%s'\n",
536 current_menu->file->name, current_menu->lineno,
544 static void zconfprint(const char *err, ...)
548 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
550 vfprintf(stderr, err, ap);
552 fprintf(stderr, "\n");
555 static void zconf_error(const char *err, ...)
560 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
562 vfprintf(stderr, err, ap);
564 fprintf(stderr, "\n");
567 static void yyerror(const char *err)
569 fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err);
572 static void print_quoted_string(FILE *out, const char *str)
578 while ((p = strchr(str, '"'))) {
581 fprintf(out, "%.*s", len, str);
589 static void print_symbol(FILE *out, struct menu *menu)
591 struct symbol *sym = menu->sym;
592 struct property *prop;
594 if (sym_is_choice(sym))
595 fprintf(out, "\nchoice\n");
597 fprintf(out, "\nconfig %s\n", sym->name);
600 fputs(" bool\n", out);
603 fputs(" tristate\n", out);
606 fputs(" string\n", out);
609 fputs(" integer\n", out);
612 fputs(" hex\n", out);
615 fputs(" ???\n", out);
618 for (prop = sym->prop; prop; prop = prop->next) {
619 if (prop->menu != menu)
621 switch (prop->type) {
623 fputs(" prompt ", out);
624 print_quoted_string(out, prop->text);
625 if (!expr_is_yes(prop->visible.expr)) {
627 expr_fprint(prop->visible.expr, out);
632 fputs( " default ", out);
633 expr_fprint(prop->expr, out);
634 if (!expr_is_yes(prop->visible.expr)) {
636 expr_fprint(prop->visible.expr, out);
641 fputs(" #choice value\n", out);
644 fputs( " select ", out);
645 expr_fprint(prop->expr, out);
649 fputs( " imply ", out);
650 expr_fprint(prop->expr, out);
654 fputs( " range ", out);
655 expr_fprint(prop->expr, out);
659 fputs( " menu ", out);
660 print_quoted_string(out, prop->text);
664 fputs( " symbol ", out);
665 fprintf(out, "%s\n", prop->menu->sym->name);
668 fprintf(out, " unknown prop %d!\n", prop->type);
673 int len = strlen(menu->help);
674 while (menu->help[--len] == '\n')
676 fprintf(out, " help\n%s\n", menu->help);
680 void zconfdump(FILE *out)
682 struct property *prop;
686 menu = rootmenu.list;
688 if ((sym = menu->sym))
689 print_symbol(out, menu);
690 else if ((prop = menu->prompt)) {
691 switch (prop->type) {
693 fputs("\ncomment ", out);
694 print_quoted_string(out, prop->text);
698 fputs("\nmenu ", out);
699 print_quoted_string(out, prop->text);
705 if (!expr_is_yes(prop->visible.expr)) {
706 fputs(" depends ", out);
707 expr_fprint(prop->visible.expr, out);
716 else while ((menu = menu->parent)) {
717 if (menu->prompt && menu->prompt->type == P_MENU)
718 fputs("\nendmenu\n", out);