menu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. static void menu_warn(struct menu *menu, const char *fmt, ...)
  15. {
  16. va_list ap;
  17. va_start(ap, fmt);
  18. fprintf(stderr, "%s:%d:warning: ", menu->file->name, menu->lineno);
  19. vfprintf(stderr, fmt, ap);
  20. fprintf(stderr, "\n");
  21. va_end(ap);
  22. }
  23. static void prop_warn(struct property *prop, const char *fmt, ...)
  24. {
  25. va_list ap;
  26. va_start(ap, fmt);
  27. fprintf(stderr, "%s:%d:warning: ", prop->file->name, prop->lineno);
  28. vfprintf(stderr, fmt, ap);
  29. fprintf(stderr, "\n");
  30. va_end(ap);
  31. }
  32. void menu_init(void)
  33. {
  34. current_entry = current_menu = &rootmenu;
  35. last_entry_ptr = &rootmenu.list;
  36. }
  37. void menu_add_entry(struct symbol *sym)
  38. {
  39. struct menu *menu;
  40. menu = malloc(sizeof(*menu));
  41. memset(menu, 0, sizeof(*menu));
  42. menu->sym = sym;
  43. menu->parent = current_menu;
  44. menu->file = current_file;
  45. menu->lineno = zconf_lineno();
  46. *last_entry_ptr = menu;
  47. last_entry_ptr = &menu->next;
  48. current_entry = menu;
  49. }
  50. void menu_end_entry(void)
  51. {
  52. }
  53. void menu_add_menu(void)
  54. {
  55. current_menu = current_entry;
  56. last_entry_ptr = &current_entry->list;
  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'\n",
  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->text = prompt;
  108. prop->expr = expr;
  109. prop->visible.expr = menu_check_dep(dep);
  110. if (prompt) {
  111. if (current_entry->prompt)
  112. menu_warn(current_entry, "prompt redefined\n");
  113. current_entry->prompt = prop;
  114. }
  115. return prop;
  116. }
  117. void menu_add_prompt(enum prop_type type, char *prompt, struct expr *dep)
  118. {
  119. menu_add_prop(type, prompt, NULL, dep);
  120. }
  121. void menu_add_expr(enum prop_type type, struct expr *expr, struct expr *dep)
  122. {
  123. menu_add_prop(type, NULL, expr, dep);
  124. }
  125. void menu_add_symbol(enum prop_type type, struct symbol *sym, struct expr *dep)
  126. {
  127. menu_add_prop(type, NULL, expr_alloc_symbol(sym), dep);
  128. }
  129. void sym_check_prop(struct symbol *sym)
  130. {
  131. struct property *prop;
  132. struct symbol *sym2;
  133. for (prop = sym->prop; prop; prop = prop->next) {
  134. switch (prop->type) {
  135. case P_DEFAULT:
  136. if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
  137. prop->expr->type != E_SYMBOL)
  138. prop_warn(prop,
  139. "default for config symbol '%'"
  140. " must be a single symbol", sym->name);
  141. break;
  142. case P_SELECT:
  143. sym2 = prop_get_symbol(prop);
  144. if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE)
  145. prop_warn(prop,
  146. "config symbol '%s' uses select, but is "
  147. "not boolean or tristate", sym->name);
  148. else if (sym2->type == S_UNKNOWN)
  149. prop_warn(prop,
  150. "'select' used by config symbol '%s' "
  151. "refer to undefined symbol '%s'",
  152. sym->name, sym2->name);
  153. else if (sym2->type != S_BOOLEAN && sym2->type != S_TRISTATE)
  154. prop_warn(prop,
  155. "'%s' has wrong type. 'select' only "
  156. "accept arguments of boolean and "
  157. "tristate type", sym2->name);
  158. break;
  159. case P_RANGE:
  160. if (sym->type != S_INT && sym->type != S_HEX)
  161. prop_warn(prop, "range is only allowed "
  162. "for int or hex symbols");
  163. if (!sym_string_valid(sym, prop->expr->left.sym->name) ||
  164. !sym_string_valid(sym, prop->expr->right.sym->name))
  165. prop_warn(prop, "range is invalid");
  166. break;
  167. default:
  168. ;
  169. }
  170. }
  171. }
  172. void menu_finalize(struct menu *parent)
  173. {
  174. struct menu *menu, *last_menu;
  175. struct symbol *sym;
  176. struct property *prop;
  177. struct expr *parentdep, *basedep, *dep, *dep2, **ep;
  178. sym = parent->sym;
  179. if (parent->list) {
  180. if (sym && sym_is_choice(sym)) {
  181. /* find the first choice value and find out choice type */
  182. for (menu = parent->list; menu; menu = menu->next) {
  183. if (menu->sym) {
  184. current_entry = parent;
  185. menu_set_type(menu->sym->type);
  186. current_entry = menu;
  187. menu_set_type(sym->type);
  188. break;
  189. }
  190. }
  191. parentdep = expr_alloc_symbol(sym);
  192. } else if (parent->prompt)
  193. parentdep = parent->prompt->visible.expr;
  194. else
  195. parentdep = parent->dep;
  196. for (menu = parent->list; menu; menu = menu->next) {
  197. basedep = expr_transform(menu->dep);
  198. basedep = expr_alloc_and(expr_copy(parentdep), basedep);
  199. basedep = expr_eliminate_dups(basedep);
  200. menu->dep = basedep;
  201. if (menu->sym)
  202. prop = menu->sym->prop;
  203. else
  204. prop = menu->prompt;
  205. for (; prop; prop = prop->next) {
  206. if (prop->menu != menu)
  207. continue;
  208. dep = expr_transform(prop->visible.expr);
  209. dep = expr_alloc_and(expr_copy(basedep), dep);
  210. dep = expr_eliminate_dups(dep);
  211. if (menu->sym && menu->sym->type != S_TRISTATE)
  212. dep = expr_trans_bool(dep);
  213. prop->visible.expr = dep;
  214. if (prop->type == P_SELECT) {
  215. struct symbol *es = prop_get_symbol(prop);
  216. es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr,
  217. expr_alloc_and(expr_alloc_symbol(menu->sym), expr_copy(dep)));
  218. }
  219. }
  220. }
  221. for (menu = parent->list; menu; menu = menu->next)
  222. menu_finalize(menu);
  223. } else if (sym) {
  224. basedep = parent->prompt ? parent->prompt->visible.expr : NULL;
  225. basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no);
  226. basedep = expr_eliminate_dups(expr_transform(basedep));
  227. last_menu = NULL;
  228. for (menu = parent->next; menu; menu = menu->next) {
  229. dep = menu->prompt ? menu->prompt->visible.expr : menu->dep;
  230. if (!expr_contains_symbol(dep, sym))
  231. break;
  232. if (expr_depends_symbol(dep, sym))
  233. goto next;
  234. dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no);
  235. dep = expr_eliminate_dups(expr_transform(dep));
  236. dep2 = expr_copy(basedep);
  237. expr_eliminate_eq(&dep, &dep2);
  238. expr_free(dep);
  239. if (!expr_is_yes(dep2)) {
  240. expr_free(dep2);
  241. break;
  242. }
  243. expr_free(dep2);
  244. next:
  245. menu_finalize(menu);
  246. menu->parent = parent;
  247. last_menu = menu;
  248. }
  249. if (last_menu) {
  250. parent->list = parent->next;
  251. parent->next = last_menu->next;
  252. last_menu->next = NULL;
  253. }
  254. }
  255. for (menu = parent->list; menu; menu = menu->next) {
  256. if (sym && sym_is_choice(sym) && menu->sym) {
  257. menu->sym->flags |= SYMBOL_CHOICEVAL;
  258. if (!menu->prompt)
  259. menu_warn(menu, "choice value must have a prompt");
  260. for (prop = menu->sym->prop; prop; prop = prop->next) {
  261. if (prop->type == P_PROMPT && prop->menu != menu) {
  262. prop_warn(prop, "choice values "
  263. "currently only support a "
  264. "single prompt");
  265. }
  266. if (prop->type == P_DEFAULT)
  267. prop_warn(prop, "defaults for choice "
  268. "values not supported");
  269. }
  270. current_entry = menu;
  271. menu_set_type(sym->type);
  272. menu_add_symbol(P_CHOICE, sym, NULL);
  273. prop = sym_get_choice_prop(sym);
  274. for (ep = &prop->expr; *ep; ep = &(*ep)->left.expr)
  275. ;
  276. *ep = expr_alloc_one(E_CHOICE, NULL);
  277. (*ep)->right.sym = menu->sym;
  278. }
  279. if (menu->list && (!menu->prompt || !menu->prompt->text)) {
  280. for (last_menu = menu->list; ; last_menu = last_menu->next) {
  281. last_menu->parent = parent;
  282. if (!last_menu->next)
  283. break;
  284. }
  285. last_menu->next = menu->next;
  286. menu->next = menu->list;
  287. menu->list = NULL;
  288. }
  289. }
  290. if (sym && !(sym->flags & SYMBOL_WARNED)) {
  291. if (sym->type == S_UNKNOWN)
  292. menu_warn(parent, "config symbol defined "
  293. "without type\n");
  294. if (sym_is_choice(sym) && !parent->prompt)
  295. menu_warn(parent, "choice must have a prompt\n");
  296. /* Check properties connected to this symbol */
  297. sym_check_prop(sym);
  298. sym->flags |= SYMBOL_WARNED;
  299. }
  300. if (sym && !sym_is_optional(sym) && parent->prompt) {
  301. sym->rev_dep.expr = expr_alloc_or(sym->rev_dep.expr,
  302. expr_alloc_and(parent->prompt->visible.expr,
  303. expr_alloc_symbol(&symbol_mod)));
  304. }
  305. }
  306. bool menu_is_visible(struct menu *menu)
  307. {
  308. struct menu *child;
  309. struct symbol *sym;
  310. tristate visible;
  311. if (!menu->prompt)
  312. return false;
  313. sym = menu->sym;
  314. if (sym) {
  315. sym_calc_value(sym);
  316. visible = menu->prompt->visible.tri;
  317. } else
  318. visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr);
  319. if (visible != no)
  320. return true;
  321. if (!sym || sym_get_tristate_value(menu->sym) == no)
  322. return false;
  323. for (child = menu->list; child; child = child->next)
  324. if (menu_is_visible(child))
  325. return true;
  326. return false;
  327. }
  328. const char *menu_get_prompt(struct menu *menu)
  329. {
  330. if (menu->prompt)
  331. return menu->prompt->text;
  332. else if (menu->sym)
  333. return menu->sym->name;
  334. return NULL;
  335. }
  336. struct menu *menu_get_root_menu(struct menu *menu)
  337. {
  338. return &rootmenu;
  339. }
  340. struct menu *menu_get_parent_menu(struct menu *menu)
  341. {
  342. enum prop_type type;
  343. for (; menu != &rootmenu; menu = menu->parent) {
  344. type = menu->prompt ? menu->prompt->type : 0;
  345. if (type == P_MENU)
  346. break;
  347. }
  348. return menu;
  349. }
  350. struct file *file_lookup(const char *name)
  351. {
  352. struct file *file;
  353. for (file = file_list; file; file = file->next) {
  354. if (!strcmp(name, file->name))
  355. return file;
  356. }
  357. file = malloc(sizeof(*file));
  358. memset(file, 0, sizeof(*file));
  359. file->name = strdup(name);
  360. file->next = file_list;
  361. file_list = file;
  362. return file;
  363. }
  364. int file_write_dep(const char *name)
  365. {
  366. struct file *file;
  367. FILE *out;
  368. if (!name)
  369. name = ".config.cmd";
  370. out = fopen(".config.tmp", "w");
  371. if (!out)
  372. return 1;
  373. fprintf(out, "deps_config := \\\n");
  374. for (file = file_list; file; file = file->next) {
  375. if (file->next)
  376. fprintf(out, "\t%s \\\n", file->name);
  377. else
  378. fprintf(out, "\t%s\n", file->name);
  379. }
  380. fprintf(out, "\n.config include/bits/uClibc_config.h: $(deps_config)\n\n$(deps_config):\n");
  381. fclose(out);
  382. rename(".config.tmp", name);
  383. return 0;
  384. }