1
0

symbol.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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. #define LKC_DIRECT_LINK
  11. #include "lkc.h"
  12. struct symbol symbol_yes = {
  13. .name = "y",
  14. .curr = { "y", yes },
  15. .flags = SYMBOL_CONST|SYMBOL_VALID,
  16. }, symbol_mod = {
  17. .name = "m",
  18. .curr = { "m", mod },
  19. .flags = SYMBOL_CONST|SYMBOL_VALID,
  20. }, symbol_no = {
  21. .name = "n",
  22. .curr = { "n", no },
  23. .flags = SYMBOL_CONST|SYMBOL_VALID,
  24. }, symbol_empty = {
  25. .name = "",
  26. .curr = { "", no },
  27. .flags = SYMBOL_VALID,
  28. };
  29. struct symbol *sym_defconfig_list;
  30. struct symbol *modules_sym;
  31. tristate modules_val;
  32. struct expr *sym_env_list;
  33. void sym_add_default(struct symbol *sym, const char *def)
  34. {
  35. struct property *prop = prop_alloc(P_DEFAULT, sym);
  36. prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
  37. }
  38. void sym_init(void)
  39. {
  40. struct symbol *sym;
  41. struct utsname uts;
  42. static bool inited = false;
  43. if (inited)
  44. return;
  45. inited = true;
  46. uname(&uts);
  47. sym = sym_lookup("UNAME_RELEASE", 0);
  48. sym->type = S_STRING;
  49. sym->flags |= SYMBOL_AUTO;
  50. sym_add_default(sym, uts.release);
  51. }
  52. enum symbol_type sym_get_type(struct symbol *sym)
  53. {
  54. enum symbol_type type = sym->type;
  55. if (type == S_TRISTATE) {
  56. if (sym_is_choice_value(sym) && sym->visible == yes)
  57. type = S_BOOLEAN;
  58. else if (modules_val == no)
  59. type = S_BOOLEAN;
  60. }
  61. return type;
  62. }
  63. const char *sym_type_name(enum symbol_type type)
  64. {
  65. switch (type) {
  66. case S_BOOLEAN:
  67. return "boolean";
  68. case S_TRISTATE:
  69. return "tristate";
  70. case S_INT:
  71. return "integer";
  72. case S_HEX:
  73. return "hex";
  74. case S_STRING:
  75. return "string";
  76. case S_UNKNOWN:
  77. return "unknown";
  78. case S_OTHER:
  79. break;
  80. }
  81. return "???";
  82. }
  83. struct property *sym_get_choice_prop(struct symbol *sym)
  84. {
  85. struct property *prop;
  86. for_all_choices(sym, prop)
  87. return prop;
  88. return NULL;
  89. }
  90. struct property *sym_get_env_prop(struct symbol *sym)
  91. {
  92. struct property *prop;
  93. for_all_properties(sym, prop, P_ENV)
  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 int sym_get_range_val(struct symbol *sym, int base)
  118. {
  119. sym_calc_value(sym);
  120. switch (sym->type) {
  121. case S_INT:
  122. base = 10;
  123. break;
  124. case S_HEX:
  125. base = 16;
  126. break;
  127. default:
  128. break;
  129. }
  130. return strtol(sym->curr.val, NULL, base);
  131. }
  132. static void sym_validate_range(struct symbol *sym)
  133. {
  134. struct property *prop;
  135. int base, val, val2;
  136. char str[64];
  137. switch (sym->type) {
  138. case S_INT:
  139. base = 10;
  140. break;
  141. case S_HEX:
  142. base = 16;
  143. break;
  144. default:
  145. return;
  146. }
  147. prop = sym_get_range_prop(sym);
  148. if (!prop)
  149. return;
  150. val = strtol(sym->curr.val, NULL, base);
  151. val2 = sym_get_range_val(prop->expr->left.sym, base);
  152. if (val >= val2) {
  153. val2 = sym_get_range_val(prop->expr->right.sym, base);
  154. if (val <= val2)
  155. return;
  156. }
  157. if (sym->type == S_INT)
  158. sprintf(str, "%d", val2);
  159. else
  160. sprintf(str, "0x%x", val2);
  161. sym->curr.val = strdup(str);
  162. }
  163. static void sym_calc_visibility(struct symbol *sym)
  164. {
  165. struct property *prop;
  166. tristate tri;
  167. /* any prompt visible? */
  168. tri = no;
  169. for_all_prompts(sym, prop) {
  170. prop->visible.tri = expr_calc_value(prop->visible.expr);
  171. tri = EXPR_OR(tri, prop->visible.tri);
  172. }
  173. if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
  174. tri = yes;
  175. if (sym->visible != tri) {
  176. sym->visible = tri;
  177. sym_set_changed(sym);
  178. }
  179. if (sym_is_choice_value(sym))
  180. return;
  181. tri = no;
  182. if (sym->rev_dep.expr)
  183. tri = expr_calc_value(sym->rev_dep.expr);
  184. if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
  185. tri = yes;
  186. if (sym->rev_dep.tri != tri) {
  187. sym->rev_dep.tri = tri;
  188. sym_set_changed(sym);
  189. }
  190. }
  191. static struct symbol *sym_calc_choice(struct symbol *sym)
  192. {
  193. struct symbol *def_sym;
  194. struct property *prop;
  195. struct expr *e;
  196. /* is the user choice visible? */
  197. def_sym = sym->def[S_DEF_USER].val;
  198. if (def_sym) {
  199. sym_calc_visibility(def_sym);
  200. if (def_sym->visible != no)
  201. return def_sym;
  202. }
  203. /* any of the defaults visible? */
  204. for_all_defaults(sym, prop) {
  205. prop->visible.tri = expr_calc_value(prop->visible.expr);
  206. if (prop->visible.tri == no)
  207. continue;
  208. def_sym = prop_get_symbol(prop);
  209. sym_calc_visibility(def_sym);
  210. if (def_sym->visible != no)
  211. return def_sym;
  212. }
  213. /* just get the first visible value */
  214. prop = sym_get_choice_prop(sym);
  215. expr_list_for_each_sym(prop->expr, e, def_sym) {
  216. sym_calc_visibility(def_sym);
  217. if (def_sym->visible != no)
  218. return def_sym;
  219. }
  220. /* no choice? reset tristate value */
  221. sym->curr.tri = no;
  222. return NULL;
  223. }
  224. void sym_calc_value(struct symbol *sym)
  225. {
  226. struct symbol_value newval, oldval;
  227. struct property *prop;
  228. struct expr *e;
  229. if (!sym)
  230. return;
  231. if (sym->flags & SYMBOL_VALID)
  232. return;
  233. sym->flags |= SYMBOL_VALID;
  234. oldval = sym->curr;
  235. switch (sym->type) {
  236. case S_INT:
  237. case S_HEX:
  238. case S_STRING:
  239. newval = symbol_empty.curr;
  240. break;
  241. case S_BOOLEAN:
  242. case S_TRISTATE:
  243. newval = symbol_no.curr;
  244. break;
  245. default:
  246. sym->curr.val = sym->name;
  247. sym->curr.tri = no;
  248. return;
  249. }
  250. if (!sym_is_choice_value(sym))
  251. sym->flags &= ~SYMBOL_WRITE;
  252. sym_calc_visibility(sym);
  253. /* set default if recursively called */
  254. sym->curr = newval;
  255. switch (sym_get_type(sym)) {
  256. case S_BOOLEAN:
  257. case S_TRISTATE:
  258. if (sym_is_choice_value(sym) && sym->visible == yes) {
  259. prop = sym_get_choice_prop(sym);
  260. newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
  261. } else {
  262. if (sym->visible != no) {
  263. /* if the symbol is visible use the user value
  264. * if available, otherwise try the default value
  265. */
  266. sym->flags |= SYMBOL_WRITE;
  267. if (sym_has_value(sym)) {
  268. newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
  269. sym->visible);
  270. goto calc_newval;
  271. }
  272. }
  273. if (sym->rev_dep.tri != no)
  274. sym->flags |= SYMBOL_WRITE;
  275. if (!sym_is_choice(sym)) {
  276. prop = sym_get_default_prop(sym);
  277. if (prop) {
  278. sym->flags |= SYMBOL_WRITE;
  279. newval.tri = EXPR_AND(expr_calc_value(prop->expr),
  280. prop->visible.tri);
  281. }
  282. }
  283. calc_newval:
  284. newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
  285. }
  286. if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
  287. newval.tri = yes;
  288. break;
  289. case S_STRING:
  290. case S_HEX:
  291. case S_INT:
  292. if (sym->visible != no) {
  293. sym->flags |= SYMBOL_WRITE;
  294. if (sym_has_value(sym)) {
  295. newval.val = sym->def[S_DEF_USER].val;
  296. break;
  297. }
  298. }
  299. prop = sym_get_default_prop(sym);
  300. if (prop) {
  301. struct symbol *ds = prop_get_symbol(prop);
  302. if (ds) {
  303. sym->flags |= SYMBOL_WRITE;
  304. sym_calc_value(ds);
  305. newval.val = ds->curr.val;
  306. }
  307. }
  308. break;
  309. default:
  310. ;
  311. }
  312. sym->curr = newval;
  313. if (sym_is_choice(sym) && newval.tri == yes)
  314. sym->curr.val = sym_calc_choice(sym);
  315. sym_validate_range(sym);
  316. if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
  317. sym_set_changed(sym);
  318. if (modules_sym == sym) {
  319. sym_set_all_changed();
  320. modules_val = modules_sym->curr.tri;
  321. }
  322. }
  323. if (sym_is_choice(sym)) {
  324. struct symbol *choice_sym;
  325. int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
  326. prop = sym_get_choice_prop(sym);
  327. expr_list_for_each_sym(prop->expr, e, choice_sym) {
  328. choice_sym->flags |= flags;
  329. if (flags & SYMBOL_CHANGED)
  330. sym_set_changed(choice_sym);
  331. }
  332. }
  333. if (sym->flags & SYMBOL_AUTO)
  334. sym->flags &= ~SYMBOL_WRITE;
  335. }
  336. void sym_clear_all_valid(void)
  337. {
  338. struct symbol *sym;
  339. int i;
  340. for_all_symbols(i, sym)
  341. sym->flags &= ~SYMBOL_VALID;
  342. sym_add_change_count(1);
  343. if (modules_sym)
  344. sym_calc_value(modules_sym);
  345. }
  346. void sym_set_changed(struct symbol *sym)
  347. {
  348. struct property *prop;
  349. sym->flags |= SYMBOL_CHANGED;
  350. for (prop = sym->prop; prop; prop = prop->next) {
  351. if (prop->menu)
  352. prop->menu->flags |= MENU_CHANGED;
  353. }
  354. }
  355. void sym_set_all_changed(void)
  356. {
  357. struct symbol *sym;
  358. int i;
  359. for_all_symbols(i, sym)
  360. sym_set_changed(sym);
  361. }
  362. bool sym_tristate_within_range(struct symbol *sym, tristate val)
  363. {
  364. int type = sym_get_type(sym);
  365. if (sym->visible == no)
  366. return false;
  367. if (type != S_BOOLEAN && type != S_TRISTATE)
  368. return false;
  369. if (type == S_BOOLEAN && val == mod)
  370. return false;
  371. if (sym->visible <= sym->rev_dep.tri)
  372. return false;
  373. if (sym_is_choice_value(sym) && sym->visible == yes)
  374. return val == yes;
  375. return val >= sym->rev_dep.tri && val <= sym->visible;
  376. }
  377. bool sym_set_tristate_value(struct symbol *sym, tristate val)
  378. {
  379. tristate oldval = sym_get_tristate_value(sym);
  380. if (oldval != val && !sym_tristate_within_range(sym, val))
  381. return false;
  382. if (!(sym->flags & SYMBOL_DEF_USER)) {
  383. sym->flags |= SYMBOL_DEF_USER;
  384. sym_set_changed(sym);
  385. }
  386. /*
  387. * setting a choice value also resets the new flag of the choice
  388. * symbol and all other choice values.
  389. */
  390. if (sym_is_choice_value(sym) && val == yes) {
  391. struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
  392. struct property *prop;
  393. struct expr *e;
  394. cs->def[S_DEF_USER].val = sym;
  395. cs->flags |= SYMBOL_DEF_USER;
  396. prop = sym_get_choice_prop(cs);
  397. for (e = prop->expr; e; e = e->left.expr) {
  398. if (e->right.sym->visible != no)
  399. e->right.sym->flags |= SYMBOL_DEF_USER;
  400. }
  401. }
  402. sym->def[S_DEF_USER].tri = val;
  403. if (oldval != val)
  404. sym_clear_all_valid();
  405. return true;
  406. }
  407. tristate sym_toggle_tristate_value(struct symbol *sym)
  408. {
  409. tristate oldval, newval;
  410. oldval = newval = sym_get_tristate_value(sym);
  411. do {
  412. switch (newval) {
  413. case no:
  414. newval = mod;
  415. break;
  416. case mod:
  417. newval = yes;
  418. break;
  419. case yes:
  420. newval = no;
  421. break;
  422. }
  423. if (sym_set_tristate_value(sym, newval))
  424. break;
  425. } while (oldval != newval);
  426. return newval;
  427. }
  428. bool sym_string_valid(struct symbol *sym, const char *str)
  429. {
  430. signed char ch;
  431. switch (sym->type) {
  432. case S_STRING:
  433. return true;
  434. case S_INT:
  435. ch = *str++;
  436. if (ch == '-')
  437. ch = *str++;
  438. if (!isdigit(ch))
  439. return false;
  440. if (ch == '0' && *str != 0)
  441. return false;
  442. while ((ch = *str++)) {
  443. if (!isdigit(ch))
  444. return false;
  445. }
  446. return true;
  447. case S_HEX:
  448. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
  449. str += 2;
  450. ch = *str++;
  451. do {
  452. if (!isxdigit(ch))
  453. return false;
  454. } while ((ch = *str++));
  455. return true;
  456. case S_BOOLEAN:
  457. case S_TRISTATE:
  458. switch (str[0]) {
  459. case 'y': case 'Y':
  460. case 'm': case 'M':
  461. case 'n': case 'N':
  462. return true;
  463. }
  464. return false;
  465. default:
  466. return false;
  467. }
  468. }
  469. bool sym_string_within_range(struct symbol *sym, const char *str)
  470. {
  471. struct property *prop;
  472. int val;
  473. switch (sym->type) {
  474. case S_STRING:
  475. return sym_string_valid(sym, str);
  476. case S_INT:
  477. if (!sym_string_valid(sym, str))
  478. return false;
  479. prop = sym_get_range_prop(sym);
  480. if (!prop)
  481. return true;
  482. val = strtol(str, NULL, 10);
  483. return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
  484. val <= sym_get_range_val(prop->expr->right.sym, 10);
  485. case S_HEX:
  486. if (!sym_string_valid(sym, str))
  487. return false;
  488. prop = sym_get_range_prop(sym);
  489. if (!prop)
  490. return true;
  491. val = strtol(str, NULL, 16);
  492. return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
  493. val <= sym_get_range_val(prop->expr->right.sym, 16);
  494. case S_BOOLEAN:
  495. case S_TRISTATE:
  496. switch (str[0]) {
  497. case 'y': case 'Y':
  498. return sym_tristate_within_range(sym, yes);
  499. case 'm': case 'M':
  500. return sym_tristate_within_range(sym, mod);
  501. case 'n': case 'N':
  502. return sym_tristate_within_range(sym, no);
  503. }
  504. return false;
  505. default:
  506. return false;
  507. }
  508. }
  509. bool sym_set_string_value(struct symbol *sym, const char *newval)
  510. {
  511. const char *oldval;
  512. char *val;
  513. int size;
  514. switch (sym->type) {
  515. case S_BOOLEAN:
  516. case S_TRISTATE:
  517. switch (newval[0]) {
  518. case 'y': case 'Y':
  519. return sym_set_tristate_value(sym, yes);
  520. case 'm': case 'M':
  521. return sym_set_tristate_value(sym, mod);
  522. case 'n': case 'N':
  523. return sym_set_tristate_value(sym, no);
  524. }
  525. return false;
  526. default:
  527. ;
  528. }
  529. if (!sym_string_within_range(sym, newval))
  530. return false;
  531. if (!(sym->flags & SYMBOL_DEF_USER)) {
  532. sym->flags |= SYMBOL_DEF_USER;
  533. sym_set_changed(sym);
  534. }
  535. oldval = sym->def[S_DEF_USER].val;
  536. size = strlen(newval) + 1;
  537. if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
  538. size += 2;
  539. sym->def[S_DEF_USER].val = val = malloc(size);
  540. *val++ = '0';
  541. *val++ = 'x';
  542. } else if (!oldval || strcmp(oldval, newval))
  543. sym->def[S_DEF_USER].val = val = malloc(size);
  544. else
  545. return true;
  546. strcpy(val, newval);
  547. free((void *)oldval);
  548. sym_clear_all_valid();
  549. return true;
  550. }
  551. const char *sym_get_string_value(struct symbol *sym)
  552. {
  553. tristate val;
  554. switch (sym->type) {
  555. case S_BOOLEAN:
  556. case S_TRISTATE:
  557. val = sym_get_tristate_value(sym);
  558. switch (val) {
  559. case no:
  560. return "n";
  561. case mod:
  562. return "m";
  563. case yes:
  564. return "y";
  565. }
  566. break;
  567. default:
  568. ;
  569. }
  570. return (const char *)sym->curr.val;
  571. }
  572. bool sym_is_changable(struct symbol *sym)
  573. {
  574. return sym->visible > sym->rev_dep.tri;
  575. }
  576. struct symbol *sym_lookup(const char *name, int flags)
  577. {
  578. struct symbol *symbol;
  579. const char *ptr;
  580. char *new_name;
  581. int hash = 0;
  582. if (name) {
  583. if (name[0] && !name[1]) {
  584. switch (name[0]) {
  585. case 'y': return &symbol_yes;
  586. case 'm': return &symbol_mod;
  587. case 'n': return &symbol_no;
  588. }
  589. }
  590. for (ptr = name; *ptr; ptr++)
  591. hash += *ptr;
  592. hash &= 0xff;
  593. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  594. if (!strcmp(symbol->name, name) &&
  595. (flags ? symbol->flags & flags
  596. : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
  597. return symbol;
  598. }
  599. new_name = strdup(name);
  600. } else {
  601. new_name = NULL;
  602. hash = 256;
  603. }
  604. symbol = malloc(sizeof(*symbol));
  605. memset(symbol, 0, sizeof(*symbol));
  606. symbol->name = new_name;
  607. symbol->type = S_UNKNOWN;
  608. symbol->flags |= flags;
  609. symbol->next = symbol_hash[hash];
  610. symbol_hash[hash] = symbol;
  611. return symbol;
  612. }
  613. struct symbol *sym_find(const char *name)
  614. {
  615. struct symbol *symbol = NULL;
  616. const char *ptr;
  617. int hash = 0;
  618. if (!name)
  619. return NULL;
  620. if (name[0] && !name[1]) {
  621. switch (name[0]) {
  622. case 'y': return &symbol_yes;
  623. case 'm': return &symbol_mod;
  624. case 'n': return &symbol_no;
  625. }
  626. }
  627. for (ptr = name; *ptr; ptr++)
  628. hash += *ptr;
  629. hash &= 0xff;
  630. for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
  631. if (!strcmp(symbol->name, name) &&
  632. !(symbol->flags & SYMBOL_CONST))
  633. break;
  634. }
  635. return symbol;
  636. }
  637. struct symbol **sym_re_search(const char *pattern)
  638. {
  639. struct symbol *sym, **sym_arr = NULL;
  640. int i, cnt, size;
  641. regex_t re;
  642. cnt = size = 0;
  643. /* Skip if empty */
  644. if (strlen(pattern) == 0)
  645. return NULL;
  646. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
  647. return NULL;
  648. for_all_symbols(i, sym) {
  649. if (sym->flags & SYMBOL_CONST || !sym->name)
  650. continue;
  651. if (regexec(&re, sym->name, 0, NULL, 0))
  652. continue;
  653. if (cnt + 1 >= size) {
  654. void *tmp = sym_arr;
  655. size += 16;
  656. sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
  657. if (!sym_arr) {
  658. free(tmp);
  659. return NULL;
  660. }
  661. }
  662. sym_arr[cnt++] = sym;
  663. }
  664. if (sym_arr)
  665. sym_arr[cnt] = NULL;
  666. regfree(&re);
  667. return sym_arr;
  668. }
  669. static struct symbol *sym_check_expr_deps(struct expr *e)
  670. {
  671. struct symbol *sym;
  672. if (!e)
  673. return NULL;
  674. switch (e->type) {
  675. case E_OR:
  676. case E_AND:
  677. sym = sym_check_expr_deps(e->left.expr);
  678. if (sym)
  679. return sym;
  680. return sym_check_expr_deps(e->right.expr);
  681. case E_NOT:
  682. return sym_check_expr_deps(e->left.expr);
  683. case E_EQUAL:
  684. case E_UNEQUAL:
  685. sym = sym_check_deps(e->left.sym);
  686. if (sym)
  687. return sym;
  688. return sym_check_deps(e->right.sym);
  689. case E_SYMBOL:
  690. return sym_check_deps(e->left.sym);
  691. default:
  692. break;
  693. }
  694. printf("Oops! How to check %d?\n", e->type);
  695. return NULL;
  696. }
  697. /* return NULL when dependencies are OK */
  698. static struct symbol *sym_check_sym_deps(struct symbol *sym)
  699. {
  700. struct symbol *sym2;
  701. struct property *prop;
  702. sym2 = sym_check_expr_deps(sym->rev_dep.expr);
  703. if (sym2)
  704. return sym2;
  705. for (prop = sym->prop; prop; prop = prop->next) {
  706. if (prop->type == P_CHOICE || prop->type == P_SELECT)
  707. continue;
  708. sym2 = sym_check_expr_deps(prop->visible.expr);
  709. if (sym2)
  710. break;
  711. if (prop->type != P_DEFAULT || sym_is_choice(sym))
  712. continue;
  713. sym2 = sym_check_expr_deps(prop->expr);
  714. if (sym2)
  715. break;
  716. }
  717. return sym2;
  718. }
  719. static struct symbol *sym_check_choice_deps(struct symbol *choice)
  720. {
  721. struct symbol *sym, *sym2;
  722. struct property *prop;
  723. struct expr *e;
  724. prop = sym_get_choice_prop(choice);
  725. expr_list_for_each_sym(prop->expr, e, sym)
  726. sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  727. choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  728. sym2 = sym_check_sym_deps(choice);
  729. choice->flags &= ~SYMBOL_CHECK;
  730. if (sym2)
  731. goto out;
  732. expr_list_for_each_sym(prop->expr, e, sym) {
  733. sym2 = sym_check_sym_deps(sym);
  734. if (sym2) {
  735. fprintf(stderr, " -> %s", sym->name);
  736. break;
  737. }
  738. }
  739. out:
  740. expr_list_for_each_sym(prop->expr, e, sym)
  741. sym->flags &= ~SYMBOL_CHECK;
  742. if (sym2 && sym_is_choice_value(sym2) &&
  743. prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
  744. sym2 = choice;
  745. return sym2;
  746. }
  747. struct symbol *sym_check_deps(struct symbol *sym)
  748. {
  749. struct symbol *sym2;
  750. struct property *prop;
  751. if (sym->flags & SYMBOL_CHECK) {
  752. fprintf(stderr, "%s:%d:error: found recursive dependency: %s",
  753. sym->prop->file->name, sym->prop->lineno,
  754. sym->name ? sym->name : "<choice>");
  755. return sym;
  756. }
  757. if (sym->flags & SYMBOL_CHECKED)
  758. return NULL;
  759. if (sym_is_choice_value(sym)) {
  760. /* for choice groups start the check with main choice symbol */
  761. prop = sym_get_choice_prop(sym);
  762. sym2 = sym_check_deps(prop_get_symbol(prop));
  763. } else if (sym_is_choice(sym)) {
  764. sym2 = sym_check_choice_deps(sym);
  765. } else {
  766. sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  767. sym2 = sym_check_sym_deps(sym);
  768. sym->flags &= ~SYMBOL_CHECK;
  769. }
  770. if (sym2) {
  771. fprintf(stderr, " -> %s", sym->name ? sym->name : "<choice>");
  772. if (sym2 == sym) {
  773. fprintf(stderr, "\n");
  774. zconfnerrs++;
  775. sym2 = NULL;
  776. }
  777. }
  778. return sym2;
  779. }
  780. struct property *prop_alloc(enum prop_type type, struct symbol *sym)
  781. {
  782. struct property *prop;
  783. struct property **propp;
  784. prop = malloc(sizeof(*prop));
  785. memset(prop, 0, sizeof(*prop));
  786. prop->type = type;
  787. prop->sym = sym;
  788. prop->file = current_file;
  789. prop->lineno = zconf_lineno();
  790. /* append property to the prop list of symbol */
  791. if (sym) {
  792. for (propp = &sym->prop; *propp; propp = &(*propp)->next)
  793. ;
  794. *propp = prop;
  795. }
  796. return prop;
  797. }
  798. struct symbol *prop_get_symbol(struct property *prop)
  799. {
  800. if (prop->expr && (prop->expr->type == E_SYMBOL ||
  801. prop->expr->type == E_LIST))
  802. return prop->expr->left.sym;
  803. return NULL;
  804. }
  805. const char *prop_get_type_name(enum prop_type type)
  806. {
  807. switch (type) {
  808. case P_PROMPT:
  809. return "prompt";
  810. case P_ENV:
  811. return "env";
  812. case P_COMMENT:
  813. return "comment";
  814. case P_MENU:
  815. return "menu";
  816. case P_DEFAULT:
  817. return "default";
  818. case P_CHOICE:
  819. return "choice";
  820. case P_SELECT:
  821. return "select";
  822. case P_RANGE:
  823. return "range";
  824. case P_UNKNOWN:
  825. break;
  826. }
  827. return "unknown";
  828. }
  829. void prop_add_env(const char *env)
  830. {
  831. struct symbol *sym, *sym2;
  832. struct property *prop;
  833. char *p;
  834. sym = current_entry->sym;
  835. sym->flags |= SYMBOL_AUTO;
  836. for_all_properties(sym, prop, P_ENV) {
  837. sym2 = prop_get_symbol(prop);
  838. if (strcmp(sym2->name, env))
  839. menu_warn(current_entry, "redefining environment symbol from %s",
  840. sym2->name);
  841. return;
  842. }
  843. prop = prop_alloc(P_ENV, sym);
  844. prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
  845. sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
  846. sym_env_list->right.sym = sym;
  847. p = getenv(env);
  848. if (p)
  849. sym_add_default(sym, p);
  850. else
  851. menu_warn(current_entry, "environment variable %s undefined", env);
  852. }