parser.y 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  4. */
  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. #include <xalloc.h>
  13. #include "lkc.h"
  14. #include "internal.h"
  15. #include "preprocess.h"
  16. #define printd(mask, fmt...) if (cdebug & (mask)) printf(fmt)
  17. #define PRINTD 0x0001
  18. #define DEBUG_PARSE 0x0002
  19. int cdebug = PRINTD;
  20. static void yyerror(const char *err);
  21. static void zconf_error(const char *err, ...);
  22. static bool zconf_endtoken(const char *tokenname,
  23. const char *expected_tokenname);
  24. struct menu *current_menu, *current_entry, *current_choice;
  25. %}
  26. %union
  27. {
  28. char *string;
  29. struct symbol *symbol;
  30. struct expr *expr;
  31. struct menu *menu;
  32. enum symbol_type type;
  33. enum variable_flavor flavor;
  34. }
  35. %token <string> T_HELPTEXT
  36. %token <string> T_WORD
  37. %token <string> T_WORD_QUOTE
  38. %token T_BOOL
  39. %token T_CHOICE
  40. %token T_CLOSE_PAREN
  41. %token T_COLON_EQUAL
  42. %token T_COMMENT
  43. %token T_CONFIG
  44. %token T_DEFAULT
  45. %token T_DEF_BOOL
  46. %token T_DEF_TRISTATE
  47. %token T_DEPENDS
  48. %token T_ENDCHOICE
  49. %token T_ENDIF
  50. %token T_ENDMENU
  51. %token T_HELP
  52. %token T_HEX
  53. %token T_IF
  54. %token T_IMPLY
  55. %token T_INT
  56. %token T_MAINMENU
  57. %token T_MENU
  58. %token T_MENUCONFIG
  59. %token T_MODULES
  60. %token T_ON
  61. %token T_OPEN_PAREN
  62. %token T_PLUS_EQUAL
  63. %token T_PROMPT
  64. %token T_RANGE
  65. %token T_SELECT
  66. %token T_SOURCE
  67. %token T_STRING
  68. %token T_TRANSITIONAL
  69. %token T_TRISTATE
  70. %token T_VISIBLE
  71. %token T_EOL
  72. %token <string> T_ASSIGN_VAL
  73. %left T_OR
  74. %left T_AND
  75. %left T_EQUAL T_UNEQUAL
  76. %left T_LESS T_LESS_EQUAL T_GREATER T_GREATER_EQUAL
  77. %nonassoc T_NOT
  78. %type <symbol> nonconst_symbol
  79. %type <symbol> symbol
  80. %type <type> type default
  81. %type <expr> expr
  82. %type <expr> if_expr
  83. %type <string> end
  84. %type <menu> if_entry menu_entry choice_entry
  85. %type <string> assign_val
  86. %type <flavor> assign_op
  87. %destructor {
  88. fprintf(stderr, "%s:%d: missing end statement for this entry\n",
  89. $$->filename, $$->lineno);
  90. if (current_menu == $$)
  91. menu_end_menu();
  92. } if_entry menu_entry choice_entry
  93. %%
  94. input: mainmenu_stmt stmt_list | stmt_list;
  95. /* mainmenu entry */
  96. mainmenu_stmt: T_MAINMENU T_WORD_QUOTE T_EOL
  97. {
  98. menu_add_prompt(P_MENU, $2, NULL);
  99. };
  100. stmt_list:
  101. /* empty */
  102. | stmt_list assignment_stmt
  103. | stmt_list choice_stmt
  104. | stmt_list comment_stmt
  105. | stmt_list config_stmt
  106. | stmt_list if_stmt
  107. | stmt_list menu_stmt
  108. | stmt_list menuconfig_stmt
  109. | stmt_list source_stmt
  110. | stmt_list T_WORD error T_EOL { zconf_error("unknown statement \"%s\"", $2); }
  111. | stmt_list error T_EOL { zconf_error("invalid statement"); }
  112. ;
  113. stmt_list_in_choice:
  114. /* empty */
  115. | stmt_list_in_choice comment_stmt
  116. | stmt_list_in_choice config_stmt
  117. | stmt_list_in_choice if_stmt_in_choice
  118. | stmt_list_in_choice error T_EOL { zconf_error("invalid statement"); }
  119. ;
  120. /* config/menuconfig entry */
  121. config_entry_start: T_CONFIG nonconst_symbol T_EOL
  122. {
  123. menu_add_entry($2, M_NORMAL);
  124. printd(DEBUG_PARSE, "%s:%d:config %s\n", cur_filename, cur_lineno, $2->name);
  125. };
  126. config_stmt: config_entry_start config_option_list
  127. {
  128. if (current_choice) {
  129. if (!current_entry->prompt) {
  130. fprintf(stderr, "%s:%d: error: choice member must have a prompt\n",
  131. current_entry->filename, current_entry->lineno);
  132. yynerrs++;
  133. }
  134. if (current_entry->sym->type != S_BOOLEAN) {
  135. fprintf(stderr, "%s:%d: error: choice member must be bool\n",
  136. current_entry->filename, current_entry->lineno);
  137. yynerrs++;
  138. }
  139. /*
  140. * If the same symbol appears twice in a choice block, the list
  141. * node would be added twice, leading to a broken linked list.
  142. * list_empty() ensures that this symbol has not yet added.
  143. */
  144. if (list_empty(&current_entry->sym->choice_link))
  145. list_add_tail(&current_entry->sym->choice_link,
  146. &current_choice->choice_members);
  147. }
  148. printd(DEBUG_PARSE, "%s:%d:endconfig\n", cur_filename, cur_lineno);
  149. };
  150. menuconfig_entry_start: T_MENUCONFIG nonconst_symbol T_EOL
  151. {
  152. menu_add_entry($2, M_MENU);
  153. printd(DEBUG_PARSE, "%s:%d:menuconfig %s\n", cur_filename, cur_lineno, $2->name);
  154. };
  155. menuconfig_stmt: menuconfig_entry_start config_option_list
  156. {
  157. if (current_entry->prompt)
  158. current_entry->prompt->type = P_MENU;
  159. else
  160. zconf_error("menuconfig statement without prompt");
  161. printd(DEBUG_PARSE, "%s:%d:endconfig\n", cur_filename, cur_lineno);
  162. };
  163. config_option_list:
  164. /* empty */
  165. | config_option_list config_option
  166. | config_option_list depends
  167. | config_option_list help
  168. ;
  169. config_option: type prompt_stmt_opt T_EOL
  170. {
  171. menu_set_type($1);
  172. printd(DEBUG_PARSE, "%s:%d:type(%u)\n", cur_filename, cur_lineno, $1);
  173. };
  174. config_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
  175. {
  176. menu_add_prompt(P_PROMPT, $2, $3);
  177. printd(DEBUG_PARSE, "%s:%d:prompt\n", cur_filename, cur_lineno);
  178. };
  179. config_option: T_TRANSITIONAL T_EOL
  180. {
  181. current_entry->sym->flags |= SYMBOL_TRANS;
  182. printd(DEBUG_PARSE, "%s:%d:transitional\n", cur_filename, cur_lineno);
  183. };
  184. config_option: default expr if_expr T_EOL
  185. {
  186. menu_add_expr(P_DEFAULT, $2, $3);
  187. if ($1 != S_UNKNOWN)
  188. menu_set_type($1);
  189. printd(DEBUG_PARSE, "%s:%d:default(%u)\n", cur_filename, cur_lineno,
  190. $1);
  191. };
  192. config_option: T_SELECT nonconst_symbol if_expr T_EOL
  193. {
  194. menu_add_symbol(P_SELECT, $2, $3);
  195. printd(DEBUG_PARSE, "%s:%d:select\n", cur_filename, cur_lineno);
  196. };
  197. config_option: T_IMPLY nonconst_symbol if_expr T_EOL
  198. {
  199. menu_add_symbol(P_IMPLY, $2, $3);
  200. printd(DEBUG_PARSE, "%s:%d:imply\n", cur_filename, cur_lineno);
  201. };
  202. config_option: T_RANGE symbol symbol if_expr T_EOL
  203. {
  204. menu_add_expr(P_RANGE, expr_alloc_comp(E_RANGE,$2, $3), $4);
  205. printd(DEBUG_PARSE, "%s:%d:range\n", cur_filename, cur_lineno);
  206. };
  207. config_option: T_MODULES T_EOL
  208. {
  209. if (modules_sym)
  210. zconf_error("symbol '%s' redefines option 'modules' already defined by symbol '%s'",
  211. current_entry->sym->name, modules_sym->name);
  212. modules_sym = current_entry->sym;
  213. };
  214. /* choice entry */
  215. choice: T_CHOICE T_EOL
  216. {
  217. struct symbol *sym = sym_lookup(NULL, 0);
  218. menu_add_entry(sym, M_CHOICE);
  219. menu_set_type(S_BOOLEAN);
  220. INIT_LIST_HEAD(&current_entry->choice_members);
  221. printd(DEBUG_PARSE, "%s:%d:choice\n", cur_filename, cur_lineno);
  222. };
  223. choice_entry: choice choice_option_list
  224. {
  225. if (!current_entry->prompt) {
  226. fprintf(stderr, "%s:%d: error: choice must have a prompt\n",
  227. current_entry->filename, current_entry->lineno);
  228. yynerrs++;
  229. }
  230. $$ = menu_add_menu();
  231. current_choice = current_entry;
  232. };
  233. choice_end: end
  234. {
  235. current_choice = NULL;
  236. if (zconf_endtoken($1, "choice")) {
  237. menu_end_menu();
  238. printd(DEBUG_PARSE, "%s:%d:endchoice\n", cur_filename, cur_lineno);
  239. }
  240. };
  241. choice_stmt: choice_entry stmt_list_in_choice choice_end
  242. ;
  243. choice_option_list:
  244. /* empty */
  245. | choice_option_list choice_option
  246. | choice_option_list depends
  247. | choice_option_list help
  248. ;
  249. choice_option: T_PROMPT T_WORD_QUOTE if_expr T_EOL
  250. {
  251. menu_add_prompt(P_PROMPT, $2, $3);
  252. printd(DEBUG_PARSE, "%s:%d:prompt\n", cur_filename, cur_lineno);
  253. };
  254. choice_option: T_DEFAULT nonconst_symbol if_expr T_EOL
  255. {
  256. menu_add_symbol(P_DEFAULT, $2, $3);
  257. printd(DEBUG_PARSE, "%s:%d:default\n", cur_filename, cur_lineno);
  258. };
  259. type:
  260. T_BOOL { $$ = S_BOOLEAN; }
  261. | T_TRISTATE { $$ = S_TRISTATE; }
  262. | T_INT { $$ = S_INT; }
  263. | T_HEX { $$ = S_HEX; }
  264. | T_STRING { $$ = S_STRING; }
  265. default:
  266. T_DEFAULT { $$ = S_UNKNOWN; }
  267. | T_DEF_BOOL { $$ = S_BOOLEAN; }
  268. | T_DEF_TRISTATE { $$ = S_TRISTATE; }
  269. /* if entry */
  270. if_entry: T_IF expr T_EOL
  271. {
  272. printd(DEBUG_PARSE, "%s:%d:if\n", cur_filename, cur_lineno);
  273. menu_add_entry(NULL, M_IF);
  274. menu_add_dep($2, NULL);
  275. $$ = menu_add_menu();
  276. };
  277. if_end: end
  278. {
  279. if (zconf_endtoken($1, "if")) {
  280. menu_end_menu();
  281. printd(DEBUG_PARSE, "%s:%d:endif\n", cur_filename, cur_lineno);
  282. }
  283. };
  284. if_stmt: if_entry stmt_list if_end
  285. ;
  286. if_stmt_in_choice: if_entry stmt_list_in_choice if_end
  287. ;
  288. /* menu entry */
  289. menu: T_MENU T_WORD_QUOTE T_EOL
  290. {
  291. menu_add_entry(NULL, M_MENU);
  292. menu_add_prompt(P_MENU, $2, NULL);
  293. printd(DEBUG_PARSE, "%s:%d:menu\n", cur_filename, cur_lineno);
  294. };
  295. menu_entry: menu menu_option_list
  296. {
  297. $$ = menu_add_menu();
  298. };
  299. menu_end: end
  300. {
  301. if (zconf_endtoken($1, "menu")) {
  302. menu_end_menu();
  303. printd(DEBUG_PARSE, "%s:%d:endmenu\n", cur_filename, cur_lineno);
  304. }
  305. };
  306. menu_stmt: menu_entry stmt_list menu_end
  307. ;
  308. menu_option_list:
  309. /* empty */
  310. | menu_option_list visible
  311. | menu_option_list depends
  312. ;
  313. source_stmt: T_SOURCE T_WORD_QUOTE T_EOL
  314. {
  315. printd(DEBUG_PARSE, "%s:%d:source %s\n", cur_filename, cur_lineno, $2);
  316. zconf_nextfile($2);
  317. free($2);
  318. };
  319. /* comment entry */
  320. comment: T_COMMENT T_WORD_QUOTE T_EOL
  321. {
  322. menu_add_entry(NULL, M_COMMENT);
  323. menu_add_prompt(P_COMMENT, $2, NULL);
  324. printd(DEBUG_PARSE, "%s:%d:comment\n", cur_filename, cur_lineno);
  325. };
  326. comment_stmt: comment comment_option_list
  327. ;
  328. comment_option_list:
  329. /* empty */
  330. | comment_option_list depends
  331. ;
  332. /* help option */
  333. help_start: T_HELP T_EOL
  334. {
  335. printd(DEBUG_PARSE, "%s:%d:help\n", cur_filename, cur_lineno);
  336. zconf_starthelp();
  337. };
  338. help: help_start T_HELPTEXT
  339. {
  340. if (current_entry->help) {
  341. free(current_entry->help);
  342. zconf_error("'%s' defined with more than one help text",
  343. current_entry->sym->name ?: "<choice>");
  344. }
  345. /* Is the help text empty or all whitespace? */
  346. if ($2[strspn($2, " \f\n\r\t\v")] == '\0')
  347. zconf_error("'%s' defined with blank help text",
  348. current_entry->sym->name ?: "<choice>");
  349. current_entry->help = $2;
  350. };
  351. /* depends option */
  352. depends: T_DEPENDS T_ON expr if_expr T_EOL
  353. {
  354. menu_add_dep($3, $4);
  355. printd(DEBUG_PARSE, "%s:%d:depends on\n", cur_filename, cur_lineno);
  356. };
  357. /* visibility option */
  358. visible: T_VISIBLE if_expr T_EOL
  359. {
  360. menu_add_visibility($2);
  361. };
  362. /* prompt statement */
  363. prompt_stmt_opt:
  364. /* empty */
  365. | T_WORD_QUOTE if_expr
  366. {
  367. menu_add_prompt(P_PROMPT, $1, $2);
  368. };
  369. end: T_ENDMENU T_EOL { $$ = "menu"; }
  370. | T_ENDCHOICE T_EOL { $$ = "choice"; }
  371. | T_ENDIF T_EOL { $$ = "if"; }
  372. ;
  373. if_expr: /* empty */ { $$ = NULL; }
  374. | T_IF expr { $$ = $2; }
  375. ;
  376. expr: symbol { $$ = expr_alloc_symbol($1); }
  377. | symbol T_LESS symbol { $$ = expr_alloc_comp(E_LTH, $1, $3); }
  378. | symbol T_LESS_EQUAL symbol { $$ = expr_alloc_comp(E_LEQ, $1, $3); }
  379. | symbol T_GREATER symbol { $$ = expr_alloc_comp(E_GTH, $1, $3); }
  380. | symbol T_GREATER_EQUAL symbol { $$ = expr_alloc_comp(E_GEQ, $1, $3); }
  381. | symbol T_EQUAL symbol { $$ = expr_alloc_comp(E_EQUAL, $1, $3); }
  382. | symbol T_UNEQUAL symbol { $$ = expr_alloc_comp(E_UNEQUAL, $1, $3); }
  383. | T_OPEN_PAREN expr T_CLOSE_PAREN { $$ = $2; }
  384. | T_NOT expr { $$ = expr_alloc_one(E_NOT, $2); }
  385. | expr T_OR expr { $$ = expr_alloc_two(E_OR, $1, $3); }
  386. | expr T_AND expr { $$ = expr_alloc_two(E_AND, $1, $3); }
  387. ;
  388. /* For symbol definitions, selects, etc., where quotes are not accepted */
  389. nonconst_symbol: T_WORD { $$ = sym_lookup($1, 0); free($1); };
  390. symbol: nonconst_symbol
  391. | T_WORD_QUOTE { $$ = sym_lookup($1, SYMBOL_CONST); free($1); }
  392. ;
  393. /* assignment statement */
  394. assignment_stmt: T_WORD assign_op assign_val T_EOL { variable_add($1, $3, $2); free($1); free($3); }
  395. assign_op:
  396. T_EQUAL { $$ = VAR_RECURSIVE; }
  397. | T_COLON_EQUAL { $$ = VAR_SIMPLE; }
  398. | T_PLUS_EQUAL { $$ = VAR_APPEND; }
  399. ;
  400. assign_val:
  401. /* empty */ { $$ = xstrdup(""); };
  402. | T_ASSIGN_VAL
  403. ;
  404. %%
  405. /**
  406. * transitional_check_sanity - check transitional symbols have no other
  407. * properties
  408. *
  409. * @menu: menu of the potentially transitional symbol
  410. *
  411. * Return: -1 if an error is found, 0 otherwise.
  412. */
  413. static int transitional_check_sanity(const struct menu *menu)
  414. {
  415. struct property *prop;
  416. if (!menu->sym || !(menu->sym->flags & SYMBOL_TRANS))
  417. return 0;
  418. /* Check for depends and visible conditions. */
  419. if ((menu->dep && !expr_is_yes(menu->dep)) ||
  420. (menu->visibility && !expr_is_yes(menu->visibility))) {
  421. fprintf(stderr, "%s:%d: error: %s",
  422. menu->filename, menu->lineno,
  423. "transitional symbols can only have help sections\n");
  424. return -1;
  425. }
  426. /* Check for any property other than "help". */
  427. for (prop = menu->sym->prop; prop; prop = prop->next) {
  428. if (prop->type != P_COMMENT) {
  429. fprintf(stderr, "%s:%d: error: %s",
  430. prop->filename, prop->lineno,
  431. "transitional symbols can only have help sections\n");
  432. return -1;
  433. }
  434. }
  435. return 0;
  436. }
  437. /**
  438. * choice_check_sanity - check sanity of a choice member
  439. *
  440. * @menu: menu of the choice member
  441. *
  442. * Return: -1 if an error is found, 0 otherwise.
  443. */
  444. static int choice_check_sanity(const struct menu *menu)
  445. {
  446. struct property *prop;
  447. int ret = 0;
  448. for (prop = menu->sym->prop; prop; prop = prop->next) {
  449. if (prop->type == P_DEFAULT) {
  450. fprintf(stderr, "%s:%d: error: %s",
  451. prop->filename, prop->lineno,
  452. "defaults for choice values not supported\n");
  453. ret = -1;
  454. }
  455. if (prop->menu != menu && prop->type == P_PROMPT &&
  456. prop->menu->parent != menu->parent) {
  457. fprintf(stderr, "%s:%d: error: %s",
  458. prop->filename, prop->lineno,
  459. "choice value has a prompt outside its choice group\n");
  460. ret = -1;
  461. }
  462. }
  463. return ret;
  464. }
  465. void conf_parse(const char *name)
  466. {
  467. struct menu *menu;
  468. autoconf_cmd = str_new();
  469. str_printf(&autoconf_cmd, "\ndeps_config := \\\n");
  470. zconf_initscan(name);
  471. _menu_init();
  472. if (getenv("ZCONF_DEBUG"))
  473. yydebug = 1;
  474. yyparse();
  475. str_printf(&autoconf_cmd,
  476. "\n"
  477. "$(autoconfig): $(deps_config)\n"
  478. "$(deps_config): ;\n");
  479. env_write_dep(&autoconf_cmd);
  480. /* Variables are expanded in the parse phase. We can free them here. */
  481. variable_all_del();
  482. if (yynerrs)
  483. exit(1);
  484. if (!modules_sym)
  485. modules_sym = &symbol_no;
  486. if (!menu_has_prompt(&rootmenu)) {
  487. current_entry = &rootmenu;
  488. menu_add_prompt(P_MENU, "Main menu", NULL);
  489. }
  490. menu_finalize();
  491. menu_for_each_entry(menu) {
  492. struct menu *child;
  493. if (menu->sym && sym_check_deps(menu->sym))
  494. yynerrs++;
  495. if (transitional_check_sanity(menu))
  496. yynerrs++;
  497. if (menu->sym && sym_is_choice(menu->sym)) {
  498. menu_for_each_sub_entry(child, menu)
  499. if (child->sym && choice_check_sanity(child))
  500. yynerrs++;
  501. }
  502. }
  503. if (yynerrs)
  504. exit(1);
  505. conf_set_changed(true);
  506. }
  507. static bool zconf_endtoken(const char *tokenname,
  508. const char *expected_tokenname)
  509. {
  510. if (strcmp(tokenname, expected_tokenname)) {
  511. zconf_error("unexpected '%s' within %s block",
  512. tokenname, expected_tokenname);
  513. yynerrs++;
  514. return false;
  515. }
  516. if (strcmp(current_menu->filename, cur_filename)) {
  517. zconf_error("'%s' in different file than '%s'",
  518. tokenname, expected_tokenname);
  519. fprintf(stderr, "%s:%d: location of the '%s'\n",
  520. current_menu->filename, current_menu->lineno,
  521. expected_tokenname);
  522. yynerrs++;
  523. return false;
  524. }
  525. return true;
  526. }
  527. static void zconf_error(const char *err, ...)
  528. {
  529. va_list ap;
  530. yynerrs++;
  531. fprintf(stderr, "%s:%d: ", cur_filename, cur_lineno);
  532. va_start(ap, err);
  533. vfprintf(stderr, err, ap);
  534. va_end(ap);
  535. fprintf(stderr, "\n");
  536. }
  537. static void yyerror(const char *err)
  538. {
  539. fprintf(stderr, "%s:%d: %s\n", cur_filename, cur_lineno, err);
  540. }
  541. static void print_quoted_string(FILE *out, const char *str)
  542. {
  543. const char *p;
  544. int len;
  545. putc('"', out);
  546. while ((p = strchr(str, '"'))) {
  547. len = p - str;
  548. if (len)
  549. fprintf(out, "%.*s", len, str);
  550. fputs("\\\"", out);
  551. str = p + 1;
  552. }
  553. fputs(str, out);
  554. putc('"', out);
  555. }
  556. static void print_symbol(FILE *out, const struct menu *menu)
  557. {
  558. struct symbol *sym = menu->sym;
  559. struct property *prop;
  560. if (sym_is_choice(sym))
  561. fprintf(out, "\nchoice\n");
  562. else
  563. fprintf(out, "\nconfig %s\n", sym->name);
  564. switch (sym->type) {
  565. case S_BOOLEAN:
  566. fputs(" bool\n", out);
  567. break;
  568. case S_TRISTATE:
  569. fputs(" tristate\n", out);
  570. break;
  571. case S_STRING:
  572. fputs(" string\n", out);
  573. break;
  574. case S_INT:
  575. fputs(" integer\n", out);
  576. break;
  577. case S_HEX:
  578. fputs(" hex\n", out);
  579. break;
  580. default:
  581. fputs(" ???\n", out);
  582. break;
  583. }
  584. for (prop = sym->prop; prop; prop = prop->next) {
  585. if (prop->menu != menu)
  586. continue;
  587. switch (prop->type) {
  588. case P_PROMPT:
  589. fputs(" prompt ", out);
  590. print_quoted_string(out, prop->text);
  591. if (!expr_is_yes(prop->visible.expr)) {
  592. fputs(" if ", out);
  593. expr_fprint(prop->visible.expr, out);
  594. }
  595. fputc('\n', out);
  596. break;
  597. case P_DEFAULT:
  598. fputs( " default ", out);
  599. expr_fprint(prop->expr, out);
  600. if (!expr_is_yes(prop->visible.expr)) {
  601. fputs(" if ", out);
  602. expr_fprint(prop->visible.expr, out);
  603. }
  604. fputc('\n', out);
  605. break;
  606. case P_SELECT:
  607. fputs( " select ", out);
  608. expr_fprint(prop->expr, out);
  609. fputc('\n', out);
  610. break;
  611. case P_IMPLY:
  612. fputs( " imply ", out);
  613. expr_fprint(prop->expr, out);
  614. fputc('\n', out);
  615. break;
  616. case P_RANGE:
  617. fputs( " range ", out);
  618. expr_fprint(prop->expr, out);
  619. fputc('\n', out);
  620. break;
  621. case P_MENU:
  622. fputs( " menu ", out);
  623. print_quoted_string(out, prop->text);
  624. fputc('\n', out);
  625. break;
  626. default:
  627. fprintf(out, " unknown prop %d!\n", prop->type);
  628. break;
  629. }
  630. }
  631. if (menu->help) {
  632. int len = strlen(menu->help);
  633. while (menu->help[--len] == '\n')
  634. menu->help[len] = 0;
  635. fprintf(out, " help\n%s\n", menu->help);
  636. }
  637. }
  638. void zconfdump(FILE *out)
  639. {
  640. struct property *prop;
  641. struct symbol *sym;
  642. struct menu *menu;
  643. menu = rootmenu.list;
  644. while (menu) {
  645. if ((sym = menu->sym))
  646. print_symbol(out, menu);
  647. else if ((prop = menu->prompt)) {
  648. switch (prop->type) {
  649. case P_COMMENT:
  650. fputs("\ncomment ", out);
  651. print_quoted_string(out, prop->text);
  652. fputs("\n", out);
  653. break;
  654. case P_MENU:
  655. fputs("\nmenu ", out);
  656. print_quoted_string(out, prop->text);
  657. fputs("\n", out);
  658. break;
  659. default:
  660. ;
  661. }
  662. if (!expr_is_yes(prop->visible.expr)) {
  663. fputs(" depends ", out);
  664. expr_fprint(prop->visible.expr, out);
  665. fputc('\n', out);
  666. }
  667. }
  668. if (menu->list)
  669. menu = menu->list;
  670. else if (menu->next)
  671. menu = menu->next;
  672. else while ((menu = menu->parent)) {
  673. if (menu->prompt && menu->prompt->type == P_MENU)
  674. fputs("\nendmenu\n", out);
  675. if (menu->next) {
  676. menu = menu->next;
  677. break;
  678. }
  679. }
  680. }
  681. }