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"
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 || IA64\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
;
285 static int jump_key_char
;
287 static void selected_conf(struct menu
*menu
, struct menu
*active_menu
);
288 static void conf(struct menu
*menu
);
289 static void conf_choice(struct menu
*menu
);
290 static void conf_string(struct menu
*menu
);
291 static void conf_load(void);
292 static void conf_save(void);
293 static void show_help(struct menu
*menu
);
294 static int do_exit(void);
295 static void setup_windows(void);
296 static void search_conf(void);
298 typedef void (*function_key_handler_t
)(int *key
, struct menu
*menu
);
299 static void handle_f1(int *key
, struct menu
*current_item
);
300 static void handle_f2(int *key
, struct menu
*current_item
);
301 static void handle_f3(int *key
, struct menu
*current_item
);
302 static void handle_f4(int *key
, struct menu
*current_item
);
303 static void handle_f5(int *key
, struct menu
*current_item
);
304 static void handle_f6(int *key
, struct menu
*current_item
);
305 static void handle_f7(int *key
, struct menu
*current_item
);
306 static void handle_f8(int *key
, struct menu
*current_item
);
307 static void handle_f9(int *key
, struct menu
*current_item
);
309 struct function_keys
{
313 function_key_handler_t handler
;
316 static const int function_keys_num
= 9;
317 static struct function_keys function_keys
[] = {
322 .handler
= handle_f1
,
328 .handler
= handle_f2
,
334 .handler
= handle_f3
,
340 .handler
= handle_f4
,
346 .handler
= handle_f5
,
352 .handler
= handle_f6
,
358 .handler
= handle_f7
,
364 .handler
= handle_f8
,
370 .handler
= handle_f9
,
374 static void print_function_line(void)
379 int lines
= getmaxy(stdscr
);
381 for (i
= 0; i
< function_keys_num
; i
++) {
382 wattrset(main_window
, attr_function_highlight
);
383 mvwprintw(main_window
, lines
-3, offset
,
385 function_keys
[i
].key_str
);
386 wattrset(main_window
, attr_function_text
);
387 offset
+= strlen(function_keys
[i
].key_str
);
388 mvwprintw(main_window
, lines
-3,
390 function_keys
[i
].func
);
391 offset
+= strlen(function_keys
[i
].func
) + skip
;
393 wattrset(main_window
, attr_normal
);
397 static void handle_f1(int *key
, struct menu
*current_item
)
399 show_scroll_win(main_window
,
400 "Global help", nconf_global_help
);
405 static void handle_f2(int *key
, struct menu
*current_item
)
407 show_help(current_item
);
412 static void handle_f3(int *key
, struct menu
*current_item
)
414 show_scroll_win(main_window
,
416 current_instructions
);
421 static void handle_f4(int *key
, struct menu
*current_item
)
423 int res
= btn_dialog(main_window
,
437 static void handle_f5(int *key
, struct menu
*current_item
)
444 static void handle_f6(int *key
, struct menu
*current_item
)
451 static void handle_f7(int *key
, struct menu
*current_item
)
458 static void handle_f8(int *key
, struct menu
*current_item
)
465 static void handle_f9(int *key
, struct menu
*current_item
)
471 /* return != 0 to indicate the key was handles */
472 static int process_special_keys(int *key
, struct menu
*menu
)
476 if (*key
== KEY_RESIZE
) {
481 for (i
= 0; i
< function_keys_num
; i
++) {
482 if (*key
== KEY_F(function_keys
[i
].key
) ||
483 *key
== '0' + function_keys
[i
].key
){
484 function_keys
[i
].handler(key
, menu
);
492 static void clean_items(void)
495 for (i
= 0; curses_menu_items
[i
]; i
++)
496 free_item(curses_menu_items
[i
]);
497 bzero(curses_menu_items
, sizeof(curses_menu_items
));
498 bzero(k_menu_items
, sizeof(k_menu_items
));
502 typedef enum {MATCH_TINKER_PATTERN_UP
, MATCH_TINKER_PATTERN_DOWN
,
503 FIND_NEXT_MATCH_DOWN
, FIND_NEXT_MATCH_UP
} match_f
;
505 /* return the index of the matched item, or -1 if no such item exists */
506 static int get_mext_match(const char *match_str
, match_f flag
)
508 int match_start
, index
;
510 /* Do not search if the menu is empty (i.e. items_num == 0) */
511 match_start
= item_index(current_item(curses_menu
));
512 if (match_start
== ERR
)
515 if (flag
== FIND_NEXT_MATCH_DOWN
)
517 else if (flag
== FIND_NEXT_MATCH_UP
)
520 match_start
= (match_start
+ items_num
) % items_num
;
523 char *str
= k_menu_items
[index
].str
;
524 if (strcasestr(str
, match_str
) != NULL
)
526 if (flag
== FIND_NEXT_MATCH_UP
||
527 flag
== MATCH_TINKER_PATTERN_UP
)
531 index
= (index
+ items_num
) % items_num
;
532 if (index
== match_start
)
537 /* Make a new item. */
538 static void item_make(struct menu
*menu
, char tag
, const char *fmt
, ...)
542 if (items_num
> MAX_MENU_ITEMS
-1)
545 bzero(&k_menu_items
[items_num
], sizeof(k_menu_items
[0]));
546 k_menu_items
[items_num
].tag
= tag
;
547 k_menu_items
[items_num
].usrptr
= menu
;
549 k_menu_items
[items_num
].is_visible
=
550 menu_is_visible(menu
);
552 k_menu_items
[items_num
].is_visible
= 1;
555 vsnprintf(k_menu_items
[items_num
].str
,
556 sizeof(k_menu_items
[items_num
].str
),
560 if (!k_menu_items
[items_num
].is_visible
)
561 memcpy(k_menu_items
[items_num
].str
, "XXX", 3);
563 curses_menu_items
[items_num
] = new_item(
564 k_menu_items
[items_num
].str
,
565 k_menu_items
[items_num
].str
);
566 set_item_userptr(curses_menu_items
[items_num
],
567 &k_menu_items
[items_num
]);
569 if (!k_menu_items[items_num].is_visible)
570 item_opts_off(curses_menu_items[items_num], O_SELECTABLE);
574 curses_menu_items
[items_num
] = NULL
;
577 /* very hackish. adds a string to the last item added */
578 static void item_add_str(const char *fmt
, ...)
581 int index
= items_num
-1;
589 vsnprintf(new_str
, sizeof(new_str
), fmt
, ap
);
591 snprintf(tmp_str
, sizeof(tmp_str
), "%s%s",
592 k_menu_items
[index
].str
, new_str
);
593 strncpy(k_menu_items
[index
].str
,
595 sizeof(k_menu_items
[index
].str
));
597 free_item(curses_menu_items
[index
]);
598 curses_menu_items
[index
] = new_item(
599 k_menu_items
[index
].str
,
600 k_menu_items
[index
].str
);
601 set_item_userptr(curses_menu_items
[index
],
602 &k_menu_items
[index
]);
605 /* get the tag of the currently selected item */
606 static char item_tag(void)
611 cur
= current_item(curses_menu
);
614 mcur
= (struct mitem
*) item_userptr(cur
);
618 static int curses_item_index(void)
620 return item_index(current_item(curses_menu
));
623 static void *item_data(void)
628 cur
= current_item(curses_menu
);
631 mcur
= (struct mitem
*) item_userptr(cur
);
636 static int item_is_tag(char tag
)
638 return item_tag() == tag
;
641 static char filename
[PATH_MAX
+1];
642 static char menu_backtitle
[PATH_MAX
+128];
643 static void set_config_filename(const char *config_filename
)
645 snprintf(menu_backtitle
, sizeof(menu_backtitle
), "%s - %s",
646 config_filename
, rootmenu
.prompt
->text
);
648 snprintf(filename
, sizeof(filename
), "%s", config_filename
);
651 /* return = 0 means we are successful.
652 * -1 means go on doing what you were doing
654 static int do_exit(void)
657 if (!conf_get_changed()) {
661 res
= btn_dialog(main_window
,
662 "Do you wish to save your new configuration?\n"
663 "<ESC> to cancel and resume nconfig.",
667 if (res
== KEY_EXIT
) {
672 /* if we got here, the user really wants to exit */
675 res
= conf_write(filename
);
679 "Error during writing of configuration.\n"
680 "Your configuration changes were NOT saved.",
683 conf_write_autoconf(0);
688 "Your configuration changes were NOT saved.",
698 struct list_head
*head
;
702 static int next_jump_key(int key
)
704 if (key
< '1' || key
> '9')
715 static int handle_search_keys(int key
, size_t start
, size_t end
, void *_data
)
717 struct search_data
*data
= _data
;
718 struct jump_key
*pos
;
721 if (key
< '1' || key
> '9')
724 list_for_each_entry(pos
, data
->head
, entries
) {
725 index
= next_jump_key(index
);
727 if (pos
->offset
< start
)
730 if (pos
->offset
>= end
)
734 data
->target
= pos
->target
;
742 int get_jump_key_char(void)
744 jump_key_char
= next_jump_key(jump_key_char
);
746 return jump_key_char
;
749 static void search_conf(void)
751 struct symbol
**sym_arr
;
755 int dres
, vscroll
= 0, hscroll
= 0;
759 str_printf( &title
, "Enter (sub)string or regexp to search for "
760 "(with or without \"%s\")", CONFIG_
);
763 dres
= dialog_inputbox(main_window
,
764 "Search Configuration Parameter",
766 "", &dialog_input_result
, &dialog_input_result_len
);
771 show_scroll_win(main_window
,
772 "Search Configuration", search_help
);
779 /* strip the prefix if necessary */
780 dialog_input
= dialog_input_result
;
781 if (strncasecmp(dialog_input_result
, CONFIG_
, strlen(CONFIG_
)) == 0)
782 dialog_input
+= strlen(CONFIG_
);
784 sym_arr
= sym_re_search(dialog_input
);
788 struct search_data data
= {
793 res
= get_relations_str(sym_arr
, &head
);
794 dres
= show_scroll_win_ext(main_window
,
795 "Search Results", str_get(&res
),
797 handle_search_keys
, &data
);
799 if (dres
>= '1' && dres
<= '9') {
800 assert(data
.target
!= NULL
);
801 selected_conf(data
.target
->parent
, data
.target
);
811 static void build_conf(struct menu
*menu
)
814 struct property
*prop
;
816 int type
, tmp
, doint
= 2;
820 if (!menu
|| (!show_all_items
&& !menu_is_visible(menu
)))
826 if (prop
&& menu
!= current_menu
) {
827 const char *prompt
= menu_get_prompt(menu
);
828 enum prop_type ptype
;
829 ptype
= menu
->prompt
? menu
->prompt
->type
: P_UNKNOWN
;
833 if (single_menu_mode
) {
836 menu
->data
? "-->" : "++>",
837 indent
+ 1, ' ', prompt
);
841 indent
+ 1, ' ', prompt
,
842 menu_is_empty(menu
) ? "----" : "--->");
844 if (single_menu_mode
&& menu
->data
)
859 item_make(menu
, ':', "---%*c%s",
869 type
= sym_get_type(sym
);
870 if (sym_is_choice(sym
)) {
871 struct symbol
*def_sym
= sym_get_choice_value(sym
);
872 struct menu
*def_menu
= NULL
;
875 for (child
= menu
->list
; child
; child
= child
->next
) {
876 if (menu_is_visible(child
) && child
->sym
== def_sym
)
880 val
= sym_get_tristate_value(sym
);
881 if (sym_is_changeable(sym
)) {
884 item_make(menu
, 't', "[%c]",
885 val
== no
? ' ' : '*');
899 item_make(menu
, 't', "<%c>", ch
);
903 item_make(menu
, def_menu
? 't' : ':', " ");
906 item_add_str("%*c%s", indent
+ 1,
907 ' ', menu_get_prompt(menu
));
910 item_add_str(" (%s)",
911 menu_get_prompt(def_menu
));
912 item_add_str(" --->");
913 if (def_menu
->list
) {
915 build_conf(def_menu
);
922 if (menu
== current_menu
) {
924 "---%*c%s", indent
+ 1,
925 ' ', menu_get_prompt(menu
));
929 val
= sym_get_tristate_value(sym
);
930 if (sym_is_choice_value(sym
) && val
== yes
) {
931 item_make(menu
, ':', " ");
935 if (sym_is_changeable(sym
))
936 item_make(menu
, 't', "[%c]",
937 val
== no
? ' ' : '*');
939 item_make(menu
, 't', "-%c-",
940 val
== no
? ' ' : '*');
954 if (sym_is_changeable(sym
)) {
955 if (sym
->rev_dep
.tri
== mod
)
962 item_make(menu
, 't', "-%c-", ch
);
965 tmp
= 2 + strlen(sym_get_string_value(sym
));
966 item_make(menu
, 's', " (%s)",
967 sym_get_string_value(sym
));
968 tmp
= indent
- tmp
+ 4;
971 item_add_str("%*c%s%s", tmp
, ' ',
972 menu_get_prompt(menu
),
973 (sym_has_value(sym
) ||
974 !sym_is_changeable(sym
)) ? "" :
979 item_add_str("%*c%s%s", indent
+ 1, ' ',
980 menu_get_prompt(menu
),
981 (sym_has_value(sym
) || !sym_is_changeable(sym
)) ?
983 if (menu
->prompt
&& menu
->prompt
->type
== P_MENU
) {
984 item_add_str(" %s", menu_is_empty(menu
) ? "----" : "--->");
991 for (child
= menu
->list
; child
; child
= child
->next
)
996 static void reset_menu(void)
998 unpost_menu(curses_menu
);
1002 /* adjust the menu to show this item.
1003 * prefer not to scroll the menu if possible*/
1004 static void center_item(int selected_index
, int *last_top_row
)
1008 set_top_row(curses_menu
, *last_top_row
);
1009 toprow
= top_row(curses_menu
);
1010 if (selected_index
< toprow
||
1011 selected_index
>= toprow
+mwin_max_lines
) {
1012 toprow
= max(selected_index
-mwin_max_lines
/2, 0);
1013 if (toprow
>= item_count(curses_menu
)-mwin_max_lines
)
1014 toprow
= item_count(curses_menu
)-mwin_max_lines
;
1015 set_top_row(curses_menu
, toprow
);
1017 set_current_item(curses_menu
,
1018 curses_menu_items
[selected_index
]);
1019 *last_top_row
= toprow
;
1020 post_menu(curses_menu
);
1021 refresh_all_windows(main_window
);
1024 /* this function assumes reset_menu has been called before */
1025 static void show_menu(const char *prompt
, const char *instructions
,
1026 int selected_index
, int *last_top_row
)
1029 WINDOW
*menu_window
;
1031 current_instructions
= instructions
;
1034 print_in_middle(stdscr
, 1, getmaxx(stdscr
),
1038 wattrset(main_window
, attr_main_menu_box
);
1039 box(main_window
, 0, 0);
1040 wattrset(main_window
, attr_main_menu_heading
);
1041 mvwprintw(main_window
, 0, 3, " %s ", prompt
);
1042 wattrset(main_window
, attr_normal
);
1044 set_menu_items(curses_menu
, curses_menu_items
);
1046 /* position the menu at the middle of the screen */
1047 scale_menu(curses_menu
, &maxy
, &maxx
);
1048 maxx
= min(maxx
, mwin_max_cols
-2);
1049 maxy
= mwin_max_lines
;
1050 menu_window
= derwin(main_window
,
1054 (mwin_max_cols
-maxx
)/2);
1055 keypad(menu_window
, TRUE
);
1056 set_menu_win(curses_menu
, menu_window
);
1057 set_menu_sub(curses_menu
, menu_window
);
1059 /* must reassert this after changing items, otherwise returns to a
1062 set_menu_format(curses_menu
, maxy
, 1);
1063 center_item(selected_index
, last_top_row
);
1064 set_menu_format(curses_menu
, maxy
, 1);
1066 print_function_line();
1069 post_menu(curses_menu
);
1070 refresh_all_windows(main_window
);
1073 static void adj_match_dir(match_f
*match_direction
)
1075 if (*match_direction
== FIND_NEXT_MATCH_DOWN
)
1077 MATCH_TINKER_PATTERN_DOWN
;
1078 else if (*match_direction
== FIND_NEXT_MATCH_UP
)
1080 MATCH_TINKER_PATTERN_UP
;
1081 /* else, do no change.. */
1087 match_f match_direction
;
1091 /* Return 0 means I have handled the key. In such a case, ans should hold the
1092 * item to center, or -1 otherwise.
1095 static int do_match(int key
, struct match_state
*state
, int *ans
)
1097 char c
= (char) key
;
1098 int terminate_search
= 0;
1100 if (key
== '/' || (state
->in_search
&& key
== 27)) {
1104 state
->in_search
= 1-state
->in_search
;
1105 bzero(state
->pattern
, sizeof(state
->pattern
));
1106 state
->match_direction
= MATCH_TINKER_PATTERN_DOWN
;
1108 } else if (!state
->in_search
)
1111 if (isalnum(c
) || isgraph(c
) || c
== ' ') {
1112 state
->pattern
[strlen(state
->pattern
)] = c
;
1113 state
->pattern
[strlen(state
->pattern
)] = '\0';
1114 adj_match_dir(&state
->match_direction
);
1115 *ans
= get_mext_match(state
->pattern
,
1116 state
->match_direction
);
1117 } else if (key
== KEY_DOWN
) {
1118 state
->match_direction
= FIND_NEXT_MATCH_DOWN
;
1119 *ans
= get_mext_match(state
->pattern
,
1120 state
->match_direction
);
1121 } else if (key
== KEY_UP
) {
1122 state
->match_direction
= FIND_NEXT_MATCH_UP
;
1123 *ans
= get_mext_match(state
->pattern
,
1124 state
->match_direction
);
1125 } else if (key
== KEY_BACKSPACE
|| key
== 8 || key
== 127) {
1126 state
->pattern
[strlen(state
->pattern
)-1] = '\0';
1127 adj_match_dir(&state
->match_direction
);
1129 terminate_search
= 1;
1131 if (terminate_search
) {
1132 state
->in_search
= 0;
1133 bzero(state
->pattern
, sizeof(state
->pattern
));
1142 static void conf(struct menu
*menu
)
1144 selected_conf(menu
, NULL
);
1147 static void selected_conf(struct menu
*menu
, struct menu
*active_menu
)
1149 struct menu
*submenu
= NULL
;
1152 int current_index
= 0;
1153 int last_top_row
= 0;
1154 struct match_state match_state
= {
1156 .match_direction
= MATCH_TINKER_PATTERN_DOWN
,
1160 while (!global_exit
) {
1162 current_menu
= menu
;
1167 if (active_menu
!= NULL
) {
1168 for (i
= 0; i
< items_num
; i
++) {
1171 mcur
= (struct mitem
*) item_userptr(curses_menu_items
[i
]);
1172 if ((struct menu
*) mcur
->usrptr
== active_menu
) {
1180 show_menu(menu_get_prompt(menu
), menu_instructions
,
1181 current_index
, &last_top_row
);
1182 keypad((menu_win(curses_menu
)), TRUE
);
1183 while (!global_exit
) {
1184 if (match_state
.in_search
) {
1186 "searching: %s", match_state
.pattern
);
1189 refresh_all_windows(main_window
);
1190 res
= wgetch(menu_win(curses_menu
));
1193 if (do_match(res
, &match_state
, ¤t_index
) == 0) {
1194 if (current_index
!= -1)
1195 center_item(current_index
,
1199 if (process_special_keys(&res
,
1200 (struct menu
*) item_data()))
1205 menu_driver(curses_menu
, REQ_DOWN_ITEM
);
1209 menu_driver(curses_menu
, REQ_UP_ITEM
);
1212 menu_driver(curses_menu
, REQ_SCR_DPAGE
);
1215 menu_driver(curses_menu
, REQ_SCR_UPAGE
);
1218 menu_driver(curses_menu
, REQ_FIRST_ITEM
);
1221 menu_driver(curses_menu
, REQ_LAST_ITEM
);
1225 show_help((struct menu
*) item_data());
1228 if (res
== 10 || res
== 27 ||
1229 res
== 32 || res
== 'n' || res
== 'y' ||
1230 res
== KEY_LEFT
|| res
== KEY_RIGHT
||
1233 refresh_all_windows(main_window
);
1236 refresh_all_windows(main_window
);
1238 if (res
== 27 || (menu
!= &rootmenu
&& res
== KEY_LEFT
))
1241 /* remember location in the menu */
1242 last_top_row
= top_row(curses_menu
);
1243 current_index
= curses_item_index();
1248 submenu
= (struct menu
*) item_data();
1249 if (!submenu
|| !menu_is_visible(submenu
))
1255 if (item_is_tag('t'))
1256 sym_toggle_tristate_value(sym
);
1257 else if (item_is_tag('m'))
1261 case 10: /* ENTER WAS PRESSED */
1262 switch (item_tag()) {
1264 if (single_menu_mode
)
1266 (void *) (long) !submenu
->data
;
1271 if (sym_is_choice(sym
) &&
1272 sym_get_tristate_value(sym
) == yes
)
1273 conf_choice(submenu
);
1274 else if (submenu
->prompt
&&
1275 submenu
->prompt
->type
== P_MENU
)
1278 sym_toggle_tristate_value(sym
);
1281 conf_string(submenu
);
1286 if (item_is_tag('t')) {
1287 if (sym_set_tristate_value(sym
, yes
))
1289 if (sym_set_tristate_value(sym
, mod
))
1290 btn_dialog(main_window
, setmod_text
, 0);
1294 if (item_is_tag('t'))
1295 sym_set_tristate_value(sym
, no
);
1298 if (item_is_tag('t'))
1299 sym_set_tristate_value(sym
, mod
);
1305 static void conf_message_callback(const char *s
)
1307 btn_dialog(main_window
, s
, 1, "<OK>");
1310 static void show_help(struct menu
*menu
)
1318 menu_get_ext_help(menu
, &help
);
1319 show_scroll_win(main_window
, menu_get_prompt(menu
), str_get(&help
));
1323 static void conf_choice(struct menu
*menu
)
1325 const char *prompt
= menu_get_prompt(menu
);
1326 struct menu
*child
= NULL
;
1327 struct symbol
*active
;
1328 struct property
*prop
;
1329 int selected_index
= 0;
1330 int last_top_row
= 0;
1332 struct match_state match_state
= {
1334 .match_direction
= MATCH_TINKER_PATTERN_DOWN
,
1338 active
= sym_get_choice_value(menu
->sym
);
1339 /* this is mostly duplicated from the conf() function. */
1340 while (!global_exit
) {
1343 for (i
= 0, child
= menu
->list
; child
; child
= child
->next
) {
1344 if (!show_all_items
&& !menu_is_visible(child
))
1347 if (child
->sym
== sym_get_choice_value(menu
->sym
))
1348 item_make(child
, ':', "<X> %s",
1349 menu_get_prompt(child
));
1350 else if (child
->sym
)
1351 item_make(child
, ':', " %s",
1352 menu_get_prompt(child
));
1354 item_make(child
, ':', "*** %s ***",
1355 menu_get_prompt(child
));
1357 if (child
->sym
== active
){
1358 last_top_row
= top_row(curses_menu
);
1363 show_menu(prompt
? prompt
: "Choice Menu",
1364 radiolist_instructions
,
1367 while (!global_exit
) {
1368 if (match_state
.in_search
) {
1369 mvprintw(0, 0, "searching: %s",
1370 match_state
.pattern
);
1373 refresh_all_windows(main_window
);
1374 res
= wgetch(menu_win(curses_menu
));
1377 if (do_match(res
, &match_state
, &selected_index
) == 0) {
1378 if (selected_index
!= -1)
1379 center_item(selected_index
,
1383 if (process_special_keys(
1385 (struct menu
*) item_data()))
1390 menu_driver(curses_menu
, REQ_DOWN_ITEM
);
1394 menu_driver(curses_menu
, REQ_UP_ITEM
);
1397 menu_driver(curses_menu
, REQ_SCR_DPAGE
);
1400 menu_driver(curses_menu
, REQ_SCR_UPAGE
);
1403 menu_driver(curses_menu
, REQ_FIRST_ITEM
);
1406 menu_driver(curses_menu
, REQ_LAST_ITEM
);
1410 show_help((struct menu
*) item_data());
1413 if (res
== 10 || res
== 27 || res
== ' ' ||
1417 refresh_all_windows(main_window
);
1419 /* if ESC or left */
1420 if (res
== 27 || res
== KEY_LEFT
)
1423 child
= item_data();
1424 if (!child
|| !menu_is_visible(child
) || !child
->sym
)
1430 if (sym_get_tristate_value(child
->sym
) != yes
) {
1431 for_all_properties(menu
->sym
, prop
, P_RESET
) {
1432 if (expr_calc_value(prop
->visible
.expr
) == no
)
1435 conf_reset(S_DEF_USER
);
1439 sym_set_tristate_value(child
->sym
, yes
);
1444 active
= child
->sym
;
1452 static void conf_string(struct menu
*menu
)
1454 const char *prompt
= menu_get_prompt(menu
);
1458 const char *heading
;
1460 switch (sym_get_type(menu
->sym
)) {
1462 heading
= inputbox_instructions_int
;
1465 heading
= inputbox_instructions_hex
;
1468 heading
= inputbox_instructions_string
;
1471 heading
= "Internal nconf error!";
1473 res
= dialog_inputbox(main_window
,
1474 prompt
? prompt
: "Main Menu",
1476 sym_get_string_value(menu
->sym
),
1477 &dialog_input_result
,
1478 &dialog_input_result_len
);
1481 if (sym_set_string_value(menu
->sym
,
1482 dialog_input_result
))
1484 btn_dialog(main_window
,
1485 "You have made an invalid entry.", 0);
1496 static void conf_load(void)
1500 res
= dialog_inputbox(main_window
,
1501 NULL
, load_config_text
,
1503 &dialog_input_result
,
1504 &dialog_input_result_len
);
1507 if (!dialog_input_result
[0])
1509 if (!conf_read(dialog_input_result
)) {
1510 set_config_filename(dialog_input_result
);
1511 conf_set_changed(true);
1514 btn_dialog(main_window
, "File does not exist!", 0);
1517 show_scroll_win(main_window
,
1518 "Load Alternate Configuration",
1527 static void conf_save(void)
1531 res
= dialog_inputbox(main_window
,
1532 NULL
, save_config_text
,
1534 &dialog_input_result
,
1535 &dialog_input_result_len
);
1538 if (!dialog_input_result
[0])
1540 res
= conf_write(dialog_input_result
);
1542 set_config_filename(dialog_input_result
);
1545 btn_dialog(main_window
, "Can't create file!",
1549 show_scroll_win(main_window
,
1550 "Save Alternate Configuration",
1559 static void setup_windows(void)
1563 getmaxyx(stdscr
, lines
, columns
);
1565 if (main_window
!= NULL
)
1566 delwin(main_window
);
1568 /* set up the menu and menu window */
1569 main_window
= newwin(lines
-2, columns
-2, 2, 1);
1570 keypad(main_window
, TRUE
);
1571 mwin_max_lines
= lines
-7;
1572 mwin_max_cols
= columns
-6;
1574 /* panels order is from bottom to top */
1575 new_panel(main_window
);
1578 int main(int ac
, char **av
)
1583 if (ac
> 1 && strcmp(av
[1], "-s") == 0) {
1584 /* Silence conf_read() until the real callback is set up */
1585 conf_set_message_callback(NULL
);
1591 mode
= getenv("NCONFIG_MODE");
1593 if (!strcasecmp(mode
, "single_menu"))
1594 single_menu_mode
= 1;
1597 /* Initialize curses */
1599 /* set color theme */
1604 keypad(stdscr
, TRUE
);
1607 getmaxyx(stdscr
, lines
, columns
);
1608 if (columns
< 75 || lines
< 20) {
1610 printf("Your terminal should have at "
1611 "least 20 lines and 75 columns\n");
1615 notimeout(stdscr
, FALSE
);
1616 #if NCURSES_REENTRANT
1623 curses_menu
= new_menu(curses_menu_items
);
1624 menu_opts_off(curses_menu
, O_SHOWDESC
);
1625 menu_opts_on(curses_menu
, O_SHOWMATCH
);
1626 menu_opts_on(curses_menu
, O_ONEVALUE
);
1627 menu_opts_on(curses_menu
, O_NONCYCLIC
);
1628 menu_opts_on(curses_menu
, O_IGNORECASE
);
1629 set_menu_mark(curses_menu
, " ");
1630 set_menu_fore(curses_menu
, attr_main_menu_fore
);
1631 set_menu_back(curses_menu
, attr_main_menu_back
);
1632 set_menu_grey(curses_menu
, attr_main_menu_grey
);
1634 set_config_filename(conf_get_configname());
1637 /* check for KEY_FUNC(1) */
1638 if (has_key(KEY_F(1)) == FALSE
) {
1639 show_scroll_win(main_window
,
1641 menu_no_f_instructions
);
1644 conf_set_message_callback(conf_message_callback
);
1646 while (!global_exit
) {
1648 if (!global_exit
&& do_exit() == 0)
1651 /* ok, we are done */
1652 unpost_menu(curses_menu
);
1653 free_menu(curses_menu
);
1654 delwin(main_window
);