PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/kconfig/symbol.c

https://github.com/ab3416/linux-2.6
C | 1303 lines | 1139 code | 115 blank | 49 comment | 212 complexity | 1ccaddef82f4949d9f8d3b3fdb83345c MD5 | raw file
  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. #include "lkc.h"
  11. struct symbol symbol_yes = {
  12. .name = "y",
  13. .curr = { "y", yes },
  14. .flags = SYMBOL_CONST|SYMBOL_VALID,
  15. }, symbol_mod = {
  16. .name = "m",
  17. .curr = { "m", mod },
  18. .flags = SYMBOL_CONST|SYMBOL_VALID,
  19. }, symbol_no = {
  20. .name = "n",
  21. .curr = { "n", no },
  22. .flags = SYMBOL_CONST|SYMBOL_VALID,
  23. }, symbol_empty = {
  24. .name = "",
  25. .curr = { "", no },
  26. .flags = SYMBOL_VALID,
  27. };
  28. struct symbol *sym_defconfig_list;
  29. struct symbol *modules_sym;
  30. tristate modules_val;
  31. struct expr *sym_env_list;
  32. static 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, SYMBOL_CONST));
  36. }
  37. void sym_init(void)
  38. {
  39. struct symbol *sym;
  40. struct utsname uts;
  41. static bool inited = false;
  42. if (inited)
  43. return;
  44. inited = true;
  45. uname(&uts);
  46. sym = sym_lookup("UNAME_RELEASE", 0);
  47. sym->type = S_STRING;
  48. sym->flags |= SYMBOL_AUTO;
  49. sym_add_default(sym, uts.release);
  50. }
  51. enum symbol_type sym_get_type(struct symbol *sym)
  52. {
  53. enum symbol_type type = sym->type;
  54. if (type == S_TRISTATE) {
  55. if (sym_is_choice_value(sym) && sym->visible == yes)
  56. type = S_BOOLEAN;
  57. else if (modules_val == no)
  58. type = S_BOOLEAN;
  59. }
  60. return type;
  61. }
  62. const char *sym_type_name(enum symbol_type type)
  63. {
  64. switch (type) {
  65. case S_BOOLEAN:
  66. return "boolean";
  67. case S_TRISTATE:
  68. return "tristate";
  69. case S_INT:
  70. return "integer";
  71. case S_HEX:
  72. return "hex";
  73. case S_STRING:
  74. return "string";
  75. case S_UNKNOWN:
  76. return "unknown";
  77. case S_OTHER:
  78. break;
  79. }
  80. return "???";
  81. }
  82. struct property *sym_get_choice_prop(struct symbol *sym)
  83. {
  84. struct property *prop;
  85. for_all_choices(sym, prop)
  86. return prop;
  87. return NULL;
  88. }
  89. struct property *sym_get_env_prop(struct symbol *sym)
  90. {
  91. struct property *prop;
  92. for_all_properties(sym, prop, P_ENV)
  93. return prop;
  94. return NULL;
  95. }
  96. struct property *sym_get_default_prop(struct symbol *sym)
  97. {
  98. struct property *prop;
  99. for_all_defaults(sym, prop) {
  100. prop->visible.tri = expr_calc_value(prop->visible.expr);
  101. if (prop->visible.tri != no)
  102. return prop;
  103. }
  104. return NULL;
  105. }
  106. static struct property *sym_get_range_prop(struct symbol *sym)
  107. {
  108. struct property *prop;
  109. for_all_properties(sym, prop, P_RANGE) {
  110. prop->visible.tri = expr_calc_value(prop->visible.expr);
  111. if (prop->visible.tri != no)
  112. return prop;
  113. }
  114. return NULL;
  115. }
  116. static int sym_get_range_val(struct symbol *sym, int base)
  117. {
  118. sym_calc_value(sym);
  119. switch (sym->type) {
  120. case S_INT:
  121. base = 10;
  122. break;
  123. case S_HEX:
  124. base = 16;
  125. break;
  126. default:
  127. break;
  128. }
  129. return strtol(sym->curr.val, NULL, base);
  130. }
  131. static void sym_validate_range(struct symbol *sym)
  132. {
  133. struct property *prop;
  134. int base, val, val2;
  135. char str[64];
  136. switch (sym->type) {
  137. case S_INT:
  138. base = 10;
  139. break;
  140. case S_HEX:
  141. base = 16;
  142. break;
  143. default:
  144. return;
  145. }
  146. prop = sym_get_range_prop(sym);
  147. if (!prop)
  148. return;
  149. val = strtol(sym->curr.val, NULL, base);
  150. val2 = sym_get_range_val(prop->expr->left.sym, base);
  151. if (val >= val2) {
  152. val2 = sym_get_range_val(prop->expr->right.sym, base);
  153. if (val <= val2)
  154. return;
  155. }
  156. if (sym->type == S_INT)
  157. sprintf(str, "%d", val2);
  158. else
  159. sprintf(str, "0x%x", val2);
  160. sym->curr.val = strdup(str);
  161. }
  162. static void sym_calc_visibility(struct symbol *sym)
  163. {
  164. struct property *prop;
  165. tristate tri;
  166. /* any prompt visible? */
  167. tri = no;
  168. for_all_prompts(sym, prop) {
  169. prop->visible.tri = expr_calc_value(prop->visible.expr);
  170. tri = EXPR_OR(tri, prop->visible.tri);
  171. }
  172. if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
  173. tri = yes;
  174. if (sym->visible != tri) {
  175. sym->visible = tri;
  176. sym_set_changed(sym);
  177. }
  178. if (sym_is_choice_value(sym))
  179. return;
  180. /* defaulting to "yes" if no explicit "depends on" are given */
  181. tri = yes;
  182. if (sym->dir_dep.expr)
  183. tri = expr_calc_value(sym->dir_dep.expr);
  184. if (tri == mod)
  185. tri = yes;
  186. if (sym->dir_dep.tri != tri) {
  187. sym->dir_dep.tri = tri;
  188. sym_set_changed(sym);
  189. }
  190. tri = no;
  191. if (sym->rev_dep.expr)
  192. tri = expr_calc_value(sym->rev_dep.expr);
  193. if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
  194. tri = yes;
  195. if (sym->rev_dep.tri != tri) {
  196. sym->rev_dep.tri = tri;
  197. sym_set_changed(sym);
  198. }
  199. }
  200. /*
  201. * Find the default symbol for a choice.
  202. * First try the default values for the choice symbol
  203. * Next locate the first visible choice value
  204. * Return NULL if none was found
  205. */
  206. struct symbol *sym_choice_default(struct symbol *sym)
  207. {
  208. struct symbol *def_sym;
  209. struct property *prop;
  210. struct expr *e;
  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. if (def_sym->visible != no)
  218. return def_sym;
  219. }
  220. /* just get the first visible value */
  221. prop = sym_get_choice_prop(sym);
  222. expr_list_for_each_sym(prop->expr, e, def_sym)
  223. if (def_sym->visible != no)
  224. return def_sym;
  225. /* failed to locate any defaults */
  226. return NULL;
  227. }
  228. static struct symbol *sym_calc_choice(struct symbol *sym)
  229. {
  230. struct symbol *def_sym;
  231. struct property *prop;
  232. struct expr *e;
  233. /* first calculate all choice values' visibilities */
  234. prop = sym_get_choice_prop(sym);
  235. expr_list_for_each_sym(prop->expr, e, def_sym)
  236. sym_calc_visibility(def_sym);
  237. /* is the user choice visible? */
  238. def_sym = sym->def[S_DEF_USER].val;
  239. if (def_sym && def_sym->visible != no)
  240. return def_sym;
  241. def_sym = sym_choice_default(sym);
  242. if (def_sym == NULL)
  243. /* no choice? reset tristate value */
  244. sym->curr.tri = no;
  245. return def_sym;
  246. }
  247. void sym_calc_value(struct symbol *sym)
  248. {
  249. struct symbol_value newval, oldval;
  250. struct property *prop;
  251. struct expr *e;
  252. if (!sym)
  253. return;
  254. if (sym->flags & SYMBOL_VALID)
  255. return;
  256. sym->flags |= SYMBOL_VALID;
  257. oldval = sym->curr;
  258. switch (sym->type) {
  259. case S_INT:
  260. case S_HEX:
  261. case S_STRING:
  262. newval = symbol_empty.curr;
  263. break;
  264. case S_BOOLEAN:
  265. case S_TRISTATE:
  266. newval = symbol_no.curr;
  267. break;
  268. default:
  269. sym->curr.val = sym->name;
  270. sym->curr.tri = no;
  271. return;
  272. }
  273. if (!sym_is_choice_value(sym))
  274. sym->flags &= ~SYMBOL_WRITE;
  275. sym_calc_visibility(sym);
  276. /* set default if recursively called */
  277. sym->curr = newval;
  278. switch (sym_get_type(sym)) {
  279. case S_BOOLEAN:
  280. case S_TRISTATE:
  281. if (sym_is_choice_value(sym) && sym->visible == yes) {
  282. prop = sym_get_choice_prop(sym);
  283. newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
  284. } else {
  285. if (sym->visible != no) {
  286. /* if the symbol is visible use the user value
  287. * if available, otherwise try the default value
  288. */
  289. sym->flags |= SYMBOL_WRITE;
  290. if (sym_has_value(sym)) {
  291. newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
  292. sym->visible);
  293. goto calc_newval;
  294. }
  295. }
  296. if (sym->rev_dep.tri != no)
  297. sym->flags |= SYMBOL_WRITE;
  298. if (!sym_is_choice(sym)) {
  299. prop = sym_get_default_prop(sym);
  300. if (prop) {
  301. sym->flags |= SYMBOL_WRITE;
  302. newval.tri = EXPR_AND(expr_calc_value(prop->expr),
  303. prop->visible.tri);
  304. }
  305. }
  306. calc_newval:
  307. if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
  308. struct expr *e;
  309. e = expr_simplify_unmet_dep(sym->rev_dep.expr,
  310. sym->dir_dep.expr);
  311. fprintf(stderr, "warning: (");
  312. expr_fprint(e, stderr);
  313. fprintf(stderr, ") selects %s which has unmet direct dependencies (",
  314. sym->name);
  315. expr_fprint(sym->dir_dep.expr, stderr);
  316. fprintf(stderr, ")\n");
  317. expr_free(e);
  318. }
  319. newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
  320. }
  321. if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
  322. newval.tri = yes;
  323. break;
  324. case S_STRING:
  325. case S_HEX:
  326. case S_INT:
  327. if (sym->visible != no) {
  328. sym->flags |= SYMBOL_WRITE;
  329. if (sym_has_value(sym)) {
  330. newval.val = sym->def[S_DEF_USER].val;
  331. break;
  332. }
  333. }
  334. prop = sym_get_default_prop(sym);
  335. if (prop) {
  336. struct symbol *ds = prop_get_symbol(prop);
  337. if (ds) {
  338. sym->flags |= SYMBOL_WRITE;
  339. sym_calc_value(ds);
  340. newval.val = ds->curr.val;
  341. }
  342. }
  343. break;
  344. default:
  345. ;
  346. }
  347. sym->curr = newval;
  348. if (sym_is_choice(sym) && newval.tri == yes)
  349. sym->curr.val = sym_calc_choice(sym);
  350. sym_validate_range(sym);
  351. if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
  352. sym_set_changed(sym);
  353. if (modules_sym == sym) {
  354. sym_set_all_changed();
  355. modules_val = modules_sym->curr.tri;
  356. }
  357. }
  358. if (sym_is_choice(sym)) {
  359. struct symbol *choice_sym;
  360. prop = sym_get_choice_prop(sym);
  361. expr_list_for_each_sym(prop->expr, e, choice_sym) {
  362. if ((sym->flags & SYMBOL_WRITE) &&
  363. choice_sym->visible != no)
  364. choice_sym->flags |= SYMBOL_WRITE;
  365. if (sym->flags & SYMBOL_CHANGED)
  366. sym_set_changed(choice_sym);
  367. }
  368. }
  369. if (sym->flags & SYMBOL_AUTO)
  370. sym->flags &= ~SYMBOL_WRITE;
  371. }
  372. void sym_clear_all_valid(void)
  373. {
  374. struct symbol *sym;
  375. int i;
  376. for_all_symbols(i, sym)
  377. sym->flags &= ~SYMBOL_VALID;
  378. sym_add_change_count(1);
  379. if (modules_sym)
  380. sym_calc_value(modules_sym);
  381. }
  382. void sym_set_changed(struct symbol *sym)
  383. {
  384. struct property *prop;
  385. sym->flags |= SYMBOL_CHANGED;
  386. for (prop = sym->prop; prop; prop = prop->next) {
  387. if (prop->menu)
  388. prop->menu->flags |= MENU_CHANGED;
  389. }
  390. }
  391. void sym_set_all_changed(void)
  392. {
  393. struct symbol *sym;
  394. int i;
  395. for_all_symbols(i, sym)
  396. sym_set_changed(sym);
  397. }
  398. bool sym_tristate_within_range(struct symbol *sym, tristate val)
  399. {
  400. int type = sym_get_type(sym);
  401. if (sym->visible == no)
  402. return false;
  403. if (type != S_BOOLEAN && type != S_TRISTATE)
  404. return false;
  405. if (type == S_BOOLEAN && val == mod)
  406. return false;
  407. if (sym->visible <= sym->rev_dep.tri)
  408. return false;
  409. if (sym_is_choice_value(sym) && sym->visible == yes)
  410. return val == yes;
  411. return val >= sym->rev_dep.tri && val <= sym->visible;
  412. }
  413. bool sym_set_tristate_value(struct symbol *sym, tristate val)
  414. {
  415. tristate oldval = sym_get_tristate_value(sym);
  416. if (oldval != val && !sym_tristate_within_range(sym, val))
  417. return false;
  418. if (!(sym->flags & SYMBOL_DEF_USER)) {
  419. sym->flags |= SYMBOL_DEF_USER;
  420. sym_set_changed(sym);
  421. }
  422. /*
  423. * setting a choice value also resets the new flag of the choice
  424. * symbol and all other choice values.
  425. */
  426. if (sym_is_choice_value(sym) && val == yes) {
  427. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  428. struct property *prop;
  429. struct expr *e;
  430. cs->def[S_DEF_USER].val = sym;
  431. cs->flags |= SYMBOL_DEF_USER;
  432. prop = sym_get_choice_prop(cs);
  433. for (e = prop->expr; e; e = e->left.expr) {
  434. if (e->right.sym->visible != no)
  435. e->right.sym->flags |= SYMBOL_DEF_USER;
  436. }
  437. }
  438. sym->def[S_DEF_USER].tri = val;
  439. if (oldval != val)
  440. sym_clear_all_valid();
  441. return true;
  442. }
  443. tristate sym_toggle_tristate_value(struct symbol *sym)
  444. {
  445. tristate oldval, newval;
  446. oldval = newval = sym_get_tristate_value(sym);
  447. do {
  448. switch (newval) {
  449. case no:
  450. newval = mod;
  451. break;
  452. case mod:
  453. newval = yes;
  454. break;
  455. case yes:
  456. newval = no;
  457. break;
  458. }
  459. if (sym_set_tristate_value(sym, newval))
  460. break;
  461. } while (oldval != newval);
  462. return newval;
  463. }
  464. bool sym_string_valid(struct symbol *sym, const char *str)
  465. {
  466. signed char ch;
  467. switch (sym->type) {
  468. case S_STRING:
  469. return true;
  470. case S_INT:
  471. ch = *str++;
  472. if (ch == '-')
  473. ch = *str++;
  474. if (!isdigit(ch))
  475. return false;
  476. if (ch == '0' && *str != 0)
  477. return false;
  478. while ((ch = *str++)) {
  479. if (!isdigit(ch))
  480. return false;
  481. }
  482. return true;
  483. case S_HEX:
  484. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
  485. str += 2;
  486. ch = *str++;
  487. do {
  488. if (!isxdigit(ch))
  489. return false;
  490. } while ((ch = *str++));
  491. return true;
  492. case S_BOOLEAN:
  493. case S_TRISTATE:
  494. switch (str[0]) {
  495. case 'y': case 'Y':
  496. case 'm': case 'M':
  497. case 'n': case 'N':
  498. return true;
  499. }
  500. return false;
  501. default:
  502. return false;
  503. }
  504. }
  505. bool sym_string_within_range(struct symbol *sym, const char *str)
  506. {
  507. struct property *prop;
  508. int val;
  509. switch (sym->type) {
  510. case S_STRING:
  511. return sym_string_valid(sym, str);
  512. case S_INT:
  513. if (!sym_string_valid(sym, str))
  514. return false;
  515. prop = sym_get_range_prop(sym);
  516. if (!prop)
  517. return true;
  518. val = strtol(str, NULL, 10);
  519. return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
  520. val <= sym_get_range_val(prop->expr->right.sym, 10);
  521. case S_HEX:
  522. if (!sym_string_valid(sym, str))
  523. return false;
  524. prop = sym_get_range_prop(sym);
  525. if (!prop)
  526. return true;
  527. val = strtol(str, NULL, 16);
  528. return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
  529. val <= sym_get_range_val(prop->expr->right.sym, 16);
  530. case S_BOOLEAN:
  531. case S_TRISTATE:
  532. switch (str[0]) {
  533. case 'y': case 'Y':
  534. return sym_tristate_within_range(sym, yes);
  535. case 'm': case 'M':
  536. return sym_tristate_within_range(sym, mod);
  537. case 'n': case 'N':
  538. return sym_tristate_within_range(sym, no);
  539. }
  540. return false;
  541. default:
  542. return false;
  543. }
  544. }
  545. bool sym_set_string_value(struct symbol *sym, const char *newval)
  546. {
  547. const char *oldval;
  548. char *val;
  549. int size;
  550. switch (sym->type) {
  551. case S_BOOLEAN:
  552. case S_TRISTATE:
  553. switch (newval[0]) {
  554. case 'y': case 'Y':
  555. return sym_set_tristate_value(sym, yes);
  556. case 'm': case 'M':
  557. return sym_set_tristate_value(sym, mod);
  558. case 'n': case 'N':
  559. return sym_set_tristate_value(sym, no);
  560. }
  561. return false;
  562. default:
  563. ;
  564. }
  565. if (!sym_string_within_range(sym, newval))
  566. return false;
  567. if (!(sym->flags & SYMBOL_DEF_USER)) {
  568. sym->flags |= SYMBOL_DEF_USER;
  569. sym_set_changed(sym);
  570. }
  571. oldval = sym->def[S_DEF_USER].val;
  572. size = strlen(newval) + 1;
  573. if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
  574. size += 2;
  575. sym->def[S_DEF_USER].val = val = malloc(size);
  576. *val++ = '0';
  577. *val++ = 'x';
  578. } else if (!oldval || strcmp(oldval, newval))
  579. sym->def[S_DEF_USER].val = val = malloc(size);
  580. else
  581. return true;
  582. strcpy(val, newval);
  583. free((void *)oldval);
  584. sym_clear_all_valid();
  585. return true;
  586. }
  587. /*
  588. * Find the default value associated to a symbol.
  589. * For tristate symbol handle the modules=n case
  590. * in which case "m" becomes "y".
  591. * If the symbol does not have any default then fallback
  592. * to the fixed default values.
  593. */
  594. const char *sym_get_string_default(struct symbol *sym)
  595. {
  596. struct property *prop;
  597. struct symbol *ds;
  598. const char *str;
  599. tristate val;
  600. sym_calc_visibility(sym);
  601. sym_calc_value(modules_sym);
  602. val = symbol_no.curr.tri;
  603. str = symbol_empty.curr.val;
  604. /* If symbol has a default value look it up */
  605. prop = sym_get_default_prop(sym);
  606. if (prop != NULL) {
  607. switch (sym->type) {
  608. case S_BOOLEAN:
  609. case S_TRISTATE:
  610. /* The visibility may limit the value from yes => mod */
  611. val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
  612. break;
  613. default:
  614. /*
  615. * The following fails to handle the situation
  616. * where a default value is further limited by
  617. * the valid range.
  618. */
  619. ds = prop_get_symbol(prop);
  620. if (ds != NULL) {
  621. sym_calc_value(ds);
  622. str = (const char *)ds->curr.val;
  623. }
  624. }
  625. }
  626. /* Handle select statements */
  627. val = EXPR_OR(val, sym->rev_dep.tri);
  628. /* transpose mod to yes if modules are not enabled */
  629. if (val == mod)
  630. if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
  631. val = yes;
  632. /* transpose mod to yes if type is bool */
  633. if (sym->type == S_BOOLEAN && val == mod)
  634. val = yes;
  635. switch (sym->type) {
  636. case S_BOOLEAN:
  637. case S_TRISTATE:
  638. switch (val) {
  639. case no: return "n";
  640. case mod: return "m";
  641. case yes: return "y";
  642. }
  643. case S_INT:
  644. case S_HEX:
  645. return str;
  646. case S_STRING:
  647. return str;
  648. case S_OTHER:
  649. case S_UNKNOWN:
  650. break;
  651. }
  652. return "";
  653. }
  654. const char *sym_get_string_value(struct symbol *sym)
  655. {
  656. tristate val;
  657. switch (sym->type) {
  658. case S_BOOLEAN:
  659. case S_TRISTATE:
  660. val = sym_get_tristate_value(sym);
  661. switch (val) {
  662. case no:
  663. return "n";
  664. case mod:
  665. sym_calc_value(modules_sym);
  666. return (modules_sym->curr.tri == no) ? "n" : "m";
  667. case yes:
  668. return "y";
  669. }
  670. break;
  671. default:
  672. ;
  673. }
  674. return (const char *)sym->curr.val;
  675. }
  676. bool sym_is_changable(struct symbol *sym)
  677. {
  678. return sym->visible > sym->rev_dep.tri;
  679. }
  680. static unsigned strhash(const char *s)
  681. {
  682. /* fnv32 hash */
  683. unsigned hash = 2166136261U;
  684. for (; *s; s++)
  685. hash = (hash ^ *s) * 0x01000193;
  686. return hash;
  687. }
  688. struct symbol *sym_lookup(const char *name, int flags)
  689. {
  690. struct symbol *symbol;
  691. char *new_name;
  692. int hash;
  693. if (name) {
  694. if (name[0] && !name[1]) {
  695. switch (name[0]) {
  696. case 'y': return &symbol_yes;
  697. case 'm': return &symbol_mod;
  698. case 'n': return &symbol_no;
  699. }
  700. }
  701. hash = strhash(name) % SYMBOL_HASHSIZE;
  702. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  703. if (symbol->name &&
  704. !strcmp(symbol->name, name) &&
  705. (flags ? symbol->flags & flags
  706. : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
  707. return symbol;
  708. }
  709. new_name = strdup(name);
  710. } else {
  711. new_name = NULL;
  712. hash = 0;
  713. }
  714. symbol = malloc(sizeof(*symbol));
  715. memset(symbol, 0, sizeof(*symbol));
  716. symbol->name = new_name;
  717. symbol->type = S_UNKNOWN;
  718. symbol->flags |= flags;
  719. symbol->next = symbol_hash[hash];
  720. symbol_hash[hash] = symbol;
  721. return symbol;
  722. }
  723. struct symbol *sym_find(const char *name)
  724. {
  725. struct symbol *symbol = NULL;
  726. int hash = 0;
  727. if (!name)
  728. return NULL;
  729. if (name[0] && !name[1]) {
  730. switch (name[0]) {
  731. case 'y': return &symbol_yes;
  732. case 'm': return &symbol_mod;
  733. case 'n': return &symbol_no;
  734. }
  735. }
  736. hash = strhash(name) % SYMBOL_HASHSIZE;
  737. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  738. if (symbol->name &&
  739. !strcmp(symbol->name, name) &&
  740. !(symbol->flags & SYMBOL_CONST))
  741. break;
  742. }
  743. return symbol;
  744. }
  745. /*
  746. * Expand symbol's names embedded in the string given in argument. Symbols'
  747. * name to be expanded shall be prefixed by a '$'. Unknown symbol expands to
  748. * the empty string.
  749. */
  750. const char *sym_expand_string_value(const char *in)
  751. {
  752. const char *src;
  753. char *res;
  754. size_t reslen;
  755. reslen = strlen(in) + 1;
  756. res = malloc(reslen);
  757. res[0] = '\0';
  758. while ((src = strchr(in, '$'))) {
  759. char *p, name[SYMBOL_MAXLENGTH];
  760. const char *symval = "";
  761. struct symbol *sym;
  762. size_t newlen;
  763. strncat(res, in, src - in);
  764. src++;
  765. p = name;
  766. while (isalnum(*src) || *src == '_')
  767. *p++ = *src++;
  768. *p = '\0';
  769. sym = sym_find(name);
  770. if (sym != NULL) {
  771. sym_calc_value(sym);
  772. symval = sym_get_string_value(sym);
  773. }
  774. newlen = strlen(res) + strlen(symval) + strlen(src) + 1;
  775. if (newlen > reslen) {
  776. reslen = newlen;
  777. res = realloc(res, reslen);
  778. }
  779. strcat(res, symval);
  780. in = src;
  781. }
  782. strcat(res, in);
  783. return res;
  784. }
  785. const char *sym_escape_string_value(const char *in)
  786. {
  787. const char *p;
  788. size_t reslen;
  789. char *res;
  790. size_t l;
  791. reslen = strlen(in) + strlen("\"\"") + 1;
  792. p = in;
  793. for (;;) {
  794. l = strcspn(p, "\"\\");
  795. p += l;
  796. if (p[0] == '\0')
  797. break;
  798. reslen++;
  799. p++;
  800. }
  801. res = malloc(reslen);
  802. res[0] = '\0';
  803. strcat(res, "\"");
  804. p = in;
  805. for (;;) {
  806. l = strcspn(p, "\"\\");
  807. strncat(res, p, l);
  808. p += l;
  809. if (p[0] == '\0')
  810. break;
  811. strcat(res, "\\");
  812. strncat(res, p++, 1);
  813. }
  814. strcat(res, "\"");
  815. return res;
  816. }
  817. struct symbol **sym_re_search(const char *pattern)
  818. {
  819. struct symbol *sym, **sym_arr = NULL;
  820. int i, cnt, size;
  821. regex_t re;
  822. cnt = size = 0;
  823. /* Skip if empty */
  824. if (strlen(pattern) == 0)
  825. return NULL;
  826. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
  827. return NULL;
  828. for_all_symbols(i, sym) {
  829. if (sym->flags & SYMBOL_CONST || !sym->name)
  830. continue;
  831. if (regexec(&re, sym->name, 0, NULL, 0))
  832. continue;
  833. if (cnt + 1 >= size) {
  834. void *tmp = sym_arr;
  835. size += 16;
  836. sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
  837. if (!sym_arr) {
  838. free(tmp);
  839. return NULL;
  840. }
  841. }
  842. sym_calc_value(sym);
  843. sym_arr[cnt++] = sym;
  844. }
  845. if (sym_arr)
  846. sym_arr[cnt] = NULL;
  847. regfree(&re);
  848. return sym_arr;
  849. }
  850. /*
  851. * When we check for recursive dependencies we use a stack to save
  852. * current state so we can print out relevant info to user.
  853. * The entries are located on the call stack so no need to free memory.
  854. * Note inser() remove() must always match to properly clear the stack.
  855. */
  856. static struct dep_stack {
  857. struct dep_stack *prev, *next;
  858. struct symbol *sym;
  859. struct property *prop;
  860. struct expr *expr;
  861. } *check_top;
  862. static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
  863. {
  864. memset(stack, 0, sizeof(*stack));
  865. if (check_top)
  866. check_top->next = stack;
  867. stack->prev = check_top;
  868. stack->sym = sym;
  869. check_top = stack;
  870. }
  871. static void dep_stack_remove(void)
  872. {
  873. check_top = check_top->prev;
  874. if (check_top)
  875. check_top->next = NULL;
  876. }
  877. /*
  878. * Called when we have detected a recursive dependency.
  879. * check_top point to the top of the stact so we use
  880. * the ->prev pointer to locate the bottom of the stack.
  881. */
  882. static void sym_check_print_recursive(struct symbol *last_sym)
  883. {
  884. struct dep_stack *stack;
  885. struct symbol *sym, *next_sym;
  886. struct menu *menu = NULL;
  887. struct property *prop;
  888. struct dep_stack cv_stack;
  889. if (sym_is_choice_value(last_sym)) {
  890. dep_stack_insert(&cv_stack, last_sym);
  891. last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
  892. }
  893. for (stack = check_top; stack != NULL; stack = stack->prev)
  894. if (stack->sym == last_sym)
  895. break;
  896. if (!stack) {
  897. fprintf(stderr, "unexpected recursive dependency error\n");
  898. return;
  899. }
  900. for (; stack; stack = stack->next) {
  901. sym = stack->sym;
  902. next_sym = stack->next ? stack->next->sym : last_sym;
  903. prop = stack->prop;
  904. if (prop == NULL)
  905. prop = stack->sym->prop;
  906. /* for choice values find the menu entry (used below) */
  907. if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
  908. for (prop = sym->prop; prop; prop = prop->next) {
  909. menu = prop->menu;
  910. if (prop->menu)
  911. break;
  912. }
  913. }
  914. if (stack->sym == last_sym)
  915. fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
  916. prop->file->name, prop->lineno);
  917. if (stack->expr) {
  918. fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
  919. prop->file->name, prop->lineno,
  920. sym->name ? sym->name : "<choice>",
  921. prop_get_type_name(prop->type),
  922. next_sym->name ? next_sym->name : "<choice>");
  923. } else if (stack->prop) {
  924. fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
  925. prop->file->name, prop->lineno,
  926. sym->name ? sym->name : "<choice>",
  927. next_sym->name ? next_sym->name : "<choice>");
  928. } else if (sym_is_choice(sym)) {
  929. fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
  930. menu->file->name, menu->lineno,
  931. sym->name ? sym->name : "<choice>",
  932. next_sym->name ? next_sym->name : "<choice>");
  933. } else if (sym_is_choice_value(sym)) {
  934. fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
  935. menu->file->name, menu->lineno,
  936. sym->name ? sym->name : "<choice>",
  937. next_sym->name ? next_sym->name : "<choice>");
  938. } else {
  939. fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
  940. prop->file->name, prop->lineno,
  941. sym->name ? sym->name : "<choice>",
  942. next_sym->name ? next_sym->name : "<choice>");
  943. }
  944. }
  945. if (check_top == &cv_stack)
  946. dep_stack_remove();
  947. }
  948. static struct symbol *sym_check_expr_deps(struct expr *e)
  949. {
  950. struct symbol *sym;
  951. if (!e)
  952. return NULL;
  953. switch (e->type) {
  954. case E_OR:
  955. case E_AND:
  956. sym = sym_check_expr_deps(e->left.expr);
  957. if (sym)
  958. return sym;
  959. return sym_check_expr_deps(e->right.expr);
  960. case E_NOT:
  961. return sym_check_expr_deps(e->left.expr);
  962. case E_EQUAL:
  963. case E_UNEQUAL:
  964. sym = sym_check_deps(e->left.sym);
  965. if (sym)
  966. return sym;
  967. return sym_check_deps(e->right.sym);
  968. case E_SYMBOL:
  969. return sym_check_deps(e->left.sym);
  970. default:
  971. break;
  972. }
  973. printf("Oops! How to check %d?\n", e->type);
  974. return NULL;
  975. }
  976. /* return NULL when dependencies are OK */
  977. static struct symbol *sym_check_sym_deps(struct symbol *sym)
  978. {
  979. struct symbol *sym2;
  980. struct property *prop;
  981. struct dep_stack stack;
  982. dep_stack_insert(&stack, sym);
  983. sym2 = sym_check_expr_deps(sym->rev_dep.expr);
  984. if (sym2)
  985. goto out;
  986. for (prop = sym->prop; prop; prop = prop->next) {
  987. if (prop->type == P_CHOICE || prop->type == P_SELECT)
  988. continue;
  989. stack.prop = prop;
  990. sym2 = sym_check_expr_deps(prop->visible.expr);
  991. if (sym2)
  992. break;
  993. if (prop->type != P_DEFAULT || sym_is_choice(sym))
  994. continue;
  995. stack.expr = prop->expr;
  996. sym2 = sym_check_expr_deps(prop->expr);
  997. if (sym2)
  998. break;
  999. stack.expr = NULL;
  1000. }
  1001. out:
  1002. dep_stack_remove();
  1003. return sym2;
  1004. }
  1005. static struct symbol *sym_check_choice_deps(struct symbol *choice)
  1006. {
  1007. struct symbol *sym, *sym2;
  1008. struct property *prop;
  1009. struct expr *e;
  1010. struct dep_stack stack;
  1011. dep_stack_insert(&stack, choice);
  1012. prop = sym_get_choice_prop(choice);
  1013. expr_list_for_each_sym(prop->expr, e, sym)
  1014. sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  1015. choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  1016. sym2 = sym_check_sym_deps(choice);
  1017. choice->flags &= ~SYMBOL_CHECK;
  1018. if (sym2)
  1019. goto out;
  1020. expr_list_for_each_sym(prop->expr, e, sym) {
  1021. sym2 = sym_check_sym_deps(sym);
  1022. if (sym2)
  1023. break;
  1024. }
  1025. out:
  1026. expr_list_for_each_sym(prop->expr, e, sym)
  1027. sym->flags &= ~SYMBOL_CHECK;
  1028. if (sym2 && sym_is_choice_value(sym2) &&
  1029. prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
  1030. sym2 = choice;
  1031. dep_stack_remove();
  1032. return sym2;
  1033. }
  1034. struct symbol *sym_check_deps(struct symbol *sym)
  1035. {
  1036. struct symbol *sym2;
  1037. struct property *prop;
  1038. if (sym->flags & SYMBOL_CHECK) {
  1039. sym_check_print_recursive(sym);
  1040. return sym;
  1041. }
  1042. if (sym->flags & SYMBOL_CHECKED)
  1043. return NULL;
  1044. if (sym_is_choice_value(sym)) {
  1045. struct dep_stack stack;
  1046. /* for choice groups start the check with main choice symbol */
  1047. dep_stack_insert(&stack, sym);
  1048. prop = sym_get_choice_prop(sym);
  1049. sym2 = sym_check_deps(prop_get_symbol(prop));
  1050. dep_stack_remove();
  1051. } else if (sym_is_choice(sym)) {
  1052. sym2 = sym_check_choice_deps(sym);
  1053. } else {
  1054. sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  1055. sym2 = sym_check_sym_deps(sym);
  1056. sym->flags &= ~SYMBOL_CHECK;
  1057. }
  1058. if (sym2 && sym2 == sym)
  1059. sym2 = NULL;
  1060. return sym2;
  1061. }
  1062. struct property *prop_alloc(enum prop_type type, struct symbol *sym)
  1063. {
  1064. struct property *prop;
  1065. struct property **propp;
  1066. prop = malloc(sizeof(*prop));
  1067. memset(prop, 0, sizeof(*prop));
  1068. prop->type = type;
  1069. prop->sym = sym;
  1070. prop->file = current_file;
  1071. prop->lineno = zconf_lineno();
  1072. /* append property to the prop list of symbol */
  1073. if (sym) {
  1074. for (propp = &sym->prop; *propp; propp = &(*propp)->next)
  1075. ;
  1076. *propp = prop;
  1077. }
  1078. return prop;
  1079. }
  1080. struct symbol *prop_get_symbol(struct property *prop)
  1081. {
  1082. if (prop->expr && (prop->expr->type == E_SYMBOL ||
  1083. prop->expr->type == E_LIST))
  1084. return prop->expr->left.sym;
  1085. return NULL;
  1086. }
  1087. const char *prop_get_type_name(enum prop_type type)
  1088. {
  1089. switch (type) {
  1090. case P_PROMPT:
  1091. return "prompt";
  1092. case P_ENV:
  1093. return "env";
  1094. case P_COMMENT:
  1095. return "comment";
  1096. case P_MENU:
  1097. return "menu";
  1098. case P_DEFAULT:
  1099. return "default";
  1100. case P_CHOICE:
  1101. return "choice";
  1102. case P_SELECT:
  1103. return "select";
  1104. case P_RANGE:
  1105. return "range";
  1106. case P_SYMBOL:
  1107. return "symbol";
  1108. case P_UNKNOWN:
  1109. break;
  1110. }
  1111. return "unknown";
  1112. }
  1113. static void prop_add_env(const char *env)
  1114. {
  1115. struct symbol *sym, *sym2;
  1116. struct property *prop;
  1117. char *p;
  1118. sym = current_entry->sym;
  1119. sym->flags |= SYMBOL_AUTO;
  1120. for_all_properties(sym, prop, P_ENV) {
  1121. sym2 = prop_get_symbol(prop);
  1122. if (strcmp(sym2->name, env))
  1123. menu_warn(current_entry, "redefining environment symbol from %s",
  1124. sym2->name);
  1125. return;
  1126. }
  1127. prop = prop_alloc(P_ENV, sym);
  1128. prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
  1129. sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
  1130. sym_env_list->right.sym = sym;
  1131. p = getenv(env);
  1132. if (p)
  1133. sym_add_default(sym, p);
  1134. else
  1135. menu_warn(current_entry, "environment variable %s undefined", env);
  1136. }