conf.c 11 KB

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