symbol.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /*
  2. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  3. * Released under the terms of the GNU GPL v2.0.
  4. */
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <regex.h>
  9. #include <sys/utsname.h>
  10. #define LKC_DIRECT_LINK
  11. #include "lkc.h"
  12. struct symbol symbol_yes = {
  13. .name = "y",
  14. .curr = { "y", yes },
  15. .flags = SYMBOL_CONST|SYMBOL_VALID,
  16. }, symbol_mod = {
  17. .name = "m",
  18. .curr = { "m", mod },
  19. .flags = SYMBOL_CONST|SYMBOL_VALID,
  20. }, symbol_no = {
  21. .name = "n",
  22. .curr = { "n", no },
  23. .flags = SYMBOL_CONST|SYMBOL_VALID,
  24. }, symbol_empty = {
  25. .name = "",
  26. .curr = { "", no },
  27. .flags = SYMBOL_VALID,
  28. };
  29. struct symbol *sym_defconfig_list;
  30. struct symbol *modules_sym;
  31. tristate modules_val;
  32. 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("VERSION", 0);
  54. sym->type = S_STRING;
  55. sym->flags |= SYMBOL_AUTO;
  56. p = getenv("VERSION");
  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->def[S_DEF_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->def[S_DEF_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->def[S_DEF_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. sym_set_all_changed();
  318. modules_val = modules_sym->curr.tri;
  319. }
  320. }
  321. if (sym_is_choice(sym)) {
  322. int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
  323. prop = sym_get_choice_prop(sym);
  324. for (e = prop->expr; e; e = e->left.expr) {
  325. e->right.sym->flags |= flags;
  326. if (flags & SYMBOL_CHANGED)
  327. sym_set_changed(e->right.sym);
  328. }
  329. }
  330. }
  331. void sym_clear_all_valid(void)
  332. {
  333. struct symbol *sym;
  334. int i;
  335. for_all_symbols(i, sym)
  336. sym->flags &= ~SYMBOL_VALID;
  337. sym_add_change_count(1);
  338. if (modules_sym)
  339. sym_calc_value(modules_sym);
  340. }
  341. void sym_set_changed(struct symbol *sym)
  342. {
  343. struct property *prop;
  344. sym->flags |= SYMBOL_CHANGED;
  345. for (prop = sym->prop; prop; prop = prop->next) {
  346. if (prop->menu)
  347. prop->menu->flags |= MENU_CHANGED;
  348. }
  349. }
  350. void sym_set_all_changed(void)
  351. {
  352. struct symbol *sym;
  353. int i;
  354. for_all_symbols(i, sym)
  355. sym_set_changed(sym);
  356. }
  357. bool sym_tristate_within_range(struct symbol *sym, tristate val)
  358. {
  359. int type = sym_get_type(sym);
  360. if (sym->visible == no)
  361. return false;
  362. if (type != S_BOOLEAN && type != S_TRISTATE)
  363. return false;
  364. if (type == S_BOOLEAN && val == mod)
  365. return false;
  366. if (sym->visible <= sym->rev_dep.tri)
  367. return false;
  368. if (sym_is_choice_value(sym) && sym->visible == yes)
  369. return val == yes;
  370. return val >= sym->rev_dep.tri && val <= sym->visible;
  371. }
  372. bool sym_set_tristate_value(struct symbol *sym, tristate val)
  373. {
  374. tristate oldval = sym_get_tristate_value(sym);
  375. if (oldval != val && !sym_tristate_within_range(sym, val))
  376. return false;
  377. if (!(sym->flags & SYMBOL_DEF_USER)) {
  378. sym->flags |= SYMBOL_DEF_USER;
  379. sym_set_changed(sym);
  380. }
  381. /*
  382. * setting a choice value also resets the new flag of the choice
  383. * symbol and all other choice values.
  384. */
  385. if (sym_is_choice_value(sym) && val == yes) {
  386. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  387. struct property *prop;
  388. struct expr *e;
  389. cs->def[S_DEF_USER].val = sym;
  390. cs->flags |= SYMBOL_DEF_USER;
  391. prop = sym_get_choice_prop(cs);
  392. for (e = prop->expr; e; e = e->left.expr) {
  393. if (e->right.sym->visible != no)
  394. e->right.sym->flags |= SYMBOL_DEF_USER;
  395. }
  396. }
  397. sym->def[S_DEF_USER].tri = val;
  398. if (oldval != val)
  399. sym_clear_all_valid();
  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_DEF_USER)) {
  527. sym->flags |= SYMBOL_DEF_USER;
  528. sym_set_changed(sym);
  529. }
  530. oldval = sym->def[S_DEF_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->def[S_DEF_USER].val = val = malloc(size);
  535. *val++ = '0';
  536. *val++ = 'x';
  537. } else if (!oldval || strcmp(oldval, newval))
  538. sym->def[S_DEF_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. if (isconst)
  605. symbol->flags |= SYMBOL_CONST;
  606. symbol->next = symbol_hash[hash];
  607. symbol_hash[hash] = symbol;
  608. return symbol;
  609. }
  610. struct symbol *sym_find(const char *name)
  611. {
  612. struct symbol *symbol = NULL;
  613. const char *ptr;
  614. int hash = 0;
  615. if (!name)
  616. return NULL;
  617. if (name[0] && !name[1]) {
  618. switch (name[0]) {
  619. case 'y': return &symbol_yes;
  620. case 'm': return &symbol_mod;
  621. case 'n': return &symbol_no;
  622. }
  623. }
  624. for (ptr = name; *ptr; ptr++)
  625. hash += *ptr;
  626. hash &= 0xff;
  627. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  628. if (!strcmp(symbol->name, name) &&
  629. !(symbol->flags & SYMBOL_CONST))
  630. break;
  631. }
  632. return symbol;
  633. }
  634. struct symbol **sym_re_search(const char *pattern)
  635. {
  636. struct symbol *sym, **sym_arr = NULL;
  637. int i, cnt, size;
  638. regex_t re;
  639. cnt = size = 0;
  640. /* Skip if empty */
  641. if (strlen(pattern) == 0)
  642. return NULL;
  643. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
  644. return NULL;
  645. for_all_symbols(i, sym) {
  646. if (sym->flags & SYMBOL_CONST || !sym->name)
  647. continue;
  648. if (regexec(&re, sym->name, 0, NULL, 0))
  649. continue;
  650. if (cnt + 1 >= size) {
  651. void *tmp = sym_arr;
  652. size += 16;
  653. sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
  654. if (!sym_arr) {
  655. free(tmp);
  656. return NULL;
  657. }
  658. }
  659. sym_arr[cnt++] = sym;
  660. }
  661. if (sym_arr)
  662. sym_arr[cnt] = NULL;
  663. regfree(&re);
  664. return sym_arr;
  665. }
  666. struct symbol *sym_check_deps(struct symbol *sym);
  667. static struct symbol *sym_check_expr_deps(struct expr *e)
  668. {
  669. struct symbol *sym;
  670. if (!e)
  671. return NULL;
  672. switch (e->type) {
  673. case E_OR:
  674. case E_AND:
  675. sym = sym_check_expr_deps(e->left.expr);
  676. if (sym)
  677. return sym;
  678. return sym_check_expr_deps(e->right.expr);
  679. case E_NOT:
  680. return sym_check_expr_deps(e->left.expr);
  681. case E_EQUAL:
  682. case E_UNEQUAL:
  683. sym = sym_check_deps(e->left.sym);
  684. if (sym)
  685. return sym;
  686. return sym_check_deps(e->right.sym);
  687. case E_SYMBOL:
  688. return sym_check_deps(e->left.sym);
  689. default:
  690. break;
  691. }
  692. printf("Oops! How to check %d?\n", e->type);
  693. return NULL;
  694. }
  695. struct symbol *sym_check_deps(struct symbol *sym)
  696. {
  697. struct symbol *sym2;
  698. struct property *prop;
  699. if (sym->flags & SYMBOL_CHECK) {
  700. printf("Warning! Found recursive dependency: %s", sym->name);
  701. return sym;
  702. }
  703. if (sym->flags & SYMBOL_CHECKED)
  704. return NULL;
  705. sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  706. sym2 = sym_check_expr_deps(sym->rev_dep.expr);
  707. if (sym2)
  708. goto out;
  709. for (prop = sym->prop; prop; prop = prop->next) {
  710. if (prop->type == P_CHOICE || prop->type == P_SELECT)
  711. continue;
  712. sym2 = sym_check_expr_deps(prop->visible.expr);
  713. if (sym2)
  714. goto out;
  715. if (prop->type != P_DEFAULT || sym_is_choice(sym))
  716. continue;
  717. sym2 = sym_check_expr_deps(prop->expr);
  718. if (sym2)
  719. goto out;
  720. }
  721. out:
  722. if (sym2) {
  723. printf(" %s", sym->name);
  724. if (sym2 == sym) {
  725. printf("\n");
  726. sym2 = NULL;
  727. }
  728. }
  729. sym->flags &= ~SYMBOL_CHECK;
  730. return sym2;
  731. }
  732. struct property *prop_alloc(enum prop_type type, struct symbol *sym)
  733. {
  734. struct property *prop;
  735. struct property **propp;
  736. prop = malloc(sizeof(*prop));
  737. memset(prop, 0, sizeof(*prop));
  738. prop->type = type;
  739. prop->sym = sym;
  740. prop->file = current_file;
  741. prop->lineno = zconf_lineno();
  742. /* append property to the prop list of symbol */
  743. if (sym) {
  744. for (propp = &sym->prop; *propp; propp = &(*propp)->next)
  745. ;
  746. *propp = prop;
  747. }
  748. return prop;
  749. }
  750. struct symbol *prop_get_symbol(struct property *prop)
  751. {
  752. if (prop->expr && (prop->expr->type == E_SYMBOL ||
  753. prop->expr->type == E_CHOICE))
  754. return prop->expr->left.sym;
  755. return NULL;
  756. }
  757. const char *prop_get_type_name(enum prop_type type)
  758. {
  759. switch (type) {
  760. case P_PROMPT:
  761. return "prompt";
  762. case P_COMMENT:
  763. return "comment";
  764. case P_MENU:
  765. return "menu";
  766. case P_DEFAULT:
  767. return "default";
  768. case P_CHOICE:
  769. return "choice";
  770. case P_SELECT:
  771. return "select";
  772. case P_RANGE:
  773. return "range";
  774. case P_UNKNOWN:
  775. break;
  776. }
  777. return "unknown";
  778. }