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