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