conf.c 11 KB

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