symbol.c 29 KB

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