conf.c 12 KB

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