conf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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 <limits.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <unistd.h>
  13. #include <getopt.h>
  14. #include <sys/stat.h>
  15. #include <sys/time.h>
  16. #include <errno.h>
  17. #include "lkc.h"
  18. static void conf(struct menu *menu);
  19. static void check_conf(struct menu *menu);
  20. enum input_mode {
  21. oldaskconfig,
  22. silentoldconfig,
  23. oldconfig,
  24. allnoconfig,
  25. allyesconfig,
  26. allmodconfig,
  27. alldefconfig,
  28. randconfig,
  29. defconfig,
  30. savedefconfig,
  31. listnewconfig,
  32. olddefconfig,
  33. };
  34. static enum input_mode input_mode = oldaskconfig;
  35. static int indent = 1;
  36. static int tty_stdio;
  37. static int valid_stdin = 1;
  38. static int sync_kconfig;
  39. static int conf_cnt;
  40. static char line[PATH_MAX];
  41. static struct menu *rootEntry;
  42. static void print_help(struct menu *menu)
  43. {
  44. struct gstr help = str_new();
  45. menu_get_ext_help(menu, &help);
  46. printf("\n%s\n", str_get(&help));
  47. str_free(&help);
  48. }
  49. static void strip(char *str)
  50. {
  51. char *p = str;
  52. int l;
  53. while ((isspace(*p)))
  54. p++;
  55. l = strlen(p);
  56. if (p != str)
  57. memmove(str, p, l + 1);
  58. if (!l)
  59. return;
  60. p = str + l - 1;
  61. while ((isspace(*p)))
  62. *p-- = 0;
  63. }
  64. static void check_stdin(void)
  65. {
  66. if (!valid_stdin) {
  67. printf(_("aborted!\n\n"));
  68. printf(_("Console input/output is redirected. "));
  69. printf(_("Run 'make oldconfig' to update configuration.\n\n"));
  70. exit(1);
  71. }
  72. }
  73. /* Helper function to facilitate fgets() by Jean Sacren. */
  74. static void xfgets(char *str, int size, FILE *in)
  75. {
  76. if (!fgets(str, size, in))
  77. fprintf(stderr, "\nError in reading or end of file.\n");
  78. }
  79. static int conf_askvalue(struct symbol *sym, const char *def)
  80. {
  81. enum symbol_type type = sym_get_type(sym);
  82. if (!sym_has_value(sym))
  83. printf(_("(NEW) "));
  84. line[0] = '\n';
  85. line[1] = 0;
  86. if (!sym_is_changable(sym)) {
  87. printf("%s\n", def);
  88. line[0] = '\n';
  89. line[1] = 0;
  90. return 0;
  91. }
  92. switch (input_mode) {
  93. case oldconfig:
  94. case silentoldconfig:
  95. if (sym_has_value(sym)) {
  96. printf("%s\n", def);
  97. return 0;
  98. }
  99. check_stdin();
  100. /* fall through */
  101. case oldaskconfig:
  102. fflush(stdout);
  103. xfgets(line, sizeof(line), stdin);
  104. if (!tty_stdio)
  105. printf("\n");
  106. return 1;
  107. default:
  108. break;
  109. }
  110. switch (type) {
  111. case S_INT:
  112. case S_HEX:
  113. case S_STRING:
  114. printf("%s\n", def);
  115. return 1;
  116. default:
  117. ;
  118. }
  119. printf("%s", line);
  120. return 1;
  121. }
  122. static int conf_string(struct menu *menu)
  123. {
  124. struct symbol *sym = menu->sym;
  125. const char *def;
  126. while (1) {
  127. printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
  128. printf("(%s) ", sym->name);
  129. def = sym_get_string_value(sym);
  130. if (sym_get_string_value(sym))
  131. printf("[%s] ", def);
  132. if (!conf_askvalue(sym, def))
  133. return 0;
  134. switch (line[0]) {
  135. case '\n':
  136. break;
  137. case '?':
  138. /* print help */
  139. if (line[1] == '\n') {
  140. print_help(menu);
  141. def = NULL;
  142. break;
  143. }
  144. /* fall through */
  145. default:
  146. line[strlen(line)-1] = 0;
  147. def = line;
  148. }
  149. if (def && sym_set_string_value(sym, def))
  150. return 0;
  151. }
  152. }
  153. static int conf_sym(struct menu *menu)
  154. {
  155. struct symbol *sym = menu->sym;
  156. tristate oldval, newval;
  157. while (1) {
  158. printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
  159. if (sym->name)
  160. printf("(%s) ", sym->name);
  161. putchar('[');
  162. oldval = sym_get_tristate_value(sym);
  163. switch (oldval) {
  164. case no:
  165. putchar('N');
  166. break;
  167. case mod:
  168. putchar('M');
  169. break;
  170. case yes:
  171. putchar('Y');
  172. break;
  173. }
  174. if (oldval != no && sym_tristate_within_range(sym, no))
  175. printf("/n");
  176. if (oldval != mod && sym_tristate_within_range(sym, mod))
  177. printf("/m");
  178. if (oldval != yes && sym_tristate_within_range(sym, yes))
  179. printf("/y");
  180. if (menu_has_help(menu))
  181. printf("/?");
  182. printf("] ");
  183. if (!conf_askvalue(sym, sym_get_string_value(sym)))
  184. return 0;
  185. strip(line);
  186. switch (line[0]) {
  187. case 'n':
  188. case 'N':
  189. newval = no;
  190. if (!line[1] || !strcmp(&line[1], "o"))
  191. break;
  192. continue;
  193. case 'm':
  194. case 'M':
  195. newval = mod;
  196. if (!line[1])
  197. break;
  198. continue;
  199. case 'y':
  200. case 'Y':
  201. newval = yes;
  202. if (!line[1] || !strcmp(&line[1], "es"))
  203. break;
  204. continue;
  205. case 0:
  206. newval = oldval;
  207. break;
  208. case '?':
  209. goto help;
  210. default:
  211. continue;
  212. }
  213. if (sym_set_tristate_value(sym, newval))
  214. return 0;
  215. help:
  216. print_help(menu);
  217. }
  218. }
  219. static int conf_choice(struct menu *menu)
  220. {
  221. struct symbol *sym, *def_sym;
  222. struct menu *child;
  223. bool is_new;
  224. sym = menu->sym;
  225. is_new = !sym_has_value(sym);
  226. if (sym_is_changable(sym)) {
  227. conf_sym(menu);
  228. sym_calc_value(sym);
  229. switch (sym_get_tristate_value(sym)) {
  230. case no:
  231. return 1;
  232. case mod:
  233. return 0;
  234. case yes:
  235. break;
  236. }
  237. } else {
  238. switch (sym_get_tristate_value(sym)) {
  239. case no:
  240. return 1;
  241. case mod:
  242. printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
  243. return 0;
  244. case yes:
  245. break;
  246. }
  247. }
  248. while (1) {
  249. int cnt, def;
  250. printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
  251. def_sym = sym_get_choice_value(sym);
  252. cnt = def = 0;
  253. line[0] = 0;
  254. for (child = menu->list; child; child = child->next) {
  255. if (!menu_is_visible(child))
  256. continue;
  257. if (!child->sym) {
  258. printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
  259. continue;
  260. }
  261. cnt++;
  262. if (child->sym == def_sym) {
  263. def = cnt;
  264. printf("%*c", indent, '>');
  265. } else
  266. printf("%*c", indent, ' ');
  267. printf(" %d. %s", cnt, _(menu_get_prompt(child)));
  268. if (child->sym->name)
  269. printf(" (%s)", child->sym->name);
  270. if (!sym_has_value(child->sym))
  271. printf(_(" (NEW)"));
  272. printf("\n");
  273. }
  274. printf(_("%*schoice"), indent - 1, "");
  275. if (cnt == 1) {
  276. printf("[1]: 1\n");
  277. goto conf_childs;
  278. }
  279. printf("[1-%d", cnt);
  280. if (menu_has_help(menu))
  281. printf("?");
  282. printf("]: ");
  283. switch (input_mode) {
  284. case oldconfig:
  285. case silentoldconfig:
  286. if (!is_new) {
  287. cnt = def;
  288. printf("%d\n", cnt);
  289. break;
  290. }
  291. check_stdin();
  292. /* fall through */
  293. case oldaskconfig:
  294. fflush(stdout);
  295. xfgets(line, sizeof(line), stdin);
  296. strip(line);
  297. if (line[0] == '?') {
  298. print_help(menu);
  299. continue;
  300. }
  301. if (!line[0])
  302. cnt = def;
  303. else if (isdigit(line[0]))
  304. cnt = atoi(line);
  305. else
  306. continue;
  307. break;
  308. default:
  309. break;
  310. }
  311. conf_childs:
  312. for (child = menu->list; child; child = child->next) {
  313. if (!child->sym || !menu_is_visible(child))
  314. continue;
  315. if (!--cnt)
  316. break;
  317. }
  318. if (!child)
  319. continue;
  320. if (line[0] && line[strlen(line) - 1] == '?') {
  321. print_help(child);
  322. continue;
  323. }
  324. sym_set_choice_value(sym, child->sym);
  325. for (child = child->list; child; child = child->next) {
  326. indent += 2;
  327. conf(child);
  328. indent -= 2;
  329. }
  330. return 1;
  331. }
  332. }
  333. static void conf(struct menu *menu)
  334. {
  335. struct symbol *sym;
  336. struct property *prop;
  337. struct menu *child;
  338. if (!menu_is_visible(menu))
  339. return;
  340. sym = menu->sym;
  341. prop = menu->prompt;
  342. if (prop) {
  343. const char *prompt;
  344. switch (prop->type) {
  345. case P_MENU:
  346. if ((input_mode == silentoldconfig ||
  347. input_mode == listnewconfig ||
  348. input_mode == olddefconfig) &&
  349. rootEntry != menu) {
  350. check_conf(menu);
  351. return;
  352. }
  353. /* fall through */
  354. case P_COMMENT:
  355. prompt = menu_get_prompt(menu);
  356. if (prompt)
  357. printf("%*c\n%*c %s\n%*c\n",
  358. indent, '*',
  359. indent, '*', _(prompt),
  360. indent, '*');
  361. default:
  362. ;
  363. }
  364. }
  365. if (!sym)
  366. goto conf_childs;
  367. if (sym_is_choice(sym)) {
  368. conf_choice(menu);
  369. if (sym->curr.tri != mod)
  370. return;
  371. goto conf_childs;
  372. }
  373. switch (sym->type) {
  374. case S_INT:
  375. case S_HEX:
  376. case S_STRING:
  377. conf_string(menu);
  378. break;
  379. default:
  380. conf_sym(menu);
  381. break;
  382. }
  383. conf_childs:
  384. if (sym)
  385. indent += 2;
  386. for (child = menu->list; child; child = child->next)
  387. conf(child);
  388. if (sym)
  389. indent -= 2;
  390. }
  391. static void check_conf(struct menu *menu)
  392. {
  393. struct symbol *sym;
  394. struct menu *child;
  395. if (!menu_is_visible(menu))
  396. return;
  397. sym = menu->sym;
  398. if (sym && !sym_has_value(sym)) {
  399. if (sym_is_changable(sym) ||
  400. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
  401. if (input_mode == listnewconfig) {
  402. if (sym->name && !sym_is_choice_value(sym)) {
  403. printf("%s%s\n", CONFIG_, sym->name);
  404. }
  405. } else if (input_mode != olddefconfig) {
  406. if (!conf_cnt++)
  407. printf(_("*\n* Restart config...\n*\n"));
  408. rootEntry = menu_get_parent_menu(menu);
  409. conf(rootEntry);
  410. }
  411. }
  412. }
  413. for (child = menu->list; child; child = child->next)
  414. check_conf(child);
  415. }
  416. static struct option long_opts[] = {
  417. {"oldaskconfig", no_argument, NULL, oldaskconfig},
  418. {"oldconfig", no_argument, NULL, oldconfig},
  419. {"silentoldconfig", no_argument, NULL, silentoldconfig},
  420. {"defconfig", optional_argument, NULL, defconfig},
  421. {"savedefconfig", required_argument, NULL, savedefconfig},
  422. {"allnoconfig", no_argument, NULL, allnoconfig},
  423. {"allyesconfig", no_argument, NULL, allyesconfig},
  424. {"allmodconfig", no_argument, NULL, allmodconfig},
  425. {"alldefconfig", no_argument, NULL, alldefconfig},
  426. {"randconfig", no_argument, NULL, randconfig},
  427. {"listnewconfig", no_argument, NULL, listnewconfig},
  428. {"olddefconfig", no_argument, NULL, olddefconfig},
  429. /*
  430. * oldnoconfig is an alias of olddefconfig, because people already
  431. * are dependent on its behavior(sets new symbols to their default
  432. * value but not 'n') with the counter-intuitive name.
  433. */
  434. {"oldnoconfig", no_argument, NULL, olddefconfig},
  435. {NULL, 0, NULL, 0}
  436. };
  437. static void conf_usage(const char *progname)
  438. {
  439. printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
  440. printf("[option] is _one_ of the following:\n");
  441. printf(" --listnewconfig List new options\n");
  442. printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
  443. printf(" --oldconfig Update a configuration using a provided .config as base\n");
  444. printf(" --silentoldconfig Similar to oldconfig but generates configuration in\n"
  445. " include/{generated/,config/} (oldconfig used to be more verbose)\n");
  446. printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
  447. printf(" --oldnoconfig An alias of olddefconfig\n");
  448. printf(" --defconfig <file> New config with default defined in <file>\n");
  449. printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
  450. printf(" --allnoconfig New config where all options are answered with no\n");
  451. printf(" --allyesconfig New config where all options are answered with yes\n");
  452. printf(" --allmodconfig New config where all options are answered with mod\n");
  453. printf(" --alldefconfig New config with all symbols set to default\n");
  454. printf(" --randconfig New config with random answer to all options\n");
  455. }
  456. int main(int ac, char **av)
  457. {
  458. const char *progname = av[0];
  459. int opt;
  460. const char *name, *defconfig_file = NULL /* gcc uninit */;
  461. struct stat tmpstat;
  462. setlocale(LC_ALL, "");
  463. bindtextdomain(PACKAGE, LOCALEDIR);
  464. textdomain(PACKAGE);
  465. tty_stdio = isatty(0) && isatty(1) && isatty(2);
  466. while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
  467. if (opt == 's') {
  468. conf_set_message_callback(NULL);
  469. continue;
  470. }
  471. input_mode = (enum input_mode)opt;
  472. switch (opt) {
  473. case silentoldconfig:
  474. sync_kconfig = 1;
  475. break;
  476. case defconfig:
  477. case savedefconfig:
  478. defconfig_file = optarg;
  479. break;
  480. case randconfig:
  481. {
  482. struct timeval now;
  483. unsigned int seed;
  484. char *seed_env;
  485. /*
  486. * Use microseconds derived seed,
  487. * compensate for systems where it may be zero
  488. */
  489. gettimeofday(&now, NULL);
  490. seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
  491. seed_env = getenv("KCONFIG_SEED");
  492. if( seed_env && *seed_env ) {
  493. char *endp;
  494. int tmp = (int)strtol(seed_env, &endp, 0);
  495. if (*endp == '\0') {
  496. seed = tmp;
  497. }
  498. }
  499. fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
  500. srand(seed);
  501. break;
  502. }
  503. case oldaskconfig:
  504. case oldconfig:
  505. case allnoconfig:
  506. case allyesconfig:
  507. case allmodconfig:
  508. case alldefconfig:
  509. case listnewconfig:
  510. case olddefconfig:
  511. break;
  512. case '?':
  513. conf_usage(progname);
  514. exit(1);
  515. break;
  516. }
  517. }
  518. if (ac == optind) {
  519. printf(_("%s: Kconfig file missing\n"), av[0]);
  520. conf_usage(progname);
  521. exit(1);
  522. }
  523. name = av[optind];
  524. conf_parse(name);
  525. //zconfdump(stdout);
  526. if (sync_kconfig) {
  527. name = conf_get_configname();
  528. if (stat(name, &tmpstat)) {
  529. fprintf(stderr, _("***\n"
  530. "*** Configuration file \"%s\" not found!\n"
  531. "***\n"
  532. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  533. "*** \"make menuconfig\" or \"make xconfig\").\n"
  534. "***\n"), name);
  535. exit(1);
  536. }
  537. }
  538. switch (input_mode) {
  539. case defconfig:
  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 savedefconfig:
  550. case silentoldconfig:
  551. case oldaskconfig:
  552. case oldconfig:
  553. case listnewconfig:
  554. case olddefconfig:
  555. conf_read(NULL);
  556. break;
  557. case allnoconfig:
  558. case allyesconfig:
  559. case allmodconfig:
  560. case alldefconfig:
  561. case randconfig:
  562. name = getenv("KCONFIG_ALLCONFIG");
  563. if (!name)
  564. break;
  565. if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
  566. if (conf_read_simple(name, S_DEF_USER)) {
  567. fprintf(stderr,
  568. _("*** Can't read seed configuration \"%s\"!\n"),
  569. name);
  570. exit(1);
  571. }
  572. break;
  573. }
  574. switch (input_mode) {
  575. case allnoconfig: name = "allno.config"; break;
  576. case allyesconfig: name = "allyes.config"; break;
  577. case allmodconfig: name = "allmod.config"; break;
  578. case alldefconfig: name = "alldef.config"; break;
  579. case randconfig: name = "allrandom.config"; break;
  580. default: break;
  581. }
  582. if (conf_read_simple(name, S_DEF_USER) &&
  583. conf_read_simple("all.config", S_DEF_USER)) {
  584. fprintf(stderr,
  585. _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
  586. name);
  587. exit(1);
  588. }
  589. break;
  590. default:
  591. break;
  592. }
  593. if (sync_kconfig) {
  594. if (conf_get_changed()) {
  595. name = getenv("KCONFIG_NOSILENTUPDATE");
  596. if (name && *name) {
  597. fprintf(stderr,
  598. _("\n*** The configuration requires explicit update.\n\n"));
  599. return 1;
  600. }
  601. }
  602. valid_stdin = tty_stdio;
  603. }
  604. switch (input_mode) {
  605. case allnoconfig:
  606. conf_set_all_new_symbols(def_no);
  607. break;
  608. case allyesconfig:
  609. conf_set_all_new_symbols(def_yes);
  610. break;
  611. case allmodconfig:
  612. conf_set_all_new_symbols(def_mod);
  613. break;
  614. case alldefconfig:
  615. conf_set_all_new_symbols(def_default);
  616. break;
  617. case randconfig:
  618. /* Really nothing to do in this loop */
  619. while (conf_set_all_new_symbols(def_random)) ;
  620. break;
  621. case defconfig:
  622. conf_set_all_new_symbols(def_default);
  623. break;
  624. case savedefconfig:
  625. break;
  626. case oldaskconfig:
  627. rootEntry = &rootmenu;
  628. conf(&rootmenu);
  629. input_mode = silentoldconfig;
  630. /* fall through */
  631. case oldconfig:
  632. case listnewconfig:
  633. case olddefconfig:
  634. case silentoldconfig:
  635. /* Update until a loop caused no more changes */
  636. do {
  637. conf_cnt = 0;
  638. check_conf(&rootmenu);
  639. } while (conf_cnt &&
  640. (input_mode != listnewconfig &&
  641. input_mode != olddefconfig));
  642. break;
  643. }
  644. if (sync_kconfig) {
  645. /* silentoldconfig is used during the build so we shall update autoconf.
  646. * All other commands are only used to generate a config.
  647. */
  648. if (conf_get_changed() && conf_write(NULL)) {
  649. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  650. exit(1);
  651. }
  652. if (conf_write_autoconf()) {
  653. fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
  654. return 1;
  655. }
  656. } else if (input_mode == savedefconfig) {
  657. if (conf_write_defconfig(defconfig_file)) {
  658. fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
  659. defconfig_file);
  660. return 1;
  661. }
  662. } else if (input_mode != listnewconfig) {
  663. if (conf_write(NULL)) {
  664. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  665. exit(1);
  666. }
  667. }
  668. return 0;
  669. }