symbol.c 29 KB

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