symbol.c 28 KB

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