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, SYMBOL_CONST));
50 static bool inited = false;
58 sym = sym_lookup("UNAME_RELEASE", 0);
60 sym->flags |= SYMBOL_AUTO;
61 sym_add_default(sym, uts.release);
64 enum symbol_type sym_get_type(struct symbol *sym)
66 enum symbol_type type = sym->type;
68 if (type == S_TRISTATE) {
69 if (sym_is_choice_value(sym) && sym->visible == yes)
71 else if (modules_val == no)
77 const char *sym_type_name(enum symbol_type type)
98 struct property *sym_get_choice_prop(struct symbol *sym)
100 struct property *prop;
102 for_all_choices(sym, prop)
107 struct property *sym_get_env_prop(struct symbol *sym)
109 struct property *prop;
111 for_all_properties(sym, prop, P_ENV)
116 struct property *sym_get_default_prop(struct symbol *sym)
118 struct property *prop;
120 for_all_defaults(sym, prop) {
121 prop->visible.tri = expr_calc_value(prop->visible.expr);
122 if (prop->visible.tri != no)
128 struct property *sym_get_range_prop(struct symbol *sym)
130 struct property *prop;
132 for_all_properties(sym, prop, P_RANGE) {
133 prop->visible.tri = expr_calc_value(prop->visible.expr);
134 if (prop->visible.tri != no)
140 static int sym_get_range_val(struct symbol *sym, int base)
153 return strtol(sym->curr.val, NULL, base);
156 static void sym_validate_range(struct symbol *sym)
158 struct property *prop;
172 prop = sym_get_range_prop(sym);
175 val = strtol(sym->curr.val, NULL, base);
176 val2 = sym_get_range_val(prop->expr->left.sym, base);
178 val2 = sym_get_range_val(prop->expr->right.sym, base);
182 if (sym->type == S_INT)
183 sprintf(str, "%d", val2);
185 sprintf(str, "0x%x", val2);
186 sym->curr.val = strdup(str);
189 static void sym_calc_visibility(struct symbol *sym)
191 struct property *prop;
194 /* any prompt visible? */
196 for_all_prompts(sym, prop) {
197 prop->visible.tri = expr_calc_value(prop->visible.expr);
198 tri = EXPR_OR(tri, prop->visible.tri);
200 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
202 if (sym->visible != tri) {
204 sym_set_changed(sym);
206 if (sym_is_choice_value(sym))
209 if (sym->rev_dep.expr)
210 tri = expr_calc_value(sym->rev_dep.expr);
211 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
213 if (sym->rev_dep.tri != tri) {
214 sym->rev_dep.tri = tri;
215 sym_set_changed(sym);
219 static struct symbol *sym_calc_choice(struct symbol *sym)
221 struct symbol *def_sym;
222 struct property *prop;
225 /* is the user choice visible? */
226 def_sym = sym->def[S_DEF_USER].val;
228 sym_calc_visibility(def_sym);
229 if (def_sym->visible != no)
233 /* any of the defaults visible? */
234 for_all_defaults(sym, prop) {
235 prop->visible.tri = expr_calc_value(prop->visible.expr);
236 if (prop->visible.tri == no)
238 def_sym = prop_get_symbol(prop);
239 sym_calc_visibility(def_sym);
240 if (def_sym->visible != no)
244 /* just get the first visible value */
245 prop = sym_get_choice_prop(sym);
246 expr_list_for_each_sym(prop->expr, e, def_sym) {
247 sym_calc_visibility(def_sym);
248 if (def_sym->visible != no)
252 /* no choice? reset tristate value */
257 void sym_calc_value(struct symbol *sym)
259 struct symbol_value newval, oldval;
260 struct property *prop;
266 if (sym->flags & SYMBOL_VALID)
268 sym->flags |= SYMBOL_VALID;
276 newval = symbol_empty.curr;
280 newval = symbol_no.curr;
283 sym->curr.val = sym->name;
287 if (!sym_is_choice_value(sym))
288 sym->flags &= ~SYMBOL_WRITE;
290 sym_calc_visibility(sym);
292 /* set default if recursively called */
295 switch (sym_get_type(sym)) {
298 if (sym_is_choice_value(sym) && sym->visible == yes) {
299 prop = sym_get_choice_prop(sym);
300 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
302 if (sym->visible != no) {
303 /* if the symbol is visible use the user value
304 * if available, otherwise try the default value
306 sym->flags |= SYMBOL_WRITE;
307 if (sym_has_value(sym)) {
308 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
313 if (sym->rev_dep.tri != no)
314 sym->flags |= SYMBOL_WRITE;
315 if (!sym_is_choice(sym)) {
316 prop = sym_get_default_prop(sym);
318 sym->flags |= SYMBOL_WRITE;
319 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
324 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
326 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
332 if (sym->visible != no) {
333 sym->flags |= SYMBOL_WRITE;
334 if (sym_has_value(sym)) {
335 newval.val = sym->def[S_DEF_USER].val;
339 prop = sym_get_default_prop(sym);
341 struct symbol *ds = prop_get_symbol(prop);
343 sym->flags |= SYMBOL_WRITE;
345 newval.val = ds->curr.val;
354 if (sym_is_choice(sym) && newval.tri == yes)
355 sym->curr.val = sym_calc_choice(sym);
356 sym_validate_range(sym);
358 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
359 sym_set_changed(sym);
360 if (modules_sym == sym) {
361 sym_set_all_changed();
362 modules_val = modules_sym->curr.tri;
366 if (sym_is_choice(sym)) {
367 struct symbol *choice_sym;
368 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
370 prop = sym_get_choice_prop(sym);
371 expr_list_for_each_sym(prop->expr, e, choice_sym) {
372 choice_sym->flags |= flags;
373 if (flags & SYMBOL_CHANGED)
374 sym_set_changed(choice_sym);
378 if (sym->flags & SYMBOL_AUTO)
379 sym->flags &= ~SYMBOL_WRITE;
382 void sym_clear_all_valid(void)
387 for_all_symbols(i, sym)
388 sym->flags &= ~SYMBOL_VALID;
389 sym_add_change_count(1);
391 sym_calc_value(modules_sym);
394 void sym_set_changed(struct symbol *sym)
396 struct property *prop;
398 sym->flags |= SYMBOL_CHANGED;
399 for (prop = sym->prop; prop; prop = prop->next) {
401 prop->menu->flags |= MENU_CHANGED;
405 void sym_set_all_changed(void)
410 for_all_symbols(i, sym)
411 sym_set_changed(sym);
414 bool sym_tristate_within_range(struct symbol *sym, tristate val)
416 int type = sym_get_type(sym);
418 if (sym->visible == no)
421 if (type != S_BOOLEAN && type != S_TRISTATE)
424 if (type == S_BOOLEAN && val == mod)
426 if (sym->visible <= sym->rev_dep.tri)
428 if (sym_is_choice_value(sym) && sym->visible == yes)
430 return val >= sym->rev_dep.tri && val <= sym->visible;
433 bool sym_set_tristate_value(struct symbol *sym, tristate val)
435 tristate oldval = sym_get_tristate_value(sym);
437 if (oldval != val && !sym_tristate_within_range(sym, val))
440 if (!(sym->flags & SYMBOL_DEF_USER)) {
441 sym->flags |= SYMBOL_DEF_USER;
442 sym_set_changed(sym);
445 * setting a choice value also resets the new flag of the choice
446 * symbol and all other choice values.
448 if (sym_is_choice_value(sym) && val == yes) {
449 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
450 struct property *prop;
453 cs->def[S_DEF_USER].val = sym;
454 cs->flags |= SYMBOL_DEF_USER;
455 prop = sym_get_choice_prop(cs);
456 for (e = prop->expr; e; e = e->left.expr) {
457 if (e->right.sym->visible != no)
458 e->right.sym->flags |= SYMBOL_DEF_USER;
462 sym->def[S_DEF_USER].tri = val;
464 sym_clear_all_valid();
469 tristate sym_toggle_tristate_value(struct symbol *sym)
471 tristate oldval, newval;
473 oldval = newval = sym_get_tristate_value(sym);
486 if (sym_set_tristate_value(sym, newval))
488 } while (oldval != newval);
492 bool sym_string_valid(struct symbol *sym, const char *str)
505 if (ch == '0' && *str != 0)
507 while ((ch = *str++)) {
513 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
519 } while ((ch = *str++));
535 bool sym_string_within_range(struct symbol *sym, const char *str)
537 struct property *prop;
542 return sym_string_valid(sym, str);
544 if (!sym_string_valid(sym, str))
546 prop = sym_get_range_prop(sym);
549 val = strtol(str, NULL, 10);
550 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
551 val <= sym_get_range_val(prop->expr->right.sym, 10);
553 if (!sym_string_valid(sym, str))
555 prop = sym_get_range_prop(sym);
558 val = strtol(str, NULL, 16);
559 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
560 val <= sym_get_range_val(prop->expr->right.sym, 16);
565 return sym_tristate_within_range(sym, yes);
567 return sym_tristate_within_range(sym, mod);
569 return sym_tristate_within_range(sym, no);
577 bool sym_set_string_value(struct symbol *sym, const char *newval)
588 return sym_set_tristate_value(sym, yes);
590 return sym_set_tristate_value(sym, mod);
592 return sym_set_tristate_value(sym, no);
599 if (!sym_string_within_range(sym, newval))
602 if (!(sym->flags & SYMBOL_DEF_USER)) {
603 sym->flags |= SYMBOL_DEF_USER;
604 sym_set_changed(sym);
607 oldval = sym->def[S_DEF_USER].val;
608 size = strlen(newval) + 1;
609 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
611 sym->def[S_DEF_USER].val = val = malloc(size);
614 } else if (!oldval || strcmp(oldval, newval))
615 sym->def[S_DEF_USER].val = val = malloc(size);
620 free((void *)oldval);
621 sym_clear_all_valid();
626 const char *sym_get_string_value(struct symbol *sym)
633 val = sym_get_tristate_value(sym);
646 return (const char *)sym->curr.val;
649 bool sym_is_changable(struct symbol *sym)
651 return sym->visible > sym->rev_dep.tri;
654 struct symbol *sym_lookup(const char *name, int flags)
656 struct symbol *symbol;
662 if (name[0] && !name[1]) {
664 case 'y': return &symbol_yes;
665 case 'm': return &symbol_mod;
666 case 'n': return &symbol_no;
669 for (ptr = name; *ptr; ptr++)
673 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
674 if (!strcmp(symbol->name, name) &&
675 (flags ? symbol->flags & flags
676 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
679 new_name = strdup(name);
685 symbol = malloc(sizeof(*symbol));
686 memset(symbol, 0, sizeof(*symbol));
687 symbol->name = new_name;
688 symbol->type = S_UNKNOWN;
689 symbol->flags |= flags;
691 symbol->next = symbol_hash[hash];
692 symbol_hash[hash] = symbol;
697 struct symbol *sym_find(const char *name)
699 struct symbol *symbol = NULL;
706 if (name[0] && !name[1]) {
708 case 'y': return &symbol_yes;
709 case 'm': return &symbol_mod;
710 case 'n': return &symbol_no;
713 for (ptr = name; *ptr; ptr++)
717 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
718 if (!strcmp(symbol->name, name) &&
719 !(symbol->flags & SYMBOL_CONST))
726 struct symbol **sym_re_search(const char *pattern)
728 struct symbol *sym, **sym_arr = NULL;
734 if (strlen(pattern) == 0)
736 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
739 for_all_symbols(i, sym) {
740 if (sym->flags & SYMBOL_CONST || !sym->name)
742 if (regexec(&re, sym->name, 0, NULL, 0))
744 if (cnt + 1 >= size) {
747 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
753 sym_arr[cnt++] = sym;
763 static struct symbol *sym_check_expr_deps(struct expr *e)
772 sym = sym_check_expr_deps(e->left.expr);
775 return sym_check_expr_deps(e->right.expr);
777 return sym_check_expr_deps(e->left.expr);
780 sym = sym_check_deps(e->left.sym);
783 return sym_check_deps(e->right.sym);
785 return sym_check_deps(e->left.sym);
789 printf("Oops! How to check %d?\n", e->type);
793 /* return NULL when dependencies are OK */
794 static struct symbol *sym_check_sym_deps(struct symbol *sym)
797 struct property *prop;
799 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
803 for (prop = sym->prop; prop; prop = prop->next) {
804 if (prop->type == P_CHOICE || prop->type == P_SELECT)
806 sym2 = sym_check_expr_deps(prop->visible.expr);
809 if (prop->type != P_DEFAULT || sym_is_choice(sym))
811 sym2 = sym_check_expr_deps(prop->expr);
819 static struct symbol *sym_check_choice_deps(struct symbol *choice)
821 struct symbol *sym, *sym2;
822 struct property *prop;
825 prop = sym_get_choice_prop(choice);
826 expr_list_for_each_sym(prop->expr, e, sym)
827 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
829 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
830 sym2 = sym_check_sym_deps(choice);
831 choice->flags &= ~SYMBOL_CHECK;
835 expr_list_for_each_sym(prop->expr, e, sym) {
836 sym2 = sym_check_sym_deps(sym);
838 fprintf(stderr, " -> %s", sym->name);
843 expr_list_for_each_sym(prop->expr, e, sym)
844 sym->flags &= ~SYMBOL_CHECK;
846 if (sym2 && sym_is_choice_value(sym2) &&
847 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
853 struct symbol *sym_check_deps(struct symbol *sym)
856 struct property *prop;
858 if (sym->flags & SYMBOL_CHECK) {
859 fprintf(stderr, "%s:%d:error: found recursive dependency: %s",
860 sym->prop->file->name, sym->prop->lineno,
861 sym->name ? sym->name : "<choice>");
864 if (sym->flags & SYMBOL_CHECKED)
867 if (sym_is_choice_value(sym)) {
868 /* for choice groups start the check with main choice symbol */
869 prop = sym_get_choice_prop(sym);
870 sym2 = sym_check_deps(prop_get_symbol(prop));
871 } else if (sym_is_choice(sym)) {
872 sym2 = sym_check_choice_deps(sym);
874 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
875 sym2 = sym_check_sym_deps(sym);
876 sym->flags &= ~SYMBOL_CHECK;
880 fprintf(stderr, " -> %s", sym->name ? sym->name : "<choice>");
882 fprintf(stderr, "\n");
891 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
893 struct property *prop;
894 struct property **propp;
896 prop = malloc(sizeof(*prop));
897 memset(prop, 0, sizeof(*prop));
900 prop->file = current_file;
901 prop->lineno = zconf_lineno();
903 /* append property to the prop list of symbol */
905 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
913 struct symbol *prop_get_symbol(struct property *prop)
915 if (prop->expr && (prop->expr->type == E_SYMBOL ||
916 prop->expr->type == E_LIST))
917 return prop->expr->left.sym;
921 const char *prop_get_type_name(enum prop_type type)
946 void prop_add_env(const char *env)
948 struct symbol *sym, *sym2;
949 struct property *prop;
952 sym = current_entry->sym;
953 sym->flags |= SYMBOL_AUTO;
954 for_all_properties(sym, prop, P_ENV) {
955 sym2 = prop_get_symbol(prop);
956 if (strcmp(sym2->name, env))
957 menu_warn(current_entry, "redefining environment symbol from %s",
962 prop = prop_alloc(P_ENV, sym);
963 prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
965 sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
966 sym_env_list->right.sym = sym;
970 sym_add_default(sym, p);
972 menu_warn(current_entry, "environment variable %s undefined", env);