build: scripts/config - update to kconfig-v6.6.16
[openwrt/staging/mans0n.git] / scripts / config / nconf.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com>
4 *
5 * Derived from menuconfig.
6 */
7 #ifndef _GNU_SOURCE
8 #define _GNU_SOURCE
9 #endif
10 #include <string.h>
11 #include <strings.h>
12 #include <stdlib.h>
13
14 #include "lkc.h"
15 #include "nconf.h"
16 #include <ctype.h>
17
18 static const char nconf_global_help[] =
19 "Help windows\n"
20 "------------\n"
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"
23 "\n"
24 "o A short version of the global help is available by pressing <F3>.\n"
25 "\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"
28 "\n"
29 "\n"
30 "OpenWrt config is based on Kernel kconfig\n"
31 "so ipkg packages are referred here as modules.\n"
32 "\n"
33 "Menu entries\n"
34 "------------\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"
38 "\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"
47 "\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"
52 "\n"
53 "A trailing \"--->\" designates a submenu, a trailing \"----\" an\n"
54 "empty submenu.\n"
55 "\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"
63 "Last entry <End>\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"
71 " </>STRING\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"
80 "\n"
81 "Unless in a data entry window, key <1> may be used instead of <F1>,\n"
82 "<2> instead of <F2>, etc.\n"
83 "\n"
84 "\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"
89 "\n"
90 "\n"
91 "Data entry\n"
92 "----------\n"
93 "Enter the requested information and press <Enter>. Hexadecimal values\n"
94 "may be entered without the \"0x\" prefix.\n"
95 "\n"
96 "\n"
97 "Text Box (Help Window)\n"
98 "----------------------\n"
99 "Use movement keys as listed in table above.\n"
100 "\n"
101 "Press any of <Enter> <Esc> <q> <F5> <F9> to exit.\n"
102 "\n"
103 "\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"
109 "\n"
110 "\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"
116 "\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"
120 "\n"
121 "\n"
122 "Single menu mode\n"
123 "----------------\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"
127 "\n"
128 "make NCONFIG_MODE=single_menu nconfig\n"
129 "\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"
133 "\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"
136 "\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"
140 "\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"
149 "\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"
157 "\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"
166 "\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"
172 "with <Space>.\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.",
185 setmod_text[] =
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.",
188 load_config_text[] =
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.",
192 load_config_help[] =
193 "For various reasons, one may wish to keep several different\n"
194 "configurations available on a single machine.\n"
195 "\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"
199 "\n"
200 "Leave empty to abort.\n",
201 save_config_text[] =
202 "Enter a filename to which this configuration should be saved\n"
203 "as an alternate. Leave empty to abort.",
204 save_config_help[] =
205 "For various reasons, one may wish to keep several different\n"
206 "configurations available on a single machine.\n"
207 "\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"
211 "\n"
212 "Leave empty to abort.\n",
213 search_help[] =
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"
217 "Result:\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"
223 "Location:\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"
228 "Selected by: BAR\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"
233 " defined.\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.\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"
240 " brackets.\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"
247 "\n"
248 "Only relevant lines are shown.\n"
249 "\n\n"
250 "Search examples:\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"
254 "\n";
255
256 struct mitem {
257 char str[256];
258 char tag;
259 void *usrptr;
260 int is_visible;
261 };
262
263 #define MAX_MENU_ITEMS 4096
264 static int show_all_items;
265 static int indent;
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;
282
283 static char *dialog_input_result;
284 static int dialog_input_result_len;
285 static int jump_key_char;
286
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);
297
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);
308
309 struct function_keys {
310 const char *key_str;
311 const char *func;
312 function_key key;
313 function_key_handler_t handler;
314 };
315
316 static const int function_keys_num = 9;
317 static struct function_keys function_keys[] = {
318 {
319 .key_str = "F1",
320 .func = "Help",
321 .key = F_HELP,
322 .handler = handle_f1,
323 },
324 {
325 .key_str = "F2",
326 .func = "SymInfo",
327 .key = F_SYMBOL,
328 .handler = handle_f2,
329 },
330 {
331 .key_str = "F3",
332 .func = "Help 2",
333 .key = F_INSTS,
334 .handler = handle_f3,
335 },
336 {
337 .key_str = "F4",
338 .func = "ShowAll",
339 .key = F_CONF,
340 .handler = handle_f4,
341 },
342 {
343 .key_str = "F5",
344 .func = "Back",
345 .key = F_BACK,
346 .handler = handle_f5,
347 },
348 {
349 .key_str = "F6",
350 .func = "Save",
351 .key = F_SAVE,
352 .handler = handle_f6,
353 },
354 {
355 .key_str = "F7",
356 .func = "Load",
357 .key = F_LOAD,
358 .handler = handle_f7,
359 },
360 {
361 .key_str = "F8",
362 .func = "SymSearch",
363 .key = F_SEARCH,
364 .handler = handle_f8,
365 },
366 {
367 .key_str = "F9",
368 .func = "Exit",
369 .key = F_EXIT,
370 .handler = handle_f9,
371 },
372 };
373
374 static void print_function_line(void)
375 {
376 int i;
377 int offset = 1;
378 const int skip = 1;
379 int lines = getmaxy(stdscr);
380
381 for (i = 0; i < function_keys_num; i++) {
382 wattrset(main_window, attr_function_highlight);
383 mvwprintw(main_window, lines-3, offset,
384 "%s",
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,
389 offset, "%s",
390 function_keys[i].func);
391 offset += strlen(function_keys[i].func) + skip;
392 }
393 wattrset(main_window, attr_normal);
394 }
395
396 /* help */
397 static void handle_f1(int *key, struct menu *current_item)
398 {
399 show_scroll_win(main_window,
400 "Global help", nconf_global_help);
401 return;
402 }
403
404 /* symbole help */
405 static void handle_f2(int *key, struct menu *current_item)
406 {
407 show_help(current_item);
408 return;
409 }
410
411 /* instructions */
412 static void handle_f3(int *key, struct menu *current_item)
413 {
414 show_scroll_win(main_window,
415 "Short help",
416 current_instructions);
417 return;
418 }
419
420 /* config */
421 static void handle_f4(int *key, struct menu *current_item)
422 {
423 int res = btn_dialog(main_window,
424 "Show all symbols?",
425 2,
426 " <Show All> ",
427 "<Don't show all>");
428 if (res == 0)
429 show_all_items = 1;
430 else if (res == 1)
431 show_all_items = 0;
432
433 return;
434 }
435
436 /* back */
437 static void handle_f5(int *key, struct menu *current_item)
438 {
439 *key = KEY_LEFT;
440 return;
441 }
442
443 /* save */
444 static void handle_f6(int *key, struct menu *current_item)
445 {
446 conf_save();
447 return;
448 }
449
450 /* load */
451 static void handle_f7(int *key, struct menu *current_item)
452 {
453 conf_load();
454 return;
455 }
456
457 /* search */
458 static void handle_f8(int *key, struct menu *current_item)
459 {
460 search_conf();
461 return;
462 }
463
464 /* exit */
465 static void handle_f9(int *key, struct menu *current_item)
466 {
467 do_exit();
468 return;
469 }
470
471 /* return != 0 to indicate the key was handles */
472 static int process_special_keys(int *key, struct menu *menu)
473 {
474 int i;
475
476 if (*key == KEY_RESIZE) {
477 setup_windows();
478 return 1;
479 }
480
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);
485 return 1;
486 }
487 }
488
489 return 0;
490 }
491
492 static void clean_items(void)
493 {
494 int i;
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));
499 items_num = 0;
500 }
501
502 typedef enum {MATCH_TINKER_PATTERN_UP, MATCH_TINKER_PATTERN_DOWN,
503 FIND_NEXT_MATCH_DOWN, FIND_NEXT_MATCH_UP} match_f;
504
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)
507 {
508 int match_start, index;
509
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)
513 return -1;
514
515 if (flag == FIND_NEXT_MATCH_DOWN)
516 ++match_start;
517 else if (flag == FIND_NEXT_MATCH_UP)
518 --match_start;
519
520 match_start = (match_start + items_num) % items_num;
521 index = match_start;
522 while (true) {
523 char *str = k_menu_items[index].str;
524 if (strcasestr(str, match_str) != NULL)
525 return index;
526 if (flag == FIND_NEXT_MATCH_UP ||
527 flag == MATCH_TINKER_PATTERN_UP)
528 --index;
529 else
530 ++index;
531 index = (index + items_num) % items_num;
532 if (index == match_start)
533 return -1;
534 }
535 }
536
537 /* Make a new item. */
538 static void item_make(struct menu *menu, char tag, const char *fmt, ...)
539 {
540 va_list ap;
541
542 if (items_num > MAX_MENU_ITEMS-1)
543 return;
544
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;
548 if (menu != NULL)
549 k_menu_items[items_num].is_visible =
550 menu_is_visible(menu);
551 else
552 k_menu_items[items_num].is_visible = 1;
553
554 va_start(ap, fmt);
555 vsnprintf(k_menu_items[items_num].str,
556 sizeof(k_menu_items[items_num].str),
557 fmt, ap);
558 va_end(ap);
559
560 if (!k_menu_items[items_num].is_visible)
561 memcpy(k_menu_items[items_num].str, "XXX", 3);
562
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]);
568 /*
569 if (!k_menu_items[items_num].is_visible)
570 item_opts_off(curses_menu_items[items_num], O_SELECTABLE);
571 */
572
573 items_num++;
574 curses_menu_items[items_num] = NULL;
575 }
576
577 /* very hackish. adds a string to the last item added */
578 static void item_add_str(const char *fmt, ...)
579 {
580 va_list ap;
581 int index = items_num-1;
582 char new_str[256];
583 char tmp_str[256];
584
585 if (index < 0)
586 return;
587
588 va_start(ap, fmt);
589 vsnprintf(new_str, sizeof(new_str), fmt, ap);
590 va_end(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,
594 tmp_str,
595 sizeof(k_menu_items[index].str));
596
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]);
603 }
604
605 /* get the tag of the currently selected item */
606 static char item_tag(void)
607 {
608 ITEM *cur;
609 struct mitem *mcur;
610
611 cur = current_item(curses_menu);
612 if (cur == NULL)
613 return 0;
614 mcur = (struct mitem *) item_userptr(cur);
615 return mcur->tag;
616 }
617
618 static int curses_item_index(void)
619 {
620 return item_index(current_item(curses_menu));
621 }
622
623 static void *item_data(void)
624 {
625 ITEM *cur;
626 struct mitem *mcur;
627
628 cur = current_item(curses_menu);
629 if (!cur)
630 return NULL;
631 mcur = (struct mitem *) item_userptr(cur);
632 return mcur->usrptr;
633
634 }
635
636 static int item_is_tag(char tag)
637 {
638 return item_tag() == tag;
639 }
640
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)
644 {
645 snprintf(menu_backtitle, sizeof(menu_backtitle), "%s - %s",
646 config_filename, rootmenu.prompt->text);
647
648 snprintf(filename, sizeof(filename), "%s", config_filename);
649 }
650
651 /* return = 0 means we are successful.
652 * -1 means go on doing what you were doing
653 */
654 static int do_exit(void)
655 {
656 int res;
657 if (!conf_get_changed()) {
658 global_exit = 1;
659 return 0;
660 }
661 res = btn_dialog(main_window,
662 "Do you wish to save your new configuration?\n"
663 "<ESC> to cancel and resume nconfig.",
664 2,
665 " <save> ",
666 "<don't save>");
667 if (res == KEY_EXIT) {
668 global_exit = 0;
669 return -1;
670 }
671
672 /* if we got here, the user really wants to exit */
673 switch (res) {
674 case 0:
675 res = conf_write(filename);
676 if (res)
677 btn_dialog(
678 main_window,
679 "Error during writing of configuration.\n"
680 "Your configuration changes were NOT saved.",
681 1,
682 "<OK>");
683 conf_write_autoconf(0);
684 break;
685 default:
686 btn_dialog(
687 main_window,
688 "Your configuration changes were NOT saved.",
689 1,
690 "<OK>");
691 break;
692 }
693 global_exit = 1;
694 return 0;
695 }
696
697 struct search_data {
698 struct list_head *head;
699 struct menu *target;
700 };
701
702 static int next_jump_key(int key)
703 {
704 if (key < '1' || key > '9')
705 return '1';
706
707 key++;
708
709 if (key > '9')
710 key = '1';
711
712 return key;
713 }
714
715 static int handle_search_keys(int key, size_t start, size_t end, void *_data)
716 {
717 struct search_data *data = _data;
718 struct jump_key *pos;
719 int index = 0;
720
721 if (key < '1' || key > '9')
722 return 0;
723
724 list_for_each_entry(pos, data->head, entries) {
725 index = next_jump_key(index);
726
727 if (pos->offset < start)
728 continue;
729
730 if (pos->offset >= end)
731 break;
732
733 if (key == index) {
734 data->target = pos->target;
735 return 1;
736 }
737 }
738
739 return 0;
740 }
741
742 int get_jump_key_char(void)
743 {
744 jump_key_char = next_jump_key(jump_key_char);
745
746 return jump_key_char;
747 }
748
749 static void search_conf(void)
750 {
751 struct symbol **sym_arr;
752 struct gstr res;
753 struct gstr title;
754 char *dialog_input;
755 int dres, vscroll = 0, hscroll = 0;
756 bool again;
757
758 title = str_new();
759 str_printf( &title, "Enter (sub)string or regexp to search for "
760 "(with or without \"%s\")", CONFIG_);
761
762 again:
763 dres = dialog_inputbox(main_window,
764 "Search Configuration Parameter",
765 str_get(&title),
766 "", &dialog_input_result, &dialog_input_result_len);
767 switch (dres) {
768 case 0:
769 break;
770 case 1:
771 show_scroll_win(main_window,
772 "Search Configuration", search_help);
773 goto again;
774 default:
775 str_free(&title);
776 return;
777 }
778
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_);
783
784 sym_arr = sym_re_search(dialog_input);
785
786 do {
787 LIST_HEAD(head);
788 struct search_data data = {
789 .head = &head,
790 .target = NULL,
791 };
792 jump_key_char = 0;
793 res = get_relations_str(sym_arr, &head);
794 dres = show_scroll_win_ext(main_window,
795 "Search Results", str_get(&res),
796 &vscroll, &hscroll,
797 handle_search_keys, &data);
798 again = false;
799 if (dres >= '1' && dres <= '9') {
800 assert(data.target != NULL);
801 selected_conf(data.target->parent, data.target);
802 again = true;
803 }
804 str_free(&res);
805 } while (again);
806 free(sym_arr);
807 str_free(&title);
808 }
809
810
811 static void build_conf(struct menu *menu)
812 {
813 struct symbol *sym;
814 struct property *prop;
815 struct menu *child;
816 int type, tmp, doint = 2;
817 tristate val;
818 char ch;
819
820 if (!menu || (!show_all_items && !menu_is_visible(menu)))
821 return;
822
823 sym = menu->sym;
824 prop = menu->prompt;
825 if (!sym) {
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;
830 switch (ptype) {
831 case P_MENU:
832 child_count++;
833 if (single_menu_mode) {
834 item_make(menu, 'm',
835 "%s%*c%s",
836 menu->data ? "-->" : "++>",
837 indent + 1, ' ', prompt);
838 } else
839 item_make(menu, 'm',
840 " %*c%s %s",
841 indent + 1, ' ', prompt,
842 menu_is_empty(menu) ? "----" : "--->");
843
844 if (single_menu_mode && menu->data)
845 goto conf_childs;
846 return;
847 case P_COMMENT:
848 if (prompt) {
849 child_count++;
850 item_make(menu, ':',
851 " %*c*** %s ***",
852 indent + 1, ' ',
853 prompt);
854 }
855 break;
856 default:
857 if (prompt) {
858 child_count++;
859 item_make(menu, ':', "---%*c%s",
860 indent + 1, ' ',
861 prompt);
862 }
863 }
864 } else
865 doint = 0;
866 goto conf_childs;
867 }
868
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;
873
874 child_count++;
875 for (child = menu->list; child; child = child->next) {
876 if (menu_is_visible(child) && child->sym == def_sym)
877 def_menu = child;
878 }
879
880 val = sym_get_tristate_value(sym);
881 if (sym_is_changeable(sym)) {
882 switch (type) {
883 case S_BOOLEAN:
884 item_make(menu, 't', "[%c]",
885 val == no ? ' ' : '*');
886 break;
887 case S_TRISTATE:
888 switch (val) {
889 case yes:
890 ch = '*';
891 break;
892 case mod:
893 ch = 'M';
894 break;
895 default:
896 ch = ' ';
897 break;
898 }
899 item_make(menu, 't', "<%c>", ch);
900 break;
901 }
902 } else {
903 item_make(menu, def_menu ? 't' : ':', " ");
904 }
905
906 item_add_str("%*c%s", indent + 1,
907 ' ', menu_get_prompt(menu));
908 if (val == yes) {
909 if (def_menu) {
910 item_add_str(" (%s)",
911 menu_get_prompt(def_menu));
912 item_add_str(" --->");
913 if (def_menu->list) {
914 indent += 2;
915 build_conf(def_menu);
916 indent -= 2;
917 }
918 }
919 return;
920 }
921 } else {
922 if (menu == current_menu) {
923 item_make(menu, ':',
924 "---%*c%s", indent + 1,
925 ' ', menu_get_prompt(menu));
926 goto conf_childs;
927 }
928 child_count++;
929 val = sym_get_tristate_value(sym);
930 if (sym_is_choice_value(sym) && val == yes) {
931 item_make(menu, ':', " ");
932 } else {
933 switch (type) {
934 case S_BOOLEAN:
935 if (sym_is_changeable(sym))
936 item_make(menu, 't', "[%c]",
937 val == no ? ' ' : '*');
938 else
939 item_make(menu, 't', "-%c-",
940 val == no ? ' ' : '*');
941 break;
942 case S_TRISTATE:
943 switch (val) {
944 case yes:
945 ch = '*';
946 break;
947 case mod:
948 ch = 'M';
949 break;
950 default:
951 ch = ' ';
952 break;
953 }
954 if (sym_is_changeable(sym)) {
955 if (sym->rev_dep.tri == mod)
956 item_make(menu,
957 't', "{%c}", ch);
958 else
959 item_make(menu,
960 't', "<%c>", ch);
961 } else
962 item_make(menu, 't', "-%c-", ch);
963 break;
964 default:
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;
969 if (tmp < 0)
970 tmp = 0;
971 item_add_str("%*c%s%s", tmp, ' ',
972 menu_get_prompt(menu),
973 (sym_has_value(sym) ||
974 !sym_is_changeable(sym)) ? "" :
975 " (NEW)");
976 goto conf_childs;
977 }
978 }
979 item_add_str("%*c%s%s", indent + 1, ' ',
980 menu_get_prompt(menu),
981 (sym_has_value(sym) || !sym_is_changeable(sym)) ?
982 "" : " (NEW)");
983 if (menu->prompt && menu->prompt->type == P_MENU) {
984 item_add_str(" %s", menu_is_empty(menu) ? "----" : "--->");
985 return;
986 }
987 }
988
989 conf_childs:
990 indent += doint;
991 for (child = menu->list; child; child = child->next)
992 build_conf(child);
993 indent -= doint;
994 }
995
996 static void reset_menu(void)
997 {
998 unpost_menu(curses_menu);
999 clean_items();
1000 }
1001
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)
1005 {
1006 int toprow;
1007
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);
1016 }
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);
1022 }
1023
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)
1027 {
1028 int maxx, maxy;
1029 WINDOW *menu_window;
1030
1031 current_instructions = instructions;
1032
1033 clear();
1034 print_in_middle(stdscr, 1, getmaxx(stdscr),
1035 menu_backtitle,
1036 attr_main_heading);
1037
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);
1043
1044 set_menu_items(curses_menu, curses_menu_items);
1045
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,
1051 maxy,
1052 maxx,
1053 2,
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);
1058
1059 /* must reassert this after changing items, otherwise returns to a
1060 * default of 16
1061 */
1062 set_menu_format(curses_menu, maxy, 1);
1063 center_item(selected_index, last_top_row);
1064 set_menu_format(curses_menu, maxy, 1);
1065
1066 print_function_line();
1067
1068 /* Post the menu */
1069 post_menu(curses_menu);
1070 refresh_all_windows(main_window);
1071 }
1072
1073 static void adj_match_dir(match_f *match_direction)
1074 {
1075 if (*match_direction == FIND_NEXT_MATCH_DOWN)
1076 *match_direction =
1077 MATCH_TINKER_PATTERN_DOWN;
1078 else if (*match_direction == FIND_NEXT_MATCH_UP)
1079 *match_direction =
1080 MATCH_TINKER_PATTERN_UP;
1081 /* else, do no change.. */
1082 }
1083
1084 struct match_state
1085 {
1086 int in_search;
1087 match_f match_direction;
1088 char pattern[256];
1089 };
1090
1091 /* Return 0 means I have handled the key. In such a case, ans should hold the
1092 * item to center, or -1 otherwise.
1093 * Else return -1 .
1094 */
1095 static int do_match(int key, struct match_state *state, int *ans)
1096 {
1097 char c = (char) key;
1098 int terminate_search = 0;
1099 *ans = -1;
1100 if (key == '/' || (state->in_search && key == 27)) {
1101 move(0, 0);
1102 refresh();
1103 clrtoeol();
1104 state->in_search = 1-state->in_search;
1105 bzero(state->pattern, sizeof(state->pattern));
1106 state->match_direction = MATCH_TINKER_PATTERN_DOWN;
1107 return 0;
1108 } else if (!state->in_search)
1109 return 1;
1110
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);
1128 } else
1129 terminate_search = 1;
1130
1131 if (terminate_search) {
1132 state->in_search = 0;
1133 bzero(state->pattern, sizeof(state->pattern));
1134 move(0, 0);
1135 refresh();
1136 clrtoeol();
1137 return -1;
1138 }
1139 return 0;
1140 }
1141
1142 static void conf(struct menu *menu)
1143 {
1144 selected_conf(menu, NULL);
1145 }
1146
1147 static void selected_conf(struct menu *menu, struct menu *active_menu)
1148 {
1149 struct menu *submenu = NULL;
1150 struct symbol *sym;
1151 int i, res;
1152 int current_index = 0;
1153 int last_top_row = 0;
1154 struct match_state match_state = {
1155 .in_search = 0,
1156 .match_direction = MATCH_TINKER_PATTERN_DOWN,
1157 .pattern = "",
1158 };
1159
1160 while (!global_exit) {
1161 reset_menu();
1162 current_menu = menu;
1163 build_conf(menu);
1164 if (!child_count)
1165 break;
1166
1167 if (active_menu != NULL) {
1168 for (i = 0; i < items_num; i++) {
1169 struct mitem *mcur;
1170
1171 mcur = (struct mitem *) item_userptr(curses_menu_items[i]);
1172 if ((struct menu *) mcur->usrptr == active_menu) {
1173 current_index = i;
1174 break;
1175 }
1176 }
1177 active_menu = NULL;
1178 }
1179
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) {
1185 mvprintw(0, 0,
1186 "searching: %s", match_state.pattern);
1187 clrtoeol();
1188 }
1189 refresh_all_windows(main_window);
1190 res = wgetch(menu_win(curses_menu));
1191 if (!res)
1192 break;
1193 if (do_match(res, &match_state, &current_index) == 0) {
1194 if (current_index != -1)
1195 center_item(current_index,
1196 &last_top_row);
1197 continue;
1198 }
1199 if (process_special_keys(&res,
1200 (struct menu *) item_data()))
1201 break;
1202 switch (res) {
1203 case KEY_DOWN:
1204 case 'j':
1205 menu_driver(curses_menu, REQ_DOWN_ITEM);
1206 break;
1207 case KEY_UP:
1208 case 'k':
1209 menu_driver(curses_menu, REQ_UP_ITEM);
1210 break;
1211 case KEY_NPAGE:
1212 menu_driver(curses_menu, REQ_SCR_DPAGE);
1213 break;
1214 case KEY_PPAGE:
1215 menu_driver(curses_menu, REQ_SCR_UPAGE);
1216 break;
1217 case KEY_HOME:
1218 menu_driver(curses_menu, REQ_FIRST_ITEM);
1219 break;
1220 case KEY_END:
1221 menu_driver(curses_menu, REQ_LAST_ITEM);
1222 break;
1223 case 'h':
1224 case '?':
1225 show_help((struct menu *) item_data());
1226 break;
1227 }
1228 if (res == 10 || res == 27 ||
1229 res == 32 || res == 'n' || res == 'y' ||
1230 res == KEY_LEFT || res == KEY_RIGHT ||
1231 res == 'm')
1232 break;
1233 refresh_all_windows(main_window);
1234 }
1235
1236 refresh_all_windows(main_window);
1237 /* if ESC or left*/
1238 if (res == 27 || (menu != &rootmenu && res == KEY_LEFT))
1239 break;
1240
1241 /* remember location in the menu */
1242 last_top_row = top_row(curses_menu);
1243 current_index = curses_item_index();
1244
1245 if (!item_tag())
1246 continue;
1247
1248 submenu = (struct menu *) item_data();
1249 if (!submenu || !menu_is_visible(submenu))
1250 continue;
1251 sym = submenu->sym;
1252
1253 switch (res) {
1254 case ' ':
1255 if (item_is_tag('t'))
1256 sym_toggle_tristate_value(sym);
1257 else if (item_is_tag('m'))
1258 conf(submenu);
1259 break;
1260 case KEY_RIGHT:
1261 case 10: /* ENTER WAS PRESSED */
1262 switch (item_tag()) {
1263 case 'm':
1264 if (single_menu_mode)
1265 submenu->data =
1266 (void *) (long) !submenu->data;
1267 else
1268 conf(submenu);
1269 break;
1270 case 't':
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)
1276 conf(submenu);
1277 else if (res == 10)
1278 sym_toggle_tristate_value(sym);
1279 break;
1280 case 's':
1281 conf_string(submenu);
1282 break;
1283 }
1284 break;
1285 case 'y':
1286 if (item_is_tag('t')) {
1287 if (sym_set_tristate_value(sym, yes))
1288 break;
1289 if (sym_set_tristate_value(sym, mod))
1290 btn_dialog(main_window, setmod_text, 0);
1291 }
1292 break;
1293 case 'n':
1294 if (item_is_tag('t'))
1295 sym_set_tristate_value(sym, no);
1296 break;
1297 case 'm':
1298 if (item_is_tag('t'))
1299 sym_set_tristate_value(sym, mod);
1300 break;
1301 }
1302 }
1303 }
1304
1305 static void conf_message_callback(const char *s)
1306 {
1307 btn_dialog(main_window, s, 1, "<OK>");
1308 }
1309
1310 static void show_help(struct menu *menu)
1311 {
1312 struct gstr help;
1313
1314 if (!menu)
1315 return;
1316
1317 help = str_new();
1318 menu_get_ext_help(menu, &help);
1319 show_scroll_win(main_window, menu_get_prompt(menu), str_get(&help));
1320 str_free(&help);
1321 }
1322
1323 static void conf_choice(struct menu *menu)
1324 {
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;
1331 int res, i = 0;
1332 struct match_state match_state = {
1333 .in_search = 0,
1334 .match_direction = MATCH_TINKER_PATTERN_DOWN,
1335 .pattern = "",
1336 };
1337
1338 active = sym_get_choice_value(menu->sym);
1339 /* this is mostly duplicated from the conf() function. */
1340 while (!global_exit) {
1341 reset_menu();
1342
1343 for (i = 0, child = menu->list; child; child = child->next) {
1344 if (!show_all_items && !menu_is_visible(child))
1345 continue;
1346
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));
1353 else
1354 item_make(child, ':', "*** %s ***",
1355 menu_get_prompt(child));
1356
1357 if (child->sym == active){
1358 last_top_row = top_row(curses_menu);
1359 selected_index = i;
1360 }
1361 i++;
1362 }
1363 show_menu(prompt ? prompt : "Choice Menu",
1364 radiolist_instructions,
1365 selected_index,
1366 &last_top_row);
1367 while (!global_exit) {
1368 if (match_state.in_search) {
1369 mvprintw(0, 0, "searching: %s",
1370 match_state.pattern);
1371 clrtoeol();
1372 }
1373 refresh_all_windows(main_window);
1374 res = wgetch(menu_win(curses_menu));
1375 if (!res)
1376 break;
1377 if (do_match(res, &match_state, &selected_index) == 0) {
1378 if (selected_index != -1)
1379 center_item(selected_index,
1380 &last_top_row);
1381 continue;
1382 }
1383 if (process_special_keys(
1384 &res,
1385 (struct menu *) item_data()))
1386 break;
1387 switch (res) {
1388 case KEY_DOWN:
1389 case 'j':
1390 menu_driver(curses_menu, REQ_DOWN_ITEM);
1391 break;
1392 case KEY_UP:
1393 case 'k':
1394 menu_driver(curses_menu, REQ_UP_ITEM);
1395 break;
1396 case KEY_NPAGE:
1397 menu_driver(curses_menu, REQ_SCR_DPAGE);
1398 break;
1399 case KEY_PPAGE:
1400 menu_driver(curses_menu, REQ_SCR_UPAGE);
1401 break;
1402 case KEY_HOME:
1403 menu_driver(curses_menu, REQ_FIRST_ITEM);
1404 break;
1405 case KEY_END:
1406 menu_driver(curses_menu, REQ_LAST_ITEM);
1407 break;
1408 case 'h':
1409 case '?':
1410 show_help((struct menu *) item_data());
1411 break;
1412 }
1413 if (res == 10 || res == 27 || res == ' ' ||
1414 res == KEY_LEFT){
1415 break;
1416 }
1417 refresh_all_windows(main_window);
1418 }
1419 /* if ESC or left */
1420 if (res == 27 || res == KEY_LEFT)
1421 break;
1422
1423 child = item_data();
1424 if (!child || !menu_is_visible(child) || !child->sym)
1425 continue;
1426 switch (res) {
1427 case ' ':
1428 case 10:
1429 case KEY_RIGHT:
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)
1433 continue;
1434
1435 conf_reset(S_DEF_USER);
1436 break;
1437 }
1438 }
1439 sym_set_tristate_value(child->sym, yes);
1440 return;
1441 case 'h':
1442 case '?':
1443 show_help(child);
1444 active = child->sym;
1445 break;
1446 case KEY_EXIT:
1447 return;
1448 }
1449 }
1450 }
1451
1452 static void conf_string(struct menu *menu)
1453 {
1454 const char *prompt = menu_get_prompt(menu);
1455
1456 while (1) {
1457 int res;
1458 const char *heading;
1459
1460 switch (sym_get_type(menu->sym)) {
1461 case S_INT:
1462 heading = inputbox_instructions_int;
1463 break;
1464 case S_HEX:
1465 heading = inputbox_instructions_hex;
1466 break;
1467 case S_STRING:
1468 heading = inputbox_instructions_string;
1469 break;
1470 default:
1471 heading = "Internal nconf error!";
1472 }
1473 res = dialog_inputbox(main_window,
1474 prompt ? prompt : "Main Menu",
1475 heading,
1476 sym_get_string_value(menu->sym),
1477 &dialog_input_result,
1478 &dialog_input_result_len);
1479 switch (res) {
1480 case 0:
1481 if (sym_set_string_value(menu->sym,
1482 dialog_input_result))
1483 return;
1484 btn_dialog(main_window,
1485 "You have made an invalid entry.", 0);
1486 break;
1487 case 1:
1488 show_help(menu);
1489 break;
1490 case KEY_EXIT:
1491 return;
1492 }
1493 }
1494 }
1495
1496 static void conf_load(void)
1497 {
1498 while (1) {
1499 int res;
1500 res = dialog_inputbox(main_window,
1501 NULL, load_config_text,
1502 filename,
1503 &dialog_input_result,
1504 &dialog_input_result_len);
1505 switch (res) {
1506 case 0:
1507 if (!dialog_input_result[0])
1508 return;
1509 if (!conf_read(dialog_input_result)) {
1510 set_config_filename(dialog_input_result);
1511 conf_set_changed(true);
1512 return;
1513 }
1514 btn_dialog(main_window, "File does not exist!", 0);
1515 break;
1516 case 1:
1517 show_scroll_win(main_window,
1518 "Load Alternate Configuration",
1519 load_config_help);
1520 break;
1521 case KEY_EXIT:
1522 return;
1523 }
1524 }
1525 }
1526
1527 static void conf_save(void)
1528 {
1529 while (1) {
1530 int res;
1531 res = dialog_inputbox(main_window,
1532 NULL, save_config_text,
1533 filename,
1534 &dialog_input_result,
1535 &dialog_input_result_len);
1536 switch (res) {
1537 case 0:
1538 if (!dialog_input_result[0])
1539 return;
1540 res = conf_write(dialog_input_result);
1541 if (!res) {
1542 set_config_filename(dialog_input_result);
1543 return;
1544 }
1545 btn_dialog(main_window, "Can't create file!",
1546 1, "<OK>");
1547 break;
1548 case 1:
1549 show_scroll_win(main_window,
1550 "Save Alternate Configuration",
1551 save_config_help);
1552 break;
1553 case KEY_EXIT:
1554 return;
1555 }
1556 }
1557 }
1558
1559 static void setup_windows(void)
1560 {
1561 int lines, columns;
1562
1563 getmaxyx(stdscr, lines, columns);
1564
1565 if (main_window != NULL)
1566 delwin(main_window);
1567
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;
1573
1574 /* panels order is from bottom to top */
1575 new_panel(main_window);
1576 }
1577
1578 int main(int ac, char **av)
1579 {
1580 int lines, columns;
1581 char *mode;
1582
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);
1586 av++;
1587 }
1588 conf_parse(av[1]);
1589 conf_read(NULL);
1590
1591 mode = getenv("NCONFIG_MODE");
1592 if (mode) {
1593 if (!strcasecmp(mode, "single_menu"))
1594 single_menu_mode = 1;
1595 }
1596
1597 /* Initialize curses */
1598 initscr();
1599 /* set color theme */
1600 set_colors();
1601
1602 cbreak();
1603 noecho();
1604 keypad(stdscr, TRUE);
1605 curs_set(0);
1606
1607 getmaxyx(stdscr, lines, columns);
1608 if (columns < 75 || lines < 20) {
1609 endwin();
1610 printf("Your terminal should have at "
1611 "least 20 lines and 75 columns\n");
1612 return 1;
1613 }
1614
1615 notimeout(stdscr, FALSE);
1616 #if NCURSES_REENTRANT
1617 set_escdelay(1);
1618 #else
1619 ESCDELAY = 1;
1620 #endif
1621
1622 /* set btns menu */
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);
1633
1634 set_config_filename(conf_get_configname());
1635 setup_windows();
1636
1637 /* check for KEY_FUNC(1) */
1638 if (has_key(KEY_F(1)) == FALSE) {
1639 show_scroll_win(main_window,
1640 "Instructions",
1641 menu_no_f_instructions);
1642 }
1643
1644 conf_set_message_callback(conf_message_callback);
1645 /* do the work */
1646 while (!global_exit) {
1647 conf(&rootmenu);
1648 if (!global_exit && do_exit() == 0)
1649 break;
1650 }
1651 /* ok, we are done */
1652 unpost_menu(curses_menu);
1653 free_menu(curses_menu);
1654 delwin(main_window);
1655 clear();
1656 refresh();
1657 endwin();
1658 return 0;
1659 }