conf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 <sys/stat.h>
  13. #define LKC_DIRECT_LINK
  14. #include "lkc.h"
  15. static void conf(struct menu *menu);
  16. static void check_conf(struct menu *menu);
  17. enum {
  18. ask_all,
  19. ask_new,
  20. ask_silent,
  21. set_default,
  22. set_yes,
  23. set_mod,
  24. set_no,
  25. set_random
  26. } input_mode = ask_all;
  27. char *defconfig_file;
  28. static int indent = 1;
  29. static int valid_stdin = 1;
  30. static int conf_cnt;
  31. static char line[128];
  32. static struct menu *rootEntry;
  33. static char nohelp_text[] = N_("Sorry, no help available for this option yet.\n");
  34. static const char *get_help(struct menu *menu)
  35. {
  36. if (menu_has_help(menu))
  37. return _(menu_get_help(menu));
  38. else
  39. return nohelp_text;
  40. }
  41. static void strip(char *str)
  42. {
  43. char *p = str;
  44. int l;
  45. while ((isspace(*p)))
  46. p++;
  47. l = strlen(p);
  48. if (p != str)
  49. memmove(str, p, l + 1);
  50. if (!l)
  51. return;
  52. p = str + l - 1;
  53. while ((isspace(*p)))
  54. *p-- = 0;
  55. }
  56. static void check_stdin(void)
  57. {
  58. if (!valid_stdin && input_mode == ask_silent) {
  59. printf(_("aborted!\n\n"));
  60. printf(_("Console input/output is redirected. "));
  61. printf(_("Run 'make oldconfig' to update configuration.\n\n"));
  62. exit(1);
  63. }
  64. }
  65. static int conf_askvalue(struct symbol *sym, const char *def)
  66. {
  67. enum symbol_type type = sym_get_type(sym);
  68. if (!sym_has_value(sym))
  69. printf(_("(NEW) "));
  70. line[0] = '\n';
  71. line[1] = 0;
  72. if (!sym_is_changable(sym)) {
  73. printf("%s\n", def);
  74. line[0] = '\n';
  75. line[1] = 0;
  76. return 0;
  77. }
  78. switch (input_mode) {
  79. case ask_new:
  80. case ask_silent:
  81. if (sym_has_value(sym)) {
  82. printf("%s\n", def);
  83. return 0;
  84. }
  85. check_stdin();
  86. case ask_all:
  87. fflush(stdout);
  88. fgets(line, 128, stdin);
  89. return 1;
  90. default:
  91. break;
  92. }
  93. switch (type) {
  94. case S_INT:
  95. case S_HEX:
  96. case S_STRING:
  97. printf("%s\n", def);
  98. return 1;
  99. default:
  100. ;
  101. }
  102. printf("%s", line);
  103. return 1;
  104. }
  105. int conf_string(struct menu *menu)
  106. {
  107. struct symbol *sym = menu->sym;
  108. const char *def;
  109. while (1) {
  110. printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
  111. printf("(%s) ", sym->name);
  112. def = sym_get_string_value(sym);
  113. if (sym_get_string_value(sym))
  114. printf("[%s] ", def);
  115. if (!conf_askvalue(sym, def))
  116. return 0;
  117. switch (line[0]) {
  118. case '\n':
  119. break;
  120. case '?':
  121. /* print help */
  122. if (line[1] == '\n') {
  123. printf("\n%s\n", get_help(menu));
  124. def = NULL;
  125. break;
  126. }
  127. default:
  128. line[strlen(line)-1] = 0;
  129. def = line;
  130. }
  131. if (def && sym_set_string_value(sym, def))
  132. return 0;
  133. }
  134. }
  135. static int conf_sym(struct menu *menu)
  136. {
  137. struct symbol *sym = menu->sym;
  138. int type;
  139. tristate oldval, newval;
  140. while (1) {
  141. printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
  142. if (sym->name)
  143. printf("(%s) ", sym->name);
  144. type = sym_get_type(sym);
  145. putchar('[');
  146. oldval = sym_get_tristate_value(sym);
  147. switch (oldval) {
  148. case no:
  149. putchar('N');
  150. break;
  151. case mod:
  152. putchar('M');
  153. break;
  154. case yes:
  155. putchar('Y');
  156. break;
  157. }
  158. if (oldval != no && sym_tristate_within_range(sym, no))
  159. printf("/n");
  160. if (oldval != mod && sym_tristate_within_range(sym, mod))
  161. printf("/m");
  162. if (oldval != yes && sym_tristate_within_range(sym, yes))
  163. printf("/y");
  164. if (menu_has_help(menu))
  165. printf("/?");
  166. printf("] ");
  167. if (!conf_askvalue(sym, sym_get_string_value(sym)))
  168. return 0;
  169. strip(line);
  170. switch (line[0]) {
  171. case 'n':
  172. case 'N':
  173. newval = no;
  174. if (!line[1] || !strcmp(&line[1], "o"))
  175. break;
  176. continue;
  177. case 'm':
  178. case 'M':
  179. newval = mod;
  180. if (!line[1])
  181. break;
  182. continue;
  183. case 'y':
  184. case 'Y':
  185. newval = yes;
  186. if (!line[1] || !strcmp(&line[1], "es"))
  187. break;
  188. continue;
  189. case 0:
  190. newval = oldval;
  191. break;
  192. case '?':
  193. goto help;
  194. default:
  195. continue;
  196. }
  197. if (sym_set_tristate_value(sym, newval))
  198. return 0;
  199. help:
  200. printf("\n%s\n", get_help(menu));
  201. }
  202. }
  203. static int conf_choice(struct menu *menu)
  204. {
  205. struct symbol *sym, *def_sym;
  206. struct menu *child;
  207. int type;
  208. bool is_new;
  209. sym = menu->sym;
  210. type = sym_get_type(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. if (menu_has_help(menu))
  267. printf("?");
  268. printf("]: ");
  269. switch (input_mode) {
  270. case ask_new:
  271. case ask_silent:
  272. if (!is_new) {
  273. cnt = def;
  274. printf("%d\n", cnt);
  275. break;
  276. }
  277. check_stdin();
  278. case ask_all:
  279. fflush(stdout);
  280. fgets(line, 128, stdin);
  281. strip(line);
  282. if (line[0] == '?') {
  283. printf("\n%s\n", get_help(menu));
  284. continue;
  285. }
  286. if (!line[0])
  287. cnt = def;
  288. else if (isdigit(line[0]))
  289. cnt = atoi(line);
  290. else
  291. continue;
  292. break;
  293. default:
  294. break;
  295. }
  296. conf_childs:
  297. for (child = menu->list; child; child = child->next) {
  298. if (!child->sym || !menu_is_visible(child))
  299. continue;
  300. if (!--cnt)
  301. break;
  302. }
  303. if (!child)
  304. continue;
  305. if (line[strlen(line) - 1] == '?') {
  306. printf("\n%s\n", get_help(child));
  307. continue;
  308. }
  309. sym_set_choice_value(sym, child->sym);
  310. for (child = child->list; child; child = child->next) {
  311. indent += 2;
  312. conf(child);
  313. indent -= 2;
  314. }
  315. return 1;
  316. }
  317. }
  318. static void conf(struct menu *menu)
  319. {
  320. struct symbol *sym;
  321. struct property *prop;
  322. struct menu *child;
  323. if (!menu_is_visible(menu))
  324. return;
  325. sym = menu->sym;
  326. prop = menu->prompt;
  327. if (prop) {
  328. const char *prompt;
  329. switch (prop->type) {
  330. case P_MENU:
  331. if (input_mode == ask_silent && rootEntry != menu) {
  332. check_conf(menu);
  333. return;
  334. }
  335. case P_COMMENT:
  336. prompt = menu_get_prompt(menu);
  337. if (prompt)
  338. printf("%*c\n%*c %s\n%*c\n",
  339. indent, '*',
  340. indent, '*', _(prompt),
  341. indent, '*');
  342. default:
  343. ;
  344. }
  345. }
  346. if (!sym)
  347. goto conf_childs;
  348. if (sym_is_choice(sym)) {
  349. conf_choice(menu);
  350. if (sym->curr.tri != mod)
  351. return;
  352. goto conf_childs;
  353. }
  354. switch (sym->type) {
  355. case S_INT:
  356. case S_HEX:
  357. case S_STRING:
  358. conf_string(menu);
  359. break;
  360. default:
  361. conf_sym(menu);
  362. break;
  363. }
  364. conf_childs:
  365. if (sym)
  366. indent += 2;
  367. for (child = menu->list; child; child = child->next)
  368. conf(child);
  369. if (sym)
  370. indent -= 2;
  371. }
  372. static void check_conf(struct menu *menu)
  373. {
  374. struct symbol *sym;
  375. struct menu *child;
  376. if (!menu_is_visible(menu))
  377. return;
  378. sym = menu->sym;
  379. if (sym && !sym_has_value(sym)) {
  380. if (sym_is_changable(sym) ||
  381. (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
  382. if (!conf_cnt++)
  383. printf(_("*\n* Restart config...\n*\n"));
  384. rootEntry = menu_get_parent_menu(menu);
  385. conf(rootEntry);
  386. }
  387. }
  388. for (child = menu->list; child; child = child->next)
  389. check_conf(child);
  390. }
  391. static void conf_do_update(void)
  392. {
  393. /* Update until a loop caused no more changes */
  394. do {
  395. conf_cnt = 0;
  396. check_conf(&rootmenu);
  397. } while (conf_cnt);
  398. }
  399. static int conf_silent_update(void)
  400. {
  401. const char *name;
  402. if (conf_get_changed()) {
  403. name = getenv("KCONFIG_NOSILENTUPDATE");
  404. if (name && *name) {
  405. fprintf(stderr,
  406. _("\n*** Kernel configuration requires explicit update.\n\n"));
  407. return 1;
  408. }
  409. conf_do_update();
  410. }
  411. return 0;
  412. }
  413. static int conf_update(void)
  414. {
  415. rootEntry = &rootmenu;
  416. conf(&rootmenu);
  417. if (input_mode == ask_all) {
  418. input_mode = ask_silent;
  419. valid_stdin = 1;
  420. }
  421. conf_do_update();
  422. return 0;
  423. }
  424. int main(int ac, char **av)
  425. {
  426. int opt;
  427. const char *name;
  428. struct stat tmpstat;
  429. setlocale(LC_ALL, "");
  430. bindtextdomain(PACKAGE, LOCALEDIR);
  431. textdomain(PACKAGE);
  432. while ((opt = getopt(ac, av, "osdD:nmyrh")) != -1) {
  433. switch (opt) {
  434. case 'o':
  435. input_mode = ask_new;
  436. break;
  437. case 's':
  438. input_mode = ask_silent;
  439. valid_stdin = isatty(0) && isatty(1) && isatty(2);
  440. break;
  441. case 'd':
  442. input_mode = set_default;
  443. break;
  444. case 'D':
  445. input_mode = set_default;
  446. defconfig_file = optarg;
  447. break;
  448. case 'n':
  449. input_mode = set_no;
  450. break;
  451. case 'm':
  452. input_mode = set_mod;
  453. break;
  454. case 'y':
  455. input_mode = set_yes;
  456. break;
  457. case 'r':
  458. input_mode = set_random;
  459. srand(time(NULL));
  460. break;
  461. case 'h':
  462. printf(_("See README for usage info\n"));
  463. exit(0);
  464. break;
  465. default:
  466. fprintf(stderr, _("See README for usage info\n"));
  467. exit(1);
  468. }
  469. }
  470. if (ac == optind) {
  471. printf(_("%s: Kconfig file missing\n"), av[0]);
  472. exit(1);
  473. }
  474. name = av[optind];
  475. conf_parse(name);
  476. //zconfdump(stdout);
  477. switch (input_mode) {
  478. case set_default:
  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 ask_silent:
  489. if (stat(".config", &tmpstat)) {
  490. printf(_("***\n"
  491. "*** You have not yet configured your kernel!\n"
  492. "*** (missing kernel .config file)\n"
  493. "***\n"
  494. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  495. "*** \"make menuconfig\" or \"make xconfig\").\n"
  496. "***\n"));
  497. exit(1);
  498. }
  499. case ask_all:
  500. case ask_new:
  501. conf_read(NULL);
  502. break;
  503. case set_no:
  504. case set_mod:
  505. case set_yes:
  506. case set_random:
  507. name = getenv("KCONFIG_ALLCONFIG");
  508. if (name && !stat(name, &tmpstat)) {
  509. conf_read_simple(name, S_DEF_USER);
  510. break;
  511. }
  512. switch (input_mode) {
  513. case set_no: name = "allno.config"; break;
  514. case set_mod: name = "allmod.config"; break;
  515. case set_yes: name = "allyes.config"; break;
  516. case set_random: name = "allrandom.config"; break;
  517. default: break;
  518. }
  519. if (!stat(name, &tmpstat))
  520. conf_read_simple(name, S_DEF_USER);
  521. else if (!stat("all.config", &tmpstat))
  522. conf_read_simple("all.config", S_DEF_USER);
  523. break;
  524. default:
  525. break;
  526. }
  527. switch (input_mode) {
  528. case set_no:
  529. conf_set_all_new_symbols(def_no);
  530. break;
  531. case set_yes:
  532. conf_set_all_new_symbols(def_yes);
  533. break;
  534. case set_mod:
  535. conf_set_all_new_symbols(def_mod);
  536. break;
  537. case set_random:
  538. conf_set_all_new_symbols(def_random);
  539. break;
  540. case set_default:
  541. conf_set_all_new_symbols(def_default);
  542. break;
  543. case ask_silent:
  544. case ask_new:
  545. if (conf_silent_update())
  546. exit(1);
  547. break;
  548. case ask_all:
  549. if (conf_update())
  550. exit(1);
  551. break;
  552. }
  553. if (conf_write(NULL)) {
  554. fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
  555. exit(1);
  556. }
  557. /* ask_silent is used during the build so we shall update autoconf.
  558. * All other commands are only used to generate a config.
  559. */
  560. if (input_mode == ask_silent && conf_write_autoconf()) {
  561. fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
  562. return 1;
  563. }
  564. return 0;
  565. }