GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / scripts / kconfig / symbol.c
blobfe4627d4ef4ea7ad87f8b78b8abb0bd52421cb7a
1 /*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
6 #include <ctype.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <regex.h>
10 #include <sys/utsname.h>
12 #define LKC_DIRECT_LINK
13 #include "lkc.h"
15 struct symbol symbol_yes = {
16 .name = "y",
17 .curr = { "y", yes },
18 .flags = SYMBOL_CONST|SYMBOL_VALID,
19 }, symbol_mod = {
20 .name = "m",
21 .curr = { "m", mod },
22 .flags = SYMBOL_CONST|SYMBOL_VALID,
23 }, symbol_no = {
24 .name = "n",
25 .curr = { "n", no },
26 .flags = SYMBOL_CONST|SYMBOL_VALID,
27 }, symbol_empty = {
28 .name = "",
29 .curr = { "", no },
30 .flags = SYMBOL_VALID,
33 struct symbol *sym_defconfig_list;
34 struct symbol *modules_sym;
35 tristate modules_val;
37 struct expr *sym_env_list;
39 static void sym_add_default(struct symbol *sym, const char *def)
41 struct property *prop = prop_alloc(P_DEFAULT, sym);
43 prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
46 void sym_init(void)
48 struct symbol *sym;
49 struct utsname uts;
50 static bool inited = false;
52 if (inited)
53 return;
54 inited = true;
56 uname(&uts);
58 sym = sym_lookup("UNAME_RELEASE", 0);
59 sym->type = S_STRING;
60 sym->flags |= SYMBOL_AUTO;
61 sym_add_default(sym, uts.release);
64 enum symbol_type sym_get_type(struct symbol *sym)
66 enum symbol_type type = sym->type;
68 if (type == S_TRISTATE) {
69 if (sym_is_choice_value(sym) && sym->visible == yes)
70 type = S_BOOLEAN;
71 else if (modules_val == no)
72 type = S_BOOLEAN;
74 return type;
77 const char *sym_type_name(enum symbol_type type)
79 switch (type) {
80 case S_BOOLEAN:
81 return "boolean";
82 case S_TRISTATE:
83 return "tristate";
84 case S_INT:
85 return "integer";
86 case S_HEX:
87 return "hex";
88 case S_STRING:
89 return "string";
90 case S_UNKNOWN:
91 return "unknown";
92 case S_OTHER:
93 break;
95 return "???";
98 struct property *sym_get_choice_prop(struct symbol *sym)
100 struct property *prop;
102 for_all_choices(sym, prop)
103 return prop;
104 return NULL;
107 struct property *sym_get_env_prop(struct symbol *sym)
109 struct property *prop;
111 for_all_properties(sym, prop, P_ENV)
112 return prop;
113 return NULL;
116 struct property *sym_get_default_prop(struct symbol *sym)
118 struct property *prop;
120 for_all_defaults(sym, prop) {
121 prop->visible.tri = expr_calc_value(prop->visible.expr);
122 if (prop->visible.tri != no)
123 return prop;
125 return NULL;
128 static struct property *sym_get_range_prop(struct symbol *sym)
130 struct property *prop;
132 for_all_properties(sym, prop, P_RANGE) {
133 prop->visible.tri = expr_calc_value(prop->visible.expr);
134 if (prop->visible.tri != no)
135 return prop;
137 return NULL;
140 static int sym_get_range_val(struct symbol *sym, int base)
142 sym_calc_value(sym);
143 switch (sym->type) {
144 case S_INT:
145 base = 10;
146 break;
147 case S_HEX:
148 base = 16;
149 break;
150 default:
151 break;
153 return strtol(sym->curr.val, NULL, base);
156 static void sym_validate_range(struct symbol *sym)
158 struct property *prop;
159 int base, val, val2;
160 char str[64];
162 switch (sym->type) {
163 case S_INT:
164 base = 10;
165 break;
166 case S_HEX:
167 base = 16;
168 break;
169 default:
170 return;
172 prop = sym_get_range_prop(sym);
173 if (!prop)
174 return;
175 val = strtol(sym->curr.val, NULL, base);
176 val2 = sym_get_range_val(prop->expr->left.sym, base);
177 if (val >= val2) {
178 val2 = sym_get_range_val(prop->expr->right.sym, base);
179 if (val <= val2)
180 return;
182 if (sym->type == S_INT)
183 sprintf(str, "%d", val2);
184 else
185 sprintf(str, "0x%x", val2);
186 sym->curr.val = strdup(str);
189 static void sym_calc_visibility(struct symbol *sym)
191 struct property *prop;
192 tristate tri;
194 /* any prompt visible? */
195 tri = no;
196 for_all_prompts(sym, prop) {
197 prop->visible.tri = expr_calc_value(prop->visible.expr);
198 tri = EXPR_OR(tri, prop->visible.tri);
200 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
201 tri = yes;
202 if (sym->visible != tri) {
203 sym->visible = tri;
204 sym_set_changed(sym);
206 if (sym_is_choice_value(sym))
207 return;
208 /* defaulting to "yes" if no explicit "depends on" are given */
209 tri = yes;
210 if (sym->dir_dep.expr)
211 tri = expr_calc_value(sym->dir_dep.expr);
212 if (tri == mod)
213 tri = yes;
214 if (sym->dir_dep.tri != tri) {
215 sym->dir_dep.tri = tri;
216 sym_set_changed(sym);
218 tri = no;
219 if (sym->rev_dep.expr)
220 tri = expr_calc_value(sym->rev_dep.expr);
221 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
222 tri = yes;
223 if (sym->rev_dep.tri != tri) {
224 sym->rev_dep.tri = tri;
225 sym_set_changed(sym);
230 * Find the default symbol for a choice.
231 * First try the default values for the choice symbol
232 * Next locate the first visible choice value
233 * Return NULL if none was found
235 struct symbol *sym_choice_default(struct symbol *sym)
237 struct symbol *def_sym;
238 struct property *prop;
239 struct expr *e;
241 /* any of the defaults visible? */
242 for_all_defaults(sym, prop) {
243 prop->visible.tri = expr_calc_value(prop->visible.expr);
244 if (prop->visible.tri == no)
245 continue;
246 def_sym = prop_get_symbol(prop);
247 if (def_sym->visible != no)
248 return def_sym;
251 /* just get the first visible value */
252 prop = sym_get_choice_prop(sym);
253 expr_list_for_each_sym(prop->expr, e, def_sym)
254 if (def_sym->visible != no)
255 return def_sym;
257 /* failed to locate any defaults */
258 return NULL;
261 static struct symbol *sym_calc_choice(struct symbol *sym)
263 struct symbol *def_sym;
264 struct property *prop;
265 struct expr *e;
267 /* first calculate all choice values' visibilities */
268 prop = sym_get_choice_prop(sym);
269 expr_list_for_each_sym(prop->expr, e, def_sym)
270 sym_calc_visibility(def_sym);
272 /* is the user choice visible? */
273 def_sym = sym->def[S_DEF_USER].val;
274 if (def_sym && def_sym->visible != no)
275 return def_sym;
277 def_sym = sym_choice_default(sym);
279 if (def_sym == NULL)
280 /* no choice? reset tristate value */
281 sym->curr.tri = no;
283 return def_sym;
286 void sym_calc_value(struct symbol *sym)
288 struct symbol_value newval, oldval;
289 struct property *prop;
290 struct expr *e;
292 if (!sym)
293 return;
295 if (sym->flags & SYMBOL_VALID)
296 return;
297 sym->flags |= SYMBOL_VALID;
299 oldval = sym->curr;
301 switch (sym->type) {
302 case S_INT:
303 case S_HEX:
304 case S_STRING:
305 newval = symbol_empty.curr;
306 break;
307 case S_BOOLEAN:
308 case S_TRISTATE:
309 newval = symbol_no.curr;
310 break;
311 default:
312 sym->curr.val = sym->name;
313 sym->curr.tri = no;
314 return;
316 if (!sym_is_choice_value(sym))
317 sym->flags &= ~SYMBOL_WRITE;
319 sym_calc_visibility(sym);
321 /* set default if recursively called */
322 sym->curr = newval;
324 switch (sym_get_type(sym)) {
325 case S_BOOLEAN:
326 case S_TRISTATE:
327 if (sym_is_choice_value(sym) && sym->visible == yes) {
328 prop = sym_get_choice_prop(sym);
329 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
330 } else {
331 if (sym->visible != no) {
332 /* if the symbol is visible use the user value
333 * if available, otherwise try the default value
335 sym->flags |= SYMBOL_WRITE;
336 if (sym_has_value(sym)) {
337 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
338 sym->visible);
339 goto calc_newval;
342 if (sym->rev_dep.tri != no)
343 sym->flags |= SYMBOL_WRITE;
344 if (!sym_is_choice(sym)) {
345 prop = sym_get_default_prop(sym);
346 if (prop) {
347 sym->flags |= SYMBOL_WRITE;
348 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
349 prop->visible.tri);
352 calc_newval:
353 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
355 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
356 newval.tri = yes;
357 break;
358 case S_STRING:
359 case S_HEX:
360 case S_INT:
361 if (sym->visible != no) {
362 sym->flags |= SYMBOL_WRITE;
363 if (sym_has_value(sym)) {
364 newval.val = sym->def[S_DEF_USER].val;
365 break;
368 prop = sym_get_default_prop(sym);
369 if (prop) {
370 struct symbol *ds = prop_get_symbol(prop);
371 if (ds) {
372 sym->flags |= SYMBOL_WRITE;
373 sym_calc_value(ds);
374 newval.val = ds->curr.val;
377 break;
378 default:
382 sym->curr = newval;
383 if (sym_is_choice(sym) && newval.tri == yes)
384 sym->curr.val = sym_calc_choice(sym);
385 sym_validate_range(sym);
387 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
388 sym_set_changed(sym);
389 if (modules_sym == sym) {
390 sym_set_all_changed();
391 modules_val = modules_sym->curr.tri;
395 if (sym_is_choice(sym)) {
396 struct symbol *choice_sym;
398 prop = sym_get_choice_prop(sym);
399 expr_list_for_each_sym(prop->expr, e, choice_sym) {
400 if ((sym->flags & SYMBOL_WRITE) &&
401 choice_sym->visible != no)
402 choice_sym->flags |= SYMBOL_WRITE;
403 if (sym->flags & SYMBOL_CHANGED)
404 sym_set_changed(choice_sym);
408 if (sym->flags & SYMBOL_AUTO)
409 sym->flags &= ~SYMBOL_WRITE;
412 void sym_clear_all_valid(void)
414 struct symbol *sym;
415 int i;
417 for_all_symbols(i, sym)
418 sym->flags &= ~SYMBOL_VALID;
419 sym_add_change_count(1);
420 if (modules_sym)
421 sym_calc_value(modules_sym);
424 void sym_set_changed(struct symbol *sym)
426 struct property *prop;
428 sym->flags |= SYMBOL_CHANGED;
429 for (prop = sym->prop; prop; prop = prop->next) {
430 if (prop->menu)
431 prop->menu->flags |= MENU_CHANGED;
435 void sym_set_all_changed(void)
437 struct symbol *sym;
438 int i;
440 for_all_symbols(i, sym)
441 sym_set_changed(sym);
444 bool sym_tristate_within_range(struct symbol *sym, tristate val)
446 int type = sym_get_type(sym);
448 if (sym->visible == no)
449 return false;
451 if (type != S_BOOLEAN && type != S_TRISTATE)
452 return false;
454 if (type == S_BOOLEAN && val == mod)
455 return false;
456 if (sym->visible <= sym->rev_dep.tri)
457 return false;
458 if (sym_is_choice_value(sym) && sym->visible == yes)
459 return val == yes;
460 return val >= sym->rev_dep.tri && val <= sym->visible;
463 bool sym_set_tristate_value(struct symbol *sym, tristate val)
465 tristate oldval = sym_get_tristate_value(sym);
467 if (oldval != val && !sym_tristate_within_range(sym, val))
468 return false;
470 if (!(sym->flags & SYMBOL_DEF_USER)) {
471 sym->flags |= SYMBOL_DEF_USER;
472 sym_set_changed(sym);
475 * setting a choice value also resets the new flag of the choice
476 * symbol and all other choice values.
478 if (sym_is_choice_value(sym) && val == yes) {
479 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
480 struct property *prop;
481 struct expr *e;
483 cs->def[S_DEF_USER].val = sym;
484 cs->flags |= SYMBOL_DEF_USER;
485 prop = sym_get_choice_prop(cs);
486 for (e = prop->expr; e; e = e->left.expr) {
487 if (e->right.sym->visible != no)
488 e->right.sym->flags |= SYMBOL_DEF_USER;
492 sym->def[S_DEF_USER].tri = val;
493 if (oldval != val)
494 sym_clear_all_valid();
496 return true;
499 tristate sym_toggle_tristate_value(struct symbol *sym)
501 tristate oldval, newval;
503 oldval = newval = sym_get_tristate_value(sym);
504 do {
505 switch (newval) {
506 case no:
507 newval = mod;
508 break;
509 case mod:
510 newval = yes;
511 break;
512 case yes:
513 newval = no;
514 break;
516 if (sym_set_tristate_value(sym, newval))
517 break;
518 } while (oldval != newval);
519 return newval;
522 bool sym_string_valid(struct symbol *sym, const char *str)
524 signed char ch;
526 switch (sym->type) {
527 case S_STRING:
528 return true;
529 case S_INT:
530 ch = *str++;
531 if (ch == '-')
532 ch = *str++;
533 if (!isdigit(ch))
534 return false;
535 if (ch == '0' && *str != 0)
536 return false;
537 while ((ch = *str++)) {
538 if (!isdigit(ch))
539 return false;
541 return true;
542 case S_HEX:
543 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
544 str += 2;
545 ch = *str++;
546 do {
547 if (!isxdigit(ch))
548 return false;
549 } while ((ch = *str++));
550 return true;
551 case S_BOOLEAN:
552 case S_TRISTATE:
553 switch (str[0]) {
554 case 'y': case 'Y':
555 case 'm': case 'M':
556 case 'n': case 'N':
557 return true;
559 return false;
560 default:
561 return false;
565 bool sym_string_within_range(struct symbol *sym, const char *str)
567 struct property *prop;
568 int val;
570 switch (sym->type) {
571 case S_STRING:
572 return sym_string_valid(sym, str);
573 case S_INT:
574 if (!sym_string_valid(sym, str))
575 return false;
576 prop = sym_get_range_prop(sym);
577 if (!prop)
578 return true;
579 val = strtol(str, NULL, 10);
580 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
581 val <= sym_get_range_val(prop->expr->right.sym, 10);
582 case S_HEX:
583 if (!sym_string_valid(sym, str))
584 return false;
585 prop = sym_get_range_prop(sym);
586 if (!prop)
587 return true;
588 val = strtol(str, NULL, 16);
589 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
590 val <= sym_get_range_val(prop->expr->right.sym, 16);
591 case S_BOOLEAN:
592 case S_TRISTATE:
593 switch (str[0]) {
594 case 'y': case 'Y':
595 return sym_tristate_within_range(sym, yes);
596 case 'm': case 'M':
597 return sym_tristate_within_range(sym, mod);
598 case 'n': case 'N':
599 return sym_tristate_within_range(sym, no);
601 return false;
602 default:
603 return false;
607 bool sym_set_string_value(struct symbol *sym, const char *newval)
609 const char *oldval;
610 char *val;
611 int size;
613 switch (sym->type) {
614 case S_BOOLEAN:
615 case S_TRISTATE:
616 switch (newval[0]) {
617 case 'y': case 'Y':
618 return sym_set_tristate_value(sym, yes);
619 case 'm': case 'M':
620 return sym_set_tristate_value(sym, mod);
621 case 'n': case 'N':
622 return sym_set_tristate_value(sym, no);
624 return false;
625 default:
629 if (!sym_string_within_range(sym, newval))
630 return false;
632 if (!(sym->flags & SYMBOL_DEF_USER)) {
633 sym->flags |= SYMBOL_DEF_USER;
634 sym_set_changed(sym);
637 oldval = sym->def[S_DEF_USER].val;
638 size = strlen(newval) + 1;
639 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
640 size += 2;
641 sym->def[S_DEF_USER].val = val = malloc(size);
642 *val++ = '0';
643 *val++ = 'x';
644 } else if (!oldval || strcmp(oldval, newval))
645 sym->def[S_DEF_USER].val = val = malloc(size);
646 else
647 return true;
649 strcpy(val, newval);
650 free((void *)oldval);
651 sym_clear_all_valid();
653 return true;
657 * Find the default value associated to a symbol.
658 * For tristate symbol handle the modules=n case
659 * in which case "m" becomes "y".
660 * If the symbol does not have any default then fallback
661 * to the fixed default values.
663 const char *sym_get_string_default(struct symbol *sym)
665 struct property *prop;
666 struct symbol *ds;
667 const char *str;
668 tristate val;
670 sym_calc_visibility(sym);
671 sym_calc_value(modules_sym);
672 val = symbol_no.curr.tri;
673 str = symbol_empty.curr.val;
675 /* If symbol has a default value look it up */
676 prop = sym_get_default_prop(sym);
677 if (prop != NULL) {
678 switch (sym->type) {
679 case S_BOOLEAN:
680 case S_TRISTATE:
681 /* The visibility imay limit the value from yes => mod */
682 val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
683 break;
684 default:
686 * The following fails to handle the situation
687 * where a default value is further limited by
688 * the valid range.
690 ds = prop_get_symbol(prop);
691 if (ds != NULL) {
692 sym_calc_value(ds);
693 str = (const char *)ds->curr.val;
698 /* Handle select statements */
699 val = EXPR_OR(val, sym->rev_dep.tri);
701 /* transpose mod to yes if modules are not enabled */
702 if (val == mod)
703 if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
704 val = yes;
706 /* transpose mod to yes if type is bool */
707 if (sym->type == S_BOOLEAN && val == mod)
708 val = yes;
710 switch (sym->type) {
711 case S_BOOLEAN:
712 case S_TRISTATE:
713 switch (val) {
714 case no: return "n";
715 case mod: return "m";
716 case yes: return "y";
718 case S_INT:
719 case S_HEX:
720 return str;
721 case S_STRING:
722 return str;
723 case S_OTHER:
724 case S_UNKNOWN:
725 break;
727 return "";
730 const char *sym_get_string_value(struct symbol *sym)
732 tristate val;
734 switch (sym->type) {
735 case S_BOOLEAN:
736 case S_TRISTATE:
737 val = sym_get_tristate_value(sym);
738 switch (val) {
739 case no:
740 return "n";
741 case mod:
742 return "m";
743 case yes:
744 return "y";
746 break;
747 default:
750 return (const char *)sym->curr.val;
753 bool sym_is_changable(struct symbol *sym)
755 return sym->visible > sym->rev_dep.tri;
758 static unsigned strhash(const char *s)
760 /* fnv32 hash */
761 unsigned hash = 2166136261U;
762 for (; *s; s++)
763 hash = (hash ^ *s) * 0x01000193;
764 return hash;
767 struct symbol *sym_lookup(const char *name, int flags)
769 struct symbol *symbol;
770 char *new_name;
771 int hash;
773 if (name) {
774 if (name[0] && !name[1]) {
775 switch (name[0]) {
776 case 'y': return &symbol_yes;
777 case 'm': return &symbol_mod;
778 case 'n': return &symbol_no;
781 hash = strhash(name) % SYMBOL_HASHSIZE;
783 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
784 if (symbol->name &&
785 !strcmp(symbol->name, name) &&
786 (flags ? symbol->flags & flags
787 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
788 return symbol;
790 new_name = strdup(name);
791 } else {
792 new_name = NULL;
793 hash = 0;
796 symbol = malloc(sizeof(*symbol));
797 memset(symbol, 0, sizeof(*symbol));
798 symbol->name = new_name;
799 symbol->type = S_UNKNOWN;
800 symbol->flags |= flags;
802 symbol->next = symbol_hash[hash];
803 symbol_hash[hash] = symbol;
805 return symbol;
808 struct symbol *sym_find(const char *name)
810 struct symbol *symbol = NULL;
811 int hash = 0;
813 if (!name)
814 return NULL;
816 if (name[0] && !name[1]) {
817 switch (name[0]) {
818 case 'y': return &symbol_yes;
819 case 'm': return &symbol_mod;
820 case 'n': return &symbol_no;
823 hash = strhash(name) % SYMBOL_HASHSIZE;
825 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
826 if (symbol->name &&
827 !strcmp(symbol->name, name) &&
828 !(symbol->flags & SYMBOL_CONST))
829 break;
832 return symbol;
835 struct symbol **sym_re_search(const char *pattern)
837 struct symbol *sym, **sym_arr = NULL;
838 int i, cnt, size;
839 regex_t re;
841 cnt = size = 0;
842 /* Skip if empty */
843 if (strlen(pattern) == 0)
844 return NULL;
845 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
846 return NULL;
848 for_all_symbols(i, sym) {
849 if (sym->flags & SYMBOL_CONST || !sym->name)
850 continue;
851 if (regexec(&re, sym->name, 0, NULL, 0))
852 continue;
853 if (cnt + 1 >= size) {
854 void *tmp = sym_arr;
855 size += 16;
856 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
857 if (!sym_arr) {
858 free(tmp);
859 return NULL;
862 sym_calc_value(sym);
863 sym_arr[cnt++] = sym;
865 if (sym_arr)
866 sym_arr[cnt] = NULL;
867 regfree(&re);
869 return sym_arr;
873 * When we check for recursive dependencies we use a stack to save
874 * current state so we can print out relevant info to user.
875 * The entries are located on the call stack so no need to free memory.
876 * Note inser() remove() must always match to properly clear the stack.
878 static struct dep_stack {
879 struct dep_stack *prev, *next;
880 struct symbol *sym;
881 struct property *prop;
882 struct expr *expr;
883 } *check_top;
885 static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
887 memset(stack, 0, sizeof(*stack));
888 if (check_top)
889 check_top->next = stack;
890 stack->prev = check_top;
891 stack->sym = sym;
892 check_top = stack;
895 static void dep_stack_remove(void)
897 check_top = check_top->prev;
898 if (check_top)
899 check_top->next = NULL;
903 * Called when we have detected a recursive dependency.
904 * check_top point to the top of the stact so we use
905 * the ->prev pointer to locate the bottom of the stack.
907 static void sym_check_print_recursive(struct symbol *last_sym)
909 struct dep_stack *stack;
910 struct symbol *sym, *next_sym;
911 struct menu *menu = NULL;
912 struct property *prop;
913 struct dep_stack cv_stack;
915 if (sym_is_choice_value(last_sym)) {
916 dep_stack_insert(&cv_stack, last_sym);
917 last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
920 for (stack = check_top; stack != NULL; stack = stack->prev)
921 if (stack->sym == last_sym)
922 break;
923 if (!stack) {
924 fprintf(stderr, "unexpected recursive dependency error\n");
925 return;
928 for (; stack; stack = stack->next) {
929 sym = stack->sym;
930 next_sym = stack->next ? stack->next->sym : last_sym;
931 prop = stack->prop;
932 if (prop == NULL)
933 prop = stack->sym->prop;
935 /* for choice values find the menu entry (used below) */
936 if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
937 for (prop = sym->prop; prop; prop = prop->next) {
938 menu = prop->menu;
939 if (prop->menu)
940 break;
943 if (stack->sym == last_sym)
944 fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
945 prop->file->name, prop->lineno);
946 if (stack->expr) {
947 fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
948 prop->file->name, prop->lineno,
949 sym->name ? sym->name : "<choice>",
950 prop_get_type_name(prop->type),
951 next_sym->name ? next_sym->name : "<choice>");
952 } else if (stack->prop) {
953 fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
954 prop->file->name, prop->lineno,
955 sym->name ? sym->name : "<choice>",
956 next_sym->name ? next_sym->name : "<choice>");
957 } else if (sym_is_choice(sym)) {
958 fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
959 menu->file->name, menu->lineno,
960 sym->name ? sym->name : "<choice>",
961 next_sym->name ? next_sym->name : "<choice>");
962 } else if (sym_is_choice_value(sym)) {
963 fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
964 menu->file->name, menu->lineno,
965 sym->name ? sym->name : "<choice>",
966 next_sym->name ? next_sym->name : "<choice>");
967 } else {
968 fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
969 prop->file->name, prop->lineno,
970 sym->name ? sym->name : "<choice>",
971 next_sym->name ? next_sym->name : "<choice>");
975 if (check_top == &cv_stack)
976 dep_stack_remove();
979 static struct symbol *sym_check_expr_deps(struct expr *e)
981 struct symbol *sym;
983 if (!e)
984 return NULL;
985 switch (e->type) {
986 case E_OR:
987 case E_AND:
988 sym = sym_check_expr_deps(e->left.expr);
989 if (sym)
990 return sym;
991 return sym_check_expr_deps(e->right.expr);
992 case E_NOT:
993 return sym_check_expr_deps(e->left.expr);
994 case E_EQUAL:
995 case E_UNEQUAL:
996 sym = sym_check_deps(e->left.sym);
997 if (sym)
998 return sym;
999 return sym_check_deps(e->right.sym);
1000 case E_SYMBOL:
1001 return sym_check_deps(e->left.sym);
1002 default:
1003 break;
1005 printf("Oops! How to check %d?\n", e->type);
1006 return NULL;
1009 /* return NULL when dependencies are OK */
1010 static struct symbol *sym_check_sym_deps(struct symbol *sym)
1012 struct symbol *sym2;
1013 struct property *prop;
1014 struct dep_stack stack;
1016 dep_stack_insert(&stack, sym);
1018 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1019 if (sym2)
1020 goto out;
1022 for (prop = sym->prop; prop; prop = prop->next) {
1023 if (prop->type == P_CHOICE || prop->type == P_SELECT)
1024 continue;
1025 stack.prop = prop;
1026 sym2 = sym_check_expr_deps(prop->visible.expr);
1027 if (sym2)
1028 break;
1029 if (prop->type != P_DEFAULT || sym_is_choice(sym))
1030 continue;
1031 stack.expr = prop->expr;
1032 sym2 = sym_check_expr_deps(prop->expr);
1033 if (sym2)
1034 break;
1035 stack.expr = NULL;
1038 out:
1039 dep_stack_remove();
1041 return sym2;
1044 static struct symbol *sym_check_choice_deps(struct symbol *choice)
1046 struct symbol *sym, *sym2;
1047 struct property *prop;
1048 struct expr *e;
1049 struct dep_stack stack;
1051 dep_stack_insert(&stack, choice);
1053 prop = sym_get_choice_prop(choice);
1054 expr_list_for_each_sym(prop->expr, e, sym)
1055 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1057 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1058 sym2 = sym_check_sym_deps(choice);
1059 choice->flags &= ~SYMBOL_CHECK;
1060 if (sym2)
1061 goto out;
1063 expr_list_for_each_sym(prop->expr, e, sym) {
1064 sym2 = sym_check_sym_deps(sym);
1065 if (sym2)
1066 break;
1068 out:
1069 expr_list_for_each_sym(prop->expr, e, sym)
1070 sym->flags &= ~SYMBOL_CHECK;
1072 if (sym2 && sym_is_choice_value(sym2) &&
1073 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1074 sym2 = choice;
1076 dep_stack_remove();
1078 return sym2;
1081 struct symbol *sym_check_deps(struct symbol *sym)
1083 struct symbol *sym2;
1084 struct property *prop;
1086 if (sym->flags & SYMBOL_CHECK) {
1087 sym_check_print_recursive(sym);
1088 return sym;
1090 if (sym->flags & SYMBOL_CHECKED)
1091 return NULL;
1093 if (sym_is_choice_value(sym)) {
1094 struct dep_stack stack;
1096 /* for choice groups start the check with main choice symbol */
1097 dep_stack_insert(&stack, sym);
1098 prop = sym_get_choice_prop(sym);
1099 sym2 = sym_check_deps(prop_get_symbol(prop));
1100 dep_stack_remove();
1101 } else if (sym_is_choice(sym)) {
1102 sym2 = sym_check_choice_deps(sym);
1103 } else {
1104 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1105 sym2 = sym_check_sym_deps(sym);
1106 sym->flags &= ~SYMBOL_CHECK;
1109 if (sym2 && sym2 == sym)
1110 sym2 = NULL;
1112 return sym2;
1115 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
1117 struct property *prop;
1118 struct property **propp;
1120 prop = malloc(sizeof(*prop));
1121 memset(prop, 0, sizeof(*prop));
1122 prop->type = type;
1123 prop->sym = sym;
1124 prop->file = current_file;
1125 prop->lineno = zconf_lineno();
1127 /* append property to the prop list of symbol */
1128 if (sym) {
1129 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
1131 *propp = prop;
1134 return prop;
1137 struct symbol *prop_get_symbol(struct property *prop)
1139 if (prop->expr && (prop->expr->type == E_SYMBOL ||
1140 prop->expr->type == E_LIST))
1141 return prop->expr->left.sym;
1142 return NULL;
1145 const char *prop_get_type_name(enum prop_type type)
1147 switch (type) {
1148 case P_PROMPT:
1149 return "prompt";
1150 case P_ENV:
1151 return "env";
1152 case P_COMMENT:
1153 return "comment";
1154 case P_MENU:
1155 return "menu";
1156 case P_DEFAULT:
1157 return "default";
1158 case P_CHOICE:
1159 return "choice";
1160 case P_SELECT:
1161 return "select";
1162 case P_RANGE:
1163 return "range";
1164 case P_SYMBOL:
1165 return "symbol";
1166 case P_UNKNOWN:
1167 break;
1169 return "unknown";
1172 static void prop_add_env(const char *env)
1174 struct symbol *sym, *sym2;
1175 struct property *prop;
1176 char *p;
1178 sym = current_entry->sym;
1179 sym->flags |= SYMBOL_AUTO;
1180 for_all_properties(sym, prop, P_ENV) {
1181 sym2 = prop_get_symbol(prop);
1182 if (strcmp(sym2->name, env))
1183 menu_warn(current_entry, "redefining environment symbol from %s",
1184 sym2->name);
1185 return;
1188 prop = prop_alloc(P_ENV, sym);
1189 prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
1191 sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
1192 sym_env_list->right.sym = sym;
1194 p = getenv(env);
1195 if (p)
1196 sym_add_default(sym, p);
1197 else
1198 menu_warn(current_entry, "environment variable %s undefined", env);