conf.c 12 KB

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