symbol.c 31 KB

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