menu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. struct menu rootmenu;
  10. static struct menu **last_entry_ptr;
  11. struct file *file_list;
  12. struct file *current_file;
  13. void menu_warn(struct menu *menu, const char *fmt, ...)
  14. {
  15. va_list ap;
  16. va_start(ap, fmt);
  17. fprintf(stderr, "%s:%d:warning: ", menu->file->name, menu->lineno);
  18. vfprintf(stderr, fmt, ap);
  19. fprintf(stderr, "\n");
  20. va_end(ap);
  21. }
  22. static void prop_warn(struct property *prop, const char *fmt, ...)
  23. {
  24. va_list ap;
  25. va_start(ap, fmt);
  26. fprintf(stderr, "%s:%d:warning: ", prop->file->name, prop->lineno);
  27. vfprintf(stderr, fmt, ap);
  28. fprintf(stderr, "\n");
  29. va_end(ap);
  30. }
  31. void menu_init(void)
  32. {
  33. current_entry = current_menu = &rootmenu;
  34. last_entry_ptr = &rootmenu.list;
  35. }
  36. void menu_add_entry(struct symbol *sym)
  37. {
  38. struct menu *menu;
  39. menu = malloc(sizeof(*menu));
  40. memset(menu, 0, sizeof(*menu));
  41. menu->sym = sym;
  42. menu->parent = current_menu;
  43. menu->file = current_file;
  44. menu->lineno = zconf_lineno();
  45. *last_entry_ptr = menu;
  46. last_entry_ptr = &menu->next;
  47. current_entry = menu;
  48. }
  49. void menu_end_entry(void)
  50. {
  51. }
  52. struct menu *menu_add_menu(void)
  53. {
  54. menu_end_entry();
  55. last_entry_ptr = &current_entry->list;
  56. return current_menu = current_entry;
  57. }
  58. void menu_end_menu(void)
  59. {
  60. last_entry_ptr = &current_menu->next;
  61. current_menu = current_menu->parent;
  62. }
  63. struct expr *menu_check_dep(struct expr *e)
  64. {
  65. if (!e)
  66. return e;
  67. switch (e->type) {
  68. case E_NOT:
  69. e->left.expr = menu_check_dep(e->left.expr);
  70. break;
  71. case E_OR:
  72. case E_AND:
  73. e->left.expr = menu_check_dep(e->left.expr);
  74. e->right.expr = menu_check_dep(e->right.expr);
  75. break;
  76. case E_SYMBOL:
  77. /* change 'm' into 'm' && MODULES */
  78. if (e->left.sym == &symbol_mod)
  79. return expr_alloc_and(e, expr_alloc_symbol(modules_sym));
  80. break;
  81. default:
  82. break;
  83. }
  84. return e;
  85. }
  86. void menu_add_dep(struct expr *dep)
  87. {
  88. current_entry->dep = expr_alloc_and(current_entry->dep, menu_check_dep(dep));
  89. }
  90. void menu_set_type(int type)
  91. {
  92. struct symbol *sym = current_entry->sym;
  93. if (sym->type == type)
  94. return;
  95. if (sym->type == S_UNKNOWN) {
  96. sym->type = type;
  97. return;
  98. }
  99. menu_warn(current_entry, "type of '%s' redefined from '%s' to '%s'",
  100. sym->name ? sym->name : "<choice>",
  101. sym_type_name(sym->type), sym_type_name(type));
  102. }
  103. struct property *menu_add_prop(enum prop_type type, char *prompt, struct expr *expr, struct expr *dep)
  104. {
  105. struct property *prop = prop_alloc(type, current_entry->sym);
  106. prop->menu = current_entry;
  107. prop->expr = expr;
  108. prop->visible.expr = menu_check_dep(dep);
  109. if (prompt) {
  110. if (isspace(*prompt)) {
  111. prop_warn(prop, "leading whitespace ignored");
  112. while (isspace(*prompt))
  113. prompt++;
  114. }
  115. if (current_entry->prompt)
  116. prop_warn(prop, "prompt redefined");
  117. current_entry->prompt = prop;
  118. }
  119. prop->text = prompt;
  120. return prop;
  121. }
  122. struct property *menu_add_prompt(enum prop_type type, char *prompt, struct expr *dep)
  123. {
  124. return menu_add_prop(type, prompt, NULL, dep);
  125. }
  126. void menu_add_expr(enum prop_type type, struct expr *expr, struct expr *dep)
  127. {
  128. menu_add_prop(type, NULL, expr, dep);
  129. }
  130. void menu_add_symbol(enum prop_type type, struct symbol *sym, struct expr *dep)
  131. {
  132. menu_add_prop(type, NULL, expr_alloc_symbol(sym), dep);
  133. }
  134. void menu_add_option(int token, char *arg)
  135. {
  136. struct property *prop;
  137. switch (token) {
  138. case T_OPT_MODULES:
  139. prop = prop_alloc(P_DEFAULT, modules_sym);
  140. prop->expr = expr_alloc_symbol(current_entry->sym);
  141. break;
  142. case T_OPT_DEFCONFIG_LIST:
  143. if (!sym_defconfig_list)
  144. sym_defconfig_list = current_entry->sym;
  145. else if (sym_defconfig_list != current_entry->sym)
  146. zconf_error("trying to redefine defconfig symbol");
  147. break;
  148. case T_OPT_ENV:
  149. prop_add_env(arg);
  150. break;
  151. }
  152. }
  153. static int menu_range_valid_sym(struct symbol *sym, struct symbol *sym2)
  154. {
  155. return sym2->type == S_INT || sym2->type == S_HEX ||
  156. (sym2->type == S_UNKNOWN && sym_string_valid(sym, sym2->name));
  157. }
  158. void sym_check_prop(struct symbol *sym)
  159. {
  160. struct property *prop;
  161. struct symbol *sym2;
  162. for (prop = sym->prop; prop; prop = prop->next) {
  163. switch (prop->type) {
  164. case P_DEFAULT:
  165. if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
  166. prop->expr->type != E_SYMBOL)
  167. prop_warn(prop,
  168. "default for config symbol '%'"
  169. " must be a single symbol", sym->name);
  170. break;
  171. case P_SELECT:
  172. sym2 = prop_get_symbol(prop);
  173. if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE)
  174. prop_warn(prop,
  175. "config symbol '%s' uses select, but is "
  176. "not boolean or tristate", sym->name);
  177. else if (sym2->type == S_UNKNOWN)
  178. prop_warn(prop,
  179. "'select' used by config symbol '%s' "
  180. "refers to undefined symbol '%s'",
  181. sym->name, sym2->name);
  182. else if (sym2->type != S_BOOLEAN && sym2->type != S_TRISTATE)
  183. prop_warn(prop,
  184. "'%s' has wrong type. 'select' only "
  185. "accept arguments of boolean and "
  186. "tristate type", sym2->name);
  187. break;
  188. case P_RANGE:
  189. if (sym->type != S_INT && sym->type != S_HEX)
  190. prop_warn(prop, "range is only allowed "
  191. "for int or hex symbols");
  192. if (!menu_range_valid_sym(sym, prop->expr->left.sym) ||
  193. !menu_range_valid_sym(sym, prop->expr->right.sym))
  194. prop_warn(prop, "range is invalid");
  195. break;
  196. default:
  197. ;
  198. }
  199. }
  200. }
  201. void menu_finalize(struct menu *parent)
  202. {
  203. struct menu *menu, *last_menu;
  204. struct symbol *sym;
  205. struct property *prop;
  206. struct expr *parentdep, *basedep, *dep, *dep2, **ep;
  207. sym = parent->sym;
  208. if (parent->list) {
  209. if (sym && sym_is_choice(sym)) {
  210. /* find the first choice value and find out choice type */
  211. for (menu = parent->list; menu; menu = menu->next) {
  212. if (menu->sym) {
  213. current_entry = parent;
  214. if (sym->type == S_UNKNOWN)
  215. menu_set_type(menu->sym->type);
  216. current_entry = menu;
  217. if (menu->sym->type == S_UNKNOWN)
  218. menu_set_type(sym->type);
  219. break;
  220. }
  221. }
  222. parentdep = expr_alloc_symbol(sym);
  223. } else if (parent->prompt)
  224. parentdep = parent->prompt->visible.expr;
  225. else
  226. parentdep = parent->dep;
  227. for (menu = parent->list; menu; menu = menu->next) {
  228. basedep = expr_transform(menu->dep);
  229. basedep = expr_alloc_and(expr_copy(parentdep), basedep);
  230. basedep = expr_eliminate_dups(basedep);
  231. menu->dep = basedep;
  232. if (menu->sym)
  233. prop = menu->sym->prop;
  234. else
  235. prop = menu->prompt;
  236. for (; prop; prop = prop->next) {
  237. if (prop->menu != menu)
  238. continue;
  239. dep = expr_transform(prop->visible.expr);
  240. dep = expr_alloc_and(expr_copy(basedep), dep);
  241. dep = expr_eliminate_dups(dep);
  242. if (menu->sym && menu->sym->type != S_TRISTATE)
  243. dep = expr_trans_bool(dep);
  244. prop->visible.expr = dep;
  245. if (prop->type == P_SELECT) {
  246. struct symbol *es = prop_get_symbol(prop);
  247. es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
  248. expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
  249. }
  250. }
  251. }
  252. for (menu = parent->list; menu; menu = menu->next)
  253. menu_finalize(menu);
  254. } else if (sym) {
  255. basedep = parent->prompt ? parent->prompt->visible.expr : NULL;
  256. basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no);
  257. basedep = expr_eliminate_dups(expr_transform(basedep));
  258. last_menu = NULL;
  259. for (menu = parent->next; menu; menu = menu->next) {
  260. dep = menu->prompt ? menu->prompt->visible.expr : menu->dep;
  261. if (!expr_contains_symbol(dep, sym))
  262. break;
  263. if (expr_depends_symbol(dep, sym))
  264. goto next;
  265. dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no);
  266. dep = expr_eliminate_dups(expr_transform(dep));
  267. dep2 = expr_copy(basedep);
  268. expr_eliminate_eq(&dep, &dep2);
  269. expr_free(dep);
  270. if (!expr_is_yes(dep2)) {
  271. expr_free(dep2);
  272. break;
  273. }
  274. expr_free(dep2);
  275. next:
  276. menu_finalize(menu);
  277. menu->parent = parent;
  278. last_menu = menu;
  279. }
  280. if (last_menu) {
  281. parent->list = parent->next;
  282. parent->next = last_menu->next;
  283. last_menu->next = NULL;
  284. }
  285. }
  286. for (menu = parent->list; menu; menu = menu->next) {
  287. if (sym && sym_is_choice(sym) && menu->sym) {
  288. menu->sym->flags |= SYMBOL_CHOICEVAL;
  289. if (!menu->prompt)
  290. menu_warn(menu, "choice value must have a prompt");
  291. for (prop = menu->sym->prop; prop; prop = prop->next) {
  292. if (prop->type == P_PROMPT && prop->menu != menu) {
  293. prop_warn(prop, "choice values "
  294. "currently only support a "
  295. "single prompt");
  296. }
  297. if (prop->type == P_DEFAULT)
  298. prop_warn(prop, "defaults for choice "
  299. "values not supported");
  300. }
  301. current_entry = menu;
  302. if (menu->sym->type == S_UNKNOWN)
  303. menu_set_type(sym->type);
  304. /* Non-tristate choice values of tristate choices must
  305. * depend on the choice being set to Y. The choice
  306. * values' dependencies were propagated to their
  307. * properties above, so the change here must be re-
  308. * propagated. */
  309. if (sym->type == S_TRISTATE && menu->sym->type != S_TRISTATE) {
  310. basedep = expr_alloc_comp(E_EQUAL, sym, &symbol_yes);
  311. basedep = expr_alloc_and(basedep, menu->dep);
  312. basedep = expr_eliminate_dups(basedep);
  313. menu->dep = basedep;
  314. for (prop = menu->sym->prop; prop; prop = prop->next) {
  315. if (prop->menu != menu)
  316. continue;
  317. dep = expr_alloc_and(expr_copy(basedep),
  318. prop->visible.expr);
  319. dep = expr_eliminate_dups(dep);
  320. dep = expr_trans_bool(dep);
  321. prop->visible.expr = dep;
  322. if (prop->type == P_SELECT) {
  323. struct symbol *es = prop_get_symbol(prop);
  324. dep2 = expr_alloc_symbol(menu->sym);
  325. dep = expr_alloc_and(dep2,
  326. expr_copy(dep));
  327. dep = expr_alloc_or(es->rev_dep.expr, dep);
  328. dep = expr_eliminate_dups(dep);
  329. es->rev_dep.expr = dep;
  330. }
  331. }
  332. }
  333. menu_add_symbol(P_CHOICE, sym, NULL);
  334. prop = sym_get_choice_prop(sym);
  335. for (ep = &prop->expr; *ep; ep = &(*ep)->left.expr)
  336. ;
  337. *ep = expr_alloc_one(E_LIST, NULL);
  338. (*ep)->right.sym = menu->sym;
  339. }
  340. if (menu->list && (!menu->prompt || !menu->prompt->text)) {
  341. for (last_menu = menu->list; ; last_menu = last_menu->next) {
  342. last_menu->parent = parent;
  343. if (!last_menu->next)
  344. break;
  345. }
  346. last_menu->next = menu->next;
  347. menu->next = menu->list;
  348. menu->list = NULL;
  349. }
  350. }
  351. if (sym && !(sym->flags & SYMBOL_WARNED)) {
  352. if (sym->type == S_UNKNOWN)
  353. menu_warn(parent, "config symbol defined without type");
  354. if (sym_is_choice(sym) && !parent->prompt)
  355. menu_warn(parent, "choice must have a prompt");
  356. /* Check properties connected to this symbol */
  357. sym_check_prop(sym);
  358. sym->flags |= SYMBOL_WARNED;
  359. }
  360. if (sym && !sym_is_optional(sym) && parent->prompt) {
  361. sym->rev_dep.expr = expr_alloc_or(sym->rev_dep.expr,
  362. expr_alloc_and(parent->prompt->visible.expr,
  363. expr_alloc_symbol(&symbol_mod)));
  364. }
  365. }
  366. bool menu_is_visible(struct menu *menu)
  367. {
  368. struct menu *child;
  369. struct symbol *sym;
  370. tristate visible;
  371. if (!menu->prompt)
  372. return false;
  373. sym = menu->sym;
  374. if (sym) {
  375. sym_calc_value(sym);
  376. visible = menu->prompt->visible.tri;
  377. } else
  378. visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr);
  379. if (visible != no)
  380. return true;
  381. if (!sym || sym_get_tristate_value(menu->sym) == no)
  382. return false;
  383. for (child = menu->list; child; child = child->next)
  384. if (menu_is_visible(child))
  385. return true;
  386. return false;
  387. }
  388. const char *menu_get_prompt(struct menu *menu)
  389. {
  390. if (menu->prompt)
  391. return menu->prompt->text;
  392. else if (menu->sym)
  393. return menu->sym->name;
  394. return NULL;
  395. }
  396. struct menu *menu_get_root_menu(struct menu *menu)
  397. {
  398. return &rootmenu;
  399. }
  400. struct menu *menu_get_parent_menu(struct menu *menu)
  401. {
  402. enum prop_type type;
  403. for (; menu != &rootmenu; menu = menu->parent) {
  404. type = menu->prompt ? menu->prompt->type : 0;
  405. if (type == P_MENU)
  406. break;
  407. }
  408. return menu;
  409. }
  410. bool menu_has_help(struct menu *menu)
  411. {
  412. return menu->help != NULL;
  413. }
  414. const char *menu_get_help(struct menu *menu)
  415. {
  416. if (menu->help)
  417. return menu->help;
  418. else
  419. return "";
  420. }