conf.c 15 KB

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