menu.c 14 KB

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