conf.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. // SPDX-License-Identifier: GPL-2.0
  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/time.h>
  14. #include <errno.h>
  15. #include "lkc.h"
  16. static void conf(struct menu *menu);
  17. static void check_conf(struct menu *menu);
  18. enum input_mode {
  19. oldaskconfig,
  20. syncconfig,
  21. oldconfig,
  22. allnoconfig,
  23. allyesconfig,
  24. allmodconfig,
  25. alldefconfig,
  26. randconfig,
  27. defconfig,
  28. savedefconfig,
  29. listnewconfig,
  30. helpnewconfig,
  31. olddefconfig,
  32. yes2modconfig,
  33. mod2yesconfig,
  34. mod2noconfig,
  35. };
  36. static enum input_mode input_mode = oldaskconfig;
  37. static int input_mode_opt;
  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 void set_randconfig_seed(void)
  75. {
  76. unsigned int seed;
  77. char *env;
  78. bool seed_set = false;
  79. env = getenv("KCONFIG_SEED");
  80. if (env && *env) {
  81. char *endp;
  82. seed = strtol(env, &endp, 0);
  83. if (*endp == '\0')
  84. seed_set = true;
  85. }
  86. if (!seed_set) {
  87. struct timeval now;
  88. /*
  89. * Use microseconds derived seed, compensate for systems where it may
  90. * be zero.
  91. */
  92. gettimeofday(&now, NULL);
  93. seed = (now.tv_sec + 1) * (now.tv_usec + 1);
  94. }
  95. printf("KCONFIG_SEED=0x%X\n", seed);
  96. srand(seed);
  97. }
  98. static bool randomize_choice_values(struct symbol *csym)
  99. {
  100. struct property *prop;
  101. struct symbol *sym;
  102. struct expr *e;
  103. int cnt, def;
  104. /*
  105. * If choice is mod then we may have more items selected
  106. * and if no then no-one.
  107. * In both cases stop.
  108. */
  109. if (csym->curr.tri != yes)
  110. return false;
  111. prop = sym_get_choice_prop(csym);
  112. /* count entries in choice block */
  113. cnt = 0;
  114. expr_list_for_each_sym(prop->expr, e, sym)
  115. cnt++;
  116. /*
  117. * find a random value and set it to yes,
  118. * set the rest to no so we have only one set
  119. */
  120. def = rand() % cnt;
  121. cnt = 0;
  122. expr_list_for_each_sym(prop->expr, e, sym) {
  123. if (def == cnt++) {
  124. sym->def[S_DEF_USER].tri = yes;
  125. csym->def[S_DEF_USER].val = sym;
  126. } else {
  127. sym->def[S_DEF_USER].tri = no;
  128. }
  129. sym->flags |= SYMBOL_DEF_USER;
  130. /* clear VALID to get value calculated */
  131. sym->flags &= ~SYMBOL_VALID;
  132. }
  133. csym->flags |= SYMBOL_DEF_USER;
  134. /* clear VALID to get value calculated */
  135. csym->flags &= ~SYMBOL_VALID;
  136. return true;
  137. }
  138. enum conf_def_mode {
  139. def_default,
  140. def_yes,
  141. def_mod,
  142. def_no,
  143. def_random
  144. };
  145. static bool conf_set_all_new_symbols(enum conf_def_mode mode)
  146. {
  147. struct symbol *sym, *csym;
  148. int i, cnt;
  149. /*
  150. * can't go as the default in switch-case below, otherwise gcc whines
  151. * about -Wmaybe-uninitialized
  152. */
  153. int pby = 50; /* probability of bool = y */
  154. int pty = 33; /* probability of tristate = y */
  155. int ptm = 33; /* probability of tristate = m */
  156. bool has_changed = false;
  157. if (mode == def_random) {
  158. int n, p[3];
  159. char *env = getenv("KCONFIG_PROBABILITY");
  160. n = 0;
  161. while (env && *env) {
  162. char *endp;
  163. int tmp = strtol(env, &endp, 10);
  164. if (tmp >= 0 && tmp <= 100) {
  165. p[n++] = tmp;
  166. } else {
  167. errno = ERANGE;
  168. perror("KCONFIG_PROBABILITY");
  169. exit(1);
  170. }
  171. env = (*endp == ':') ? endp + 1 : endp;
  172. if (n >= 3)
  173. break;
  174. }
  175. switch (n) {
  176. case 1:
  177. pby = p[0];
  178. ptm = pby / 2;
  179. pty = pby - ptm;
  180. break;
  181. case 2:
  182. pty = p[0];
  183. ptm = p[1];
  184. pby = pty + ptm;
  185. break;
  186. case 3:
  187. pby = p[0];
  188. pty = p[1];
  189. ptm = p[2];
  190. break;
  191. }
  192. if (pty + ptm > 100) {
  193. errno = ERANGE;
  194. perror("KCONFIG_PROBABILITY");
  195. exit(1);
  196. }
  197. }
  198. for_all_symbols(i, sym) {
  199. if (sym_has_value(sym) || sym->flags & SYMBOL_VALID)
  200. continue;
  201. switch (sym_get_type(sym)) {
  202. case S_BOOLEAN:
  203. case S_TRISTATE:
  204. has_changed = true;
  205. switch (mode) {
  206. case def_yes:
  207. sym->def[S_DEF_USER].tri = yes;
  208. break;
  209. case def_mod:
  210. sym->def[S_DEF_USER].tri = mod;
  211. break;
  212. case def_no:
  213. sym->def[S_DEF_USER].tri = no;
  214. break;
  215. case def_random:
  216. sym->def[S_DEF_USER].tri = no;
  217. cnt = rand() % 100;
  218. if (sym->type == S_TRISTATE) {
  219. if (cnt < pty)
  220. sym->def[S_DEF_USER].tri = yes;
  221. else if (cnt < pty + ptm)
  222. sym->def[S_DEF_USER].tri = mod;
  223. } else if (cnt < pby)
  224. sym->def[S_DEF_USER].tri = yes;
  225. break;
  226. default:
  227. continue;
  228. }
  229. if (!(sym_is_choice(sym) && mode == def_random))
  230. sym->flags |= SYMBOL_DEF_USER;
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. sym_clear_all_valid();
  237. /*
  238. * We have different type of choice blocks.
  239. * If curr.tri equals to mod then we can select several
  240. * choice symbols in one block.
  241. * In this case we do nothing.
  242. * If curr.tri equals yes then only one symbol can be
  243. * selected in a choice block and we set it to yes,
  244. * and the rest to no.
  245. */
  246. if (mode != def_random) {
  247. for_all_symbols(i, csym) {
  248. if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
  249. sym_is_choice_value(csym))
  250. csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
  251. }
  252. }
  253. for_all_symbols(i, csym) {
  254. if (sym_has_value(csym) || !sym_is_choice(csym))
  255. continue;
  256. sym_calc_value(csym);
  257. if (mode == def_random)
  258. has_changed |= randomize_choice_values(csym);
  259. else {
  260. set_all_choice_values(csym);
  261. has_changed = true;
  262. }
  263. }
  264. return has_changed;
  265. }
  266. static void conf_rewrite_tristates(tristate old_val, tristate new_val)
  267. {
  268. struct symbol *sym;
  269. int i;
  270. for_all_symbols(i, sym) {
  271. if (sym_get_type(sym) == S_TRISTATE &&
  272. sym->def[S_DEF_USER].tri == old_val)
  273. sym->def[S_DEF_USER].tri = new_val;
  274. }
  275. sym_clear_all_valid();
  276. }
  277. static int conf_askvalue(struct symbol *sym, const char *def)
  278. {
  279. if (!sym_has_value(sym))
  280. printf("(NEW) ");
  281. line[0] = '\n';
  282. line[1] = 0;
  283. if (!sym_is_changeable(sym)) {
  284. printf("%s\n", def);
  285. line[0] = '\n';
  286. line[1] = 0;
  287. return 0;
  288. }
  289. switch (input_mode) {
  290. case oldconfig:
  291. case syncconfig:
  292. if (sym_has_value(sym)) {
  293. printf("%s\n", def);
  294. return 0;
  295. }
  296. /* fall through */
  297. default:
  298. fflush(stdout);
  299. xfgets(line, sizeof(line), stdin);
  300. break;
  301. }
  302. return 1;
  303. }
  304. static int conf_string(struct menu *menu)
  305. {
  306. struct symbol *sym = menu->sym;
  307. const char *def;
  308. while (1) {
  309. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  310. printf("(%s) ", sym->name);
  311. def = sym_get_string_value(sym);
  312. if (def)
  313. printf("[%s] ", def);
  314. if (!conf_askvalue(sym, def))
  315. return 0;
  316. switch (line[0]) {
  317. case '\n':
  318. break;
  319. case '?':
  320. /* print help */
  321. if (line[1] == '\n') {
  322. print_help(menu);
  323. def = NULL;
  324. break;
  325. }
  326. /* fall through */
  327. default:
  328. line[strlen(line)-1] = 0;
  329. def = line;
  330. }
  331. if (def && sym_set_string_value(sym, def))
  332. return 0;
  333. }
  334. }
  335. static int conf_sym(struct menu *menu)
  336. {
  337. struct symbol *sym = menu->sym;
  338. tristate oldval, newval;
  339. while (1) {
  340. printf("%*s%s ", indent - 1, "", menu->prompt->text);
  341. if (sym->name)
  342. printf("(%s) ", sym->name);
  343. putchar('[');
  344. oldval = sym_get_tristate_value(sym);
  345. switch (oldval) {
  346. case no:
  347. putchar('N');
  348. break;
  349. case mod:
  350. putchar('M');
  351. break;
  352. case yes:
  353. putchar('Y');
  354. break;
  355. }
  356. if (oldval != no && sym_tristate_within_range(sym, no))
  357. printf("/n");
  358. if (oldval != mod && sym_tristate_within_range(sym, mod))
  359. printf("/m");
  360. if (oldval != yes && sym_tristate_within_range(sym, yes))
  361. printf("/y");
  362. printf("/?] ");
  363. if (!conf_askvalue(sym, sym_get_string_value(sym)))
  364. return 0;
  365. strip(line);
  366. switch (line[0]) {
  367. case 'n':
  368. case 'N':
  369. newval = no;
  370. if (!line[1] || !strcmp(&line[1], "o"))
  371. break;
  372. continue;
  373. case 'm':
  374. case 'M':
  375. newval = mod;
  376. if (!line[1])
  377. break;
  378. continue;
  379. case 'y':
  380. case 'Y':
  381. newval = yes;
  382. if (!line[1] || !strcmp(&line[1], "es"))
  383. break;
  384. continue;
  385. case 0:
  386. newval = oldval;
  387. break;
  388. case '?':
  389. goto help;
  390. default:
  391. continue;
  392. }
  393. if (sym_set_tristate_value(sym, newval))
  394. return 0;
  395. help:
  396. print_help(menu);
  397. }
  398. }
  399. static int conf_choice(struct menu *menu)
  400. {
  401. struct symbol *sym, *def_sym;
  402. struct menu *child;
  403. bool is_new;
  404. sym = menu->sym;
  405. is_new = !sym_has_value(sym);
  406. if (sym_is_changeable(sym)) {
  407. conf_sym(menu);
  408. sym_calc_value(sym);
  409. switch (sym_get_tristate_value(sym)) {
  410. case no:
  411. return 1;
  412. case mod:
  413. return 0;
  414. case yes:
  415. break;
  416. }
  417. } else {
  418. switch (sym_get_tristate_value(sym)) {
  419. case no:
  420. return 1;
  421. case mod:
  422. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  423. return 0;
  424. case yes:
  425. break;
  426. }
  427. }
  428. while (1) {
  429. int cnt, def;
  430. printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
  431. def_sym = sym_get_choice_value(sym);
  432. cnt = def = 0;
  433. line[0] = 0;
  434. for (child = menu->list; child; child = child->next) {
  435. if (!menu_is_visible(child))
  436. continue;
  437. if (!child->sym) {
  438. printf("%*c %s\n", indent, '*', menu_get_prompt(child));
  439. continue;
  440. }
  441. cnt++;
  442. if (child->sym == def_sym) {
  443. def = cnt;
  444. printf("%*c", indent, '>');
  445. } else
  446. printf("%*c", indent, ' ');
  447. printf(" %d. %s", cnt, menu_get_prompt(child));
  448. if (child->sym->name)
  449. printf(" (%s)", child->sym->name);
  450. if (!sym_has_value(child->sym))
  451. printf(" (NEW)");
  452. printf("\n");
  453. }
  454. printf("%*schoice", indent - 1, "");
  455. if (cnt == 1) {
  456. printf("[1]: 1\n");
  457. goto conf_childs;
  458. }
  459. printf("[1-%d?]: ", cnt);
  460. switch (input_mode) {
  461. case oldconfig:
  462. case syncconfig:
  463. if (!is_new) {
  464. cnt = def;
  465. printf("%d\n", cnt);
  466. break;
  467. }
  468. /* fall through */
  469. case oldaskconfig:
  470. fflush(stdout);
  471. xfgets(line, sizeof(line), stdin);
  472. strip(line);
  473. if (line[0] == '?') {
  474. print_help(menu);
  475. continue;
  476. }
  477. if (!line[0])
  478. cnt = def;
  479. else if (isdigit(line[0]))
  480. cnt = atoi(line);
  481. else
  482. continue;
  483. break;
  484. default:
  485. break;
  486. }
  487. conf_childs:
  488. for (child = menu->list; child; child = child->next) {
  489. if (!child->sym || !menu_is_visible(child))
  490. continue;
  491. if (!--cnt)
  492. break;
  493. }
  494. if (!child)
  495. continue;
  496. if (line[0] && line[strlen(line) - 1] == '?') {
  497. print_help(child);
  498. continue;
  499. }
  500. sym_set_tristate_value(child->sym, yes);
  501. for (child = child->list; child; child = child->next) {
  502. indent += 2;
  503. conf(child);
  504. indent -= 2;
  505. }
  506. return 1;
  507. }
  508. }
  509. static void conf(struct menu *menu)
  510. {
  511. struct symbol *sym;
  512. struct property *prop;
  513. struct menu *child;
  514. if (!menu_is_visible(menu))
  515. return;
  516. sym = menu->sym;
  517. prop = menu->prompt;
  518. if (prop) {
  519. const char *prompt;
  520. switch (prop->type) {
  521. case P_MENU:
  522. /*
  523. * Except in oldaskconfig mode, we show only menus that
  524. * contain new symbols.
  525. */
  526. if (input_mode != oldaskconfig && rootEntry != menu) {
  527. check_conf(menu);
  528. return;
  529. }
  530. /* fall through */
  531. case P_COMMENT:
  532. prompt = menu_get_prompt(menu);
  533. if (prompt)
  534. printf("%*c\n%*c %s\n%*c\n",
  535. indent, '*',
  536. indent, '*', prompt,
  537. indent, '*');
  538. default:
  539. ;
  540. }
  541. }
  542. if (!sym)
  543. goto conf_childs;
  544. if (sym_is_choice(sym)) {
  545. conf_choice(menu);
  546. if (sym->curr.tri != mod)
  547. return;
  548. goto conf_childs;
  549. }
  550. switch (sym->type) {
  551. case S_INT:
  552. case S_HEX:
  553. case S_STRING:
  554. conf_string(menu);
  555. break;
  556. default:
  557. conf_sym(menu);
  558. break;
  559. }
  560. conf_childs:
  561. if (sym)
  562. indent += 2;
  563. for (child = menu->list; child; child = child->next)
  564. conf(child);
  565. if (sym)
  566. indent -= 2;
  567. }
  568. static void check_conf(struct menu *menu)
  569. {
  570. struct symbol *sym;
  571. struct menu *child;
  572. if (!menu_is_visible(menu))
  573. return;
  574. sym = menu->sym;
  575. if (sym && !sym_has_value(sym) &&
  576. (sym_is_changeable(sym) ||
  577. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
  578. switch (input_mode) {
  579. case listnewconfig:
  580. if (sym->name)
  581. print_symbol_for_listconfig(sym);
  582. break;
  583. case helpnewconfig:
  584. printf("-----\n");
  585. print_help(menu);
  586. printf("-----\n");
  587. break;
  588. default:
  589. if (!conf_cnt++)
  590. printf("*\n* Restart config...\n*\n");
  591. rootEntry = menu_get_parent_menu(menu);
  592. conf(rootEntry);
  593. break;
  594. }
  595. }
  596. for (child = menu->list; child; child = child->next)
  597. check_conf(child);
  598. }
  599. static const struct option long_opts[] = {
  600. {"help", no_argument, NULL, 'h'},
  601. {"silent", no_argument, NULL, 's'},
  602. {"oldaskconfig", no_argument, &input_mode_opt, oldaskconfig},
  603. {"oldconfig", no_argument, &input_mode_opt, oldconfig},
  604. {"syncconfig", no_argument, &input_mode_opt, syncconfig},
  605. {"defconfig", required_argument, &input_mode_opt, defconfig},
  606. {"savedefconfig", required_argument, &input_mode_opt, savedefconfig},
  607. {"allnoconfig", no_argument, &input_mode_opt, allnoconfig},
  608. {"allyesconfig", no_argument, &input_mode_opt, allyesconfig},
  609. {"allmodconfig", no_argument, &input_mode_opt, allmodconfig},
  610. {"alldefconfig", no_argument, &input_mode_opt, alldefconfig},
  611. {"randconfig", no_argument, &input_mode_opt, randconfig},
  612. {"listnewconfig", no_argument, &input_mode_opt, listnewconfig},
  613. {"helpnewconfig", no_argument, &input_mode_opt, helpnewconfig},
  614. {"olddefconfig", no_argument, &input_mode_opt, olddefconfig},
  615. {"yes2modconfig", no_argument, &input_mode_opt, yes2modconfig},
  616. {"mod2yesconfig", no_argument, &input_mode_opt, mod2yesconfig},
  617. {"mod2noconfig", no_argument, &input_mode_opt, mod2noconfig},
  618. {NULL, 0, NULL, 0}
  619. };
  620. static void conf_usage(const char *progname)
  621. {
  622. printf("Usage: %s [options] <kconfig-file>\n", progname);
  623. printf("\n");
  624. printf("Generic options:\n");
  625. printf(" -h, --help Print this message and exit.\n");
  626. printf(" -s, --silent Do not print log.\n");
  627. printf("\n");
  628. printf("Mode options:\n");
  629. printf(" --listnewconfig List new options\n");
  630. printf(" --helpnewconfig List new options and help text\n");
  631. printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
  632. printf(" --oldconfig Update a configuration using a provided .config as base\n");
  633. printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
  634. " include/{generated/,config/}\n");
  635. printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
  636. printf(" --defconfig <file> New config with default defined in <file>\n");
  637. printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
  638. printf(" --allnoconfig New config where all options are answered with no\n");
  639. printf(" --allyesconfig New config where all options are answered with yes\n");
  640. printf(" --allmodconfig New config where all options are answered with mod\n");
  641. printf(" --alldefconfig New config with all symbols set to default\n");
  642. printf(" --randconfig New config with random answer to all options\n");
  643. printf(" --yes2modconfig Change answers from yes to mod if possible\n");
  644. printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
  645. printf(" --mod2noconfig Change answers from mod to no if possible\n");
  646. printf(" (If none of the above is given, --oldaskconfig is the default)\n");
  647. }
  648. int main(int ac, char **av)
  649. {
  650. const char *progname = av[0];
  651. int opt;
  652. const char *name, *defconfig_file = NULL /* gcc uninit */;
  653. int no_conf_write = 0;
  654. tty_stdio = isatty(0) && isatty(1);
  655. while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) {
  656. switch (opt) {
  657. case 'h':
  658. conf_usage(progname);
  659. exit(1);
  660. break;
  661. case 's':
  662. conf_set_message_callback(NULL);
  663. break;
  664. case 0:
  665. input_mode = input_mode_opt;
  666. switch (input_mode) {
  667. case syncconfig:
  668. /*
  669. * syncconfig is invoked during the build stage.
  670. * Suppress distracting
  671. * "configuration written to ..."
  672. */
  673. conf_set_message_callback(NULL);
  674. sync_kconfig = 1;
  675. break;
  676. case defconfig:
  677. case savedefconfig:
  678. defconfig_file = optarg;
  679. break;
  680. case randconfig:
  681. set_randconfig_seed();
  682. break;
  683. default:
  684. break;
  685. }
  686. default:
  687. break;
  688. }
  689. }
  690. if (ac == optind) {
  691. fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
  692. conf_usage(progname);
  693. exit(1);
  694. }
  695. conf_parse(av[optind]);
  696. //zconfdump(stdout);
  697. switch (input_mode) {
  698. case defconfig:
  699. if (conf_read(defconfig_file)) {
  700. fprintf(stderr,
  701. "***\n"
  702. "*** Can't find default configuration \"%s\"!\n"
  703. "***\n",
  704. defconfig_file);
  705. exit(1);
  706. }
  707. break;
  708. case savedefconfig:
  709. case syncconfig:
  710. case oldaskconfig:
  711. case oldconfig:
  712. case listnewconfig:
  713. case helpnewconfig:
  714. case olddefconfig:
  715. case yes2modconfig:
  716. case mod2yesconfig:
  717. case mod2noconfig:
  718. conf_read(NULL);
  719. break;
  720. case allnoconfig:
  721. case allyesconfig:
  722. case allmodconfig:
  723. case alldefconfig:
  724. case randconfig:
  725. name = getenv("KCONFIG_ALLCONFIG");
  726. if (!name)
  727. break;
  728. if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
  729. if (conf_read_simple(name, S_DEF_USER)) {
  730. fprintf(stderr,
  731. "*** Can't read seed configuration \"%s\"!\n",
  732. name);
  733. exit(1);
  734. }
  735. break;
  736. }
  737. switch (input_mode) {
  738. case allnoconfig: name = "allno.config"; break;
  739. case allyesconfig: name = "allyes.config"; break;
  740. case allmodconfig: name = "allmod.config"; break;
  741. case alldefconfig: name = "alldef.config"; break;
  742. case randconfig: name = "allrandom.config"; break;
  743. default: break;
  744. }
  745. if (conf_read_simple(name, S_DEF_USER) &&
  746. conf_read_simple("all.config", S_DEF_USER)) {
  747. fprintf(stderr,
  748. "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
  749. name);
  750. exit(1);
  751. }
  752. break;
  753. default:
  754. break;
  755. }
  756. if (sync_kconfig) {
  757. name = getenv("KCONFIG_NOSILENTUPDATE");
  758. if (name && *name) {
  759. if (conf_get_changed()) {
  760. fprintf(stderr,
  761. "\n*** The configuration requires explicit update.\n\n");
  762. return 1;
  763. }
  764. no_conf_write = 1;
  765. }
  766. }
  767. switch (input_mode) {
  768. case allnoconfig:
  769. conf_set_all_new_symbols(def_no);
  770. break;
  771. case allyesconfig:
  772. conf_set_all_new_symbols(def_yes);
  773. break;
  774. case allmodconfig:
  775. conf_set_all_new_symbols(def_mod);
  776. break;
  777. case alldefconfig:
  778. conf_set_all_new_symbols(def_default);
  779. break;
  780. case randconfig:
  781. /* Really nothing to do in this loop */
  782. while (conf_set_all_new_symbols(def_random)) ;
  783. break;
  784. case defconfig:
  785. conf_set_all_new_symbols(def_default);
  786. break;
  787. case savedefconfig:
  788. break;
  789. case yes2modconfig:
  790. conf_rewrite_tristates(yes, mod);
  791. break;
  792. case mod2yesconfig:
  793. conf_rewrite_tristates(mod, yes);
  794. break;
  795. case mod2noconfig:
  796. conf_rewrite_tristates(mod, no);
  797. break;
  798. case oldaskconfig:
  799. rootEntry = &rootmenu;
  800. conf(&rootmenu);
  801. input_mode = oldconfig;
  802. /* fall through */
  803. case oldconfig:
  804. case listnewconfig:
  805. case helpnewconfig:
  806. case syncconfig:
  807. /* Update until a loop caused no more changes */
  808. do {
  809. conf_cnt = 0;
  810. check_conf(&rootmenu);
  811. } while (conf_cnt);
  812. break;
  813. case olddefconfig:
  814. default:
  815. break;
  816. }
  817. if (input_mode == savedefconfig) {
  818. if (conf_write_defconfig(defconfig_file)) {
  819. fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
  820. defconfig_file);
  821. return 1;
  822. }
  823. } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
  824. if (!no_conf_write && conf_write(NULL)) {
  825. fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
  826. exit(1);
  827. }
  828. /*
  829. * Create auto.conf if it does not exist.
  830. * This prevents GNU Make 4.1 or older from emitting
  831. * "include/config/auto.conf: No such file or directory"
  832. * in the top-level Makefile
  833. *
  834. * syncconfig always creates or updates auto.conf because it is
  835. * used during the build.
  836. */
  837. if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
  838. fprintf(stderr,
  839. "\n*** Error during sync of the configuration.\n\n");
  840. return 1;
  841. }
  842. }
  843. return 0;
  844. }