symbol.c 28 KB

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