conf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. if (fgets(line, 128, stdin) != NULL)
  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. if (fgets(line, 128, stdin) != NULL)
  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. #ifndef KBUILD_NO_NLS
  399. setlocale(LC_ALL, "");
  400. bindtextdomain(PACKAGE, LOCALEDIR);
  401. textdomain(PACKAGE);
  402. #endif
  403. while ((opt = getopt(ac, av, "osdD:nmyrh")) != -1) {
  404. switch (opt) {
  405. case 'o':
  406. input_mode = ask_silent;
  407. break;
  408. case 's':
  409. input_mode = ask_silent;
  410. sync_kconfig = 1;
  411. break;
  412. case 'd':
  413. input_mode = set_default;
  414. break;
  415. case 'D':
  416. input_mode = set_default;
  417. defconfig_file = optarg;
  418. break;
  419. case 'n':
  420. input_mode = set_no;
  421. break;
  422. case 'm':
  423. input_mode = set_mod;
  424. break;
  425. case 'y':
  426. input_mode = set_yes;
  427. break;
  428. case 'r':
  429. {
  430. struct timeval now;
  431. unsigned int seed;
  432. /*
  433. * Use microseconds derived seed,
  434. * compensate for systems where it may be zero
  435. */
  436. gettimeofday(&now, NULL);
  437. seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
  438. srand(seed);
  439. input_mode = set_random;
  440. break;
  441. }
  442. case 'h':
  443. printf("See README for usage info\n");
  444. exit(0);
  445. break;
  446. default:
  447. fprintf(stderr, "See README for usage info\n");
  448. exit(1);
  449. }
  450. }
  451. if (ac == optind) {
  452. printf(_("%s: Kconfig file missing\n"), av[0]);
  453. exit(1);
  454. }
  455. name = av[optind];
  456. conf_parse(name);
  457. //zconfdump(stdout);
  458. if (sync_kconfig) {
  459. name = conf_get_configname();
  460. if (stat(name, &tmpstat)) {
  461. fprintf(stderr, _("***\n"
  462. "*** You have not yet configured your kernel!\n"
  463. "*** (missing kernel config file \"%s\")\n"
  464. "***\n"
  465. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  466. "*** \"make menuconfig\" or \"make xconfig\").\n"
  467. "***\n"), name);
  468. exit(1);
  469. }
  470. }
  471. switch (input_mode) {
  472. case set_default:
  473. if (!defconfig_file)
  474. defconfig_file = conf_get_default_confname();
  475. if (conf_read(defconfig_file)) {
  476. printf(_("***\n"
  477. "*** Can't find default configuration \"%s\"!\n"
  478. "***\n"), defconfig_file);
  479. exit(1);
  480. }
  481. break;
  482. case ask_silent:
  483. case ask_all:
  484. case ask_new:
  485. conf_read(NULL);
  486. break;
  487. case set_no:
  488. case set_mod:
  489. case set_yes:
  490. case set_random:
  491. name = getenv("KCONFIG_ALLCONFIG");
  492. if (name && !stat(name, &tmpstat)) {
  493. conf_read_simple(name, S_DEF_USER);
  494. break;
  495. }
  496. switch (input_mode) {
  497. case set_no: name = "allno.config"; break;
  498. case set_mod: name = "allmod.config"; break;
  499. case set_yes: name = "allyes.config"; break;
  500. case set_random: name = "allrandom.config"; break;
  501. default: break;
  502. }
  503. if (!stat(name, &tmpstat))
  504. conf_read_simple(name, S_DEF_USER);
  505. else if (!stat("all.config", &tmpstat))
  506. conf_read_simple("all.config", S_DEF_USER);
  507. break;
  508. default:
  509. break;
  510. }
  511. if (sync_kconfig) {
  512. if (conf_get_changed()) {
  513. name = getenv("KCONFIG_NOSILENTUPDATE");
  514. if (name && *name) {
  515. fprintf(stderr,
  516. "\n*** Kernel configuration requires explicit update.\n\n");
  517. return 1;
  518. }
  519. }
  520. valid_stdin = isatty(0) && isatty(1) && isatty(2);
  521. }
  522. switch (input_mode) {
  523. case set_no:
  524. conf_set_all_new_symbols(def_no);
  525. break;
  526. case set_yes:
  527. conf_set_all_new_symbols(def_yes);
  528. break;
  529. case set_mod:
  530. conf_set_all_new_symbols(def_mod);
  531. break;
  532. case set_random:
  533. conf_set_all_new_symbols(def_random);
  534. break;
  535. case set_default:
  536. conf_set_all_new_symbols(def_default);
  537. break;
  538. case ask_new:
  539. case ask_all:
  540. rootEntry = &rootmenu;
  541. conf(&rootmenu);
  542. input_mode = ask_silent;
  543. /* fall through */
  544. case ask_silent:
  545. /* Update until a loop caused no more changes */
  546. do {
  547. conf_cnt = 0;
  548. check_conf(&rootmenu);
  549. } while (conf_cnt);
  550. break;
  551. }
  552. if (sync_kconfig) {
  553. /* silentoldconfig is used during the build so we shall update autoconf.
  554. * All other commands are only used to generate a config.
  555. */
  556. if (conf_get_changed() && conf_write(NULL)) {
  557. fprintf(stderr, "\n*** Error during writing of the kernel configuration.\n\n");
  558. exit(1);
  559. }
  560. if (conf_write_autoconf()) {
  561. fprintf(stderr, "\n*** Error during update of the kernel configuration.\n\n");
  562. return 1;
  563. }
  564. } else {
  565. if (conf_write(NULL)) {
  566. fprintf(stderr, "\n*** Error during writing of the kernel configuration.\n\n");
  567. exit(1);
  568. }
  569. }
  570. return 0;
  571. }