1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com>
5 * Derived from menuconfig.
18 static const char nconf_global_help
[] =
21 "o Global help: Unless in a data entry window, pressing <F1> will give \n"
22 " you the global help window, which you are just reading.\n"
24 "o A short version of the global help is available by pressing <F3>.\n"
26 "o Local help: To get help related to the current menu entry, use any\n"
27 " of <?> <h>, or if in a data entry window then press <F1>.\n"
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 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 (void) wattrset(main_window
, attributes
[FUNCTION_HIGHLIGHT
]);
377 mvwprintw(main_window
, lines
-3, offset
,
379 function_keys
[i
].key_str
);
380 (void) wattrset(main_window
, attributes
[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 (void) wattrset(main_window
, attributes
[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
= item_index(current_item(curses_menu
));
505 if (flag
== FIND_NEXT_MATCH_DOWN
)
507 else if (flag
== FIND_NEXT_MATCH_UP
)
511 index
= (index
+ items_num
) % items_num
;
513 char *str
= k_menu_items
[index
].str
;
514 if (strcasestr(str
, match_str
) != NULL
)
516 if (flag
== FIND_NEXT_MATCH_UP
||
517 flag
== MATCH_TINKER_PATTERN_UP
)
521 index
= (index
+ items_num
) % items_num
;
522 if (index
== match_start
)
527 /* Make a new item. */
528 static void item_make(struct menu
*menu
, char tag
, const char *fmt
, ...)
532 if (items_num
> MAX_MENU_ITEMS
-1)
535 bzero(&k_menu_items
[items_num
], sizeof(k_menu_items
[0]));
536 k_menu_items
[items_num
].tag
= tag
;
537 k_menu_items
[items_num
].usrptr
= menu
;
539 k_menu_items
[items_num
].is_visible
=
540 menu_is_visible(menu
);
542 k_menu_items
[items_num
].is_visible
= 1;
545 vsnprintf(k_menu_items
[items_num
].str
,
546 sizeof(k_menu_items
[items_num
].str
),
550 if (!k_menu_items
[items_num
].is_visible
)
551 memcpy(k_menu_items
[items_num
].str
, "XXX", 3);
553 curses_menu_items
[items_num
] = new_item(
554 k_menu_items
[items_num
].str
,
555 k_menu_items
[items_num
].str
);
556 set_item_userptr(curses_menu_items
[items_num
],
557 &k_menu_items
[items_num
]);
559 if (!k_menu_items[items_num].is_visible)
560 item_opts_off(curses_menu_items[items_num], O_SELECTABLE);
564 curses_menu_items
[items_num
] = NULL
;
567 /* very hackish. adds a string to the last item added */
568 static void item_add_str(const char *fmt
, ...)
571 int index
= items_num
-1;
579 vsnprintf(new_str
, sizeof(new_str
), fmt
, ap
);
581 snprintf(tmp_str
, sizeof(tmp_str
), "%s%s",
582 k_menu_items
[index
].str
, new_str
);
583 strncpy(k_menu_items
[index
].str
,
585 sizeof(k_menu_items
[index
].str
));
587 free_item(curses_menu_items
[index
]);
588 curses_menu_items
[index
] = new_item(
589 k_menu_items
[index
].str
,
590 k_menu_items
[index
].str
);
591 set_item_userptr(curses_menu_items
[index
],
592 &k_menu_items
[index
]);
595 /* get the tag of the currently selected item */
596 static char item_tag(void)
601 cur
= current_item(curses_menu
);
604 mcur
= (struct mitem
*) item_userptr(cur
);
608 static int curses_item_index(void)
610 return item_index(current_item(curses_menu
));
613 static void *item_data(void)
618 cur
= current_item(curses_menu
);
621 mcur
= (struct mitem
*) item_userptr(cur
);
626 static int item_is_tag(char tag
)
628 return item_tag() == tag
;
631 static char filename
[PATH_MAX
+1];
632 static char menu_backtitle
[PATH_MAX
+128];
633 static const char *set_config_filename(const char *config_filename
)
637 size
= snprintf(menu_backtitle
, sizeof(menu_backtitle
),
638 "%s - %s", config_filename
, rootmenu
.prompt
->text
);
639 if (size
>= sizeof(menu_backtitle
))
640 menu_backtitle
[sizeof(menu_backtitle
)-1] = '\0';
642 size
= snprintf(filename
, sizeof(filename
), "%s", config_filename
);
643 if (size
>= sizeof(filename
))
644 filename
[sizeof(filename
)-1] = '\0';
645 return menu_backtitle
;
648 /* return = 0 means we are successful.
649 * -1 means go on doing what you were doing
651 static int do_exit(void)
654 if (!conf_get_changed()) {
658 res
= btn_dialog(main_window
,
659 "Do you wish to save your new configuration?\n"
660 "<ESC> to cancel and resume nconfig.",
664 if (res
== KEY_EXIT
) {
669 /* if we got here, the user really wants to exit */
672 res
= conf_write(filename
);
676 "Error during writing of configuration.\n"
677 "Your configuration changes were NOT saved.",
680 conf_write_autoconf(0);
685 "Your configuration changes were NOT saved.",
695 static void search_conf(void)
697 struct symbol
**sym_arr
;
704 str_printf( &title
, "Enter (sub)string or regexp to search for "
705 "(with or without \"%s\")", CONFIG_
);
708 dres
= dialog_inputbox(main_window
,
709 "Search Configuration Parameter",
711 "", &dialog_input_result
, &dialog_input_result_len
);
716 show_scroll_win(main_window
,
717 "Search Configuration", search_help
);
724 /* strip the prefix if necessary */
725 dialog_input
= dialog_input_result
;
726 if (strncasecmp(dialog_input_result
, CONFIG_
, strlen(CONFIG_
)) == 0)
727 dialog_input
+= strlen(CONFIG_
);
729 sym_arr
= sym_re_search(dialog_input
);
730 res
= get_relations_str(sym_arr
, NULL
);
732 show_scroll_win(main_window
,
733 "Search Results", str_get(&res
));
739 static void build_conf(struct menu
*menu
)
742 struct property
*prop
;
744 int type
, tmp
, doint
= 2;
748 if (!menu
|| (!show_all_items
&& !menu_is_visible(menu
)))
754 if (prop
&& menu
!= current_menu
) {
755 const char *prompt
= menu_get_prompt(menu
);
756 enum prop_type ptype
;
757 ptype
= menu
->prompt
? menu
->prompt
->type
: P_UNKNOWN
;
762 if (single_menu_mode
) {
765 menu
->data
? "-->" : "++>",
766 indent
+ 1, ' ', prompt
);
770 indent
+ 1, ' ', prompt
,
771 menu_is_empty(menu
) ? "----" : "--->");
773 if (single_menu_mode
&& menu
->data
)
788 item_make(menu
, ':', "---%*c%s",
798 type
= sym_get_type(sym
);
799 if (sym_is_choice(sym
)) {
800 struct symbol
*def_sym
= sym_get_choice_value(sym
);
801 struct menu
*def_menu
= NULL
;
804 for (child
= menu
->list
; child
; child
= child
->next
) {
805 if (menu_is_visible(child
) && child
->sym
== def_sym
)
809 val
= sym_get_tristate_value(sym
);
810 if (sym_is_changeable(sym
)) {
813 item_make(menu
, 't', "[%c]",
814 val
== no
? ' ' : '*');
828 item_make(menu
, 't', "<%c>", ch
);
832 item_make(menu
, def_menu
? 't' : ':', " ");
835 item_add_str("%*c%s", indent
+ 1,
836 ' ', menu_get_prompt(menu
));
839 item_add_str(" (%s)",
840 menu_get_prompt(def_menu
));
841 item_add_str(" --->");
842 if (def_menu
->list
) {
844 build_conf(def_menu
);
851 if (menu
== current_menu
) {
853 "---%*c%s", indent
+ 1,
854 ' ', menu_get_prompt(menu
));
858 val
= sym_get_tristate_value(sym
);
859 if (sym_is_choice_value(sym
) && val
== yes
) {
860 item_make(menu
, ':', " ");
864 if (sym_is_changeable(sym
))
865 item_make(menu
, 't', "[%c]",
866 val
== no
? ' ' : '*');
868 item_make(menu
, 't', "-%c-",
869 val
== no
? ' ' : '*');
883 if (sym_is_changeable(sym
)) {
884 if (sym
->rev_dep
.tri
== mod
)
891 item_make(menu
, 't', "-%c-", ch
);
894 tmp
= 2 + strlen(sym_get_string_value(sym
));
895 item_make(menu
, 's', " (%s)",
896 sym_get_string_value(sym
));
897 tmp
= indent
- tmp
+ 4;
900 item_add_str("%*c%s%s", tmp
, ' ',
901 menu_get_prompt(menu
),
902 (sym_has_value(sym
) ||
903 !sym_is_changeable(sym
)) ? "" :
908 item_add_str("%*c%s%s", indent
+ 1, ' ',
909 menu_get_prompt(menu
),
910 (sym_has_value(sym
) || !sym_is_changeable(sym
)) ?
912 if (menu
->prompt
&& menu
->prompt
->type
== P_MENU
) {
913 item_add_str(" %s", menu_is_empty(menu
) ? "----" : "--->");
920 for (child
= menu
->list
; child
; child
= child
->next
)
925 static void reset_menu(void)
927 unpost_menu(curses_menu
);
931 /* adjust the menu to show this item.
932 * prefer not to scroll the menu if possible*/
933 static void center_item(int selected_index
, int *last_top_row
)
937 set_top_row(curses_menu
, *last_top_row
);
938 toprow
= top_row(curses_menu
);
939 if (selected_index
< toprow
||
940 selected_index
>= toprow
+mwin_max_lines
) {
941 toprow
= max(selected_index
-mwin_max_lines
/2, 0);
942 if (toprow
>= item_count(curses_menu
)-mwin_max_lines
)
943 toprow
= item_count(curses_menu
)-mwin_max_lines
;
944 set_top_row(curses_menu
, toprow
);
946 set_current_item(curses_menu
,
947 curses_menu_items
[selected_index
]);
948 *last_top_row
= toprow
;
949 post_menu(curses_menu
);
950 refresh_all_windows(main_window
);
953 /* this function assumes reset_menu has been called before */
954 static void show_menu(const char *prompt
, const char *instructions
,
955 int selected_index
, int *last_top_row
)
960 current_instructions
= instructions
;
963 (void) wattrset(main_window
, attributes
[NORMAL
]);
964 print_in_middle(stdscr
, 1, 0, getmaxx(stdscr
),
966 attributes
[MAIN_HEADING
]);
968 (void) wattrset(main_window
, attributes
[MAIN_MENU_BOX
]);
969 box(main_window
, 0, 0);
970 (void) wattrset(main_window
, attributes
[MAIN_MENU_HEADING
]);
971 mvwprintw(main_window
, 0, 3, " %s ", prompt
);
972 (void) wattrset(main_window
, attributes
[NORMAL
]);
974 set_menu_items(curses_menu
, curses_menu_items
);
976 /* position the menu at the middle of the screen */
977 scale_menu(curses_menu
, &maxy
, &maxx
);
978 maxx
= min(maxx
, mwin_max_cols
-2);
979 maxy
= mwin_max_lines
;
980 menu_window
= derwin(main_window
,
984 (mwin_max_cols
-maxx
)/2);
985 keypad(menu_window
, TRUE
);
986 set_menu_win(curses_menu
, menu_window
);
987 set_menu_sub(curses_menu
, menu_window
);
989 /* must reassert this after changing items, otherwise returns to a
992 set_menu_format(curses_menu
, maxy
, 1);
993 center_item(selected_index
, last_top_row
);
994 set_menu_format(curses_menu
, maxy
, 1);
996 print_function_line();
999 post_menu(curses_menu
);
1000 refresh_all_windows(main_window
);
1003 static void adj_match_dir(match_f
*match_direction
)
1005 if (*match_direction
== FIND_NEXT_MATCH_DOWN
)
1007 MATCH_TINKER_PATTERN_DOWN
;
1008 else if (*match_direction
== FIND_NEXT_MATCH_UP
)
1010 MATCH_TINKER_PATTERN_UP
;
1011 /* else, do no change.. */
1017 match_f match_direction
;
1021 /* Return 0 means I have handled the key. In such a case, ans should hold the
1022 * item to center, or -1 otherwise.
1025 static int do_match(int key
, struct match_state
*state
, int *ans
)
1027 char c
= (char) key
;
1028 int terminate_search
= 0;
1030 if (key
== '/' || (state
->in_search
&& key
== 27)) {
1034 state
->in_search
= 1-state
->in_search
;
1035 bzero(state
->pattern
, sizeof(state
->pattern
));
1036 state
->match_direction
= MATCH_TINKER_PATTERN_DOWN
;
1038 } else if (!state
->in_search
)
1041 if (isalnum(c
) || isgraph(c
) || c
== ' ') {
1042 state
->pattern
[strlen(state
->pattern
)] = c
;
1043 state
->pattern
[strlen(state
->pattern
)] = '\0';
1044 adj_match_dir(&state
->match_direction
);
1045 *ans
= get_mext_match(state
->pattern
,
1046 state
->match_direction
);
1047 } else if (key
== KEY_DOWN
) {
1048 state
->match_direction
= FIND_NEXT_MATCH_DOWN
;
1049 *ans
= get_mext_match(state
->pattern
,
1050 state
->match_direction
);
1051 } else if (key
== KEY_UP
) {
1052 state
->match_direction
= FIND_NEXT_MATCH_UP
;
1053 *ans
= get_mext_match(state
->pattern
,
1054 state
->match_direction
);
1055 } else if (key
== KEY_BACKSPACE
|| key
== 8 || key
== 127) {
1056 state
->pattern
[strlen(state
->pattern
)-1] = '\0';
1057 adj_match_dir(&state
->match_direction
);
1059 terminate_search
= 1;
1061 if (terminate_search
) {
1062 state
->in_search
= 0;
1063 bzero(state
->pattern
, sizeof(state
->pattern
));
1072 static void conf(struct menu
*menu
)
1074 struct menu
*submenu
= NULL
;
1075 const char *prompt
= menu_get_prompt(menu
);
1078 int current_index
= 0;
1079 int last_top_row
= 0;
1080 struct match_state match_state
= {
1082 .match_direction
= MATCH_TINKER_PATTERN_DOWN
,
1086 while (!global_exit
) {
1088 current_menu
= menu
;
1093 show_menu(prompt
? prompt
: "Main Menu",
1095 current_index
, &last_top_row
);
1096 keypad((menu_win(curses_menu
)), TRUE
);
1097 while (!global_exit
) {
1098 if (match_state
.in_search
) {
1100 "searching: %s", match_state
.pattern
);
1103 refresh_all_windows(main_window
);
1104 res
= wgetch(menu_win(curses_menu
));
1107 if (do_match(res
, &match_state
, ¤t_index
) == 0) {
1108 if (current_index
!= -1)
1109 center_item(current_index
,
1113 if (process_special_keys(&res
,
1114 (struct menu
*) item_data()))
1118 menu_driver(curses_menu
, REQ_DOWN_ITEM
);
1121 menu_driver(curses_menu
, REQ_UP_ITEM
);
1124 menu_driver(curses_menu
, REQ_SCR_DPAGE
);
1127 menu_driver(curses_menu
, REQ_SCR_UPAGE
);
1130 menu_driver(curses_menu
, REQ_FIRST_ITEM
);
1133 menu_driver(curses_menu
, REQ_LAST_ITEM
);
1137 show_help((struct menu
*) item_data());
1140 if (res
== 10 || res
== 27 ||
1141 res
== 32 || res
== 'n' || res
== 'y' ||
1142 res
== KEY_LEFT
|| res
== KEY_RIGHT
||
1145 refresh_all_windows(main_window
);
1148 refresh_all_windows(main_window
);
1150 if (res
== 27 || (menu
!= &rootmenu
&& res
== KEY_LEFT
))
1153 /* remember location in the menu */
1154 last_top_row
= top_row(curses_menu
);
1155 current_index
= curses_item_index();
1160 submenu
= (struct menu
*) item_data();
1161 if (!submenu
|| !menu_is_visible(submenu
))
1167 if (item_is_tag('t'))
1168 sym_toggle_tristate_value(sym
);
1169 else if (item_is_tag('m'))
1173 case 10: /* ENTER WAS PRESSED */
1174 switch (item_tag()) {
1176 if (single_menu_mode
)
1178 (void *) (long) !submenu
->data
;
1183 if (sym_is_choice(sym
) &&
1184 sym_get_tristate_value(sym
) == yes
)
1185 conf_choice(submenu
);
1186 else if (submenu
->prompt
&&
1187 submenu
->prompt
->type
== P_MENU
)
1190 sym_toggle_tristate_value(sym
);
1193 conf_string(submenu
);
1198 if (item_is_tag('t')) {
1199 if (sym_set_tristate_value(sym
, yes
))
1201 if (sym_set_tristate_value(sym
, mod
))
1202 btn_dialog(main_window
, setmod_text
, 0);
1206 if (item_is_tag('t'))
1207 sym_set_tristate_value(sym
, no
);
1210 if (item_is_tag('t'))
1211 sym_set_tristate_value(sym
, mod
);
1217 static void conf_message_callback(const char *s
)
1219 btn_dialog(main_window
, s
, 1, "<OK>");
1222 static void show_help(struct menu
*menu
)
1230 menu_get_ext_help(menu
, &help
);
1231 show_scroll_win(main_window
, menu_get_prompt(menu
), str_get(&help
));
1235 static void conf_choice(struct menu
*menu
)
1237 const char *prompt
= menu_get_prompt(menu
);
1238 struct menu
*child
= NULL
;
1239 struct symbol
*active
;
1240 struct property
*prop
;
1241 int selected_index
= 0;
1242 int last_top_row
= 0;
1244 struct match_state match_state
= {
1246 .match_direction
= MATCH_TINKER_PATTERN_DOWN
,
1250 active
= sym_get_choice_value(menu
->sym
);
1251 /* this is mostly duplicated from the conf() function. */
1252 while (!global_exit
) {
1255 for (i
= 0, child
= menu
->list
; child
; child
= child
->next
) {
1256 if (!show_all_items
&& !menu_is_visible(child
))
1259 if (child
->sym
== sym_get_choice_value(menu
->sym
))
1260 item_make(child
, ':', "<X> %s",
1261 menu_get_prompt(child
));
1262 else if (child
->sym
)
1263 item_make(child
, ':', " %s",
1264 menu_get_prompt(child
));
1266 item_make(child
, ':', "*** %s ***",
1267 menu_get_prompt(child
));
1269 if (child
->sym
== active
){
1270 last_top_row
= top_row(curses_menu
);
1275 show_menu(prompt
? prompt
: "Choice Menu",
1276 radiolist_instructions
,
1279 while (!global_exit
) {
1280 if (match_state
.in_search
) {
1281 mvprintw(0, 0, "searching: %s",
1282 match_state
.pattern
);
1285 refresh_all_windows(main_window
);
1286 res
= wgetch(menu_win(curses_menu
));
1289 if (do_match(res
, &match_state
, &selected_index
) == 0) {
1290 if (selected_index
!= -1)
1291 center_item(selected_index
,
1295 if (process_special_keys(
1297 (struct menu
*) item_data()))
1301 menu_driver(curses_menu
, REQ_DOWN_ITEM
);
1304 menu_driver(curses_menu
, REQ_UP_ITEM
);
1307 menu_driver(curses_menu
, REQ_SCR_DPAGE
);
1310 menu_driver(curses_menu
, REQ_SCR_UPAGE
);
1313 menu_driver(curses_menu
, REQ_FIRST_ITEM
);
1316 menu_driver(curses_menu
, REQ_LAST_ITEM
);
1320 show_help((struct menu
*) item_data());
1323 if (res
== 10 || res
== 27 || res
== ' ' ||
1327 refresh_all_windows(main_window
);
1329 /* if ESC or left */
1330 if (res
== 27 || res
== KEY_LEFT
)
1333 child
= item_data();
1334 if (!child
|| !menu_is_visible(child
) || !child
->sym
)
1340 if (sym_get_tristate_value(child
->sym
) != yes
) {
1341 for_all_properties(menu
->sym
, prop
, P_RESET
) {
1342 if (expr_calc_value(prop
->visible
.expr
) == no
)
1345 conf_reset(S_DEF_USER
);
1349 sym_set_tristate_value(child
->sym
, yes
);
1354 active
= child
->sym
;
1362 static void conf_string(struct menu
*menu
)
1364 const char *prompt
= menu_get_prompt(menu
);
1368 const char *heading
;
1370 switch (sym_get_type(menu
->sym
)) {
1372 heading
= inputbox_instructions_int
;
1375 heading
= inputbox_instructions_hex
;
1378 heading
= inputbox_instructions_string
;
1381 heading
= "Internal nconf error!";
1383 res
= dialog_inputbox(main_window
,
1384 prompt
? prompt
: "Main Menu",
1386 sym_get_string_value(menu
->sym
),
1387 &dialog_input_result
,
1388 &dialog_input_result_len
);
1391 if (sym_set_string_value(menu
->sym
,
1392 dialog_input_result
))
1394 btn_dialog(main_window
,
1395 "You have made an invalid entry.", 0);
1406 static void conf_load(void)
1410 res
= dialog_inputbox(main_window
,
1411 NULL
, load_config_text
,
1413 &dialog_input_result
,
1414 &dialog_input_result_len
);
1417 if (!dialog_input_result
[0])
1419 if (!conf_read(dialog_input_result
)) {
1420 set_config_filename(dialog_input_result
);
1421 sym_set_change_count(1);
1424 btn_dialog(main_window
, "File does not exist!", 0);
1427 show_scroll_win(main_window
,
1428 "Load Alternate Configuration",
1437 static void conf_save(void)
1441 res
= dialog_inputbox(main_window
,
1442 NULL
, save_config_text
,
1444 &dialog_input_result
,
1445 &dialog_input_result_len
);
1448 if (!dialog_input_result
[0])
1450 res
= conf_write(dialog_input_result
);
1452 set_config_filename(dialog_input_result
);
1455 btn_dialog(main_window
, "Can't create file!",
1459 show_scroll_win(main_window
,
1460 "Save Alternate Configuration",
1469 static void setup_windows(void)
1473 getmaxyx(stdscr
, lines
, columns
);
1475 if (main_window
!= NULL
)
1476 delwin(main_window
);
1478 /* set up the menu and menu window */
1479 main_window
= newwin(lines
-2, columns
-2, 2, 1);
1480 keypad(main_window
, TRUE
);
1481 mwin_max_lines
= lines
-7;
1482 mwin_max_cols
= columns
-6;
1484 /* panels order is from bottom to top */
1485 new_panel(main_window
);
1488 int main(int ac
, char **av
)
1493 if (ac
> 1 && strcmp(av
[1], "-s") == 0) {
1494 /* Silence conf_read() until the real callback is set up */
1495 conf_set_message_callback(NULL
);
1501 mode
= getenv("NCONFIG_MODE");
1503 if (!strcasecmp(mode
, "single_menu"))
1504 single_menu_mode
= 1;
1507 /* Initialize curses */
1509 /* set color theme */
1514 keypad(stdscr
, TRUE
);
1517 getmaxyx(stdscr
, lines
, columns
);
1518 if (columns
< 75 || lines
< 20) {
1520 printf("Your terminal should have at "
1521 "least 20 lines and 75 columns\n");
1525 notimeout(stdscr
, FALSE
);
1526 #if NCURSES_REENTRANT
1533 curses_menu
= new_menu(curses_menu_items
);
1534 menu_opts_off(curses_menu
, O_SHOWDESC
);
1535 menu_opts_on(curses_menu
, O_SHOWMATCH
);
1536 menu_opts_on(curses_menu
, O_ONEVALUE
);
1537 menu_opts_on(curses_menu
, O_NONCYCLIC
);
1538 menu_opts_on(curses_menu
, O_IGNORECASE
);
1539 set_menu_mark(curses_menu
, " ");
1540 set_menu_fore(curses_menu
, attributes
[MAIN_MENU_FORE
]);
1541 set_menu_back(curses_menu
, attributes
[MAIN_MENU_BACK
]);
1542 set_menu_grey(curses_menu
, attributes
[MAIN_MENU_GREY
]);
1544 set_config_filename(conf_get_configname());
1547 /* check for KEY_FUNC(1) */
1548 if (has_key(KEY_F(1)) == FALSE
) {
1549 show_scroll_win(main_window
,
1551 menu_no_f_instructions
);
1554 conf_set_message_callback(conf_message_callback
);
1556 while (!global_exit
) {
1558 if (!global_exit
&& do_exit() == 0)
1561 /* ok, we are done */
1562 unpost_menu(curses_menu
);
1563 free_menu(curses_menu
);
1564 delwin(main_window
);