1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com>
5 * Derived from menuconfig.
18 static const char nconf_global_help[] =
21 "o Global help: Unless in a data entry window, pressing <F1> will give \n"
22 " you the global help window, which you are just reading.\n"
24 "o A short version of the global help is available by pressing <F3>.\n"
26 "o Local help: To get help related to the current menu entry, use any\n"
27 " of <?> <h>, or if in a data entry window then press <F1>.\n"
32 "This interface lets you select features and parameters for the kernel\n"
33 "build. Kernel features can either be built-in, modularized, or removed.\n"
34 "Parameters must be entered as text or decimal or hexadecimal numbers.\n"
36 "Menu entries beginning with following braces represent features that\n"
37 " [ ] can be built in or removed\n"
38 " < > can be built in, modularized or removed\n"
39 " { } can be built in or modularized, are selected by another feature\n"
40 " - - are selected by another feature\n"
41 " XXX cannot be selected. Symbol Info <F2> tells you why.\n"
42 "*, M or whitespace inside braces means to build in, build as a module\n"
43 "or to exclude the feature respectively.\n"
45 "To change any of these features, highlight it with the movement keys\n"
46 "listed below and press <y> to build it in, <m> to make it a module or\n"
47 "<n> to remove it. You may press the <Space> key to cycle through the\n"
48 "available options.\n"
50 "A trailing \"--->\" designates a submenu, a trailing \"----\" an\n"
53 "Menu navigation keys\n"
54 "----------------------------------------------------------------------\n"
55 "Linewise up <Up> <k>\n"
56 "Linewise down <Down> <j>\n"
57 "Pagewise up <Page Up>\n"
58 "Pagewise down <Page Down>\n"
59 "First entry <Home>\n"
61 "Enter a submenu <Right> <Enter>\n"
62 "Go back to parent menu <Left> <Esc> <F5>\n"
63 "Close a help window <Enter> <Esc> <F5>\n"
64 "Close entry window, apply <Enter>\n"
65 "Close entry window, forget <Esc> <F5>\n"
66 "Start incremental, case-insensitive search for STRING in menu entries,\n"
67 " no regex support, STRING is displayed in upper left corner\n"
69 " Remove last character <Backspace>\n"
70 " Jump to next hit <Down>\n"
71 " Jump to previous hit <Up>\n"
72 "Exit menu search mode </> <Esc>\n"
73 "Search for configuration variables with or without leading CONFIG_\n"
74 " <F8>RegExpr<Enter>\n"
75 "Verbose search help <F8><F1>\n"
76 "----------------------------------------------------------------------\n"
78 "Unless in a data entry window, key <1> may be used instead of <F1>,\n"
79 "<2> instead of <F2>, etc.\n"
82 "Radiolist (Choice list)\n"
83 "-----------------------\n"
84 "Use the movement keys listed above to select the option you wish to set\n"
85 "and press <Space>.\n"
90 "Enter the requested information and press <Enter>. Hexadecimal values\n"
91 "may be entered without the \"0x\" prefix.\n"
94 "Text Box (Help Window)\n"
95 "----------------------\n"
96 "Use movement keys as listed in table above.\n"
98 "Press any of <Enter> <Esc> <q> <F5> <F9> to exit.\n"
101 "Alternate configuration files\n"
102 "-----------------------------\n"
103 "nconfig supports switching between different configurations.\n"
104 "Press <F6> to save your current configuration. Press <F7> and enter\n"
105 "a file name to load a previously saved configuration.\n"
108 "Terminal configuration\n"
109 "----------------------\n"
110 "If you use nconfig in a xterm window, make sure your TERM environment\n"
111 "variable specifies a terminal configuration which supports at least\n"
112 "16 colors. Otherwise nconfig will look rather bad.\n"
114 "If the \"stty size\" command reports the current terminalsize correctly,\n"
115 "nconfig will adapt to sizes larger than the traditional 80x25 \"standard\"\n"
116 "and display longer menus properly.\n"
121 "If you prefer to have all of the menu entries listed in a single menu,\n"
122 "rather than the default multimenu hierarchy, run nconfig with\n"
123 "NCONFIG_MODE environment variable set to single_menu. Example:\n"
125 "make NCONFIG_MODE=single_menu nconfig\n"
127 "<Enter> will then unfold the appropriate category, or fold it if it\n"
128 "is already unfolded. Folded menu entries will be designated by a\n"
129 "leading \"++>\" and unfolded entries by a leading \"-->\".\n"
131 "Note that this mode can eventually be a little more CPU expensive than\n"
132 "the default mode, especially with a larger number of unfolded submenus.\n"
134 menu_no_f_instructions[] =
135 "Legend: [*] built-in [ ] excluded <M> module < > module capable.\n"
136 "Submenus are designated by a trailing \"--->\", empty ones by \"----\".\n"
138 "Use the following keys to navigate the menus:\n"
139 "Move up or down with <Up> and <Down>.\n"
140 "Enter a submenu with <Enter> or <Right>.\n"
141 "Exit a submenu to its parent menu with <Esc> or <Left>.\n"
142 "Pressing <y> includes, <n> excludes, <m> modularizes features.\n"
143 "Pressing <Space> cycles through the available options.\n"
144 "To search for menu entries press </>.\n"
145 "<Esc> always leaves the current window.\n"
147 "You do not have function keys support.\n"
148 "Press <1> instead of <F1>, <2> instead of <F2>, etc.\n"
149 "For verbose global help use key <1>.\n"
150 "For help related to the current menu entry press <?> or <h>.\n",
151 menu_instructions[] =
152 "Legend: [*] built-in [ ] excluded <M> module < > module capable.\n"
153 "Submenus are designated by a trailing \"--->\", empty ones by \"----\".\n"
155 "Use the following keys to navigate the menus:\n"
156 "Move up or down with <Up> or <Down>.\n"
157 "Enter a submenu with <Enter> or <Right>.\n"
158 "Exit a submenu to its parent menu with <Esc> or <Left>.\n"
159 "Pressing <y> includes, <n> excludes, <m> modularizes features.\n"
160 "Pressing <Space> cycles through the available options.\n"
161 "To search for menu entries press </>.\n"
162 "<Esc> always leaves the current window.\n"
164 "Pressing <1> may be used instead of <F1>, <2> instead of <F2>, etc.\n"
165 "For verbose global help press <F1>.\n"
166 "For help related to the current menu entry press <?> or <h>.\n",
167 radiolist_instructions[] =
168 "Press <Up>, <Down>, <Home> or <End> to navigate a radiolist, select\n"
170 "For help related to the current entry press <?> or <h>.\n"
171 "For global help press <F1>.\n",
172 inputbox_instructions_int[] =
173 "Please enter a decimal value.\n"
174 "Fractions will not be accepted.\n"
175 "Press <Enter> to apply, <Esc> to cancel.",
176 inputbox_instructions_hex[] =
177 "Please enter a hexadecimal value.\n"
178 "Press <Enter> to apply, <Esc> to cancel.",
179 inputbox_instructions_string[] =
180 "Please enter a string value.\n"
181 "Press <Enter> to apply, <Esc> to cancel.",
183 "This feature depends on another feature which has been configured as a\n"
184 "module. As a result, the current feature will be built as a module too.",
186 "Enter the name of the configuration file you wish to load.\n"
187 "Accept the name shown to restore the configuration you last\n"
188 "retrieved. Leave empty to abort.",
190 "For various reasons, one may wish to keep several different\n"
191 "configurations available on a single machine.\n"
193 "If you have saved a previous configuration in a file other than the\n"
194 "default one, entering its name here will allow you to load and modify\n"
195 "that configuration.\n"
197 "Leave empty to abort.\n",
199 "Enter a filename to which this configuration should be saved\n"
200 "as an alternate. Leave empty to abort.",
202 "For various reasons, one may wish to keep several different\n"
203 "configurations available on a single machine.\n"
205 "Entering a file name here will allow you to later retrieve, modify\n"
206 "and use the current configuration as an alternate to whatever\n"
207 "configuration options you have selected at that time.\n"
209 "Leave empty to abort.\n",
211 "Search for symbols (configuration variable names CONFIG_*) and display\n"
212 "their relations. Regular expressions are supported.\n"
213 "Example: Search for \"^FOO\".\n"
215 "-----------------------------------------------------------------\n"
216 "Symbol: FOO [ = m]\n"
217 "Prompt: Foo bus is used to drive the bar HW\n"
218 "Defined at drivers/pci/Kconfig:47\n"
219 "Depends on: X86_LOCAL_APIC && X86_IO_APIC || IA64\n"
221 " -> Bus options (PCI, PCMCIA, EISA, ISA)\n"
222 " -> PCI support (PCI [ = y])\n"
223 "(1) -> PCI access mode (<choice> [ = y])\n"
224 "Selects: LIBCRC32\n"
226 "-----------------------------------------------------------------\n"
227 "o The line 'Prompt:' shows the text displayed for this symbol in\n"
228 " the menu hierarchy.\n"
229 "o The 'Defined at' line tells at what file / line number the symbol is\n"
231 "o The 'Depends on:' line lists symbols that need to be defined for\n"
232 " this symbol to be visible and selectable in the menu.\n"
233 "o The 'Location:' lines tell, where in the menu structure this symbol\n"
235 " A location followed by a [ = y] indicates that this is\n"
236 " a selectable menu item, and the current value is displayed inside\n"
238 " Press the key in the (#) prefix to jump directly to that\n"
239 " location. You will be returned to the current search results\n"
240 " after exiting this new menu.\n"
241 "o The 'Selects:' line tells, what symbol will be automatically selected\n"
242 " if this symbol is selected (y or m).\n"
243 "o The 'Selected by' line tells what symbol has selected this symbol.\n"
245 "Only relevant lines are shown.\n"
248 "USB => find all symbols containing USB\n"
249 "^USB => find all symbols starting with USB\n"
250 "USB$ => find all symbols ending with USB\n"
260 #define MAX_MENU_ITEMS 4096
261 static int show_all_items;
263 static struct menu *current_menu;
264 static int child_count;
265 static int single_menu_mode;
266 /* the window in which all information appears */
267 static WINDOW *main_window;
268 /* the largest size of the menu window */
269 static int mwin_max_lines;
270 static int mwin_max_cols;
271 /* the window in which we show option buttons */
272 static MENU *curses_menu;
273 static ITEM *curses_menu_items[MAX_MENU_ITEMS];
274 static struct mitem k_menu_items[MAX_MENU_ITEMS];
275 static unsigned int items_num;
276 static int global_exit;
277 /* the currently selected button */
278 static const char *current_instructions = menu_instructions;
280 static char *dialog_input_result;
281 static int dialog_input_result_len;
282 static int jump_key_char;
284 static void selected_conf(struct menu *menu, struct menu *active_menu);
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 static 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,
361 .handler = handle_f8,
367 .handler = handle_f9,
371 static void print_function_line(void)
376 int lines = getmaxy(stdscr);
378 for (i = 0; i < function_keys_num; i++) {
379 wattrset(main_window, attr_function_highlight);
380 mvwprintw(main_window, lines-3, offset,
382 function_keys[i].key_str);
383 wattrset(main_window, attr_function_text);
384 offset += strlen(function_keys[i].key_str);
385 mvwprintw(main_window, lines-3,
387 function_keys[i].func);
388 offset += strlen(function_keys[i].func) + skip;
390 wattrset(main_window, attr_normal);
394 static void handle_f1(int *key, struct menu *current_item)
396 show_scroll_win(main_window,
397 "Global help", nconf_global_help);
402 static void handle_f2(int *key, struct menu *current_item)
404 show_help(current_item);
409 static void handle_f3(int *key, struct menu *current_item)
411 show_scroll_win(main_window,
413 current_instructions);
418 static void handle_f4(int *key, struct menu *current_item)
420 int res = btn_dialog(main_window,
434 static void handle_f5(int *key, struct menu *current_item)
441 static void handle_f6(int *key, struct menu *current_item)
448 static void handle_f7(int *key, struct menu *current_item)
455 static void handle_f8(int *key, struct menu *current_item)
462 static void handle_f9(int *key, struct menu *current_item)
468 /* return != 0 to indicate the key was handles */
469 static int process_special_keys(int *key, struct menu *menu)
473 if (*key == KEY_RESIZE) {
478 for (i = 0; i < function_keys_num; i++) {
479 if (*key == KEY_F(function_keys[i].key) ||
480 *key == '0' + function_keys[i].key){
481 function_keys[i].handler(key, menu);
489 static void clean_items(void)
492 for (i = 0; curses_menu_items[i]; i++)
493 free_item(curses_menu_items[i]);
494 bzero(curses_menu_items, sizeof(curses_menu_items));
495 bzero(k_menu_items, sizeof(k_menu_items));
499 typedef enum {MATCH_TINKER_PATTERN_UP, MATCH_TINKER_PATTERN_DOWN,
500 FIND_NEXT_MATCH_DOWN, FIND_NEXT_MATCH_UP} match_f;
502 /* return the index of the matched item, or -1 if no such item exists */
503 static int get_mext_match(const char *match_str, match_f flag)
505 int match_start, index;
507 /* Do not search if the menu is empty (i.e. items_num == 0) */
508 match_start = item_index(current_item(curses_menu));
509 if (match_start == ERR)
512 if (flag == FIND_NEXT_MATCH_DOWN)
514 else if (flag == FIND_NEXT_MATCH_UP)
517 match_start = (match_start + items_num) % items_num;
520 char *str = k_menu_items[index].str;
521 if (strcasestr(str, match_str) != NULL)
523 if (flag == FIND_NEXT_MATCH_UP ||
524 flag == MATCH_TINKER_PATTERN_UP)
528 index = (index + items_num) % items_num;
529 if (index == match_start)
534 /* Make a new item. */
535 static void item_make(struct menu *menu, char tag, const char *fmt, ...)
539 if (items_num > MAX_MENU_ITEMS-1)
542 bzero(&k_menu_items[items_num], sizeof(k_menu_items[0]));
543 k_menu_items[items_num].tag = tag;
544 k_menu_items[items_num].usrptr = menu;
546 k_menu_items[items_num].is_visible =
547 menu_is_visible(menu);
549 k_menu_items[items_num].is_visible = 1;
552 vsnprintf(k_menu_items[items_num].str,
553 sizeof(k_menu_items[items_num].str),
557 if (!k_menu_items[items_num].is_visible)
558 memcpy(k_menu_items[items_num].str, "XXX", 3);
560 curses_menu_items[items_num] = new_item(
561 k_menu_items[items_num].str,
562 k_menu_items[items_num].str);
563 set_item_userptr(curses_menu_items[items_num],
564 &k_menu_items[items_num]);
566 if (!k_menu_items[items_num].is_visible)
567 item_opts_off(curses_menu_items[items_num], O_SELECTABLE);
571 curses_menu_items[items_num] = NULL;
574 /* very hackish. adds a string to the last item added */
575 static void item_add_str(const char *fmt, ...)
578 int index = items_num-1;
586 vsnprintf(new_str, sizeof(new_str), fmt, ap);
588 snprintf(tmp_str, sizeof(tmp_str), "%s%s",
589 k_menu_items[index].str, new_str);
590 strncpy(k_menu_items[index].str,
592 sizeof(k_menu_items[index].str));
594 free_item(curses_menu_items[index]);
595 curses_menu_items[index] = new_item(
596 k_menu_items[index].str,
597 k_menu_items[index].str);
598 set_item_userptr(curses_menu_items[index],
599 &k_menu_items[index]);
602 /* get the tag of the currently selected item */
603 static char item_tag(void)
608 cur = current_item(curses_menu);
611 mcur = (struct mitem *) item_userptr(cur);
615 static int curses_item_index(void)
617 return item_index(current_item(curses_menu));
620 static void *item_data(void)
625 cur = current_item(curses_menu);
628 mcur = (struct mitem *) item_userptr(cur);
633 static int item_is_tag(char tag)
635 return item_tag() == tag;
638 static char filename[PATH_MAX+1];
639 static char menu_backtitle[PATH_MAX+128];
640 static void set_config_filename(const char *config_filename)
642 snprintf(menu_backtitle, sizeof(menu_backtitle), "%s - %s",
643 config_filename, rootmenu.prompt->text);
645 snprintf(filename, sizeof(filename), "%s", config_filename);
648 /* return = 0 means we are successful.
649 * -1 means go on doing what you were doing
651 static int do_exit(void)
654 if (!conf_get_changed()) {
658 res = btn_dialog(main_window,
659 "Do you wish to save your new configuration?\n"
660 "<ESC> to cancel and resume nconfig.",
664 if (res == KEY_EXIT) {
669 /* if we got here, the user really wants to exit */
672 res = conf_write(filename);
676 "Error during writing of configuration.\n"
677 "Your configuration changes were NOT saved.",
680 conf_write_autoconf(0);
685 "Your configuration changes were NOT saved.",
695 struct list_head *head;
699 static int next_jump_key(int key)
701 if (key < '1' || key > '9')
712 static int handle_search_keys(int key, size_t start, size_t end, void *_data)
714 struct search_data *data = _data;
715 struct jump_key *pos;
718 if (key < '1' || key > '9')
721 list_for_each_entry(pos, data->head, entries) {
722 index = next_jump_key(index);
724 if (pos->offset < start)
727 if (pos->offset >= end)
731 data->target = pos->target;
739 int get_jump_key_char(void)
741 jump_key_char = next_jump_key(jump_key_char);
743 return jump_key_char;
746 static void search_conf(void)
748 struct symbol **sym_arr;
752 int dres, vscroll = 0, hscroll = 0;
756 str_printf( &title, "Enter (sub)string or regexp to search for "
757 "(with or without \"%s\")", CONFIG_);
760 dres = dialog_inputbox(main_window,
761 "Search Configuration Parameter",
763 "", &dialog_input_result, &dialog_input_result_len);
768 show_scroll_win(main_window,
769 "Search Configuration", search_help);
776 /* strip the prefix if necessary */
777 dialog_input = dialog_input_result;
778 if (strncasecmp(dialog_input_result, CONFIG_, strlen(CONFIG_)) == 0)
779 dialog_input += strlen(CONFIG_);
781 sym_arr = sym_re_search(dialog_input);
785 struct search_data data = {
790 res = get_relations_str(sym_arr, &head);
791 dres = show_scroll_win_ext(main_window,
792 "Search Results", str_get(&res),
794 handle_search_keys, &data);
796 if (dres >= '1' && dres <= '9') {
797 assert(data.target != NULL);
798 selected_conf(data.target->parent, data.target);
808 static void build_conf(struct menu *menu)
811 struct property *prop;
813 int type, tmp, doint = 2;
817 if (!menu || (!show_all_items && !menu_is_visible(menu)))
823 if (prop && menu != current_menu) {
824 const char *prompt = menu_get_prompt(menu);
825 enum prop_type ptype;
826 ptype = menu->prompt ? menu->prompt->type : P_UNKNOWN;
830 if (single_menu_mode) {
833 menu->data ? "-->" : "++>",
834 indent + 1, ' ', prompt);
838 indent + 1, ' ', prompt,
839 menu_is_empty(menu) ? "----" : "--->");
841 if (single_menu_mode && menu->data)
856 item_make(menu, ':', "---%*c%s",
866 type = sym_get_type(sym);
867 if (sym_is_choice(sym)) {
868 struct symbol *def_sym = sym_get_choice_value(sym);
869 struct menu *def_menu = NULL;
872 for (child = menu->list; child; child = child->next) {
873 if (menu_is_visible(child) && child->sym == def_sym)
877 val = sym_get_tristate_value(sym);
878 if (sym_is_changeable(sym)) {
881 item_make(menu, 't', "[%c]",
882 val == no ? ' ' : '*');
896 item_make(menu, 't', "<%c>", ch);
900 item_make(menu, def_menu ? 't' : ':', " ");
903 item_add_str("%*c%s", indent + 1,
904 ' ', menu_get_prompt(menu));
907 item_add_str(" (%s)",
908 menu_get_prompt(def_menu));
909 item_add_str(" --->");
910 if (def_menu->list) {
912 build_conf(def_menu);
919 if (menu == current_menu) {
921 "---%*c%s", indent + 1,
922 ' ', menu_get_prompt(menu));
926 val = sym_get_tristate_value(sym);
927 if (sym_is_choice_value(sym) && val == yes) {
928 item_make(menu, ':', " ");
932 if (sym_is_changeable(sym))
933 item_make(menu, 't', "[%c]",
934 val == no ? ' ' : '*');
936 item_make(menu, 't', "-%c-",
937 val == no ? ' ' : '*');
951 if (sym_is_changeable(sym)) {
952 if (sym->rev_dep.tri == mod)
959 item_make(menu, 't', "-%c-", ch);
962 tmp = 2 + strlen(sym_get_string_value(sym));
963 item_make(menu, 's', " (%s)",
964 sym_get_string_value(sym));
965 tmp = indent - tmp + 4;
968 item_add_str("%*c%s%s", tmp, ' ',
969 menu_get_prompt(menu),
970 (sym_has_value(sym) ||
971 !sym_is_changeable(sym)) ? "" :
976 item_add_str("%*c%s%s", indent + 1, ' ',
977 menu_get_prompt(menu),
978 (sym_has_value(sym) || !sym_is_changeable(sym)) ?
980 if (menu->prompt && menu->prompt->type == P_MENU) {
981 item_add_str(" %s", menu_is_empty(menu) ? "----" : "--->");
988 for (child = menu->list; child; child = child->next)
993 static void reset_menu(void)
995 unpost_menu(curses_menu);
999 /* adjust the menu to show this item.
1000 * prefer not to scroll the menu if possible*/
1001 static void center_item(int selected_index, int *last_top_row)
1005 set_top_row(curses_menu, *last_top_row);
1006 toprow = top_row(curses_menu);
1007 if (selected_index < toprow ||
1008 selected_index >= toprow+mwin_max_lines) {
1009 toprow = max(selected_index-mwin_max_lines/2, 0);
1010 if (toprow >= item_count(curses_menu)-mwin_max_lines)
1011 toprow = item_count(curses_menu)-mwin_max_lines;
1012 set_top_row(curses_menu, toprow);
1014 set_current_item(curses_menu,
1015 curses_menu_items[selected_index]);
1016 *last_top_row = toprow;
1017 post_menu(curses_menu);
1018 refresh_all_windows(main_window);
1021 /* this function assumes reset_menu has been called before */
1022 static void show_menu(const char *prompt, const char *instructions,
1023 int selected_index, int *last_top_row)
1026 WINDOW *menu_window;
1028 current_instructions = instructions;
1031 print_in_middle(stdscr, 1, getmaxx(stdscr),
1035 wattrset(main_window, attr_main_menu_box);
1036 box(main_window, 0, 0);
1037 wattrset(main_window, attr_main_menu_heading);
1038 mvwprintw(main_window, 0, 3, " %s ", prompt);
1039 wattrset(main_window, attr_normal);
1041 set_menu_items(curses_menu, curses_menu_items);
1043 /* position the menu at the middle of the screen */
1044 scale_menu(curses_menu, &maxy, &maxx);
1045 maxx = min(maxx, mwin_max_cols-2);
1046 maxy = mwin_max_lines;
1047 menu_window = derwin(main_window,
1051 (mwin_max_cols-maxx)/2);
1052 keypad(menu_window, TRUE);
1053 set_menu_win(curses_menu, menu_window);
1054 set_menu_sub(curses_menu, menu_window);
1056 /* must reassert this after changing items, otherwise returns to a
1059 set_menu_format(curses_menu, maxy, 1);
1060 center_item(selected_index, last_top_row);
1061 set_menu_format(curses_menu, maxy, 1);
1063 print_function_line();
1066 post_menu(curses_menu);
1067 refresh_all_windows(main_window);
1070 static void adj_match_dir(match_f *match_direction)
1072 if (*match_direction == FIND_NEXT_MATCH_DOWN)
1074 MATCH_TINKER_PATTERN_DOWN;
1075 else if (*match_direction == FIND_NEXT_MATCH_UP)
1077 MATCH_TINKER_PATTERN_UP;
1078 /* else, do no change.. */
1084 match_f match_direction;
1088 /* Return 0 means I have handled the key. In such a case, ans should hold the
1089 * item to center, or -1 otherwise.
1092 static int do_match(int key, struct match_state *state, int *ans)
1094 char c = (char) key;
1095 int terminate_search = 0;
1097 if (key == '/' || (state->in_search && key == 27)) {
1101 state->in_search = 1-state->in_search;
1102 bzero(state->pattern, sizeof(state->pattern));
1103 state->match_direction = MATCH_TINKER_PATTERN_DOWN;
1105 } else if (!state->in_search)
1108 if (isalnum(c) || isgraph(c) || c == ' ') {
1109 state->pattern[strlen(state->pattern)] = c;
1110 state->pattern[strlen(state->pattern)] = '\0';
1111 adj_match_dir(&state->match_direction);
1112 *ans = get_mext_match(state->pattern,
1113 state->match_direction);
1114 } else if (key == KEY_DOWN) {
1115 state->match_direction = FIND_NEXT_MATCH_DOWN;
1116 *ans = get_mext_match(state->pattern,
1117 state->match_direction);
1118 } else if (key == KEY_UP) {
1119 state->match_direction = FIND_NEXT_MATCH_UP;
1120 *ans = get_mext_match(state->pattern,
1121 state->match_direction);
1122 } else if (key == KEY_BACKSPACE || key == 8 || key == 127) {
1123 state->pattern[strlen(state->pattern)-1] = '\0';
1124 adj_match_dir(&state->match_direction);
1126 terminate_search = 1;
1128 if (terminate_search) {
1129 state->in_search = 0;
1130 bzero(state->pattern, sizeof(state->pattern));
1139 static void conf(struct menu *menu)
1141 selected_conf(menu, NULL);
1144 static void selected_conf(struct menu *menu, struct menu *active_menu)
1146 struct menu *submenu = NULL;
1149 int current_index = 0;
1150 int last_top_row = 0;
1151 struct match_state match_state = {
1153 .match_direction = MATCH_TINKER_PATTERN_DOWN,
1157 while (!global_exit) {
1159 current_menu = menu;
1164 if (active_menu != NULL) {
1165 for (i = 0; i < items_num; i++) {
1168 mcur = (struct mitem *) item_userptr(curses_menu_items[i]);
1169 if ((struct menu *) mcur->usrptr == active_menu) {
1177 show_menu(menu_get_prompt(menu), menu_instructions,
1178 current_index, &last_top_row);
1179 keypad((menu_win(curses_menu)), TRUE);
1180 while (!global_exit) {
1181 if (match_state.in_search) {
1183 "searching: %s", match_state.pattern);
1186 refresh_all_windows(main_window);
1187 res = wgetch(menu_win(curses_menu));
1190 if (do_match(res, &match_state, ¤t_index) == 0) {
1191 if (current_index != -1)
1192 center_item(current_index,
1196 if (process_special_keys(&res,
1197 (struct menu *) item_data()))
1202 menu_driver(curses_menu, REQ_DOWN_ITEM);
1206 menu_driver(curses_menu, REQ_UP_ITEM);
1209 menu_driver(curses_menu, REQ_SCR_DPAGE);
1212 menu_driver(curses_menu, REQ_SCR_UPAGE);
1215 menu_driver(curses_menu, REQ_FIRST_ITEM);
1218 menu_driver(curses_menu, REQ_LAST_ITEM);
1222 show_help((struct menu *) item_data());
1225 if (res == 10 || res == 27 ||
1226 res == 32 || res == 'n' || res == 'y' ||
1227 res == KEY_LEFT || res == KEY_RIGHT ||
1230 refresh_all_windows(main_window);
1233 refresh_all_windows(main_window);
1235 if (res == 27 || (menu != &rootmenu && res == KEY_LEFT))
1238 /* remember location in the menu */
1239 last_top_row = top_row(curses_menu);
1240 current_index = curses_item_index();
1245 submenu = (struct menu *) item_data();
1246 if (!submenu || !menu_is_visible(submenu))
1252 if (item_is_tag('t'))
1253 sym_toggle_tristate_value(sym);
1254 else if (item_is_tag('m'))
1258 case 10: /* ENTER WAS PRESSED */
1259 switch (item_tag()) {
1261 if (single_menu_mode)
1263 (void *) (long) !submenu->data;
1268 if (sym_is_choice(sym) &&
1269 sym_get_tristate_value(sym) == yes)
1270 conf_choice(submenu);
1271 else if (submenu->prompt &&
1272 submenu->prompt->type == P_MENU)
1275 sym_toggle_tristate_value(sym);
1278 conf_string(submenu);
1283 if (item_is_tag('t')) {
1284 if (sym_set_tristate_value(sym, yes))
1286 if (sym_set_tristate_value(sym, mod))
1287 btn_dialog(main_window, setmod_text, 0);
1291 if (item_is_tag('t'))
1292 sym_set_tristate_value(sym, no);
1295 if (item_is_tag('t'))
1296 sym_set_tristate_value(sym, mod);
1302 static void conf_message_callback(const char *s)
1304 btn_dialog(main_window, s, 1, "<OK>");
1307 static void show_help(struct menu *menu)
1315 menu_get_ext_help(menu, &help);
1316 show_scroll_win(main_window, menu_get_prompt(menu), str_get(&help));
1320 static void conf_choice(struct menu *menu)
1322 const char *prompt = menu_get_prompt(menu);
1323 struct menu *child = NULL;
1324 struct symbol *active;
1325 int selected_index = 0;
1326 int last_top_row = 0;
1328 struct match_state match_state = {
1330 .match_direction = MATCH_TINKER_PATTERN_DOWN,
1334 active = sym_get_choice_value(menu->sym);
1335 /* this is mostly duplicated from the conf() function. */
1336 while (!global_exit) {
1339 for (i = 0, child = menu->list; child; child = child->next) {
1340 if (!show_all_items && !menu_is_visible(child))
1343 if (child->sym == sym_get_choice_value(menu->sym))
1344 item_make(child, ':', "<X> %s",
1345 menu_get_prompt(child));
1346 else if (child->sym)
1347 item_make(child, ':', " %s",
1348 menu_get_prompt(child));
1350 item_make(child, ':', "*** %s ***",
1351 menu_get_prompt(child));
1353 if (child->sym == active){
1354 last_top_row = top_row(curses_menu);
1359 show_menu(prompt ? prompt : "Choice Menu",
1360 radiolist_instructions,
1363 while (!global_exit) {
1364 if (match_state.in_search) {
1365 mvprintw(0, 0, "searching: %s",
1366 match_state.pattern);
1369 refresh_all_windows(main_window);
1370 res = wgetch(menu_win(curses_menu));
1373 if (do_match(res, &match_state, &selected_index) == 0) {
1374 if (selected_index != -1)
1375 center_item(selected_index,
1379 if (process_special_keys(
1381 (struct menu *) item_data()))
1386 menu_driver(curses_menu, REQ_DOWN_ITEM);
1390 menu_driver(curses_menu, REQ_UP_ITEM);
1393 menu_driver(curses_menu, REQ_SCR_DPAGE);
1396 menu_driver(curses_menu, REQ_SCR_UPAGE);
1399 menu_driver(curses_menu, REQ_FIRST_ITEM);
1402 menu_driver(curses_menu, REQ_LAST_ITEM);
1406 show_help((struct menu *) item_data());
1409 if (res == 10 || res == 27 || res == ' ' ||
1413 refresh_all_windows(main_window);
1415 /* if ESC or left */
1416 if (res == 27 || res == KEY_LEFT)
1419 child = item_data();
1420 if (!child || !menu_is_visible(child) || !child->sym)
1426 sym_set_tristate_value(child->sym, yes);
1431 active = child->sym;
1439 static void conf_string(struct menu *menu)
1441 const char *prompt = menu_get_prompt(menu);
1445 const char *heading;
1447 switch (sym_get_type(menu->sym)) {
1449 heading = inputbox_instructions_int;
1452 heading = inputbox_instructions_hex;
1455 heading = inputbox_instructions_string;
1458 heading = "Internal nconf error!";
1460 res = dialog_inputbox(main_window,
1461 prompt ? prompt : "Main Menu",
1463 sym_get_string_value(menu->sym),
1464 &dialog_input_result,
1465 &dialog_input_result_len);
1468 if (sym_set_string_value(menu->sym,
1469 dialog_input_result))
1471 btn_dialog(main_window,
1472 "You have made an invalid entry.", 0);
1483 static void conf_load(void)
1487 res = dialog_inputbox(main_window,
1488 NULL, load_config_text,
1490 &dialog_input_result,
1491 &dialog_input_result_len);
1494 if (!dialog_input_result[0])
1496 if (!conf_read(dialog_input_result)) {
1497 set_config_filename(dialog_input_result);
1498 conf_set_changed(true);
1501 btn_dialog(main_window, "File does not exist!", 0);
1504 show_scroll_win(main_window,
1505 "Load Alternate Configuration",
1514 static void conf_save(void)
1518 res = dialog_inputbox(main_window,
1519 NULL, save_config_text,
1521 &dialog_input_result,
1522 &dialog_input_result_len);
1525 if (!dialog_input_result[0])
1527 res = conf_write(dialog_input_result);
1529 set_config_filename(dialog_input_result);
1532 btn_dialog(main_window, "Can't create file!",
1536 show_scroll_win(main_window,
1537 "Save Alternate Configuration",
1546 static void setup_windows(void)
1550 getmaxyx(stdscr, lines, columns);
1552 if (main_window != NULL)
1553 delwin(main_window);
1555 /* set up the menu and menu window */
1556 main_window = newwin(lines-2, columns-2, 2, 1);
1557 keypad(main_window, TRUE);
1558 mwin_max_lines = lines-7;
1559 mwin_max_cols = columns-6;
1561 /* panels order is from bottom to top */
1562 new_panel(main_window);
1565 int main(int ac, char **av)
1570 if (ac > 1 && strcmp(av[1], "-s") == 0) {
1571 /* Silence conf_read() until the real callback is set up */
1572 conf_set_message_callback(NULL);
1578 mode = getenv("NCONFIG_MODE");
1580 if (!strcasecmp(mode, "single_menu"))
1581 single_menu_mode = 1;
1584 /* Initialize curses */
1586 /* set color theme */
1591 keypad(stdscr, TRUE);
1594 getmaxyx(stdscr, lines, columns);
1595 if (columns < 75 || lines < 20) {
1597 printf("Your terminal should have at "
1598 "least 20 lines and 75 columns\n");
1602 notimeout(stdscr, FALSE);
1603 #if NCURSES_REENTRANT
1610 curses_menu = new_menu(curses_menu_items);
1611 menu_opts_off(curses_menu, O_SHOWDESC);
1612 menu_opts_on(curses_menu, O_SHOWMATCH);
1613 menu_opts_on(curses_menu, O_ONEVALUE);
1614 menu_opts_on(curses_menu, O_NONCYCLIC);
1615 menu_opts_on(curses_menu, O_IGNORECASE);
1616 set_menu_mark(curses_menu, " ");
1617 set_menu_fore(curses_menu, attr_main_menu_fore);
1618 set_menu_back(curses_menu, attr_main_menu_back);
1619 set_menu_grey(curses_menu, attr_main_menu_grey);
1621 set_config_filename(conf_get_configname());
1624 /* check for KEY_FUNC(1) */
1625 if (has_key(KEY_F(1)) == FALSE) {
1626 show_scroll_win(main_window,
1628 menu_no_f_instructions);
1631 conf_set_message_callback(conf_message_callback);
1633 while (!global_exit) {
1635 if (!global_exit && do_exit() == 0)
1638 /* ok, we are done */
1639 unpost_menu(curses_menu);
1640 free_menu(curses_menu);
1641 delwin(main_window);