symbol.c 17 KB

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