1 // SPDX-License-Identifier: GPL-2.0-only
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"
30 "OpenWrt config is based on Kernel kconfig\n"
31 "so ipkg packages are referred here as modules.\n"
35 "This interface lets you select features and parameters for the build.\n"
36 "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"
59 "Linewise down <Down>\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 || IA64\n"
224 " -> Bus options (PCI, PCMCIA, EISA, ISA)\n"
225 " -> PCI support (PCI [ = y])\n"
226 " -> 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"
237 " is located. A location followed by a [ = y] indicates that this is\n"
238 " a selectable menu item, and the current value is displayed inside\n"
240 "o The 'Selects:' line tells, what symbol will be automatically selected\n"
241 " if this symbol is selected (y or m).\n"
242 "o The 'Selected by' line tells what symbol has selected this symbol.\n"
244 "Only relevant lines are shown.\n"
247 "USB => find all symbols containing USB\n"
248 "^USB => find all symbols starting with USB\n"
249 "USB$ => find all symbols ending with USB\n"
259 #define MAX_MENU_ITEMS 4096
260 static int show_all_items
;
262 static struct menu
*current_menu
;
263 static int child_count
;
264 static int single_menu_mode
;
265 /* the window in which all information appears */
266 static WINDOW
*main_window
;
267 /* the largest size of the menu window */
268 static int mwin_max_lines
;
269 static int mwin_max_cols
;
270 /* the window in which we show option buttons */
271 static MENU
*curses_menu
;
272 static ITEM
*curses_menu_items
[MAX_MENU_ITEMS
];
273 static struct mitem k_menu_items
[MAX_MENU_ITEMS
];
274 static unsigned int items_num
;
275 static int global_exit
;
276 /* the currently selected button */
277 static const char *current_instructions
= menu_instructions
;
279 static char *dialog_input_result
;
280 static int dialog_input_result_len
;
282 static void conf(struct menu
*menu
);
283 static void conf_choice(struct menu
*menu
);
284 static void conf_string(struct menu
*menu
);
285 static void conf_load(void);
286 static void conf_save(void);
287 static void show_help(struct menu
*menu
);
288 static int do_exit(void);
289 static void setup_windows(void);
290 static void search_conf(void);
292 typedef void (*function_key_handler_t
)(int *key
, struct menu
*menu
);
293 static void handle_f1(int *key
, struct menu
*current_item
);
294 static void handle_f2(int *key
, struct menu
*current_item
);
295 static void handle_f3(int *key
, struct menu
*current_item
);
296 static void handle_f4(int *key
, struct menu
*current_item
);
297 static void handle_f5(int *key
, struct menu
*current_item
);
298 static void handle_f6(int *key
, struct menu
*current_item
);
299 static void handle_f7(int *key
, struct menu
*current_item
);
300 static void handle_f8(int *key
, struct menu
*current_item
);
301 static void handle_f9(int *key
, struct menu
*current_item
);
303 struct function_keys
{
307 function_key_handler_t handler
;
310 static const int function_keys_num
= 9;
311 static struct function_keys function_keys
[] = {
316 .handler
= handle_f1
,
322 .handler
= handle_f2
,
328 .handler
= handle_f3
,
334 .handler
= handle_f4
,
340 .handler
= handle_f5
,
346 .handler
= handle_f6
,
352 .handler
= handle_f7
,
358 .handler
= handle_f8
,
364 .handler
= handle_f9
,
368 static void print_function_line(void)
373 int lines
= getmaxy(stdscr
);
375 for (i
= 0; i
< function_keys_num
; i
++) {
376 wattrset(main_window
, attr_function_highlight
);
377 mvwprintw(main_window
, lines
-3, offset
,
379 function_keys
[i
].key_str
);
380 wattrset(main_window
, attr_function_text
);
381 offset
+= strlen(function_keys
[i
].key_str
);
382 mvwprintw(main_window
, lines
-3,
384 function_keys
[i
].func
);
385 offset
+= strlen(function_keys
[i
].func
) + skip
;
387 wattrset(main_window
, attr_normal
);
391 static void handle_f1(int *key
, struct menu
*current_item
)
393 show_scroll_win(main_window
,
394 "Global help", nconf_global_help
);
399 static void handle_f2(int *key
, struct menu
*current_item
)
401 show_help(current_item
);
406 static void handle_f3(int *key
, struct menu
*current_item
)
408 show_scroll_win(main_window
,
410 current_instructions
);
415 static void handle_f4(int *key
, struct menu
*current_item
)
417 int res
= btn_dialog(main_window
,
431 static void handle_f5(int *key
, struct menu
*current_item
)
438 static void handle_f6(int *key
, struct menu
*current_item
)
445 static void handle_f7(int *key
, struct menu
*current_item
)
452 static void handle_f8(int *key
, struct menu
*current_item
)
459 static void handle_f9(int *key
, struct menu
*current_item
)
465 /* return != 0 to indicate the key was handles */
466 static int process_special_keys(int *key
, struct menu
*menu
)
470 if (*key
== KEY_RESIZE
) {
475 for (i
= 0; i
< function_keys_num
; i
++) {
476 if (*key
== KEY_F(function_keys
[i
].key
) ||
477 *key
== '0' + function_keys
[i
].key
){
478 function_keys
[i
].handler(key
, menu
);
486 static void clean_items(void)
489 for (i
= 0; curses_menu_items
[i
]; i
++)
490 free_item(curses_menu_items
[i
]);
491 bzero(curses_menu_items
, sizeof(curses_menu_items
));
492 bzero(k_menu_items
, sizeof(k_menu_items
));
496 typedef enum {MATCH_TINKER_PATTERN_UP
, MATCH_TINKER_PATTERN_DOWN
,
497 FIND_NEXT_MATCH_DOWN
, FIND_NEXT_MATCH_UP
} match_f
;
499 /* return the index of the matched item, or -1 if no such item exists */
500 static int get_mext_match(const char *match_str
, match_f flag
)
502 int match_start
, index
;
504 /* Do not search if the menu is empty (i.e. items_num == 0) */
505 match_start
= item_index(current_item(curses_menu
));
506 if (match_start
== ERR
)
509 if (flag
== FIND_NEXT_MATCH_DOWN
)
511 else if (flag
== FIND_NEXT_MATCH_UP
)
514 match_start
= (match_start
+ items_num
) % items_num
;
517 char *str
= k_menu_items
[index
].str
;
518 if (strcasestr(str
, match_str
) != NULL
)
520 if (flag
== FIND_NEXT_MATCH_UP
||
521 flag
== MATCH_TINKER_PATTERN_UP
)
525 index
= (index
+ items_num
) % items_num
;
526 if (index
== match_start
)
531 /* Make a new item. */
532 static void item_make(struct menu
*menu
, char tag
, const char *fmt
, ...)
536 if (items_num
> MAX_MENU_ITEMS
-1)
539 bzero(&k_menu_items
[items_num
], sizeof(k_menu_items
[0]));
540 k_menu_items
[items_num
].tag
= tag
;
541 k_menu_items
[items_num
].usrptr
= menu
;
543 k_menu_items
[items_num
].is_visible
=
544 menu_is_visible(menu
);
546 k_menu_items
[items_num
].is_visible
= 1;
549 vsnprintf(k_menu_items
[items_num
].str
,
550 sizeof(k_menu_items
[items_num
].str
),
554 if (!k_menu_items
[items_num
].is_visible
)
555 memcpy(k_menu_items
[items_num
].str
, "XXX", 3);
557 curses_menu_items
[items_num
] = new_item(
558 k_menu_items
[items_num
].str
,
559 k_menu_items
[items_num
].str
);
560 set_item_userptr(curses_menu_items
[items_num
],
561 &k_menu_items
[items_num
]);
563 if (!k_menu_items[items_num].is_visible)
564 item_opts_off(curses_menu_items[items_num], O_SELECTABLE);
568 curses_menu_items
[items_num
] = NULL
;
571 /* very hackish. adds a string to the last item added */
572 static void item_add_str(const char *fmt
, ...)
575 int index
= items_num
-1;
583 vsnprintf(new_str
, sizeof(new_str
), fmt
, ap
);
585 snprintf(tmp_str
, sizeof(tmp_str
), "%s%s",
586 k_menu_items
[index
].str
, new_str
);
587 strncpy(k_menu_items
[index
].str
,
589 sizeof(k_menu_items
[index
].str
));
591 free_item(curses_menu_items
[index
]);
592 curses_menu_items
[index
] = new_item(
593 k_menu_items
[index
].str
,
594 k_menu_items
[index
].str
);
595 set_item_userptr(curses_menu_items
[index
],
596 &k_menu_items
[index
]);
599 /* get the tag of the currently selected item */
600 static char item_tag(void)
605 cur
= current_item(curses_menu
);
608 mcur
= (struct mitem
*) item_userptr(cur
);
612 static int curses_item_index(void)
614 return item_index(current_item(curses_menu
));
617 static void *item_data(void)
622 cur
= current_item(curses_menu
);
625 mcur
= (struct mitem
*) item_userptr(cur
);
630 static int item_is_tag(char tag
)
632 return item_tag() == tag
;
635 static char filename
[PATH_MAX
+1];
636 static char menu_backtitle
[PATH_MAX
+128];
637 static void set_config_filename(const char *config_filename
)
639 snprintf(menu_backtitle
, sizeof(menu_backtitle
), "%s - %s",
640 config_filename
, rootmenu
.prompt
->text
);
642 snprintf(filename
, sizeof(filename
), "%s", config_filename
);
645 /* return = 0 means we are successful.
646 * -1 means go on doing what you were doing
648 static int do_exit(void)
651 if (!conf_get_changed()) {
655 res
= btn_dialog(main_window
,
656 "Do you wish to save your new configuration?\n"
657 "<ESC> to cancel and resume nconfig.",
661 if (res
== KEY_EXIT
) {
666 /* if we got here, the user really wants to exit */
669 res
= conf_write(filename
);
673 "Error during writing of configuration.\n"
674 "Your configuration changes were NOT saved.",
677 conf_write_autoconf(0);
682 "Your configuration changes were NOT saved.",
692 static void search_conf(void)
694 struct symbol
**sym_arr
;
701 str_printf( &title
, "Enter (sub)string or regexp to search for "
702 "(with or without \"%s\")", CONFIG_
);
705 dres
= dialog_inputbox(main_window
,
706 "Search Configuration Parameter",
708 "", &dialog_input_result
, &dialog_input_result_len
);
713 show_scroll_win(main_window
,
714 "Search Configuration", search_help
);
721 /* strip the prefix if necessary */
722 dialog_input
= dialog_input_result
;
723 if (strncasecmp(dialog_input_result
, CONFIG_
, strlen(CONFIG_
)) == 0)
724 dialog_input
+= strlen(CONFIG_
);
726 sym_arr
= sym_re_search(dialog_input
);
727 res
= get_relations_str(sym_arr
, NULL
);
729 show_scroll_win(main_window
,
730 "Search Results", str_get(&res
));
736 static void build_conf(struct menu
*menu
)
739 struct property
*prop
;
741 int type
, tmp
, doint
= 2;
745 if (!menu
|| (!show_all_items
&& !menu_is_visible(menu
)))
751 if (prop
&& menu
!= current_menu
) {
752 const char *prompt
= menu_get_prompt(menu
);
753 enum prop_type ptype
;
754 ptype
= menu
->prompt
? menu
->prompt
->type
: P_UNKNOWN
;
758 if (single_menu_mode
) {
761 menu
->data
? "-->" : "++>",
762 indent
+ 1, ' ', prompt
);
766 indent
+ 1, ' ', prompt
,
767 menu_is_empty(menu
) ? "----" : "--->");
769 if (single_menu_mode
&& menu
->data
)
784 item_make(menu
, ':', "---%*c%s",
794 type
= sym_get_type(sym
);
795 if (sym_is_choice(sym
)) {
796 struct symbol
*def_sym
= sym_get_choice_value(sym
);
797 struct menu
*def_menu
= NULL
;
800 for (child
= menu
->list
; child
; child
= child
->next
) {
801 if (menu_is_visible(child
) && child
->sym
== def_sym
)
805 val
= sym_get_tristate_value(sym
);
806 if (sym_is_changeable(sym
)) {
809 item_make(menu
, 't', "[%c]",
810 val
== no
? ' ' : '*');
824 item_make(menu
, 't', "<%c>", ch
);
828 item_make(menu
, def_menu
? 't' : ':', " ");
831 item_add_str("%*c%s", indent
+ 1,
832 ' ', menu_get_prompt(menu
));
835 item_add_str(" (%s)",
836 menu_get_prompt(def_menu
));
837 item_add_str(" --->");
838 if (def_menu
->list
) {
840 build_conf(def_menu
);
847 if (menu
== current_menu
) {
849 "---%*c%s", indent
+ 1,
850 ' ', menu_get_prompt(menu
));
854 val
= sym_get_tristate_value(sym
);
855 if (sym_is_choice_value(sym
) && val
== yes
) {
856 item_make(menu
, ':', " ");
860 if (sym_is_changeable(sym
))
861 item_make(menu
, 't', "[%c]",
862 val
== no
? ' ' : '*');
864 item_make(menu
, 't', "-%c-",
865 val
== no
? ' ' : '*');
879 if (sym_is_changeable(sym
)) {
880 if (sym
->rev_dep
.tri
== mod
)
887 item_make(menu
, 't', "-%c-", ch
);
890 tmp
= 2 + strlen(sym_get_string_value(sym
));
891 item_make(menu
, 's', " (%s)",
892 sym_get_string_value(sym
));
893 tmp
= indent
- tmp
+ 4;
896 item_add_str("%*c%s%s", tmp
, ' ',
897 menu_get_prompt(menu
),
898 (sym_has_value(sym
) ||
899 !sym_is_changeable(sym
)) ? "" :
904 item_add_str("%*c%s%s", indent
+ 1, ' ',
905 menu_get_prompt(menu
),
906 (sym_has_value(sym
) || !sym_is_changeable(sym
)) ?
908 if (menu
->prompt
&& menu
->prompt
->type
== P_MENU
) {
909 item_add_str(" %s", menu_is_empty(menu
) ? "----" : "--->");
916 for (child
= menu
->list
; child
; child
= child
->next
)
921 static void reset_menu(void)
923 unpost_menu(curses_menu
);
927 /* adjust the menu to show this item.
928 * prefer not to scroll the menu if possible*/
929 static void center_item(int selected_index
, int *last_top_row
)
933 set_top_row(curses_menu
, *last_top_row
);
934 toprow
= top_row(curses_menu
);
935 if (selected_index
< toprow
||
936 selected_index
>= toprow
+mwin_max_lines
) {
937 toprow
= max(selected_index
-mwin_max_lines
/2, 0);
938 if (toprow
>= item_count(curses_menu
)-mwin_max_lines
)
939 toprow
= item_count(curses_menu
)-mwin_max_lines
;
940 set_top_row(curses_menu
, toprow
);
942 set_current_item(curses_menu
,
943 curses_menu_items
[selected_index
]);
944 *last_top_row
= toprow
;
945 post_menu(curses_menu
);
946 refresh_all_windows(main_window
);
949 /* this function assumes reset_menu has been called before */
950 static void show_menu(const char *prompt
, const char *instructions
,
951 int selected_index
, int *last_top_row
)
956 current_instructions
= instructions
;
959 print_in_middle(stdscr
, 1, getmaxx(stdscr
),
963 wattrset(main_window
, attr_main_menu_box
);
964 box(main_window
, 0, 0);
965 wattrset(main_window
, attr_main_menu_heading
);
966 mvwprintw(main_window
, 0, 3, " %s ", prompt
);
967 wattrset(main_window
, attr_normal
);
969 set_menu_items(curses_menu
, curses_menu_items
);
971 /* position the menu at the middle of the screen */
972 scale_menu(curses_menu
, &maxy
, &maxx
);
973 maxx
= min(maxx
, mwin_max_cols
-2);
974 maxy
= mwin_max_lines
;
975 menu_window
= derwin(main_window
,
979 (mwin_max_cols
-maxx
)/2);
980 keypad(menu_window
, TRUE
);
981 set_menu_win(curses_menu
, menu_window
);
982 set_menu_sub(curses_menu
, menu_window
);
984 /* must reassert this after changing items, otherwise returns to a
987 set_menu_format(curses_menu
, maxy
, 1);
988 center_item(selected_index
, last_top_row
);
989 set_menu_format(curses_menu
, maxy
, 1);
991 print_function_line();
994 post_menu(curses_menu
);
995 refresh_all_windows(main_window
);
998 static void adj_match_dir(match_f
*match_direction
)
1000 if (*match_direction
== FIND_NEXT_MATCH_DOWN
)
1002 MATCH_TINKER_PATTERN_DOWN
;
1003 else if (*match_direction
== FIND_NEXT_MATCH_UP
)
1005 MATCH_TINKER_PATTERN_UP
;
1006 /* else, do no change.. */
1012 match_f match_direction
;
1016 /* Return 0 means I have handled the key. In such a case, ans should hold the
1017 * item to center, or -1 otherwise.
1020 static int do_match(int key
, struct match_state
*state
, int *ans
)
1022 char c
= (char) key
;
1023 int terminate_search
= 0;
1025 if (key
== '/' || (state
->in_search
&& key
== 27)) {
1029 state
->in_search
= 1-state
->in_search
;
1030 bzero(state
->pattern
, sizeof(state
->pattern
));
1031 state
->match_direction
= MATCH_TINKER_PATTERN_DOWN
;
1033 } else if (!state
->in_search
)
1036 if (isalnum(c
) || isgraph(c
) || c
== ' ') {
1037 state
->pattern
[strlen(state
->pattern
)] = c
;
1038 state
->pattern
[strlen(state
->pattern
)] = '\0';
1039 adj_match_dir(&state
->match_direction
);
1040 *ans
= get_mext_match(state
->pattern
,
1041 state
->match_direction
);
1042 } else if (key
== KEY_DOWN
) {
1043 state
->match_direction
= FIND_NEXT_MATCH_DOWN
;
1044 *ans
= get_mext_match(state
->pattern
,
1045 state
->match_direction
);
1046 } else if (key
== KEY_UP
) {
1047 state
->match_direction
= FIND_NEXT_MATCH_UP
;
1048 *ans
= get_mext_match(state
->pattern
,
1049 state
->match_direction
);
1050 } else if (key
== KEY_BACKSPACE
|| key
== 8 || key
== 127) {
1051 state
->pattern
[strlen(state
->pattern
)-1] = '\0';
1052 adj_match_dir(&state
->match_direction
);
1054 terminate_search
= 1;
1056 if (terminate_search
) {
1057 state
->in_search
= 0;
1058 bzero(state
->pattern
, sizeof(state
->pattern
));
1067 static void conf(struct menu
*menu
)
1069 struct menu
*submenu
= NULL
;
1072 int current_index
= 0;
1073 int last_top_row
= 0;
1074 struct match_state match_state
= {
1076 .match_direction
= MATCH_TINKER_PATTERN_DOWN
,
1080 while (!global_exit
) {
1082 current_menu
= 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()))
1111 menu_driver(curses_menu
, REQ_DOWN_ITEM
);
1114 menu_driver(curses_menu
, REQ_UP_ITEM
);
1117 menu_driver(curses_menu
, REQ_SCR_DPAGE
);
1120 menu_driver(curses_menu
, REQ_SCR_UPAGE
);
1123 menu_driver(curses_menu
, REQ_FIRST_ITEM
);
1126 menu_driver(curses_menu
, REQ_LAST_ITEM
);
1130 show_help((struct menu
*) item_data());
1133 if (res
== 10 || res
== 27 ||
1134 res
== 32 || res
== 'n' || res
== 'y' ||
1135 res
== KEY_LEFT
|| res
== KEY_RIGHT
||
1138 refresh_all_windows(main_window
);
1141 refresh_all_windows(main_window
);
1143 if (res
== 27 || (menu
!= &rootmenu
&& res
== KEY_LEFT
))
1146 /* remember location in the menu */
1147 last_top_row
= top_row(curses_menu
);
1148 current_index
= curses_item_index();
1153 submenu
= (struct menu
*) item_data();
1154 if (!submenu
|| !menu_is_visible(submenu
))
1160 if (item_is_tag('t'))
1161 sym_toggle_tristate_value(sym
);
1162 else if (item_is_tag('m'))
1166 case 10: /* ENTER WAS PRESSED */
1167 switch (item_tag()) {
1169 if (single_menu_mode
)
1171 (void *) (long) !submenu
->data
;
1176 if (sym_is_choice(sym
) &&
1177 sym_get_tristate_value(sym
) == yes
)
1178 conf_choice(submenu
);
1179 else if (submenu
->prompt
&&
1180 submenu
->prompt
->type
== P_MENU
)
1183 sym_toggle_tristate_value(sym
);
1186 conf_string(submenu
);
1191 if (item_is_tag('t')) {
1192 if (sym_set_tristate_value(sym
, yes
))
1194 if (sym_set_tristate_value(sym
, mod
))
1195 btn_dialog(main_window
, setmod_text
, 0);
1199 if (item_is_tag('t'))
1200 sym_set_tristate_value(sym
, no
);
1203 if (item_is_tag('t'))
1204 sym_set_tristate_value(sym
, mod
);
1210 static void conf_message_callback(const char *s
)
1212 btn_dialog(main_window
, s
, 1, "<OK>");
1215 static void show_help(struct menu
*menu
)
1223 menu_get_ext_help(menu
, &help
);
1224 show_scroll_win(main_window
, menu_get_prompt(menu
), str_get(&help
));
1228 static void conf_choice(struct menu
*menu
)
1230 const char *prompt
= menu_get_prompt(menu
);
1231 struct menu
*child
= NULL
;
1232 struct symbol
*active
;
1233 struct property
*prop
;
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_get_choice_value(menu
->sym
);
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_get_choice_value(menu
->sym
))
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()))
1294 menu_driver(curses_menu
, REQ_DOWN_ITEM
);
1297 menu_driver(curses_menu
, REQ_UP_ITEM
);
1300 menu_driver(curses_menu
, REQ_SCR_DPAGE
);
1303 menu_driver(curses_menu
, REQ_SCR_UPAGE
);
1306 menu_driver(curses_menu
, REQ_FIRST_ITEM
);
1309 menu_driver(curses_menu
, REQ_LAST_ITEM
);
1313 show_help((struct menu
*) item_data());
1316 if (res
== 10 || res
== 27 || res
== ' ' ||
1320 refresh_all_windows(main_window
);
1322 /* if ESC or left */
1323 if (res
== 27 || res
== KEY_LEFT
)
1326 child
= item_data();
1327 if (!child
|| !menu_is_visible(child
) || !child
->sym
)
1333 if (sym_get_tristate_value(child
->sym
) != yes
) {
1334 for_all_properties(menu
->sym
, prop
, P_RESET
) {
1335 if (expr_calc_value(prop
->visible
.expr
) == no
)
1338 conf_reset(S_DEF_USER
);
1342 sym_set_tristate_value(child
->sym
, yes
);
1347 active
= child
->sym
;
1355 static void conf_string(struct menu
*menu
)
1357 const char *prompt
= menu_get_prompt(menu
);
1361 const char *heading
;
1363 switch (sym_get_type(menu
->sym
)) {
1365 heading
= inputbox_instructions_int
;
1368 heading
= inputbox_instructions_hex
;
1371 heading
= inputbox_instructions_string
;
1374 heading
= "Internal nconf error!";
1376 res
= dialog_inputbox(main_window
,
1377 prompt
? prompt
: "Main Menu",
1379 sym_get_string_value(menu
->sym
),
1380 &dialog_input_result
,
1381 &dialog_input_result_len
);
1384 if (sym_set_string_value(menu
->sym
,
1385 dialog_input_result
))
1387 btn_dialog(main_window
,
1388 "You have made an invalid entry.", 0);
1399 static void conf_load(void)
1403 res
= dialog_inputbox(main_window
,
1404 NULL
, load_config_text
,
1406 &dialog_input_result
,
1407 &dialog_input_result_len
);
1410 if (!dialog_input_result
[0])
1412 if (!conf_read(dialog_input_result
)) {
1413 set_config_filename(dialog_input_result
);
1414 conf_set_changed(true);
1417 btn_dialog(main_window
, "File does not exist!", 0);
1420 show_scroll_win(main_window
,
1421 "Load Alternate Configuration",
1430 static void conf_save(void)
1434 res
= dialog_inputbox(main_window
,
1435 NULL
, save_config_text
,
1437 &dialog_input_result
,
1438 &dialog_input_result_len
);
1441 if (!dialog_input_result
[0])
1443 res
= conf_write(dialog_input_result
);
1445 set_config_filename(dialog_input_result
);
1448 btn_dialog(main_window
, "Can't create file!",
1452 show_scroll_win(main_window
,
1453 "Save Alternate Configuration",
1462 static void setup_windows(void)
1466 getmaxyx(stdscr
, lines
, columns
);
1468 if (main_window
!= NULL
)
1469 delwin(main_window
);
1471 /* set up the menu and menu window */
1472 main_window
= newwin(lines
-2, columns
-2, 2, 1);
1473 keypad(main_window
, TRUE
);
1474 mwin_max_lines
= lines
-7;
1475 mwin_max_cols
= columns
-6;
1477 /* panels order is from bottom to top */
1478 new_panel(main_window
);
1481 int main(int ac
, char **av
)
1486 if (ac
> 1 && strcmp(av
[1], "-s") == 0) {
1487 /* Silence conf_read() until the real callback is set up */
1488 conf_set_message_callback(NULL
);
1494 mode
= getenv("NCONFIG_MODE");
1496 if (!strcasecmp(mode
, "single_menu"))
1497 single_menu_mode
= 1;
1500 /* Initialize curses */
1502 /* set color theme */
1507 keypad(stdscr
, TRUE
);
1510 getmaxyx(stdscr
, lines
, columns
);
1511 if (columns
< 75 || lines
< 20) {
1513 printf("Your terminal should have at "
1514 "least 20 lines and 75 columns\n");
1518 notimeout(stdscr
, FALSE
);
1519 #if NCURSES_REENTRANT
1526 curses_menu
= new_menu(curses_menu_items
);
1527 menu_opts_off(curses_menu
, O_SHOWDESC
);
1528 menu_opts_on(curses_menu
, O_SHOWMATCH
);
1529 menu_opts_on(curses_menu
, O_ONEVALUE
);
1530 menu_opts_on(curses_menu
, O_NONCYCLIC
);
1531 menu_opts_on(curses_menu
, O_IGNORECASE
);
1532 set_menu_mark(curses_menu
, " ");
1533 set_menu_fore(curses_menu
, attr_main_menu_fore
);
1534 set_menu_back(curses_menu
, attr_main_menu_back
);
1535 set_menu_grey(curses_menu
, attr_main_menu_grey
);
1537 set_config_filename(conf_get_configname());
1540 /* check for KEY_FUNC(1) */
1541 if (has_key(KEY_F(1)) == FALSE
) {
1542 show_scroll_win(main_window
,
1544 menu_no_f_instructions
);
1547 conf_set_message_callback(conf_message_callback
);
1549 while (!global_exit
) {
1551 if (!global_exit
&& do_exit() == 0)
1554 /* ok, we are done */
1555 unpost_menu(curses_menu
);
1556 free_menu(curses_menu
);
1557 delwin(main_window
);