symbol.c 30 KB

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