menu.c 22 KB

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