menu.c 21 KB

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