conf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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 <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 "lkc.h"
  16. static void conf(struct menu *menu);
  17. static void check_conf(struct menu *menu);
  18. static void xfgets(char *str, int size, FILE *in);
  19. enum input_mode {
  20. oldaskconfig,
  21. silentoldconfig,
  22. oldconfig,
  23. allnoconfig,
  24. allyesconfig,
  25. allmodconfig,
  26. alldefconfig,
  27. randconfig,
  28. defconfig,
  29. savedefconfig,
  30. listnewconfig,
  31. oldnoconfig,
  32. } input_mode = oldaskconfig;
  33. static int indent = 1;
  34. static int valid_stdin = 1;
  35. static int sync_kconfig;
  36. static int conf_cnt;
  37. static char line[128];
  38. static struct menu *rootEntry;
  39. static void print_help(struct menu *menu)
  40. {
  41. struct gstr help = str_new();
  42. menu_get_ext_help(menu, &help);
  43. printf("\n%s\n", str_get(&help));
  44. str_free(&help);
  45. }
  46. static void strip(char *str)
  47. {
  48. char *p = str;
  49. int l;
  50. while ((isspace(*p)))
  51. p++;
  52. l = strlen(p);
  53. if (p != str)
  54. memmove(str, p, l + 1);
  55. if (!l)
  56. return;
  57. p = str + l - 1;
  58. while ((isspace(*p)))
  59. *p-- = 0;
  60. }
  61. static void check_stdin(void)
  62. {
  63. if (!valid_stdin) {
  64. printf(_("aborted!\n\n"));
  65. printf(_("Console input/output is redirected. "));
  66. printf(_("Run 'make oldconfig' to update configuration.\n\n"));
  67. exit(1);
  68. }
  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 silentoldconfig:
  86. if (sym_has_value(sym)) {
  87. printf("%s\n", def);
  88. return 0;
  89. }
  90. check_stdin();
  91. /* fall through */
  92. case oldaskconfig:
  93. fflush(stdout);
  94. xfgets(line, 128, 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. if (menu_has_help(menu))
  170. printf("/?");
  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_changable(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. if (menu_has_help(menu))
  270. printf("?");
  271. printf("]: ");
  272. switch (input_mode) {
  273. case oldconfig:
  274. case silentoldconfig:
  275. if (!is_new) {
  276. cnt = def;
  277. printf("%d\n", cnt);
  278. break;
  279. }
  280. check_stdin();
  281. /* fall through */
  282. case oldaskconfig:
  283. fflush(stdout);
  284. xfgets(line, 128, stdin);
  285. strip(line);
  286. if (line[0] == '?') {
  287. print_help(menu);
  288. continue;
  289. }
  290. if (!line[0])
  291. cnt = def;
  292. else if (isdigit(line[0]))
  293. cnt = atoi(line);
  294. else
  295. continue;
  296. break;
  297. default:
  298. break;
  299. }
  300. conf_childs:
  301. for (child = menu->list; child; child = child->next) {
  302. if (!child->sym || !menu_is_visible(child))
  303. continue;
  304. if (!--cnt)
  305. break;
  306. }
  307. if (!child)
  308. continue;
  309. if (line[0] && line[strlen(line) - 1] == '?') {
  310. print_help(child);
  311. continue;
  312. }
  313. sym_set_choice_value(sym, child->sym);
  314. for (child = child->list; child; child = child->next) {
  315. indent += 2;
  316. conf(child);
  317. indent -= 2;
  318. }
  319. return 1;
  320. }
  321. }
  322. static void conf(struct menu *menu)
  323. {
  324. struct symbol *sym;
  325. struct property *prop;
  326. struct menu *child;
  327. if (!menu_is_visible(menu))
  328. return;
  329. sym = menu->sym;
  330. prop = menu->prompt;
  331. if (prop) {
  332. const char *prompt;
  333. switch (prop->type) {
  334. case P_MENU:
  335. if ((input_mode == silentoldconfig ||
  336. input_mode == listnewconfig ||
  337. input_mode == oldnoconfig) &&
  338. rootEntry != menu) {
  339. check_conf(menu);
  340. return;
  341. }
  342. /* fall through */
  343. case P_COMMENT:
  344. prompt = menu_get_prompt(menu);
  345. if (prompt)
  346. printf("%*c\n%*c %s\n%*c\n",
  347. indent, '*',
  348. indent, '*', _(prompt),
  349. indent, '*');
  350. default:
  351. ;
  352. }
  353. }
  354. if (!sym)
  355. goto conf_childs;
  356. if (sym_is_choice(sym)) {
  357. conf_choice(menu);
  358. if (sym->curr.tri != mod)
  359. return;
  360. goto conf_childs;
  361. }
  362. switch (sym->type) {
  363. case S_INT:
  364. case S_HEX:
  365. case S_STRING:
  366. conf_string(menu);
  367. break;
  368. default:
  369. conf_sym(menu);
  370. break;
  371. }
  372. conf_childs:
  373. if (sym)
  374. indent += 2;
  375. for (child = menu->list; child; child = child->next)
  376. conf(child);
  377. if (sym)
  378. indent -= 2;
  379. }
  380. static void check_conf(struct menu *menu)
  381. {
  382. struct symbol *sym;
  383. struct menu *child;
  384. if (!menu_is_visible(menu))
  385. return;
  386. sym = menu->sym;
  387. if (sym && !sym_has_value(sym)) {
  388. if (sym_is_changable(sym) ||
  389. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
  390. if (input_mode == listnewconfig) {
  391. if (sym->name && !sym_is_choice_value(sym)) {
  392. printf("%s%s\n", CONFIG_, sym->name);
  393. }
  394. } else if (input_mode != oldnoconfig) {
  395. if (!conf_cnt++)
  396. printf(_("*\n* Restart config...\n*\n"));
  397. rootEntry = menu_get_parent_menu(menu);
  398. conf(rootEntry);
  399. }
  400. }
  401. }
  402. for (child = menu->list; child; child = child->next)
  403. check_conf(child);
  404. }
  405. static struct option long_opts[] = {
  406. {"oldaskconfig", no_argument, NULL, oldaskconfig},
  407. {"oldconfig", no_argument, NULL, oldconfig},
  408. {"silentoldconfig", no_argument, NULL, silentoldconfig},
  409. {"defconfig", optional_argument, NULL, defconfig},
  410. {"savedefconfig", required_argument, NULL, savedefconfig},
  411. {"allnoconfig", no_argument, NULL, allnoconfig},
  412. {"allyesconfig", no_argument, NULL, allyesconfig},
  413. {"allmodconfig", no_argument, NULL, allmodconfig},
  414. {"alldefconfig", no_argument, NULL, alldefconfig},
  415. {"randconfig", no_argument, NULL, randconfig},
  416. {"listnewconfig", no_argument, NULL, listnewconfig},
  417. {"oldnoconfig", no_argument, NULL, oldnoconfig},
  418. {NULL, 0, NULL, 0}
  419. };
  420. static void conf_usage(const char *progname)
  421. {
  422. printf("Usage: %s [option] <kconfig-file>\n", progname);
  423. printf("[option] is _one_ of the following:\n");
  424. printf(" --listnewconfig List new options\n");
  425. printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
  426. printf(" --oldconfig Update a configuration using a provided .config as base\n");
  427. printf(" --silentoldconfig Same as oldconfig, but quietly, additionally update deps\n");
  428. printf(" --oldnoconfig Same as silentoldconfig but set new symbols to no\n");
  429. printf(" --defconfig <file> New config with default defined in <file>\n");
  430. printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
  431. printf(" --allnoconfig New config where all options are answered with no\n");
  432. printf(" --allyesconfig New config where all options are answered with yes\n");
  433. printf(" --allmodconfig New config where all options are answered with mod\n");
  434. printf(" --alldefconfig New config with all symbols set to default\n");
  435. printf(" --randconfig New config with random answer to all options\n");
  436. }
  437. int main(int ac, char **av)
  438. {
  439. const char *progname = av[0];
  440. int opt;
  441. const char *name, *defconfig_file = NULL /* gcc uninit */;
  442. struct stat tmpstat;
  443. setlocale(LC_ALL, "");
  444. bindtextdomain(PACKAGE, LOCALEDIR);
  445. textdomain(PACKAGE);
  446. while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
  447. input_mode = (enum input_mode)opt;
  448. switch (opt) {
  449. case silentoldconfig:
  450. sync_kconfig = 1;
  451. break;
  452. case defconfig:
  453. case savedefconfig:
  454. defconfig_file = optarg;
  455. break;
  456. case randconfig:
  457. {
  458. struct timeval now;
  459. unsigned int seed;
  460. /*
  461. * Use microseconds derived seed,
  462. * compensate for systems where it may be zero
  463. */
  464. gettimeofday(&now, NULL);
  465. seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
  466. srand(seed);
  467. break;
  468. }
  469. case oldaskconfig:
  470. case oldconfig:
  471. case allnoconfig:
  472. case allyesconfig:
  473. case allmodconfig:
  474. case alldefconfig:
  475. case listnewconfig:
  476. case oldnoconfig:
  477. break;
  478. case '?':
  479. conf_usage(progname);
  480. exit(1);
  481. break;
  482. }
  483. }
  484. if (ac == optind) {
  485. printf(_("%s: Kconfig file missing\n"), av[0]);
  486. conf_usage(progname);
  487. exit(1);
  488. }
  489. name = av[optind];
  490. conf_parse(name);
  491. //zconfdump(stdout);
  492. if (sync_kconfig) {
  493. name = conf_get_configname();
  494. if (stat(name, &tmpstat)) {
  495. fprintf(stderr, _("***\n"
  496. "*** Configuration file \"%s\" not found!\n"
  497. "***\n"
  498. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  499. "*** \"make menuconfig\" or \"make xconfig\").\n"
  500. "***\n"), name);
  501. exit(1);
  502. }
  503. }
  504. switch (input_mode) {
  505. case defconfig:
  506. if (!defconfig_file)
  507. defconfig_file = conf_get_default_confname();
  508. if (conf_read(defconfig_file)) {
  509. printf(_("***\n"
  510. "*** Can't find default configuration \"%s\"!\n"
  511. "***\n"), defconfig_file);
  512. exit(1);
  513. }
  514. break;
  515. case savedefconfig:
  516. case silentoldconfig:
  517. case oldaskconfig:
  518. case oldconfig:
  519. case listnewconfig:
  520. case oldnoconfig:
  521. conf_read(NULL);
  522. break;
  523. case allnoconfig:
  524. case allyesconfig:
  525. case allmodconfig:
  526. case alldefconfig:
  527. case randconfig:
  528. name = getenv("KCONFIG_ALLCONFIG");
  529. if (name && !stat(name, &tmpstat)) {
  530. conf_read_simple(name, S_DEF_USER, true);
  531. break;
  532. }
  533. switch (input_mode) {
  534. case allnoconfig: name = "allno.config"; break;
  535. case allyesconfig: name = "allyes.config"; break;
  536. case allmodconfig: name = "allmod.config"; break;
  537. case alldefconfig: name = "alldef.config"; break;
  538. case randconfig: name = "allrandom.config"; break;
  539. default: break;
  540. }
  541. if (!stat(name, &tmpstat))
  542. conf_read_simple(name, S_DEF_USER, true);
  543. else if (!stat("all.config", &tmpstat))
  544. conf_read_simple("all.config", S_DEF_USER, true);
  545. break;
  546. default:
  547. break;
  548. }
  549. if (sync_kconfig) {
  550. if (conf_get_changed()) {
  551. name = getenv("KCONFIG_NOSILENTUPDATE");
  552. if (name && *name) {
  553. fprintf(stderr,
  554. _("\n*** The configuration requires explicit update.\n\n"));
  555. return 1;
  556. }
  557. }
  558. valid_stdin = isatty(0) && isatty(1) && isatty(2);
  559. }
  560. switch (input_mode) {
  561. case allnoconfig:
  562. conf_set_all_new_symbols(def_no);
  563. break;
  564. case allyesconfig:
  565. conf_set_all_new_symbols(def_yes);
  566. break;
  567. case allmodconfig:
  568. conf_set_all_new_symbols(def_mod);
  569. break;
  570. case alldefconfig:
  571. conf_set_all_new_symbols(def_default);
  572. break;
  573. case randconfig:
  574. conf_set_all_new_symbols(def_random);
  575. break;
  576. case defconfig:
  577. conf_set_all_new_symbols(def_default);
  578. break;
  579. case savedefconfig:
  580. break;
  581. case oldaskconfig:
  582. rootEntry = &rootmenu;
  583. conf(&rootmenu);
  584. input_mode = silentoldconfig;
  585. /* fall through */
  586. case oldconfig:
  587. case listnewconfig:
  588. case oldnoconfig:
  589. case silentoldconfig:
  590. /* Update until a loop caused no more changes */
  591. do {
  592. conf_cnt = 0;
  593. check_conf(&rootmenu);
  594. } while (conf_cnt &&
  595. (input_mode != listnewconfig &&
  596. input_mode != oldnoconfig));
  597. break;
  598. }
  599. if (sync_kconfig) {
  600. /* silentoldconfig is used during the build so we shall update autoconf.
  601. * All other commands are only used to generate a config.
  602. */
  603. if (conf_get_changed() && conf_write(NULL)) {
  604. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  605. exit(1);
  606. }
  607. if (conf_write_autoconf()) {
  608. fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
  609. return 1;
  610. }
  611. } else if (input_mode == savedefconfig) {
  612. if (conf_write_defconfig(defconfig_file)) {
  613. fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
  614. defconfig_file);
  615. return 1;
  616. }
  617. } else if (input_mode != listnewconfig) {
  618. if (conf_write(NULL)) {
  619. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  620. exit(1);
  621. }
  622. }
  623. return 0;
  624. }
  625. /*
  626. * Helper function to facilitate fgets() by Jean Sacren.
  627. */
  628. void xfgets(char *str, int size, FILE *in)
  629. {
  630. if (fgets(str, size, in) == NULL)
  631. fprintf(stderr, "\nError in reading or end of file.\n");
  632. }