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>
8 * i18n, 2005, Arnaldo Carvalho de Melo <acme@conectiva.com.br>
23 #include "lxdialog/dialog.h"
25 static const char mconf_readme[] = N_(
28 "Some OpenWrt features may be built directly into the image.\n"
29 "Some may be made into installable ipkg packages. Some features\n"
30 "may be completely removed altogether.\n"
32 "Menu items beginning with [*], <M> or [ ] represent features\n"
33 "configured to be included, built as package or removed respectively.\n"
34 "Pointed brackets <> represent packaging capable features.\n"
36 "To change any of these features, highlight it with the cursor\n"
37 "keys and press <Y> to build it in, <M> to make it a module or\n"
38 "<N> to removed it. You may also press the <Space Bar> to cycle\n"
39 "through the available options (ie. Y->N->M->Y).\n"
41 "Some additional keyboard hints:\n"
45 "o Use the Up/Down arrow keys (cursor keys) to highlight the item\n"
46 " you wish to change or submenu wish to select and press <Enter>.\n"
47 " Submenus are designated by \"--->\".\n"
49 " Shortcut: Press the option's highlighted letter (hotkey).\n"
50 " Pressing a hotkey more than once will sequence\n"
51 " through all visible items which use that hotkey.\n"
53 " You may also use the <PAGE UP> and <PAGE DOWN> keys to scroll\n"
54 " unseen options into view.\n"
56 "o To exit a menu use the cursor keys to highlight the <Exit> button\n"
57 " and press <ENTER>.\n"
59 " Shortcut: Press <ESC><ESC> or <E> or <X> if there is no hotkey\n"
60 " using those letters. You may press a single <ESC>, but\n"
61 " there is a delayed response which you may find annoying.\n"
63 " Also, the <TAB> and cursor keys will cycle between <Select>,\n"
64 " <Exit> and <Help>.\n"
66 "o To get help with an item, use the cursor keys to highlight <Help>\n"
67 " and press <ENTER>.\n"
69 " Shortcut: Press <H> or <?>.\n"
71 "o To toggle the display of hidden options, press <Z>.\n"
74 "Radiolists (Choice lists)\n"
76 "o Use the cursor keys to select the option you wish to set and press\n"
77 " <S> or the <SPACE BAR>.\n"
79 " Shortcut: Press the first letter of the option you wish to set then\n"
80 " press <S> or <SPACE BAR>.\n"
82 "o To see available help for the item, use the cursor keys to highlight\n"
83 " <Help> and Press <ENTER>.\n"
85 " Shortcut: Press <H> or <?>.\n"
87 " Also, the <TAB> and cursor keys will cycle between <Select> and\n"
93 "o Enter the requested information and press <ENTER>\n"
94 " If you are entering hexadecimal values, it is not necessary to\n"
95 " add the '0x' prefix to the entry.\n"
97 "o For help, use the <TAB> or cursor keys to highlight the help option\n"
98 " and press <ENTER>. You can try <TAB><H> as well.\n"
101 "Text Box (Help Window)\n"
103 "o Use the cursor keys to scroll up/down/left/right. The VI editor\n"
104 " keys h,j,k,l function here as do <u>, <d>, <SPACE BAR> and <B> for \n"
105 " those who are familiar with less and lynx.\n"
107 "o Press <E>, <X>, <q>, <Enter> or <Esc><Esc> to exit.\n"
110 "Alternate Configuration Files\n"
111 "-----------------------------\n"
112 "Menuconfig supports the use of alternate configuration files for\n"
113 "those who, for various reasons, find it necessary to switch\n"
114 "between different configurations.\n"
116 "At the end of the main menu you will find two options. One is\n"
117 "for saving the current configuration to a file of your choosing.\n"
118 "The other option is for loading a previously saved alternate\n"
121 "Even if you don't use alternate configuration files, but you\n"
122 "find during a Menuconfig session that you have completely messed\n"
123 "up your settings, you may use the \"Load Alternate...\" option to\n"
124 "restore your previously saved settings from \".config\" without\n"
125 "restarting Menuconfig.\n"
127 "Other information\n"
128 "-----------------\n"
129 "If you use Menuconfig in an XTERM window make sure you have your\n"
130 "$TERM variable set to point to a xterm definition which supports color.\n"
131 "Otherwise, Menuconfig will look rather bad. Menuconfig will not\n"
132 "display correctly in a RXVT window because rxvt displays only one\n"
133 "intensity of color, bright.\n"
135 "Menuconfig will display larger menus on screens or xterms which are\n"
136 "set to display more than the standard 25 row by 80 column geometry.\n"
137 "In order for this to work, the \"stty size\" command must be able to\n"
138 "display the screen's current row and column geometry. I STRONGLY\n"
139 "RECOMMEND that you make sure you do NOT have the shell variables\n"
140 "LINES and COLUMNS exported into your environment. Some distributions\n"
141 "export those variables via /etc/profile. Some ncurses programs can\n"
142 "become confused when those variables (LINES & COLUMNS) don't reflect\n"
143 "the true screen size.\n"
145 "Optional personality available\n"
146 "------------------------------\n"
147 "If you prefer to have all of the options listed in a single menu, rather\n"
148 "than the default multimenu hierarchy, run the menuconfig with\n"
149 "MENUCONFIG_MODE environment variable set to single_menu. Example:\n"
151 "make MENUCONFIG_MODE=single_menu menuconfig\n"
153 "<Enter> will then unroll the appropriate category, or enfold it if it\n"
154 "is already unrolled.\n"
156 "Note that this mode can eventually be a little more CPU expensive\n"
157 "(especially with a larger number of unrolled categories) than the\n"
160 "Different color themes available\n"
161 "--------------------------------\n"
162 "It is possible to select different color themes using the variable\n"
163 "MENUCONFIG_COLOR. To select a theme use:\n"
165 "make MENUCONFIG_COLOR=<theme> menuconfig\n"
167 "Available themes are\n"
168 " mono => selects colors suitable for monochrome displays\n"
169 " blackbg => selects a color scheme with black background\n"
170 " classic => theme with blue background. The classic look\n"
171 " bluetitle => a LCD friendly version of classic. (default)\n"
173 menu_instructions[] = N_(
174 "Arrow keys navigate the menu. "
175 "<Enter> selects submenus --->. "
176 "Highlighted letters are hotkeys. "
177 "Pressing <Y> includes, <N> excludes, <M> modularizes features. "
178 "Press <Esc><Esc> to exit, <?> for Help, </> for Search. "
179 "Legend: [*] built-in [ ] excluded <M> module < > module capable"),
180 radiolist_instructions[] = N_(
181 "Use the arrow keys to navigate this window or "
182 "press the hotkey of the item you wish to select "
183 "followed by the <SPACE BAR>. "
184 "Press <?> for additional information about this option."),
185 inputbox_instructions_int[] = N_(
186 "Please enter a decimal value. "
187 "Fractions will not be accepted. "
188 "Use the <TAB> key to move from the input field to the buttons below it."),
189 inputbox_instructions_hex[] = N_(
190 "Please enter a hexadecimal value. "
191 "Use the <TAB> key to move from the input field to the buttons below it."),
192 inputbox_instructions_string[] = N_(
193 "Please enter a string value. "
194 "Use the <TAB> key to move from the input field to the buttons below it."),
196 "This feature depends on another which has been configured as a module.\n"
197 "As a result, this feature will be built as a module."),
198 load_config_text[] = N_(
199 "Enter the name of the configuration file you wish to load. "
200 "Accept the name shown to restore the configuration you "
201 "last retrieved. Leave blank to abort."),
202 load_config_help[] = N_(
204 "For various reasons, one may wish to keep several different\n"
205 "configurations available on a single machine.\n"
207 "If you have saved a previous configuration in a file other than the\n"
208 "default one, entering its name here will allow you to modify that\n"
211 "If you are uncertain, then you have probably never used alternate\n"
212 "configuration files. You should therefore leave this blank to abort.\n"),
213 save_config_text[] = N_(
214 "Enter a filename to which this configuration should be saved "
215 "as an alternate. Leave blank to abort."),
216 save_config_help[] = N_(
218 "For various reasons, one may wish to keep different configurations\n"
219 "available on a single machine.\n"
221 "Entering a file name here will allow you to later retrieve, modify\n"
222 "and use the current configuration as an alternate to whatever\n"
223 "configuration options you have selected at that time.\n"
225 "If you are uncertain what all this means then you should probably\n"
226 "leave this blank.\n"),
229 "Search for symbols and display their relations.\n"
230 "Regular expressions are allowed.\n"
231 "Example: search for \"^FOO\"\n"
233 "-----------------------------------------------------------------\n"
236 "Prompt: Foo bus is used to drive the bar HW\n"
237 " Defined at drivers/pci/Kconfig:47\n"
238 " Depends on: X86_LOCAL_APIC && X86_IO_APIC || IA64\n"
240 " -> Bus options (PCI, PCMCIA, EISA, ISA)\n"
241 " -> PCI support (PCI [=y])\n"
242 "(1) -> PCI access mode (<choice> [=y])\n"
243 " Selects: LIBCRC32\n"
244 " Selected by: BAR\n"
245 "-----------------------------------------------------------------\n"
246 "o The line 'Type:' shows the type of the configuration option for\n"
247 " this symbol (boolean, tristate, string, ...)\n"
248 "o The line 'Prompt:' shows the text used in the menu structure for\n"
250 "o The 'Defined at' line tell at what file / line number the symbol\n"
252 "o The 'Depends on:' line tell what symbols needs to be defined for\n"
253 " this symbol to be visible in the menu (selectable)\n"
254 "o The 'Location:' lines tell where in the menu structure this symbol\n"
256 " A location followed by a [=y] indicates that this is a\n"
257 " selectable menu item - and the current value is displayed inside\n"
259 " Press the key in the (#) prefix to jump directly to that\n"
260 " location. You will be returned to the current search results\n"
261 " after exiting this new menu.\n"
262 "o The 'Selects:' line tell what symbol will be automatically\n"
263 " selected if this symbol is selected (y or m)\n"
264 "o The 'Selected by' line tell what symbol has selected this symbol\n"
266 "Only relevant lines are shown.\n"
269 "Examples: USB => find all symbols containing USB\n"
270 " ^USB => find all symbols starting with USB\n"
271 " USB$ => find all symbols ending with USB\n"
275 static struct menu *current_menu;
276 static int child_count;
277 static int single_menu_mode;
278 static int show_all_options;
279 static int save_and_exit;
281 static void conf(struct menu *menu, struct menu *active_menu);
282 static void conf_choice(struct menu *menu);
283 static void conf_string(struct menu *menu);
284 static void conf_load(void);
285 static void conf_save(void);
286 static int show_textbox_ext(const char *title, char *text, int r, int c,
287 int *keys, int *vscroll, int *hscroll,
288 update_text_fn update_text, void *data);
289 static void show_textbox(const char *title, const char *text, int r, int c);
290 static void show_helptext(const char *title, const char *text);
291 static void show_help(struct menu *menu);
293 static char filename[PATH_MAX+1];
294 static void set_config_filename(const char *config_filename)
296 static char menu_backtitle[PATH_MAX+128];
299 size = snprintf(menu_backtitle, sizeof(menu_backtitle),
300 "%s - %s", config_filename, rootmenu.prompt->text);
301 if (size >= sizeof(menu_backtitle))
302 menu_backtitle[sizeof(menu_backtitle)-1] = '\0';
303 set_dialog_backtitle(menu_backtitle);
305 size = snprintf(filename, sizeof(filename), "%s", config_filename);
306 if (size >= sizeof(filename))
307 filename[sizeof(filename)-1] = '\0';
312 struct list_head *head;
313 struct menu **targets;
317 static void update_text(char *buf, size_t start, size_t end, void *_data)
319 struct search_data *data = _data;
320 struct jump_key *pos;
323 list_for_each_entry(pos, data->head, entries) {
324 if (pos->offset >= start && pos->offset < end) {
328 int key = '0' + (pos->index % JUMP_NB) + 1;
330 sprintf(header, "(%c)", key);
332 data->targets[k] = pos->target;
335 sprintf(header, " ");
338 memcpy(buf + pos->offset, header, sizeof(header) - 1);
344 static void search_conf(void)
346 struct symbol **sym_arr;
350 int dres, vscroll = 0, hscroll = 0;
354 str_printf( &title, _("Enter %s (sub)string to search for "
355 "(with or without \"%s\")"), CONFIG_, CONFIG_);
359 dres = dialog_inputbox(_("Search Configuration Parameter"),
366 show_helptext(_("Search Configuration"), search_help);
373 /* strip the prefix if necessary */
374 dialog_input = dialog_input_result;
375 if (strncasecmp(dialog_input_result, CONFIG_, strlen(CONFIG_)) == 0)
376 dialog_input += strlen(CONFIG_);
378 sym_arr = sym_re_search(dialog_input);
381 struct menu *targets[JUMP_NB];
382 int keys[JUMP_NB + 1], i;
383 struct search_data data = {
389 res = get_relations_str(sym_arr, &head);
390 dres = show_textbox_ext(_("Search Results"), (char *)
391 str_get(&res), 0, 0, keys, &vscroll,
392 &hscroll, &update_text, (void *)
395 for (i = 0; i < JUMP_NB && keys[i]; i++)
396 if (dres == keys[i]) {
397 conf(targets[i]->parent, targets[i]);
406 static void build_conf(struct menu *menu)
409 struct property *prop;
411 int type, tmp, doint = 2;
417 * note: menu_is_visible() has side effect that it will
418 * recalc the value of the symbol.
420 visible = menu_is_visible(menu);
421 if (show_all_options && !menu_has_prompt(menu))
423 else if (!show_all_options && !visible)
429 if (prop && menu != current_menu) {
430 const char *prompt = menu_get_prompt(menu);
431 switch (prop->type) {
435 if (single_menu_mode) {
437 menu->data ? "-->" : "++>",
438 indent + 1, ' ', prompt);
440 item_make(" %*c%s --->", indent + 1, ' ', prompt);
444 if (single_menu_mode && menu->data)
450 item_make(" %*c*** %s ***", indent + 1, ' ', _(prompt));
458 item_make("---%*c%s", indent + 1, ' ', _(prompt));
468 type = sym_get_type(sym);
469 if (sym_is_choice(sym)) {
470 struct symbol *def_sym = sym_get_choice_value(sym);
471 struct menu *def_menu = NULL;
474 for (child = menu->list; child; child = child->next) {
475 if (menu_is_visible(child) && child->sym == def_sym)
479 val = sym_get_tristate_value(sym);
480 if (sym_is_changable(sym)) {
483 item_make("[%c]", val == no ? ' ' : '*');
487 case yes: ch = '*'; break;
488 case mod: ch = 'M'; break;
489 default: ch = ' '; break;
491 item_make("<%c>", ch);
498 item_set_tag(def_menu ? 't' : ':');
502 item_add_str("%*c%s", indent + 1, ' ', _(menu_get_prompt(menu)));
505 item_add_str(" (%s)", _(menu_get_prompt(def_menu)));
506 item_add_str(" --->");
507 if (def_menu->list) {
509 build_conf(def_menu);
516 if (menu == current_menu) {
517 item_make("---%*c%s", indent + 1, ' ', _(menu_get_prompt(menu)));
523 val = sym_get_tristate_value(sym);
524 if (sym_is_choice_value(sym) && val == yes) {
531 if (sym_is_changable(sym))
532 item_make("[%c]", val == no ? ' ' : '*');
534 item_make("-%c-", val == no ? ' ' : '*');
540 case yes: ch = '*'; break;
541 case mod: ch = 'M'; break;
542 default: ch = ' '; break;
544 if (sym_is_changable(sym)) {
545 if (sym->rev_dep.tri == mod)
546 item_make("{%c}", ch);
548 item_make("<%c>", ch);
550 item_make("-%c-", ch);
555 tmp = 2 + strlen(sym_get_string_value(sym)); /* () = 2 */
556 item_make("(%s)", sym_get_string_value(sym));
557 tmp = indent - tmp + 4;
560 item_add_str("%*c%s%s", tmp, ' ', _(menu_get_prompt(menu)),
561 (sym_has_value(sym) || !sym_is_changable(sym)) ?
568 item_add_str("%*c%s%s", indent + 1, ' ', _(menu_get_prompt(menu)),
569 (sym_has_value(sym) || !sym_is_changable(sym)) ?
571 if (menu->prompt->type == P_MENU) {
572 item_add_str(" --->");
579 for (child = menu->list; child; child = child->next)
584 static void conf(struct menu *menu, struct menu *active_menu)
586 struct menu *submenu;
587 const char *prompt = menu_get_prompt(menu);
599 res = dialog_menu(prompt ? _(prompt) : _("Main Menu"),
600 _(menu_instructions),
601 active_menu, &s_scroll);
602 if (res == 1 || res == KEY_ESC || res == -ERRDISPLAYTOOSMALL)
604 if (!item_activate_selected())
609 submenu = item_data();
610 active_menu = item_data();
618 switch (item_tag()) {
620 if (single_menu_mode)
621 submenu->data = (void *) (long) !submenu->data;
626 if (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)
627 conf_choice(submenu);
628 else if (submenu->prompt->type == P_MENU)
632 conf_string(submenu);
640 show_helptext(_("README"), _(mconf_readme));
649 if (item_is_tag('t')) {
650 if (sym_set_tristate_value(sym, yes))
652 if (sym_set_tristate_value(sym, mod))
653 show_textbox(NULL, setmod_text, 6, 74);
657 if (item_is_tag('t'))
658 sym_set_tristate_value(sym, no);
661 if (item_is_tag('t'))
662 sym_set_tristate_value(sym, mod);
665 if (item_is_tag('t'))
666 sym_toggle_tristate_value(sym);
667 else if (item_is_tag('m'))
674 show_all_options = !show_all_options;
680 static int show_textbox_ext(const char *title, char *text, int r, int c, int
681 *keys, int *vscroll, int *hscroll, update_text_fn
682 update_text, void *data)
685 return dialog_textbox(title, text, r, c, keys, vscroll, hscroll,
689 static void show_textbox(const char *title, const char *text, int r, int c)
691 show_textbox_ext(title, (char *) text, r, c, (int []) {0}, NULL, NULL,
695 static void show_helptext(const char *title, const char *text)
697 show_textbox(title, text, 0, 0);
700 static void conf_message_callback(const char *fmt, va_list ap)
702 char buf[PATH_MAX+1];
704 vsnprintf(buf, sizeof(buf), fmt, ap);
708 show_textbox(NULL, buf, 6, 60);
711 static void show_help(struct menu *menu)
713 struct gstr help = str_new();
715 help.max_width = getmaxx(stdscr) - 10;
716 menu_get_ext_help(menu, &help);
718 show_helptext(_(menu_get_prompt(menu)), str_get(&help));
722 static void conf_choice(struct menu *menu)
724 const char *prompt = _(menu_get_prompt(menu));
726 struct symbol *active;
727 struct property *prop;
729 active = sym_get_choice_value(menu->sym);
736 for (child = menu->list; child; child = child->next) {
737 if (!menu_is_visible(child))
740 item_make("%s", _(menu_get_prompt(child)));
742 item_make("*** %s ***", _(menu_get_prompt(child)));
745 item_set_data(child);
746 if (child->sym == active)
747 item_set_selected(1);
748 if (child->sym == sym_get_choice_value(menu->sym))
752 res = dialog_checklist(prompt ? _(prompt) : _("Main Menu"),
753 _(radiolist_instructions),
755 selected = item_activate_selected();
763 if (sym_get_tristate_value(child->sym) != yes) {
764 for_all_properties(menu->sym, prop, P_RESET) {
765 if (expr_calc_value(prop->visible.expr) == no)
768 conf_reset(S_DEF_USER);
772 sym_set_tristate_value(child->sym, yes);
785 case -ERRDISPLAYTOOSMALL:
791 static void conf_string(struct menu *menu)
793 const char *prompt = menu_get_prompt(menu);
799 switch (sym_get_type(menu->sym)) {
801 heading = _(inputbox_instructions_int);
804 heading = _(inputbox_instructions_hex);
807 heading = _(inputbox_instructions_string);
810 heading = _("Internal mconf error!");
813 res = dialog_inputbox(prompt ? _(prompt) : _("Main Menu"),
815 sym_get_string_value(menu->sym));
818 if (sym_set_string_value(menu->sym, dialog_input_result))
820 show_textbox(NULL, _("You have made an invalid entry."), 5, 43);
831 static void conf_load(void)
837 res = dialog_inputbox(NULL, load_config_text,
841 if (!dialog_input_result[0])
843 if (!conf_read(dialog_input_result)) {
844 set_config_filename(dialog_input_result);
845 sym_set_change_count(1);
848 show_textbox(NULL, _("File does not exist!"), 5, 38);
851 show_helptext(_("Load Alternate Configuration"), load_config_help);
859 static void conf_save(void)
864 res = dialog_inputbox(NULL, save_config_text,
868 if (!dialog_input_result[0])
870 if (!conf_write(dialog_input_result)) {
871 set_config_filename(dialog_input_result);
874 show_textbox(NULL, _("Can't create file! Probably a nonexistent directory."), 5, 60);
877 show_helptext(_("Save Alternate Configuration"), save_config_help);
885 static int handle_exit(void)
891 if (conf_get_changed())
892 res = dialog_yesno(NULL,
893 _("Do you wish to save your new configuration ?\n"
894 "<ESC><ESC> to continue."),
899 end_dialog(saved_x, saved_y);
903 if (conf_write(filename)) {
904 fprintf(stderr, _("\n\n"
905 "Error while writing of the configuration.\n"
906 "Your configuration changes were NOT saved."
913 "*** End of the configuration.\n"
914 "*** Execute 'make' to start the build or try 'make help'."
919 fprintf(stderr, _("\n\n"
920 "Your configuration changes were NOT saved."
929 static void sig_handler(int signo)
934 int main(int ac, char **av)
939 setlocale(LC_ALL, "");
940 bindtextdomain(PACKAGE, LOCALEDIR);
943 signal(SIGINT, sig_handler);
948 mode = getenv("MENUCONFIG_MODE");
950 if (!strcasecmp(mode, "single_menu"))
951 single_menu_mode = 1;
954 if (init_dialog(NULL)) {
955 fprintf(stderr, N_("Your display is too small to run Menuconfig!\n"));
956 fprintf(stderr, N_("It must be at least 19 lines by 80 columns.\n"));
960 set_config_filename(conf_get_configname());
961 conf_set_message_callback(conf_message_callback);
963 conf(&rootmenu, NULL);
965 } while (res == KEY_ESC);