symbol.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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 <sys/utsname.h>
  9. #define LKC_DIRECT_LINK
  10. #include "lkc.h"
  11. struct symbol symbol_yes = {
  12. name: "y",
  13. curr: { "y", yes },
  14. flags: SYMBOL_YES|SYMBOL_VALID,
  15. }, symbol_mod = {
  16. name: "m",
  17. curr: { "m", mod },
  18. flags: SYMBOL_MOD|SYMBOL_VALID,
  19. }, symbol_no = {
  20. name: "n",
  21. curr: { "n", no },
  22. flags: SYMBOL_NO|SYMBOL_VALID,
  23. }, symbol_empty = {
  24. name: "",
  25. curr: { "", no },
  26. flags: SYMBOL_VALID,
  27. };
  28. int sym_change_count;
  29. struct symbol *modules_sym;
  30. void sym_add_default(struct symbol *sym, const char *def)
  31. {
  32. struct property *prop = prop_alloc(P_DEFAULT, sym);
  33. prop->expr = expr_alloc_symbol(sym_lookup(def, 1));
  34. }
  35. void sym_init(void)
  36. {
  37. struct symbol *sym;
  38. char *p;
  39. static bool inited = false;
  40. if (inited)
  41. return;
  42. inited = true;
  43. sym = sym_lookup("VERSION", 0);
  44. sym->type = S_STRING;
  45. sym->flags |= SYMBOL_AUTO;
  46. p = getenv("VERSION");
  47. if (p)
  48. sym_add_default(sym, p);
  49. sym = sym_lookup("TARGET_ARCH", 0);
  50. sym->type = S_STRING;
  51. sym->flags |= SYMBOL_AUTO;
  52. p = getenv("TARGET_ARCH");
  53. if (p)
  54. sym_add_default(sym, p);
  55. }
  56. enum symbol_type sym_get_type(struct symbol *sym)
  57. {
  58. enum symbol_type type = sym->type;
  59. if (type == S_TRISTATE) {
  60. if (sym_is_choice_value(sym) && sym->visible == yes)
  61. type = S_BOOLEAN;
  62. else {
  63. sym_calc_value(modules_sym);
  64. if (modules_sym->curr.tri == no)
  65. type = S_BOOLEAN;
  66. }
  67. }
  68. return type;
  69. }
  70. const char *sym_type_name(enum symbol_type type)
  71. {
  72. switch (type) {
  73. case S_BOOLEAN:
  74. return "boolean";
  75. case S_TRISTATE:
  76. return "tristate";
  77. case S_INT:
  78. return "integer";
  79. case S_HEX:
  80. return "hex";
  81. case S_STRING:
  82. return "string";
  83. case S_UNKNOWN:
  84. return "unknown";
  85. case S_OTHER:
  86. break;
  87. }
  88. return "???";
  89. }
  90. struct property *sym_get_choice_prop(struct symbol *sym)
  91. {
  92. struct property *prop;
  93. for_all_choices(sym, prop)
  94. return prop;
  95. return NULL;
  96. }
  97. struct property *sym_get_default_prop(struct symbol *sym)
  98. {
  99. struct property *prop;
  100. for_all_defaults(sym, prop) {
  101. prop->visible.tri = expr_calc_value(prop->visible.expr);
  102. if (prop->visible.tri != no)
  103. return prop;
  104. }
  105. return NULL;
  106. }
  107. struct property *sym_get_range_prop(struct symbol *sym)
  108. {
  109. struct property *prop;
  110. for_all_properties(sym, prop, P_RANGE) {
  111. prop->visible.tri = expr_calc_value(prop->visible.expr);
  112. if (prop->visible.tri != no)
  113. return prop;
  114. }
  115. return NULL;
  116. }
  117. static void sym_calc_visibility(struct symbol *sym)
  118. {
  119. struct property *prop;
  120. tristate tri;
  121. /* any prompt visible? */
  122. tri = no;
  123. for_all_prompts(sym, prop) {
  124. prop->visible.tri = expr_calc_value(prop->visible.expr);
  125. tri = E_OR(tri, prop->visible.tri);
  126. }
  127. if (sym->visible != tri) {
  128. sym->visible = tri;
  129. sym_set_changed(sym);
  130. }
  131. if (sym_is_choice_value(sym))
  132. return;
  133. tri = no;
  134. if (sym->rev_dep.expr)
  135. tri = expr_calc_value(sym->rev_dep.expr);
  136. if (sym->rev_dep.tri != tri) {
  137. sym->rev_dep.tri = tri;
  138. sym_set_changed(sym);
  139. }
  140. }
  141. static struct symbol *sym_calc_choice(struct symbol *sym)
  142. {
  143. struct symbol *def_sym;
  144. struct property *prop;
  145. struct expr *e;
  146. /* is the user choice visible? */
  147. def_sym = sym->user.val;
  148. if (def_sym) {
  149. sym_calc_visibility(def_sym);
  150. if (def_sym->visible != no)
  151. return def_sym;
  152. }
  153. /* any of the defaults visible? */
  154. for_all_defaults(sym, prop) {
  155. prop->visible.tri = expr_calc_value(prop->visible.expr);
  156. if (prop->visible.tri == no)
  157. continue;
  158. def_sym = prop_get_symbol(prop);
  159. sym_calc_visibility(def_sym);
  160. if (def_sym->visible != no)
  161. return def_sym;
  162. }
  163. /* just get the first visible value */
  164. prop = sym_get_choice_prop(sym);
  165. for (e = prop->expr; e; e = e->left.expr) {
  166. def_sym = e->right.sym;
  167. sym_calc_visibility(def_sym);
  168. if (def_sym->visible != no)
  169. return def_sym;
  170. }
  171. /* no choice? reset tristate value */
  172. sym->curr.tri = no;
  173. return NULL;
  174. }
  175. void sym_calc_value(struct symbol *sym)
  176. {
  177. struct symbol_value newval, oldval;
  178. struct property *prop;
  179. struct expr *e;
  180. if (!sym)
  181. return;
  182. if (sym->flags & SYMBOL_VALID)
  183. return;
  184. sym->flags |= SYMBOL_VALID;
  185. oldval = sym->curr;
  186. switch (sym->type) {
  187. case S_INT:
  188. case S_HEX:
  189. case S_STRING:
  190. newval = symbol_empty.curr;
  191. break;
  192. case S_BOOLEAN:
  193. case S_TRISTATE:
  194. newval = symbol_no.curr;
  195. break;
  196. default:
  197. sym->curr.val = sym->name;
  198. sym->curr.tri = no;
  199. return;
  200. }
  201. if (!sym_is_choice_value(sym))
  202. sym->flags &= ~SYMBOL_WRITE;
  203. sym_calc_visibility(sym);
  204. /* set default if recursively called */
  205. sym->curr = newval;
  206. switch (sym_get_type(sym)) {
  207. case S_BOOLEAN:
  208. case S_TRISTATE:
  209. if (sym_is_choice_value(sym) && sym->visible == yes) {
  210. prop = sym_get_choice_prop(sym);
  211. newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
  212. } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
  213. sym->flags |= SYMBOL_WRITE;
  214. if (sym_has_value(sym))
  215. newval.tri = sym->user.tri;
  216. else if (!sym_is_choice(sym)) {
  217. prop = sym_get_default_prop(sym);
  218. if (prop)
  219. newval.tri = expr_calc_value(prop->expr);
  220. }
  221. newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
  222. } else if (!sym_is_choice(sym)) {
  223. prop = sym_get_default_prop(sym);
  224. if (prop) {
  225. sym->flags |= SYMBOL_WRITE;
  226. newval.tri = expr_calc_value(prop->expr);
  227. }
  228. }
  229. if (sym_get_type(sym) == S_BOOLEAN) {
  230. if (newval.tri == mod)
  231. newval.tri = yes;
  232. if (sym->visible == mod)
  233. sym->visible = yes;
  234. if (sym->rev_dep.tri == mod)
  235. sym->rev_dep.tri = yes;
  236. }
  237. break;
  238. case S_STRING:
  239. case S_HEX:
  240. case S_INT:
  241. if (sym->visible != no) {
  242. sym->flags |= SYMBOL_WRITE;
  243. if (sym_has_value(sym)) {
  244. newval.val = sym->user.val;
  245. break;
  246. }
  247. }
  248. prop = sym_get_default_prop(sym);
  249. if (prop) {
  250. struct symbol *ds = prop_get_symbol(prop);
  251. if (ds) {
  252. sym->flags |= SYMBOL_WRITE;
  253. sym_calc_value(ds);
  254. newval.val = ds->curr.val;
  255. }
  256. }
  257. break;
  258. default:
  259. ;
  260. }
  261. sym->curr = newval;
  262. if (sym_is_choice(sym) && newval.tri == yes)
  263. sym->curr.val = sym_calc_choice(sym);
  264. if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
  265. sym_set_changed(sym);
  266. if (sym_is_choice(sym)) {
  267. int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
  268. prop = sym_get_choice_prop(sym);
  269. for (e = prop->expr; e; e = e->left.expr) {
  270. e->right.sym->flags |= flags;
  271. if (flags & SYMBOL_CHANGED)
  272. sym_set_changed(e->right.sym);
  273. }
  274. }
  275. }
  276. void sym_clear_all_valid(void)
  277. {
  278. struct symbol *sym;
  279. int i;
  280. for_all_symbols(i, sym)
  281. sym->flags &= ~SYMBOL_VALID;
  282. sym_change_count++;
  283. }
  284. void sym_set_changed(struct symbol *sym)
  285. {
  286. struct property *prop;
  287. sym->flags |= SYMBOL_CHANGED;
  288. for (prop = sym->prop; prop; prop = prop->next) {
  289. if (prop->menu)
  290. prop->menu->flags |= MENU_CHANGED;
  291. }
  292. }
  293. void sym_set_all_changed(void)
  294. {
  295. struct symbol *sym;
  296. int i;
  297. for_all_symbols(i, sym)
  298. sym_set_changed(sym);
  299. }
  300. bool sym_tristate_within_range(struct symbol *sym, tristate val)
  301. {
  302. int type = sym_get_type(sym);
  303. if (sym->visible == no)
  304. return false;
  305. if (type != S_BOOLEAN && type != S_TRISTATE)
  306. return false;
  307. if (type == S_BOOLEAN && val == mod)
  308. return false;
  309. if (sym->visible <= sym->rev_dep.tri)
  310. return false;
  311. if (sym_is_choice_value(sym) && sym->visible == yes)
  312. return val == yes;
  313. return val >= sym->rev_dep.tri && val <= sym->visible;
  314. }
  315. bool sym_set_tristate_value(struct symbol *sym, tristate val)
  316. {
  317. tristate oldval = sym_get_tristate_value(sym);
  318. if (oldval != val && !sym_tristate_within_range(sym, val))
  319. return false;
  320. if (sym->flags & SYMBOL_NEW) {
  321. sym->flags &= ~SYMBOL_NEW;
  322. sym_set_changed(sym);
  323. }
  324. if (sym_is_choice_value(sym) && val == yes) {
  325. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  326. cs->user.val = sym;
  327. cs->flags &= ~SYMBOL_NEW;
  328. }
  329. sym->user.tri = val;
  330. if (oldval != val) {
  331. sym_clear_all_valid();
  332. if (sym == modules_sym)
  333. sym_set_all_changed();
  334. }
  335. return true;
  336. }
  337. tristate sym_toggle_tristate_value(struct symbol *sym)
  338. {
  339. tristate oldval, newval;
  340. oldval = newval = sym_get_tristate_value(sym);
  341. do {
  342. switch (newval) {
  343. case no:
  344. newval = mod;
  345. break;
  346. case mod:
  347. newval = yes;
  348. break;
  349. case yes:
  350. newval = no;
  351. break;
  352. }
  353. if (sym_set_tristate_value(sym, newval))
  354. break;
  355. } while (oldval != newval);
  356. return newval;
  357. }
  358. bool sym_string_valid(struct symbol *sym, const char *str)
  359. {
  360. char ch;
  361. switch (sym->type) {
  362. case S_STRING:
  363. return true;
  364. case S_INT:
  365. ch = *str++;
  366. if (ch == '-')
  367. ch = *str++;
  368. if (!isdigit(ch))
  369. return false;
  370. if (ch == '0' && *str != 0)
  371. return false;
  372. while ((ch = *str++)) {
  373. if (!isdigit(ch))
  374. return false;
  375. }
  376. return true;
  377. case S_HEX:
  378. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
  379. str += 2;
  380. ch = *str++;
  381. do {
  382. if (!isxdigit(ch))
  383. return false;
  384. } while ((ch = *str++));
  385. return true;
  386. case S_BOOLEAN:
  387. case S_TRISTATE:
  388. switch (str[0]) {
  389. case 'y': case 'Y':
  390. case 'm': case 'M':
  391. case 'n': case 'N':
  392. return true;
  393. }
  394. return false;
  395. default:
  396. return false;
  397. }
  398. }
  399. bool sym_string_within_range(struct symbol *sym, const char *str)
  400. {
  401. struct property *prop;
  402. int val;
  403. switch (sym->type) {
  404. case S_STRING:
  405. return sym_string_valid(sym, str);
  406. case S_INT:
  407. if (!sym_string_valid(sym, str))
  408. return false;
  409. prop = sym_get_range_prop(sym);
  410. if (!prop)
  411. return true;
  412. val = strtol(str, NULL, 10);
  413. return val >= strtol(prop->expr->left.sym->name, NULL, 10) &&
  414. val <= strtol(prop->expr->right.sym->name, NULL, 10);
  415. case S_HEX:
  416. if (!sym_string_valid(sym, str))
  417. return false;
  418. prop = sym_get_range_prop(sym);
  419. if (!prop)
  420. return true;
  421. val = strtol(str, NULL, 16);
  422. return val >= strtol(prop->expr->left.sym->name, NULL, 16) &&
  423. val <= strtol(prop->expr->right.sym->name, NULL, 16);
  424. case S_BOOLEAN:
  425. case S_TRISTATE:
  426. switch (str[0]) {
  427. case 'y': case 'Y':
  428. return sym_tristate_within_range(sym, yes);
  429. case 'm': case 'M':
  430. return sym_tristate_within_range(sym, mod);
  431. case 'n': case 'N':
  432. return sym_tristate_within_range(sym, no);
  433. }
  434. return false;
  435. default:
  436. return false;
  437. }
  438. }
  439. bool sym_set_string_value(struct symbol *sym, const char *newval)
  440. {
  441. const char *oldval;
  442. char *val;
  443. int size;
  444. switch (sym->type) {
  445. case S_BOOLEAN:
  446. case S_TRISTATE:
  447. switch (newval[0]) {
  448. case 'y': case 'Y':
  449. return sym_set_tristate_value(sym, yes);
  450. case 'm': case 'M':
  451. return sym_set_tristate_value(sym, mod);
  452. case 'n': case 'N':
  453. return sym_set_tristate_value(sym, no);
  454. }
  455. return false;
  456. default:
  457. ;
  458. }
  459. if (!sym_string_within_range(sym, newval))
  460. return false;
  461. if (sym->flags & SYMBOL_NEW) {
  462. sym->flags &= ~SYMBOL_NEW;
  463. sym_set_changed(sym);
  464. }
  465. oldval = sym->user.val;
  466. size = strlen(newval) + 1;
  467. if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
  468. size += 2;
  469. sym->user.val = val = malloc(size);
  470. *val++ = '0';
  471. *val++ = 'x';
  472. } else if (!oldval || strcmp(oldval, newval))
  473. sym->user.val = val = malloc(size);
  474. else
  475. return true;
  476. strcpy(val, newval);
  477. free((void *)oldval);
  478. sym_clear_all_valid();
  479. return true;
  480. }
  481. const char *sym_get_string_value(struct symbol *sym)
  482. {
  483. tristate val;
  484. switch (sym->type) {
  485. case S_BOOLEAN:
  486. case S_TRISTATE:
  487. val = sym_get_tristate_value(sym);
  488. switch (val) {
  489. case no:
  490. return "n";
  491. case mod:
  492. return "m";
  493. case yes:
  494. return "y";
  495. }
  496. break;
  497. default:
  498. ;
  499. }
  500. return (const char *)sym->curr.val;
  501. }
  502. bool sym_is_changable(struct symbol *sym)
  503. {
  504. return sym->visible > sym->rev_dep.tri;
  505. }
  506. struct symbol *sym_lookup(const char *name, int isconst)
  507. {
  508. struct symbol *symbol;
  509. const char *ptr;
  510. char *new_name;
  511. int hash = 0;
  512. if (name) {
  513. if (name[0] && !name[1]) {
  514. switch (name[0]) {
  515. case 'y': return &symbol_yes;
  516. case 'm': return &symbol_mod;
  517. case 'n': return &symbol_no;
  518. }
  519. }
  520. for (ptr = name; *ptr; ptr++)
  521. hash += *ptr;
  522. hash &= 0xff;
  523. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  524. if (!strcmp(symbol->name, name)) {
  525. if ((isconst && symbol->flags & SYMBOL_CONST) ||
  526. (!isconst && !(symbol->flags & SYMBOL_CONST)))
  527. return symbol;
  528. }
  529. }
  530. new_name = strdup(name);
  531. } else {
  532. new_name = NULL;
  533. hash = 256;
  534. }
  535. symbol = malloc(sizeof(*symbol));
  536. memset(symbol, 0, sizeof(*symbol));
  537. symbol->name = new_name;
  538. symbol->type = S_UNKNOWN;
  539. symbol->flags = SYMBOL_NEW;
  540. if (isconst)
  541. symbol->flags |= SYMBOL_CONST;
  542. symbol->next = symbol_hash[hash];
  543. symbol_hash[hash] = symbol;
  544. return symbol;
  545. }
  546. struct symbol *sym_find(const char *name)
  547. {
  548. struct symbol *symbol = NULL;
  549. const char *ptr;
  550. int hash = 0;
  551. if (!name)
  552. return NULL;
  553. if (name[0] && !name[1]) {
  554. switch (name[0]) {
  555. case 'y': return &symbol_yes;
  556. case 'm': return &symbol_mod;
  557. case 'n': return &symbol_no;
  558. }
  559. }
  560. for (ptr = name; *ptr; ptr++)
  561. hash += *ptr;
  562. hash &= 0xff;
  563. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  564. if (!strcmp(symbol->name, name) &&
  565. !(symbol->flags & SYMBOL_CONST))
  566. break;
  567. }
  568. return symbol;
  569. }
  570. struct symbol *sym_check_deps(struct symbol *sym);
  571. static struct symbol *sym_check_expr_deps(struct expr *e)
  572. {
  573. struct symbol *sym;
  574. if (!e)
  575. return NULL;
  576. switch (e->type) {
  577. case E_OR:
  578. case E_AND:
  579. sym = sym_check_expr_deps(e->left.expr);
  580. if (sym)
  581. return sym;
  582. return sym_check_expr_deps(e->right.expr);
  583. case E_NOT:
  584. return sym_check_expr_deps(e->left.expr);
  585. case E_EQUAL:
  586. case E_UNEQUAL:
  587. sym = sym_check_deps(e->left.sym);
  588. if (sym)
  589. return sym;
  590. return sym_check_deps(e->right.sym);
  591. case E_SYMBOL:
  592. return sym_check_deps(e->left.sym);
  593. default:
  594. break;
  595. }
  596. printf("Oops! How to check %d?\n", e->type);
  597. return NULL;
  598. }
  599. struct symbol *sym_check_deps(struct symbol *sym)
  600. {
  601. struct symbol *sym2;
  602. struct property *prop;
  603. if (sym->flags & SYMBOL_CHECK_DONE)
  604. return NULL;
  605. if (sym->flags & SYMBOL_CHECK) {
  606. printf("Warning! Found recursive dependency: %s", sym->name);
  607. return sym;
  608. }
  609. sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  610. sym2 = sym_check_expr_deps(sym->rev_dep.expr);
  611. if (sym2)
  612. goto out;
  613. for (prop = sym->prop; prop; prop = prop->next) {
  614. if (prop->type == P_CHOICE)
  615. continue;
  616. sym2 = sym_check_expr_deps(prop->visible.expr);
  617. if (sym2)
  618. goto out;
  619. if (prop->type != P_DEFAULT || sym_is_choice(sym))
  620. continue;
  621. sym2 = sym_check_expr_deps(prop->expr);
  622. if (sym2)
  623. goto out;
  624. }
  625. out:
  626. if (sym2)
  627. printf(" %s", sym->name);
  628. sym->flags &= ~SYMBOL_CHECK;
  629. return sym2;
  630. }
  631. struct property *prop_alloc(enum prop_type type, struct symbol *sym)
  632. {
  633. struct property *prop;
  634. struct property **propp;
  635. prop = malloc(sizeof(*prop));
  636. memset(prop, 0, sizeof(*prop));
  637. prop->type = type;
  638. prop->sym = sym;
  639. prop->file = current_file;
  640. prop->lineno = zconf_lineno();
  641. /* append property to the prop list of symbol */
  642. if (sym) {
  643. for (propp = &sym->prop; *propp; propp = &(*propp)->next)
  644. ;
  645. *propp = prop;
  646. }
  647. return prop;
  648. }
  649. struct symbol *prop_get_symbol(struct property *prop)
  650. {
  651. if (prop->expr && (prop->expr->type == E_SYMBOL ||
  652. prop->expr->type == E_CHOICE))
  653. return prop->expr->left.sym;
  654. return NULL;
  655. }
  656. const char *prop_get_type_name(enum prop_type type)
  657. {
  658. switch (type) {
  659. case P_PROMPT:
  660. return "prompt";
  661. case P_COMMENT:
  662. return "comment";
  663. case P_MENU:
  664. return "menu";
  665. case P_DEFAULT:
  666. return "default";
  667. case P_CHOICE:
  668. return "choice";
  669. case P_SELECT:
  670. return "select";
  671. case P_RANGE:
  672. return "range";
  673. case P_UNKNOWN:
  674. break;
  675. }
  676. return "unknown";
  677. }