symbol.c 28 KB

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