symbol.c 28 KB

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