1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com>
5 * Derived from menuconfig.
17 #include "mnconf-common.h"
21 static const char nconf_global_help
[] =
24 "o Global help: Unless in a data entry window, pressing <F1> will give \n"
25 " you the global help window, which you are just reading.\n"
27 "o A short version of the global help is available by pressing <F3>.\n"
29 "o Local help: To get help related to the current menu entry, use any\n"
30 " of <?> <h>, or if in a data entry window then press <F1>.\n"
35 "This interface lets you select features and parameters for the kernel\n"
36 "build. Kernel features can either be built-in, modularized, or removed.\n"
37 "Parameters must be entered as text or decimal or hexadecimal numbers.\n"
39 "Menu entries beginning with following braces represent features that\n"
40 " [ ] can be built in or removed\n"
41 " < > can be built in, modularized or removed\n"
42 " { } can be built in or modularized, are selected by another feature\n"
43 " - - are selected by another feature\n"
44 " XXX cannot be selected. Symbol Info <F2> tells you why.\n"
45 "*, M or whitespace inside braces means to build in, build as a module\n"
46 "or to exclude the feature respectively.\n"
48 "To change any of these features, highlight it with the movement keys\n"
49 "listed below and press <y> to build it in, <m> to make it a module or\n"
50 "<n> to remove it. You may press the <Space> key to cycle through the\n"
51 "available options.\n"
53 "A trailing \"--->\" designates a submenu, a trailing \"----\" an\n"
56 "Menu navigation keys\n"
57 "----------------------------------------------------------------------\n"
58 "Linewise up <Up> <k>\n"
59 "Linewise down <Down> <j>\n"
60 "Pagewise up <Page Up>\n"
61 "Pagewise down <Page Down>\n"
62 "First entry <Home>\n"
64 "Enter a submenu <Right> <Enter>\n"
65 "Go back to parent menu <Left> <Esc> <F5>\n"
66 "Close a help window <Enter> <Esc> <F5>\n"
67 "Close entry window, apply <Enter>\n"
68 "Close entry window, forget <Esc> <F5>\n"
69 "Start incremental, case-insensitive search for STRING in menu entries,\n"
70 " no regex support, STRING is displayed in upper left corner\n"
72 " Remove last character <Backspace>\n"
73 " Jump to next hit <Down>\n"
74 " Jump to previous hit <Up>\n"
75 "Exit menu search mode </> <Esc>\n"
76 "Search for configuration variables with or without leading CONFIG_\n"
77 " <F8>RegExpr<Enter>\n"
78 "Verbose search help <F8><F1>\n"
79 "----------------------------------------------------------------------\n"
81 "Unless in a data entry window, key <1> may be used instead of <F1>,\n"
82 "<2> instead of <F2>, etc.\n"
85 "Radiolist (Choice list)\n"
86 "-----------------------\n"
87 "Use the movement keys listed above to select the option you wish to set\n"
88 "and press <Space>.\n"
93 "Enter the requested information and press <Enter>. Hexadecimal values\n"
94 "may be entered without the \"0x\" prefix.\n"
97 "Text Box (Help Window)\n"
98 "----------------------\n"
99 "Use movement keys as listed in table above.\n"
101 "Press any of <Enter> <Esc> <q> <F5> <F9> to exit.\n"
104 "Alternate configuration files\n"
105 "-----------------------------\n"
106 "nconfig supports switching between different configurations.\n"
107 "Press <F6> to save your current configuration. Press <F7> and enter\n"
108 "a file name to load a previously saved configuration.\n"
111 "Terminal configuration\n"
112 "----------------------\n"
113 "If you use nconfig in a xterm window, make sure your TERM environment\n"
114 "variable specifies a terminal configuration which supports at least\n"
115 "16 colors. Otherwise nconfig will look rather bad.\n"
117 "If the \"stty size\" command reports the current terminalsize correctly,\n"
118 "nconfig will adapt to sizes larger than the traditional 80x25 \"standard\"\n"
119 "and display longer menus properly.\n"
124 "If you prefer to have all of the menu entries listed in a single menu,\n"
125 "rather than the default multimenu hierarchy, run nconfig with\n"
126 "NCONFIG_MODE environment variable set to single_menu. Example:\n"
128 "make NCONFIG_MODE=single_menu nconfig\n"
130 "<Enter> will then unfold the appropriate category, or fold it if it\n"
131 "is already unfolded. Folded menu entries will be designated by a\n"
132 "leading \"++>\" and unfolded entries by a leading \"-->\".\n"
134 "Note that this mode can eventually be a little more CPU expensive than\n"
135 "the default mode, especially with a larger number of unfolded submenus.\n"
137 menu_no_f_instructions
[] =
138 "Legend: [*] built-in [ ] excluded <M> module < > module capable.\n"
139 "Submenus are designated by a trailing \"--->\", empty ones by \"----\".\n"
141 "Use the following keys to navigate the menus:\n"
142 "Move up or down with <Up> and <Down>.\n"
143 "Enter a submenu with <Enter> or <Right>.\n"
144 "Exit a submenu to its parent menu with <Esc> or <Left>.\n"
145 "Pressing <y> includes, <n> excludes, <m> modularizes features.\n"
146 "Pressing <Space> cycles through the available options.\n"
147 "To search for menu entries press </>.\n"
148 "<Esc> always leaves the current window.\n"
150 "You do not have function keys support.\n"
151 "Press <1> instead of <F1>, <2> instead of <F2>, etc.\n"
152 "For verbose global help use key <1>.\n"
153 "For help related to the current menu entry press <?> or <h>.\n",
154 menu_instructions
[] =
155 "Legend: [*] built-in [ ] excluded <M> module < > module capable.\n"
156 "Submenus are designated by a trailing \"--->\", empty ones by \"----\".\n"
158 "Use the following keys to navigate the menus:\n"
159 "Move up or down with <Up> or <Down>.\n"
160 "Enter a submenu with <Enter> or <Right>.\n"
161 "Exit a submenu to its parent menu with <Esc> or <Left>.\n"
162 "Pressing <y> includes, <n> excludes, <m> modularizes features.\n"
163 "Pressing <Space> cycles through the available options.\n"
164 "To search for menu entries press </>.\n"
165 "<Esc> always leaves the current window.\n"
167 "Pressing <1> may be used instead of <F1>, <2> instead of <F2>, etc.\n"
168 "For verbose global help press <F1>.\n"
169 "For help related to the current menu entry press <?> or <h>.\n",
170 radiolist_instructions
[] =
171 "Press <Up>, <Down>, <Home> or <End> to navigate a radiolist, select\n"
173 "For help related to the current entry press <?> or <h>.\n"
174 "For global help press <F1>.\n",
175 inputbox_instructions_int
[] =
176 "Please enter a decimal value.\n"
177 "Fractions will not be accepted.\n"
178 "Press <Enter> to apply, <Esc> to cancel.",
179 inputbox_instructions_hex
[] =
180 "Please enter a hexadecimal value.\n"
181 "Press <Enter> to apply, <Esc> to cancel.",
182 inputbox_instructions_string
[] =
183 "Please enter a string value.\n"
184 "Press <Enter> to apply, <Esc> to cancel.",
186 "This feature depends on another feature which has been configured as a\n"
187 "module. As a result, the current feature will be built as a module too.",
189 "Enter the name of the configuration file you wish to load.\n"
190 "Accept the name shown to restore the configuration you last\n"
191 "retrieved. Leave empty to abort.",
193 "For various reasons, one may wish to keep several different\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 "default one, entering its name here will allow you to load and modify\n"
198 "that configuration.\n"
200 "Leave empty to abort.\n",
202 "Enter a filename to which this configuration should be saved\n"
203 "as an alternate. Leave empty to abort.",
205 "For various reasons, one may wish to keep several different\n"
206 "configurations available on a single machine.\n"
208 "Entering a file name here will allow you to later retrieve, modify\n"
209 "and use the current configuration as an alternate to whatever\n"
210 "configuration options you have selected at that time.\n"
212 "Leave empty to abort.\n",
214 "Search for symbols (configuration variable names CONFIG_*) and display\n"
215 "their relations. Regular expressions are supported.\n"
216 "Example: Search for \"^FOO\".\n"
218 "-----------------------------------------------------------------\n"
219 "Symbol: FOO [ = m]\n"
220 "Prompt: Foo bus is used to drive the bar HW\n"
221 "Defined at drivers/pci/Kconfig:47\n"
222 "Depends on: X86_LOCAL_APIC && X86_IO_APIC\n"
224 " -> Bus options (PCI, PCMCIA, EISA, ISA)\n"
225 " -> PCI support (PCI [ = y])\n"
226 "(1) -> PCI access mode (<choice> [ = y])\n"
227 "Selects: LIBCRC32\n"
229 "-----------------------------------------------------------------\n"
230 "o The line 'Prompt:' shows the text displayed for this symbol in\n"
231 " the menu hierarchy.\n"
232 "o The 'Defined at' line tells at what file / line number the symbol is\n"
234 "o The 'Depends on:' line lists symbols that need to be defined for\n"
235 " this symbol to be visible and selectable in the menu.\n"
236 "o The 'Location:' lines tell, where in the menu structure this symbol\n"
238 " A location followed by a [ = y] indicates that this is\n"
239 " a selectable menu item, and the current value is displayed inside\n"
241 " Press the key in the (#) prefix to jump directly to that\n"
242 " location. You will be returned to the current search results\n"
243 " after exiting this new menu.\n"
244 "o The 'Selects:' line tells, what symbol will be automatically selected\n"
245 " if this symbol is selected (y or m).\n"
246 "o The 'Selected by' line tells what symbol has selected this symbol.\n"
248 "Only relevant lines are shown.\n"
251 "USB => find all symbols containing USB\n"
252 "^USB => find all symbols starting with USB\n"
253 "USB$ => find all symbols ending with USB\n"
263 #define MAX_MENU_ITEMS 4096
264 static int show_all_items
;
266 static struct menu
*current_menu
;
267 static int child_count
;
268 static int single_menu_mode
;
269 /* the window in which all information appears */
270 static WINDOW
*main_window
;
271 /* the largest size of the menu window */
272 static int mwin_max_lines
;
273 static int mwin_max_cols
;
274 /* the window in which we show option buttons */
275 static MENU
*curses_menu
;
276 static ITEM
*curses_menu_items
[MAX_MENU_ITEMS
];
277 static struct mitem k_menu_items
[MAX_MENU_ITEMS
];
278 static unsigned int items_num
;
279 static int global_exit
;
280 /* the currently selected button */
281 static const char *current_instructions
= menu_instructions
;
283 static char *dialog_input_result
;
284 static int dialog_input_result_len
;
286 static void selected_conf(struct menu
*menu
, struct menu
*active_menu
);
287 static void conf(struct menu
*menu
);
288 static void conf_choice(struct menu
*menu
);
289 static void conf_string(struct menu
*menu
);
290 static void conf_load(void);
291 static void conf_save(void);
292 static void show_help(struct menu
*menu
);
293 static int do_exit(void);
294 static void setup_windows(void);
295 static void search_conf(void);
297 typedef void (*function_key_handler_t
)(int *key
, struct menu
*menu
);
298 static void handle_f1(int *key
, struct menu
*current_item
);
299 static void handle_f2(int *key
, struct menu
*current_item
);
300 static void handle_f3(int *key
, struct menu
*current_item
);
301 static void handle_f4(int *key
, struct menu
*current_item
);
302 static void handle_f5(int *key
, struct menu
*current_item
);
303 static void handle_f6(int *key
, struct menu
*current_item
);
304 static void handle_f7(int *key
, struct menu
*current_item
);
305 static void handle_f8(int *key
, struct menu
*current_item
);
306 static void handle_f9(int *key
, struct menu
*current_item
);
308 struct function_keys
{
312 function_key_handler_t handler
;
315 static const int function_keys_num
= 9;
316 static struct function_keys function_keys
[] = {
321 .handler
= handle_f1
,
327 .handler
= handle_f2
,
333 .handler
= handle_f3
,
339 .handler
= handle_f4
,
345 .handler
= handle_f5
,
351 .handler
= handle_f6
,
357 .handler
= handle_f7
,
363 .handler
= handle_f8
,
369 .handler
= handle_f9
,
373 static void print_function_line(void)
378 int lines
= getmaxy(stdscr
);
380 for (i
= 0; i
< function_keys_num
; i
++) {
381 wattrset(main_window
, attr_function_highlight
);
382 mvwprintw(main_window
, lines
-3, offset
,
384 function_keys
[i
].key_str
);
385 wattrset(main_window
, attr_function_text
);
386 offset
+= strlen(function_keys
[i
].key_str
);
387 mvwprintw(main_window
, lines
-3,
389 function_keys
[i
].func
);
390 offset
+= strlen(function_keys
[i
].func
) + skip
;
392 wattrset(main_window
, attr_normal
);
396 static void handle_f1(int *key
, struct menu
*current_item
)
398 show_scroll_win(main_window
,
399 "Global help", nconf_global_help
);
404 static void handle_f2(int *key
, struct menu
*current_item
)
406 show_help(current_item
);
411 static void handle_f3(int *key
, struct menu
*current_item
)
413 show_scroll_win(main_window
,
415 current_instructions
);
420 static void handle_f4(int *key
, struct menu
*current_item
)
422 int res
= btn_dialog(main_window
,
436 static void handle_f5(int *key
, struct menu
*current_item
)
443 static void handle_f6(int *key
, struct menu
*current_item
)
450 static void handle_f7(int *key
, struct menu
*current_item
)
457 static void handle_f8(int *key
, struct menu
*current_item
)
464 static void handle_f9(int *key
, struct menu
*current_item
)
470 /* return != 0 to indicate the key was handles */
471 static int process_special_keys(int *key
, struct menu
*menu
)
475 if (*key
== KEY_RESIZE
) {
480 for (i
= 0; i
< function_keys_num
; i
++) {
481 if (*key
== KEY_F(function_keys
[i
].key
) ||
482 *key
== '0' + function_keys
[i
].key
){
483 function_keys
[i
].handler(key
, menu
);
491 static void clean_items(void)
494 for (i
= 0; curses_menu_items
[i
]; i
++)
495 free_item(curses_menu_items
[i
]);
496 bzero(curses_menu_items
, sizeof(curses_menu_items
));
497 bzero(k_menu_items
, sizeof(k_menu_items
));
501 typedef enum {MATCH_TINKER_PATTERN_UP
, MATCH_TINKER_PATTERN_DOWN
,
502 FIND_NEXT_MATCH_DOWN
, FIND_NEXT_MATCH_UP
} match_f
;
504 /* return the index of the matched item, or -1 if no such item exists */
505 static int get_mext_match(const char *match_str
, match_f flag
)
507 int match_start
, index
;
509 /* Do not search if the menu is empty (i.e. items_num == 0) */
510 match_start
= item_index(current_item(curses_menu
));
511 if (match_start
== ERR
)
514 if (flag
== FIND_NEXT_MATCH_DOWN
)
516 else if (flag
== FIND_NEXT_MATCH_UP
)
519 match_start
= (match_start
+ items_num
) % items_num
;
522 char *str
= k_menu_items
[index
].str
;
523 if (strcasestr(str
, match_str
) != NULL
)
525 if (flag
== FIND_NEXT_MATCH_UP
||
526 flag
== MATCH_TINKER_PATTERN_UP
)
530 index
= (index
+ items_num
) % items_num
;
531 if (index
== match_start
)
536 /* Make a new item. */
537 static void item_make(struct menu
*menu
, char tag
, const char *fmt
, ...)
541 if (items_num
> MAX_MENU_ITEMS
-1)
544 bzero(&k_menu_items
[items_num
], sizeof(k_menu_items
[0]));
545 k_menu_items
[items_num
].tag
= tag
;
546 k_menu_items
[items_num
].usrptr
= menu
;
548 k_menu_items
[items_num
].is_visible
=
549 menu_is_visible(menu
);
551 k_menu_items
[items_num
].is_visible
= 1;
554 vsnprintf(k_menu_items
[items_num
].str
,
555 sizeof(k_menu_items
[items_num
].str
),
559 if (!k_menu_items
[items_num
].is_visible
)
560 memcpy(k_menu_items
[items_num
].str
, "XXX", 3);
562 curses_menu_items
[items_num
] = new_item(
563 k_menu_items
[items_num
].str
,
564 k_menu_items
[items_num
].str
);
565 set_item_userptr(curses_menu_items
[items_num
],
566 &k_menu_items
[items_num
]);
568 if (!k_menu_items[items_num].is_visible)
569 item_opts_off(curses_menu_items[items_num], O_SELECTABLE);
573 curses_menu_items
[items_num
] = NULL
;
576 /* very hackish. adds a string to the last item added */
577 static void item_add_str(const char *fmt
, ...)
580 int index
= items_num
-1;
588 vsnprintf(new_str
, sizeof(new_str
), fmt
, ap
);
590 snprintf(tmp_str
, sizeof(tmp_str
), "%s%s",
591 k_menu_items
[index
].str
, new_str
);
592 strncpy(k_menu_items
[index
].str
,
594 sizeof(k_menu_items
[index
].str
));
596 free_item(curses_menu_items
[index
]);
597 curses_menu_items
[index
] = new_item(
598 k_menu_items
[index
].str
,
599 k_menu_items
[index
].str
);
600 set_item_userptr(curses_menu_items
[index
],
601 &k_menu_items
[index
]);
604 /* get the tag of the currently selected item */
605 static char item_tag(void)
610 cur
= current_item(curses_menu
);
613 mcur
= (struct mitem
*) item_userptr(cur
);
617 static int curses_item_index(void)
619 return item_index(current_item(curses_menu
));
622 static void *item_data(void)
627 cur
= current_item(curses_menu
);
630 mcur
= (struct mitem
*) item_userptr(cur
);
635 static int item_is_tag(char tag
)
637 return item_tag() == tag
;
640 static char filename
[PATH_MAX
+1];
641 static char menu_backtitle
[PATH_MAX
+128];
642 static void set_config_filename(const char *config_filename
)
644 snprintf(menu_backtitle
, sizeof(menu_backtitle
), "%s - %s",
645 config_filename
, rootmenu
.prompt
->text
);
647 snprintf(filename
, sizeof(filename
), "%s", config_filename
);
650 /* return = 0 means we are successful.
651 * -1 means go on doing what you were doing
653 static int do_exit(void)
656 if (!conf_get_changed()) {
660 res
= btn_dialog(main_window
,
661 "Do you wish to save your new configuration?\n"
662 "<ESC> to cancel and resume nconfig.",
666 if (res
== KEY_EXIT
) {
671 /* if we got here, the user really wants to exit */
674 res
= conf_write(filename
);
678 "Error during writing of configuration.\n"
679 "Your configuration changes were NOT saved.",
682 conf_write_autoconf(0);
687 "Your configuration changes were NOT saved.",
697 static void search_conf(void)
699 struct symbol
**sym_arr
;
703 int dres
, vscroll
= 0, hscroll
= 0;
707 str_printf( &title
, "Enter (sub)string or regexp to search for "
708 "(with or without \"%s\")", CONFIG_
);
711 dres
= dialog_inputbox(main_window
,
712 "Search Configuration Parameter",
714 "", &dialog_input_result
, &dialog_input_result_len
);
719 show_scroll_win(main_window
,
720 "Search Configuration", search_help
);
727 /* strip the prefix if necessary */
728 dialog_input
= dialog_input_result
;
729 if (strncasecmp(dialog_input_result
, CONFIG_
, strlen(CONFIG_
)) == 0)
730 dialog_input
+= strlen(CONFIG_
);
732 sym_arr
= sym_re_search(dialog_input
);
736 struct search_data data
= {
741 res
= get_relations_str(sym_arr
, &head
);
742 dres
= show_scroll_win_ext(main_window
,
743 "Search Results", str_get(&res
),
745 handle_search_keys
, &data
);
747 if (dres
>= '1' && dres
<= '9') {
748 assert(data
.target
!= NULL
);
749 selected_conf(data
.target
->parent
, data
.target
);
759 static void build_conf(struct menu
*menu
)
762 struct property
*prop
;
764 int type
, tmp
, doint
= 2;
768 if (!menu
|| (!show_all_items
&& !menu_is_visible(menu
)))
774 if (prop
&& menu
!= current_menu
) {
775 const char *prompt
= menu_get_prompt(menu
);
776 enum prop_type ptype
;
777 ptype
= menu
->prompt
? menu
->prompt
->type
: P_UNKNOWN
;
781 if (single_menu_mode
) {
784 menu
->data
? "-->" : "++>",
785 indent
+ 1, ' ', prompt
);
789 indent
+ 1, ' ', prompt
,
790 menu_is_empty(menu
) ? "----" : "--->");
792 if (single_menu_mode
&& menu
->data
)
807 item_make(menu
, ':', "---%*c%s",
817 type
= sym_get_type(sym
);
818 if (sym_is_choice(sym
)) {
819 struct symbol
*def_sym
= sym_calc_choice(menu
);
820 struct menu
*def_menu
= NULL
;
823 for (child
= menu
->list
; child
; child
= child
->next
) {
824 if (menu_is_visible(child
) && child
->sym
== def_sym
)
828 val
= sym_get_tristate_value(sym
);
829 item_make(menu
, def_menu
? 't' : ':', " ");
831 item_add_str("%*c%s", indent
+ 1,
832 ' ', menu_get_prompt(menu
));
834 item_add_str(" (%s) --->", menu_get_prompt(def_menu
));
837 if (menu
== current_menu
) {
839 "---%*c%s", indent
+ 1,
840 ' ', menu_get_prompt(menu
));
844 val
= sym_get_tristate_value(sym
);
847 if (sym_is_changeable(sym
))
848 item_make(menu
, 't', "[%c]",
849 val
== no
? ' ' : '*');
851 item_make(menu
, 't', "-%c-",
852 val
== no
? ' ' : '*');
866 if (sym_is_changeable(sym
)) {
867 if (sym
->rev_dep
.tri
== mod
)
868 item_make(menu
, 't', "{%c}", ch
);
870 item_make(menu
, 't', "<%c>", ch
);
872 item_make(menu
, 't', "-%c-", ch
);
875 tmp
= 2 + strlen(sym_get_string_value(sym
));
876 item_make(menu
, 's', " (%s)",
877 sym_get_string_value(sym
));
878 tmp
= indent
- tmp
+ 4;
881 item_add_str("%*c%s%s", tmp
, ' ', menu_get_prompt(menu
),
882 (sym_has_value(sym
) ||
883 !sym_is_changeable(sym
)) ? "" : " (NEW)");
886 item_add_str("%*c%s%s", indent
+ 1, ' ',
887 menu_get_prompt(menu
),
888 (sym_has_value(sym
) || !sym_is_changeable(sym
)) ?
890 if (menu
->prompt
&& menu
->prompt
->type
== P_MENU
) {
891 item_add_str(" %s", menu_is_empty(menu
) ? "----" : "--->");
898 for (child
= menu
->list
; child
; child
= child
->next
)
903 static void reset_menu(void)
905 unpost_menu(curses_menu
);
909 /* adjust the menu to show this item.
910 * prefer not to scroll the menu if possible*/
911 static void center_item(int selected_index
, int *last_top_row
)
915 set_top_row(curses_menu
, *last_top_row
);
916 toprow
= top_row(curses_menu
);
917 if (selected_index
< toprow
||
918 selected_index
>= toprow
+mwin_max_lines
) {
919 toprow
= max(selected_index
-mwin_max_lines
/2, 0);
920 if (toprow
>= item_count(curses_menu
)-mwin_max_lines
)
921 toprow
= item_count(curses_menu
)-mwin_max_lines
;
922 set_top_row(curses_menu
, toprow
);
924 set_current_item(curses_menu
,
925 curses_menu_items
[selected_index
]);
926 *last_top_row
= toprow
;
927 post_menu(curses_menu
);
928 refresh_all_windows(main_window
);
931 /* this function assumes reset_menu has been called before */
932 static void show_menu(const char *prompt
, const char *instructions
,
933 int selected_index
, int *last_top_row
)
938 current_instructions
= instructions
;
941 print_in_middle(stdscr
, 1, getmaxx(stdscr
),
945 wattrset(main_window
, attr_main_menu_box
);
946 box(main_window
, 0, 0);
947 wattrset(main_window
, attr_main_menu_heading
);
948 mvwprintw(main_window
, 0, 3, " %s ", prompt
);
949 wattrset(main_window
, attr_normal
);
951 set_menu_items(curses_menu
, curses_menu_items
);
953 /* position the menu at the middle of the screen */
954 scale_menu(curses_menu
, &maxy
, &maxx
);
955 maxx
= min(maxx
, mwin_max_cols
-2);
956 maxy
= mwin_max_lines
;
957 menu_window
= derwin(main_window
,
961 (mwin_max_cols
-maxx
)/2);
962 keypad(menu_window
, TRUE
);
963 set_menu_win(curses_menu
, menu_window
);
964 set_menu_sub(curses_menu
, menu_window
);
966 /* must reassert this after changing items, otherwise returns to a
969 set_menu_format(curses_menu
, maxy
, 1);
970 center_item(selected_index
, last_top_row
);
971 set_menu_format(curses_menu
, maxy
, 1);
973 print_function_line();
976 post_menu(curses_menu
);
977 refresh_all_windows(main_window
);
980 static void adj_match_dir(match_f
*match_direction
)
982 if (*match_direction
== FIND_NEXT_MATCH_DOWN
)
984 MATCH_TINKER_PATTERN_DOWN
;
985 else if (*match_direction
== FIND_NEXT_MATCH_UP
)
987 MATCH_TINKER_PATTERN_UP
;
988 /* else, do no change.. */
994 match_f match_direction
;
998 /* Return 0 means I have handled the key. In such a case, ans should hold the
999 * item to center, or -1 otherwise.
1002 static int do_match(int key
, struct match_state
*state
, int *ans
)
1004 char c
= (char) key
;
1005 int terminate_search
= 0;
1007 if (key
== '/' || (state
->in_search
&& key
== 27)) {
1011 state
->in_search
= 1-state
->in_search
;
1012 bzero(state
->pattern
, sizeof(state
->pattern
));
1013 state
->match_direction
= MATCH_TINKER_PATTERN_DOWN
;
1015 } else if (!state
->in_search
)
1018 if (isalnum(c
) || isgraph(c
) || c
== ' ') {
1019 state
->pattern
[strlen(state
->pattern
)] = c
;
1020 state
->pattern
[strlen(state
->pattern
)] = '\0';
1021 adj_match_dir(&state
->match_direction
);
1022 *ans
= get_mext_match(state
->pattern
,
1023 state
->match_direction
);
1024 } else if (key
== KEY_DOWN
) {
1025 state
->match_direction
= FIND_NEXT_MATCH_DOWN
;
1026 *ans
= get_mext_match(state
->pattern
,
1027 state
->match_direction
);
1028 } else if (key
== KEY_UP
) {
1029 state
->match_direction
= FIND_NEXT_MATCH_UP
;
1030 *ans
= get_mext_match(state
->pattern
,
1031 state
->match_direction
);
1032 } else if (key
== KEY_BACKSPACE
|| key
== 8 || key
== 127) {
1033 state
->pattern
[strlen(state
->pattern
)-1] = '\0';
1034 adj_match_dir(&state
->match_direction
);
1036 terminate_search
= 1;
1038 if (terminate_search
) {
1039 state
->in_search
= 0;
1040 bzero(state
->pattern
, sizeof(state
->pattern
));
1049 static void conf(struct menu
*menu
)
1051 selected_conf(menu
, NULL
);
1054 static void selected_conf(struct menu
*menu
, struct menu
*active_menu
)
1056 struct menu
*submenu
= NULL
;
1059 int current_index
= 0;
1060 int last_top_row
= 0;
1061 struct match_state match_state
= {
1063 .match_direction
= MATCH_TINKER_PATTERN_DOWN
,
1067 while (!global_exit
) {
1069 current_menu
= menu
;
1074 if (active_menu
!= NULL
) {
1075 for (i
= 0; i
< items_num
; i
++) {
1078 mcur
= (struct mitem
*) item_userptr(curses_menu_items
[i
]);
1079 if ((struct menu
*) mcur
->usrptr
== active_menu
) {
1087 show_menu(menu_get_prompt(menu
), menu_instructions
,
1088 current_index
, &last_top_row
);
1089 keypad((menu_win(curses_menu
)), TRUE
);
1090 while (!global_exit
) {
1091 if (match_state
.in_search
) {
1093 "searching: %s", match_state
.pattern
);
1096 refresh_all_windows(main_window
);
1097 res
= wgetch(menu_win(curses_menu
));
1100 if (do_match(res
, &match_state
, ¤t_index
) == 0) {
1101 if (current_index
!= -1)
1102 center_item(current_index
,
1106 if (process_special_keys(&res
,
1107 (struct menu
*) item_data()))
1112 menu_driver(curses_menu
, REQ_DOWN_ITEM
);
1116 menu_driver(curses_menu
, REQ_UP_ITEM
);
1119 menu_driver(curses_menu
, REQ_SCR_DPAGE
);
1122 menu_driver(curses_menu
, REQ_SCR_UPAGE
);
1125 menu_driver(curses_menu
, REQ_FIRST_ITEM
);
1128 menu_driver(curses_menu
, REQ_LAST_ITEM
);
1132 show_help((struct menu
*) item_data());
1135 if (res
== 10 || res
== 27 ||
1136 res
== 32 || res
== 'n' || res
== 'y' ||
1137 res
== KEY_LEFT
|| res
== KEY_RIGHT
||
1140 refresh_all_windows(main_window
);
1143 refresh_all_windows(main_window
);
1145 if (res
== 27 || (menu
!= &rootmenu
&& res
== KEY_LEFT
))
1148 /* remember location in the menu */
1149 last_top_row
= top_row(curses_menu
);
1150 current_index
= curses_item_index();
1155 submenu
= (struct menu
*) item_data();
1156 if (!submenu
|| !menu_is_visible(submenu
))
1162 if (item_is_tag('t'))
1163 sym_toggle_tristate_value(sym
);
1164 else if (item_is_tag('m'))
1168 case 10: /* ENTER WAS PRESSED */
1169 switch (item_tag()) {
1171 if (single_menu_mode
)
1173 (void *) (long) !submenu
->data
;
1178 if (sym_is_choice(sym
))
1179 conf_choice(submenu
);
1180 else if (submenu
->prompt
&&
1181 submenu
->prompt
->type
== P_MENU
)
1184 sym_toggle_tristate_value(sym
);
1187 conf_string(submenu
);
1192 if (item_is_tag('t')) {
1193 if (sym_set_tristate_value(sym
, yes
))
1195 if (sym_set_tristate_value(sym
, mod
))
1196 btn_dialog(main_window
, setmod_text
, 0);
1200 if (item_is_tag('t'))
1201 sym_set_tristate_value(sym
, no
);
1204 if (item_is_tag('t'))
1205 sym_set_tristate_value(sym
, mod
);
1211 static void conf_message_callback(const char *s
)
1213 btn_dialog(main_window
, s
, 1, "<OK>");
1216 static void show_help(struct menu
*menu
)
1224 menu_get_ext_help(menu
, &help
);
1225 show_scroll_win(main_window
, menu_get_prompt(menu
), str_get(&help
));
1229 static void conf_choice(struct menu
*menu
)
1231 const char *prompt
= menu_get_prompt(menu
);
1232 struct menu
*child
= NULL
;
1233 struct symbol
*active
;
1234 int selected_index
= 0;
1235 int last_top_row
= 0;
1237 struct match_state match_state
= {
1239 .match_direction
= MATCH_TINKER_PATTERN_DOWN
,
1243 active
= sym_calc_choice(menu
);
1244 /* this is mostly duplicated from the conf() function. */
1245 while (!global_exit
) {
1248 for (i
= 0, child
= menu
->list
; child
; child
= child
->next
) {
1249 if (!show_all_items
&& !menu_is_visible(child
))
1252 if (child
->sym
== sym_calc_choice(menu
))
1253 item_make(child
, ':', "<X> %s",
1254 menu_get_prompt(child
));
1255 else if (child
->sym
)
1256 item_make(child
, ':', " %s",
1257 menu_get_prompt(child
));
1259 item_make(child
, ':', "*** %s ***",
1260 menu_get_prompt(child
));
1262 if (child
->sym
== active
){
1263 last_top_row
= top_row(curses_menu
);
1268 show_menu(prompt
? prompt
: "Choice Menu",
1269 radiolist_instructions
,
1272 while (!global_exit
) {
1273 if (match_state
.in_search
) {
1274 mvprintw(0, 0, "searching: %s",
1275 match_state
.pattern
);
1278 refresh_all_windows(main_window
);
1279 res
= wgetch(menu_win(curses_menu
));
1282 if (do_match(res
, &match_state
, &selected_index
) == 0) {
1283 if (selected_index
!= -1)
1284 center_item(selected_index
,
1288 if (process_special_keys(
1290 (struct menu
*) item_data()))
1295 menu_driver(curses_menu
, REQ_DOWN_ITEM
);
1299 menu_driver(curses_menu
, REQ_UP_ITEM
);
1302 menu_driver(curses_menu
, REQ_SCR_DPAGE
);
1305 menu_driver(curses_menu
, REQ_SCR_UPAGE
);
1308 menu_driver(curses_menu
, REQ_FIRST_ITEM
);
1311 menu_driver(curses_menu
, REQ_LAST_ITEM
);
1315 show_help((struct menu
*) item_data());
1318 if (res
== 10 || res
== 27 || res
== ' ' ||
1322 refresh_all_windows(main_window
);
1324 /* if ESC or left */
1325 if (res
== 27 || res
== KEY_LEFT
)
1328 child
= item_data();
1329 if (!child
|| !menu_is_visible(child
) || !child
->sym
)
1335 choice_set_value(menu
, child
->sym
);
1340 active
= child
->sym
;
1348 static void conf_string(struct menu
*menu
)
1350 const char *prompt
= menu_get_prompt(menu
);
1354 const char *heading
;
1356 switch (sym_get_type(menu
->sym
)) {
1358 heading
= inputbox_instructions_int
;
1361 heading
= inputbox_instructions_hex
;
1364 heading
= inputbox_instructions_string
;
1367 heading
= "Internal nconf error!";
1369 res
= dialog_inputbox(main_window
,
1370 prompt
? prompt
: "Main Menu",
1372 sym_get_string_value(menu
->sym
),
1373 &dialog_input_result
,
1374 &dialog_input_result_len
);
1377 if (sym_set_string_value(menu
->sym
,
1378 dialog_input_result
))
1380 btn_dialog(main_window
,
1381 "You have made an invalid entry.", 0);
1392 static void conf_load(void)
1396 res
= dialog_inputbox(main_window
,
1397 NULL
, load_config_text
,
1399 &dialog_input_result
,
1400 &dialog_input_result_len
);
1403 if (!dialog_input_result
[0])
1405 if (!conf_read(dialog_input_result
)) {
1406 set_config_filename(dialog_input_result
);
1407 conf_set_changed(true);
1410 btn_dialog(main_window
, "File does not exist!", 0);
1413 show_scroll_win(main_window
,
1414 "Load Alternate Configuration",
1423 static void conf_save(void)
1427 res
= dialog_inputbox(main_window
,
1428 NULL
, save_config_text
,
1430 &dialog_input_result
,
1431 &dialog_input_result_len
);
1434 if (!dialog_input_result
[0])
1436 res
= conf_write(dialog_input_result
);
1438 set_config_filename(dialog_input_result
);
1441 btn_dialog(main_window
, "Can't create file!",
1445 show_scroll_win(main_window
,
1446 "Save Alternate Configuration",
1455 static void setup_windows(void)
1459 getmaxyx(stdscr
, lines
, columns
);
1461 if (main_window
!= NULL
)
1462 delwin(main_window
);
1464 /* set up the menu and menu window */
1465 main_window
= newwin(lines
-2, columns
-2, 2, 1);
1466 keypad(main_window
, TRUE
);
1467 mwin_max_lines
= lines
-7;
1468 mwin_max_cols
= columns
-6;
1470 /* panels order is from bottom to top */
1471 new_panel(main_window
);
1474 int main(int ac
, char **av
)
1479 if (ac
> 1 && strcmp(av
[1], "-s") == 0) {
1480 /* Silence conf_read() until the real callback is set up */
1481 conf_set_message_callback(NULL
);
1487 mode
= getenv("NCONFIG_MODE");
1489 if (!strcasecmp(mode
, "single_menu"))
1490 single_menu_mode
= 1;
1493 /* Initialize curses */
1495 /* set color theme */
1500 keypad(stdscr
, TRUE
);
1503 getmaxyx(stdscr
, lines
, columns
);
1504 if (columns
< 75 || lines
< 20) {
1506 printf("Your terminal should have at "
1507 "least 20 lines and 75 columns\n");
1511 notimeout(stdscr
, FALSE
);
1512 #if NCURSES_REENTRANT
1519 curses_menu
= new_menu(curses_menu_items
);
1520 menu_opts_off(curses_menu
, O_SHOWDESC
);
1521 menu_opts_on(curses_menu
, O_SHOWMATCH
);
1522 menu_opts_on(curses_menu
, O_ONEVALUE
);
1523 menu_opts_on(curses_menu
, O_NONCYCLIC
);
1524 menu_opts_on(curses_menu
, O_IGNORECASE
);
1525 set_menu_mark(curses_menu
, " ");
1526 set_menu_fore(curses_menu
, attr_main_menu_fore
);
1527 set_menu_back(curses_menu
, attr_main_menu_back
);
1528 set_menu_grey(curses_menu
, attr_main_menu_grey
);
1530 set_config_filename(conf_get_configname());
1533 /* check for KEY_FUNC(1) */
1534 if (has_key(KEY_F(1)) == FALSE
) {
1535 show_scroll_win(main_window
,
1537 menu_no_f_instructions
);
1540 conf_set_message_callback(conf_message_callback
);
1542 while (!global_exit
) {
1544 if (!global_exit
&& do_exit() == 0)
1547 /* ok, we are done */
1548 unpost_menu(curses_menu
);
1549 free_menu(curses_menu
);
1550 delwin(main_window
);