PageRenderTime 36ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/scripts/kconfig/symbol.c

https://github.com/dmitriy103/bravo_kernel-2.6.35
C | 979 lines | 835 code | 122 blank | 22 comment | 219 complexity | ef938b4cbb81deee83bb555a6b4f6aee MD5 | raw file
  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 <regex.h>
  9. #include <sys/utsname.h>
  10. #define LKC_DIRECT_LINK
  11. #include "lkc.h"
  12. struct symbol symbol_yes = {
  13. .name = "y",
  14. .curr = { "y", yes },
  15. .flags = SYMBOL_CONST|SYMBOL_VALID,
  16. }, symbol_mod = {
  17. .name = "m",
  18. .curr = { "m", mod },
  19. .flags = SYMBOL_CONST|SYMBOL_VALID,
  20. }, symbol_no = {
  21. .name = "n",
  22. .curr = { "n", no },
  23. .flags = SYMBOL_CONST|SYMBOL_VALID,
  24. }, symbol_empty = {
  25. .name = "",
  26. .curr = { "", no },
  27. .flags = SYMBOL_VALID,
  28. };
  29. struct symbol *sym_defconfig_list;
  30. struct symbol *modules_sym;
  31. tristate modules_val;
  32. struct expr *sym_env_list;
  33. static void sym_add_default(struct symbol *sym, const char *def)
  34. {
  35. struct property *prop = prop_alloc(P_DEFAULT, sym);
  36. prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
  37. }
  38. void sym_init(void)
  39. {
  40. struct symbol *sym;
  41. struct utsname uts;
  42. static bool inited = false;
  43. if (inited)
  44. return;
  45. inited = true;
  46. uname(&uts);
  47. sym = sym_lookup("UNAME_RELEASE", 0);
  48. sym->type = S_STRING;
  49. sym->flags |= SYMBOL_AUTO;
  50. sym_add_default(sym, uts.release);
  51. }
  52. enum symbol_type sym_get_type(struct symbol *sym)
  53. {
  54. enum symbol_type type = sym->type;
  55. if (type == S_TRISTATE) {
  56. if (sym_is_choice_value(sym) && sym->visible == yes)
  57. type = S_BOOLEAN;
  58. else if (modules_val == no)
  59. type = S_BOOLEAN;
  60. }
  61. return type;
  62. }
  63. const char *sym_type_name(enum symbol_type type)
  64. {
  65. switch (type) {
  66. case S_BOOLEAN:
  67. return "boolean";
  68. case S_TRISTATE:
  69. return "tristate";
  70. case S_INT:
  71. return "integer";
  72. case S_HEX:
  73. return "hex";
  74. case S_STRING:
  75. return "string";
  76. case S_UNKNOWN:
  77. return "unknown";
  78. case S_OTHER:
  79. break;
  80. }
  81. return "???";
  82. }
  83. struct property *sym_get_choice_prop(struct symbol *sym)
  84. {
  85. struct property *prop;
  86. for_all_choices(sym, prop)
  87. return prop;
  88. return NULL;
  89. }
  90. struct property *sym_get_env_prop(struct symbol *sym)
  91. {
  92. struct property *prop;
  93. for_all_properties(sym, prop, P_ENV)
  94. return prop;
  95. return NULL;
  96. }
  97. struct property *sym_get_default_prop(struct symbol *sym)
  98. {
  99. struct property *prop;
  100. for_all_defaults(sym, prop) {
  101. prop->visible.tri = expr_calc_value(prop->visible.expr);
  102. if (prop->visible.tri != no)
  103. return prop;
  104. }
  105. return NULL;
  106. }
  107. static struct property *sym_get_range_prop(struct symbol *sym)
  108. {
  109. struct property *prop;
  110. for_all_properties(sym, prop, P_RANGE) {
  111. prop->visible.tri = expr_calc_value(prop->visible.expr);
  112. if (prop->visible.tri != no)
  113. return prop;
  114. }
  115. return NULL;
  116. }
  117. static int sym_get_range_val(struct symbol *sym, int base)
  118. {
  119. sym_calc_value(sym);
  120. switch (sym->type) {
  121. case S_INT:
  122. base = 10;
  123. break;
  124. case S_HEX:
  125. base = 16;
  126. break;
  127. default:
  128. break;
  129. }
  130. return strtol(sym->curr.val, NULL, base);
  131. }
  132. static void sym_validate_range(struct symbol *sym)
  133. {
  134. struct property *prop;
  135. int base, val, val2;
  136. char str[64];
  137. switch (sym->type) {
  138. case S_INT:
  139. base = 10;
  140. break;
  141. case S_HEX:
  142. base = 16;
  143. break;
  144. default:
  145. return;
  146. }
  147. prop = sym_get_range_prop(sym);
  148. if (!prop)
  149. return;
  150. val = strtol(sym->curr.val, NULL, base);
  151. val2 = sym_get_range_val(prop->expr->left.sym, base);
  152. if (val >= val2) {
  153. val2 = sym_get_range_val(prop->expr->right.sym, base);
  154. if (val <= val2)
  155. return;
  156. }
  157. if (sym->type == S_INT)
  158. sprintf(str, "%d", val2);
  159. else
  160. sprintf(str, "0x%x", val2);
  161. sym->curr.val = strdup(str);
  162. }
  163. static void sym_calc_visibility(struct symbol *sym)
  164. {
  165. struct property *prop;
  166. tristate tri;
  167. /* any prompt visible? */
  168. tri = no;
  169. for_all_prompts(sym, prop) {
  170. prop->visible.tri = expr_calc_value(prop->visible.expr);
  171. tri = EXPR_OR(tri, prop->visible.tri);
  172. }
  173. if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
  174. tri = yes;
  175. if (sym->visible != tri) {
  176. sym->visible = tri;
  177. sym_set_changed(sym);
  178. }
  179. if (sym_is_choice_value(sym))
  180. return;
  181. tri = no;
  182. if (sym->rev_dep.expr)
  183. tri = expr_calc_value(sym->rev_dep.expr);
  184. if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
  185. tri = yes;
  186. if (sym->rev_dep.tri != tri) {
  187. sym->rev_dep.tri = tri;
  188. sym_set_changed(sym);
  189. }
  190. }
  191. static struct symbol *sym_calc_choice(struct symbol *sym)
  192. {
  193. struct symbol *def_sym;
  194. struct property *prop;
  195. struct expr *e;
  196. /* is the user choice visible? */
  197. def_sym = sym->def[S_DEF_USER].val;
  198. if (def_sym) {
  199. sym_calc_visibility(def_sym);
  200. if (def_sym->visible != no)
  201. return def_sym;
  202. }
  203. /* any of the defaults visible? */
  204. for_all_defaults(sym, prop) {
  205. prop->visible.tri = expr_calc_value(prop->visible.expr);
  206. if (prop->visible.tri == no)
  207. continue;
  208. def_sym = prop_get_symbol(prop);
  209. sym_calc_visibility(def_sym);
  210. if (def_sym->visible != no)
  211. return def_sym;
  212. }
  213. /* just get the first visible value */
  214. prop = sym_get_choice_prop(sym);
  215. expr_list_for_each_sym(prop->expr, e, def_sym) {
  216. sym_calc_visibility(def_sym);
  217. if (def_sym->visible != no)
  218. return def_sym;
  219. }
  220. /* no choice? reset tristate value */
  221. sym->curr.tri = no;
  222. return NULL;
  223. }
  224. void sym_calc_value(struct symbol *sym)
  225. {
  226. struct symbol_value newval, oldval;
  227. struct property *prop;
  228. struct expr *e;
  229. if (!sym)
  230. return;
  231. if (sym->flags & SYMBOL_VALID)
  232. return;
  233. sym->flags |= SYMBOL_VALID;
  234. oldval = sym->curr;
  235. switch (sym->type) {
  236. case S_INT:
  237. case S_HEX:
  238. case S_STRING:
  239. newval = symbol_empty.curr;
  240. break;
  241. case S_BOOLEAN:
  242. case S_TRISTATE:
  243. newval = symbol_no.curr;
  244. break;
  245. default:
  246. sym->curr.val = sym->name;
  247. sym->curr.tri = no;
  248. return;
  249. }
  250. if (!sym_is_choice_value(sym))
  251. sym->flags &= ~SYMBOL_WRITE;
  252. sym_calc_visibility(sym);
  253. /* set default if recursively called */
  254. sym->curr = newval;
  255. switch (sym_get_type(sym)) {
  256. case S_BOOLEAN:
  257. case S_TRISTATE:
  258. if (sym_is_choice_value(sym) && sym->visible == yes) {
  259. prop = sym_get_choice_prop(sym);
  260. newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
  261. } else {
  262. if (sym->visible != no) {
  263. /* if the symbol is visible use the user value
  264. * if available, otherwise try the default value
  265. */
  266. sym->flags |= SYMBOL_WRITE;
  267. if (sym_has_value(sym)) {
  268. newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
  269. sym->visible);
  270. goto calc_newval;
  271. }
  272. }
  273. if (sym->rev_dep.tri != no)
  274. sym->flags |= SYMBOL_WRITE;
  275. if (!sym_is_choice(sym)) {
  276. prop = sym_get_default_prop(sym);
  277. if (prop) {
  278. sym->flags |= SYMBOL_WRITE;
  279. newval.tri = EXPR_AND(expr_calc_value(prop->expr),
  280. prop->visible.tri);
  281. }
  282. }
  283. calc_newval:
  284. newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
  285. }
  286. if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
  287. newval.tri = yes;
  288. break;
  289. case S_STRING:
  290. case S_HEX:
  291. case S_INT:
  292. if (sym->visible != no) {
  293. sym->flags |= SYMBOL_WRITE;
  294. if (sym_has_value(sym)) {
  295. newval.val = sym->def[S_DEF_USER].val;
  296. break;
  297. }
  298. }
  299. prop = sym_get_default_prop(sym);
  300. if (prop) {
  301. struct symbol *ds = prop_get_symbol(prop);
  302. if (ds) {
  303. sym->flags |= SYMBOL_WRITE;
  304. sym_calc_value(ds);
  305. newval.val = ds->curr.val;
  306. }
  307. }
  308. break;
  309. default:
  310. ;
  311. }
  312. sym->curr = newval;
  313. if (sym_is_choice(sym) && newval.tri == yes)
  314. sym->curr.val = sym_calc_choice(sym);
  315. sym_validate_range(sym);
  316. if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
  317. sym_set_changed(sym);
  318. if (modules_sym == sym) {
  319. sym_set_all_changed();
  320. modules_val = modules_sym->curr.tri;
  321. }
  322. }
  323. if (sym_is_choice(sym)) {
  324. struct symbol *choice_sym;
  325. int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
  326. prop = sym_get_choice_prop(sym);
  327. expr_list_for_each_sym(prop->expr, e, choice_sym) {
  328. choice_sym->flags |= flags;
  329. if (flags & SYMBOL_CHANGED)
  330. sym_set_changed(choice_sym);
  331. }
  332. }
  333. if (sym->flags & SYMBOL_AUTO)
  334. sym->flags &= ~SYMBOL_WRITE;
  335. }
  336. void sym_clear_all_valid(void)
  337. {
  338. struct symbol *sym;
  339. int i;
  340. for_all_symbols(i, sym)
  341. sym->flags &= ~SYMBOL_VALID;
  342. sym_add_change_count(1);
  343. if (modules_sym)
  344. sym_calc_value(modules_sym);
  345. }
  346. void sym_set_changed(struct symbol *sym)
  347. {
  348. struct property *prop;
  349. sym->flags |= SYMBOL_CHANGED;
  350. for (prop = sym->prop; prop; prop = prop->next) {
  351. if (prop->menu)
  352. prop->menu->flags |= MENU_CHANGED;
  353. }
  354. }
  355. void sym_set_all_changed(void)
  356. {
  357. struct symbol *sym;
  358. int i;
  359. for_all_symbols(i, sym)
  360. sym_set_changed(sym);
  361. }
  362. bool sym_tristate_within_range(struct symbol *sym, tristate val)
  363. {
  364. int type = sym_get_type(sym);
  365. if (sym->visible == no)
  366. return false;
  367. if (type != S_BOOLEAN && type != S_TRISTATE)
  368. return false;
  369. if (type == S_BOOLEAN && val == mod)
  370. return false;
  371. if (sym->visible <= sym->rev_dep.tri)
  372. return false;
  373. if (sym_is_choice_value(sym) && sym->visible == yes)
  374. return val == yes;
  375. return val >= sym->rev_dep.tri && val <= sym->visible;
  376. }
  377. bool sym_set_tristate_value(struct symbol *sym, tristate val)
  378. {
  379. tristate oldval = sym_get_tristate_value(sym);
  380. if (oldval != val && !sym_tristate_within_range(sym, val))
  381. return false;
  382. if (!(sym->flags & SYMBOL_DEF_USER)) {
  383. sym->flags |= SYMBOL_DEF_USER;
  384. sym_set_changed(sym);
  385. }
  386. /*
  387. * setting a choice value also resets the new flag of the choice
  388. * symbol and all other choice values.
  389. */
  390. if (sym_is_choice_value(sym) && val == yes) {
  391. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  392. struct property *prop;
  393. struct expr *e;
  394. cs->def[S_DEF_USER].val = sym;
  395. cs->flags |= SYMBOL_DEF_USER;
  396. prop = sym_get_choice_prop(cs);
  397. for (e = prop->expr; e; e = e->left.expr) {
  398. if (e->right.sym->visible != no)
  399. e->right.sym->flags |= SYMBOL_DEF_USER;
  400. }
  401. }
  402. sym->def[S_DEF_USER].tri = val;
  403. if (oldval != val)
  404. sym_clear_all_valid();
  405. return true;
  406. }
  407. tristate sym_toggle_tristate_value(struct symbol *sym)
  408. {
  409. tristate oldval, newval;
  410. oldval = newval = sym_get_tristate_value(sym);
  411. do {
  412. switch (newval) {
  413. case no:
  414. newval = mod;
  415. break;
  416. case mod:
  417. newval = yes;
  418. break;
  419. case yes:
  420. newval = no;
  421. break;
  422. }
  423. if (sym_set_tristate_value(sym, newval))
  424. break;
  425. } while (oldval != newval);
  426. return newval;
  427. }
  428. bool sym_string_valid(struct symbol *sym, const char *str)
  429. {
  430. signed char ch;
  431. switch (sym->type) {
  432. case S_STRING:
  433. return true;
  434. case S_INT:
  435. ch = *str++;
  436. if (ch == '-')
  437. ch = *str++;
  438. if (!isdigit(ch))
  439. return false;
  440. if (ch == '0' && *str != 0)
  441. return false;
  442. while ((ch = *str++)) {
  443. if (!isdigit(ch))
  444. return false;
  445. }
  446. return true;
  447. case S_HEX:
  448. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
  449. str += 2;
  450. ch = *str++;
  451. do {
  452. if (!isxdigit(ch))
  453. return false;
  454. } while ((ch = *str++));
  455. return true;
  456. case S_BOOLEAN:
  457. case S_TRISTATE:
  458. switch (str[0]) {
  459. case 'y': case 'Y':
  460. case 'm': case 'M':
  461. case 'n': case 'N':
  462. return true;
  463. }
  464. return false;
  465. default:
  466. return false;
  467. }
  468. }
  469. bool sym_string_within_range(struct symbol *sym, const char *str)
  470. {
  471. struct property *prop;
  472. int val;
  473. switch (sym->type) {
  474. case S_STRING:
  475. return sym_string_valid(sym, str);
  476. case S_INT:
  477. if (!sym_string_valid(sym, str))
  478. return false;
  479. prop = sym_get_range_prop(sym);
  480. if (!prop)
  481. return true;
  482. val = strtol(str, NULL, 10);
  483. return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
  484. val <= sym_get_range_val(prop->expr->right.sym, 10);
  485. case S_HEX:
  486. if (!sym_string_valid(sym, str))
  487. return false;
  488. prop = sym_get_range_prop(sym);
  489. if (!prop)
  490. return true;
  491. val = strtol(str, NULL, 16);
  492. return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
  493. val <= sym_get_range_val(prop->expr->right.sym, 16);
  494. case S_BOOLEAN:
  495. case S_TRISTATE:
  496. switch (str[0]) {
  497. case 'y': case 'Y':
  498. return sym_tristate_within_range(sym, yes);
  499. case 'm': case 'M':
  500. return sym_tristate_within_range(sym, mod);
  501. case 'n': case 'N':
  502. return sym_tristate_within_range(sym, no);
  503. }
  504. return false;
  505. default:
  506. return false;
  507. }
  508. }
  509. bool sym_set_string_value(struct symbol *sym, const char *newval)
  510. {
  511. const char *oldval;
  512. char *val;
  513. int size;
  514. switch (sym->type) {
  515. case S_BOOLEAN:
  516. case S_TRISTATE:
  517. switch (newval[0]) {
  518. case 'y': case 'Y':
  519. return sym_set_tristate_value(sym, yes);
  520. case 'm': case 'M':
  521. return sym_set_tristate_value(sym, mod);
  522. case 'n': case 'N':
  523. return sym_set_tristate_value(sym, no);
  524. }
  525. return false;
  526. default:
  527. ;
  528. }
  529. if (!sym_string_within_range(sym, newval))
  530. return false;
  531. if (!(sym->flags & SYMBOL_DEF_USER)) {
  532. sym->flags |= SYMBOL_DEF_USER;
  533. sym_set_changed(sym);
  534. }
  535. oldval = sym->def[S_DEF_USER].val;
  536. size = strlen(newval) + 1;
  537. if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
  538. size += 2;
  539. sym->def[S_DEF_USER].val = val = malloc(size);
  540. *val++ = '0';
  541. *val++ = 'x';
  542. } else if (!oldval || strcmp(oldval, newval))
  543. sym->def[S_DEF_USER].val = val = malloc(size);
  544. else
  545. return true;
  546. strcpy(val, newval);
  547. free((void *)oldval);
  548. sym_clear_all_valid();
  549. return true;
  550. }
  551. const char *sym_get_string_value(struct symbol *sym)
  552. {
  553. tristate val;
  554. switch (sym->type) {
  555. case S_BOOLEAN:
  556. case S_TRISTATE:
  557. val = sym_get_tristate_value(sym);
  558. switch (val) {
  559. case no:
  560. return "n";
  561. case mod:
  562. return "m";
  563. case yes:
  564. return "y";
  565. }
  566. break;
  567. default:
  568. ;
  569. }
  570. return (const char *)sym->curr.val;
  571. }
  572. bool sym_is_changable(struct symbol *sym)
  573. {
  574. return sym->visible > sym->rev_dep.tri;
  575. }
  576. static unsigned strhash(const char *s)
  577. {
  578. /* fnv32 hash */
  579. unsigned hash = 2166136261U;
  580. for (; *s; s++)
  581. hash = (hash ^ *s) * 0x01000193;
  582. return hash;
  583. }
  584. struct symbol *sym_lookup(const char *name, int flags)
  585. {
  586. struct symbol *symbol;
  587. char *new_name;
  588. int hash;
  589. if (name) {
  590. if (name[0] && !name[1]) {
  591. switch (name[0]) {
  592. case 'y': return &symbol_yes;
  593. case 'm': return &symbol_mod;
  594. case 'n': return &symbol_no;
  595. }
  596. }
  597. hash = strhash(name) % SYMBOL_HASHSIZE;
  598. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  599. if (symbol->name &&
  600. !strcmp(symbol->name, name) &&
  601. (flags ? symbol->flags & flags
  602. : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
  603. return symbol;
  604. }
  605. new_name = strdup(name);
  606. } else {
  607. new_name = NULL;
  608. hash = 0;
  609. }
  610. symbol = malloc(sizeof(*symbol));
  611. memset(symbol, 0, sizeof(*symbol));
  612. symbol->name = new_name;
  613. symbol->type = S_UNKNOWN;
  614. symbol->flags |= flags;
  615. symbol->next = symbol_hash[hash];
  616. symbol_hash[hash] = symbol;
  617. return symbol;
  618. }
  619. struct symbol *sym_find(const char *name)
  620. {
  621. struct symbol *symbol = NULL;
  622. int hash = 0;
  623. if (!name)
  624. return NULL;
  625. if (name[0] && !name[1]) {
  626. switch (name[0]) {
  627. case 'y': return &symbol_yes;
  628. case 'm': return &symbol_mod;
  629. case 'n': return &symbol_no;
  630. }
  631. }
  632. hash = strhash(name) % SYMBOL_HASHSIZE;
  633. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  634. if (symbol->name &&
  635. !strcmp(symbol->name, name) &&
  636. !(symbol->flags & SYMBOL_CONST))
  637. break;
  638. }
  639. return symbol;
  640. }
  641. struct symbol **sym_re_search(const char *pattern)
  642. {
  643. struct symbol *sym, **sym_arr = NULL;
  644. int i, cnt, size;
  645. regex_t re;
  646. cnt = size = 0;
  647. /* Skip if empty */
  648. if (strlen(pattern) == 0)
  649. return NULL;
  650. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
  651. return NULL;
  652. for_all_symbols(i, sym) {
  653. if (sym->flags & SYMBOL_CONST || !sym->name)
  654. continue;
  655. if (regexec(&re, sym->name, 0, NULL, 0))
  656. continue;
  657. if (cnt + 1 >= size) {
  658. void *tmp = sym_arr;
  659. size += 16;
  660. sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
  661. if (!sym_arr) {
  662. free(tmp);
  663. return NULL;
  664. }
  665. }
  666. sym_calc_value(sym);
  667. sym_arr[cnt++] = sym;
  668. }
  669. if (sym_arr)
  670. sym_arr[cnt] = NULL;
  671. regfree(&re);
  672. return sym_arr;
  673. }
  674. static struct symbol *sym_check_expr_deps(struct expr *e)
  675. {
  676. struct symbol *sym;
  677. if (!e)
  678. return NULL;
  679. switch (e->type) {
  680. case E_OR:
  681. case E_AND:
  682. sym = sym_check_expr_deps(e->left.expr);
  683. if (sym)
  684. return sym;
  685. return sym_check_expr_deps(e->right.expr);
  686. case E_NOT:
  687. return sym_check_expr_deps(e->left.expr);
  688. case E_EQUAL:
  689. case E_UNEQUAL:
  690. sym = sym_check_deps(e->left.sym);
  691. if (sym)
  692. return sym;
  693. return sym_check_deps(e->right.sym);
  694. case E_SYMBOL:
  695. return sym_check_deps(e->left.sym);
  696. default:
  697. break;
  698. }
  699. printf("Oops! How to check %d?\n", e->type);
  700. return NULL;
  701. }
  702. /* return NULL when dependencies are OK */
  703. static struct symbol *sym_check_sym_deps(struct symbol *sym)
  704. {
  705. struct symbol *sym2;
  706. struct property *prop;
  707. sym2 = sym_check_expr_deps(sym->rev_dep.expr);
  708. if (sym2)
  709. return sym2;
  710. for (prop = sym->prop; prop; prop = prop->next) {
  711. if (prop->type == P_CHOICE || prop->type == P_SELECT)
  712. continue;
  713. sym2 = sym_check_expr_deps(prop->visible.expr);
  714. if (sym2)
  715. break;
  716. if (prop->type != P_DEFAULT || sym_is_choice(sym))
  717. continue;
  718. sym2 = sym_check_expr_deps(prop->expr);
  719. if (sym2)
  720. break;
  721. }
  722. return sym2;
  723. }
  724. static struct symbol *sym_check_choice_deps(struct symbol *choice)
  725. {
  726. struct symbol *sym, *sym2;
  727. struct property *prop;
  728. struct expr *e;
  729. prop = sym_get_choice_prop(choice);
  730. expr_list_for_each_sym(prop->expr, e, sym)
  731. sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  732. choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  733. sym2 = sym_check_sym_deps(choice);
  734. choice->flags &= ~SYMBOL_CHECK;
  735. if (sym2)
  736. goto out;
  737. expr_list_for_each_sym(prop->expr, e, sym) {
  738. sym2 = sym_check_sym_deps(sym);
  739. if (sym2) {
  740. fprintf(stderr, " -> %s", sym->name);
  741. break;
  742. }
  743. }
  744. out:
  745. expr_list_for_each_sym(prop->expr, e, sym)
  746. sym->flags &= ~SYMBOL_CHECK;
  747. if (sym2 && sym_is_choice_value(sym2) &&
  748. prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
  749. sym2 = choice;
  750. return sym2;
  751. }
  752. struct symbol *sym_check_deps(struct symbol *sym)
  753. {
  754. struct symbol *sym2;
  755. struct property *prop;
  756. if (sym->flags & SYMBOL_CHECK) {
  757. fprintf(stderr, "%s:%d:error: found recursive dependency: %s",
  758. sym->prop->file->name, sym->prop->lineno,
  759. sym->name ? sym->name : "<choice>");
  760. return sym;
  761. }
  762. if (sym->flags & SYMBOL_CHECKED)
  763. return NULL;
  764. if (sym_is_choice_value(sym)) {
  765. /* for choice groups start the check with main choice symbol */
  766. prop = sym_get_choice_prop(sym);
  767. sym2 = sym_check_deps(prop_get_symbol(prop));
  768. } else if (sym_is_choice(sym)) {
  769. sym2 = sym_check_choice_deps(sym);
  770. } else {
  771. sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  772. sym2 = sym_check_sym_deps(sym);
  773. sym->flags &= ~SYMBOL_CHECK;
  774. }
  775. if (sym2) {
  776. fprintf(stderr, " -> %s", sym->name ? sym->name : "<choice>");
  777. if (sym2 == sym) {
  778. fprintf(stderr, "\n");
  779. zconfnerrs++;
  780. sym2 = NULL;
  781. }
  782. }
  783. return sym2;
  784. }
  785. struct property *prop_alloc(enum prop_type type, struct symbol *sym)
  786. {
  787. struct property *prop;
  788. struct property **propp;
  789. prop = malloc(sizeof(*prop));
  790. memset(prop, 0, sizeof(*prop));
  791. prop->type = type;
  792. prop->sym = sym;
  793. prop->file = current_file;
  794. prop->lineno = zconf_lineno();
  795. /* append property to the prop list of symbol */
  796. if (sym) {
  797. for (propp = &sym->prop; *propp; propp = &(*propp)->next)
  798. ;
  799. *propp = prop;
  800. }
  801. return prop;
  802. }
  803. struct symbol *prop_get_symbol(struct property *prop)
  804. {
  805. if (prop->expr && (prop->expr->type == E_SYMBOL ||
  806. prop->expr->type == E_LIST))
  807. return prop->expr->left.sym;
  808. return NULL;
  809. }
  810. const char *prop_get_type_name(enum prop_type type)
  811. {
  812. switch (type) {
  813. case P_PROMPT:
  814. return "prompt";
  815. case P_ENV:
  816. return "env";
  817. case P_COMMENT:
  818. return "comment";
  819. case P_MENU:
  820. return "menu";
  821. case P_DEFAULT:
  822. return "default";
  823. case P_CHOICE:
  824. return "choice";
  825. case P_SELECT:
  826. return "select";
  827. case P_RANGE:
  828. return "range";
  829. case P_UNKNOWN:
  830. break;
  831. }
  832. return "unknown";
  833. }
  834. static void prop_add_env(const char *env)
  835. {
  836. struct symbol *sym, *sym2;
  837. struct property *prop;
  838. char *p;
  839. sym = current_entry->sym;
  840. sym->flags |= SYMBOL_AUTO;
  841. for_all_properties(sym, prop, P_ENV) {
  842. sym2 = prop_get_symbol(prop);
  843. if (strcmp(sym2->name, env))
  844. menu_warn(current_entry, "redefining environment symbol from %s",
  845. sym2->name);
  846. return;
  847. }
  848. prop = prop_alloc(P_ENV, sym);
  849. prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
  850. sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
  851. sym_env_list->right.sym = sym;
  852. p = getenv(env);
  853. if (p)
  854. sym_add_default(sym, p);
  855. else
  856. menu_warn(current_entry, "environment variable %s undefined", env);
  857. }