conf.c 12 KB

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