menu.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. void menu_end_entry(void)
  54. {
  55. }
  56. struct menu *menu_add_menu(void)
  57. {
  58. menu_end_entry();
  59. last_entry_ptr = &current_entry->list;
  60. return current_menu = current_entry;
  61. }
  62. void menu_end_menu(void)
  63. {
  64. last_entry_ptr = &current_menu->next;
  65. current_menu = current_menu->parent;
  66. }
  67. static struct expr *menu_check_dep(struct expr *e)
  68. {
  69. if (!e)
  70. return e;
  71. switch (e->type) {
  72. case E_NOT:
  73. e->left.expr = menu_check_dep(e->left.expr);
  74. break;
  75. case E_OR:
  76. case E_AND:
  77. e->left.expr = menu_check_dep(e->left.expr);
  78. e->right.expr = menu_check_dep(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, menu_check_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, "type of '%s' redefined from '%s' to '%s'",
  104. sym->name ? sym->name : "<choice>",
  105. sym_type_name(sym->type), sym_type_name(type));
  106. }
  107. struct property *menu_add_prop(enum prop_type type, char *prompt, struct expr *expr, struct expr *dep)
  108. {
  109. struct property *prop = prop_alloc(type, current_entry->sym);
  110. prop->menu = current_entry;
  111. prop->expr = expr;
  112. prop->visible.expr = menu_check_dep(dep);
  113. if (prompt) {
  114. if (isspace(*prompt)) {
  115. prop_warn(prop, "leading whitespace ignored");
  116. while (isspace(*prompt))
  117. prompt++;
  118. }
  119. if (current_entry->prompt && current_entry != &rootmenu)
  120. prop_warn(prop, "prompt redefined");
  121. /* Apply all upper menus' visibilities to actual prompts. */
  122. if(type == P_PROMPT) {
  123. struct menu *menu = current_entry;
  124. while ((menu = menu->parent) != NULL) {
  125. struct expr *dup_expr;
  126. if (!menu->visibility)
  127. continue;
  128. /*
  129. * Do not add a reference to the
  130. * menu's visibility expression but
  131. * use a copy of it. Otherwise the
  132. * expression reduction functions
  133. * will modify expressions that have
  134. * multiple references which can
  135. * cause unwanted side effects.
  136. */
  137. dup_expr = expr_copy(menu->visibility);
  138. prop->visible.expr
  139. = expr_alloc_and(prop->visible.expr,
  140. dup_expr);
  141. }
  142. }
  143. current_entry->prompt = prop;
  144. }
  145. prop->text = prompt;
  146. return prop;
  147. }
  148. struct property *menu_add_prompt(enum prop_type type, char *prompt, struct expr *dep)
  149. {
  150. return menu_add_prop(type, prompt, NULL, dep);
  151. }
  152. void menu_add_visibility(struct expr *expr)
  153. {
  154. current_entry->visibility = expr_alloc_and(current_entry->visibility,
  155. expr);
  156. }
  157. void menu_add_expr(enum prop_type type, struct expr *expr, struct expr *dep)
  158. {
  159. menu_add_prop(type, NULL, expr, dep);
  160. }
  161. void menu_add_symbol(enum prop_type type, struct symbol *sym, struct expr *dep)
  162. {
  163. menu_add_prop(type, NULL, expr_alloc_symbol(sym), dep);
  164. }
  165. void menu_add_option(int token, char *arg)
  166. {
  167. struct property *prop;
  168. switch (token) {
  169. case T_OPT_MODULES:
  170. prop = prop_alloc(P_DEFAULT, modules_sym);
  171. prop->expr = expr_alloc_symbol(current_entry->sym);
  172. break;
  173. case T_OPT_DEFCONFIG_LIST:
  174. if (!sym_defconfig_list)
  175. sym_defconfig_list = current_entry->sym;
  176. else if (sym_defconfig_list != current_entry->sym)
  177. zconf_error("trying to redefine defconfig symbol");
  178. break;
  179. case T_OPT_ENV:
  180. prop_add_env(arg);
  181. break;
  182. }
  183. }
  184. static int menu_validate_number(struct symbol *sym, struct symbol *sym2)
  185. {
  186. return sym2->type == S_INT || sym2->type == S_HEX ||
  187. (sym2->type == S_UNKNOWN && sym_string_valid(sym, sym2->name));
  188. }
  189. static void sym_check_prop(struct symbol *sym)
  190. {
  191. struct property *prop;
  192. struct symbol *sym2;
  193. for (prop = sym->prop; prop; prop = prop->next) {
  194. switch (prop->type) {
  195. case P_DEFAULT:
  196. if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
  197. prop->expr->type != E_SYMBOL)
  198. prop_warn(prop,
  199. "default for config symbol '%s'"
  200. " must be a single symbol", sym->name);
  201. if (prop->expr->type != E_SYMBOL)
  202. break;
  203. sym2 = prop_get_symbol(prop);
  204. if (sym->type == S_HEX || sym->type == S_INT) {
  205. if (!menu_validate_number(sym, sym2))
  206. prop_warn(prop,
  207. "'%s': number is invalid",
  208. sym->name);
  209. }
  210. break;
  211. case P_SELECT:
  212. sym2 = prop_get_symbol(prop);
  213. if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE)
  214. prop_warn(prop,
  215. "config symbol '%s' uses select, but is "
  216. "not boolean or tristate", sym->name);
  217. else if (sym2->type != S_UNKNOWN &&
  218. sym2->type != S_BOOLEAN &&
  219. sym2->type != S_TRISTATE)
  220. prop_warn(prop,
  221. "'%s' has wrong type. 'select' only "
  222. "accept arguments of boolean and "
  223. "tristate type", sym2->name);
  224. break;
  225. case P_RANGE:
  226. if (sym->type != S_INT && sym->type != S_HEX)
  227. prop_warn(prop, "range is only allowed "
  228. "for int or hex symbols");
  229. if (!menu_validate_number(sym, prop->expr->left.sym) ||
  230. !menu_validate_number(sym, prop->expr->right.sym))
  231. prop_warn(prop, "range is invalid");
  232. break;
  233. default:
  234. ;
  235. }
  236. }
  237. }
  238. void menu_finalize(struct menu *parent)
  239. {
  240. struct menu *menu, *last_menu;
  241. struct symbol *sym;
  242. struct property *prop;
  243. struct expr *parentdep, *basedep, *dep, *dep2, **ep;
  244. sym = parent->sym;
  245. if (parent->list) {
  246. if (sym && sym_is_choice(sym)) {
  247. if (sym->type == S_UNKNOWN) {
  248. /* find the first choice value to find out choice type */
  249. current_entry = parent;
  250. for (menu = parent->list; menu; menu = menu->next) {
  251. if (menu->sym && menu->sym->type != S_UNKNOWN) {
  252. menu_set_type(menu->sym->type);
  253. break;
  254. }
  255. }
  256. }
  257. /* set the type of the remaining choice values */
  258. for (menu = parent->list; menu; menu = menu->next) {
  259. current_entry = menu;
  260. if (menu->sym && menu->sym->type == S_UNKNOWN)
  261. menu_set_type(sym->type);
  262. }
  263. parentdep = expr_alloc_symbol(sym);
  264. } else if (parent->prompt)
  265. parentdep = parent->prompt->visible.expr;
  266. else
  267. parentdep = parent->dep;
  268. for (menu = parent->list; menu; menu = menu->next) {
  269. basedep = expr_transform(menu->dep);
  270. basedep = expr_alloc_and(expr_copy(parentdep), basedep);
  271. basedep = expr_eliminate_dups(basedep);
  272. menu->dep = basedep;
  273. if (menu->sym)
  274. prop = menu->sym->prop;
  275. else
  276. prop = menu->prompt;
  277. for (; prop; prop = prop->next) {
  278. if (prop->menu != menu)
  279. continue;
  280. dep = expr_transform(prop->visible.expr);
  281. dep = expr_alloc_and(expr_copy(basedep), dep);
  282. dep = expr_eliminate_dups(dep);
  283. if (menu->sym && menu->sym->type != S_TRISTATE)
  284. dep = expr_trans_bool(dep);
  285. prop->visible.expr = dep;
  286. if (prop->type == P_SELECT) {
  287. struct symbol *es = prop_get_symbol(prop);
  288. es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
  289. expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
  290. }
  291. }
  292. }
  293. for (menu = parent->list; menu; menu = menu->next)
  294. menu_finalize(menu);
  295. } else if (sym) {
  296. basedep = parent->prompt ? parent->prompt->visible.expr : NULL;
  297. basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no);
  298. basedep = expr_eliminate_dups(expr_transform(basedep));
  299. last_menu = NULL;
  300. for (menu = parent->next; menu; menu = menu->next) {
  301. dep = menu->prompt ? menu->prompt->visible.expr : menu->dep;
  302. if (!expr_contains_symbol(dep, sym))
  303. break;
  304. if (expr_depends_symbol(dep, sym))
  305. goto next;
  306. dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no);
  307. dep = expr_eliminate_dups(expr_transform(dep));
  308. dep2 = expr_copy(basedep);
  309. expr_eliminate_eq(&dep, &dep2);
  310. expr_free(dep);
  311. if (!expr_is_yes(dep2)) {
  312. expr_free(dep2);
  313. break;
  314. }
  315. expr_free(dep2);
  316. next:
  317. menu_finalize(menu);
  318. menu->parent = parent;
  319. last_menu = menu;
  320. }
  321. if (last_menu) {
  322. parent->list = parent->next;
  323. parent->next = last_menu->next;
  324. last_menu->next = NULL;
  325. }
  326. sym->dir_dep.expr = expr_alloc_or(sym->dir_dep.expr, parent->dep);
  327. }
  328. for (menu = parent->list; menu; menu = menu->next) {
  329. if (sym && sym_is_choice(sym) &&
  330. menu->sym && !sym_is_choice_value(menu->sym)) {
  331. current_entry = menu;
  332. menu->sym->flags |= SYMBOL_CHOICEVAL;
  333. if (!menu->prompt)
  334. menu_warn(menu, "choice value must have a prompt");
  335. for (prop = menu->sym->prop; prop; prop = prop->next) {
  336. if (prop->type == P_DEFAULT)
  337. prop_warn(prop, "defaults for choice "
  338. "values not supported");
  339. if (prop->menu == menu)
  340. continue;
  341. if (prop->type == P_PROMPT &&
  342. prop->menu->parent->sym != sym)
  343. prop_warn(prop, "choice value used outside its choice group");
  344. }
  345. /* Non-tristate choice values of tristate choices must
  346. * depend on the choice being set to Y. The choice
  347. * values' dependencies were propagated to their
  348. * properties above, so the change here must be re-
  349. * propagated.
  350. */
  351. if (sym->type == S_TRISTATE && menu->sym->type != S_TRISTATE) {
  352. basedep = expr_alloc_comp(E_EQUAL, sym, &symbol_yes);
  353. menu->dep = expr_alloc_and(basedep, menu->dep);
  354. for (prop = menu->sym->prop; prop; prop = prop->next) {
  355. if (prop->menu != menu)
  356. continue;
  357. prop->visible.expr = expr_alloc_and(expr_copy(basedep),
  358. prop->visible.expr);
  359. }
  360. }
  361. menu_add_symbol(P_CHOICE, sym, NULL);
  362. prop = sym_get_choice_prop(sym);
  363. for (ep = &prop->expr; *ep; ep = &(*ep)->left.expr)
  364. ;
  365. *ep = expr_alloc_one(E_LIST, NULL);
  366. (*ep)->right.sym = menu->sym;
  367. }
  368. if (menu->list && (!menu->prompt || !menu->prompt->text)) {
  369. for (last_menu = menu->list; ; last_menu = last_menu->next) {
  370. last_menu->parent = parent;
  371. if (!last_menu->next)
  372. break;
  373. }
  374. last_menu->next = menu->next;
  375. menu->next = menu->list;
  376. menu->list = NULL;
  377. }
  378. }
  379. if (sym && !(sym->flags & SYMBOL_WARNED)) {
  380. if (sym->type == S_UNKNOWN)
  381. menu_warn(parent, "config symbol defined without type");
  382. if (sym_is_choice(sym) && !parent->prompt)
  383. menu_warn(parent, "choice must have a prompt");
  384. /* Check properties connected to this symbol */
  385. sym_check_prop(sym);
  386. sym->flags |= SYMBOL_WARNED;
  387. }
  388. if (sym && !sym_is_optional(sym) && parent->prompt) {
  389. sym->rev_dep.expr = expr_alloc_or(sym->rev_dep.expr,
  390. expr_alloc_and(parent->prompt->visible.expr,
  391. expr_alloc_symbol(&symbol_mod)));
  392. }
  393. }
  394. bool menu_has_prompt(struct menu *menu)
  395. {
  396. if (!menu->prompt)
  397. return false;
  398. return true;
  399. }
  400. /*
  401. * Determine if a menu is empty.
  402. * A menu is considered empty if it contains no or only
  403. * invisible entries.
  404. */
  405. bool menu_is_empty(struct menu *menu)
  406. {
  407. struct menu *child;
  408. for (child = menu->list; child; child = child->next) {
  409. if (menu_is_visible(child))
  410. return(false);
  411. }
  412. return(true);
  413. }
  414. bool menu_is_visible(struct menu *menu)
  415. {
  416. struct menu *child;
  417. struct symbol *sym;
  418. tristate visible;
  419. if (!menu->prompt)
  420. return false;
  421. if (menu->visibility) {
  422. if (expr_calc_value(menu->visibility) == no)
  423. return no;
  424. }
  425. sym = menu->sym;
  426. if (sym) {
  427. sym_calc_value(sym);
  428. visible = menu->prompt->visible.tri;
  429. } else
  430. visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr);
  431. if (visible != no)
  432. return true;
  433. if (!sym || sym_get_tristate_value(menu->sym) == no)
  434. return false;
  435. for (child = menu->list; child; child = child->next) {
  436. if (menu_is_visible(child)) {
  437. if (sym)
  438. sym->flags |= SYMBOL_DEF_USER;
  439. return true;
  440. }
  441. }
  442. return false;
  443. }
  444. const char *menu_get_prompt(struct menu *menu)
  445. {
  446. if (menu->prompt)
  447. return menu->prompt->text;
  448. else if (menu->sym)
  449. return menu->sym->name;
  450. return NULL;
  451. }
  452. struct menu *menu_get_root_menu(struct menu *menu)
  453. {
  454. return &rootmenu;
  455. }
  456. struct menu *menu_get_parent_menu(struct menu *menu)
  457. {
  458. enum prop_type type;
  459. for (; menu != &rootmenu; menu = menu->parent) {
  460. type = menu->prompt ? menu->prompt->type : 0;
  461. if (type == P_MENU)
  462. break;
  463. }
  464. return menu;
  465. }
  466. bool menu_has_help(struct menu *menu)
  467. {
  468. return menu->help != NULL;
  469. }
  470. const char *menu_get_help(struct menu *menu)
  471. {
  472. if (menu->help)
  473. return menu->help;
  474. else
  475. return "";
  476. }
  477. static void get_prompt_str(struct gstr *r, struct property *prop,
  478. struct list_head *head)
  479. {
  480. int i, j;
  481. struct menu *submenu[8], *menu, *location = NULL;
  482. struct jump_key *jump;
  483. str_printf(r, _("Prompt: %s\n"), _(prop->text));
  484. menu = prop->menu->parent;
  485. for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent) {
  486. bool accessible = menu_is_visible(menu);
  487. submenu[i++] = menu;
  488. if (location == NULL && accessible)
  489. location = menu;
  490. }
  491. if (head && location) {
  492. jump = xmalloc(sizeof(struct jump_key));
  493. if (menu_is_visible(prop->menu)) {
  494. /*
  495. * There is not enough room to put the hint at the
  496. * beginning of the "Prompt" line. Put the hint on the
  497. * last "Location" line even when it would belong on
  498. * the former.
  499. */
  500. jump->target = prop->menu;
  501. } else
  502. jump->target = location;
  503. if (list_empty(head))
  504. jump->index = 0;
  505. else
  506. jump->index = list_entry(head->prev, struct jump_key,
  507. entries)->index + 1;
  508. list_add_tail(&jump->entries, head);
  509. }
  510. if (i > 0) {
  511. str_printf(r, _(" Location:\n"));
  512. for (j = 4; --i >= 0; j += 2) {
  513. menu = submenu[i];
  514. if (head && location && menu == location)
  515. jump->offset = r->len - 1;
  516. str_printf(r, "%*c-> %s", j, ' ',
  517. _(menu_get_prompt(menu)));
  518. if (menu->sym) {
  519. str_printf(r, " (%s [=%s])", menu->sym->name ?
  520. menu->sym->name : _("<choice>"),
  521. sym_get_string_value(menu->sym));
  522. }
  523. str_append(r, "\n");
  524. }
  525. }
  526. }
  527. /*
  528. * get peoperty of type P_SYMBOL
  529. */
  530. static struct property *get_symbol_prop(struct symbol *sym)
  531. {
  532. struct property *prop = NULL;
  533. for_all_properties(sym, prop, P_SYMBOL)
  534. break;
  535. return prop;
  536. }
  537. /*
  538. * head is optional and may be NULL
  539. */
  540. void get_symbol_str(struct gstr *r, struct symbol *sym,
  541. struct list_head *head)
  542. {
  543. bool hit;
  544. struct property *prop;
  545. if (sym && sym->name) {
  546. str_printf(r, "Symbol: %s [=%s]\n", sym->name,
  547. sym_get_string_value(sym));
  548. str_printf(r, "Type : %s\n", sym_type_name(sym->type));
  549. if (sym->type == S_INT || sym->type == S_HEX) {
  550. prop = sym_get_range_prop(sym);
  551. if (prop) {
  552. str_printf(r, "Range : ");
  553. expr_gstr_print(prop->expr, r);
  554. str_append(r, "\n");
  555. }
  556. }
  557. }
  558. for_all_prompts(sym, prop)
  559. get_prompt_str(r, prop, head);
  560. prop = get_symbol_prop(sym);
  561. if (prop) {
  562. str_printf(r, _(" Defined at %s:%d\n"), prop->menu->file->name,
  563. prop->menu->lineno);
  564. if (!expr_is_yes(prop->visible.expr)) {
  565. str_append(r, _(" Depends on: "));
  566. expr_gstr_print(prop->visible.expr, r);
  567. str_append(r, "\n");
  568. }
  569. }
  570. hit = false;
  571. for_all_properties(sym, prop, P_SELECT) {
  572. if (!hit) {
  573. str_append(r, " Selects: ");
  574. hit = true;
  575. } else
  576. str_printf(r, " && ");
  577. expr_gstr_print(prop->expr, r);
  578. }
  579. if (hit)
  580. str_append(r, "\n");
  581. if (sym->rev_dep.expr) {
  582. str_append(r, _(" Selected by: "));
  583. expr_gstr_print(sym->rev_dep.expr, r);
  584. str_append(r, "\n");
  585. }
  586. str_append(r, "\n\n");
  587. }
  588. struct gstr get_relations_str(struct symbol **sym_arr, struct list_head *head)
  589. {
  590. struct symbol *sym;
  591. struct gstr res = str_new();
  592. int i;
  593. for (i = 0; sym_arr && (sym = sym_arr[i]); i++)
  594. get_symbol_str(&res, sym, head);
  595. if (!i)
  596. str_append(&res, _("No matches found.\n"));
  597. return res;
  598. }
  599. void menu_get_ext_help(struct menu *menu, struct gstr *help)
  600. {
  601. struct symbol *sym = menu->sym;
  602. const char *help_text = nohelp_text;
  603. if (menu_has_help(menu)) {
  604. if (sym->name)
  605. str_printf(help, "%s%s:\n\n", CONFIG_, sym->name);
  606. help_text = menu_get_help(menu);
  607. }
  608. str_printf(help, "%s\n", _(help_text));
  609. if (sym)
  610. get_symbol_str(help, sym, NULL);
  611. }