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 static 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 static 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))
208 /* defaulting to "yes" if no explicit "depends on" are given */
210 if (sym->dir_dep.expr)
211 tri = expr_calc_value(sym->dir_dep.expr);
214 if (sym->dir_dep.tri != tri) {
215 sym->dir_dep.tri = tri;
216 sym_set_changed(sym);
219 if (sym->rev_dep.expr)
220 tri = expr_calc_value(sym->rev_dep.expr);
221 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
223 if (sym->rev_dep.tri != tri) {
224 sym->rev_dep.tri = tri;
225 sym_set_changed(sym);
229 static struct symbol *sym_calc_choice(struct symbol *sym)
231 struct symbol *def_sym;
232 struct property *prop;
235 /* first calculate all choice values' visibilities */
236 prop = sym_get_choice_prop(sym);
237 expr_list_for_each_sym(prop->expr, e, def_sym)
238 sym_calc_visibility(def_sym);
240 /* is the user choice visible? */
241 def_sym = sym->def[S_DEF_USER].val;
242 if (def_sym && def_sym->visible != no)
245 /* any of the defaults visible? */
246 for_all_defaults(sym, prop) {
247 prop->visible.tri = expr_calc_value(prop->visible.expr);
248 if (prop->visible.tri == no)
250 def_sym = prop_get_symbol(prop);
251 if (def_sym->visible != no)
255 /* just get the first visible value */
256 prop = sym_get_choice_prop(sym);
257 expr_list_for_each_sym(prop->expr, e, def_sym)
258 if (def_sym->visible != no)
261 /* no choice? reset tristate value */
266 void sym_calc_value(struct symbol *sym)
268 struct symbol_value newval, oldval;
269 struct property *prop;
275 if (sym->flags & SYMBOL_VALID)
277 sym->flags |= SYMBOL_VALID;
285 newval = symbol_empty.curr;
289 newval = symbol_no.curr;
292 sym->curr.val = sym->name;
296 if (!sym_is_choice_value(sym))
297 sym->flags &= ~SYMBOL_WRITE;
299 sym_calc_visibility(sym);
301 /* set default if recursively called */
304 switch (sym_get_type(sym)) {
307 if (sym_is_choice_value(sym) && sym->visible == yes) {
308 prop = sym_get_choice_prop(sym);
309 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
311 if (sym->visible != no) {
312 /* if the symbol is visible use the user value
313 * if available, otherwise try the default value
315 sym->flags |= SYMBOL_WRITE;
316 if (sym_has_value(sym)) {
317 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
322 if (sym->rev_dep.tri != no)
323 sym->flags |= SYMBOL_WRITE;
324 if (!sym_is_choice(sym)) {
325 prop = sym_get_default_prop(sym);
327 sym->flags |= SYMBOL_WRITE;
328 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
333 if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
334 fprintf(stderr, "warning: (");
335 expr_fprint(sym->rev_dep.expr, stderr);
336 fprintf(stderr, ") selects %s which has unmet direct dependencies (",
338 expr_fprint(sym->dir_dep.expr, stderr);
339 fprintf(stderr, ")\n");
341 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
343 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
349 if (sym->visible != no) {
350 sym->flags |= SYMBOL_WRITE;
351 if (sym_has_value(sym)) {
352 newval.val = sym->def[S_DEF_USER].val;
356 prop = sym_get_default_prop(sym);
358 struct symbol *ds = prop_get_symbol(prop);
360 sym->flags |= SYMBOL_WRITE;
362 newval.val = ds->curr.val;
371 if (sym_is_choice(sym) && newval.tri == yes)
372 sym->curr.val = sym_calc_choice(sym);
373 sym_validate_range(sym);
375 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
376 sym_set_changed(sym);
377 if (modules_sym == sym) {
378 sym_set_all_changed();
379 modules_val = modules_sym->curr.tri;
383 if (sym_is_choice(sym)) {
384 struct symbol *choice_sym;
386 prop = sym_get_choice_prop(sym);
387 expr_list_for_each_sym(prop->expr, e, choice_sym) {
388 if ((sym->flags & SYMBOL_WRITE) &&
389 choice_sym->visible != no)
390 choice_sym->flags |= SYMBOL_WRITE;
391 if (sym->flags & SYMBOL_CHANGED)
392 sym_set_changed(choice_sym);
396 if (sym->flags & SYMBOL_AUTO)
397 sym->flags &= ~SYMBOL_WRITE;
400 void sym_clear_all_valid(void)
405 for_all_symbols(i, sym)
406 sym->flags &= ~SYMBOL_VALID;
407 sym_add_change_count(1);
409 sym_calc_value(modules_sym);
412 void sym_set_changed(struct symbol *sym)
414 struct property *prop;
416 sym->flags |= SYMBOL_CHANGED;
417 for (prop = sym->prop; prop; prop = prop->next) {
419 prop->menu->flags |= MENU_CHANGED;
423 void sym_set_all_changed(void)
428 for_all_symbols(i, sym)
429 sym_set_changed(sym);
432 bool sym_tristate_within_range(struct symbol *sym, tristate val)
434 int type = sym_get_type(sym);
436 if (sym->visible == no)
439 if (type != S_BOOLEAN && type != S_TRISTATE)
442 if (type == S_BOOLEAN && val == mod)
444 if (sym->visible <= sym->rev_dep.tri)
446 if (sym_is_choice_value(sym) && sym->visible == yes)
448 return val >= sym->rev_dep.tri && val <= sym->visible;
451 bool sym_set_tristate_value(struct symbol *sym, tristate val)
453 tristate oldval = sym_get_tristate_value(sym);
455 if (oldval != val && !sym_tristate_within_range(sym, val))
458 if (!(sym->flags & SYMBOL_DEF_USER)) {
459 sym->flags |= SYMBOL_DEF_USER;
460 sym_set_changed(sym);
463 * setting a choice value also resets the new flag of the choice
464 * symbol and all other choice values.
466 if (sym_is_choice_value(sym) && val == yes) {
467 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
468 struct property *prop;
471 cs->def[S_DEF_USER].val = sym;
472 cs->flags |= SYMBOL_DEF_USER;
473 prop = sym_get_choice_prop(cs);
474 for (e = prop->expr; e; e = e->left.expr) {
475 if (e->right.sym->visible != no)
476 e->right.sym->flags |= SYMBOL_DEF_USER;
480 sym->def[S_DEF_USER].tri = val;
482 sym_clear_all_valid();
487 tristate sym_toggle_tristate_value(struct symbol *sym)
489 tristate oldval, newval;
491 oldval = newval = sym_get_tristate_value(sym);
504 if (sym_set_tristate_value(sym, newval))
506 } while (oldval != newval);
510 bool sym_string_valid(struct symbol *sym, const char *str)
523 if (ch == '0' && *str != 0)
525 while ((ch = *str++)) {
531 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
537 } while ((ch = *str++));
553 bool sym_string_within_range(struct symbol *sym, const char *str)
555 struct property *prop;
560 return sym_string_valid(sym, str);
562 if (!sym_string_valid(sym, str))
564 prop = sym_get_range_prop(sym);
567 val = strtol(str, NULL, 10);
568 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
569 val <= sym_get_range_val(prop->expr->right.sym, 10);
571 if (!sym_string_valid(sym, str))
573 prop = sym_get_range_prop(sym);
576 val = strtol(str, NULL, 16);
577 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
578 val <= sym_get_range_val(prop->expr->right.sym, 16);
583 return sym_tristate_within_range(sym, yes);
585 return sym_tristate_within_range(sym, mod);
587 return sym_tristate_within_range(sym, no);
595 bool sym_set_string_value(struct symbol *sym, const char *newval)
606 return sym_set_tristate_value(sym, yes);
608 return sym_set_tristate_value(sym, mod);
610 return sym_set_tristate_value(sym, no);
617 if (!sym_string_within_range(sym, newval))
620 if (!(sym->flags & SYMBOL_DEF_USER)) {
621 sym->flags |= SYMBOL_DEF_USER;
622 sym_set_changed(sym);
625 oldval = sym->def[S_DEF_USER].val;
626 size = strlen(newval) + 1;
627 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
629 sym->def[S_DEF_USER].val = val = malloc(size);
632 } else if (!oldval || strcmp(oldval, newval))
633 sym->def[S_DEF_USER].val = val = malloc(size);
638 free((void *)oldval);
639 sym_clear_all_valid();
644 const char *sym_get_string_value(struct symbol *sym)
651 val = sym_get_tristate_value(sym);
664 return (const char *)sym->curr.val;
667 bool sym_is_changable(struct symbol *sym)
669 return sym->visible > sym->rev_dep.tri;
672 static unsigned strhash(const char *s)
675 unsigned hash = 2166136261U;
677 hash = (hash ^ *s) * 0x01000193;
681 struct symbol *sym_lookup(const char *name, int flags)
683 struct symbol *symbol;
688 if (name[0] && !name[1]) {
690 case 'y': return &symbol_yes;
691 case 'm': return &symbol_mod;
692 case 'n': return &symbol_no;
695 hash = strhash(name) % SYMBOL_HASHSIZE;
697 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
699 !strcmp(symbol->name, name) &&
700 (flags ? symbol->flags & flags
701 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
704 new_name = strdup(name);
710 symbol = malloc(sizeof(*symbol));
711 memset(symbol, 0, sizeof(*symbol));
712 symbol->name = new_name;
713 symbol->type = S_UNKNOWN;
714 symbol->flags |= flags;
716 symbol->next = symbol_hash[hash];
717 symbol_hash[hash] = symbol;
722 struct symbol *sym_find(const char *name)
724 struct symbol *symbol = NULL;
730 if (name[0] && !name[1]) {
732 case 'y': return &symbol_yes;
733 case 'm': return &symbol_mod;
734 case 'n': return &symbol_no;
737 hash = strhash(name) % SYMBOL_HASHSIZE;
739 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
741 !strcmp(symbol->name, name) &&
742 !(symbol->flags & SYMBOL_CONST))
749 struct symbol **sym_re_search(const char *pattern)
751 struct symbol *sym, **sym_arr = NULL;
757 if (strlen(pattern) == 0)
759 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
762 for_all_symbols(i, sym) {
763 if (sym->flags & SYMBOL_CONST || !sym->name)
765 if (regexec(&re, sym->name, 0, NULL, 0))
767 if (cnt + 1 >= size) {
770 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
777 sym_arr[cnt++] = sym;
787 static struct symbol *sym_check_expr_deps(struct expr *e)
796 sym = sym_check_expr_deps(e->left.expr);
799 return sym_check_expr_deps(e->right.expr);
801 return sym_check_expr_deps(e->left.expr);
804 sym = sym_check_deps(e->left.sym);
807 return sym_check_deps(e->right.sym);
809 return sym_check_deps(e->left.sym);
813 printf("Oops! How to check %d?\n", e->type);
817 /* return NULL when dependencies are OK */
818 static struct symbol *sym_check_sym_deps(struct symbol *sym)
821 struct property *prop;
823 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
827 for (prop = sym->prop; prop; prop = prop->next) {
828 if (prop->type == P_CHOICE || prop->type == P_SELECT)
830 sym2 = sym_check_expr_deps(prop->visible.expr);
833 if (prop->type != P_DEFAULT || sym_is_choice(sym))
835 sym2 = sym_check_expr_deps(prop->expr);
843 static struct symbol *sym_check_choice_deps(struct symbol *choice)
845 struct symbol *sym, *sym2;
846 struct property *prop;
849 prop = sym_get_choice_prop(choice);
850 expr_list_for_each_sym(prop->expr, e, sym)
851 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
853 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
854 sym2 = sym_check_sym_deps(choice);
855 choice->flags &= ~SYMBOL_CHECK;
859 expr_list_for_each_sym(prop->expr, e, sym) {
860 sym2 = sym_check_sym_deps(sym);
862 fprintf(stderr, " -> %s", sym->name);
867 expr_list_for_each_sym(prop->expr, e, sym)
868 sym->flags &= ~SYMBOL_CHECK;
870 if (sym2 && sym_is_choice_value(sym2) &&
871 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
877 struct symbol *sym_check_deps(struct symbol *sym)
880 struct property *prop;
882 if (sym->flags & SYMBOL_CHECK) {
883 fprintf(stderr, "%s:%d:error: found recursive dependency: %s",
884 sym->prop->file->name, sym->prop->lineno,
885 sym->name ? sym->name : "<choice>");
888 if (sym->flags & SYMBOL_CHECKED)
891 if (sym_is_choice_value(sym)) {
892 /* for choice groups start the check with main choice symbol */
893 prop = sym_get_choice_prop(sym);
894 sym2 = sym_check_deps(prop_get_symbol(prop));
895 } else if (sym_is_choice(sym)) {
896 sym2 = sym_check_choice_deps(sym);
898 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
899 sym2 = sym_check_sym_deps(sym);
900 sym->flags &= ~SYMBOL_CHECK;
904 fprintf(stderr, " -> %s", sym->name ? sym->name : "<choice>");
906 fprintf(stderr, "\n");
915 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
917 struct property *prop;
918 struct property **propp;
920 prop = malloc(sizeof(*prop));
921 memset(prop, 0, sizeof(*prop));
924 prop->file = current_file;
925 prop->lineno = zconf_lineno();
927 /* append property to the prop list of symbol */
929 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
937 struct symbol *prop_get_symbol(struct property *prop)
939 if (prop->expr && (prop->expr->type == E_SYMBOL ||
940 prop->expr->type == E_LIST))
941 return prop->expr->left.sym;
945 const char *prop_get_type_name(enum prop_type type)
972 static void prop_add_env(const char *env)
974 struct symbol *sym, *sym2;
975 struct property *prop;
978 sym = current_entry->sym;
979 sym->flags |= SYMBOL_AUTO;
980 for_all_properties(sym, prop, P_ENV) {
981 sym2 = prop_get_symbol(prop);
982 if (strcmp(sym2->name, env))
983 menu_warn(current_entry, "redefining environment symbol from %s",
988 prop = prop_alloc(P_ENV, sym);
989 prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
991 sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
992 sym_env_list->right.sym = sym;
996 sym_add_default(sym, p);
998 menu_warn(current_entry, "environment variable %s undefined", env);