conf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. 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. 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. case oldaskconfig:
  281. fflush(stdout);
  282. xfgets(line, 128, stdin);
  283. strip(line);
  284. if (line[0] == '?') {
  285. print_help(menu);
  286. continue;
  287. }
  288. if (!line[0])
  289. cnt = def;
  290. else if (isdigit(line[0]))
  291. cnt = atoi(line);
  292. else
  293. continue;
  294. break;
  295. default:
  296. break;
  297. }
  298. conf_childs:
  299. for (child = menu->list; child; child = child->next) {
  300. if (!child->sym || !menu_is_visible(child))
  301. continue;
  302. if (!--cnt)
  303. break;
  304. }
  305. if (!child)
  306. continue;
  307. if (line[strlen(line) - 1] == '?') {
  308. print_help(child);
  309. continue;
  310. }
  311. sym_set_choice_value(sym, child->sym);
  312. for (child = child->list; child; child = child->next) {
  313. indent += 2;
  314. conf(child);
  315. indent -= 2;
  316. }
  317. return 1;
  318. }
  319. }
  320. static void conf(struct menu *menu)
  321. {
  322. struct symbol *sym;
  323. struct property *prop;
  324. struct menu *child;
  325. if (!menu_is_visible(menu))
  326. return;
  327. sym = menu->sym;
  328. prop = menu->prompt;
  329. if (prop) {
  330. const char *prompt;
  331. switch (prop->type) {
  332. case P_MENU:
  333. if ((input_mode == silentoldconfig ||
  334. input_mode == listnewconfig ||
  335. input_mode == oldnoconfig) &&
  336. rootEntry != menu) {
  337. check_conf(menu);
  338. return;
  339. }
  340. case P_COMMENT:
  341. prompt = menu_get_prompt(menu);
  342. if (prompt)
  343. printf("%*c\n%*c %s\n%*c\n",
  344. indent, '*',
  345. indent, '*', _(prompt),
  346. indent, '*');
  347. default:
  348. ;
  349. }
  350. }
  351. if (!sym)
  352. goto conf_childs;
  353. if (sym_is_choice(sym)) {
  354. conf_choice(menu);
  355. if (sym->curr.tri != mod)
  356. return;
  357. goto conf_childs;
  358. }
  359. switch (sym->type) {
  360. case S_INT:
  361. case S_HEX:
  362. case S_STRING:
  363. conf_string(menu);
  364. break;
  365. default:
  366. conf_sym(menu);
  367. break;
  368. }
  369. conf_childs:
  370. if (sym)
  371. indent += 2;
  372. for (child = menu->list; child; child = child->next)
  373. conf(child);
  374. if (sym)
  375. indent -= 2;
  376. }
  377. static void check_conf(struct menu *menu)
  378. {
  379. struct symbol *sym;
  380. struct menu *child;
  381. if (!menu_is_visible(menu))
  382. return;
  383. sym = menu->sym;
  384. if (sym && !sym_has_value(sym)) {
  385. if (sym_is_changable(sym) ||
  386. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
  387. if (input_mode == listnewconfig) {
  388. if (sym->name && !sym_is_choice_value(sym)) {
  389. printf("CONFIG_%s\n", sym->name);
  390. }
  391. } else {
  392. if (!conf_cnt++)
  393. printf(_("*\n* Restart config...\n*\n"));
  394. rootEntry = menu_get_parent_menu(menu);
  395. conf(rootEntry);
  396. }
  397. }
  398. }
  399. for (child = menu->list; child; child = child->next)
  400. check_conf(child);
  401. }
  402. static struct option long_opts[] = {
  403. {"oldaskconfig", no_argument, NULL, oldaskconfig},
  404. {"oldconfig", no_argument, NULL, oldconfig},
  405. {"silentoldconfig", no_argument, NULL, silentoldconfig},
  406. {"defconfig", optional_argument, NULL, defconfig},
  407. {"savedefconfig", required_argument, NULL, savedefconfig},
  408. {"allnoconfig", no_argument, NULL, allnoconfig},
  409. {"allyesconfig", no_argument, NULL, allyesconfig},
  410. {"allmodconfig", no_argument, NULL, allmodconfig},
  411. {"alldefconfig", no_argument, NULL, alldefconfig},
  412. {"randconfig", no_argument, NULL, randconfig},
  413. {"listnewconfig", no_argument, NULL, listnewconfig},
  414. {"oldnoconfig", no_argument, NULL, oldnoconfig},
  415. {NULL, 0, NULL, 0}
  416. };
  417. int main(int ac, char **av)
  418. {
  419. int opt;
  420. const char *name;
  421. struct stat tmpstat;
  422. setlocale(LC_ALL, "");
  423. bindtextdomain(PACKAGE, LOCALEDIR);
  424. textdomain(PACKAGE);
  425. while ((opt = getopt_long_only(ac, av, "", long_opts, NULL)) != -1) {
  426. input_mode = (enum input_mode)opt;
  427. switch (opt) {
  428. case silentoldconfig:
  429. sync_kconfig = 1;
  430. break;
  431. case defconfig:
  432. case savedefconfig:
  433. defconfig_file = optarg;
  434. break;
  435. case randconfig:
  436. {
  437. struct timeval now;
  438. unsigned int seed;
  439. /*
  440. * Use microseconds derived seed,
  441. * compensate for systems where it may be zero
  442. */
  443. gettimeofday(&now, NULL);
  444. seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
  445. srand(seed);
  446. break;
  447. }
  448. case '?':
  449. fprintf(stderr, _("See README for usage info\n"));
  450. exit(1);
  451. break;
  452. }
  453. }
  454. if (ac == optind) {
  455. printf(_("%s: Kconfig file missing\n"), av[0]);
  456. exit(1);
  457. }
  458. name = av[optind];
  459. conf_parse(name);
  460. //zconfdump(stdout);
  461. if (sync_kconfig) {
  462. name = conf_get_configname();
  463. if (stat(name, &tmpstat)) {
  464. fprintf(stderr, _("***\n"
  465. "*** You have not yet configured your kernel!\n"
  466. "*** (missing kernel config file \"%s\")\n"
  467. "***\n"
  468. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  469. "*** \"make menuconfig\" or \"make xconfig\").\n"
  470. "***\n"), name);
  471. exit(1);
  472. }
  473. }
  474. switch (input_mode) {
  475. case defconfig:
  476. if (!defconfig_file)
  477. defconfig_file = conf_get_default_confname();
  478. if (conf_read(defconfig_file)) {
  479. printf(_("***\n"
  480. "*** Can't find default configuration \"%s\"!\n"
  481. "***\n"), defconfig_file);
  482. exit(1);
  483. }
  484. break;
  485. case savedefconfig:
  486. conf_read(NULL);
  487. break;
  488. case silentoldconfig:
  489. case oldaskconfig:
  490. case oldconfig:
  491. case listnewconfig:
  492. case oldnoconfig:
  493. conf_read(NULL);
  494. break;
  495. case allnoconfig:
  496. case allyesconfig:
  497. case allmodconfig:
  498. case alldefconfig:
  499. case randconfig:
  500. name = getenv("KCONFIG_ALLCONFIG");
  501. if (name && !stat(name, &tmpstat)) {
  502. conf_read_simple(name, S_DEF_USER);
  503. break;
  504. }
  505. switch (input_mode) {
  506. case allnoconfig: name = "allno.config"; break;
  507. case allyesconfig: name = "allyes.config"; break;
  508. case allmodconfig: name = "allmod.config"; break;
  509. case alldefconfig: name = "alldef.config"; break;
  510. case randconfig: name = "allrandom.config"; break;
  511. default: break;
  512. }
  513. if (!stat(name, &tmpstat))
  514. conf_read_simple(name, S_DEF_USER);
  515. else if (!stat("all.config", &tmpstat))
  516. conf_read_simple("all.config", S_DEF_USER);
  517. break;
  518. default:
  519. break;
  520. }
  521. if (sync_kconfig) {
  522. if (conf_get_changed()) {
  523. name = getenv("KCONFIG_NOSILENTUPDATE");
  524. if (name && *name) {
  525. fprintf(stderr,
  526. _("\n*** Kernel configuration requires explicit update.\n\n"));
  527. return 1;
  528. }
  529. }
  530. valid_stdin = isatty(0) && isatty(1) && isatty(2);
  531. }
  532. switch (input_mode) {
  533. case allnoconfig:
  534. conf_set_all_new_symbols(def_no);
  535. break;
  536. case allyesconfig:
  537. conf_set_all_new_symbols(def_yes);
  538. break;
  539. case allmodconfig:
  540. conf_set_all_new_symbols(def_mod);
  541. break;
  542. case alldefconfig:
  543. conf_set_all_new_symbols(def_default);
  544. break;
  545. case randconfig:
  546. conf_set_all_new_symbols(def_random);
  547. break;
  548. case defconfig:
  549. conf_set_all_new_symbols(def_default);
  550. break;
  551. case savedefconfig:
  552. break;
  553. case oldaskconfig:
  554. rootEntry = &rootmenu;
  555. conf(&rootmenu);
  556. input_mode = silentoldconfig;
  557. /* fall through */
  558. case oldconfig:
  559. case listnewconfig:
  560. case oldnoconfig:
  561. case silentoldconfig:
  562. /* Update until a loop caused no more changes */
  563. do {
  564. conf_cnt = 0;
  565. check_conf(&rootmenu);
  566. } while (conf_cnt &&
  567. (input_mode != listnewconfig &&
  568. input_mode != oldnoconfig));
  569. break;
  570. }
  571. if (sync_kconfig) {
  572. /* silentoldconfig is used during the build so we shall update autoconf.
  573. * All other commands are only used to generate a config.
  574. */
  575. if (conf_get_changed() && conf_write(NULL)) {
  576. fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
  577. exit(1);
  578. }
  579. if (conf_write_autoconf()) {
  580. fprintf(stderr, _("\n*** Error during update of the kernel configuration.\n\n"));
  581. return 1;
  582. }
  583. } else if (input_mode == savedefconfig) {
  584. if (conf_write_defconfig(defconfig_file)) {
  585. fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
  586. defconfig_file);
  587. return 1;
  588. }
  589. } else if (input_mode != listnewconfig) {
  590. if (conf_write(NULL)) {
  591. fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
  592. exit(1);
  593. }
  594. }
  595. return 0;
  596. }
  597. /*
  598. * Helper function to facilitate fgets() by Jean Sacren.
  599. */
  600. void xfgets(str, size, in)
  601. char *str;
  602. int size;
  603. FILE *in;
  604. {
  605. if (fgets(str, size, in) == NULL)
  606. fprintf(stderr, "\nError in reading or end of file.\n");
  607. }