conf.c 11 KB

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