conf.c 15 KB

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