symbol.c 27 KB

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