conf.c 11 KB

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