menu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. struct menu *current_menu, *current_entry;
  11. static struct menu **last_entry_ptr;
  12. struct file *file_list;
  13. struct file *current_file;
  14. void menu_init(void)
  15. {
  16. current_entry = current_menu = &rootmenu;
  17. last_entry_ptr = &rootmenu.list;
  18. }
  19. void menu_add_entry(struct symbol *sym)
  20. {
  21. struct menu *menu;
  22. menu = malloc(sizeof(*menu));
  23. memset(menu, 0, sizeof(*menu));
  24. menu->sym = sym;
  25. menu->parent = current_menu;
  26. menu->file = current_file;
  27. menu->lineno = zconf_lineno();
  28. *last_entry_ptr = menu;
  29. last_entry_ptr = &menu->next;
  30. current_entry = menu;
  31. }
  32. void menu_end_entry(void)
  33. {
  34. }
  35. void menu_add_menu(void)
  36. {
  37. current_menu = current_entry;
  38. last_entry_ptr = &current_entry->list;
  39. }
  40. void menu_end_menu(void)
  41. {
  42. last_entry_ptr = &current_menu->next;
  43. current_menu = current_menu->parent;
  44. }
  45. struct expr *menu_check_dep(struct expr *e)
  46. {
  47. if (!e)
  48. return e;
  49. switch (e->type) {
  50. case E_NOT:
  51. e->left.expr = menu_check_dep(e->left.expr);
  52. break;
  53. case E_OR:
  54. case E_AND:
  55. e->left.expr = menu_check_dep(e->left.expr);
  56. e->right.expr = menu_check_dep(e->right.expr);
  57. break;
  58. case E_SYMBOL:
  59. /* change 'm' into 'm' && MODULES */
  60. if (e->left.sym == &symbol_mod)
  61. return expr_alloc_and(e, expr_alloc_symbol(modules_sym));
  62. break;
  63. default:
  64. break;
  65. }
  66. return e;
  67. }
  68. void menu_add_dep(struct expr *dep)
  69. {
  70. current_entry->dep = expr_alloc_and(current_entry->dep, menu_check_dep(dep));
  71. }
  72. void menu_set_type(int type)
  73. {
  74. struct symbol *sym = current_entry->sym;
  75. if (sym->type == type)
  76. return;
  77. if (sym->type == S_UNKNOWN) {
  78. sym->type = type;
  79. return;
  80. }
  81. fprintf(stderr, "%s:%d:warning: type of '%s' redefined from '%s' to '%s'\n",
  82. current_entry->file->name, current_entry->lineno,
  83. sym->name ? sym->name : "<choice>", sym_type_name(sym->type), sym_type_name(type));
  84. }
  85. struct property *menu_add_prop(enum prop_type type, char *prompt, struct expr *expr, struct expr *dep)
  86. {
  87. struct property *prop = prop_alloc(type, current_entry->sym);
  88. prop->menu = current_entry;
  89. prop->text = prompt;
  90. prop->expr = expr;
  91. prop->visible.expr = menu_check_dep(dep);
  92. if (prompt) {
  93. if (current_entry->prompt)
  94. fprintf(stderr, "%s:%d: prompt redefined\n",
  95. current_entry->file->name, current_entry->lineno);
  96. current_entry->prompt = prop;
  97. }
  98. return prop;
  99. }
  100. void menu_add_prompt(enum prop_type type, char *prompt, struct expr *dep)
  101. {
  102. menu_add_prop(type, prompt, NULL, dep);
  103. }
  104. void menu_add_expr(enum prop_type type, struct expr *expr, struct expr *dep)
  105. {
  106. menu_add_prop(type, NULL, expr, dep);
  107. }
  108. void menu_add_symbol(enum prop_type type, struct symbol *sym, struct expr *dep)
  109. {
  110. menu_add_prop(type, NULL, expr_alloc_symbol(sym), dep);
  111. }
  112. void menu_finalize(struct menu *parent)
  113. {
  114. struct menu *menu, *last_menu;
  115. struct symbol *sym;
  116. struct property *prop;
  117. struct expr *parentdep, *basedep, *dep, *dep2, **ep;
  118. sym = parent->sym;
  119. if (parent->list) {
  120. if (sym && sym_is_choice(sym)) {
  121. /* find the first choice value and find out choice type */
  122. for (menu = parent->list; menu; menu = menu->next) {
  123. if (menu->sym) {
  124. current_entry = parent;
  125. menu_set_type(menu->sym->type);
  126. current_entry = menu;
  127. menu_set_type(sym->type);
  128. break;
  129. }
  130. }
  131. parentdep = expr_alloc_symbol(sym);
  132. } else if (parent->prompt)
  133. parentdep = parent->prompt->visible.expr;
  134. else
  135. parentdep = parent->dep;
  136. for (menu = parent->list; menu; menu = menu->next) {
  137. basedep = expr_transform(menu->dep);
  138. basedep = expr_alloc_and(expr_copy(parentdep), basedep);
  139. basedep = expr_eliminate_dups(basedep);
  140. menu->dep = basedep;
  141. if (menu->sym)
  142. prop = menu->sym->prop;
  143. else
  144. prop = menu->prompt;
  145. for (; prop; prop = prop->next) {
  146. if (prop->menu != menu)
  147. continue;
  148. dep = expr_transform(prop->visible.expr);
  149. dep = expr_alloc_and(expr_copy(basedep), dep);
  150. dep = expr_eliminate_dups(dep);
  151. if (menu->sym && menu->sym->type != S_TRISTATE)
  152. dep = expr_trans_bool(dep);
  153. prop->visible.expr = dep;
  154. if (prop->type == P_SELECT) {
  155. struct symbol *es = prop_get_symbol(prop);
  156. es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
  157. expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
  158. }
  159. }
  160. }
  161. for (menu = parent->list; menu; menu = menu->next)
  162. menu_finalize(menu);
  163. } else if (sym) {
  164. basedep = parent->prompt ? parent->prompt->visible.expr : NULL;
  165. basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no);
  166. basedep = expr_eliminate_dups(expr_transform(basedep));
  167. last_menu = NULL;
  168. for (menu = parent->next; menu; menu = menu->next) {
  169. dep = menu->prompt ? menu->prompt->visible.expr : menu->dep;
  170. if (!expr_contains_symbol(dep, sym))
  171. break;
  172. if (expr_depends_symbol(dep, sym))
  173. goto next;
  174. dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no);
  175. dep = expr_eliminate_dups(expr_transform(dep));
  176. dep2 = expr_copy(basedep);
  177. expr_eliminate_eq(&dep, &dep2);
  178. expr_free(dep);
  179. if (!expr_is_yes(dep2)) {
  180. expr_free(dep2);
  181. break;
  182. }
  183. expr_free(dep2);
  184. next:
  185. menu_finalize(menu);
  186. menu->parent = parent;
  187. last_menu = menu;
  188. }
  189. if (last_menu) {
  190. parent->list = parent->next;
  191. parent->next = last_menu->next;
  192. last_menu->next = NULL;
  193. }
  194. }
  195. for (menu = parent->list; menu; menu = menu->next) {
  196. if (sym && sym_is_choice(sym) && menu->sym) {
  197. menu->sym->flags |= SYMBOL_CHOICEVAL;
  198. if (!menu->prompt)
  199. fprintf(stderr, "%s:%d:warning: choice value must have a prompt\n",
  200. menu->file->name, menu->lineno);
  201. for (prop = menu->sym->prop; prop; prop = prop->next) {
  202. if (prop->type == P_PROMPT && prop->menu != menu) {
  203. fprintf(stderr, "%s:%d:warning: choice values currently only support a single prompt\n",
  204. prop->file->name, prop->lineno);
  205. }
  206. if (prop->type == P_DEFAULT)
  207. fprintf(stderr, "%s:%d:warning: defaults for choice values not supported\n",
  208. prop->file->name, prop->lineno);
  209. }
  210. current_entry = menu;
  211. menu_set_type(sym->type);
  212. menu_add_symbol(P_CHOICE, sym, NULL);
  213. prop = sym_get_choice_prop(sym);
  214. for (ep = &prop->expr; *ep; ep = &(*ep)->left.expr)
  215. ;
  216. *ep = expr_alloc_one(E_CHOICE, NULL);
  217. (*ep)->right.sym = menu->sym;
  218. }
  219. if (menu->list && (!menu->prompt || !menu->prompt->text)) {
  220. for (last_menu = menu->list; ; last_menu = last_menu->next) {
  221. last_menu->parent = parent;
  222. if (!last_menu->next)
  223. break;
  224. }
  225. last_menu->next = menu->next;
  226. menu->next = menu->list;
  227. menu->list = NULL;
  228. }
  229. }
  230. if (sym && !(sym->flags & SYMBOL_WARNED)) {
  231. struct symbol *sym2;
  232. if (sym->type == S_UNKNOWN)
  233. fprintf(stderr, "%s:%d:warning: config symbol defined without type\n",
  234. parent->file->name, parent->lineno);
  235. if (sym_is_choice(sym) && !parent->prompt)
  236. fprintf(stderr, "%s:%d:warning: choice must have a prompt\n",
  237. parent->file->name, parent->lineno);
  238. for (prop = sym->prop; prop; prop = prop->next) {
  239. switch (prop->type) {
  240. case P_DEFAULT:
  241. if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
  242. prop->expr->type != E_SYMBOL)
  243. fprintf(stderr, "%s:%d:warning: default must be a single symbol\n",
  244. prop->file->name, prop->lineno);
  245. break;
  246. case P_SELECT:
  247. sym2 = prop_get_symbol(prop);
  248. if ((sym->type != S_BOOLEAN && sym->type != S_TRISTATE) ||
  249. (sym2->type != S_BOOLEAN && sym2->type != S_TRISTATE))
  250. fprintf(stderr, "%s:%d:warning: enable is only allowed with boolean and tristate symbols\n",
  251. prop->file->name, prop->lineno);
  252. break;
  253. case P_RANGE:
  254. if (sym->type != S_INT && sym->type != S_HEX)
  255. fprintf(stderr, "%s:%d:warning: range is only allowed for int or hex symbols\n",
  256. prop->file->name, prop->lineno);
  257. if (!sym_string_valid(sym, prop->expr->left.sym->name) ||
  258. !sym_string_valid(sym, prop->expr->right.sym->name))
  259. fprintf(stderr, "%s:%d:warning: range is invalid\n",
  260. prop->file->name, prop->lineno);
  261. break;
  262. default:
  263. ;
  264. }
  265. }
  266. sym->flags |= SYMBOL_WARNED;
  267. }
  268. if (sym && !sym_is_optional(sym) && parent->prompt) {
  269. sym->rev_dep.expr = expr_alloc_or(sym->rev_dep.expr,
  270. expr_alloc_and(parent->prompt->visible.expr,
  271. expr_alloc_symbol(&symbol_mod)));
  272. }
  273. }
  274. bool menu_is_visible(struct menu *menu)
  275. {
  276. struct menu *child;
  277. struct symbol *sym;
  278. tristate visible;
  279. if (!menu->prompt)
  280. return false;
  281. sym = menu->sym;
  282. if (sym) {
  283. sym_calc_value(sym);
  284. visible = menu->prompt->visible.tri;
  285. } else
  286. visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr);
  287. if (visible != no)
  288. return true;
  289. if (!sym || sym_get_tristate_value(menu->sym) == no)
  290. return false;
  291. for (child = menu->list; child; child = child->next)
  292. if (menu_is_visible(child))
  293. return true;
  294. return false;
  295. }
  296. const char *menu_get_prompt(struct menu *menu)
  297. {
  298. if (menu->prompt)
  299. return menu->prompt->text;
  300. else if (menu->sym)
  301. return menu->sym->name;
  302. return NULL;
  303. }
  304. struct menu *menu_get_root_menu(struct menu *menu)
  305. {
  306. return &rootmenu;
  307. }
  308. struct menu *menu_get_parent_menu(struct menu *menu)
  309. {
  310. enum prop_type type;
  311. for (; menu != &rootmenu; menu = menu->parent) {
  312. type = menu->prompt ? menu->prompt->type : 0;
  313. if (type == P_MENU)
  314. break;
  315. }
  316. return menu;
  317. }
  318. struct file *file_lookup(const char *name)
  319. {
  320. struct file *file;
  321. for (file = file_list; file; file = file->next) {
  322. if (!strcmp(name, file->name))
  323. return file;
  324. }
  325. file = malloc(sizeof(*file));
  326. memset(file, 0, sizeof(*file));
  327. file->name = strdup(name);
  328. file->next = file_list;
  329. file_list = file;
  330. return file;
  331. }
  332. int file_write_dep(const char *name)
  333. {
  334. struct file *file;
  335. FILE *out;
  336. if (!name)
  337. name = ".config.cmd";
  338. out = fopen(".config.tmp", "w");
  339. if (!out)
  340. return 1;
  341. fprintf(out, "deps_config := \\\n");
  342. for (file = file_list; file; file = file->next) {
  343. if (file->next)
  344. fprintf(out, "\t%s \\\n", file->name);
  345. else
  346. fprintf(out, "\t%s\n", file->name);
  347. }
  348. fprintf(out, "\n.config include/bits/uClibc_config.h: $(deps_config)\n\n$(deps_config):\n");
  349. fclose(out);
  350. rename(".config.tmp", name);
  351. return 0;
  352. }