symbol.c 29 KB

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