conf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. fgets(line, 128, stdin);
  130. return;
  131. case set_default:
  132. printf("%s\n", def);
  133. return;
  134. default:
  135. break;
  136. }
  137. switch (type) {
  138. case S_INT:
  139. case S_HEX:
  140. case S_STRING:
  141. printf("%s\n", def);
  142. return;
  143. default:
  144. ;
  145. }
  146. switch (input_mode) {
  147. case set_yes:
  148. if (sym_tristate_within_range(sym, yes)) {
  149. line[0] = 'y';
  150. line[1] = '\n';
  151. line[2] = 0;
  152. break;
  153. }
  154. case set_mod:
  155. if (type == S_TRISTATE) {
  156. if (sym_tristate_within_range(sym, mod)) {
  157. line[0] = 'm';
  158. line[1] = '\n';
  159. line[2] = 0;
  160. break;
  161. }
  162. } else {
  163. if (sym_tristate_within_range(sym, yes)) {
  164. line[0] = 'y';
  165. line[1] = '\n';
  166. line[2] = 0;
  167. break;
  168. }
  169. }
  170. case set_no:
  171. if (sym_tristate_within_range(sym, no)) {
  172. line[0] = 'n';
  173. line[1] = '\n';
  174. line[2] = 0;
  175. break;
  176. }
  177. case set_random:
  178. do {
  179. val = (tristate)(random() % 3);
  180. } while (!sym_tristate_within_range(sym, val));
  181. switch (val) {
  182. case no: line[0] = 'n'; break;
  183. case mod: line[0] = 'm'; break;
  184. case yes: line[0] = 'y'; break;
  185. }
  186. line[1] = '\n';
  187. line[2] = 0;
  188. break;
  189. default:
  190. break;
  191. }
  192. printf("%s", line);
  193. }
  194. int conf_string(struct menu *menu)
  195. {
  196. struct symbol *sym = menu->sym;
  197. const char *def;
  198. while (1) {
  199. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  200. printf("(%s) ", sym->name);
  201. def = sym_get_string_value(sym);
  202. if (sym_get_string_value(sym))
  203. printf("[%s] ", def);
  204. conf_askvalue(sym, def);
  205. switch (line[0]) {
  206. case '\n':
  207. break;
  208. case '?':
  209. /* print help */
  210. if (line[1] == '\n') {
  211. printf("\n%s\n", menu->sym->help ? menu->sym->help : nohelp_text);
  212. def = NULL;
  213. break;
  214. }
  215. default:
  216. line[strlen(line)-1] = 0;
  217. def = line;
  218. }
  219. if (def && sym_set_string_value(sym, def))
  220. return 0;
  221. }
  222. }
  223. static int conf_sym(struct menu *menu)
  224. {
  225. struct symbol *sym = menu->sym;
  226. tristate oldval, newval;
  227. const char *help;
  228. while (1) {
  229. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  230. if (sym->name)
  231. printf("(%s) ", sym->name);
  232. putchar('[');
  233. oldval = sym_get_tristate_value(sym);
  234. switch (oldval) {
  235. case no:
  236. putchar('N');
  237. break;
  238. case mod:
  239. putchar('M');
  240. break;
  241. case yes:
  242. putchar('Y');
  243. break;
  244. }
  245. if (oldval != no && sym_tristate_within_range(sym, no))
  246. printf("/n");
  247. if (oldval != mod && sym_tristate_within_range(sym, mod))
  248. printf("/m");
  249. if (oldval != yes && sym_tristate_within_range(sym, yes))
  250. printf("/y");
  251. if (sym->help)
  252. printf("/?");
  253. printf("] ");
  254. conf_askvalue(sym, sym_get_string_value(sym));
  255. strip(line);
  256. switch (line[0]) {
  257. case 'n':
  258. case 'N':
  259. newval = no;
  260. if (!line[1] || !strcmp(&line[1], "o"))
  261. break;
  262. continue;
  263. case 'm':
  264. case 'M':
  265. newval = mod;
  266. if (!line[1])
  267. break;
  268. continue;
  269. case 'y':
  270. case 'Y':
  271. newval = yes;
  272. if (!line[1] || !strcmp(&line[1], "es"))
  273. break;
  274. continue;
  275. case 0:
  276. newval = oldval;
  277. break;
  278. case '?':
  279. goto help;
  280. default:
  281. continue;
  282. }
  283. if (sym_set_tristate_value(sym, newval))
  284. return 0;
  285. help:
  286. help = nohelp_text;
  287. if (sym->help)
  288. help = sym->help;
  289. printf("\n%s\n", help);
  290. }
  291. }
  292. static int conf_choice(struct menu *menu)
  293. {
  294. struct symbol *sym, *def_sym;
  295. struct menu *child;
  296. bool is_new;
  297. sym = menu->sym;
  298. is_new = !sym_has_value(sym);
  299. if (sym_is_changable(sym)) {
  300. conf_sym(menu);
  301. sym_calc_value(sym);
  302. switch (sym_get_tristate_value(sym)) {
  303. case no:
  304. return 1;
  305. case mod:
  306. return 0;
  307. case yes:
  308. break;
  309. }
  310. } else {
  311. switch (sym_get_tristate_value(sym)) {
  312. case no:
  313. return 1;
  314. case mod:
  315. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  316. return 0;
  317. case yes:
  318. break;
  319. }
  320. }
  321. while (1) {
  322. int cnt, def;
  323. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  324. def_sym = sym_get_choice_value(sym);
  325. cnt = def = 0;
  326. line[0] = 0;
  327. for (child = menu->list; child; child = child->next) {
  328. if (!menu_is_visible(child))
  329. continue;
  330. if (!child->sym) {
  331. printf("%*c %s\n", indent, '*', menu_get_prompt(child));
  332. continue;
  333. }
  334. cnt++;
  335. if (child->sym == def_sym) {
  336. def = cnt;
  337. printf("%*c", indent, '>');
  338. } else
  339. printf("%*c", indent, ' ');
  340. printf(" %d. %s", cnt, menu_get_prompt(child));
  341. if (child->sym->name)
  342. printf(" (%s)", child->sym->name);
  343. if (!sym_has_value(child->sym))
  344. printf(" (NEW)");
  345. printf("\n");
  346. }
  347. printf("%*schoice", indent - 1, "");
  348. if (cnt == 1) {
  349. printf("[1]: 1\n");
  350. goto conf_childs;
  351. }
  352. printf("[1-%d", cnt);
  353. if (sym->help)
  354. printf("?");
  355. printf("]: ");
  356. switch (input_mode) {
  357. case ask_new:
  358. case ask_silent:
  359. if (!is_new) {
  360. cnt = def;
  361. printf("%d\n", cnt);
  362. break;
  363. }
  364. check_stdin();
  365. case ask_all:
  366. fflush(stdout);
  367. fgets(line, 128, stdin);
  368. strip(line);
  369. if (line[0] == '?') {
  370. printf("\n%s\n", menu->sym->help ?
  371. menu->sym->help : nohelp_text);
  372. continue;
  373. }
  374. if (!line[0])
  375. cnt = def;
  376. else if (isdigit(line[0]))
  377. cnt = atoi(line);
  378. else
  379. continue;
  380. break;
  381. case set_random:
  382. def = (random() % cnt) + 1;
  383. case set_default:
  384. case set_yes:
  385. case set_mod:
  386. case set_no:
  387. cnt = def;
  388. printf("%d\n", cnt);
  389. break;
  390. }
  391. conf_childs:
  392. for (child = menu->list; child; child = child->next) {
  393. if (!child->sym || !menu_is_visible(child))
  394. continue;
  395. if (!--cnt)
  396. break;
  397. }
  398. if (!child)
  399. continue;
  400. if (strlen(line) > 0 && line[strlen(line) - 1] == '?') {
  401. printf("\n%s\n", child->sym->help ?
  402. child->sym->help : nohelp_text);
  403. continue;
  404. }
  405. sym_set_choice_value(sym, child->sym);
  406. if (child->list) {
  407. indent += 2;
  408. conf(child->list);
  409. indent -= 2;
  410. }
  411. return 1;
  412. }
  413. }
  414. static void conf(struct menu *menu)
  415. {
  416. struct symbol *sym;
  417. struct property *prop;
  418. struct menu *child;
  419. if (!menu_is_visible(menu))
  420. return;
  421. sym = menu->sym;
  422. prop = menu->prompt;
  423. if (prop) {
  424. const char *prompt;
  425. switch (prop->type) {
  426. case P_MENU:
  427. if (input_mode == ask_silent && rootEntry != menu) {
  428. check_conf(menu);
  429. return;
  430. }
  431. case P_COMMENT:
  432. prompt = menu_get_prompt(menu);
  433. if (prompt)
  434. printf("%*c\n%*c %s\n%*c\n",
  435. indent, '*',
  436. indent, '*', prompt,
  437. indent, '*');
  438. default:
  439. ;
  440. }
  441. }
  442. if (!sym)
  443. goto conf_childs;
  444. if (sym_is_choice(sym)) {
  445. conf_choice(menu);
  446. if (sym->curr.tri != mod)
  447. return;
  448. goto conf_childs;
  449. }
  450. switch (sym->type) {
  451. case S_INT:
  452. case S_HEX:
  453. case S_STRING:
  454. conf_string(menu);
  455. break;
  456. default:
  457. conf_sym(menu);
  458. break;
  459. }
  460. conf_childs:
  461. if (sym)
  462. indent += 2;
  463. for (child = menu->list; child; child = child->next)
  464. conf(child);
  465. if (sym)
  466. indent -= 2;
  467. }
  468. static void check_conf(struct menu *menu)
  469. {
  470. struct symbol *sym;
  471. struct menu *child;
  472. if (!menu_is_visible(menu))
  473. return;
  474. sym = menu->sym;
  475. if (sym && !sym_has_value(sym)) {
  476. if (sym_is_changable(sym) ||
  477. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
  478. if (!conf_cnt++)
  479. printf(_("*\n* Restart config...\n*\n"));
  480. rootEntry = menu_get_parent_menu(menu);
  481. conf(rootEntry);
  482. }
  483. }
  484. for (child = menu->list; child; child = child->next)
  485. check_conf(child);
  486. }
  487. int main(int ac, char **av)
  488. {
  489. int i = 1;
  490. const char *name;
  491. struct stat tmpstat;
  492. if (ac > i && av[i][0] == '-') {
  493. switch (av[i++][1]) {
  494. case 'o':
  495. input_mode = ask_new;
  496. break;
  497. case 's':
  498. input_mode = ask_silent;
  499. valid_stdin = isatty(0); //bbox: && isatty(1) && isatty(2);
  500. break;
  501. case 'd':
  502. input_mode = set_default;
  503. break;
  504. case 'D':
  505. input_mode = set_default;
  506. defconfig_file = av[i++];
  507. if (!defconfig_file) {
  508. printf(_("%s: No default config file specified\n"),
  509. av[0]);
  510. exit(1);
  511. }
  512. break;
  513. case 'n':
  514. input_mode = set_no;
  515. break;
  516. case 'm':
  517. input_mode = set_mod;
  518. break;
  519. case 'y':
  520. input_mode = set_yes;
  521. break;
  522. case 'r':
  523. input_mode = set_random;
  524. srandom(time(NULL));
  525. break;
  526. case 'h':
  527. case '?':
  528. fprintf(stderr, "See README for usage info\n");
  529. exit(0);
  530. }
  531. }
  532. name = av[i];
  533. if (!name) {
  534. printf(_("%s: Kconfig file missing\n"), av[0]);
  535. }
  536. conf_parse(name);
  537. //zconfdump(stdout);
  538. switch (input_mode) {
  539. case set_default:
  540. if (!defconfig_file)
  541. defconfig_file = conf_get_default_confname();
  542. if (conf_read(defconfig_file)) {
  543. printf("***\n"
  544. "*** Can't find default configuration \"%s\"!\n"
  545. "***\n", defconfig_file);
  546. exit(1);
  547. }
  548. break;
  549. case ask_silent:
  550. if (stat(".config", &tmpstat)) {
  551. printf(_("***\n"
  552. "*** You have not yet configured busybox!\n"
  553. "***\n"
  554. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  555. "*** \"make menuconfig\" or \"make defconfig\").\n"
  556. "***\n"));
  557. exit(1);
  558. }
  559. case ask_all:
  560. case ask_new:
  561. conf_read(NULL);
  562. break;
  563. case set_no:
  564. case set_mod:
  565. case set_yes:
  566. case set_random:
  567. name = getenv("KCONFIG_ALLCONFIG");
  568. if (name && !stat(name, &tmpstat)) {
  569. conf_read_simple(name);
  570. break;
  571. }
  572. switch (input_mode) {
  573. case set_no: name = "allno.config"; break;
  574. case set_mod: name = "allmod.config"; break;
  575. case set_yes: name = "allyes.config"; break;
  576. case set_random: name = "allrandom.config"; break;
  577. default: break;
  578. }
  579. if (!stat(name, &tmpstat))
  580. conf_read_simple(name);
  581. else if (!stat("all.config", &tmpstat))
  582. conf_read_simple("all.config");
  583. break;
  584. default:
  585. break;
  586. }
  587. if (input_mode != ask_silent) {
  588. rootEntry = &rootmenu;
  589. conf(&rootmenu);
  590. // If autoconf run (allnoconfig and such), run it twice:
  591. // "select ITEM" sets ITEM=y and then parent item
  592. // is reset to "n" later. Second run sets ITEM to "n".
  593. // Example: ADDUSER selects LONG_OPTS.
  594. // allnoconfig must set _both_ to "n".
  595. // Before, LONG_OPTS remained "y".
  596. if (input_mode == set_no
  597. || input_mode == set_mod
  598. || input_mode == set_yes
  599. ) {
  600. rootEntry = &rootmenu;
  601. conf(&rootmenu);
  602. }
  603. if (input_mode == ask_all) {
  604. input_mode = ask_silent;
  605. valid_stdin = 1;
  606. }
  607. }
  608. do {
  609. conf_cnt = 0;
  610. check_conf(&rootmenu);
  611. } while (conf_cnt);
  612. if (conf_write(NULL)) {
  613. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  614. return 1;
  615. }
  616. return 0;
  617. }