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