symbol.c 28 KB

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