Commit | Line | Data |
---|---|---|
0c874100 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org> | |
1da177e4 LT |
4 | */ |
5 | ||
dd003306 | 6 | #include <ctype.h> |
10a4b277 | 7 | #include <stdarg.h> |
1da177e4 LT |
8 | #include <stdlib.h> |
9 | #include <string.h> | |
10 | ||
fbaf242c | 11 | #include <list.h> |
a9d83d74 | 12 | #include <xalloc.h> |
1da177e4 | 13 | #include "lkc.h" |
a77a05dc | 14 | #include "internal.h" |
1da177e4 | 15 | |
57e6292d | 16 | static const char nohelp_text[] = "There is no help available for this option."; |
6bd5999d | 17 | |
0f57c759 | 18 | struct menu rootmenu = { .type = M_MENU }; |
1da177e4 LT |
19 | static struct menu **last_entry_ptr; |
20 | ||
7284b4fb MY |
21 | /** |
22 | * menu_next - return the next menu entry with depth-first traversal | |
23 | * @menu: pointer to the current menu | |
24 | * @root: root of the sub-tree to traverse. If NULL is given, the traveral | |
25 | * continues until it reaches the end of the entire menu tree. | |
26 | * return: the menu to visit next, or NULL when it reaches the end. | |
27 | */ | |
28 | struct menu *menu_next(struct menu *menu, struct menu *root) | |
29 | { | |
30 | if (menu->list) | |
31 | return menu->list; | |
32 | ||
33 | while (menu != root && !menu->next) | |
34 | menu = menu->parent; | |
35 | ||
36 | if (menu == root) | |
37 | return NULL; | |
38 | ||
39 | return menu->next; | |
40 | } | |
41 | ||
6425e3b2 | 42 | void menu_warn(const struct menu *menu, const char *fmt, ...) |
1da177e4 LT |
43 | { |
44 | va_list ap; | |
45 | va_start(ap, fmt); | |
40bab83a | 46 | fprintf(stderr, "%s:%d:warning: ", menu->filename, menu->lineno); |
1da177e4 LT |
47 | vfprintf(stderr, fmt, ap); |
48 | fprintf(stderr, "\n"); | |
49 | va_end(ap); | |
50 | } | |
51 | ||
6425e3b2 | 52 | static void prop_warn(const struct property *prop, const char *fmt, ...) |
1da177e4 LT |
53 | { |
54 | va_list ap; | |
55 | va_start(ap, fmt); | |
1a90b0cd | 56 | fprintf(stderr, "%s:%d:warning: ", prop->filename, prop->lineno); |
1da177e4 LT |
57 | vfprintf(stderr, fmt, ap); |
58 | fprintf(stderr, "\n"); | |
59 | va_end(ap); | |
60 | } | |
61 | ||
692d97c3 | 62 | void _menu_init(void) |
1da177e4 LT |
63 | { |
64 | current_entry = current_menu = &rootmenu; | |
65 | last_entry_ptr = &rootmenu.list; | |
66 | } | |
67 | ||
0f57c759 | 68 | void menu_add_entry(struct symbol *sym, enum menu_type type) |
1da177e4 LT |
69 | { |
70 | struct menu *menu; | |
71 | ||
177acf78 | 72 | menu = xmalloc(sizeof(*menu)); |
1da177e4 | 73 | memset(menu, 0, sizeof(*menu)); |
0f57c759 | 74 | menu->type = type; |
1da177e4 LT |
75 | menu->sym = sym; |
76 | menu->parent = current_menu; | |
40bab83a | 77 | menu->filename = cur_filename; |
1d7c4f10 | 78 | menu->lineno = cur_lineno; |
1da177e4 LT |
79 | |
80 | *last_entry_ptr = menu; | |
81 | last_entry_ptr = &menu->next; | |
82 | current_entry = menu; | |
5e6cc7e3 | 83 | if (sym) |
e0492219 | 84 | list_add_tail(&menu->link, &sym->menus); |
1da177e4 LT |
85 | } |
86 | ||
a02f0570 | 87 | struct menu *menu_add_menu(void) |
1da177e4 | 88 | { |
1da177e4 | 89 | last_entry_ptr = ¤t_entry->list; |
644a4b6c MY |
90 | current_menu = current_entry; |
91 | return current_menu; | |
1da177e4 LT |
92 | } |
93 | ||
94 | void menu_end_menu(void) | |
95 | { | |
96 | last_entry_ptr = ¤t_menu->next; | |
97 | current_menu = current_menu->parent; | |
98 | } | |
99 | ||
9a826842 UM |
100 | /* |
101 | * Rewrites 'm' to 'm' && MODULES, so that it evaluates to 'n' when running | |
102 | * without modules | |
103 | */ | |
104 | static struct expr *rewrite_m(struct expr *e) | |
1da177e4 LT |
105 | { |
106 | if (!e) | |
107 | return e; | |
108 | ||
109 | switch (e->type) { | |
110 | case E_NOT: | |
f93d6bfb | 111 | e = expr_alloc_one(E_NOT, rewrite_m(e->left.expr)); |
1da177e4 LT |
112 | break; |
113 | case E_OR: | |
114 | case E_AND: | |
f93d6bfb MY |
115 | e = expr_alloc_two(e->type, |
116 | rewrite_m(e->left.expr), | |
117 | rewrite_m(e->right.expr)); | |
1da177e4 LT |
118 | break; |
119 | case E_SYMBOL: | |
120 | /* change 'm' into 'm' && MODULES */ | |
121 | if (e->left.sym == &symbol_mod) | |
122 | return expr_alloc_and(e, expr_alloc_symbol(modules_sym)); | |
123 | break; | |
124 | default: | |
125 | break; | |
126 | } | |
127 | return e; | |
128 | } | |
129 | ||
130 | void menu_add_dep(struct expr *dep) | |
131 | { | |
f77850d3 | 132 | current_entry->dep = expr_alloc_and(current_entry->dep, dep); |
1da177e4 LT |
133 | } |
134 | ||
135 | void menu_set_type(int type) | |
136 | { | |
137 | struct symbol *sym = current_entry->sym; | |
138 | ||
139 | if (sym->type == type) | |
140 | return; | |
141 | if (sym->type == S_UNKNOWN) { | |
142 | sym->type = type; | |
143 | return; | |
144 | } | |
57540f1d MW |
145 | menu_warn(current_entry, |
146 | "ignoring type redefinition of '%s' from '%s' to '%s'", | |
147 | sym->name ? sym->name : "<choice>", | |
148 | sym_type_name(sym->type), sym_type_name(type)); | |
1da177e4 LT |
149 | } |
150 | ||
2ffeef61 MY |
151 | static struct property *menu_add_prop(enum prop_type type, struct expr *expr, |
152 | struct expr *dep) | |
1da177e4 | 153 | { |
adf7c5bd | 154 | struct property *prop; |
1da177e4 | 155 | |
adf7c5bd MY |
156 | prop = xmalloc(sizeof(*prop)); |
157 | memset(prop, 0, sizeof(*prop)); | |
158 | prop->type = type; | |
1a90b0cd | 159 | prop->filename = cur_filename; |
1d7c4f10 | 160 | prop->lineno = cur_lineno; |
1da177e4 | 161 | prop->menu = current_entry; |
1da177e4 | 162 | prop->expr = expr; |
f77850d3 | 163 | prop->visible.expr = dep; |
1da177e4 | 164 | |
adf7c5bd MY |
165 | /* append property to the prop list of symbol */ |
166 | if (current_entry->sym) { | |
167 | struct property **propp; | |
168 | ||
169 | for (propp = ¤t_entry->sym->prop; | |
170 | *propp; | |
171 | propp = &(*propp)->next) | |
172 | ; | |
173 | *propp = prop; | |
174 | } | |
175 | ||
024352ff MY |
176 | return prop; |
177 | } | |
178 | ||
6425e3b2 | 179 | struct property *menu_add_prompt(enum prop_type type, const char *prompt, |
024352ff MY |
180 | struct expr *dep) |
181 | { | |
2ffeef61 | 182 | struct property *prop = menu_add_prop(type, NULL, dep); |
7ad12278 | 183 | |
024352ff MY |
184 | if (isspace(*prompt)) { |
185 | prop_warn(prop, "leading whitespace ignored"); | |
186 | while (isspace(*prompt)) | |
187 | prompt++; | |
1da177e4 | 188 | } |
024352ff MY |
189 | if (current_entry->prompt) |
190 | prop_warn(prop, "prompt redefined"); | |
191 | ||
192 | /* Apply all upper menus' visibilities to actual prompts. */ | |
193 | if (type == P_PROMPT) { | |
194 | struct menu *menu = current_entry; | |
195 | ||
196 | while ((menu = menu->parent) != NULL) { | |
024352ff MY |
197 | |
198 | if (!menu->visibility) | |
199 | continue; | |
024352ff | 200 | prop->visible.expr = expr_alloc_and(prop->visible.expr, |
f93d6bfb | 201 | menu->visibility); |
024352ff MY |
202 | } |
203 | } | |
204 | ||
205 | current_entry->prompt = prop; | |
f001f7f8 | 206 | prop->text = prompt; |
1da177e4 LT |
207 | |
208 | return prop; | |
209 | } | |
210 | ||
86e187ff AL |
211 | void menu_add_visibility(struct expr *expr) |
212 | { | |
213 | current_entry->visibility = expr_alloc_and(current_entry->visibility, | |
214 | expr); | |
215 | } | |
216 | ||
1da177e4 LT |
217 | void menu_add_expr(enum prop_type type, struct expr *expr, struct expr *dep) |
218 | { | |
2ffeef61 | 219 | menu_add_prop(type, expr, dep); |
1da177e4 LT |
220 | } |
221 | ||
222 | void menu_add_symbol(enum prop_type type, struct symbol *sym, struct expr *dep) | |
223 | { | |
2ffeef61 | 224 | menu_add_prop(type, expr_alloc_symbol(sym), dep); |
1da177e4 LT |
225 | } |
226 | ||
ab60bd0b | 227 | static int menu_validate_number(struct symbol *sym, struct symbol *sym2) |
4cf3cbe2 RZ |
228 | { |
229 | return sym2->type == S_INT || sym2->type == S_HEX || | |
230 | (sym2->type == S_UNKNOWN && sym_string_valid(sym, sym2->name)); | |
231 | } | |
232 | ||
4356f489 | 233 | static void sym_check_prop(struct symbol *sym) |
1da177e4 LT |
234 | { |
235 | struct property *prop; | |
236 | struct symbol *sym2; | |
237e3ad0 NP |
237 | char *use; |
238 | ||
1da177e4 LT |
239 | for (prop = sym->prop; prop; prop = prop->next) { |
240 | switch (prop->type) { | |
241 | case P_DEFAULT: | |
242 | if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) && | |
243 | prop->expr->type != E_SYMBOL) | |
244 | prop_warn(prop, | |
4280eae0 | 245 | "default for config symbol '%s'" |
1da177e4 | 246 | " must be a single symbol", sym->name); |
ab60bd0b AL |
247 | if (prop->expr->type != E_SYMBOL) |
248 | break; | |
249 | sym2 = prop_get_symbol(prop); | |
250 | if (sym->type == S_HEX || sym->type == S_INT) { | |
251 | if (!menu_validate_number(sym, sym2)) | |
252 | prop_warn(prop, | |
253 | "'%s': number is invalid", | |
254 | sym->name); | |
255 | } | |
2c37e084 | 256 | if (sym_is_choice(sym)) { |
6ffe4fdf | 257 | struct menu *choice = sym_get_choice_menu(sym2); |
2c37e084 | 258 | |
6ffe4fdf | 259 | if (!choice || choice->sym != sym) |
2c37e084 UM |
260 | prop_warn(prop, |
261 | "choice default symbol '%s' is not contained in the choice", | |
262 | sym2->name); | |
263 | } | |
1da177e4 LT |
264 | break; |
265 | case P_SELECT: | |
237e3ad0 NP |
266 | case P_IMPLY: |
267 | use = prop->type == P_SELECT ? "select" : "imply"; | |
1da177e4 LT |
268 | sym2 = prop_get_symbol(prop); |
269 | if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE) | |
270 | prop_warn(prop, | |
237e3ad0 | 271 | "config symbol '%s' uses %s, but is " |
b92d804a | 272 | "not bool or tristate", sym->name, use); |
603d4988 | 273 | else if (sym2->type != S_UNKNOWN && |
bb66fc67 MY |
274 | sym2->type != S_BOOLEAN && |
275 | sym2->type != S_TRISTATE) | |
1da177e4 | 276 | prop_warn(prop, |
237e3ad0 | 277 | "'%s' has wrong type. '%s' only " |
b92d804a | 278 | "accept arguments of bool and " |
237e3ad0 | 279 | "tristate type", sym2->name, use); |
1da177e4 LT |
280 | break; |
281 | case P_RANGE: | |
282 | if (sym->type != S_INT && sym->type != S_HEX) | |
283 | prop_warn(prop, "range is only allowed " | |
bb66fc67 | 284 | "for int or hex symbols"); |
ab60bd0b AL |
285 | if (!menu_validate_number(sym, prop->expr->left.sym) || |
286 | !menu_validate_number(sym, prop->expr->right.sym)) | |
1da177e4 LT |
287 | prop_warn(prop, "range is invalid"); |
288 | break; | |
289 | default: | |
290 | ; | |
291 | } | |
292 | } | |
293 | } | |
294 | ||
7e3465f6 | 295 | static void _menu_finalize(struct menu *parent, bool inside_choice) |
1da177e4 LT |
296 | { |
297 | struct menu *menu, *last_menu; | |
298 | struct symbol *sym; | |
299 | struct property *prop; | |
d533828e | 300 | struct expr *basedep, *dep, *dep2; |
1da177e4 LT |
301 | |
302 | sym = parent->sym; | |
303 | if (parent->list) { | |
fa8cedae UM |
304 | /* |
305 | * This menu node has children. We (recursively) process them | |
306 | * and propagate parent dependencies before moving on. | |
307 | */ | |
308 | ||
fa8cedae | 309 | /* For each child menu node... */ |
1da177e4 | 310 | for (menu = parent->list; menu; menu = menu->next) { |
fa8cedae UM |
311 | /* |
312 | * Propagate parent dependencies to the child menu | |
313 | * node, also rewriting and simplifying expressions | |
314 | */ | |
f77850d3 UM |
315 | basedep = rewrite_m(menu->dep); |
316 | basedep = expr_transform(basedep); | |
f93d6bfb | 317 | basedep = expr_alloc_and(parent->dep, basedep); |
1da177e4 LT |
318 | basedep = expr_eliminate_dups(basedep); |
319 | menu->dep = basedep; | |
fa8cedae | 320 | |
1da177e4 | 321 | if (menu->sym) |
fa8cedae UM |
322 | /* |
323 | * Note: For symbols, all prompts are included | |
324 | * too in the symbol's own property list | |
325 | */ | |
1da177e4 LT |
326 | prop = menu->sym->prop; |
327 | else | |
fa8cedae UM |
328 | /* |
329 | * For non-symbol menu nodes, we just need to | |
330 | * handle the prompt | |
331 | */ | |
1da177e4 | 332 | prop = menu->prompt; |
fa8cedae UM |
333 | |
334 | /* For each property... */ | |
1da177e4 LT |
335 | for (; prop; prop = prop->next) { |
336 | if (prop->menu != menu) | |
fa8cedae UM |
337 | /* |
338 | * Two possibilities: | |
339 | * | |
340 | * 1. The property lacks dependencies | |
341 | * and so isn't location-specific, | |
342 | * e.g. an 'option' | |
343 | * | |
344 | * 2. The property belongs to a symbol | |
345 | * defined in multiple locations and | |
346 | * is from some other location. It | |
347 | * will be handled there in that | |
348 | * case. | |
349 | * | |
350 | * Skip the property. | |
351 | */ | |
1da177e4 | 352 | continue; |
fa8cedae UM |
353 | |
354 | /* | |
355 | * Propagate parent dependencies to the | |
356 | * property's condition, rewriting and | |
357 | * simplifying expressions at the same time | |
358 | */ | |
f77850d3 UM |
359 | dep = rewrite_m(prop->visible.expr); |
360 | dep = expr_transform(dep); | |
f93d6bfb | 361 | dep = expr_alloc_and(basedep, dep); |
1da177e4 | 362 | dep = expr_eliminate_dups(dep); |
1da177e4 | 363 | prop->visible.expr = dep; |
fa8cedae UM |
364 | |
365 | /* | |
366 | * Handle selects and implies, which modify the | |
367 | * dependencies of the selected/implied symbol | |
368 | */ | |
1da177e4 LT |
369 | if (prop->type == P_SELECT) { |
370 | struct symbol *es = prop_get_symbol(prop); | |
371 | es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr, | |
f93d6bfb | 372 | expr_alloc_and(expr_alloc_symbol(menu->sym), dep)); |
237e3ad0 NP |
373 | } else if (prop->type == P_IMPLY) { |
374 | struct symbol *es = prop_get_symbol(prop); | |
375 | es->implied.expr = expr_alloc_or(es->implied.expr, | |
f93d6bfb | 376 | expr_alloc_and(expr_alloc_symbol(menu->sym), dep)); |
1da177e4 LT |
377 | } |
378 | } | |
379 | } | |
fa8cedae UM |
380 | |
381 | /* | |
382 | * Recursively process children in the same fashion before | |
383 | * moving on | |
384 | */ | |
1da177e4 | 385 | for (menu = parent->list; menu; menu = menu->next) |
d533828e | 386 | _menu_finalize(menu, sym && sym_is_choice(sym)); |
7e3465f6 | 387 | } else if (!inside_choice && sym) { |
05cccce5 UM |
388 | /* |
389 | * Automatic submenu creation. If sym is a symbol and A, B, C, | |
390 | * ... are consecutive items (symbols, menus, ifs, etc.) that | |
391 | * all depend on sym, then the following menu structure is | |
392 | * created: | |
393 | * | |
394 | * sym | |
395 | * +-A | |
396 | * +-B | |
397 | * +-C | |
398 | * ... | |
399 | * | |
400 | * This also works recursively, giving the following structure | |
401 | * if A is a symbol and B depends on A: | |
402 | * | |
403 | * sym | |
404 | * +-A | |
405 | * | +-B | |
406 | * +-C | |
407 | * ... | |
408 | */ | |
409 | ||
1da177e4 LT |
410 | basedep = parent->prompt ? parent->prompt->visible.expr : NULL; |
411 | basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no); | |
412 | basedep = expr_eliminate_dups(expr_transform(basedep)); | |
05cccce5 UM |
413 | |
414 | /* Examine consecutive elements after sym */ | |
1da177e4 LT |
415 | last_menu = NULL; |
416 | for (menu = parent->next; menu; menu = menu->next) { | |
417 | dep = menu->prompt ? menu->prompt->visible.expr : menu->dep; | |
418 | if (!expr_contains_symbol(dep, sym)) | |
05cccce5 | 419 | /* No dependency, quit */ |
1da177e4 LT |
420 | break; |
421 | if (expr_depends_symbol(dep, sym)) | |
05cccce5 | 422 | /* Absolute dependency, put in submenu */ |
1da177e4 | 423 | goto next; |
05cccce5 UM |
424 | |
425 | /* | |
426 | * Also consider it a dependency on sym if our | |
427 | * dependencies contain sym and are a "superset" of | |
428 | * sym's dependencies, e.g. '(sym || Q) && R' when sym | |
429 | * depends on R. | |
430 | * | |
431 | * Note that 'R' might be from an enclosing menu or if, | |
432 | * making this a more common case than it might seem. | |
433 | */ | |
1da177e4 LT |
434 | dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no); |
435 | dep = expr_eliminate_dups(expr_transform(dep)); | |
f93d6bfb | 436 | dep2 = basedep; |
1da177e4 | 437 | expr_eliminate_eq(&dep, &dep2); |
1da177e4 | 438 | if (!expr_is_yes(dep2)) { |
05cccce5 | 439 | /* Not superset, quit */ |
1da177e4 LT |
440 | break; |
441 | } | |
05cccce5 | 442 | /* Superset, put in submenu */ |
1da177e4 | 443 | next: |
7e3465f6 | 444 | _menu_finalize(menu, false); |
1da177e4 LT |
445 | menu->parent = parent; |
446 | last_menu = menu; | |
447 | } | |
448 | if (last_menu) { | |
449 | parent->list = parent->next; | |
450 | parent->next = last_menu->next; | |
451 | last_menu->next = NULL; | |
452 | } | |
ff5ff606 | 453 | |
ec6452a5 | 454 | sym->dir_dep.expr = expr_alloc_or(sym->dir_dep.expr, parent->dep); |
1da177e4 LT |
455 | } |
456 | for (menu = parent->list; menu; menu = menu->next) { | |
9d1a9e8b UM |
457 | /* |
458 | * This code serves two purposes: | |
459 | * | |
460 | * (1) Flattening 'if' blocks, which do not specify a submenu | |
461 | * and only add dependencies. | |
462 | * | |
463 | * (Automatic submenu creation might still create a submenu | |
464 | * from an 'if' before this code runs.) | |
465 | * | |
466 | * (2) "Undoing" any automatic submenus created earlier below | |
467 | * promptless symbols. | |
468 | * | |
469 | * Before: | |
470 | * | |
471 | * A | |
472 | * if ... (or promptless symbol) | |
473 | * +-B | |
474 | * +-C | |
475 | * D | |
476 | * | |
477 | * After: | |
478 | * | |
479 | * A | |
480 | * if ... (or promptless symbol) | |
481 | * B | |
482 | * C | |
483 | * D | |
484 | */ | |
1da177e4 LT |
485 | if (menu->list && (!menu->prompt || !menu->prompt->text)) { |
486 | for (last_menu = menu->list; ; last_menu = last_menu->next) { | |
487 | last_menu->parent = parent; | |
488 | if (!last_menu->next) | |
489 | break; | |
490 | } | |
491 | last_menu->next = menu->next; | |
492 | menu->next = menu->list; | |
493 | menu->list = NULL; | |
494 | } | |
495 | } | |
496 | ||
497 | if (sym && !(sym->flags & SYMBOL_WARNED)) { | |
498 | if (sym->type == S_UNKNOWN) | |
f001f7f8 | 499 | menu_warn(parent, "config symbol defined without type"); |
1da177e4 | 500 | |
1da177e4 LT |
501 | /* Check properties connected to this symbol */ |
502 | sym_check_prop(sym); | |
503 | sym->flags |= SYMBOL_WARNED; | |
504 | } | |
1da177e4 LT |
505 | } |
506 | ||
7e3465f6 MY |
507 | void menu_finalize(void) |
508 | { | |
509 | _menu_finalize(&rootmenu, false); | |
510 | } | |
511 | ||
6425e3b2 | 512 | bool menu_has_prompt(const struct menu *menu) |
22c7eca6 LZ |
513 | { |
514 | if (!menu->prompt) | |
515 | return false; | |
516 | return true; | |
517 | } | |
518 | ||
1278ebdb DG |
519 | /* |
520 | * Determine if a menu is empty. | |
521 | * A menu is considered empty if it contains no or only | |
522 | * invisible entries. | |
523 | */ | |
524 | bool menu_is_empty(struct menu *menu) | |
525 | { | |
526 | struct menu *child; | |
527 | ||
528 | for (child = menu->list; child; child = child->next) { | |
529 | if (menu_is_visible(child)) | |
530 | return(false); | |
531 | } | |
532 | return(true); | |
533 | } | |
534 | ||
1da177e4 LT |
535 | bool menu_is_visible(struct menu *menu) |
536 | { | |
d01661e1 | 537 | struct menu *child; |
1da177e4 LT |
538 | struct symbol *sym; |
539 | tristate visible; | |
540 | ||
541 | if (!menu->prompt) | |
542 | return false; | |
22c7eca6 | 543 | |
86e187ff AL |
544 | if (menu->visibility) { |
545 | if (expr_calc_value(menu->visibility) == no) | |
aab24a89 | 546 | return false; |
86e187ff AL |
547 | } |
548 | ||
1da177e4 LT |
549 | sym = menu->sym; |
550 | if (sym) { | |
551 | sym_calc_value(sym); | |
552 | visible = menu->prompt->visible.tri; | |
553 | } else | |
554 | visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr); | |
555 | ||
d01661e1 MY |
556 | if (visible != no) |
557 | return true; | |
558 | ||
559 | if (!sym || sym_get_tristate_value(menu->sym) == no) | |
560 | return false; | |
561 | ||
562 | for (child = menu->list; child; child = child->next) | |
563 | if (menu_is_visible(child)) | |
564 | return true; | |
565 | ||
566 | return false; | |
1da177e4 LT |
567 | } |
568 | ||
6425e3b2 | 569 | const char *menu_get_prompt(const struct menu *menu) |
1da177e4 LT |
570 | { |
571 | if (menu->prompt) | |
01771b0f | 572 | return menu->prompt->text; |
1da177e4 | 573 | else if (menu->sym) |
01771b0f | 574 | return menu->sym->name; |
1da177e4 LT |
575 | return NULL; |
576 | } | |
577 | ||
1da177e4 LT |
578 | struct menu *menu_get_parent_menu(struct menu *menu) |
579 | { | |
580 | enum prop_type type; | |
581 | ||
582 | for (; menu != &rootmenu; menu = menu->parent) { | |
583 | type = menu->prompt ? menu->prompt->type : 0; | |
584 | if (type == P_MENU) | |
585 | break; | |
586 | } | |
587 | return menu; | |
588 | } | |
589 | ||
6425e3b2 | 590 | static void get_def_str(struct gstr *r, const struct menu *menu) |
edda15f2 TH |
591 | { |
592 | str_printf(r, "Defined at %s:%d\n", | |
40bab83a | 593 | menu->filename, menu->lineno); |
edda15f2 TH |
594 | } |
595 | ||
6425e3b2 MY |
596 | static void get_dep_str(struct gstr *r, const struct expr *expr, |
597 | const char *prefix) | |
edda15f2 TH |
598 | { |
599 | if (!expr_is_yes(expr)) { | |
600 | str_append(r, prefix); | |
601 | expr_gstr_print(expr, r); | |
602 | str_append(r, "\n"); | |
603 | } | |
604 | } | |
605 | ||
e14f1242 MY |
606 | int __attribute__((weak)) get_jump_key_char(void) |
607 | { | |
608 | return -1; | |
609 | } | |
610 | ||
95ac9b3b | 611 | static void get_prompt_str(struct gstr *r, struct property *prop, |
bad9955d | 612 | struct list_head *head) |
6bd5999d CR |
613 | { |
614 | int i, j; | |
5e609add | 615 | struct menu *submenu[8], *menu, *location = NULL; |
2d560306 | 616 | struct jump_key *jump = NULL; |
6bd5999d | 617 | |
edda15f2 TH |
618 | str_printf(r, " Prompt: %s\n", prop->text); |
619 | ||
620 | get_dep_str(r, prop->menu->dep, " Depends on: "); | |
3460d0bc TH |
621 | /* |
622 | * Most prompts in Linux have visibility that exactly matches their | |
623 | * dependencies. For these, we print only the dependencies to improve | |
624 | * readability. However, prompts with inline "if" expressions and | |
625 | * prompts with a parent that has a "visible if" expression have | |
626 | * differing dependencies and visibility. In these rare cases, we | |
627 | * print both. | |
628 | */ | |
629 | if (!expr_eq(prop->menu->dep, prop->visible.expr)) | |
630 | get_dep_str(r, prop->visible.expr, " Visible if: "); | |
631 | ||
7a263a04 MY |
632 | menu = prop->menu; |
633 | for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent) { | |
6bd5999d | 634 | submenu[i++] = menu; |
1791360c | 635 | if (location == NULL && menu_is_visible(menu)) |
5e609add BP |
636 | location = menu; |
637 | } | |
95ac9b3b | 638 | if (head && location) { |
177acf78 | 639 | jump = xmalloc(sizeof(struct jump_key)); |
7a263a04 | 640 | jump->target = location; |
bad9955d | 641 | list_add_tail(&jump->entries, head); |
95ac9b3b | 642 | } |
5e609add | 643 | |
d05377e1 | 644 | str_printf(r, " Location:\n"); |
e14f1242 MY |
645 | for (j = 0; --i >= 0; j++) { |
646 | int jk = -1; | |
647 | int indent = 2 * j + 4; | |
648 | ||
d05377e1 | 649 | menu = submenu[i]; |
e14f1242 | 650 | if (jump && menu == location) { |
d05377e1 | 651 | jump->offset = strlen(r->s); |
e14f1242 MY |
652 | jk = get_jump_key_char(); |
653 | } | |
654 | ||
655 | if (jk >= 0) { | |
656 | str_printf(r, "(%c)", jk); | |
657 | indent -= 3; | |
658 | } | |
659 | ||
660 | str_printf(r, "%*c-> %s", indent, ' ', menu_get_prompt(menu)); | |
d05377e1 AM |
661 | if (menu->sym) { |
662 | str_printf(r, " (%s [=%s])", menu->sym->name ? | |
663 | menu->sym->name : "<choice>", | |
664 | sym_get_string_value(menu->sym)); | |
6bd5999d | 665 | } |
d05377e1 | 666 | str_append(r, "\n"); |
6bd5999d CR |
667 | } |
668 | } | |
669 | ||
237e3ad0 NP |
670 | static void get_symbol_props_str(struct gstr *r, struct symbol *sym, |
671 | enum prop_type tok, const char *prefix) | |
672 | { | |
673 | bool hit = false; | |
674 | struct property *prop; | |
675 | ||
676 | for_all_properties(sym, prop, tok) { | |
677 | if (!hit) { | |
678 | str_append(r, prefix); | |
679 | hit = true; | |
680 | } else | |
681 | str_printf(r, " && "); | |
682 | expr_gstr_print(prop->expr, r); | |
683 | } | |
684 | if (hit) | |
685 | str_append(r, "\n"); | |
686 | } | |
687 | ||
5e609add | 688 | /* |
95ac9b3b | 689 | * head is optional and may be NULL |
5e609add | 690 | */ |
ad8d40cd | 691 | static void get_symbol_str(struct gstr *r, struct symbol *sym, |
bad9955d | 692 | struct list_head *head) |
6bd5999d | 693 | { |
6bd5999d | 694 | struct property *prop; |
bedf9236 | 695 | struct menu *menu; |
6bd5999d | 696 | |
b040b44c | 697 | if (sym && sym->name) { |
6bd5999d CR |
698 | str_printf(r, "Symbol: %s [=%s]\n", sym->name, |
699 | sym_get_string_value(sym)); | |
b040b44c | 700 | str_printf(r, "Type : %s\n", sym_type_name(sym->type)); |
70ed0747 LZ |
701 | if (sym->type == S_INT || sym->type == S_HEX) { |
702 | prop = sym_get_range_prop(sym); | |
703 | if (prop) { | |
704 | str_printf(r, "Range : "); | |
705 | expr_gstr_print(prop->expr, r); | |
706 | str_append(r, "\n"); | |
707 | } | |
708 | } | |
b040b44c | 709 | } |
edda15f2 TH |
710 | |
711 | /* Print the definitions with prompts before the ones without */ | |
bedf9236 MY |
712 | list_for_each_entry(menu, &sym->menus, link) { |
713 | if (menu->prompt) { | |
714 | get_def_str(r, menu); | |
715 | get_prompt_str(r, menu->prompt, head); | |
edda15f2 TH |
716 | } |
717 | } | |
718 | ||
bedf9236 MY |
719 | list_for_each_entry(menu, &sym->menus, link) { |
720 | if (!menu->prompt) { | |
721 | get_def_str(r, menu); | |
722 | get_dep_str(r, menu->dep, " Depends on: "); | |
383da76f | 723 | } |
bcdedcc1 | 724 | } |
383da76f | 725 | |
a9609686 | 726 | get_symbol_props_str(r, sym, P_SELECT, "Selects: "); |
6bd5999d | 727 | if (sym->rev_dep.expr) { |
a9609686 TH |
728 | expr_gstr_print_revdep(sym->rev_dep.expr, r, yes, "Selected by [y]:\n"); |
729 | expr_gstr_print_revdep(sym->rev_dep.expr, r, mod, "Selected by [m]:\n"); | |
730 | expr_gstr_print_revdep(sym->rev_dep.expr, r, no, "Selected by [n]:\n"); | |
6bd5999d | 731 | } |
237e3ad0 | 732 | |
a9609686 | 733 | get_symbol_props_str(r, sym, P_IMPLY, "Implies: "); |
237e3ad0 | 734 | if (sym->implied.expr) { |
a9609686 TH |
735 | expr_gstr_print_revdep(sym->implied.expr, r, yes, "Implied by [y]:\n"); |
736 | expr_gstr_print_revdep(sym->implied.expr, r, mod, "Implied by [m]:\n"); | |
737 | expr_gstr_print_revdep(sym->implied.expr, r, no, "Implied by [n]:\n"); | |
237e3ad0 NP |
738 | } |
739 | ||
6bd5999d CR |
740 | str_append(r, "\n\n"); |
741 | } | |
742 | ||
bad9955d | 743 | struct gstr get_relations_str(struct symbol **sym_arr, struct list_head *head) |
692d97c3 | 744 | { |
745 | struct symbol *sym; | |
746 | struct gstr res = str_new(); | |
95ac9b3b | 747 | int i; |
692d97c3 | 748 | |
749 | for (i = 0; sym_arr && (sym = sym_arr[i]); i++) | |
95ac9b3b | 750 | get_symbol_str(&res, sym, head); |
692d97c3 | 751 | if (!i) |
694c49a7 | 752 | str_append(&res, "No matches found.\n"); |
692d97c3 | 753 | return res; |
754 | } | |
755 | ||
756 | ||
6bd5999d CR |
757 | void menu_get_ext_help(struct menu *menu, struct gstr *help) |
758 | { | |
759 | struct symbol *sym = menu->sym; | |
57e6292d | 760 | const char *help_text = nohelp_text; |
6bd5999d | 761 | |
092e39d1 | 762 | if (menu->help) { |
3f198dfe | 763 | if (sym->name) |
ffb5957b | 764 | str_printf(help, "%s%s:\n\n", CONFIG_, sym->name); |
092e39d1 | 765 | help_text = menu->help; |
6bd5999d | 766 | } |
694c49a7 | 767 | str_printf(help, "%s\n", help_text); |
4779105e | 768 | if (sym) |
95ac9b3b | 769 | get_symbol_str(help, sym, NULL); |
6bd5999d | 770 | } |