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