conf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. {NULL, 0, NULL, 0}
  422. };
  423. static void conf_usage(const char *progname)
  424. {
  425. printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
  426. printf("[option] is _one_ of the following:\n");
  427. printf(" --listnewconfig List new options\n");
  428. printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
  429. printf(" --oldconfig Update a configuration using a provided .config as base\n");
  430. printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
  431. " include/{generated/,config/}\n");
  432. printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
  433. printf(" --defconfig <file> New config with default defined in <file>\n");
  434. printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
  435. printf(" --allnoconfig New config where all options are answered with no\n");
  436. printf(" --allyesconfig New config where all options are answered with yes\n");
  437. printf(" --allmodconfig New config where all options are answered with mod\n");
  438. printf(" --alldefconfig New config with all symbols set to default\n");
  439. printf(" --randconfig New config with random answer to all options\n");
  440. }
  441. int main(int ac, char **av)
  442. {
  443. const char *progname = av[0];
  444. int opt;
  445. const char *name, *defconfig_file = NULL /* gcc uninit */;
  446. struct stat tmpstat;
  447. int no_conf_write = 0;
  448. tty_stdio = isatty(0) && isatty(1);
  449. while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
  450. if (opt == 's') {
  451. conf_set_message_callback(NULL);
  452. continue;
  453. }
  454. input_mode = (enum input_mode)opt;
  455. switch (opt) {
  456. case syncconfig:
  457. /*
  458. * syncconfig is invoked during the build stage.
  459. * Suppress distracting "configuration written to ..."
  460. */
  461. conf_set_message_callback(NULL);
  462. sync_kconfig = 1;
  463. break;
  464. case defconfig:
  465. case savedefconfig:
  466. defconfig_file = optarg;
  467. break;
  468. case randconfig:
  469. {
  470. struct timeval now;
  471. unsigned int seed;
  472. char *seed_env;
  473. /*
  474. * Use microseconds derived seed,
  475. * compensate for systems where it may be zero
  476. */
  477. gettimeofday(&now, NULL);
  478. seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
  479. seed_env = getenv("KCONFIG_SEED");
  480. if( seed_env && *seed_env ) {
  481. char *endp;
  482. int tmp = (int)strtol(seed_env, &endp, 0);
  483. if (*endp == '\0') {
  484. seed = tmp;
  485. }
  486. }
  487. fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
  488. srand(seed);
  489. break;
  490. }
  491. case oldaskconfig:
  492. case oldconfig:
  493. case allnoconfig:
  494. case allyesconfig:
  495. case allmodconfig:
  496. case alldefconfig:
  497. case listnewconfig:
  498. case olddefconfig:
  499. break;
  500. case '?':
  501. conf_usage(progname);
  502. exit(1);
  503. break;
  504. }
  505. }
  506. if (ac == optind) {
  507. fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
  508. conf_usage(progname);
  509. exit(1);
  510. }
  511. name = av[optind];
  512. conf_parse(name);
  513. //zconfdump(stdout);
  514. if (sync_kconfig) {
  515. name = conf_get_configname();
  516. if (stat(name, &tmpstat)) {
  517. fprintf(stderr, "***\n"
  518. "*** Configuration file \"%s\" not found!\n"
  519. "***\n"
  520. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  521. "*** \"make menuconfig\" or \"make xconfig\").\n"
  522. "***\n", name);
  523. exit(1);
  524. }
  525. }
  526. switch (input_mode) {
  527. case defconfig:
  528. if (!defconfig_file)
  529. defconfig_file = conf_get_default_confname();
  530. if (conf_read(defconfig_file)) {
  531. fprintf(stderr,
  532. "***\n"
  533. "*** Can't find default configuration \"%s\"!\n"
  534. "***\n",
  535. defconfig_file);
  536. exit(1);
  537. }
  538. break;
  539. case savedefconfig:
  540. case syncconfig:
  541. case oldaskconfig:
  542. case oldconfig:
  543. case listnewconfig:
  544. case olddefconfig:
  545. conf_read(NULL);
  546. break;
  547. case allnoconfig:
  548. case allyesconfig:
  549. case allmodconfig:
  550. case alldefconfig:
  551. case randconfig:
  552. name = getenv("KCONFIG_ALLCONFIG");
  553. if (!name)
  554. break;
  555. if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
  556. if (conf_read_simple(name, S_DEF_USER)) {
  557. fprintf(stderr,
  558. "*** Can't read seed configuration \"%s\"!\n",
  559. name);
  560. exit(1);
  561. }
  562. break;
  563. }
  564. switch (input_mode) {
  565. case allnoconfig: name = "allno.config"; break;
  566. case allyesconfig: name = "allyes.config"; break;
  567. case allmodconfig: name = "allmod.config"; break;
  568. case alldefconfig: name = "alldef.config"; break;
  569. case randconfig: name = "allrandom.config"; break;
  570. default: break;
  571. }
  572. if (conf_read_simple(name, S_DEF_USER) &&
  573. conf_read_simple("all.config", S_DEF_USER)) {
  574. fprintf(stderr,
  575. "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
  576. name);
  577. exit(1);
  578. }
  579. break;
  580. default:
  581. break;
  582. }
  583. if (sync_kconfig) {
  584. name = getenv("KCONFIG_NOSILENTUPDATE");
  585. if (name && *name) {
  586. if (conf_get_changed()) {
  587. fprintf(stderr,
  588. "\n*** The configuration requires explicit update.\n\n");
  589. return 1;
  590. }
  591. no_conf_write = 1;
  592. }
  593. }
  594. switch (input_mode) {
  595. case allnoconfig:
  596. conf_set_all_new_symbols(def_no);
  597. break;
  598. case allyesconfig:
  599. conf_set_all_new_symbols(def_yes);
  600. break;
  601. case allmodconfig:
  602. conf_set_all_new_symbols(def_mod);
  603. break;
  604. case alldefconfig:
  605. conf_set_all_new_symbols(def_default);
  606. break;
  607. case randconfig:
  608. /* Really nothing to do in this loop */
  609. while (conf_set_all_new_symbols(def_random)) ;
  610. break;
  611. case defconfig:
  612. conf_set_all_new_symbols(def_default);
  613. break;
  614. case savedefconfig:
  615. break;
  616. case oldaskconfig:
  617. rootEntry = &rootmenu;
  618. conf(&rootmenu);
  619. input_mode = oldconfig;
  620. /* fall through */
  621. case oldconfig:
  622. case listnewconfig:
  623. case syncconfig:
  624. /* Update until a loop caused no more changes */
  625. do {
  626. conf_cnt = 0;
  627. check_conf(&rootmenu);
  628. } while (conf_cnt);
  629. break;
  630. case olddefconfig:
  631. default:
  632. break;
  633. }
  634. if (sync_kconfig) {
  635. /* syncconfig is used during the build so we shall update autoconf.
  636. * All other commands are only used to generate a config.
  637. */
  638. if (!no_conf_write && conf_write(NULL)) {
  639. fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
  640. exit(1);
  641. }
  642. if (conf_write_autoconf()) {
  643. fprintf(stderr, "\n*** Error during update of the configuration.\n\n");
  644. return 1;
  645. }
  646. } else if (input_mode == savedefconfig) {
  647. if (conf_write_defconfig(defconfig_file)) {
  648. fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
  649. defconfig_file);
  650. return 1;
  651. }
  652. } else if (input_mode != listnewconfig) {
  653. if (conf_write(NULL)) {
  654. fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
  655. exit(1);
  656. }
  657. }
  658. return 0;
  659. }