symbol.c 29 KB

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