symbol.c 30 KB

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