symbol.c 29 KB

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