1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
15 static const char nohelp_text[] = "There is no help available for this option.";
18 static struct menu **last_entry_ptr;
20 void menu_warn(struct menu *menu, const char *fmt, ...)
24 fprintf(stderr, "%s:%d:warning: ", menu->filename, menu->lineno);
25 vfprintf(stderr, fmt, ap);
26 fprintf(stderr, "\n");
30 static void prop_warn(struct property *prop, const char *fmt, ...)
34 fprintf(stderr, "%s:%d:warning: ", prop->filename, prop->lineno);
35 vfprintf(stderr, fmt, ap);
36 fprintf(stderr, "\n");
42 current_entry = current_menu = &rootmenu;
43 last_entry_ptr = &rootmenu.list;
46 void menu_add_entry(struct symbol *sym)
50 menu = xmalloc(sizeof(*menu));
51 memset(menu, 0, sizeof(*menu));
53 menu->parent = current_menu;
54 menu->filename = cur_filename;
55 menu->lineno = cur_lineno;
57 *last_entry_ptr = menu;
58 last_entry_ptr = &menu->next;
61 menu_add_symbol(P_SYMBOL, sym, NULL);
62 list_add_tail(&menu->link, &sym->menus);
66 struct menu *menu_add_menu(void)
68 last_entry_ptr = ¤t_entry->list;
69 current_menu = current_entry;
73 void menu_end_menu(void)
75 last_entry_ptr = ¤t_menu->next;
76 current_menu = current_menu->parent;
80 * Rewrites 'm' to 'm' && MODULES, so that it evaluates to 'n' when running
83 static struct expr *rewrite_m(struct expr *e)
90 e->left.expr = rewrite_m(e->left.expr);
94 e->left.expr = rewrite_m(e->left.expr);
95 e->right.expr = rewrite_m(e->right.expr);
98 /* change 'm' into 'm' && MODULES */
99 if (e->left.sym == &symbol_mod)
100 return expr_alloc_and(e, expr_alloc_symbol(modules_sym));
108 void menu_add_dep(struct expr *dep)
110 current_entry->dep = expr_alloc_and(current_entry->dep, dep);
113 void menu_set_type(int type)
115 struct symbol *sym = current_entry->sym;
117 if (sym->type == type)
119 if (sym->type == S_UNKNOWN) {
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));
129 static struct property *menu_add_prop(enum prop_type type, struct expr *expr,
132 struct property *prop;
134 prop = xmalloc(sizeof(*prop));
135 memset(prop, 0, sizeof(*prop));
137 prop->filename = cur_filename;
138 prop->lineno = cur_lineno;
139 prop->menu = current_entry;
141 prop->visible.expr = dep;
143 /* append property to the prop list of symbol */
144 if (current_entry->sym) {
145 struct property **propp;
147 for (propp = ¤t_entry->sym->prop;
149 propp = &(*propp)->next)
157 struct property *menu_add_prompt(enum prop_type type, char *prompt,
160 struct property *prop = menu_add_prop(type, NULL, dep);
162 if (isspace(*prompt)) {
163 prop_warn(prop, "leading whitespace ignored");
164 while (isspace(*prompt))
167 if (current_entry->prompt)
168 prop_warn(prop, "prompt redefined");
170 /* Apply all upper menus' visibilities to actual prompts. */
171 if (type == P_PROMPT) {
172 struct menu *menu = current_entry;
174 while ((menu = menu->parent) != NULL) {
175 struct expr *dup_expr;
177 if (!menu->visibility)
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.
186 dup_expr = expr_copy(menu->visibility);
188 prop->visible.expr = expr_alloc_and(prop->visible.expr,
193 current_entry->prompt = prop;
199 void menu_add_visibility(struct expr *expr)
201 current_entry->visibility = expr_alloc_and(current_entry->visibility,
205 void menu_add_expr(enum prop_type type, struct expr *expr, struct expr *dep)
207 menu_add_prop(type, expr, dep);
210 void menu_add_symbol(enum prop_type type, struct symbol *sym, struct expr *dep)
212 menu_add_prop(type, expr_alloc_symbol(sym), dep);
215 static int menu_validate_number(struct symbol *sym, struct symbol *sym2)
217 return sym2->type == S_INT || sym2->type == S_HEX ||
218 (sym2->type == S_UNKNOWN && sym_string_valid(sym, sym2->name));
221 static void sym_check_prop(struct symbol *sym)
223 struct property *prop;
227 for (prop = sym->prop; prop; prop = prop->next) {
228 switch (prop->type) {
230 if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) &&
231 prop->expr->type != E_SYMBOL)
233 "default for config symbol '%s'"
234 " must be a single symbol", sym->name);
235 if (prop->expr->type != E_SYMBOL)
237 sym2 = prop_get_symbol(prop);
238 if (sym->type == S_HEX || sym->type == S_INT) {
239 if (!menu_validate_number(sym, sym2))
241 "'%s': number is invalid",
244 if (sym_is_choice(sym)) {
245 struct property *choice_prop =
246 sym_get_choice_prop(sym2);
249 prop_get_symbol(choice_prop) != sym)
251 "choice default symbol '%s' is not contained in the choice",
257 use = prop->type == P_SELECT ? "select" : "imply";
258 sym2 = prop_get_symbol(prop);
259 if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE)
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)
267 "'%s' has wrong type. '%s' only "
268 "accept arguments of bool and "
269 "tristate type", sym2->name, use);
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");
285 void menu_finalize(struct menu *parent)
287 struct menu *menu, *last_menu;
289 struct property *prop;
290 struct expr *parentdep, *basedep, *dep, *dep2, **ep;
295 * This menu node has children. We (recursively) process them
296 * and propagate parent dependencies before moving on.
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);
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.
317 parentdep = expr_alloc_symbol(sym);
319 /* Menu node for 'menu', 'if' */
320 parentdep = parent->dep;
323 /* For each child menu node... */
324 for (menu = parent->list; menu; menu = menu->next) {
326 * Propagate parent dependencies to the child menu
327 * node, also rewriting and simplifying expressions
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);
337 * Note: For symbols, all prompts are included
338 * too in the symbol's own property list
340 prop = menu->sym->prop;
343 * For non-symbol menu nodes, we just need to
348 /* For each property... */
349 for (; prop; prop = prop->next) {
350 if (prop->menu != menu)
354 * 1. The property lacks dependencies
355 * and so isn't location-specific,
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
369 * Propagate parent dependencies to the
370 * property's condition, rewriting and
371 * simplifying expressions at the same time
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;
382 * Handle selects and implies, which modify the
383 * dependencies of the selected/implied symbol
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)));
397 if (sym && sym_is_choice(sym))
398 expr_free(parentdep);
401 * Recursively process children in the same fashion before
404 for (menu = parent->list; menu; menu = menu->next)
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
419 * This also works recursively, giving the following structure
420 * if A is a symbol and B depends on A:
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));
433 /* Examine consecutive elements after sym */
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 */
440 if (expr_depends_symbol(dep, sym))
441 /* Absolute dependency, put in submenu */
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
450 * Note that 'R' might be from an enclosing menu or if,
451 * making this a more common case than it might seem.
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);
458 if (!expr_is_yes(dep2)) {
459 /* Not superset, quit */
463 /* Superset, put in submenu */
467 menu->parent = parent;
472 parent->list = parent->next;
473 parent->next = last_menu->next;
474 last_menu->next = NULL;
477 sym->dir_dep.expr = expr_alloc_or(sym->dir_dep.expr, parent->dep);
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;
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)
492 if (prop->type == P_PROMPT &&
493 prop->menu->parent->sym != sym)
494 prop_warn(prop, "choice value used outside its choice group");
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-
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)
508 prop->visible.expr = expr_alloc_and(expr_copy(basedep),
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)
516 *ep = expr_alloc_one(E_LIST, NULL);
517 (*ep)->right.sym = menu->sym;
521 * This code serves two purposes:
523 * (1) Flattening 'if' blocks, which do not specify a submenu
524 * and only add dependencies.
526 * (Automatic submenu creation might still create a submenu
527 * from an 'if' before this code runs.)
529 * (2) "Undoing" any automatic submenus created earlier below
530 * promptless symbols.
535 * if ... (or promptless symbol)
543 * if ... (or promptless symbol)
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)
554 last_menu->next = menu->next;
555 menu->next = menu->list;
560 if (sym && !(sym->flags & SYMBOL_WARNED)) {
561 if (sym->type == S_UNKNOWN)
562 menu_warn(parent, "config symbol defined without type");
564 /* Check properties connected to this symbol */
566 sym->flags |= SYMBOL_WARNED;
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.
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.
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)));
585 bool menu_has_prompt(struct menu *menu)
593 * Determine if a menu is empty.
594 * A menu is considered empty if it contains no or only
597 bool menu_is_empty(struct menu *menu)
601 for (child = menu->list; child; child = child->next) {
602 if (menu_is_visible(child))
608 bool menu_is_visible(struct menu *menu)
617 if (menu->visibility) {
618 if (expr_calc_value(menu->visibility) == no)
625 visible = menu->prompt->visible.tri;
627 visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr);
632 if (!sym || sym_get_tristate_value(menu->sym) == no)
635 for (child = menu->list; child; child = child->next) {
636 if (menu_is_visible(child)) {
638 sym->flags |= SYMBOL_DEF_USER;
646 const char *menu_get_prompt(struct menu *menu)
649 return menu->prompt->text;
651 return menu->sym->name;
655 struct menu *menu_get_parent_menu(struct menu *menu)
659 for (; menu != &rootmenu; menu = menu->parent) {
660 type = menu->prompt ? menu->prompt->type : 0;
667 static void get_def_str(struct gstr *r, struct menu *menu)
669 str_printf(r, "Defined at %s:%d\n",
670 menu->filename, menu->lineno);
673 static void get_dep_str(struct gstr *r, struct expr *expr, const char *prefix)
675 if (!expr_is_yes(expr)) {
676 str_append(r, prefix);
677 expr_gstr_print(expr, r);
682 int __attribute__((weak)) get_jump_key_char(void)
687 static void get_prompt_str(struct gstr *r, struct property *prop,
688 struct list_head *head)
691 struct menu *submenu[8], *menu, *location = NULL;
692 struct jump_key *jump = NULL;
694 str_printf(r, " Prompt: %s\n", prop->text);
696 get_dep_str(r, prop->menu->dep, " Depends on: ");
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
705 if (!expr_eq(prop->menu->dep, prop->visible.expr))
706 get_dep_str(r, prop->visible.expr, " Visible if: ");
709 for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent) {
711 if (location == NULL && menu_is_visible(menu))
714 if (head && location) {
715 jump = xmalloc(sizeof(struct jump_key));
716 jump->target = location;
717 list_add_tail(&jump->entries, head);
720 str_printf(r, " Location:\n");
721 for (j = 0; --i >= 0; j++) {
723 int indent = 2 * j + 4;
726 if (jump && menu == location) {
727 jump->offset = strlen(r->s);
728 jk = get_jump_key_char();
732 str_printf(r, "(%c)", jk);
736 str_printf(r, "%*c-> %s", indent, ' ', menu_get_prompt(menu));
738 str_printf(r, " (%s [=%s])", menu->sym->name ?
739 menu->sym->name : "<choice>",
740 sym_get_string_value(menu->sym));
746 static void get_symbol_props_str(struct gstr *r, struct symbol *sym,
747 enum prop_type tok, const char *prefix)
750 struct property *prop;
752 for_all_properties(sym, prop, tok) {
754 str_append(r, prefix);
757 str_printf(r, " && ");
758 expr_gstr_print(prop->expr, r);
765 * head is optional and may be NULL
767 static void get_symbol_str(struct gstr *r, struct symbol *sym,
768 struct list_head *head)
770 struct property *prop;
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);
780 str_printf(r, "Range : ");
781 expr_gstr_print(prop->expr, r);
787 /* Print the definitions with prompts before the ones without */
788 list_for_each_entry(menu, &sym->menus, link) {
790 get_def_str(r, menu);
791 get_prompt_str(r, menu->prompt, head);
795 list_for_each_entry(menu, &sym->menus, link) {
797 get_def_str(r, menu);
798 get_dep_str(r, menu->dep, " Depends on: ");
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");
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");
816 str_append(r, "\n\n");
819 struct gstr get_relations_str(struct symbol **sym_arr, struct list_head *head)
822 struct gstr res = str_new();
825 for (i = 0; sym_arr && (sym = sym_arr[i]); i++)
826 get_symbol_str(&res, sym, head);
828 str_append(&res, "No matches found.\n");
833 void menu_get_ext_help(struct menu *menu, struct gstr *help)
835 struct symbol *sym = menu->sym;
836 const char *help_text = nohelp_text;
840 str_printf(help, "%s%s:\n\n", CONFIG_, sym->name);
841 help_text = menu->help;
843 str_printf(help, "%s\n", help_text);
845 get_symbol_str(help, sym, NULL);