conf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. #define LKC_DIRECT_LINK
  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. 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. char *defconfig_file;
  34. static int indent = 1;
  35. static int valid_stdin = 1;
  36. static int sync_kconfig;
  37. static int conf_cnt;
  38. static char line[128];
  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. static void check_stdin(void)
  63. {
  64. if (!valid_stdin) {
  65. printf(_("aborted!\n\n"));
  66. printf(_("Console input/output is redirected. "));
  67. printf(_("Run 'make oldconfig' to update configuration.\n\n"));
  68. exit(1);
  69. }
  70. }
  71. static int conf_askvalue(struct symbol *sym, const char *def)
  72. {
  73. enum symbol_type type = sym_get_type(sym);
  74. if (!sym_has_value(sym))
  75. printf(_("(NEW) "));
  76. line[0] = '\n';
  77. line[1] = 0;
  78. if (!sym_is_changable(sym)) {
  79. printf("%s\n", def);
  80. line[0] = '\n';
  81. line[1] = 0;
  82. return 0;
  83. }
  84. switch (input_mode) {
  85. case oldconfig:
  86. case silentoldconfig:
  87. if (sym_has_value(sym)) {
  88. printf("%s\n", def);
  89. return 0;
  90. }
  91. check_stdin();
  92. /* fall through */
  93. case oldaskconfig:
  94. fflush(stdout);
  95. xfgets(line, 128, stdin);
  96. return 1;
  97. default:
  98. break;
  99. }
  100. switch (type) {
  101. case S_INT:
  102. case S_HEX:
  103. case S_STRING:
  104. printf("%s\n", def);
  105. return 1;
  106. default:
  107. ;
  108. }
  109. printf("%s", line);
  110. return 1;
  111. }
  112. static int conf_string(struct menu *menu)
  113. {
  114. struct symbol *sym = menu->sym;
  115. const char *def;
  116. while (1) {
  117. printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
  118. printf("(%s) ", sym->name);
  119. def = sym_get_string_value(sym);
  120. if (sym_get_string_value(sym))
  121. printf("[%s] ", def);
  122. if (!conf_askvalue(sym, def))
  123. return 0;
  124. switch (line[0]) {
  125. case '\n':
  126. break;
  127. case '?':
  128. /* print help */
  129. if (line[1] == '\n') {
  130. print_help(menu);
  131. def = NULL;
  132. break;
  133. }
  134. /* fall through */
  135. default:
  136. line[strlen(line)-1] = 0;
  137. def = line;
  138. }
  139. if (def && sym_set_string_value(sym, def))
  140. return 0;
  141. }
  142. }
  143. static int conf_sym(struct menu *menu)
  144. {
  145. struct symbol *sym = menu->sym;
  146. tristate oldval, newval;
  147. while (1) {
  148. printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
  149. if (sym->name)
  150. printf("(%s) ", sym->name);
  151. putchar('[');
  152. oldval = sym_get_tristate_value(sym);
  153. switch (oldval) {
  154. case no:
  155. putchar('N');
  156. break;
  157. case mod:
  158. putchar('M');
  159. break;
  160. case yes:
  161. putchar('Y');
  162. break;
  163. }
  164. if (oldval != no && sym_tristate_within_range(sym, no))
  165. printf("/n");
  166. if (oldval != mod && sym_tristate_within_range(sym, mod))
  167. printf("/m");
  168. if (oldval != yes && sym_tristate_within_range(sym, yes))
  169. printf("/y");
  170. if (menu_has_help(menu))
  171. printf("/?");
  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_changable(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. if (menu_has_help(menu))
  271. printf("?");
  272. printf("]: ");
  273. switch (input_mode) {
  274. case oldconfig:
  275. case silentoldconfig:
  276. if (!is_new) {
  277. cnt = def;
  278. printf("%d\n", cnt);
  279. break;
  280. }
  281. check_stdin();
  282. /* fall through */
  283. case oldaskconfig:
  284. fflush(stdout);
  285. xfgets(line, 128, stdin);
  286. strip(line);
  287. if (line[0] == '?') {
  288. print_help(menu);
  289. continue;
  290. }
  291. if (!line[0])
  292. cnt = def;
  293. else if (isdigit(line[0]))
  294. cnt = atoi(line);
  295. else
  296. continue;
  297. break;
  298. default:
  299. break;
  300. }
  301. conf_childs:
  302. for (child = menu->list; child; child = child->next) {
  303. if (!child->sym || !menu_is_visible(child))
  304. continue;
  305. if (!--cnt)
  306. break;
  307. }
  308. if (!child)
  309. continue;
  310. if (line[0] && line[strlen(line) - 1] == '?') {
  311. print_help(child);
  312. continue;
  313. }
  314. sym_set_choice_value(sym, child->sym);
  315. for (child = child->list; child; child = child->next) {
  316. indent += 2;
  317. conf(child);
  318. indent -= 2;
  319. }
  320. return 1;
  321. }
  322. }
  323. static void conf(struct menu *menu)
  324. {
  325. struct symbol *sym;
  326. struct property *prop;
  327. struct menu *child;
  328. if (!menu_is_visible(menu))
  329. return;
  330. sym = menu->sym;
  331. prop = menu->prompt;
  332. if (prop) {
  333. const char *prompt;
  334. switch (prop->type) {
  335. case P_MENU:
  336. if ((input_mode == silentoldconfig ||
  337. input_mode == listnewconfig ||
  338. input_mode == oldnoconfig) &&
  339. rootEntry != menu) {
  340. check_conf(menu);
  341. return;
  342. }
  343. /* fall through */
  344. case P_COMMENT:
  345. prompt = menu_get_prompt(menu);
  346. if (prompt)
  347. printf("%*c\n%*c %s\n%*c\n",
  348. indent, '*',
  349. indent, '*', _(prompt),
  350. indent, '*');
  351. default:
  352. ;
  353. }
  354. }
  355. if (!sym)
  356. goto conf_childs;
  357. if (sym_is_choice(sym)) {
  358. conf_choice(menu);
  359. if (sym->curr.tri != mod)
  360. return;
  361. goto conf_childs;
  362. }
  363. switch (sym->type) {
  364. case S_INT:
  365. case S_HEX:
  366. case S_STRING:
  367. conf_string(menu);
  368. break;
  369. default:
  370. conf_sym(menu);
  371. break;
  372. }
  373. conf_childs:
  374. if (sym)
  375. indent += 2;
  376. for (child = menu->list; child; child = child->next)
  377. conf(child);
  378. if (sym)
  379. indent -= 2;
  380. }
  381. static void check_conf(struct menu *menu)
  382. {
  383. struct symbol *sym;
  384. struct menu *child;
  385. if (!menu_is_visible(menu))
  386. return;
  387. sym = menu->sym;
  388. if (sym && !sym_has_value(sym)) {
  389. if (sym_is_changable(sym) ||
  390. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
  391. if (input_mode == listnewconfig) {
  392. if (sym->name && !sym_is_choice_value(sym)) {
  393. printf("%s%s\n", CONFIG_, sym->name);
  394. }
  395. } else if (input_mode != oldnoconfig) {
  396. if (!conf_cnt++)
  397. printf(_("*\n* Restart config...\n*\n"));
  398. rootEntry = menu_get_parent_menu(menu);
  399. conf(rootEntry);
  400. }
  401. }
  402. }
  403. for (child = menu->list; child; child = child->next)
  404. check_conf(child);
  405. }
  406. static struct option long_opts[] = {
  407. {"oldaskconfig", no_argument, NULL, oldaskconfig},
  408. {"oldconfig", no_argument, NULL, oldconfig},
  409. {"silentoldconfig", no_argument, NULL, silentoldconfig},
  410. {"defconfig", optional_argument, NULL, defconfig},
  411. {"savedefconfig", required_argument, NULL, savedefconfig},
  412. {"allnoconfig", no_argument, NULL, allnoconfig},
  413. {"allyesconfig", no_argument, NULL, allyesconfig},
  414. {"allmodconfig", no_argument, NULL, allmodconfig},
  415. {"alldefconfig", no_argument, NULL, alldefconfig},
  416. {"randconfig", no_argument, NULL, randconfig},
  417. {"listnewconfig", no_argument, NULL, listnewconfig},
  418. {"oldnoconfig", no_argument, NULL, oldnoconfig},
  419. {NULL, 0, NULL, 0}
  420. };
  421. int main(int ac, char **av)
  422. {
  423. int opt;
  424. const char *name;
  425. struct stat tmpstat;
  426. setlocale(LC_ALL, "");
  427. bindtextdomain(PACKAGE, LOCALEDIR);
  428. textdomain(PACKAGE);
  429. while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
  430. input_mode = (enum input_mode)opt;
  431. switch (opt) {
  432. case silentoldconfig:
  433. sync_kconfig = 1;
  434. break;
  435. case defconfig:
  436. case savedefconfig:
  437. defconfig_file = optarg;
  438. break;
  439. case randconfig:
  440. {
  441. struct timeval now;
  442. unsigned int seed;
  443. /*
  444. * Use microseconds derived seed,
  445. * compensate for systems where it may be zero
  446. */
  447. gettimeofday(&now, NULL);
  448. seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
  449. srand(seed);
  450. break;
  451. }
  452. case '?':
  453. fprintf(stderr, _("See README for usage info\n"));
  454. exit(1);
  455. break;
  456. }
  457. }
  458. if (ac == optind) {
  459. printf(_("%s: Kconfig file missing\n"), av[0]);
  460. exit(1);
  461. }
  462. name = av[optind];
  463. conf_parse(name);
  464. //zconfdump(stdout);
  465. if (sync_kconfig) {
  466. name = conf_get_configname();
  467. if (stat(name, &tmpstat)) {
  468. fprintf(stderr, _("***\n"
  469. "*** Configuration file \"%s\" not found!\n"
  470. "***\n"
  471. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  472. "*** \"make menuconfig\" or \"make xconfig\").\n"
  473. "***\n"), name);
  474. exit(1);
  475. }
  476. }
  477. switch (input_mode) {
  478. case defconfig:
  479. if (!defconfig_file)
  480. defconfig_file = conf_get_default_confname();
  481. if (conf_read(defconfig_file)) {
  482. printf(_("***\n"
  483. "*** Can't find default configuration \"%s\"!\n"
  484. "***\n"), defconfig_file);
  485. exit(1);
  486. }
  487. break;
  488. case savedefconfig:
  489. case silentoldconfig:
  490. case oldaskconfig:
  491. case oldconfig:
  492. case listnewconfig:
  493. case oldnoconfig:
  494. conf_read(NULL);
  495. break;
  496. case allnoconfig:
  497. case allyesconfig:
  498. case allmodconfig:
  499. case alldefconfig:
  500. case randconfig:
  501. name = getenv("KCONFIG_ALLCONFIG");
  502. if (name && !stat(name, &tmpstat)) {
  503. conf_read_simple(name, S_DEF_USER);
  504. break;
  505. }
  506. switch (input_mode) {
  507. case allnoconfig: name = "allno.config"; break;
  508. case allyesconfig: name = "allyes.config"; break;
  509. case allmodconfig: name = "allmod.config"; break;
  510. case alldefconfig: name = "alldef.config"; break;
  511. case randconfig: name = "allrandom.config"; break;
  512. default: break;
  513. }
  514. if (!stat(name, &tmpstat))
  515. conf_read_simple(name, S_DEF_USER);
  516. else if (!stat("all.config", &tmpstat))
  517. conf_read_simple("all.config", S_DEF_USER);
  518. break;
  519. default:
  520. break;
  521. }
  522. if (sync_kconfig) {
  523. if (conf_get_changed()) {
  524. name = getenv("KCONFIG_NOSILENTUPDATE");
  525. if (name && *name) {
  526. fprintf(stderr,
  527. _("\n*** The configuration requires explicit update.\n\n"));
  528. return 1;
  529. }
  530. }
  531. valid_stdin = isatty(0) && isatty(1) && isatty(2);
  532. }
  533. switch (input_mode) {
  534. case allnoconfig:
  535. conf_set_all_new_symbols(def_no);
  536. break;
  537. case allyesconfig:
  538. conf_set_all_new_symbols(def_yes);
  539. break;
  540. case allmodconfig:
  541. conf_set_all_new_symbols(def_mod);
  542. break;
  543. case alldefconfig:
  544. conf_set_all_new_symbols(def_default);
  545. break;
  546. case randconfig:
  547. conf_set_all_new_symbols(def_random);
  548. break;
  549. case defconfig:
  550. conf_set_all_new_symbols(def_default);
  551. break;
  552. case savedefconfig:
  553. break;
  554. case oldaskconfig:
  555. rootEntry = &rootmenu;
  556. conf(&rootmenu);
  557. input_mode = silentoldconfig;
  558. /* fall through */
  559. case oldconfig:
  560. case listnewconfig:
  561. case oldnoconfig:
  562. case silentoldconfig:
  563. /* Update until a loop caused no more changes */
  564. do {
  565. conf_cnt = 0;
  566. check_conf(&rootmenu);
  567. } while (conf_cnt &&
  568. (input_mode != listnewconfig &&
  569. input_mode != oldnoconfig));
  570. break;
  571. }
  572. if (sync_kconfig) {
  573. /* silentoldconfig is used during the build so we shall update autoconf.
  574. * All other commands are only used to generate a config.
  575. */
  576. if (conf_get_changed() && conf_write(NULL)) {
  577. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  578. exit(1);
  579. }
  580. if (conf_write_autoconf()) {
  581. fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
  582. return 1;
  583. }
  584. } else if (input_mode == savedefconfig) {
  585. if (conf_write_defconfig(defconfig_file)) {
  586. fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
  587. defconfig_file);
  588. return 1;
  589. }
  590. } else if (input_mode != listnewconfig) {
  591. if (conf_write(NULL)) {
  592. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  593. exit(1);
  594. }
  595. }
  596. return 0;
  597. }
  598. /*
  599. * Helper function to facilitate fgets() by Jean Sacren.
  600. */
  601. void xfgets(str, size, in)
  602. char *str;
  603. int size;
  604. FILE *in;
  605. {
  606. if (fgets(str, size, in) == NULL)
  607. fprintf(stderr, "\nError in reading or end of file.\n");
  608. }