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 struct symbol symbol_yes
= {
18 .flags
= SYMBOL_YES
|SYMBOL_VALID
,
22 .flags
= SYMBOL_MOD
|SYMBOL_VALID
,
26 .flags
= SYMBOL_NO
|SYMBOL_VALID
,
30 .flags
= SYMBOL_VALID
,
34 struct symbol
*modules_sym
;
37 void sym_add_default(struct symbol
*sym
, const char *def
)
39 struct property
*prop
= prop_alloc(P_DEFAULT
, sym
);
41 prop
->expr
= expr_alloc_symbol(sym_lookup(def
, 1));
49 static bool inited
= false;
57 sym
= sym_lookup("ARCH", 0);
59 sym
->flags
|= SYMBOL_AUTO
;
62 sym_add_default(sym
, p
);
64 sym
= sym_lookup("OPENWRTVERSION", 0);
66 sym
->flags
|= SYMBOL_AUTO
;
67 p
= getenv("OPENWRTVERSION");
69 sym_add_default(sym
, p
);
71 sym
= sym_lookup("UNAME_RELEASE", 0);
73 sym
->flags
|= SYMBOL_AUTO
;
74 sym_add_default(sym
, uts
.release
);
77 enum symbol_type
sym_get_type(struct symbol
*sym
)
79 enum symbol_type type
= sym
->type
;
81 if (type
== S_TRISTATE
) {
82 if (sym_is_choice_value(sym
) && sym
->visible
== yes
)
84 /* tristate always enabled */
86 else if (modules_val
== no
)
93 const char *sym_type_name(enum symbol_type type
)
114 struct property
*sym_get_choice_prop(struct symbol
*sym
)
116 struct property
*prop
;
118 for_all_choices(sym
, prop
)
123 struct property
*sym_get_default_prop(struct symbol
*sym
)
125 struct property
*prop
;
127 for_all_defaults(sym
, prop
) {
128 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
129 if (prop
->visible
.tri
!= no
)
135 struct property
*sym_get_range_prop(struct symbol
*sym
)
137 struct property
*prop
;
139 for_all_properties(sym
, prop
, P_RANGE
) {
140 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
141 if (prop
->visible
.tri
!= no
)
147 static int sym_get_range_val(struct symbol
*sym
, int base
)
160 return strtol(sym
->curr
.val
, NULL
, base
);
163 static void sym_validate_range(struct symbol
*sym
)
165 struct property
*prop
;
179 prop
= sym_get_range_prop(sym
);
182 val
= strtol(sym
->curr
.val
, NULL
, base
);
183 val2
= sym_get_range_val(prop
->expr
->left
.sym
, base
);
185 val2
= sym_get_range_val(prop
->expr
->right
.sym
, base
);
189 if (sym
->type
== S_INT
)
190 sprintf(str
, "%d", val2
);
192 sprintf(str
, "0x%x", val2
);
193 sym
->curr
.val
= strdup(str
);
196 static void sym_calc_visibility(struct symbol
*sym
)
198 struct property
*prop
;
202 /* any prompt visible? */
204 for_all_prompts(sym
, prop
) {
205 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
206 tri
= E_OR(tri
, prop
->visible
.tri
);
208 if (tri
== mod
&& (sym
->type
!= S_TRISTATE
))
210 if (sym
->rev_dep_inv
.expr
&& (expr_calc_value(sym
->rev_dep_inv
.expr
) == yes
)) {
214 if (sym
->visible
!= tri
) {
216 sym_set_changed(sym
);
218 if (sym_is_choice_value(sym
) || deselected
)
221 if (sym
->rev_dep
.expr
)
222 tri
= expr_calc_value(sym
->rev_dep
.expr
);
223 if (tri
== mod
&& sym_get_type(sym
) == S_BOOLEAN
)
225 if (sym
->rev_dep
.tri
!= tri
) {
226 sym
->rev_dep
.tri
= tri
;
227 sym_set_changed(sym
);
231 static struct symbol
*sym_calc_choice(struct symbol
*sym
)
233 struct symbol
*def_sym
;
234 struct property
*prop
;
237 /* is the user choice visible? */
238 def_sym
= sym
->user
.val
;
240 sym_calc_visibility(def_sym
);
241 if (def_sym
->visible
!= no
)
245 /* any of the defaults visible? */
246 for_all_defaults(sym
, prop
) {
247 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
248 if (prop
->visible
.tri
== no
)
250 def_sym
= prop_get_symbol(prop
);
251 sym_calc_visibility(def_sym
);
252 if (def_sym
->visible
!= no
)
256 /* just get the first visible value */
257 prop
= sym_get_choice_prop(sym
);
258 for (e
= prop
->expr
; e
; e
= e
->left
.expr
) {
259 def_sym
= e
->right
.sym
;
260 sym_calc_visibility(def_sym
);
261 if (def_sym
->visible
!= no
)
265 /* no choice? reset tristate value */
270 void sym_calc_value(struct symbol
*sym
)
272 struct symbol_value newval
, oldval
;
273 struct property
*prop
;
279 if (sym
->flags
& SYMBOL_VALID
)
281 sym
->flags
|= SYMBOL_VALID
;
289 newval
= symbol_empty
.curr
;
293 newval
= symbol_no
.curr
;
296 sym
->curr
.val
= sym
->name
;
300 if (!sym_is_choice_value(sym
))
301 sym
->flags
&= ~SYMBOL_WRITE
;
303 sym_calc_visibility(sym
);
305 /* set default if recursively called */
308 switch (sym_get_type(sym
)) {
311 if (sym_is_choice_value(sym
) && sym
->visible
== yes
) {
312 prop
= sym_get_choice_prop(sym
);
313 newval
.tri
= (prop_get_symbol(prop
)->curr
.val
== sym
) ? yes
: no
;
314 } else if (sym
->rev_dep_inv
.expr
&& (expr_calc_value(sym
->rev_dep_inv
.expr
) == yes
)) {
316 } else if (E_OR(sym
->visible
, sym
->rev_dep
.tri
) != no
) {
317 sym
->flags
|= SYMBOL_WRITE
;
318 if (sym_has_value(sym
))
319 newval
.tri
= sym
->user
.tri
;
320 else if (!sym_is_choice(sym
)) {
321 prop
= sym_get_default_prop(sym
);
323 newval
.tri
= expr_calc_value(prop
->expr
);
325 newval
.tri
= E_OR(E_AND(newval
.tri
, sym
->visible
), sym
->rev_dep
.tri
);
326 } else if (!sym_is_choice(sym
)) {
327 prop
= sym_get_default_prop(sym
);
329 sym
->flags
|= SYMBOL_WRITE
;
330 newval
.tri
= expr_calc_value(prop
->expr
);
333 if (newval
.tri
== mod
&& sym_get_type(sym
) == S_BOOLEAN
)
339 if (sym
->visible
!= no
) {
340 sym
->flags
|= SYMBOL_WRITE
;
341 if (sym_has_value(sym
)) {
342 newval
.val
= sym
->user
.val
;
346 prop
= sym_get_default_prop(sym
);
348 struct symbol
*ds
= prop_get_symbol(prop
);
350 sym
->flags
|= SYMBOL_WRITE
;
352 newval
.val
= ds
->curr
.val
;
361 if (sym_is_choice(sym
) && newval
.tri
== yes
)
362 sym
->curr
.val
= sym_calc_choice(sym
);
363 sym_validate_range(sym
);
365 if (memcmp(&oldval
, &sym
->curr
, sizeof(oldval
)))
366 sym_set_changed(sym
);
368 if (modules_sym
== sym
)
369 modules_val
= modules_sym
->curr
.tri
;
371 if (sym_is_choice(sym
)) {
372 int flags
= sym
->flags
& (SYMBOL_CHANGED
| SYMBOL_WRITE
);
373 prop
= sym_get_choice_prop(sym
);
374 for (e
= prop
->expr
; e
; e
= e
->left
.expr
) {
375 e
->right
.sym
->flags
|= flags
;
376 if (flags
& SYMBOL_CHANGED
)
377 sym_set_changed(e
->right
.sym
);
382 void sym_clear_all_valid(void)
387 for_all_symbols(i
, sym
)
388 sym
->flags
&= ~SYMBOL_VALID
;
391 sym_calc_value(modules_sym
);
394 void sym_set_changed(struct symbol
*sym
)
396 struct property
*prop
;
398 sym
->flags
|= SYMBOL_CHANGED
;
399 for (prop
= sym
->prop
; prop
; prop
= prop
->next
) {
401 prop
->menu
->flags
|= MENU_CHANGED
;
405 void sym_set_all_changed(void)
410 for_all_symbols(i
, sym
)
411 sym_set_changed(sym
);
414 bool sym_tristate_within_range(struct symbol
*sym
, tristate val
)
416 int type
= sym_get_type(sym
);
418 if (sym
->visible
== no
)
421 if (type
!= S_BOOLEAN
&& type
!= S_TRISTATE
)
424 if (type
== S_BOOLEAN
&& val
== mod
)
426 if (sym
->visible
<= sym
->rev_dep
.tri
)
428 if (sym_is_choice_value(sym
) && sym
->visible
== yes
)
430 return val
>= sym
->rev_dep
.tri
&& val
<= sym
->visible
;
433 bool sym_set_tristate_value(struct symbol
*sym
, tristate val
)
435 tristate oldval
= sym_get_tristate_value(sym
);
437 if (oldval
!= val
&& !sym_tristate_within_range(sym
, val
))
440 if (sym
->flags
& SYMBOL_NEW
) {
441 sym
->flags
&= ~SYMBOL_NEW
;
442 sym_set_changed(sym
);
445 * setting a choice value also resets the new flag of the choice
446 * symbol and all other choice values.
448 if (sym_is_choice_value(sym
) && val
== yes
) {
449 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
450 struct property
*prop
;
454 cs
->flags
&= ~SYMBOL_NEW
;
455 prop
= sym_get_choice_prop(cs
);
456 for (e
= prop
->expr
; e
; e
= e
->left
.expr
) {
457 if (e
->right
.sym
->visible
!= no
)
458 e
->right
.sym
->flags
&= ~SYMBOL_NEW
;
464 sym_clear_all_valid();
465 if (sym
== modules_sym
)
466 sym_set_all_changed();
472 tristate
sym_toggle_tristate_value(struct symbol
*sym
)
474 tristate oldval
, newval
;
476 oldval
= newval
= sym_get_tristate_value(sym
);
489 if (sym_set_tristate_value(sym
, newval
))
491 } while (oldval
!= newval
);
495 bool sym_string_valid(struct symbol
*sym
, const char *str
)
508 if (ch
== '0' && *str
!= 0)
510 while ((ch
= *str
++)) {
516 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
522 } while ((ch
= *str
++));
538 bool sym_string_within_range(struct symbol
*sym
, const char *str
)
540 struct property
*prop
;
545 return sym_string_valid(sym
, str
);
547 if (!sym_string_valid(sym
, str
))
549 prop
= sym_get_range_prop(sym
);
552 val
= strtol(str
, NULL
, 10);
553 return val
>= sym_get_range_val(prop
->expr
->left
.sym
, 10) &&
554 val
<= sym_get_range_val(prop
->expr
->right
.sym
, 10);
556 if (!sym_string_valid(sym
, str
))
558 prop
= sym_get_range_prop(sym
);
561 val
= strtol(str
, NULL
, 16);
562 return val
>= sym_get_range_val(prop
->expr
->left
.sym
, 16) &&
563 val
<= sym_get_range_val(prop
->expr
->right
.sym
, 16);
568 return sym_tristate_within_range(sym
, yes
);
570 return sym_tristate_within_range(sym
, mod
);
572 return sym_tristate_within_range(sym
, no
);
580 bool sym_set_string_value(struct symbol
*sym
, const char *newval
)
591 return sym_set_tristate_value(sym
, yes
);
593 return sym_set_tristate_value(sym
, mod
);
595 return sym_set_tristate_value(sym
, no
);
602 if (!sym_string_within_range(sym
, newval
))
605 if (sym
->flags
& SYMBOL_NEW
) {
606 sym
->flags
&= ~SYMBOL_NEW
;
607 sym_set_changed(sym
);
610 oldval
= sym
->user
.val
;
611 size
= strlen(newval
) + 1;
612 if (sym
->type
== S_HEX
&& (newval
[0] != '0' || (newval
[1] != 'x' && newval
[1] != 'X'))) {
614 sym
->user
.val
= val
= malloc(size
);
617 } else if (!oldval
|| strcmp(oldval
, newval
))
618 sym
->user
.val
= val
= malloc(size
);
623 free((void *)oldval
);
624 sym_clear_all_valid();
629 const char *sym_get_string_value(struct symbol
*sym
)
636 val
= sym_get_tristate_value(sym
);
649 return (const char *)sym
->curr
.val
;
652 bool sym_is_changable(struct symbol
*sym
)
654 return sym
->visible
> sym
->rev_dep
.tri
;
657 struct symbol
*sym_lookup(const char *name
, int isconst
)
659 struct symbol
*symbol
;
665 if (name
[0] && !name
[1]) {
667 case 'y': return &symbol_yes
;
668 case 'm': return &symbol_mod
;
669 case 'n': return &symbol_no
;
672 for (ptr
= name
; *ptr
; ptr
++)
676 for (symbol
= symbol_hash
[hash
]; symbol
; symbol
= symbol
->next
) {
677 if (!strcmp(symbol
->name
, name
)) {
678 if ((isconst
&& symbol
->flags
& SYMBOL_CONST
) ||
679 (!isconst
&& !(symbol
->flags
& SYMBOL_CONST
)))
683 new_name
= strdup(name
);
689 symbol
= malloc(sizeof(*symbol
));
690 memset(symbol
, 0, sizeof(*symbol
));
691 symbol
->name
= new_name
;
692 symbol
->type
= S_UNKNOWN
;
693 symbol
->flags
= SYMBOL_NEW
;
695 symbol
->flags
|= SYMBOL_CONST
;
697 symbol
->next
= symbol_hash
[hash
];
698 symbol_hash
[hash
] = symbol
;
703 struct symbol
*sym_find(const char *name
)
705 struct symbol
*symbol
= NULL
;
712 if (name
[0] && !name
[1]) {
714 case 'y': return &symbol_yes
;
715 case 'm': return &symbol_mod
;
716 case 'n': return &symbol_no
;
719 for (ptr
= name
; *ptr
; ptr
++)
723 for (symbol
= symbol_hash
[hash
]; symbol
; symbol
= symbol
->next
) {
724 if (!strcmp(symbol
->name
, name
) &&
725 !(symbol
->flags
& SYMBOL_CONST
))
732 struct symbol
**sym_re_search(const char *pattern
)
734 struct symbol
*sym
, **sym_arr
= NULL
;
740 if (strlen(pattern
) == 0)
742 if (regcomp(&re
, pattern
, REG_EXTENDED
|REG_NOSUB
|REG_ICASE
))
745 for_all_symbols(i
, sym
) {
746 if (sym
->flags
& SYMBOL_CONST
|| !sym
->name
)
748 if (regexec(&re
, sym
->name
, 0, NULL
, 0))
750 if (cnt
+ 1 >= size
) {
753 sym_arr
= realloc(sym_arr
, size
* sizeof(struct symbol
*));
759 sym_arr
[cnt
++] = sym
;
769 struct symbol
*sym_check_deps(struct symbol
*sym
);
771 static struct symbol
*sym_check_expr_deps(struct expr
*e
)
780 sym
= sym_check_expr_deps(e
->left
.expr
);
783 return sym_check_expr_deps(e
->right
.expr
);
785 return sym_check_expr_deps(e
->left
.expr
);
788 sym
= sym_check_deps(e
->left
.sym
);
791 return sym_check_deps(e
->right
.sym
);
793 return sym_check_deps(e
->left
.sym
);
797 printf("Oops! How to check %d?\n", e
->type
);
801 struct symbol
*sym_check_deps(struct symbol
*sym
)
804 struct property
*prop
;
806 if (sym
->flags
& SYMBOL_CHECK
) {
807 printf("Warning! Found recursive dependency: %s", sym
->name
);
810 if (sym
->flags
& SYMBOL_CHECKED
)
813 sym
->flags
|= (SYMBOL_CHECK
| SYMBOL_CHECKED
);
814 sym2
= sym_check_expr_deps(sym
->rev_dep
.expr
);
818 for (prop
= sym
->prop
; prop
; prop
= prop
->next
) {
819 if (prop
->type
== P_CHOICE
|| prop
->type
== P_SELECT
|| prop
->type
== P_DESELECT
)
821 sym2
= sym_check_expr_deps(prop
->visible
.expr
);
824 if (prop
->type
!= P_DEFAULT
|| sym_is_choice(sym
))
826 sym2
= sym_check_expr_deps(prop
->expr
);
832 printf(" %s", sym
->name
);
838 sym
->flags
&= ~SYMBOL_CHECK
;
842 struct property
*prop_alloc(enum prop_type type
, struct symbol
*sym
)
844 struct property
*prop
;
845 struct property
**propp
;
847 prop
= malloc(sizeof(*prop
));
848 memset(prop
, 0, sizeof(*prop
));
851 prop
->file
= current_file
;
852 prop
->lineno
= zconf_lineno();
854 /* append property to the prop list of symbol */
856 for (propp
= &sym
->prop
; *propp
; propp
= &(*propp
)->next
)
864 struct symbol
*prop_get_symbol(struct property
*prop
)
866 if (prop
->expr
&& (prop
->expr
->type
== E_SYMBOL
||
867 prop
->expr
->type
== E_CHOICE
))
868 return prop
->expr
->left
.sym
;
872 const char *prop_get_type_name(enum prop_type type
)