zconf.y 14 KB

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