3
0

zconf.y 14 KB

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