1 /* vi: set sw=4 ts=4: */
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 * Released under the terms of the GNU GPL v2.0.
11 #include <sys/utsname.h>
13 #define LKC_DIRECT_LINK
16 struct symbol symbol_yes = {
19 .flags = SYMBOL_YES|SYMBOL_VALID,
23 .flags = SYMBOL_MOD|SYMBOL_VALID,
27 .flags = SYMBOL_NO|SYMBOL_VALID,
31 .flags = SYMBOL_VALID,
35 struct symbol *modules_sym;
38 void sym_add_default(struct symbol *sym, const char *def)
40 struct property *prop = prop_alloc(P_DEFAULT, sym);
42 prop->expr = expr_alloc_symbol(sym_lookup(def, 1));
49 static bool inited = false;
55 sym = sym_lookup("VERSION", 0);
57 sym->flags |= SYMBOL_AUTO;
58 p = getenv("VERSION");
60 sym_add_default(sym, p);
62 sym = sym_lookup("TARGET_ARCH", 0);
64 sym->flags |= SYMBOL_AUTO;
65 p = getenv("TARGET_ARCH");
67 sym_add_default(sym, p);
71 enum symbol_type sym_get_type(struct symbol *sym)
73 enum symbol_type type = sym->type;
75 if (type == S_TRISTATE) {
76 if (sym_is_choice_value(sym) && sym->visible == yes)
78 else if (modules_val == no)
84 const char *sym_type_name(enum symbol_type type)
105 struct property *sym_get_choice_prop(struct symbol *sym)
107 struct property *prop;
109 for_all_choices(sym, prop)
114 struct property *sym_get_default_prop(struct symbol *sym)
116 struct property *prop;
118 for_all_defaults(sym, prop) {
119 prop->visible.tri = expr_calc_value(prop->visible.expr);
120 if (prop->visible.tri != no)
126 struct property *sym_get_range_prop(struct symbol *sym)
128 struct property *prop;
130 for_all_properties(sym, prop, P_RANGE) {
131 prop->visible.tri = expr_calc_value(prop->visible.expr);
132 if (prop->visible.tri != no)
138 static void sym_calc_visibility(struct symbol *sym)
140 struct property *prop;
143 /* any prompt visible? */
145 for_all_prompts(sym, prop) {
146 prop->visible.tri = expr_calc_value(prop->visible.expr);
147 tri = E_OR(tri, prop->visible.tri);
149 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
151 if (sym->visible != tri) {
153 sym_set_changed(sym);
155 if (sym_is_choice_value(sym))
158 if (sym->rev_dep.expr)
159 tri = expr_calc_value(sym->rev_dep.expr);
160 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
162 if (sym->rev_dep.tri != tri) {
163 sym->rev_dep.tri = tri;
164 sym_set_changed(sym);
168 static struct symbol *sym_calc_choice(struct symbol *sym)
170 struct symbol *def_sym;
171 struct property *prop;
174 /* is the user choice visible? */
175 def_sym = sym->user.val;
177 sym_calc_visibility(def_sym);
178 if (def_sym->visible != no)
182 /* any of the defaults visible? */
183 for_all_defaults(sym, prop) {
184 prop->visible.tri = expr_calc_value(prop->visible.expr);
185 if (prop->visible.tri == no)
187 def_sym = prop_get_symbol(prop);
188 sym_calc_visibility(def_sym);
189 if (def_sym->visible != no)
193 /* just get the first visible value */
194 prop = sym_get_choice_prop(sym);
195 for (e = prop->expr; e; e = e->left.expr) {
196 def_sym = e->right.sym;
197 sym_calc_visibility(def_sym);
198 if (def_sym->visible != no)
202 /* no choice? reset tristate value */
207 void sym_calc_value(struct symbol *sym)
209 struct symbol_value newval, oldval;
210 struct property *prop;
216 if (sym->flags & SYMBOL_VALID)
218 sym->flags |= SYMBOL_VALID;
226 newval = symbol_empty.curr;
230 newval = symbol_no.curr;
233 sym->curr.val = sym->name;
237 if (!sym_is_choice_value(sym))
238 sym->flags &= ~SYMBOL_WRITE;
240 sym_calc_visibility(sym);
242 /* set default if recursively called */
245 switch (sym_get_type(sym)) {
248 if (sym_is_choice_value(sym) && sym->visible == yes) {
249 prop = sym_get_choice_prop(sym);
250 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
251 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
252 sym->flags |= SYMBOL_WRITE;
253 if (sym_has_value(sym))
254 newval.tri = sym->user.tri;
255 else if (!sym_is_choice(sym)) {
256 prop = sym_get_default_prop(sym);
258 newval.tri = expr_calc_value(prop->expr);
260 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
261 } else if (!sym_is_choice(sym)) {
262 prop = sym_get_default_prop(sym);
264 sym->flags |= SYMBOL_WRITE;
265 newval.tri = expr_calc_value(prop->expr);
268 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
274 if (sym->visible != no) {
275 sym->flags |= SYMBOL_WRITE;
276 if (sym_has_value(sym)) {
277 newval.val = sym->user.val;
281 prop = sym_get_default_prop(sym);
283 struct symbol *ds = prop_get_symbol(prop);
285 sym->flags |= SYMBOL_WRITE;
287 newval.val = ds->curr.val;
296 if (sym_is_choice(sym) && newval.tri == yes)
297 sym->curr.val = sym_calc_choice(sym);
299 if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
300 sym_set_changed(sym);
301 if (modules_sym == sym)
302 modules_val = modules_sym->curr.tri;
304 if (sym_is_choice(sym)) {
305 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
306 prop = sym_get_choice_prop(sym);
307 for (e = prop->expr; e; e = e->left.expr) {
308 e->right.sym->flags |= flags;
309 if (flags & SYMBOL_CHANGED)
310 sym_set_changed(e->right.sym);
315 void sym_clear_all_valid(void)
320 for_all_symbols(i, sym)
321 sym->flags &= ~SYMBOL_VALID;
324 sym_calc_value(modules_sym);
327 void sym_set_changed(struct symbol *sym)
329 struct property *prop;
331 sym->flags |= SYMBOL_CHANGED;
332 for (prop = sym->prop; prop; prop = prop->next) {
334 prop->menu->flags |= MENU_CHANGED;
338 void sym_set_all_changed(void)
343 for_all_symbols(i, sym)
344 sym_set_changed(sym);
347 bool sym_tristate_within_range(struct symbol *sym, tristate val)
349 int type = sym_get_type(sym);
351 if (sym->visible == no)
354 if (type != S_BOOLEAN && type != S_TRISTATE)
357 if (type == S_BOOLEAN && val == mod)
359 if (sym->visible <= sym->rev_dep.tri)
361 if (sym_is_choice_value(sym) && sym->visible == yes)
363 return val >= sym->rev_dep.tri && val <= sym->visible;
366 bool sym_set_tristate_value(struct symbol *sym, tristate val)
368 tristate oldval = sym_get_tristate_value(sym);
370 if (oldval != val && !sym_tristate_within_range(sym, val))
373 if (sym->flags & SYMBOL_NEW) {
374 sym->flags &= ~SYMBOL_NEW;
375 sym_set_changed(sym);
377 if (sym_is_choice_value(sym) && val == yes) {
378 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
381 cs->flags &= ~SYMBOL_NEW;
386 sym_clear_all_valid();
387 if (sym == modules_sym)
388 sym_set_all_changed();
394 tristate sym_toggle_tristate_value(struct symbol *sym)
396 tristate oldval, newval;
398 oldval = newval = sym_get_tristate_value(sym);
411 if (sym_set_tristate_value(sym, newval))
413 } while (oldval != newval);
417 bool sym_string_valid(struct symbol *sym, const char *str)
430 if (ch == '0' && *str != 0)
432 while ((ch = *str++)) {
438 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
444 } while ((ch = *str++));
460 bool sym_string_within_range(struct symbol *sym, const char *str)
462 struct property *prop;
467 return sym_string_valid(sym, str);
469 if (!sym_string_valid(sym, str))
471 prop = sym_get_range_prop(sym);
474 val = strtol(str, NULL, 10);
475 return val >= strtol(prop->expr->left.sym->name, NULL, 10) &&
476 val <= strtol(prop->expr->right.sym->name, NULL, 10);
478 if (!sym_string_valid(sym, str))
480 prop = sym_get_range_prop(sym);
483 val = strtol(str, NULL, 16);
484 return val >= strtol(prop->expr->left.sym->name, NULL, 16) &&
485 val <= strtol(prop->expr->right.sym->name, NULL, 16);
490 return sym_tristate_within_range(sym, yes);
492 return sym_tristate_within_range(sym, mod);
494 return sym_tristate_within_range(sym, no);
502 bool sym_set_string_value(struct symbol *sym, const char *newval)
513 return sym_set_tristate_value(sym, yes);
515 return sym_set_tristate_value(sym, mod);
517 return sym_set_tristate_value(sym, no);
524 if (!sym_string_within_range(sym, newval))
527 if (sym->flags & SYMBOL_NEW) {
528 sym->flags &= ~SYMBOL_NEW;
529 sym_set_changed(sym);
532 oldval = sym->user.val;
533 size = strlen(newval) + 1;
534 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
536 sym->user.val = val = malloc(size);
539 } else if (!oldval || strcmp(oldval, newval))
540 sym->user.val = val = malloc(size);
545 free((void *)oldval);
546 sym_clear_all_valid();
551 const char *sym_get_string_value(struct symbol *sym)
558 val = sym_get_tristate_value(sym);
571 return (const char *)sym->curr.val;
574 bool sym_is_changable(struct symbol *sym)
576 return sym->visible > sym->rev_dep.tri;
579 struct symbol *sym_lookup(const char *name, int isconst)
581 struct symbol *symbol;
587 if (name[0] && !name[1]) {
589 case 'y': return &symbol_yes;
590 case 'm': return &symbol_mod;
591 case 'n': return &symbol_no;
594 for (ptr = name; *ptr; ptr++)
598 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
599 if (!strcmp(symbol->name, name)) {
600 if ((isconst && symbol->flags & SYMBOL_CONST) ||
601 (!isconst && !(symbol->flags & SYMBOL_CONST)))
605 new_name = strdup(name);
611 symbol = malloc(sizeof(*symbol));
612 memset(symbol, 0, sizeof(*symbol));
613 symbol->name = new_name;
614 symbol->type = S_UNKNOWN;
615 symbol->flags = SYMBOL_NEW;
617 symbol->flags |= SYMBOL_CONST;
619 symbol->next = symbol_hash[hash];
620 symbol_hash[hash] = symbol;
625 struct symbol *sym_find(const char *name)
627 struct symbol *symbol = NULL;
634 if (name[0] && !name[1]) {
636 case 'y': return &symbol_yes;
637 case 'm': return &symbol_mod;
638 case 'n': return &symbol_no;
641 for (ptr = name; *ptr; ptr++)
645 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
646 if (!strcmp(symbol->name, name) &&
647 !(symbol->flags & SYMBOL_CONST))
654 struct symbol **sym_re_search(const char *pattern)
656 struct symbol *sym, **sym_arr = NULL;
662 if (strlen(pattern) == 0)
664 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
667 for_all_symbols(i, sym) {
668 if (sym->flags & SYMBOL_CONST || !sym->name)
670 if (regexec(&re, sym->name, 0, NULL, 0))
672 if (cnt + 1 >= size) {
675 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
681 sym_arr[cnt++] = sym;
691 struct symbol *sym_check_deps(struct symbol *sym);
693 static struct symbol *sym_check_expr_deps(struct expr *e)
702 sym = sym_check_expr_deps(e->left.expr);
705 return sym_check_expr_deps(e->right.expr);
707 return sym_check_expr_deps(e->left.expr);
710 sym = sym_check_deps(e->left.sym);
713 return sym_check_deps(e->right.sym);
715 return sym_check_deps(e->left.sym);
719 printf("Oops! How to check %d?\n", e->type);
723 struct symbol *sym_check_deps(struct symbol *sym)
726 struct property *prop;
728 if (sym->flags & SYMBOL_CHECK_DONE)
730 if (sym->flags & SYMBOL_CHECK) {
731 printf("Warning! Found recursive dependency: %s", sym->name);
735 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
736 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
740 for (prop = sym->prop; prop; prop = prop->next) {
741 if (prop->type == P_CHOICE || prop->type == P_SELECT)
743 sym2 = sym_check_expr_deps(prop->visible.expr);
746 if (prop->type != P_DEFAULT || sym_is_choice(sym))
748 sym2 = sym_check_expr_deps(prop->expr);
754 printf(" %s", sym->name);
755 sym->flags &= ~SYMBOL_CHECK;
759 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
761 struct property *prop;
762 struct property **propp;
764 prop = malloc(sizeof(*prop));
765 memset(prop, 0, sizeof(*prop));
768 prop->file = current_file;
769 prop->lineno = zconf_lineno();
771 /* append property to the prop list of symbol */
773 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
781 struct symbol *prop_get_symbol(struct property *prop)
783 if (prop->expr && (prop->expr->type == E_SYMBOL ||
784 prop->expr->type == E_CHOICE))
785 return prop->expr->left.sym;
789 const char *prop_get_type_name(enum prop_type type)