conf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  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. helpnewconfig,
  32. olddefconfig,
  33. yes2modconfig,
  34. mod2yesconfig,
  35. };
  36. static enum input_mode input_mode = oldaskconfig;
  37. static int indent = 1;
  38. static int tty_stdio;
  39. static int sync_kconfig;
  40. static int conf_cnt;
  41. static char line[PATH_MAX];
  42. static struct menu *rootEntry;
  43. static void print_help(struct menu *menu)
  44. {
  45. struct gstr help = str_new();
  46. menu_get_ext_help(menu, &help);
  47. printf("\n%s\n", str_get(&help));
  48. str_free(&help);
  49. }
  50. static void strip(char *str)
  51. {
  52. char *p = str;
  53. int l;
  54. while ((isspace(*p)))
  55. p++;
  56. l = strlen(p);
  57. if (p != str)
  58. memmove(str, p, l + 1);
  59. if (!l)
  60. return;
  61. p = str + l - 1;
  62. while ((isspace(*p)))
  63. *p-- = 0;
  64. }
  65. /* Helper function to facilitate fgets() by Jean Sacren. */
  66. static void xfgets(char *str, int size, FILE *in)
  67. {
  68. if (!fgets(str, size, in))
  69. fprintf(stderr, "\nError in reading or end of file.\n");
  70. if (!tty_stdio)
  71. printf("%s", str);
  72. }
  73. static int conf_askvalue(struct symbol *sym, const char *def)
  74. {
  75. enum symbol_type type = sym_get_type(sym);
  76. if (!sym_has_value(sym))
  77. printf("(NEW) ");
  78. line[0] = '\n';
  79. line[1] = 0;
  80. if (!sym_is_changeable(sym)) {
  81. printf("%s\n", def);
  82. line[0] = '\n';
  83. line[1] = 0;
  84. return 0;
  85. }
  86. switch (input_mode) {
  87. case oldconfig:
  88. case syncconfig:
  89. if (sym_has_value(sym)) {
  90. printf("%s\n", def);
  91. return 0;
  92. }
  93. /* fall through */
  94. case oldaskconfig:
  95. fflush(stdout);
  96. xfgets(line, sizeof(line), stdin);
  97. return 1;
  98. default:
  99. break;
  100. }
  101. switch (type) {
  102. case S_INT:
  103. case S_HEX:
  104. case S_STRING:
  105. printf("%s\n", def);
  106. return 1;
  107. default:
  108. ;
  109. }
  110. printf("%s", line);
  111. return 1;
  112. }
  113. static int conf_string(struct menu *menu)
  114. {
  115. struct symbol *sym = menu->sym;
  116. const char *def;
  117. while (1) {
  118. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  119. printf("(%s) ", sym->name);
  120. def = sym_get_string_value(sym);
  121. if (sym_get_string_value(sym))
  122. printf("[%s] ", def);
  123. if (!conf_askvalue(sym, def))
  124. return 0;
  125. switch (line[0]) {
  126. case '\n':
  127. break;
  128. case '?':
  129. /* print help */
  130. if (line[1] == '\n') {
  131. print_help(menu);
  132. def = NULL;
  133. break;
  134. }
  135. /* fall through */
  136. default:
  137. line[strlen(line)-1] = 0;
  138. def = line;
  139. }
  140. if (def && sym_set_string_value(sym, def))
  141. return 0;
  142. }
  143. }
  144. static int conf_sym(struct menu *menu)
  145. {
  146. struct symbol *sym = menu->sym;
  147. tristate oldval, newval;
  148. while (1) {
  149. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  150. if (sym->name)
  151. printf("(%s) ", sym->name);
  152. putchar('[');
  153. oldval = sym_get_tristate_value(sym);
  154. switch (oldval) {
  155. case no:
  156. putchar('N');
  157. break;
  158. case mod:
  159. putchar('M');
  160. break;
  161. case yes:
  162. putchar('Y');
  163. break;
  164. }
  165. if (oldval != no && sym_tristate_within_range(sym, no))
  166. printf("/n");
  167. if (oldval != mod && sym_tristate_within_range(sym, mod))
  168. printf("/m");
  169. if (oldval != yes && sym_tristate_within_range(sym, yes))
  170. printf("/y");
  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_changeable(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. switch (input_mode) {
  270. case oldconfig:
  271. case syncconfig:
  272. if (!is_new) {
  273. cnt = def;
  274. printf("%d\n", cnt);
  275. break;
  276. }
  277. /* fall through */
  278. case oldaskconfig:
  279. fflush(stdout);
  280. xfgets(line, sizeof(line), stdin);
  281. strip(line);
  282. if (line[0] == '?') {
  283. print_help(menu);
  284. continue;
  285. }
  286. if (!line[0])
  287. cnt = def;
  288. else if (isdigit(line[0]))
  289. cnt = atoi(line);
  290. else
  291. continue;
  292. break;
  293. default:
  294. break;
  295. }
  296. conf_childs:
  297. for (child = menu->list; child; child = child->next) {
  298. if (!child->sym || !menu_is_visible(child))
  299. continue;
  300. if (!--cnt)
  301. break;
  302. }
  303. if (!child)
  304. continue;
  305. if (line[0] && line[strlen(line) - 1] == '?') {
  306. print_help(child);
  307. continue;
  308. }
  309. sym_set_choice_value(sym, child->sym);
  310. for (child = child->list; child; child = child->next) {
  311. indent += 2;
  312. conf(child);
  313. indent -= 2;
  314. }
  315. return 1;
  316. }
  317. }
  318. static void conf(struct menu *menu)
  319. {
  320. struct symbol *sym;
  321. struct property *prop;
  322. struct menu *child;
  323. if (!menu_is_visible(menu))
  324. return;
  325. sym = menu->sym;
  326. prop = menu->prompt;
  327. if (prop) {
  328. const char *prompt;
  329. switch (prop->type) {
  330. case P_MENU:
  331. /*
  332. * Except in oldaskconfig mode, we show only menus that
  333. * contain new symbols.
  334. */
  335. if (input_mode != oldaskconfig && rootEntry != menu) {
  336. check_conf(menu);
  337. return;
  338. }
  339. /* fall through */
  340. case P_COMMENT:
  341. prompt = menu_get_prompt(menu);
  342. if (prompt)
  343. printf("%*c\n%*c %s\n%*c\n",
  344. indent, '*',
  345. indent, '*', prompt,
  346. indent, '*');
  347. default:
  348. ;
  349. }
  350. }
  351. if (!sym)
  352. goto conf_childs;
  353. if (sym_is_choice(sym)) {
  354. conf_choice(menu);
  355. if (sym->curr.tri != mod)
  356. return;
  357. goto conf_childs;
  358. }
  359. switch (sym->type) {
  360. case S_INT:
  361. case S_HEX:
  362. case S_STRING:
  363. conf_string(menu);
  364. break;
  365. default:
  366. conf_sym(menu);
  367. break;
  368. }
  369. conf_childs:
  370. if (sym)
  371. indent += 2;
  372. for (child = menu->list; child; child = child->next)
  373. conf(child);
  374. if (sym)
  375. indent -= 2;
  376. }
  377. static void check_conf(struct menu *menu)
  378. {
  379. struct symbol *sym;
  380. struct menu *child;
  381. if (!menu_is_visible(menu))
  382. return;
  383. sym = menu->sym;
  384. if (sym && !sym_has_value(sym)) {
  385. if (sym_is_changeable(sym) ||
  386. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
  387. if (input_mode == listnewconfig) {
  388. if (sym->name) {
  389. const char *str;
  390. if (sym->type == S_STRING) {
  391. str = sym_get_string_value(sym);
  392. str = sym_escape_string_value(str);
  393. printf("%s%s=%s\n", CONFIG_, sym->name, str);
  394. free((void *)str);
  395. } else {
  396. str = sym_get_string_value(sym);
  397. printf("%s%s=%s\n", CONFIG_, sym->name, str);
  398. }
  399. }
  400. } else if (input_mode == helpnewconfig) {
  401. printf("-----\n");
  402. print_help(menu);
  403. printf("-----\n");
  404. } else {
  405. if (!conf_cnt++)
  406. printf("*\n* Restart config...\n*\n");
  407. rootEntry = menu_get_parent_menu(menu);
  408. conf(rootEntry);
  409. }
  410. }
  411. }
  412. for (child = menu->list; child; child = child->next)
  413. check_conf(child);
  414. }
  415. static struct option long_opts[] = {
  416. {"oldaskconfig", no_argument, NULL, oldaskconfig},
  417. {"oldconfig", no_argument, NULL, oldconfig},
  418. {"syncconfig", no_argument, NULL, syncconfig},
  419. {"defconfig", required_argument, NULL, defconfig},
  420. {"savedefconfig", required_argument, NULL, savedefconfig},
  421. {"allnoconfig", no_argument, NULL, allnoconfig},
  422. {"allyesconfig", no_argument, NULL, allyesconfig},
  423. {"allmodconfig", no_argument, NULL, allmodconfig},
  424. {"alldefconfig", no_argument, NULL, alldefconfig},
  425. {"randconfig", no_argument, NULL, randconfig},
  426. {"listnewconfig", no_argument, NULL, listnewconfig},
  427. {"helpnewconfig", no_argument, NULL, helpnewconfig},
  428. {"olddefconfig", no_argument, NULL, olddefconfig},
  429. {"yes2modconfig", no_argument, NULL, yes2modconfig},
  430. {"mod2yesconfig", no_argument, NULL, mod2yesconfig},
  431. {NULL, 0, NULL, 0}
  432. };
  433. static void conf_usage(const char *progname)
  434. {
  435. printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
  436. printf("[option] is _one_ of the following:\n");
  437. printf(" --listnewconfig List new options\n");
  438. printf(" --helpnewconfig List new options and help text\n");
  439. printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
  440. printf(" --oldconfig Update a configuration using a provided .config as base\n");
  441. printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
  442. " include/{generated/,config/}\n");
  443. printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
  444. printf(" --defconfig <file> New config with default defined in <file>\n");
  445. printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
  446. printf(" --allnoconfig New config where all options are answered with no\n");
  447. printf(" --allyesconfig New config where all options are answered with yes\n");
  448. printf(" --allmodconfig New config where all options are answered with mod\n");
  449. printf(" --alldefconfig New config with all symbols set to default\n");
  450. printf(" --randconfig New config with random answer to all options\n");
  451. printf(" --yes2modconfig Change answers from yes to mod if possible\n");
  452. printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
  453. }
  454. int main(int ac, char **av)
  455. {
  456. const char *progname = av[0];
  457. int opt;
  458. const char *name, *defconfig_file = NULL /* gcc uninit */;
  459. int no_conf_write = 0;
  460. tty_stdio = isatty(0) && isatty(1);
  461. while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
  462. if (opt == 's') {
  463. conf_set_message_callback(NULL);
  464. continue;
  465. }
  466. input_mode = (enum input_mode)opt;
  467. switch (opt) {
  468. case syncconfig:
  469. /*
  470. * syncconfig is invoked during the build stage.
  471. * Suppress distracting "configuration written to ..."
  472. */
  473. conf_set_message_callback(NULL);
  474. sync_kconfig = 1;
  475. break;
  476. case defconfig:
  477. case savedefconfig:
  478. defconfig_file = optarg;
  479. break;
  480. case randconfig:
  481. {
  482. struct timeval now;
  483. unsigned int seed;
  484. char *seed_env;
  485. /*
  486. * Use microseconds derived seed,
  487. * compensate for systems where it may be zero
  488. */
  489. gettimeofday(&now, NULL);
  490. seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
  491. seed_env = getenv("KCONFIG_SEED");
  492. if( seed_env && *seed_env ) {
  493. char *endp;
  494. int tmp = (int)strtol(seed_env, &endp, 0);
  495. if (*endp == '\0') {
  496. seed = tmp;
  497. }
  498. }
  499. fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
  500. srand(seed);
  501. break;
  502. }
  503. case oldaskconfig:
  504. case oldconfig:
  505. case allnoconfig:
  506. case allyesconfig:
  507. case allmodconfig:
  508. case alldefconfig:
  509. case listnewconfig:
  510. case helpnewconfig:
  511. case olddefconfig:
  512. case yes2modconfig:
  513. case mod2yesconfig:
  514. break;
  515. case '?':
  516. conf_usage(progname);
  517. exit(1);
  518. break;
  519. }
  520. }
  521. if (ac == optind) {
  522. fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
  523. conf_usage(progname);
  524. exit(1);
  525. }
  526. name = av[optind];
  527. conf_parse(name);
  528. //zconfdump(stdout);
  529. switch (input_mode) {
  530. case defconfig:
  531. if (conf_read(defconfig_file)) {
  532. fprintf(stderr,
  533. "***\n"
  534. "*** Can't find default configuration \"%s\"!\n"
  535. "***\n",
  536. defconfig_file);
  537. exit(1);
  538. }
  539. break;
  540. case savedefconfig:
  541. case syncconfig:
  542. case oldaskconfig:
  543. case oldconfig:
  544. case listnewconfig:
  545. case helpnewconfig:
  546. case olddefconfig:
  547. case yes2modconfig:
  548. case mod2yesconfig:
  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. name = getenv("KCONFIG_NOSILENTUPDATE");
  589. if (name && *name) {
  590. if (conf_get_changed()) {
  591. fprintf(stderr,
  592. "\n*** The configuration requires explicit update.\n\n");
  593. return 1;
  594. }
  595. no_conf_write = 1;
  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 yes2modconfig:
  621. conf_rewrite_mod_or_yes(def_y2m);
  622. break;
  623. case mod2yesconfig:
  624. conf_rewrite_mod_or_yes(def_m2y);
  625. break;
  626. case oldaskconfig:
  627. rootEntry = &rootmenu;
  628. conf(&rootmenu);
  629. input_mode = oldconfig;
  630. /* fall through */
  631. case oldconfig:
  632. case listnewconfig:
  633. case helpnewconfig:
  634. case syncconfig:
  635. /* Update until a loop caused no more changes */
  636. do {
  637. conf_cnt = 0;
  638. check_conf(&rootmenu);
  639. } while (conf_cnt);
  640. break;
  641. case olddefconfig:
  642. default:
  643. break;
  644. }
  645. if (input_mode == savedefconfig) {
  646. if (conf_write_defconfig(defconfig_file)) {
  647. fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
  648. defconfig_file);
  649. return 1;
  650. }
  651. } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
  652. if (!no_conf_write && conf_write(NULL)) {
  653. fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
  654. exit(1);
  655. }
  656. /*
  657. * Create auto.conf if it does not exist.
  658. * This prevents GNU Make 4.1 or older from emitting
  659. * "include/config/auto.conf: No such file or directory"
  660. * in the top-level Makefile
  661. *
  662. * syncconfig always creates or updates auto.conf because it is
  663. * used during the build.
  664. */
  665. if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
  666. fprintf(stderr,
  667. "\n*** Error during sync of the configuration.\n\n");
  668. return 1;
  669. }
  670. }
  671. return 0;
  672. }