conf.c 11 KB

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