symbol.c 29 KB

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