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
94 %type
<symbol
> nonconst_symbol
96 %type
<type
> type logic_type default
100 %type
<menu
> if_entry menu_entry choice_entry
101 %type
<string> word_opt assign_val
102 %type
<flavor
> assign_op
105 fprintf
(stderr
, "%s:%d: missing end statement for this entry\n",
106 $$
->file
->name
, $$
->lineno
);
107 if
(current_menu
== $$
)
109 } if_entry menu_entry choice_entry
112 input: mainmenu_stmt stmt_list | stmt_list
;
116 mainmenu_stmt: T_MAINMENU prompt T_EOL
118 menu_add_prompt
(P_MENU
, $2, NULL
);
123 | stmt_list common_stmt
124 | stmt_list choice_stmt
125 | stmt_list menu_stmt
126 | stmt_list T_WORD
error T_EOL
{ zconf_error
("unknown statement \"%s\"", $2); }
127 | stmt_list
error T_EOL
{ zconf_error
("invalid statement"); }
139 /* config/menuconfig entry */
141 config_entry_start: T_CONFIG nonconst_symbol T_EOL
143 $2->flags |
= SYMBOL_OPTIONAL
;
145 printd
(DEBUG_PARSE
, "%s:%d:config %s\n", zconf_curname
(), zconf_lineno
(), $2->name
);
148 config_stmt: config_entry_start config_option_list
150 printd
(DEBUG_PARSE
, "%s:%d:endconfig\n", zconf_curname
(), zconf_lineno
());
153 menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL
155 $2->flags |
= SYMBOL_OPTIONAL
;
157 printd
(DEBUG_PARSE
, "%s:%d:menuconfig %s\n", zconf_curname
(), zconf_lineno
(), $2->name
);
160 menuconfig_stmt: menuconfig_entry_start config_option_list
162 if
(current_entry
->prompt
)
163 current_entry
->prompt
->type
= P_MENU
;
165 zconfprint
("warning: menuconfig statement without prompt");
166 printd
(DEBUG_PARSE
, "%s:%d:endconfig\n", zconf_curname
(), zconf_lineno
());
171 | config_option_list config_option
172 | config_option_list depends
173 | config_option_list help
176 config_option: type prompt_stmt_opt T_EOL
179 printd
(DEBUG_PARSE
, "%s:%d:type(%u)\n",
180 zconf_curname
(), zconf_lineno
(),
184 config_option: T_PROMPT prompt if_expr T_EOL
186 menu_add_prompt
(P_PROMPT
, $2, $3);
187 printd
(DEBUG_PARSE
, "%s:%d:prompt\n", zconf_curname
(), zconf_lineno
());
190 config_option: default expr if_expr T_EOL
192 menu_add_expr
(P_DEFAULT
, $2, $3);
195 printd
(DEBUG_PARSE
, "%s:%d:default(%u)\n",
196 zconf_curname
(), zconf_lineno
(),
200 config_option: T_SELECT nonconst_symbol if_expr T_EOL
202 menu_add_symbol
(P_SELECT
, $2, $3);
203 printd
(DEBUG_PARSE
, "%s:%d:select\n", zconf_curname
(), zconf_lineno
());
206 config_option: T_IMPLY nonconst_symbol if_expr T_EOL
208 menu_add_symbol
(P_IMPLY
, $2, $3);
209 printd
(DEBUG_PARSE
, "%s:%d:imply\n", zconf_curname
(), zconf_lineno
());
212 config_option: T_RANGE symbol symbol if_expr T_EOL
214 menu_add_expr
(P_RANGE
, expr_alloc_comp
(E_RANGE
,$2, $3), $4);
215 printd
(DEBUG_PARSE
, "%s:%d:range\n", zconf_curname
(), zconf_lineno
());
218 config_option: T_OPTION T_MODULES T_EOL
220 menu_add_option_modules
();
223 config_option: T_OPTION T_DEFCONFIG_LIST T_EOL
225 menu_add_option_defconfig_list
();
228 config_option: T_OPTION T_ALLNOCONFIG_Y T_EOL
230 menu_add_option_allnoconfig_y
();
235 choice: T_CHOICE word_opt T_EOL
237 struct symbol
*sym
= sym_lookup
($2, SYMBOL_CHOICE
);
238 sym
->flags |
= SYMBOL_NO_WRITE
;
240 menu_add_expr
(P_CHOICE
, NULL
, NULL
);
242 printd
(DEBUG_PARSE
, "%s:%d:choice\n", zconf_curname
(), zconf_lineno
());
245 choice_entry: choice choice_option_list
247 $$
= menu_add_menu
();
252 if
(zconf_endtoken
($1, "choice")) {
254 printd
(DEBUG_PARSE
, "%s:%d:endchoice\n", zconf_curname
(), zconf_lineno
());
258 choice_stmt: choice_entry choice_block choice_end
263 | choice_option_list choice_option
264 | choice_option_list depends
265 | choice_option_list help
268 choice_option: T_PROMPT prompt if_expr T_EOL
270 menu_add_prompt
(P_PROMPT
, $2, $3);
271 printd
(DEBUG_PARSE
, "%s:%d:prompt\n", zconf_curname
(), zconf_lineno
());
274 choice_option: logic_type prompt_stmt_opt T_EOL
277 printd
(DEBUG_PARSE
, "%s:%d:type(%u)\n",
278 zconf_curname
(), zconf_lineno
(), $1);
281 choice_option: T_OPTIONAL T_EOL
283 current_entry
->sym
->flags |
= SYMBOL_OPTIONAL
;
284 printd
(DEBUG_PARSE
, "%s:%d:optional\n", zconf_curname
(), zconf_lineno
());
287 choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL
289 menu_add_symbol
(P_DEFAULT
, $2, $3);
290 printd
(DEBUG_PARSE
, "%s:%d:default\n",
291 zconf_curname
(), zconf_lineno
());
296 | T_INT
{ $$
= S_INT
; }
297 | T_HEX
{ $$
= S_HEX
; }
298 | T_STRING
{ $$
= S_STRING
; }
301 T_BOOL
{ $$
= S_BOOLEAN
; }
302 | T_TRISTATE
{ $$
= S_TRISTATE
; }
305 T_DEFAULT
{ $$
= S_UNKNOWN
; }
306 | T_DEF_BOOL
{ $$
= S_BOOLEAN
; }
307 | T_DEF_TRISTATE
{ $$
= S_TRISTATE
; }
311 | choice_block common_stmt
316 if_entry: T_IF expr T_EOL
318 printd
(DEBUG_PARSE
, "%s:%d:if\n", zconf_curname
(), zconf_lineno
());
319 menu_add_entry
(NULL
);
321 $$
= menu_add_menu
();
326 if
(zconf_endtoken
($1, "if")) {
328 printd
(DEBUG_PARSE
, "%s:%d:endif\n", zconf_curname
(), zconf_lineno
());
332 if_stmt: if_entry stmt_list if_end
337 menu: T_MENU prompt T_EOL
339 menu_add_entry
(NULL
);
340 menu_add_prompt
(P_MENU
, $2, NULL
);
341 printd
(DEBUG_PARSE
, "%s:%d:menu\n", zconf_curname
(), zconf_lineno
());
344 menu_entry: menu menu_option_list
346 $$
= menu_add_menu
();
351 if
(zconf_endtoken
($1, "menu")) {
353 printd
(DEBUG_PARSE
, "%s:%d:endmenu\n", zconf_curname
(), zconf_lineno
());
357 menu_stmt: menu_entry stmt_list menu_end
362 | menu_option_list visible
363 | menu_option_list depends
366 source_stmt: T_SOURCE prompt T_EOL
368 printd
(DEBUG_PARSE
, "%s:%d:source %s\n", zconf_curname
(), zconf_lineno
(), $2);
375 comment: T_COMMENT prompt T_EOL
377 menu_add_entry
(NULL
);
378 menu_add_prompt
(P_COMMENT
, $2, NULL
);
379 printd
(DEBUG_PARSE
, "%s:%d:comment\n", zconf_curname
(), zconf_lineno
());
382 comment_stmt: comment comment_option_list
387 | comment_option_list depends
392 help_start: T_HELP T_EOL
394 printd
(DEBUG_PARSE
, "%s:%d:help\n", zconf_curname
(), zconf_lineno
());
398 help: help_start T_HELPTEXT
400 if
(current_entry
->help
) {
401 free
(current_entry
->help
);
402 zconfprint
("warning: '%s' defined with more than one help text -- only the last one will be used",
403 current_entry
->sym
->name ?
: "<choice>");
406 /* Is the help text empty or all whitespace? */
407 if
($2[strspn
($2, " \f\n\r\t\v")] == '\0')
408 zconfprint
("warning: '%s' defined with blank help text",
409 current_entry
->sym
->name ?
: "<choice>");
411 current_entry
->help
= $2;
416 depends: T_DEPENDS T_ON expr T_EOL
419 printd
(DEBUG_PARSE
, "%s:%d:depends on\n", zconf_curname
(), zconf_lineno
());
422 /* visibility option */
423 visible: T_VISIBLE if_expr T_EOL
425 menu_add_visibility
($2);
428 /* prompt statement */
434 menu_add_prompt
(P_PROMPT
, $1, $2);
441 end: T_ENDMENU T_EOL
{ $$
= "menu"; }
442 | T_ENDCHOICE T_EOL
{ $$
= "choice"; }
443 | T_ENDIF T_EOL
{ $$
= "if"; }
446 if_expr: /* empty */ { $$
= NULL
; }
447 | T_IF expr
{ $$
= $2; }
450 expr: symbol
{ $$
= expr_alloc_symbol
($1); }
451 | symbol T_LESS symbol
{ $$
= expr_alloc_comp
(E_LTH
, $1, $3); }
452 | symbol T_LESS_EQUAL symbol
{ $$
= expr_alloc_comp
(E_LEQ
, $1, $3); }
453 | symbol T_GREATER symbol
{ $$
= expr_alloc_comp
(E_GTH
, $1, $3); }
454 | symbol T_GREATER_EQUAL symbol
{ $$
= expr_alloc_comp
(E_GEQ
, $1, $3); }
455 | symbol T_EQUAL symbol
{ $$
= expr_alloc_comp
(E_EQUAL
, $1, $3); }
456 | symbol T_UNEQUAL symbol
{ $$
= expr_alloc_comp
(E_UNEQUAL
, $1, $3); }
457 | T_OPEN_PAREN expr T_CLOSE_PAREN
{ $$
= $2; }
458 | T_NOT expr
{ $$
= expr_alloc_one
(E_NOT
, $2); }
459 | expr T_OR expr
{ $$
= expr_alloc_two
(E_OR
, $1, $3); }
460 | expr T_AND expr
{ $$
= expr_alloc_two
(E_AND
, $1, $3); }
463 /* For symbol definitions, selects, etc., where quotes are not accepted */
464 nonconst_symbol: T_WORD
{ $$
= sym_lookup
($1, 0); free
($1); };
466 symbol: nonconst_symbol
467 | T_WORD_QUOTE
{ $$
= sym_lookup
($1, SYMBOL_CONST
); free
($1); }
470 word_opt: /* empty */ { $$
= NULL
; }
473 /* assignment statement */
475 assignment_stmt: T_WORD assign_op assign_val T_EOL
{ variable_add
($1, $3, $2); free
($1); free
($3); }
478 T_EQUAL
{ $$
= VAR_RECURSIVE
; }
479 | T_COLON_EQUAL
{ $$
= VAR_SIMPLE
; }
480 | T_PLUS_EQUAL
{ $$
= VAR_APPEND
; }
484 /* empty */ { $$
= xstrdup
(""); };
490 void conf_parse
(const char *name
)
495 zconf_initscan
(name
);
499 if
(getenv
("ZCONF_DEBUG"))
503 /* Variables are expanded in the parse phase. We can free them here. */
509 modules_sym
= sym_find
( "n" );
511 if
(!menu_has_prompt
(&rootmenu
)) {
512 current_entry
= &rootmenu
;
513 menu_add_prompt
(P_MENU
, "Main menu", NULL
);
516 menu_finalize
(&rootmenu
);
517 for_all_symbols
(i
, sym
) {
518 if
(sym_check_deps
(sym
))
523 sym_set_change_count
(1);
526 static bool zconf_endtoken
(const char *tokenname
,
527 const char *expected_tokenname
)
529 if
(strcmp
(tokenname
, expected_tokenname
)) {
530 zconf_error
("unexpected '%s' within %s block",
531 tokenname
, expected_tokenname
);
535 if
(current_menu
->file
!= current_file
) {
536 zconf_error
("'%s' in different file than '%s'",
537 tokenname
, expected_tokenname
);
538 fprintf
(stderr
, "%s:%d: location of the '%s'\n",
539 current_menu
->file
->name
, current_menu
->lineno
,
547 static void zconfprint
(const char *err
, ...
)
551 fprintf
(stderr
, "%s:%d: ", zconf_curname
(), zconf_lineno
());
553 vfprintf
(stderr
, err
, ap
);
555 fprintf
(stderr
, "\n");
558 static void zconf_error
(const char *err
, ...
)
563 fprintf
(stderr
, "%s:%d: ", zconf_curname
(), zconf_lineno
());
565 vfprintf
(stderr
, err
, ap
);
567 fprintf
(stderr
, "\n");
570 static void yyerror(const char *err
)
572 fprintf
(stderr
, "%s:%d: %s\n", zconf_curname
(), zconf_lineno
() + 1, err
);
575 static void print_quoted_string
(FILE *out
, const char *str
)
581 while
((p
= strchr
(str
, '"'))) {
584 fprintf
(out
, "%.*s", len
, str
);
592 static void print_symbol
(FILE *out
, struct menu
*menu
)
594 struct symbol
*sym
= menu
->sym
;
595 struct property
*prop
;
597 if
(sym_is_choice
(sym
))
598 fprintf
(out
, "\nchoice\n");
600 fprintf
(out
, "\nconfig %s\n", sym
->name
);
603 fputs
(" bool\n", out
);
606 fputs
(" tristate\n", out
);
609 fputs
(" string\n", out
);
612 fputs
(" integer\n", out
);
615 fputs
(" hex\n", out
);
618 fputs
(" ???\n", out
);
621 for
(prop
= sym
->prop
; prop
; prop
= prop
->next
) {
622 if
(prop
->menu
!= menu
)
624 switch
(prop
->type
) {
626 fputs
(" prompt ", out
);
627 print_quoted_string
(out
, prop
->text
);
628 if
(!expr_is_yes
(prop
->visible.expr
)) {
630 expr_fprint
(prop
->visible.expr
, out
);
635 fputs
( " default ", out
);
636 expr_fprint
(prop
->expr
, out
);
637 if
(!expr_is_yes
(prop
->visible.expr
)) {
639 expr_fprint
(prop
->visible.expr
, out
);
644 fputs
(" #choice value\n", out
);
647 fputs
( " select ", out
);
648 expr_fprint
(prop
->expr
, out
);
652 fputs
( " imply ", out
);
653 expr_fprint
(prop
->expr
, out
);
657 fputs
( " range ", out
);
658 expr_fprint
(prop
->expr
, out
);
662 fputs
( " menu ", out
);
663 print_quoted_string
(out
, prop
->text
);
667 fputs
( " symbol ", out
);
668 fprintf
(out
, "%s\n", prop
->sym
->name
);
671 fprintf
(out
, " unknown prop %d!\n", prop
->type
);
676 int len
= strlen
(menu
->help
);
677 while
(menu
->help
[--len
] == '\n')
679 fprintf
(out
, " help\n%s\n", menu
->help
);
683 void zconfdump
(FILE *out
)
685 struct property
*prop
;
689 menu
= rootmenu.list
;
691 if
((sym
= menu
->sym
))
692 print_symbol
(out
, menu
);
693 else if
((prop
= menu
->prompt
)) {
694 switch
(prop
->type
) {
696 fputs
("\ncomment ", out
);
697 print_quoted_string
(out
, prop
->text
);
701 fputs
("\nmenu ", out
);
702 print_quoted_string
(out
, prop
->text
);
708 if
(!expr_is_yes
(prop
->visible.expr
)) {
709 fputs
(" depends ", out
);
710 expr_fprint
(prop
->visible.expr
, out
);
719 else while
((menu
= menu
->parent
)) {
720 if
(menu
->prompt
&& menu
->prompt
->type
== P_MENU
)
721 fputs
("\nendmenu\n", out
);