symbol.c 12 KB

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