symbol.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. /* tristate always enabled */
  182. #if 0
  183. if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
  184. #else
  185. if (tri == mod && (sym->type != S_TRISTATE))
  186. #endif
  187. tri = yes;
  188. if (sym->visible != tri) {
  189. sym->visible = tri;
  190. sym_set_changed(sym);
  191. }
  192. if (sym_is_choice_value(sym))
  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 (E_OR(sym->visible, sym->rev_dep.tri) != no) {
  276. sym->flags |= SYMBOL_WRITE;
  277. if (sym_has_value(sym))
  278. newval.tri = sym->user.tri;
  279. else if (!sym_is_choice(sym)) {
  280. prop = sym_get_default_prop(sym);
  281. if (prop)
  282. newval.tri = expr_calc_value(prop->expr);
  283. }
  284. newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
  285. } else if (!sym_is_choice(sym)) {
  286. prop = sym_get_default_prop(sym);
  287. if (prop) {
  288. sym->flags |= SYMBOL_WRITE;
  289. newval.tri = expr_calc_value(prop->expr);
  290. }
  291. }
  292. if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
  293. newval.tri = yes;
  294. break;
  295. case S_STRING:
  296. case S_HEX:
  297. case S_INT:
  298. if (sym->visible != no) {
  299. sym->flags |= SYMBOL_WRITE;
  300. if (sym_has_value(sym)) {
  301. newval.val = sym->user.val;
  302. break;
  303. }
  304. }
  305. prop = sym_get_default_prop(sym);
  306. if (prop) {
  307. struct symbol *ds = prop_get_symbol(prop);
  308. if (ds) {
  309. sym->flags |= SYMBOL_WRITE;
  310. sym_calc_value(ds);
  311. newval.val = ds->curr.val;
  312. }
  313. }
  314. break;
  315. default:
  316. ;
  317. }
  318. sym->curr = newval;
  319. if (sym_is_choice(sym) && newval.tri == yes)
  320. sym->curr.val = sym_calc_choice(sym);
  321. sym_validate_range(sym);
  322. if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
  323. sym_set_changed(sym);
  324. if (modules_sym == sym)
  325. modules_val = modules_sym->curr.tri;
  326. if (sym_is_choice(sym)) {
  327. int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
  328. prop = sym_get_choice_prop(sym);
  329. for (e = prop->expr; e; e = e->left.expr) {
  330. e->right.sym->flags |= flags;
  331. if (flags & SYMBOL_CHANGED)
  332. sym_set_changed(e->right.sym);
  333. }
  334. }
  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_change_count++;
  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_NEW) {
  383. sym->flags &= ~SYMBOL_NEW;
  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->user.val = sym;
  395. cs->flags &= ~SYMBOL_NEW;
  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_NEW;
  400. }
  401. }
  402. sym->user.tri = val;
  403. if (oldval != val) {
  404. sym_clear_all_valid();
  405. if (sym == modules_sym)
  406. sym_set_all_changed();
  407. }
  408. return true;
  409. }
  410. tristate sym_toggle_tristate_value(struct symbol *sym)
  411. {
  412. tristate oldval, newval;
  413. oldval = newval = sym_get_tristate_value(sym);
  414. do {
  415. switch (newval) {
  416. case no:
  417. newval = mod;
  418. break;
  419. case mod:
  420. newval = yes;
  421. break;
  422. case yes:
  423. newval = no;
  424. break;
  425. }
  426. if (sym_set_tristate_value(sym, newval))
  427. break;
  428. } while (oldval != newval);
  429. return newval;
  430. }
  431. bool sym_string_valid(struct symbol *sym, const char *str)
  432. {
  433. signed char ch;
  434. switch (sym->type) {
  435. case S_STRING:
  436. return true;
  437. case S_INT:
  438. ch = *str++;
  439. if (ch == '-')
  440. ch = *str++;
  441. if (!isdigit(ch))
  442. return false;
  443. if (ch == '0' && *str != 0)
  444. return false;
  445. while ((ch = *str++)) {
  446. if (!isdigit(ch))
  447. return false;
  448. }
  449. return true;
  450. case S_HEX:
  451. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
  452. str += 2;
  453. ch = *str++;
  454. do {
  455. if (!isxdigit(ch))
  456. return false;
  457. } while ((ch = *str++));
  458. return true;
  459. case S_BOOLEAN:
  460. case S_TRISTATE:
  461. switch (str[0]) {
  462. case 'y': case 'Y':
  463. case 'm': case 'M':
  464. case 'n': case 'N':
  465. return true;
  466. }
  467. return false;
  468. default:
  469. return false;
  470. }
  471. }
  472. bool sym_string_within_range(struct symbol *sym, const char *str)
  473. {
  474. struct property *prop;
  475. int val;
  476. switch (sym->type) {
  477. case S_STRING:
  478. return sym_string_valid(sym, str);
  479. case S_INT:
  480. if (!sym_string_valid(sym, str))
  481. return false;
  482. prop = sym_get_range_prop(sym);
  483. if (!prop)
  484. return true;
  485. val = strtol(str, NULL, 10);
  486. return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
  487. val <= sym_get_range_val(prop->expr->right.sym, 10);
  488. case S_HEX:
  489. if (!sym_string_valid(sym, str))
  490. return false;
  491. prop = sym_get_range_prop(sym);
  492. if (!prop)
  493. return true;
  494. val = strtol(str, NULL, 16);
  495. return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
  496. val <= sym_get_range_val(prop->expr->right.sym, 16);
  497. case S_BOOLEAN:
  498. case S_TRISTATE:
  499. switch (str[0]) {
  500. case 'y': case 'Y':
  501. return sym_tristate_within_range(sym, yes);
  502. case 'm': case 'M':
  503. return sym_tristate_within_range(sym, mod);
  504. case 'n': case 'N':
  505. return sym_tristate_within_range(sym, no);
  506. }
  507. return false;
  508. default:
  509. return false;
  510. }
  511. }
  512. bool sym_set_string_value(struct symbol *sym, const char *newval)
  513. {
  514. const char *oldval;
  515. char *val;
  516. int size;
  517. switch (sym->type) {
  518. case S_BOOLEAN:
  519. case S_TRISTATE:
  520. switch (newval[0]) {
  521. case 'y': case 'Y':
  522. return sym_set_tristate_value(sym, yes);
  523. case 'm': case 'M':
  524. return sym_set_tristate_value(sym, mod);
  525. case 'n': case 'N':
  526. return sym_set_tristate_value(sym, no);
  527. }
  528. return false;
  529. default:
  530. ;
  531. }
  532. if (!sym_string_within_range(sym, newval))
  533. return false;
  534. if (sym->flags & SYMBOL_NEW) {
  535. sym->flags &= ~SYMBOL_NEW;
  536. sym_set_changed(sym);
  537. }
  538. oldval = sym->user.val;
  539. size = strlen(newval) + 1;
  540. if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
  541. size += 2;
  542. sym->user.val = val = malloc(size);
  543. *val++ = '0';
  544. *val++ = 'x';
  545. } else if (!oldval || strcmp(oldval, newval))
  546. sym->user.val = val = malloc(size);
  547. else
  548. return true;
  549. strcpy(val, newval);
  550. free((void *)oldval);
  551. sym_clear_all_valid();
  552. return true;
  553. }
  554. const char *sym_get_string_value(struct symbol *sym)
  555. {
  556. tristate val;
  557. switch (sym->type) {
  558. case S_BOOLEAN:
  559. case S_TRISTATE:
  560. val = sym_get_tristate_value(sym);
  561. switch (val) {
  562. case no:
  563. return "n";
  564. case mod:
  565. return "m";
  566. case yes:
  567. return "y";
  568. }
  569. break;
  570. default:
  571. ;
  572. }
  573. return (const char *)sym->curr.val;
  574. }
  575. bool sym_is_changable(struct symbol *sym)
  576. {
  577. return sym->visible > sym->rev_dep.tri;
  578. }
  579. struct symbol *sym_lookup(const char *name, int isconst)
  580. {
  581. struct symbol *symbol;
  582. const char *ptr;
  583. char *new_name;
  584. int hash = 0;
  585. if (name) {
  586. if (name[0] && !name[1]) {
  587. switch (name[0]) {
  588. case 'y': return &symbol_yes;
  589. case 'm': return &symbol_mod;
  590. case 'n': return &symbol_no;
  591. }
  592. }
  593. for (ptr = name; *ptr; ptr++)
  594. hash += *ptr;
  595. hash &= 0xff;
  596. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  597. if (!strcmp(symbol->name, name)) {
  598. if ((isconst && symbol->flags & SYMBOL_CONST) ||
  599. (!isconst && !(symbol->flags & SYMBOL_CONST)))
  600. return symbol;
  601. }
  602. }
  603. new_name = strdup(name);
  604. } else {
  605. new_name = NULL;
  606. hash = 256;
  607. }
  608. symbol = malloc(sizeof(*symbol));
  609. memset(symbol, 0, sizeof(*symbol));
  610. symbol->name = new_name;
  611. symbol->type = S_UNKNOWN;
  612. symbol->flags = SYMBOL_NEW;
  613. if (isconst)
  614. symbol->flags |= SYMBOL_CONST;
  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. const char *ptr;
  623. int hash = 0;
  624. if (!name)
  625. return NULL;
  626. if (name[0] && !name[1]) {
  627. switch (name[0]) {
  628. case 'y': return &symbol_yes;
  629. case 'm': return &symbol_mod;
  630. case 'n': return &symbol_no;
  631. }
  632. }
  633. for (ptr = name; *ptr; ptr++)
  634. hash += *ptr;
  635. hash &= 0xff;
  636. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  637. if (!strcmp(symbol->name, name) &&
  638. !(symbol->flags & SYMBOL_CONST))
  639. break;
  640. }
  641. return symbol;
  642. }
  643. struct symbol **sym_re_search(const char *pattern)
  644. {
  645. struct symbol *sym, **sym_arr = NULL;
  646. int i, cnt, size;
  647. regex_t re;
  648. cnt = size = 0;
  649. /* Skip if empty */
  650. if (strlen(pattern) == 0)
  651. return NULL;
  652. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
  653. return NULL;
  654. for_all_symbols(i, sym) {
  655. if (sym->flags & SYMBOL_CONST || !sym->name)
  656. continue;
  657. if (regexec(&re, sym->name, 0, NULL, 0))
  658. continue;
  659. if (cnt + 1 >= size) {
  660. void *tmp = sym_arr;
  661. size += 16;
  662. sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
  663. if (!sym_arr) {
  664. free(tmp);
  665. return NULL;
  666. }
  667. }
  668. sym_arr[cnt++] = sym;
  669. }
  670. if (sym_arr)
  671. sym_arr[cnt] = NULL;
  672. regfree(&re);
  673. return sym_arr;
  674. }
  675. struct symbol *sym_check_deps(struct symbol *sym);
  676. static struct symbol *sym_check_expr_deps(struct expr *e)
  677. {
  678. struct symbol *sym;
  679. if (!e)
  680. return NULL;
  681. switch (e->type) {
  682. case E_OR:
  683. case E_AND:
  684. sym = sym_check_expr_deps(e->left.expr);
  685. if (sym)
  686. return sym;
  687. return sym_check_expr_deps(e->right.expr);
  688. case E_NOT:
  689. return sym_check_expr_deps(e->left.expr);
  690. case E_EQUAL:
  691. case E_UNEQUAL:
  692. sym = sym_check_deps(e->left.sym);
  693. if (sym)
  694. return sym;
  695. return sym_check_deps(e->right.sym);
  696. case E_SYMBOL:
  697. return sym_check_deps(e->left.sym);
  698. default:
  699. break;
  700. }
  701. printf("Oops! How to check %d?\n", e->type);
  702. return NULL;
  703. }
  704. struct symbol *sym_check_deps(struct symbol *sym)
  705. {
  706. struct symbol *sym2;
  707. struct property *prop;
  708. if (sym->flags & SYMBOL_CHECK) {
  709. printf("Warning! Found recursive dependency: %s", sym->name);
  710. return sym;
  711. }
  712. if (sym->flags & SYMBOL_CHECKED)
  713. return NULL;
  714. sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  715. sym2 = sym_check_expr_deps(sym->rev_dep.expr);
  716. if (sym2)
  717. goto out;
  718. for (prop = sym->prop; prop; prop = prop->next) {
  719. if (prop->type == P_CHOICE || prop->type == P_SELECT)
  720. continue;
  721. sym2 = sym_check_expr_deps(prop->visible.expr);
  722. if (sym2)
  723. goto out;
  724. if (prop->type != P_DEFAULT || sym_is_choice(sym))
  725. continue;
  726. sym2 = sym_check_expr_deps(prop->expr);
  727. if (sym2)
  728. goto out;
  729. }
  730. out:
  731. if (sym2) {
  732. printf(" %s", sym->name);
  733. if (sym2 == sym) {
  734. printf("\n");
  735. sym2 = NULL;
  736. }
  737. }
  738. sym->flags &= ~SYMBOL_CHECK;
  739. return sym2;
  740. }
  741. struct property *prop_alloc(enum prop_type type, struct symbol *sym)
  742. {
  743. struct property *prop;
  744. struct property **propp;
  745. prop = malloc(sizeof(*prop));
  746. memset(prop, 0, sizeof(*prop));
  747. prop->type = type;
  748. prop->sym = sym;
  749. prop->file = current_file;
  750. prop->lineno = zconf_lineno();
  751. /* append property to the prop list of symbol */
  752. if (sym) {
  753. for (propp = &sym->prop; *propp; propp = &(*propp)->next)
  754. ;
  755. *propp = prop;
  756. }
  757. return prop;
  758. }
  759. struct symbol *prop_get_symbol(struct property *prop)
  760. {
  761. if (prop->expr && (prop->expr->type == E_SYMBOL ||
  762. prop->expr->type == E_CHOICE))
  763. return prop->expr->left.sym;
  764. return NULL;
  765. }
  766. const char *prop_get_type_name(enum prop_type type)
  767. {
  768. switch (type) {
  769. case P_PROMPT:
  770. return "prompt";
  771. case P_COMMENT:
  772. return "comment";
  773. case P_MENU:
  774. return "menu";
  775. case P_DEFAULT:
  776. return "default";
  777. case P_CHOICE:
  778. return "choice";
  779. case P_SELECT:
  780. return "select";
  781. case P_RANGE:
  782. return "range";
  783. case P_UNKNOWN:
  784. break;
  785. }
  786. return "unknown";
  787. }