symbol.c 30 KB

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