conf.c 12 KB

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