2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
9 #include <sys/utsname.h>
11 #define LKC_DIRECT_LINK
14 struct symbol symbol_yes = {
17 .flags = SYMBOL_YES|SYMBOL_VALID,
21 .flags = SYMBOL_MOD|SYMBOL_VALID,
25 .flags = SYMBOL_NO|SYMBOL_VALID,
29 .flags = SYMBOL_VALID,
33 struct symbol *modules_sym;
36 void sym_add_default(struct symbol *sym, const char *def)
38 struct property *prop = prop_alloc(P_DEFAULT, sym);
40 prop->expr = expr_alloc_symbol(sym_lookup(def, 1));
48 static bool inited = false;
56 sym = sym_lookup("ARCH", 0);
58 sym->flags |= SYMBOL_AUTO;
61 sym_add_default(sym, p);
63 sym = sym_lookup("KERNELRELEASE", 0);
65 sym->flags |= SYMBOL_AUTO;
66 p = getenv("KERNELRELEASE");
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 else if (modules_val == no)
89 const char *sym_type_name(enum symbol_type type)
110 struct property *sym_get_choice_prop(struct symbol *sym)
112 struct property *prop;
114 for_all_choices(sym, prop)
119 struct property *sym_get_default_prop(struct symbol *sym)
121 struct property *prop;
123 for_all_defaults(sym, prop) {
124 prop->visible.tri = expr_calc_value(prop->visible.expr);
125 if (prop->visible.tri != no)
131 struct property *sym_get_range_prop(struct symbol *sym)
133 struct property *prop;
135 for_all_properties(sym, prop, P_RANGE) {
136 prop->visible.tri = expr_calc_value(prop->visible.expr);
137 if (prop->visible.tri != no)
143 static void sym_calc_visibility(struct symbol *sym)
145 struct property *prop;
148 /* any prompt visible? */
150 for_all_prompts(sym, prop) {
151 prop->visible.tri = expr_calc_value(prop->visible.expr);
152 tri = E_OR(tri, prop->visible.tri);
154 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
156 if (sym->visible != tri) {
158 sym_set_changed(sym);
160 if (sym_is_choice_value(sym))
163 if (sym->rev_dep.expr)
164 tri = expr_calc_value(sym->rev_dep.expr);
165 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
167 if (sym->rev_dep.tri != tri) {
168 sym->rev_dep.tri = tri;
169 sym_set_changed(sym);
173 static struct symbol *sym_calc_choice(struct symbol *sym)
175 struct symbol *def_sym;
176 struct property *prop;
179 /* is the user choice visible? */
180 def_sym = sym->user.val;
182 sym_calc_visibility(def_sym);
183 if (def_sym->visible != no)
187 /* any of the defaults visible? */
188 for_all_defaults(sym, prop) {
189 prop->visible.tri = expr_calc_value(prop->visible.expr);
190 if (prop->visible.tri == no)
192 def_sym = prop_get_symbol(prop);
193 sym_calc_visibility(def_sym);
194 if (def_sym->visible != no)
198 /* just get the first visible value */
199 prop = sym_get_choice_prop(sym);
200 for (e = prop->expr; e; e = e->left.expr) {
201 def_sym = e->right.sym;
202 sym_calc_visibility(def_sym);
203 if (def_sym->visible != no)
207 /* no choice? reset tristate value */
212 void sym_calc_value(struct symbol *sym)
214 struct symbol_value newval, oldval;
215 struct property *prop;
221 if (sym->flags & SYMBOL_VALID)
223 sym->flags |= SYMBOL_VALID;
231 newval = symbol_empty.curr;
235 newval = symbol_no.curr;
238 sym->curr.val = sym->name;
242 if (!sym_is_choice_value(sym))
243 sym->flags &= ~SYMBOL_WRITE;
245 sym_calc_visibility(sym);
247 /* set default if recursively called */
250 switch (sym_get_type(sym)) {
253 if (sym_is_choice_value(sym) && sym->visible == yes) {
254 prop = sym_get_choice_prop(sym);
255 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
256 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
257 sym->flags |= SYMBOL_WRITE;
258 if (sym_has_value(sym))
259 newval.tri = sym->user.tri;
260 else if (!sym_is_choice(sym)) {
261 prop = sym_get_default_prop(sym);
263 newval.tri = expr_calc_value(prop->expr);
265 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
266 } else if (!sym_is_choice(sym)) {
267 prop = sym_get_default_prop(sym);
269 sym->flags |= SYMBOL_WRITE;
270 newval.tri = expr_calc_value(prop->expr);
273 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
279 if (sym->visible != no) {
280 sym->flags |= SYMBOL_WRITE;
281 if (sym_has_value(sym)) {
282 newval.val = sym->user.val;
286 prop = sym_get_default_prop(sym);
288 struct symbol *ds = prop_get_symbol(prop);
290 sym->flags |= SYMBOL_WRITE;
292 newval.val = ds->curr.val;
301 if (sym_is_choice(sym) && newval.tri == yes)
302 sym->curr.val = sym_calc_choice(sym);
304 if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
305 sym_set_changed(sym);
306 if (modules_sym == sym)
307 modules_val = modules_sym->curr.tri;
309 if (sym_is_choice(sym)) {
310 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
311 prop = sym_get_choice_prop(sym);
312 for (e = prop->expr; e; e = e->left.expr) {
313 e->right.sym->flags |= flags;
314 if (flags & SYMBOL_CHANGED)
315 sym_set_changed(e->right.sym);
320 void sym_clear_all_valid(void)
325 for_all_symbols(i, sym)
326 sym->flags &= ~SYMBOL_VALID;
329 sym_calc_value(modules_sym);
332 void sym_set_changed(struct symbol *sym)
334 struct property *prop;
336 sym->flags |= SYMBOL_CHANGED;
337 for (prop = sym->prop; prop; prop = prop->next) {
339 prop->menu->flags |= MENU_CHANGED;
343 void sym_set_all_changed(void)
348 for_all_symbols(i, sym)
349 sym_set_changed(sym);
352 bool sym_tristate_within_range(struct symbol *sym, tristate val)
354 int type = sym_get_type(sym);
356 if (sym->visible == no)
359 if (type != S_BOOLEAN && type != S_TRISTATE)
362 if (type == S_BOOLEAN && val == mod)
364 if (sym->visible <= sym->rev_dep.tri)
366 if (sym_is_choice_value(sym) && sym->visible == yes)
368 return val >= sym->rev_dep.tri && val <= sym->visible;
371 bool sym_set_tristate_value(struct symbol *sym, tristate val)
373 tristate oldval = sym_get_tristate_value(sym);
375 if (oldval != val && !sym_tristate_within_range(sym, val))
378 if (sym->flags & SYMBOL_NEW) {
379 sym->flags &= ~SYMBOL_NEW;
380 sym_set_changed(sym);
382 if (sym_is_choice_value(sym) && val == yes) {
383 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
386 cs->flags &= ~SYMBOL_NEW;
391 sym_clear_all_valid();
392 if (sym == modules_sym)
393 sym_set_all_changed();
399 tristate sym_toggle_tristate_value(struct symbol *sym)
401 tristate oldval, newval;
403 oldval = newval = sym_get_tristate_value(sym);
416 if (sym_set_tristate_value(sym, newval))
418 } while (oldval != newval);
422 bool sym_string_valid(struct symbol *sym, const char *str)
435 if (ch == '0' && *str != 0)
437 while ((ch = *str++)) {
443 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
449 } while ((ch = *str++));
465 bool sym_string_within_range(struct symbol *sym, const char *str)
467 struct property *prop;
472 return sym_string_valid(sym, str);
474 if (!sym_string_valid(sym, str))
476 prop = sym_get_range_prop(sym);
479 val = strtol(str, NULL, 10);
480 return val >= strtol(prop->expr->left.sym->name, NULL, 10) &&
481 val <= strtol(prop->expr->right.sym->name, NULL, 10);
483 if (!sym_string_valid(sym, str))
485 prop = sym_get_range_prop(sym);
488 val = strtol(str, NULL, 16);
489 return val >= strtol(prop->expr->left.sym->name, NULL, 16) &&
490 val <= strtol(prop->expr->right.sym->name, NULL, 16);
495 return sym_tristate_within_range(sym, yes);
497 return sym_tristate_within_range(sym, mod);
499 return sym_tristate_within_range(sym, no);
507 bool sym_set_string_value(struct symbol *sym, const char *newval)
518 return sym_set_tristate_value(sym, yes);
520 return sym_set_tristate_value(sym, mod);
522 return sym_set_tristate_value(sym, no);
529 if (!sym_string_within_range(sym, newval))
532 if (sym->flags & SYMBOL_NEW) {
533 sym->flags &= ~SYMBOL_NEW;
534 sym_set_changed(sym);
537 oldval = sym->user.val;
538 size = strlen(newval) + 1;
539 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
541 sym->user.val = val = malloc(size);
544 } else if (!oldval || strcmp(oldval, newval))
545 sym->user.val = val = malloc(size);
550 free((void *)oldval);
551 sym_clear_all_valid();
556 const char *sym_get_string_value(struct symbol *sym)
563 val = sym_get_tristate_value(sym);
576 return (const char *)sym->curr.val;
579 bool sym_is_changable(struct symbol *sym)
581 return sym->visible > sym->rev_dep.tri;
584 struct symbol *sym_lookup(const char *name, int isconst)
586 struct symbol *symbol;
592 if (name[0] && !name[1]) {
594 case 'y': return &symbol_yes;
595 case 'm': return &symbol_mod;
596 case 'n': return &symbol_no;
599 for (ptr = name; *ptr; ptr++)
603 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
604 if (!strcmp(symbol->name, name)) {
605 if ((isconst && symbol->flags & SYMBOL_CONST) ||
606 (!isconst && !(symbol->flags & SYMBOL_CONST)))
610 new_name = strdup(name);
616 symbol = malloc(sizeof(*symbol));
617 memset(symbol, 0, sizeof(*symbol));
618 symbol->name = new_name;
619 symbol->type = S_UNKNOWN;
620 symbol->flags = SYMBOL_NEW;
622 symbol->flags |= SYMBOL_CONST;
624 symbol->next = symbol_hash[hash];
625 symbol_hash[hash] = symbol;
630 struct symbol *sym_find(const char *name)
632 struct symbol *symbol = NULL;
639 if (name[0] && !name[1]) {
641 case 'y': return &symbol_yes;
642 case 'm': return &symbol_mod;
643 case 'n': return &symbol_no;
646 for (ptr = name; *ptr; ptr++)
650 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
651 if (!strcmp(symbol->name, name) &&
652 !(symbol->flags & SYMBOL_CONST))
659 struct symbol *sym_check_deps(struct symbol *sym);
661 static struct symbol *sym_check_expr_deps(struct expr *e)
670 sym = sym_check_expr_deps(e->left.expr);
673 return sym_check_expr_deps(e->right.expr);
675 return sym_check_expr_deps(e->left.expr);
678 sym = sym_check_deps(e->left.sym);
681 return sym_check_deps(e->right.sym);
683 return sym_check_deps(e->left.sym);
687 printf("Oops! How to check %d?\n", e->type);
691 struct symbol *sym_check_deps(struct symbol *sym)
694 struct property *prop;
696 if (sym->flags & SYMBOL_CHECK_DONE)
698 if (sym->flags & SYMBOL_CHECK) {
699 printf("Warning! Found recursive dependency: %s", sym->name);
703 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
704 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
708 for (prop = sym->prop; prop; prop = prop->next) {
709 if (prop->type == P_CHOICE || prop->type == P_SELECT)
711 sym2 = sym_check_expr_deps(prop->visible.expr);
714 if (prop->type != P_DEFAULT || sym_is_choice(sym))
716 sym2 = sym_check_expr_deps(prop->expr);
722 printf(" %s", sym->name);
723 sym->flags &= ~SYMBOL_CHECK;
727 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
729 struct property *prop;
730 struct property **propp;
732 prop = malloc(sizeof(*prop));
733 memset(prop, 0, sizeof(*prop));
736 prop->file = current_file;
737 prop->lineno = zconf_lineno();
739 /* append property to the prop list of symbol */
741 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
749 struct symbol *prop_get_symbol(struct property *prop)
751 if (prop->expr && (prop->expr->type == E_SYMBOL ||
752 prop->expr->type == E_CHOICE))
753 return prop->expr->left.sym;
757 const char *prop_get_type_name(enum prop_type type)