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