conf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 <ctype.h>
  6. #include <limits.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 <errno.h>
  16. #include "lkc.h"
  17. static void conf(struct menu *menu);
  18. static void check_conf(struct menu *menu);
  19. enum input_mode {
  20. oldaskconfig,
  21. syncconfig,
  22. oldconfig,
  23. allnoconfig,
  24. allyesconfig,
  25. allmodconfig,
  26. alldefconfig,
  27. randconfig,
  28. defconfig,
  29. savedefconfig,
  30. listnewconfig,
  31. olddefconfig,
  32. };
  33. static enum input_mode input_mode = oldaskconfig;
  34. static int indent = 1;
  35. static int tty_stdio;
  36. static int sync_kconfig;
  37. static int conf_cnt;
  38. static char line[PATH_MAX];
  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. /* Helper function to facilitate fgets() by Jean Sacren. */
  63. static void xfgets(char *str, int size, FILE *in)
  64. {
  65. if (!fgets(str, size, in))
  66. fprintf(stderr, "\nError in reading or end of file.\n");
  67. if (!tty_stdio)
  68. printf("%s", str);
  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 syncconfig:
  86. if (sym_has_value(sym)) {
  87. printf("%s\n", def);
  88. return 0;
  89. }
  90. /* fall through */
  91. case oldaskconfig:
  92. fflush(stdout);
  93. xfgets(line, sizeof(line), stdin);
  94. return 1;
  95. default:
  96. break;
  97. }
  98. switch (type) {
  99. case S_INT:
  100. case S_HEX:
  101. case S_STRING:
  102. printf("%s\n", def);
  103. return 1;
  104. default:
  105. ;
  106. }
  107. printf("%s", line);
  108. return 1;
  109. }
  110. static int conf_string(struct menu *menu)
  111. {
  112. struct symbol *sym = menu->sym;
  113. const char *def;
  114. while (1) {
  115. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  116. printf("(%s) ", sym->name);
  117. def = sym_get_string_value(sym);
  118. if (sym_get_string_value(sym))
  119. printf("[%s] ", def);
  120. if (!conf_askvalue(sym, def))
  121. return 0;
  122. switch (line[0]) {
  123. case '\n':
  124. break;
  125. case '?':
  126. /* print help */
  127. if (line[1] == '\n') {
  128. print_help(menu);
  129. def = NULL;
  130. break;
  131. }
  132. /* fall through */
  133. default:
  134. line[strlen(line)-1] = 0;
  135. def = line;
  136. }
  137. if (def && sym_set_string_value(sym, def))
  138. return 0;
  139. }
  140. }
  141. static int conf_sym(struct menu *menu)
  142. {
  143. struct symbol *sym = menu->sym;
  144. tristate oldval, newval;
  145. while (1) {
  146. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  147. if (sym->name)
  148. printf("(%s) ", sym->name);
  149. putchar('[');
  150. oldval = sym_get_tristate_value(sym);
  151. switch (oldval) {
  152. case no:
  153. putchar('N');
  154. break;
  155. case mod:
  156. putchar('M');
  157. break;
  158. case yes:
  159. putchar('Y');
  160. break;
  161. }
  162. if (oldval != no && sym_tristate_within_range(sym, no))
  163. printf("/n");
  164. if (oldval != mod && sym_tristate_within_range(sym, mod))
  165. printf("/m");
  166. if (oldval != yes && sym_tristate_within_range(sym, yes))
  167. printf("/y");
  168. printf("/?] ");
  169. if (!conf_askvalue(sym, sym_get_string_value(sym)))
  170. return 0;
  171. strip(line);
  172. switch (line[0]) {
  173. case 'n':
  174. case 'N':
  175. newval = no;
  176. if (!line[1] || !strcmp(&line[1], "o"))
  177. break;
  178. continue;
  179. case 'm':
  180. case 'M':
  181. newval = mod;
  182. if (!line[1])
  183. break;
  184. continue;
  185. case 'y':
  186. case 'Y':
  187. newval = yes;
  188. if (!line[1] || !strcmp(&line[1], "es"))
  189. break;
  190. continue;
  191. case 0:
  192. newval = oldval;
  193. break;
  194. case '?':
  195. goto help;
  196. default:
  197. continue;
  198. }
  199. if (sym_set_tristate_value(sym, newval))
  200. return 0;
  201. help:
  202. print_help(menu);
  203. }
  204. }
  205. static int conf_choice(struct menu *menu)
  206. {
  207. struct symbol *sym, *def_sym;
  208. struct menu *child;
  209. bool is_new;
  210. sym = menu->sym;
  211. is_new = !sym_has_value(sym);
  212. if (sym_is_changable(sym)) {
  213. conf_sym(menu);
  214. sym_calc_value(sym);
  215. switch (sym_get_tristate_value(sym)) {
  216. case no:
  217. return 1;
  218. case mod:
  219. return 0;
  220. case yes:
  221. break;
  222. }
  223. } else {
  224. switch (sym_get_tristate_value(sym)) {
  225. case no:
  226. return 1;
  227. case mod:
  228. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  229. return 0;
  230. case yes:
  231. break;
  232. }
  233. }
  234. while (1) {
  235. int cnt, def;
  236. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  237. def_sym = sym_get_choice_value(sym);
  238. cnt = def = 0;
  239. line[0] = 0;
  240. for (child = menu->list; child; child = child->next) {
  241. if (!menu_is_visible(child))
  242. continue;
  243. if (!child->sym) {
  244. printf("%*c %s\n", indent, '*', menu_get_prompt(child));
  245. continue;
  246. }
  247. cnt++;
  248. if (child->sym == def_sym) {
  249. def = cnt;
  250. printf("%*c", indent, '>');
  251. } else
  252. printf("%*c", indent, ' ');
  253. printf(" %d. %s", cnt, menu_get_prompt(child));
  254. if (child->sym->name)
  255. printf(" (%s)", child->sym->name);
  256. if (!sym_has_value(child->sym))
  257. printf(" (NEW)");
  258. printf("\n");
  259. }
  260. printf("%*schoice", indent - 1, "");
  261. if (cnt == 1) {
  262. printf("[1]: 1\n");
  263. goto conf_childs;
  264. }
  265. printf("[1-%d?]: ", cnt);
  266. switch (input_mode) {
  267. case oldconfig:
  268. case syncconfig:
  269. if (!is_new) {
  270. cnt = def;
  271. printf("%d\n", cnt);
  272. break;
  273. }
  274. /* fall through */
  275. case oldaskconfig:
  276. fflush(stdout);
  277. xfgets(line, sizeof(line), stdin);
  278. strip(line);
  279. if (line[0] == '?') {
  280. print_help(menu);
  281. continue;
  282. }
  283. if (!line[0])
  284. cnt = def;
  285. else if (isdigit(line[0]))
  286. cnt = atoi(line);
  287. else
  288. continue;
  289. break;
  290. default:
  291. break;
  292. }
  293. conf_childs:
  294. for (child = menu->list; child; child = child->next) {
  295. if (!child->sym || !menu_is_visible(child))
  296. continue;
  297. if (!--cnt)
  298. break;
  299. }
  300. if (!child)
  301. continue;
  302. if (line[0] && line[strlen(line) - 1] == '?') {
  303. print_help(child);
  304. continue;
  305. }
  306. sym_set_choice_value(sym, child->sym);
  307. for (child = child->list; child; child = child->next) {
  308. indent += 2;
  309. conf(child);
  310. indent -= 2;
  311. }
  312. return 1;
  313. }
  314. }
  315. static void conf(struct menu *menu)
  316. {
  317. struct symbol *sym;
  318. struct property *prop;
  319. struct menu *child;
  320. if (!menu_is_visible(menu))
  321. return;
  322. sym = menu->sym;
  323. prop = menu->prompt;
  324. if (prop) {
  325. const char *prompt;
  326. switch (prop->type) {
  327. case P_MENU:
  328. /*
  329. * Except in oldaskconfig mode, we show only menus that
  330. * contain new symbols.
  331. */
  332. if (input_mode != oldaskconfig && rootEntry != menu) {
  333. check_conf(menu);
  334. return;
  335. }
  336. /* fall through */
  337. case P_COMMENT:
  338. prompt = menu_get_prompt(menu);
  339. if (prompt)
  340. printf("%*c\n%*c %s\n%*c\n",
  341. indent, '*',
  342. indent, '*', prompt,
  343. indent, '*');
  344. default:
  345. ;
  346. }
  347. }
  348. if (!sym)
  349. goto conf_childs;
  350. if (sym_is_choice(sym)) {
  351. conf_choice(menu);
  352. if (sym->curr.tri != mod)
  353. return;
  354. goto conf_childs;
  355. }
  356. switch (sym->type) {
  357. case S_INT:
  358. case S_HEX:
  359. case S_STRING:
  360. conf_string(menu);
  361. break;
  362. default:
  363. conf_sym(menu);
  364. break;
  365. }
  366. conf_childs:
  367. if (sym)
  368. indent += 2;
  369. for (child = menu->list; child; child = child->next)
  370. conf(child);
  371. if (sym)
  372. indent -= 2;
  373. }
  374. static void check_conf(struct menu *menu)
  375. {
  376. struct symbol *sym;
  377. struct menu *child;
  378. if (!menu_is_visible(menu))
  379. return;
  380. sym = menu->sym;
  381. if (sym && !sym_has_value(sym)) {
  382. if (sym_is_changable(sym) ||
  383. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
  384. if (input_mode == listnewconfig) {
  385. if (sym->name) {
  386. const char *str;
  387. if (sym->type == S_STRING) {
  388. str = sym_get_string_value(sym);
  389. str = sym_escape_string_value(str);
  390. printf("%s%s=%s\n", CONFIG_, sym->name, str);
  391. free((void *)str);
  392. } else {
  393. str = sym_get_string_value(sym);
  394. printf("%s%s=%s\n", CONFIG_, sym->name, str);
  395. }
  396. }
  397. } else {
  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. {"syncconfig", no_argument, NULL, syncconfig},
  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 [-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(" --syncconfig Similar to oldconfig but generates configuration in\n"
  437. " include/{generated/,config/}\n");
  438. printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
  439. printf(" --oldnoconfig An alias of olddefconfig\n");
  440. printf(" --defconfig <file> New config with default defined in <file>\n");
  441. printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
  442. printf(" --allnoconfig New config where all options are answered with no\n");
  443. printf(" --allyesconfig New config where all options are answered with yes\n");
  444. printf(" --allmodconfig New config where all options are answered with mod\n");
  445. printf(" --alldefconfig New config with all symbols set to default\n");
  446. printf(" --randconfig New config with random answer to all options\n");
  447. }
  448. int main(int ac, char **av)
  449. {
  450. const char *progname = av[0];
  451. int opt;
  452. const char *name, *defconfig_file = NULL /* gcc uninit */;
  453. struct stat tmpstat;
  454. int no_conf_write = 0;
  455. tty_stdio = isatty(0) && isatty(1);
  456. while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
  457. if (opt == 's') {
  458. conf_set_message_callback(NULL);
  459. continue;
  460. }
  461. input_mode = (enum input_mode)opt;
  462. switch (opt) {
  463. case syncconfig:
  464. /*
  465. * syncconfig is invoked during the build stage.
  466. * Suppress distracting "configuration written to ..."
  467. */
  468. conf_set_message_callback(NULL);
  469. sync_kconfig = 1;
  470. break;
  471. case defconfig:
  472. case savedefconfig:
  473. defconfig_file = optarg;
  474. break;
  475. case randconfig:
  476. {
  477. struct timeval now;
  478. unsigned int seed;
  479. char *seed_env;
  480. /*
  481. * Use microseconds derived seed,
  482. * compensate for systems where it may be zero
  483. */
  484. gettimeofday(&now, NULL);
  485. seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
  486. seed_env = getenv("KCONFIG_SEED");
  487. if( seed_env && *seed_env ) {
  488. char *endp;
  489. int tmp = (int)strtol(seed_env, &endp, 0);
  490. if (*endp == '\0') {
  491. seed = tmp;
  492. }
  493. }
  494. fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
  495. srand(seed);
  496. break;
  497. }
  498. case oldaskconfig:
  499. case oldconfig:
  500. case allnoconfig:
  501. case allyesconfig:
  502. case allmodconfig:
  503. case alldefconfig:
  504. case listnewconfig:
  505. case olddefconfig:
  506. break;
  507. case '?':
  508. conf_usage(progname);
  509. exit(1);
  510. break;
  511. }
  512. }
  513. if (ac == optind) {
  514. fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
  515. conf_usage(progname);
  516. exit(1);
  517. }
  518. name = av[optind];
  519. conf_parse(name);
  520. //zconfdump(stdout);
  521. if (sync_kconfig) {
  522. name = conf_get_configname();
  523. if (stat(name, &tmpstat)) {
  524. fprintf(stderr, "***\n"
  525. "*** Configuration file \"%s\" not found!\n"
  526. "***\n"
  527. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  528. "*** \"make menuconfig\" or \"make xconfig\").\n"
  529. "***\n", name);
  530. exit(1);
  531. }
  532. }
  533. switch (input_mode) {
  534. case defconfig:
  535. if (!defconfig_file)
  536. defconfig_file = conf_get_default_confname();
  537. if (conf_read(defconfig_file)) {
  538. fprintf(stderr,
  539. "***\n"
  540. "*** Can't find default configuration \"%s\"!\n"
  541. "***\n",
  542. defconfig_file);
  543. exit(1);
  544. }
  545. break;
  546. case savedefconfig:
  547. case syncconfig:
  548. case oldaskconfig:
  549. case oldconfig:
  550. case listnewconfig:
  551. case olddefconfig:
  552. conf_read(NULL);
  553. break;
  554. case allnoconfig:
  555. case allyesconfig:
  556. case allmodconfig:
  557. case alldefconfig:
  558. case randconfig:
  559. name = getenv("KCONFIG_ALLCONFIG");
  560. if (!name)
  561. break;
  562. if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
  563. if (conf_read_simple(name, S_DEF_USER)) {
  564. fprintf(stderr,
  565. "*** Can't read seed configuration \"%s\"!\n",
  566. name);
  567. exit(1);
  568. }
  569. break;
  570. }
  571. switch (input_mode) {
  572. case allnoconfig: name = "allno.config"; break;
  573. case allyesconfig: name = "allyes.config"; break;
  574. case allmodconfig: name = "allmod.config"; break;
  575. case alldefconfig: name = "alldef.config"; break;
  576. case randconfig: name = "allrandom.config"; break;
  577. default: break;
  578. }
  579. if (conf_read_simple(name, S_DEF_USER) &&
  580. conf_read_simple("all.config", S_DEF_USER)) {
  581. fprintf(stderr,
  582. "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
  583. name);
  584. exit(1);
  585. }
  586. break;
  587. default:
  588. break;
  589. }
  590. if (sync_kconfig) {
  591. name = getenv("KCONFIG_NOSILENTUPDATE");
  592. if (name && *name) {
  593. if (conf_get_changed()) {
  594. fprintf(stderr,
  595. "\n*** The configuration requires explicit update.\n\n");
  596. return 1;
  597. }
  598. no_conf_write = 1;
  599. }
  600. }
  601. switch (input_mode) {
  602. case allnoconfig:
  603. conf_set_all_new_symbols(def_no);
  604. break;
  605. case allyesconfig:
  606. conf_set_all_new_symbols(def_yes);
  607. break;
  608. case allmodconfig:
  609. conf_set_all_new_symbols(def_mod);
  610. break;
  611. case alldefconfig:
  612. conf_set_all_new_symbols(def_default);
  613. break;
  614. case randconfig:
  615. /* Really nothing to do in this loop */
  616. while (conf_set_all_new_symbols(def_random)) ;
  617. break;
  618. case defconfig:
  619. conf_set_all_new_symbols(def_default);
  620. break;
  621. case savedefconfig:
  622. break;
  623. case oldaskconfig:
  624. rootEntry = &rootmenu;
  625. conf(&rootmenu);
  626. input_mode = oldconfig;
  627. /* fall through */
  628. case oldconfig:
  629. case listnewconfig:
  630. case syncconfig:
  631. /* Update until a loop caused no more changes */
  632. do {
  633. conf_cnt = 0;
  634. check_conf(&rootmenu);
  635. } while (conf_cnt);
  636. break;
  637. case olddefconfig:
  638. default:
  639. break;
  640. }
  641. 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 (!no_conf_write && conf_write(NULL)) {
  649. fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
  650. exit(1);
  651. }
  652. /*
  653. * Create auto.conf if it does not exist.
  654. * This prevents GNU Make 4.1 or older from emitting
  655. * "include/config/auto.conf: No such file or directory"
  656. * in the top-level Makefile
  657. *
  658. * syncconfig always creates or updates auto.conf because it is
  659. * used during the build.
  660. */
  661. if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
  662. fprintf(stderr,
  663. "\n*** Error during sync of the configuration.\n\n");
  664. return 1;
  665. }
  666. }
  667. return 0;
  668. }