conf.c 12 KB

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