menu.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. void menu_add_dep(struct expr *dep)
  46. {
  47. current_entry->dep = expr_alloc_and(current_entry->dep, dep);
  48. }
  49. void menu_set_type(int type)
  50. {
  51. struct symbol *sym = current_entry->sym;
  52. if (sym->type == type)
  53. return;
  54. if (sym->type == S_UNKNOWN) {
  55. sym->type = type;
  56. return;
  57. }
  58. fprintf(stderr, "%s:%d: type of '%s' redefined from '%s' to '%s'\n",
  59. current_entry->file->name, current_entry->lineno,
  60. sym->name ? sym->name : "<choice>", sym_type_name(sym->type), sym_type_name(type));
  61. }
  62. struct property *create_prop(enum prop_type type)
  63. {
  64. struct property *prop;
  65. prop = malloc(sizeof(*prop));
  66. memset(prop, 0, sizeof(*prop));
  67. prop->type = type;
  68. prop->file = current_file;
  69. prop->lineno = zconf_lineno();
  70. return prop;
  71. }
  72. struct property *menu_add_prop(int token, char *prompt, struct symbol *def, struct expr *dep)
  73. {
  74. struct property *prop = create_prop(token);
  75. struct property **propp;
  76. prop->sym = current_entry->sym;
  77. prop->menu = current_entry;
  78. prop->text = prompt;
  79. prop->def = def;
  80. E_EXPR(prop->visible) = dep;
  81. if (prompt)
  82. current_entry->prompt = prop;
  83. /* append property to the prop list of symbol */
  84. if (prop->sym) {
  85. for (propp = &prop->sym->prop; *propp; propp = &(*propp)->next)
  86. ;
  87. *propp = prop;
  88. }
  89. return prop;
  90. }
  91. void menu_add_prompt(int token, char *prompt, struct expr *dep)
  92. {
  93. current_entry->prompt = menu_add_prop(token, prompt, NULL, dep);
  94. }
  95. void menu_add_default(int token, struct symbol *def, struct expr *dep)
  96. {
  97. current_entry->prompt = menu_add_prop(token, NULL, def, dep);
  98. }
  99. void menu_finalize(struct menu *parent)
  100. {
  101. struct menu *menu, *last_menu;
  102. struct symbol *sym;
  103. struct property *prop;
  104. struct expr *parentdep, *basedep, *dep, *dep2;
  105. sym = parent->sym;
  106. if (parent->list) {
  107. if (sym && sym_is_choice(sym)) {
  108. /* find the first choice value and find out choice type */
  109. for (menu = parent->list; menu; menu = menu->next) {
  110. if (menu->sym) {
  111. current_entry = parent;
  112. menu_set_type(menu->sym->type);
  113. current_entry = menu;
  114. menu_set_type(sym->type);
  115. break;
  116. }
  117. }
  118. parentdep = expr_alloc_symbol(sym);
  119. } else if (parent->prompt)
  120. parentdep = E_EXPR(parent->prompt->visible);
  121. else
  122. parentdep = parent->dep;
  123. for (menu = parent->list; menu; menu = menu->next) {
  124. basedep = expr_transform(menu->dep);
  125. basedep = expr_alloc_and(expr_copy(parentdep), basedep);
  126. basedep = expr_eliminate_dups(basedep);
  127. menu->dep = basedep;
  128. if (menu->sym)
  129. prop = menu->sym->prop;
  130. else
  131. prop = menu->prompt;
  132. for (; prop; prop = prop->next) {
  133. if (prop->menu != menu)
  134. continue;
  135. dep = expr_transform(E_EXPR(prop->visible));
  136. dep = expr_alloc_and(expr_copy(basedep), dep);
  137. dep = expr_eliminate_dups(dep);
  138. if (menu->sym && menu->sym->type != S_TRISTATE)
  139. dep = expr_trans_bool(dep);
  140. E_EXPR(prop->visible) = dep;
  141. }
  142. }
  143. for (menu = parent->list; menu; menu = menu->next)
  144. menu_finalize(menu);
  145. } else if (sym && parent->prompt) {
  146. basedep = E_EXPR(parent->prompt->visible);
  147. basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no);
  148. basedep = expr_eliminate_dups(expr_transform(basedep));
  149. last_menu = NULL;
  150. for (menu = parent->next; menu; menu = menu->next) {
  151. dep = menu->prompt ? E_EXPR(menu->prompt->visible) : menu->dep;
  152. if (!expr_contains_symbol(dep, sym))
  153. break;
  154. if (expr_depends_symbol(dep, sym))
  155. goto next;
  156. dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no);
  157. dep = expr_eliminate_dups(expr_transform(dep));
  158. dep2 = expr_copy(basedep);
  159. expr_eliminate_eq(&dep, &dep2);
  160. expr_free(dep);
  161. if (!expr_is_yes(dep2)) {
  162. expr_free(dep2);
  163. break;
  164. }
  165. expr_free(dep2);
  166. next:
  167. menu_finalize(menu);
  168. menu->parent = parent;
  169. last_menu = menu;
  170. }
  171. if (last_menu) {
  172. parent->list = parent->next;
  173. parent->next = last_menu->next;
  174. last_menu->next = NULL;
  175. }
  176. }
  177. for (menu = parent->list; menu; menu = menu->next) {
  178. if (sym && sym_is_choice(sym) && menu->sym) {
  179. menu->sym->flags |= SYMBOL_CHOICEVAL;
  180. current_entry = menu;
  181. menu_set_type(sym->type);
  182. menu_add_prop(P_CHOICE, NULL, parent->sym, NULL);
  183. prop = sym_get_choice_prop(parent->sym);
  184. //dep = expr_alloc_one(E_CHOICE, dep);
  185. //dep->right.sym = menu->sym;
  186. prop->dep = expr_alloc_one(E_CHOICE, prop->dep);
  187. prop->dep->right.sym = menu->sym;
  188. }
  189. if (menu->list && (!menu->prompt || !menu->prompt->text)) {
  190. for (last_menu = menu->list; ; last_menu = last_menu->next) {
  191. last_menu->parent = parent;
  192. if (!last_menu->next)
  193. break;
  194. }
  195. last_menu->next = menu->next;
  196. menu->next = menu->list;
  197. menu->list = NULL;
  198. }
  199. }
  200. }
  201. bool menu_is_visible(struct menu *menu)
  202. {
  203. tristate visible;
  204. if (!menu->prompt)
  205. return false;
  206. if (menu->sym) {
  207. sym_calc_value(menu->sym);
  208. visible = E_TRI(menu->prompt->visible);
  209. } else
  210. visible = E_CALC(menu->prompt->visible);
  211. return visible != no;
  212. }
  213. const char *menu_get_prompt(struct menu *menu)
  214. {
  215. if (menu->prompt)
  216. return menu->prompt->text;
  217. else if (menu->sym)
  218. return menu->sym->name;
  219. return NULL;
  220. }
  221. struct menu *menu_get_root_menu(struct menu *menu)
  222. {
  223. return &rootmenu;
  224. }
  225. struct menu *menu_get_parent_menu(struct menu *menu)
  226. {
  227. enum prop_type type;
  228. while (menu != &rootmenu) {
  229. menu = menu->parent;
  230. type = menu->prompt ? menu->prompt->type : 0;
  231. if (type == P_MENU || type == P_ROOTMENU)
  232. break;
  233. }
  234. return menu;
  235. }
  236. struct file *file_lookup(const char *name)
  237. {
  238. struct file *file;
  239. for (file = file_list; file; file = file->next) {
  240. if (!strcmp(name, file->name))
  241. return file;
  242. }
  243. file = malloc(sizeof(*file));
  244. memset(file, 0, sizeof(*file));
  245. file->name = strdup(name);
  246. file->next = file_list;
  247. file_list = file;
  248. return file;
  249. }
  250. int file_write_dep(const char *name)
  251. {
  252. struct file *file;
  253. FILE *out;
  254. if (!name)
  255. name = ".config.cmd";
  256. out = fopen(".config.tmp", "w");
  257. if (!out)
  258. return 1;
  259. fprintf(out, "deps_config := \\\n");
  260. for (file = file_list; file; file = file->next) {
  261. if (file->next)
  262. fprintf(out, "\t%s \\\n", file->name);
  263. else
  264. fprintf(out, "\t%s\n", file->name);
  265. }
  266. fprintf(out, "\n.config include/bits/uClibc_config.h: $(deps_config)\n\n$(deps_config):\n");
  267. fclose(out);
  268. rename(".config.tmp", name);
  269. return 0;
  270. }