symbol.c 29 KB

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