mconf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  3. * Released under the terms of the GNU GPL v2.0.
  4. *
  5. * Introduced single menu mode (show all sub-menus in one large tree).
  6. * 2002-11-06 Petr Baudis <pasky@ucw.cz>
  7. *
  8. * Directly use liblxdialog library routines.
  9. * 2002-11-14 Petr Baudis <pasky@ucw.cz>
  10. */
  11. #include <sys/ioctl.h>
  12. #include <sys/wait.h>
  13. #include <sys/termios.h>
  14. #include <ctype.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <limits.h>
  18. #include <signal.h>
  19. #include <stdarg.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <termios.h>
  23. #include <unistd.h>
  24. #include "dialog.h"
  25. #define LKC_DIRECT_LINK
  26. #include "lkc.h"
  27. static char menu_backtitle[128];
  28. static const char menu_instructions[] =
  29. "Arrow keys navigate the menu. "
  30. "<Enter> selects submenus --->. "
  31. "Highlighted letters are hotkeys. "
  32. "Pressing <Y> selectes a feature, while <N> will exclude a feature. "
  33. "Press <Esc><Esc> to exit, <?> for Help. "
  34. "Legend: [*] feature is selected [ ] feature is excluded",
  35. radiolist_instructions[] =
  36. "Use the arrow keys to navigate this window or "
  37. "press the hotkey of the item you wish to select "
  38. "followed by the <SPACE BAR>. "
  39. "Press <?> for additional information about this option.",
  40. inputbox_instructions_int[] =
  41. "Please enter a decimal value. "
  42. "Fractions will not be accepted. "
  43. "Use the <TAB> key to move from the input field to the buttons below it.",
  44. inputbox_instructions_hex[] =
  45. "Please enter a hexadecimal value. "
  46. "Use the <TAB> key to move from the input field to the buttons below it.",
  47. inputbox_instructions_string[] =
  48. "Please enter a string value. "
  49. "Use the <TAB> key to move from the input field to the buttons below it.",
  50. setmod_text[] =
  51. "This feature depends on another which has been configured as a module.\n"
  52. "As a result, this feature will be built as a module.",
  53. nohelp_text[] =
  54. "There is no help available for this option.\n",
  55. load_config_text[] =
  56. "Enter the name of the configuration file you wish to load. "
  57. "Accept the name shown to restore the configuration you "
  58. "last retrieved. Leave blank to abort.",
  59. load_config_help[] =
  60. "\n"
  61. "For various reasons, one may wish to keep several different OpenADK\n"
  62. "configurations available on a single machine.\n"
  63. "\n"
  64. "If you have saved a previous configuration in a file other than the\n"
  65. "OpenADK's default, entering the name of the file here will allow you\n"
  66. "to modify that configuration.\n"
  67. "\n"
  68. "If you are uncertain, then you have probably never used alternate\n"
  69. "configuration files. You should therefor leave this blank to abort.\n",
  70. save_config_text[] =
  71. "Enter a filename to which this configuration should be saved "
  72. "as an alternate. Leave blank to abort.",
  73. save_config_help[] =
  74. "\n"
  75. "For various reasons, one may wish to keep different OpenADK\n"
  76. "configurations available on a single machine.\n"
  77. "\n"
  78. "Entering a file name here will allow you to later retrieve, modify\n"
  79. "and use the current configuration as an alternate to whatever\n"
  80. "configuration options you have selected at that time.\n"
  81. "\n"
  82. "If you are uncertain what all this means then you should probably\n"
  83. "leave this blank.\n",
  84. top_menu_help[] =
  85. "\n"
  86. "Use the Up/Down arrow keys (cursor keys) to highlight the item\n"
  87. "you wish to change or submenu wish to select and press <Enter>.\n"
  88. "Submenus are designated by \"--->\".\n"
  89. "\n"
  90. "Shortcut: Press the option's highlighted letter (hotkey).\n"
  91. "\n"
  92. "You may also use the <PAGE UP> and <PAGE DOWN> keys to scroll\n"
  93. "unseen options into view.\n"
  94. ;
  95. static char filename[PATH_MAX+1] = ".config";
  96. static int indent = 0;
  97. static struct termios ios_org;
  98. static int rows, cols;
  99. struct menu *current_menu;
  100. static int child_count;
  101. static int single_menu_mode;
  102. static struct dialog_list_item *items[16384]; /* FIXME: This ought to be dynamic. */
  103. static int item_no;
  104. static void conf(struct menu *menu);
  105. static void conf_choice(struct menu *menu);
  106. static void conf_string(struct menu *menu);
  107. static void conf_load(void);
  108. static void conf_save(void);
  109. static void show_textbox(const char *title, const char *text, int r, int c);
  110. static void show_helptext(const char *title, const char *text);
  111. static void show_help(struct menu *menu);
  112. static void show_readme(void);
  113. static void init_wsize(void)
  114. {
  115. struct winsize ws;
  116. char *env;
  117. if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
  118. rows = 24;
  119. cols = 80;
  120. } else {
  121. rows = ws.ws_row;
  122. cols = ws.ws_col;
  123. if (!rows) {
  124. env = getenv("LINES");
  125. if (env)
  126. rows = atoi(env);
  127. if (!rows)
  128. rows = 24;
  129. }
  130. if (!cols) {
  131. env = getenv("COLUMNS");
  132. if (env)
  133. cols = atoi(env);
  134. if (!cols)
  135. cols = 80;
  136. }
  137. }
  138. if (rows < 19 || cols < 80) {
  139. fprintf(stderr, "Your display is too small to run Menuconfig!\n");
  140. fprintf(stderr, "It must be at least 19 lines by 80 columns.\n");
  141. exit(1);
  142. }
  143. rows -= 4;
  144. cols -= 5;
  145. }
  146. static void cinit(void)
  147. {
  148. item_no = 0;
  149. }
  150. static void cmake(void)
  151. {
  152. items[item_no] = malloc(sizeof(struct dialog_list_item));
  153. memset(items[item_no], 0, sizeof(struct dialog_list_item));
  154. items[item_no]->tag = malloc(32); items[item_no]->tag[0] = 0;
  155. items[item_no]->name = malloc(512); items[item_no]->name[0] = 0;
  156. items[item_no]->namelen = 0;
  157. item_no++;
  158. }
  159. static int cprint_name(const char *fmt, ...)
  160. {
  161. va_list ap;
  162. int res;
  163. if (!item_no)
  164. cmake();
  165. va_start(ap, fmt);
  166. res = vsnprintf(items[item_no - 1]->name + items[item_no - 1]->namelen,
  167. 512 - items[item_no - 1]->namelen, fmt, ap);
  168. if (res > 0)
  169. items[item_no - 1]->namelen += res;
  170. va_end(ap);
  171. return res;
  172. }
  173. static int cprint_tag(const char *fmt, ...)
  174. {
  175. va_list ap;
  176. int res;
  177. if (!item_no)
  178. cmake();
  179. va_start(ap, fmt);
  180. res = vsnprintf(items[item_no - 1]->tag, 32, fmt, ap);
  181. va_end(ap);
  182. return res;
  183. }
  184. static void cdone(void)
  185. {
  186. int i;
  187. for (i = 0; i < item_no; i++) {
  188. free(items[i]->tag);
  189. free(items[i]->name);
  190. free(items[i]);
  191. }
  192. item_no = 0;
  193. }
  194. static void build_conf(struct menu *menu)
  195. {
  196. struct symbol *sym;
  197. struct property *prop;
  198. struct menu *child;
  199. int type, tmp, doint = 2;
  200. tristate val;
  201. char ch;
  202. if (!menu_is_visible(menu))
  203. return;
  204. sym = menu->sym;
  205. prop = menu->prompt;
  206. if (!sym) {
  207. if (prop && menu != current_menu) {
  208. const char *prompt = menu_get_prompt(menu);
  209. switch (prop->type) {
  210. case P_MENU:
  211. child_count++;
  212. cmake();
  213. cprint_tag("m%p", menu);
  214. if (single_menu_mode) {
  215. cprint_name("%s%*c%s",
  216. menu->data ? "-->" : "++>",
  217. indent + 1, ' ', prompt);
  218. } else {
  219. cprint_name(" %*c%s --->", indent + 1, ' ', prompt);
  220. }
  221. if (single_menu_mode && menu->data)
  222. goto conf_childs;
  223. return;
  224. default:
  225. if (prompt) {
  226. child_count++;
  227. cmake();
  228. cprint_tag(":%p", menu);
  229. cprint_name("---%*c%s", indent + 1, ' ', prompt);
  230. }
  231. }
  232. } else
  233. doint = 0;
  234. goto conf_childs;
  235. }
  236. cmake();
  237. type = sym_get_type(sym);
  238. if (sym_is_choice(sym)) {
  239. struct symbol *def_sym = sym_get_choice_value(sym);
  240. struct menu *def_menu = NULL;
  241. child_count++;
  242. for (child = menu->list; child; child = child->next) {
  243. if (menu_is_visible(child) && child->sym == def_sym)
  244. def_menu = child;
  245. }
  246. val = sym_get_tristate_value(sym);
  247. if (sym_is_changable(sym)) {
  248. cprint_tag("t%p", menu);
  249. switch (type) {
  250. case S_BOOLEAN:
  251. cprint_name("[%c]", val == no ? ' ' : '*');
  252. break;
  253. case S_TRISTATE:
  254. switch (val) {
  255. case yes: ch = '*'; break;
  256. case mod: ch = 'M'; break;
  257. default: ch = ' '; break;
  258. }
  259. cprint_name("<%c>", ch);
  260. break;
  261. }
  262. } else {
  263. cprint_tag("%c%p", def_menu ? 't' : ':', menu);
  264. cprint_name(" ");
  265. }
  266. cprint_name("%*c%s", indent + 1, ' ', menu_get_prompt(menu));
  267. if (val == yes) {
  268. if (def_menu) {
  269. cprint_name(" (%s)", menu_get_prompt(def_menu));
  270. cprint_name(" --->");
  271. if (def_menu->list) {
  272. indent += 2;
  273. build_conf(def_menu);
  274. indent -= 2;
  275. }
  276. }
  277. return;
  278. }
  279. } else {
  280. if (menu == current_menu) {
  281. cprint_tag(":%p", menu);
  282. cprint_name("---%*c%s", indent + 1, ' ', menu_get_prompt(menu));
  283. goto conf_childs;
  284. }
  285. child_count++;
  286. val = sym_get_tristate_value(sym);
  287. if (sym_is_choice_value(sym) && val == yes) {
  288. cprint_tag(":%p", menu);
  289. cprint_name(" ");
  290. } else {
  291. switch (type) {
  292. case S_BOOLEAN:
  293. cprint_tag("t%p", menu);
  294. if (sym_is_changable(sym))
  295. cprint_name("[%c]", val == no ? ' ' : '*');
  296. else
  297. cprint_name(val == no ? "_ _" : "-+-");
  298. break;
  299. case S_TRISTATE:
  300. cprint_tag("t%p", menu);
  301. switch (val) {
  302. case yes: ch = '*'; break;
  303. case mod: ch = 'M'; break;
  304. default: ch = ' '; break;
  305. }
  306. if (sym_is_changable(sym))
  307. cprint_name("<%c>", ch);
  308. else
  309. cprint_name(val == no ? "_ _" : "-+-");
  310. break;
  311. default:
  312. cprint_tag("s%p", menu);
  313. tmp = cprint_name("(%s)", sym_get_string_value(sym));
  314. tmp = indent - tmp + 4;
  315. if (tmp < 0)
  316. tmp = 0;
  317. cprint_name("%*c%s%s", tmp, ' ', menu_get_prompt(menu),
  318. (sym_has_value(sym) || !sym_is_changable(sym)) ?
  319. "" : " (NEW)");
  320. goto conf_childs;
  321. }
  322. }
  323. cprint_name("%*c%s%s", indent + 1, ' ', menu_get_prompt(menu),
  324. (sym_has_value(sym) || !sym_is_changable(sym)) ?
  325. "" : " (NEW)");
  326. if (menu->prompt->type == P_MENU) {
  327. cprint_name(" --->");
  328. return;
  329. }
  330. }
  331. conf_childs:
  332. indent += doint;
  333. for (child = menu->list; child; child = child->next)
  334. build_conf(child);
  335. indent -= doint;
  336. }
  337. static void conf(struct menu *menu)
  338. {
  339. struct dialog_list_item *active_item = NULL;
  340. struct menu *submenu;
  341. const char *prompt = menu_get_prompt(menu);
  342. struct symbol *sym;
  343. char active_entry[40];
  344. int stat, type;
  345. unlink("lxdialog.scrltmp");
  346. active_entry[0] = 0;
  347. while (1) {
  348. indent = 0;
  349. child_count = 0;
  350. current_menu = menu;
  351. cdone(); cinit();
  352. build_conf(menu);
  353. if (!child_count)
  354. break;
  355. if (menu == &rootmenu) {
  356. cmake(); cprint_tag(":"); cprint_name("--- ");
  357. cmake(); cprint_tag("L"); cprint_name("Load an Alternate Configuration File");
  358. cmake(); cprint_tag("S"); cprint_name("Save Configuration to an Alternate File");
  359. }
  360. dialog_clear();
  361. stat = dialog_menu(prompt ? prompt : "Main Menu",
  362. menu_instructions, rows, cols, rows - 10,
  363. active_entry, item_no, items);
  364. if (stat < 0)
  365. return;
  366. if (stat == 1 || stat == 255)
  367. break;
  368. active_item = first_sel_item(item_no, items);
  369. if (!active_item)
  370. continue;
  371. active_item->selected = 0;
  372. strncpy(active_entry, active_item->tag, sizeof(active_entry));
  373. active_entry[sizeof(active_entry)-1] = 0;
  374. type = active_entry[0];
  375. if (!type)
  376. continue;
  377. sym = NULL;
  378. submenu = NULL;
  379. if (sscanf(active_entry + 1, "%p", &submenu) == 1)
  380. sym = submenu->sym;
  381. switch (stat) {
  382. case 0:
  383. switch (type) {
  384. case 'm':
  385. if (single_menu_mode)
  386. submenu->data = (void *) (long) !submenu->data;
  387. else
  388. conf(submenu);
  389. break;
  390. case 't':
  391. if (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)
  392. conf_choice(submenu);
  393. else if (submenu->prompt->type == P_MENU)
  394. conf(submenu);
  395. break;
  396. case 's':
  397. conf_string(submenu);
  398. break;
  399. case 'L':
  400. conf_load();
  401. break;
  402. case 'S':
  403. conf_save();
  404. break;
  405. }
  406. break;
  407. case 2:
  408. if (sym)
  409. show_help(submenu);
  410. else
  411. show_readme();
  412. break;
  413. case 3:
  414. if (type == 't') {
  415. if (sym_set_tristate_value(sym, yes))
  416. break;
  417. if (sym_set_tristate_value(sym, mod))
  418. show_textbox(NULL, setmod_text, 6, 74);
  419. }
  420. break;
  421. case 4:
  422. if (type == 't')
  423. sym_set_tristate_value(sym, no);
  424. break;
  425. case 5:
  426. if (type == 't')
  427. sym_set_tristate_value(sym, mod);
  428. break;
  429. case 6:
  430. if (type == 't')
  431. sym_toggle_tristate_value(sym);
  432. else if (type == 'm')
  433. conf(submenu);
  434. break;
  435. }
  436. }
  437. }
  438. static void show_textbox(const char *title, const char *text, int r, int c)
  439. {
  440. int fd;
  441. fd = creat(".help.tmp", 0777);
  442. write(fd, text, strlen(text));
  443. close(fd);
  444. while (dialog_textbox(title, ".help.tmp", r, c) < 0)
  445. ;
  446. unlink(".help.tmp");
  447. }
  448. static void show_helptext(const char *title, const char *text)
  449. {
  450. show_textbox(title, text, rows, cols);
  451. }
  452. static void show_help(struct menu *menu)
  453. {
  454. const char *help;
  455. char *helptext;
  456. struct symbol *sym = menu->sym;
  457. help = sym->help;
  458. if (!help)
  459. help = nohelp_text;
  460. if (sym->name) {
  461. helptext = malloc(strlen(sym->name) + strlen(help) + 16);
  462. sprintf(helptext, "%s:\n\n%s", sym->name, help);
  463. show_helptext(menu_get_prompt(menu), helptext);
  464. free(helptext);
  465. } else
  466. show_helptext(menu_get_prompt(menu), help);
  467. }
  468. static void show_readme(void)
  469. {
  470. show_helptext("Help", top_menu_help);
  471. }
  472. static void conf_choice(struct menu *menu)
  473. {
  474. const char *prompt = menu_get_prompt(menu);
  475. struct menu *child;
  476. struct symbol *active;
  477. active = sym_get_choice_value(menu->sym);
  478. while (1) {
  479. current_menu = menu;
  480. cdone(); cinit();
  481. for (child = menu->list; child; child = child->next) {
  482. if (!menu_is_visible(child))
  483. continue;
  484. cmake();
  485. cprint_tag("%p", child);
  486. cprint_name("%s", menu_get_prompt(child));
  487. if (child->sym == sym_get_choice_value(menu->sym))
  488. items[item_no - 1]->selected = 1; /* ON */
  489. else if (child->sym == active)
  490. items[item_no - 1]->selected = 2; /* SELECTED */
  491. else
  492. items[item_no - 1]->selected = 0; /* OFF */
  493. }
  494. switch (dialog_checklist(prompt ? prompt : "Main Menu",
  495. radiolist_instructions, 15, 70, 6,
  496. item_no, items, FLAG_RADIO)) {
  497. case 0:
  498. if (sscanf(first_sel_item(item_no, items)->tag, "%p", &child) != 1)
  499. break;
  500. sym_set_tristate_value(child->sym, yes);
  501. return;
  502. case 1:
  503. if (sscanf(first_sel_item(item_no, items)->tag, "%p", &child) == 1) {
  504. show_help(child);
  505. active = child->sym;
  506. } else
  507. show_help(menu);
  508. break;
  509. case 255:
  510. return;
  511. }
  512. }
  513. }
  514. static void conf_string(struct menu *menu)
  515. {
  516. const char *prompt = menu_get_prompt(menu);
  517. while (1) {
  518. char *heading;
  519. switch (sym_get_type(menu->sym)) {
  520. case S_INT:
  521. heading = (char *) inputbox_instructions_int;
  522. break;
  523. case S_HEX:
  524. heading = (char *) inputbox_instructions_hex;
  525. break;
  526. case S_STRING:
  527. heading = (char *) inputbox_instructions_string;
  528. break;
  529. default:
  530. heading = "Internal mconf error!";
  531. /* panic? */;
  532. }
  533. switch (dialog_inputbox(prompt ? prompt : "Main Menu",
  534. heading, 10, 75,
  535. sym_get_string_value(menu->sym))) {
  536. case 0:
  537. if (sym_set_string_value(menu->sym, (const char*)dialog_input_result))
  538. return;
  539. show_textbox(NULL, "You have made an invalid entry.", 5, 43);
  540. break;
  541. case 1:
  542. show_help(menu);
  543. break;
  544. case 255:
  545. return;
  546. }
  547. }
  548. }
  549. static void conf_load(void)
  550. {
  551. while (1) {
  552. switch (dialog_inputbox(NULL, load_config_text, 11, 55,
  553. filename)) {
  554. case 0:
  555. if (!dialog_input_result[0])
  556. return;
  557. if (!conf_read((const char*)dialog_input_result))
  558. return;
  559. show_textbox(NULL, "File does not exist!", 5, 38);
  560. break;
  561. case 1:
  562. show_helptext("Load Alternate Configuration", load_config_help);
  563. break;
  564. case 255:
  565. return;
  566. }
  567. }
  568. }
  569. static void conf_save(void)
  570. {
  571. while (1) {
  572. switch (dialog_inputbox(NULL, save_config_text, 11, 55,
  573. filename)) {
  574. case 0:
  575. if (!dialog_input_result[0])
  576. return;
  577. if (!conf_write((const char*)dialog_input_result))
  578. return;
  579. show_textbox(NULL, "Can't create file! Probably a nonexistent directory.", 5, 60);
  580. break;
  581. case 1:
  582. show_helptext("Save Alternate Configuration", save_config_help);
  583. break;
  584. case 255:
  585. return;
  586. }
  587. }
  588. }
  589. static void conf_cleanup(void)
  590. {
  591. tcsetattr(1, TCSAFLUSH, &ios_org);
  592. unlink(".help.tmp");
  593. }
  594. static void winch_handler(int sig)
  595. {
  596. struct winsize ws;
  597. if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
  598. rows = 24;
  599. cols = 80;
  600. } else {
  601. rows = ws.ws_row;
  602. cols = ws.ws_col;
  603. }
  604. if (rows < 19 || cols < 80) {
  605. end_dialog();
  606. fprintf(stderr, "Your display is too small to run Menuconfig!\n");
  607. fprintf(stderr, "It must be at least 19 lines by 80 columns.\n");
  608. exit(1);
  609. }
  610. rows -= 4;
  611. cols -= 5;
  612. }
  613. int main(int ac, char **av)
  614. {
  615. int stat;
  616. char *mode;
  617. struct symbol *sym;
  618. conf_parse(av[1]);
  619. conf_read(NULL);
  620. sym = sym_lookup("VERSION", 0);
  621. sym_calc_value(sym);
  622. snprintf(menu_backtitle, 128, "OpenADK v%s Configuration",
  623. sym_get_string_value(sym));
  624. mode = getenv("MENUCONFIG_MODE");
  625. if (mode) {
  626. if (!strcasecmp(mode, "single_menu"))
  627. single_menu_mode = 1;
  628. }
  629. tcgetattr(1, &ios_org);
  630. atexit(conf_cleanup);
  631. init_wsize();
  632. init_dialog();
  633. signal(SIGWINCH, winch_handler);
  634. conf(&rootmenu);
  635. end_dialog();
  636. /* Restart dialog to act more like when lxdialog was still separate */
  637. init_dialog();
  638. do {
  639. stat = dialog_yesno(NULL,
  640. "Do you wish to save your new OpenADK configuration?", 5, 60);
  641. } while (stat < 0);
  642. end_dialog();
  643. if (stat == 0) {
  644. conf_write(NULL);
  645. printf("\n\n*** End of OpenADK configuration.\n");
  646. } else
  647. printf("\n\nYour OpenADK configuration changes were NOT saved.\n\n");
  648. return 0;
  649. }