2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
5 * Introduced single menu mode (show all sub-menus in one large tree).
6 * 2002-11-06 Petr Baudis <pasky@ucw.cz>
22 #define LKC_DIRECT_LINK
25 static char menu_backtitle[128];
26 static const char mconf_readme[] =
29 "Some kernel features may be built directly into the kernel.\n"
30 "Some may be made into loadable runtime modules. Some features\n"
31 "may be completely removed altogether. There are also certain\n"
32 "kernel parameters which are not really features, but must be\n"
33 "entered in as decimal or hexadecimal numbers or possibly text.\n"
35 "Menu items beginning with [*], <M> or [ ] represent features\n"
36 "configured to be built in, modularized or removed respectively.\n"
37 "Pointed brackets <> represent module capable features.\n"
39 "To change any of these features, highlight it with the cursor\n"
40 "keys and press <Y> to build it in, <M> to make it a module or\n"
41 "<N> to removed it. You may also press the <Space Bar> to cycle\n"
42 "through the available options (ie. Y->N->M->Y).\n"
44 "Some additional keyboard hints:\n"
48 "o Use the Up/Down arrow keys (cursor keys) to highlight the item\n"
49 " you wish to change or submenu wish to select and press <Enter>.\n"
50 " Submenus are designated by \"--->\".\n"
52 " Shortcut: Press the option's highlighted letter (hotkey).\n"
53 " Pressing a hotkey more than once will sequence\n"
54 " through all visible items which use that hotkey.\n"
56 " You may also use the <PAGE UP> and <PAGE DOWN> keys to scroll\n"
57 " unseen options into view.\n"
59 "o To exit a menu use the cursor keys to highlight the <Exit> button\n"
60 " and press <ENTER>.\n"
62 " Shortcut: Press <ESC><ESC> or <E> or <X> if there is no hotkey\n"
63 " using those letters. You may press a single <ESC>, but\n"
64 " there is a delayed response which you may find annoying.\n"
66 " Also, the <TAB> and cursor keys will cycle between <Select>,\n"
67 " <Exit> and <Help>\n"
69 "o To get help with an item, use the cursor keys to highlight <Help>\n"
70 " and Press <ENTER>.\n"
72 " Shortcut: Press <H> or <?>.\n"
75 "Radiolists (Choice lists)\n"
77 "o Use the cursor keys to select the option you wish to set and press\n"
78 " <S> or the <SPACE BAR>.\n"
80 " Shortcut: Press the first letter of the option you wish to set then\n"
81 " press <S> or <SPACE BAR>.\n"
83 "o To see available help for the item, use the cursor keys to highlight\n"
84 " <Help> and Press <ENTER>.\n"
86 " Shortcut: Press <H> or <?>.\n"
88 " Also, the <TAB> and cursor keys will cycle between <Select> and\n"
94 "o Enter the requested information and press <ENTER>\n"
95 " If you are entering hexadecimal values, it is not necessary to\n"
96 " add the '0x' prefix to the entry.\n"
98 "o For help, use the <TAB> or cursor keys to highlight the help option\n"
99 " and press <ENTER>. You can try <TAB><H> as well.\n"
102 "Text Box (Help Window)\n"
104 "o Use the cursor keys to scroll up/down/left/right. The VI editor\n"
105 " keys h,j,k,l function here as do <SPACE BAR> and <B> for those\n"
106 " who are familiar with less and lynx.\n"
108 "o Press <E>, <X>, <Enter> or <Esc><Esc> to exit.\n"
111 "Alternate Configuration Files\n"
112 "-----------------------------\n"
113 "Menuconfig supports the use of alternate configuration files for\n"
114 "those who, for various reasons, find it necessary to switch\n"
115 "between different kernel configurations.\n"
117 "At the end of the main menu you will find two options. One is\n"
118 "for saving the current configuration to a file of your choosing.\n"
119 "The other option is for loading a previously saved alternate\n"
122 "Even if you don't use alternate configuration files, but you\n"
123 "find during a Menuconfig session that you have completely messed\n"
124 "up your settings, you may use the \"Load Alternate...\" option to\n"
125 "restore your previously saved settings from \".config\" without\n"
126 "restarting Menuconfig.\n"
128 "Other information\n"
129 "-----------------\n"
130 "If you use Menuconfig in an XTERM window make sure you have your\n"
131 "$TERM variable set to point to a xterm definition which supports color.\n"
132 "Otherwise, Menuconfig will look rather bad. Menuconfig will not\n"
133 "display correctly in a RXVT window because rxvt displays only one\n"
134 "intensity of color, bright.\n"
136 "Menuconfig will display larger menus on screens or xterms which are\n"
137 "set to display more than the standard 25 row by 80 column geometry.\n"
138 "In order for this to work, the \"stty size\" command must be able to\n"
139 "display the screen's current row and column geometry. I STRONGLY\n"
140 "RECOMMEND that you make sure you do NOT have the shell variables\n"
141 "LINES and COLUMNS exported into your environment. Some distributions\n"
142 "export those variables via /etc/profile. Some ncurses programs can\n"
143 "become confused when those variables (LINES & COLUMNS) don't reflect\n"
144 "the true screen size.\n"
146 "Optional personality available\n"
147 "------------------------------\n"
148 "If you prefer to have all of the kernel options listed in a single\n"
149 "menu, rather than the default multimenu hierarchy, run the menuconfig\n"
150 "with MENUCONFIG_MODE environment variable set to single_menu. Example:\n"
152 "make MENUCONFIG_MODE=single_menu menuconfig\n"
154 "<Enter> will then unroll the appropriate category, or enfold it if it\n"
155 "is already unrolled.\n"
157 "Note that this mode can eventually be a little more CPU expensive\n"
158 "(especially with a larger number of unrolled categories) than the\n"
160 menu_instructions[] =
161 "Arrow keys navigate the menu. "
162 "<Enter> selects submenus --->. "
163 "Highlighted letters are hotkeys. "
164 "Pressing <Y> includes, <N> excludes, <M> modularizes features. "
165 "Press <Esc><Esc> to exit, <?> for Help, </> for Search. "
166 "Legend: [*] built-in [ ] excluded <M> module < > module capable",
167 radiolist_instructions[] =
168 "Use the arrow keys to navigate this window or "
169 "press the hotkey of the item you wish to select "
170 "followed by the <SPACE BAR>. "
171 "Press <?> for additional information about this option.",
172 inputbox_instructions_int[] =
173 "Please enter a decimal value. "
174 "Fractions will not be accepted. "
175 "Use the <TAB> key to move from the input field to the buttons below it.",
176 inputbox_instructions_hex[] =
177 "Please enter a hexadecimal value. "
178 "Use the <TAB> key to move from the input field to the buttons below it.",
179 inputbox_instructions_string[] =
180 "Please enter a string value. "
181 "Use the <TAB> key to move from the input field to the buttons below it.",
183 "This feature depends on another which has been configured as a module.\n"
184 "As a result, this feature will be built as a module.",
186 "There is no help available for this kernel option.\n",
188 "Enter the name of the configuration file you wish to load. "
189 "Accept the name shown to restore the configuration you "
190 "last retrieved. Leave blank to abort.",
193 "For various reasons, one may wish to keep several different kernel\n"
194 "configurations available on a single machine.\n"
196 "If you have saved a previous configuration in a file other than the\n"
197 "kernel's default, entering the name of the file here will allow you\n"
198 "to modify that configuration.\n"
200 "If you are uncertain, then you have probably never used alternate\n"
201 "configuration files. You should therefor leave this blank to abort.\n",
203 "Enter a filename to which this configuration should be saved "
204 "as an alternate. Leave blank to abort.",
207 "For various reasons, one may wish to keep different kernel\n"
208 "configurations available on a single machine.\n"
210 "Entering a file name here will allow you to later retrieve, modify\n"
211 "and use the current configuration as an alternate to whatever\n"
212 "configuration options you have selected at that time.\n"
214 "If you are uncertain what all this means then you should probably\n"
215 "leave this blank.\n",
218 "Search for CONFIG_ symbols and display their relations.\n"
219 "Example: search for \"^FOO\"\n"
221 "-----------------------------------------------------------------\n"
223 "Prompt: Foo bus is used to drive the bar HW\n"
224 "Defined at drivers/pci/Kconfig:47\n"
225 "Depends on: X86_LOCAL_APIC && X86_IO_APIC || IA64\n"
227 " -> Bus options (PCI, PCMCIA, EISA, MCA, ISA)\n"
228 " -> PCI support (PCI [=y])\n"
229 " -> PCI access mode (<choice> [=y])\n"
230 "Selects: LIBCRC32\n"
232 "-----------------------------------------------------------------\n"
233 "o The line 'Prompt:' shows the text used in the menu structure for\n"
234 " this CONFIG_ symbol\n"
235 "o The 'Defined at' line tell at what file / line number the symbol\n"
237 "o The 'Depends on:' line tell what symbols needs to be defined for\n"
238 " this symbol to be visible in the menu (selectable)\n"
239 "o The 'Location:' lines tell where in the menu structure this symbol\n"
241 " A location followed by a [=y] indicate that this is a selectable\n"
242 " menu item - and current value is displayed inside brackets.\n"
243 "o The 'Selects:' line tell what symbol will be automatically\n"
244 " selected if this symbol is selected (y or m)\n"
245 "o The 'Selected by' line tell what symbol has selected this symbol\n"
247 "Only relevant lines are shown.\n"
250 "Examples: USB => find all CONFIG_ symbols containing USB\n"
251 " ^USB => find all CONFIG_ symbols starting with USB\n"
252 " USB$ => find all CONFIG_ symbols ending with USB\n"
255 static signed char buf[4096], *bufptr = buf;
256 static signed char input_buf[4096];
257 static char filename[PATH_MAX+1] = ".config";
258 static char *args[1024], **argptr = args;
260 static struct termios ios_org;
261 static int rows = 0, cols = 0;
262 static struct menu *current_menu;
263 static int child_count;
264 static int do_resize;
265 static int single_menu_mode;
267 static void conf(struct menu *menu);
268 static void conf_choice(struct menu *menu);
269 static void conf_string(struct menu *menu);
270 static void conf_load(void);
271 static void conf_save(void);
272 static void show_textbox(const char *title, const char *text, int r, int c);
273 static void show_helptext(const char *title, const char *text);
274 static void show_help(struct menu *menu);
275 static void show_file(const char *filename, const char *title, int r, int c);
277 static void cprint_init(void);
278 static int cprint1(const char *fmt, ...);
279 static void cprint_done(void);
280 static int cprint(const char *fmt, ...);
282 static void init_wsize(void)
287 if (!ioctl(STDIN_FILENO, TIOCGWINSZ, &ws)) {
293 env = getenv("LINES");
300 env = getenv("COLUMNS");
307 if (rows < 19 || cols < 80) {
308 fprintf(stderr, "Your display is too small to run Menuconfig!\n");
309 fprintf(stderr, "It must be at least 19 lines by 80 columns.\n");
317 static void cprint_init(void)
321 memset(args, 0, sizeof(args));
324 cprint("./scripts/lxdialog/lxdialog");
325 cprint("--backtitle");
326 cprint(menu_backtitle);
329 static int cprint1(const char *fmt, ...)
337 res = vsprintf(bufptr, fmt, ap);
344 static void cprint_done(void)
350 static int cprint(const char *fmt, ...)
357 res = vsprintf(bufptr, fmt, ap);
365 static void get_prompt_str(struct gstr *r, struct property *prop)
368 struct menu *submenu[8], *menu;
370 str_printf(r, "Prompt: %s\n", prop->text);
371 str_printf(r, " Defined at %s:%d\n", prop->menu->file->name,
373 if (!expr_is_yes(prop->visible.expr)) {
374 str_append(r, " Depends on: ");
375 expr_gstr_print(prop->visible.expr, r);
378 menu = prop->menu->parent;
379 for (i = 0; menu != &rootmenu && i < 8; menu = menu->parent)
382 str_printf(r, " Location:\n");
383 for (j = 4; --i >= 0; j += 2) {
385 str_printf(r, "%*c-> %s", j, ' ', menu_get_prompt(menu));
387 str_printf(r, " (%s [=%s])", menu->sym->name ?
388 menu->sym->name : "<choice>",
389 sym_get_string_value(menu->sym));
396 static void get_symbol_str(struct gstr *r, struct symbol *sym)
399 struct property *prop;
401 str_printf(r, "Symbol: %s [=%s]\n", sym->name,
402 sym_get_string_value(sym));
403 for_all_prompts(sym, prop)
404 get_prompt_str(r, prop);
406 for_all_properties(sym, prop, P_SELECT) {
408 str_append(r, " Selects: ");
411 str_printf(r, " && ");
412 expr_gstr_print(prop->expr, r);
416 if (sym->rev_dep.expr) {
417 str_append(r, " Selected by: ");
418 expr_gstr_print(sym->rev_dep.expr, r);
421 str_append(r, "\n\n");
424 static struct gstr get_relations_str(struct symbol **sym_arr)
427 struct gstr res = str_new();
430 for (i = 0; sym_arr && (sym = sym_arr[i]); i++)
431 get_symbol_str(&res, sym);
433 str_append(&res, "No matches found.\n");
439 static void winch_handler(int sig)
447 static int exec_conf(void)
449 int pipefd[2], stat, size;
451 sigset_t sset, osset;
454 sigaddset(&sset, SIGINT);
455 sigprocmask(SIG_BLOCK, &sset, &osset);
457 signal(SIGINT, SIG_DFL);
459 sa.sa_handler = winch_handler;
460 sigemptyset(&sa.sa_mask);
461 sa.sa_flags = SA_RESTART;
462 sigaction(SIGWINCH, &sa, NULL);
469 sigprocmask(SIG_SETMASK, &osset, NULL);
473 execv(args[0], args);
480 size = input_buf + sizeof(input_buf) - bufptr;
481 size = read(pipefd[0], bufptr, size);
484 if (errno == EINTR || errno == EAGAIN)
494 waitpid(pid, &stat, 0);
499 sigprocmask(SIG_SETMASK, &osset, NULL);
502 if (WIFSIGNALED(stat)) {
503 printf("\finterrupted(%d)\n", WTERMSIG(stat));
507 printf("\fexit state: %d\nexit data: '%s'\n", WEXITSTATUS(stat), input_buf);
511 if (sigismember(&sset, SIGINT)) {
512 printf("\finterrupted\n");
515 sigprocmask(SIG_SETMASK, &osset, NULL);
517 return WEXITSTATUS(stat);
520 static void search_conf(void)
522 struct symbol **sym_arr;
529 cprint("Search Configuration Parameter");
530 cprint("--inputbox");
531 cprint("Enter Keyword");
542 show_helptext("Search Configuration", search_help);
548 sym_arr = sym_re_search(input_buf);
549 res = get_relations_str(sym_arr);
551 show_textbox("Search Results", str_get(&res), 0, 0);
555 static void build_conf(struct menu *menu)
558 struct property *prop;
560 int type, tmp, doint = 2;
564 if (!menu_is_visible(menu))
570 if (prop && menu != current_menu) {
571 const char *prompt = menu_get_prompt(menu);
572 switch (prop->type) {
577 if (single_menu_mode) {
579 menu->data ? "-->" : "++>",
580 indent + 1, ' ', prompt);
582 cprint1(" %*c%s --->", indent + 1, ' ', prompt);
585 if (single_menu_mode && menu->data)
592 cprint("---%*c%s", indent + 1, ' ', prompt);
600 type = sym_get_type(sym);
601 if (sym_is_choice(sym)) {
602 struct symbol *def_sym = sym_get_choice_value(sym);
603 struct menu *def_menu = NULL;
606 for (child = menu->list; child; child = child->next) {
607 if (menu_is_visible(child) && child->sym == def_sym)
611 val = sym_get_tristate_value(sym);
612 if (sym_is_changable(sym)) {
616 cprint1("[%c]", val == no ? ' ' : '*');
620 case yes: ch = '*'; break;
621 case mod: ch = 'M'; break;
622 default: ch = ' '; break;
628 cprint("%c%p", def_menu ? 't' : ':', menu);
632 cprint1("%*c%s", indent + 1, ' ', menu_get_prompt(menu));
635 cprint1(" (%s)", menu_get_prompt(def_menu));
638 if (def_menu->list) {
640 build_conf(def_menu);
649 if (menu == current_menu) {
651 cprint("---%*c%s", indent + 1, ' ', menu_get_prompt(menu));
655 val = sym_get_tristate_value(sym);
656 if (sym_is_choice_value(sym) && val == yes) {
663 if (sym_is_changable(sym))
664 cprint1("[%c]", val == no ? ' ' : '*');
671 case yes: ch = '*'; break;
672 case mod: ch = 'M'; break;
673 default: ch = ' '; break;
675 if (sym_is_changable(sym))
682 tmp = cprint1("(%s)", sym_get_string_value(sym));
683 tmp = indent - tmp + 4;
686 cprint1("%*c%s%s", tmp, ' ', menu_get_prompt(menu),
687 (sym_has_value(sym) || !sym_is_changable(sym)) ?
693 cprint1("%*c%s%s", indent + 1, ' ', menu_get_prompt(menu),
694 (sym_has_value(sym) || !sym_is_changable(sym)) ?
696 if (menu->prompt->type == P_MENU) {
706 for (child = menu->list; child; child = child->next)
711 static void conf(struct menu *menu)
713 struct menu *submenu;
714 const char *prompt = menu_get_prompt(menu);
716 char active_entry[40];
719 unlink("lxdialog.scrltmp");
724 cprint("%s", prompt ? prompt : "Main Menu");
726 cprint(menu_instructions);
729 cprint("%d", rows - 10);
730 cprint("%s", active_entry);
735 if (menu == &rootmenu) {
739 cprint(" Load an Alternate Configuration File");
741 cprint(" Save Configuration to an Alternate File");
747 if (stat == 1 || stat == 255)
754 for (i = 0; input_buf[i] && !isspace(input_buf[i]); i++)
756 if (i >= sizeof(active_entry))
757 i = sizeof(active_entry) - 1;
759 strcpy(active_entry, input_buf);
763 if (sscanf(input_buf + 1, "%p", &submenu) == 1)
770 if (single_menu_mode)
771 submenu->data = (void *) (long) !submenu->data;
776 if (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)
777 conf_choice(submenu);
778 else if (submenu->prompt->type == P_MENU)
782 conf_string(submenu);
796 show_helptext("README", mconf_readme);
800 if (sym_set_tristate_value(sym, yes))
802 if (sym_set_tristate_value(sym, mod))
803 show_textbox(NULL, setmod_text, 6, 74);
808 sym_set_tristate_value(sym, no);
812 sym_set_tristate_value(sym, mod);
816 sym_toggle_tristate_value(sym);
817 else if (type == 'm')
827 static void show_textbox(const char *title, const char *text, int r, int c)
831 fd = creat(".help.tmp", 0777);
832 write(fd, text, strlen(text));
834 show_file(".help.tmp", title, r, c);
838 static void show_helptext(const char *title, const char *text)
840 show_textbox(title, text, 0, 0);
843 static void show_help(struct menu *menu)
845 struct gstr help = str_new();
846 struct symbol *sym = menu->sym;
851 str_printf(&help, "CONFIG_%s:\n\n", sym->name);
852 str_append(&help, sym->help);
853 str_append(&help, "\n");
856 str_append(&help, nohelp_text);
858 get_symbol_str(&help, sym);
859 show_helptext(menu_get_prompt(menu), str_get(&help));
863 static void show_file(const char *filename, const char *title, int r, int c)
872 cprint("%s", filename);
873 cprint("%d", r ? r : rows);
874 cprint("%d", c ? c : cols);
875 } while (exec_conf() < 0);
878 static void conf_choice(struct menu *menu)
880 const char *prompt = menu_get_prompt(menu);
882 struct symbol *active;
885 active = sym_get_choice_value(menu->sym);
889 cprint("%s", prompt ? prompt : "Main Menu");
890 cprint("--radiolist");
891 cprint(radiolist_instructions);
897 for (child = menu->list; child; child = child->next) {
898 if (!menu_is_visible(child))
901 cprint("%s", menu_get_prompt(child));
902 if (child->sym == sym_get_choice_value(menu->sym))
904 else if (child->sym == active)
913 if (sscanf(input_buf, "%p", &child) != 1)
915 sym_set_tristate_value(child->sym, yes);
918 if (sscanf(input_buf, "%p", &child) == 1) {
930 static void conf_string(struct menu *menu)
932 const char *prompt = menu_get_prompt(menu);
938 cprint("%s", prompt ? prompt : "Main Menu");
939 cprint("--inputbox");
940 switch (sym_get_type(menu->sym)) {
942 cprint(inputbox_instructions_int);
945 cprint(inputbox_instructions_hex);
948 cprint(inputbox_instructions_string);
955 cprint("%s", sym_get_string_value(menu->sym));
959 if (sym_set_string_value(menu->sym, input_buf))
961 show_textbox(NULL, "You have made an invalid entry.", 5, 43);
972 static void conf_load(void)
978 cprint("--inputbox");
979 cprint(load_config_text);
982 cprint("%s", filename);
988 if (!conf_read(input_buf))
990 show_textbox(NULL, "File does not exist!", 5, 38);
993 show_helptext("Load Alternate Configuration", load_config_help);
1001 static void conf_save(void)
1007 cprint("--inputbox");
1008 cprint(save_config_text);
1011 cprint("%s", filename);
1017 if (!conf_write(input_buf))
1019 show_textbox(NULL, "Can't create file! Probably a nonexistent directory.", 5, 60);
1022 show_helptext("Save Alternate Configuration", save_config_help);
1030 static void conf_cleanup(void)
1032 tcsetattr(1, TCSAFLUSH, &ios_org);
1033 unlink(".help.tmp");
1034 unlink("lxdialog.scrltmp");
1037 int main(int ac, char **av)
1046 sym = sym_lookup("KERNELRELEASE", 0);
1047 sym_calc_value(sym);
1048 sprintf(menu_backtitle, "Linux Kernel v%s Configuration",
1049 sym_get_string_value(sym));
1051 mode = getenv("MENUCONFIG_MODE");
1053 if (!strcasecmp(mode, "single_menu"))
1054 single_menu_mode = 1;
1057 tcgetattr(1, &ios_org);
1058 atexit(conf_cleanup);
1065 cprint("Do you wish to save your new kernel configuration?");
1072 if (conf_write(NULL)) {
1073 fprintf(stderr, "\n\n"
1074 "Error during writing of the kernel configuration.\n"
1075 "Your kernel configuration changes were NOT saved."
1080 "*** End of Linux kernel configuration.\n"
1081 "*** Execute 'make' to build the kernel or try 'make help'."
1084 fprintf(stderr, "\n\n"
1085 "Your kernel configuration changes were NOT saved."