zconf.y 14 KB

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