symbol.c 18 KB

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