menu.c 20 KB

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