symbol.c 27 KB

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