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>
12 #define LKC_DIRECT_LINK
15 extern int output_mode
;
17 struct symbol symbol_yes
= {
20 .flags
= SYMBOL_YES
|SYMBOL_VALID
,
24 .flags
= SYMBOL_MOD
|SYMBOL_VALID
,
28 .flags
= SYMBOL_NO
|SYMBOL_VALID
,
32 .flags
= SYMBOL_VALID
,
36 struct symbol
*modules_sym
;
39 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
, 1));
50 static bool inited
= false;
56 sym
= sym_lookup("VERSION", 0);
58 sym
->flags
|= SYMBOL_AUTO
;
59 p
= getenv("VERSION");
61 sym_add_default(sym
, p
);
63 sym
= sym_lookup("TARGET_ARCH", 0);
65 sym
->flags
|= SYMBOL_AUTO
;
66 p
= getenv("TARGET_ARCH");
68 sym_add_default(sym
, p
);
72 enum symbol_type
sym_get_type(struct symbol
*sym
)
74 enum symbol_type type
= sym
->type
;
76 if (type
== S_TRISTATE
) {
77 if (sym_is_choice_value(sym
) && sym
->visible
== yes
)
79 else if (modules_val
== no
)
85 const char *sym_type_name(enum symbol_type type
)
106 struct property
*sym_get_choice_prop(struct symbol
*sym
)
108 struct property
*prop
;
110 for_all_choices(sym
, prop
)
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
|| output_mode
)
127 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 void sym_calc_visibility(struct symbol
*sym
)
141 struct property
*prop
;
143 bool deselected
= false;
145 /* any prompt visible? */
147 for_all_prompts(sym
, prop
) {
148 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
149 tri
= E_OR(tri
, prop
->visible
.tri
);
151 if (tri
== mod
&& (sym
->type
!= S_TRISTATE
|| modules_val
== no
))
153 if (sym
->rev_dep_inv
.expr
&& (expr_calc_value(sym
->rev_dep_inv
.expr
) == yes
)) {
157 if (sym
->visible
!= tri
) {
159 sym_set_changed(sym
);
161 if (sym_is_choice_value(sym
) || deselected
)
164 if (sym
->rev_dep
.expr
)
165 tri
= expr_calc_value(sym
->rev_dep
.expr
);
166 if (tri
== mod
&& sym_get_type(sym
) == S_BOOLEAN
)
168 if (sym
->rev_dep
.tri
!= tri
) {
169 sym
->rev_dep
.tri
= tri
;
170 sym_set_changed(sym
);
174 static struct symbol
*sym_calc_choice(struct symbol
*sym
)
176 struct symbol
*def_sym
;
177 struct property
*prop
;
180 /* is the user choice visible? */
181 def_sym
= sym
->user
.val
;
183 sym_calc_visibility(def_sym
);
184 if (def_sym
->visible
!= no
)
188 /* any of the defaults visible? */
189 for_all_defaults(sym
, prop
) {
190 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
191 if (prop
->visible
.tri
== no
)
193 def_sym
= prop_get_symbol(prop
);
194 sym_calc_visibility(def_sym
);
195 if (def_sym
->visible
!= no
)
199 /* just get the first visible value */
200 prop
= sym_get_choice_prop(sym
);
201 for (e
= prop
->expr
; e
; e
= e
->left
.expr
) {
202 def_sym
= e
->right
.sym
;
203 sym_calc_visibility(def_sym
);
204 if (def_sym
->visible
!= no
)
208 /* no choice? reset tristate value */
213 void sym_calc_value(struct symbol
*sym
)
215 struct symbol_value newval
, oldval
;
216 struct property
*prop
;
222 if (sym
->flags
& SYMBOL_VALID
)
224 sym
->flags
|= SYMBOL_VALID
;
232 newval
= symbol_empty
.curr
;
236 newval
= symbol_no
.curr
;
239 sym
->curr
.val
= sym
->name
;
243 if (!sym_is_choice_value(sym
))
244 sym
->flags
&= ~SYMBOL_WRITE
;
246 sym_calc_visibility(sym
);
248 /* set default if recursively called */
251 switch (sym_get_type(sym
)) {
254 if (sym_is_choice_value(sym
) && sym
->visible
== yes
) {
255 prop
= sym_get_choice_prop(sym
);
256 newval
.tri
= (prop_get_symbol(prop
)->curr
.val
== sym
) ? yes
: no
;
257 } else if (sym
->rev_dep_inv
.expr
&& (expr_calc_value(sym
->rev_dep_inv
.expr
) == yes
)) {
259 } else if (E_OR(sym
->visible
, sym
->rev_dep
.tri
) != no
) {
260 sym
->flags
|= SYMBOL_WRITE
;
261 if (sym_has_value(sym
))
262 newval
.tri
= sym
->user
.tri
;
263 else if (!sym_is_choice(sym
)) {
264 prop
= sym_get_default_prop(sym
);
266 newval
.tri
= expr_calc_value(prop
->expr
);
268 newval
.tri
= E_OR(E_AND(newval
.tri
, sym
->visible
), sym
->rev_dep
.tri
);
269 } else if (!sym_is_choice(sym
)) {
270 prop
= sym_get_default_prop(sym
);
272 sym
->flags
|= SYMBOL_WRITE
;
273 newval
.tri
= expr_calc_value(prop
->expr
);
276 if (newval
.tri
== mod
&& sym_get_type(sym
) == S_BOOLEAN
)
282 if (sym
->visible
!= no
) {
283 sym
->flags
|= SYMBOL_WRITE
;
284 if (sym_has_value(sym
)) {
285 newval
.val
= sym
->user
.val
;
289 prop
= sym_get_default_prop(sym
);
291 struct symbol
*ds
= prop_get_symbol(prop
);
293 sym
->flags
|= SYMBOL_WRITE
;
295 newval
.val
= ds
->curr
.val
;
304 if (sym_is_choice(sym
) && newval
.tri
== yes
)
305 sym
->curr
.val
= sym_calc_choice(sym
);
307 if (memcmp(&oldval
, &sym
->curr
, sizeof(oldval
)))
308 sym_set_changed(sym
);
309 if (modules_sym
== sym
)
310 modules_val
= modules_sym
->curr
.tri
;
312 if (sym_is_choice(sym
)) {
313 int flags
= sym
->flags
& (SYMBOL_CHANGED
| SYMBOL_WRITE
);
314 prop
= sym_get_choice_prop(sym
);
315 for (e
= prop
->expr
; e
; e
= e
->left
.expr
) {
316 e
->right
.sym
->flags
|= flags
;
317 if (flags
& SYMBOL_CHANGED
)
318 sym_set_changed(e
->right
.sym
);
323 void sym_clear_all_valid(void)
328 for_all_symbols(i
, sym
)
329 sym
->flags
&= ~SYMBOL_VALID
;
332 sym_calc_value(modules_sym
);
335 void sym_set_changed(struct symbol
*sym
)
337 struct property
*prop
;
339 sym
->flags
|= SYMBOL_CHANGED
;
340 for (prop
= sym
->prop
; prop
; prop
= prop
->next
) {
342 prop
->menu
->flags
|= MENU_CHANGED
;
346 void sym_set_all_changed(void)
351 for_all_symbols(i
, sym
)
352 sym_set_changed(sym
);
355 bool sym_tristate_within_range(struct symbol
*sym
, tristate val
)
357 int type
= sym_get_type(sym
);
359 if (sym
->visible
== no
)
362 if (type
!= S_BOOLEAN
&& type
!= S_TRISTATE
)
365 if (type
== S_BOOLEAN
&& val
== mod
)
367 if (sym
->visible
<= sym
->rev_dep
.tri
)
369 if (sym_is_choice_value(sym
) && sym
->visible
== yes
)
371 return val
>= sym
->rev_dep
.tri
&& val
<= sym
->visible
;
374 bool sym_set_tristate_value(struct symbol
*sym
, tristate val
)
376 tristate oldval
= sym_get_tristate_value(sym
);
378 if (oldval
!= val
&& !sym_tristate_within_range(sym
, val
))
381 if (sym
->flags
& SYMBOL_NEW
) {
382 sym
->flags
&= ~SYMBOL_NEW
;
383 sym_set_changed(sym
);
385 if (sym_is_choice_value(sym
) && val
== yes
) {
386 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
389 cs
->flags
&= ~SYMBOL_NEW
;
394 sym_clear_all_valid();
395 if (sym
== modules_sym
)
396 sym_set_all_changed();
402 tristate
sym_toggle_tristate_value(struct symbol
*sym
)
404 tristate oldval
, newval
;
406 oldval
= newval
= sym_get_tristate_value(sym
);
419 if (sym_set_tristate_value(sym
, newval
))
421 } while (oldval
!= newval
);
425 bool sym_string_valid(struct symbol
*sym
, const char *str
)
438 if (ch
== '0' && *str
!= 0)
440 while ((ch
= *str
++)) {
446 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
452 } while ((ch
= *str
++));
468 bool sym_string_within_range(struct symbol
*sym
, const char *str
)
470 struct property
*prop
;
475 return sym_string_valid(sym
, str
);
477 if (!sym_string_valid(sym
, str
))
479 prop
= sym_get_range_prop(sym
);
482 val
= strtol(str
, NULL
, 10);
483 return val
>= strtol(prop
->expr
->left
.sym
->name
, NULL
, 10) &&
484 val
<= strtol(prop
->expr
->right
.sym
->name
, NULL
, 10);
486 if (!sym_string_valid(sym
, str
))
488 prop
= sym_get_range_prop(sym
);
491 val
= strtol(str
, NULL
, 16);
492 return val
>= strtol(prop
->expr
->left
.sym
->name
, NULL
, 16) &&
493 val
<= strtol(prop
->expr
->right
.sym
->name
, NULL
, 16);
498 return sym_tristate_within_range(sym
, yes
);
500 return sym_tristate_within_range(sym
, mod
);
502 return sym_tristate_within_range(sym
, no
);
510 bool sym_set_string_value(struct symbol
*sym
, const char *newval
)
521 return sym_set_tristate_value(sym
, yes
);
523 return sym_set_tristate_value(sym
, mod
);
525 return sym_set_tristate_value(sym
, no
);
532 if (!sym_string_within_range(sym
, newval
))
535 if (sym
->flags
& SYMBOL_NEW
) {
536 sym
->flags
&= ~SYMBOL_NEW
;
537 sym_set_changed(sym
);
540 oldval
= sym
->user
.val
;
541 size
= strlen(newval
) + 1;
542 if (sym
->type
== S_HEX
&& (newval
[0] != '0' || (newval
[1] != 'x' && newval
[1] != 'X'))) {
544 sym
->user
.val
= val
= malloc(size
);
547 } else if (!oldval
|| strcmp(oldval
, newval
))
548 sym
->user
.val
= val
= malloc(size
);
553 free((void *)oldval
);
554 sym_clear_all_valid();
559 const char *sym_get_string_value(struct symbol
*sym
)
566 val
= sym_get_tristate_value(sym
);
579 return (const char *)sym
->curr
.val
;
582 bool sym_is_changable(struct symbol
*sym
)
584 return sym
->visible
> sym
->rev_dep
.tri
;
587 struct symbol
*sym_lookup(const char *name
, int isconst
)
589 struct symbol
*symbol
;
595 if (name
[0] && !name
[1]) {
597 case 'y': return &symbol_yes
;
598 case 'm': return &symbol_mod
;
599 case 'n': return &symbol_no
;
602 for (ptr
= name
; *ptr
; ptr
++)
606 for (symbol
= symbol_hash
[hash
]; symbol
; symbol
= symbol
->next
) {
607 if (!strcmp(symbol
->name
, name
)) {
608 if ((isconst
&& symbol
->flags
& SYMBOL_CONST
) ||
609 (!isconst
&& !(symbol
->flags
& SYMBOL_CONST
)))
613 new_name
= strdup(name
);
619 symbol
= malloc(sizeof(*symbol
));
620 memset(symbol
, 0, sizeof(*symbol
));
621 symbol
->name
= new_name
;
622 symbol
->type
= S_UNKNOWN
;
623 symbol
->flags
= SYMBOL_NEW
;
625 symbol
->flags
|= SYMBOL_CONST
;
627 symbol
->next
= symbol_hash
[hash
];
628 symbol_hash
[hash
] = symbol
;
633 struct symbol
*sym_find(const char *name
)
635 struct symbol
*symbol
= NULL
;
642 if (name
[0] && !name
[1]) {
644 case 'y': return &symbol_yes
;
645 case 'm': return &symbol_mod
;
646 case 'n': return &symbol_no
;
649 for (ptr
= name
; *ptr
; ptr
++)
653 for (symbol
= symbol_hash
[hash
]; symbol
; symbol
= symbol
->next
) {
654 if (!strcmp(symbol
->name
, name
) &&
655 !(symbol
->flags
& SYMBOL_CONST
))
662 struct symbol
*sym_check_deps(struct symbol
*sym
);
664 static struct symbol
*sym_check_expr_deps(struct expr
*e
)
673 sym
= sym_check_expr_deps(e
->left
.expr
);
676 return sym_check_expr_deps(e
->right
.expr
);
678 return sym_check_expr_deps(e
->left
.expr
);
681 sym
= sym_check_deps(e
->left
.sym
);
684 return sym_check_deps(e
->right
.sym
);
686 return sym_check_deps(e
->left
.sym
);
690 printf("Oops! How to check %d?\n", e
->type
);
694 struct symbol
*sym_check_deps(struct symbol
*sym
)
697 struct property
*prop
;
699 if (sym
->flags
& SYMBOL_CHECK_DONE
)
701 if (sym
->flags
& SYMBOL_CHECK
) {
702 printf("Warning! Found recursive dependency: %s", sym
->name
);
706 sym
->flags
|= (SYMBOL_CHECK
| SYMBOL_CHECKED
);
707 sym2
= sym_check_expr_deps(sym
->rev_dep
.expr
);
711 for (prop
= sym
->prop
; prop
; prop
= prop
->next
) {
712 if (prop
->type
== P_CHOICE
|| prop
->type
== P_SELECT
|| prop
->type
== P_SELECTNOT
)
714 sym2
= sym_check_expr_deps(prop
->visible
.expr
);
717 if (prop
->type
!= P_DEFAULT
|| sym_is_choice(sym
))
719 sym2
= sym_check_expr_deps(prop
->expr
);
725 printf(" %s", sym
->name
);
726 sym
->flags
&= ~SYMBOL_CHECK
;
730 struct property
*prop_alloc(enum prop_type type
, struct symbol
*sym
)
732 struct property
*prop
;
733 struct property
**propp
;
735 prop
= malloc(sizeof(*prop
));
736 memset(prop
, 0, sizeof(*prop
));
739 prop
->file
= current_file
;
740 prop
->lineno
= zconf_lineno();
742 /* append property to the prop list of symbol */
744 for (propp
= &sym
->prop
; *propp
; propp
= &(*propp
)->next
)
752 struct symbol
*prop_get_symbol(struct property
*prop
)
754 if (prop
->expr
&& (prop
->expr
->type
== E_SYMBOL
||
755 prop
->expr
->type
== E_CHOICE
))
756 return prop
->expr
->left
.sym
;
760 const char *prop_get_type_name(enum prop_type type
)