conf.c 9.9 KB

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