conf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. enum input_mode {
  19. oldaskconfig,
  20. silentoldconfig,
  21. oldconfig,
  22. allnoconfig,
  23. allyesconfig,
  24. allmodconfig,
  25. alldefconfig,
  26. randconfig,
  27. defconfig,
  28. savedefconfig,
  29. listnewconfig,
  30. oldnoconfig,
  31. } input_mode = oldaskconfig;
  32. static int indent = 1;
  33. static int valid_stdin = 1;
  34. static int sync_kconfig;
  35. static int conf_cnt;
  36. static char line[128];
  37. static struct menu *rootEntry;
  38. static void print_help(struct menu *menu)
  39. {
  40. struct gstr help = str_new();
  41. menu_get_ext_help(menu, &help);
  42. printf("\n%s\n", str_get(&help));
  43. str_free(&help);
  44. }
  45. static void strip(char *str)
  46. {
  47. char *p = str;
  48. int l;
  49. while ((isspace(*p)))
  50. p++;
  51. l = strlen(p);
  52. if (p != str)
  53. memmove(str, p, l + 1);
  54. if (!l)
  55. return;
  56. p = str + l - 1;
  57. while ((isspace(*p)))
  58. *p-- = 0;
  59. }
  60. static void check_stdin(void)
  61. {
  62. if (!valid_stdin) {
  63. printf(_("aborted!\n\n"));
  64. printf(_("Console input/output is redirected. "));
  65. printf(_("Run 'make oldconfig' to update configuration.\n\n"));
  66. exit(1);
  67. }
  68. }
  69. static int conf_askvalue(struct symbol *sym, const char *def)
  70. {
  71. enum symbol_type type = sym_get_type(sym);
  72. if (!sym_has_value(sym))
  73. printf(_("(NEW) "));
  74. line[0] = '\n';
  75. line[1] = 0;
  76. if (!sym_is_changable(sym)) {
  77. printf("%s\n", def);
  78. line[0] = '\n';
  79. line[1] = 0;
  80. return 0;
  81. }
  82. switch (input_mode) {
  83. case oldconfig:
  84. case silentoldconfig:
  85. if (sym_has_value(sym)) {
  86. printf("%s\n", def);
  87. return 0;
  88. }
  89. check_stdin();
  90. /* fall through */
  91. case oldaskconfig:
  92. fflush(stdout);
  93. xfgets(line, 128, 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. if (menu_has_help(menu))
  169. printf("/?");
  170. printf("] ");
  171. if (!conf_askvalue(sym, sym_get_string_value(sym)))
  172. return 0;
  173. strip(line);
  174. switch (line[0]) {
  175. case 'n':
  176. case 'N':
  177. newval = no;
  178. if (!line[1] || !strcmp(&line[1], "o"))
  179. break;
  180. continue;
  181. case 'm':
  182. case 'M':
  183. newval = mod;
  184. if (!line[1])
  185. break;
  186. continue;
  187. case 'y':
  188. case 'Y':
  189. newval = yes;
  190. if (!line[1] || !strcmp(&line[1], "es"))
  191. break;
  192. continue;
  193. case 0:
  194. newval = oldval;
  195. break;
  196. case '?':
  197. goto help;
  198. default:
  199. continue;
  200. }
  201. if (sym_set_tristate_value(sym, newval))
  202. return 0;
  203. help:
  204. print_help(menu);
  205. }
  206. }
  207. static int conf_choice(struct menu *menu)
  208. {
  209. struct symbol *sym, *def_sym;
  210. struct menu *child;
  211. bool is_new;
  212. sym = menu->sym;
  213. is_new = !sym_has_value(sym);
  214. if (sym_is_changable(sym)) {
  215. conf_sym(menu);
  216. sym_calc_value(sym);
  217. switch (sym_get_tristate_value(sym)) {
  218. case no:
  219. return 1;
  220. case mod:
  221. return 0;
  222. case yes:
  223. break;
  224. }
  225. } else {
  226. switch (sym_get_tristate_value(sym)) {
  227. case no:
  228. return 1;
  229. case mod:
  230. printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
  231. return 0;
  232. case yes:
  233. break;
  234. }
  235. }
  236. while (1) {
  237. int cnt, def;
  238. printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
  239. def_sym = sym_get_choice_value(sym);
  240. cnt = def = 0;
  241. line[0] = 0;
  242. for (child = menu->list; child; child = child->next) {
  243. if (!menu_is_visible(child))
  244. continue;
  245. if (!child->sym) {
  246. printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
  247. continue;
  248. }
  249. cnt++;
  250. if (child->sym == def_sym) {
  251. def = cnt;
  252. printf("%*c", indent, '>');
  253. } else
  254. printf("%*c", indent, ' ');
  255. printf(" %d. %s", cnt, _(menu_get_prompt(child)));
  256. if (child->sym->name)
  257. printf(" (%s)", child->sym->name);
  258. if (!sym_has_value(child->sym))
  259. printf(_(" (NEW)"));
  260. printf("\n");
  261. }
  262. printf(_("%*schoice"), indent - 1, "");
  263. if (cnt == 1) {
  264. printf("[1]: 1\n");
  265. goto conf_childs;
  266. }
  267. printf("[1-%d", cnt);
  268. if (menu_has_help(menu))
  269. printf("?");
  270. printf("]: ");
  271. switch (input_mode) {
  272. case oldconfig:
  273. case silentoldconfig:
  274. if (!is_new) {
  275. cnt = def;
  276. printf("%d\n", cnt);
  277. break;
  278. }
  279. check_stdin();
  280. /* fall through */
  281. case oldaskconfig:
  282. fflush(stdout);
  283. xfgets(line, 128, stdin);
  284. strip(line);
  285. if (line[0] == '?') {
  286. print_help(menu);
  287. continue;
  288. }
  289. if (!line[0])
  290. cnt = def;
  291. else if (isdigit(line[0]))
  292. cnt = atoi(line);
  293. else
  294. continue;
  295. break;
  296. default:
  297. break;
  298. }
  299. conf_childs:
  300. for (child = menu->list; child; child = child->next) {
  301. if (!child->sym || !menu_is_visible(child))
  302. continue;
  303. if (!--cnt)
  304. break;
  305. }
  306. if (!child)
  307. continue;
  308. if (line[0] && line[strlen(line) - 1] == '?') {
  309. print_help(child);
  310. continue;
  311. }
  312. sym_set_choice_value(sym, child->sym);
  313. for (child = child->list; child; child = child->next) {
  314. indent += 2;
  315. conf(child);
  316. indent -= 2;
  317. }
  318. return 1;
  319. }
  320. }
  321. static void conf(struct menu *menu)
  322. {
  323. struct symbol *sym;
  324. struct property *prop;
  325. struct menu *child;
  326. if (!menu_is_visible(menu))
  327. return;
  328. sym = menu->sym;
  329. prop = menu->prompt;
  330. if (prop) {
  331. const char *prompt;
  332. switch (prop->type) {
  333. case P_MENU:
  334. if ((input_mode == silentoldconfig ||
  335. input_mode == listnewconfig ||
  336. input_mode == oldnoconfig) &&
  337. rootEntry != menu) {
  338. check_conf(menu);
  339. return;
  340. }
  341. /* fall through */
  342. case P_COMMENT:
  343. prompt = menu_get_prompt(menu);
  344. if (prompt)
  345. printf("%*c\n%*c %s\n%*c\n",
  346. indent, '*',
  347. indent, '*', _(prompt),
  348. indent, '*');
  349. default:
  350. ;
  351. }
  352. }
  353. if (!sym)
  354. goto conf_childs;
  355. if (sym_is_choice(sym)) {
  356. conf_choice(menu);
  357. if (sym->curr.tri != mod)
  358. return;
  359. goto conf_childs;
  360. }
  361. switch (sym->type) {
  362. case S_INT:
  363. case S_HEX:
  364. case S_STRING:
  365. conf_string(menu);
  366. break;
  367. default:
  368. conf_sym(menu);
  369. break;
  370. }
  371. conf_childs:
  372. if (sym)
  373. indent += 2;
  374. for (child = menu->list; child; child = child->next)
  375. conf(child);
  376. if (sym)
  377. indent -= 2;
  378. }
  379. static void check_conf(struct menu *menu)
  380. {
  381. struct symbol *sym;
  382. struct menu *child;
  383. if (!menu_is_visible(menu))
  384. return;
  385. sym = menu->sym;
  386. if (sym && !sym_has_value(sym)) {
  387. if (sym_is_changable(sym) ||
  388. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
  389. if (input_mode == listnewconfig) {
  390. if (sym->name && !sym_is_choice_value(sym)) {
  391. printf("%s%s\n", CONFIG_, sym->name);
  392. }
  393. } else if (input_mode != oldnoconfig) {
  394. if (!conf_cnt++)
  395. printf(_("*\n* Restart config...\n*\n"));
  396. rootEntry = menu_get_parent_menu(menu);
  397. conf(rootEntry);
  398. }
  399. }
  400. }
  401. for (child = menu->list; child; child = child->next)
  402. check_conf(child);
  403. }
  404. static struct option long_opts[] = {
  405. {"oldaskconfig", no_argument, NULL, oldaskconfig},
  406. {"oldconfig", no_argument, NULL, oldconfig},
  407. {"silentoldconfig", no_argument, NULL, silentoldconfig},
  408. {"defconfig", optional_argument, NULL, defconfig},
  409. {"savedefconfig", required_argument, NULL, savedefconfig},
  410. {"allnoconfig", no_argument, NULL, allnoconfig},
  411. {"allyesconfig", no_argument, NULL, allyesconfig},
  412. {"allmodconfig", no_argument, NULL, allmodconfig},
  413. {"alldefconfig", no_argument, NULL, alldefconfig},
  414. {"randconfig", no_argument, NULL, randconfig},
  415. {"listnewconfig", no_argument, NULL, listnewconfig},
  416. {"oldnoconfig", no_argument, NULL, oldnoconfig},
  417. {NULL, 0, NULL, 0}
  418. };
  419. int main(int ac, char **av)
  420. {
  421. int opt;
  422. const char *name, *defconfig_file = NULL /* gcc uninit */;
  423. struct stat tmpstat;
  424. setlocale(LC_ALL, "");
  425. bindtextdomain(PACKAGE, LOCALEDIR);
  426. textdomain(PACKAGE);
  427. while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
  428. input_mode = (enum input_mode)opt;
  429. switch (opt) {
  430. case silentoldconfig:
  431. sync_kconfig = 1;
  432. break;
  433. case defconfig:
  434. case savedefconfig:
  435. defconfig_file = optarg;
  436. break;
  437. case randconfig:
  438. {
  439. struct timeval now;
  440. unsigned int seed;
  441. /*
  442. * Use microseconds derived seed,
  443. * compensate for systems where it may be zero
  444. */
  445. gettimeofday(&now, NULL);
  446. seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
  447. srand(seed);
  448. break;
  449. }
  450. case '?':
  451. fprintf(stderr, _("See README for usage info\n"));
  452. exit(1);
  453. break;
  454. }
  455. }
  456. if (ac == optind) {
  457. printf(_("%s: Kconfig file missing\n"), av[0]);
  458. exit(1);
  459. }
  460. name = av[optind];
  461. conf_parse(name);
  462. //zconfdump(stdout);
  463. if (sync_kconfig) {
  464. name = conf_get_configname();
  465. if (stat(name, &tmpstat)) {
  466. fprintf(stderr, _("***\n"
  467. "*** Configuration file \"%s\" not found!\n"
  468. "***\n"
  469. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  470. "*** \"make menuconfig\" or \"make xconfig\").\n"
  471. "***\n"), name);
  472. exit(1);
  473. }
  474. }
  475. switch (input_mode) {
  476. case defconfig:
  477. if (!defconfig_file)
  478. defconfig_file = conf_get_default_confname();
  479. if (conf_read(defconfig_file)) {
  480. printf(_("***\n"
  481. "*** Can't find default configuration \"%s\"!\n"
  482. "***\n"), defconfig_file);
  483. exit(1);
  484. }
  485. break;
  486. case savedefconfig:
  487. case silentoldconfig:
  488. case oldaskconfig:
  489. case oldconfig:
  490. case listnewconfig:
  491. case oldnoconfig:
  492. conf_read(NULL);
  493. break;
  494. case allnoconfig:
  495. case allyesconfig:
  496. case allmodconfig:
  497. case alldefconfig:
  498. case randconfig:
  499. name = getenv("KCONFIG_ALLCONFIG");
  500. if (name && !stat(name, &tmpstat)) {
  501. conf_read_simple(name, S_DEF_USER);
  502. break;
  503. }
  504. switch (input_mode) {
  505. case allnoconfig: name = "allno.config"; break;
  506. case allyesconfig: name = "allyes.config"; break;
  507. case allmodconfig: name = "allmod.config"; break;
  508. case alldefconfig: name = "alldef.config"; break;
  509. case randconfig: name = "allrandom.config"; break;
  510. default: break;
  511. }
  512. if (!stat(name, &tmpstat))
  513. conf_read_simple(name, S_DEF_USER);
  514. else if (!stat("all.config", &tmpstat))
  515. conf_read_simple("all.config", S_DEF_USER);
  516. break;
  517. default:
  518. break;
  519. }
  520. if (sync_kconfig) {
  521. if (conf_get_changed()) {
  522. name = getenv("KCONFIG_NOSILENTUPDATE");
  523. if (name && *name) {
  524. fprintf(stderr,
  525. _("\n*** The configuration requires explicit update.\n\n"));
  526. return 1;
  527. }
  528. }
  529. valid_stdin = isatty(0) && isatty(1) && isatty(2);
  530. }
  531. switch (input_mode) {
  532. case allnoconfig:
  533. conf_set_all_new_symbols(def_no);
  534. break;
  535. case allyesconfig:
  536. conf_set_all_new_symbols(def_yes);
  537. break;
  538. case allmodconfig:
  539. conf_set_all_new_symbols(def_mod);
  540. break;
  541. case alldefconfig:
  542. conf_set_all_new_symbols(def_default);
  543. break;
  544. case randconfig:
  545. conf_set_all_new_symbols(def_random);
  546. break;
  547. case defconfig:
  548. conf_set_all_new_symbols(def_default);
  549. break;
  550. case savedefconfig:
  551. break;
  552. case oldaskconfig:
  553. rootEntry = &rootmenu;
  554. conf(&rootmenu);
  555. input_mode = silentoldconfig;
  556. /* fall through */
  557. case oldconfig:
  558. case listnewconfig:
  559. case oldnoconfig:
  560. case silentoldconfig:
  561. /* Update until a loop caused no more changes */
  562. do {
  563. conf_cnt = 0;
  564. check_conf(&rootmenu);
  565. } while (conf_cnt &&
  566. (input_mode != listnewconfig &&
  567. input_mode != oldnoconfig));
  568. break;
  569. }
  570. if (sync_kconfig) {
  571. /* silentoldconfig is used during the build so we shall update autoconf.
  572. * All other commands are only used to generate a config.
  573. */
  574. if (conf_get_changed() && conf_write(NULL)) {
  575. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  576. exit(1);
  577. }
  578. if (conf_write_autoconf()) {
  579. fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
  580. return 1;
  581. }
  582. } else if (input_mode == savedefconfig) {
  583. if (conf_write_defconfig(defconfig_file)) {
  584. fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
  585. defconfig_file);
  586. return 1;
  587. }
  588. } else if (input_mode != listnewconfig) {
  589. if (conf_write(NULL)) {
  590. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  591. exit(1);
  592. }
  593. }
  594. return 0;
  595. }
  596. /*
  597. * Helper function to facilitate fgets() by Jean Sacren.
  598. */
  599. void xfgets(str, size, in)
  600. char *str;
  601. int size;
  602. FILE *in;
  603. {
  604. if (fgets(str, size, in) == NULL)
  605. fprintf(stderr, "\nError in reading or end of file.\n");
  606. }