kernel: add missing symbol for 4.4
[openwrt/openwrt.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 rootmenu.prompt = menu_add_prompt(P_MENU, "Linux Kernel Configuration", NULL);
507
508 #if YYDEBUG
509 if (getenv("ZCONF_DEBUG"))
510 zconfdebug = 1;
511 #endif
512 zconfparse();
513 if (zconfnerrs)
514 exit(1);
515 if (!modules_sym)
516 modules_sym = sym_find( "n" );
517
518 rootmenu.prompt->text = _(rootmenu.prompt->text);
519 rootmenu.prompt->text = sym_expand_string_value(rootmenu.prompt->text);
520
521 menu_finalize(&rootmenu);
522 for_all_symbols(i, sym) {
523 if (sym_check_deps(sym))
524 zconfnerrs++;
525 }
526 if (zconfnerrs)
527 exit(1);
528 sym_set_change_count(1);
529 }
530
531 static const char *zconf_tokenname(int token)
532 {
533 switch (token) {
534 case T_MENU: return "menu";
535 case T_ENDMENU: return "endmenu";
536 case T_CHOICE: return "choice";
537 case T_ENDCHOICE: return "endchoice";
538 case T_IF: return "if";
539 case T_ENDIF: return "endif";
540 case T_DEPENDS: return "depends";
541 case T_VISIBLE: return "visible";
542 }
543 return "<token>";
544 }
545
546 static bool zconf_endtoken(const struct kconf_id *id, int starttoken, int endtoken)
547 {
548 if (id->token != endtoken) {
549 zconf_error("unexpected '%s' within %s block",
550 kconf_id_strings + id->name, zconf_tokenname(starttoken));
551 zconfnerrs++;
552 return false;
553 }
554 if (current_menu->file != current_file) {
555 zconf_error("'%s' in different file than '%s'",
556 kconf_id_strings + id->name, zconf_tokenname(starttoken));
557 fprintf(stderr, "%s:%d: location of the '%s'\n",
558 current_menu->file->name, current_menu->lineno,
559 zconf_tokenname(starttoken));
560 zconfnerrs++;
561 return false;
562 }
563 return true;
564 }
565
566 static void zconfprint(const char *err, ...)
567 {
568 va_list ap;
569
570 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
571 va_start(ap, err);
572 vfprintf(stderr, err, ap);
573 va_end(ap);
574 fprintf(stderr, "\n");
575 }
576
577 static void zconf_error(const char *err, ...)
578 {
579 va_list ap;
580
581 zconfnerrs++;
582 fprintf(stderr, "%s:%d: ", zconf_curname(), zconf_lineno());
583 va_start(ap, err);
584 vfprintf(stderr, err, ap);
585 va_end(ap);
586 fprintf(stderr, "\n");
587 }
588
589 static void zconferror(const char *err)
590 {
591 fprintf(stderr, "%s:%d: %s\n", zconf_curname(), zconf_lineno() + 1, err);
592 }
593
594 static void print_quoted_string(FILE *out, const char *str)
595 {
596 const char *p;
597 int len;
598
599 putc('"', out);
600 while ((p = strchr(str, '"'))) {
601 len = p - str;
602 if (len)
603 fprintf(out, "%.*s", len, str);
604 fputs("\\\"", out);
605 str = p + 1;
606 }
607 fputs(str, out);
608 putc('"', out);
609 }
610
611 static void print_symbol(FILE *out, struct menu *menu)
612 {
613 struct symbol *sym = menu->sym;
614 struct property *prop;
615
616 if (sym_is_choice(sym))
617 fprintf(out, "\nchoice\n");
618 else
619 fprintf(out, "\nconfig %s\n", sym->name);
620 switch (sym->type) {
621 case S_BOOLEAN:
622 fputs(" boolean\n", out);
623 break;
624 case S_TRISTATE:
625 fputs(" tristate\n", out);
626 break;
627 case S_STRING:
628 fputs(" string\n", out);
629 break;
630 case S_INT:
631 fputs(" integer\n", out);
632 break;
633 case S_HEX:
634 fputs(" hex\n", out);
635 break;
636 default:
637 fputs(" ???\n", out);
638 break;
639 }
640 for (prop = sym->prop; prop; prop = prop->next) {
641 if (prop->menu != menu)
642 continue;
643 switch (prop->type) {
644 case P_PROMPT:
645 fputs(" prompt ", out);
646 print_quoted_string(out, prop->text);
647 if (!expr_is_yes(prop->visible.expr)) {
648 fputs(" if ", out);
649 expr_fprint(prop->visible.expr, out);
650 }
651 fputc('\n', out);
652 break;
653 case P_DEFAULT:
654 fputs( " default ", out);
655 expr_fprint(prop->expr, out);
656 if (!expr_is_yes(prop->visible.expr)) {
657 fputs(" if ", out);
658 expr_fprint(prop->visible.expr, out);
659 }
660 fputc('\n', out);
661 break;
662 case P_CHOICE:
663 fputs(" #choice value\n", out);
664 break;
665 case P_SELECT:
666 fputs( " select ", out);
667 expr_fprint(prop->expr, out);
668 fputc('\n', out);
669 break;
670 case P_RANGE:
671 fputs( " range ", out);
672 expr_fprint(prop->expr, out);
673 fputc('\n', out);
674 break;
675 case P_MENU:
676 fputs( " menu ", out);
677 print_quoted_string(out, prop->text);
678 fputc('\n', out);
679 break;
680 default:
681 fprintf(out, " unknown prop %d!\n", prop->type);
682 break;
683 }
684 }
685 if (menu->help) {
686 int len = strlen(menu->help);
687 while (menu->help[--len] == '\n')
688 menu->help[len] = 0;
689 fprintf(out, " help\n%s\n", menu->help);
690 }
691 }
692
693 void zconfdump(FILE *out)
694 {
695 struct property *prop;
696 struct symbol *sym;
697 struct menu *menu;
698
699 menu = rootmenu.list;
700 while (menu) {
701 if ((sym = menu->sym))
702 print_symbol(out, menu);
703 else if ((prop = menu->prompt)) {
704 switch (prop->type) {
705 case P_COMMENT:
706 fputs("\ncomment ", out);
707 print_quoted_string(out, prop->text);
708 fputs("\n", out);
709 break;
710 case P_MENU:
711 fputs("\nmenu ", out);
712 print_quoted_string(out, prop->text);
713 fputs("\n", out);
714 break;
715 default:
716 ;
717 }
718 if (!expr_is_yes(prop->visible.expr)) {
719 fputs(" depends ", out);
720 expr_fprint(prop->visible.expr, out);
721 fputc('\n', out);
722 }
723 }
724
725 if (menu->list)
726 menu = menu->list;
727 else if (menu->next)
728 menu = menu->next;
729 else while ((menu = menu->parent)) {
730 if (menu->prompt && menu->prompt->type == P_MENU)
731 fputs("\nendmenu\n", out);
732 if (menu->next) {
733 menu = menu->next;
734 break;
735 }
736 }
737 }
738 }
739
740 #include "zconf.lex.c"
741 #include "util.c"
742 #include "confdata.c"
743 #include "expr.c"
744 #include "symbol.c"
745 #include "menu.c"