conf.c 16 KB

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