menu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (C) 2002 Roman Zippel <[email protected]>
  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. static 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. void menu_add_dep(struct expr *dep)
  64. {
  65. current_entry->dep = expr_alloc_and(current_entry->dep, dep);
  66. }
  67. void menu_set_type(int type)
  68. {
  69. struct symbol *sym = current_entry->sym;
  70. if (sym->type == type)
  71. return;
  72. if (sym->type == S_UNKNOWN) {
  73. sym->type = type;
  74. return;
  75. }
  76. menu_warn(current_entry, "type of '%s' redefined from '%s' to '%s'\n",
  77. sym->name ? sym->name : "<choice>",
  78. sym_type_name(sym->type), sym_type_name(type));
  79. }
  80. struct property *menu_add_prop(enum prop_type type, char *prompt, struct expr *expr, struct expr *dep)
  81. {
  82. struct property *prop = prop_alloc(type, current_entry->sym);
  83. prop->menu = current_entry;
  84. prop->text = prompt;
  85. prop->expr = expr;
  86. prop->visible.expr = dep;
  87. if (prompt) {
  88. if (current_entry->prompt)
  89. menu_warn(current_entry, "prompt redefined\n");
  90. current_entry->prompt = prop;
  91. }
  92. return prop;
  93. }
  94. struct property *menu_add_prompt(enum prop_type type, char *prompt, struct expr *dep)
  95. {
  96. return menu_add_prop(type, prompt, NULL, dep);
  97. }
  98. void menu_add_expr(enum prop_type type, struct expr *expr, struct expr *dep)
  99. {
  100. menu_add_prop(type, NULL, expr, dep);
  101. }
  102. void menu_add_symbol(enum prop_type type, struct symbol *sym, struct expr *dep)
  103. {
  104. menu_add_prop(type, NULL, expr_alloc_symbol(sym), dep);
  105. }
  106. static int menu_range_valid_sym(struct symbol *sym, struct symbol *sym2)
  107. {
  108. return sym2->type == S_INT || sym2->type == S_HEX ||
  109. (sym2->type == S_UNKNOWN && sym_string_valid(sym, sym2->name));
  110. }
  111. void sym_check_prop(struct symbol *sym)
  112. {
  113. struct property *prop;
  114. struct symbol *sym2;
  115. for (prop = sym->prop; prop; prop = prop->next) {
  116. switch (prop->type) {
  117. case P_DEFAULT:
  118. if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
  119. prop->expr->type != E_SYMBOL)
  120. prop_warn(prop,
  121. "default for config symbol '%'"
  122. " must be a single symbol", sym->name);
  123. break;
  124. case P_SELECT:
  125. sym2 = prop_get_symbol(prop);
  126. if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE)
  127. prop_warn(prop,
  128. "config symbol '%s' uses select, but is "
  129. "not boolean or tristate", sym->name);
  130. else if (sym2->type == S_UNKNOWN)
  131. prop_warn(prop,
  132. "'select' used by config symbol '%s' "
  133. "refer to undefined symbol '%s'",
  134. sym->name, sym2->name);
  135. else if (sym2->type != S_BOOLEAN && sym2->type != S_TRISTATE)
  136. prop_warn(prop,
  137. "'%s' has wrong type. 'select' only "
  138. "accept arguments of boolean and "
  139. "tristate type", sym2->name);
  140. break;
  141. case P_DESELECT:
  142. sym2 = prop_get_symbol(prop);
  143. if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE)
  144. prop_warn(prop,
  145. "config symbol '%s' uses deselect, but is "
  146. "not boolean or tristate", sym->name);
  147. else if (sym2->type == S_UNKNOWN)
  148. prop_warn(prop,
  149. "'deselect' used by config symbol '%s' "
  150. "refer to undefined symbol '%s'",
  151. sym->name, sym2->name);
  152. else if (sym2->type != S_BOOLEAN && sym2->type != S_TRISTATE)
  153. prop_warn(prop,
  154. "'%s' has wrong type. 'deselect' only "
  155. "accept arguments of boolean and "
  156. "tristate type", sym2->name);
  157. break;
  158. case P_RANGE:
  159. if (sym->type != S_INT && sym->type != S_HEX)
  160. prop_warn(prop, "range is only allowed "
  161. "for int or hex symbols");
  162. if (!menu_range_valid_sym(sym, prop->expr->left.sym) ||
  163. !menu_range_valid_sym(sym, prop->expr->right.sym))
  164. prop_warn(prop, "range is invalid");
  165. break;
  166. default:
  167. ;
  168. }
  169. }
  170. }
  171. void menu_finalize(struct menu *parent)
  172. {
  173. struct menu *menu, *last_menu;
  174. struct symbol *sym;
  175. struct property *prop;
  176. struct expr *parentdep, *basedep, *dep, *dep2, **ep;
  177. sym = parent->sym;
  178. if (parent->list) {
  179. if (sym && sym_is_choice(sym)) {
  180. /* find the first choice value and find out choice type */
  181. for (menu = parent->list; menu; menu = menu->next) {
  182. if (menu->sym) {
  183. current_entry = parent;
  184. menu_set_type(menu->sym->type);
  185. current_entry = menu;
  186. menu_set_type(sym->type);
  187. break;
  188. }
  189. }
  190. parentdep = expr_alloc_symbol(sym);
  191. } else if (parent->prompt)
  192. parentdep = parent->prompt->visible.expr;
  193. else
  194. parentdep = parent->dep;
  195. for (menu = parent->list; menu; menu = menu->next) {
  196. basedep = expr_transform(menu->dep);
  197. basedep = expr_alloc_and(expr_copy(parentdep), basedep);
  198. basedep = expr_eliminate_dups(basedep);
  199. menu->dep = basedep;
  200. if (menu->sym)
  201. prop = menu->sym->prop;
  202. else
  203. prop = menu->prompt;
  204. for (; prop; prop = prop->next) {
  205. if (prop->menu != menu)
  206. continue;
  207. dep = expr_transform(prop->visible.expr);
  208. dep = expr_alloc_and(expr_copy(menu->dep), dep);
  209. dep = expr_eliminate_dups(dep);
  210. if (menu->sym && menu->sym->type != S_TRISTATE)
  211. dep = expr_trans_bool(dep);
  212. prop->visible.expr = dep;
  213. if (prop->type == P_SELECT) {
  214. struct symbol *es = prop_get_symbol(prop);
  215. es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
  216. expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
  217. }
  218. if (prop->type == P_DESELECT) {
  219. struct symbol *es = prop_get_symbol(prop);
  220. es->rev_dep_inv.expr = expr_alloc_or(es->rev_dep_inv.expr,
  221. expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
  222. }
  223. }
  224. }
  225. for (menu = parent->list; menu; menu = menu->next)
  226. menu_finalize(menu);
  227. } else if (sym) {
  228. basedep = parent->prompt ? parent->prompt->visible.expr : NULL;
  229. basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no);
  230. basedep = expr_eliminate_dups(expr_transform(basedep));
  231. last_menu = NULL;
  232. for (menu = parent->next; menu; menu = menu->next) {
  233. dep = menu->prompt ? menu->prompt->visible.expr : menu->dep;
  234. if (!expr_contains_symbol(dep, sym))
  235. break;
  236. if (expr_depends_symbol(dep, sym))
  237. goto next;
  238. dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no);
  239. dep = expr_eliminate_dups(expr_transform(dep));
  240. dep2 = expr_copy(basedep);
  241. expr_eliminate_eq(&dep, &dep2);
  242. expr_free(dep);
  243. if (!expr_is_yes(dep2)) {
  244. expr_free(dep2);
  245. break;
  246. }
  247. expr_free(dep2);
  248. next:
  249. menu_finalize(menu);
  250. menu->parent = parent;
  251. last_menu = menu;
  252. }
  253. if (last_menu) {
  254. parent->list = parent->next;
  255. parent->next = last_menu->next;
  256. last_menu->next = NULL;
  257. }
  258. }
  259. for (menu = parent->list; menu; menu = menu->next) {
  260. if (sym && sym_is_choice(sym) && menu->sym) {
  261. menu->sym->flags |= SYMBOL_CHOICEVAL;
  262. if (!menu->prompt)
  263. menu_warn(menu, "choice value must have a prompt");
  264. for (prop = menu->sym->prop; prop; prop = prop->next) {
  265. if (prop->type == P_PROMPT && prop->menu != menu) {
  266. prop_warn(prop, "choice values "
  267. "currently only support a "
  268. "single prompt");
  269. }
  270. }
  271. current_entry = menu;
  272. menu_set_type(sym->type);
  273. menu_add_symbol(P_CHOICE, sym, NULL);
  274. prop = sym_get_choice_prop(sym);
  275. for (ep = &prop->expr; *ep; ep = &(*ep)->left.expr)
  276. ;
  277. *ep = expr_alloc_one(E_CHOICE, NULL);
  278. (*ep)->right.sym = menu->sym;
  279. }
  280. if (menu->list && (!menu->prompt || !menu->prompt->text)) {
  281. for (last_menu = menu->list; ; last_menu = last_menu->next) {
  282. last_menu->parent = parent;
  283. if (!last_menu->next)
  284. break;
  285. }
  286. last_menu->next = menu->next;
  287. menu->next = menu->list;
  288. menu->list = NULL;
  289. }
  290. }
  291. if (sym && !(sym->flags & SYMBOL_WARNED)) {
  292. if (sym->type == S_UNKNOWN)
  293. menu_warn(parent, "config symbol defined "
  294. "without type\n");
  295. if (sym_is_choice(sym) && !parent->prompt)
  296. menu_warn(parent, "choice must have a prompt\n");
  297. /* Check properties connected to this symbol */
  298. sym_check_prop(sym);
  299. sym->flags |= SYMBOL_WARNED;
  300. }
  301. if (sym && !sym_is_optional(sym) && parent->prompt) {
  302. sym->rev_dep.expr = expr_alloc_or(sym->rev_dep.expr,
  303. expr_alloc_and(parent->prompt->visible.expr,
  304. expr_alloc_symbol(&symbol_mod)));
  305. }
  306. }
  307. bool menu_is_visible(struct menu *menu)
  308. {
  309. struct menu *child;
  310. struct symbol *sym;
  311. tristate visible;
  312. if (!menu->prompt)
  313. return false;
  314. sym = menu->sym;
  315. if (sym) {
  316. sym_calc_value(sym);
  317. visible = menu->prompt->visible.tri;
  318. } else
  319. visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr);
  320. if (visible != no)
  321. return true;
  322. if (!sym || sym_get_tristate_value(menu->sym) == no)
  323. return false;
  324. for (child = menu->list; child; child = child->next)
  325. if (menu_is_visible(child))
  326. return true;
  327. return false;
  328. }
  329. const char *menu_get_prompt(struct menu *menu)
  330. {
  331. if (menu->prompt)
  332. return _(menu->prompt->text);
  333. else if (menu->sym)
  334. return _(menu->sym->name);
  335. return NULL;
  336. }
  337. struct menu *menu_get_root_menu(struct menu *menu)
  338. {
  339. return &rootmenu;
  340. }
  341. struct menu *menu_get_parent_menu(struct menu *menu)
  342. {
  343. enum prop_type type;
  344. for (; menu != &rootmenu; menu = menu->parent) {
  345. type = menu->prompt ? menu->prompt->type : 0;
  346. if (type == P_MENU)
  347. break;
  348. }
  349. return menu;
  350. }