symbol.c 29 KB

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