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);
61 sym_add_default(sym
, p
);
63 sym
= sym_lookup("OPENWRTVERSION", 0);
65 sym
->flags
|= SYMBOL_AUTO
;
66 p
= getenv("OPENWRTVERSION");
68 sym_add_default(sym
, p
);
70 sym
= sym_lookup("UNAME_RELEASE", 0);
72 sym
->flags
|= SYMBOL_AUTO
;
73 sym_add_default(sym
, uts
.release
);
76 enum symbol_type
sym_get_type(struct symbol
*sym
)
78 enum symbol_type type
= sym
->type
;
80 if (type
== S_TRISTATE
) {
81 if (sym_is_choice_value(sym
) && sym
->visible
== yes
)
83 /* tristate always enabled */
85 else if (modules_val
== no
)
92 const char *sym_type_name(enum symbol_type type
)
113 struct property
*sym_get_choice_prop(struct symbol
*sym
)
115 struct property
*prop
;
117 for_all_choices(sym
, prop
)
122 struct property
*sym_get_default_prop(struct symbol
*sym
)
124 struct property
*prop
;
126 for_all_defaults(sym
, prop
) {
127 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
128 if (prop
->visible
.tri
!= no
)
134 struct property
*sym_get_range_prop(struct symbol
*sym
)
136 struct property
*prop
;
138 for_all_properties(sym
, prop
, P_RANGE
) {
139 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
140 if (prop
->visible
.tri
!= no
)
146 static int sym_get_range_val(struct symbol
*sym
, int base
)
159 return strtol(sym
->curr
.val
, NULL
, base
);
162 static void sym_validate_range(struct symbol
*sym
)
164 struct property
*prop
;
178 prop
= sym_get_range_prop(sym
);
181 val
= strtol(sym
->curr
.val
, NULL
, base
);
182 val2
= sym_get_range_val(prop
->expr
->left
.sym
, base
);
184 val2
= sym_get_range_val(prop
->expr
->right
.sym
, base
);
188 if (sym
->type
== S_INT
)
189 sprintf(str
, "%d", val2
);
191 sprintf(str
, "0x%x", val2
);
192 sym
->curr
.val
= strdup(str
);
195 static void sym_calc_visibility(struct symbol
*sym
)
197 struct property
*prop
;
201 /* any prompt visible? */
203 for_all_prompts(sym
, prop
) {
204 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
205 tri
= E_OR(tri
, prop
->visible
.tri
);
207 if (tri
== mod
&& (sym
->type
!= S_TRISTATE
))
209 if (sym
->rev_dep_inv
.expr
&& (expr_calc_value(sym
->rev_dep_inv
.expr
) == yes
)) {
213 if (sym
->visible
!= tri
) {
215 sym_set_changed(sym
);
217 if (sym_is_choice_value(sym
) || deselected
)
220 if (sym
->rev_dep
.expr
)
221 tri
= expr_calc_value(sym
->rev_dep
.expr
);
222 if (tri
== mod
&& sym_get_type(sym
) == S_BOOLEAN
)
224 if (sym
->rev_dep
.tri
!= tri
) {
225 sym
->rev_dep
.tri
= tri
;
226 sym_set_changed(sym
);
230 static struct symbol
*sym_calc_choice(struct symbol
*sym
)
232 struct symbol
*def_sym
;
233 struct property
*prop
;
236 /* is the user choice visible? */
237 def_sym
= sym
->user
.val
;
239 sym_calc_visibility(def_sym
);
240 if (def_sym
->visible
!= no
)
244 /* any of the defaults visible? */
245 for_all_defaults(sym
, prop
) {
246 prop
->visible
.tri
= expr_calc_value(prop
->visible
.expr
);
247 if (prop
->visible
.tri
== no
)
249 def_sym
= prop_get_symbol(prop
);
250 sym_calc_visibility(def_sym
);
251 if (def_sym
->visible
!= no
)
255 /* just get the first visible value */
256 prop
= sym_get_choice_prop(sym
);
257 for (e
= prop
->expr
; e
; e
= e
->left
.expr
) {
258 def_sym
= e
->right
.sym
;
259 sym_calc_visibility(def_sym
);
260 if (def_sym
->visible
!= no
)
264 /* no choice? reset tristate value */
269 void sym_calc_value(struct symbol
*sym
)
271 struct symbol_value newval
, oldval
;
272 struct property
*prop
;
278 if (sym
->flags
& SYMBOL_VALID
)
280 sym
->flags
|= SYMBOL_VALID
;
288 newval
= symbol_empty
.curr
;
292 newval
= symbol_no
.curr
;
295 sym
->curr
.val
= sym
->name
;
299 if (!sym_is_choice_value(sym
))
300 sym
->flags
&= ~SYMBOL_WRITE
;
302 sym_calc_visibility(sym
);
304 /* set default if recursively called */
307 switch (sym_get_type(sym
)) {
310 if (sym_is_choice_value(sym
) && sym
->visible
== yes
) {
311 prop
= sym_get_choice_prop(sym
);
312 newval
.tri
= (prop_get_symbol(prop
)->curr
.val
== sym
) ? yes
: no
;
313 } else if (sym
->rev_dep_inv
.expr
&& (expr_calc_value(sym
->rev_dep_inv
.expr
) == yes
)) {
315 } else if (E_OR(sym
->visible
, sym
->rev_dep
.tri
) != no
) {
316 sym
->flags
|= SYMBOL_WRITE
;
317 if (sym_has_value(sym
))
318 newval
.tri
= sym
->user
.tri
;
319 else if (!sym_is_choice(sym
)) {
320 prop
= sym_get_default_prop(sym
);
322 newval
.tri
= expr_calc_value(prop
->expr
);
324 newval
.tri
= E_OR(E_AND(newval
.tri
, sym
->visible
), sym
->rev_dep
.tri
);
325 } else if (!sym_is_choice(sym
)) {
326 prop
= sym_get_default_prop(sym
);
328 sym
->flags
|= SYMBOL_WRITE
;
329 newval
.tri
= expr_calc_value(prop
->expr
);
332 if (newval
.tri
== mod
&& sym_get_type(sym
) == S_BOOLEAN
)
338 if (sym
->visible
!= no
) {
339 sym
->flags
|= SYMBOL_WRITE
;
340 if (sym_has_value(sym
)) {
341 newval
.val
= sym
->user
.val
;
345 prop
= sym_get_default_prop(sym
);
347 struct symbol
*ds
= prop_get_symbol(prop
);
349 sym
->flags
|= SYMBOL_WRITE
;
351 newval
.val
= ds
->curr
.val
;
360 if (sym_is_choice(sym
) && newval
.tri
== yes
)
361 sym
->curr
.val
= sym_calc_choice(sym
);
362 sym_validate_range(sym
);
364 if (memcmp(&oldval
, &sym
->curr
, sizeof(oldval
))) {
365 sym
->flags
&= ~SYMBOL_VALID
;
366 sym_set_changed(sym
);
367 if (modules_sym
== sym
) {
368 sym_set_all_changed();
369 modules_val
= modules_sym
->curr
.tri
;
373 if (sym_is_choice(sym
)) {
374 int flags
= sym
->flags
& (SYMBOL_CHANGED
| SYMBOL_WRITE
);
375 prop
= sym_get_choice_prop(sym
);
376 for (e
= prop
->expr
; e
; e
= e
->left
.expr
) {
377 e
->right
.sym
->flags
|= flags
;
378 if (flags
& SYMBOL_CHANGED
)
379 sym_set_changed(e
->right
.sym
);
383 if (sym
->flags
& SYMBOL_AUTO
)
384 sym
->flags
&= ~SYMBOL_WRITE
;
387 void sym_clear_all_valid(void)
392 for_all_symbols(i
, sym
)
393 sym
->flags
&= ~SYMBOL_VALID
;
396 sym_calc_value(modules_sym
);
399 void sym_set_changed(struct symbol
*sym
)
401 struct property
*prop
;
403 sym
->flags
|= SYMBOL_CHANGED
;
404 for (prop
= sym
->prop
; prop
; prop
= prop
->next
) {
406 prop
->menu
->flags
|= MENU_CHANGED
;
410 void sym_set_all_changed(void)
415 for_all_symbols(i
, sym
)
416 sym_set_changed(sym
);
419 bool sym_tristate_within_range(struct symbol
*sym
, tristate val
)
421 int type
= sym_get_type(sym
);
423 if (sym
->visible
== no
)
426 if (type
!= S_BOOLEAN
&& type
!= S_TRISTATE
)
429 if (type
== S_BOOLEAN
&& val
== mod
)
431 if (sym
->visible
<= sym
->rev_dep
.tri
)
433 if (sym_is_choice_value(sym
) && sym
->visible
== yes
)
435 return val
>= sym
->rev_dep
.tri
&& val
<= sym
->visible
;
438 bool sym_set_tristate_value(struct symbol
*sym
, tristate val
)
440 tristate oldval
= sym_get_tristate_value(sym
);
442 if (oldval
!= val
&& !sym_tristate_within_range(sym
, val
))
445 if (sym
->flags
& SYMBOL_NEW
) {
446 sym
->flags
&= ~SYMBOL_NEW
;
447 sym_set_changed(sym
);
450 * setting a choice value also resets the new flag of the choice
451 * symbol and all other choice values.
453 if (sym_is_choice_value(sym
) && val
== yes
) {
454 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
455 struct property
*prop
;
459 cs
->flags
&= ~SYMBOL_NEW
;
460 prop
= sym_get_choice_prop(cs
);
461 for (e
= prop
->expr
; e
; e
= e
->left
.expr
) {
462 if (e
->right
.sym
->visible
!= no
)
463 e
->right
.sym
->flags
&= ~SYMBOL_NEW
;
469 sym_clear_all_valid();
475 tristate
sym_toggle_tristate_value(struct symbol
*sym
)
477 tristate oldval
, newval
;
479 oldval
= newval
= sym_get_tristate_value(sym
);
492 if (sym_set_tristate_value(sym
, newval
))
494 } while (oldval
!= newval
);
498 bool sym_string_valid(struct symbol
*sym
, const char *str
)
511 if (ch
== '0' && *str
!= 0)
513 while ((ch
= *str
++)) {
519 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
525 } while ((ch
= *str
++));
541 bool sym_string_within_range(struct symbol
*sym
, const char *str
)
543 struct property
*prop
;
548 return sym_string_valid(sym
, str
);
550 if (!sym_string_valid(sym
, str
))
552 prop
= sym_get_range_prop(sym
);
555 val
= strtol(str
, NULL
, 10);
556 return val
>= sym_get_range_val(prop
->expr
->left
.sym
, 10) &&
557 val
<= sym_get_range_val(prop
->expr
->right
.sym
, 10);
559 if (!sym_string_valid(sym
, str
))
561 prop
= sym_get_range_prop(sym
);
564 val
= strtol(str
, NULL
, 16);
565 return val
>= sym_get_range_val(prop
->expr
->left
.sym
, 16) &&
566 val
<= sym_get_range_val(prop
->expr
->right
.sym
, 16);
571 return sym_tristate_within_range(sym
, yes
);
573 return sym_tristate_within_range(sym
, mod
);
575 return sym_tristate_within_range(sym
, no
);
583 bool sym_set_string_value(struct symbol
*sym
, const char *newval
)
594 return sym_set_tristate_value(sym
, yes
);
596 return sym_set_tristate_value(sym
, mod
);
598 return sym_set_tristate_value(sym
, no
);
605 if (!sym_string_within_range(sym
, newval
))
608 if (sym
->flags
& SYMBOL_NEW
) {
609 sym
->flags
&= ~SYMBOL_NEW
;
610 sym_set_changed(sym
);
613 oldval
= sym
->user
.val
;
614 size
= strlen(newval
) + 1;
615 if (sym
->type
== S_HEX
&& (newval
[0] != '0' || (newval
[1] != 'x' && newval
[1] != 'X'))) {
617 sym
->user
.val
= val
= malloc(size
);
620 } else if (!oldval
|| strcmp(oldval
, newval
))
621 sym
->user
.val
= val
= malloc(size
);
626 free((void *)oldval
);
627 sym_clear_all_valid();
632 const char *sym_get_string_value(struct symbol
*sym
)
639 val
= sym_get_tristate_value(sym
);
652 return (const char *)sym
->curr
.val
;
655 bool sym_is_changable(struct symbol
*sym
)
657 return sym
->visible
> sym
->rev_dep
.tri
;
660 struct symbol
*sym_lookup(const char *name
, int isconst
)
662 struct symbol
*symbol
;
668 if (name
[0] && !name
[1]) {
670 case 'y': return &symbol_yes
;
671 case 'm': return &symbol_mod
;
672 case 'n': return &symbol_no
;
675 for (ptr
= name
; *ptr
; ptr
++)
679 for (symbol
= symbol_hash
[hash
]; symbol
; symbol
= symbol
->next
) {
680 if (!strcmp(symbol
->name
, name
)) {
681 if ((isconst
&& symbol
->flags
& SYMBOL_CONST
) ||
682 (!isconst
&& !(symbol
->flags
& SYMBOL_CONST
)))
686 new_name
= strdup(name
);
692 symbol
= malloc(sizeof(*symbol
));
693 memset(symbol
, 0, sizeof(*symbol
));
694 symbol
->name
= new_name
;
695 symbol
->type
= S_UNKNOWN
;
696 symbol
->flags
= SYMBOL_NEW
;
698 symbol
->flags
|= SYMBOL_CONST
;
700 symbol
->next
= symbol_hash
[hash
];
701 symbol_hash
[hash
] = symbol
;
706 struct symbol
*sym_find(const char *name
)
708 struct symbol
*symbol
= NULL
;
715 if (name
[0] && !name
[1]) {
717 case 'y': return &symbol_yes
;
718 case 'm': return &symbol_mod
;
719 case 'n': return &symbol_no
;
722 for (ptr
= name
; *ptr
; ptr
++)
726 for (symbol
= symbol_hash
[hash
]; symbol
; symbol
= symbol
->next
) {
727 if (!strcmp(symbol
->name
, name
) &&
728 !(symbol
->flags
& SYMBOL_CONST
))
735 struct symbol
**sym_re_search(const char *pattern
)
737 struct symbol
*sym
, **sym_arr
= NULL
;
743 if (strlen(pattern
) == 0)
745 if (regcomp(&re
, pattern
, REG_EXTENDED
|REG_NOSUB
|REG_ICASE
))
748 for_all_symbols(i
, sym
) {
749 if (sym
->flags
& SYMBOL_CONST
|| !sym
->name
)
751 if (regexec(&re
, sym
->name
, 0, NULL
, 0))
753 if (cnt
+ 1 >= size
) {
756 sym_arr
= realloc(sym_arr
, size
* sizeof(struct symbol
*));
763 sym_arr
[cnt
++] = sym
;
773 struct symbol
*sym_check_deps(struct symbol
*sym
);
775 static struct symbol
*sym_check_expr_deps(struct expr
*e
)
784 sym
= sym_check_expr_deps(e
->left
.expr
);
787 return sym_check_expr_deps(e
->right
.expr
);
789 return sym_check_expr_deps(e
->left
.expr
);
792 sym
= sym_check_deps(e
->left
.sym
);
795 return sym_check_deps(e
->right
.sym
);
797 return sym_check_deps(e
->left
.sym
);
801 printf("Oops! How to check %d?\n", e
->type
);
805 struct symbol
*sym_check_deps(struct symbol
*sym
)
808 struct property
*prop
;
810 if (sym
->flags
& SYMBOL_CHECK
) {
811 printf("Warning! Found recursive dependency: %s", sym
->name
);
814 if (sym
->flags
& SYMBOL_CHECKED
)
817 sym
->flags
|= (SYMBOL_CHECK
| SYMBOL_CHECKED
);
818 sym2
= sym_check_expr_deps(sym
->rev_dep
.expr
);
822 for (prop
= sym
->prop
; prop
; prop
= prop
->next
) {
823 if (prop
->type
== P_CHOICE
|| prop
->type
== P_SELECT
|| prop
->type
== P_DESELECT
)
825 sym2
= sym_check_expr_deps(prop
->visible
.expr
);
828 if (prop
->type
!= P_DEFAULT
|| sym_is_choice(sym
))
830 sym2
= sym_check_expr_deps(prop
->expr
);
836 printf(" %s", sym
->name
);
842 sym
->flags
&= ~SYMBOL_CHECK
;
846 struct property
*prop_alloc(enum prop_type type
, struct symbol
*sym
)
848 struct property
*prop
;
849 struct property
**propp
;
851 prop
= malloc(sizeof(*prop
));
852 memset(prop
, 0, sizeof(*prop
));
855 prop
->file
= current_file
;
856 prop
->lineno
= zconf_lineno();
858 /* append property to the prop list of symbol */
860 for (propp
= &sym
->prop
; *propp
; propp
= &(*propp
)->next
)
868 struct symbol
*prop_get_symbol(struct property
*prop
)
870 if (prop
->expr
&& (prop
->expr
->type
== E_SYMBOL
||
871 prop
->expr
->type
== E_CHOICE
))
872 return prop
->expr
->left
.sym
;
876 const char *prop_get_type_name(enum prop_type type
)
This page took 0.094973 seconds and 5 git commands to generate.