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