menu.c 13 KB

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