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_CONST|SYMBOL_VALID,
22 .flags = SYMBOL_CONST|SYMBOL_VALID,
26 .flags = SYMBOL_CONST|SYMBOL_VALID,
30 .flags = SYMBOL_VALID,
33 struct symbol *sym_defconfig_list;
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("KERNELVERSION", 0);
66 sym->flags |= SYMBOL_AUTO;
67 p = getenv("KERNELVERSION");
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 int sym_get_range_val(struct symbol *sym, int base)
157 return strtol(sym->curr.val, NULL, base);
160 static void sym_validate_range(struct symbol *sym)
162 struct property *prop;
176 prop = sym_get_range_prop(sym);
179 val = strtol(sym->curr.val, NULL, base);
180 val2 = sym_get_range_val(prop->expr->left.sym, base);
182 val2 = sym_get_range_val(prop->expr->right.sym, base);
186 if (sym->type == S_INT)
187 sprintf(str, "%d", val2);
189 sprintf(str, "0x%x", val2);
190 sym->curr.val = strdup(str);
193 static void sym_calc_visibility(struct symbol *sym)
195 struct property *prop;
198 /* any prompt visible? */
200 for_all_prompts(sym, prop) {
201 prop->visible.tri = expr_calc_value(prop->visible.expr);
202 tri = EXPR_OR(tri, prop->visible.tri);
204 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
206 if (sym->visible != tri) {
208 sym_set_changed(sym);
210 if (sym_is_choice_value(sym))
213 if (sym->rev_dep.expr)
214 tri = expr_calc_value(sym->rev_dep.expr);
215 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
217 if (sym->rev_dep.tri != tri) {
218 sym->rev_dep.tri = tri;
219 sym_set_changed(sym);
223 static struct symbol *sym_calc_choice(struct symbol *sym)
225 struct symbol *def_sym;
226 struct property *prop;
229 /* is the user choice visible? */
230 def_sym = sym->def[S_DEF_USER].val;
232 sym_calc_visibility(def_sym);
233 if (def_sym->visible != no)
237 /* any of the defaults visible? */
238 for_all_defaults(sym, prop) {
239 prop->visible.tri = expr_calc_value(prop->visible.expr);
240 if (prop->visible.tri == no)
242 def_sym = prop_get_symbol(prop);
243 sym_calc_visibility(def_sym);
244 if (def_sym->visible != no)
248 /* just get the first visible value */
249 prop = sym_get_choice_prop(sym);
250 expr_list_for_each_sym(prop->expr, e, def_sym) {
251 sym_calc_visibility(def_sym);
252 if (def_sym->visible != no)
256 /* no choice? reset tristate value */
261 void sym_calc_value(struct symbol *sym)
263 struct symbol_value newval, oldval;
264 struct property *prop;
270 if (sym->flags & SYMBOL_VALID)
272 sym->flags |= SYMBOL_VALID;
280 newval = symbol_empty.curr;
284 newval = symbol_no.curr;
287 sym->curr.val = sym->name;
291 if (!sym_is_choice_value(sym))
292 sym->flags &= ~SYMBOL_WRITE;
294 sym_calc_visibility(sym);
296 /* set default if recursively called */
299 switch (sym_get_type(sym)) {
302 if (sym_is_choice_value(sym) && sym->visible == yes) {
303 prop = sym_get_choice_prop(sym);
304 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
305 } else if (EXPR_OR(sym->visible, sym->rev_dep.tri) != no) {
306 sym->flags |= SYMBOL_WRITE;
307 if (sym_has_value(sym))
308 newval.tri = sym->def[S_DEF_USER].tri;
309 else if (!sym_is_choice(sym)) {
310 prop = sym_get_default_prop(sym);
312 newval.tri = expr_calc_value(prop->expr);
314 newval.tri = EXPR_OR(EXPR_AND(newval.tri, sym->visible), sym->rev_dep.tri);
315 } else if (!sym_is_choice(sym)) {
316 prop = sym_get_default_prop(sym);
318 sym->flags |= SYMBOL_WRITE;
319 newval.tri = expr_calc_value(prop->expr);
322 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
328 if (sym->visible != no) {
329 sym->flags |= SYMBOL_WRITE;
330 if (sym_has_value(sym)) {
331 newval.val = sym->def[S_DEF_USER].val;
335 prop = sym_get_default_prop(sym);
337 struct symbol *ds = prop_get_symbol(prop);
339 sym->flags |= SYMBOL_WRITE;
341 newval.val = ds->curr.val;
350 if (sym_is_choice(sym) && newval.tri == yes)
351 sym->curr.val = sym_calc_choice(sym);
352 sym_validate_range(sym);
354 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
355 sym_set_changed(sym);
356 if (modules_sym == sym) {
357 sym_set_all_changed();
358 modules_val = modules_sym->curr.tri;
362 if (sym_is_choice(sym)) {
363 struct symbol *choice_sym;
364 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
366 prop = sym_get_choice_prop(sym);
367 expr_list_for_each_sym(prop->expr, e, choice_sym) {
368 choice_sym->flags |= flags;
369 if (flags & SYMBOL_CHANGED)
370 sym_set_changed(choice_sym);
375 void sym_clear_all_valid(void)
380 for_all_symbols(i, sym)
381 sym->flags &= ~SYMBOL_VALID;
382 sym_add_change_count(1);
384 sym_calc_value(modules_sym);
387 void sym_set_changed(struct symbol *sym)
389 struct property *prop;
391 sym->flags |= SYMBOL_CHANGED;
392 for (prop = sym->prop; prop; prop = prop->next) {
394 prop->menu->flags |= MENU_CHANGED;
398 void sym_set_all_changed(void)
403 for_all_symbols(i, sym)
404 sym_set_changed(sym);
407 bool sym_tristate_within_range(struct symbol *sym, tristate val)
409 int type = sym_get_type(sym);
411 if (sym->visible == no)
414 if (type != S_BOOLEAN && type != S_TRISTATE)
417 if (type == S_BOOLEAN && val == mod)
419 if (sym->visible <= sym->rev_dep.tri)
421 if (sym_is_choice_value(sym) && sym->visible == yes)
423 return val >= sym->rev_dep.tri && val <= sym->visible;
426 bool sym_set_tristate_value(struct symbol *sym, tristate val)
428 tristate oldval = sym_get_tristate_value(sym);
430 if (oldval != val && !sym_tristate_within_range(sym, val))
433 if (!(sym->flags & SYMBOL_DEF_USER)) {
434 sym->flags |= SYMBOL_DEF_USER;
435 sym_set_changed(sym);
438 * setting a choice value also resets the new flag of the choice
439 * symbol and all other choice values.
441 if (sym_is_choice_value(sym) && val == yes) {
442 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
443 struct property *prop;
446 cs->def[S_DEF_USER].val = sym;
447 cs->flags |= SYMBOL_DEF_USER;
448 prop = sym_get_choice_prop(cs);
449 for (e = prop->expr; e; e = e->left.expr) {
450 if (e->right.sym->visible != no)
451 e->right.sym->flags |= SYMBOL_DEF_USER;
455 sym->def[S_DEF_USER].tri = val;
457 sym_clear_all_valid();
462 tristate sym_toggle_tristate_value(struct symbol *sym)
464 tristate oldval, newval;
466 oldval = newval = sym_get_tristate_value(sym);
479 if (sym_set_tristate_value(sym, newval))
481 } while (oldval != newval);
485 bool sym_string_valid(struct symbol *sym, const char *str)
498 if (ch == '0' && *str != 0)
500 while ((ch = *str++)) {
506 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
512 } while ((ch = *str++));
528 bool sym_string_within_range(struct symbol *sym, const char *str)
530 struct property *prop;
535 return sym_string_valid(sym, str);
537 if (!sym_string_valid(sym, str))
539 prop = sym_get_range_prop(sym);
542 val = strtol(str, NULL, 10);
543 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
544 val <= sym_get_range_val(prop->expr->right.sym, 10);
546 if (!sym_string_valid(sym, str))
548 prop = sym_get_range_prop(sym);
551 val = strtol(str, NULL, 16);
552 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
553 val <= sym_get_range_val(prop->expr->right.sym, 16);
558 return sym_tristate_within_range(sym, yes);
560 return sym_tristate_within_range(sym, mod);
562 return sym_tristate_within_range(sym, no);
570 bool sym_set_string_value(struct symbol *sym, const char *newval)
581 return sym_set_tristate_value(sym, yes);
583 return sym_set_tristate_value(sym, mod);
585 return sym_set_tristate_value(sym, no);
592 if (!sym_string_within_range(sym, newval))
595 if (!(sym->flags & SYMBOL_DEF_USER)) {
596 sym->flags |= SYMBOL_DEF_USER;
597 sym_set_changed(sym);
600 oldval = sym->def[S_DEF_USER].val;
601 size = strlen(newval) + 1;
602 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
604 sym->def[S_DEF_USER].val = val = malloc(size);
607 } else if (!oldval || strcmp(oldval, newval))
608 sym->def[S_DEF_USER].val = val = malloc(size);
613 free((void *)oldval);
614 sym_clear_all_valid();
619 const char *sym_get_string_value(struct symbol *sym)
626 val = sym_get_tristate_value(sym);
639 return (const char *)sym->curr.val;
642 bool sym_is_changable(struct symbol *sym)
644 return sym->visible > sym->rev_dep.tri;
647 struct symbol *sym_lookup(const char *name, int isconst)
649 struct symbol *symbol;
655 if (name[0] && !name[1]) {
657 case 'y': return &symbol_yes;
658 case 'm': return &symbol_mod;
659 case 'n': return &symbol_no;
662 for (ptr = name; *ptr; ptr++)
666 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
667 if (!strcmp(symbol->name, name)) {
668 if ((isconst && symbol->flags & SYMBOL_CONST) ||
669 (!isconst && !(symbol->flags & SYMBOL_CONST)))
673 new_name = strdup(name);
679 symbol = malloc(sizeof(*symbol));
680 memset(symbol, 0, sizeof(*symbol));
681 symbol->name = new_name;
682 symbol->type = S_UNKNOWN;
684 symbol->flags |= SYMBOL_CONST;
686 symbol->next = symbol_hash[hash];
687 symbol_hash[hash] = symbol;
692 struct symbol *sym_find(const char *name)
694 struct symbol *symbol = NULL;
701 if (name[0] && !name[1]) {
703 case 'y': return &symbol_yes;
704 case 'm': return &symbol_mod;
705 case 'n': return &symbol_no;
708 for (ptr = name; *ptr; ptr++)
712 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
713 if (!strcmp(symbol->name, name) &&
714 !(symbol->flags & SYMBOL_CONST))
721 struct symbol **sym_re_search(const char *pattern)
723 struct symbol *sym, **sym_arr = NULL;
729 if (strlen(pattern) == 0)
731 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
734 for_all_symbols(i, sym) {
735 if (sym->flags & SYMBOL_CONST || !sym->name)
737 if (regexec(&re, sym->name, 0, NULL, 0))
739 if (cnt + 1 >= size) {
742 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
748 sym_arr[cnt++] = sym;
758 struct symbol *sym_check_deps(struct symbol *sym);
760 static struct symbol *sym_check_expr_deps(struct expr *e)
769 sym = sym_check_expr_deps(e->left.expr);
772 return sym_check_expr_deps(e->right.expr);
774 return sym_check_expr_deps(e->left.expr);
777 sym = sym_check_deps(e->left.sym);
780 return sym_check_deps(e->right.sym);
782 return sym_check_deps(e->left.sym);
786 printf("Oops! How to check %d?\n", e->type);
790 /* return NULL when dependencies are OK */
791 struct symbol *sym_check_deps(struct symbol *sym)
794 struct property *prop;
796 if (sym->flags & SYMBOL_CHECK) {
797 fprintf(stderr, "%s:%d:error: found recursive dependency: %s",
798 sym->prop->file->name, sym->prop->lineno, sym->name);
801 if (sym->flags & SYMBOL_CHECKED)
804 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
805 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
809 for (prop = sym->prop; prop; prop = prop->next) {
810 if (prop->type == P_CHOICE || prop->type == P_SELECT)
812 sym2 = sym_check_expr_deps(prop->visible.expr);
815 if (prop->type != P_DEFAULT || sym_is_choice(sym))
817 sym2 = sym_check_expr_deps(prop->expr);
823 fprintf(stderr, " -> %s%s", sym->name, sym2 == sym? "\n": "");
824 sym->flags &= ~SYMBOL_CHECK;
828 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
830 struct property *prop;
831 struct property **propp;
833 prop = malloc(sizeof(*prop));
834 memset(prop, 0, sizeof(*prop));
837 prop->file = current_file;
838 prop->lineno = zconf_lineno();
840 /* append property to the prop list of symbol */
842 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
850 struct symbol *prop_get_symbol(struct property *prop)
852 if (prop->expr && (prop->expr->type == E_SYMBOL ||
853 prop->expr->type == E_LIST))
854 return prop->expr->left.sym;
858 const char *prop_get_type_name(enum prop_type type)