PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/scripts/kconfig/symbol.c

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