Update build system, documentation and delete temp files
[wave300.git] / tools / kconfig / zconf.y
blob2c45edec62a43c0d0e8fee1130bab3d74284d870
1 %{
2 /*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 * Released under the terms of the GNU GPL v2.0.
5 */
7 #include <ctype.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdbool.h>
14 #include "lkc.h"
16 #define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt)
18 #define PRINTD 0x0001
19 #define DEBUG_PARSE 0x0002
21 #define yyparse zconfparse
22 #define yylex zconflex
23 #define yyerror zconferror
24 #define yydebug zconfdebug
25 #define yynerrs zconfnerrs
26 #define yylval zconflval
27 #define yychar zconfchar
29 int cdebug = PRINTD;
31 extern int zconflex(void);
32 static void zconfprint(const char *err, ...);
33 static void zconf_error(const char *err, ...);
34 static void zconferror(const char *err);
35 static bool zconf_endtoken(const struct kconf_id *id, int starttoken, int endtoken);
37 struct symbol *symbol_hash[SYMBOL_HASHSIZE];
39 static struct menu *current_menu, *current_entry;
42 %expect 30
44 %union
46 char *string;
47 struct file *file;
48 struct symbol *symbol;
49 struct expr *expr;
50 struct menu *menu;
51 const struct kconf_id *id;
54 %token <id>T_MAINMENU
55 %token <id>T_MENU
56 %token <id>T_ENDMENU
57 %token <id>T_SOURCE
58 %token <id>T_CHOICE
59 %token <id>T_ENDCHOICE
60 %token <id>T_COMMENT
61 %token <id>T_CONFIG
62 %token <id>T_MENUCONFIG
63 %token <id>T_HELP
64 %token <string> T_HELPTEXT
65 %token <id>T_IF
66 %token <id>T_ENDIF
67 %token <id>T_DEPENDS
68 %token <id>T_OPTIONAL
69 %token <id>T_PROMPT
70 %token <id>T_TYPE
71 %token <id>T_DEFAULT
72 %token <id>T_SELECT
73 %token <id>T_IMPLY
74 %token <id>T_RANGE
75 %token <id>T_VISIBLE
76 %token <id>T_OPTION
77 %token <id>T_ON
78 %token <string> T_WORD
79 %token <string> T_WORD_QUOTE
80 %token T_UNEQUAL
81 %token T_LESS
82 %token T_LESS_EQUAL
83 %token T_GREATER
84 %token T_GREATER_EQUAL
85 %token T_CLOSE_PAREN
86 %token T_OPEN_PAREN
87 %token T_EOL
89 %left T_OR
90 %left T_AND
91 %left T_EQUAL T_UNEQUAL
92 %left T_LESS T_LESS_EQUAL T_GREATER T_GREATER_EQUAL
93 %nonassoc T_NOT
95 %type <string> prompt
96 %type <symbol> symbol
97 %type <expr> expr
98 %type <expr> if_expr
99 %type <id> end
100 %type <id> option_name
101 %type <menu> if_entry menu_entry choice_entry
102 %type <string> symbol_option_arg word_opt
104 %destructor {
105 fprintf(stderr, "%s:%d: missing end statement for this entry\n",
106 $$->file->name, $$->lineno);
107 if (current_menu == $$)
108 menu_end_menu();
109 } if_entry menu_entry choice_entry
112 /* Include zconf_id.c here so it can see the token constants. */
113 #include "kconf_id.c"
117 input: nl start | start;
119 start: mainmenu_stmt stmt_list | no_mainmenu_stmt stmt_list;
121 /* mainmenu entry */
123 mainmenu_stmt: T_MAINMENU prompt T_EOL
125 menu_add_prompt(P_MENU, $2, NULL);
128 /* Default main menu, if there's no mainmenu entry */
130 no_mainmenu_stmt: /* empty */
133 * Hack: Keep the main menu title on the heap so we can safely free it
134 * later regardless of whether it comes from the 'prompt' in
135 * mainmenu_stmt or here
137 menu_add_prompt(P_MENU, strdup("Linux Kernel Configuration"), NULL);
141 stmt_list:
142 /* empty */
143 | stmt_list common_stmt
144 | stmt_list choice_stmt
145 | stmt_list menu_stmt
146 | stmt_list end { zconf_error("unexpected end statement"); }
147 | stmt_list T_WORD error T_EOL { zconf_error("unknown statement \"%s\"", $2); }
148 | stmt_list option_name error T_EOL
150 zconf_error("unexpected option \"%s\"", $2->name);
152 | stmt_list error T_EOL { zconf_error("invalid statement"); }
155 option_name:
156 T_DEPENDS | T_PROMPT | T_TYPE | T_SELECT | T_IMPLY | T_OPTIONAL | T_RANGE | T_DEFAULT | T_VISIBLE
159 common_stmt:
160 T_EOL
161 | if_stmt
162 | comment_stmt
163 | config_stmt
164 | menuconfig_stmt
165 | source_stmt
168 option_error:
169 T_WORD error T_EOL { zconf_error("unknown option \"%s\"", $1); }
170 | error T_EOL { zconf_error("invalid option"); }
174 /* config/menuconfig entry */
176 config_entry_start: T_CONFIG T_WORD T_EOL
178 struct symbol *sym = sym_lookup($2, 0);
179 sym->flags |= SYMBOL_OPTIONAL;
180 menu_add_entry(sym);
181 printd(DEBUG_PARSE, "%s:%d:config %s\n", zconf_curname(), zconf_lineno(), $2);
184 config_stmt: config_entry_start config_option_list
186 menu_end_entry();
187 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
190 menuconfig_entry_start: T_MENUCONFIG T_WORD T_EOL
192 struct symbol *sym = sym_lookup($2, 0);
193 sym->flags |= SYMBOL_OPTIONAL;
194 menu_add_entry(sym);
195 printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", zconf_curname(), zconf_lineno(), $2);
198 menuconfig_stmt: menuconfig_entry_start config_option_list
200 if (current_entry->prompt)
201 current_entry->prompt->type = P_MENU;
202 else
203 zconfprint("warning: menuconfig statement without prompt");
204 menu_end_entry();
205 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
208 config_option_list:
209 /* empty */
210 | config_option_list config_option
211 | config_option_list symbol_option
212 | config_option_list depends
213 | config_option_list help
214 | config_option_list option_error
215 | config_option_list T_EOL
218 config_option: T_TYPE prompt_stmt_opt T_EOL
220 menu_set_type($1->stype);
221 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
222 zconf_curname(), zconf_lineno(),
223 $1->stype);
226 config_option: T_PROMPT prompt if_expr T_EOL
228 menu_add_prompt(P_PROMPT, $2, $3);
229 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
232 config_option: T_DEFAULT expr if_expr T_EOL
234 menu_add_expr(P_DEFAULT, $2, $3);
235 if ($1->stype != S_UNKNOWN)
236 menu_set_type($1->stype);
237 printd(DEBUG_PARSE, "%s:%d:default(%u)\n",
238 zconf_curname(), zconf_lineno(),
239 $1->stype);
242 config_option: T_SELECT T_WORD if_expr T_EOL
244 menu_add_symbol(P_SELECT, sym_lookup($2, 0), $3);
245 printd(DEBUG_PARSE, "%s:%d:select\n", zconf_curname(), zconf_lineno());
248 config_option: T_IMPLY T_WORD if_expr T_EOL
250 menu_add_symbol(P_IMPLY, sym_lookup($2, 0), $3);
251 printd(DEBUG_PARSE, "%s:%d:imply\n", zconf_curname(), zconf_lineno());
254 config_option: T_RANGE symbol symbol if_expr T_EOL
256 menu_add_expr(P_RANGE, expr_alloc_comp(E_RANGE,$2, $3), $4);
257 printd(DEBUG_PARSE, "%s:%d:range\n", zconf_curname(), zconf_lineno());
260 symbol_option: T_OPTION symbol_option_list T_EOL
263 symbol_option_list:
264 /* empty */
265 | symbol_option_list T_WORD symbol_option_arg
267 const struct kconf_id *id = kconf_id_lookup($2, strlen($2));
268 if (id && id->flags & TF_OPTION)
269 menu_add_option(id->token, $3);
270 else
271 zconfprint("warning: ignoring unknown option %s", $2);
272 free($2);
275 symbol_option_arg:
276 /* empty */ { $$ = NULL; }
277 | T_EQUAL prompt { $$ = $2; }
280 /* choice entry */
282 choice: T_CHOICE word_opt T_EOL
284 struct symbol *sym = sym_lookup($2, SYMBOL_CHOICE);
285 sym->flags |= SYMBOL_AUTO;
286 menu_add_entry(sym);
287 menu_add_expr(P_CHOICE, NULL, NULL);
288 printd(DEBUG_PARSE, "%s:%d:choice\n", zconf_curname(), zconf_lineno());
291 choice_entry: choice choice_option_list
293 $$ = menu_add_menu();
296 choice_end: end
298 if (zconf_endtoken($1, T_CHOICE, T_ENDCHOICE)) {
299 menu_end_menu();
300 printd(DEBUG_PARSE, "%s:%d:endchoice\n", zconf_curname(), zconf_lineno());
304 choice_stmt: choice_entry choice_block choice_end
307 choice_option_list:
308 /* empty */
309 | choice_option_list choice_option
310 | choice_option_list depends
311 | choice_option_list help
312 | choice_option_list T_EOL
313 | choice_option_list option_error
316 choice_option: T_PROMPT prompt if_expr T_EOL
318 menu_add_prompt(P_PROMPT, $2, $3);
319 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
322 choice_option: T_TYPE prompt_stmt_opt T_EOL
324 if ($1->stype == S_BOOLEAN || $1->stype == S_TRISTATE) {
325 menu_set_type($1->stype);
326 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
327 zconf_curname(), zconf_lineno(),
328 $1->stype);
329 } else
330 YYERROR;
333 choice_option: T_OPTIONAL T_EOL
335 current_entry->sym->flags |= SYMBOL_OPTIONAL;
336 printd(DEBUG_PARSE, "%s:%d:optional\n", zconf_curname(), zconf_lineno());
339 choice_option: T_DEFAULT T_WORD if_expr T_EOL
341 if ($1->stype == S_UNKNOWN) {
342 menu_add_symbol(P_DEFAULT, sym_lookup($2, 0), $3);
343 printd(DEBUG_PARSE, "%s:%d:default\n",
344 zconf_curname(), zconf_lineno());
345 } else
346 YYERROR;
349 choice_block:
350 /* empty */
351 | choice_block common_stmt
354 /* if entry */
356 if_entry: T_IF expr T_EOL
358 printd(DEBUG_PARSE, "%s:%d:if\n", zconf_curname(), zconf_lineno());
359 menu_add_entry(NULL);
360 menu_add_dep($2);
361 $$ = menu_add_menu();
364 if_end: end
366 if (zconf_endtoken($1, T_IF, T_ENDIF)) {
367 menu_end_menu();
368 printd(DEBUG_PARSE, "%s:%d:endif\n", zconf_curname(), zconf_lineno());
372 if_stmt: if_entry if_block if_end
375 if_block:
376 /* empty */
377 | if_block common_stmt
378 | if_block menu_stmt
379 | if_block choice_stmt
382 /* menu entry */
384 menu: T_MENU prompt T_EOL
386 menu_add_entry(NULL);
387 menu_add_prompt(P_MENU, $2, NULL);
388 printd(DEBUG_PARSE, "%s:%d:menu\n", zconf_curname(), zconf_lineno());
391 menu_entry: menu visibility_list depends_list
393 $$ = menu_add_menu();
396 menu_end: end
398 if (zconf_endtoken($1, T_MENU, T_ENDMENU)) {
399 menu_end_menu();
400 printd(DEBUG_PARSE, "%s:%d:endmenu\n", zconf_curname(), zconf_lineno());
404 menu_stmt: menu_entry menu_block menu_end
407 menu_block:
408 /* empty */
409 | menu_block common_stmt
410 | menu_block menu_stmt
411 | menu_block choice_stmt
414 source_stmt: T_SOURCE prompt T_EOL
416 printd(DEBUG_PARSE, "%s:%d:source %s\n", zconf_curname(), zconf_lineno(), $2);
417 zconf_nextfile($2);
420 /* comment entry */
422 comment: T_COMMENT prompt T_EOL
424 menu_add_entry(NULL);
425 menu_add_prompt(P_COMMENT, $2, NULL);
426 printd(DEBUG_PARSE, "%s:%d:comment\n", zconf_curname(), zconf_lineno());
429 comment_stmt: comment depends_list
431 menu_end_entry();
434 /* help option */
436 help_start: T_HELP T_EOL
438 printd(DEBUG_PARSE, "%s:%d:help\n", zconf_curname(), zconf_lineno());
439 zconf_starthelp();
442 help: help_start T_HELPTEXT
444 current_entry->help = $2;
447 /* depends option */
449 depends_list:
450 /* empty */
451 | depends_list depends
452 | depends_list T_EOL
453 | depends_list option_error
456 depends: T_DEPENDS T_ON expr T_EOL
458 menu_add_dep($3);
459 printd(DEBUG_PARSE, "%s:%d:depends on\n", zconf_curname(), zconf_lineno());
462 /* visibility option */
464 visibility_list:
465 /* empty */
466 | visibility_list visible
467 | visibility_list T_EOL
470 visible: T_VISIBLE if_expr
472 menu_add_visibility($2);
475 /* prompt statement */
477 prompt_stmt_opt:
478 /* empty */
479 | prompt if_expr
481 menu_add_prompt(P_PROMPT, $1, $2);
484 prompt: T_WORD
485 | T_WORD_QUOTE
488 end: T_ENDMENU T_EOL { $$ = $1; }
489 | T_ENDCHOICE T_EOL { $$ = $1; }
490 | T_ENDIF T_EOL { $$ = $1; }
494 T_EOL
495 | nl T_EOL
498 if_expr: /* empty */ { $$ = NULL; }
499 | T_IF expr { $$ = $2; }
502 expr: symbol { $$ = expr_alloc_symbol($1); }
503 | symbol T_LESS symbol { $$ = expr_alloc_comp(E_LTH, $1, $3); }
504 | symbol T_LESS_EQUAL symbol { $$ = expr_alloc_comp(E_LEQ, $1, $3); }
505 | symbol T_GREATER symbol { $$ = expr_alloc_comp(E_GTH, $1, $3); }
506 | symbol T_GREATER_EQUAL symbol { $$ = expr_alloc_comp(E_GEQ, $1, $3); }
507 | symbol T_EQUAL symbol { $$ = expr_alloc_comp(E_EQUAL, $1, $3); }
508 | symbol T_UNEQUAL symbol { $$ = expr_alloc_comp(E_UNEQUAL, $1, $3); }
509 | T_OPEN_PAREN expr T_CLOSE_PAREN { $$ = $2; }
510 | T_NOT expr { $$ = expr_alloc_one(E_NOT, $2); }
511 | expr T_OR expr { $$ = expr_alloc_two(E_OR, $1, $3); }
512 | expr T_AND expr { $$ = expr_alloc_two(E_AND, $1, $3); }
515 symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); }
516 | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); }
519 word_opt: /* empty */ { $$ = NULL; }
520 | T_WORD
524 void conf_parse(const char *name)
526 const char *tmp;
527 struct symbol *sym;
528 int i;
530 zconf_initscan(name);
532 sym_init();
533 _menu_init();
535 if (getenv("ZCONF_DEBUG"))
536 zconfdebug = 1;
537 zconfparse();
538 if (zconfnerrs)
539 exit(1);
540 if (!modules_sym)
541 modules_sym = sym_find( "n" );
543 tmp = rootmenu.prompt->text;
544 rootmenu.prompt->text = _(rootmenu.prompt->text);
545 rootmenu.prompt->text = sym_expand_string_value(rootmenu.prompt->text);
546 free((char*)tmp);
548 menu_finalize(&rootmenu);
549 for_all_symbols(i, sym) {
550 if (sym_check_deps(sym))
551 zconfnerrs++;
553 if (zconfnerrs)
554 exit(1);
555 sym_set_change_count(1);
558 static const char *zconf_tokenname(int token)
560 switch (token) {
561 case T_MENU: return "menu";
562 case T_ENDMENU: return "endmenu";
563 case T_CHOICE: return "choice";
564 case T_ENDCHOICE: return "endchoice";
565 case T_IF: return "if";
566 case T_ENDIF: return "endif";
567 case T_DEPENDS: return "depends";
568 case T_VISIBLE: return "visible";
570 return "<token>";
573 static bool zconf_endtoken(const struct kconf_id *id, int starttoken, int endtoken)
575 if (id->token != endtoken) {
576 zconf_error("unexpected '%s' within %s block",
577 id->name, zconf_tokenname(starttoken));
578 zconfnerrs++;
579 return false;
581 if (current_menu->file != current_file) {
582 zconf_error("'%s' in different file than '%s'",
583 id->name, zconf_tokenname(starttoken));
584 fprintf(stderr, "%s:%d: location of the '%s'\n",
585 current_menu->file->name, current_menu->lineno,
586 zconf_tokenname(starttoken));
587 zconfnerrs++;
588 return false;
590 return true;
593 static void zconfprint(const char *err, ...)
595 va_list ap;
597 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
598 va_start(ap, err);
599 vfprintf(stderr, err, ap);
600 va_end(ap);
601 fprintf(stderr, "\n");
604 static void zconf_error(const char *err, ...)
606 va_list ap;
608 zconfnerrs++;
609 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
610 va_start(ap, err);
611 vfprintf(stderr, err, ap);
612 va_end(ap);
613 fprintf(stderr, "\n");
616 static void zconferror(const char *err)
618 fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err);
621 static void print_quoted_string(FILE *out, const char *str)
623 const char *p;
624 int len;
626 putc('"', out);
627 while ((p = strchr(str, '"'))) {
628 len = p - str;
629 if (len)
630 fprintf(out, "%.*s", len, str);
631 fputs("\\\"", out);
632 str = p + 1;
634 fputs(str, out);
635 putc('"', out);
638 static void print_symbol(FILE *out, struct menu *menu)
640 struct symbol *sym = menu->sym;
641 struct property *prop;
643 if (sym_is_choice(sym))
644 fprintf(out, "\nchoice\n");
645 else
646 fprintf(out, "\nconfig %s\n", sym->name);
647 switch (sym->type) {
648 case S_BOOLEAN:
649 fputs(" boolean\n", out);
650 break;
651 case S_TRISTATE:
652 fputs(" tristate\n", out);
653 break;
654 case S_STRING:
655 fputs(" string\n", out);
656 break;
657 case S_INT:
658 fputs(" integer\n", out);
659 break;
660 case S_HEX:
661 fputs(" hex\n", out);
662 break;
663 default:
664 fputs(" ???\n", out);
665 break;
667 for (prop = sym->prop; prop; prop = prop->next) {
668 if (prop->menu != menu)
669 continue;
670 switch (prop->type) {
671 case P_PROMPT:
672 fputs(" prompt ", out);
673 print_quoted_string(out, prop->text);
674 if (!expr_is_yes(prop->visible.expr)) {
675 fputs(" if ", out);
676 expr_fprint(prop->visible.expr, out);
678 fputc('\n', out);
679 break;
680 case P_DEFAULT:
681 fputs( " default ", out);
682 expr_fprint(prop->expr, out);
683 if (!expr_is_yes(prop->visible.expr)) {
684 fputs(" if ", out);
685 expr_fprint(prop->visible.expr, out);
687 fputc('\n', out);
688 break;
689 case P_CHOICE:
690 fputs(" #choice value\n", out);
691 break;
692 case P_SELECT:
693 fputs( " select ", out);
694 expr_fprint(prop->expr, out);
695 fputc('\n', out);
696 break;
697 case P_IMPLY:
698 fputs( " imply ", out);
699 expr_fprint(prop->expr, out);
700 fputc('\n', out);
701 break;
702 case P_RANGE:
703 fputs( " range ", out);
704 expr_fprint(prop->expr, out);
705 fputc('\n', out);
706 break;
707 case P_MENU:
708 fputs( " menu ", out);
709 print_quoted_string(out, prop->text);
710 fputc('\n', out);
711 break;
712 default:
713 fprintf(out, " unknown prop %d!\n", prop->type);
714 break;
717 if (menu->help) {
718 int len = strlen(menu->help);
719 while (menu->help[--len] == '\n')
720 menu->help[len] = 0;
721 fprintf(out, " help\n%s\n", menu->help);
725 void zconfdump(FILE *out)
727 struct property *prop;
728 struct symbol *sym;
729 struct menu *menu;
731 menu = rootmenu.list;
732 while (menu) {
733 if ((sym = menu->sym))
734 print_symbol(out, menu);
735 else if ((prop = menu->prompt)) {
736 switch (prop->type) {
737 case P_COMMENT:
738 fputs("\ncomment ", out);
739 print_quoted_string(out, prop->text);
740 fputs("\n", out);
741 break;
742 case P_MENU:
743 fputs("\nmenu ", out);
744 print_quoted_string(out, prop->text);
745 fputs("\n", out);
746 break;
747 default:
750 if (!expr_is_yes(prop->visible.expr)) {
751 fputs(" depends ", out);
752 expr_fprint(prop->visible.expr, out);
753 fputc('\n', out);
757 if (menu->list)
758 menu = menu->list;
759 else if (menu->next)
760 menu = menu->next;
761 else while ((menu = menu->parent)) {
762 if (menu->prompt && menu->prompt->type == P_MENU)
763 fputs("\nendmenu\n", out);
764 if (menu->next) {
765 menu = menu->next;
766 break;
772 #include "zconf.lex.c"
773 #include "util.c"
774 #include "confdata.c"
775 #include "expr.c"
776 #include "symbol.c"
777 #include "menu.c"