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);
59 sym->flags |= SYMBOL_AUTO;
62 sym_add_default(sym, p);
64 sym = sym_lookup("KERNELRELEASE", 0);
66 sym->flags |= SYMBOL_AUTO;
67 p = getenv("KERNELRELEASE");
69 sym_add_default(sym, p);
71 sym = sym_lookup("UNAME_RELEASE", 0);
73 sym->flags |= SYMBOL_AUTO;
74 sym_add_default(sym, uts.release);
77 enum symbol_type sym_get_type(struct symbol *sym)
79 enum symbol_type type = sym->type;
81 if (type == S_TRISTATE) {
82 if (sym_is_choice_value(sym) && sym->visible == yes)
84 else if (modules_val == no)
90 const char *sym_type_name(enum symbol_type type)
111 struct property *sym_get_choice_prop(struct symbol *sym)
113 struct property *prop;
115 for_all_choices(sym, prop)
120 struct property *sym_get_default_prop(struct symbol *sym)
122 struct property *prop;
124 for_all_defaults(sym, prop) {
125 prop->visible.tri = expr_calc_value(prop->visible.expr);
126 if (prop->visible.tri != no)
132 struct property *sym_get_range_prop(struct symbol *sym)
134 struct property *prop;
136 for_all_properties(sym, prop, P_RANGE) {
137 prop->visible.tri = expr_calc_value(prop->visible.expr);
138 if (prop->visible.tri != no)
144 static void sym_calc_visibility(struct symbol *sym)
146 struct property *prop;
149 /* any prompt visible? */
151 for_all_prompts(sym, prop) {
152 prop->visible.tri = expr_calc_value(prop->visible.expr);
153 tri = E_OR(tri, prop->visible.tri);
155 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
157 if (sym->visible != tri) {
159 sym_set_changed(sym);
161 if (sym_is_choice_value(sym))
164 if (sym->rev_dep.expr)
165 tri = expr_calc_value(sym->rev_dep.expr);
166 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
168 if (sym->rev_dep.tri != tri) {
169 sym->rev_dep.tri = tri;
170 sym_set_changed(sym);
174 static struct symbol *sym_calc_choice(struct symbol *sym)
176 struct symbol *def_sym;
177 struct property *prop;
180 /* is the user choice visible? */
181 def_sym = sym->user.val;
183 sym_calc_visibility(def_sym);
184 if (def_sym->visible != no)
188 /* any of the defaults visible? */
189 for_all_defaults(sym, prop) {
190 prop->visible.tri = expr_calc_value(prop->visible.expr);
191 if (prop->visible.tri == no)
193 def_sym = prop_get_symbol(prop);
194 sym_calc_visibility(def_sym);
195 if (def_sym->visible != no)
199 /* just get the first visible value */
200 prop = sym_get_choice_prop(sym);
201 for (e = prop->expr; e; e = e->left.expr) {
202 def_sym = e->right.sym;
203 sym_calc_visibility(def_sym);
204 if (def_sym->visible != no)
208 /* no choice? reset tristate value */
213 void sym_calc_value(struct symbol *sym)
215 struct symbol_value newval, oldval;
216 struct property *prop;
222 if (sym->flags & SYMBOL_VALID)
224 sym->flags |= SYMBOL_VALID;
232 newval = symbol_empty.curr;
236 newval = symbol_no.curr;
239 sym->curr.val = sym->name;
243 if (!sym_is_choice_value(sym))
244 sym->flags &= ~SYMBOL_WRITE;
246 sym_calc_visibility(sym);
248 /* set default if recursively called */
251 switch (sym_get_type(sym)) {
254 if (sym_is_choice_value(sym) && sym->visible == yes) {
255 prop = sym_get_choice_prop(sym);
256 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
257 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
258 sym->flags |= SYMBOL_WRITE;
259 if (sym_has_value(sym))
260 newval.tri = sym->user.tri;
261 else if (!sym_is_choice(sym)) {
262 prop = sym_get_default_prop(sym);
264 newval.tri = expr_calc_value(prop->expr);
266 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
267 } else if (!sym_is_choice(sym)) {
268 prop = sym_get_default_prop(sym);
270 sym->flags |= SYMBOL_WRITE;
271 newval.tri = expr_calc_value(prop->expr);
274 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
280 if (sym->visible != no) {
281 sym->flags |= SYMBOL_WRITE;
282 if (sym_has_value(sym)) {
283 newval.val = sym->user.val;
287 prop = sym_get_default_prop(sym);
289 struct symbol *ds = prop_get_symbol(prop);
291 sym->flags |= SYMBOL_WRITE;
293 newval.val = ds->curr.val;
302 if (sym_is_choice(sym) && newval.tri == yes)
303 sym->curr.val = sym_calc_choice(sym);
305 if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
306 sym_set_changed(sym);
307 if (modules_sym == sym)
308 modules_val = modules_sym->curr.tri;
310 if (sym_is_choice(sym)) {
311 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
312 prop = sym_get_choice_prop(sym);
313 for (e = prop->expr; e; e = e->left.expr) {
314 e->right.sym->flags |= flags;
315 if (flags & SYMBOL_CHANGED)
316 sym_set_changed(e->right.sym);
321 void sym_clear_all_valid(void)
326 for_all_symbols(i, sym)
327 sym->flags &= ~SYMBOL_VALID;
330 sym_calc_value(modules_sym);
333 void sym_set_changed(struct symbol *sym)
335 struct property *prop;
337 sym->flags |= SYMBOL_CHANGED;
338 for (prop = sym->prop; prop; prop = prop->next) {
340 prop->menu->flags |= MENU_CHANGED;
344 void sym_set_all_changed(void)
349 for_all_symbols(i, sym)
350 sym_set_changed(sym);
353 bool sym_tristate_within_range(struct symbol *sym, tristate val)
355 int type = sym_get_type(sym);
357 if (sym->visible == no)
360 if (type != S_BOOLEAN && type != S_TRISTATE)
363 if (type == S_BOOLEAN && val == mod)
365 if (sym->visible <= sym->rev_dep.tri)
367 if (sym_is_choice_value(sym) && sym->visible == yes)
369 return val >= sym->rev_dep.tri && val <= sym->visible;
372 bool sym_set_tristate_value(struct symbol *sym, tristate val)
374 tristate oldval = sym_get_tristate_value(sym);
376 if (oldval != val && !sym_tristate_within_range(sym, val))
379 if (sym->flags & SYMBOL_NEW) {
380 sym->flags &= ~SYMBOL_NEW;
381 sym_set_changed(sym);
383 if (sym_is_choice_value(sym) && val == yes) {
384 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
387 cs->flags &= ~SYMBOL_NEW;
392 sym_clear_all_valid();
393 if (sym == modules_sym)
394 sym_set_all_changed();
400 tristate sym_toggle_tristate_value(struct symbol *sym)
402 tristate oldval, newval;
404 oldval = newval = sym_get_tristate_value(sym);
417 if (sym_set_tristate_value(sym, newval))
419 } while (oldval != newval);
423 bool sym_string_valid(struct symbol *sym, const char *str)
436 if (ch == '0' && *str != 0)
438 while ((ch = *str++)) {
444 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
450 } while ((ch = *str++));
466 bool sym_string_within_range(struct symbol *sym, const char *str)
468 struct property *prop;
473 return sym_string_valid(sym, str);
475 if (!sym_string_valid(sym, str))
477 prop = sym_get_range_prop(sym);
480 val = strtol(str, NULL, 10);
481 return val >= strtol(prop->expr->left.sym->name, NULL, 10) &&
482 val <= strtol(prop->expr->right.sym->name, NULL, 10);
484 if (!sym_string_valid(sym, str))
486 prop = sym_get_range_prop(sym);
489 val = strtol(str, NULL, 16);
490 return val >= strtol(prop->expr->left.sym->name, NULL, 16) &&
491 val <= strtol(prop->expr->right.sym->name, NULL, 16);
496 return sym_tristate_within_range(sym, yes);
498 return sym_tristate_within_range(sym, mod);
500 return sym_tristate_within_range(sym, no);
508 bool sym_set_string_value(struct symbol *sym, const char *newval)
519 return sym_set_tristate_value(sym, yes);
521 return sym_set_tristate_value(sym, mod);
523 return sym_set_tristate_value(sym, no);
530 if (!sym_string_within_range(sym, newval))
533 if (sym->flags & SYMBOL_NEW) {
534 sym->flags &= ~SYMBOL_NEW;
535 sym_set_changed(sym);
538 oldval = sym->user.val;
539 size = strlen(newval) + 1;
540 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
542 sym->user.val = val = malloc(size);
545 } else if (!oldval || strcmp(oldval, newval))
546 sym->user.val = val = malloc(size);
551 free((void *)oldval);
552 sym_clear_all_valid();
557 const char *sym_get_string_value(struct symbol *sym)
564 val = sym_get_tristate_value(sym);
577 return (const char *)sym->curr.val;
580 bool sym_is_changable(struct symbol *sym)
582 return sym->visible > sym->rev_dep.tri;
585 struct symbol *sym_lookup(const char *name, int isconst)
587 struct symbol *symbol;
593 if (name[0] && !name[1]) {
595 case 'y': return &symbol_yes;
596 case 'm': return &symbol_mod;
597 case 'n': return &symbol_no;
600 for (ptr = name; *ptr; ptr++)
604 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
605 if (!strcmp(symbol->name, name)) {
606 if ((isconst && symbol->flags & SYMBOL_CONST) ||
607 (!isconst && !(symbol->flags & SYMBOL_CONST)))
611 new_name = strdup(name);
617 symbol = malloc(sizeof(*symbol));
618 memset(symbol, 0, sizeof(*symbol));
619 symbol->name = new_name;
620 symbol->type = S_UNKNOWN;
621 symbol->flags = SYMBOL_NEW;
623 symbol->flags |= SYMBOL_CONST;
625 symbol->next = symbol_hash[hash];
626 symbol_hash[hash] = symbol;
631 struct symbol *sym_find(const char *name)
633 struct symbol *symbol = NULL;
640 if (name[0] && !name[1]) {
642 case 'y': return &symbol_yes;
643 case 'm': return &symbol_mod;
644 case 'n': return &symbol_no;
647 for (ptr = name; *ptr; ptr++)
651 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
652 if (!strcmp(symbol->name, name) &&
653 !(symbol->flags & SYMBOL_CONST))
660 struct symbol **sym_re_search(const char *pattern)
662 struct symbol *sym, **sym_arr = NULL;
668 if (strlen(pattern) == 0)
670 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
673 for_all_symbols(i, sym) {
674 if (sym->flags & SYMBOL_CONST || !sym->name)
676 if (regexec(&re, sym->name, 0, NULL, 0))
678 if (cnt + 1 >= size) {
681 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
687 sym_arr[cnt++] = sym;
697 struct symbol *sym_check_deps(struct symbol *sym);
699 static struct symbol *sym_check_expr_deps(struct expr *e)
708 sym = sym_check_expr_deps(e->left.expr);
711 return sym_check_expr_deps(e->right.expr);
713 return sym_check_expr_deps(e->left.expr);
716 sym = sym_check_deps(e->left.sym);
719 return sym_check_deps(e->right.sym);
721 return sym_check_deps(e->left.sym);
725 printf("Oops! How to check %d?\n", e->type);
729 struct symbol *sym_check_deps(struct symbol *sym)
732 struct property *prop;
734 if (sym->flags & SYMBOL_CHECK_DONE)
736 if (sym->flags & SYMBOL_CHECK) {
737 printf("Warning! Found recursive dependency: %s", sym->name);
741 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
742 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
746 for (prop = sym->prop; prop; prop = prop->next) {
747 if (prop->type == P_CHOICE || prop->type == P_SELECT)
749 sym2 = sym_check_expr_deps(prop->visible.expr);
752 if (prop->type != P_DEFAULT || sym_is_choice(sym))
754 sym2 = sym_check_expr_deps(prop->expr);
760 printf(" %s", sym->name);
761 sym->flags &= ~SYMBOL_CHECK;
765 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
767 struct property *prop;
768 struct property **propp;
770 prop = malloc(sizeof(*prop));
771 memset(prop, 0, sizeof(*prop));
774 prop->file = current_file;
775 prop->lineno = zconf_lineno();
777 /* append property to the prop list of symbol */
779 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
787 struct symbol *prop_get_symbol(struct property *prop)
789 if (prop->expr && (prop->expr->type == E_SYMBOL ||
790 prop->expr->type == E_CHOICE))
791 return prop->expr->left.sym;
795 const char *prop_get_type_name(enum prop_type type)