symbol.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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 = create_prop(P_DEFAULT);
  33. struct property **propp;
  34. prop->sym = sym;
  35. prop->def = sym_lookup(def, 1);
  36. /* append property to the prop list of symbol */
  37. if (prop->sym) {
  38. for (propp = &prop->sym->prop; *propp; propp = &(*propp)->next)
  39. ;
  40. *propp = prop;
  41. }
  42. }
  43. void sym_init(void)
  44. {
  45. struct symbol *sym;
  46. struct utsname uts;
  47. char *p;
  48. static bool inited = false;
  49. if (inited)
  50. return;
  51. inited = true;
  52. uname(&uts);
  53. #if 0
  54. sym = sym_lookup("ARCH", 0);
  55. sym->type = S_STRING;
  56. sym->flags |= SYMBOL_AUTO;
  57. p = getenv("ARCH");
  58. if (p)
  59. sym_add_default(sym, p);
  60. #endif
  61. sym = sym_lookup("VERSION", 0);
  62. sym->type = S_STRING;
  63. sym->flags |= SYMBOL_AUTO;
  64. p = getenv("VERSION");
  65. if (p)
  66. sym_add_default(sym, p);
  67. #if 0
  68. sym = sym_lookup("UNAME_RELEASE", 0);
  69. sym->type = S_STRING;
  70. sym->flags |= SYMBOL_AUTO;
  71. sym_add_default(sym, uts.release);
  72. #endif
  73. sym = sym_lookup("TARGET_ARCH", 0);
  74. sym->type = S_STRING;
  75. sym->flags |= SYMBOL_AUTO;
  76. p = getenv("TARGET_ARCH");
  77. if (p)
  78. sym_add_default(sym, p);
  79. }
  80. int sym_get_type(struct symbol *sym)
  81. {
  82. int type = sym->type;
  83. if (type == S_TRISTATE) {
  84. if (sym_is_choice_value(sym) && sym->visible == yes)
  85. type = S_BOOLEAN;
  86. else {
  87. sym_calc_value(modules_sym);
  88. if (S_TRI(modules_sym->curr) == no)
  89. type = S_BOOLEAN;
  90. }
  91. }
  92. return type;
  93. }
  94. const char *sym_type_name(int type)
  95. {
  96. switch (type) {
  97. case S_BOOLEAN:
  98. return "boolean";
  99. case S_TRISTATE:
  100. return "tristate";
  101. case S_INT:
  102. return "integer";
  103. case S_HEX:
  104. return "hex";
  105. case S_STRING:
  106. return "string";
  107. case S_UNKNOWN:
  108. return "unknown";
  109. }
  110. return "???";
  111. }
  112. struct property *sym_get_choice_prop(struct symbol *sym)
  113. {
  114. struct property *prop;
  115. for_all_choices(sym, prop)
  116. return prop;
  117. return NULL;
  118. }
  119. struct property *sym_get_default_prop(struct symbol *sym)
  120. {
  121. struct property *prop;
  122. tristate visible;
  123. for_all_defaults(sym, prop) {
  124. visible = E_CALC(prop->visible);
  125. if (visible != no)
  126. return prop;
  127. }
  128. return NULL;
  129. }
  130. void sym_calc_visibility(struct symbol *sym)
  131. {
  132. struct property *prop;
  133. tristate visible, oldvisible;
  134. /* any prompt visible? */
  135. oldvisible = sym->visible;
  136. visible = no;
  137. for_all_prompts(sym, prop)
  138. visible = E_OR(visible, E_CALC(prop->visible));
  139. if (oldvisible != visible) {
  140. sym->visible = visible;
  141. sym->flags |= SYMBOL_CHANGED;
  142. }
  143. }
  144. void sym_calc_value(struct symbol *sym)
  145. {
  146. struct symbol_value newval, oldval;
  147. struct property *prop, *def_prop;
  148. struct symbol *def_sym;
  149. struct expr *e;
  150. if (sym->flags & SYMBOL_VALID)
  151. return;
  152. oldval = sym->curr;
  153. switch (sym->type) {
  154. case S_INT:
  155. case S_HEX:
  156. case S_STRING:
  157. newval = symbol_empty.curr;
  158. break;
  159. case S_BOOLEAN:
  160. case S_TRISTATE:
  161. newval = symbol_no.curr;
  162. break;
  163. default:
  164. S_VAL(newval) = sym->name;
  165. S_TRI(newval) = no;
  166. if (sym->flags & SYMBOL_CONST) {
  167. goto out;
  168. }
  169. //newval = symbol_empty.curr;
  170. // generate warning somewhere here later
  171. //S_TRI(newval) = yes;
  172. goto out;
  173. }
  174. sym->flags |= SYMBOL_VALID;
  175. if (!sym_is_choice_value(sym))
  176. sym->flags &= ~SYMBOL_WRITE;
  177. sym_calc_visibility(sym);
  178. /* set default if recursively called */
  179. sym->curr = newval;
  180. if (sym->visible != no) {
  181. sym->flags |= SYMBOL_WRITE;
  182. if (!sym_has_value(sym)) {
  183. if (!sym_is_choice(sym)) {
  184. prop = sym_get_default_prop(sym);
  185. if (prop) {
  186. sym_calc_value(prop->def);
  187. newval = prop->def->curr;
  188. }
  189. }
  190. } else
  191. newval = sym->def;
  192. S_TRI(newval) = E_AND(S_TRI(newval), sym->visible);
  193. /* if the symbol is visible and not optionial,
  194. * possibly ignore old user choice. */
  195. if (!sym_is_optional(sym) && S_TRI(newval) == no)
  196. S_TRI(newval) = sym->visible;
  197. if (sym_is_choice_value(sym) && sym->visible == yes) {
  198. prop = sym_get_choice_prop(sym);
  199. S_TRI(newval) = (S_VAL(prop->def->curr) == sym) ? yes : no;
  200. }
  201. } else {
  202. prop = sym_get_default_prop(sym);
  203. if (prop) {
  204. sym->flags |= SYMBOL_WRITE;
  205. sym_calc_value(prop->def);
  206. newval = prop->def->curr;
  207. }
  208. }
  209. switch (sym_get_type(sym)) {
  210. case S_TRISTATE:
  211. if (S_TRI(newval) != mod)
  212. break;
  213. sym_calc_value(modules_sym);
  214. if (S_TRI(modules_sym->curr) == no)
  215. S_TRI(newval) = yes;
  216. break;
  217. case S_BOOLEAN:
  218. if (S_TRI(newval) == mod)
  219. S_TRI(newval) = yes;
  220. }
  221. out:
  222. sym->curr = newval;
  223. if (sym_is_choice(sym) && S_TRI(newval) == yes) {
  224. def_sym = S_VAL(sym->def);
  225. if (def_sym) {
  226. sym_calc_visibility(def_sym);
  227. if (def_sym->visible == no)
  228. def_sym = NULL;
  229. }
  230. if (!def_sym) {
  231. for_all_defaults(sym, def_prop) {
  232. if (E_CALC(def_prop->visible) == no)
  233. continue;
  234. sym_calc_visibility(def_prop->def);
  235. if (def_prop->def->visible != no) {
  236. def_sym = def_prop->def;
  237. break;
  238. }
  239. }
  240. }
  241. if (!def_sym) {
  242. prop = sym_get_choice_prop(sym);
  243. for (e = prop->dep; e; e = e->left.expr) {
  244. sym_calc_visibility(e->right.sym);
  245. if (e->right.sym->visible != no) {
  246. def_sym = e->right.sym;
  247. break;
  248. }
  249. }
  250. }
  251. S_VAL(newval) = def_sym;
  252. }
  253. if (memcmp(&oldval, &newval, sizeof(newval)))
  254. sym->flags |= SYMBOL_CHANGED;
  255. sym->curr = newval;
  256. if (sym_is_choice(sym)) {
  257. int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
  258. prop = sym_get_choice_prop(sym);
  259. for (e = prop->dep; e; e = e->left.expr)
  260. e->right.sym->flags |= flags;
  261. }
  262. }
  263. void sym_clear_all_valid(void)
  264. {
  265. struct symbol *sym;
  266. int i;
  267. for_all_symbols(i, sym)
  268. sym->flags &= ~SYMBOL_VALID;
  269. sym_change_count++;
  270. }
  271. void sym_set_all_changed(void)
  272. {
  273. struct symbol *sym;
  274. int i;
  275. for_all_symbols(i, sym)
  276. sym->flags |= SYMBOL_CHANGED;
  277. }
  278. bool sym_tristate_within_range(struct symbol *sym, tristate val)
  279. {
  280. int type = sym_get_type(sym);
  281. if (sym->visible == no)
  282. return false;
  283. if (type != S_BOOLEAN && type != S_TRISTATE)
  284. return false;
  285. switch (val) {
  286. case no:
  287. if (sym_is_choice_value(sym) && sym->visible == yes)
  288. return false;
  289. return sym_is_optional(sym);
  290. case mod:
  291. if (sym_is_choice_value(sym) && sym->visible == yes)
  292. return false;
  293. return type == S_TRISTATE;
  294. case yes:
  295. return type == S_BOOLEAN || sym->visible == yes;
  296. }
  297. return false;
  298. }
  299. bool sym_set_tristate_value(struct symbol *sym, tristate val)
  300. {
  301. tristate oldval = sym_get_tristate_value(sym);
  302. if (oldval != val && !sym_tristate_within_range(sym, val))
  303. return false;
  304. if (sym->flags & SYMBOL_NEW) {
  305. sym->flags &= ~SYMBOL_NEW;
  306. sym->flags |= SYMBOL_CHANGED;
  307. }
  308. if (sym_is_choice_value(sym) && val == yes) {
  309. struct property *prop = sym_get_choice_prop(sym);
  310. S_VAL(prop->def->def) = sym;
  311. prop->def->flags &= ~SYMBOL_NEW;
  312. }
  313. S_TRI(sym->def) = val;
  314. if (oldval != val) {
  315. sym_clear_all_valid();
  316. if (sym == modules_sym)
  317. sym_set_all_changed();
  318. }
  319. return true;
  320. }
  321. tristate sym_toggle_tristate_value(struct symbol *sym)
  322. {
  323. tristate oldval, newval;
  324. oldval = newval = sym_get_tristate_value(sym);
  325. do {
  326. switch (newval) {
  327. case no:
  328. newval = mod;
  329. break;
  330. case mod:
  331. newval = yes;
  332. break;
  333. case yes:
  334. newval = no;
  335. break;
  336. }
  337. if (sym_set_tristate_value(sym, newval))
  338. break;
  339. } while (oldval != newval);
  340. return newval;
  341. }
  342. bool sym_string_valid(struct symbol *sym, const char *str)
  343. {
  344. char ch;
  345. switch (sym->type) {
  346. case S_STRING:
  347. return true;
  348. case S_INT:
  349. ch = *str++;
  350. if (ch == '-')
  351. ch = *str++;
  352. if (!isdigit((int)ch))
  353. return false;
  354. if (ch == '0' && *str != 0)
  355. return false;
  356. while ((ch = *str++)) {
  357. if (!isdigit((int)ch))
  358. return false;
  359. }
  360. return true;
  361. case S_HEX:
  362. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
  363. str += 2;
  364. ch = *str++;
  365. do {
  366. if (!isxdigit((int)ch))
  367. return false;
  368. } while ((ch = *str++));
  369. return true;
  370. case S_BOOLEAN:
  371. case S_TRISTATE:
  372. switch (str[0]) {
  373. case 'y':
  374. case 'Y':
  375. return sym_tristate_within_range(sym, yes);
  376. case 'm':
  377. case 'M':
  378. return sym_tristate_within_range(sym, mod);
  379. case 'n':
  380. case 'N':
  381. return sym_tristate_within_range(sym, no);
  382. }
  383. return false;
  384. default:
  385. return false;
  386. }
  387. }
  388. bool sym_set_string_value(struct symbol *sym, const char *newval)
  389. {
  390. const char *oldval;
  391. char *val;
  392. int size;
  393. switch (sym->type) {
  394. case S_BOOLEAN:
  395. case S_TRISTATE:
  396. switch (newval[0]) {
  397. case 'y':
  398. case 'Y':
  399. return sym_set_tristate_value(sym, yes);
  400. case 'm':
  401. case 'M':
  402. return sym_set_tristate_value(sym, mod);
  403. case 'n':
  404. case 'N':
  405. return sym_set_tristate_value(sym, no);
  406. }
  407. return false;
  408. default:
  409. ;
  410. }
  411. if (!sym_string_valid(sym, newval))
  412. return false;
  413. if (sym->flags & SYMBOL_NEW) {
  414. sym->flags &= ~SYMBOL_NEW;
  415. sym->flags |= SYMBOL_CHANGED;
  416. }
  417. oldval = S_VAL(sym->def);
  418. size = strlen(newval) + 1;
  419. if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
  420. size += 2;
  421. S_VAL(sym->def) = val = malloc(size);
  422. *val++ = '0';
  423. *val++ = 'x';
  424. } else if (!oldval || strcmp(oldval, newval))
  425. S_VAL(sym->def) = val = malloc(size);
  426. else
  427. return true;
  428. strcpy(val, newval);
  429. free((void *)oldval);
  430. sym_clear_all_valid();
  431. return true;
  432. }
  433. const char *sym_get_string_value(struct symbol *sym)
  434. {
  435. tristate val;
  436. switch (sym->type) {
  437. case S_BOOLEAN:
  438. case S_TRISTATE:
  439. val = sym_get_tristate_value(sym);
  440. switch (val) {
  441. case no:
  442. return "n";
  443. case mod:
  444. return "m";
  445. case yes:
  446. return "y";
  447. }
  448. break;
  449. default:
  450. ;
  451. }
  452. return (const char *)S_VAL(sym->curr);
  453. }
  454. bool sym_is_changable(struct symbol *sym)
  455. {
  456. if (sym->visible == no)
  457. return false;
  458. /* at least 'n' and 'y'/'m' is selectable */
  459. if (sym_is_optional(sym))
  460. return true;
  461. /* no 'n', so 'y' and 'm' must be selectable */
  462. if (sym_get_type(sym) == S_TRISTATE && sym->visible == yes)
  463. return true;
  464. return false;
  465. }
  466. struct symbol *sym_lookup(const char *name, int isconst)
  467. {
  468. struct symbol *symbol;
  469. const char *ptr;
  470. char *new_name;
  471. int hash = 0;
  472. //printf("lookup: %s -> ", name);
  473. if (name) {
  474. if (name[0] && !name[1]) {
  475. switch (name[0]) {
  476. case 'y': return &symbol_yes;
  477. case 'm': return &symbol_mod;
  478. case 'n': return &symbol_no;
  479. }
  480. }
  481. for (ptr = name; *ptr; ptr++)
  482. hash += *ptr;
  483. hash &= 0xff;
  484. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  485. if (!strcmp(symbol->name, name)) {
  486. if ((isconst && symbol->flags & SYMBOL_CONST) ||
  487. (!isconst && !(symbol->flags & SYMBOL_CONST))) {
  488. //printf("h:%p\n", symbol);
  489. return symbol;
  490. }
  491. }
  492. }
  493. new_name = strdup(name);
  494. } else {
  495. new_name = NULL;
  496. hash = 256;
  497. }
  498. symbol = malloc(sizeof(*symbol));
  499. memset(symbol, 0, sizeof(*symbol));
  500. symbol->name = new_name;
  501. symbol->type = S_UNKNOWN;
  502. symbol->flags = SYMBOL_NEW;
  503. if (isconst)
  504. symbol->flags |= SYMBOL_CONST;
  505. symbol->next = symbol_hash[hash];
  506. symbol_hash[hash] = symbol;
  507. //printf("n:%p\n", symbol);
  508. return symbol;
  509. }
  510. struct symbol *sym_find(const char *name)
  511. {
  512. struct symbol *symbol = NULL;
  513. const char *ptr;
  514. int hash = 0;
  515. if (!name)
  516. return NULL;
  517. if (name[0] && !name[1]) {
  518. switch (name[0]) {
  519. case 'y': return &symbol_yes;
  520. case 'm': return &symbol_mod;
  521. case 'n': return &symbol_no;
  522. }
  523. }
  524. for (ptr = name; *ptr; ptr++)
  525. hash += *ptr;
  526. hash &= 0xff;
  527. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  528. if (!strcmp(symbol->name, name) &&
  529. !(symbol->flags & SYMBOL_CONST))
  530. break;
  531. }
  532. return symbol;
  533. }
  534. const char *prop_get_type_name(enum prop_type type)
  535. {
  536. switch (type) {
  537. case P_PROMPT:
  538. return "prompt";
  539. case P_COMMENT:
  540. return "comment";
  541. case P_MENU:
  542. return "menu";
  543. case P_ROOTMENU:
  544. return "rootmenu";
  545. case P_DEFAULT:
  546. return "default";
  547. case P_CHOICE:
  548. return "choice";
  549. default:
  550. return "unknown";
  551. }
  552. }