zconf.y 13 KB

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