symbol.c 29 KB

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