conf.c 10 KB

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