menu.c 16 KB

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