blob: 523c9a95a8384bbfdd12beeb5211b2c27d2a1f58 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 */
5%{
6
7#include <ctype.h>
8#include <stdarg.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <stdbool.h>
13
14#include "lkc.h"
15#include "internal.h"
16
17#define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt)
18
19#define PRINTD 0x0001
20#define DEBUG_PARSE 0x0002
21
22int cdebug = PRINTD;
23
24static void yyerror(const char *err);
25static void zconfprint(const char *err, ...);
26static void zconf_error(const char *err, ...);
27static bool zconf_endtoken(const char *tokenname,
28 const char *expected_tokenname);
29
30struct symbol *symbol_hash[SYMBOL_HASHSIZE];
31
32struct menu *current_menu, *current_entry;
33
34%}
35
36%union
37{
38 char *string;
39 struct symbol *symbol;
40 struct expr *expr;
41 struct menu *menu;
42 enum symbol_type type;
43 enum variable_flavor flavor;
44}
45
46%token <string> T_HELPTEXT
47%token <string> T_WORD
48%token <string> T_WORD_QUOTE
49%token T_BOOL
50%token T_CHOICE
51%token T_CLOSE_PAREN
52%token T_COLON_EQUAL
53%token T_COMMENT
54%token T_CONFIG
55%token T_DEFAULT
56%token T_DEF_BOOL
57%token T_DEF_TRISTATE
58%token T_DEPENDS
59%token T_ENDCHOICE
60%token T_ENDIF
61%token T_ENDMENU
62%token T_HELP
63%token T_HEX
64%token T_IF
65%token T_IMPLY
66%token T_INT
67%token T_MAINMENU
68%token T_MENU
69%token T_MENUCONFIG
70%token T_MODULES
71%token T_ON
72%token T_OPEN_PAREN
73%token T_OPTIONAL
74%token T_PLUS_EQUAL
75%token T_PROMPT
76%token T_RANGE
77%token T_RESET
78%token T_SELECT
79%token T_SOURCE
80%token T_STRING
81%token T_TRISTATE
82%token T_VISIBLE
83%token T_EOL
84%token <string> T_ASSIGN_VAL
85
86%left T_OR
87%left T_AND
88%left T_EQUAL T_UNEQUAL
89%left T_LESS T_LESS_EQUAL T_GREATER T_GREATER_EQUAL
90%nonassoc T_NOT
91
92%type <symbol> nonconst_symbol
93%type <symbol> symbol
94%type <type> type logic_type default
95%type <expr> expr
96%type <expr> if_expr
97%type <string> end
98%type <menu> if_entry menu_entry choice_entry
99%type <string> word_opt assign_val
100%type <flavor> assign_op
101
102%destructor {
103 fprintf(stderr, "%s:%d: missing end statement for this entry\n",
104 $$->file->name, $$->lineno);
105 if (current_menu == $$)
106 menu_end_menu();
107} if_entry menu_entry choice_entry
108
109%%
110input: mainmenu_stmt stmt_list | stmt_list;
111
112/* mainmenu entry */
113
114mainmenu_stmt: T_MAINMENU T_WORD_QUOTE T_EOL
115{
116 menu_add_prompt(P_MENU, $2, NULL);
117};
118
119stmt_list:
120 /* empty */
121 | stmt_list assignment_stmt
122 | stmt_list choice_stmt
123 | stmt_list comment_stmt
124 | stmt_list config_stmt
125 | stmt_list if_stmt
126 | stmt_list menu_stmt
127 | stmt_list menuconfig_stmt
128 | stmt_list source_stmt
129 | stmt_list T_WORD error T_EOL { zconf_error("unknown statement \"%s\"", $2); }
130 | stmt_list error T_EOL { zconf_error("invalid statement"); }
131;
132
133stmt_list_in_choice:
134 /* empty */
135 | stmt_list_in_choice comment_stmt
136 | stmt_list_in_choice config_stmt
137 | stmt_list_in_choice if_stmt_in_choice
138 | stmt_list_in_choice error T_EOL { zconf_error("invalid statement"); }
139;
140
141/* config/menuconfig entry */
142
143config_entry_start: T_CONFIG nonconst_symbol T_EOL
144{
145 $2->flags |= SYMBOL_OPTIONAL;
146 menu_add_entry($2);
147 printd(DEBUG_PARSE, "%s:%d:config %s\n", zconf_curname(), zconf_lineno(), $2->name);
148};
149
150config_stmt: config_entry_start config_option_list
151{
152 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
153};
154
155menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL
156{
157 $2->flags |= SYMBOL_OPTIONAL;
158 menu_add_entry($2);
159 printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", zconf_curname(), zconf_lineno(), $2->name);
160};
161
162menuconfig_stmt: menuconfig_entry_start config_option_list
163{
164 if (current_entry->prompt)
165 current_entry->prompt->type = P_MENU;
166 else
167 zconfprint("warning: menuconfig statement without prompt");
168 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
169};
170
171config_option_list:
172 /* empty */
173 | config_option_list config_option
174 | config_option_list depends
175 | config_option_list help
176;
177
178config_option: type prompt_stmt_opt T_EOL
179{
180 menu_set_type($1);
181 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
182 zconf_curname(), zconf_lineno(),
183 $1);
184};
185
186config_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
187{
188 menu_add_prompt(P_PROMPT, $2, $3);
189 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
190};
191
192config_option: default expr if_expr T_EOL
193{
194 menu_add_expr(P_DEFAULT, $2, $3);
195 if ($1 != S_UNKNOWN)
196 menu_set_type($1);
197 printd(DEBUG_PARSE, "%s:%d:default(%u)\n",
198 zconf_curname(), zconf_lineno(),
199 $1);
200};
201
202config_option: T_SELECT nonconst_symbol if_expr T_EOL
203{
204 menu_add_symbol(P_SELECT, $2, $3);
205 printd(DEBUG_PARSE, "%s:%d:select\n", zconf_curname(), zconf_lineno());
206};
207
208config_option: T_IMPLY nonconst_symbol if_expr T_EOL
209{
210 menu_add_symbol(P_IMPLY, $2, $3);
211 printd(DEBUG_PARSE, "%s:%d:imply\n", zconf_curname(), zconf_lineno());
212};
213
214config_option: T_RANGE symbol symbol if_expr T_EOL
215{
216 menu_add_expr(P_RANGE, expr_alloc_comp(E_RANGE,$2, $3), $4);
217 printd(DEBUG_PARSE, "%s:%d:range\n", zconf_curname(), zconf_lineno());
218};
219
220config_option: T_MODULES T_EOL
221{
222 if (modules_sym)
223 zconf_error("symbol '%s' redefines option 'modules' already defined by symbol '%s'",
224 current_entry->sym->name, modules_sym->name);
225 modules_sym = current_entry->sym;
226};
227
228/* choice entry */
229
230choice: T_CHOICE word_opt T_EOL
231{
232 struct symbol *sym = sym_lookup($2, SYMBOL_CHOICE);
233 sym->flags |= SYMBOL_NO_WRITE;
234 menu_add_entry(sym);
235 menu_add_expr(P_CHOICE, NULL, NULL);
236 free($2);
237 printd(DEBUG_PARSE, "%s:%d:choice\n", zconf_curname(), zconf_lineno());
238};
239
240choice_entry: choice choice_option_list
241{
242 $$ = menu_add_menu();
243};
244
245choice_end: end
246{
247 if (zconf_endtoken($1, "choice")) {
248 menu_end_menu();
249 printd(DEBUG_PARSE, "%s:%d:endchoice\n", zconf_curname(), zconf_lineno());
250 }
251};
252
253choice_stmt: choice_entry stmt_list_in_choice choice_end
254;
255
256choice_option_list:
257 /* empty */
258 | choice_option_list choice_option
259 | choice_option_list depends
260 | choice_option_list help
261;
262
263choice_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
264{
265 menu_add_prompt(P_PROMPT, $2, $3);
266 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
267};
268
269choice_option: logic_type prompt_stmt_opt T_EOL
270{
271 menu_set_type($1);
272 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
273 zconf_curname(), zconf_lineno(), $1);
274};
275
276choice_option: T_OPTIONAL T_EOL
277{
278 current_entry->sym->flags |= SYMBOL_OPTIONAL;
279 printd(DEBUG_PARSE, "%s:%d:optional\n", zconf_curname(), zconf_lineno());
280};
281
282choice_option: T_RESET if_expr T_EOL
283{
284 menu_add_prop(P_RESET, NULL, $2);
285};
286
287choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL
288{
289 menu_add_symbol(P_DEFAULT, $2, $3);
290 printd(DEBUG_PARSE, "%s:%d:default\n",
291 zconf_curname(), zconf_lineno());
292};
293
294type:
295 logic_type
296 | T_INT { $$ = S_INT; }
297 | T_HEX { $$ = S_HEX; }
298 | T_STRING { $$ = S_STRING; }
299
300logic_type:
301 T_BOOL { $$ = S_BOOLEAN; }
302 | T_TRISTATE { $$ = S_TRISTATE; }
303
304default:
305 T_DEFAULT { $$ = S_UNKNOWN; }
306 | T_DEF_BOOL { $$ = S_BOOLEAN; }
307 | T_DEF_TRISTATE { $$ = S_TRISTATE; }
308
309/* if entry */
310
311if_entry: T_IF expr T_EOL
312{
313 printd(DEBUG_PARSE, "%s:%d:if\n", zconf_curname(), zconf_lineno());
314 menu_add_entry(NULL);
315 menu_add_dep($2);
316 $$ = menu_add_menu();
317};
318
319if_end: end
320{
321 if (zconf_endtoken($1, "if")) {
322 menu_end_menu();
323 printd(DEBUG_PARSE, "%s:%d:endif\n", zconf_curname(), zconf_lineno());
324 }
325};
326
327if_stmt: if_entry stmt_list if_end
328;
329
330if_stmt_in_choice: if_entry stmt_list_in_choice if_end
331;
332
333/* menu entry */
334
335menu: T_MENU T_WORD_QUOTE T_EOL
336{
337 menu_add_entry(NULL);
338 menu_add_prompt(P_MENU, $2, NULL);
339 printd(DEBUG_PARSE, "%s:%d:menu\n", zconf_curname(), zconf_lineno());
340};
341
342menu_entry: menu menu_option_list
343{
344 $$ = menu_add_menu();
345};
346
347menu_end: end
348{
349 if (zconf_endtoken($1, "menu")) {
350 menu_end_menu();
351 printd(DEBUG_PARSE, "%s:%d:endmenu\n", zconf_curname(), zconf_lineno());
352 }
353};
354
355menu_stmt: menu_entry stmt_list menu_end
356;
357
358menu_option_list:
359 /* empty */
360 | menu_option_list visible
361 | menu_option_list depends
362;
363
364source_stmt: T_SOURCE T_WORD_QUOTE T_EOL
365{
366 printd(DEBUG_PARSE, "%s:%d:source %s\n", zconf_curname(), zconf_lineno(), $2);
367 zconf_nextfile($2);
368 free($2);
369};
370
371/* comment entry */
372
373comment: T_COMMENT T_WORD_QUOTE T_EOL
374{
375 menu_add_entry(NULL);
376 menu_add_prompt(P_COMMENT, $2, NULL);
377 printd(DEBUG_PARSE, "%s:%d:comment\n", zconf_curname(), zconf_lineno());
378};
379
380comment_stmt: comment comment_option_list
381;
382
383comment_option_list:
384 /* empty */
385 | comment_option_list depends
386;
387
388/* help option */
389
390help_start: T_HELP T_EOL
391{
392 printd(DEBUG_PARSE, "%s:%d:help\n", zconf_curname(), zconf_lineno());
393 zconf_starthelp();
394};
395
396help: help_start T_HELPTEXT
397{
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>");
402
403 current_entry->help = $2;
404};
405
406/* depends option */
407
408depends: T_DEPENDS T_ON expr T_EOL
409{
410 menu_add_dep($3);
411 printd(DEBUG_PARSE, "%s:%d:depends on\n", zconf_curname(), zconf_lineno());
412};
413
414/* visibility option */
415visible: T_VISIBLE if_expr T_EOL
416{
417 menu_add_visibility($2);
418};
419
420/* prompt statement */
421
422prompt_stmt_opt:
423 /* empty */
424 | T_WORD_QUOTE if_expr
425{
426 menu_add_prompt(P_PROMPT, $1, $2);
427};
428
429end: T_ENDMENU T_EOL { $$ = "menu"; }
430 | T_ENDCHOICE T_EOL { $$ = "choice"; }
431 | T_ENDIF T_EOL { $$ = "if"; }
432;
433
434if_expr: /* empty */ { $$ = NULL; }
435 | T_IF expr { $$ = $2; }
436;
437
438expr: 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); }
449;
450
451/* For symbol definitions, selects, etc., where quotes are not accepted */
452nonconst_symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); };
453
454symbol: nonconst_symbol
455 | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); }
456;
457
458word_opt: /* empty */ { $$ = NULL; }
459 | T_WORD
460
461/* assignment statement */
462
463assignment_stmt: T_WORD assign_op assign_val T_EOL { variable_add($1, $3, $2); free($1); free($3); }
464
465assign_op:
466 T_EQUAL { $$ = VAR_RECURSIVE; }
467 | T_COLON_EQUAL { $$ = VAR_SIMPLE; }
468 | T_PLUS_EQUAL { $$ = VAR_APPEND; }
469;
470
471assign_val:
472 /* empty */ { $$ = xstrdup(""); };
473 | T_ASSIGN_VAL
474;
475
476%%
477
478void conf_parse(const char *name)
479{
480 struct symbol *sym;
481 int i;
482
483 zconf_initscan(name);
484
485 _menu_init();
486
487#if YYDEBUG
488 if (getenv("ZCONF_DEBUG"))
489 yydebug = 1;
490#endif
491 yyparse();
492
493 /* Variables are expanded in the parse phase. We can free them here. */
494 variable_all_del();
495
496 if (yynerrs)
497 exit(1);
498 if (!modules_sym)
499 modules_sym = sym_find( "n" );
500
501 if (!menu_has_prompt(&rootmenu)) {
502 current_entry = &rootmenu;
503 menu_add_prompt(P_MENU, "Main menu", NULL);
504 }
505
506 menu_finalize(&rootmenu);
507 for_all_symbols(i, sym) {
508 if (sym_check_deps(sym))
509 yynerrs++;
510 }
511 if (yynerrs)
512 exit(1);
513 conf_set_changed(true);
514}
515
516static bool zconf_endtoken(const char *tokenname,
517 const char *expected_tokenname)
518{
519 if (strcmp(tokenname, expected_tokenname)) {
520 zconf_error("unexpected '%s' within %s block",
521 tokenname, expected_tokenname);
522 yynerrs++;
523 return false;
524 }
525 if (current_menu->file != current_file) {
526 zconf_error("'%s' in different file than '%s'",
527 tokenname, expected_tokenname);
528 fprintf(stderr, "%s:%d: location of the '%s'\n",
529 current_menu->file->name, current_menu->lineno,
530 expected_tokenname);
531 yynerrs++;
532 return false;
533 }
534 return true;
535}
536
537static void zconfprint(const char *err, ...)
538{
539 va_list ap;
540
541 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
542 va_start(ap, err);
543 vfprintf(stderr, err, ap);
544 va_end(ap);
545 fprintf(stderr, "\n");
546}
547
548static void zconf_error(const char *err, ...)
549{
550 va_list ap;
551
552 yynerrs++;
553 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
554 va_start(ap, err);
555 vfprintf(stderr, err, ap);
556 va_end(ap);
557 fprintf(stderr, "\n");
558}
559
560static void yyerror(const char *err)
561{
562 fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err);
563}
564
565static void print_quoted_string(FILE *out, const char *str)
566{
567 const char *p;
568 int len;
569
570 putc('"', out);
571 while ((p = strchr(str, '"'))) {
572 len = p - str;
573 if (len)
574 fprintf(out, "%.*s", len, str);
575 fputs("\\\"", out);
576 str = p + 1;
577 }
578 fputs(str, out);
579 putc('"', out);
580}
581
582static void print_symbol(FILE *out, struct menu *menu)
583{
584 struct symbol *sym = menu->sym;
585 struct property *prop;
586
587 if (sym_is_choice(sym))
588 fprintf(out, "\nchoice\n");
589 else
590 fprintf(out, "\nconfig %s\n", sym->name);
591 switch (sym->type) {
592 case S_BOOLEAN:
593 fputs(" bool\n", out);
594 break;
595 case S_TRISTATE:
596 fputs(" tristate\n", out);
597 break;
598 case S_STRING:
599 fputs(" string\n", out);
600 break;
601 case S_INT:
602 fputs(" integer\n", out);
603 break;
604 case S_HEX:
605 fputs(" hex\n", out);
606 break;
607 default:
608 fputs(" ???\n", out);
609 break;
610 }
611 for (prop = sym->prop; prop; prop = prop->next) {
612 if (prop->menu != menu)
613 continue;
614 switch (prop->type) {
615 case P_PROMPT:
616 fputs(" prompt ", out);
617 print_quoted_string(out, prop->text);
618 if (!expr_is_yes(prop->visible.expr)) {
619 fputs(" if ", out);
620 expr_fprint(prop->visible.expr, out);
621 }
622 fputc('\n', out);
623 break;
624 case P_DEFAULT:
625 fputs( " default ", out);
626 expr_fprint(prop->expr, out);
627 if (!expr_is_yes(prop->visible.expr)) {
628 fputs(" if ", out);
629 expr_fprint(prop->visible.expr, out);
630 }
631 fputc('\n', out);
632 break;
633 case P_CHOICE:
634 fputs(" #choice value\n", out);
635 break;
636 case P_SELECT:
637 fputs( " select ", out);
638 expr_fprint(prop->expr, out);
639 fputc('\n', out);
640 break;
641 case P_IMPLY:
642 fputs( " imply ", out);
643 expr_fprint(prop->expr, out);
644 fputc('\n', out);
645 break;
646 case P_RANGE:
647 fputs( " range ", out);
648 expr_fprint(prop->expr, out);
649 fputc('\n', out);
650 break;
651 case P_MENU:
652 fputs( " menu ", out);
653 print_quoted_string(out, prop->text);
654 fputc('\n', out);
655 break;
656 case P_SYMBOL:
657 fputs( " symbol ", out);
658 fprintf(out, "%s\n", prop->menu->sym->name);
659 break;
660 default:
661 fprintf(out, " unknown prop %d!\n", prop->type);
662 break;
663 }
664 }
665 if (menu->help) {
666 int len = strlen(menu->help);
667 while (menu->help[--len] == '\n')
668 menu->help[len] = 0;
669 fprintf(out, " help\n%s\n", menu->help);
670 }
671}
672
673void zconfdump(FILE *out)
674{
675 struct property *prop;
676 struct symbol *sym;
677 struct menu *menu;
678
679 menu = rootmenu.list;
680 while (menu) {
681 if ((sym = menu->sym))
682 print_symbol(out, menu);
683 else if ((prop = menu->prompt)) {
684 switch (prop->type) {
685 case P_COMMENT:
686 fputs("\ncomment ", out);
687 print_quoted_string(out, prop->text);
688 fputs("\n", out);
689 break;
690 case P_MENU:
691 fputs("\nmenu ", out);
692 print_quoted_string(out, prop->text);
693 fputs("\n", out);
694 break;
695 default:
696 ;
697 }
698 if (!expr_is_yes(prop->visible.expr)) {
699 fputs(" depends ", out);
700 expr_fprint(prop->visible.expr, out);
701 fputc('\n', out);
702 }
703 }
704
705 if (menu->list)
706 menu = menu->list;
707 else if (menu->next)
708 menu = menu->next;
709 else while ((menu = menu->parent)) {
710 if (menu->prompt && menu->prompt->type == P_MENU)
711 fputs("\nendmenu\n", out);
712 if (menu->next) {
713 menu = menu->next;
714 break;
715 }
716 }
717 }
718}