symbol.c 18 KB

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