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));
47 static bool inited = false;
53 sym = sym_lookup("VERSION", 0);
55 sym->flags |= SYMBOL_AUTO;
56 p = getenv("VERSION");
58 sym_add_default(sym, p);
60 sym = sym_lookup("TARGET_ARCH", 0);
62 sym->flags |= SYMBOL_AUTO;
63 p = getenv("TARGET_ARCH");
65 sym_add_default(sym, p);
69 enum symbol_type sym_get_type(struct symbol *sym)
71 enum symbol_type type = sym->type;
73 if (type == S_TRISTATE) {
74 if (sym_is_choice_value(sym) && sym->visible == yes)
76 else if (modules_val == no)
82 const char *sym_type_name(enum symbol_type type)
103 struct property *sym_get_choice_prop(struct symbol *sym)
105 struct property *prop;
107 for_all_choices(sym, prop)
112 struct property *sym_get_default_prop(struct symbol *sym)
114 struct property *prop;
116 for_all_defaults(sym, prop) {
117 prop->visible.tri = expr_calc_value(prop->visible.expr);
118 if (prop->visible.tri != no)
124 struct property *sym_get_range_prop(struct symbol *sym)
126 struct property *prop;
128 for_all_properties(sym, prop, P_RANGE) {
129 prop->visible.tri = expr_calc_value(prop->visible.expr);
130 if (prop->visible.tri != no)
136 static void sym_calc_visibility(struct symbol *sym)
138 struct property *prop;
141 /* any prompt visible? */
143 for_all_prompts(sym, prop) {
144 prop->visible.tri = expr_calc_value(prop->visible.expr);
145 tri = E_OR(tri, prop->visible.tri);
147 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
149 if (sym->visible != tri) {
151 sym_set_changed(sym);
153 if (sym_is_choice_value(sym))
156 if (sym->rev_dep.expr)
157 tri = expr_calc_value(sym->rev_dep.expr);
158 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
160 if (sym->rev_dep.tri != tri) {
161 sym->rev_dep.tri = tri;
162 sym_set_changed(sym);
166 static struct symbol *sym_calc_choice(struct symbol *sym)
168 struct symbol *def_sym;
169 struct property *prop;
172 /* is the user choice visible? */
173 def_sym = sym->user.val;
175 sym_calc_visibility(def_sym);
176 if (def_sym->visible != no)
180 /* any of the defaults visible? */
181 for_all_defaults(sym, prop) {
182 prop->visible.tri = expr_calc_value(prop->visible.expr);
183 if (prop->visible.tri == no)
185 def_sym = prop_get_symbol(prop);
186 sym_calc_visibility(def_sym);
187 if (def_sym->visible != no)
191 /* just get the first visible value */
192 prop = sym_get_choice_prop(sym);
193 for (e = prop->expr; e; e = e->left.expr) {
194 def_sym = e->right.sym;
195 sym_calc_visibility(def_sym);
196 if (def_sym->visible != no)
200 /* no choice? reset tristate value */
205 void sym_calc_value(struct symbol *sym)
207 struct symbol_value newval, oldval;
208 struct property *prop;
214 if (sym->flags & SYMBOL_VALID)
216 sym->flags |= SYMBOL_VALID;
224 newval = symbol_empty.curr;
228 newval = symbol_no.curr;
231 sym->curr.val = sym->name;
235 if (!sym_is_choice_value(sym))
236 sym->flags &= ~SYMBOL_WRITE;
238 sym_calc_visibility(sym);
240 /* set default if recursively called */
243 switch (sym_get_type(sym)) {
246 if (sym_is_choice_value(sym) && sym->visible == yes) {
247 prop = sym_get_choice_prop(sym);
248 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
249 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
250 sym->flags |= SYMBOL_WRITE;
251 if (sym_has_value(sym))
252 newval.tri = sym->user.tri;
253 else if (!sym_is_choice(sym)) {
254 prop = sym_get_default_prop(sym);
256 newval.tri = expr_calc_value(prop->expr);
258 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
259 } else if (!sym_is_choice(sym)) {
260 prop = sym_get_default_prop(sym);
262 sym->flags |= SYMBOL_WRITE;
263 newval.tri = expr_calc_value(prop->expr);
266 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
272 if (sym->visible != no) {
273 sym->flags |= SYMBOL_WRITE;
274 if (sym_has_value(sym)) {
275 newval.val = sym->user.val;
279 prop = sym_get_default_prop(sym);
281 struct symbol *ds = prop_get_symbol(prop);
283 sym->flags |= SYMBOL_WRITE;
285 newval.val = ds->curr.val;
294 if (sym_is_choice(sym) && newval.tri == yes)
295 sym->curr.val = sym_calc_choice(sym);
297 if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
298 sym_set_changed(sym);
299 if (modules_sym == sym)
300 modules_val = modules_sym->curr.tri;
302 if (sym_is_choice(sym)) {
303 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
304 prop = sym_get_choice_prop(sym);
305 for (e = prop->expr; e; e = e->left.expr) {
306 e->right.sym->flags |= flags;
307 if (flags & SYMBOL_CHANGED)
308 sym_set_changed(e->right.sym);
313 void sym_clear_all_valid(void)
318 for_all_symbols(i, sym)
319 sym->flags &= ~SYMBOL_VALID;
322 sym_calc_value(modules_sym);
325 void sym_set_changed(struct symbol *sym)
327 struct property *prop;
329 sym->flags |= SYMBOL_CHANGED;
330 for (prop = sym->prop; prop; prop = prop->next) {
332 prop->menu->flags |= MENU_CHANGED;
336 void sym_set_all_changed(void)
341 for_all_symbols(i, sym)
342 sym_set_changed(sym);
345 bool sym_tristate_within_range(struct symbol *sym, tristate val)
347 int type = sym_get_type(sym);
349 if (sym->visible == no)
352 if (type != S_BOOLEAN && type != S_TRISTATE)
355 if (type == S_BOOLEAN && val == mod)
357 if (sym->visible <= sym->rev_dep.tri)
359 if (sym_is_choice_value(sym) && sym->visible == yes)
361 return val >= sym->rev_dep.tri && val <= sym->visible;
364 bool sym_set_tristate_value(struct symbol *sym, tristate val)
366 tristate oldval = sym_get_tristate_value(sym);
368 if (oldval != val && !sym_tristate_within_range(sym, val))
371 if (sym->flags & SYMBOL_NEW) {
372 sym->flags &= ~SYMBOL_NEW;
373 sym_set_changed(sym);
375 if (sym_is_choice_value(sym) && val == yes) {
376 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
379 cs->flags &= ~SYMBOL_NEW;
384 sym_clear_all_valid();
385 if (sym == modules_sym)
386 sym_set_all_changed();
392 tristate sym_toggle_tristate_value(struct symbol *sym)
394 tristate oldval, newval;
396 oldval = newval = sym_get_tristate_value(sym);
409 if (sym_set_tristate_value(sym, newval))
411 } while (oldval != newval);
415 bool sym_string_valid(struct symbol *sym, const char *str)
428 if (ch == '0' && *str != 0)
430 while ((ch = *str++)) {
436 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
442 } while ((ch = *str++));
458 bool sym_string_within_range(struct symbol *sym, const char *str)
460 struct property *prop;
465 return sym_string_valid(sym, str);
467 if (!sym_string_valid(sym, str))
469 prop = sym_get_range_prop(sym);
472 val = strtol(str, NULL, 10);
473 return val >= strtol(prop->expr->left.sym->name, NULL, 10) &&
474 val <= strtol(prop->expr->right.sym->name, NULL, 10);
476 if (!sym_string_valid(sym, str))
478 prop = sym_get_range_prop(sym);
481 val = strtol(str, NULL, 16);
482 return val >= strtol(prop->expr->left.sym->name, NULL, 16) &&
483 val <= strtol(prop->expr->right.sym->name, NULL, 16);
488 return sym_tristate_within_range(sym, yes);
490 return sym_tristate_within_range(sym, mod);
492 return sym_tristate_within_range(sym, no);
500 bool sym_set_string_value(struct symbol *sym, const char *newval)
511 return sym_set_tristate_value(sym, yes);
513 return sym_set_tristate_value(sym, mod);
515 return sym_set_tristate_value(sym, no);
522 if (!sym_string_within_range(sym, newval))
525 if (sym->flags & SYMBOL_NEW) {
526 sym->flags &= ~SYMBOL_NEW;
527 sym_set_changed(sym);
530 oldval = sym->user.val;
531 size = strlen(newval) + 1;
532 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
534 sym->user.val = val = malloc(size);
537 } else if (!oldval || strcmp(oldval, newval))
538 sym->user.val = val = malloc(size);
543 free((void *)oldval);
544 sym_clear_all_valid();
549 const char *sym_get_string_value(struct symbol *sym)
556 val = sym_get_tristate_value(sym);
569 return (const char *)sym->curr.val;
572 bool sym_is_changable(struct symbol *sym)
574 return sym->visible > sym->rev_dep.tri;
577 struct symbol *sym_lookup(const char *name, int isconst)
579 struct symbol *symbol;
585 if (name[0] && !name[1]) {
587 case 'y': return &symbol_yes;
588 case 'm': return &symbol_mod;
589 case 'n': return &symbol_no;
592 for (ptr = name; *ptr; ptr++)
596 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
597 if (!strcmp(symbol->name, name)) {
598 if ((isconst && symbol->flags & SYMBOL_CONST) ||
599 (!isconst && !(symbol->flags & SYMBOL_CONST)))
603 new_name = strdup(name);
609 symbol = malloc(sizeof(*symbol));
610 memset(symbol, 0, sizeof(*symbol));
611 symbol->name = new_name;
612 symbol->type = S_UNKNOWN;
613 symbol->flags = SYMBOL_NEW;
615 symbol->flags |= SYMBOL_CONST;
617 symbol->next = symbol_hash[hash];
618 symbol_hash[hash] = symbol;
623 struct symbol *sym_find(const char *name)
625 struct symbol *symbol = NULL;
632 if (name[0] && !name[1]) {
634 case 'y': return &symbol_yes;
635 case 'm': return &symbol_mod;
636 case 'n': return &symbol_no;
639 for (ptr = name; *ptr; ptr++)
643 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
644 if (!strcmp(symbol->name, name) &&
645 !(symbol->flags & SYMBOL_CONST))
652 struct symbol *sym_check_deps(struct symbol *sym);
654 static struct symbol *sym_check_expr_deps(struct expr *e)
663 sym = sym_check_expr_deps(e->left.expr);
666 return sym_check_expr_deps(e->right.expr);
668 return sym_check_expr_deps(e->left.expr);
671 sym = sym_check_deps(e->left.sym);
674 return sym_check_deps(e->right.sym);
676 return sym_check_deps(e->left.sym);
680 printf("Oops! How to check %d?\n", e->type);
684 struct symbol *sym_check_deps(struct symbol *sym)
687 struct property *prop;
689 if (sym->flags & SYMBOL_CHECK_DONE)
691 if (sym->flags & SYMBOL_CHECK) {
692 printf("Warning! Found recursive dependency: %s", sym->name);
696 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
697 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
701 for (prop = sym->prop; prop; prop = prop->next) {
702 if (prop->type == P_CHOICE || prop->type == P_SELECT)
704 sym2 = sym_check_expr_deps(prop->visible.expr);
707 if (prop->type != P_DEFAULT || sym_is_choice(sym))
709 sym2 = sym_check_expr_deps(prop->expr);
715 printf(" %s", sym->name);
716 sym->flags &= ~SYMBOL_CHECK;
720 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
722 struct property *prop;
723 struct property **propp;
725 prop = malloc(sizeof(*prop));
726 memset(prop, 0, sizeof(*prop));
729 prop->file = current_file;
730 prop->lineno = zconf_lineno();
732 /* append property to the prop list of symbol */
734 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
742 struct symbol *prop_get_symbol(struct property *prop)
744 if (prop->expr && (prop->expr->type == E_SYMBOL ||
745 prop->expr->type == E_CHOICE))
746 return prop->expr->left.sym;
750 const char *prop_get_type_name(enum prop_type type)