conf.c 12 KB

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