symbol.c 18 KB

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