xref: /openbmc/linux/scripts/kconfig/mconf.c (revision bad9955d)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
31da177e4SLinus Torvalds  * Released under the terms of the GNU GPL v2.0.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Introduced single menu mode (show all sub-menus in one large tree).
61da177e4SLinus Torvalds  * 2002-11-06 Petr Baudis <pasky@ucw.cz>
73b9fa093SArnaldo Carvalho de Melo  *
83b9fa093SArnaldo Carvalho de Melo  * i18n, 2005, Arnaldo Carvalho de Melo <acme@conectiva.com.br>
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <ctype.h>
121da177e4SLinus Torvalds #include <errno.h>
131da177e4SLinus Torvalds #include <fcntl.h>
141da177e4SLinus Torvalds #include <limits.h>
151da177e4SLinus Torvalds #include <stdarg.h>
161da177e4SLinus Torvalds #include <stdlib.h>
171da177e4SLinus Torvalds #include <string.h>
18564899f9SDavidlohr Bueso #include <signal.h>
191da177e4SLinus Torvalds #include <unistd.h>
20442ff702SJean-Christophe Dubois #include <locale.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include "lkc.h"
232982de69SSam Ravnborg #include "lxdialog/dialog.h"
241da177e4SLinus Torvalds 
253b9fa093SArnaldo Carvalho de Melo static const char mconf_readme[] = N_(
261da177e4SLinus Torvalds "Overview\n"
271da177e4SLinus Torvalds "--------\n"
28652cf982SArnaud Lacombe "This interface let you select features and parameters for the build.\n"
29652cf982SArnaud Lacombe "Features can either be built-in, modularized, or ignored. Parameters\n"
30652cf982SArnaud Lacombe "must be entered in as decimal or hexadecimal numbers or text.\n"
311da177e4SLinus Torvalds "\n"
32b5d609dbSMatej Laitl "Menu items beginning with following braces represent features that\n"
33b5d609dbSMatej Laitl "  [ ] can be built in or removed\n"
34b5d609dbSMatej Laitl "  < > can be built in, modularized or removed\n"
35b5d609dbSMatej Laitl "  { } can be built in or modularized (selected by other feature)\n"
36b5d609dbSMatej Laitl "  - - are selected by other feature,\n"
37b5d609dbSMatej Laitl "while *, M or whitespace inside braces means to build in, build as\n"
38b5d609dbSMatej Laitl "a module or to exclude the feature respectively.\n"
391da177e4SLinus Torvalds "\n"
401da177e4SLinus Torvalds "To change any of these features, highlight it with the cursor\n"
411da177e4SLinus Torvalds "keys and press <Y> to build it in, <M> to make it a module or\n"
421da177e4SLinus Torvalds "<N> to removed it.  You may also press the <Space Bar> to cycle\n"
431da177e4SLinus Torvalds "through the available options (ie. Y->N->M->Y).\n"
441da177e4SLinus Torvalds "\n"
451da177e4SLinus Torvalds "Some additional keyboard hints:\n"
461da177e4SLinus Torvalds "\n"
471da177e4SLinus Torvalds "Menus\n"
481da177e4SLinus Torvalds "----------\n"
491da177e4SLinus Torvalds "o  Use the Up/Down arrow keys (cursor keys) to highlight the item\n"
501da177e4SLinus Torvalds "   you wish to change or submenu wish to select and press <Enter>.\n"
511da177e4SLinus Torvalds "   Submenus are designated by \"--->\".\n"
521da177e4SLinus Torvalds "\n"
531da177e4SLinus Torvalds "   Shortcut: Press the option's highlighted letter (hotkey).\n"
541da177e4SLinus Torvalds "             Pressing a hotkey more than once will sequence\n"
551da177e4SLinus Torvalds "             through all visible items which use that hotkey.\n"
561da177e4SLinus Torvalds "\n"
571da177e4SLinus Torvalds "   You may also use the <PAGE UP> and <PAGE DOWN> keys to scroll\n"
581da177e4SLinus Torvalds "   unseen options into view.\n"
591da177e4SLinus Torvalds "\n"
601da177e4SLinus Torvalds "o  To exit a menu use the cursor keys to highlight the <Exit> button\n"
611da177e4SLinus Torvalds "   and press <ENTER>.\n"
621da177e4SLinus Torvalds "\n"
631da177e4SLinus Torvalds "   Shortcut: Press <ESC><ESC> or <E> or <X> if there is no hotkey\n"
641da177e4SLinus Torvalds "             using those letters.  You may press a single <ESC>, but\n"
651da177e4SLinus Torvalds "             there is a delayed response which you may find annoying.\n"
661da177e4SLinus Torvalds "\n"
671da177e4SLinus Torvalds "   Also, the <TAB> and cursor keys will cycle between <Select>,\n"
6822c7eca6SLi Zefan "   <Exit> and <Help>.\n"
691da177e4SLinus Torvalds "\n"
701da177e4SLinus Torvalds "o  To get help with an item, use the cursor keys to highlight <Help>\n"
7122c7eca6SLi Zefan "   and press <ENTER>.\n"
721da177e4SLinus Torvalds "\n"
731da177e4SLinus Torvalds "   Shortcut: Press <H> or <?>.\n"
741da177e4SLinus Torvalds "\n"
75f9447c49SLi Zefan "o  To toggle the display of hidden options, press <Z>.\n"
7622c7eca6SLi Zefan "\n"
771da177e4SLinus Torvalds "\n"
781da177e4SLinus Torvalds "Radiolists  (Choice lists)\n"
791da177e4SLinus Torvalds "-----------\n"
801da177e4SLinus Torvalds "o  Use the cursor keys to select the option you wish to set and press\n"
811da177e4SLinus Torvalds "   <S> or the <SPACE BAR>.\n"
821da177e4SLinus Torvalds "\n"
831da177e4SLinus Torvalds "   Shortcut: Press the first letter of the option you wish to set then\n"
841da177e4SLinus Torvalds "             press <S> or <SPACE BAR>.\n"
851da177e4SLinus Torvalds "\n"
861da177e4SLinus Torvalds "o  To see available help for the item, use the cursor keys to highlight\n"
871da177e4SLinus Torvalds "   <Help> and Press <ENTER>.\n"
881da177e4SLinus Torvalds "\n"
891da177e4SLinus Torvalds "   Shortcut: Press <H> or <?>.\n"
901da177e4SLinus Torvalds "\n"
911da177e4SLinus Torvalds "   Also, the <TAB> and cursor keys will cycle between <Select> and\n"
921da177e4SLinus Torvalds "   <Help>\n"
931da177e4SLinus Torvalds "\n"
941da177e4SLinus Torvalds "\n"
951da177e4SLinus Torvalds "Data Entry\n"
961da177e4SLinus Torvalds "-----------\n"
971da177e4SLinus Torvalds "o  Enter the requested information and press <ENTER>\n"
981da177e4SLinus Torvalds "   If you are entering hexadecimal values, it is not necessary to\n"
991da177e4SLinus Torvalds "   add the '0x' prefix to the entry.\n"
1001da177e4SLinus Torvalds "\n"
1011da177e4SLinus Torvalds "o  For help, use the <TAB> or cursor keys to highlight the help option\n"
1021da177e4SLinus Torvalds "   and press <ENTER>.  You can try <TAB><H> as well.\n"
1031da177e4SLinus Torvalds "\n"
1041da177e4SLinus Torvalds "\n"
1051da177e4SLinus Torvalds "Text Box    (Help Window)\n"
1061da177e4SLinus Torvalds "--------\n"
1071da177e4SLinus Torvalds "o  Use the cursor keys to scroll up/down/left/right.  The VI editor\n"
1089d4792c9SBenjamin Poirier "   keys h,j,k,l function here as do <u>, <d>, <SPACE BAR> and <B> for \n"
1099d4792c9SBenjamin Poirier "   those who are familiar with less and lynx.\n"
1101da177e4SLinus Torvalds "\n"
1119d4792c9SBenjamin Poirier "o  Press <E>, <X>, <q>, <Enter> or <Esc><Esc> to exit.\n"
1121da177e4SLinus Torvalds "\n"
1131da177e4SLinus Torvalds "\n"
1141da177e4SLinus Torvalds "Alternate Configuration Files\n"
1151da177e4SLinus Torvalds "-----------------------------\n"
1161da177e4SLinus Torvalds "Menuconfig supports the use of alternate configuration files for\n"
1171da177e4SLinus Torvalds "those who, for various reasons, find it necessary to switch\n"
118652cf982SArnaud Lacombe "between different configurations.\n"
1191da177e4SLinus Torvalds "\n"
1201da177e4SLinus Torvalds "At the end of the main menu you will find two options.  One is\n"
1211da177e4SLinus Torvalds "for saving the current configuration to a file of your choosing.\n"
1221da177e4SLinus Torvalds "The other option is for loading a previously saved alternate\n"
1231da177e4SLinus Torvalds "configuration.\n"
1241da177e4SLinus Torvalds "\n"
1251da177e4SLinus Torvalds "Even if you don't use alternate configuration files, but you\n"
1261da177e4SLinus Torvalds "find during a Menuconfig session that you have completely messed\n"
1271da177e4SLinus Torvalds "up your settings, you may use the \"Load Alternate...\" option to\n"
1281da177e4SLinus Torvalds "restore your previously saved settings from \".config\" without\n"
1291da177e4SLinus Torvalds "restarting Menuconfig.\n"
1301da177e4SLinus Torvalds "\n"
1311da177e4SLinus Torvalds "Other information\n"
1321da177e4SLinus Torvalds "-----------------\n"
1331da177e4SLinus Torvalds "If you use Menuconfig in an XTERM window make sure you have your\n"
1341da177e4SLinus Torvalds "$TERM variable set to point to a xterm definition which supports color.\n"
1351da177e4SLinus Torvalds "Otherwise, Menuconfig will look rather bad.  Menuconfig will not\n"
1361da177e4SLinus Torvalds "display correctly in a RXVT window because rxvt displays only one\n"
1371da177e4SLinus Torvalds "intensity of color, bright.\n"
1381da177e4SLinus Torvalds "\n"
1391da177e4SLinus Torvalds "Menuconfig will display larger menus on screens or xterms which are\n"
1401da177e4SLinus Torvalds "set to display more than the standard 25 row by 80 column geometry.\n"
1411da177e4SLinus Torvalds "In order for this to work, the \"stty size\" command must be able to\n"
1421da177e4SLinus Torvalds "display the screen's current row and column geometry.  I STRONGLY\n"
1431da177e4SLinus Torvalds "RECOMMEND that you make sure you do NOT have the shell variables\n"
1441da177e4SLinus Torvalds "LINES and COLUMNS exported into your environment.  Some distributions\n"
1451da177e4SLinus Torvalds "export those variables via /etc/profile.  Some ncurses programs can\n"
1461da177e4SLinus Torvalds "become confused when those variables (LINES & COLUMNS) don't reflect\n"
1471da177e4SLinus Torvalds "the true screen size.\n"
1481da177e4SLinus Torvalds "\n"
1491da177e4SLinus Torvalds "Optional personality available\n"
1501da177e4SLinus Torvalds "------------------------------\n"
151652cf982SArnaud Lacombe "If you prefer to have all of the options listed in a single menu, rather\n"
152652cf982SArnaud Lacombe "than the default multimenu hierarchy, run the menuconfig with\n"
153652cf982SArnaud Lacombe "MENUCONFIG_MODE environment variable set to single_menu. Example:\n"
1541da177e4SLinus Torvalds "\n"
1551da177e4SLinus Torvalds "make MENUCONFIG_MODE=single_menu menuconfig\n"
1561da177e4SLinus Torvalds "\n"
1571da177e4SLinus Torvalds "<Enter> will then unroll the appropriate category, or enfold it if it\n"
1581da177e4SLinus Torvalds "is already unrolled.\n"
1591da177e4SLinus Torvalds "\n"
1601da177e4SLinus Torvalds "Note that this mode can eventually be a little more CPU expensive\n"
1611da177e4SLinus Torvalds "(especially with a larger number of unrolled categories) than the\n"
16245897213SSam Ravnborg "default mode.\n"
16345897213SSam Ravnborg "\n"
16445897213SSam Ravnborg "Different color themes available\n"
16545897213SSam Ravnborg "--------------------------------\n"
16645897213SSam Ravnborg "It is possible to select different color themes using the variable\n"
16745897213SSam Ravnborg "MENUCONFIG_COLOR. To select a theme use:\n"
16845897213SSam Ravnborg "\n"
16945897213SSam Ravnborg "make MENUCONFIG_COLOR=<theme> menuconfig\n"
17045897213SSam Ravnborg "\n"
17145897213SSam Ravnborg "Available themes are\n"
17245897213SSam Ravnborg " mono       => selects colors suitable for monochrome displays\n"
17345897213SSam Ravnborg " blackbg    => selects a color scheme with black background\n"
174350b5b76SSam Ravnborg " classic    => theme with blue background. The classic look\n"
175350b5b76SSam Ravnborg " bluetitle  => a LCD friendly version of classic. (default)\n"
17645897213SSam Ravnborg "\n"),
1773b9fa093SArnaldo Carvalho de Melo menu_instructions[] = N_(
1781da177e4SLinus Torvalds 	"Arrow keys navigate the menu.  "
1791da177e4SLinus Torvalds 	"<Enter> selects submenus --->.  "
1801da177e4SLinus Torvalds 	"Highlighted letters are hotkeys.  "
1811da177e4SLinus Torvalds 	"Pressing <Y> includes, <N> excludes, <M> modularizes features.  "
1821da177e4SLinus Torvalds 	"Press <Esc><Esc> to exit, <?> for Help, </> for Search.  "
1833b9fa093SArnaldo Carvalho de Melo 	"Legend: [*] built-in  [ ] excluded  <M> module  < > module capable"),
1843b9fa093SArnaldo Carvalho de Melo radiolist_instructions[] = N_(
1851da177e4SLinus Torvalds 	"Use the arrow keys to navigate this window or "
1861da177e4SLinus Torvalds 	"press the hotkey of the item you wish to select "
1871da177e4SLinus Torvalds 	"followed by the <SPACE BAR>. "
1883b9fa093SArnaldo Carvalho de Melo 	"Press <?> for additional information about this option."),
1893b9fa093SArnaldo Carvalho de Melo inputbox_instructions_int[] = N_(
1901da177e4SLinus Torvalds 	"Please enter a decimal value. "
1911da177e4SLinus Torvalds 	"Fractions will not be accepted.  "
1923b9fa093SArnaldo Carvalho de Melo 	"Use the <TAB> key to move from the input field to the buttons below it."),
1933b9fa093SArnaldo Carvalho de Melo inputbox_instructions_hex[] = N_(
1941da177e4SLinus Torvalds 	"Please enter a hexadecimal value. "
1953b9fa093SArnaldo Carvalho de Melo 	"Use the <TAB> key to move from the input field to the buttons below it."),
1963b9fa093SArnaldo Carvalho de Melo inputbox_instructions_string[] = N_(
1971da177e4SLinus Torvalds 	"Please enter a string value. "
1983b9fa093SArnaldo Carvalho de Melo 	"Use the <TAB> key to move from the input field to the buttons below it."),
1993b9fa093SArnaldo Carvalho de Melo setmod_text[] = N_(
2001da177e4SLinus Torvalds 	"This feature depends on another which has been configured as a module.\n"
2013b9fa093SArnaldo Carvalho de Melo 	"As a result, this feature will be built as a module."),
2023b9fa093SArnaldo Carvalho de Melo load_config_text[] = N_(
2031da177e4SLinus Torvalds 	"Enter the name of the configuration file you wish to load.  "
2041da177e4SLinus Torvalds 	"Accept the name shown to restore the configuration you "
2053b9fa093SArnaldo Carvalho de Melo 	"last retrieved.  Leave blank to abort."),
2063b9fa093SArnaldo Carvalho de Melo load_config_help[] = N_(
2071da177e4SLinus Torvalds 	"\n"
208652cf982SArnaud Lacombe 	"For various reasons, one may wish to keep several different\n"
2091da177e4SLinus Torvalds 	"configurations available on a single machine.\n"
2101da177e4SLinus Torvalds 	"\n"
2111da177e4SLinus Torvalds 	"If you have saved a previous configuration in a file other than the\n"
212652cf982SArnaud Lacombe 	"default one, entering its name here will allow you to modify that\n"
213652cf982SArnaud Lacombe 	"configuration.\n"
2141da177e4SLinus Torvalds 	"\n"
2151da177e4SLinus Torvalds 	"If you are uncertain, then you have probably never used alternate\n"
216af901ca1SAndré Goddard Rosa 	"configuration files. You should therefore leave this blank to abort.\n"),
2173b9fa093SArnaldo Carvalho de Melo save_config_text[] = N_(
2181da177e4SLinus Torvalds 	"Enter a filename to which this configuration should be saved "
2193b9fa093SArnaldo Carvalho de Melo 	"as an alternate.  Leave blank to abort."),
2203b9fa093SArnaldo Carvalho de Melo save_config_help[] = N_(
2211da177e4SLinus Torvalds 	"\n"
222652cf982SArnaud Lacombe 	"For various reasons, one may wish to keep different configurations\n"
223652cf982SArnaud Lacombe 	"available on a single machine.\n"
2241da177e4SLinus Torvalds 	"\n"
2251da177e4SLinus Torvalds 	"Entering a file name here will allow you to later retrieve, modify\n"
2261da177e4SLinus Torvalds 	"and use the current configuration as an alternate to whatever\n"
2271da177e4SLinus Torvalds 	"configuration options you have selected at that time.\n"
2281da177e4SLinus Torvalds 	"\n"
2291da177e4SLinus Torvalds 	"If you are uncertain what all this means then you should probably\n"
2303b9fa093SArnaldo Carvalho de Melo 	"leave this blank.\n"),
2313b9fa093SArnaldo Carvalho de Melo search_help[] = N_(
2321da177e4SLinus Torvalds 	"\n"
23359dfa24dSArnaud Lacombe 	"Search for symbols and display their relations.\n"
234503af334SRandy Dunlap 	"Regular expressions are allowed.\n"
2351da177e4SLinus Torvalds 	"Example: search for \"^FOO\"\n"
2361da177e4SLinus Torvalds 	"Result:\n"
2371da177e4SLinus Torvalds 	"-----------------------------------------------------------------\n"
2381da177e4SLinus Torvalds 	"Symbol: FOO [=m]\n"
2395e609addSBenjamin Poirier 	"Type  : tristate\n"
2401da177e4SLinus Torvalds 	"Prompt: Foo bus is used to drive the bar HW\n"
2411da177e4SLinus Torvalds 	"  Defined at drivers/pci/Kconfig:47\n"
2421da177e4SLinus Torvalds 	"  Depends on: X86_LOCAL_APIC && X86_IO_APIC || IA64\n"
2431da177e4SLinus Torvalds 	"  Location:\n"
244bb8187d3SPaul Gortmaker 	"    -> Bus options (PCI, PCMCIA, EISA, ISA)\n"
2451da177e4SLinus Torvalds 	"      -> PCI support (PCI [=y])\n"
2465e609addSBenjamin Poirier 	"(1)     -> PCI access mode (<choice> [=y])\n"
2471da177e4SLinus Torvalds 	"  Selects: LIBCRC32\n"
2481da177e4SLinus Torvalds 	"  Selected by: BAR\n"
2491da177e4SLinus Torvalds 	"-----------------------------------------------------------------\n"
2505e609addSBenjamin Poirier 	"o The line 'Type:' shows the type of the configuration option for\n"
2515e609addSBenjamin Poirier 	"  this symbol (boolean, tristate, string, ...)\n"
2521da177e4SLinus Torvalds 	"o The line 'Prompt:' shows the text used in the menu structure for\n"
25359dfa24dSArnaud Lacombe 	"  this symbol\n"
2541da177e4SLinus Torvalds 	"o The 'Defined at' line tell at what file / line number the symbol\n"
2551da177e4SLinus Torvalds 	"  is defined\n"
2561da177e4SLinus Torvalds 	"o The 'Depends on:' line tell what symbols needs to be defined for\n"
2571da177e4SLinus Torvalds 	"  this symbol to be visible in the menu (selectable)\n"
2581da177e4SLinus Torvalds 	"o The 'Location:' lines tell where in the menu structure this symbol\n"
2591da177e4SLinus Torvalds 	"  is located\n"
2605e609addSBenjamin Poirier 	"    A location followed by a [=y] indicates that this is a\n"
2615e609addSBenjamin Poirier 	"    selectable menu item - and the current value is displayed inside\n"
2625e609addSBenjamin Poirier 	"    brackets.\n"
2635e609addSBenjamin Poirier 	"    Press the key in the (#) prefix to jump directly to that\n"
2645e609addSBenjamin Poirier 	"    location. You will be returned to the current search results\n"
2655e609addSBenjamin Poirier 	"    after exiting this new menu.\n"
2661da177e4SLinus Torvalds 	"o The 'Selects:' line tell what symbol will be automatically\n"
2671da177e4SLinus Torvalds 	"  selected if this symbol is selected (y or m)\n"
2681da177e4SLinus Torvalds 	"o The 'Selected by' line tell what symbol has selected this symbol\n"
2691da177e4SLinus Torvalds 	"\n"
2701da177e4SLinus Torvalds 	"Only relevant lines are shown.\n"
2711da177e4SLinus Torvalds 	"\n\n"
2721da177e4SLinus Torvalds 	"Search examples:\n"
27359dfa24dSArnaud Lacombe 	"Examples: USB	=> find all symbols containing USB\n"
27459dfa24dSArnaud Lacombe 	"          ^USB => find all symbols starting with USB\n"
27559dfa24dSArnaud Lacombe 	"          USB$ => find all symbols ending with USB\n"
2763b9fa093SArnaldo Carvalho de Melo 	"\n");
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static int indent;
2791da177e4SLinus Torvalds static struct menu *current_menu;
2801da177e4SLinus Torvalds static int child_count;
2811da177e4SLinus Torvalds static int single_menu_mode;
28222c7eca6SLi Zefan static int show_all_options;
2831da177e4SLinus Torvalds 
2845e609addSBenjamin Poirier static void conf(struct menu *menu, struct menu *active_menu);
2851da177e4SLinus Torvalds static void conf_choice(struct menu *menu);
2861da177e4SLinus Torvalds static void conf_string(struct menu *menu);
2871da177e4SLinus Torvalds static void conf_load(void);
2881da177e4SLinus Torvalds static void conf_save(void);
28995ac9b3bSBenjamin Poirier static int show_textbox_ext(const char *title, char *text, int r, int c,
29095ac9b3bSBenjamin Poirier 			    int *keys, int *vscroll, int *hscroll,
29195ac9b3bSBenjamin Poirier 			    update_text_fn update_text, void *data);
2921da177e4SLinus Torvalds static void show_textbox(const char *title, const char *text, int r, int c);
2931da177e4SLinus Torvalds static void show_helptext(const char *title, const char *text);
2941da177e4SLinus Torvalds static void show_help(struct menu *menu);
2951da177e4SLinus Torvalds 
29695e30f95SSam Ravnborg static char filename[PATH_MAX+1];
29795e30f95SSam Ravnborg static void set_config_filename(const char *config_filename)
29895e30f95SSam Ravnborg {
29995e30f95SSam Ravnborg 	static char menu_backtitle[PATH_MAX+128];
30095e30f95SSam Ravnborg 	int size;
30195e30f95SSam Ravnborg 
30295e30f95SSam Ravnborg 	size = snprintf(menu_backtitle, sizeof(menu_backtitle),
3030954828fSArnaud Lacombe 	                "%s - %s", config_filename, rootmenu.prompt->text);
30495e30f95SSam Ravnborg 	if (size >= sizeof(menu_backtitle))
30595e30f95SSam Ravnborg 		menu_backtitle[sizeof(menu_backtitle)-1] = '\0';
30695e30f95SSam Ravnborg 	set_dialog_backtitle(menu_backtitle);
30795e30f95SSam Ravnborg 
30895e30f95SSam Ravnborg 	size = snprintf(filename, sizeof(filename), "%s", config_filename);
30995e30f95SSam Ravnborg 	if (size >= sizeof(filename))
31095e30f95SSam Ravnborg 		filename[sizeof(filename)-1] = '\0';
31195e30f95SSam Ravnborg }
31295e30f95SSam Ravnborg 
31395e30f95SSam Ravnborg 
31495ac9b3bSBenjamin Poirier struct search_data {
315bad9955dSBenjamin Poirier 	struct list_head *head;
31695ac9b3bSBenjamin Poirier 	struct menu **targets;
31795ac9b3bSBenjamin Poirier 	int *keys;
31895ac9b3bSBenjamin Poirier };
31995ac9b3bSBenjamin Poirier 
32095ac9b3bSBenjamin Poirier static void update_text(char *buf, size_t start, size_t end, void *_data)
32195ac9b3bSBenjamin Poirier {
32295ac9b3bSBenjamin Poirier 	struct search_data *data = _data;
32395ac9b3bSBenjamin Poirier 	struct jump_key *pos;
32495ac9b3bSBenjamin Poirier 	int k = 0;
32595ac9b3bSBenjamin Poirier 
326bad9955dSBenjamin Poirier 	list_for_each_entry(pos, data->head, entries) {
32795ac9b3bSBenjamin Poirier 		if (pos->offset >= start && pos->offset < end) {
32895ac9b3bSBenjamin Poirier 			char header[4];
32995ac9b3bSBenjamin Poirier 
33095ac9b3bSBenjamin Poirier 			if (k < JUMP_NB) {
33195ac9b3bSBenjamin Poirier 				int key = '0' + (pos->index % JUMP_NB) + 1;
33295ac9b3bSBenjamin Poirier 
33395ac9b3bSBenjamin Poirier 				sprintf(header, "(%c)", key);
33495ac9b3bSBenjamin Poirier 				data->keys[k] = key;
33595ac9b3bSBenjamin Poirier 				data->targets[k] = pos->target;
33695ac9b3bSBenjamin Poirier 				k++;
33795ac9b3bSBenjamin Poirier 			} else {
33895ac9b3bSBenjamin Poirier 				sprintf(header, "   ");
33995ac9b3bSBenjamin Poirier 			}
34095ac9b3bSBenjamin Poirier 
34195ac9b3bSBenjamin Poirier 			memcpy(buf + pos->offset, header, sizeof(header) - 1);
34295ac9b3bSBenjamin Poirier 		}
34395ac9b3bSBenjamin Poirier 	}
34495ac9b3bSBenjamin Poirier 	data->keys[k] = 0;
34595ac9b3bSBenjamin Poirier }
34695ac9b3bSBenjamin Poirier 
3471da177e4SLinus Torvalds static void search_conf(void)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	struct symbol **sym_arr;
3501da177e4SLinus Torvalds 	struct gstr res;
3510584f9f9SBernhard Walle 	char *dialog_input;
3525e609addSBenjamin Poirier 	int dres, vscroll = 0, hscroll = 0;
3535e609addSBenjamin Poirier 	bool again;
3545e609addSBenjamin Poirier 
3551da177e4SLinus Torvalds again:
356e94c5bdeSSam Ravnborg 	dialog_clear();
3572982de69SSam Ravnborg 	dres = dialog_inputbox(_("Search Configuration Parameter"),
358ffb5957bSArnaud Lacombe 			      _("Enter " CONFIG_ " (sub)string to search for "
359ffb5957bSArnaud Lacombe 				"(with or without \"" CONFIG_ "\")"),
3602982de69SSam Ravnborg 			      10, 75, "");
3612982de69SSam Ravnborg 	switch (dres) {
3621da177e4SLinus Torvalds 	case 0:
3631da177e4SLinus Torvalds 		break;
3641da177e4SLinus Torvalds 	case 1:
3653b9fa093SArnaldo Carvalho de Melo 		show_helptext(_("Search Configuration"), search_help);
3661da177e4SLinus Torvalds 		goto again;
3671da177e4SLinus Torvalds 	default:
3681da177e4SLinus Torvalds 		return;
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 
371ffb5957bSArnaud Lacombe 	/* strip the prefix if necessary */
3720584f9f9SBernhard Walle 	dialog_input = dialog_input_result;
373ffb5957bSArnaud Lacombe 	if (strncasecmp(dialog_input_result, CONFIG_, strlen(CONFIG_)) == 0)
374ffb5957bSArnaud Lacombe 		dialog_input += strlen(CONFIG_);
3750584f9f9SBernhard Walle 
3760584f9f9SBernhard Walle 	sym_arr = sym_re_search(dialog_input);
3775e609addSBenjamin Poirier 	do {
378bad9955dSBenjamin Poirier 		LIST_HEAD(head);
37995ac9b3bSBenjamin Poirier 		struct menu *targets[JUMP_NB];
38095ac9b3bSBenjamin Poirier 		int keys[JUMP_NB + 1], i;
38195ac9b3bSBenjamin Poirier 		struct search_data data = {
38295ac9b3bSBenjamin Poirier 			.head = &head,
38395ac9b3bSBenjamin Poirier 			.targets = targets,
38495ac9b3bSBenjamin Poirier 			.keys = keys,
38595ac9b3bSBenjamin Poirier 		};
3865e609addSBenjamin Poirier 
38795ac9b3bSBenjamin Poirier 		res = get_relations_str(sym_arr, &head);
38895ac9b3bSBenjamin Poirier 		dres = show_textbox_ext(_("Search Results"), (char *)
38995ac9b3bSBenjamin Poirier 					str_get(&res), 0, 0, keys, &vscroll,
39095ac9b3bSBenjamin Poirier 					&hscroll, &update_text, (void *)
39195ac9b3bSBenjamin Poirier 					&data);
3925e609addSBenjamin Poirier 		again = false;
39395ac9b3bSBenjamin Poirier 		for (i = 0; i < JUMP_NB && keys[i]; i++)
3945e609addSBenjamin Poirier 			if (dres == keys[i]) {
39595ac9b3bSBenjamin Poirier 				conf(targets[i]->parent, targets[i]);
3965e609addSBenjamin Poirier 				again = true;
3975e609addSBenjamin Poirier 			}
3981da177e4SLinus Torvalds 		str_free(&res);
3995e609addSBenjamin Poirier 	} while (again);
4005e609addSBenjamin Poirier 	free(sym_arr);
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds static void build_conf(struct menu *menu)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	struct symbol *sym;
4061da177e4SLinus Torvalds 	struct property *prop;
4071da177e4SLinus Torvalds 	struct menu *child;
4081da177e4SLinus Torvalds 	int type, tmp, doint = 2;
4091da177e4SLinus Torvalds 	tristate val;
4101da177e4SLinus Torvalds 	char ch;
41122c7eca6SLi Zefan 	bool visible;
4121da177e4SLinus Torvalds 
41322c7eca6SLi Zefan 	/*
41422c7eca6SLi Zefan 	 * note: menu_is_visible() has side effect that it will
41522c7eca6SLi Zefan 	 * recalc the value of the symbol.
41622c7eca6SLi Zefan 	 */
41722c7eca6SLi Zefan 	visible = menu_is_visible(menu);
41822c7eca6SLi Zefan 	if (show_all_options && !menu_has_prompt(menu))
41922c7eca6SLi Zefan 		return;
42022c7eca6SLi Zefan 	else if (!show_all_options && !visible)
4211da177e4SLinus Torvalds 		return;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	sym = menu->sym;
4241da177e4SLinus Torvalds 	prop = menu->prompt;
4251da177e4SLinus Torvalds 	if (!sym) {
4261da177e4SLinus Torvalds 		if (prop && menu != current_menu) {
4271da177e4SLinus Torvalds 			const char *prompt = menu_get_prompt(menu);
4281da177e4SLinus Torvalds 			switch (prop->type) {
4291da177e4SLinus Torvalds 			case P_MENU:
4301da177e4SLinus Torvalds 				child_count++;
431413f006bSEGRY Gabor 				prompt = _(prompt);
4321da177e4SLinus Torvalds 				if (single_menu_mode) {
4332982de69SSam Ravnborg 					item_make("%s%*c%s",
4341da177e4SLinus Torvalds 						  menu->data ? "-->" : "++>",
4351da177e4SLinus Torvalds 						  indent + 1, ' ', prompt);
4361da177e4SLinus Torvalds 				} else
4372982de69SSam Ravnborg 					item_make("   %*c%s  --->", indent + 1, ' ', prompt);
4381da177e4SLinus Torvalds 
4392982de69SSam Ravnborg 				item_set_tag('m');
4402982de69SSam Ravnborg 				item_set_data(menu);
4411da177e4SLinus Torvalds 				if (single_menu_mode && menu->data)
4421da177e4SLinus Torvalds 					goto conf_childs;
4431da177e4SLinus Torvalds 				return;
44448874077SSam Ravnborg 			case P_COMMENT:
44548874077SSam Ravnborg 				if (prompt) {
44648874077SSam Ravnborg 					child_count++;
447413f006bSEGRY Gabor 					item_make("   %*c*** %s ***", indent + 1, ' ', _(prompt));
44848874077SSam Ravnborg 					item_set_tag(':');
44948874077SSam Ravnborg 					item_set_data(menu);
45048874077SSam Ravnborg 				}
45148874077SSam Ravnborg 				break;
4521da177e4SLinus Torvalds 			default:
4531da177e4SLinus Torvalds 				if (prompt) {
4541da177e4SLinus Torvalds 					child_count++;
455413f006bSEGRY Gabor 					item_make("---%*c%s", indent + 1, ' ', _(prompt));
4562982de69SSam Ravnborg 					item_set_tag(':');
4572982de69SSam Ravnborg 					item_set_data(menu);
4581da177e4SLinus Torvalds 				}
4591da177e4SLinus Torvalds 			}
4601da177e4SLinus Torvalds 		} else
4611da177e4SLinus Torvalds 			doint = 0;
4621da177e4SLinus Torvalds 		goto conf_childs;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	type = sym_get_type(sym);
4661da177e4SLinus Torvalds 	if (sym_is_choice(sym)) {
4671da177e4SLinus Torvalds 		struct symbol *def_sym = sym_get_choice_value(sym);
4681da177e4SLinus Torvalds 		struct menu *def_menu = NULL;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 		child_count++;
4711da177e4SLinus Torvalds 		for (child = menu->list; child; child = child->next) {
4721da177e4SLinus Torvalds 			if (menu_is_visible(child) && child->sym == def_sym)
4731da177e4SLinus Torvalds 				def_menu = child;
4741da177e4SLinus Torvalds 		}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 		val = sym_get_tristate_value(sym);
4771da177e4SLinus Torvalds 		if (sym_is_changable(sym)) {
4781da177e4SLinus Torvalds 			switch (type) {
4791da177e4SLinus Torvalds 			case S_BOOLEAN:
4802982de69SSam Ravnborg 				item_make("[%c]", val == no ? ' ' : '*');
4811da177e4SLinus Torvalds 				break;
4821da177e4SLinus Torvalds 			case S_TRISTATE:
4831da177e4SLinus Torvalds 				switch (val) {
4841da177e4SLinus Torvalds 				case yes: ch = '*'; break;
4851da177e4SLinus Torvalds 				case mod: ch = 'M'; break;
4861da177e4SLinus Torvalds 				default:  ch = ' '; break;
4871da177e4SLinus Torvalds 				}
4882982de69SSam Ravnborg 				item_make("<%c>", ch);
4891da177e4SLinus Torvalds 				break;
4901da177e4SLinus Torvalds 			}
4912982de69SSam Ravnborg 			item_set_tag('t');
4922982de69SSam Ravnborg 			item_set_data(menu);
4931da177e4SLinus Torvalds 		} else {
4942982de69SSam Ravnborg 			item_make("   ");
4952982de69SSam Ravnborg 			item_set_tag(def_menu ? 't' : ':');
4962982de69SSam Ravnborg 			item_set_data(menu);
4971da177e4SLinus Torvalds 		}
4981da177e4SLinus Torvalds 
499413f006bSEGRY Gabor 		item_add_str("%*c%s", indent + 1, ' ', _(menu_get_prompt(menu)));
5001da177e4SLinus Torvalds 		if (val == yes) {
5011da177e4SLinus Torvalds 			if (def_menu) {
502413f006bSEGRY Gabor 				item_add_str(" (%s)", _(menu_get_prompt(def_menu)));
5032982de69SSam Ravnborg 				item_add_str("  --->");
5041da177e4SLinus Torvalds 				if (def_menu->list) {
5051da177e4SLinus Torvalds 					indent += 2;
5061da177e4SLinus Torvalds 					build_conf(def_menu);
5071da177e4SLinus Torvalds 					indent -= 2;
5081da177e4SLinus Torvalds 				}
5092982de69SSam Ravnborg 			}
5101da177e4SLinus Torvalds 			return;
5111da177e4SLinus Torvalds 		}
5121da177e4SLinus Torvalds 	} else {
5131da177e4SLinus Torvalds 		if (menu == current_menu) {
514413f006bSEGRY Gabor 			item_make("---%*c%s", indent + 1, ' ', _(menu_get_prompt(menu)));
5152982de69SSam Ravnborg 			item_set_tag(':');
5162982de69SSam Ravnborg 			item_set_data(menu);
5171da177e4SLinus Torvalds 			goto conf_childs;
5181da177e4SLinus Torvalds 		}
5191da177e4SLinus Torvalds 		child_count++;
5201da177e4SLinus Torvalds 		val = sym_get_tristate_value(sym);
5211da177e4SLinus Torvalds 		if (sym_is_choice_value(sym) && val == yes) {
5222982de69SSam Ravnborg 			item_make("   ");
5232982de69SSam Ravnborg 			item_set_tag(':');
5242982de69SSam Ravnborg 			item_set_data(menu);
5251da177e4SLinus Torvalds 		} else {
5261da177e4SLinus Torvalds 			switch (type) {
5271da177e4SLinus Torvalds 			case S_BOOLEAN:
5281da177e4SLinus Torvalds 				if (sym_is_changable(sym))
5292982de69SSam Ravnborg 					item_make("[%c]", val == no ? ' ' : '*');
5301da177e4SLinus Torvalds 				else
531b5d609dbSMatej Laitl 					item_make("-%c-", val == no ? ' ' : '*');
5322982de69SSam Ravnborg 				item_set_tag('t');
5332982de69SSam Ravnborg 				item_set_data(menu);
5341da177e4SLinus Torvalds 				break;
5351da177e4SLinus Torvalds 			case S_TRISTATE:
5361da177e4SLinus Torvalds 				switch (val) {
5371da177e4SLinus Torvalds 				case yes: ch = '*'; break;
5381da177e4SLinus Torvalds 				case mod: ch = 'M'; break;
5391da177e4SLinus Torvalds 				default:  ch = ' '; break;
5401da177e4SLinus Torvalds 				}
541b5d609dbSMatej Laitl 				if (sym_is_changable(sym)) {
542b5d609dbSMatej Laitl 					if (sym->rev_dep.tri == mod)
543b5d609dbSMatej Laitl 						item_make("{%c}", ch);
5441da177e4SLinus Torvalds 					else
545b5d609dbSMatej Laitl 						item_make("<%c>", ch);
546b5d609dbSMatej Laitl 				} else
547b5d609dbSMatej Laitl 					item_make("-%c-", ch);
5482982de69SSam Ravnborg 				item_set_tag('t');
5492982de69SSam Ravnborg 				item_set_data(menu);
5501da177e4SLinus Torvalds 				break;
5511da177e4SLinus Torvalds 			default:
5522982de69SSam Ravnborg 				tmp = 2 + strlen(sym_get_string_value(sym)); /* () = 2 */
5532982de69SSam Ravnborg 				item_make("(%s)", sym_get_string_value(sym));
5541da177e4SLinus Torvalds 				tmp = indent - tmp + 4;
5551da177e4SLinus Torvalds 				if (tmp < 0)
5561da177e4SLinus Torvalds 					tmp = 0;
557413f006bSEGRY Gabor 				item_add_str("%*c%s%s", tmp, ' ', _(menu_get_prompt(menu)),
5581da177e4SLinus Torvalds 					     (sym_has_value(sym) || !sym_is_changable(sym)) ?
559413f006bSEGRY Gabor 					     "" : _(" (NEW)"));
5602982de69SSam Ravnborg 				item_set_tag('s');
5612982de69SSam Ravnborg 				item_set_data(menu);
5621da177e4SLinus Torvalds 				goto conf_childs;
5631da177e4SLinus Torvalds 			}
5641da177e4SLinus Torvalds 		}
565413f006bSEGRY Gabor 		item_add_str("%*c%s%s", indent + 1, ' ', _(menu_get_prompt(menu)),
5661da177e4SLinus Torvalds 			  (sym_has_value(sym) || !sym_is_changable(sym)) ?
567413f006bSEGRY Gabor 			  "" : _(" (NEW)"));
5681da177e4SLinus Torvalds 		if (menu->prompt->type == P_MENU) {
5692982de69SSam Ravnborg 			item_add_str("  --->");
5701da177e4SLinus Torvalds 			return;
5711da177e4SLinus Torvalds 		}
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds conf_childs:
5751da177e4SLinus Torvalds 	indent += doint;
5761da177e4SLinus Torvalds 	for (child = menu->list; child; child = child->next)
5771da177e4SLinus Torvalds 		build_conf(child);
5781da177e4SLinus Torvalds 	indent -= doint;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5815e609addSBenjamin Poirier static void conf(struct menu *menu, struct menu *active_menu)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds 	struct menu *submenu;
5841da177e4SLinus Torvalds 	const char *prompt = menu_get_prompt(menu);
5851da177e4SLinus Torvalds 	struct symbol *sym;
5862982de69SSam Ravnborg 	int res;
5872982de69SSam Ravnborg 	int s_scroll = 0;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	while (1) {
5902982de69SSam Ravnborg 		item_reset();
5911da177e4SLinus Torvalds 		current_menu = menu;
5921da177e4SLinus Torvalds 		build_conf(menu);
5931da177e4SLinus Torvalds 		if (!child_count)
5941da177e4SLinus Torvalds 			break;
5951da177e4SLinus Torvalds 		if (menu == &rootmenu) {
5962982de69SSam Ravnborg 			item_make("--- ");
5972982de69SSam Ravnborg 			item_set_tag(':');
5982982de69SSam Ravnborg 			item_make(_("    Load an Alternate Configuration File"));
5992982de69SSam Ravnborg 			item_set_tag('L');
6002982de69SSam Ravnborg 			item_make(_("    Save an Alternate Configuration File"));
6012982de69SSam Ravnborg 			item_set_tag('S');
6021da177e4SLinus Torvalds 		}
603e94c5bdeSSam Ravnborg 		dialog_clear();
604413f006bSEGRY Gabor 		res = dialog_menu(prompt ? _(prompt) : _("Main Menu"),
6052982de69SSam Ravnborg 				  _(menu_instructions),
6062982de69SSam Ravnborg 				  active_menu, &s_scroll);
607c8dc68adSSam Ravnborg 		if (res == 1 || res == KEY_ESC || res == -ERRDISPLAYTOOSMALL)
6081da177e4SLinus Torvalds 			break;
6092982de69SSam Ravnborg 		if (!item_activate_selected())
6102982de69SSam Ravnborg 			continue;
6112982de69SSam Ravnborg 		if (!item_tag())
6121da177e4SLinus Torvalds 			continue;
6131da177e4SLinus Torvalds 
6142982de69SSam Ravnborg 		submenu = item_data();
6152982de69SSam Ravnborg 		active_menu = item_data();
616c8dc68adSSam Ravnborg 		if (submenu)
6171da177e4SLinus Torvalds 			sym = submenu->sym;
618c8dc68adSSam Ravnborg 		else
619c8dc68adSSam Ravnborg 			sym = NULL;
6201da177e4SLinus Torvalds 
6212982de69SSam Ravnborg 		switch (res) {
6221da177e4SLinus Torvalds 		case 0:
6232982de69SSam Ravnborg 			switch (item_tag()) {
6241da177e4SLinus Torvalds 			case 'm':
6251da177e4SLinus Torvalds 				if (single_menu_mode)
6261da177e4SLinus Torvalds 					submenu->data = (void *) (long) !submenu->data;
6271da177e4SLinus Torvalds 				else
6285e609addSBenjamin Poirier 					conf(submenu, NULL);
6291da177e4SLinus Torvalds 				break;
6301da177e4SLinus Torvalds 			case 't':
6311da177e4SLinus Torvalds 				if (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)
6321da177e4SLinus Torvalds 					conf_choice(submenu);
6331da177e4SLinus Torvalds 				else if (submenu->prompt->type == P_MENU)
6345e609addSBenjamin Poirier 					conf(submenu, NULL);
6351da177e4SLinus Torvalds 				break;
6361da177e4SLinus Torvalds 			case 's':
6371da177e4SLinus Torvalds 				conf_string(submenu);
6381da177e4SLinus Torvalds 				break;
6391da177e4SLinus Torvalds 			case 'L':
6401da177e4SLinus Torvalds 				conf_load();
6411da177e4SLinus Torvalds 				break;
6421da177e4SLinus Torvalds 			case 'S':
6431da177e4SLinus Torvalds 				conf_save();
6441da177e4SLinus Torvalds 				break;
6451da177e4SLinus Torvalds 			}
6461da177e4SLinus Torvalds 			break;
6471da177e4SLinus Torvalds 		case 2:
6481da177e4SLinus Torvalds 			if (sym)
6491da177e4SLinus Torvalds 				show_help(submenu);
6501da177e4SLinus Torvalds 			else
651413f006bSEGRY Gabor 				show_helptext(_("README"), _(mconf_readme));
6521da177e4SLinus Torvalds 			break;
6531da177e4SLinus Torvalds 		case 3:
6542982de69SSam Ravnborg 			if (item_is_tag('t')) {
6551da177e4SLinus Torvalds 				if (sym_set_tristate_value(sym, yes))
6561da177e4SLinus Torvalds 					break;
6571da177e4SLinus Torvalds 				if (sym_set_tristate_value(sym, mod))
6581da177e4SLinus Torvalds 					show_textbox(NULL, setmod_text, 6, 74);
6591da177e4SLinus Torvalds 			}
6601da177e4SLinus Torvalds 			break;
6611da177e4SLinus Torvalds 		case 4:
6622982de69SSam Ravnborg 			if (item_is_tag('t'))
6631da177e4SLinus Torvalds 				sym_set_tristate_value(sym, no);
6641da177e4SLinus Torvalds 			break;
6651da177e4SLinus Torvalds 		case 5:
6662982de69SSam Ravnborg 			if (item_is_tag('t'))
6671da177e4SLinus Torvalds 				sym_set_tristate_value(sym, mod);
6681da177e4SLinus Torvalds 			break;
6691da177e4SLinus Torvalds 		case 6:
6702982de69SSam Ravnborg 			if (item_is_tag('t'))
6711da177e4SLinus Torvalds 				sym_toggle_tristate_value(sym);
6722982de69SSam Ravnborg 			else if (item_is_tag('m'))
6735e609addSBenjamin Poirier 				conf(submenu, NULL);
6741da177e4SLinus Torvalds 			break;
6751da177e4SLinus Torvalds 		case 7:
6761da177e4SLinus Torvalds 			search_conf();
6771da177e4SLinus Torvalds 			break;
67822c7eca6SLi Zefan 		case 8:
67922c7eca6SLi Zefan 			show_all_options = !show_all_options;
68022c7eca6SLi Zefan 			break;
6811da177e4SLinus Torvalds 		}
6821da177e4SLinus Torvalds 	}
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
68595ac9b3bSBenjamin Poirier static int show_textbox_ext(const char *title, char *text, int r, int c, int
68695ac9b3bSBenjamin Poirier 			    *keys, int *vscroll, int *hscroll, update_text_fn
68795ac9b3bSBenjamin Poirier 			    update_text, void *data)
6881da177e4SLinus Torvalds {
689e94c5bdeSSam Ravnborg 	dialog_clear();
69095ac9b3bSBenjamin Poirier 	return dialog_textbox(title, text, r, c, keys, vscroll, hscroll,
69195ac9b3bSBenjamin Poirier 			      update_text, data);
692537ddae7SBenjamin Poirier }
693537ddae7SBenjamin Poirier 
694537ddae7SBenjamin Poirier static void show_textbox(const char *title, const char *text, int r, int c)
695537ddae7SBenjamin Poirier {
69695ac9b3bSBenjamin Poirier 	show_textbox_ext(title, (char *) text, r, c, (int []) {0}, NULL, NULL,
69795ac9b3bSBenjamin Poirier 			 NULL, NULL);
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds static void show_helptext(const char *title, const char *text)
7011da177e4SLinus Torvalds {
7021da177e4SLinus Torvalds 	show_textbox(title, text, 0, 0);
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds static void show_help(struct menu *menu)
7061da177e4SLinus Torvalds {
7071da177e4SLinus Torvalds 	struct gstr help = str_new();
7081da177e4SLinus Torvalds 
709da60fbbcSVadim Bendebury (вб) 	help.max_width = getmaxx(stdscr) - 10;
7101d525e7cSCheng Renquan 	menu_get_ext_help(menu, &help);
7111d525e7cSCheng Renquan 
712413f006bSEGRY Gabor 	show_helptext(_(menu_get_prompt(menu)), str_get(&help));
7131da177e4SLinus Torvalds 	str_free(&help);
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds static void conf_choice(struct menu *menu)
7171da177e4SLinus Torvalds {
718413f006bSEGRY Gabor 	const char *prompt = _(menu_get_prompt(menu));
7191da177e4SLinus Torvalds 	struct menu *child;
7201da177e4SLinus Torvalds 	struct symbol *active;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	active = sym_get_choice_value(menu->sym);
7231da177e4SLinus Torvalds 	while (1) {
7242982de69SSam Ravnborg 		int res;
7252982de69SSam Ravnborg 		int selected;
7262982de69SSam Ravnborg 		item_reset();
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 		current_menu = menu;
7291da177e4SLinus Torvalds 		for (child = menu->list; child; child = child->next) {
7301da177e4SLinus Torvalds 			if (!menu_is_visible(child))
7311da177e4SLinus Torvalds 				continue;
732af6c1598SPeter Korsgaard 			if (child->sym)
733413f006bSEGRY Gabor 				item_make("%s", _(menu_get_prompt(child)));
734af6c1598SPeter Korsgaard 			else {
735af6c1598SPeter Korsgaard 				item_make("*** %s ***", _(menu_get_prompt(child)));
736af6c1598SPeter Korsgaard 				item_set_tag(':');
737af6c1598SPeter Korsgaard 			}
7382982de69SSam Ravnborg 			item_set_data(child);
7392982de69SSam Ravnborg 			if (child->sym == active)
7402982de69SSam Ravnborg 				item_set_selected(1);
7411da177e4SLinus Torvalds 			if (child->sym == sym_get_choice_value(menu->sym))
7422982de69SSam Ravnborg 				item_set_tag('X');
7431da177e4SLinus Torvalds 		}
744e94c5bdeSSam Ravnborg 		dialog_clear();
745413f006bSEGRY Gabor 		res = dialog_checklist(prompt ? _(prompt) : _("Main Menu"),
7462982de69SSam Ravnborg 					_(radiolist_instructions),
7472982de69SSam Ravnborg 					 15, 70, 6);
7482982de69SSam Ravnborg 		selected = item_activate_selected();
7492982de69SSam Ravnborg 		switch (res) {
7501da177e4SLinus Torvalds 		case 0:
7512982de69SSam Ravnborg 			if (selected) {
7522982de69SSam Ravnborg 				child = item_data();
753af6c1598SPeter Korsgaard 				if (!child->sym)
754af6c1598SPeter Korsgaard 					break;
755af6c1598SPeter Korsgaard 
7561da177e4SLinus Torvalds 				sym_set_tristate_value(child->sym, yes);
7572982de69SSam Ravnborg 			}
7581da177e4SLinus Torvalds 			return;
7591da177e4SLinus Torvalds 		case 1:
7602982de69SSam Ravnborg 			if (selected) {
7612982de69SSam Ravnborg 				child = item_data();
7621da177e4SLinus Torvalds 				show_help(child);
7631da177e4SLinus Torvalds 				active = child->sym;
7641da177e4SLinus Torvalds 			} else
7651da177e4SLinus Torvalds 				show_help(menu);
7661da177e4SLinus Torvalds 			break;
767f3cbcdc9SSam Ravnborg 		case KEY_ESC:
7681da177e4SLinus Torvalds 			return;
769c8dc68adSSam Ravnborg 		case -ERRDISPLAYTOOSMALL:
770c8dc68adSSam Ravnborg 			return;
7711da177e4SLinus Torvalds 		}
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds static void conf_string(struct menu *menu)
7761da177e4SLinus Torvalds {
7771da177e4SLinus Torvalds 	const char *prompt = menu_get_prompt(menu);
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	while (1) {
7802982de69SSam Ravnborg 		int res;
781c4143a83SSam Ravnborg 		const char *heading;
7822982de69SSam Ravnborg 
7831da177e4SLinus Torvalds 		switch (sym_get_type(menu->sym)) {
7841da177e4SLinus Torvalds 		case S_INT:
7852982de69SSam Ravnborg 			heading = _(inputbox_instructions_int);
7861da177e4SLinus Torvalds 			break;
7871da177e4SLinus Torvalds 		case S_HEX:
7882982de69SSam Ravnborg 			heading = _(inputbox_instructions_hex);
7891da177e4SLinus Torvalds 			break;
7901da177e4SLinus Torvalds 		case S_STRING:
7912982de69SSam Ravnborg 			heading = _(inputbox_instructions_string);
7921da177e4SLinus Torvalds 			break;
7931da177e4SLinus Torvalds 		default:
794413f006bSEGRY Gabor 			heading = _("Internal mconf error!");
7951da177e4SLinus Torvalds 		}
796e94c5bdeSSam Ravnborg 		dialog_clear();
797413f006bSEGRY Gabor 		res = dialog_inputbox(prompt ? _(prompt) : _("Main Menu"),
7982982de69SSam Ravnborg 				      heading, 10, 75,
7992982de69SSam Ravnborg 				      sym_get_string_value(menu->sym));
8002982de69SSam Ravnborg 		switch (res) {
8011da177e4SLinus Torvalds 		case 0:
8022982de69SSam Ravnborg 			if (sym_set_string_value(menu->sym, dialog_input_result))
8031da177e4SLinus Torvalds 				return;
8043b9fa093SArnaldo Carvalho de Melo 			show_textbox(NULL, _("You have made an invalid entry."), 5, 43);
8051da177e4SLinus Torvalds 			break;
8061da177e4SLinus Torvalds 		case 1:
8071da177e4SLinus Torvalds 			show_help(menu);
8081da177e4SLinus Torvalds 			break;
809f3cbcdc9SSam Ravnborg 		case KEY_ESC:
8101da177e4SLinus Torvalds 			return;
8111da177e4SLinus Torvalds 		}
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds static void conf_load(void)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	while (1) {
8192982de69SSam Ravnborg 		int res;
820e94c5bdeSSam Ravnborg 		dialog_clear();
8212982de69SSam Ravnborg 		res = dialog_inputbox(NULL, load_config_text,
8222982de69SSam Ravnborg 				      11, 55, filename);
8232982de69SSam Ravnborg 		switch(res) {
8241da177e4SLinus Torvalds 		case 0:
8252982de69SSam Ravnborg 			if (!dialog_input_result[0])
8261da177e4SLinus Torvalds 				return;
82795e30f95SSam Ravnborg 			if (!conf_read(dialog_input_result)) {
82895e30f95SSam Ravnborg 				set_config_filename(dialog_input_result);
82936ef805bSSam Ravnborg 				sym_set_change_count(1);
8301da177e4SLinus Torvalds 				return;
83195e30f95SSam Ravnborg 			}
8323b9fa093SArnaldo Carvalho de Melo 			show_textbox(NULL, _("File does not exist!"), 5, 38);
8331da177e4SLinus Torvalds 			break;
8341da177e4SLinus Torvalds 		case 1:
8353b9fa093SArnaldo Carvalho de Melo 			show_helptext(_("Load Alternate Configuration"), load_config_help);
8361da177e4SLinus Torvalds 			break;
837f3cbcdc9SSam Ravnborg 		case KEY_ESC:
8381da177e4SLinus Torvalds 			return;
8391da177e4SLinus Torvalds 		}
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds static void conf_save(void)
8441da177e4SLinus Torvalds {
8451da177e4SLinus Torvalds 	while (1) {
8462982de69SSam Ravnborg 		int res;
847e94c5bdeSSam Ravnborg 		dialog_clear();
8482982de69SSam Ravnborg 		res = dialog_inputbox(NULL, save_config_text,
8492982de69SSam Ravnborg 				      11, 55, filename);
8502982de69SSam Ravnborg 		switch(res) {
8511da177e4SLinus Torvalds 		case 0:
8522982de69SSam Ravnborg 			if (!dialog_input_result[0])
8531da177e4SLinus Torvalds 				return;
85495e30f95SSam Ravnborg 			if (!conf_write(dialog_input_result)) {
85595e30f95SSam Ravnborg 				set_config_filename(dialog_input_result);
8561da177e4SLinus Torvalds 				return;
85795e30f95SSam Ravnborg 			}
8583b9fa093SArnaldo Carvalho de Melo 			show_textbox(NULL, _("Can't create file!  Probably a nonexistent directory."), 5, 60);
8591da177e4SLinus Torvalds 			break;
8601da177e4SLinus Torvalds 		case 1:
8613b9fa093SArnaldo Carvalho de Melo 			show_helptext(_("Save Alternate Configuration"), save_config_help);
8621da177e4SLinus Torvalds 			break;
863f3cbcdc9SSam Ravnborg 		case KEY_ESC:
8641da177e4SLinus Torvalds 			return;
8651da177e4SLinus Torvalds 		}
8661da177e4SLinus Torvalds 	}
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
869564899f9SDavidlohr Bueso static int handle_exit(void)
870564899f9SDavidlohr Bueso {
871564899f9SDavidlohr Bueso 	int res;
872564899f9SDavidlohr Bueso 
873564899f9SDavidlohr Bueso 	dialog_clear();
874564899f9SDavidlohr Bueso 	if (conf_get_changed())
875564899f9SDavidlohr Bueso 		res = dialog_yesno(NULL,
876564899f9SDavidlohr Bueso 				   _("Do you wish to save your new configuration ?\n"
877564899f9SDavidlohr Bueso 				     "<ESC><ESC> to continue."),
878564899f9SDavidlohr Bueso 				   6, 60);
879564899f9SDavidlohr Bueso 	else
880564899f9SDavidlohr Bueso 		res = -1;
881564899f9SDavidlohr Bueso 
882564899f9SDavidlohr Bueso 	end_dialog(saved_x, saved_y);
883564899f9SDavidlohr Bueso 
884564899f9SDavidlohr Bueso 	switch (res) {
885564899f9SDavidlohr Bueso 	case 0:
886564899f9SDavidlohr Bueso 		if (conf_write(filename)) {
887564899f9SDavidlohr Bueso 			fprintf(stderr, _("\n\n"
888564899f9SDavidlohr Bueso 					  "Error while writing of the configuration.\n"
889564899f9SDavidlohr Bueso 					  "Your configuration changes were NOT saved."
890564899f9SDavidlohr Bueso 					  "\n\n"));
891564899f9SDavidlohr Bueso 			return 1;
892564899f9SDavidlohr Bueso 		}
893564899f9SDavidlohr Bueso 		/* fall through */
894564899f9SDavidlohr Bueso 	case -1:
895564899f9SDavidlohr Bueso 		printf(_("\n\n"
896564899f9SDavidlohr Bueso 			 "*** End of the configuration.\n"
897564899f9SDavidlohr Bueso 			 "*** Execute 'make' to start the build or try 'make help'."
898564899f9SDavidlohr Bueso 			 "\n\n"));
899564899f9SDavidlohr Bueso 		res = 0;
900564899f9SDavidlohr Bueso 		break;
901564899f9SDavidlohr Bueso 	default:
902564899f9SDavidlohr Bueso 		fprintf(stderr, _("\n\n"
903564899f9SDavidlohr Bueso 				  "Your configuration changes were NOT saved."
904564899f9SDavidlohr Bueso 				  "\n\n"));
90530c4eaafSLi Zefan 		if (res != KEY_ESC)
906c55ac154SWang YanQing 			res = 0;
907564899f9SDavidlohr Bueso 	}
908564899f9SDavidlohr Bueso 
909564899f9SDavidlohr Bueso 	return res;
910564899f9SDavidlohr Bueso }
911564899f9SDavidlohr Bueso 
912564899f9SDavidlohr Bueso static void sig_handler(int signo)
913564899f9SDavidlohr Bueso {
914564899f9SDavidlohr Bueso 	exit(handle_exit());
915564899f9SDavidlohr Bueso }
916564899f9SDavidlohr Bueso 
9171da177e4SLinus Torvalds int main(int ac, char **av)
9181da177e4SLinus Torvalds {
9191da177e4SLinus Torvalds 	char *mode;
9202982de69SSam Ravnborg 	int res;
9211da177e4SLinus Torvalds 
9223b9fa093SArnaldo Carvalho de Melo 	setlocale(LC_ALL, "");
9233b9fa093SArnaldo Carvalho de Melo 	bindtextdomain(PACKAGE, LOCALEDIR);
9243b9fa093SArnaldo Carvalho de Melo 	textdomain(PACKAGE);
9253b9fa093SArnaldo Carvalho de Melo 
926564899f9SDavidlohr Bueso 	signal(SIGINT, sig_handler);
927564899f9SDavidlohr Bueso 
9281da177e4SLinus Torvalds 	conf_parse(av[1]);
9291da177e4SLinus Torvalds 	conf_read(NULL);
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	mode = getenv("MENUCONFIG_MODE");
9321da177e4SLinus Torvalds 	if (mode) {
9331da177e4SLinus Torvalds 		if (!strcasecmp(mode, "single_menu"))
9341da177e4SLinus Torvalds 			single_menu_mode = 1;
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds 
93709af091fSLadislav Michl 	if (init_dialog(NULL)) {
93809af091fSLadislav Michl 		fprintf(stderr, N_("Your display is too small to run Menuconfig!\n"));
93909af091fSLadislav Michl 		fprintf(stderr, N_("It must be at least 19 lines by 80 columns.\n"));
94009af091fSLadislav Michl 		return 1;
94109af091fSLadislav Michl 	}
94209af091fSLadislav Michl 
943d802b50fSSam Ravnborg 	set_config_filename(conf_get_configname());
944f3cbcdc9SSam Ravnborg 	do {
9455e609addSBenjamin Poirier 		conf(&rootmenu, NULL);
946564899f9SDavidlohr Bueso 		res = handle_exit();
947f3cbcdc9SSam Ravnborg 	} while (res == KEY_ESC);
948b3214293SKarsten Wiese 
949564899f9SDavidlohr Bueso 	return res;
9501da177e4SLinus Torvalds }
951c4143a83SSam Ravnborg 
952