conf.c 13 KB

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