menu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  3. * Released under the terms of the GNU GPL v2.0.
  4. */
  5. #include <ctype.h>
  6. #include <stdarg.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "lkc.h"
  10. static const char nohelp_text[] = "There is no help available for this option.";
  11. struct menu rootmenu;
  12. static struct menu **last_entry_ptr;
  13. struct file *file_list;
  14. struct file *current_file;
  15. void menu_warn(struct menu *menu, const char *fmt, ...)
  16. {
  17. va_list ap;
  18. va_start(ap, fmt);
  19. fprintf(stderr, "%s:%d:warning: ", menu->file->name, menu->lineno);
  20. vfprintf(stderr, fmt, ap);
  21. fprintf(stderr, "\n");
  22. va_end(ap);
  23. }
  24. static void prop_warn(struct property *prop, const char *fmt, ...)
  25. {
  26. va_list ap;
  27. va_start(ap, fmt);
  28. fprintf(stderr, "%s:%d:warning: ", prop->file->name, prop->lineno);
  29. vfprintf(stderr, fmt, ap);
  30. fprintf(stderr, "\n");
  31. va_end(ap);
  32. }
  33. void _menu_init(void)
  34. {
  35. current_entry = current_menu = &rootmenu;
  36. last_entry_ptr = &rootmenu.list;
  37. }
  38. void menu_add_entry(struct symbol *sym)
  39. {
  40. struct menu *menu;
  41. menu = xmalloc(sizeof(*menu));
  42. memset(menu, 0, sizeof(*menu));
  43. menu->sym = sym;
  44. menu->parent = current_menu;
  45. menu->file = current_file;
  46. menu->lineno = zconf_lineno();
  47. *last_entry_ptr = menu;
  48. last_entry_ptr = &menu->next;
  49. current_entry = menu;
  50. if (sym)
  51. menu_add_symbol(P_SYMBOL, sym, NULL);
  52. }
  53. struct menu *menu_add_menu(void)
  54. {
  55. last_entry_ptr = &current_entry->list;
  56. return current_menu = current_entry;
  57. }
  58. void menu_end_menu(void)
  59. {
  60. last_entry_ptr = &current_menu->next;
  61. current_menu = current_menu->parent;
  62. }
  63. /*
  64. * Rewrites 'm' to 'm' && MODULES, so that it evaluates to 'n' when running
  65. * without modules
  66. */
  67. static struct expr *rewrite_m(struct expr *e)
  68. {
  69. if (!e)
  70. return e;
  71. switch (e->type) {
  72. case E_NOT:
  73. e->left.expr = rewrite_m(e->left.expr);
  74. break;
  75. case E_OR:
  76. case E_AND:
  77. e->left.expr = rewrite_m(e->left.expr);
  78. e->right.expr = rewrite_m(e->right.expr);
  79. break;
  80. case E_SYMBOL:
  81. /* change 'm' into 'm' && MODULES */
  82. if (e->left.sym == &symbol_mod)
  83. return expr_alloc_and(e, expr_alloc_symbol(modules_sym));
  84. break;
  85. default:
  86. break;
  87. }
  88. return e;
  89. }
  90. void menu_add_dep(struct expr *dep)
  91. {
  92. current_entry->dep = expr_alloc_and(current_entry->dep, dep);
  93. }
  94. void menu_set_type(int type)
  95. {
  96. struct symbol *sym = current_entry->sym;
  97. if (sym->type == type)
  98. return;
  99. if (sym->type == S_UNKNOWN) {
  100. sym->type = type;
  101. return;
  102. }
  103. menu_warn(current_entry,
  104. "ignoring type redefinition of '%s' from '%s' to '%s'",
  105. sym->name ? sym->name : "<choice>",
  106. sym_type_name(sym->type), sym_type_name(type));
  107. }
  108. static struct property *menu_add_prop(enum prop_type type, char *prompt, struct expr *expr, struct expr *dep)
  109. {
  110. struct property *prop = prop_alloc(type, current_entry->sym);
  111. prop->menu = current_entry;
  112. prop->expr = expr;
  113. prop->visible.expr = dep;
  114. if (prompt) {
  115. if (isspace(*prompt)) {
  116. prop_warn(prop, "leading whitespace ignored");
  117. while (isspace(*prompt))
  118. prompt++;
  119. }
  120. if (current_entry->prompt && current_entry != &rootmenu)
  121. prop_warn(prop, "prompt redefined");
  122. /* Apply all upper menus' visibilities to actual prompts. */
  123. if(type == P_PROMPT) {
  124. struct menu *menu = current_entry;
  125. while ((menu = menu->parent) != NULL) {
  126. struct expr *dup_expr;
  127. if (!menu->visibility)
  128. continue;
  129. /*
  130. * Do not add a reference to the
  131. * menu's visibility expression but
  132. * use a copy of it. Otherwise the
  133. * expression reduction functions
  134. * will modify expressions that have
  135. * multiple references which can
  136. * cause unwanted side effects.
  137. */
  138. dup_expr = expr_copy(menu->visibility);
  139. prop->visible.expr
  140. = expr_alloc_and(prop->visible.expr,
  141. dup_expr);
  142. }
  143. }
  144. current_entry->prompt = prop;
  145. }
  146. prop->text = prompt;
  147. return prop;
  148. }
  149. struct property *menu_add_prompt(enum prop_type type, char *prompt, struct expr *dep)
  150. {
  151. return menu_add_prop(type, prompt, NULL, dep);
  152. }
  153. void menu_add_visibility(struct expr *expr)
  154. {
  155. current_entry->visibility = expr_alloc_and(current_entry->visibility,
  156. expr);
  157. }
  158. void menu_add_expr(enum prop_type type, struct expr *expr, struct expr *dep)
  159. {
  160. menu_add_prop(type, NULL, expr, dep);
  161. }
  162. void menu_add_symbol(enum prop_type type, struct symbol *sym, struct expr *dep)
  163. {
  164. menu_add_prop(type, NULL, expr_alloc_symbol(sym), dep);
  165. }
  166. void menu_add_option(int token, char *arg)
  167. {
  168. switch (token) {
  169. case T_OPT_MODULES:
  170. if (modules_sym)
  171. zconf_error("symbol '%s' redefines option 'modules'"
  172. " already defined by symbol '%s'",
  173. current_entry->sym->name,
  174. modules_sym->name
  175. );
  176. modules_sym = current_entry->sym;
  177. break;
  178. case T_OPT_DEFCONFIG_LIST:
  179. if (!sym_defconfig_list)
  180. sym_defconfig_list = current_entry->sym;
  181. else if (sym_defconfig_list != current_entry->sym)
  182. zconf_error("trying to redefine defconfig symbol");
  183. sym_defconfig_list->flags |= SYMBOL_AUTO;
  184. break;
  185. case T_OPT_ENV:
  186. prop_add_env(arg);
  187. break;
  188. case T_OPT_ALLNOCONFIG_Y:
  189. current_entry->sym->flags |= SYMBOL_ALLNOCONFIG_Y;
  190. break;
  191. }
  192. }
  193. static int menu_validate_number(struct symbol *sym, struct symbol *sym2)
  194. {
  195. return sym2->type == S_INT || sym2->type == S_HEX ||
  196. (sym2->type == S_UNKNOWN && sym_string_valid(sym, sym2->name));
  197. }
  198. static void sym_check_prop(struct symbol *sym)
  199. {
  200. struct property *prop;
  201. struct symbol *sym2;
  202. char *use;
  203. for (prop = sym->prop; prop; prop = prop->next) {
  204. switch (prop->type) {
  205. case P_DEFAULT:
  206. if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
  207. prop->expr->type != E_SYMBOL)
  208. prop_warn(prop,
  209. "default for config symbol '%s'"
  210. " must be a single symbol", sym->name);
  211. if (prop->expr->type != E_SYMBOL)
  212. break;
  213. sym2 = prop_get_symbol(prop);
  214. if (sym->type == S_HEX || sym->type == S_INT) {
  215. if (!menu_validate_number(sym, sym2))
  216. prop_warn(prop,
  217. "'%s': number is invalid",
  218. sym->name);
  219. }
  220. if (sym_is_choice(sym)) {
  221. struct property *choice_prop =
  222. sym_get_choice_prop(sym2);
  223. if (!choice_prop ||
  224. prop_get_symbol(choice_prop) != sym)
  225. prop_warn(prop,
  226. "choice default symbol '%s' is not contained in the choice",
  227. sym2->name);
  228. }
  229. break;
  230. case P_SELECT:
  231. case P_IMPLY:
  232. use = prop->type == P_SELECT ? "select" : "imply";
  233. sym2 = prop_get_symbol(prop);
  234. if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE)
  235. prop_warn(prop,
  236. "config symbol '%s' uses %s, but is "
  237. "not bool or tristate", sym->name, use);
  238. else if (sym2->type != S_UNKNOWN &&
  239. sym2->type != S_BOOLEAN &&
  240. sym2->type != S_TRISTATE)
  241. prop_warn(prop,
  242. "'%s' has wrong type. '%s' only "
  243. "accept arguments of bool and "
  244. "tristate type", sym2->name, use);
  245. break;
  246. case P_RANGE:
  247. if (sym->type != S_INT && sym->type != S_HEX)
  248. prop_warn(prop, "range is only allowed "
  249. "for int or hex symbols");
  250. if (!menu_validate_number(sym, prop->expr->left.sym) ||
  251. !menu_validate_number(sym, prop->expr->right.sym))
  252. prop_warn(prop, "range is invalid");
  253. break;
  254. default:
  255. ;
  256. }
  257. }
  258. }
  259. void menu_finalize(struct menu *parent)
  260. {
  261. struct menu *menu, *last_menu;
  262. struct symbol *sym;
  263. struct property *prop;
  264. struct expr *parentdep, *basedep, *dep, *dep2, **ep;
  265. sym = parent->sym;
  266. if (parent->list) {
  267. /*
  268. * This menu node has children. We (recursively) process them
  269. * and propagate parent dependencies before moving on.
  270. */
  271. if (sym && sym_is_choice(sym)) {
  272. if (sym->type == S_UNKNOWN) {
  273. /* find the first choice value to find out choice type */
  274. current_entry = parent;
  275. for (menu = parent->list; menu; menu = menu->next) {
  276. if (menu->sym && menu->sym->type != S_UNKNOWN) {
  277. menu_set_type(menu->sym->type);
  278. break;
  279. }
  280. }
  281. }
  282. /* set the type of the remaining choice values */
  283. for (menu = parent->list; menu; menu = menu->next) {
  284. current_entry = menu;
  285. if (menu->sym && menu->sym->type == S_UNKNOWN)
  286. menu_set_type(sym->type);
  287. }
  288. /*
  289. * Use the choice itself as the parent dependency of
  290. * the contained items. This turns the mode of the
  291. * choice into an upper bound on the visibility of the
  292. * choice value symbols.
  293. */
  294. parentdep = expr_alloc_symbol(sym);
  295. } else if (parent->prompt)
  296. /* Menu node for 'menu' */
  297. parentdep = parent->prompt->visible.expr;
  298. else
  299. /* Menu node for 'if' */
  300. parentdep = parent->dep;
  301. /* For each child menu node... */
  302. for (menu = parent->list; menu; menu = menu->next) {
  303. /*
  304. * Propagate parent dependencies to the child menu
  305. * node, also rewriting and simplifying expressions
  306. */
  307. basedep = rewrite_m(menu->dep);
  308. basedep = expr_transform(basedep);
  309. basedep = expr_alloc_and(expr_copy(parentdep), basedep);
  310. basedep = expr_eliminate_dups(basedep);
  311. menu->dep = basedep;
  312. if (menu->sym)
  313. /*
  314. * Note: For symbols, all prompts are included
  315. * too in the symbol's own property list
  316. */
  317. prop = menu->sym->prop;
  318. else
  319. /*
  320. * For non-symbol menu nodes, we just need to
  321. * handle the prompt
  322. */
  323. prop = menu->prompt;
  324. /* For each property... */
  325. for (; prop; prop = prop->next) {
  326. if (prop->menu != menu)
  327. /*
  328. * Two possibilities:
  329. *
  330. * 1. The property lacks dependencies
  331. * and so isn't location-specific,
  332. * e.g. an 'option'
  333. *
  334. * 2. The property belongs to a symbol
  335. * defined in multiple locations and
  336. * is from some other location. It
  337. * will be handled there in that
  338. * case.
  339. *
  340. * Skip the property.
  341. */
  342. continue;
  343. /*
  344. * Propagate parent dependencies to the
  345. * property's condition, rewriting and
  346. * simplifying expressions at the same time
  347. */
  348. dep = rewrite_m(prop->visible.expr);
  349. dep = expr_transform(dep);
  350. dep = expr_alloc_and(expr_copy(basedep), dep);
  351. dep = expr_eliminate_dups(dep);
  352. if (menu->sym && menu->sym->type != S_TRISTATE)
  353. dep = expr_trans_bool(dep);
  354. prop->visible.expr = dep;
  355. /*
  356. * Handle selects and implies, which modify the
  357. * dependencies of the selected/implied symbol
  358. */
  359. if (prop->type == P_SELECT) {
  360. struct symbol *es = prop_get_symbol(prop);
  361. es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
  362. expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
  363. } else if (prop->type == P_IMPLY) {
  364. struct symbol *es = prop_get_symbol(prop);
  365. es->implied.expr = expr_alloc_or(es->implied.expr,
  366. expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
  367. }
  368. }
  369. }
  370. if (sym && sym_is_choice(sym))
  371. expr_free(parentdep);
  372. /*
  373. * Recursively process children in the same fashion before
  374. * moving on
  375. */
  376. for (menu = parent->list; menu; menu = menu->next)
  377. menu_finalize(menu);
  378. } else if (sym) {
  379. /*
  380. * Automatic submenu creation. If sym is a symbol and A, B, C,
  381. * ... are consecutive items (symbols, menus, ifs, etc.) that
  382. * all depend on sym, then the following menu structure is
  383. * created:
  384. *
  385. * sym
  386. * +-A
  387. * +-B
  388. * +-C
  389. * ...
  390. *
  391. * This also works recursively, giving the following structure
  392. * if A is a symbol and B depends on A:
  393. *
  394. * sym
  395. * +-A
  396. * | +-B
  397. * +-C
  398. * ...
  399. */
  400. basedep = parent->prompt ? parent->prompt->visible.expr : NULL;
  401. basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no);
  402. basedep = expr_eliminate_dups(expr_transform(basedep));
  403. /* Examine consecutive elements after sym */
  404. last_menu = NULL;
  405. for (menu = parent->next; menu; menu = menu->next) {
  406. dep = menu->prompt ? menu->prompt->visible.expr : menu->dep;
  407. if (!expr_contains_symbol(dep, sym))
  408. /* No dependency, quit */
  409. break;
  410. if (expr_depends_symbol(dep, sym))
  411. /* Absolute dependency, put in submenu */
  412. goto next;
  413. /*
  414. * Also consider it a dependency on sym if our
  415. * dependencies contain sym and are a "superset" of
  416. * sym's dependencies, e.g. '(sym || Q) && R' when sym
  417. * depends on R.
  418. *
  419. * Note that 'R' might be from an enclosing menu or if,
  420. * making this a more common case than it might seem.
  421. */
  422. dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no);
  423. dep = expr_eliminate_dups(expr_transform(dep));
  424. dep2 = expr_copy(basedep);
  425. expr_eliminate_eq(&dep, &dep2);
  426. expr_free(dep);
  427. if (!expr_is_yes(dep2)) {
  428. /* Not superset, quit */
  429. expr_free(dep2);
  430. break;
  431. }
  432. /* Superset, put in submenu */
  433. expr_free(dep2);
  434. next:
  435. menu_finalize(menu);
  436. menu->parent = parent;
  437. last_menu = menu;
  438. }
  439. expr_free(basedep);
  440. if (last_menu) {
  441. parent->list = parent->next;
  442. parent->next = last_menu->next;
  443. last_menu->next = NULL;
  444. }
  445. sym->dir_dep.expr = expr_alloc_or(sym->dir_dep.expr, parent->dep);
  446. }
  447. for (menu = parent->list; menu; menu = menu->next) {
  448. if (sym && sym_is_choice(sym) &&
  449. menu->sym && !sym_is_choice_value(menu->sym)) {
  450. current_entry = menu;
  451. menu->sym->flags |= SYMBOL_CHOICEVAL;
  452. if (!menu->prompt)
  453. menu_warn(menu, "choice value must have a prompt");
  454. for (prop = menu->sym->prop; prop; prop = prop->next) {
  455. if (prop->type == P_DEFAULT)
  456. prop_warn(prop, "defaults for choice "
  457. "values not supported");
  458. if (prop->menu == menu)
  459. continue;
  460. if (prop->type == P_PROMPT &&
  461. prop->menu->parent->sym != sym)
  462. prop_warn(prop, "choice value used outside its choice group");
  463. }
  464. /* Non-tristate choice values of tristate choices must
  465. * depend on the choice being set to Y. The choice
  466. * values' dependencies were propagated to their
  467. * properties above, so the change here must be re-
  468. * propagated.
  469. */
  470. if (sym->type == S_TRISTATE && menu->sym->type != S_TRISTATE) {
  471. basedep = expr_alloc_comp(E_EQUAL, sym, &symbol_yes);
  472. menu->dep = expr_alloc_and(basedep, menu->dep);
  473. for (prop = menu->sym->prop; prop; prop = prop->next) {
  474. if (prop->menu != menu)
  475. continue;
  476. prop->visible.expr = expr_alloc_and(expr_copy(basedep),
  477. prop->visible.expr);
  478. }
  479. }
  480. menu_add_symbol(P_CHOICE, sym, NULL);
  481. prop = sym_get_choice_prop(sym);
  482. for (ep = &prop->expr; *ep; ep = &(*ep)->left.expr)
  483. ;
  484. *ep = expr_alloc_one(E_LIST, NULL);
  485. (*ep)->right.sym = menu->sym;
  486. }
  487. /*
  488. * This code serves two purposes:
  489. *
  490. * (1) Flattening 'if' blocks, which do not specify a submenu
  491. * and only add dependencies.
  492. *
  493. * (Automatic submenu creation might still create a submenu
  494. * from an 'if' before this code runs.)
  495. *
  496. * (2) "Undoing" any automatic submenus created earlier below
  497. * promptless symbols.
  498. *
  499. * Before:
  500. *
  501. * A
  502. * if ... (or promptless symbol)
  503. * +-B
  504. * +-C
  505. * D
  506. *
  507. * After:
  508. *
  509. * A
  510. * if ... (or promptless symbol)
  511. * B
  512. * C
  513. * D
  514. */
  515. if (menu->list && (!menu->prompt || !menu->prompt->text)) {
  516. for (last_menu = menu->list; ; last_menu = last_menu->next) {
  517. last_menu->parent = parent;
  518. if (!last_menu->next)
  519. break;
  520. }
  521. last_menu->next = menu->next;
  522. menu->next = menu->list;
  523. menu->list = NULL;
  524. }
  525. }
  526. if (sym && !(sym->flags & SYMBOL_WARNED)) {
  527. if (sym->type == S_UNKNOWN)
  528. menu_warn(parent, "config symbol defined without type");
  529. if (sym_is_choice(sym) && !parent->prompt)
  530. menu_warn(parent, "choice must have a prompt");
  531. /* Check properties connected to this symbol */
  532. sym_check_prop(sym);
  533. sym->flags |= SYMBOL_WARNED;
  534. }
  535. /*
  536. * For non-optional choices, add a reverse dependency (corresponding to
  537. * a select) of '<visibility> && m'. This prevents the user from
  538. * setting the choice mode to 'n' when the choice is visible.
  539. *
  540. * This would also work for non-choice symbols, but only non-optional
  541. * choices clear SYMBOL_OPTIONAL as of writing. Choices are implemented
  542. * as a type of symbol.
  543. */
  544. if (sym && !sym_is_optional(sym) && parent->prompt) {
  545. sym->rev_dep.expr = expr_alloc_or(sym->rev_dep.expr,
  546. expr_alloc_and(parent->prompt->visible.expr,
  547. expr_alloc_symbol(&symbol_mod)));
  548. }
  549. }
  550. bool menu_has_prompt(struct menu *menu)
  551. {
  552. if (!menu->prompt)
  553. return false;
  554. return true;
  555. }
  556. /*
  557. * Determine if a menu is empty.
  558. * A menu is considered empty if it contains no or only
  559. * invisible entries.
  560. */
  561. bool menu_is_empty(struct menu *menu)
  562. {
  563. struct menu *child;
  564. for (child = menu->list; child; child = child->next) {
  565. if (menu_is_visible(child))
  566. return(false);
  567. }
  568. return(true);
  569. }
  570. bool menu_is_visible(struct menu *menu)
  571. {
  572. struct menu *child;
  573. struct symbol *sym;
  574. tristate visible;
  575. if (!menu->prompt)
  576. return false;
  577. if (menu->visibility) {
  578. if (expr_calc_value(menu->visibility) == no)
  579. return false;
  580. }
  581. sym = menu->sym;
  582. if (sym) {
  583. sym_calc_value(sym);
  584. visible = menu->prompt->visible.tri;
  585. } else
  586. visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr);
  587. if (visible != no)
  588. return true;
  589. if (!sym || sym_get_tristate_value(menu->sym) == no)
  590. return false;
  591. for (child = menu->list; child; child = child->next) {
  592. if (menu_is_visible(child)) {
  593. if (sym)
  594. sym->flags |= SYMBOL_DEF_USER;
  595. return true;
  596. }
  597. }
  598. return false;
  599. }
  600. const char *menu_get_prompt(struct menu *menu)
  601. {
  602. if (menu->prompt)
  603. return menu->prompt->text;
  604. else if (menu->sym)
  605. return menu->sym->name;
  606. return NULL;
  607. }
  608. struct menu *menu_get_root_menu(struct menu *menu)
  609. {
  610. return &rootmenu;
  611. }
  612. struct menu *menu_get_parent_menu(struct menu *menu)
  613. {
  614. enum prop_type type;
  615. for (; menu != &rootmenu; menu = menu->parent) {
  616. type = menu->prompt ? menu->prompt->type : 0;
  617. if (type == P_MENU)
  618. break;
  619. }
  620. return menu;
  621. }
  622. bool menu_has_help(struct menu *menu)
  623. {
  624. return menu->help != NULL;
  625. }
  626. const char *menu_get_help(struct menu *menu)
  627. {
  628. if (menu->help)
  629. return menu->help;
  630. else
  631. return "";
  632. }
  633. static void get_prompt_str(struct gstr *r, struct property *prop,
  634. struct list_head *head)
  635. {
  636. int i, j;
  637. struct menu *submenu[8], *menu, *location = NULL;
  638. struct jump_key *jump = NULL;
  639. str_printf(r, _("Prompt: %s\n"), _(prop->text));
  640. menu = prop->menu->parent;
  641. for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent) {
  642. bool accessible = menu_is_visible(menu);
  643. submenu[i++] = menu;
  644. if (location == NULL && accessible)
  645. location = menu;
  646. }
  647. if (head && location) {
  648. jump = xmalloc(sizeof(struct jump_key));
  649. if (menu_is_visible(prop->menu)) {
  650. /*
  651. * There is not enough room to put the hint at the
  652. * beginning of the "Prompt" line. Put the hint on the
  653. * last "Location" line even when it would belong on
  654. * the former.
  655. */
  656. jump->target = prop->menu;
  657. } else
  658. jump->target = location;
  659. if (list_empty(head))
  660. jump->index = 0;
  661. else
  662. jump->index = list_entry(head->prev, struct jump_key,
  663. entries)->index + 1;
  664. list_add_tail(&jump->entries, head);
  665. }
  666. if (i > 0) {
  667. str_printf(r, _(" Location:\n"));
  668. for (j = 4; --i >= 0; j += 2) {
  669. menu = submenu[i];
  670. if (jump && menu == location)
  671. jump->offset = strlen(r->s);
  672. str_printf(r, "%*c-> %s", j, ' ',
  673. _(menu_get_prompt(menu)));
  674. if (menu->sym) {
  675. str_printf(r, " (%s [=%s])", menu->sym->name ?
  676. menu->sym->name : _("<choice>"),
  677. sym_get_string_value(menu->sym));
  678. }
  679. str_append(r, "\n");
  680. }
  681. }
  682. }
  683. /*
  684. * get property of type P_SYMBOL
  685. */
  686. static struct property *get_symbol_prop(struct symbol *sym)
  687. {
  688. struct property *prop = NULL;
  689. for_all_properties(sym, prop, P_SYMBOL)
  690. break;
  691. return prop;
  692. }
  693. static void get_symbol_props_str(struct gstr *r, struct symbol *sym,
  694. enum prop_type tok, const char *prefix)
  695. {
  696. bool hit = false;
  697. struct property *prop;
  698. for_all_properties(sym, prop, tok) {
  699. if (!hit) {
  700. str_append(r, prefix);
  701. hit = true;
  702. } else
  703. str_printf(r, " && ");
  704. expr_gstr_print(prop->expr, r);
  705. }
  706. if (hit)
  707. str_append(r, "\n");
  708. }
  709. /*
  710. * head is optional and may be NULL
  711. */
  712. static void get_symbol_str(struct gstr *r, struct symbol *sym,
  713. struct list_head *head)
  714. {
  715. struct property *prop;
  716. if (sym && sym->name) {
  717. str_printf(r, "Symbol: %s [=%s]\n", sym->name,
  718. sym_get_string_value(sym));
  719. str_printf(r, "Type : %s\n", sym_type_name(sym->type));
  720. if (sym->type == S_INT || sym->type == S_HEX) {
  721. prop = sym_get_range_prop(sym);
  722. if (prop) {
  723. str_printf(r, "Range : ");
  724. expr_gstr_print(prop->expr, r);
  725. str_append(r, "\n");
  726. }
  727. }
  728. }
  729. for_all_prompts(sym, prop)
  730. get_prompt_str(r, prop, head);
  731. prop = get_symbol_prop(sym);
  732. if (prop) {
  733. str_printf(r, _(" Defined at %s:%d\n"), prop->menu->file->name,
  734. prop->menu->lineno);
  735. if (!expr_is_yes(prop->visible.expr)) {
  736. str_append(r, _(" Depends on: "));
  737. expr_gstr_print(prop->visible.expr, r);
  738. str_append(r, "\n");
  739. }
  740. }
  741. get_symbol_props_str(r, sym, P_SELECT, _(" Selects: "));
  742. if (sym->rev_dep.expr) {
  743. expr_gstr_print_revdep(sym->rev_dep.expr, r, yes, " Selected by [y]:\n");
  744. expr_gstr_print_revdep(sym->rev_dep.expr, r, mod, " Selected by [m]:\n");
  745. expr_gstr_print_revdep(sym->rev_dep.expr, r, no, " Selected by [n]:\n");
  746. }
  747. get_symbol_props_str(r, sym, P_IMPLY, _(" Implies: "));
  748. if (sym->implied.expr) {
  749. expr_gstr_print_revdep(sym->implied.expr, r, yes, " Implied by [y]:\n");
  750. expr_gstr_print_revdep(sym->implied.expr, r, mod, " Implied by [m]:\n");
  751. expr_gstr_print_revdep(sym->implied.expr, r, no, " Implied by [n]:\n");
  752. }
  753. str_append(r, "\n\n");
  754. }
  755. struct gstr get_relations_str(struct symbol **sym_arr, struct list_head *head)
  756. {
  757. struct symbol *sym;
  758. struct gstr res = str_new();
  759. int i;
  760. for (i = 0; sym_arr && (sym = sym_arr[i]); i++)
  761. get_symbol_str(&res, sym, head);
  762. if (!i)
  763. str_append(&res, _("No matches found.\n"));
  764. return res;
  765. }
  766. void menu_get_ext_help(struct menu *menu, struct gstr *help)
  767. {
  768. struct symbol *sym = menu->sym;
  769. const char *help_text = nohelp_text;
  770. if (menu_has_help(menu)) {
  771. if (sym->name)
  772. str_printf(help, "%s%s:\n\n", CONFIG_, sym->name);
  773. help_text = menu_get_help(menu);
  774. }
  775. str_printf(help, "%s\n", _(help_text));
  776. if (sym)
  777. get_symbol_str(help, sym, NULL);
  778. }