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 struct expr *sym_env_list;
39 void sym_add_default(struct symbol *sym, const char *def)
41 struct property *prop = prop_alloc(P_DEFAULT, sym);
43 prop->expr = expr_alloc_symbol(sym_lookup(def, 1));
51 static bool inited = false;
59 sym = sym_lookup("ARCH", 0);
61 sym->flags |= SYMBOL_AUTO;
64 sym_add_default(sym, p);
66 sym = sym_lookup("KERNELVERSION", 0);
68 sym->flags |= SYMBOL_AUTO;
69 p = getenv("KERNELVERSION");
71 sym_add_default(sym, p);
73 sym = sym_lookup("UNAME_RELEASE", 0);
75 sym->flags |= SYMBOL_AUTO;
76 sym_add_default(sym, uts.release);
79 enum symbol_type sym_get_type(struct symbol *sym)
81 enum symbol_type type = sym->type;
83 if (type == S_TRISTATE) {
84 if (sym_is_choice_value(sym) && sym->visible == yes)
86 else if (modules_val == no)
92 const char *sym_type_name(enum symbol_type type)
113 struct property *sym_get_choice_prop(struct symbol *sym)
115 struct property *prop;
117 for_all_choices(sym, prop)
122 struct property *sym_get_env_prop(struct symbol *sym)
124 struct property *prop;
126 for_all_properties(sym, prop, P_ENV)
131 struct property *sym_get_default_prop(struct symbol *sym)
133 struct property *prop;
135 for_all_defaults(sym, prop) {
136 prop->visible.tri = expr_calc_value(prop->visible.expr);
137 if (prop->visible.tri != no)
143 struct property *sym_get_range_prop(struct symbol *sym)
145 struct property *prop;
147 for_all_properties(sym, prop, P_RANGE) {
148 prop->visible.tri = expr_calc_value(prop->visible.expr);
149 if (prop->visible.tri != no)
155 static int sym_get_range_val(struct symbol *sym, int base)
168 return strtol(sym->curr.val, NULL, base);
171 static void sym_validate_range(struct symbol *sym)
173 struct property *prop;
187 prop = sym_get_range_prop(sym);
190 val = strtol(sym->curr.val, NULL, base);
191 val2 = sym_get_range_val(prop->expr->left.sym, base);
193 val2 = sym_get_range_val(prop->expr->right.sym, base);
197 if (sym->type == S_INT)
198 sprintf(str, "%d", val2);
200 sprintf(str, "0x%x", val2);
201 sym->curr.val = strdup(str);
204 static void sym_calc_visibility(struct symbol *sym)
206 struct property *prop;
209 /* any prompt visible? */
211 for_all_prompts(sym, prop) {
212 prop->visible.tri = expr_calc_value(prop->visible.expr);
213 tri = EXPR_OR(tri, prop->visible.tri);
215 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
217 if (sym->visible != tri) {
219 sym_set_changed(sym);
221 if (sym_is_choice_value(sym))
224 if (sym->rev_dep.expr)
225 tri = expr_calc_value(sym->rev_dep.expr);
226 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
228 if (sym->rev_dep.tri != tri) {
229 sym->rev_dep.tri = tri;
230 sym_set_changed(sym);
234 static struct symbol *sym_calc_choice(struct symbol *sym)
236 struct symbol *def_sym;
237 struct property *prop;
240 /* is the user choice visible? */
241 def_sym = sym->def[S_DEF_USER].val;
243 sym_calc_visibility(def_sym);
244 if (def_sym->visible != no)
248 /* any of the defaults visible? */
249 for_all_defaults(sym, prop) {
250 prop->visible.tri = expr_calc_value(prop->visible.expr);
251 if (prop->visible.tri == no)
253 def_sym = prop_get_symbol(prop);
254 sym_calc_visibility(def_sym);
255 if (def_sym->visible != no)
259 /* just get the first visible value */
260 prop = sym_get_choice_prop(sym);
261 expr_list_for_each_sym(prop->expr, e, def_sym) {
262 sym_calc_visibility(def_sym);
263 if (def_sym->visible != no)
267 /* no choice? reset tristate value */
272 void sym_calc_value(struct symbol *sym)
274 struct symbol_value newval, oldval;
275 struct property *prop;
281 if (sym->flags & SYMBOL_VALID)
283 sym->flags |= SYMBOL_VALID;
291 newval = symbol_empty.curr;
295 newval = symbol_no.curr;
298 sym->curr.val = sym->name;
302 if (!sym_is_choice_value(sym))
303 sym->flags &= ~SYMBOL_WRITE;
305 sym_calc_visibility(sym);
307 /* set default if recursively called */
310 switch (sym_get_type(sym)) {
313 if (sym_is_choice_value(sym) && sym->visible == yes) {
314 prop = sym_get_choice_prop(sym);
315 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
316 } else if (EXPR_OR(sym->visible, sym->rev_dep.tri) != no) {
317 sym->flags |= SYMBOL_WRITE;
318 if (sym_has_value(sym))
319 newval.tri = sym->def[S_DEF_USER].tri;
320 else if (!sym_is_choice(sym)) {
321 prop = sym_get_default_prop(sym);
323 newval.tri = expr_calc_value(prop->expr);
325 newval.tri = EXPR_OR(EXPR_AND(newval.tri, sym->visible), sym->rev_dep.tri);
326 } else if (!sym_is_choice(sym)) {
327 prop = sym_get_default_prop(sym);
329 sym->flags |= SYMBOL_WRITE;
330 newval.tri = expr_calc_value(prop->expr);
333 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
339 if (sym->visible != no) {
340 sym->flags |= SYMBOL_WRITE;
341 if (sym_has_value(sym)) {
342 newval.val = sym->def[S_DEF_USER].val;
346 prop = sym_get_default_prop(sym);
348 struct symbol *ds = prop_get_symbol(prop);
350 sym->flags |= SYMBOL_WRITE;
352 newval.val = ds->curr.val;
360 if (sym->flags & SYMBOL_AUTO)
361 sym->flags &= ~SYMBOL_WRITE;
364 if (sym_is_choice(sym) && newval.tri == yes)
365 sym->curr.val = sym_calc_choice(sym);
366 sym_validate_range(sym);
368 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
369 sym_set_changed(sym);
370 if (modules_sym == sym) {
371 sym_set_all_changed();
372 modules_val = modules_sym->curr.tri;
376 if (sym_is_choice(sym)) {
377 struct symbol *choice_sym;
378 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
380 prop = sym_get_choice_prop(sym);
381 expr_list_for_each_sym(prop->expr, e, choice_sym) {
382 choice_sym->flags |= flags;
383 if (flags & SYMBOL_CHANGED)
384 sym_set_changed(choice_sym);
389 void sym_clear_all_valid(void)
394 for_all_symbols(i, sym)
395 sym->flags &= ~SYMBOL_VALID;
396 sym_add_change_count(1);
398 sym_calc_value(modules_sym);
401 void sym_set_changed(struct symbol *sym)
403 struct property *prop;
405 sym->flags |= SYMBOL_CHANGED;
406 for (prop = sym->prop; prop; prop = prop->next) {
408 prop->menu->flags |= MENU_CHANGED;
412 void sym_set_all_changed(void)
417 for_all_symbols(i, sym)
418 sym_set_changed(sym);
421 bool sym_tristate_within_range(struct symbol *sym, tristate val)
423 int type = sym_get_type(sym);
425 if (sym->visible == no)
428 if (type != S_BOOLEAN && type != S_TRISTATE)
431 if (type == S_BOOLEAN && val == mod)
433 if (sym->visible <= sym->rev_dep.tri)
435 if (sym_is_choice_value(sym) && sym->visible == yes)
437 return val >= sym->rev_dep.tri && val <= sym->visible;
440 bool sym_set_tristate_value(struct symbol *sym, tristate val)
442 tristate oldval = sym_get_tristate_value(sym);
444 if (oldval != val && !sym_tristate_within_range(sym, val))
447 if (!(sym->flags & SYMBOL_DEF_USER)) {
448 sym->flags |= SYMBOL_DEF_USER;
449 sym_set_changed(sym);
452 * setting a choice value also resets the new flag of the choice
453 * symbol and all other choice values.
455 if (sym_is_choice_value(sym) && val == yes) {
456 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
457 struct property *prop;
460 cs->def[S_DEF_USER].val = sym;
461 cs->flags |= SYMBOL_DEF_USER;
462 prop = sym_get_choice_prop(cs);
463 for (e = prop->expr; e; e = e->left.expr) {
464 if (e->right.sym->visible != no)
465 e->right.sym->flags |= SYMBOL_DEF_USER;
469 sym->def[S_DEF_USER].tri = val;
471 sym_clear_all_valid();
476 tristate sym_toggle_tristate_value(struct symbol *sym)
478 tristate oldval, newval;
480 oldval = newval = sym_get_tristate_value(sym);
493 if (sym_set_tristate_value(sym, newval))
495 } while (oldval != newval);
499 bool sym_string_valid(struct symbol *sym, const char *str)
512 if (ch == '0' && *str != 0)
514 while ((ch = *str++)) {
520 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
526 } while ((ch = *str++));
542 bool sym_string_within_range(struct symbol *sym, const char *str)
544 struct property *prop;
549 return sym_string_valid(sym, str);
551 if (!sym_string_valid(sym, str))
553 prop = sym_get_range_prop(sym);
556 val = strtol(str, NULL, 10);
557 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
558 val <= sym_get_range_val(prop->expr->right.sym, 10);
560 if (!sym_string_valid(sym, str))
562 prop = sym_get_range_prop(sym);
565 val = strtol(str, NULL, 16);
566 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
567 val <= sym_get_range_val(prop->expr->right.sym, 16);
572 return sym_tristate_within_range(sym, yes);
574 return sym_tristate_within_range(sym, mod);
576 return sym_tristate_within_range(sym, no);
584 bool sym_set_string_value(struct symbol *sym, const char *newval)
595 return sym_set_tristate_value(sym, yes);
597 return sym_set_tristate_value(sym, mod);
599 return sym_set_tristate_value(sym, no);
606 if (!sym_string_within_range(sym, newval))
609 if (!(sym->flags & SYMBOL_DEF_USER)) {
610 sym->flags |= SYMBOL_DEF_USER;
611 sym_set_changed(sym);
614 oldval = sym->def[S_DEF_USER].val;
615 size = strlen(newval) + 1;
616 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
618 sym->def[S_DEF_USER].val = val = malloc(size);
621 } else if (!oldval || strcmp(oldval, newval))
622 sym->def[S_DEF_USER].val = val = malloc(size);
627 free((void *)oldval);
628 sym_clear_all_valid();
633 const char *sym_get_string_value(struct symbol *sym)
640 val = sym_get_tristate_value(sym);
653 return (const char *)sym->curr.val;
656 bool sym_is_changable(struct symbol *sym)
658 return sym->visible > sym->rev_dep.tri;
661 struct symbol *sym_lookup(const char *name, int isconst)
663 struct symbol *symbol;
669 if (name[0] && !name[1]) {
671 case 'y': return &symbol_yes;
672 case 'm': return &symbol_mod;
673 case 'n': return &symbol_no;
676 for (ptr = name; *ptr; ptr++)
680 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
681 if (!strcmp(symbol->name, name)) {
682 if ((isconst && symbol->flags & SYMBOL_CONST) ||
683 (!isconst && !(symbol->flags & SYMBOL_CONST)))
687 new_name = strdup(name);
693 symbol = malloc(sizeof(*symbol));
694 memset(symbol, 0, sizeof(*symbol));
695 symbol->name = new_name;
696 symbol->type = S_UNKNOWN;
698 symbol->flags |= SYMBOL_CONST;
700 symbol->next = symbol_hash[hash];
701 symbol_hash[hash] = symbol;
706 struct symbol *sym_find(const char *name)
708 struct symbol *symbol = NULL;
715 if (name[0] && !name[1]) {
717 case 'y': return &symbol_yes;
718 case 'm': return &symbol_mod;
719 case 'n': return &symbol_no;
722 for (ptr = name; *ptr; ptr++)
726 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
727 if (!strcmp(symbol->name, name) &&
728 !(symbol->flags & SYMBOL_CONST))
735 struct symbol **sym_re_search(const char *pattern)
737 struct symbol *sym, **sym_arr = NULL;
743 if (strlen(pattern) == 0)
745 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
748 for_all_symbols(i, sym) {
749 if (sym->flags & SYMBOL_CONST || !sym->name)
751 if (regexec(&re, sym->name, 0, NULL, 0))
753 if (cnt + 1 >= size) {
756 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
762 sym_arr[cnt++] = sym;
772 struct symbol *sym_check_deps(struct symbol *sym);
774 static struct symbol *sym_check_expr_deps(struct expr *e)
783 sym = sym_check_expr_deps(e->left.expr);
786 return sym_check_expr_deps(e->right.expr);
788 return sym_check_expr_deps(e->left.expr);
791 sym = sym_check_deps(e->left.sym);
794 return sym_check_deps(e->right.sym);
796 return sym_check_deps(e->left.sym);
800 printf("Oops! How to check %d?\n", e->type);
804 /* return NULL when dependencies are OK */
805 struct symbol *sym_check_deps(struct symbol *sym)
808 struct property *prop;
810 if (sym->flags & SYMBOL_CHECK) {
811 fprintf(stderr, "%s:%d:error: found recursive dependency: %s",
812 sym->prop->file->name, sym->prop->lineno, sym->name);
815 if (sym->flags & SYMBOL_CHECKED)
818 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
819 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
823 for (prop = sym->prop; prop; prop = prop->next) {
824 if (prop->type == P_CHOICE || prop->type == P_SELECT)
826 sym2 = sym_check_expr_deps(prop->visible.expr);
829 if (prop->type != P_DEFAULT || sym_is_choice(sym))
831 sym2 = sym_check_expr_deps(prop->expr);
837 fprintf(stderr, " -> %s%s", sym->name, sym2 == sym? "\n": "");
838 sym->flags &= ~SYMBOL_CHECK;
842 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
844 struct property *prop;
845 struct property **propp;
847 prop = malloc(sizeof(*prop));
848 memset(prop, 0, sizeof(*prop));
851 prop->file = current_file;
852 prop->lineno = zconf_lineno();
854 /* append property to the prop list of symbol */
856 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
864 struct symbol *prop_get_symbol(struct property *prop)
866 if (prop->expr && (prop->expr->type == E_SYMBOL ||
867 prop->expr->type == E_LIST))
868 return prop->expr->left.sym;
872 const char *prop_get_type_name(enum prop_type type)
897 void prop_add_env(const char *env)
899 struct symbol *sym, *sym2;
900 struct property *prop;
903 sym = current_entry->sym;
904 sym->flags |= SYMBOL_AUTO;
905 for_all_properties(sym, prop, P_ENV) {
906 sym2 = prop_get_symbol(prop);
907 if (strcmp(sym2->name, env))
908 menu_warn(current_entry, "redefining environment symbol from %s",
913 prop = prop_alloc(P_ENV, sym);
914 prop->expr = expr_alloc_symbol(sym_lookup(env, 1));
916 sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
917 sym_env_list->right.sym = sym;
921 sym_add_default(sym, p);
923 menu_warn(current_entry, "environment variable %s undefined", env);