3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 * Released under the terms of the GNU GPL v2.0.
16 #define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt)
19 #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 struct kconf_id *id, int starttoken, int endtoken);
29 struct symbol *symbol_hash[SYMBOL_HASHSIZE];
31 static struct menu *current_menu, *current_entry;
40 struct symbol *symbol;
43 const struct kconf_id *id;
44 enum variable_flavor flavor;
52 %token <id>T_ENDCHOICE
55 %token <id>T_MENUCONFIG
57 %token <string> T_HELPTEXT
71 %token <string> T_WORD
72 %token <string> T_WORD_QUOTE
77 %token T_GREATER_EQUAL
81 %token <string> T_VARIABLE
82 %token <flavor> T_ASSIGN
83 %token <string> T_ASSIGN_VAL
87 %left T_EQUAL T_UNEQUAL
88 %left T_LESS T_LESS_EQUAL T_GREATER T_GREATER_EQUAL
92 %type <symbol> nonconst_symbol
97 %type <id> option_name
98 %type <menu> if_entry menu_entry choice_entry
99 %type <string> symbol_option_arg word_opt assign_val
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 /* Include kconf_id.c here so it can see the token constants. */
110 #include "kconf_id.c"
114 input: nl start | start;
116 start: mainmenu_stmt stmt_list | stmt_list;
120 mainmenu_stmt: T_MAINMENU prompt T_EOL
122 menu_add_prompt(P_MENU, $2, NULL);
127 | stmt_list common_stmt
128 | stmt_list choice_stmt
129 | stmt_list menu_stmt
130 | 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
134 zconf_error("unexpected option \"%s\"", $2->name);
136 | stmt_list error T_EOL { zconf_error("invalid statement"); }
140 T_DEPENDS | T_PROMPT | T_TYPE | T_SELECT | T_IMPLY | T_OPTIONAL | T_RANGE | T_DEFAULT | T_VISIBLE
154 T_WORD error T_EOL { zconf_error("unknown option \"%s\"", $1); }
155 | error T_EOL { zconf_error("invalid option"); }
159 /* config/menuconfig entry */
161 config_entry_start: T_CONFIG nonconst_symbol T_EOL
163 $2->flags |= SYMBOL_OPTIONAL;
165 printd(DEBUG_PARSE, "%s:%d:config %s\n", zconf_curname(), zconf_lineno(), $2->name);
168 config_stmt: config_entry_start config_option_list
170 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
173 menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL
175 $2->flags |= SYMBOL_OPTIONAL;
177 printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", zconf_curname(), zconf_lineno(), $2->name);
180 menuconfig_stmt: menuconfig_entry_start config_option_list
182 if (current_entry->prompt)
183 current_entry->prompt->type = P_MENU;
185 zconfprint("warning: menuconfig statement without prompt");
186 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
191 | config_option_list config_option
192 | config_option_list symbol_option
193 | config_option_list depends
194 | config_option_list help
195 | config_option_list option_error
196 | config_option_list T_EOL
199 config_option: T_TYPE prompt_stmt_opt T_EOL
201 menu_set_type($1->stype);
202 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
203 zconf_curname(), zconf_lineno(),
207 config_option: T_PROMPT prompt if_expr T_EOL
209 menu_add_prompt(P_PROMPT, $2, $3);
210 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
213 config_option: T_DEFAULT expr if_expr T_EOL
215 menu_add_expr(P_DEFAULT, $2, $3);
216 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(),
223 config_option: T_SELECT nonconst_symbol if_expr T_EOL
225 menu_add_symbol(P_SELECT, $2, $3);
226 printd(DEBUG_PARSE, "%s:%d:select\n", zconf_curname(), zconf_lineno());
229 config_option: T_IMPLY nonconst_symbol if_expr T_EOL
231 menu_add_symbol(P_IMPLY, $2, $3);
232 printd(DEBUG_PARSE, "%s:%d:imply\n", zconf_curname(), zconf_lineno());
235 config_option: T_RANGE symbol symbol if_expr T_EOL
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());
241 symbol_option: T_OPTION symbol_option_list T_EOL
246 | symbol_option_list T_WORD symbol_option_arg
248 const struct kconf_id *id = kconf_id_lookup($2, strlen($2));
249 if (id && id->flags & TF_OPTION) {
250 menu_add_option(id->token, $3);
254 zconfprint("warning: ignoring unknown option %s", $2);
259 /* empty */ { $$ = NULL; }
260 | T_EQUAL prompt { $$ = $2; }
265 choice: T_CHOICE word_opt T_EOL
267 struct symbol *sym = sym_lookup($2, SYMBOL_CHOICE);
268 sym->flags |= SYMBOL_NO_WRITE;
270 menu_add_expr(P_CHOICE, NULL, NULL);
272 printd(DEBUG_PARSE, "%s:%d:choice\n", zconf_curname(), zconf_lineno());
275 choice_entry: choice choice_option_list
277 $$ = menu_add_menu();
282 if (zconf_endtoken($1, T_CHOICE, T_ENDCHOICE)) {
284 printd(DEBUG_PARSE, "%s:%d:endchoice\n", zconf_curname(), zconf_lineno());
288 choice_stmt: choice_entry choice_block choice_end
293 | choice_option_list choice_option
294 | choice_option_list depends
295 | choice_option_list help
296 | choice_option_list T_EOL
297 | choice_option_list option_error
300 choice_option: T_PROMPT prompt if_expr T_EOL
302 menu_add_prompt(P_PROMPT, $2, $3);
303 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
306 choice_option: T_TYPE prompt_stmt_opt T_EOL
308 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(),
317 choice_option: T_OPTIONAL T_EOL
319 current_entry->sym->flags |= SYMBOL_OPTIONAL;
320 printd(DEBUG_PARSE, "%s:%d:optional\n", zconf_curname(), zconf_lineno());
323 choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL
325 if ($1->stype == S_UNKNOWN) {
326 menu_add_symbol(P_DEFAULT, $2, $3);
327 printd(DEBUG_PARSE, "%s:%d:default\n",
328 zconf_curname(), zconf_lineno());
335 | choice_block common_stmt
340 if_entry: T_IF expr T_EOL
342 printd(DEBUG_PARSE, "%s:%d:if\n", zconf_curname(), zconf_lineno());
343 menu_add_entry(NULL);
345 $$ = menu_add_menu();
350 if (zconf_endtoken($1, T_IF, T_ENDIF)) {
352 printd(DEBUG_PARSE, "%s:%d:endif\n", zconf_curname(), zconf_lineno());
356 if_stmt: if_entry if_block if_end
361 | if_block common_stmt
363 | if_block choice_stmt
368 menu: T_MENU prompt T_EOL
370 menu_add_entry(NULL);
371 menu_add_prompt(P_MENU, $2, NULL);
372 printd(DEBUG_PARSE, "%s:%d:menu\n", zconf_curname(), zconf_lineno());
375 menu_entry: menu visibility_list depends_list
377 $$ = menu_add_menu();
382 if (zconf_endtoken($1, T_MENU, T_ENDMENU)) {
384 printd(DEBUG_PARSE, "%s:%d:endmenu\n", zconf_curname(), zconf_lineno());
388 menu_stmt: menu_entry menu_block menu_end
393 | menu_block common_stmt
394 | menu_block menu_stmt
395 | menu_block choice_stmt
398 source_stmt: T_SOURCE prompt T_EOL
400 printd(DEBUG_PARSE, "%s:%d:source %s\n", zconf_curname(), zconf_lineno(), $2);
407 comment: T_COMMENT prompt T_EOL
409 menu_add_entry(NULL);
410 menu_add_prompt(P_COMMENT, $2, NULL);
411 printd(DEBUG_PARSE, "%s:%d:comment\n", zconf_curname(), zconf_lineno());
414 comment_stmt: comment depends_list
419 help_start: T_HELP T_EOL
421 printd(DEBUG_PARSE, "%s:%d:help\n", zconf_curname(), zconf_lineno());
425 help: help_start T_HELPTEXT
427 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>");
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>");
438 current_entry->help = $2;
445 | depends_list depends
447 | depends_list option_error
450 depends: T_DEPENDS T_ON expr T_EOL
453 printd(DEBUG_PARSE, "%s:%d:depends on\n", zconf_curname(), zconf_lineno());
456 /* visibility option */
460 | visibility_list visible
461 | visibility_list T_EOL
464 visible: T_VISIBLE if_expr
466 menu_add_visibility($2);
469 /* prompt statement */
475 menu_add_prompt(P_PROMPT, $1, $2);
482 end: T_ENDMENU T_EOL { $$ = $1; }
483 | T_ENDCHOICE T_EOL { $$ = $1; }
484 | T_ENDIF T_EOL { $$ = $1; }
492 if_expr: /* empty */ { $$ = NULL; }
493 | T_IF expr { $$ = $2; }
496 expr: symbol { $$ = expr_alloc_symbol($1); }
497 | 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); }
501 | 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); }
509 /* For symbol definitions, selects, etc., where quotes are not accepted */
510 nonconst_symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); };
512 symbol: nonconst_symbol
513 | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); }
516 word_opt: /* empty */ { $$ = NULL; }
519 /* assignment statement */
521 assignment_stmt: T_VARIABLE T_ASSIGN assign_val T_EOL { variable_add($1, $3, $2); free($1); free($3); }
524 /* empty */ { $$ = xstrdup(""); };
530 void conf_parse(const char *name)
535 zconf_initscan(name);
539 if (getenv("ZCONF_DEBUG"))
543 /* Variables are expanded in the parse phase. We can free them here. */
549 modules_sym = sym_find( "n" );
551 if (!menu_has_prompt(&rootmenu)) {
552 current_entry = &rootmenu;
553 menu_add_prompt(P_MENU, "Main menu", NULL);
556 menu_finalize(&rootmenu);
557 for_all_symbols(i, sym) {
558 if (sym_check_deps(sym))
563 sym_set_change_count(1);
566 static const char *zconf_tokenname(int 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";
575 case T_DEPENDS: return "depends";
576 case T_VISIBLE: return "visible";
581 static bool zconf_endtoken(const struct kconf_id *id, int starttoken, int endtoken)
583 if (id->token != endtoken) {
584 zconf_error("unexpected '%s' within %s block",
585 id->name, zconf_tokenname(starttoken));
589 if (current_menu->file != current_file) {
590 zconf_error("'%s' in different file than '%s'",
591 id->name, zconf_tokenname(starttoken));
592 fprintf(stderr, "%s:%d: location of the '%s'\n",
593 current_menu->file->name, current_menu->lineno,
594 zconf_tokenname(starttoken));
601 static void zconfprint(const char *err, ...)
605 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
607 vfprintf(stderr, err, ap);
609 fprintf(stderr, "\n");
612 static void zconf_error(const char *err, ...)
617 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
619 vfprintf(stderr, err, ap);
621 fprintf(stderr, "\n");
624 static void yyerror(const char *err)
626 fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err);
629 static void print_quoted_string(FILE *out, const char *str)
635 while ((p = strchr(str, '"'))) {
638 fprintf(out, "%.*s", len, str);
646 static void print_symbol(FILE *out, struct menu *menu)
648 struct symbol *sym = menu->sym;
649 struct property *prop;
651 if (sym_is_choice(sym))
652 fprintf(out, "\nchoice\n");
654 fprintf(out, "\nconfig %s\n", sym->name);
657 fputs(" bool\n", out);
660 fputs(" tristate\n", out);
663 fputs(" string\n", out);
666 fputs(" integer\n", out);
669 fputs(" hex\n", out);
672 fputs(" ???\n", out);
675 for (prop = sym->prop; prop; prop = prop->next) {
676 if (prop->menu != menu)
678 switch (prop->type) {
680 fputs(" prompt ", out);
681 print_quoted_string(out, prop->text);
682 if (!expr_is_yes(prop->visible.expr)) {
684 expr_fprint(prop->visible.expr, out);
689 fputs( " default ", out);
690 expr_fprint(prop->expr, out);
691 if (!expr_is_yes(prop->visible.expr)) {
693 expr_fprint(prop->visible.expr, out);
698 fputs(" #choice value\n", out);
701 fputs( " select ", out);
702 expr_fprint(prop->expr, out);
706 fputs( " imply ", out);
707 expr_fprint(prop->expr, out);
711 fputs( " range ", out);
712 expr_fprint(prop->expr, out);
716 fputs( " menu ", out);
717 print_quoted_string(out, prop->text);
721 fputs( " symbol ", out);
722 fprintf(out, "%s\n", prop->sym->name);
725 fprintf(out, " unknown prop %d!\n", prop->type);
730 int len = strlen(menu->help);
731 while (menu->help[--len] == '\n')
733 fprintf(out, " help\n%s\n", menu->help);
737 void zconfdump(FILE *out)
739 struct property *prop;
743 menu = rootmenu.list;
745 if ((sym = menu->sym))
746 print_symbol(out, menu);
747 else if ((prop = menu->prompt)) {
748 switch (prop->type) {
750 fputs("\ncomment ", out);
751 print_quoted_string(out, prop->text);
755 fputs("\nmenu ", out);
756 print_quoted_string(out, prop->text);
762 if (!expr_is_yes(prop->visible.expr)) {
763 fputs(" depends ", out);
764 expr_fprint(prop->visible.expr, out);
773 else while ((menu = menu->parent)) {
774 if (menu->prompt && menu->prompt->type == P_MENU)
775 fputs("\nendmenu\n", out);
784 #include "zconf.lex.c"
786 #include "confdata.c"
790 #include "preprocess.c"