zconf.y 14 KB

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