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