conf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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) {
  387. const char *str;
  388. if (sym->type == S_STRING) {
  389. str = sym_get_string_value(sym);
  390. str = sym_escape_string_value(str);
  391. printf("%s%s=%s\n", CONFIG_, sym->name, str);
  392. free((void *)str);
  393. } else {
  394. str = sym_get_string_value(sym);
  395. printf("%s%s=%s\n", CONFIG_, sym->name, str);
  396. }
  397. }
  398. } else {
  399. if (!conf_cnt++)
  400. printf(_("*\n* Restart config...\n*\n"));
  401. rootEntry = menu_get_parent_menu(menu);
  402. conf(rootEntry);
  403. }
  404. }
  405. }
  406. for (child = menu->list; child; child = child->next)
  407. check_conf(child);
  408. }
  409. static struct option long_opts[] = {
  410. {"oldaskconfig", no_argument, NULL, oldaskconfig},
  411. {"oldconfig", no_argument, NULL, oldconfig},
  412. {"syncconfig", no_argument, NULL, syncconfig},
  413. {"defconfig", optional_argument, NULL, defconfig},
  414. {"savedefconfig", required_argument, NULL, savedefconfig},
  415. {"allnoconfig", no_argument, NULL, allnoconfig},
  416. {"allyesconfig", no_argument, NULL, allyesconfig},
  417. {"allmodconfig", no_argument, NULL, allmodconfig},
  418. {"alldefconfig", no_argument, NULL, alldefconfig},
  419. {"randconfig", no_argument, NULL, randconfig},
  420. {"listnewconfig", no_argument, NULL, listnewconfig},
  421. {"olddefconfig", no_argument, NULL, olddefconfig},
  422. /*
  423. * oldnoconfig is an alias of olddefconfig, because people already
  424. * are dependent on its behavior(sets new symbols to their default
  425. * value but not 'n') with the counter-intuitive name.
  426. */
  427. {"oldnoconfig", no_argument, NULL, olddefconfig},
  428. {NULL, 0, NULL, 0}
  429. };
  430. static void conf_usage(const char *progname)
  431. {
  432. printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
  433. printf("[option] is _one_ of the following:\n");
  434. printf(" --listnewconfig List new options\n");
  435. printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
  436. printf(" --oldconfig Update a configuration using a provided .config as base\n");
  437. printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
  438. " include/{generated/,config/}\n");
  439. printf(" --olddefconfig Same as oldconfig 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);
  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 syncconfig:
  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. fprintf(stderr, _("%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. fprintf(stderr,
  537. _("***\n"
  538. "*** Can't find default configuration \"%s\"!\n"
  539. "***\n"),
  540. defconfig_file);
  541. exit(1);
  542. }
  543. break;
  544. case savedefconfig:
  545. case syncconfig:
  546. case oldaskconfig:
  547. case oldconfig:
  548. case listnewconfig:
  549. case olddefconfig:
  550. conf_read(NULL);
  551. break;
  552. case allnoconfig:
  553. case allyesconfig:
  554. case allmodconfig:
  555. case alldefconfig:
  556. case randconfig:
  557. name = getenv("KCONFIG_ALLCONFIG");
  558. if (!name)
  559. break;
  560. if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
  561. if (conf_read_simple(name, S_DEF_USER)) {
  562. fprintf(stderr,
  563. _("*** Can't read seed configuration \"%s\"!\n"),
  564. name);
  565. exit(1);
  566. }
  567. break;
  568. }
  569. switch (input_mode) {
  570. case allnoconfig: name = "allno.config"; break;
  571. case allyesconfig: name = "allyes.config"; break;
  572. case allmodconfig: name = "allmod.config"; break;
  573. case alldefconfig: name = "alldef.config"; break;
  574. case randconfig: name = "allrandom.config"; break;
  575. default: break;
  576. }
  577. if (conf_read_simple(name, S_DEF_USER) &&
  578. conf_read_simple("all.config", S_DEF_USER)) {
  579. fprintf(stderr,
  580. _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
  581. name);
  582. exit(1);
  583. }
  584. break;
  585. default:
  586. break;
  587. }
  588. if (sync_kconfig) {
  589. if (conf_get_changed()) {
  590. name = getenv("KCONFIG_NOSILENTUPDATE");
  591. if (name && *name) {
  592. fprintf(stderr,
  593. _("\n*** The configuration requires explicit update.\n\n"));
  594. return 1;
  595. }
  596. }
  597. }
  598. switch (input_mode) {
  599. case allnoconfig:
  600. conf_set_all_new_symbols(def_no);
  601. break;
  602. case allyesconfig:
  603. conf_set_all_new_symbols(def_yes);
  604. break;
  605. case allmodconfig:
  606. conf_set_all_new_symbols(def_mod);
  607. break;
  608. case alldefconfig:
  609. conf_set_all_new_symbols(def_default);
  610. break;
  611. case randconfig:
  612. /* Really nothing to do in this loop */
  613. while (conf_set_all_new_symbols(def_random)) ;
  614. break;
  615. case defconfig:
  616. conf_set_all_new_symbols(def_default);
  617. break;
  618. case savedefconfig:
  619. break;
  620. case oldaskconfig:
  621. rootEntry = &rootmenu;
  622. conf(&rootmenu);
  623. input_mode = oldconfig;
  624. /* fall through */
  625. case oldconfig:
  626. case listnewconfig:
  627. case syncconfig:
  628. /* Update until a loop caused no more changes */
  629. do {
  630. conf_cnt = 0;
  631. check_conf(&rootmenu);
  632. } while (conf_cnt);
  633. break;
  634. case olddefconfig:
  635. default:
  636. break;
  637. }
  638. if (sync_kconfig) {
  639. /* syncconfig is used during the build so we shall update autoconf.
  640. * All other commands are only used to generate a config.
  641. */
  642. if (conf_get_changed() && conf_write(NULL)) {
  643. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  644. exit(1);
  645. }
  646. if (conf_write_autoconf()) {
  647. fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
  648. return 1;
  649. }
  650. } else if (input_mode == savedefconfig) {
  651. if (conf_write_defconfig(defconfig_file)) {
  652. fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
  653. defconfig_file);
  654. return 1;
  655. }
  656. } else if (input_mode != listnewconfig) {
  657. if (conf_write(NULL)) {
  658. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  659. exit(1);
  660. }
  661. }
  662. return 0;
  663. }