conf.c 16 KB

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