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