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