conf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2002 Roman Zippel <[email protected]>
  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. fatalrecursive,
  36. };
  37. static enum input_mode input_mode = oldaskconfig;
  38. static int indent = 1;
  39. static int tty_stdio;
  40. static int sync_kconfig;
  41. static int conf_cnt;
  42. static char line[PATH_MAX];
  43. static struct menu *rootEntry;
  44. static void print_help(struct menu *menu)
  45. {
  46. struct gstr help = str_new();
  47. menu_get_ext_help(menu, &help);
  48. printf("\n%s\n", str_get(&help));
  49. str_free(&help);
  50. }
  51. static void strip(char *str)
  52. {
  53. char *p = str;
  54. int l;
  55. while ((isspace(*p)))
  56. p++;
  57. l = strlen(p);
  58. if (p != str)
  59. memmove(str, p, l + 1);
  60. if (!l)
  61. return;
  62. p = str + l - 1;
  63. while ((isspace(*p)))
  64. *p-- = 0;
  65. }
  66. /* Helper function to facilitate fgets() by Jean Sacren. */
  67. static void xfgets(char *str, int size, FILE *in)
  68. {
  69. if (!fgets(str, size, in))
  70. fprintf(stderr, "\nError in reading or end of file.\n");
  71. if (!tty_stdio)
  72. printf("%s", str);
  73. }
  74. static int conf_askvalue(struct symbol *sym, const char *def)
  75. {
  76. enum symbol_type type = sym_get_type(sym);
  77. if (!sym_has_value(sym))
  78. printf("(NEW) ");
  79. line[0] = '\n';
  80. line[1] = 0;
  81. if (!sym_is_changeable(sym)) {
  82. printf("%s\n", def);
  83. line[0] = '\n';
  84. line[1] = 0;
  85. return 0;
  86. }
  87. switch (input_mode) {
  88. case oldconfig:
  89. case syncconfig:
  90. if (sym_has_value(sym)) {
  91. printf("%s\n", def);
  92. return 0;
  93. }
  94. /* fall through */
  95. case oldaskconfig:
  96. fflush(stdout);
  97. xfgets(line, sizeof(line), stdin);
  98. return 1;
  99. default:
  100. break;
  101. }
  102. switch (type) {
  103. case S_INT:
  104. case S_HEX:
  105. case S_STRING:
  106. printf("%s\n", def);
  107. return 1;
  108. default:
  109. ;
  110. }
  111. printf("%s", line);
  112. return 1;
  113. }
  114. static int conf_string(struct menu *menu)
  115. {
  116. struct symbol *sym = menu->sym;
  117. const char *def;
  118. while (1) {
  119. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  120. printf("(%s) ", sym->name);
  121. def = sym_get_string_value(sym);
  122. if (sym_get_string_value(sym))
  123. printf("[%s] ", def);
  124. if (!conf_askvalue(sym, def))
  125. return 0;
  126. switch (line[0]) {
  127. case '\n':
  128. break;
  129. case '?':
  130. /* print help */
  131. if (line[1] == '\n') {
  132. print_help(menu);
  133. def = NULL;
  134. break;
  135. }
  136. /* fall through */
  137. default:
  138. line[strlen(line)-1] = 0;
  139. def = line;
  140. }
  141. if (def && sym_set_string_value(sym, def))
  142. return 0;
  143. }
  144. }
  145. static int conf_sym(struct menu *menu)
  146. {
  147. struct symbol *sym = menu->sym;
  148. tristate oldval, newval;
  149. while (1) {
  150. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  151. if (sym->name)
  152. printf("(%s) ", sym->name);
  153. putchar('[');
  154. oldval = sym_get_tristate_value(sym);
  155. switch (oldval) {
  156. case no:
  157. putchar('N');
  158. break;
  159. case mod:
  160. putchar('M');
  161. break;
  162. case yes:
  163. putchar('Y');
  164. break;
  165. }
  166. if (oldval != no && sym_tristate_within_range(sym, no))
  167. printf("/n");
  168. if (oldval != mod && sym_tristate_within_range(sym, mod))
  169. printf("/m");
  170. if (oldval != yes && sym_tristate_within_range(sym, yes))
  171. printf("/y");
  172. printf("/?] ");
  173. if (!conf_askvalue(sym, sym_get_string_value(sym)))
  174. return 0;
  175. strip(line);
  176. switch (line[0]) {
  177. case 'n':
  178. case 'N':
  179. newval = no;
  180. if (!line[1] || !strcmp(&line[1], "o"))
  181. break;
  182. continue;
  183. case 'm':
  184. case 'M':
  185. newval = mod;
  186. if (!line[1])
  187. break;
  188. continue;
  189. case 'y':
  190. case 'Y':
  191. newval = yes;
  192. if (!line[1] || !strcmp(&line[1], "es"))
  193. break;
  194. continue;
  195. case 0:
  196. newval = oldval;
  197. break;
  198. case '?':
  199. goto help;
  200. default:
  201. continue;
  202. }
  203. if (sym_set_tristate_value(sym, newval))
  204. return 0;
  205. help:
  206. print_help(menu);
  207. }
  208. }
  209. static int conf_choice(struct menu *menu)
  210. {
  211. struct symbol *sym, *def_sym;
  212. struct menu *child;
  213. bool is_new;
  214. sym = menu->sym;
  215. is_new = !sym_has_value(sym);
  216. if (sym_is_changeable(sym)) {
  217. conf_sym(menu);
  218. sym_calc_value(sym);
  219. switch (sym_get_tristate_value(sym)) {
  220. case no:
  221. return 1;
  222. case mod:
  223. return 0;
  224. case yes:
  225. break;
  226. }
  227. } else {
  228. switch (sym_get_tristate_value(sym)) {
  229. case no:
  230. return 1;
  231. case mod:
  232. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  233. return 0;
  234. case yes:
  235. break;
  236. }
  237. }
  238. while (1) {
  239. int cnt, def;
  240. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  241. def_sym = sym_get_choice_value(sym);
  242. cnt = def = 0;
  243. line[0] = 0;
  244. for (child = menu->list; child; child = child->next) {
  245. if (!menu_is_visible(child))
  246. continue;
  247. if (!child->sym) {
  248. printf("%*c %s\n", indent, '*', menu_get_prompt(child));
  249. continue;
  250. }
  251. cnt++;
  252. if (child->sym == def_sym) {
  253. def = cnt;
  254. printf("%*c", indent, '>');
  255. } else
  256. printf("%*c", indent, ' ');
  257. printf(" %d. %s", cnt, menu_get_prompt(child));
  258. if (child->sym->name)
  259. printf(" (%s)", child->sym->name);
  260. if (!sym_has_value(child->sym))
  261. printf(" (NEW)");
  262. printf("\n");
  263. }
  264. printf("%*schoice", indent - 1, "");
  265. if (cnt == 1) {
  266. printf("[1]: 1\n");
  267. goto conf_childs;
  268. }
  269. printf("[1-%d?]: ", cnt);
  270. switch (input_mode) {
  271. case oldconfig:
  272. case syncconfig:
  273. if (!is_new) {
  274. cnt = def;
  275. printf("%d\n", cnt);
  276. break;
  277. }
  278. /* fall through */
  279. case oldaskconfig:
  280. fflush(stdout);
  281. xfgets(line, sizeof(line), stdin);
  282. strip(line);
  283. if (line[0] == '?') {
  284. print_help(menu);
  285. continue;
  286. }
  287. if (!line[0])
  288. cnt = def;
  289. else if (isdigit(line[0]))
  290. cnt = atoi(line);
  291. else
  292. continue;
  293. break;
  294. default:
  295. break;
  296. }
  297. conf_childs:
  298. for (child = menu->list; child; child = child->next) {
  299. if (!child->sym || !menu_is_visible(child))
  300. continue;
  301. if (!--cnt)
  302. break;
  303. }
  304. if (!child)
  305. continue;
  306. if (line[0] && line[strlen(line) - 1] == '?') {
  307. print_help(child);
  308. continue;
  309. }
  310. sym_set_choice_value(sym, child->sym);
  311. for (child = child->list; child; child = child->next) {
  312. indent += 2;
  313. conf(child);
  314. indent -= 2;
  315. }
  316. return 1;
  317. }
  318. }
  319. static void conf(struct menu *menu)
  320. {
  321. struct symbol *sym;
  322. struct property *prop;
  323. struct menu *child;
  324. if (!menu_is_visible(menu))
  325. return;
  326. sym = menu->sym;
  327. prop = menu->prompt;
  328. if (prop) {
  329. const char *prompt;
  330. switch (prop->type) {
  331. case P_MENU:
  332. /*
  333. * Except in oldaskconfig mode, we show only menus that
  334. * contain new symbols.
  335. */
  336. if (input_mode != oldaskconfig && rootEntry != menu) {
  337. check_conf(menu);
  338. return;
  339. }
  340. /* fall through */
  341. case P_COMMENT:
  342. prompt = menu_get_prompt(menu);
  343. if (prompt)
  344. printf("%*c\n%*c %s\n%*c\n",
  345. indent, '*',
  346. indent, '*', prompt,
  347. indent, '*');
  348. default:
  349. ;
  350. }
  351. }
  352. if (!sym)
  353. goto conf_childs;
  354. if (sym_is_choice(sym)) {
  355. conf_choice(menu);
  356. if (sym->curr.tri != mod)
  357. return;
  358. goto conf_childs;
  359. }
  360. switch (sym->type) {
  361. case S_INT:
  362. case S_HEX:
  363. case S_STRING:
  364. conf_string(menu);
  365. break;
  366. default:
  367. conf_sym(menu);
  368. break;
  369. }
  370. conf_childs:
  371. if (sym)
  372. indent += 2;
  373. for (child = menu->list; child; child = child->next)
  374. conf(child);
  375. if (sym)
  376. indent -= 2;
  377. }
  378. static void check_conf(struct menu *menu)
  379. {
  380. struct symbol *sym;
  381. struct menu *child;
  382. if (!menu_is_visible(menu))
  383. return;
  384. sym = menu->sym;
  385. if (sym && !sym_has_value(sym)) {
  386. if (sym_is_changeable(sym) ||
  387. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
  388. if (input_mode == listnewconfig) {
  389. if (sym->name) {
  390. const char *str;
  391. if (sym->type == S_STRING) {
  392. str = sym_get_string_value(sym);
  393. str = sym_escape_string_value(str);
  394. printf("%s%s=%s\n", CONFIG_, sym->name, str);
  395. free((void *)str);
  396. } else {
  397. str = sym_get_string_value(sym);
  398. printf("%s%s=%s\n", CONFIG_, sym->name, str);
  399. }
  400. }
  401. } else if (input_mode == helpnewconfig) {
  402. printf("-----\n");
  403. print_help(menu);
  404. printf("-----\n");
  405. } else {
  406. if (!conf_cnt++)
  407. printf("*\n* Restart config...\n*\n");
  408. rootEntry = menu_get_parent_menu(menu);
  409. conf(rootEntry);
  410. }
  411. }
  412. }
  413. for (child = menu->list; child; child = child->next)
  414. check_conf(child);
  415. }
  416. static struct option long_opts[] = {
  417. {"oldaskconfig", no_argument, NULL, oldaskconfig},
  418. {"oldconfig", no_argument, NULL, oldconfig},
  419. {"syncconfig", no_argument, NULL, syncconfig},
  420. {"defconfig", required_argument, NULL, defconfig},
  421. {"savedefconfig", required_argument, NULL, savedefconfig},
  422. {"allnoconfig", no_argument, NULL, allnoconfig},
  423. {"allyesconfig", no_argument, NULL, allyesconfig},
  424. {"allmodconfig", no_argument, NULL, allmodconfig},
  425. {"alldefconfig", no_argument, NULL, alldefconfig},
  426. {"randconfig", no_argument, NULL, randconfig},
  427. {"listnewconfig", no_argument, NULL, listnewconfig},
  428. {"helpnewconfig", no_argument, NULL, helpnewconfig},
  429. {"olddefconfig", no_argument, NULL, olddefconfig},
  430. {"yes2modconfig", no_argument, NULL, yes2modconfig},
  431. {"mod2yesconfig", no_argument, NULL, mod2yesconfig},
  432. {"fatalrecursive", no_argument, NULL, fatalrecursive},
  433. {NULL, 0, NULL, 0}
  434. };
  435. static void conf_usage(const char *progname)
  436. {
  437. printf("Usage: %s [-s] [--fatalrecursive] [option] <kconfig-file>\n", progname);
  438. printf("[option] is _one_ of the following:\n");
  439. printf(" --listnewconfig List new options\n");
  440. printf(" --helpnewconfig List new options and help text\n");
  441. printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
  442. printf(" --oldconfig Update a configuration using a provided .config as base\n");
  443. printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
  444. " include/{generated/,config/}\n");
  445. printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
  446. printf(" --defconfig <file> New config with default defined in <file>\n");
  447. printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
  448. printf(" --allnoconfig New config where all options are answered with no\n");
  449. printf(" --allyesconfig New config where all options are answered with yes\n");
  450. printf(" --allmodconfig New config where all options are answered with mod\n");
  451. printf(" --alldefconfig New config with all symbols set to default\n");
  452. printf(" --randconfig New config with random answer to all options\n");
  453. printf(" --yes2modconfig Change answers from yes to mod if possible\n");
  454. printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
  455. }
  456. int main(int ac, char **av)
  457. {
  458. const char *progname = av[0];
  459. int opt;
  460. const char *name, *defconfig_file = NULL /* gcc uninit */;
  461. const char *input_file = NULL, *output_file = NULL;
  462. int no_conf_write = 0;
  463. tty_stdio = isatty(0) && isatty(1);
  464. while ((opt = getopt_long(ac, av, "r:w:s", long_opts, NULL)) != -1) {
  465. if (opt == 's') {
  466. conf_set_message_callback(NULL);
  467. continue;
  468. }
  469. switch (opt) {
  470. case syncconfig:
  471. /*
  472. * syncconfig is invoked during the build stage.
  473. * Suppress distracting "configuration written to ..."
  474. */
  475. conf_set_message_callback(NULL);
  476. sync_kconfig = 1;
  477. break;
  478. case defconfig:
  479. case savedefconfig:
  480. defconfig_file = optarg;
  481. break;
  482. case randconfig:
  483. {
  484. struct timeval now;
  485. unsigned int seed;
  486. char *seed_env;
  487. /*
  488. * Use microseconds derived seed,
  489. * compensate for systems where it may be zero
  490. */
  491. gettimeofday(&now, NULL);
  492. seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
  493. seed_env = getenv("KCONFIG_SEED");
  494. if( seed_env && *seed_env ) {
  495. char *endp;
  496. int tmp = (int)strtol(seed_env, &endp, 0);
  497. if (*endp == '\0') {
  498. seed = tmp;
  499. }
  500. }
  501. fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
  502. srand(seed);
  503. break;
  504. }
  505. case oldaskconfig:
  506. case oldconfig:
  507. case allnoconfig:
  508. case allyesconfig:
  509. case allmodconfig:
  510. case alldefconfig:
  511. case listnewconfig:
  512. case helpnewconfig:
  513. case olddefconfig:
  514. case yes2modconfig:
  515. case mod2yesconfig:
  516. break;
  517. case fatalrecursive:
  518. recursive_is_error = 1;
  519. continue;
  520. case 'r':
  521. input_file = optarg;
  522. continue;
  523. case 'w':
  524. output_file = optarg;
  525. continue;
  526. case '?':
  527. conf_usage(progname);
  528. exit(1);
  529. break;
  530. }
  531. input_mode = (enum input_mode)opt;
  532. }
  533. if (ac == optind) {
  534. fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
  535. conf_usage(progname);
  536. exit(1);
  537. }
  538. name = av[optind];
  539. conf_parse(name);
  540. //zconfdump(stdout);
  541. switch (input_mode) {
  542. case defconfig:
  543. if (conf_read(defconfig_file)) {
  544. fprintf(stderr,
  545. "***\n"
  546. "*** Can't find default configuration \"%s\"!\n"
  547. "***\n",
  548. defconfig_file);
  549. exit(1);
  550. }
  551. break;
  552. case savedefconfig:
  553. case syncconfig:
  554. case oldaskconfig:
  555. case oldconfig:
  556. case listnewconfig:
  557. case helpnewconfig:
  558. case olddefconfig:
  559. case yes2modconfig:
  560. case mod2yesconfig:
  561. case allnoconfig:
  562. case allyesconfig:
  563. case allmodconfig:
  564. case alldefconfig:
  565. case randconfig:
  566. conf_read(input_file);
  567. break;
  568. default:
  569. break;
  570. }
  571. if (sync_kconfig) {
  572. name = getenv("KCONFIG_NOSILENTUPDATE");
  573. if (name && *name) {
  574. if (conf_get_changed()) {
  575. fprintf(stderr,
  576. "\n*** The configuration requires explicit update.\n\n");
  577. return 1;
  578. }
  579. no_conf_write = 1;
  580. }
  581. }
  582. switch (input_mode) {
  583. case allnoconfig:
  584. conf_set_all_new_symbols(def_no);
  585. break;
  586. case allyesconfig:
  587. conf_set_all_new_symbols(def_yes);
  588. break;
  589. case allmodconfig:
  590. conf_set_all_new_symbols(def_mod);
  591. break;
  592. case alldefconfig:
  593. conf_set_all_new_symbols(def_default);
  594. break;
  595. case randconfig:
  596. /* Really nothing to do in this loop */
  597. while (conf_set_all_new_symbols(def_random)) ;
  598. break;
  599. case defconfig:
  600. conf_set_all_new_symbols(def_default);
  601. break;
  602. case savedefconfig:
  603. break;
  604. case yes2modconfig:
  605. conf_rewrite_mod_or_yes(def_y2m);
  606. break;
  607. case mod2yesconfig:
  608. conf_rewrite_mod_or_yes(def_m2y);
  609. break;
  610. case oldaskconfig:
  611. rootEntry = &rootmenu;
  612. conf(&rootmenu);
  613. input_mode = oldconfig;
  614. /* fall through */
  615. case oldconfig:
  616. case listnewconfig:
  617. case helpnewconfig:
  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 (input_mode == savedefconfig) {
  630. if (conf_write_defconfig(defconfig_file)) {
  631. fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
  632. defconfig_file);
  633. return 1;
  634. }
  635. } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
  636. if ((output_file || !no_conf_write) &&
  637. conf_write(output_file)) {
  638. fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
  639. exit(1);
  640. }
  641. /*
  642. * Create auto.conf if it does not exist.
  643. * This prevents GNU Make 4.1 or older from emitting
  644. * "include/config/auto.conf: No such file or directory"
  645. * in the top-level Makefile
  646. *
  647. * syncconfig always creates or updates auto.conf because it is
  648. * used during the build.
  649. */
  650. if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
  651. fprintf(stderr,
  652. "\n*** Error during sync of the configuration.\n\n");
  653. return 1;
  654. }
  655. }
  656. return 0;
  657. }