symbol.c 26 KB

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