symbol.c 29 KB

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