mconf.c 15 KB

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