symbol.c 26 KB

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