conf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. case set_default:
  91. printf("%s\n", def);
  92. return 1;
  93. default:
  94. break;
  95. }
  96. switch (type) {
  97. case S_INT:
  98. case S_HEX:
  99. case S_STRING:
  100. printf("%s\n", def);
  101. return 1;
  102. default:
  103. ;
  104. }
  105. printf("%s", line);
  106. return 1;
  107. }
  108. int conf_string(struct menu *menu)
  109. {
  110. struct symbol *sym = menu->sym;
  111. const char *def;
  112. while (1) {
  113. printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
  114. printf("(%s) ", sym->name);
  115. def = sym_get_string_value(sym);
  116. if (sym_get_string_value(sym))
  117. printf("[%s] ", def);
  118. if (!conf_askvalue(sym, def))
  119. return 0;
  120. switch (line[0]) {
  121. case '\n':
  122. break;
  123. case '?':
  124. /* print help */
  125. if (line[1] == '\n') {
  126. printf("\n%s\n", get_help(menu));
  127. def = NULL;
  128. break;
  129. }
  130. default:
  131. line[strlen(line)-1] = 0;
  132. def = line;
  133. }
  134. if (def && sym_set_string_value(sym, def))
  135. return 0;
  136. }
  137. }
  138. static int conf_sym(struct menu *menu)
  139. {
  140. struct symbol *sym = menu->sym;
  141. int type;
  142. tristate oldval, newval;
  143. while (1) {
  144. printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
  145. if (sym->name)
  146. printf("(%s) ", sym->name);
  147. type = sym_get_type(sym);
  148. putchar('[');
  149. oldval = sym_get_tristate_value(sym);
  150. switch (oldval) {
  151. case no:
  152. putchar('N');
  153. break;
  154. case mod:
  155. putchar('M');
  156. break;
  157. case yes:
  158. putchar('Y');
  159. break;
  160. }
  161. if (oldval != no && sym_tristate_within_range(sym, no))
  162. printf("/n");
  163. if (oldval != mod && sym_tristate_within_range(sym, mod))
  164. printf("/m");
  165. if (oldval != yes && sym_tristate_within_range(sym, yes))
  166. printf("/y");
  167. if (menu_has_help(menu))
  168. printf("/?");
  169. printf("] ");
  170. if (!conf_askvalue(sym, sym_get_string_value(sym)))
  171. return 0;
  172. strip(line);
  173. switch (line[0]) {
  174. case 'n':
  175. case 'N':
  176. newval = no;
  177. if (!line[1] || !strcmp(&line[1], "o"))
  178. break;
  179. continue;
  180. case 'm':
  181. case 'M':
  182. newval = mod;
  183. if (!line[1])
  184. break;
  185. continue;
  186. case 'y':
  187. case 'Y':
  188. newval = yes;
  189. if (!line[1] || !strcmp(&line[1], "es"))
  190. break;
  191. continue;
  192. case 0:
  193. newval = oldval;
  194. break;
  195. case '?':
  196. goto help;
  197. default:
  198. continue;
  199. }
  200. if (sym_set_tristate_value(sym, newval))
  201. return 0;
  202. help:
  203. printf("\n%s\n", get_help(menu));
  204. }
  205. }
  206. static int conf_choice(struct menu *menu)
  207. {
  208. struct symbol *sym, *def_sym;
  209. struct menu *child;
  210. int type;
  211. bool is_new;
  212. sym = menu->sym;
  213. type = sym_get_type(sym);
  214. is_new = !sym_has_value(sym);
  215. if (sym_is_changable(sym)) {
  216. conf_sym(menu);
  217. sym_calc_value(sym);
  218. switch (sym_get_tristate_value(sym)) {
  219. case no:
  220. return 1;
  221. case mod:
  222. return 0;
  223. case yes:
  224. break;
  225. }
  226. } else {
  227. switch (sym_get_tristate_value(sym)) {
  228. case no:
  229. return 1;
  230. case mod:
  231. printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
  232. return 0;
  233. case yes:
  234. break;
  235. }
  236. }
  237. while (1) {
  238. int cnt, def;
  239. printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
  240. def_sym = sym_get_choice_value(sym);
  241. cnt = def = 0;
  242. line[0] = 0;
  243. for (child = menu->list; child; child = child->next) {
  244. if (!menu_is_visible(child))
  245. continue;
  246. if (!child->sym) {
  247. printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
  248. continue;
  249. }
  250. cnt++;
  251. if (child->sym == def_sym) {
  252. def = cnt;
  253. printf("%*c", indent, '>');
  254. } else
  255. printf("%*c", indent, ' ');
  256. printf(" %d. %s", cnt, _(menu_get_prompt(child)));
  257. if (child->sym->name)
  258. printf(" (%s)", child->sym->name);
  259. if (!sym_has_value(child->sym))
  260. printf(_(" (NEW)"));
  261. printf("\n");
  262. }
  263. printf(_("%*schoice"), indent - 1, "");
  264. if (cnt == 1) {
  265. printf("[1]: 1\n");
  266. goto conf_childs;
  267. }
  268. printf("[1-%d", cnt);
  269. if (menu_has_help(menu))
  270. printf("?");
  271. printf("]: ");
  272. switch (input_mode) {
  273. case ask_new:
  274. case ask_silent:
  275. if (!is_new) {
  276. cnt = def;
  277. printf("%d\n", cnt);
  278. break;
  279. }
  280. check_stdin();
  281. case ask_all:
  282. fflush(stdout);
  283. fgets(line, 128, stdin);
  284. strip(line);
  285. if (line[0] == '?') {
  286. printf("\n%s\n", get_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. case set_default:
  297. cnt = def;
  298. printf("%d\n", cnt);
  299. break;
  300. default:
  301. break;
  302. }
  303. conf_childs:
  304. for (child = menu->list; child; child = child->next) {
  305. if (!child->sym || !menu_is_visible(child))
  306. continue;
  307. if (!--cnt)
  308. break;
  309. }
  310. if (!child)
  311. continue;
  312. if (line[strlen(line) - 1] == '?') {
  313. printf("\n%s\n", get_help(child));
  314. continue;
  315. }
  316. sym_set_choice_value(sym, child->sym);
  317. for (child = child->list; child; child = child->next) {
  318. indent += 2;
  319. conf(child);
  320. indent -= 2;
  321. }
  322. return 1;
  323. }
  324. }
  325. static void conf(struct menu *menu)
  326. {
  327. struct symbol *sym;
  328. struct property *prop;
  329. struct menu *child;
  330. if (!menu_is_visible(menu))
  331. return;
  332. sym = menu->sym;
  333. prop = menu->prompt;
  334. if (prop) {
  335. const char *prompt;
  336. switch (prop->type) {
  337. case P_MENU:
  338. if (input_mode == ask_silent && rootEntry != menu) {
  339. check_conf(menu);
  340. return;
  341. }
  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 (!conf_cnt++)
  390. printf(_("*\n* Restart config...\n*\n"));
  391. rootEntry = menu_get_parent_menu(menu);
  392. conf(rootEntry);
  393. }
  394. }
  395. for (child = menu->list; child; child = child->next)
  396. check_conf(child);
  397. }
  398. static void conf_do_update(void)
  399. {
  400. /* Update until a loop caused no more changes */
  401. do {
  402. conf_cnt = 0;
  403. check_conf(&rootmenu);
  404. } while (conf_cnt);
  405. }
  406. static int conf_silent_update(void)
  407. {
  408. const char *name;
  409. if (conf_get_changed()) {
  410. name = getenv("KCONFIG_NOSILENTUPDATE");
  411. if (name && *name) {
  412. fprintf(stderr,
  413. _("\n*** Kernel configuration requires explicit update.\n\n"));
  414. return 1;
  415. }
  416. conf_do_update();
  417. }
  418. return 0;
  419. }
  420. static int conf_update(void)
  421. {
  422. rootEntry = &rootmenu;
  423. conf(&rootmenu);
  424. if (input_mode == ask_all) {
  425. input_mode = ask_silent;
  426. valid_stdin = 1;
  427. }
  428. conf_do_update();
  429. return 0;
  430. }
  431. int main(int ac, char **av)
  432. {
  433. int opt;
  434. const char *name;
  435. struct stat tmpstat;
  436. setlocale(LC_ALL, "");
  437. bindtextdomain(PACKAGE, LOCALEDIR);
  438. textdomain(PACKAGE);
  439. while ((opt = getopt(ac, av, "osdD:nmyrh")) != -1) {
  440. switch (opt) {
  441. case 'o':
  442. input_mode = ask_new;
  443. break;
  444. case 's':
  445. input_mode = ask_silent;
  446. valid_stdin = isatty(0) && isatty(1) && isatty(2);
  447. break;
  448. case 'd':
  449. input_mode = set_default;
  450. break;
  451. case 'D':
  452. input_mode = set_default;
  453. defconfig_file = optarg;
  454. break;
  455. case 'n':
  456. input_mode = set_no;
  457. break;
  458. case 'm':
  459. input_mode = set_mod;
  460. break;
  461. case 'y':
  462. input_mode = set_yes;
  463. break;
  464. case 'r':
  465. input_mode = set_random;
  466. srand(time(NULL));
  467. break;
  468. case 'h':
  469. printf(_("See README for usage info\n"));
  470. exit(0);
  471. break;
  472. default:
  473. fprintf(stderr, _("See README for usage info\n"));
  474. exit(1);
  475. }
  476. }
  477. if (ac == optind) {
  478. printf(_("%s: Kconfig file missing\n"), av[0]);
  479. exit(1);
  480. }
  481. name = av[optind];
  482. conf_parse(name);
  483. //zconfdump(stdout);
  484. switch (input_mode) {
  485. case set_default:
  486. if (!defconfig_file)
  487. defconfig_file = conf_get_default_confname();
  488. if (conf_read(defconfig_file)) {
  489. printf(_("***\n"
  490. "*** Can't find default configuration \"%s\"!\n"
  491. "***\n"), defconfig_file);
  492. exit(1);
  493. }
  494. break;
  495. case ask_silent:
  496. if (stat(".config", &tmpstat)) {
  497. printf(_("***\n"
  498. "*** You have not yet configured your kernel!\n"
  499. "*** (missing kernel .config file)\n"
  500. "***\n"
  501. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  502. "*** \"make menuconfig\" or \"make xconfig\").\n"
  503. "***\n"));
  504. exit(1);
  505. }
  506. case ask_all:
  507. case ask_new:
  508. conf_read(NULL);
  509. break;
  510. case set_no:
  511. case set_mod:
  512. case set_yes:
  513. case set_random:
  514. name = getenv("KCONFIG_ALLCONFIG");
  515. if (name && !stat(name, &tmpstat)) {
  516. conf_read_simple(name, S_DEF_USER);
  517. break;
  518. }
  519. switch (input_mode) {
  520. case set_no: name = "allno.config"; break;
  521. case set_mod: name = "allmod.config"; break;
  522. case set_yes: name = "allyes.config"; break;
  523. case set_random: name = "allrandom.config"; break;
  524. default: break;
  525. }
  526. if (!stat(name, &tmpstat))
  527. conf_read_simple(name, S_DEF_USER);
  528. else if (!stat("all.config", &tmpstat))
  529. conf_read_simple("all.config", S_DEF_USER);
  530. break;
  531. default:
  532. break;
  533. }
  534. switch (input_mode) {
  535. case set_no:
  536. conf_set_all_new_symbols(def_no);
  537. break;
  538. case set_yes:
  539. conf_set_all_new_symbols(def_yes);
  540. break;
  541. case set_mod:
  542. conf_set_all_new_symbols(def_mod);
  543. break;
  544. case set_random:
  545. conf_set_all_new_symbols(def_random);
  546. break;
  547. case ask_silent:
  548. case ask_new:
  549. if (conf_silent_update())
  550. exit(1);
  551. break;
  552. case ask_all:
  553. case set_default:
  554. if (conf_update())
  555. exit(1);
  556. break;
  557. }
  558. if (conf_get_changed() && conf_write(NULL)) {
  559. fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
  560. exit(1);
  561. }
  562. /* ask_silent is used during the build so we shall update autoconf.
  563. * All other commands are only used to generate a config.
  564. */
  565. if (input_mode == ask_silent && conf_write_autoconf()) {
  566. fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
  567. return 1;
  568. }
  569. return 0;
  570. }