nconf.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. /*
  2. * Copyright (C) 2008 Nir Tzachar <nir.tzachar@gmail.com?
  3. * Released under the terms of the GNU GPL v2.0.
  4. *
  5. * Derived from menuconfig.
  6. *
  7. */
  8. #define _GNU_SOURCE
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include "lkc.h"
  12. #include "nconf.h"
  13. #include <ctype.h>
  14. static const char nconf_global_help[] = N_(
  15. "Help windows\n"
  16. "------------\n"
  17. "o Global help: Unless in a data entry window, pressing <F1> will give \n"
  18. " you the global help window, which you are just reading.\n"
  19. "\n"
  20. "o A short version of the global help is available by pressing <F3>.\n"
  21. "\n"
  22. "o Local help: To get help related to the current menu entry, use any\n"
  23. " of <?> <h>, or if in a data entry window then press <F1>.\n"
  24. "\n"
  25. "\n"
  26. "Menu entries\n"
  27. "------------\n"
  28. "This interface lets you select features and parameters for the kernel\n"
  29. "build. Kernel features can either be built-in, modularized, or removed.\n"
  30. "Parameters must be entered as text or decimal or hexadecimal numbers.\n"
  31. "\n"
  32. "Menu entries beginning with following braces represent features that\n"
  33. " [ ] can be built in or removed\n"
  34. " < > can be built in, modularized or removed\n"
  35. " { } can be built in or modularized, are selected by another feature\n"
  36. " - - are selected by another feature\n"
  37. " XXX cannot be selected. Symbol Info <F2> tells you why.\n"
  38. "*, M or whitespace inside braces means to build in, build as a module\n"
  39. "or to exclude the feature respectively.\n"
  40. "\n"
  41. "To change any of these features, highlight it with the movement keys\n"
  42. "listed below and press <y> to build it in, <m> to make it a module or\n"
  43. "<n> to remove it. You may press the <Space> key to cycle through the\n"
  44. "available options.\n"
  45. "\n"
  46. "A trailing \"--->\" designates a submenu, a trailing \"----\" an\n"
  47. "empty submenu.\n"
  48. "\n"
  49. "Menu navigation keys\n"
  50. "----------------------------------------------------------------------\n"
  51. "Linewise up <Up>\n"
  52. "Linewise down <Down>\n"
  53. "Pagewise up <Page Up>\n"
  54. "Pagewise down <Page Down>\n"
  55. "First entry <Home>\n"
  56. "Last entry <End>\n"
  57. "Enter a submenu <Right> <Enter>\n"
  58. "Go back to parent menu <Left> <Esc> <F5>\n"
  59. "Close a help window <Enter> <Esc> <F5>\n"
  60. "Close entry window, apply <Enter>\n"
  61. "Close entry window, forget <Esc> <F5>\n"
  62. "Start incremental, case-insensitive search for STRING in menu entries,\n"
  63. " no regex support, STRING is displayed in upper left corner\n"
  64. " </>STRING\n"
  65. " Remove last character <Backspace>\n"
  66. " Jump to next hit <Down>\n"
  67. " Jump to previous hit <Up>\n"
  68. "Exit menu search mode </> <Esc>\n"
  69. "Search for configuration variables with or without leading CONFIG_\n"
  70. " <F8>RegExpr<Enter>\n"
  71. "Verbose search help <F8><F1>\n"
  72. "----------------------------------------------------------------------\n"
  73. "\n"
  74. "Unless in a data entry window, key <1> may be used instead of <F1>,\n"
  75. "<2> instead of <F2>, etc.\n"
  76. "\n"
  77. "\n"
  78. "Radiolist (Choice list)\n"
  79. "-----------------------\n"
  80. "Use the movement keys listed above to select the option you wish to set\n"
  81. "and press <Space>.\n"
  82. "\n"
  83. "\n"
  84. "Data entry\n"
  85. "----------\n"
  86. "Enter the requested information and press <Enter>. Hexadecimal values\n"
  87. "may be entered without the \"0x\" prefix.\n"
  88. "\n"
  89. "\n"
  90. "Text Box (Help Window)\n"
  91. "----------------------\n"
  92. "Use movement keys as listed in table above.\n"
  93. "\n"
  94. "Press any of <Enter> <Esc> <q> <F5> <F9> to exit.\n"
  95. "\n"
  96. "\n"
  97. "Alternate configuration files\n"
  98. "-----------------------------\n"
  99. "nconfig supports switching between different configurations.\n"
  100. "Press <F6> to save your current configuration. Press <F7> and enter\n"
  101. "a file name to load a previously saved configuration.\n"
  102. "\n"
  103. "\n"
  104. "Terminal configuration\n"
  105. "----------------------\n"
  106. "If you use nconfig in a xterm window, make sure your TERM environment\n"
  107. "variable specifies a terminal configuration which supports at least\n"
  108. "16 colors. Otherwise nconfig will look rather bad.\n"
  109. "\n"
  110. "If the \"stty size\" command reports the current terminalsize correctly,\n"
  111. "nconfig will adapt to sizes larger than the traditional 80x25 \"standard\"\n"
  112. "and display longer menus properly.\n"
  113. "\n"
  114. "\n"
  115. "Single menu mode\n"
  116. "----------------\n"
  117. "If you prefer to have all of the menu entries listed in a single menu,\n"
  118. "rather than the default multimenu hierarchy, run nconfig with\n"
  119. "NCONFIG_MODE environment variable set to single_menu. Example:\n"
  120. "\n"
  121. "make NCONFIG_MODE=single_menu nconfig\n"
  122. "\n"
  123. "<Enter> will then unfold the appropriate category, or fold it if it\n"
  124. "is already unfolded. Folded menu entries will be designated by a\n"
  125. "leading \"++>\" and unfolded entries by a leading \"-->\".\n"
  126. "\n"
  127. "Note that this mode can eventually be a little more CPU expensive than\n"
  128. "the default mode, especially with a larger number of unfolded submenus.\n"
  129. "\n"),
  130. menu_no_f_instructions[] = N_(
  131. "Legend: [*] built-in [ ] excluded <M> module < > module capable.\n"
  132. "Submenus are designated by a trailing \"--->\", empty ones by \"----\".\n"
  133. "\n"
  134. "Use the following keys to navigate the menus:\n"
  135. "Move up or down with <Up> and <Down>.\n"
  136. "Enter a submenu with <Enter> or <Right>.\n"
  137. "Exit a submenu to its parent menu with <Esc> or <Left>.\n"
  138. "Pressing <y> includes, <n> excludes, <m> modularizes features.\n"
  139. "Pressing <Space> cycles through the available options.\n"
  140. "To search for menu entries press </>.\n"
  141. "<Esc> always leaves the current window.\n"
  142. "\n"
  143. "You do not have function keys support.\n"
  144. "Press <1> instead of <F1>, <2> instead of <F2>, etc.\n"
  145. "For verbose global help use key <1>.\n"
  146. "For help related to the current menu entry press <?> or <h>.\n"),
  147. menu_instructions[] = N_(
  148. "Legend: [*] built-in [ ] excluded <M> module < > module capable.\n"
  149. "Submenus are designated by a trailing \"--->\", empty ones by \"----\".\n"
  150. "\n"
  151. "Use the following keys to navigate the menus:\n"
  152. "Move up or down with <Up> or <Down>.\n"
  153. "Enter a submenu with <Enter> or <Right>.\n"
  154. "Exit a submenu to its parent menu with <Esc> or <Left>.\n"
  155. "Pressing <y> includes, <n> excludes, <m> modularizes features.\n"
  156. "Pressing <Space> cycles through the available options.\n"
  157. "To search for menu entries press </>.\n"
  158. "<Esc> always leaves the current window.\n"
  159. "\n"
  160. "Pressing <1> may be used instead of <F1>, <2> instead of <F2>, etc.\n"
  161. "For verbose global help press <F1>.\n"
  162. "For help related to the current menu entry press <?> or <h>.\n"),
  163. radiolist_instructions[] = N_(
  164. "Press <Up>, <Down>, <Home> or <End> to navigate a radiolist, select\n"
  165. "with <Space>.\n"
  166. "For help related to the current entry press <?> or <h>.\n"
  167. "For global help press <F1>.\n"),
  168. inputbox_instructions_int[] = N_(
  169. "Please enter a decimal value.\n"
  170. "Fractions will not be accepted.\n"
  171. "Press <Enter> to apply, <Esc> to cancel."),
  172. inputbox_instructions_hex[] = N_(
  173. "Please enter a hexadecimal value.\n"
  174. "Press <Enter> to apply, <Esc> to cancel."),
  175. inputbox_instructions_string[] = N_(
  176. "Please enter a string value.\n"
  177. "Press <Enter> to apply, <Esc> to cancel."),
  178. setmod_text[] = N_(
  179. "This feature depends on another feature which has been configured as a\n"
  180. "module. As a result, the current feature will be built as a module too."),
  181. load_config_text[] = N_(
  182. "Enter the name of the configuration file you wish to load.\n"
  183. "Accept the name shown to restore the configuration you last\n"
  184. "retrieved. Leave empty to abort."),
  185. load_config_help[] = N_(
  186. "For various reasons, one may wish to keep several different\n"
  187. "configurations available on a single machine.\n"
  188. "\n"
  189. "If you have saved a previous configuration in a file other than the\n"
  190. "default one, entering its name here will allow you to load and modify\n"
  191. "that configuration.\n"
  192. "\n"
  193. "Leave empty to abort.\n"),
  194. save_config_text[] = N_(
  195. "Enter a filename to which this configuration should be saved\n"
  196. "as an alternate. Leave empty to abort."),
  197. save_config_help[] = N_(
  198. "For various reasons, one may wish to keep several different\n"
  199. "configurations available on a single machine.\n"
  200. "\n"
  201. "Entering a file name here will allow you to later retrieve, modify\n"
  202. "and use the current configuration as an alternate to whatever\n"
  203. "configuration options you have selected at that time.\n"
  204. "\n"
  205. "Leave empty to abort.\n"),
  206. search_help[] = N_(
  207. "Search for symbols (configuration variable names CONFIG_*) and display\n"
  208. "their relations. Regular expressions are supported.\n"
  209. "Example: Search for \"^FOO\".\n"
  210. "Result:\n"
  211. "-----------------------------------------------------------------\n"
  212. "Symbol: FOO [ = m]\n"
  213. "Prompt: Foo bus is used to drive the bar HW\n"
  214. "Defined at drivers/pci/Kconfig:47\n"
  215. "Depends on: X86_LOCAL_APIC && X86_IO_APIC || IA64\n"
  216. "Location:\n"
  217. " -> Bus options (PCI, PCMCIA, EISA, ISA)\n"
  218. " -> PCI support (PCI [ = y])\n"
  219. " -> PCI access mode (<choice> [ = y])\n"
  220. "Selects: LIBCRC32\n"
  221. "Selected by: BAR\n"
  222. "-----------------------------------------------------------------\n"
  223. "o The line 'Prompt:' shows the text displayed for this symbol in\n"
  224. " the menu hierarchy.\n"
  225. "o The 'Defined at' line tells at what file / line number the symbol is\n"
  226. " defined.\n"
  227. "o The 'Depends on:' line lists symbols that need to be defined for\n"
  228. " this symbol to be visible and selectable in the menu.\n"
  229. "o The 'Location:' lines tell, where in the menu structure this symbol\n"
  230. " is located. A location followed by a [ = y] indicates that this is\n"
  231. " a selectable menu item, and the current value is displayed inside\n"
  232. " brackets.\n"
  233. "o The 'Selects:' line tells, what symbol will be automatically selected\n"
  234. " if this symbol is selected (y or m).\n"
  235. "o The 'Selected by' line tells what symbol has selected this symbol.\n"
  236. "\n"
  237. "Only relevant lines are shown.\n"
  238. "\n\n"
  239. "Search examples:\n"
  240. "USB => find all symbols containing USB\n"
  241. "^USB => find all symbols starting with USB\n"
  242. "USB$ => find all symbols ending with USB\n"
  243. "\n");
  244. struct mitem {
  245. char str[256];
  246. char tag;
  247. void *usrptr;
  248. int is_visible;
  249. };
  250. #define MAX_MENU_ITEMS 4096
  251. static int show_all_items;
  252. static int indent;
  253. static struct menu *current_menu;
  254. static int child_count;
  255. static int single_menu_mode;
  256. /* the window in which all information appears */
  257. static WINDOW *main_window;
  258. /* the largest size of the menu window */
  259. static int mwin_max_lines;
  260. static int mwin_max_cols;
  261. /* the window in which we show option buttons */
  262. static MENU *curses_menu;
  263. static ITEM *curses_menu_items[MAX_MENU_ITEMS];
  264. static struct mitem k_menu_items[MAX_MENU_ITEMS];
  265. static int items_num;
  266. static int global_exit;
  267. /* the currently selected button */
  268. const char *current_instructions = menu_instructions;
  269. static char *dialog_input_result;
  270. static int dialog_input_result_len;
  271. static void conf(struct menu *menu);
  272. static void conf_choice(struct menu *menu);
  273. static void conf_string(struct menu *menu);
  274. static void conf_load(void);
  275. static void conf_save(void);
  276. static void show_help(struct menu *menu);
  277. static int do_exit(void);
  278. static void setup_windows(void);
  279. static void search_conf(void);
  280. typedef void (*function_key_handler_t)(int *key, struct menu *menu);
  281. static void handle_f1(int *key, struct menu *current_item);
  282. static void handle_f2(int *key, struct menu *current_item);
  283. static void handle_f3(int *key, struct menu *current_item);
  284. static void handle_f4(int *key, struct menu *current_item);
  285. static void handle_f5(int *key, struct menu *current_item);
  286. static void handle_f6(int *key, struct menu *current_item);
  287. static void handle_f7(int *key, struct menu *current_item);
  288. static void handle_f8(int *key, struct menu *current_item);
  289. static void handle_f9(int *key, struct menu *current_item);
  290. struct function_keys {
  291. const char *key_str;
  292. const char *func;
  293. function_key key;
  294. function_key_handler_t handler;
  295. };
  296. static const int function_keys_num = 9;
  297. struct function_keys function_keys[] = {
  298. {
  299. .key_str = "F1",
  300. .func = "Help",
  301. .key = F_HELP,
  302. .handler = handle_f1,
  303. },
  304. {
  305. .key_str = "F2",
  306. .func = "SymInfo",
  307. .key = F_SYMBOL,
  308. .handler = handle_f2,
  309. },
  310. {
  311. .key_str = "F3",
  312. .func = "Help 2",
  313. .key = F_INSTS,
  314. .handler = handle_f3,
  315. },
  316. {
  317. .key_str = "F4",
  318. .func = "ShowAll",
  319. .key = F_CONF,
  320. .handler = handle_f4,
  321. },
  322. {
  323. .key_str = "F5",
  324. .func = "Back",
  325. .key = F_BACK,
  326. .handler = handle_f5,
  327. },
  328. {
  329. .key_str = "F6",
  330. .func = "Save",
  331. .key = F_SAVE,
  332. .handler = handle_f6,
  333. },
  334. {
  335. .key_str = "F7",
  336. .func = "Load",
  337. .key = F_LOAD,
  338. .handler = handle_f7,
  339. },
  340. {
  341. .key_str = "F8",
  342. .func = "SymSearch",
  343. .key = F_SEARCH,
  344. .handler = handle_f8,
  345. },
  346. {
  347. .key_str = "F9",
  348. .func = "Exit",
  349. .key = F_EXIT,
  350. .handler = handle_f9,
  351. },
  352. };
  353. static void print_function_line(void)
  354. {
  355. int i;
  356. int offset = 1;
  357. const int skip = 1;
  358. int lines = getmaxy(stdscr);
  359. for (i = 0; i < function_keys_num; i++) {
  360. (void) wattrset(main_window, attributes[FUNCTION_HIGHLIGHT]);
  361. mvwprintw(main_window, lines-3, offset,
  362. "%s",
  363. function_keys[i].key_str);
  364. (void) wattrset(main_window, attributes[FUNCTION_TEXT]);
  365. offset += strlen(function_keys[i].key_str);
  366. mvwprintw(main_window, lines-3,
  367. offset, "%s",
  368. function_keys[i].func);
  369. offset += strlen(function_keys[i].func) + skip;
  370. }
  371. (void) wattrset(main_window, attributes[NORMAL]);
  372. }
  373. /* help */
  374. static void handle_f1(int *key, struct menu *current_item)
  375. {
  376. show_scroll_win(main_window,
  377. _("Global help"), _(nconf_global_help));
  378. return;
  379. }
  380. /* symbole help */
  381. static void handle_f2(int *key, struct menu *current_item)
  382. {
  383. show_help(current_item);
  384. return;
  385. }
  386. /* instructions */
  387. static void handle_f3(int *key, struct menu *current_item)
  388. {
  389. show_scroll_win(main_window,
  390. _("Short help"),
  391. _(current_instructions));
  392. return;
  393. }
  394. /* config */
  395. static void handle_f4(int *key, struct menu *current_item)
  396. {
  397. int res = btn_dialog(main_window,
  398. _("Show all symbols?"),
  399. 2,
  400. " <Show All> ",
  401. "<Don't show all>");
  402. if (res == 0)
  403. show_all_items = 1;
  404. else if (res == 1)
  405. show_all_items = 0;
  406. return;
  407. }
  408. /* back */
  409. static void handle_f5(int *key, struct menu *current_item)
  410. {
  411. *key = KEY_LEFT;
  412. return;
  413. }
  414. /* save */
  415. static void handle_f6(int *key, struct menu *current_item)
  416. {
  417. conf_save();
  418. return;
  419. }
  420. /* load */
  421. static void handle_f7(int *key, struct menu *current_item)
  422. {
  423. conf_load();
  424. return;
  425. }
  426. /* search */
  427. static void handle_f8(int *key, struct menu *current_item)
  428. {
  429. search_conf();
  430. return;
  431. }
  432. /* exit */
  433. static void handle_f9(int *key, struct menu *current_item)
  434. {
  435. do_exit();
  436. return;
  437. }
  438. /* return != 0 to indicate the key was handles */
  439. static int process_special_keys(int *key, struct menu *menu)
  440. {
  441. int i;
  442. if (*key == KEY_RESIZE) {
  443. setup_windows();
  444. return 1;
  445. }
  446. for (i = 0; i < function_keys_num; i++) {
  447. if (*key == KEY_F(function_keys[i].key) ||
  448. *key == '0' + function_keys[i].key){
  449. function_keys[i].handler(key, menu);
  450. return 1;
  451. }
  452. }
  453. return 0;
  454. }
  455. static void clean_items(void)
  456. {
  457. int i;
  458. for (i = 0; curses_menu_items[i]; i++)
  459. free_item(curses_menu_items[i]);
  460. bzero(curses_menu_items, sizeof(curses_menu_items));
  461. bzero(k_menu_items, sizeof(k_menu_items));
  462. items_num = 0;
  463. }
  464. typedef enum {MATCH_TINKER_PATTERN_UP, MATCH_TINKER_PATTERN_DOWN,
  465. FIND_NEXT_MATCH_DOWN, FIND_NEXT_MATCH_UP} match_f;
  466. /* return the index of the matched item, or -1 if no such item exists */
  467. static int get_mext_match(const char *match_str, match_f flag)
  468. {
  469. int match_start = item_index(current_item(curses_menu));
  470. int index;
  471. if (flag == FIND_NEXT_MATCH_DOWN)
  472. ++match_start;
  473. else if (flag == FIND_NEXT_MATCH_UP)
  474. --match_start;
  475. index = match_start;
  476. index = (index + items_num) % items_num;
  477. while (true) {
  478. char *str = k_menu_items[index].str;
  479. if (strcasestr(str, match_str) != 0)
  480. return index;
  481. if (flag == FIND_NEXT_MATCH_UP ||
  482. flag == MATCH_TINKER_PATTERN_UP)
  483. --index;
  484. else
  485. ++index;
  486. index = (index + items_num) % items_num;
  487. if (index == match_start)
  488. return -1;
  489. }
  490. }
  491. /* Make a new item. */
  492. static void item_make(struct menu *menu, char tag, const char *fmt, ...)
  493. {
  494. va_list ap;
  495. if (items_num > MAX_MENU_ITEMS-1)
  496. return;
  497. bzero(&k_menu_items[items_num], sizeof(k_menu_items[0]));
  498. k_menu_items[items_num].tag = tag;
  499. k_menu_items[items_num].usrptr = menu;
  500. if (menu != NULL)
  501. k_menu_items[items_num].is_visible =
  502. menu_is_visible(menu);
  503. else
  504. k_menu_items[items_num].is_visible = 1;
  505. va_start(ap, fmt);
  506. vsnprintf(k_menu_items[items_num].str,
  507. sizeof(k_menu_items[items_num].str),
  508. fmt, ap);
  509. va_end(ap);
  510. if (!k_menu_items[items_num].is_visible)
  511. memcpy(k_menu_items[items_num].str, "XXX", 3);
  512. curses_menu_items[items_num] = new_item(
  513. k_menu_items[items_num].str,
  514. k_menu_items[items_num].str);
  515. set_item_userptr(curses_menu_items[items_num],
  516. &k_menu_items[items_num]);
  517. /*
  518. if (!k_menu_items[items_num].is_visible)
  519. item_opts_off(curses_menu_items[items_num], O_SELECTABLE);
  520. */
  521. items_num++;
  522. curses_menu_items[items_num] = NULL;
  523. }
  524. /* very hackish. adds a string to the last item added */
  525. static void item_add_str(const char *fmt, ...)
  526. {
  527. va_list ap;
  528. int index = items_num-1;
  529. char new_str[256];
  530. char tmp_str[256];
  531. if (index < 0)
  532. return;
  533. va_start(ap, fmt);
  534. vsnprintf(new_str, sizeof(new_str), fmt, ap);
  535. va_end(ap);
  536. snprintf(tmp_str, sizeof(tmp_str), "%s%s",
  537. k_menu_items[index].str, new_str);
  538. strncpy(k_menu_items[index].str,
  539. tmp_str,
  540. sizeof(k_menu_items[index].str));
  541. free_item(curses_menu_items[index]);
  542. curses_menu_items[index] = new_item(
  543. k_menu_items[index].str,
  544. k_menu_items[index].str);
  545. set_item_userptr(curses_menu_items[index],
  546. &k_menu_items[index]);
  547. }
  548. /* get the tag of the currently selected item */
  549. static char item_tag(void)
  550. {
  551. ITEM *cur;
  552. struct mitem *mcur;
  553. cur = current_item(curses_menu);
  554. if (cur == NULL)
  555. return 0;
  556. mcur = (struct mitem *) item_userptr(cur);
  557. return mcur->tag;
  558. }
  559. static int curses_item_index(void)
  560. {
  561. return item_index(current_item(curses_menu));
  562. }
  563. static void *item_data(void)
  564. {
  565. ITEM *cur;
  566. struct mitem *mcur;
  567. cur = current_item(curses_menu);
  568. if (!cur)
  569. return NULL;
  570. mcur = (struct mitem *) item_userptr(cur);
  571. return mcur->usrptr;
  572. }
  573. static int item_is_tag(char tag)
  574. {
  575. return item_tag() == tag;
  576. }
  577. static char filename[PATH_MAX+1];
  578. static char menu_backtitle[PATH_MAX+128];
  579. static const char *set_config_filename(const char *config_filename)
  580. {
  581. int size;
  582. size = snprintf(menu_backtitle, sizeof(menu_backtitle),
  583. "%s - %s", config_filename, rootmenu.prompt->text);
  584. if (size >= sizeof(menu_backtitle))
  585. menu_backtitle[sizeof(menu_backtitle)-1] = '\0';
  586. size = snprintf(filename, sizeof(filename), "%s", config_filename);
  587. if (size >= sizeof(filename))
  588. filename[sizeof(filename)-1] = '\0';
  589. return menu_backtitle;
  590. }
  591. /* return = 0 means we are successful.
  592. * -1 means go on doing what you were doing
  593. */
  594. static int do_exit(void)
  595. {
  596. int res;
  597. if (!conf_get_changed()) {
  598. global_exit = 1;
  599. return 0;
  600. }
  601. res = btn_dialog(main_window,
  602. _("Do you wish to save your new configuration?\n"
  603. "<ESC> to cancel and resume nconfig."),
  604. 2,
  605. " <save> ",
  606. "<don't save>");
  607. if (res == KEY_EXIT) {
  608. global_exit = 0;
  609. return -1;
  610. }
  611. /* if we got here, the user really wants to exit */
  612. switch (res) {
  613. case 0:
  614. res = conf_write(filename);
  615. if (res)
  616. btn_dialog(
  617. main_window,
  618. _("Error during writing of configuration.\n"
  619. "Your configuration changes were NOT saved."),
  620. 1,
  621. "<OK>");
  622. break;
  623. default:
  624. btn_dialog(
  625. main_window,
  626. _("Your configuration changes were NOT saved."),
  627. 1,
  628. "<OK>");
  629. break;
  630. }
  631. global_exit = 1;
  632. return 0;
  633. }
  634. static void search_conf(void)
  635. {
  636. struct symbol **sym_arr;
  637. struct gstr res;
  638. struct gstr title;
  639. char *dialog_input;
  640. int dres;
  641. title = str_new();
  642. str_printf( &title, _("Enter (sub)string or regexp to search for "
  643. "(with or without \"%s\")"), CONFIG_);
  644. again:
  645. dres = dialog_inputbox(main_window,
  646. _("Search Configuration Parameter"),
  647. str_get(&title),
  648. "", &dialog_input_result, &dialog_input_result_len);
  649. switch (dres) {
  650. case 0:
  651. break;
  652. case 1:
  653. show_scroll_win(main_window,
  654. _("Search Configuration"), search_help);
  655. goto again;
  656. default:
  657. str_free(&title);
  658. return;
  659. }
  660. /* strip the prefix if necessary */
  661. dialog_input = dialog_input_result;
  662. if (strncasecmp(dialog_input_result, CONFIG_, strlen(CONFIG_)) == 0)
  663. dialog_input += strlen(CONFIG_);
  664. sym_arr = sym_re_search(dialog_input);
  665. res = get_relations_str(sym_arr, NULL);
  666. free(sym_arr);
  667. show_scroll_win(main_window,
  668. _("Search Results"), str_get(&res));
  669. str_free(&res);
  670. str_free(&title);
  671. }
  672. static void build_conf(struct menu *menu)
  673. {
  674. struct symbol *sym;
  675. struct property *prop;
  676. struct menu *child;
  677. int type, tmp, doint = 2;
  678. tristate val;
  679. char ch;
  680. if (!menu || (!show_all_items && !menu_is_visible(menu)))
  681. return;
  682. sym = menu->sym;
  683. prop = menu->prompt;
  684. if (!sym) {
  685. if (prop && menu != current_menu) {
  686. const char *prompt = menu_get_prompt(menu);
  687. enum prop_type ptype;
  688. ptype = menu->prompt ? menu->prompt->type : P_UNKNOWN;
  689. switch (ptype) {
  690. case P_MENU:
  691. child_count++;
  692. prompt = _(prompt);
  693. if (single_menu_mode) {
  694. item_make(menu, 'm',
  695. "%s%*c%s",
  696. menu->data ? "-->" : "++>",
  697. indent + 1, ' ', prompt);
  698. } else
  699. item_make(menu, 'm',
  700. " %*c%s %s",
  701. indent + 1, ' ', prompt,
  702. menu_is_empty(menu) ? "----" : "--->");
  703. if (single_menu_mode && menu->data)
  704. goto conf_childs;
  705. return;
  706. case P_COMMENT:
  707. if (prompt) {
  708. child_count++;
  709. item_make(menu, ':',
  710. " %*c*** %s ***",
  711. indent + 1, ' ',
  712. _(prompt));
  713. }
  714. break;
  715. default:
  716. if (prompt) {
  717. child_count++;
  718. item_make(menu, ':', "---%*c%s",
  719. indent + 1, ' ',
  720. _(prompt));
  721. }
  722. }
  723. } else
  724. doint = 0;
  725. goto conf_childs;
  726. }
  727. type = sym_get_type(sym);
  728. if (sym_is_choice(sym)) {
  729. struct symbol *def_sym = sym_get_choice_value(sym);
  730. struct menu *def_menu = NULL;
  731. child_count++;
  732. for (child = menu->list; child; child = child->next) {
  733. if (menu_is_visible(child) && child->sym == def_sym)
  734. def_menu = child;
  735. }
  736. val = sym_get_tristate_value(sym);
  737. if (sym_is_changable(sym)) {
  738. switch (type) {
  739. case S_BOOLEAN:
  740. item_make(menu, 't', "[%c]",
  741. val == no ? ' ' : '*');
  742. break;
  743. case S_TRISTATE:
  744. switch (val) {
  745. case yes:
  746. ch = '*';
  747. break;
  748. case mod:
  749. ch = 'M';
  750. break;
  751. default:
  752. ch = ' ';
  753. break;
  754. }
  755. item_make(menu, 't', "<%c>", ch);
  756. break;
  757. }
  758. } else {
  759. item_make(menu, def_menu ? 't' : ':', " ");
  760. }
  761. item_add_str("%*c%s", indent + 1,
  762. ' ', _(menu_get_prompt(menu)));
  763. if (val == yes) {
  764. if (def_menu) {
  765. item_add_str(" (%s)",
  766. _(menu_get_prompt(def_menu)));
  767. item_add_str(" --->");
  768. if (def_menu->list) {
  769. indent += 2;
  770. build_conf(def_menu);
  771. indent -= 2;
  772. }
  773. }
  774. return;
  775. }
  776. } else {
  777. if (menu == current_menu) {
  778. item_make(menu, ':',
  779. "---%*c%s", indent + 1,
  780. ' ', _(menu_get_prompt(menu)));
  781. goto conf_childs;
  782. }
  783. child_count++;
  784. val = sym_get_tristate_value(sym);
  785. if (sym_is_choice_value(sym) && val == yes) {
  786. item_make(menu, ':', " ");
  787. } else {
  788. switch (type) {
  789. case S_BOOLEAN:
  790. if (sym_is_changable(sym))
  791. item_make(menu, 't', "[%c]",
  792. val == no ? ' ' : '*');
  793. else
  794. item_make(menu, 't', "-%c-",
  795. val == no ? ' ' : '*');
  796. break;
  797. case S_TRISTATE:
  798. switch (val) {
  799. case yes:
  800. ch = '*';
  801. break;
  802. case mod:
  803. ch = 'M';
  804. break;
  805. default:
  806. ch = ' ';
  807. break;
  808. }
  809. if (sym_is_changable(sym)) {
  810. if (sym->rev_dep.tri == mod)
  811. item_make(menu,
  812. 't', "{%c}", ch);
  813. else
  814. item_make(menu,
  815. 't', "<%c>", ch);
  816. } else
  817. item_make(menu, 't', "-%c-", ch);
  818. break;
  819. default:
  820. tmp = 2 + strlen(sym_get_string_value(sym));
  821. item_make(menu, 's', " (%s)",
  822. sym_get_string_value(sym));
  823. tmp = indent - tmp + 4;
  824. if (tmp < 0)
  825. tmp = 0;
  826. item_add_str("%*c%s%s", tmp, ' ',
  827. _(menu_get_prompt(menu)),
  828. (sym_has_value(sym) ||
  829. !sym_is_changable(sym)) ? "" :
  830. _(" (NEW)"));
  831. goto conf_childs;
  832. }
  833. }
  834. item_add_str("%*c%s%s", indent + 1, ' ',
  835. _(menu_get_prompt(menu)),
  836. (sym_has_value(sym) || !sym_is_changable(sym)) ?
  837. "" : _(" (NEW)"));
  838. if (menu->prompt && menu->prompt->type == P_MENU) {
  839. item_add_str(" %s", menu_is_empty(menu) ? "----" : "--->");
  840. return;
  841. }
  842. }
  843. conf_childs:
  844. indent += doint;
  845. for (child = menu->list; child; child = child->next)
  846. build_conf(child);
  847. indent -= doint;
  848. }
  849. static void reset_menu(void)
  850. {
  851. unpost_menu(curses_menu);
  852. clean_items();
  853. }
  854. /* adjust the menu to show this item.
  855. * prefer not to scroll the menu if possible*/
  856. static void center_item(int selected_index, int *last_top_row)
  857. {
  858. int toprow;
  859. set_top_row(curses_menu, *last_top_row);
  860. toprow = top_row(curses_menu);
  861. if (selected_index < toprow ||
  862. selected_index >= toprow+mwin_max_lines) {
  863. toprow = max(selected_index-mwin_max_lines/2, 0);
  864. if (toprow >= item_count(curses_menu)-mwin_max_lines)
  865. toprow = item_count(curses_menu)-mwin_max_lines;
  866. set_top_row(curses_menu, toprow);
  867. }
  868. set_current_item(curses_menu,
  869. curses_menu_items[selected_index]);
  870. *last_top_row = toprow;
  871. post_menu(curses_menu);
  872. refresh_all_windows(main_window);
  873. }
  874. /* this function assumes reset_menu has been called before */
  875. static void show_menu(const char *prompt, const char *instructions,
  876. int selected_index, int *last_top_row)
  877. {
  878. int maxx, maxy;
  879. WINDOW *menu_window;
  880. current_instructions = instructions;
  881. clear();
  882. (void) wattrset(main_window, attributes[NORMAL]);
  883. print_in_middle(stdscr, 1, 0, getmaxx(stdscr),
  884. menu_backtitle,
  885. attributes[MAIN_HEADING]);
  886. (void) wattrset(main_window, attributes[MAIN_MENU_BOX]);
  887. box(main_window, 0, 0);
  888. (void) wattrset(main_window, attributes[MAIN_MENU_HEADING]);
  889. mvwprintw(main_window, 0, 3, " %s ", prompt);
  890. (void) wattrset(main_window, attributes[NORMAL]);
  891. set_menu_items(curses_menu, curses_menu_items);
  892. /* position the menu at the middle of the screen */
  893. scale_menu(curses_menu, &maxy, &maxx);
  894. maxx = min(maxx, mwin_max_cols-2);
  895. maxy = mwin_max_lines;
  896. menu_window = derwin(main_window,
  897. maxy,
  898. maxx,
  899. 2,
  900. (mwin_max_cols-maxx)/2);
  901. keypad(menu_window, TRUE);
  902. set_menu_win(curses_menu, menu_window);
  903. set_menu_sub(curses_menu, menu_window);
  904. /* must reassert this after changing items, otherwise returns to a
  905. * default of 16
  906. */
  907. set_menu_format(curses_menu, maxy, 1);
  908. center_item(selected_index, last_top_row);
  909. set_menu_format(curses_menu, maxy, 1);
  910. print_function_line();
  911. /* Post the menu */
  912. post_menu(curses_menu);
  913. refresh_all_windows(main_window);
  914. }
  915. static void adj_match_dir(match_f *match_direction)
  916. {
  917. if (*match_direction == FIND_NEXT_MATCH_DOWN)
  918. *match_direction =
  919. MATCH_TINKER_PATTERN_DOWN;
  920. else if (*match_direction == FIND_NEXT_MATCH_UP)
  921. *match_direction =
  922. MATCH_TINKER_PATTERN_UP;
  923. /* else, do no change.. */
  924. }
  925. struct match_state
  926. {
  927. int in_search;
  928. match_f match_direction;
  929. char pattern[256];
  930. };
  931. /* Return 0 means I have handled the key. In such a case, ans should hold the
  932. * item to center, or -1 otherwise.
  933. * Else return -1 .
  934. */
  935. static int do_match(int key, struct match_state *state, int *ans)
  936. {
  937. char c = (char) key;
  938. int terminate_search = 0;
  939. *ans = -1;
  940. if (key == '/' || (state->in_search && key == 27)) {
  941. move(0, 0);
  942. refresh();
  943. clrtoeol();
  944. state->in_search = 1-state->in_search;
  945. bzero(state->pattern, sizeof(state->pattern));
  946. state->match_direction = MATCH_TINKER_PATTERN_DOWN;
  947. return 0;
  948. } else if (!state->in_search)
  949. return 1;
  950. if (isalnum(c) || isgraph(c) || c == ' ') {
  951. state->pattern[strlen(state->pattern)] = c;
  952. state->pattern[strlen(state->pattern)] = '\0';
  953. adj_match_dir(&state->match_direction);
  954. *ans = get_mext_match(state->pattern,
  955. state->match_direction);
  956. } else if (key == KEY_DOWN) {
  957. state->match_direction = FIND_NEXT_MATCH_DOWN;
  958. *ans = get_mext_match(state->pattern,
  959. state->match_direction);
  960. } else if (key == KEY_UP) {
  961. state->match_direction = FIND_NEXT_MATCH_UP;
  962. *ans = get_mext_match(state->pattern,
  963. state->match_direction);
  964. } else if (key == KEY_BACKSPACE || key == 127) {
  965. state->pattern[strlen(state->pattern)-1] = '\0';
  966. adj_match_dir(&state->match_direction);
  967. } else
  968. terminate_search = 1;
  969. if (terminate_search) {
  970. state->in_search = 0;
  971. bzero(state->pattern, sizeof(state->pattern));
  972. move(0, 0);
  973. refresh();
  974. clrtoeol();
  975. return -1;
  976. }
  977. return 0;
  978. }
  979. static void conf(struct menu *menu)
  980. {
  981. struct menu *submenu = 0;
  982. const char *prompt = menu_get_prompt(menu);
  983. struct symbol *sym;
  984. int res;
  985. int current_index = 0;
  986. int last_top_row = 0;
  987. struct match_state match_state = {
  988. .in_search = 0,
  989. .match_direction = MATCH_TINKER_PATTERN_DOWN,
  990. .pattern = "",
  991. };
  992. while (!global_exit) {
  993. reset_menu();
  994. current_menu = menu;
  995. build_conf(menu);
  996. if (!child_count)
  997. break;
  998. show_menu(prompt ? _(prompt) : _("Main Menu"),
  999. _(menu_instructions),
  1000. current_index, &last_top_row);
  1001. keypad((menu_win(curses_menu)), TRUE);
  1002. while (!global_exit) {
  1003. if (match_state.in_search) {
  1004. mvprintw(0, 0,
  1005. "searching: %s", match_state.pattern);
  1006. clrtoeol();
  1007. }
  1008. refresh_all_windows(main_window);
  1009. res = wgetch(menu_win(curses_menu));
  1010. if (!res)
  1011. break;
  1012. if (do_match(res, &match_state, &current_index) == 0) {
  1013. if (current_index != -1)
  1014. center_item(current_index,
  1015. &last_top_row);
  1016. continue;
  1017. }
  1018. if (process_special_keys(&res,
  1019. (struct menu *) item_data()))
  1020. break;
  1021. switch (res) {
  1022. case KEY_DOWN:
  1023. menu_driver(curses_menu, REQ_DOWN_ITEM);
  1024. break;
  1025. case KEY_UP:
  1026. menu_driver(curses_menu, REQ_UP_ITEM);
  1027. break;
  1028. case KEY_NPAGE:
  1029. menu_driver(curses_menu, REQ_SCR_DPAGE);
  1030. break;
  1031. case KEY_PPAGE:
  1032. menu_driver(curses_menu, REQ_SCR_UPAGE);
  1033. break;
  1034. case KEY_HOME:
  1035. menu_driver(curses_menu, REQ_FIRST_ITEM);
  1036. break;
  1037. case KEY_END:
  1038. menu_driver(curses_menu, REQ_LAST_ITEM);
  1039. break;
  1040. case 'h':
  1041. case '?':
  1042. show_help((struct menu *) item_data());
  1043. break;
  1044. }
  1045. if (res == 10 || res == 27 ||
  1046. res == 32 || res == 'n' || res == 'y' ||
  1047. res == KEY_LEFT || res == KEY_RIGHT ||
  1048. res == 'm')
  1049. break;
  1050. refresh_all_windows(main_window);
  1051. }
  1052. refresh_all_windows(main_window);
  1053. /* if ESC or left*/
  1054. if (res == 27 || (menu != &rootmenu && res == KEY_LEFT))
  1055. break;
  1056. /* remember location in the menu */
  1057. last_top_row = top_row(curses_menu);
  1058. current_index = curses_item_index();
  1059. if (!item_tag())
  1060. continue;
  1061. submenu = (struct menu *) item_data();
  1062. if (!submenu || !menu_is_visible(submenu))
  1063. continue;
  1064. sym = submenu->sym;
  1065. switch (res) {
  1066. case ' ':
  1067. if (item_is_tag('t'))
  1068. sym_toggle_tristate_value(sym);
  1069. else if (item_is_tag('m'))
  1070. conf(submenu);
  1071. break;
  1072. case KEY_RIGHT:
  1073. case 10: /* ENTER WAS PRESSED */
  1074. switch (item_tag()) {
  1075. case 'm':
  1076. if (single_menu_mode)
  1077. submenu->data =
  1078. (void *) (long) !submenu->data;
  1079. else
  1080. conf(submenu);
  1081. break;
  1082. case 't':
  1083. if (sym_is_choice(sym) &&
  1084. sym_get_tristate_value(sym) == yes)
  1085. conf_choice(submenu);
  1086. else if (submenu->prompt &&
  1087. submenu->prompt->type == P_MENU)
  1088. conf(submenu);
  1089. else if (res == 10)
  1090. sym_toggle_tristate_value(sym);
  1091. break;
  1092. case 's':
  1093. conf_string(submenu);
  1094. break;
  1095. }
  1096. break;
  1097. case 'y':
  1098. if (item_is_tag('t')) {
  1099. if (sym_set_tristate_value(sym, yes))
  1100. break;
  1101. if (sym_set_tristate_value(sym, mod))
  1102. btn_dialog(main_window, setmod_text, 0);
  1103. }
  1104. break;
  1105. case 'n':
  1106. if (item_is_tag('t'))
  1107. sym_set_tristate_value(sym, no);
  1108. break;
  1109. case 'm':
  1110. if (item_is_tag('t'))
  1111. sym_set_tristate_value(sym, mod);
  1112. break;
  1113. }
  1114. }
  1115. }
  1116. static void conf_message_callback(const char *fmt, va_list ap)
  1117. {
  1118. char buf[1024];
  1119. vsnprintf(buf, sizeof(buf), fmt, ap);
  1120. btn_dialog(main_window, buf, 1, "<OK>");
  1121. }
  1122. static void show_help(struct menu *menu)
  1123. {
  1124. struct gstr help;
  1125. if (!menu)
  1126. return;
  1127. help = str_new();
  1128. menu_get_ext_help(menu, &help);
  1129. show_scroll_win(main_window, _(menu_get_prompt(menu)), str_get(&help));
  1130. str_free(&help);
  1131. }
  1132. static void conf_choice(struct menu *menu)
  1133. {
  1134. const char *prompt = _(menu_get_prompt(menu));
  1135. struct menu *child = 0;
  1136. struct symbol *active;
  1137. int selected_index = 0;
  1138. int last_top_row = 0;
  1139. int res, i = 0;
  1140. struct match_state match_state = {
  1141. .in_search = 0,
  1142. .match_direction = MATCH_TINKER_PATTERN_DOWN,
  1143. .pattern = "",
  1144. };
  1145. active = sym_get_choice_value(menu->sym);
  1146. /* this is mostly duplicated from the conf() function. */
  1147. while (!global_exit) {
  1148. reset_menu();
  1149. for (i = 0, child = menu->list; child; child = child->next) {
  1150. if (!show_all_items && !menu_is_visible(child))
  1151. continue;
  1152. if (child->sym == sym_get_choice_value(menu->sym))
  1153. item_make(child, ':', "<X> %s",
  1154. _(menu_get_prompt(child)));
  1155. else if (child->sym)
  1156. item_make(child, ':', " %s",
  1157. _(menu_get_prompt(child)));
  1158. else
  1159. item_make(child, ':', "*** %s ***",
  1160. _(menu_get_prompt(child)));
  1161. if (child->sym == active){
  1162. last_top_row = top_row(curses_menu);
  1163. selected_index = i;
  1164. }
  1165. i++;
  1166. }
  1167. show_menu(prompt ? _(prompt) : _("Choice Menu"),
  1168. _(radiolist_instructions),
  1169. selected_index,
  1170. &last_top_row);
  1171. while (!global_exit) {
  1172. if (match_state.in_search) {
  1173. mvprintw(0, 0, "searching: %s",
  1174. match_state.pattern);
  1175. clrtoeol();
  1176. }
  1177. refresh_all_windows(main_window);
  1178. res = wgetch(menu_win(curses_menu));
  1179. if (!res)
  1180. break;
  1181. if (do_match(res, &match_state, &selected_index) == 0) {
  1182. if (selected_index != -1)
  1183. center_item(selected_index,
  1184. &last_top_row);
  1185. continue;
  1186. }
  1187. if (process_special_keys(
  1188. &res,
  1189. (struct menu *) item_data()))
  1190. break;
  1191. switch (res) {
  1192. case KEY_DOWN:
  1193. menu_driver(curses_menu, REQ_DOWN_ITEM);
  1194. break;
  1195. case KEY_UP:
  1196. menu_driver(curses_menu, REQ_UP_ITEM);
  1197. break;
  1198. case KEY_NPAGE:
  1199. menu_driver(curses_menu, REQ_SCR_DPAGE);
  1200. break;
  1201. case KEY_PPAGE:
  1202. menu_driver(curses_menu, REQ_SCR_UPAGE);
  1203. break;
  1204. case KEY_HOME:
  1205. menu_driver(curses_menu, REQ_FIRST_ITEM);
  1206. break;
  1207. case KEY_END:
  1208. menu_driver(curses_menu, REQ_LAST_ITEM);
  1209. break;
  1210. case 'h':
  1211. case '?':
  1212. show_help((struct menu *) item_data());
  1213. break;
  1214. }
  1215. if (res == 10 || res == 27 || res == ' ' ||
  1216. res == KEY_LEFT){
  1217. break;
  1218. }
  1219. refresh_all_windows(main_window);
  1220. }
  1221. /* if ESC or left */
  1222. if (res == 27 || res == KEY_LEFT)
  1223. break;
  1224. child = item_data();
  1225. if (!child || !menu_is_visible(child) || !child->sym)
  1226. continue;
  1227. switch (res) {
  1228. case ' ':
  1229. case 10:
  1230. case KEY_RIGHT:
  1231. sym_set_tristate_value(child->sym, yes);
  1232. return;
  1233. case 'h':
  1234. case '?':
  1235. show_help(child);
  1236. active = child->sym;
  1237. break;
  1238. case KEY_EXIT:
  1239. return;
  1240. }
  1241. }
  1242. }
  1243. static void conf_string(struct menu *menu)
  1244. {
  1245. const char *prompt = menu_get_prompt(menu);
  1246. while (1) {
  1247. int res;
  1248. const char *heading;
  1249. switch (sym_get_type(menu->sym)) {
  1250. case S_INT:
  1251. heading = _(inputbox_instructions_int);
  1252. break;
  1253. case S_HEX:
  1254. heading = _(inputbox_instructions_hex);
  1255. break;
  1256. case S_STRING:
  1257. heading = _(inputbox_instructions_string);
  1258. break;
  1259. default:
  1260. heading = _("Internal nconf error!");
  1261. }
  1262. res = dialog_inputbox(main_window,
  1263. prompt ? _(prompt) : _("Main Menu"),
  1264. heading,
  1265. sym_get_string_value(menu->sym),
  1266. &dialog_input_result,
  1267. &dialog_input_result_len);
  1268. switch (res) {
  1269. case 0:
  1270. if (sym_set_string_value(menu->sym,
  1271. dialog_input_result))
  1272. return;
  1273. btn_dialog(main_window,
  1274. _("You have made an invalid entry."), 0);
  1275. break;
  1276. case 1:
  1277. show_help(menu);
  1278. break;
  1279. case KEY_EXIT:
  1280. return;
  1281. }
  1282. }
  1283. }
  1284. static void conf_load(void)
  1285. {
  1286. while (1) {
  1287. int res;
  1288. res = dialog_inputbox(main_window,
  1289. NULL, load_config_text,
  1290. filename,
  1291. &dialog_input_result,
  1292. &dialog_input_result_len);
  1293. switch (res) {
  1294. case 0:
  1295. if (!dialog_input_result[0])
  1296. return;
  1297. if (!conf_read(dialog_input_result)) {
  1298. set_config_filename(dialog_input_result);
  1299. sym_set_change_count(1);
  1300. return;
  1301. }
  1302. btn_dialog(main_window, _("File does not exist!"), 0);
  1303. break;
  1304. case 1:
  1305. show_scroll_win(main_window,
  1306. _("Load Alternate Configuration"),
  1307. load_config_help);
  1308. break;
  1309. case KEY_EXIT:
  1310. return;
  1311. }
  1312. }
  1313. }
  1314. static void conf_save(void)
  1315. {
  1316. while (1) {
  1317. int res;
  1318. res = dialog_inputbox(main_window,
  1319. NULL, save_config_text,
  1320. filename,
  1321. &dialog_input_result,
  1322. &dialog_input_result_len);
  1323. switch (res) {
  1324. case 0:
  1325. if (!dialog_input_result[0])
  1326. return;
  1327. res = conf_write(dialog_input_result);
  1328. if (!res) {
  1329. set_config_filename(dialog_input_result);
  1330. return;
  1331. }
  1332. btn_dialog(main_window, _("Can't create file! "
  1333. "Probably a nonexistent directory."),
  1334. 1, "<OK>");
  1335. break;
  1336. case 1:
  1337. show_scroll_win(main_window,
  1338. _("Save Alternate Configuration"),
  1339. save_config_help);
  1340. break;
  1341. case KEY_EXIT:
  1342. return;
  1343. }
  1344. }
  1345. }
  1346. void setup_windows(void)
  1347. {
  1348. int lines, columns;
  1349. getmaxyx(stdscr, lines, columns);
  1350. if (main_window != NULL)
  1351. delwin(main_window);
  1352. /* set up the menu and menu window */
  1353. main_window = newwin(lines-2, columns-2, 2, 1);
  1354. keypad(main_window, TRUE);
  1355. mwin_max_lines = lines-7;
  1356. mwin_max_cols = columns-6;
  1357. /* panels order is from bottom to top */
  1358. new_panel(main_window);
  1359. }
  1360. int main(int ac, char **av)
  1361. {
  1362. int lines, columns;
  1363. char *mode;
  1364. setlocale(LC_ALL, "");
  1365. bindtextdomain(PACKAGE, LOCALEDIR);
  1366. textdomain(PACKAGE);
  1367. conf_parse(av[1]);
  1368. conf_read(NULL);
  1369. mode = getenv("NCONFIG_MODE");
  1370. if (mode) {
  1371. if (!strcasecmp(mode, "single_menu"))
  1372. single_menu_mode = 1;
  1373. }
  1374. /* Initialize curses */
  1375. initscr();
  1376. /* set color theme */
  1377. set_colors();
  1378. cbreak();
  1379. noecho();
  1380. keypad(stdscr, TRUE);
  1381. curs_set(0);
  1382. getmaxyx(stdscr, lines, columns);
  1383. if (columns < 75 || lines < 20) {
  1384. endwin();
  1385. printf("Your terminal should have at "
  1386. "least 20 lines and 75 columns\n");
  1387. return 1;
  1388. }
  1389. notimeout(stdscr, FALSE);
  1390. #if NCURSES_REENTRANT
  1391. set_escdelay(1);
  1392. #else
  1393. ESCDELAY = 1;
  1394. #endif
  1395. /* set btns menu */
  1396. curses_menu = new_menu(curses_menu_items);
  1397. menu_opts_off(curses_menu, O_SHOWDESC);
  1398. menu_opts_on(curses_menu, O_SHOWMATCH);
  1399. menu_opts_on(curses_menu, O_ONEVALUE);
  1400. menu_opts_on(curses_menu, O_NONCYCLIC);
  1401. menu_opts_on(curses_menu, O_IGNORECASE);
  1402. set_menu_mark(curses_menu, " ");
  1403. set_menu_fore(curses_menu, attributes[MAIN_MENU_FORE]);
  1404. set_menu_back(curses_menu, attributes[MAIN_MENU_BACK]);
  1405. set_menu_grey(curses_menu, attributes[MAIN_MENU_GREY]);
  1406. set_config_filename(conf_get_configname());
  1407. setup_windows();
  1408. /* check for KEY_FUNC(1) */
  1409. if (has_key(KEY_F(1)) == FALSE) {
  1410. show_scroll_win(main_window,
  1411. _("Instructions"),
  1412. _(menu_no_f_instructions));
  1413. }
  1414. conf_set_message_callback(conf_message_callback);
  1415. /* do the work */
  1416. while (!global_exit) {
  1417. conf(&rootmenu);
  1418. if (!global_exit && do_exit() == 0)
  1419. break;
  1420. }
  1421. /* ok, we are done */
  1422. unpost_menu(curses_menu);
  1423. free_menu(curses_menu);
  1424. delwin(main_window);
  1425. clear();
  1426. refresh();
  1427. endwin();
  1428. return 0;
  1429. }