ar71xx: enable wlan2g led in the default configuration of dir-825-c1
[openwrt/svn-archive/archive.git] / scripts / config / zconf.y
1 %{
2 /*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 * Released under the terms of the GNU GPL v2.0.
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
16 #define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt)
17
18 #define PRINTD 0x0001
19 #define DEBUG_PARSE 0x0002
20
21 int cdebug = PRINTD;
22
23 extern int zconflex(void);
24 static void zconfprint(const char *err, ...);
25 static void zconf_error(const char *err, ...);
26 static void zconferror(const char *err);
27 static bool zconf_endtoken(const struct kconf_id *id, int starttoken, int endtoken);
28
29 struct symbol *symbol_hash[SYMBOL_HASHSIZE];
30
31 static struct menu *current_menu, *current_entry;
32
33 %}
34 %expect 30
35
36 %union
37 {
38 char *string;
39 struct file *file;
40 struct symbol *symbol;
41 struct expr *expr;
42 struct menu *menu;
43 const struct kconf_id *id;
44 }
45
46 %token <id>T_MAINMENU
47 %token <id>T_MENU
48 %token <id>T_ENDMENU
49 %token <id>T_SOURCE
50 %token <id>T_CHOICE
51 %token <id>T_ENDCHOICE
52 %token <id>T_COMMENT
53 %token <id>T_CONFIG
54 %token <id>T_MENUCONFIG
55 %token <id>T_HELP
56 %token <string> T_HELPTEXT
57 %token <id>T_IF
58 %token <id>T_ENDIF
59 %token <id>T_DEPENDS
60 %token <id>T_OPTIONAL
61 %token <id>T_PROMPT
62 %token <id>T_TYPE
63 %token <id>T_DEFAULT
64 %token <id>T_SELECT
65 %token <id>T_RANGE
66 %token <id>T_VISIBLE
67 %token <id>T_OPTION
68 %token <id>T_ON
69 %token <id>T_RESET
70 %token <string> T_WORD
71 %token <string> T_WORD_QUOTE
72 %token T_UNEQUAL
73 %token T_CLOSE_PAREN
74 %token T_OPEN_PAREN
75 %token T_EOL
76
77 %left T_OR
78 %left T_AND
79 %left T_EQUAL T_UNEQUAL
80 %nonassoc T_NOT
81
82 %type <string> prompt
83 %type <symbol> symbol
84 %type <expr> expr
85 %type <expr> if_expr
86 %type <id> end
87 %type <id> option_name
88 %type <menu> if_entry menu_entry choice_entry
89 %type <string> symbol_option_arg word_opt
90
91 %destructor {
92 fprintf(stderr, "%s:%d: missing end statement for this entry\n",
93 $$->file->name, $$->lineno);
94 if (current_menu == $$)
95 menu_end_menu();
96 } if_entry menu_entry choice_entry
97
98 %{
99 /* Include zconf.hash.c here so it can see the token constants. */
100 #include "zconf.hash.c"
101 %}
102
103 %%
104 input: nl start | start;
105
106 start: mainmenu_stmt stmt_list | stmt_list;
107
108 stmt_list:
109 /* empty */
110 | stmt_list common_stmt
111 | stmt_list choice_stmt
112 | stmt_list menu_stmt
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
116 {
117 zconf_error("unexpected option \"%s\"", kconf_id_strings + $2->name);
118 }
119 | stmt_list error T_EOL { zconf_error("invalid statement"); }
120 ;
121
122 option_name:
123 T_DEPENDS | T_PROMPT | T_TYPE | T_SELECT | T_OPTIONAL | T_RANGE | T_DEFAULT | T_VISIBLE | T_RESET
124 ;
125
126 common_stmt:
127 T_EOL
128 | if_stmt
129 | comment_stmt
130 | config_stmt
131 | menuconfig_stmt
132 | source_stmt
133 ;
134
135 option_error:
136 T_WORD error T_EOL { zconf_error("unknown option \"%s\"", $1); }
137 | error T_EOL { zconf_error("invalid option"); }
138 ;
139
140
141 /* config/menuconfig entry */
142
143 config_entry_start: T_CONFIG T_WORD T_EOL
144 {
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);
149 };
150
151 config_stmt: config_entry_start config_option_list
152 {
153 menu_end_entry();
154 printd(DEBUG_PARSE, "%s:%d:endconfig\n", zconf_curname(), zconf_lineno());
155 };
156
157 menuconfig_entry_start: T_MENUCONFIG T_WORD T_EOL
158 {
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);
163 };
164
165 menuconfig_stmt: menuconfig_entry_start config_option_list
166 {
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());
173 };
174
175 config_option_list:
176 /* empty */
177 | config_option_list config_option
178 | config_option_list symbol_option
179 | config_option_list depends
180 | config_option_list help
181 | config_option_list option_error
182 | config_option_list T_EOL
183 ;
184
185 config_option: T_TYPE prompt_stmt_opt T_EOL
186 {
187 menu_set_type($1->stype);
188 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
189 zconf_curname(), zconf_lineno(),
190 $1->stype);
191 };
192
193 config_option: T_PROMPT prompt if_expr T_EOL
194 {
195 menu_add_prompt(P_PROMPT, $2, $3);
196 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
197 };
198
199 config_option: T_DEFAULT expr if_expr T_EOL
200 {
201 menu_add_expr(P_DEFAULT, $2, $3);
202 if ($1->stype != S_UNKNOWN)
203 menu_set_type($1->stype);
204 printd(DEBUG_PARSE, "%s:%d:default(%u)\n",
205 zconf_curname(), zconf_lineno(),
206 $1->stype);
207 };
208
209 config_option: T_SELECT T_WORD if_expr T_EOL
210 {
211 menu_add_symbol(P_SELECT, sym_lookup($2, 0), $3);
212 printd(DEBUG_PARSE, "%s:%d:select\n", zconf_curname(), zconf_lineno());
213 };
214
215 config_option: T_RANGE symbol symbol if_expr T_EOL
216 {
217 menu_add_expr(P_RANGE, expr_alloc_comp(E_RANGE,$2, $3), $4);
218 printd(DEBUG_PARSE, "%s:%d:range\n", zconf_curname(), zconf_lineno());
219 };
220
221 symbol_option: T_OPTION symbol_option_list T_EOL
222 ;
223
224 symbol_option_list:
225 /* empty */
226 | symbol_option_list T_WORD symbol_option_arg
227 {
228 const struct kconf_id *id = kconf_id_lookup($2, strlen($2));
229 if (id && id->flags & TF_OPTION)
230 menu_add_option(id->token, $3);
231 else
232 zconfprint("warning: ignoring unknown option %s", $2);
233 free($2);
234 };
235
236 symbol_option_arg:
237 /* empty */ { $$ = NULL; }
238 | T_EQUAL prompt { $$ = $2; }
239 ;
240
241 /* choice entry */
242
243 choice: T_CHOICE word_opt T_EOL
244 {
245 struct symbol *sym = sym_lookup($2, SYMBOL_CHOICE);
246 sym->flags |= SYMBOL_AUTO;
247 menu_add_entry(sym);
248 menu_add_expr(P_CHOICE, NULL, NULL);
249 printd(DEBUG_PARSE, "%s:%d:choice\n", zconf_curname(), zconf_lineno());
250 };
251
252 choice_entry: choice choice_option_list
253 {
254 $$ = menu_add_menu();
255 };
256
257 choice_end: end
258 {
259 if (zconf_endtoken($1, T_CHOICE, T_ENDCHOICE)) {
260 menu_end_menu();
261 printd(DEBUG_PARSE, "%s:%d:endchoice\n", zconf_curname(), zconf_lineno());
262 }
263 };
264
265 choice_stmt: choice_entry choice_block choice_end
266 ;
267
268 choice_option_list:
269 /* empty */
270 | choice_option_list choice_option
271 | choice_option_list depends
272 | choice_option_list help
273 | choice_option_list T_EOL
274 | choice_option_list option_error
275 ;
276
277 choice_option: T_PROMPT prompt if_expr T_EOL
278 {
279 menu_add_prompt(P_PROMPT, $2, $3);
280 printd(DEBUG_PARSE, "%s:%d:prompt\n", zconf_curname(), zconf_lineno());
281 };
282
283 choice_option: T_TYPE prompt_stmt_opt T_EOL
284 {
285 if ($1->stype == S_BOOLEAN || $1->stype == S_TRISTATE) {
286 menu_set_type($1->stype);
287 printd(DEBUG_PARSE, "%s:%d:type(%u)\n",
288 zconf_curname(), zconf_lineno(),
289 $1->stype);
290 } else
291 YYERROR;
292 };
293
294 choice_option: T_OPTIONAL T_EOL
295 {
296 current_entry->sym->flags |= SYMBOL_OPTIONAL;
297 printd(DEBUG_PARSE, "%s:%d:optional\n", zconf_curname(), zconf_lineno());
298 };
299
300 choice_option: T_RESET if_expr T_EOL
301 {
302 menu_add_prop(P_RESET, NULL, NULL, $2);
303 };
304
305 choice_option: T_DEFAULT T_WORD if_expr T_EOL
306 {
307 if ($1->stype == S_UNKNOWN) {
308 menu_add_symbol(P_DEFAULT, sym_lookup($2, 0), $3);
309 printd(DEBUG_PARSE, "%s:%d:default\n",
310 zconf_curname(), zconf_lineno());
311 } else
312 YYERROR;
313 };
314
315 choice_block:
316 /* empty */
317 | choice_block common_stmt
318 ;
319
320 /* if entry */
321
322 if_entry: T_IF expr nl
323 {
324 printd(DEBUG_PARSE, "%s:%d:if\n", zconf_curname(), zconf_lineno());
325 menu_add_entry(NULL);
326 menu_add_dep($2);
327 $$ = menu_add_menu();
328 };
329
330 if_end: end
331 {
332 if (zconf_endtoken($1, T_IF, T_ENDIF)) {
333 menu_end_menu();
334 printd(DEBUG_PARSE, "%s:%d:endif\n", zconf_curname(), zconf_lineno());
335 }
336 };
337
338 if_stmt: if_entry if_block if_end
339 ;
340
341 if_block:
342 /* empty */
343 | if_block common_stmt
344 | if_block menu_stmt
345 | if_block choice_stmt
346 ;
347
348 /* mainmenu entry */
349
350 mainmenu_stmt: T_MAINMENU prompt nl
351 {
352 menu_add_prompt(P_MENU, $2, NULL);
353 };
354
355 /* menu entry */
356
357 menu: T_MENU prompt T_EOL
358 {
359 menu_add_entry(NULL);
360 menu_add_prompt(P_MENU, $2, NULL);
361 printd(DEBUG_PARSE, "%s:%d:menu\n", zconf_curname(), zconf_lineno());
362 };
363
364 menu_entry: menu visibility_list depends_list
365 {
366 $$ = menu_add_menu();
367 };
368
369 menu_end: end
370 {
371 if (zconf_endtoken($1, T_MENU, T_ENDMENU)) {
372 menu_end_menu();
373 printd(DEBUG_PARSE, "%s:%d:endmenu\n", zconf_curname(), zconf_lineno());
374 }
375 };
376
377 menu_stmt: menu_entry menu_block menu_end
378 ;
379
380 menu_block:
381 /* empty */
382 | menu_block common_stmt
383 | menu_block menu_stmt
384 | menu_block choice_stmt
385 ;
386
387 source_stmt: T_SOURCE prompt T_EOL
388 {
389 printd(DEBUG_PARSE, "%s:%d:source %s\n", zconf_curname(), zconf_lineno(), $2);
390 zconf_nextfile($2);
391 };
392
393 /* comment entry */
394
395 comment: T_COMMENT prompt T_EOL
396 {
397 menu_add_entry(NULL);
398 menu_add_prompt(P_COMMENT, $2, NULL);
399 printd(DEBUG_PARSE, "%s:%d:comment\n", zconf_curname(), zconf_lineno());
400 };
401
402 comment_stmt: comment depends_list
403 {
404 menu_end_entry();
405 };
406
407 /* help option */
408
409 help_start: T_HELP T_EOL
410 {
411 printd(DEBUG_PARSE, "%s:%d:help\n", zconf_curname(), zconf_lineno());
412 zconf_starthelp();
413 };
414
415 help: help_start T_HELPTEXT
416 {
417 current_entry->help = $2;
418 };
419
420 /* depends option */
421
422 depends_list:
423 /* empty */
424 | depends_list depends
425 | depends_list T_EOL
426 | depends_list option_error
427 ;
428
429 depends: T_DEPENDS T_ON expr T_EOL
430 {
431 menu_add_dep($3);
432 printd(DEBUG_PARSE, "%s:%d:depends on\n", zconf_curname(), zconf_lineno());
433 } | T_DEPENDS expr T_EOL
434 {
435 menu_add_dep($2);
436 zconfprint("warning: deprecated 'depends' syntax, use 'depends on' instead.");
437 };
438
439 /* visibility option */
440
441 visibility_list:
442 /* empty */
443 | visibility_list visible
444 | visibility_list T_EOL
445 ;
446
447 visible: T_VISIBLE if_expr
448 {
449 menu_add_visibility($2);
450 };
451
452 /* prompt statement */
453
454 prompt_stmt_opt:
455 /* empty */
456 | prompt if_expr
457 {
458 menu_add_prompt(P_PROMPT, $1, $2);
459 };
460
461 prompt: T_WORD
462 | T_WORD_QUOTE
463 ;
464
465 end: T_ENDMENU T_EOL { $$ = $1; }
466 | T_ENDCHOICE T_EOL { $$ = $1; }
467 | T_ENDIF T_EOL { $$ = $1; }
468 ;
469
470 nl:
471 T_EOL
472 | nl T_EOL
473 ;
474
475 if_expr: /* empty */ { $$ = NULL; }
476 | T_IF expr { $$ = $2; }
477 ;
478
479 expr: symbol { $$ = expr_alloc_symbol($1); }
480 | symbol T_EQUAL symbol { $$ = expr_alloc_comp(E_EQUAL, $1, $3); }
481 | symbol T_UNEQUAL symbol { $$ = expr_alloc_comp(E_UNEQUAL, $1, $3); }
482 | T_OPEN_PAREN expr T_CLOSE_PAREN { $$ = $2; }
483 | T_NOT expr { $$ = expr_alloc_one(E_NOT, $2); }
484 | expr T_OR expr { $$ = expr_alloc_two(E_OR, $1, $3); }
485 | expr T_AND expr { $$ = expr_alloc_two(E_AND, $1, $3); }
486 ;
487
488 symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); }
489 | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); }
490 ;
491
492 word_opt: /* empty */ { $$ = NULL; }
493 | T_WORD
494
495 %%
496
497 void conf_parse(const char *name)
498 {
499 struct symbol *sym;
500 int i;
501
502 zconf_initscan(name);
503
504 sym_init();
505 _menu_init();
506 modules_sym = sym_lookup(NULL, 0);
507 modules_sym->type = S_BOOLEAN;
508 modules_sym->flags |= SYMBOL_AUTO;
509 rootmenu.prompt = menu_add_prompt(P_MENU, "Linux Kernel Configuration", NULL);
510
511 #if YYDEBUG
512 if (getenv("ZCONF_DEBUG"))
513 zconfdebug = 1;
514 #endif
515 zconfparse();
516 if (zconfnerrs)
517 exit(1);
518 if (!modules_sym->prop) {
519 struct property *prop;
520
521 prop = prop_alloc(P_DEFAULT, modules_sym);
522 prop->expr = expr_alloc_symbol(sym_lookup("MODULES", 0));
523 }
524
525 rootmenu.prompt->text = _(rootmenu.prompt->text);
526 rootmenu.prompt->text = sym_expand_string_value(rootmenu.prompt->text);
527
528 menu_finalize(&rootmenu);
529 for_all_symbols(i, sym) {
530 if (sym_check_deps(sym))
531 zconfnerrs++;
532 }
533 if (zconfnerrs)
534 exit(1);
535 sym_set_change_count(1);
536 }
537
538 static const char *zconf_tokenname(int token)
539 {
540 switch (token) {
541 case T_MENU: return "menu";
542 case T_ENDMENU: return "endmenu";
543 case T_CHOICE: return "choice";
544 case T_ENDCHOICE: return "endchoice";
545 case T_IF: return "if";
546 case T_ENDIF: return "endif";
547 case T_DEPENDS: return "depends";
548 case T_VISIBLE: return "visible";
549 }
550 return "<token>";
551 }
552
553 static bool zconf_endtoken(const struct kconf_id *id, int starttoken, int endtoken)
554 {
555 if (id->token != endtoken) {
556 zconf_error("unexpected '%s' within %s block",
557 kconf_id_strings + id->name, zconf_tokenname(starttoken));
558 zconfnerrs++;
559 return false;
560 }
561 if (current_menu->file != current_file) {
562 zconf_error("'%s' in different file than '%s'",
563 kconf_id_strings + id->name, zconf_tokenname(starttoken));
564 fprintf(stderr, "%s:%d: location of the '%s'\n",
565 current_menu->file->name, current_menu->lineno,
566 zconf_tokenname(starttoken));
567 zconfnerrs++;
568 return false;
569 }
570 return true;
571 }
572
573 static void zconfprint(const char *err, ...)
574 {
575 va_list ap;
576
577 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
578 va_start(ap, err);
579 vfprintf(stderr, err, ap);
580 va_end(ap);
581 fprintf(stderr, "\n");
582 }
583
584 static void zconf_error(const char *err, ...)
585 {
586 va_list ap;
587
588 zconfnerrs++;
589 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
590 va_start(ap, err);
591 vfprintf(stderr, err, ap);
592 va_end(ap);
593 fprintf(stderr, "\n");
594 }
595
596 static void zconferror(const char *err)
597 {
598 fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err);
599 }
600
601 static void print_quoted_string(FILE *out, const char *str)
602 {
603 const char *p;
604 int len;
605
606 putc('"', out);
607 while ((p = strchr(str, '"'))) {
608 len = p - str;
609 if (len)
610 fprintf(out, "%.*s", len, str);
611 fputs("\\\"", out);
612 str = p + 1;
613 }
614 fputs(str, out);
615 putc('"', out);
616 }
617
618 static void print_symbol(FILE *out, struct menu *menu)
619 {
620 struct symbol *sym = menu->sym;
621 struct property *prop;
622
623 if (sym_is_choice(sym))
624 fprintf(out, "\nchoice\n");
625 else
626 fprintf(out, "\nconfig %s\n", sym->name);
627 switch (sym->type) {
628 case S_BOOLEAN:
629 fputs(" boolean\n", out);
630 break;
631 case S_TRISTATE:
632 fputs(" tristate\n", out);
633 break;
634 case S_STRING:
635 fputs(" string\n", out);
636 break;
637 case S_INT:
638 fputs(" integer\n", out);
639 break;
640 case S_HEX:
641 fputs(" hex\n", out);
642 break;
643 default:
644 fputs(" ???\n", out);
645 break;
646 }
647 for (prop = sym->prop; prop; prop = prop->next) {
648 if (prop->menu != menu)
649 continue;
650 switch (prop->type) {
651 case P_PROMPT:
652 fputs(" prompt ", out);
653 print_quoted_string(out, prop->text);
654 if (!expr_is_yes(prop->visible.expr)) {
655 fputs(" if ", out);
656 expr_fprint(prop->visible.expr, out);
657 }
658 fputc('\n', out);
659 break;
660 case P_DEFAULT:
661 fputs( " default ", out);
662 expr_fprint(prop->expr, out);
663 if (!expr_is_yes(prop->visible.expr)) {
664 fputs(" if ", out);
665 expr_fprint(prop->visible.expr, out);
666 }
667 fputc('\n', out);
668 break;
669 case P_CHOICE:
670 fputs(" #choice value\n", out);
671 break;
672 case P_SELECT:
673 fputs( " select ", out);
674 expr_fprint(prop->expr, out);
675 fputc('\n', out);
676 break;
677 case P_RANGE:
678 fputs( " range ", out);
679 expr_fprint(prop->expr, out);
680 fputc('\n', out);
681 break;
682 case P_MENU:
683 fputs( " menu ", out);
684 print_quoted_string(out, prop->text);
685 fputc('\n', out);
686 break;
687 default:
688 fprintf(out, " unknown prop %d!\n", prop->type);
689 break;
690 }
691 }
692 if (menu->help) {
693 int len = strlen(menu->help);
694 while (menu->help[--len] == '\n')
695 menu->help[len] = 0;
696 fprintf(out, " help\n%s\n", menu->help);
697 }
698 }
699
700 void zconfdump(FILE *out)
701 {
702 struct property *prop;
703 struct symbol *sym;
704 struct menu *menu;
705
706 menu = rootmenu.list;
707 while (menu) {
708 if ((sym = menu->sym))
709 print_symbol(out, menu);
710 else if ((prop = menu->prompt)) {
711 switch (prop->type) {
712 case P_COMMENT:
713 fputs("\ncomment ", out);
714 print_quoted_string(out, prop->text);
715 fputs("\n", out);
716 break;
717 case P_MENU:
718 fputs("\nmenu ", out);
719 print_quoted_string(out, prop->text);
720 fputs("\n", out);
721 break;
722 default:
723 ;
724 }
725 if (!expr_is_yes(prop->visible.expr)) {
726 fputs(" depends ", out);
727 expr_fprint(prop->visible.expr, out);
728 fputc('\n', out);
729 }
730 }
731
732 if (menu->list)
733 menu = menu->list;
734 else if (menu->next)
735 menu = menu->next;
736 else while ((menu = menu->parent)) {
737 if (menu->prompt && menu->prompt->type == P_MENU)
738 fputs("\nendmenu\n", out);
739 if (menu->next) {
740 menu = menu->next;
741 break;
742 }
743 }
744 }
745 }
746
747 #include "zconf.lex.c"
748 #include "util.c"
749 #include "confdata.c"
750 #include "expr.c"
751 #include "symbol.c"
752 #include "menu.c"