3
0

conf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  3. * Released under the terms of the GNU GPL v2.0.
  4. */
  5. #define _XOPEN_SOURCE 700
  6. #include <ctype.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <time.h>
  12. #include <sys/stat.h>
  13. #define LKC_DIRECT_LINK
  14. #include "lkc.h"
  15. static void conf(struct menu *menu);
  16. static void check_conf(struct menu *menu);
  17. enum {
  18. ask_all,
  19. ask_new,
  20. ask_silent,
  21. set_default,
  22. set_yes,
  23. set_mod,
  24. set_no,
  25. set_random
  26. } input_mode = ask_all;
  27. char *defconfig_file;
  28. static int indent = 1;
  29. static int valid_stdin = 1;
  30. static int conf_cnt;
  31. static char line[128];
  32. static struct menu *rootEntry;
  33. static char nohelp_text[] = N_("Sorry, no help available for this option yet.\n");
  34. static void strip(char *str)
  35. {
  36. char *p = str;
  37. int l;
  38. while ((isspace(*p)))
  39. p++;
  40. l = strlen(p);
  41. if (p != str)
  42. memmove(str, p, l + 1);
  43. if (!l)
  44. return;
  45. p = str + l - 1;
  46. while ((isspace(*p)))
  47. *p-- = 0;
  48. }
  49. static void check_stdin(void)
  50. {
  51. if (!valid_stdin && input_mode == ask_silent) {
  52. printf(_("aborted!\n\n"));
  53. printf(_("Console input/output is redirected. "));
  54. printf(_("Run 'make oldconfig' to update configuration.\n\n"));
  55. exit(1);
  56. }
  57. }
  58. static void conf_askvalue(struct symbol *sym, const char *def)
  59. {
  60. enum symbol_type type = sym_get_type(sym);
  61. tristate val;
  62. if (!sym_has_value(sym))
  63. printf("(NEW) ");
  64. line[0] = '\n';
  65. line[1] = 0;
  66. line[2] = 0;
  67. if (!sym_is_changable(sym)) {
  68. printf("%s\n", def);
  69. return;
  70. }
  71. // If autoconf run (allnoconfig and such), reset bool and tristates:
  72. // "select ITEM" sets ITEM=y and then parent item might have been
  73. // reset to "n" later. Try to set ITEM to "n" on the second run.
  74. if (type == S_BOOLEAN || type == S_TRISTATE) {
  75. switch (input_mode) {
  76. case set_yes:
  77. if (sym_tristate_within_range(sym, yes)) {
  78. line[0] = 'y';
  79. line[1] = '\n';
  80. printf("%s", line);
  81. return;
  82. }
  83. case set_mod:
  84. if (type == S_TRISTATE) {
  85. if (sym_tristate_within_range(sym, mod)) {
  86. line[0] = 'm';
  87. line[1] = '\n';
  88. printf("%s", line);
  89. return;
  90. }
  91. } else {
  92. if (sym_tristate_within_range(sym, yes)) {
  93. line[0] = 'y';
  94. line[1] = '\n';
  95. printf("%s", line);
  96. return;
  97. }
  98. }
  99. case set_no:
  100. if (sym_tristate_within_range(sym, no)) {
  101. line[0] = 'n';
  102. line[1] = '\n';
  103. printf("%s", line);
  104. return;
  105. }
  106. default: // placate compiler
  107. break;
  108. }
  109. }
  110. switch (input_mode) {
  111. case set_no:
  112. case set_mod:
  113. case set_yes:
  114. case set_random:
  115. if (sym_has_value(sym)) {
  116. printf("%s\n", def);
  117. return;
  118. }
  119. break;
  120. case ask_new:
  121. case ask_silent:
  122. if (sym_has_value(sym)) {
  123. printf("%s\n", def);
  124. return;
  125. }
  126. check_stdin();
  127. case ask_all:
  128. fflush(stdout);
  129. if (!fgets(line, 128, stdin))
  130. exit(1);
  131. return;
  132. case set_default:
  133. printf("%s\n", def);
  134. return;
  135. default:
  136. break;
  137. }
  138. switch (type) {
  139. case S_INT:
  140. case S_HEX:
  141. case S_STRING:
  142. printf("%s\n", def);
  143. return;
  144. default:
  145. ;
  146. }
  147. switch (input_mode) {
  148. case set_yes:
  149. if (sym_tristate_within_range(sym, yes)) {
  150. line[0] = 'y';
  151. line[1] = '\n';
  152. line[2] = 0;
  153. break;
  154. }
  155. case set_mod:
  156. if (type == S_TRISTATE) {
  157. if (sym_tristate_within_range(sym, mod)) {
  158. line[0] = 'm';
  159. line[1] = '\n';
  160. line[2] = 0;
  161. break;
  162. }
  163. } else {
  164. if (sym_tristate_within_range(sym, yes)) {
  165. line[0] = 'y';
  166. line[1] = '\n';
  167. line[2] = 0;
  168. break;
  169. }
  170. }
  171. case set_no:
  172. if (sym_tristate_within_range(sym, no)) {
  173. line[0] = 'n';
  174. line[1] = '\n';
  175. line[2] = 0;
  176. break;
  177. }
  178. case set_random:
  179. do {
  180. val = (tristate)(random() % 3);
  181. } while (!sym_tristate_within_range(sym, val));
  182. switch (val) {
  183. case no: line[0] = 'n'; break;
  184. case mod: line[0] = 'm'; break;
  185. case yes: line[0] = 'y'; break;
  186. }
  187. line[1] = '\n';
  188. line[2] = 0;
  189. break;
  190. default:
  191. break;
  192. }
  193. printf("%s", line);
  194. }
  195. int conf_string(struct menu *menu)
  196. {
  197. struct symbol *sym = menu->sym;
  198. const char *def;
  199. while (1) {
  200. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  201. printf("(%s) ", sym->name);
  202. def = sym_get_string_value(sym);
  203. if (sym_get_string_value(sym))
  204. printf("[%s] ", def);
  205. conf_askvalue(sym, def);
  206. switch (line[0]) {
  207. case '\n':
  208. break;
  209. case '?':
  210. /* print help */
  211. if (line[1] == '\n') {
  212. printf("\n%s\n", menu->sym->help ? menu->sym->help : nohelp_text);
  213. def = NULL;
  214. break;
  215. }
  216. default:
  217. line[strlen(line)-1] = 0;
  218. def = line;
  219. }
  220. if (def && sym_set_string_value(sym, def))
  221. return 0;
  222. }
  223. }
  224. static int conf_sym(struct menu *menu)
  225. {
  226. struct symbol *sym = menu->sym;
  227. tristate oldval, newval;
  228. const char *help;
  229. while (1) {
  230. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  231. if (sym->name)
  232. printf("(%s) ", sym->name);
  233. putchar('[');
  234. oldval = sym_get_tristate_value(sym);
  235. switch (oldval) {
  236. case no:
  237. putchar('N');
  238. break;
  239. case mod:
  240. putchar('M');
  241. break;
  242. case yes:
  243. putchar('Y');
  244. break;
  245. }
  246. if (oldval != no && sym_tristate_within_range(sym, no))
  247. printf("/n");
  248. if (oldval != mod && sym_tristate_within_range(sym, mod))
  249. printf("/m");
  250. if (oldval != yes && sym_tristate_within_range(sym, yes))
  251. printf("/y");
  252. if (sym->help)
  253. printf("/?");
  254. printf("] ");
  255. conf_askvalue(sym, sym_get_string_value(sym));
  256. strip(line);
  257. switch (line[0]) {
  258. case 'n':
  259. case 'N':
  260. newval = no;
  261. if (!line[1] || !strcmp(&line[1], "o"))
  262. break;
  263. continue;
  264. case 'm':
  265. case 'M':
  266. newval = mod;
  267. if (!line[1])
  268. break;
  269. continue;
  270. case 'y':
  271. case 'Y':
  272. newval = yes;
  273. if (!line[1] || !strcmp(&line[1], "es"))
  274. break;
  275. continue;
  276. case 0:
  277. newval = oldval;
  278. break;
  279. case '?':
  280. goto help;
  281. default:
  282. continue;
  283. }
  284. if (sym_set_tristate_value(sym, newval))
  285. return 0;
  286. help:
  287. help = nohelp_text;
  288. if (sym->help)
  289. help = sym->help;
  290. printf("\n%s\n", help);
  291. }
  292. }
  293. static int conf_choice(struct menu *menu)
  294. {
  295. struct symbol *sym, *def_sym;
  296. struct menu *child;
  297. bool is_new;
  298. sym = menu->sym;
  299. is_new = !sym_has_value(sym);
  300. if (sym_is_changable(sym)) {
  301. conf_sym(menu);
  302. sym_calc_value(sym);
  303. switch (sym_get_tristate_value(sym)) {
  304. case no:
  305. return 1;
  306. case mod:
  307. return 0;
  308. case yes:
  309. break;
  310. }
  311. } else {
  312. switch (sym_get_tristate_value(sym)) {
  313. case no:
  314. return 1;
  315. case mod:
  316. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  317. return 0;
  318. case yes:
  319. break;
  320. }
  321. }
  322. while (1) {
  323. int cnt, def;
  324. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  325. def_sym = sym_get_choice_value(sym);
  326. cnt = def = 0;
  327. line[0] = 0;
  328. for (child = menu->list; child; child = child->next) {
  329. if (!menu_is_visible(child))
  330. continue;
  331. if (!child->sym) {
  332. printf("%*c %s\n", indent, '*', menu_get_prompt(child));
  333. continue;
  334. }
  335. cnt++;
  336. if (child->sym == def_sym) {
  337. def = cnt;
  338. printf("%*c", indent, '>');
  339. } else
  340. printf("%*c", indent, ' ');
  341. printf(" %d. %s", cnt, menu_get_prompt(child));
  342. if (child->sym->name)
  343. printf(" (%s)", child->sym->name);
  344. if (!sym_has_value(child->sym))
  345. printf(" (NEW)");
  346. printf("\n");
  347. }
  348. printf("%*schoice", indent - 1, "");
  349. if (cnt == 1) {
  350. printf("[1]: 1\n");
  351. goto conf_childs;
  352. }
  353. printf("[1-%d", cnt);
  354. if (sym->help)
  355. printf("?");
  356. printf("]: ");
  357. switch (input_mode) {
  358. case ask_new:
  359. case ask_silent:
  360. if (!is_new) {
  361. cnt = def;
  362. printf("%d\n", cnt);
  363. break;
  364. }
  365. check_stdin();
  366. case ask_all:
  367. fflush(stdout);
  368. if (!fgets(line, 128, stdin))
  369. exit(1);
  370. strip(line);
  371. if (line[0] == '?') {
  372. printf("\n%s\n", menu->sym->help ?
  373. menu->sym->help : nohelp_text);
  374. continue;
  375. }
  376. if (!line[0])
  377. cnt = def;
  378. else if (isdigit(line[0]))
  379. cnt = atoi(line);
  380. else
  381. continue;
  382. break;
  383. case set_random:
  384. def = (random() % cnt) + 1;
  385. case set_default:
  386. case set_yes:
  387. case set_mod:
  388. case set_no:
  389. cnt = def;
  390. printf("%d\n", cnt);
  391. break;
  392. }
  393. conf_childs:
  394. for (child = menu->list; child; child = child->next) {
  395. if (!child->sym || !menu_is_visible(child))
  396. continue;
  397. if (!--cnt)
  398. break;
  399. }
  400. if (!child)
  401. continue;
  402. if (strlen(line) > 0 && line[strlen(line) - 1] == '?') {
  403. printf("\n%s\n", child->sym->help ?
  404. child->sym->help : nohelp_text);
  405. continue;
  406. }
  407. sym_set_choice_value(sym, child->sym);
  408. if (child->list) {
  409. indent += 2;
  410. conf(child->list);
  411. indent -= 2;
  412. }
  413. return 1;
  414. }
  415. }
  416. static void conf(struct menu *menu)
  417. {
  418. struct symbol *sym;
  419. struct property *prop;
  420. struct menu *child;
  421. if (!menu_is_visible(menu))
  422. return;
  423. sym = menu->sym;
  424. prop = menu->prompt;
  425. if (prop) {
  426. const char *prompt;
  427. switch (prop->type) {
  428. case P_MENU:
  429. if (input_mode == ask_silent && rootEntry != menu) {
  430. check_conf(menu);
  431. return;
  432. }
  433. case P_COMMENT:
  434. prompt = menu_get_prompt(menu);
  435. if (prompt)
  436. printf("%*c\n%*c %s\n%*c\n",
  437. indent, '*',
  438. indent, '*', prompt,
  439. indent, '*');
  440. default:
  441. ;
  442. }
  443. }
  444. if (!sym)
  445. goto conf_childs;
  446. if (sym_is_choice(sym)) {
  447. conf_choice(menu);
  448. if (sym->curr.tri != mod)
  449. return;
  450. goto conf_childs;
  451. }
  452. switch (sym->type) {
  453. case S_INT:
  454. case S_HEX:
  455. case S_STRING:
  456. conf_string(menu);
  457. break;
  458. default:
  459. conf_sym(menu);
  460. break;
  461. }
  462. conf_childs:
  463. if (sym)
  464. indent += 2;
  465. for (child = menu->list; child; child = child->next)
  466. conf(child);
  467. if (sym)
  468. indent -= 2;
  469. }
  470. static void check_conf(struct menu *menu)
  471. {
  472. struct symbol *sym;
  473. struct menu *child;
  474. if (!menu_is_visible(menu))
  475. return;
  476. sym = menu->sym;
  477. if (sym && !sym_has_value(sym)) {
  478. if (sym_is_changable(sym) ||
  479. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
  480. if (!conf_cnt++)
  481. printf(_("*\n* Restart config...\n*\n"));
  482. rootEntry = menu_get_parent_menu(menu);
  483. conf(rootEntry);
  484. }
  485. }
  486. for (child = menu->list; child; child = child->next)
  487. check_conf(child);
  488. }
  489. int main(int ac, char **av)
  490. {
  491. int i = 1;
  492. const char *name;
  493. struct stat tmpstat;
  494. if (ac > i && av[i][0] == '-') {
  495. switch (av[i++][1]) {
  496. case 'o':
  497. input_mode = ask_new;
  498. break;
  499. case 's':
  500. input_mode = ask_silent;
  501. valid_stdin = isatty(0); //bbox: && isatty(1) && isatty(2);
  502. break;
  503. case 'd':
  504. input_mode = set_default;
  505. break;
  506. case 'D':
  507. input_mode = set_default;
  508. defconfig_file = av[i++];
  509. if (!defconfig_file) {
  510. printf(_("%s: No default config file specified\n"),
  511. av[0]);
  512. exit(1);
  513. }
  514. break;
  515. case 'n':
  516. input_mode = set_no;
  517. break;
  518. case 'm':
  519. input_mode = set_mod;
  520. break;
  521. case 'y':
  522. input_mode = set_yes;
  523. break;
  524. case 'r':
  525. input_mode = set_random;
  526. srandom(time(NULL));
  527. break;
  528. case 'h':
  529. case '?':
  530. fprintf(stderr, "See README for usage info\n");
  531. exit(0);
  532. }
  533. }
  534. name = av[i];
  535. if (!name) {
  536. printf(_("%s: Kconfig file missing\n"), av[0]);
  537. }
  538. conf_parse(name);
  539. //zconfdump(stdout);
  540. switch (input_mode) {
  541. case set_default:
  542. if (!defconfig_file)
  543. defconfig_file = conf_get_default_confname();
  544. if (conf_read(defconfig_file)) {
  545. printf("***\n"
  546. "*** Can't find default configuration \"%s\"!\n"
  547. "***\n", defconfig_file);
  548. exit(1);
  549. }
  550. break;
  551. case ask_silent:
  552. if (stat(".config", &tmpstat)) {
  553. printf(_("***\n"
  554. "*** You have not yet configured busybox!\n"
  555. "***\n"
  556. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  557. "*** \"make menuconfig\" or \"make defconfig\").\n"
  558. "***\n"));
  559. exit(1);
  560. }
  561. case ask_all:
  562. case ask_new:
  563. conf_read(NULL);
  564. break;
  565. case set_no:
  566. case set_mod:
  567. case set_yes:
  568. case set_random:
  569. name = getenv("KCONFIG_ALLCONFIG");
  570. if (name && !stat(name, &tmpstat)) {
  571. conf_read_simple(name);
  572. break;
  573. }
  574. switch (input_mode) {
  575. case set_no: name = "allno.config"; break;
  576. case set_mod: name = "allmod.config"; break;
  577. case set_yes: name = "allyes.config"; break;
  578. case set_random: name = "allrandom.config"; break;
  579. default: break;
  580. }
  581. if (!stat(name, &tmpstat))
  582. conf_read_simple(name);
  583. else if (!stat("all.config", &tmpstat))
  584. conf_read_simple("all.config");
  585. break;
  586. default:
  587. break;
  588. }
  589. if (input_mode != ask_silent) {
  590. rootEntry = &rootmenu;
  591. conf(&rootmenu);
  592. // If autoconf run (allnoconfig and such), run it twice:
  593. // "select ITEM" sets ITEM=y and then parent item
  594. // is reset to "n" later. Second run sets ITEM to "n".
  595. // Example: ADDUSER selects LONG_OPTS.
  596. // allnoconfig must set _both_ to "n".
  597. // Before, LONG_OPTS remained "y".
  598. if (input_mode == set_no
  599. || input_mode == set_mod
  600. || input_mode == set_yes
  601. ) {
  602. rootEntry = &rootmenu;
  603. conf(&rootmenu);
  604. }
  605. if (input_mode == ask_all) {
  606. input_mode = ask_silent;
  607. valid_stdin = 1;
  608. }
  609. }
  610. do {
  611. conf_cnt = 0;
  612. check_conf(&rootmenu);
  613. } while (conf_cnt);
  614. if (conf_write(NULL)) {
  615. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  616. return 1;
  617. }
  618. return 0;
  619. }