conf.c 15 KB

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