Merge branch 'master' of ../OpenWRT.svn
[openwrt_comio.git] / scripts / config / zconf.y
blob9689a3ed80af076c206b8e4a57ea73443fb3e671
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 #define LKC_DIRECT_LINK
15 #include "lkc.h"
17 #include "zconf.hash.c"
19 #define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt)
21 #define PRINTD 0x0001
22 #define DEBUG_PARSE 0x0002
24 int cdebug = PRINTD;
26 extern int zconflex(void);
27 static void zconfprint(const char *err, ...);
28 static void zconf_error(const char *err, ...);
29 static void zconferror(const char *err);
30 static bool zconf_endtoken(struct kconf_id *id, int starttoken, int endtoken);
32 struct symbol *symbol_hash[257];
34 static struct menu *current_menu, *current_entry;
36 #define YYDEBUG 0
37 #if YYDEBUG
38 #define YYERROR_VERBOSE
39 #endif
41 %expect 26
43 %union
45 char *string;
46 struct file *file;
47 struct symbol *symbol;
48 struct expr *expr;
49 struct menu *menu;
50 struct kconf_id *id;
53 %token <id>T_MAINMENU
54 %token <id>T_MENU
55 %token <id>T_ENDMENU
56 %token <id>T_SOURCE
57 %token <id>T_CHOICE
58 %token <id>T_ENDCHOICE
59 %token <id>T_COMMENT
60 %token <id>T_CONFIG
61 %token <id>T_MENUCONFIG
62 %token <id>T_HELP
63 %token <string> T_HELPTEXT
64 %token <id>T_IF
65 %token <id>T_ENDIF
66 %token <id>T_DEPENDS
67 %token <id>T_REQUIRES
68 %token <id>T_OPTIONAL
69 %token <id>T_PROMPT
70 %token <id>T_TYPE
71 %token <id>T_DEFAULT
72 %token <id>T_DESELECT
73 %token <id>T_SELECT
74 %token <id>T_RANGE
75 %token <id>T_ON
76 %token <id>T_RESET
77 %token <string> T_WORD
78 %token <string> T_WORD_QUOTE
79 %token T_UNEQUAL
80 %token T_CLOSE_PAREN
81 %token T_OPEN_PAREN
82 %token T_EOL
84 %left T_OR
85 %left T_AND
86 %left T_EQUAL T_UNEQUAL
87 %nonassoc T_NOT
89 %type <string> prompt
90 %type <symbol> symbol
91 %type <expr> expr
92 %type <expr> if_expr
93 %type <id> end
94 %type <id> option_name
95 %type <menu> if_entry menu_entry choice_entry
97 %destructor {
98 fprintf(stderr, "%s:%d: missing end statement for this entry\n",
99 $$->file->name, $$->lineno);
100 if (current_menu == $$)
101 menu_end_menu();
102 } if_entry menu_entry choice_entry
105 input: stmt_list;
107 stmt_list:
108 /* empty */
109 | stmt_list common_stmt
110 | stmt_list choice_stmt
111 | stmt_list menu_stmt
112 | stmt_list T_MAINMENU prompt nl
113 | stmt_list end { zconf_error("unexpected end statement"); }
114 | stmt_list T_WORD error T_EOL { zconf_error("unknown statement \"%s\"", $2); }
115 | stmt_list option_name error T_EOL
117 zconf_error("unexpected option \"%s\"", kconf_id_strings + $2->name);
119 | stmt_list error T_EOL { zconf_error("invalid statement"); }
122 option_name:
123 T_DEPENDS | T_PROMPT | T_TYPE | T_DESELECT | T_SELECT | T_OPTIONAL | T_RANGE | T_DEFAULT | T_RESET
126 common_stmt:
127 T_EOL
128 | if_stmt
129 | comment_stmt
130 | config_stmt
131 | menuconfig_stmt
132 | source_stmt
135 option_error:
136 T_WORD error T_EOL { zconf_error("unknown option \"%s\"", $1); }
137 | error T_EOL { zconf_error("invalid option"); }
141 /* config/menuconfig entry */
143 config_entry_start: T_CONFIG T_WORD T_EOL
145 struct symbol *sym = sym_lookup($2, 0);
146 sym->flags |= SYMBOL_OPTIONAL;
147 menu_add_entry(sym);
148 printd(DEBUG_PARSE, "%s:%d:config %s\n", zconf_curname(), zconf_lineno(), $2);
151 config_stmt: config_entry_start config_option_list
153 menu_end_entry();
154 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
157 menuconfig_entry_start: T_MENUCONFIG T_WORD T_EOL
159 struct symbol *sym = sym_lookup($2, 0);
160 sym->flags |= SYMBOL_OPTIONAL;
161 menu_add_entry(sym);
162 printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", zconf_curname(), zconf_lineno(), $2);
165 menuconfig_stmt: menuconfig_entry_start config_option_list
167 if (current_entry->prompt)
168 current_entry->prompt->type = P_MENU;
169 else
170 zconfprint("warning: menuconfig statement without prompt");
171 menu_end_entry();
172 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
175 config_option_list:
176 /* empty */
177 | config_option_list config_option
178 | config_option_list depends
179 | config_option_list help
180 | config_option_list option_error
181 | config_option_list T_EOL
184 config_option: T_TYPE prompt_stmt_opt T_EOL
186 menu_set_type($1->stype);
187 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
188 zconf_curname(), zconf_lineno(),
189 $1->stype);
192 config_option: T_PROMPT prompt if_expr T_EOL
194 menu_add_prompt(P_PROMPT, $2, $3);
195 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
198 config_option: T_DEFAULT expr if_expr T_EOL
200 menu_add_expr(P_DEFAULT, $2, $3);
201 if ($1->stype != S_UNKNOWN)
202 menu_set_type($1->stype);
203 printd(DEBUG_PARSE, "%s:%d:default(%u)\n",
204 zconf_curname(), zconf_lineno(),
205 $1->stype);
208 config_option: T_DESELECT T_WORD if_expr T_EOL
210 menu_add_symbol(P_DESELECT, sym_lookup($2, 0), $3);
211 printd(DEBUG_PARSE, "%s:%d:deselect\n", zconf_curname(), zconf_lineno());
214 config_option: T_SELECT T_WORD if_expr T_EOL
216 menu_add_symbol(P_SELECT, sym_lookup($2, 0), $3);
217 printd(DEBUG_PARSE, "%s:%d:select\n", zconf_curname(), zconf_lineno());
220 config_option: T_RANGE symbol symbol if_expr T_EOL
222 menu_add_expr(P_RANGE, expr_alloc_comp(E_RANGE,$2, $3), $4);
223 printd(DEBUG_PARSE, "%s:%d:range\n", zconf_curname(), zconf_lineno());
226 /* choice entry */
228 choice: T_CHOICE T_EOL
230 struct symbol *sym = sym_lookup(NULL, 0);
231 sym->flags |= SYMBOL_CHOICE;
232 menu_add_entry(sym);
233 menu_add_expr(P_CHOICE, NULL, NULL);
234 printd(DEBUG_PARSE, "%s:%d:choice\n", zconf_curname(), zconf_lineno());
237 choice_entry: choice choice_option_list
239 $$ = menu_add_menu();
242 choice_end: end
244 if (zconf_endtoken($1, T_CHOICE, T_ENDCHOICE)) {
245 menu_end_menu();
246 printd(DEBUG_PARSE, "%s:%d:endchoice\n", zconf_curname(), zconf_lineno());
250 choice_stmt: choice_entry choice_block choice_end
253 choice_option_list:
254 /* empty */
255 | choice_option_list choice_option
256 | choice_option_list depends
257 | choice_option_list help
258 | choice_option_list T_EOL
259 | choice_option_list option_error
262 choice_option: T_PROMPT prompt if_expr T_EOL
264 menu_add_prompt(P_PROMPT, $2, $3);
265 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
268 choice_option: T_TYPE prompt_stmt_opt T_EOL
270 if ($1->stype == S_BOOLEAN || $1->stype == S_TRISTATE) {
271 menu_set_type($1->stype);
272 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
273 zconf_curname(), zconf_lineno(),
274 $1->stype);
275 } else
276 YYERROR;
279 choice_option: T_OPTIONAL T_EOL
281 current_entry->sym->flags |= SYMBOL_OPTIONAL;
282 printd(DEBUG_PARSE, "%s:%d:optional\n", zconf_curname(), zconf_lineno());
285 choice_option: T_RESET if_expr T_EOL
287 menu_add_prop(P_RESET, NULL, NULL, $2);
290 choice_option: T_DEFAULT T_WORD if_expr T_EOL
292 if ($1->stype == S_UNKNOWN) {
293 menu_add_symbol(P_DEFAULT, sym_lookup($2, 0), $3);
294 printd(DEBUG_PARSE, "%s:%d:default\n",
295 zconf_curname(), zconf_lineno());
296 } else
297 YYERROR;
300 choice_block:
301 /* empty */
302 | choice_block common_stmt
305 /* if entry */
307 if_entry: T_IF expr nl
309 printd(DEBUG_PARSE, "%s:%d:if\n", zconf_curname(), zconf_lineno());
310 menu_add_entry(NULL);
311 menu_add_dep($2);
312 $$ = menu_add_menu();
315 if_end: end
317 if (zconf_endtoken($1, T_IF, T_ENDIF)) {
318 menu_end_menu();
319 printd(DEBUG_PARSE, "%s:%d:endif\n", zconf_curname(), zconf_lineno());
323 if_stmt: if_entry if_block if_end
326 if_block:
327 /* empty */
328 | if_block common_stmt
329 | if_block menu_stmt
330 | if_block choice_stmt
333 /* menu entry */
335 menu: T_MENU prompt T_EOL
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());
342 menu_entry: menu depends_list
344 $$ = menu_add_menu();
347 menu_end: end
349 if (zconf_endtoken($1, T_MENU, T_ENDMENU)) {
350 menu_end_menu();
351 printd(DEBUG_PARSE, "%s:%d:endmenu\n", zconf_curname(), zconf_lineno());
355 menu_stmt: menu_entry menu_block menu_end
358 menu_block:
359 /* empty */
360 | menu_block common_stmt
361 | menu_block menu_stmt
362 | menu_block choice_stmt
365 source_stmt: T_SOURCE prompt T_EOL
367 printd(DEBUG_PARSE, "%s:%d:source %s\n", zconf_curname(), zconf_lineno(), $2);
368 zconf_nextfile($2);
371 /* comment entry */
373 comment: T_COMMENT prompt T_EOL
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());
380 comment_stmt: comment depends_list
382 menu_end_entry();
385 /* help option */
387 help_start: T_HELP T_EOL
389 printd(DEBUG_PARSE, "%s:%d:help\n", zconf_curname(), zconf_lineno());
390 zconf_starthelp();
393 help: help_start T_HELPTEXT
395 current_entry->sym->help = $2;
398 /* depends option */
400 depends_list:
401 /* empty */
402 | depends_list depends
403 | depends_list T_EOL
404 | depends_list option_error
407 depends: T_DEPENDS T_ON expr T_EOL
409 menu_add_dep($3);
410 printd(DEBUG_PARSE, "%s:%d:depends on\n", zconf_curname(), zconf_lineno());
412 | T_DEPENDS expr T_EOL
414 menu_add_dep($2);
415 printd(DEBUG_PARSE, "%s:%d:depends\n", zconf_curname(), zconf_lineno());
417 | T_REQUIRES expr T_EOL
419 menu_add_dep($2);
420 printd(DEBUG_PARSE, "%s:%d:requires\n", zconf_curname(), zconf_lineno());
423 /* prompt statement */
425 prompt_stmt_opt:
426 /* empty */
427 | prompt if_expr
429 menu_add_prompt(P_PROMPT, $1, $2);
432 prompt: T_WORD
433 | T_WORD_QUOTE
436 end: T_ENDMENU T_EOL { $$ = $1; }
437 | T_ENDCHOICE T_EOL { $$ = $1; }
438 | T_ENDIF T_EOL { $$ = $1; }
442 T_EOL
443 | nl T_EOL
446 if_expr: /* empty */ { $$ = NULL; }
447 | T_IF expr { $$ = $2; }
450 expr: symbol { $$ = expr_alloc_symbol($1); }
451 | symbol T_EQUAL symbol { $$ = expr_alloc_comp(E_EQUAL, $1, $3); }
452 | symbol T_UNEQUAL symbol { $$ = expr_alloc_comp(E_UNEQUAL, $1, $3); }
453 | T_OPEN_PAREN expr T_CLOSE_PAREN { $$ = $2; }
454 | T_NOT expr { $$ = expr_alloc_one(E_NOT, $2); }
455 | expr T_OR expr { $$ = expr_alloc_two(E_OR, $1, $3); }
456 | expr T_AND expr { $$ = expr_alloc_two(E_AND, $1, $3); }
459 symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); }
460 | T_WORD_QUOTE { $$ = sym_lookup($1, 1); free($1); }
465 void conf_parse(const char *name)
467 struct symbol *sym;
468 int i;
470 zconf_initscan(name);
472 sym_init();
473 menu_init();
474 modules_sym = sym_lookup("MODULES", 0);
475 rootmenu.prompt = menu_add_prompt(P_MENU, "OpenWrt Configuration", NULL);
477 #if YYDEBUG
478 if (getenv("ZCONF_DEBUG"))
479 zconfdebug = 1;
480 #endif
481 zconfparse();
482 if (zconfnerrs)
483 exit(1);
484 menu_finalize(&rootmenu);
485 for_all_symbols(i, sym) {
486 sym_check_deps(sym);
489 sym_change_count = 1;
492 const char *zconf_tokenname(int token)
494 switch (token) {
495 case T_MENU: return "menu";
496 case T_ENDMENU: return "endmenu";
497 case T_CHOICE: return "choice";
498 case T_ENDCHOICE: return "endchoice";
499 case T_IF: return "if";
500 case T_ENDIF: return "endif";
501 case T_DEPENDS: return "depends";
503 return "<token>";
506 static bool zconf_endtoken(struct kconf_id *id, int starttoken, int endtoken)
508 if (id->token != endtoken) {
509 zconf_error("unexpected '%s' within %s block",
510 kconf_id_strings + id->name, zconf_tokenname(starttoken));
511 zconfnerrs++;
512 return false;
514 if (current_menu->file != current_file) {
515 zconf_error("'%s' in different file than '%s'",
516 kconf_id_strings + id->name, zconf_tokenname(starttoken));
517 fprintf(stderr, "%s:%d: location of the '%s'\n",
518 current_menu->file->name, current_menu->lineno,
519 zconf_tokenname(starttoken));
520 zconfnerrs++;
521 return false;
523 return true;
526 static void zconfprint(const char *err, ...)
528 va_list ap;
530 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
531 va_start(ap, err);
532 vfprintf(stderr, err, ap);
533 va_end(ap);
534 fprintf(stderr, "\n");
537 static void zconf_error(const char *err, ...)
539 va_list ap;
541 zconfnerrs++;
542 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
543 va_start(ap, err);
544 vfprintf(stderr, err, ap);
545 va_end(ap);
546 fprintf(stderr, "\n");
549 static void zconferror(const char *err)
551 #if YYDEBUG
552 fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err);
553 #endif
556 void print_quoted_string(FILE *out, const char *str)
558 const char *p;
559 int len;
561 putc('"', out);
562 while ((p = strchr(str, '"'))) {
563 len = p - str;
564 if (len)
565 fprintf(out, "%.*s", len, str);
566 fputs("\\\"", out);
567 str = p + 1;
569 fputs(str, out);
570 putc('"', out);
573 void print_symbol(FILE *out, struct menu *menu)
575 struct symbol *sym = menu->sym;
576 struct property *prop;
578 if (sym_is_choice(sym))
579 fprintf(out, "choice\n");
580 else
581 fprintf(out, "config %s\n", sym->name);
582 switch (sym->type) {
583 case S_BOOLEAN:
584 fputs(" boolean\n", out);
585 break;
586 case S_TRISTATE:
587 fputs(" tristate\n", out);
588 break;
589 case S_STRING:
590 fputs(" string\n", out);
591 break;
592 case S_INT:
593 fputs(" integer\n", out);
594 break;
595 case S_HEX:
596 fputs(" hex\n", out);
597 break;
598 default:
599 fputs(" ???\n", out);
600 break;
602 for (prop = sym->prop; prop; prop = prop->next) {
603 if (prop->menu != menu)
604 continue;
605 switch (prop->type) {
606 case P_PROMPT:
607 fputs(" prompt ", out);
608 print_quoted_string(out, prop->text);
609 if (!expr_is_yes(prop->visible.expr)) {
610 fputs(" if ", out);
611 expr_fprint(prop->visible.expr, out);
613 fputc('\n', out);
614 break;
615 case P_DEFAULT:
616 fputs( " default ", out);
617 expr_fprint(prop->expr, out);
618 if (!expr_is_yes(prop->visible.expr)) {
619 fputs(" if ", out);
620 expr_fprint(prop->visible.expr, out);
622 fputc('\n', out);
623 break;
624 case P_CHOICE:
625 fputs(" #choice value\n", out);
626 break;
627 default:
628 fprintf(out, " unknown prop %d!\n", prop->type);
629 break;
632 if (sym->help) {
633 int len = strlen(sym->help);
634 while (sym->help[--len] == '\n')
635 sym->help[len] = 0;
636 fprintf(out, " help\n%s\n", sym->help);
638 fputc('\n', out);
641 void zconfdump(FILE *out)
643 struct property *prop;
644 struct symbol *sym;
645 struct menu *menu;
647 menu = rootmenu.list;
648 while (menu) {
649 if ((sym = menu->sym))
650 print_symbol(out, menu);
651 else if ((prop = menu->prompt)) {
652 switch (prop->type) {
653 case P_COMMENT:
654 fputs("\ncomment ", out);
655 print_quoted_string(out, prop->text);
656 fputs("\n", out);
657 break;
658 case P_MENU:
659 fputs("\nmenu ", out);
660 print_quoted_string(out, prop->text);
661 fputs("\n", out);
662 break;
663 default:
666 if (!expr_is_yes(prop->visible.expr)) {
667 fputs(" depends ", out);
668 expr_fprint(prop->visible.expr, out);
669 fputc('\n', out);
671 fputs("\n", out);
674 if (menu->list)
675 menu = menu->list;
676 else if (menu->next)
677 menu = menu->next;
678 else while ((menu = menu->parent)) {
679 if (menu->prompt && menu->prompt->type == P_MENU)
680 fputs("\nendmenu\n", out);
681 if (menu->next) {
682 menu = menu->next;
683 break;
689 #include "lex.zconf.c"
690 #include "util.c"
691 #include "confdata.c"
692 #include "expr.c"
693 #include "symbol.c"
694 #include "menu.c"