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 common_stmt
123 | stmt_list choice_stmt
124 | stmt_list menu_stmt
125 | stmt_list T_WORD
error T_EOL
{ zconf_error
("unknown statement \"%s\"", $2); }
126 | stmt_list
error T_EOL
{ zconf_error
("invalid statement"); }
138 /* config/menuconfig entry */
140 config_entry_start: T_CONFIG nonconst_symbol T_EOL
142 $2->flags |
= SYMBOL_OPTIONAL
;
144 printd
(DEBUG_PARSE
, "%s:%d:config %s\n", zconf_curname
(), zconf_lineno
(), $2->name
);
147 config_stmt: config_entry_start config_option_list
149 printd
(DEBUG_PARSE
, "%s:%d:endconfig\n", zconf_curname
(), zconf_lineno
());
152 menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL
154 $2->flags |
= SYMBOL_OPTIONAL
;
156 printd
(DEBUG_PARSE
, "%s:%d:menuconfig %s\n", zconf_curname
(), zconf_lineno
(), $2->name
);
159 menuconfig_stmt: menuconfig_entry_start config_option_list
161 if
(current_entry
->prompt
)
162 current_entry
->prompt
->type
= P_MENU
;
164 zconfprint
("warning: menuconfig statement without prompt");
165 printd
(DEBUG_PARSE
, "%s:%d:endconfig\n", zconf_curname
(), zconf_lineno
());
170 | config_option_list config_option
171 | config_option_list depends
172 | config_option_list help
175 config_option: type prompt_stmt_opt T_EOL
178 printd
(DEBUG_PARSE
, "%s:%d:type(%u)\n",
179 zconf_curname
(), zconf_lineno
(),
183 config_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
185 menu_add_prompt
(P_PROMPT
, $2, $3);
186 printd
(DEBUG_PARSE
, "%s:%d:prompt\n", zconf_curname
(), zconf_lineno
());
189 config_option: default expr if_expr T_EOL
191 menu_add_expr
(P_DEFAULT
, $2, $3);
194 printd
(DEBUG_PARSE
, "%s:%d:default(%u)\n",
195 zconf_curname
(), zconf_lineno
(),
199 config_option: T_SELECT nonconst_symbol if_expr T_EOL
201 menu_add_symbol
(P_SELECT
, $2, $3);
202 printd
(DEBUG_PARSE
, "%s:%d:select\n", zconf_curname
(), zconf_lineno
());
205 config_option: T_IMPLY nonconst_symbol if_expr T_EOL
207 menu_add_symbol
(P_IMPLY
, $2, $3);
208 printd
(DEBUG_PARSE
, "%s:%d:imply\n", zconf_curname
(), zconf_lineno
());
211 config_option: T_RANGE symbol symbol if_expr T_EOL
213 menu_add_expr
(P_RANGE
, expr_alloc_comp
(E_RANGE
,$2, $3), $4);
214 printd
(DEBUG_PARSE
, "%s:%d:range\n", zconf_curname
(), zconf_lineno
());
217 config_option: T_OPTION T_MODULES T_EOL
219 menu_add_option_modules
();
222 config_option: T_OPTION T_DEFCONFIG_LIST T_EOL
224 menu_add_option_defconfig_list
();
227 config_option: T_OPTION T_ALLNOCONFIG_Y T_EOL
229 menu_add_option_allnoconfig_y
();
234 choice: T_CHOICE word_opt T_EOL
236 struct symbol
*sym
= sym_lookup
($2, SYMBOL_CHOICE
);
237 sym
->flags |
= SYMBOL_NO_WRITE
;
239 menu_add_expr
(P_CHOICE
, NULL
, NULL
);
241 printd
(DEBUG_PARSE
, "%s:%d:choice\n", zconf_curname
(), zconf_lineno
());
244 choice_entry: choice choice_option_list
246 $$
= menu_add_menu
();
251 if
(zconf_endtoken
($1, "choice")) {
253 printd
(DEBUG_PARSE
, "%s:%d:endchoice\n", zconf_curname
(), zconf_lineno
());
257 choice_stmt: choice_entry choice_block choice_end
262 | choice_option_list choice_option
263 | choice_option_list depends
264 | choice_option_list help
267 choice_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
269 menu_add_prompt
(P_PROMPT
, $2, $3);
270 printd
(DEBUG_PARSE
, "%s:%d:prompt\n", zconf_curname
(), zconf_lineno
());
273 choice_option: logic_type prompt_stmt_opt T_EOL
276 printd
(DEBUG_PARSE
, "%s:%d:type(%u)\n",
277 zconf_curname
(), zconf_lineno
(), $1);
280 choice_option: T_OPTIONAL T_EOL
282 current_entry
->sym
->flags |
= SYMBOL_OPTIONAL
;
283 printd
(DEBUG_PARSE
, "%s:%d:optional\n", zconf_curname
(), zconf_lineno
());
286 choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL
288 menu_add_symbol
(P_DEFAULT
, $2, $3);
289 printd
(DEBUG_PARSE
, "%s:%d:default\n",
290 zconf_curname
(), zconf_lineno
());
295 | T_INT
{ $$
= S_INT
; }
296 | T_HEX
{ $$
= S_HEX
; }
297 | T_STRING
{ $$
= S_STRING
; }
300 T_BOOL
{ $$
= S_BOOLEAN
; }
301 | T_TRISTATE
{ $$
= S_TRISTATE
; }
304 T_DEFAULT
{ $$
= S_UNKNOWN
; }
305 | T_DEF_BOOL
{ $$
= S_BOOLEAN
; }
306 | T_DEF_TRISTATE
{ $$
= S_TRISTATE
; }
310 | choice_block common_stmt
315 if_entry: T_IF expr T_EOL
317 printd
(DEBUG_PARSE
, "%s:%d:if\n", zconf_curname
(), zconf_lineno
());
318 menu_add_entry
(NULL
);
320 $$
= menu_add_menu
();
325 if
(zconf_endtoken
($1, "if")) {
327 printd
(DEBUG_PARSE
, "%s:%d:endif\n", zconf_curname
(), zconf_lineno
());
331 if_stmt: if_entry stmt_list if_end
336 menu: T_MENU T_WORD_QUOTE T_EOL
338 menu_add_entry
(NULL
);
339 menu_add_prompt
(P_MENU
, $2, NULL
);
340 printd
(DEBUG_PARSE
, "%s:%d:menu\n", zconf_curname
(), zconf_lineno
());
343 menu_entry: menu menu_option_list
345 $$
= menu_add_menu
();
350 if
(zconf_endtoken
($1, "menu")) {
352 printd
(DEBUG_PARSE
, "%s:%d:endmenu\n", zconf_curname
(), zconf_lineno
());
356 menu_stmt: menu_entry stmt_list menu_end
361 | menu_option_list visible
362 | menu_option_list depends
365 source_stmt: T_SOURCE T_WORD_QUOTE T_EOL
367 printd
(DEBUG_PARSE
, "%s:%d:source %s\n", zconf_curname
(), zconf_lineno
(), $2);
374 comment: T_COMMENT T_WORD_QUOTE T_EOL
376 menu_add_entry
(NULL
);
377 menu_add_prompt
(P_COMMENT
, $2, NULL
);
378 printd
(DEBUG_PARSE
, "%s:%d:comment\n", zconf_curname
(), zconf_lineno
());
381 comment_stmt: comment comment_option_list
386 | comment_option_list depends
391 help_start: T_HELP T_EOL
393 printd
(DEBUG_PARSE
, "%s:%d:help\n", zconf_curname
(), zconf_lineno
());
397 help: help_start T_HELPTEXT
399 if
(current_entry
->help
) {
400 free
(current_entry
->help
);
401 zconfprint
("warning: '%s' defined with more than one help text -- only the last one will be used",
402 current_entry
->sym
->name ?
: "<choice>");
405 /* Is the help text empty or all whitespace? */
406 if
($2[strspn
($2, " \f\n\r\t\v")] == '\0')
407 zconfprint
("warning: '%s' defined with blank help text",
408 current_entry
->sym
->name ?
: "<choice>");
410 current_entry
->help
= $2;
415 depends: T_DEPENDS T_ON expr T_EOL
418 printd
(DEBUG_PARSE
, "%s:%d:depends on\n", zconf_curname
(), zconf_lineno
());
421 /* visibility option */
422 visible: T_VISIBLE if_expr T_EOL
424 menu_add_visibility
($2);
427 /* prompt statement */
431 | T_WORD_QUOTE if_expr
433 menu_add_prompt
(P_PROMPT
, $1, $2);
436 end: T_ENDMENU T_EOL
{ $$
= "menu"; }
437 | T_ENDCHOICE T_EOL
{ $$
= "choice"; }
438 | T_ENDIF T_EOL
{ $$
= "if"; }
441 if_expr: /* empty */ { $$
= NULL
; }
442 | T_IF expr
{ $$
= $2; }
445 expr: symbol
{ $$
= expr_alloc_symbol
($1); }
446 | symbol T_LESS symbol
{ $$
= expr_alloc_comp
(E_LTH
, $1, $3); }
447 | symbol T_LESS_EQUAL symbol
{ $$
= expr_alloc_comp
(E_LEQ
, $1, $3); }
448 | symbol T_GREATER symbol
{ $$
= expr_alloc_comp
(E_GTH
, $1, $3); }
449 | symbol T_GREATER_EQUAL symbol
{ $$
= expr_alloc_comp
(E_GEQ
, $1, $3); }
450 | symbol T_EQUAL symbol
{ $$
= expr_alloc_comp
(E_EQUAL
, $1, $3); }
451 | symbol T_UNEQUAL symbol
{ $$
= expr_alloc_comp
(E_UNEQUAL
, $1, $3); }
452 | T_OPEN_PAREN expr T_CLOSE_PAREN
{ $$
= $2; }
453 | T_NOT expr
{ $$
= expr_alloc_one
(E_NOT
, $2); }
454 | expr T_OR expr
{ $$
= expr_alloc_two
(E_OR
, $1, $3); }
455 | expr T_AND expr
{ $$
= expr_alloc_two
(E_AND
, $1, $3); }
458 /* For symbol definitions, selects, etc., where quotes are not accepted */
459 nonconst_symbol: T_WORD
{ $$
= sym_lookup
($1, 0); free
($1); };
461 symbol: nonconst_symbol
462 | T_WORD_QUOTE
{ $$
= sym_lookup
($1, SYMBOL_CONST
); free
($1); }
465 word_opt: /* empty */ { $$
= NULL
; }
468 /* assignment statement */
470 assignment_stmt: T_WORD assign_op assign_val T_EOL
{ variable_add
($1, $3, $2); free
($1); free
($3); }
473 T_EQUAL
{ $$
= VAR_RECURSIVE
; }
474 | T_COLON_EQUAL
{ $$
= VAR_SIMPLE
; }
475 | T_PLUS_EQUAL
{ $$
= VAR_APPEND
; }
479 /* empty */ { $$
= xstrdup
(""); };
485 void conf_parse
(const char *name
)
490 zconf_initscan
(name
);
494 if
(getenv
("ZCONF_DEBUG"))
498 /* Variables are expanded in the parse phase. We can free them here. */
504 modules_sym
= sym_find
( "n" );
506 if
(!menu_has_prompt
(&rootmenu
)) {
507 current_entry
= &rootmenu
;
508 menu_add_prompt
(P_MENU
, "Main menu", NULL
);
511 menu_finalize
(&rootmenu
);
512 for_all_symbols
(i
, sym
) {
513 if
(sym_check_deps
(sym
))
518 sym_set_change_count
(1);
521 static bool zconf_endtoken
(const char *tokenname
,
522 const char *expected_tokenname
)
524 if
(strcmp
(tokenname
, expected_tokenname
)) {
525 zconf_error
("unexpected '%s' within %s block",
526 tokenname
, expected_tokenname
);
530 if
(current_menu
->file
!= current_file
) {
531 zconf_error
("'%s' in different file than '%s'",
532 tokenname
, expected_tokenname
);
533 fprintf
(stderr
, "%s:%d: location of the '%s'\n",
534 current_menu
->file
->name
, current_menu
->lineno
,
542 static void zconfprint
(const char *err
, ...
)
546 fprintf
(stderr
, "%s:%d: ", zconf_curname
(), zconf_lineno
());
548 vfprintf
(stderr
, err
, ap
);
550 fprintf
(stderr
, "\n");
553 static void zconf_error
(const char *err
, ...
)
558 fprintf
(stderr
, "%s:%d: ", zconf_curname
(), zconf_lineno
());
560 vfprintf
(stderr
, err
, ap
);
562 fprintf
(stderr
, "\n");
565 static void yyerror(const char *err
)
567 fprintf
(stderr
, "%s:%d: %s\n", zconf_curname
(), zconf_lineno
() + 1, err
);
570 static void print_quoted_string
(FILE *out
, const char *str
)
576 while
((p
= strchr
(str
, '"'))) {
579 fprintf
(out
, "%.*s", len
, str
);
587 static void print_symbol
(FILE *out
, struct menu
*menu
)
589 struct symbol
*sym
= menu
->sym
;
590 struct property
*prop
;
592 if
(sym_is_choice
(sym
))
593 fprintf
(out
, "\nchoice\n");
595 fprintf
(out
, "\nconfig %s\n", sym
->name
);
598 fputs
(" bool\n", out
);
601 fputs
(" tristate\n", out
);
604 fputs
(" string\n", out
);
607 fputs
(" integer\n", out
);
610 fputs
(" hex\n", out
);
613 fputs
(" ???\n", out
);
616 for
(prop
= sym
->prop
; prop
; prop
= prop
->next
) {
617 if
(prop
->menu
!= menu
)
619 switch
(prop
->type
) {
621 fputs
(" prompt ", out
);
622 print_quoted_string
(out
, prop
->text
);
623 if
(!expr_is_yes
(prop
->visible.expr
)) {
625 expr_fprint
(prop
->visible.expr
, out
);
630 fputs
( " default ", out
);
631 expr_fprint
(prop
->expr
, out
);
632 if
(!expr_is_yes
(prop
->visible.expr
)) {
634 expr_fprint
(prop
->visible.expr
, out
);
639 fputs
(" #choice value\n", out
);
642 fputs
( " select ", out
);
643 expr_fprint
(prop
->expr
, out
);
647 fputs
( " imply ", out
);
648 expr_fprint
(prop
->expr
, out
);
652 fputs
( " range ", out
);
653 expr_fprint
(prop
->expr
, out
);
657 fputs
( " menu ", out
);
658 print_quoted_string
(out
, prop
->text
);
662 fputs
( " symbol ", out
);
663 fprintf
(out
, "%s\n", prop
->menu
->sym
->name
);
666 fprintf
(out
, " unknown prop %d!\n", prop
->type
);
671 int len
= strlen
(menu
->help
);
672 while
(menu
->help
[--len
] == '\n')
674 fprintf
(out
, " help\n%s\n", menu
->help
);
678 void zconfdump
(FILE *out
)
680 struct property
*prop
;
684 menu
= rootmenu.list
;
686 if
((sym
= menu
->sym
))
687 print_symbol
(out
, menu
);
688 else if
((prop
= menu
->prompt
)) {
689 switch
(prop
->type
) {
691 fputs
("\ncomment ", out
);
692 print_quoted_string
(out
, prop
->text
);
696 fputs
("\nmenu ", out
);
697 print_quoted_string
(out
, prop
->text
);
703 if
(!expr_is_yes
(prop
->visible.expr
)) {
704 fputs
(" depends ", out
);
705 expr_fprint
(prop
->visible.expr
, out
);
714 else while
((menu
= menu
->parent
)) {
715 if
(menu
->prompt
&& menu
->prompt
->type
== P_MENU
)
716 fputs
("\nendmenu\n", out
);