3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 * Released under the terms of the GNU GPL v2.0.
14 #define LKC_DIRECT_LINK
17 #include "zconf.hash.c"
19 #define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt)
22 #define DEBUG_PARSE 0x0002
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
);
32 struct symbol
*symbol_hash
[257];
34 static struct menu
*current_menu
, *current_entry
;
38 #define YYERROR_VERBOSE
47 struct symbol
*symbol
;
58 %token
<id
>T_ENDCHOICE
61 %token
<id
>T_MENUCONFIG
63 %token
<string> T_HELPTEXT
77 %token
<string> T_WORD
78 %token
<string> T_WORD_QUOTE
86 %left T_EQUAL T_UNEQUAL
94 %type
<id
> option_name
95 %type
<menu
> if_entry menu_entry choice_entry
98 fprintf
(stderr
, "%s:%d: missing end statement for this entry\n",
99 $$
->file
->name
, $$
->lineno
);
100 if
(current_menu
== $$
)
102 } if_entry menu_entry choice_entry
109 | stmt_list common_stmt
110 | stmt_list choice_stmt
111 | stmt_list menu_stmt
112 | stmt_list T_MAINMENU prompt nl
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
117 zconf_error
("unexpected option \"%s\"", kconf_id_strings
+ $2->name
);
119 | stmt_list
error T_EOL
{ zconf_error
("invalid statement"); }
123 T_DEPENDS | T_PROMPT | T_TYPE | T_DESELECT | T_SELECT | T_OPTIONAL | T_RANGE | T_DEFAULT | T_RESET
136 T_WORD
error T_EOL
{ zconf_error
("unknown option \"%s\"", $1); }
137 |
error T_EOL
{ zconf_error
("invalid option"); }
141 /* config/menuconfig entry */
143 config_entry_start: T_CONFIG T_WORD T_EOL
145 struct symbol
*sym
= sym_lookup
($2, 0);
146 sym
->flags |
= SYMBOL_OPTIONAL
;
148 printd
(DEBUG_PARSE
, "%s:%d:config %s\n", zconf_curname
(), zconf_lineno
(), $2);
151 config_stmt: config_entry_start config_option_list
154 printd
(DEBUG_PARSE
, "%s:%d:endconfig\n", zconf_curname
(), zconf_lineno
());
157 menuconfig_entry_start: T_MENUCONFIG T_WORD T_EOL
159 struct symbol
*sym
= sym_lookup
($2, 0);
160 sym
->flags |
= SYMBOL_OPTIONAL
;
162 printd
(DEBUG_PARSE
, "%s:%d:menuconfig %s\n", zconf_curname
(), zconf_lineno
(), $2);
165 menuconfig_stmt: menuconfig_entry_start config_option_list
167 if
(current_entry
->prompt
)
168 current_entry
->prompt
->type
= P_MENU
;
170 zconfprint
("warning: menuconfig statement without prompt");
172 printd
(DEBUG_PARSE
, "%s:%d:endconfig\n", zconf_curname
(), zconf_lineno
());
177 | config_option_list config_option
178 | config_option_list depends
179 | config_option_list help
180 | config_option_list option_error
181 | config_option_list T_EOL
184 config_option: T_TYPE prompt_stmt_opt T_EOL
186 menu_set_type
($1->stype
);
187 printd
(DEBUG_PARSE
, "%s:%d:type(%u)\n",
188 zconf_curname
(), zconf_lineno
(),
192 config_option: T_PROMPT prompt if_expr T_EOL
194 menu_add_prompt
(P_PROMPT
, $2, $3);
195 printd
(DEBUG_PARSE
, "%s:%d:prompt\n", zconf_curname
(), zconf_lineno
());
198 config_option: T_DEFAULT expr if_expr T_EOL
200 menu_add_expr
(P_DEFAULT
, $2, $3);
201 if
($1->stype
!= S_UNKNOWN
)
202 menu_set_type
($1->stype
);
203 printd
(DEBUG_PARSE
, "%s:%d:default(%u)\n",
204 zconf_curname
(), zconf_lineno
(),
208 config_option: T_DESELECT T_WORD if_expr T_EOL
210 menu_add_symbol
(P_DESELECT
, sym_lookup
($2, 0), $3);
211 printd
(DEBUG_PARSE
, "%s:%d:deselect\n", zconf_curname
(), zconf_lineno
());
214 config_option: T_SELECT T_WORD if_expr T_EOL
216 menu_add_symbol
(P_SELECT
, sym_lookup
($2, 0), $3);
217 printd
(DEBUG_PARSE
, "%s:%d:select\n", zconf_curname
(), zconf_lineno
());
220 config_option: T_RANGE symbol symbol if_expr T_EOL
222 menu_add_expr
(P_RANGE
, expr_alloc_comp
(E_RANGE
,$2, $3), $4);
223 printd
(DEBUG_PARSE
, "%s:%d:range\n", zconf_curname
(), zconf_lineno
());
228 choice: T_CHOICE T_EOL
230 struct symbol
*sym
= sym_lookup
(NULL
, 0);
231 sym
->flags |
= SYMBOL_CHOICE
;
233 menu_add_expr
(P_CHOICE
, NULL
, NULL
);
234 printd
(DEBUG_PARSE
, "%s:%d:choice\n", zconf_curname
(), zconf_lineno
());
237 choice_entry: choice choice_option_list
239 $$
= menu_add_menu
();
244 if
(zconf_endtoken
($1, T_CHOICE
, T_ENDCHOICE
)) {
246 printd
(DEBUG_PARSE
, "%s:%d:endchoice\n", zconf_curname
(), zconf_lineno
());
250 choice_stmt: choice_entry choice_block choice_end
255 | choice_option_list choice_option
256 | choice_option_list depends
257 | choice_option_list help
258 | choice_option_list T_EOL
259 | choice_option_list option_error
262 choice_option: T_PROMPT prompt if_expr T_EOL
264 menu_add_prompt
(P_PROMPT
, $2, $3);
265 printd
(DEBUG_PARSE
, "%s:%d:prompt\n", zconf_curname
(), zconf_lineno
());
268 choice_option: T_TYPE prompt_stmt_opt T_EOL
270 if
($1->stype
== S_BOOLEAN ||
$1->stype
== S_TRISTATE
) {
271 menu_set_type
($1->stype
);
272 printd
(DEBUG_PARSE
, "%s:%d:type(%u)\n",
273 zconf_curname
(), zconf_lineno
(),
279 choice_option: T_OPTIONAL T_EOL
281 current_entry
->sym
->flags |
= SYMBOL_OPTIONAL
;
282 printd
(DEBUG_PARSE
, "%s:%d:optional\n", zconf_curname
(), zconf_lineno
());
285 choice_option: T_RESET if_expr T_EOL
287 menu_add_prop
(P_RESET
, NULL
, NULL
, $2);
290 choice_option: T_DEFAULT T_WORD if_expr T_EOL
292 if
($1->stype
== S_UNKNOWN
) {
293 menu_add_symbol
(P_DEFAULT
, sym_lookup
($2, 0), $3);
294 printd
(DEBUG_PARSE
, "%s:%d:default\n",
295 zconf_curname
(), zconf_lineno
());
302 | choice_block common_stmt
307 if_entry: T_IF expr nl
309 printd
(DEBUG_PARSE
, "%s:%d:if\n", zconf_curname
(), zconf_lineno
());
310 menu_add_entry
(NULL
);
312 $$
= menu_add_menu
();
317 if
(zconf_endtoken
($1, T_IF
, T_ENDIF
)) {
319 printd
(DEBUG_PARSE
, "%s:%d:endif\n", zconf_curname
(), zconf_lineno
());
323 if_stmt: if_entry if_block if_end
328 | if_block common_stmt
330 | if_block choice_stmt
335 menu: T_MENU prompt T_EOL
337 menu_add_entry
(NULL
);
338 menu_add_prompt
(P_MENU
, $2, NULL
);
339 printd
(DEBUG_PARSE
, "%s:%d:menu\n", zconf_curname
(), zconf_lineno
());
342 menu_entry: menu depends_list
344 $$
= menu_add_menu
();
349 if
(zconf_endtoken
($1, T_MENU
, T_ENDMENU
)) {
351 printd
(DEBUG_PARSE
, "%s:%d:endmenu\n", zconf_curname
(), zconf_lineno
());
355 menu_stmt: menu_entry menu_block menu_end
360 | menu_block common_stmt
361 | menu_block menu_stmt
362 | menu_block choice_stmt
365 source_stmt: T_SOURCE prompt T_EOL
367 printd
(DEBUG_PARSE
, "%s:%d:source %s\n", zconf_curname
(), zconf_lineno
(), $2);
373 comment: T_COMMENT prompt T_EOL
375 menu_add_entry
(NULL
);
376 menu_add_prompt
(P_COMMENT
, $2, NULL
);
377 printd
(DEBUG_PARSE
, "%s:%d:comment\n", zconf_curname
(), zconf_lineno
());
380 comment_stmt: comment depends_list
387 help_start: T_HELP T_EOL
389 printd
(DEBUG_PARSE
, "%s:%d:help\n", zconf_curname
(), zconf_lineno
());
393 help: help_start T_HELPTEXT
395 current_entry
->sym
->help
= $2;
402 | depends_list depends
404 | depends_list option_error
407 depends: T_DEPENDS T_ON expr T_EOL
410 printd
(DEBUG_PARSE
, "%s:%d:depends on\n", zconf_curname
(), zconf_lineno
());
412 | T_DEPENDS expr T_EOL
415 printd
(DEBUG_PARSE
, "%s:%d:depends\n", zconf_curname
(), zconf_lineno
());
417 | T_REQUIRES expr T_EOL
420 printd
(DEBUG_PARSE
, "%s:%d:requires\n", zconf_curname
(), zconf_lineno
());
423 /* prompt statement */
429 menu_add_prompt
(P_PROMPT
, $1, $2);
436 end: T_ENDMENU T_EOL
{ $$
= $1; }
437 | T_ENDCHOICE T_EOL
{ $$
= $1; }
438 | T_ENDIF T_EOL
{ $$
= $1; }
446 if_expr: /* empty */ { $$
= NULL
; }
447 | T_IF expr
{ $$
= $2; }
450 expr: symbol
{ $$
= expr_alloc_symbol
($1); }
451 | symbol T_EQUAL symbol
{ $$
= expr_alloc_comp
(E_EQUAL
, $1, $3); }
452 | symbol T_UNEQUAL symbol
{ $$
= expr_alloc_comp
(E_UNEQUAL
, $1, $3); }
453 | T_OPEN_PAREN expr T_CLOSE_PAREN
{ $$
= $2; }
454 | T_NOT expr
{ $$
= expr_alloc_one
(E_NOT
, $2); }
455 | expr T_OR expr
{ $$
= expr_alloc_two
(E_OR
, $1, $3); }
456 | expr T_AND expr
{ $$
= expr_alloc_two
(E_AND
, $1, $3); }
459 symbol: T_WORD
{ $$
= sym_lookup
($1, 0); free
($1); }
460 | T_WORD_QUOTE
{ $$
= sym_lookup
($1, 1); free
($1); }
465 void conf_parse
(const char *name
)
470 zconf_initscan
(name
);
474 modules_sym
= sym_lookup
("MODULES", 0);
475 rootmenu.prompt
= menu_add_prompt
(P_MENU
, "OpenWrt Configuration", NULL
);
478 if
(getenv
("ZCONF_DEBUG"))
484 menu_finalize
(&rootmenu
);
485 for_all_symbols
(i
, sym
) {
489 sym_change_count
= 1;
492 const char *zconf_tokenname
(int token
)
495 case T_MENU
: return
"menu";
496 case T_ENDMENU
: return
"endmenu";
497 case T_CHOICE
: return
"choice";
498 case T_ENDCHOICE
: return
"endchoice";
499 case T_IF
: return
"if";
500 case T_ENDIF
: return
"endif";
501 case T_DEPENDS
: return
"depends";
506 static bool zconf_endtoken
(struct kconf_id
*id
, int starttoken
, int endtoken
)
508 if
(id
->token
!= endtoken
) {
509 zconf_error
("unexpected '%s' within %s block",
510 kconf_id_strings
+ id
->name
, zconf_tokenname
(starttoken
));
514 if
(current_menu
->file
!= current_file
) {
515 zconf_error
("'%s' in different file than '%s'",
516 kconf_id_strings
+ id
->name
, zconf_tokenname
(starttoken
));
517 fprintf
(stderr
, "%s:%d: location of the '%s'\n",
518 current_menu
->file
->name
, current_menu
->lineno
,
519 zconf_tokenname
(starttoken
));
526 static void zconfprint
(const char *err
, ...
)
530 fprintf
(stderr
, "%s:%d: ", zconf_curname
(), zconf_lineno
());
532 vfprintf
(stderr
, err
, ap
);
534 fprintf
(stderr
, "\n");
537 static void zconf_error
(const char *err
, ...
)
542 fprintf
(stderr
, "%s:%d: ", zconf_curname
(), zconf_lineno
());
544 vfprintf
(stderr
, err
, ap
);
546 fprintf
(stderr
, "\n");
549 static void zconferror
(const char *err
)
552 fprintf
(stderr
, "%s:%d: %s\n", zconf_curname
(), zconf_lineno
() + 1, err
);
556 void print_quoted_string
(FILE *out
, const char *str
)
562 while
((p
= strchr
(str
, '"'))) {
565 fprintf
(out
, "%.*s", len
, str
);
573 void print_symbol
(FILE *out
, struct menu
*menu
)
575 struct symbol
*sym
= menu
->sym
;
576 struct property
*prop
;
578 if
(sym_is_choice
(sym
))
579 fprintf
(out
, "choice\n");
581 fprintf
(out
, "config %s\n", sym
->name
);
584 fputs
(" boolean\n", out
);
587 fputs
(" tristate\n", out
);
590 fputs
(" string\n", out
);
593 fputs
(" integer\n", out
);
596 fputs
(" hex\n", out
);
599 fputs
(" ???\n", out
);
602 for
(prop
= sym
->prop
; prop
; prop
= prop
->next
) {
603 if
(prop
->menu
!= menu
)
605 switch
(prop
->type
) {
607 fputs
(" prompt ", out
);
608 print_quoted_string
(out
, prop
->text
);
609 if
(!expr_is_yes
(prop
->visible.expr
)) {
611 expr_fprint
(prop
->visible.expr
, out
);
616 fputs
( " default ", out
);
617 expr_fprint
(prop
->expr
, out
);
618 if
(!expr_is_yes
(prop
->visible.expr
)) {
620 expr_fprint
(prop
->visible.expr
, out
);
625 fputs
(" #choice value\n", out
);
628 fprintf
(out
, " unknown prop %d!\n", prop
->type
);
633 int len
= strlen
(sym
->help
);
634 while
(sym
->help
[--len
] == '\n')
636 fprintf
(out
, " help\n%s\n", sym
->help
);
641 void zconfdump
(FILE *out
)
643 struct property
*prop
;
647 menu
= rootmenu.list
;
649 if
((sym
= menu
->sym
))
650 print_symbol
(out
, menu
);
651 else if
((prop
= menu
->prompt
)) {
652 switch
(prop
->type
) {
654 fputs
("\ncomment ", out
);
655 print_quoted_string
(out
, prop
->text
);
659 fputs
("\nmenu ", out
);
660 print_quoted_string
(out
, prop
->text
);
666 if
(!expr_is_yes
(prop
->visible.expr
)) {
667 fputs
(" depends ", out
);
668 expr_fprint
(prop
->visible.expr
, out
);
678 else while
((menu
= menu
->parent
)) {
679 if
(menu
->prompt
&& menu
->prompt
->type
== P_MENU
)
680 fputs
("\nendmenu\n", out
);
689 #include "lex.zconf.c"
691 #include "confdata.c"