at v6.12 17 kB view raw
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 <xalloc.h> 15#include "lkc.h" 16#include "internal.h" 17#include "preprocess.h" 18 19#define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt) 20 21#define PRINTD 0x0001 22#define DEBUG_PARSE 0x0002 23 24int cdebug = PRINTD; 25 26static void yyerror(const char *err); 27static void zconfprint(const char *err, ...); 28static void zconf_error(const char *err, ...); 29static bool zconf_endtoken(const char *tokenname, 30 const char *expected_tokenname); 31 32struct menu *current_menu, *current_entry, *current_choice; 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_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 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 menu_add_entry($2); 144 printd(DEBUG_PARSE, "%s:%d:config %s\n", cur_filename, cur_lineno, $2->name); 145}; 146 147config_stmt: config_entry_start config_option_list 148{ 149 if (current_choice) { 150 if (!current_entry->prompt) { 151 fprintf(stderr, "%s:%d: error: choice member must have a prompt\n", 152 current_entry->filename, current_entry->lineno); 153 yynerrs++; 154 } 155 156 if (current_entry->sym->type != S_BOOLEAN) { 157 fprintf(stderr, "%s:%d: error: choice member must be bool\n", 158 current_entry->filename, current_entry->lineno); 159 yynerrs++; 160 } 161 162 /* 163 * If the same symbol appears twice in a choice block, the list 164 * node would be added twice, leading to a broken linked list. 165 * list_empty() ensures that this symbol has not yet added. 166 */ 167 if (list_empty(&current_entry->sym->choice_link)) 168 list_add_tail(&current_entry->sym->choice_link, 169 &current_choice->choice_members); 170 } 171 172 printd(DEBUG_PARSE, "%s:%d:endconfig\n", cur_filename, cur_lineno); 173}; 174 175menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL 176{ 177 menu_add_entry($2); 178 printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", cur_filename, cur_lineno, $2->name); 179}; 180 181menuconfig_stmt: menuconfig_entry_start config_option_list 182{ 183 if (current_entry->prompt) 184 current_entry->prompt->type = P_MENU; 185 else 186 zconfprint("warning: menuconfig statement without prompt"); 187 printd(DEBUG_PARSE, "%s:%d:endconfig\n", cur_filename, cur_lineno); 188}; 189 190config_option_list: 191 /* empty */ 192 | config_option_list config_option 193 | config_option_list depends 194 | config_option_list help 195; 196 197config_option: type prompt_stmt_opt T_EOL 198{ 199 menu_set_type($1); 200 printd(DEBUG_PARSE, "%s:%d:type(%u)\n", cur_filename, cur_lineno, $1); 201}; 202 203config_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL 204{ 205 menu_add_prompt(P_PROMPT, $2, $3); 206 printd(DEBUG_PARSE, "%s:%d:prompt\n", cur_filename, cur_lineno); 207}; 208 209config_option: default expr if_expr T_EOL 210{ 211 menu_add_expr(P_DEFAULT, $2, $3); 212 if ($1 != S_UNKNOWN) 213 menu_set_type($1); 214 printd(DEBUG_PARSE, "%s:%d:default(%u)\n", cur_filename, cur_lineno, 215 $1); 216}; 217 218config_option: T_SELECT nonconst_symbol if_expr T_EOL 219{ 220 menu_add_symbol(P_SELECT, $2, $3); 221 printd(DEBUG_PARSE, "%s:%d:select\n", cur_filename, cur_lineno); 222}; 223 224config_option: T_IMPLY nonconst_symbol if_expr T_EOL 225{ 226 menu_add_symbol(P_IMPLY, $2, $3); 227 printd(DEBUG_PARSE, "%s:%d:imply\n", cur_filename, cur_lineno); 228}; 229 230config_option: T_RANGE symbol symbol if_expr T_EOL 231{ 232 menu_add_expr(P_RANGE, expr_alloc_comp(E_RANGE,$2, $3), $4); 233 printd(DEBUG_PARSE, "%s:%d:range\n", cur_filename, cur_lineno); 234}; 235 236config_option: T_MODULES T_EOL 237{ 238 if (modules_sym) 239 zconf_error("symbol '%s' redefines option 'modules' already defined by symbol '%s'", 240 current_entry->sym->name, modules_sym->name); 241 modules_sym = current_entry->sym; 242}; 243 244/* choice entry */ 245 246choice: T_CHOICE T_EOL 247{ 248 struct symbol *sym = sym_lookup(NULL, 0); 249 250 menu_add_entry(sym); 251 menu_set_type(S_BOOLEAN); 252 INIT_LIST_HEAD(&current_entry->choice_members); 253 254 printd(DEBUG_PARSE, "%s:%d:choice\n", cur_filename, cur_lineno); 255}; 256 257choice_entry: choice choice_option_list 258{ 259 if (!current_entry->prompt) { 260 fprintf(stderr, "%s:%d: error: choice must have a prompt\n", 261 current_entry->filename, current_entry->lineno); 262 yynerrs++; 263 } 264 265 $$ = menu_add_menu(); 266 267 current_choice = current_entry; 268}; 269 270choice_end: end 271{ 272 current_choice = NULL; 273 274 if (zconf_endtoken($1, "choice")) { 275 menu_end_menu(); 276 printd(DEBUG_PARSE, "%s:%d:endchoice\n", cur_filename, cur_lineno); 277 } 278}; 279 280choice_stmt: choice_entry stmt_list_in_choice choice_end 281; 282 283choice_option_list: 284 /* empty */ 285 | choice_option_list choice_option 286 | choice_option_list depends 287 | choice_option_list help 288; 289 290choice_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL 291{ 292 menu_add_prompt(P_PROMPT, $2, $3); 293 printd(DEBUG_PARSE, "%s:%d:prompt\n", cur_filename, cur_lineno); 294}; 295 296choice_option: T_BOOL T_WORD_QUOTE if_expr T_EOL 297{ 298 menu_add_prompt(P_PROMPT, $2, $3); 299 printd(DEBUG_PARSE, "%s:%d:bool\n", cur_filename, cur_lineno); 300}; 301 302choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL 303{ 304 menu_add_symbol(P_DEFAULT, $2, $3); 305 printd(DEBUG_PARSE, "%s:%d:default\n", cur_filename, cur_lineno); 306}; 307 308type: 309 T_BOOL { $$ = S_BOOLEAN; } 310 | T_TRISTATE { $$ = S_TRISTATE; } 311 | T_INT { $$ = S_INT; } 312 | T_HEX { $$ = S_HEX; } 313 | T_STRING { $$ = S_STRING; } 314 315default: 316 T_DEFAULT { $$ = S_UNKNOWN; } 317 | T_DEF_BOOL { $$ = S_BOOLEAN; } 318 | T_DEF_TRISTATE { $$ = S_TRISTATE; } 319 320/* if entry */ 321 322if_entry: T_IF expr T_EOL 323{ 324 printd(DEBUG_PARSE, "%s:%d:if\n", cur_filename, cur_lineno); 325 menu_add_entry(NULL); 326 menu_add_dep($2); 327 $$ = menu_add_menu(); 328}; 329 330if_end: end 331{ 332 if (zconf_endtoken($1, "if")) { 333 menu_end_menu(); 334 printd(DEBUG_PARSE, "%s:%d:endif\n", cur_filename, cur_lineno); 335 } 336}; 337 338if_stmt: if_entry stmt_list if_end 339; 340 341if_stmt_in_choice: if_entry stmt_list_in_choice if_end 342; 343 344/* menu entry */ 345 346menu: T_MENU T_WORD_QUOTE T_EOL 347{ 348 menu_add_entry(NULL); 349 menu_add_prompt(P_MENU, $2, NULL); 350 printd(DEBUG_PARSE, "%s:%d:menu\n", cur_filename, cur_lineno); 351}; 352 353menu_entry: menu menu_option_list 354{ 355 $$ = menu_add_menu(); 356}; 357 358menu_end: end 359{ 360 if (zconf_endtoken($1, "menu")) { 361 menu_end_menu(); 362 printd(DEBUG_PARSE, "%s:%d:endmenu\n", cur_filename, cur_lineno); 363 } 364}; 365 366menu_stmt: menu_entry stmt_list menu_end 367; 368 369menu_option_list: 370 /* empty */ 371 | menu_option_list visible 372 | menu_option_list depends 373; 374 375source_stmt: T_SOURCE T_WORD_QUOTE T_EOL 376{ 377 printd(DEBUG_PARSE, "%s:%d:source %s\n", cur_filename, cur_lineno, $2); 378 zconf_nextfile($2); 379 free($2); 380}; 381 382/* comment entry */ 383 384comment: T_COMMENT T_WORD_QUOTE T_EOL 385{ 386 menu_add_entry(NULL); 387 menu_add_prompt(P_COMMENT, $2, NULL); 388 printd(DEBUG_PARSE, "%s:%d:comment\n", cur_filename, cur_lineno); 389}; 390 391comment_stmt: comment comment_option_list 392; 393 394comment_option_list: 395 /* empty */ 396 | comment_option_list depends 397; 398 399/* help option */ 400 401help_start: T_HELP T_EOL 402{ 403 printd(DEBUG_PARSE, "%s:%d:help\n", cur_filename, cur_lineno); 404 zconf_starthelp(); 405}; 406 407help: help_start T_HELPTEXT 408{ 409 if (current_entry->help) { 410 free(current_entry->help); 411 zconfprint("warning: '%s' defined with more than one help text -- only the last one will be used", 412 current_entry->sym->name ?: "<choice>"); 413 } 414 415 /* Is the help text empty or all whitespace? */ 416 if ($2[strspn($2, " \f\n\r\t\v")] == '\0') 417 zconfprint("warning: '%s' defined with blank help text", 418 current_entry->sym->name ?: "<choice>"); 419 420 current_entry->help = $2; 421}; 422 423/* depends option */ 424 425depends: T_DEPENDS T_ON expr T_EOL 426{ 427 menu_add_dep($3); 428 printd(DEBUG_PARSE, "%s:%d:depends on\n", cur_filename, cur_lineno); 429}; 430 431/* visibility option */ 432visible: T_VISIBLE if_expr T_EOL 433{ 434 menu_add_visibility($2); 435}; 436 437/* prompt statement */ 438 439prompt_stmt_opt: 440 /* empty */ 441 | T_WORD_QUOTE if_expr 442{ 443 menu_add_prompt(P_PROMPT, $1, $2); 444}; 445 446end: T_ENDMENU T_EOL { $$ = "menu"; } 447 | T_ENDCHOICE T_EOL { $$ = "choice"; } 448 | T_ENDIF T_EOL { $$ = "if"; } 449; 450 451if_expr: /* empty */ { $$ = NULL; } 452 | T_IF expr { $$ = $2; } 453; 454 455expr: symbol { $$ = expr_alloc_symbol($1); } 456 | symbol T_LESS symbol { $$ = expr_alloc_comp(E_LTH, $1, $3); } 457 | symbol T_LESS_EQUAL symbol { $$ = expr_alloc_comp(E_LEQ, $1, $3); } 458 | symbol T_GREATER symbol { $$ = expr_alloc_comp(E_GTH, $1, $3); } 459 | symbol T_GREATER_EQUAL symbol { $$ = expr_alloc_comp(E_GEQ, $1, $3); } 460 | symbol T_EQUAL symbol { $$ = expr_alloc_comp(E_EQUAL, $1, $3); } 461 | symbol T_UNEQUAL symbol { $$ = expr_alloc_comp(E_UNEQUAL, $1, $3); } 462 | T_OPEN_PAREN expr T_CLOSE_PAREN { $$ = $2; } 463 | T_NOT expr { $$ = expr_alloc_one(E_NOT, $2); } 464 | expr T_OR expr { $$ = expr_alloc_two(E_OR, $1, $3); } 465 | expr T_AND expr { $$ = expr_alloc_two(E_AND, $1, $3); } 466; 467 468/* For symbol definitions, selects, etc., where quotes are not accepted */ 469nonconst_symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); }; 470 471symbol: nonconst_symbol 472 | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); } 473; 474 475/* assignment statement */ 476 477assignment_stmt: T_WORD assign_op assign_val T_EOL { variable_add($1, $3, $2); free($1); free($3); } 478 479assign_op: 480 T_EQUAL { $$ = VAR_RECURSIVE; } 481 | T_COLON_EQUAL { $$ = VAR_SIMPLE; } 482 | T_PLUS_EQUAL { $$ = VAR_APPEND; } 483; 484 485assign_val: 486 /* empty */ { $$ = xstrdup(""); }; 487 | T_ASSIGN_VAL 488; 489 490%% 491 492/** 493 * choice_check_sanity - check sanity of a choice member 494 * 495 * @menu: menu of the choice member 496 * 497 * Return: -1 if an error is found, 0 otherwise. 498 */ 499static int choice_check_sanity(const struct menu *menu) 500{ 501 struct property *prop; 502 int ret = 0; 503 504 for (prop = menu->sym->prop; prop; prop = prop->next) { 505 if (prop->type == P_DEFAULT) { 506 fprintf(stderr, "%s:%d: error: %s", 507 prop->filename, prop->lineno, 508 "defaults for choice values not supported\n"); 509 ret = -1; 510 } 511 512 if (prop->menu != menu && prop->type == P_PROMPT && 513 prop->menu->parent != menu->parent) { 514 fprintf(stderr, "%s:%d: error: %s", 515 prop->filename, prop->lineno, 516 "choice value has a prompt outside its choice group\n"); 517 ret = -1; 518 } 519 } 520 521 return ret; 522} 523 524void conf_parse(const char *name) 525{ 526 struct menu *menu; 527 528 autoconf_cmd = str_new(); 529 530 str_printf(&autoconf_cmd, "\ndeps_config := \\\n"); 531 532 zconf_initscan(name); 533 534 _menu_init(); 535 536 if (getenv("ZCONF_DEBUG")) 537 yydebug = 1; 538 yyparse(); 539 540 str_printf(&autoconf_cmd, 541 "\n" 542 "$(autoconfig): $(deps_config)\n" 543 "$(deps_config): ;\n"); 544 545 env_write_dep(&autoconf_cmd); 546 547 /* Variables are expanded in the parse phase. We can free them here. */ 548 variable_all_del(); 549 550 if (yynerrs) 551 exit(1); 552 if (!modules_sym) 553 modules_sym = &symbol_no; 554 555 if (!menu_has_prompt(&rootmenu)) { 556 current_entry = &rootmenu; 557 menu_add_prompt(P_MENU, "Main menu", NULL); 558 } 559 560 menu_finalize(); 561 562 menu_for_each_entry(menu) { 563 struct menu *child; 564 565 if (menu->sym && sym_check_deps(menu->sym)) 566 yynerrs++; 567 568 if (menu->sym && sym_is_choice(menu->sym)) { 569 menu_for_each_sub_entry(child, menu) 570 if (child->sym && choice_check_sanity(child)) 571 yynerrs++; 572 } 573 } 574 575 if (yynerrs) 576 exit(1); 577 conf_set_changed(true); 578} 579 580static bool zconf_endtoken(const char *tokenname, 581 const char *expected_tokenname) 582{ 583 if (strcmp(tokenname, expected_tokenname)) { 584 zconf_error("unexpected '%s' within %s block", 585 tokenname, expected_tokenname); 586 yynerrs++; 587 return false; 588 } 589 if (strcmp(current_menu->filename, cur_filename)) { 590 zconf_error("'%s' in different file than '%s'", 591 tokenname, expected_tokenname); 592 fprintf(stderr, "%s:%d: location of the '%s'\n", 593 current_menu->filename, current_menu->lineno, 594 expected_tokenname); 595 yynerrs++; 596 return false; 597 } 598 return true; 599} 600 601static void zconfprint(const char *err, ...) 602{ 603 va_list ap; 604 605 fprintf(stderr, "%s:%d: ", cur_filename, cur_lineno); 606 va_start(ap, err); 607 vfprintf(stderr, err, ap); 608 va_end(ap); 609 fprintf(stderr, "\n"); 610} 611 612static void zconf_error(const char *err, ...) 613{ 614 va_list ap; 615 616 yynerrs++; 617 fprintf(stderr, "%s:%d: ", cur_filename, cur_lineno); 618 va_start(ap, err); 619 vfprintf(stderr, err, ap); 620 va_end(ap); 621 fprintf(stderr, "\n"); 622} 623 624static void yyerror(const char *err) 625{ 626 fprintf(stderr, "%s:%d: %s\n", cur_filename, cur_lineno, err); 627} 628 629static void print_quoted_string(FILE *out, const char *str) 630{ 631 const char *p; 632 int len; 633 634 putc('"', out); 635 while ((p = strchr(str, '"'))) { 636 len = p - str; 637 if (len) 638 fprintf(out, "%.*s", len, str); 639 fputs("\\\"", out); 640 str = p + 1; 641 } 642 fputs(str, out); 643 putc('"', out); 644} 645 646static void print_symbol(FILE *out, const struct menu *menu) 647{ 648 struct symbol *sym = menu->sym; 649 struct property *prop; 650 651 if (sym_is_choice(sym)) 652 fprintf(out, "\nchoice\n"); 653 else 654 fprintf(out, "\nconfig %s\n", sym->name); 655 switch (sym->type) { 656 case S_BOOLEAN: 657 fputs(" bool\n", out); 658 break; 659 case S_TRISTATE: 660 fputs(" tristate\n", out); 661 break; 662 case S_STRING: 663 fputs(" string\n", out); 664 break; 665 case S_INT: 666 fputs(" integer\n", out); 667 break; 668 case S_HEX: 669 fputs(" hex\n", out); 670 break; 671 default: 672 fputs(" ???\n", out); 673 break; 674 } 675 for (prop = sym->prop; prop; prop = prop->next) { 676 if (prop->menu != menu) 677 continue; 678 switch (prop->type) { 679 case P_PROMPT: 680 fputs(" prompt ", out); 681 print_quoted_string(out, prop->text); 682 if (!expr_is_yes(prop->visible.expr)) { 683 fputs(" if ", out); 684 expr_fprint(prop->visible.expr, out); 685 } 686 fputc('\n', out); 687 break; 688 case P_DEFAULT: 689 fputs( " default ", out); 690 expr_fprint(prop->expr, out); 691 if (!expr_is_yes(prop->visible.expr)) { 692 fputs(" if ", out); 693 expr_fprint(prop->visible.expr, out); 694 } 695 fputc('\n', out); 696 break; 697 case P_SELECT: 698 fputs( " select ", out); 699 expr_fprint(prop->expr, out); 700 fputc('\n', out); 701 break; 702 case P_IMPLY: 703 fputs( " imply ", out); 704 expr_fprint(prop->expr, out); 705 fputc('\n', out); 706 break; 707 case P_RANGE: 708 fputs( " range ", out); 709 expr_fprint(prop->expr, out); 710 fputc('\n', out); 711 break; 712 case P_MENU: 713 fputs( " menu ", out); 714 print_quoted_string(out, prop->text); 715 fputc('\n', out); 716 break; 717 default: 718 fprintf(out, " unknown prop %d!\n", prop->type); 719 break; 720 } 721 } 722 if (menu->help) { 723 int len = strlen(menu->help); 724 while (menu->help[--len] == '\n') 725 menu->help[len] = 0; 726 fprintf(out, " help\n%s\n", menu->help); 727 } 728} 729 730void zconfdump(FILE *out) 731{ 732 struct property *prop; 733 struct symbol *sym; 734 struct menu *menu; 735 736 menu = rootmenu.list; 737 while (menu) { 738 if ((sym = menu->sym)) 739 print_symbol(out, menu); 740 else if ((prop = menu->prompt)) { 741 switch (prop->type) { 742 case P_COMMENT: 743 fputs("\ncomment ", out); 744 print_quoted_string(out, prop->text); 745 fputs("\n", out); 746 break; 747 case P_MENU: 748 fputs("\nmenu ", out); 749 print_quoted_string(out, prop->text); 750 fputs("\n", out); 751 break; 752 default: 753 ; 754 } 755 if (!expr_is_yes(prop->visible.expr)) { 756 fputs(" depends ", out); 757 expr_fprint(prop->visible.expr, out); 758 fputc('\n', out); 759 } 760 } 761 762 if (menu->list) 763 menu = menu->list; 764 else if (menu->next) 765 menu = menu->next; 766 else while ((menu = menu->parent)) { 767 if (menu->prompt && menu->prompt->type == P_MENU) 768 fputs("\nendmenu\n", out); 769 if (menu->next) { 770 menu = menu->next; 771 break; 772 } 773 } 774 } 775}