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));
48 static bool inited = false;
54 sym = sym_lookup("VERSION", 0);
56 sym->flags |= SYMBOL_AUTO;
57 p = getenv("VERSION");
59 sym_add_default(sym, p);
61 sym = sym_lookup("TARGET_ARCH", 0);
63 sym->flags |= SYMBOL_AUTO;
64 p = getenv("TARGET_ARCH");
66 sym_add_default(sym, p);
70 enum symbol_type sym_get_type(struct symbol *sym)
72 enum symbol_type type = sym->type;
74 if (type == S_TRISTATE) {
75 if (sym_is_choice_value(sym) && sym->visible == yes)
77 else if (modules_val == no)
83 const char *sym_type_name(enum symbol_type type)
104 struct property *sym_get_choice_prop(struct symbol *sym)
106 struct property *prop;
108 for_all_choices(sym, prop)
113 struct property *sym_get_default_prop(struct symbol *sym)
115 struct property *prop;
117 for_all_defaults(sym, prop) {
118 prop->visible.tri = expr_calc_value(prop->visible.expr);
119 if (prop->visible.tri != no)
125 struct property *sym_get_range_prop(struct symbol *sym)
127 struct property *prop;
129 for_all_properties(sym, prop, P_RANGE) {
130 prop->visible.tri = expr_calc_value(prop->visible.expr);
131 if (prop->visible.tri != no)
137 static void sym_calc_visibility(struct symbol *sym)
139 struct property *prop;
142 /* any prompt visible? */
144 for_all_prompts(sym, prop) {
145 prop->visible.tri = expr_calc_value(prop->visible.expr);
146 tri = E_OR(tri, prop->visible.tri);
148 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
150 if (sym->visible != tri) {
152 sym_set_changed(sym);
154 if (sym_is_choice_value(sym))
157 if (sym->rev_dep.expr)
158 tri = expr_calc_value(sym->rev_dep.expr);
159 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
161 if (sym->rev_dep.tri != tri) {
162 sym->rev_dep.tri = tri;
163 sym_set_changed(sym);
167 static struct symbol *sym_calc_choice(struct symbol *sym)
169 struct symbol *def_sym;
170 struct property *prop;
173 /* is the user choice visible? */
174 def_sym = sym->user.val;
176 sym_calc_visibility(def_sym);
177 if (def_sym->visible != no)
181 /* any of the defaults visible? */
182 for_all_defaults(sym, prop) {
183 prop->visible.tri = expr_calc_value(prop->visible.expr);
184 if (prop->visible.tri == no)
186 def_sym = prop_get_symbol(prop);
187 sym_calc_visibility(def_sym);
188 if (def_sym->visible != no)
192 /* just get the first visible value */
193 prop = sym_get_choice_prop(sym);
194 for (e = prop->expr; e; e = e->left.expr) {
195 def_sym = e->right.sym;
196 sym_calc_visibility(def_sym);
197 if (def_sym->visible != no)
201 /* no choice? reset tristate value */
206 void sym_calc_value(struct symbol *sym)
208 struct symbol_value newval, oldval;
209 struct property *prop;
215 if (sym->flags & SYMBOL_VALID)
217 sym->flags |= SYMBOL_VALID;
225 newval = symbol_empty.curr;
229 newval = symbol_no.curr;
232 sym->curr.val = sym->name;
236 if (!sym_is_choice_value(sym))
237 sym->flags &= ~SYMBOL_WRITE;
239 sym_calc_visibility(sym);
241 /* set default if recursively called */
244 switch (sym_get_type(sym)) {
247 if (sym_is_choice_value(sym) && sym->visible == yes) {
248 prop = sym_get_choice_prop(sym);
249 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
250 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
251 sym->flags |= SYMBOL_WRITE;
252 if (sym_has_value(sym))
253 newval.tri = sym->user.tri;
254 else if (!sym_is_choice(sym)) {
255 prop = sym_get_default_prop(sym);
257 newval.tri = expr_calc_value(prop->expr);
259 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
260 } else if (!sym_is_choice(sym)) {
261 prop = sym_get_default_prop(sym);
263 sym->flags |= SYMBOL_WRITE;
264 newval.tri = expr_calc_value(prop->expr);
267 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
273 if (sym->visible != no) {
274 sym->flags |= SYMBOL_WRITE;
275 if (sym_has_value(sym)) {
276 newval.val = sym->user.val;
280 prop = sym_get_default_prop(sym);
282 struct symbol *ds = prop_get_symbol(prop);
284 sym->flags |= SYMBOL_WRITE;
286 newval.val = ds->curr.val;
295 if (sym_is_choice(sym) && newval.tri == yes)
296 sym->curr.val = sym_calc_choice(sym);
298 if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
299 sym_set_changed(sym);
300 if (modules_sym == sym)
301 modules_val = modules_sym->curr.tri;
303 if (sym_is_choice(sym)) {
304 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
305 prop = sym_get_choice_prop(sym);
306 for (e = prop->expr; e; e = e->left.expr) {
307 e->right.sym->flags |= flags;
308 if (flags & SYMBOL_CHANGED)
309 sym_set_changed(e->right.sym);
314 void sym_clear_all_valid(void)
319 for_all_symbols(i, sym)
320 sym->flags &= ~SYMBOL_VALID;
323 sym_calc_value(modules_sym);
326 void sym_set_changed(struct symbol *sym)
328 struct property *prop;
330 sym->flags |= SYMBOL_CHANGED;
331 for (prop = sym->prop; prop; prop = prop->next) {
333 prop->menu->flags |= MENU_CHANGED;
337 void sym_set_all_changed(void)
342 for_all_symbols(i, sym)
343 sym_set_changed(sym);
346 bool sym_tristate_within_range(struct symbol *sym, tristate val)
348 int type = sym_get_type(sym);
350 if (sym->visible == no)
353 if (type != S_BOOLEAN && type != S_TRISTATE)
356 if (type == S_BOOLEAN && val == mod)
358 if (sym->visible <= sym->rev_dep.tri)
360 if (sym_is_choice_value(sym) && sym->visible == yes)
362 return val >= sym->rev_dep.tri && val <= sym->visible;
365 bool sym_set_tristate_value(struct symbol *sym, tristate val)
367 tristate oldval = sym_get_tristate_value(sym);
369 if (oldval != val && !sym_tristate_within_range(sym, val))
372 if (sym->flags & SYMBOL_NEW) {
373 sym->flags &= ~SYMBOL_NEW;
374 sym_set_changed(sym);
376 if (sym_is_choice_value(sym) && val == yes) {
377 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
380 cs->flags &= ~SYMBOL_NEW;
385 sym_clear_all_valid();
386 if (sym == modules_sym)
387 sym_set_all_changed();
393 tristate sym_toggle_tristate_value(struct symbol *sym)
395 tristate oldval, newval;
397 oldval = newval = sym_get_tristate_value(sym);
410 if (sym_set_tristate_value(sym, newval))
412 } while (oldval != newval);
416 bool sym_string_valid(struct symbol *sym, const char *str)
429 if (ch == '0' && *str != 0)
431 while ((ch = *str++)) {
437 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
443 } while ((ch = *str++));
459 bool sym_string_within_range(struct symbol *sym, const char *str)
461 struct property *prop;
466 return sym_string_valid(sym, str);
468 if (!sym_string_valid(sym, str))
470 prop = sym_get_range_prop(sym);
473 val = strtol(str, NULL, 10);
474 return val >= strtol(prop->expr->left.sym->name, NULL, 10) &&
475 val <= strtol(prop->expr->right.sym->name, NULL, 10);
477 if (!sym_string_valid(sym, str))
479 prop = sym_get_range_prop(sym);
482 val = strtol(str, NULL, 16);
483 return val >= strtol(prop->expr->left.sym->name, NULL, 16) &&
484 val <= strtol(prop->expr->right.sym->name, NULL, 16);
489 return sym_tristate_within_range(sym, yes);
491 return sym_tristate_within_range(sym, mod);
493 return sym_tristate_within_range(sym, no);
501 bool sym_set_string_value(struct symbol *sym, const char *newval)
512 return sym_set_tristate_value(sym, yes);
514 return sym_set_tristate_value(sym, mod);
516 return sym_set_tristate_value(sym, no);
523 if (!sym_string_within_range(sym, newval))
526 if (sym->flags & SYMBOL_NEW) {
527 sym->flags &= ~SYMBOL_NEW;
528 sym_set_changed(sym);
531 oldval = sym->user.val;
532 size = strlen(newval) + 1;
533 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
535 sym->user.val = val = malloc(size);
538 } else if (!oldval || strcmp(oldval, newval))
539 sym->user.val = val = malloc(size);
544 free((void *)oldval);
545 sym_clear_all_valid();
550 const char *sym_get_string_value(struct symbol *sym)
557 val = sym_get_tristate_value(sym);
570 return (const char *)sym->curr.val;
573 bool sym_is_changable(struct symbol *sym)
575 return sym->visible > sym->rev_dep.tri;
578 struct symbol *sym_lookup(const char *name, int isconst)
580 struct symbol *symbol;
586 if (name[0] && !name[1]) {
588 case 'y': return &symbol_yes;
589 case 'm': return &symbol_mod;
590 case 'n': return &symbol_no;
593 for (ptr = name; *ptr; ptr++)
597 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
598 if (!strcmp(symbol->name, name)) {
599 if ((isconst && symbol->flags & SYMBOL_CONST) ||
600 (!isconst && !(symbol->flags & SYMBOL_CONST)))
604 new_name = strdup(name);
610 symbol = malloc(sizeof(*symbol));
611 memset(symbol, 0, sizeof(*symbol));
612 symbol->name = new_name;
613 symbol->type = S_UNKNOWN;
614 symbol->flags = SYMBOL_NEW;
616 symbol->flags |= SYMBOL_CONST;
618 symbol->next = symbol_hash[hash];
619 symbol_hash[hash] = symbol;
624 struct symbol *sym_find(const char *name)
626 struct symbol *symbol = NULL;
633 if (name[0] && !name[1]) {
635 case 'y': return &symbol_yes;
636 case 'm': return &symbol_mod;
637 case 'n': return &symbol_no;
640 for (ptr = name; *ptr; ptr++)
644 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
645 if (!strcmp(symbol->name, name) &&
646 !(symbol->flags & SYMBOL_CONST))
653 struct symbol **sym_re_search(const char *pattern)
655 struct symbol *sym, **sym_arr = NULL;
661 if (strlen(pattern) == 0)
663 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
666 for_all_symbols(i, sym) {
667 if (sym->flags & SYMBOL_CONST || !sym->name)
669 if (regexec(&re, sym->name, 0, NULL, 0))
671 if (cnt + 1 >= size) {
674 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
680 sym_arr[cnt++] = sym;
690 struct symbol *sym_check_deps(struct symbol *sym);
692 static struct symbol *sym_check_expr_deps(struct expr *e)
701 sym = sym_check_expr_deps(e->left.expr);
704 return sym_check_expr_deps(e->right.expr);
706 return sym_check_expr_deps(e->left.expr);
709 sym = sym_check_deps(e->left.sym);
712 return sym_check_deps(e->right.sym);
714 return sym_check_deps(e->left.sym);
718 printf("Oops! How to check %d?\n", e->type);
722 struct symbol *sym_check_deps(struct symbol *sym)
725 struct property *prop;
727 if (sym->flags & SYMBOL_CHECK_DONE)
729 if (sym->flags & SYMBOL_CHECK) {
730 printf("Warning! Found recursive dependency: %s", sym->name);
734 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
735 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
739 for (prop = sym->prop; prop; prop = prop->next) {
740 if (prop->type == P_CHOICE || prop->type == P_SELECT)
742 sym2 = sym_check_expr_deps(prop->visible.expr);
745 if (prop->type != P_DEFAULT || sym_is_choice(sym))
747 sym2 = sym_check_expr_deps(prop->expr);
753 printf(" %s", sym->name);
754 sym->flags &= ~SYMBOL_CHECK;
758 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
760 struct property *prop;
761 struct property **propp;
763 prop = malloc(sizeof(*prop));
764 memset(prop, 0, sizeof(*prop));
767 prop->file = current_file;
768 prop->lineno = zconf_lineno();
770 /* append property to the prop list of symbol */
772 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
780 struct symbol *prop_get_symbol(struct property *prop)
782 if (prop->expr && (prop->expr->type == E_SYMBOL ||
783 prop->expr->type == E_CHOICE))
784 return prop->expr->left.sym;
788 const char *prop_get_type_name(enum prop_type type)