symbol.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /*
  2. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  3. * Released under the terms of the GNU GPL v2.0.
  4. */
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <regex.h>
  9. #include <sys/utsname.h>
  10. #include "lkc.h"
  11. struct symbol symbol_yes = {
  12. .name = "y",
  13. .curr = { "y", yes },
  14. .flags = SYMBOL_CONST|SYMBOL_VALID,
  15. }, symbol_mod = {
  16. .name = "m",
  17. .curr = { "m", mod },
  18. .flags = SYMBOL_CONST|SYMBOL_VALID,
  19. }, symbol_no = {
  20. .name = "n",
  21. .curr = { "n", no },
  22. .flags = SYMBOL_CONST|SYMBOL_VALID,
  23. }, symbol_empty = {
  24. .name = "",
  25. .curr = { "", no },
  26. .flags = SYMBOL_VALID,
  27. };
  28. struct symbol *sym_defconfig_list;
  29. struct symbol *modules_sym;
  30. tristate modules_val;
  31. struct expr *sym_env_list;
  32. static void sym_add_default(struct symbol *sym, const char *def)
  33. {
  34. struct property *prop = prop_alloc(P_DEFAULT, sym);
  35. prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
  36. }
  37. void sym_init(void)
  38. {
  39. struct symbol *sym;
  40. struct utsname uts;
  41. static bool inited = false;
  42. if (inited)
  43. return;
  44. inited = true;
  45. uname(&uts);
  46. sym = sym_lookup("UNAME_RELEASE", 0);
  47. sym->type = S_STRING;
  48. sym->flags |= SYMBOL_AUTO;
  49. sym_add_default(sym, uts.release);
  50. }
  51. enum symbol_type sym_get_type(struct symbol *sym)
  52. {
  53. enum symbol_type type = sym->type;
  54. if (type == S_TRISTATE) {
  55. if (sym_is_choice_value(sym) && sym->visible == yes)
  56. type = S_BOOLEAN;
  57. else if (modules_val == no)
  58. type = S_BOOLEAN;
  59. }
  60. return type;
  61. }
  62. const char *sym_type_name(enum symbol_type type)
  63. {
  64. switch (type) {
  65. case S_BOOLEAN:
  66. return "bool";
  67. case S_TRISTATE:
  68. return "tristate";
  69. case S_INT:
  70. return "integer";
  71. case S_HEX:
  72. return "hex";
  73. case S_STRING:
  74. return "string";
  75. case S_UNKNOWN:
  76. return "unknown";
  77. case S_OTHER:
  78. break;
  79. }
  80. return "???";
  81. }
  82. struct property *sym_get_choice_prop(struct symbol *sym)
  83. {
  84. struct property *prop;
  85. for_all_choices(sym, prop)
  86. return prop;
  87. return NULL;
  88. }
  89. struct property *sym_get_env_prop(struct symbol *sym)
  90. {
  91. struct property *prop;
  92. for_all_properties(sym, prop, P_ENV)
  93. return prop;
  94. return NULL;
  95. }
  96. static struct property *sym_get_default_prop(struct symbol *sym)
  97. {
  98. struct property *prop;
  99. for_all_defaults(sym, prop) {
  100. prop->visible.tri = expr_calc_value(prop->visible.expr);
  101. if (prop->visible.tri != no)
  102. return prop;
  103. }
  104. return NULL;
  105. }
  106. static struct property *sym_get_range_prop(struct symbol *sym)
  107. {
  108. struct property *prop;
  109. for_all_properties(sym, prop, P_RANGE) {
  110. prop->visible.tri = expr_calc_value(prop->visible.expr);
  111. if (prop->visible.tri != no)
  112. return prop;
  113. }
  114. return NULL;
  115. }
  116. static long long sym_get_range_val(struct symbol *sym, int base)
  117. {
  118. sym_calc_value(sym);
  119. switch (sym->type) {
  120. case S_INT:
  121. base = 10;
  122. break;
  123. case S_HEX:
  124. base = 16;
  125. break;
  126. default:
  127. break;
  128. }
  129. return strtoll(sym->curr.val, NULL, base);
  130. }
  131. static void sym_validate_range(struct symbol *sym)
  132. {
  133. struct property *prop;
  134. int base;
  135. long long val, val2;
  136. char str[64];
  137. switch (sym->type) {
  138. case S_INT:
  139. base = 10;
  140. break;
  141. case S_HEX:
  142. base = 16;
  143. break;
  144. default:
  145. return;
  146. }
  147. prop = sym_get_range_prop(sym);
  148. if (!prop)
  149. return;
  150. val = strtoll(sym->curr.val, NULL, base);
  151. val2 = sym_get_range_val(prop->expr->left.sym, base);
  152. if (val >= val2) {
  153. val2 = sym_get_range_val(prop->expr->right.sym, base);
  154. if (val <= val2)
  155. return;
  156. }
  157. if (sym->type == S_INT)
  158. sprintf(str, "%lld", val2);
  159. else
  160. sprintf(str, "0x%llx", val2);
  161. sym->curr.val = 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. /*
  800. * Note: 'in' might come from a token that's about to be
  801. * freed, so make sure to always allocate a new string
  802. */
  803. reslen = strlen(in) + 1;
  804. res = xmalloc(reslen);
  805. res[0] = '\0';
  806. while ((src = strchr(in, '$'))) {
  807. char *p, name[SYMBOL_MAXLENGTH];
  808. const char *symval = "";
  809. struct symbol *sym;
  810. size_t newlen;
  811. strncat(res, in, src - in);
  812. src++;
  813. p = name;
  814. while (isalnum(*src) || *src == '_')
  815. *p++ = *src++;
  816. *p = '\0';
  817. sym = sym_find(name);
  818. if (sym != NULL) {
  819. sym_calc_value(sym);
  820. symval = sym_get_string_value(sym);
  821. }
  822. newlen = strlen(res) + strlen(symval) + strlen(src) + 1;
  823. if (newlen > reslen) {
  824. reslen = newlen;
  825. res = realloc(res, reslen);
  826. }
  827. strcat(res, symval);
  828. in = src;
  829. }
  830. strcat(res, in);
  831. return res;
  832. }
  833. const char *sym_escape_string_value(const char *in)
  834. {
  835. const char *p;
  836. size_t reslen;
  837. char *res;
  838. size_t l;
  839. reslen = strlen(in) + strlen("\"\"") + 1;
  840. p = in;
  841. for (;;) {
  842. l = strcspn(p, "\"\\");
  843. p += l;
  844. if (p[0] == '\0')
  845. break;
  846. reslen++;
  847. p++;
  848. }
  849. res = xmalloc(reslen);
  850. res[0] = '\0';
  851. strcat(res, "\"");
  852. p = in;
  853. for (;;) {
  854. l = strcspn(p, "\"\\");
  855. strncat(res, p, l);
  856. p += l;
  857. if (p[0] == '\0')
  858. break;
  859. strcat(res, "\\");
  860. strncat(res, p++, 1);
  861. }
  862. strcat(res, "\"");
  863. return res;
  864. }
  865. struct sym_match {
  866. struct symbol *sym;
  867. off_t so, eo;
  868. };
  869. /* Compare matched symbols as thus:
  870. * - first, symbols that match exactly
  871. * - then, alphabetical sort
  872. */
  873. static int sym_rel_comp(const void *sym1, const void *sym2)
  874. {
  875. const struct sym_match *s1 = sym1;
  876. const struct sym_match *s2 = sym2;
  877. int exact1, exact2;
  878. /* Exact match:
  879. * - if matched length on symbol s1 is the length of that symbol,
  880. * then this symbol should come first;
  881. * - if matched length on symbol s2 is the length of that symbol,
  882. * then this symbol should come first.
  883. * Note: since the search can be a regexp, both symbols may match
  884. * exactly; if this is the case, we can't decide which comes first,
  885. * and we fallback to sorting alphabetically.
  886. */
  887. exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
  888. exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
  889. if (exact1 && !exact2)
  890. return -1;
  891. if (!exact1 && exact2)
  892. return 1;
  893. /* As a fallback, sort symbols alphabetically */
  894. return strcmp(s1->sym->name, s2->sym->name);
  895. }
  896. struct symbol **sym_re_search(const char *pattern)
  897. {
  898. struct symbol *sym, **sym_arr = NULL;
  899. struct sym_match *sym_match_arr = NULL;
  900. int i, cnt, size;
  901. regex_t re;
  902. regmatch_t match[1];
  903. cnt = size = 0;
  904. /* Skip if empty */
  905. if (strlen(pattern) == 0)
  906. return NULL;
  907. if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
  908. return NULL;
  909. for_all_symbols(i, sym) {
  910. if (sym->flags & SYMBOL_CONST || !sym->name)
  911. continue;
  912. if (regexec(&re, sym->name, 1, match, 0))
  913. continue;
  914. if (cnt >= size) {
  915. void *tmp;
  916. size += 16;
  917. tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
  918. if (!tmp)
  919. goto sym_re_search_free;
  920. sym_match_arr = tmp;
  921. }
  922. sym_calc_value(sym);
  923. /* As regexec returned 0, we know we have a match, so
  924. * we can use match[0].rm_[se]o without further checks
  925. */
  926. sym_match_arr[cnt].so = match[0].rm_so;
  927. sym_match_arr[cnt].eo = match[0].rm_eo;
  928. sym_match_arr[cnt++].sym = sym;
  929. }
  930. if (sym_match_arr) {
  931. qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
  932. sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
  933. if (!sym_arr)
  934. goto sym_re_search_free;
  935. for (i = 0; i < cnt; i++)
  936. sym_arr[i] = sym_match_arr[i].sym;
  937. sym_arr[cnt] = NULL;
  938. }
  939. sym_re_search_free:
  940. /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
  941. free(sym_match_arr);
  942. regfree(&re);
  943. return sym_arr;
  944. }
  945. /*
  946. * When we check for recursive dependencies we use a stack to save
  947. * current state so we can print out relevant info to user.
  948. * The entries are located on the call stack so no need to free memory.
  949. * Note insert() remove() must always match to properly clear the stack.
  950. */
  951. static struct dep_stack {
  952. struct dep_stack *prev, *next;
  953. struct symbol *sym;
  954. struct property *prop;
  955. struct expr *expr;
  956. } *check_top;
  957. static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
  958. {
  959. memset(stack, 0, sizeof(*stack));
  960. if (check_top)
  961. check_top->next = stack;
  962. stack->prev = check_top;
  963. stack->sym = sym;
  964. check_top = stack;
  965. }
  966. static void dep_stack_remove(void)
  967. {
  968. check_top = check_top->prev;
  969. if (check_top)
  970. check_top->next = NULL;
  971. }
  972. /*
  973. * Called when we have detected a recursive dependency.
  974. * check_top point to the top of the stact so we use
  975. * the ->prev pointer to locate the bottom of the stack.
  976. */
  977. static void sym_check_print_recursive(struct symbol *last_sym)
  978. {
  979. struct dep_stack *stack;
  980. struct symbol *sym, *next_sym;
  981. struct menu *menu = NULL;
  982. struct property *prop;
  983. struct dep_stack cv_stack;
  984. if (sym_is_choice_value(last_sym)) {
  985. dep_stack_insert(&cv_stack, last_sym);
  986. last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
  987. }
  988. for (stack = check_top; stack != NULL; stack = stack->prev)
  989. if (stack->sym == last_sym)
  990. break;
  991. if (!stack) {
  992. fprintf(stderr, "unexpected recursive dependency error\n");
  993. return;
  994. }
  995. for (; stack; stack = stack->next) {
  996. sym = stack->sym;
  997. next_sym = stack->next ? stack->next->sym : last_sym;
  998. prop = stack->prop;
  999. if (prop == NULL)
  1000. prop = stack->sym->prop;
  1001. /* for choice values find the menu entry (used below) */
  1002. if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
  1003. for (prop = sym->prop; prop; prop = prop->next) {
  1004. menu = prop->menu;
  1005. if (prop->menu)
  1006. break;
  1007. }
  1008. }
  1009. if (stack->sym == last_sym)
  1010. fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
  1011. prop->file->name, prop->lineno);
  1012. if (stack->expr) {
  1013. fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
  1014. prop->file->name, prop->lineno,
  1015. sym->name ? sym->name : "<choice>",
  1016. prop_get_type_name(prop->type),
  1017. next_sym->name ? next_sym->name : "<choice>");
  1018. } else if (stack->prop) {
  1019. fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
  1020. prop->file->name, prop->lineno,
  1021. sym->name ? sym->name : "<choice>",
  1022. next_sym->name ? next_sym->name : "<choice>");
  1023. } else if (sym_is_choice(sym)) {
  1024. fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
  1025. menu->file->name, menu->lineno,
  1026. sym->name ? sym->name : "<choice>",
  1027. next_sym->name ? next_sym->name : "<choice>");
  1028. } else if (sym_is_choice_value(sym)) {
  1029. fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
  1030. menu->file->name, menu->lineno,
  1031. sym->name ? sym->name : "<choice>",
  1032. next_sym->name ? next_sym->name : "<choice>");
  1033. } else {
  1034. fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
  1035. prop->file->name, prop->lineno,
  1036. sym->name ? sym->name : "<choice>",
  1037. next_sym->name ? next_sym->name : "<choice>");
  1038. }
  1039. }
  1040. fprintf(stderr,
  1041. "For a resolution refer to Documentation/kbuild/kconfig-language.txt\n"
  1042. "subsection \"Kconfig recursive dependency limitations\"\n"
  1043. "\n");
  1044. if (check_top == &cv_stack)
  1045. dep_stack_remove();
  1046. }
  1047. static struct symbol *sym_check_expr_deps(struct expr *e)
  1048. {
  1049. struct symbol *sym;
  1050. if (!e)
  1051. return NULL;
  1052. switch (e->type) {
  1053. case E_OR:
  1054. case E_AND:
  1055. sym = sym_check_expr_deps(e->left.expr);
  1056. if (sym)
  1057. return sym;
  1058. return sym_check_expr_deps(e->right.expr);
  1059. case E_NOT:
  1060. return sym_check_expr_deps(e->left.expr);
  1061. case E_EQUAL:
  1062. case E_GEQ:
  1063. case E_GTH:
  1064. case E_LEQ:
  1065. case E_LTH:
  1066. case E_UNEQUAL:
  1067. sym = sym_check_deps(e->left.sym);
  1068. if (sym)
  1069. return sym;
  1070. return sym_check_deps(e->right.sym);
  1071. case E_SYMBOL:
  1072. return sym_check_deps(e->left.sym);
  1073. default:
  1074. break;
  1075. }
  1076. printf("Oops! How to check %d?\n", e->type);
  1077. return NULL;
  1078. }
  1079. /* return NULL when dependencies are OK */
  1080. static struct symbol *sym_check_sym_deps(struct symbol *sym)
  1081. {
  1082. struct symbol *sym2;
  1083. struct property *prop;
  1084. struct dep_stack stack;
  1085. dep_stack_insert(&stack, sym);
  1086. sym2 = sym_check_expr_deps(sym->rev_dep.expr);
  1087. if (sym2)
  1088. goto out;
  1089. for (prop = sym->prop; prop; prop = prop->next) {
  1090. if (prop->type == P_CHOICE || prop->type == P_SELECT)
  1091. continue;
  1092. stack.prop = prop;
  1093. sym2 = sym_check_expr_deps(prop->visible.expr);
  1094. if (sym2)
  1095. break;
  1096. if (prop->type != P_DEFAULT || sym_is_choice(sym))
  1097. continue;
  1098. stack.expr = prop->expr;
  1099. sym2 = sym_check_expr_deps(prop->expr);
  1100. if (sym2)
  1101. break;
  1102. stack.expr = NULL;
  1103. }
  1104. out:
  1105. dep_stack_remove();
  1106. return sym2;
  1107. }
  1108. static struct symbol *sym_check_choice_deps(struct symbol *choice)
  1109. {
  1110. struct symbol *sym, *sym2;
  1111. struct property *prop;
  1112. struct expr *e;
  1113. struct dep_stack stack;
  1114. dep_stack_insert(&stack, choice);
  1115. prop = sym_get_choice_prop(choice);
  1116. expr_list_for_each_sym(prop->expr, e, sym)
  1117. sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  1118. choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  1119. sym2 = sym_check_sym_deps(choice);
  1120. choice->flags &= ~SYMBOL_CHECK;
  1121. if (sym2)
  1122. goto out;
  1123. expr_list_for_each_sym(prop->expr, e, sym) {
  1124. sym2 = sym_check_sym_deps(sym);
  1125. if (sym2)
  1126. break;
  1127. }
  1128. out:
  1129. expr_list_for_each_sym(prop->expr, e, sym)
  1130. sym->flags &= ~SYMBOL_CHECK;
  1131. if (sym2 && sym_is_choice_value(sym2) &&
  1132. prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
  1133. sym2 = choice;
  1134. dep_stack_remove();
  1135. return sym2;
  1136. }
  1137. struct symbol *sym_check_deps(struct symbol *sym)
  1138. {
  1139. struct symbol *sym2;
  1140. struct property *prop;
  1141. if (sym->flags & SYMBOL_CHECK) {
  1142. sym_check_print_recursive(sym);
  1143. return sym;
  1144. }
  1145. if (sym->flags & SYMBOL_CHECKED)
  1146. return NULL;
  1147. if (sym_is_choice_value(sym)) {
  1148. struct dep_stack stack;
  1149. /* for choice groups start the check with main choice symbol */
  1150. dep_stack_insert(&stack, sym);
  1151. prop = sym_get_choice_prop(sym);
  1152. sym2 = sym_check_deps(prop_get_symbol(prop));
  1153. dep_stack_remove();
  1154. } else if (sym_is_choice(sym)) {
  1155. sym2 = sym_check_choice_deps(sym);
  1156. } else {
  1157. sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  1158. sym2 = sym_check_sym_deps(sym);
  1159. sym->flags &= ~SYMBOL_CHECK;
  1160. }
  1161. if (sym2 && sym2 == sym)
  1162. sym2 = NULL;
  1163. return sym2;
  1164. }
  1165. struct property *prop_alloc(enum prop_type type, struct symbol *sym)
  1166. {
  1167. struct property *prop;
  1168. struct property **propp;
  1169. prop = xmalloc(sizeof(*prop));
  1170. memset(prop, 0, sizeof(*prop));
  1171. prop->type = type;
  1172. prop->sym = sym;
  1173. prop->file = current_file;
  1174. prop->lineno = zconf_lineno();
  1175. /* append property to the prop list of symbol */
  1176. if (sym) {
  1177. for (propp = &sym->prop; *propp; propp = &(*propp)->next)
  1178. ;
  1179. *propp = prop;
  1180. }
  1181. return prop;
  1182. }
  1183. struct symbol *prop_get_symbol(struct property *prop)
  1184. {
  1185. if (prop->expr && (prop->expr->type == E_SYMBOL ||
  1186. prop->expr->type == E_LIST))
  1187. return prop->expr->left.sym;
  1188. return NULL;
  1189. }
  1190. const char *prop_get_type_name(enum prop_type type)
  1191. {
  1192. switch (type) {
  1193. case P_PROMPT:
  1194. return "prompt";
  1195. case P_ENV:
  1196. return "env";
  1197. case P_COMMENT:
  1198. return "comment";
  1199. case P_MENU:
  1200. return "menu";
  1201. case P_DEFAULT:
  1202. return "default";
  1203. case P_CHOICE:
  1204. return "choice";
  1205. case P_SELECT:
  1206. return "select";
  1207. case P_IMPLY:
  1208. return "imply";
  1209. case P_RANGE:
  1210. return "range";
  1211. case P_SYMBOL:
  1212. return "symbol";
  1213. case P_UNKNOWN:
  1214. break;
  1215. }
  1216. return "unknown";
  1217. }
  1218. static void prop_add_env(const char *env)
  1219. {
  1220. struct symbol *sym, *sym2;
  1221. struct property *prop;
  1222. char *p;
  1223. sym = current_entry->sym;
  1224. sym->flags |= SYMBOL_AUTO;
  1225. for_all_properties(sym, prop, P_ENV) {
  1226. sym2 = prop_get_symbol(prop);
  1227. if (strcmp(sym2->name, env))
  1228. menu_warn(current_entry, "redefining environment symbol from %s",
  1229. sym2->name);
  1230. return;
  1231. }
  1232. prop = prop_alloc(P_ENV, sym);
  1233. prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
  1234. sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
  1235. sym_env_list->right.sym = sym;
  1236. p = getenv(env);
  1237. if (p)
  1238. sym_add_default(sym, p);
  1239. else
  1240. menu_warn(current_entry, "environment variable %s undefined", env);
  1241. }