conf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. if (sync_kconfig) {
  519. name = conf_get_configname();
  520. if (stat(name, &tmpstat)) {
  521. fprintf(stderr, _("***\n"
  522. "*** Configuration file \"%s\" not found!\n"
  523. "***\n"
  524. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  525. "*** \"make menuconfig\" or \"make xconfig\").\n"
  526. "***\n"), name);
  527. exit(1);
  528. }
  529. }
  530. switch (input_mode) {
  531. case defconfig:
  532. if (!defconfig_file)
  533. defconfig_file = conf_get_default_confname();
  534. if (conf_read(defconfig_file)) {
  535. fprintf(stderr,
  536. _("***\n"
  537. "*** Can't find default configuration \"%s\"!\n"
  538. "***\n"),
  539. defconfig_file);
  540. exit(1);
  541. }
  542. break;
  543. case savedefconfig:
  544. case syncconfig:
  545. case oldaskconfig:
  546. case oldconfig:
  547. case listnewconfig:
  548. case olddefconfig:
  549. conf_read(NULL);
  550. break;
  551. case allnoconfig:
  552. case allyesconfig:
  553. case allmodconfig:
  554. case alldefconfig:
  555. case randconfig:
  556. name = getenv("KCONFIG_ALLCONFIG");
  557. if (!name)
  558. break;
  559. if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
  560. if (conf_read_simple(name, S_DEF_USER)) {
  561. fprintf(stderr,
  562. _("*** Can't read seed configuration \"%s\"!\n"),
  563. name);
  564. exit(1);
  565. }
  566. break;
  567. }
  568. switch (input_mode) {
  569. case allnoconfig: name = "allno.config"; break;
  570. case allyesconfig: name = "allyes.config"; break;
  571. case allmodconfig: name = "allmod.config"; break;
  572. case alldefconfig: name = "alldef.config"; break;
  573. case randconfig: name = "allrandom.config"; break;
  574. default: break;
  575. }
  576. if (conf_read_simple(name, S_DEF_USER) &&
  577. conf_read_simple("all.config", S_DEF_USER)) {
  578. fprintf(stderr,
  579. _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
  580. name);
  581. exit(1);
  582. }
  583. break;
  584. default:
  585. break;
  586. }
  587. if (sync_kconfig) {
  588. if (conf_get_changed()) {
  589. name = getenv("KCONFIG_NOSILENTUPDATE");
  590. if (name && *name) {
  591. fprintf(stderr,
  592. _("\n*** The configuration requires explicit update.\n\n"));
  593. return 1;
  594. }
  595. }
  596. }
  597. switch (input_mode) {
  598. case allnoconfig:
  599. conf_set_all_new_symbols(def_no);
  600. break;
  601. case allyesconfig:
  602. conf_set_all_new_symbols(def_yes);
  603. break;
  604. case allmodconfig:
  605. conf_set_all_new_symbols(def_mod);
  606. break;
  607. case alldefconfig:
  608. conf_set_all_new_symbols(def_default);
  609. break;
  610. case randconfig:
  611. /* Really nothing to do in this loop */
  612. while (conf_set_all_new_symbols(def_random)) ;
  613. break;
  614. case defconfig:
  615. conf_set_all_new_symbols(def_default);
  616. break;
  617. case savedefconfig:
  618. break;
  619. case oldaskconfig:
  620. rootEntry = &rootmenu;
  621. conf(&rootmenu);
  622. input_mode = oldconfig;
  623. /* fall through */
  624. case oldconfig:
  625. case listnewconfig:
  626. case syncconfig:
  627. /* Update until a loop caused no more changes */
  628. do {
  629. conf_cnt = 0;
  630. check_conf(&rootmenu);
  631. } while (conf_cnt);
  632. break;
  633. case olddefconfig:
  634. default:
  635. break;
  636. }
  637. if (sync_kconfig) {
  638. /* syncconfig is used during the build so we shall update autoconf.
  639. * All other commands are only used to generate a config.
  640. */
  641. if (conf_get_changed() && conf_write(NULL)) {
  642. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  643. exit(1);
  644. }
  645. if (conf_write_autoconf()) {
  646. fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
  647. return 1;
  648. }
  649. } else if (input_mode == savedefconfig) {
  650. if (conf_write_defconfig(defconfig_file)) {
  651. fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
  652. defconfig_file);
  653. return 1;
  654. }
  655. } else if (input_mode != listnewconfig) {
  656. if (conf_write(NULL)) {
  657. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  658. exit(1);
  659. }
  660. }
  661. return 0;
  662. }