at commit 205cccd0
| 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 = ¤t_entry->list; |
| 90 | current_menu = current_entry; |
| 91 | return current_menu; |
| 92 | } |
| 93 | |
| 94 | void menu_end_menu(void) |
| 95 | { |
| 96 | last_entry_ptr = ¤t_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 = ¤t_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 | static int menu_validate_number(struct symbol *sym, struct symbol *sym2) |
| 238 | { |
| 239 | return sym2->type == S_INT || sym2->type == S_HEX || |
| 240 | (sym2->type == S_UNKNOWN && sym_string_valid(sym, sym2->name)); |
| 241 | } |
| 242 | |
| 243 | static void sym_check_prop(struct symbol *sym) |
| 244 | { |
| 245 | struct property *prop; |
| 246 | struct symbol *sym2; |
| 247 | char *use; |
| 248 | |
| 249 | for (prop = sym->prop; prop; prop = prop->next) { |
| 250 | switch (prop->type) { |
| 251 | case P_DEFAULT: |
| 252 | if ((sym->type == S_STRING || sym->type == S_INT || sym->type == S_HEX) && |
| 253 | prop->expr->type != E_SYMBOL) |
| 254 | prop_warn(prop, |
| 255 | "default for config symbol '%s'" |
| 256 | " must be a single symbol", sym->name); |
| 257 | if (prop->expr->type != E_SYMBOL) |
| 258 | break; |
| 259 | sym2 = prop_get_symbol(prop); |
| 260 | if (sym->type == S_HEX || sym->type == S_INT) { |
| 261 | if (!menu_validate_number(sym, sym2)) |
| 262 | prop_warn(prop, |
| 263 | "'%s': number is invalid", |
| 264 | sym->name); |
| 265 | } |
| 266 | if (sym_is_choice(sym)) { |
| 267 | struct menu *choice = sym_get_choice_menu(sym2); |
| 268 | |
| 269 | if (!choice || choice->sym != sym) |
| 270 | prop_warn(prop, |
| 271 | "choice default symbol '%s' is not contained in the choice", |
| 272 | sym2->name); |
| 273 | } |
| 274 | break; |
| 275 | case P_SELECT: |
| 276 | case P_IMPLY: |
| 277 | use = prop->type == P_SELECT ? "select" : "imply"; |
| 278 | sym2 = prop_get_symbol(prop); |
| 279 | if (sym->type != S_BOOLEAN && sym->type != S_TRISTATE) |
| 280 | prop_warn(prop, |
| 281 | "config symbol '%s' uses %s, but is " |
| 282 | "not bool or tristate", sym->name, use); |
| 283 | else if (sym2->type != S_UNKNOWN && |
| 284 | sym2->type != S_BOOLEAN && |
| 285 | sym2->type != S_TRISTATE) |
| 286 | prop_warn(prop, |
| 287 | "'%s' has wrong type. '%s' only " |
| 288 | "accept arguments of bool and " |
| 289 | "tristate type", sym2->name, use); |
| 290 | break; |
| 291 | case P_RANGE: |
| 292 | if (sym->type != S_INT && sym->type != S_HEX) |
| 293 | prop_warn(prop, "range is only allowed " |
| 294 | "for int or hex symbols"); |
| 295 | if (!menu_validate_number(sym, prop->expr->left.sym) || |
| 296 | !menu_validate_number(sym, prop->expr->right.sym)) |
| 297 | prop_warn(prop, "range is invalid"); |
| 298 | break; |
| 299 | default: |
| 300 | ; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | static void _menu_finalize(struct menu *parent, bool inside_choice) |
| 306 | { |
| 307 | struct menu *menu, *last_menu; |
| 308 | struct symbol *sym; |
| 309 | struct property *prop; |
| 310 | struct expr *basedep, *dep, *dep2; |
| 311 | |
| 312 | sym = parent->sym; |
| 313 | if (parent->list) { |
| 314 | /* |
| 315 | * This menu node has children. We (recursively) process them |
| 316 | * and propagate parent dependencies before moving on. |
| 317 | */ |
| 318 | |
| 319 | /* For each child menu node... */ |
| 320 | for (menu = parent->list; menu; menu = menu->next) { |
| 321 | /* |
| 322 | * Propagate parent dependencies to the child menu |
| 323 | * node, also rewriting and simplifying expressions |
| 324 | */ |
| 325 | basedep = rewrite_m(menu->dep); |
| 326 | basedep = expr_transform(basedep); |
| 327 | basedep = expr_alloc_and(parent->dep, basedep); |
| 328 | basedep = expr_eliminate_dups(basedep); |
| 329 | menu->dep = basedep; |
| 330 | |
| 331 | if (menu->sym) |
| 332 | /* |
| 333 | * Note: For symbols, all prompts are included |
| 334 | * too in the symbol's own property list |
| 335 | */ |
| 336 | prop = menu->sym->prop; |
| 337 | else |
| 338 | /* |
| 339 | * For non-symbol menu nodes, we just need to |
| 340 | * handle the prompt |
| 341 | */ |
| 342 | prop = menu->prompt; |
| 343 | |
| 344 | /* For each property... */ |
| 345 | for (; prop; prop = prop->next) { |
| 346 | if (prop->menu != menu) |
| 347 | /* |
| 348 | * Two possibilities: |
| 349 | * |
| 350 | * 1. The property lacks dependencies |
| 351 | * and so isn't location-specific, |
| 352 | * e.g. an 'option' |
| 353 | * |
| 354 | * 2. The property belongs to a symbol |
| 355 | * defined in multiple locations and |
| 356 | * is from some other location. It |
| 357 | * will be handled there in that |
| 358 | * case. |
| 359 | * |
| 360 | * Skip the property. |
| 361 | */ |
| 362 | continue; |
| 363 | |
| 364 | /* |
| 365 | * Propagate parent dependencies to the |
| 366 | * property's condition, rewriting and |
| 367 | * simplifying expressions at the same time |
| 368 | */ |
| 369 | dep = rewrite_m(prop->visible.expr); |
| 370 | dep = expr_transform(dep); |
| 371 | dep = expr_alloc_and(basedep, dep); |
| 372 | dep = expr_eliminate_dups(dep); |
| 373 | prop->visible.expr = dep; |
| 374 | |
| 375 | /* |
| 376 | * Handle selects and implies, which modify the |
| 377 | * dependencies of the selected/implied symbol |
| 378 | */ |
| 379 | if (prop->type == P_SELECT) { |
| 380 | struct symbol *es = prop_get_symbol(prop); |
| 381 | es->rev_dep.expr = expr_alloc_or(es->rev_dep.expr, |
| 382 | expr_alloc_and(expr_alloc_symbol(menu->sym), dep)); |
| 383 | } else if (prop->type == P_IMPLY) { |
| 384 | struct symbol *es = prop_get_symbol(prop); |
| 385 | es->implied.expr = expr_alloc_or(es->implied.expr, |
| 386 | expr_alloc_and(expr_alloc_symbol(menu->sym), dep)); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Recursively process children in the same fashion before |
| 393 | * moving on |
| 394 | */ |
| 395 | for (menu = parent->list; menu; menu = menu->next) |
| 396 | _menu_finalize(menu, sym && sym_is_choice(sym)); |
| 397 | } else if (!inside_choice && sym) { |
| 398 | /* |
| 399 | * Automatic submenu creation. If sym is a symbol and A, B, C, |
| 400 | * ... are consecutive items (symbols, menus, ifs, etc.) that |
| 401 | * all depend on sym, then the following menu structure is |
| 402 | * created: |
| 403 | * |
| 404 | * sym |
| 405 | * +-A |
| 406 | * +-B |
| 407 | * +-C |
| 408 | * ... |
| 409 | * |
| 410 | * This also works recursively, giving the following structure |
| 411 | * if A is a symbol and B depends on A: |
| 412 | * |
| 413 | * sym |
| 414 | * +-A |
| 415 | * | +-B |
| 416 | * +-C |
| 417 | * ... |
| 418 | */ |
| 419 | |
| 420 | basedep = parent->prompt ? parent->prompt->visible.expr : NULL; |
| 421 | basedep = expr_trans_compare(basedep, E_UNEQUAL, &symbol_no); |
| 422 | basedep = expr_eliminate_dups(expr_transform(basedep)); |
| 423 | |
| 424 | /* Examine consecutive elements after sym */ |
| 425 | last_menu = NULL; |
| 426 | for (menu = parent->next; menu; menu = menu->next) { |
| 427 | dep = menu->prompt ? menu->prompt->visible.expr : menu->dep; |
| 428 | if (!expr_contains_symbol(dep, sym)) |
| 429 | /* No dependency, quit */ |
| 430 | break; |
| 431 | /* |
| 432 | * Note that it's actually possible to depend on both |
| 433 | * 'SYM!=y' and 'SYM=y', so we need to first check if |
| 434 | * it's a positive dependency before checking if it's |
| 435 | * a negative dependency. See example: |
| 436 | * 'SFC && MTD && !(SFC=y && MTD=m)' |
| 437 | */ |
| 438 | if (expr_depends_symbol(dep, sym)) |
| 439 | /* Absolute dependency, put in submenu */ |
| 440 | goto next; |
| 441 | if (expr_contains_symbol_negated(dep, sym)) |
| 442 | /* Negative dependency, quit */ |
| 443 | break; |
| 444 | |
| 445 | /* |
| 446 | * Also consider it a dependency on sym if our |
| 447 | * dependencies contain sym and are a "superset" of |
| 448 | * sym's dependencies, e.g. '(sym || Q) && R' when sym |
| 449 | * depends on R. |
| 450 | * |
| 451 | * Note that 'R' might be from an enclosing menu or if, |
| 452 | * making this a more common case than it might seem. |
| 453 | */ |
| 454 | dep = expr_trans_compare(dep, E_UNEQUAL, &symbol_no); |
| 455 | dep = expr_eliminate_dups(expr_transform(dep)); |
| 456 | dep2 = basedep; |
| 457 | expr_eliminate_eq(&dep, &dep2); |
| 458 | if (!expr_is_yes(dep2)) { |
| 459 | /* Not superset, quit */ |
| 460 | break; |
| 461 | } |
| 462 | /* Superset, put in submenu */ |
| 463 | next: |
| 464 | _menu_finalize(menu, false); |
| 465 | menu->parent = parent; |
| 466 | last_menu = menu; |
| 467 | } |
| 468 | if (last_menu) { |
| 469 | parent->list = parent->next; |
| 470 | parent->next = last_menu->next; |
| 471 | last_menu->next = NULL; |
| 472 | } |
| 473 | |
| 474 | sym->dir_dep.expr = expr_alloc_or(sym->dir_dep.expr, parent->dep); |
| 475 | } |
| 476 | for (menu = parent->list; menu; menu = menu->next) { |
| 477 | /* |
| 478 | * This code serves two purposes: |
| 479 | * |
| 480 | * (1) Flattening 'if' blocks, which do not specify a submenu |
| 481 | * and only add dependencies. |
| 482 | * |
| 483 | * (Automatic submenu creation might still create a submenu |
| 484 | * from an 'if' before this code runs.) |
| 485 | * |
| 486 | * (2) "Undoing" any automatic submenus created earlier below |
| 487 | * promptless symbols. |
| 488 | * |
| 489 | * Before: |
| 490 | * |
| 491 | * A |
| 492 | * if ... (or promptless symbol) |
| 493 | * +-B |
| 494 | * +-C |
| 495 | * D |
| 496 | * |
| 497 | * After: |
| 498 | * |
| 499 | * A |
| 500 | * if ... (or promptless symbol) |
| 501 | * B |
| 502 | * C |
| 503 | * D |
| 504 | */ |
| 505 | if (menu->list && (!menu->prompt || !menu->prompt->text)) { |
| 506 | for (last_menu = menu->list; ; last_menu = last_menu->next) { |
| 507 | last_menu->parent = parent; |
| 508 | if (!last_menu->next) |
| 509 | break; |
| 510 | } |
| 511 | last_menu->next = menu->next; |
| 512 | menu->next = menu->list; |
| 513 | menu->list = NULL; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if (sym && !(sym->flags & SYMBOL_WARNED)) { |
| 518 | if (sym->type == S_UNKNOWN) |
| 519 | menu_warn(parent, "config symbol defined without type"); |
| 520 | |
| 521 | /* Check properties connected to this symbol */ |
| 522 | sym_check_prop(sym); |
| 523 | sym->flags |= SYMBOL_WARNED; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | void menu_finalize(void) |
| 528 | { |
| 529 | _menu_finalize(&rootmenu, false); |
| 530 | } |
| 531 | |
| 532 | bool menu_has_prompt(const struct menu *menu) |
| 533 | { |
| 534 | if (!menu->prompt) |
| 535 | return false; |
| 536 | return true; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Determine if a menu is empty. |
| 541 | * A menu is considered empty if it contains no or only |
| 542 | * invisible entries. |
| 543 | */ |
| 544 | bool menu_is_empty(struct menu *menu) |
| 545 | { |
| 546 | struct menu *child; |
| 547 | |
| 548 | for (child = menu->list; child; child = child->next) { |
| 549 | if (menu_is_visible(child)) |
| 550 | return(false); |
| 551 | } |
| 552 | return(true); |
| 553 | } |
| 554 | |
| 555 | bool menu_is_visible(struct menu *menu) |
| 556 | { |
| 557 | struct menu *child; |
| 558 | struct symbol *sym; |
| 559 | tristate visible; |
| 560 | |
| 561 | if (!menu->prompt) |
| 562 | return false; |
| 563 | |
| 564 | if (menu->visibility) { |
| 565 | if (expr_calc_value(menu->visibility) == no) |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | sym = menu->sym; |
| 570 | if (sym) { |
| 571 | sym_calc_value(sym); |
| 572 | visible = menu->prompt->visible.tri; |
| 573 | } else |
| 574 | visible = menu->prompt->visible.tri = expr_calc_value(menu->prompt->visible.expr); |
| 575 | |
| 576 | if (visible != no) |
| 577 | return true; |
| 578 | |
| 579 | if (!sym || sym_get_tristate_value(menu->sym) == no) |
| 580 | return false; |
| 581 | |
| 582 | for (child = menu->list; child; child = child->next) |
| 583 | if (menu_is_visible(child)) |
| 584 | return true; |
| 585 | |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | const char *menu_get_prompt(const struct menu *menu) |
| 590 | { |
| 591 | if (menu->prompt) |
| 592 | return menu->prompt->text; |
| 593 | else if (menu->sym) |
| 594 | return menu->sym->name; |
| 595 | return NULL; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * menu_get_parent_menu - return the parent menu or NULL |
| 600 | * @menu: pointer to the menu |
| 601 | * return: the parent menu, or NULL if there is no parent. |
| 602 | */ |
| 603 | struct menu *menu_get_parent_menu(struct menu *menu) |
| 604 | { |
| 605 | for (menu = menu->parent; menu; menu = menu->parent) |
| 606 | if (menu->type == M_MENU) |
| 607 | return menu; |
| 608 | |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * menu_get_menu_or_parent_menu - return the parent menu or the menu itself |
| 614 | * @menu: pointer to the menu |
| 615 | * return: the parent menu. If the given argument is already a menu, return |
| 616 | * itself. |
| 617 | */ |
| 618 | struct menu *menu_get_menu_or_parent_menu(struct menu *menu) |
| 619 | { |
| 620 | enum prop_type type; |
| 621 | |
| 622 | for (; menu != &rootmenu; menu = menu->parent) { |
| 623 | type = menu->prompt ? menu->prompt->type : 0; |
| 624 | if (type == P_MENU) |
| 625 | break; |
| 626 | } |
| 627 | return menu; |
| 628 | } |
| 629 | |
| 630 | static void get_def_str(struct gstr *r, const struct menu *menu) |
| 631 | { |
| 632 | str_printf(r, "Defined at %s:%d\n", |
| 633 | menu->filename, menu->lineno); |
| 634 | } |
| 635 | |
| 636 | static void get_dep_str(struct gstr *r, const struct expr *expr, |
| 637 | const char *prefix) |
| 638 | { |
| 639 | if (!expr_is_yes(expr)) { |
| 640 | str_append(r, prefix); |
| 641 | expr_gstr_print(expr, r); |
| 642 | str_append(r, "\n"); |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | int __attribute__((weak)) get_jump_key_char(void) |
| 647 | { |
| 648 | return -1; |
| 649 | } |
| 650 | |
| 651 | static void get_prompt_str(struct gstr *r, struct property *prop, |
| 652 | struct list_head *head) |
| 653 | { |
| 654 | int i, j; |
| 655 | struct menu *submenu[8], *menu, *location = NULL; |
| 656 | struct jump_key *jump = NULL; |
| 657 | |
| 658 | str_printf(r, " Prompt: %s\n", prop->text); |
| 659 | |
| 660 | get_dep_str(r, prop->menu->dep, " Depends on: "); |
| 661 | /* |
| 662 | * Most prompts in Linux have visibility that exactly matches their |
| 663 | * dependencies. For these, we print only the dependencies to improve |
| 664 | * readability. However, prompts with inline "if" expressions and |
| 665 | * prompts with a parent that has a "visible if" expression have |
| 666 | * differing dependencies and visibility. In these rare cases, we |
| 667 | * print both. |
| 668 | */ |
| 669 | if (!expr_eq(prop->menu->dep, prop->visible.expr)) |
| 670 | get_dep_str(r, prop->visible.expr, " Visible if: "); |
| 671 | |
| 672 | menu = prop->menu; |
| 673 | for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent) { |
| 674 | submenu[i++] = menu; |
| 675 | if (location == NULL && menu_is_visible(menu)) |
| 676 | location = menu; |
| 677 | } |
| 678 | if (head && location) { |
| 679 | jump = xmalloc(sizeof(struct jump_key)); |
| 680 | jump->target = location; |
| 681 | list_add_tail(&jump->entries, head); |
| 682 | } |
| 683 | |
| 684 | str_printf(r, " Location:\n"); |
| 685 | for (j = 0; --i >= 0; j++) { |
| 686 | int jk = -1; |
| 687 | int indent = 2 * j + 4; |
| 688 | |
| 689 | menu = submenu[i]; |
| 690 | if (jump && menu == location) { |
| 691 | jump->offset = strlen(r->s); |
| 692 | jk = get_jump_key_char(); |
| 693 | } |
| 694 | |
| 695 | if (jk >= 0) { |
| 696 | str_printf(r, "(%c)", jk); |
| 697 | indent -= 3; |
| 698 | } |
| 699 | |
| 700 | str_printf(r, "%*c-> %s", indent, ' ', menu_get_prompt(menu)); |
| 701 | if (menu->sym) { |
| 702 | str_printf(r, " (%s [=%s])", menu->sym->name ? |
| 703 | menu->sym->name : "<choice>", |
| 704 | sym_get_string_value(menu->sym)); |
| 705 | } |
| 706 | str_append(r, "\n"); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | static void get_symbol_props_str(struct gstr *r, struct symbol *sym, |
| 711 | enum prop_type tok, const char *prefix) |
| 712 | { |
| 713 | bool hit = false; |
| 714 | struct property *prop; |
| 715 | |
| 716 | for_all_properties(sym, prop, tok) { |
| 717 | if (!hit) { |
| 718 | str_append(r, prefix); |
| 719 | hit = true; |
| 720 | } else |
| 721 | str_printf(r, " && "); |
| 722 | expr_gstr_print(prop->expr, r); |
| 723 | } |
| 724 | if (hit) |
| 725 | str_append(r, "\n"); |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | * head is optional and may be NULL |
| 730 | */ |
| 731 | static void get_symbol_str(struct gstr *r, struct symbol *sym, |
| 732 | struct list_head *head) |
| 733 | { |
| 734 | struct property *prop; |
| 735 | struct menu *menu; |
| 736 | |
| 737 | if (sym && sym->name) { |
| 738 | str_printf(r, "Symbol: %s [=%s]\n", sym->name, |
| 739 | sym_get_string_value(sym)); |
| 740 | str_printf(r, "Type : %s\n", sym_type_name(sym->type)); |
| 741 | if (sym->type == S_INT || sym->type == S_HEX) { |
| 742 | prop = sym_get_range_prop(sym); |
| 743 | if (prop) { |
| 744 | str_printf(r, "Range : "); |
| 745 | expr_gstr_print(prop->expr, r); |
| 746 | str_append(r, "\n"); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | /* Print the definitions with prompts before the ones without */ |
| 752 | list_for_each_entry(menu, &sym->menus, link) { |
| 753 | if (menu->prompt) { |
| 754 | get_def_str(r, menu); |
| 755 | get_prompt_str(r, menu->prompt, head); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | list_for_each_entry(menu, &sym->menus, link) { |
| 760 | if (!menu->prompt) { |
| 761 | get_def_str(r, menu); |
| 762 | get_dep_str(r, menu->dep, " Depends on: "); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | get_symbol_props_str(r, sym, P_SELECT, "Selects: "); |
| 767 | if (sym->rev_dep.expr) { |
| 768 | expr_gstr_print_revdep(sym->rev_dep.expr, r, yes, "Selected by [y]:\n"); |
| 769 | expr_gstr_print_revdep(sym->rev_dep.expr, r, mod, "Selected by [m]:\n"); |
| 770 | expr_gstr_print_revdep(sym->rev_dep.expr, r, no, "Selected by [n]:\n"); |
| 771 | } |
| 772 | |
| 773 | get_symbol_props_str(r, sym, P_IMPLY, "Implies: "); |
| 774 | if (sym->implied.expr) { |
| 775 | expr_gstr_print_revdep(sym->implied.expr, r, yes, "Implied by [y]:\n"); |
| 776 | expr_gstr_print_revdep(sym->implied.expr, r, mod, "Implied by [m]:\n"); |
| 777 | expr_gstr_print_revdep(sym->implied.expr, r, no, "Implied by [n]:\n"); |
| 778 | } |
| 779 | |
| 780 | str_append(r, "\n\n"); |
| 781 | } |
| 782 | |
| 783 | struct gstr get_relations_str(struct symbol **sym_arr, struct list_head *head) |
| 784 | { |
| 785 | struct symbol *sym; |
| 786 | struct gstr res = str_new(); |
| 787 | int i; |
| 788 | |
| 789 | for (i = 0; sym_arr && (sym = sym_arr[i]); i++) |
| 790 | get_symbol_str(&res, sym, head); |
| 791 | if (!i) |
| 792 | str_append(&res, "No matches found.\n"); |
| 793 | return res; |
| 794 | } |
| 795 | |
| 796 | |
| 797 | void menu_get_ext_help(struct menu *menu, struct gstr *help) |
| 798 | { |
| 799 | struct symbol *sym = menu->sym; |
| 800 | const char *help_text = nohelp_text; |
| 801 | |
| 802 | if (menu->help) { |
| 803 | if (sym->name) |
| 804 | str_printf(help, "%s%s:\n\n", CONFIG_, sym->name); |
| 805 | help_text = menu->help; |
| 806 | } |
| 807 | str_printf(help, "%s\n", help_text); |
| 808 | if (sym) |
| 809 | get_symbol_str(help, sym, NULL); |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * menu_dump - dump all menu entries in a tree-like format |
| 814 | */ |
| 815 | void menu_dump(void) |
| 816 | { |
| 817 | struct menu *menu = &rootmenu; |
| 818 | unsigned long long bits = 0; |
| 819 | int indent = 0; |
| 820 | |
| 821 | while (menu) { |
| 822 | |
| 823 | for (int i = indent - 1; i >= 0; i--) { |
| 824 | if (bits & (1ULL << i)) { |
| 825 | if (i > 0) |
| 826 | printf("| "); |
| 827 | else |
| 828 | printf("|-- "); |
| 829 | } else { |
| 830 | if (i > 0) |
| 831 | printf(" "); |
| 832 | else |
| 833 | printf("`-- "); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | switch (menu->type) { |
| 838 | case M_CHOICE: |
| 839 | printf("choice \"%s\"\n", menu->prompt->text); |
| 840 | break; |
| 841 | case M_COMMENT: |
| 842 | printf("comment \"%s\"\n", menu->prompt->text); |
| 843 | break; |
| 844 | case M_IF: |
| 845 | printf("if\n"); |
| 846 | break; |
| 847 | case M_MENU: |
| 848 | printf("menu \"%s\"", menu->prompt->text); |
| 849 | if (!menu->sym) { |
| 850 | printf("\n"); |
| 851 | break; |
| 852 | } |
| 853 | printf(" + "); |
| 854 | /* fallthrough */ |
| 855 | case M_NORMAL: |
| 856 | printf("symbol %s\n", menu->sym->name); |
| 857 | break; |
| 858 | } |
| 859 | if (menu->list) { |
| 860 | bits <<= 1; |
| 861 | menu = menu->list; |
| 862 | if (menu->next) |
| 863 | bits |= 1; |
| 864 | else |
| 865 | bits &= ~1; |
| 866 | indent++; |
| 867 | continue; |
| 868 | } |
| 869 | |
| 870 | while (menu && !menu->next) { |
| 871 | menu = menu->parent; |
| 872 | bits >>= 1; |
| 873 | indent--; |
| 874 | } |
| 875 | |
| 876 | if (menu) { |
| 877 | menu = menu->next; |
| 878 | if (menu->next) |
| 879 | bits |= 1; |
| 880 | else |
| 881 | bits &= ~1; |
| 882 | } |
| 883 | } |
| 884 | } |