conf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 sync_kconfig;
  31. static int conf_cnt;
  32. static char line[128];
  33. static struct menu *rootEntry;
  34. static char nohelp_text[] = N_("Sorry, no help available for this option yet.\n");
  35. static const char *get_help(struct menu *menu)
  36. {
  37. if (menu_has_help(menu))
  38. return _(menu_get_help(menu));
  39. else
  40. return nohelp_text;
  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. 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. printf("\n%s\n", get_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. printf("\n%s\n", get_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. printf("\n%s\n", get_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. printf("\n%s\n", get_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. input_mode = set_random;
  427. srand(time(NULL));
  428. break;
  429. case 'h':
  430. printf(_("See README for usage info\n"));
  431. exit(0);
  432. break;
  433. default:
  434. fprintf(stderr, _("See README for usage info\n"));
  435. exit(1);
  436. }
  437. }
  438. if (ac == optind) {
  439. printf(_("%s: Kconfig file missing\n"), av[0]);
  440. exit(1);
  441. }
  442. name = av[optind];
  443. conf_parse(name);
  444. //zconfdump(stdout);
  445. if (sync_kconfig) {
  446. if (stat(".config", &tmpstat)) {
  447. fprintf(stderr, _("***\n"
  448. "*** You have not yet configured!\n"
  449. "*** (missing .config file)\n"
  450. "***\n"
  451. "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
  452. "*** \"make menuconfig\" or \"make xconfig\").\n"
  453. "***\n"));
  454. exit(1);
  455. }
  456. }
  457. switch (input_mode) {
  458. case set_default:
  459. if (!defconfig_file)
  460. defconfig_file = conf_get_default_confname();
  461. if (conf_read(defconfig_file)) {
  462. printf(_("***\n"
  463. "*** Can't find default configuration \"%s\"!\n"
  464. "***\n"), defconfig_file);
  465. exit(1);
  466. }
  467. break;
  468. case ask_silent:
  469. case ask_all:
  470. case ask_new:
  471. conf_read(NULL);
  472. break;
  473. case set_no:
  474. case set_mod:
  475. case set_yes:
  476. case set_random:
  477. name = getenv("KCONFIG_ALLCONFIG");
  478. if (name && !stat(name, &tmpstat)) {
  479. conf_read_simple(name, S_DEF_USER);
  480. break;
  481. }
  482. switch (input_mode) {
  483. case set_no: name = "allno.config"; break;
  484. case set_mod: name = "allmod.config"; break;
  485. case set_yes: name = "allyes.config"; break;
  486. case set_random: name = "allrandom.config"; break;
  487. default: break;
  488. }
  489. if (!stat(name, &tmpstat))
  490. conf_read_simple(name, S_DEF_USER);
  491. else if (!stat("all.config", &tmpstat))
  492. conf_read_simple("all.config", S_DEF_USER);
  493. break;
  494. default:
  495. break;
  496. }
  497. if (sync_kconfig) {
  498. if (conf_get_changed()) {
  499. name = getenv("KCONFIG_NOSILENTUPDATE");
  500. if (name && *name) {
  501. fprintf(stderr,
  502. _("\n*** configuration requires explicit update.\n\n"));
  503. return 1;
  504. }
  505. }
  506. valid_stdin = isatty(0) && isatty(1) && isatty(2);
  507. }
  508. switch (input_mode) {
  509. case set_no:
  510. conf_set_all_new_symbols(def_no);
  511. break;
  512. case set_yes:
  513. conf_set_all_new_symbols(def_yes);
  514. break;
  515. case set_mod:
  516. conf_set_all_new_symbols(def_mod);
  517. break;
  518. case set_random:
  519. conf_set_all_new_symbols(def_random);
  520. break;
  521. case set_default:
  522. conf_set_all_new_symbols(def_default);
  523. break;
  524. case ask_new:
  525. case ask_all:
  526. rootEntry = &rootmenu;
  527. conf(&rootmenu);
  528. input_mode = ask_silent;
  529. /* fall through */
  530. case ask_silent:
  531. /* Update until a loop caused no more changes */
  532. do {
  533. conf_cnt = 0;
  534. check_conf(&rootmenu);
  535. } while (conf_cnt);
  536. break;
  537. }
  538. if (sync_kconfig) {
  539. /* silentoldconfig is used during the build so we shall update autoconf.
  540. * All other commands are only used to generate a config.
  541. */
  542. if (conf_get_changed() && conf_write(NULL)) {
  543. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  544. exit(1);
  545. }
  546. if (conf_write_autoconf()) {
  547. fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
  548. return 1;
  549. }
  550. } else {
  551. if (conf_write(NULL)) {
  552. fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
  553. exit(1);
  554. }
  555. }
  556. return 0;
  557. }