menu.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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) || (sym2->type != S_BOOLEAN))
  249. fprintf(stderr, "%s:%d:warning: enable is only allowed with booleans\n",
  250. prop->file->name, prop->lineno);
  251. break;
  252. case P_RANGE:
  253. if (sym->type != S_INT && sym->type != S_HEX)
  254. fprintf(stderr, "%s:%d:warning: range is only allowed for int or hex symbols\n",
  255. prop->file->name, prop->lineno);
  256. if (!sym_string_valid(sym, prop->expr->left.sym->name) ||
  257. !sym_string_valid(sym, prop->expr->right.sym->name))
  258. fprintf(stderr, "%s:%d:warning: range is invalid\n",
  259. prop->file->name, prop->lineno);
  260. break;
  261. default:
  262. ;
  263. }
  264. }
  265. sym->flags |= SYMBOL_WARNED;
  266. }
  267. if (sym && !sym_is_optional(sym) && parent->prompt) {
  268. sym->rev_dep.expr = expr_alloc_or(sym->rev_dep.expr,
  269. expr_alloc_and(parent->prompt->visible.expr,
  270. expr_alloc_symbol(&symbol_mod)));
  271. }
  272. }
  273. bool menu_is_visible(struct menu *menu)
  274. {
  275. struct menu *child;
  276. struct symbol *sym;
  277. tristate visible;
  278. if (!menu->prompt)
  279. return false;
  280. sym = menu->sym;
  281. if (sym) {
  282. sym_calc_value(sym);
  283. visible = menu->prompt->visible.tri;
  284. } else
  285. visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr);
  286. if (visible != no)
  287. return true;
  288. if (!sym || sym_get_tristate_value(menu->sym) == no)
  289. return false;
  290. for (child = menu->list; child; child = child->next)
  291. if (menu_is_visible(child))
  292. return true;
  293. return false;
  294. }
  295. const char *menu_get_prompt(struct menu *menu)
  296. {
  297. if (menu->prompt)
  298. return menu->prompt->text;
  299. else if (menu->sym)
  300. return menu->sym->name;
  301. return NULL;
  302. }
  303. struct menu *menu_get_root_menu(struct menu *menu)
  304. {
  305. return &rootmenu;
  306. }
  307. struct menu *menu_get_parent_menu(struct menu *menu)
  308. {
  309. enum prop_type type;
  310. for (; menu != &rootmenu; menu = menu->parent) {
  311. type = menu->prompt ? menu->prompt->type : 0;
  312. if (type == P_MENU)
  313. break;
  314. }
  315. return menu;
  316. }
  317. struct file *file_lookup(const char *name)
  318. {
  319. struct file *file;
  320. for (file = file_list; file; file = file->next) {
  321. if (!strcmp(name, file->name))
  322. return file;
  323. }
  324. file = malloc(sizeof(*file));
  325. memset(file, 0, sizeof(*file));
  326. file->name = strdup(name);
  327. file->next = file_list;
  328. file_list = file;
  329. return file;
  330. }
  331. int file_write_dep(const char *name)
  332. {
  333. struct file *file;
  334. FILE *out;
  335. if (!name)
  336. name = ".config.cmd";
  337. out = fopen(".config.tmp", "w");
  338. if (!out)
  339. return 1;
  340. fprintf(out, "deps_config := \\\n");
  341. for (file = file_list; file; file = file->next) {
  342. if (file->next)
  343. fprintf(out, "\t%s \\\n", file->name);
  344. else
  345. fprintf(out, "\t%s\n", file->name);
  346. }
  347. fprintf(out, "\n.config include/bits/uClibc_config.h: $(deps_config)\n\n$(deps_config):\n");
  348. fclose(out);
  349. rename(".config.tmp", name);
  350. return 0;
  351. }