menu.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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_NO_WRITE;
  184. break;
  185. case T_OPT_ALLNOCONFIG_Y:
  186. current_entry->sym->flags |= SYMBOL_ALLNOCONFIG_Y;
  187. break;
  188. }
  189. }
  190. static int menu_validate_number(struct symbol *sym, struct symbol *sym2)
  191. {
  192. return sym2->type == S_INT || sym2->type == S_HEX ||
  193. (sym2->type == S_UNKNOWN && sym_string_valid(sym, sym2->name));
  194. }
  195. static void sym_check_prop(struct symbol *sym)
  196. {
  197. struct property *prop;
  198. struct symbol *sym2;
  199. char *use;
  200. for (prop = sym->prop; prop; prop = prop->next) {
  201. switch (prop->type) {
  202. case P_DEFAULT:
  203. if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
  204. prop->expr->type != E_SYMBOL)
  205. prop_warn(prop,
  206. "default for config symbol '%s'"
  207. " must be a single symbol", sym->name);
  208. if (prop->expr->type != E_SYMBOL)
  209. break;
  210. sym2 = prop_get_symbol(prop);
  211. if (sym->type == S_HEX || sym->type == S_INT) {
  212. if (!menu_validate_number(sym, sym2))
  213. prop_warn(prop,
  214. "'%s': number is invalid",
  215. sym->name);
  216. }
  217. if (sym_is_choice(sym)) {
  218. struct property *choice_prop =
  219. sym_get_choice_prop(sym2);
  220. if (!choice_prop ||
  221. prop_get_symbol(choice_prop) != sym)
  222. prop_warn(prop,
  223. "choice default symbol '%s' is not contained in the choice",
  224. sym2->name);
  225. }
  226. break;
  227. case P_SELECT:
  228. case P_IMPLY:
  229. use = prop->type == P_SELECT ? "select" : "imply";
  230. sym2 = prop_get_symbol(prop);
  231. if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE)
  232. prop_warn(prop,
  233. "config symbol '%s' uses %s, but is "
  234. "not bool or tristate", sym->name, use);
  235. else if (sym2->type != S_UNKNOWN &&
  236. sym2->type != S_BOOLEAN &&
  237. sym2->type != S_TRISTATE)
  238. prop_warn(prop,
  239. "'%s' has wrong type. '%s' only "
  240. "accept arguments of bool and "
  241. "tristate type", sym2->name, use);
  242. break;
  243. case P_RANGE:
  244. if (sym->type != S_INT && sym->type != S_HEX)
  245. prop_warn(prop, "range is only allowed "
  246. "for int or hex symbols");
  247. if (!menu_validate_number(sym, prop->expr->left.sym) ||
  248. !menu_validate_number(sym, prop->expr->right.sym))
  249. prop_warn(prop, "range is invalid");
  250. break;
  251. default:
  252. ;
  253. }
  254. }
  255. }
  256. void menu_finalize(struct menu *parent)
  257. {
  258. struct menu *menu, *last_menu;
  259. struct symbol *sym;
  260. struct property *prop;
  261. struct expr *parentdep, *basedep, *dep, *dep2, **ep;
  262. sym = parent->sym;
  263. if (parent->list) {
  264. /*
  265. * This menu node has children. We (recursively) process them
  266. * and propagate parent dependencies before moving on.
  267. */
  268. if (sym && sym_is_choice(sym)) {
  269. if (sym->type == S_UNKNOWN) {
  270. /* find the first choice value to find out choice type */
  271. current_entry = parent;
  272. for (menu = parent->list; menu; menu = menu->next) {
  273. if (menu->sym && menu->sym->type != S_UNKNOWN) {
  274. menu_set_type(menu->sym->type);
  275. break;
  276. }
  277. }
  278. }
  279. /* set the type of the remaining choice values */
  280. for (menu = parent->list; menu; menu = menu->next) {
  281. current_entry = menu;
  282. if (menu->sym && menu->sym->type == S_UNKNOWN)
  283. menu_set_type(sym->type);
  284. }
  285. /*
  286. * Use the choice itself as the parent dependency of
  287. * the contained items. This turns the mode of the
  288. * choice into an upper bound on the visibility of the
  289. * choice value symbols.
  290. */
  291. parentdep = expr_alloc_symbol(sym);
  292. } else if (parent->prompt)
  293. /* Menu node for 'menu' */
  294. parentdep = parent->prompt->visible.expr;
  295. else
  296. /* Menu node for 'if' */
  297. parentdep = parent->dep;
  298. /* For each child menu node... */
  299. for (menu = parent->list; menu; menu = menu->next) {
  300. /*
  301. * Propagate parent dependencies to the child menu
  302. * node, also rewriting and simplifying expressions
  303. */
  304. basedep = rewrite_m(menu->dep);
  305. basedep = expr_transform(basedep);
  306. basedep = expr_alloc_and(expr_copy(parentdep), basedep);
  307. basedep = expr_eliminate_dups(basedep);
  308. menu->dep = basedep;
  309. if (menu->sym)
  310. /*
  311. * Note: For symbols, all prompts are included
  312. * too in the symbol's own property list
  313. */
  314. prop = menu->sym->prop;
  315. else
  316. /*
  317. * For non-symbol menu nodes, we just need to
  318. * handle the prompt
  319. */
  320. prop = menu->prompt;
  321. /* For each property... */
  322. for (; prop; prop = prop->next) {
  323. if (prop->menu != menu)
  324. /*
  325. * Two possibilities:
  326. *
  327. * 1. The property lacks dependencies
  328. * and so isn't location-specific,
  329. * e.g. an 'option'
  330. *
  331. * 2. The property belongs to a symbol
  332. * defined in multiple locations and
  333. * is from some other location. It
  334. * will be handled there in that
  335. * case.
  336. *
  337. * Skip the property.
  338. */
  339. continue;
  340. /*
  341. * Propagate parent dependencies to the
  342. * property's condition, rewriting and
  343. * simplifying expressions at the same time
  344. */
  345. dep = rewrite_m(prop->visible.expr);
  346. dep = expr_transform(dep);
  347. dep = expr_alloc_and(expr_copy(basedep), dep);
  348. dep = expr_eliminate_dups(dep);
  349. if (menu->sym && menu->sym->type != S_TRISTATE)
  350. dep = expr_trans_bool(dep);
  351. prop->visible.expr = dep;
  352. /*
  353. * Handle selects and implies, which modify the
  354. * dependencies of the selected/implied symbol
  355. */
  356. if (prop->type == P_SELECT) {
  357. struct symbol *es = prop_get_symbol(prop);
  358. es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
  359. expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
  360. } else if (prop->type == P_IMPLY) {
  361. struct symbol *es = prop_get_symbol(prop);
  362. es->implied.expr = expr_alloc_or(es->implied.expr,
  363. expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
  364. }
  365. }
  366. }
  367. if (sym && sym_is_choice(sym))
  368. expr_free(parentdep);
  369. /*
  370. * Recursively process children in the same fashion before
  371. * moving on
  372. */
  373. for (menu = parent->list; menu; menu = menu->next)
  374. menu_finalize(menu);
  375. } else if (sym) {
  376. /*
  377. * Automatic submenu creation. If sym is a symbol and A, B, C,
  378. * ... are consecutive items (symbols, menus, ifs, etc.) that
  379. * all depend on sym, then the following menu structure is
  380. * created:
  381. *
  382. * sym
  383. * +-A
  384. * +-B
  385. * +-C
  386. * ...
  387. *
  388. * This also works recursively, giving the following structure
  389. * if A is a symbol and B depends on A:
  390. *
  391. * sym
  392. * +-A
  393. * | +-B
  394. * +-C
  395. * ...
  396. */
  397. basedep = parent->prompt ? parent->prompt->visible.expr : NULL;
  398. basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no);
  399. basedep = expr_eliminate_dups(expr_transform(basedep));
  400. /* Examine consecutive elements after sym */
  401. last_menu = NULL;
  402. for (menu = parent->next; menu; menu = menu->next) {
  403. dep = menu->prompt ? menu->prompt->visible.expr : menu->dep;
  404. if (!expr_contains_symbol(dep, sym))
  405. /* No dependency, quit */
  406. break;
  407. if (expr_depends_symbol(dep, sym))
  408. /* Absolute dependency, put in submenu */
  409. goto next;
  410. /*
  411. * Also consider it a dependency on sym if our
  412. * dependencies contain sym and are a "superset" of
  413. * sym's dependencies, e.g. '(sym || Q) && R' when sym
  414. * depends on R.
  415. *
  416. * Note that 'R' might be from an enclosing menu or if,
  417. * making this a more common case than it might seem.
  418. */
  419. dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no);
  420. dep = expr_eliminate_dups(expr_transform(dep));
  421. dep2 = expr_copy(basedep);
  422. expr_eliminate_eq(&dep, &dep2);
  423. expr_free(dep);
  424. if (!expr_is_yes(dep2)) {
  425. /* Not superset, quit */
  426. expr_free(dep2);
  427. break;
  428. }
  429. /* Superset, put in submenu */
  430. expr_free(dep2);
  431. next:
  432. menu_finalize(menu);
  433. menu->parent = parent;
  434. last_menu = menu;
  435. }
  436. expr_free(basedep);
  437. if (last_menu) {
  438. parent->list = parent->next;
  439. parent->next = last_menu->next;
  440. last_menu->next = NULL;
  441. }
  442. sym->dir_dep.expr = expr_alloc_or(sym->dir_dep.expr, parent->dep);
  443. }
  444. for (menu = parent->list; menu; menu = menu->next) {
  445. if (sym && sym_is_choice(sym) &&
  446. menu->sym && !sym_is_choice_value(menu->sym)) {
  447. current_entry = menu;
  448. menu->sym->flags |= SYMBOL_CHOICEVAL;
  449. if (!menu->prompt)
  450. menu_warn(menu, "choice value must have a prompt");
  451. for (prop = menu->sym->prop; prop; prop = prop->next) {
  452. if (prop->type == P_DEFAULT)
  453. prop_warn(prop, "defaults for choice "
  454. "values not supported");
  455. if (prop->menu == menu)
  456. continue;
  457. if (prop->type == P_PROMPT &&
  458. prop->menu->parent->sym != sym)
  459. prop_warn(prop, "choice value used outside its choice group");
  460. }
  461. /* Non-tristate choice values of tristate choices must
  462. * depend on the choice being set to Y. The choice
  463. * values' dependencies were propagated to their
  464. * properties above, so the change here must be re-
  465. * propagated.
  466. */
  467. if (sym->type == S_TRISTATE && menu->sym->type != S_TRISTATE) {
  468. basedep = expr_alloc_comp(E_EQUAL, sym, &symbol_yes);
  469. menu->dep = expr_alloc_and(basedep, menu->dep);
  470. for (prop = menu->sym->prop; prop; prop = prop->next) {
  471. if (prop->menu != menu)
  472. continue;
  473. prop->visible.expr = expr_alloc_and(expr_copy(basedep),
  474. prop->visible.expr);
  475. }
  476. }
  477. menu_add_symbol(P_CHOICE, sym, NULL);
  478. prop = sym_get_choice_prop(sym);
  479. for (ep = &prop->expr; *ep; ep = &(*ep)->left.expr)
  480. ;
  481. *ep = expr_alloc_one(E_LIST, NULL);
  482. (*ep)->right.sym = menu->sym;
  483. }
  484. /*
  485. * This code serves two purposes:
  486. *
  487. * (1) Flattening 'if' blocks, which do not specify a submenu
  488. * and only add dependencies.
  489. *
  490. * (Automatic submenu creation might still create a submenu
  491. * from an 'if' before this code runs.)
  492. *
  493. * (2) "Undoing" any automatic submenus created earlier below
  494. * promptless symbols.
  495. *
  496. * Before:
  497. *
  498. * A
  499. * if ... (or promptless symbol)
  500. * +-B
  501. * +-C
  502. * D
  503. *
  504. * After:
  505. *
  506. * A
  507. * if ... (or promptless symbol)
  508. * B
  509. * C
  510. * D
  511. */
  512. if (menu->list && (!menu->prompt || !menu->prompt->text)) {
  513. for (last_menu = menu->list; ; last_menu = last_menu->next) {
  514. last_menu->parent = parent;
  515. if (!last_menu->next)
  516. break;
  517. }
  518. last_menu->next = menu->next;
  519. menu->next = menu->list;
  520. menu->list = NULL;
  521. }
  522. }
  523. if (sym && !(sym->flags & SYMBOL_WARNED)) {
  524. if (sym->type == S_UNKNOWN)
  525. menu_warn(parent, "config symbol defined without type");
  526. if (sym_is_choice(sym) && !parent->prompt)
  527. menu_warn(parent, "choice must have a prompt");
  528. /* Check properties connected to this symbol */
  529. sym_check_prop(sym);
  530. sym->flags |= SYMBOL_WARNED;
  531. }
  532. /*
  533. * For non-optional choices, add a reverse dependency (corresponding to
  534. * a select) of '<visibility> && m'. This prevents the user from
  535. * setting the choice mode to 'n' when the choice is visible.
  536. *
  537. * This would also work for non-choice symbols, but only non-optional
  538. * choices clear SYMBOL_OPTIONAL as of writing. Choices are implemented
  539. * as a type of symbol.
  540. */
  541. if (sym && !sym_is_optional(sym) && parent->prompt) {
  542. sym->rev_dep.expr = expr_alloc_or(sym->rev_dep.expr,
  543. expr_alloc_and(parent->prompt->visible.expr,
  544. expr_alloc_symbol(&symbol_mod)));
  545. }
  546. }
  547. bool menu_has_prompt(struct menu *menu)
  548. {
  549. if (!menu->prompt)
  550. return false;
  551. return true;
  552. }
  553. /*
  554. * Determine if a menu is empty.
  555. * A menu is considered empty if it contains no or only
  556. * invisible entries.
  557. */
  558. bool menu_is_empty(struct menu *menu)
  559. {
  560. struct menu *child;
  561. for (child = menu->list; child; child = child->next) {
  562. if (menu_is_visible(child))
  563. return(false);
  564. }
  565. return(true);
  566. }
  567. bool menu_is_visible(struct menu *menu)
  568. {
  569. struct menu *child;
  570. struct symbol *sym;
  571. tristate visible;
  572. if (!menu->prompt)
  573. return false;
  574. if (menu->visibility) {
  575. if (expr_calc_value(menu->visibility) == no)
  576. return false;
  577. }
  578. sym = menu->sym;
  579. if (sym) {
  580. sym_calc_value(sym);
  581. visible = menu->prompt->visible.tri;
  582. } else
  583. visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr);
  584. if (visible != no)
  585. return true;
  586. if (!sym || sym_get_tristate_value(menu->sym) == no)
  587. return false;
  588. for (child = menu->list; child; child = child->next) {
  589. if (menu_is_visible(child)) {
  590. if (sym)
  591. sym->flags |= SYMBOL_DEF_USER;
  592. return true;
  593. }
  594. }
  595. return false;
  596. }
  597. const char *menu_get_prompt(struct menu *menu)
  598. {
  599. if (menu->prompt)
  600. return menu->prompt->text;
  601. else if (menu->sym)
  602. return menu->sym->name;
  603. return NULL;
  604. }
  605. struct menu *menu_get_root_menu(struct menu *menu)
  606. {
  607. return &rootmenu;
  608. }
  609. struct menu *menu_get_parent_menu(struct menu *menu)
  610. {
  611. enum prop_type type;
  612. for (; menu != &rootmenu; menu = menu->parent) {
  613. type = menu->prompt ? menu->prompt->type : 0;
  614. if (type == P_MENU)
  615. break;
  616. }
  617. return menu;
  618. }
  619. bool menu_has_help(struct menu *menu)
  620. {
  621. return menu->help != NULL;
  622. }
  623. const char *menu_get_help(struct menu *menu)
  624. {
  625. if (menu->help)
  626. return menu->help;
  627. else
  628. return "";
  629. }
  630. static void get_prompt_str(struct gstr *r, struct property *prop,
  631. struct list_head *head)
  632. {
  633. int i, j;
  634. struct menu *submenu[8], *menu, *location = NULL;
  635. struct jump_key *jump = NULL;
  636. str_printf(r, "Prompt: %s\n", prop->text);
  637. menu = prop->menu->parent;
  638. for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent) {
  639. bool accessible = menu_is_visible(menu);
  640. submenu[i++] = menu;
  641. if (location == NULL && accessible)
  642. location = menu;
  643. }
  644. if (head && location) {
  645. jump = xmalloc(sizeof(struct jump_key));
  646. if (menu_is_visible(prop->menu)) {
  647. /*
  648. * There is not enough room to put the hint at the
  649. * beginning of the "Prompt" line. Put the hint on the
  650. * last "Location" line even when it would belong on
  651. * the former.
  652. */
  653. jump->target = prop->menu;
  654. } else
  655. jump->target = location;
  656. if (list_empty(head))
  657. jump->index = 0;
  658. else
  659. jump->index = list_entry(head->prev, struct jump_key,
  660. entries)->index + 1;
  661. list_add_tail(&jump->entries, head);
  662. }
  663. if (i > 0) {
  664. str_printf(r, " Location:\n");
  665. for (j = 4; --i >= 0; j += 2) {
  666. menu = submenu[i];
  667. if (jump && menu == location)
  668. jump->offset = strlen(r->s);
  669. str_printf(r, "%*c-> %s", j, ' ',
  670. menu_get_prompt(menu));
  671. if (menu->sym) {
  672. str_printf(r, " (%s [=%s])", menu->sym->name ?
  673. menu->sym->name : "<choice>",
  674. sym_get_string_value(menu->sym));
  675. }
  676. str_append(r, "\n");
  677. }
  678. }
  679. }
  680. /*
  681. * get property of type P_SYMBOL
  682. */
  683. static struct property *get_symbol_prop(struct symbol *sym)
  684. {
  685. struct property *prop = NULL;
  686. for_all_properties(sym, prop, P_SYMBOL)
  687. break;
  688. return prop;
  689. }
  690. static void get_symbol_props_str(struct gstr *r, struct symbol *sym,
  691. enum prop_type tok, const char *prefix)
  692. {
  693. bool hit = false;
  694. struct property *prop;
  695. for_all_properties(sym, prop, tok) {
  696. if (!hit) {
  697. str_append(r, prefix);
  698. hit = true;
  699. } else
  700. str_printf(r, " && ");
  701. expr_gstr_print(prop->expr, r);
  702. }
  703. if (hit)
  704. str_append(r, "\n");
  705. }
  706. /*
  707. * head is optional and may be NULL
  708. */
  709. static void get_symbol_str(struct gstr *r, struct symbol *sym,
  710. struct list_head *head)
  711. {
  712. struct property *prop;
  713. if (sym && sym->name) {
  714. str_printf(r, "Symbol: %s [=%s]\n", sym->name,
  715. sym_get_string_value(sym));
  716. str_printf(r, "Type : %s\n", sym_type_name(sym->type));
  717. if (sym->type == S_INT || sym->type == S_HEX) {
  718. prop = sym_get_range_prop(sym);
  719. if (prop) {
  720. str_printf(r, "Range : ");
  721. expr_gstr_print(prop->expr, r);
  722. str_append(r, "\n");
  723. }
  724. }
  725. }
  726. for_all_prompts(sym, prop)
  727. get_prompt_str(r, prop, head);
  728. prop = get_symbol_prop(sym);
  729. if (prop) {
  730. str_printf(r, " Defined at %s:%d\n", prop->menu->file->name,
  731. prop->menu->lineno);
  732. if (!expr_is_yes(prop->visible.expr)) {
  733. str_append(r, " Depends on: ");
  734. expr_gstr_print(prop->visible.expr, r);
  735. str_append(r, "\n");
  736. }
  737. }
  738. get_symbol_props_str(r, sym, P_SELECT, " Selects: ");
  739. if (sym->rev_dep.expr) {
  740. expr_gstr_print_revdep(sym->rev_dep.expr, r, yes, " Selected by [y]:\n");
  741. expr_gstr_print_revdep(sym->rev_dep.expr, r, mod, " Selected by [m]:\n");
  742. expr_gstr_print_revdep(sym->rev_dep.expr, r, no, " Selected by [n]:\n");
  743. }
  744. get_symbol_props_str(r, sym, P_IMPLY, " Implies: ");
  745. if (sym->implied.expr) {
  746. expr_gstr_print_revdep(sym->implied.expr, r, yes, " Implied by [y]:\n");
  747. expr_gstr_print_revdep(sym->implied.expr, r, mod, " Implied by [m]:\n");
  748. expr_gstr_print_revdep(sym->implied.expr, r, no, " Implied by [n]:\n");
  749. }
  750. str_append(r, "\n\n");
  751. }
  752. struct gstr get_relations_str(struct symbol **sym_arr, struct list_head *head)
  753. {
  754. struct symbol *sym;
  755. struct gstr res = str_new();
  756. int i;
  757. for (i = 0; sym_arr && (sym = sym_arr[i]); i++)
  758. get_symbol_str(&res, sym, head);
  759. if (!i)
  760. str_append(&res, "No matches found.\n");
  761. return res;
  762. }
  763. void menu_get_ext_help(struct menu *menu, struct gstr *help)
  764. {
  765. struct symbol *sym = menu->sym;
  766. const char *help_text = nohelp_text;
  767. if (menu_has_help(menu)) {
  768. if (sym->name)
  769. str_printf(help, "%s%s:\n\n", CONFIG_, sym->name);
  770. help_text = menu_get_help(menu);
  771. }
  772. str_printf(help, "%s\n", help_text);
  773. if (sym)
  774. get_symbol_str(help, sym, NULL);
  775. }