2 * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com?
3 * Released under the terms of the GNU GPL v2.0.
5 * Derived from menuconfig.
10 #define LKC_DIRECT_LINK
15 static const char nconf_readme[] = N_(
18 "Some kernel features may be built directly into the kernel.\n"
19 "Some may be made into loadable runtime modules. Some features\n"
20 "may be completely removed altogether. There are also certain\n"
21 "kernel parameters which are not really features, but must be\n"
22 "entered in as decimal or hexadecimal numbers or possibly text.\n"
24 "Menu items beginning with following braces represent features that\n"
25 " [ ] can be built in or removed\n"
26 " < > can be built in, modularized or removed\n"
27 " { } can be built in or modularized (selected by other feature)\n"
28 " - - are selected by other feature,\n"
29 " XXX cannot be selected. Use Symbol Info to find out why,\n"
30 "while *, M or whitespace inside braces means to build in, build as\n"
31 "a module or to exclude the feature respectively.\n"
33 "To change any of these features, highlight it with the cursor\n"
34 "keys and press <Y> to build it in, <M> to make it a module or\n"
35 "<N> to removed it. You may also press the <Space Bar> to cycle\n"
36 "through the available options (ie. Y->N->M->Y).\n"
38 "Some additional keyboard hints:\n"
42 "o Use the Up/Down arrow keys (cursor keys) to highlight the item\n"
43 " you wish to change use <Enter> or <Space>. Goto submenu by \n"
44 " pressing <Enter> of <right-arrow>. Use <Esc> or <left-arrow> to go back.\n"
45 " Submenus are designated by \"--->\".\n"
47 " Searching: pressing '/' triggers interactive search mode.\n"
48 " nconfig performs a case insensitive search for the string\n"
49 " in the menu prompts (no regex support).\n"
50 " Pressing the up/down keys highlights the previous/next\n"
51 " matching item. Backspace removes one character from the\n"
52 " match string. Pressing either '/' again or ESC exits\n"
53 " search mode. All other keys behave normally.\n"
55 " You may also use the <PAGE UP> and <PAGE DOWN> keys to scroll\n"
56 " unseen options into view.\n"
58 "o To exit a menu use the just press <ESC> <F5> <F8> or <left-arrow>.\n"
60 "o To get help with an item, press <F1>\n"
61 " Shortcut: Press <h> or <?>.\n"
64 "Radiolists (Choice lists)\n"
66 "o Use the cursor keys to select the option you wish to set and press\n"
67 " <S> or the <SPACE BAR>.\n"
69 " Shortcut: Press the first letter of the option you wish to set then\n"
70 " press <S> or <SPACE BAR>.\n"
72 "o To see available help for the item, press <F1>\n"
73 " Shortcut: Press <H> or <?>.\n"
78 "o Enter the requested information and press <ENTER>\n"
79 " If you are entering hexadecimal values, it is not necessary to\n"
80 " add the '0x' prefix to the entry.\n"
82 "o For help, press <F1>.\n"
85 "Text Box (Help Window)\n"
87 "o Use the cursor keys to scroll up/down/left/right. The VI editor\n"
88 " keys h,j,k,l function here as do <SPACE BAR> for those\n"
89 " who are familiar with less and lynx.\n"
91 "o Press <Enter>, <F1>, <F5>, <F7> or <Esc> to exit.\n"
94 "Alternate Configuration Files\n"
95 "-----------------------------\n"
96 "nconfig supports the use of alternate configuration files for\n"
97 "those who, for various reasons, find it necessary to switch\n"
98 "between different kernel configurations.\n"
100 "At the end of the main menu you will find two options. One is\n"
101 "for saving the current configuration to a file of your choosing.\n"
102 "The other option is for loading a previously saved alternate\n"
105 "Even if you don't use alternate configuration files, but you\n"
106 "find during a nconfig session that you have completely messed\n"
107 "up your settings, you may use the \"Load Alternate...\" option to\n"
108 "restore your previously saved settings from \".config\" without\n"
109 "restarting nconfig.\n"
111 "Other information\n"
112 "-----------------\n"
113 "If you use nconfig in an XTERM window make sure you have your\n"
114 "$TERM variable set to point to a xterm definition which supports color.\n"
115 "Otherwise, nconfig will look rather bad. nconfig will not\n"
116 "display correctly in a RXVT window because rxvt displays only one\n"
117 "intensity of color, bright.\n"
119 "nconfig will display larger menus on screens or xterms which are\n"
120 "set to display more than the standard 25 row by 80 column geometry.\n"
121 "In order for this to work, the \"stty size\" command must be able to\n"
122 "display the screen's current row and column geometry. I STRONGLY\n"
123 "RECOMMEND that you make sure you do NOT have the shell variables\n"
124 "LINES and COLUMNS exported into your environment. Some distributions\n"
125 "export those variables via /etc/profile. Some ncurses programs can\n"
126 "become confused when those variables (LINES & COLUMNS) don't reflect\n"
127 "the true screen size.\n"
129 "Optional personality available\n"
130 "------------------------------\n"
131 "If you prefer to have all of the kernel options listed in a single\n"
132 "menu, rather than the default multimenu hierarchy, run the nconfig\n"
133 "with NCONFIG_MODE environment variable set to single_menu. Example:\n"
135 "make NCONFIG_MODE=single_menu nconfig\n"
137 "<Enter> will then unroll the appropriate category, or enfold it if it\n"
138 "is already unrolled.\n"
140 "Note that this mode can eventually be a little more CPU expensive\n"
141 "(especially with a larger number of unrolled categories) than the\n"
144 menu_no_f_instructions[] = N_(
145 " You do not have function keys support. Please follow the\n"
146 " following instructions:\n"
147 " Arrow keys navigate the menu.\n"
148 " <Enter> or <right-arrow> selects submenus --->.\n"
149 " Capital Letters are hotkeys.\n"
150 " Pressing <Y> includes, <N> excludes, <M> modularizes features.\n"
151 " Pressing SpaceBar toggles between the above options.\n"
152 " Press <Esc> or <left-arrow> to go back one menu,\n"
153 " <?> or <h> for Help, </> for Search.\n"
154 " <1> is interchangeable with <F1>, <2> with <F2>, etc.\n"
155 " Legend: [*] built-in [ ] excluded <M> module < > module capable.\n"
156 " <Esc> always leaves the current window.\n"),
157 menu_instructions[] = N_(
158 " Arrow keys navigate the menu.\n"
159 " <Enter> or <right-arrow> selects submenus --->.\n"
160 " Capital Letters are hotkeys.\n"
161 " Pressing <Y> includes, <N> excludes, <M> modularizes features.\n"
162 " Pressing SpaceBar toggles between the above options\n"
163 " Press <Esc>, <F5> or <left-arrow> to go back one menu,\n"
164 " <?>, <F1> or <h> for Help, </> for Search.\n"
165 " <1> is interchangeable with <F1>, <2> with <F2>, etc.\n"
166 " Legend: [*] built-in [ ] excluded <M> module < > module capable.\n"
167 " <Esc> always leaves the current window\n"),
168 radiolist_instructions[] = N_(
169 " Use the arrow keys to navigate this window or\n"
170 " press the hotkey of the item you wish to select\n"
171 " followed by the <SPACE BAR>.\n"
172 " Press <?>, <F1> or <h> for additional information about this option.\n"),
173 inputbox_instructions_int[] = N_(
174 "Please enter a decimal value.\n"
175 "Fractions will not be accepted.\n"
176 "Press <RETURN> to accept, <ESC> to cancel."),
177 inputbox_instructions_hex[] = N_(
178 "Please enter a hexadecimal value.\n"
179 "Press <RETURN> to accept, <ESC> to cancel."),
180 inputbox_instructions_string[] = N_(
181 "Please enter a string value.\n"
182 "Press <RETURN> to accept, <ESC> to cancel."),
184 "This feature depends on another which\n"
185 "has been configured as a module.\n"
186 "As a result, this feature will be built as a module."),
188 "There is no help available for this kernel option.\n"),
189 load_config_text[] = N_(
190 "Enter the name of the configuration file you wish to load.\n"
191 "Accept the name shown to restore the configuration you\n"
192 "last retrieved. Leave blank to abort."),
193 load_config_help[] = N_(
195 "For various reasons, one may wish to keep several different kernel\n"
196 "configurations available on a single machine.\n"
198 "If you have saved a previous configuration in a file other than the\n"
199 "kernel's default, entering the name of the file here will allow you\n"
200 "to modify that configuration.\n"
202 "If you are uncertain, then you have probably never used alternate\n"
203 "configuration files. You should therefor leave this blank to abort.\n"),
204 save_config_text[] = N_(
205 "Enter a filename to which this configuration should be saved\n"
206 "as an alternate. Leave blank to abort."),
207 save_config_help[] = N_(
209 "For various reasons, one may wish to keep different kernel\n"
210 "configurations available on a single machine.\n"
212 "Entering a file name here will allow you to later retrieve, modify\n"
213 "and use the current configuration as an alternate to whatever\n"
214 "configuration options you have selected at that time.\n"
216 "If you are uncertain what all this means then you should probably\n"
217 "leave this blank.\n"),
220 "Search for CONFIG_ symbols and display their relations.\n"
221 "Regular expressions are allowed.\n"
222 "Example: search for \"^FOO\"\n"
224 "-----------------------------------------------------------------\n"
225 "Symbol: FOO [ = m]\n"
226 "Prompt: Foo bus is used to drive the bar HW\n"
227 "Defined at drivers/pci/Kconfig:47\n"
228 "Depends on: X86_LOCAL_APIC && X86_IO_APIC || IA64\n"
230 " -> Bus options (PCI, PCMCIA, EISA, MCA, ISA)\n"
231 " -> PCI support (PCI [ = y])\n"
232 " -> PCI access mode (<choice> [ = y])\n"
233 "Selects: LIBCRC32\n"
235 "-----------------------------------------------------------------\n"
236 "o The line 'Prompt:' shows the text used in the menu structure for\n"
237 " this CONFIG_ symbol\n"
238 "o The 'Defined at' line tell at what file / line number the symbol\n"
240 "o The 'Depends on:' line tell what symbols needs to be defined for\n"
241 " this symbol to be visible in the menu (selectable)\n"
242 "o The 'Location:' lines tell where in the menu structure this symbol\n"
244 " A location followed by a [ = y] indicate that this is a selectable\n"
245 " menu item - and current value is displayed inside brackets.\n"
246 "o The 'Selects:' line tell what symbol will be automatically\n"
247 " selected if this symbol is selected (y or m)\n"
248 "o The 'Selected by' line tell what symbol has selected this symbol\n"
250 "Only relevant lines are shown.\n"
253 "Examples: USB = > find all CONFIG_ symbols containing USB\n"
254 " ^USB => find all CONFIG_ symbols starting with USB\n"
255 " USB$ => find all CONFIG_ symbols ending with USB\n"
265 #define MAX_MENU_ITEMS 4096
266 static int show_all_items;
268 static struct menu *current_menu;
269 static int child_count;
270 static int single_menu_mode;
271 /* the window in which all information appears */
272 static WINDOW *main_window;
273 /* the largest size of the menu window */
274 static int mwin_max_lines;
275 static int mwin_max_cols;
276 /* the window in which we show option buttons */
277 static MENU *curses_menu;
278 static ITEM *curses_menu_items[MAX_MENU_ITEMS];
279 static struct mitem k_menu_items[MAX_MENU_ITEMS];
280 static int items_num;
281 static int global_exit;
282 /* the currently selected button */
283 const char *current_instructions = menu_instructions;
285 static void conf(struct menu *menu);
286 static void conf_choice(struct menu *menu);
287 static void conf_string(struct menu *menu);
288 static void conf_load(void);
289 static void conf_save(void);
290 static void show_help(struct menu *menu);
291 static int do_exit(void);
292 static void setup_windows(void);
293 static void search_conf(void);
295 typedef void (*function_key_handler_t)(int *key, struct menu *menu);
296 static void handle_f1(int *key, struct menu *current_item);
297 static void handle_f2(int *key, struct menu *current_item);
298 static void handle_f3(int *key, struct menu *current_item);
299 static void handle_f4(int *key, struct menu *current_item);
300 static void handle_f5(int *key, struct menu *current_item);
301 static void handle_f6(int *key, struct menu *current_item);
302 static void handle_f7(int *key, struct menu *current_item);
303 static void handle_f8(int *key, struct menu *current_item);
304 static void handle_f9(int *key, struct menu *current_item);
306 struct function_keys {
310 function_key_handler_t handler;
313 static const int function_keys_num = 9;
314 struct function_keys function_keys[] = {
319 .handler = handle_f1,
325 .handler = handle_f2,
331 .handler = handle_f3,
337 .handler = handle_f4,
343 .handler = handle_f5,
349 .handler = handle_f6,
355 .handler = handle_f7,
359 .func = "Sym Search",
361 .handler = handle_f8,
367 .handler = handle_f9,
371 static void print_function_line(void)
377 for (i = 0; i < function_keys_num; i++) {
378 wattrset(main_window, attributes[FUNCTION_HIGHLIGHT]);
379 mvwprintw(main_window, LINES-3, offset,
381 function_keys[i].key_str);
382 wattrset(main_window, attributes[FUNCTION_TEXT]);
383 offset += strlen(function_keys[i].key_str);
384 mvwprintw(main_window, LINES-3,
386 function_keys[i].func);
387 offset += strlen(function_keys[i].func) + skip;
389 wattrset(main_window, attributes[NORMAL]);
393 static void handle_f1(int *key, struct menu *current_item)
395 show_scroll_win(main_window,
396 _("README"), _(nconf_readme));
401 static void handle_f2(int *key, struct menu *current_item)
403 show_help(current_item);
408 static void handle_f3(int *key, struct menu *current_item)
410 show_scroll_win(main_window,
412 _(current_instructions));
417 static void handle_f4(int *key, struct menu *current_item)
419 int res = btn_dialog(main_window,
420 _("Show all symbols?"),
433 static void handle_f5(int *key, struct menu *current_item)
440 static void handle_f6(int *key, struct menu *current_item)
447 static void handle_f7(int *key, struct menu *current_item)
454 static void handle_f8(int *key, struct menu *current_item)
461 static void handle_f9(int *key, struct menu *current_item)
467 /* return != 0 to indicate the key was handles */
468 static int process_special_keys(int *key, struct menu *menu)
472 if (*key == KEY_RESIZE) {
477 for (i = 0; i < function_keys_num; i++) {
478 if (*key == KEY_F(function_keys[i].key) ||
479 *key == '0' + function_keys[i].key){
480 function_keys[i].handler(key, menu);
488 static void clean_items(void)
491 for (i = 0; curses_menu_items[i]; i++)
492 free_item(curses_menu_items[i]);
493 bzero(curses_menu_items, sizeof(curses_menu_items));
494 bzero(k_menu_items, sizeof(k_menu_items));
498 typedef enum {MATCH_TINKER_PATTERN_UP, MATCH_TINKER_PATTERN_DOWN,
499 FIND_NEXT_MATCH_DOWN, FIND_NEXT_MATCH_UP} match_f;
501 /* return the index of the matched item, or -1 if no such item exists */
502 static int get_mext_match(const char *match_str, match_f flag)
504 int match_start = item_index(current_item(curses_menu));
507 if (flag == FIND_NEXT_MATCH_DOWN)
509 else if (flag == FIND_NEXT_MATCH_UP)
513 index = (index + items_num) % items_num;
515 char *str = k_menu_items[index].str;
516 if (strcasestr(str, match_str) != 0)
518 if (flag == FIND_NEXT_MATCH_UP ||
519 flag == MATCH_TINKER_PATTERN_UP)
523 index = (index + items_num) % items_num;
524 if (index == match_start)
529 /* Make a new item. */
530 static void item_make(struct menu *menu, char tag, const char *fmt, ...)
534 if (items_num > MAX_MENU_ITEMS-1)
537 bzero(&k_menu_items[items_num], sizeof(k_menu_items[0]));
538 k_menu_items[items_num].tag = tag;
539 k_menu_items[items_num].usrptr = menu;
541 k_menu_items[items_num].is_visible =
542 menu_is_visible(menu);
544 k_menu_items[items_num].is_visible = 1;
547 vsnprintf(k_menu_items[items_num].str,
548 sizeof(k_menu_items[items_num].str),
552 if (!k_menu_items[items_num].is_visible)
553 memcpy(k_menu_items[items_num].str, "XXX", 3);
555 curses_menu_items[items_num] = new_item(
556 k_menu_items[items_num].str,
557 k_menu_items[items_num].str);
558 set_item_userptr(curses_menu_items[items_num],
559 &k_menu_items[items_num]);
561 if (!k_menu_items[items_num].is_visible)
562 item_opts_off(curses_menu_items[items_num], O_SELECTABLE);
566 curses_menu_items[items_num] = NULL;
569 /* very hackish. adds a string to the last item added */
570 static void item_add_str(const char *fmt, ...)
573 int index = items_num-1;
581 vsnprintf(new_str, sizeof(new_str), fmt, ap);
583 snprintf(tmp_str, sizeof(tmp_str), "%s%s",
584 k_menu_items[index].str, new_str);
585 strncpy(k_menu_items[index].str,
587 sizeof(k_menu_items[index].str));
589 free_item(curses_menu_items[index]);
590 curses_menu_items[index] = new_item(
591 k_menu_items[index].str,
592 k_menu_items[index].str);
593 set_item_userptr(curses_menu_items[index],
594 &k_menu_items[index]);
597 /* get the tag of the currently selected item */
598 static char item_tag(void)
603 cur = current_item(curses_menu);
606 mcur = (struct mitem *) item_userptr(cur);
610 static int curses_item_index(void)
612 return item_index(current_item(curses_menu));
615 static void *item_data(void)
620 cur = current_item(curses_menu);
623 mcur = (struct mitem *) item_userptr(cur);
628 static int item_is_tag(char tag)
630 return item_tag() == tag;
633 static char filename[PATH_MAX+1];
634 static char menu_backtitle[PATH_MAX+128];
635 static const char *set_config_filename(const char *config_filename)
640 sym = sym_lookup("KERNELVERSION", 0);
642 size = snprintf(menu_backtitle, sizeof(menu_backtitle),
643 _("%s - Linux Kernel v%s Configuration"),
644 config_filename, sym_get_string_value(sym));
645 if (size >= sizeof(menu_backtitle))
646 menu_backtitle[sizeof(menu_backtitle)-1] = '\0';
648 size = snprintf(filename, sizeof(filename), "%s", config_filename);
649 if (size >= sizeof(filename))
650 filename[sizeof(filename)-1] = '\0';
651 return menu_backtitle;
654 /* command = 0 is supress, 1 is restore */
655 static void supress_stdout(int command)
657 static FILE *org_stdout;
658 static FILE *org_stderr;
663 stdout = fopen("/dev/null", "a");
664 stderr = fopen("/dev/null", "a");
673 /* return = 0 means we are successful.
674 * -1 means go on doing what you were doing
676 static int do_exit(void)
679 if (!conf_get_changed()) {
683 res = btn_dialog(main_window,
684 _("Do you wish to save your "
685 "new kernel configuration?\n"
686 "<ESC> to cancel and resume nconfig."),
690 if (res == KEY_EXIT) {
695 /* if we got here, the user really wants to exit */
699 res = conf_write(filename);
704 _("Error during writing of the kernel "
706 "Your kernel configuration "
707 "changes were NOT saved."),
713 _("Configuration written to %s\n"
714 "End of Linux kernel configuration.\n"
715 "Execute 'make' to build the kernel or try"
716 " 'make help'."), filename);
727 _("Your kernel configuration changes were NOT saved."),
737 static void search_conf(void)
739 struct symbol **sym_arr;
741 char dialog_input_result[100];
745 dres = dialog_inputbox(main_window,
746 _("Search Configuration Parameter"),
747 _("Enter CONFIG_ (sub)string to search for "
748 "(with or without \"CONFIG\")"),
749 "", dialog_input_result, 99);
754 show_scroll_win(main_window,
755 _("Search Configuration"), search_help);
761 /* strip CONFIG_ if necessary */
762 dialog_input = dialog_input_result;
763 if (strncasecmp(dialog_input_result, "CONFIG_", 7) == 0)
766 sym_arr = sym_re_search(dialog_input);
767 res = get_relations_str(sym_arr);
769 show_scroll_win(main_window,
770 _("Search Results"), str_get(&res));
775 static void build_conf(struct menu *menu)
778 struct property *prop;
780 int type, tmp, doint = 2;
784 if (!menu || (!show_all_items && !menu_is_visible(menu)))
790 if (prop && menu != current_menu) {
791 const char *prompt = menu_get_prompt(menu);
792 enum prop_type ptype;
793 ptype = menu->prompt ? menu->prompt->type : P_UNKNOWN;
798 if (single_menu_mode) {
801 menu->data ? "-->" : "++>",
802 indent + 1, ' ', prompt);
809 if (single_menu_mode && menu->data)
824 item_make(menu, ':', "---%*c%s",
834 type = sym_get_type(sym);
835 if (sym_is_choice(sym)) {
836 struct symbol *def_sym = sym_get_choice_value(sym);
837 struct menu *def_menu = NULL;
840 for (child = menu->list; child; child = child->next) {
841 if (menu_is_visible(child) && child->sym == def_sym)
845 val = sym_get_tristate_value(sym);
846 if (sym_is_changable(sym)) {
849 item_make(menu, 't', "[%c]",
850 val == no ? ' ' : '*');
864 item_make(menu, 't', "<%c>", ch);
868 item_make(menu, def_menu ? 't' : ':', " ");
871 item_add_str("%*c%s", indent + 1,
872 ' ', _(menu_get_prompt(menu)));
875 item_add_str(" (%s)",
876 _(menu_get_prompt(def_menu)));
877 item_add_str(" --->");
878 if (def_menu->list) {
880 build_conf(def_menu);
887 if (menu == current_menu) {
889 "---%*c%s", indent + 1,
890 ' ', _(menu_get_prompt(menu)));
894 val = sym_get_tristate_value(sym);
895 if (sym_is_choice_value(sym) && val == yes) {
896 item_make(menu, ':', " ");
900 if (sym_is_changable(sym))
901 item_make(menu, 't', "[%c]",
902 val == no ? ' ' : '*');
904 item_make(menu, 't', "-%c-",
905 val == no ? ' ' : '*');
919 if (sym_is_changable(sym)) {
920 if (sym->rev_dep.tri == mod)
927 item_make(menu, 't', "-%c-", ch);
930 tmp = 2 + strlen(sym_get_string_value(sym));
931 item_make(menu, 's', " (%s)",
932 sym_get_string_value(sym));
933 tmp = indent - tmp + 4;
936 item_add_str("%*c%s%s", tmp, ' ',
937 _(menu_get_prompt(menu)),
938 (sym_has_value(sym) ||
939 !sym_is_changable(sym)) ? "" :
944 item_add_str("%*c%s%s", indent + 1, ' ',
945 _(menu_get_prompt(menu)),
946 (sym_has_value(sym) || !sym_is_changable(sym)) ?
948 if (menu->prompt && menu->prompt->type == P_MENU) {
949 item_add_str(" --->");
956 for (child = menu->list; child; child = child->next)
961 static void reset_menu(void)
963 unpost_menu(curses_menu);
967 /* adjust the menu to show this item.
968 * prefer not to scroll the menu if possible*/
969 static void center_item(int selected_index, int *last_top_row)
973 set_top_row(curses_menu, *last_top_row);
974 toprow = top_row(curses_menu);
975 if (selected_index < toprow ||
976 selected_index >= toprow+mwin_max_lines) {
977 toprow = max(selected_index-mwin_max_lines/2, 0);
978 if (toprow >= item_count(curses_menu)-mwin_max_lines)
979 toprow = item_count(curses_menu)-mwin_max_lines;
980 set_top_row(curses_menu, toprow);
982 set_current_item(curses_menu,
983 curses_menu_items[selected_index]);
984 *last_top_row = toprow;
985 post_menu(curses_menu);
986 refresh_all_windows(main_window);
989 /* this function assumes reset_menu has been called before */
990 static void show_menu(const char *prompt, const char *instructions,
991 int selected_index, int *last_top_row)
996 current_instructions = instructions;
999 wattrset(main_window, attributes[NORMAL]);
1000 print_in_middle(stdscr, 1, 0, COLS,
1002 attributes[MAIN_HEADING]);
1004 wattrset(main_window, attributes[MAIN_MENU_BOX]);
1005 box(main_window, 0, 0);
1006 wattrset(main_window, attributes[MAIN_MENU_HEADING]);
1007 mvwprintw(main_window, 0, 3, " %s ", prompt);
1008 wattrset(main_window, attributes[NORMAL]);
1010 set_menu_items(curses_menu, curses_menu_items);
1012 /* position the menu at the middle of the screen */
1013 scale_menu(curses_menu, &maxy, &maxx);
1014 maxx = min(maxx, mwin_max_cols-2);
1015 maxy = mwin_max_lines;
1016 menu_window = derwin(main_window,
1020 (mwin_max_cols-maxx)/2);
1021 keypad(menu_window, TRUE);
1022 set_menu_win(curses_menu, menu_window);
1023 set_menu_sub(curses_menu, menu_window);
1025 /* must reassert this after changing items, otherwise returns to a
1028 set_menu_format(curses_menu, maxy, 1);
1029 center_item(selected_index, last_top_row);
1030 set_menu_format(curses_menu, maxy, 1);
1032 print_function_line();
1035 post_menu(curses_menu);
1036 refresh_all_windows(main_window);
1039 static void adj_match_dir(match_f *match_direction)
1041 if (*match_direction == FIND_NEXT_MATCH_DOWN)
1043 MATCH_TINKER_PATTERN_DOWN;
1044 else if (*match_direction == FIND_NEXT_MATCH_UP)
1046 MATCH_TINKER_PATTERN_UP;
1047 /* else, do no change.. */
1053 match_f match_direction;
1057 /* Return 0 means I have handled the key. In such a case, ans should hold the
1058 * item to center, or -1 otherwise.
1061 static int do_match(int key, struct match_state *state, int *ans)
1063 char c = (char) key;
1064 int terminate_search = 0;
1066 if (key == '/' || (state->in_search && key == 27)) {
1070 state->in_search = 1-state->in_search;
1071 bzero(state->pattern, sizeof(state->pattern));
1072 state->match_direction = MATCH_TINKER_PATTERN_DOWN;
1074 } else if (!state->in_search)
1077 if (isalnum(c) || isgraph(c) || c == ' ') {
1078 state->pattern[strlen(state->pattern)] = c;
1079 state->pattern[strlen(state->pattern)] = '\0';
1080 adj_match_dir(&state->match_direction);
1081 *ans = get_mext_match(state->pattern,
1082 state->match_direction);
1083 } else if (key == KEY_DOWN) {
1084 state->match_direction = FIND_NEXT_MATCH_DOWN;
1085 *ans = get_mext_match(state->pattern,
1086 state->match_direction);
1087 } else if (key == KEY_UP) {
1088 state->match_direction = FIND_NEXT_MATCH_UP;
1089 *ans = get_mext_match(state->pattern,
1090 state->match_direction);
1091 } else if (key == KEY_BACKSPACE || key == 127) {
1092 state->pattern[strlen(state->pattern)-1] = '\0';
1093 adj_match_dir(&state->match_direction);
1095 terminate_search = 1;
1097 if (terminate_search) {
1098 state->in_search = 0;
1099 bzero(state->pattern, sizeof(state->pattern));
1108 static void conf(struct menu *menu)
1110 struct menu *submenu = 0;
1111 const char *prompt = menu_get_prompt(menu);
1113 struct menu *active_menu = NULL;
1115 int current_index = 0;
1116 int last_top_row = 0;
1117 struct match_state match_state = {
1119 .match_direction = MATCH_TINKER_PATTERN_DOWN,
1123 while (!global_exit) {
1125 current_menu = menu;
1130 show_menu(prompt ? _(prompt) : _("Main Menu"),
1131 _(menu_instructions),
1132 current_index, &last_top_row);
1133 keypad((menu_win(curses_menu)), TRUE);
1134 while (!global_exit) {
1135 if (match_state.in_search) {
1137 "searching: %s", match_state.pattern);
1140 refresh_all_windows(main_window);
1141 res = wgetch(menu_win(curses_menu));
1144 if (do_match(res, &match_state, ¤t_index) == 0) {
1145 if (current_index != -1)
1146 center_item(current_index,
1150 if (process_special_keys(&res,
1151 (struct menu *) item_data()))
1155 menu_driver(curses_menu, REQ_DOWN_ITEM);
1158 menu_driver(curses_menu, REQ_UP_ITEM);
1161 menu_driver(curses_menu, REQ_SCR_DPAGE);
1164 menu_driver(curses_menu, REQ_SCR_UPAGE);
1167 menu_driver(curses_menu, REQ_FIRST_ITEM);
1170 menu_driver(curses_menu, REQ_LAST_ITEM);
1174 show_help((struct menu *) item_data());
1177 if (res == 10 || res == 27 ||
1178 res == 32 || res == 'n' || res == 'y' ||
1179 res == KEY_LEFT || res == KEY_RIGHT ||
1182 refresh_all_windows(main_window);
1185 refresh_all_windows(main_window);
1187 if (res == 27 || (menu != &rootmenu && res == KEY_LEFT))
1190 /* remember location in the menu */
1191 last_top_row = top_row(curses_menu);
1192 current_index = curses_item_index();
1197 submenu = (struct menu *) item_data();
1198 active_menu = (struct menu *)item_data();
1199 if (!submenu || !menu_is_visible(submenu))
1208 if (item_is_tag('t'))
1209 sym_toggle_tristate_value(sym);
1210 else if (item_is_tag('m'))
1214 case 10: /* ENTER WAS PRESSED */
1215 switch (item_tag()) {
1217 if (single_menu_mode)
1219 (void *) (long) !submenu->data;
1224 if (sym_is_choice(sym) &&
1225 sym_get_tristate_value(sym) == yes)
1226 conf_choice(submenu);
1227 else if (submenu->prompt &&
1228 submenu->prompt->type == P_MENU)
1231 sym_toggle_tristate_value(sym);
1234 conf_string(submenu);
1239 if (item_is_tag('t')) {
1240 if (sym_set_tristate_value(sym, yes))
1242 if (sym_set_tristate_value(sym, mod))
1243 btn_dialog(main_window, setmod_text, 0);
1247 if (item_is_tag('t'))
1248 sym_set_tristate_value(sym, no);
1251 if (item_is_tag('t'))
1252 sym_set_tristate_value(sym, mod);
1258 static void show_help(struct menu *menu)
1260 struct gstr help = str_new();
1262 if (menu && menu->sym && menu_has_help(menu)) {
1263 if (menu->sym->name) {
1264 str_printf(&help, "CONFIG_%s:\n\n", menu->sym->name);
1265 str_append(&help, _(menu_get_help(menu)));
1266 str_append(&help, "\n");
1267 get_symbol_str(&help, menu->sym);
1269 str_append(&help, _(menu_get_help(menu)));
1272 str_append(&help, nohelp_text);
1274 show_scroll_win(main_window, _(menu_get_prompt(menu)), str_get(&help));
1278 static void conf_choice(struct menu *menu)
1280 const char *prompt = _(menu_get_prompt(menu));
1281 struct menu *child = 0;
1282 struct symbol *active;
1283 int selected_index = 0;
1284 int last_top_row = 0;
1286 struct match_state match_state = {
1288 .match_direction = MATCH_TINKER_PATTERN_DOWN,
1292 active = sym_get_choice_value(menu->sym);
1293 /* this is mostly duplicated from the conf() function. */
1294 while (!global_exit) {
1297 for (i = 0, child = menu->list; child; child = child->next) {
1298 if (!show_all_items && !menu_is_visible(child))
1301 if (child->sym == sym_get_choice_value(menu->sym))
1302 item_make(child, ':', "<X> %s",
1303 _(menu_get_prompt(child)));
1305 item_make(child, ':', " %s",
1306 _(menu_get_prompt(child)));
1307 if (child->sym == active){
1308 last_top_row = top_row(curses_menu);
1313 show_menu(prompt ? _(prompt) : _("Choice Menu"),
1314 _(radiolist_instructions),
1317 while (!global_exit) {
1318 if (match_state.in_search) {
1319 mvprintw(0, 0, "searching: %s",
1320 match_state.pattern);
1323 refresh_all_windows(main_window);
1324 res = wgetch(menu_win(curses_menu));
1327 if (do_match(res, &match_state, &selected_index) == 0) {
1328 if (selected_index != -1)
1329 center_item(selected_index,
1333 if (process_special_keys(
1335 (struct menu *) item_data()))
1339 menu_driver(curses_menu, REQ_DOWN_ITEM);
1342 menu_driver(curses_menu, REQ_UP_ITEM);
1345 menu_driver(curses_menu, REQ_SCR_DPAGE);
1348 menu_driver(curses_menu, REQ_SCR_UPAGE);
1351 menu_driver(curses_menu, REQ_FIRST_ITEM);
1354 menu_driver(curses_menu, REQ_LAST_ITEM);
1358 show_help((struct menu *) item_data());
1361 if (res == 10 || res == 27 || res == ' ' ||
1365 refresh_all_windows(main_window);
1367 /* if ESC or left */
1368 if (res == 27 || res == KEY_LEFT)
1371 child = item_data();
1372 if (!child || !menu_is_visible(child))
1378 sym_set_tristate_value(child->sym, yes);
1383 active = child->sym;
1391 static void conf_string(struct menu *menu)
1393 const char *prompt = menu_get_prompt(menu);
1394 char dialog_input_result[256];
1398 const char *heading;
1400 switch (sym_get_type(menu->sym)) {
1402 heading = _(inputbox_instructions_int);
1405 heading = _(inputbox_instructions_hex);
1408 heading = _(inputbox_instructions_string);
1411 heading = _("Internal nconf error!");
1413 res = dialog_inputbox(main_window,
1414 prompt ? _(prompt) : _("Main Menu"),
1416 sym_get_string_value(menu->sym),
1417 dialog_input_result,
1418 sizeof(dialog_input_result));
1421 if (sym_set_string_value(menu->sym,
1422 dialog_input_result))
1424 btn_dialog(main_window,
1425 _("You have made an invalid entry."), 0);
1436 static void conf_load(void)
1438 char dialog_input_result[256];
1441 res = dialog_inputbox(main_window,
1442 NULL, load_config_text,
1444 dialog_input_result,
1445 sizeof(dialog_input_result));
1448 if (!dialog_input_result[0])
1450 if (!conf_read(dialog_input_result)) {
1451 set_config_filename(dialog_input_result);
1452 sym_set_change_count(1);
1455 btn_dialog(main_window, _("File does not exist!"), 0);
1458 show_scroll_win(main_window,
1459 _("Load Alternate Configuration"),
1468 static void conf_save(void)
1470 char dialog_input_result[256];
1473 res = dialog_inputbox(main_window,
1474 NULL, save_config_text,
1476 dialog_input_result,
1477 sizeof(dialog_input_result));
1480 if (!dialog_input_result[0])
1483 res = conf_write(dialog_input_result);
1487 sprintf(buf, "%s %s",
1488 _("configuration file saved to: "),
1489 dialog_input_result);
1490 btn_dialog(main_window,
1492 set_config_filename(dialog_input_result);
1495 btn_dialog(main_window, _("Can't create file! "
1496 "Probably a nonexistent directory."),
1500 show_scroll_win(main_window,
1501 _("Save Alternate Configuration"),
1510 void setup_windows(void)
1512 if (main_window != NULL)
1513 delwin(main_window);
1515 /* set up the menu and menu window */
1516 main_window = newwin(LINES-2, COLS-2, 2, 1);
1517 keypad(main_window, TRUE);
1518 mwin_max_lines = LINES-7;
1519 mwin_max_cols = COLS-6;
1521 /* panels order is from bottom to top */
1522 new_panel(main_window);
1525 int main(int ac, char **av)
1529 setlocale(LC_ALL, "");
1530 bindtextdomain(PACKAGE, LOCALEDIR);
1531 textdomain(PACKAGE);
1536 mode = getenv("NCONFIG_MODE");
1538 if (!strcasecmp(mode, "single_menu"))
1539 single_menu_mode = 1;
1542 /* Initialize curses */
1544 /* set color theme */
1549 keypad(stdscr, TRUE);
1552 if (COLS < 75 || LINES < 20) {
1554 printf("Your terminal should have at "
1555 "least 20 lines and 75 columns\n");
1559 notimeout(stdscr, FALSE);
1563 curses_menu = new_menu(curses_menu_items);
1564 menu_opts_off(curses_menu, O_SHOWDESC);
1565 menu_opts_on(curses_menu, O_SHOWMATCH);
1566 menu_opts_on(curses_menu, O_ONEVALUE);
1567 menu_opts_on(curses_menu, O_NONCYCLIC);
1568 menu_opts_on(curses_menu, O_IGNORECASE);
1569 set_menu_mark(curses_menu, " ");
1570 set_menu_fore(curses_menu, attributes[MAIN_MENU_FORE]);
1571 set_menu_back(curses_menu, attributes[MAIN_MENU_BACK]);
1572 set_menu_grey(curses_menu, attributes[MAIN_MENU_GREY]);
1574 set_config_filename(conf_get_configname());
1577 /* check for KEY_FUNC(1) */
1578 if (has_key(KEY_F(1)) == FALSE) {
1579 show_scroll_win(main_window,
1581 _(menu_no_f_instructions));
1585 while (!global_exit) {
1587 if (!global_exit && do_exit() == 0)
1590 /* ok, we are done */
1591 unpost_menu(curses_menu);
1592 free_menu(curses_menu);
1593 delwin(main_window);