2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
10 #include <sys/utsname.h>
14 struct symbol symbol_yes
= {
17 .flags
= SYMBOL_CONST
|SYMBOL_VALID
,
21 .flags
= SYMBOL_CONST
|SYMBOL_VALID
,
25 .flags
= SYMBOL_CONST
|SYMBOL_VALID
,
29 .flags
= SYMBOL_VALID
,
32 struct symbol
*sym_defconfig_list
;
33 struct symbol
*modules_sym
;
36 struct expr
*sym_env_list
;
38 static void sym_add_default(struct symbol
*sym
, const char *def
)
40 struct property
*prop
= prop_alloc(P_DEFAULT
, sym
);
42 prop
->expr
= expr_alloc_symbol(sym_lookup(def
, SYMBOL_CONST
));
49 static bool inited
= false;
57 sym
= sym_lookup("UNAME_RELEASE", 0);
59 sym
->flags
|= SYMBOL_AUTO
;
60 sym_add_default(sym
, uts
.release
);
63 enum symbol_type
sym_get_type(struct symbol
*sym
)
65 enum symbol_type type
= sym
->type
;
67 if (type
== S_TRISTATE
) {
68 if (sym_is_choice_value(sym
) && sym
->visible
== yes
)
70 else if (modules_val
== no
)
76 const char *sym_type_name(enum symbol_type type
)
97 struct property
*sym_get_choice_prop(struct symbol
*sym
)
99 struct property
*prop
;
101 for_all_choices(sym
, prop
)
106 struct property
*sym_get_env_prop(struct symbol
*sym
)
108 struct property
*prop
;
110 for_all_properties(sym
, prop
, P_ENV
)
115 struct property
*sym_get_default_prop(struct symbol
*sym
)
117 struct property
*prop
;
119 for_all_defaults(sym
, prop
) {
120 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
121 if (prop
->visible
.tri
!= no
)
127 static struct property
*sym_get_range_prop(struct symbol
*sym
)
129 struct property
*prop
;
131 for_all_properties(sym
, prop
, P_RANGE
) {
132 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
133 if (prop
->visible
.tri
!= no
)
139 static long long sym_get_range_val(struct symbol
*sym
, int base
)
152 return strtoll(sym
->curr
.val
, NULL
, base
);
155 static void sym_validate_range(struct symbol
*sym
)
157 struct property
*prop
;
172 prop
= sym_get_range_prop(sym
);
175 val
= strtoll(sym
->curr
.val
, NULL
, base
);
176 val2
= sym_get_range_val(prop
->expr
->left
.sym
, base
);
178 val2
= sym_get_range_val(prop
->expr
->right
.sym
, base
);
182 if (sym
->type
== S_INT
)
183 sprintf(str
, "%lld", val2
);
185 sprintf(str
, "0x%llx", val2
);
186 sym
->curr
.val
= strdup(str
);
189 static void sym_calc_visibility(struct symbol
*sym
)
191 struct property
*prop
;
193 struct expr_select_value
*esv
;
195 /* any prompt visible? */
197 for_all_prompts(sym
, prop
) {
198 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
199 tri
= EXPR_OR(tri
, prop
->visible
.tri
);
201 if (tri
== mod
&& (sym
->type
!= S_TRISTATE
|| modules_val
== no
))
203 if (sym
->visible
!= tri
) {
205 sym_set_changed(sym
);
207 /* defaulting to "yes" if no explicit "depends on" are given */
209 if (sym
->dir_dep
.expr
)
210 tri
= expr_calc_value(sym
->dir_dep
.expr
);
213 if (sym
->dir_dep
.tri
!= tri
) {
214 sym
->dir_dep
.tri
= tri
;
215 sym_set_changed(sym
);
218 if (sym
->rev_dep
.expr
)
219 tri
= expr_calc_value(sym
->rev_dep
.expr
);
220 if (tri
== mod
&& sym_get_type(sym
) == S_BOOLEAN
)
222 if (sym
->rev_dep
.tri
!= tri
) {
223 sym
->rev_dep
.tri
= tri
;
224 sym_set_changed(sym
);
226 for (esv
= sym
->val_dep
; esv
; esv
= esv
->next
) {
227 tri
= expr_calc_value(esv
->expr
);
228 if (tri
== mod
&& sym_get_type(sym
) == S_BOOLEAN
)
230 if (esv
->tri
!= tri
) {
232 sym_set_changed(sym
);
238 * Find the default symbol for a choice.
239 * First try the default values for the choice symbol
240 * Next locate the first visible choice value
241 * Return NULL if none was found
243 struct symbol
*sym_choice_default(struct symbol
*sym
)
245 struct symbol
*def_sym
;
246 struct symbol
*ret
= NULL
;
247 struct property
*prop
;
250 /* check to see if any are selected */
251 prop
= sym_get_choice_prop(sym
);
252 expr_list_for_each_sym(prop
->expr
, e
, def_sym
)
253 if (def_sym
->rev_dep
.tri
!= no
) {
255 fprintf(stderr
, "warning: conflicting selects "
256 "on choice block: %s\n", sym
->name
);
263 /* any of the defaults visible? */
264 for_all_defaults(sym
, prop
) {
265 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
266 if (prop
->visible
.tri
== no
)
268 def_sym
= prop_get_symbol(prop
);
269 if (def_sym
->visible
!= no
)
273 /* just get the first visible value */
274 prop
= sym_get_choice_prop(sym
);
275 expr_list_for_each_sym(prop
->expr
, e
, def_sym
)
276 if (def_sym
->visible
!= no
)
279 /* failed to locate any defaults */
283 static struct symbol
*sym_calc_choice(struct symbol
*sym
)
285 struct symbol
*def_sym
;
286 struct property
*prop
;
290 /* first calculate all choice values' visibilities */
292 prop
= sym_get_choice_prop(sym
);
293 expr_list_for_each_sym(prop
->expr
, e
, def_sym
) {
294 sym_calc_visibility(def_sym
);
295 if (def_sym
->visible
!= no
)
296 flags
&= def_sym
->flags
;
299 sym
->flags
&= flags
| ~SYMBOL_DEF_USER
;
301 /* is the user choice visible? */
302 def_sym
= sym
->def
[S_DEF_USER
].val
;
303 if (def_sym
&& def_sym
->visible
!= no
)
306 def_sym
= sym_choice_default(sym
);
309 /* no choice? reset tristate value */
315 void sym_calc_value(struct symbol
*sym
)
317 struct symbol_value newval
, oldval
;
318 struct property
*prop
;
320 struct expr_select_value
*esv
;
326 if (sym
->flags
& SYMBOL_VALID
)
329 if (sym_is_choice_value(sym
) &&
330 sym
->flags
& SYMBOL_NEED_SET_CHOICE_VALUES
) {
331 sym
->flags
&= ~SYMBOL_NEED_SET_CHOICE_VALUES
;
332 prop
= sym_get_choice_prop(sym
);
333 sym_calc_value(prop_get_symbol(prop
));
336 sym
->flags
|= SYMBOL_VALID
;
344 newval
= symbol_empty
.curr
;
348 newval
= symbol_no
.curr
;
351 sym
->curr
.val
= sym
->name
;
355 if (!sym_is_choice_value(sym
))
356 sym
->flags
&= ~SYMBOL_WRITE
;
358 sym_calc_visibility(sym
);
360 /* set default if recursively called */
363 switch (sym_get_type(sym
)) {
366 if (sym_is_choice_value(sym
) && sym
->visible
== yes
) {
367 prop
= sym_get_choice_prop(sym
);
368 newval
.tri
= (prop_get_symbol(prop
)->curr
.val
== sym
) ? yes
: no
;
370 if (sym
->visible
!= no
) {
371 /* if the symbol is visible use the user value
372 * if available, otherwise try the default value
374 sym
->flags
|= SYMBOL_WRITE
;
375 if (sym_has_value(sym
)) {
376 newval
.tri
= EXPR_AND(sym
->def
[S_DEF_USER
].tri
,
381 if (sym
->rev_dep
.tri
!= no
)
382 sym
->flags
|= SYMBOL_WRITE
;
383 for (esv
= sym
->val_dep
; esv
; esv
= esv
->next
)
385 sym
->flags
|= SYMBOL_WRITE
;
386 if (!sym_is_choice(sym
)) {
387 prop
= sym_get_default_prop(sym
);
389 sym
->flags
|= SYMBOL_WRITE
;
390 newval
.tri
= EXPR_AND(expr_calc_value(prop
->expr
),
395 if (sym
->dir_dep
.tri
== no
) {
397 if (sym->rev_dep.tri != no) {
398 fprintf(stderr, "warning: (");
399 expr_fprint(sym->rev_dep.expr, stderr);
400 fprintf(stderr, ") selects %s which has unmet direct dependencies (",
402 expr_fprint(sym->dir_dep.expr, stderr);
403 fprintf(stderr, ")\n");
406 for (esv
= sym
->val_dep
; esv
; esv
= esv
->next
) {
407 if ((esv
->tri
!= no
) &&
408 (expr_calc_value(esv
->value
) != no
)) {
409 fprintf(stderr
, "warning: (");
410 expr_fprint(esv
->expr
, stderr
);
411 fprintf(stderr
, ") selects %s (with value ",
413 expr_fprint(esv
->value
, stderr
);
414 fprintf(stderr
, ") which has unmet direct dependencies (");
415 expr_fprint(sym
->dir_dep
.expr
, stderr
);
416 fprintf(stderr
, ")\n");
420 newval
.tri
= EXPR_OR(newval
.tri
, sym
->rev_dep
.tri
);
421 for (esv
= sym
->val_dep
; esv
; esv
= esv
->next
)
423 newval
.tri
= EXPR_OR(newval
.tri
,
424 expr_calc_value(esv
->value
));
426 if (newval
.tri
== mod
&& sym_get_type(sym
) == S_BOOLEAN
)
432 if (sym
->visible
!= no
) {
433 sym
->flags
|= SYMBOL_WRITE
;
434 if (sym_has_value(sym
)) {
435 newval
.val
= sym
->def
[S_DEF_USER
].val
;
440 for (esv
= sym
->val_dep
; esv
; esv
= esv
->next
) {
441 if (esv
->tri
!= no
) {
442 struct symbol
*ss
= esv
->value
->left
.sym
;
445 /* warn of more than one value selected */
447 sym
->flags
|= SYMBOL_WRITE
;
449 newval
.val
= ss
->curr
.val
;
456 prop
= sym_get_default_prop(sym
);
458 struct symbol
*ds
= prop_get_symbol(prop
);
460 sym
->flags
|= SYMBOL_WRITE
;
462 newval
.val
= ds
->curr
.val
;
471 if (sym_is_choice(sym
) && newval
.tri
== yes
)
472 sym
->curr
.val
= sym_calc_choice(sym
);
473 sym_validate_range(sym
);
475 if (memcmp(&oldval
, &sym
->curr
, sizeof(oldval
))) {
476 sym_set_changed(sym
);
477 if (modules_sym
== sym
) {
478 sym_set_all_changed();
479 modules_val
= modules_sym
->curr
.tri
;
483 if (sym_is_choice(sym
)) {
484 struct symbol
*choice_sym
;
486 prop
= sym_get_choice_prop(sym
);
487 expr_list_for_each_sym(prop
->expr
, e
, choice_sym
) {
488 if ((sym
->flags
& SYMBOL_WRITE
) &&
489 choice_sym
->visible
!= no
)
490 choice_sym
->flags
|= SYMBOL_WRITE
;
491 if (sym
->flags
& SYMBOL_CHANGED
)
492 sym_set_changed(choice_sym
);
496 if (sym
->flags
& SYMBOL_AUTO
)
497 sym
->flags
&= ~SYMBOL_WRITE
;
499 if (sym
->flags
& SYMBOL_NEED_SET_CHOICE_VALUES
)
500 set_all_choice_values(sym
);
503 void sym_clear_all_valid(void)
508 for_all_symbols(i
, sym
)
509 sym
->flags
&= ~SYMBOL_VALID
;
510 sym_add_change_count(1);
512 sym_calc_value(modules_sym
);
515 void sym_set_changed(struct symbol
*sym
)
517 struct property
*prop
;
519 sym
->flags
|= SYMBOL_CHANGED
;
520 for (prop
= sym
->prop
; prop
; prop
= prop
->next
) {
522 prop
->menu
->flags
|= MENU_CHANGED
;
526 void sym_set_all_changed(void)
531 for_all_symbols(i
, sym
)
532 sym_set_changed(sym
);
535 bool sym_tristate_within_range(struct symbol
*sym
, tristate val
)
537 int type
= sym_get_type(sym
);
538 struct expr_select_value
*esv
;
541 if (sym
->visible
== no
)
544 if (type
!= S_BOOLEAN
&& type
!= S_TRISTATE
)
547 if (type
== S_BOOLEAN
&& val
== mod
)
549 tri
= sym
->rev_dep
.tri
;
550 for (esv
= sym
->val_dep
; esv
; esv
= esv
->next
)
551 tri
= EXPR_OR(tri
, esv
->tri
);
552 if (sym
->visible
<= tri
)
554 if (sym_is_choice_value(sym
) && sym
->visible
== yes
)
556 return val
>= tri
&& val
<= sym
->visible
;
559 bool sym_set_tristate_value(struct symbol
*sym
, tristate val
)
561 tristate oldval
= sym_get_tristate_value(sym
);
563 if (oldval
!= val
&& !sym_tristate_within_range(sym
, val
))
566 if (!(sym
->flags
& SYMBOL_DEF_USER
)) {
567 sym
->flags
|= SYMBOL_DEF_USER
;
568 sym_set_changed(sym
);
571 * setting a choice value also resets the new flag of the choice
572 * symbol and all other choice values.
574 if (sym_is_choice_value(sym
) && val
== yes
) {
575 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
576 struct property
*prop
;
579 cs
->def
[S_DEF_USER
].val
= sym
;
580 cs
->flags
|= SYMBOL_DEF_USER
;
581 prop
= sym_get_choice_prop(cs
);
582 for (e
= prop
->expr
; e
; e
= e
->left
.expr
) {
583 if (e
->right
.sym
->visible
!= no
)
584 e
->right
.sym
->flags
|= SYMBOL_DEF_USER
;
588 sym
->def
[S_DEF_USER
].tri
= val
;
590 sym_clear_all_valid();
595 tristate
sym_toggle_tristate_value(struct symbol
*sym
)
597 tristate oldval
, newval
;
599 oldval
= newval
= sym_get_tristate_value(sym
);
612 if (sym_set_tristate_value(sym
, newval
))
614 } while (oldval
!= newval
);
618 bool sym_string_valid(struct symbol
*sym
, const char *str
)
631 if (ch
== '0' && *str
!= 0)
633 while ((ch
= *str
++)) {
639 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
645 } while ((ch
= *str
++));
661 bool sym_string_within_range(struct symbol
*sym
, const char *str
)
663 struct property
*prop
;
668 return sym_string_valid(sym
, str
);
670 if (!sym_string_valid(sym
, str
))
672 prop
= sym_get_range_prop(sym
);
675 val
= strtoll(str
, NULL
, 10);
676 return val
>= sym_get_range_val(prop
->expr
->left
.sym
, 10) &&
677 val
<= sym_get_range_val(prop
->expr
->right
.sym
, 10);
679 if (!sym_string_valid(sym
, str
))
681 prop
= sym_get_range_prop(sym
);
684 val
= strtoll(str
, NULL
, 16);
685 return val
>= sym_get_range_val(prop
->expr
->left
.sym
, 16) &&
686 val
<= sym_get_range_val(prop
->expr
->right
.sym
, 16);
691 return sym_tristate_within_range(sym
, yes
);
693 return sym_tristate_within_range(sym
, mod
);
695 return sym_tristate_within_range(sym
, no
);
703 bool sym_set_string_value(struct symbol
*sym
, const char *newval
)
714 return sym_set_tristate_value(sym
, yes
);
716 return sym_set_tristate_value(sym
, mod
);
718 return sym_set_tristate_value(sym
, no
);
725 if (!sym_string_within_range(sym
, newval
))
728 if (!(sym
->flags
& SYMBOL_DEF_USER
)) {
729 sym
->flags
|= SYMBOL_DEF_USER
;
730 sym_set_changed(sym
);
733 oldval
= sym
->def
[S_DEF_USER
].val
;
734 size
= strlen(newval
) + 1;
735 if (sym
->type
== S_HEX
&& (newval
[0] != '0' || (newval
[1] != 'x' && newval
[1] != 'X'))) {
737 sym
->def
[S_DEF_USER
].val
= val
= xmalloc(size
);
740 } else if (!oldval
|| strcmp(oldval
, newval
))
741 sym
->def
[S_DEF_USER
].val
= val
= xmalloc(size
);
746 free((void *)oldval
);
747 sym_clear_all_valid();
753 * Find the default value associated to a symbol.
754 * For tristate symbol handle the modules=n case
755 * in which case "m" becomes "y".
756 * If the symbol does not have any default then fallback
757 * to the fixed default values.
759 const char *sym_get_string_default(struct symbol
*sym
)
761 struct property
*prop
;
766 sym_calc_visibility(sym
);
767 sym_calc_value(modules_sym
);
768 val
= symbol_no
.curr
.tri
;
769 str
= symbol_empty
.curr
.val
;
771 /* If symbol has a default value look it up */
772 prop
= sym_get_default_prop(sym
);
777 /* The visibility may limit the value from yes => mod */
778 val
= EXPR_AND(expr_calc_value(prop
->expr
), prop
->visible
.tri
);
782 * The following fails to handle the situation
783 * where a default value is further limited by
786 ds
= prop_get_symbol(prop
);
789 str
= (const char *)ds
->curr
.val
;
794 /* Handle select statements */
795 val
= EXPR_OR(val
, sym
->rev_dep
.tri
);
797 /* transpose mod to yes if modules are not enabled */
799 if (!sym_is_choice_value(sym
) && modules_sym
->curr
.tri
== no
)
802 /* transpose mod to yes if type is bool */
803 if (sym
->type
== S_BOOLEAN
&& val
== mod
)
811 case mod
: return "m";
812 case yes
: return "y";
826 const char *sym_get_string_value(struct symbol
*sym
)
833 val
= sym_get_tristate_value(sym
);
838 sym_calc_value(modules_sym
);
839 return (modules_sym
->curr
.tri
== no
) ? "n" : "m";
847 return (const char *)sym
->curr
.val
;
850 bool sym_is_changable(struct symbol
*sym
)
852 tristate tri
= sym
->rev_dep
.tri
;
853 struct expr_select_value
*esv
;
855 for (esv
= sym
->val_dep
; esv
; esv
= esv
->next
)
856 tri
= EXPR_OR(tri
, esv
->tri
);
858 return sym
->visible
> tri
;
861 static unsigned strhash(const char *s
)
864 unsigned hash
= 2166136261U;
866 hash
= (hash
^ *s
) * 0x01000193;
870 struct symbol
*sym_lookup(const char *name
, int flags
)
872 struct symbol
*symbol
;
877 if (name
[0] && !name
[1]) {
879 case 'y': return &symbol_yes
;
880 case 'm': return &symbol_mod
;
881 case 'n': return &symbol_no
;
884 hash
= strhash(name
) % SYMBOL_HASHSIZE
;
886 for (symbol
= symbol_hash
[hash
]; symbol
; symbol
= symbol
->next
) {
888 !strcmp(symbol
->name
, name
) &&
889 (flags
? symbol
->flags
& flags
890 : !(symbol
->flags
& (SYMBOL_CONST
|SYMBOL_CHOICE
))))
893 new_name
= strdup(name
);
899 symbol
= xmalloc(sizeof(*symbol
));
900 memset(symbol
, 0, sizeof(*symbol
));
901 symbol
->name
= new_name
;
902 symbol
->type
= S_UNKNOWN
;
903 symbol
->flags
|= flags
;
905 symbol
->next
= symbol_hash
[hash
];
906 symbol_hash
[hash
] = symbol
;
911 struct symbol
*sym_find(const char *name
)
913 struct symbol
*symbol
= NULL
;
919 if (name
[0] && !name
[1]) {
921 case 'y': return &symbol_yes
;
922 case 'm': return &symbol_mod
;
923 case 'n': return &symbol_no
;
926 hash
= strhash(name
) % SYMBOL_HASHSIZE
;
928 for (symbol
= symbol_hash
[hash
]; symbol
; symbol
= symbol
->next
) {
930 !strcmp(symbol
->name
, name
) &&
931 !(symbol
->flags
& SYMBOL_CONST
))
939 * Expand symbol's names embedded in the string given in argument. Symbols'
940 * name to be expanded shall be prefixed by a '$'. Unknown symbol expands to
943 const char *sym_expand_string_value(const char *in
)
949 reslen
= strlen(in
) + 1;
950 res
= xmalloc(reslen
);
953 while ((src
= strchr(in
, '$'))) {
954 char *p
, name
[SYMBOL_MAXLENGTH
];
955 const char *symval
= "";
959 strncat(res
, in
, src
- in
);
963 while (isalnum(*src
) || *src
== '_')
967 sym
= sym_find(name
);
970 symval
= sym_get_string_value(sym
);
973 newlen
= strlen(res
) + strlen(symval
) + strlen(src
) + 1;
974 if (newlen
> reslen
) {
976 res
= realloc(res
, reslen
);
987 const char *sym_escape_string_value(const char *in
)
994 reslen
= strlen(in
) + strlen("\"\"") + 1;
998 l
= strcspn(p
, "\"\\");
1008 res
= xmalloc(reslen
);
1015 l
= strcspn(p
, "\"\\");
1023 strncat(res
, p
++, 1);
1035 /* Compare matched symbols as thus:
1036 * - first, symbols that match exactly
1037 * - then, alphabetical sort
1039 static int sym_rel_comp(const void *sym1
, const void *sym2
)
1041 const struct sym_match
*s1
= sym1
;
1042 const struct sym_match
*s2
= sym2
;
1046 * - if matched length on symbol s1 is the length of that symbol,
1047 * then this symbol should come first;
1048 * - if matched length on symbol s2 is the length of that symbol,
1049 * then this symbol should come first.
1050 * Note: since the search can be a regexp, both symbols may match
1051 * exactly; if this is the case, we can't decide which comes first,
1052 * and we fallback to sorting alphabetically.
1054 exact1
= (s1
->eo
- s1
->so
) == strlen(s1
->sym
->name
);
1055 exact2
= (s2
->eo
- s2
->so
) == strlen(s2
->sym
->name
);
1056 if (exact1
&& !exact2
)
1058 if (!exact1
&& exact2
)
1061 /* As a fallback, sort symbols alphabetically */
1062 return strcmp(s1
->sym
->name
, s2
->sym
->name
);
1065 struct symbol
**sym_re_search(const char *pattern
)
1067 struct symbol
*sym
, **sym_arr
= NULL
;
1068 struct sym_match
*sym_match_arr
= NULL
;
1071 regmatch_t match
[1];
1075 if (strlen(pattern
) == 0)
1077 if (regcomp(&re
, pattern
, REG_EXTENDED
|REG_ICASE
))
1080 for_all_symbols(i
, sym
) {
1081 if (sym
->flags
& SYMBOL_CONST
|| !sym
->name
)
1083 if (regexec(&re
, sym
->name
, 1, match
, 0))
1088 tmp
= realloc(sym_match_arr
, size
* sizeof(struct sym_match
));
1090 goto sym_re_search_free
;
1091 sym_match_arr
= tmp
;
1093 sym_calc_value(sym
);
1094 /* As regexec returned 0, we know we have a match, so
1095 * we can use match[0].rm_[se]o without further checks
1097 sym_match_arr
[cnt
].so
= match
[0].rm_so
;
1098 sym_match_arr
[cnt
].eo
= match
[0].rm_eo
;
1099 sym_match_arr
[cnt
++].sym
= sym
;
1101 if (sym_match_arr
) {
1102 qsort(sym_match_arr
, cnt
, sizeof(struct sym_match
), sym_rel_comp
);
1103 sym_arr
= malloc((cnt
+1) * sizeof(struct symbol
));
1105 goto sym_re_search_free
;
1106 for (i
= 0; i
< cnt
; i
++)
1107 sym_arr
[i
] = sym_match_arr
[i
].sym
;
1108 sym_arr
[cnt
] = NULL
;
1111 /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
1112 free(sym_match_arr
);
1119 * When we check for recursive dependencies we use a stack to save
1120 * current state so we can print out relevant info to user.
1121 * The entries are located on the call stack so no need to free memory.
1122 * Note insert() remove() must always match to properly clear the stack.
1124 static struct dep_stack
{
1125 struct dep_stack
*prev
, *next
;
1127 struct property
*prop
;
1131 static void dep_stack_insert(struct dep_stack
*stack
, struct symbol
*sym
)
1133 memset(stack
, 0, sizeof(*stack
));
1135 check_top
->next
= stack
;
1136 stack
->prev
= check_top
;
1141 static void dep_stack_remove(void)
1143 check_top
= check_top
->prev
;
1145 check_top
->next
= NULL
;
1149 * Called when we have detected a recursive dependency.
1150 * check_top point to the top of the stact so we use
1151 * the ->prev pointer to locate the bottom of the stack.
1153 static void sym_check_print_recursive(struct symbol
*last_sym
)
1155 struct dep_stack
*stack
;
1156 struct symbol
*sym
, *next_sym
;
1157 struct menu
*menu
= NULL
;
1158 struct property
*prop
;
1159 struct dep_stack cv_stack
;
1161 if (sym_is_choice_value(last_sym
)) {
1162 dep_stack_insert(&cv_stack
, last_sym
);
1163 last_sym
= prop_get_symbol(sym_get_choice_prop(last_sym
));
1166 for (stack
= check_top
; stack
!= NULL
; stack
= stack
->prev
)
1167 if (stack
->sym
== last_sym
)
1170 fprintf(stderr
, "unexpected recursive dependency error\n");
1174 for (; stack
; stack
= stack
->next
) {
1176 next_sym
= stack
->next
? stack
->next
->sym
: last_sym
;
1179 prop
= stack
->sym
->prop
;
1181 /* for choice values find the menu entry (used below) */
1182 if (sym_is_choice(sym
) || sym_is_choice_value(sym
)) {
1183 for (prop
= sym
->prop
; prop
; prop
= prop
->next
) {
1189 if (stack
->sym
== last_sym
)
1190 fprintf(stderr
, "%s:%d:error: recursive dependency detected!\n",
1191 prop
->file
->name
, prop
->lineno
);
1193 fprintf(stderr
, "%s:%d:\tsymbol %s %s value contains %s\n",
1194 prop
->file
->name
, prop
->lineno
,
1195 sym
->name
? sym
->name
: "<choice>",
1196 prop_get_type_name(prop
->type
),
1197 next_sym
->name
? next_sym
->name
: "<choice>");
1198 } else if (stack
->prop
) {
1199 fprintf(stderr
, "%s:%d:\tsymbol %s depends on %s\n",
1200 prop
->file
->name
, prop
->lineno
,
1201 sym
->name
? sym
->name
: "<choice>",
1202 next_sym
->name
? next_sym
->name
: "<choice>");
1203 } else if (sym_is_choice(sym
)) {
1204 fprintf(stderr
, "%s:%d:\tchoice %s contains symbol %s\n",
1205 menu
->file
->name
, menu
->lineno
,
1206 sym
->name
? sym
->name
: "<choice>",
1207 next_sym
->name
? next_sym
->name
: "<choice>");
1208 } else if (sym_is_choice_value(sym
)) {
1209 fprintf(stderr
, "%s:%d:\tsymbol %s is part of choice %s\n",
1210 menu
->file
->name
, menu
->lineno
,
1211 sym
->name
? sym
->name
: "<choice>",
1212 next_sym
->name
? next_sym
->name
: "<choice>");
1214 fprintf(stderr
, "%s:%d:\tsymbol %s is selected by %s\n",
1215 prop
->file
->name
, prop
->lineno
,
1216 sym
->name
? sym
->name
: "<choice>",
1217 next_sym
->name
? next_sym
->name
: "<choice>");
1221 if (check_top
== &cv_stack
)
1225 static struct symbol
*sym_check_expr_deps(struct expr
*e
)
1234 sym
= sym_check_expr_deps(e
->left
.expr
);
1237 return sym_check_expr_deps(e
->right
.expr
);
1239 return sym_check_expr_deps(e
->left
.expr
);
1242 sym
= sym_check_deps(e
->left
.sym
);
1245 return sym_check_deps(e
->right
.sym
);
1247 return sym_check_deps(e
->left
.sym
);
1251 printf("Oops! How to check %d?\n", e
->type
);
1255 /* return NULL when dependencies are OK */
1256 static struct symbol
*sym_check_sym_deps(struct symbol
*sym
)
1258 struct symbol
*sym2
;
1259 struct property
*prop
;
1260 struct dep_stack stack
;
1262 dep_stack_insert(&stack
, sym
);
1264 sym2
= sym_check_expr_deps(sym
->rev_dep
.expr
);
1268 for (prop
= sym
->prop
; prop
; prop
= prop
->next
) {
1269 if (prop
->type
== P_CHOICE
|| prop
->type
== P_SELECT
)
1272 sym2
= sym_check_expr_deps(prop
->visible
.expr
);
1275 if (prop
->type
!= P_DEFAULT
|| sym_is_choice(sym
))
1277 stack
.expr
= prop
->expr
;
1278 sym2
= sym_check_expr_deps(prop
->expr
);
1290 static struct symbol
*sym_check_choice_deps(struct symbol
*choice
)
1292 struct symbol
*sym
, *sym2
;
1293 struct property
*prop
;
1295 struct dep_stack stack
;
1297 dep_stack_insert(&stack
, choice
);
1299 prop
= sym_get_choice_prop(choice
);
1300 expr_list_for_each_sym(prop
->expr
, e
, sym
)
1301 sym
->flags
|= (SYMBOL_CHECK
| SYMBOL_CHECKED
);
1303 choice
->flags
|= (SYMBOL_CHECK
| SYMBOL_CHECKED
);
1304 sym2
= sym_check_sym_deps(choice
);
1305 choice
->flags
&= ~SYMBOL_CHECK
;
1309 expr_list_for_each_sym(prop
->expr
, e
, sym
) {
1310 sym2
= sym_check_sym_deps(sym
);
1315 expr_list_for_each_sym(prop
->expr
, e
, sym
)
1316 sym
->flags
&= ~SYMBOL_CHECK
;
1318 if (sym2
&& sym_is_choice_value(sym2
) &&
1319 prop_get_symbol(sym_get_choice_prop(sym2
)) == choice
)
1327 struct symbol
*sym_check_deps(struct symbol
*sym
)
1329 struct symbol
*sym2
;
1330 struct property
*prop
;
1332 if (sym
->flags
& SYMBOL_CHECK
) {
1333 sym_check_print_recursive(sym
);
1336 if (sym
->flags
& SYMBOL_CHECKED
)
1339 if (sym_is_choice_value(sym
)) {
1340 struct dep_stack stack
;
1342 /* for choice groups start the check with main choice symbol */
1343 dep_stack_insert(&stack
, sym
);
1344 prop
= sym_get_choice_prop(sym
);
1345 sym2
= sym_check_deps(prop_get_symbol(prop
));
1347 } else if (sym_is_choice(sym
)) {
1348 sym2
= sym_check_choice_deps(sym
);
1350 sym
->flags
|= (SYMBOL_CHECK
| SYMBOL_CHECKED
);
1351 sym2
= sym_check_sym_deps(sym
);
1352 sym
->flags
&= ~SYMBOL_CHECK
;
1355 if (sym2
&& sym2
== sym
)
1361 struct property
*prop_alloc(enum prop_type type
, struct symbol
*sym
)
1363 struct property
*prop
;
1364 struct property
**propp
;
1366 prop
= xmalloc(sizeof(*prop
));
1367 memset(prop
, 0, sizeof(*prop
));
1370 prop
->file
= current_file
;
1371 prop
->lineno
= zconf_lineno();
1373 /* append property to the prop list of symbol */
1375 for (propp
= &sym
->prop
; *propp
; propp
= &(*propp
)->next
)
1383 struct symbol
*prop_get_symbol(struct property
*prop
)
1385 if (prop
->expr
&& (prop
->expr
->type
== E_SYMBOL
||
1386 prop
->expr
->type
== E_LIST
))
1387 return prop
->expr
->left
.sym
;
1391 const char *prop_get_type_name(enum prop_type type
)
1418 static void prop_add_env(const char *env
)
1420 struct symbol
*sym
, *sym2
;
1421 struct property
*prop
;
1424 sym
= current_entry
->sym
;
1425 sym
->flags
|= SYMBOL_AUTO
;
1426 for_all_properties(sym
, prop
, P_ENV
) {
1427 sym2
= prop_get_symbol(prop
);
1428 if (strcmp(sym2
->name
, env
))
1429 menu_warn(current_entry
, "redefining environment symbol from %s",
1434 prop
= prop_alloc(P_ENV
, sym
);
1435 prop
->expr
= expr_alloc_symbol(sym_lookup(env
, SYMBOL_CONST
));
1437 sym_env_list
= expr_alloc_one(E_LIST
, sym_env_list
);
1438 sym_env_list
->right
.sym
= sym
;
1442 sym_add_default(sym
, p
);
1444 menu_warn(current_entry
, "environment variable %s undefined", env
);