symbol.c 12 KB

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