menu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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[] = N_(
  11. "There is no help available for this option.\n");
  12. struct menu rootmenu;
  13. static struct menu **last_entry_ptr;
  14. struct file *file_list;
  15. struct file *current_file;
  16. void menu_warn(struct menu *menu, const char *fmt, ...)
  17. {
  18. va_list ap;
  19. va_start(ap, fmt);
  20. fprintf(stderr, "%s:%d:warning: ", menu->file->name, menu->lineno);
  21. vfprintf(stderr, fmt, ap);
  22. fprintf(stderr, "\n");
  23. va_end(ap);
  24. }
  25. static void prop_warn(struct property *prop, const char *fmt, ...)
  26. {
  27. va_list ap;
  28. va_start(ap, fmt);
  29. fprintf(stderr, "%s:%d:warning: ", prop->file->name, prop->lineno);
  30. vfprintf(stderr, fmt, ap);
  31. fprintf(stderr, "\n");
  32. va_end(ap);
  33. }
  34. void _menu_init(void)
  35. {
  36. current_entry = current_menu = &rootmenu;
  37. last_entry_ptr = &rootmenu.list;
  38. }
  39. void menu_add_entry(struct symbol *sym)
  40. {
  41. struct menu *menu;
  42. menu = malloc(sizeof(*menu));
  43. memset(menu, 0, sizeof(*menu));
  44. menu->sym = sym;
  45. menu->parent = current_menu;
  46. menu->file = current_file;
  47. menu->lineno = zconf_lineno();
  48. *last_entry_ptr = menu;
  49. last_entry_ptr = &menu->next;
  50. current_entry = menu;
  51. if (sym)
  52. menu_add_symbol(P_SYMBOL, sym, NULL);
  53. }
  54. void menu_end_entry(void)
  55. {
  56. }
  57. struct menu *menu_add_menu(void)
  58. {
  59. menu_end_entry();
  60. last_entry_ptr = &current_entry->list;
  61. return current_menu = current_entry;
  62. }
  63. void menu_end_menu(void)
  64. {
  65. last_entry_ptr = &current_menu->next;
  66. current_menu = current_menu->parent;
  67. }
  68. static struct expr *menu_check_dep(struct expr *e)
  69. {
  70. if (!e)
  71. return e;
  72. switch (e->type) {
  73. case E_NOT:
  74. e->left.expr = menu_check_dep(e->left.expr);
  75. break;
  76. case E_OR:
  77. case E_AND:
  78. e->left.expr = menu_check_dep(e->left.expr);
  79. e->right.expr = menu_check_dep(e->right.expr);
  80. break;
  81. case E_SYMBOL:
  82. /* change 'm' into 'm' && MODULES */
  83. if (e->left.sym == &symbol_mod)
  84. return expr_alloc_and(e, expr_alloc_symbol(modules_sym));
  85. break;
  86. default:
  87. break;
  88. }
  89. return e;
  90. }
  91. void menu_add_dep(struct expr *dep)
  92. {
  93. current_entry->dep = expr_alloc_and(current_entry->dep, menu_check_dep(dep));
  94. }
  95. void menu_set_type(int type)
  96. {
  97. struct symbol *sym = current_entry->sym;
  98. if (sym->type == type)
  99. return;
  100. if (sym->type == S_UNKNOWN) {
  101. sym->type = type;
  102. return;
  103. }
  104. menu_warn(current_entry, "type of '%s' redefined from '%s' to '%s'",
  105. sym->name ? sym->name : "<choice>",
  106. sym_type_name(sym->type), sym_type_name(type));
  107. }
  108. 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 = menu_check_dep(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. if (!menu->visibility)
  127. continue;
  128. prop->visible.expr
  129. = expr_alloc_and(prop->visible.expr,
  130. menu->visibility);
  131. }
  132. }
  133. current_entry->prompt = prop;
  134. }
  135. prop->text = prompt;
  136. return prop;
  137. }
  138. struct property *menu_add_prompt(enum prop_type type, char *prompt, struct expr *dep)
  139. {
  140. return menu_add_prop(type, prompt, NULL, dep);
  141. }
  142. void menu_add_visibility(struct expr *expr)
  143. {
  144. current_entry->visibility = expr_alloc_and(current_entry->visibility,
  145. expr);
  146. }
  147. void menu_add_expr(enum prop_type type, struct expr *expr, struct expr *dep)
  148. {
  149. menu_add_prop(type, NULL, expr, dep);
  150. }
  151. void menu_add_symbol(enum prop_type type, struct symbol *sym, struct expr *dep)
  152. {
  153. menu_add_prop(type, NULL, expr_alloc_symbol(sym), dep);
  154. }
  155. void menu_add_option(int token, char *arg)
  156. {
  157. struct property *prop;
  158. switch (token) {
  159. case T_OPT_MODULES:
  160. prop = prop_alloc(P_DEFAULT, modules_sym);
  161. prop->expr = expr_alloc_symbol(current_entry->sym);
  162. break;
  163. case T_OPT_DEFCONFIG_LIST:
  164. if (!sym_defconfig_list)
  165. sym_defconfig_list = current_entry->sym;
  166. else if (sym_defconfig_list != current_entry->sym)
  167. zconf_error("trying to redefine defconfig symbol");
  168. break;
  169. case T_OPT_ENV:
  170. prop_add_env(arg);
  171. break;
  172. }
  173. }
  174. static int menu_validate_number(struct symbol *sym, struct symbol *sym2)
  175. {
  176. return sym2->type == S_INT || sym2->type == S_HEX ||
  177. (sym2->type == S_UNKNOWN && sym_string_valid(sym, sym2->name));
  178. }
  179. static void sym_check_prop(struct symbol *sym)
  180. {
  181. struct property *prop;
  182. struct symbol *sym2;
  183. for (prop = sym->prop; prop; prop = prop->next) {
  184. switch (prop->type) {
  185. case P_DEFAULT:
  186. if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
  187. prop->expr->type != E_SYMBOL)
  188. prop_warn(prop,
  189. "default for config symbol '%s'"
  190. " must be a single symbol", sym->name);
  191. if (prop->expr->type != E_SYMBOL)
  192. break;
  193. sym2 = prop_get_symbol(prop);
  194. if (sym->type == S_HEX || sym->type == S_INT) {
  195. if (!menu_validate_number(sym, sym2))
  196. prop_warn(prop,
  197. "'%s': number is invalid",
  198. sym->name);
  199. }
  200. break;
  201. case P_SELECT:
  202. sym2 = prop_get_symbol(prop);
  203. if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE)
  204. prop_warn(prop,
  205. "config symbol '%s' uses select, but is "
  206. "not boolean or tristate", sym->name);
  207. else if (sym2->type != S_UNKNOWN &&
  208. sym2->type != S_BOOLEAN &&
  209. sym2->type != S_TRISTATE)
  210. prop_warn(prop,
  211. "'%s' has wrong type. 'select' only "
  212. "accept arguments of boolean and "
  213. "tristate type", sym2->name);
  214. break;
  215. case P_RANGE:
  216. if (sym->type != S_INT && sym->type != S_HEX)
  217. prop_warn(prop, "range is only allowed "
  218. "for int or hex symbols");
  219. if (!menu_validate_number(sym, prop->expr->left.sym) ||
  220. !menu_validate_number(sym, prop->expr->right.sym))
  221. prop_warn(prop, "range is invalid");
  222. break;
  223. default:
  224. ;
  225. }
  226. }
  227. }
  228. void menu_finalize(struct menu *parent)
  229. {
  230. struct menu *menu, *last_menu;
  231. struct symbol *sym;
  232. struct property *prop;
  233. struct expr *parentdep, *basedep, *dep, *dep2, **ep;
  234. sym = parent->sym;
  235. if (parent->list) {
  236. if (sym && sym_is_choice(sym)) {
  237. if (sym->type == S_UNKNOWN) {
  238. /* find the first choice value to find out choice type */
  239. current_entry = parent;
  240. for (menu = parent->list; menu; menu = menu->next) {
  241. if (menu->sym && menu->sym->type != S_UNKNOWN) {
  242. menu_set_type(menu->sym->type);
  243. break;
  244. }
  245. }
  246. }
  247. /* set the type of the remaining choice values */
  248. for (menu = parent->list; menu; menu = menu->next) {
  249. current_entry = menu;
  250. if (menu->sym && menu->sym->type == S_UNKNOWN)
  251. menu_set_type(sym->type);
  252. }
  253. parentdep = expr_alloc_symbol(sym);
  254. } else if (parent->prompt)
  255. parentdep = parent->prompt->visible.expr;
  256. else
  257. parentdep = parent->dep;
  258. for (menu = parent->list; menu; menu = menu->next) {
  259. basedep = expr_transform(menu->dep);
  260. basedep = expr_alloc_and(expr_copy(parentdep), basedep);
  261. basedep = expr_eliminate_dups(basedep);
  262. menu->dep = basedep;
  263. if (menu->sym)
  264. prop = menu->sym->prop;
  265. else
  266. prop = menu->prompt;
  267. for (; prop; prop = prop->next) {
  268. if (prop->menu != menu)
  269. continue;
  270. dep = expr_transform(prop->visible.expr);
  271. dep = expr_alloc_and(expr_copy(basedep), dep);
  272. dep = expr_eliminate_dups(dep);
  273. if (menu->sym && menu->sym->type != S_TRISTATE)
  274. dep = expr_trans_bool(dep);
  275. prop->visible.expr = dep;
  276. if (prop->type == P_SELECT) {
  277. struct symbol *es = prop_get_symbol(prop);
  278. es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
  279. expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
  280. }
  281. }
  282. }
  283. for (menu = parent->list; menu; menu = menu->next)
  284. menu_finalize(menu);
  285. } else if (sym) {
  286. basedep = parent->prompt ? parent->prompt->visible.expr : NULL;
  287. basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no);
  288. basedep = expr_eliminate_dups(expr_transform(basedep));
  289. last_menu = NULL;
  290. for (menu = parent->next; menu; menu = menu->next) {
  291. dep = menu->prompt ? menu->prompt->visible.expr : menu->dep;
  292. if (!expr_contains_symbol(dep, sym))
  293. break;
  294. if (expr_depends_symbol(dep, sym))
  295. goto next;
  296. dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no);
  297. dep = expr_eliminate_dups(expr_transform(dep));
  298. dep2 = expr_copy(basedep);
  299. expr_eliminate_eq(&dep, &dep2);
  300. expr_free(dep);
  301. if (!expr_is_yes(dep2)) {
  302. expr_free(dep2);
  303. break;
  304. }
  305. expr_free(dep2);
  306. next:
  307. menu_finalize(menu);
  308. menu->parent = parent;
  309. last_menu = menu;
  310. }
  311. if (last_menu) {
  312. parent->list = parent->next;
  313. parent->next = last_menu->next;
  314. last_menu->next = NULL;
  315. }
  316. sym->dir_dep.expr = expr_alloc_or(sym->dir_dep.expr, parent->dep);
  317. }
  318. for (menu = parent->list; menu; menu = menu->next) {
  319. if (sym && sym_is_choice(sym) &&
  320. menu->sym && !sym_is_choice_value(menu->sym)) {
  321. current_entry = menu;
  322. menu->sym->flags |= SYMBOL_CHOICEVAL;
  323. if (!menu->prompt)
  324. menu_warn(menu, "choice value must have a prompt");
  325. for (prop = menu->sym->prop; prop; prop = prop->next) {
  326. if (prop->type == P_DEFAULT)
  327. prop_warn(prop, "defaults for choice "
  328. "values not supported");
  329. if (prop->menu == menu)
  330. continue;
  331. if (prop->type == P_PROMPT &&
  332. prop->menu->parent->sym != sym)
  333. prop_warn(prop, "choice value used outside its choice group");
  334. }
  335. /* Non-tristate choice values of tristate choices must
  336. * depend on the choice being set to Y. The choice
  337. * values' dependencies were propagated to their
  338. * properties above, so the change here must be re-
  339. * propagated.
  340. */
  341. if (sym->type == S_TRISTATE && menu->sym->type != S_TRISTATE) {
  342. basedep = expr_alloc_comp(E_EQUAL, sym, &symbol_yes);
  343. menu->dep = expr_alloc_and(basedep, menu->dep);
  344. for (prop = menu->sym->prop; prop; prop = prop->next) {
  345. if (prop->menu != menu)
  346. continue;
  347. prop->visible.expr = expr_alloc_and(expr_copy(basedep),
  348. prop->visible.expr);
  349. }
  350. }
  351. menu_add_symbol(P_CHOICE, sym, NULL);
  352. prop = sym_get_choice_prop(sym);
  353. for (ep = &prop->expr; *ep; ep = &(*ep)->left.expr)
  354. ;
  355. *ep = expr_alloc_one(E_LIST, NULL);
  356. (*ep)->right.sym = menu->sym;
  357. }
  358. if (menu->list && (!menu->prompt || !menu->prompt->text)) {
  359. for (last_menu = menu->list; ; last_menu = last_menu->next) {
  360. last_menu->parent = parent;
  361. if (!last_menu->next)
  362. break;
  363. }
  364. last_menu->next = menu->next;
  365. menu->next = menu->list;
  366. menu->list = NULL;
  367. }
  368. }
  369. if (sym && !(sym->flags & SYMBOL_WARNED)) {
  370. if (sym->type == S_UNKNOWN)
  371. menu_warn(parent, "config symbol defined without type");
  372. if (sym_is_choice(sym) && !parent->prompt)
  373. menu_warn(parent, "choice must have a prompt");
  374. /* Check properties connected to this symbol */
  375. sym_check_prop(sym);
  376. sym->flags |= SYMBOL_WARNED;
  377. }
  378. if (sym && !sym_is_optional(sym) && parent->prompt) {
  379. sym->rev_dep.expr = expr_alloc_or(sym->rev_dep.expr,
  380. expr_alloc_and(parent->prompt->visible.expr,
  381. expr_alloc_symbol(&symbol_mod)));
  382. }
  383. }
  384. bool menu_has_prompt(struct menu *menu)
  385. {
  386. if (!menu->prompt)
  387. return false;
  388. return true;
  389. }
  390. bool menu_is_visible(struct menu *menu)
  391. {
  392. struct menu *child;
  393. struct symbol *sym;
  394. tristate visible;
  395. if (!menu->prompt)
  396. return false;
  397. if (menu->visibility) {
  398. if (expr_calc_value(menu->visibility) == no)
  399. return no;
  400. }
  401. sym = menu->sym;
  402. if (sym) {
  403. sym_calc_value(sym);
  404. visible = menu->prompt->visible.tri;
  405. } else
  406. visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr);
  407. if (visible != no)
  408. return true;
  409. if (!sym || sym_get_tristate_value(menu->sym) == no)
  410. return false;
  411. for (child = menu->list; child; child = child->next) {
  412. if (menu_is_visible(child)) {
  413. if (sym)
  414. sym->flags |= SYMBOL_DEF_USER;
  415. return true;
  416. }
  417. }
  418. return false;
  419. }
  420. const char *menu_get_prompt(struct menu *menu)
  421. {
  422. if (menu->prompt)
  423. return menu->prompt->text;
  424. else if (menu->sym)
  425. return menu->sym->name;
  426. return NULL;
  427. }
  428. struct menu *menu_get_root_menu(struct menu *menu)
  429. {
  430. return &rootmenu;
  431. }
  432. struct menu *menu_get_parent_menu(struct menu *menu)
  433. {
  434. enum prop_type type;
  435. for (; menu != &rootmenu; menu = menu->parent) {
  436. type = menu->prompt ? menu->prompt->type : 0;
  437. if (type == P_MENU)
  438. break;
  439. }
  440. return menu;
  441. }
  442. bool menu_has_help(struct menu *menu)
  443. {
  444. return menu->help != NULL;
  445. }
  446. const char *menu_get_help(struct menu *menu)
  447. {
  448. if (menu->help)
  449. return menu->help;
  450. else
  451. return "";
  452. }
  453. static void get_prompt_str(struct gstr *r, struct property *prop)
  454. {
  455. int i, j;
  456. struct menu *submenu[8], *menu;
  457. str_printf(r, _("Prompt: %s\n"), _(prop->text));
  458. str_printf(r, _(" Defined at %s:%d\n"), prop->menu->file->name,
  459. prop->menu->lineno);
  460. if (!expr_is_yes(prop->visible.expr)) {
  461. str_append(r, _(" Depends on: "));
  462. expr_gstr_print(prop->visible.expr, r);
  463. str_append(r, "\n");
  464. }
  465. menu = prop->menu->parent;
  466. for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent)
  467. submenu[i++] = menu;
  468. if (i > 0) {
  469. str_printf(r, _(" Location:\n"));
  470. for (j = 4; --i >= 0; j += 2) {
  471. menu = submenu[i];
  472. str_printf(r, "%*c-> %s", j, ' ', _(menu_get_prompt(menu)));
  473. if (menu->sym) {
  474. str_printf(r, " (%s [=%s])", menu->sym->name ?
  475. menu->sym->name : _("<choice>"),
  476. sym_get_string_value(menu->sym));
  477. }
  478. str_append(r, "\n");
  479. }
  480. }
  481. }
  482. void get_symbol_str(struct gstr *r, struct symbol *sym)
  483. {
  484. bool hit;
  485. struct property *prop;
  486. if (sym && sym->name) {
  487. str_printf(r, "Symbol: %s [=%s]\n", sym->name,
  488. sym_get_string_value(sym));
  489. str_printf(r, "Type : %s\n", sym_type_name(sym->type));
  490. if (sym->type == S_INT || sym->type == S_HEX) {
  491. prop = sym_get_range_prop(sym);
  492. if (prop) {
  493. str_printf(r, "Range : ");
  494. expr_gstr_print(prop->expr, r);
  495. str_append(r, "\n");
  496. }
  497. }
  498. }
  499. for_all_prompts(sym, prop)
  500. get_prompt_str(r, prop);
  501. hit = false;
  502. for_all_properties(sym, prop, P_SELECT) {
  503. if (!hit) {
  504. str_append(r, " Selects: ");
  505. hit = true;
  506. } else
  507. str_printf(r, " && ");
  508. expr_gstr_print(prop->expr, r);
  509. }
  510. if (hit)
  511. str_append(r, "\n");
  512. if (sym->rev_dep.expr) {
  513. str_append(r, _(" Selected by: "));
  514. expr_gstr_print(sym->rev_dep.expr, r);
  515. str_append(r, "\n");
  516. }
  517. str_append(r, "\n\n");
  518. }
  519. struct gstr get_relations_str(struct symbol **sym_arr)
  520. {
  521. struct symbol *sym;
  522. struct gstr res = str_new();
  523. int i;
  524. for (i = 0; sym_arr && (sym = sym_arr[i]); i++)
  525. get_symbol_str(&res, sym);
  526. if (!i)
  527. str_append(&res, _("No matches found.\n"));
  528. return res;
  529. }
  530. void menu_get_ext_help(struct menu *menu, struct gstr *help)
  531. {
  532. struct symbol *sym = menu->sym;
  533. if (menu_has_help(menu)) {
  534. if (sym->name) {
  535. str_printf(help, "%s%s:\n\n", CONFIG_, sym->name);
  536. str_append(help, _(menu_get_help(menu)));
  537. str_append(help, "\n");
  538. }
  539. } else {
  540. str_append(help, nohelp_text);
  541. }
  542. if (sym)
  543. get_symbol_str(help, sym);
  544. }