symbol.c 17 KB

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