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,
34 struct symbol *sym_defconfig_list;
35 struct symbol *modules_sym;
38 void sym_add_default(struct symbol *sym, const char *def)
40 struct property *prop = prop_alloc(P_DEFAULT, sym);
42 prop->expr = expr_alloc_symbol(sym_lookup(def, 1));
50 static bool inited = false;
58 sym = sym_lookup("ARCH", 0);
60 sym->flags |= SYMBOL_AUTO;
63 sym_add_default(sym, p);
65 sym = sym_lookup("KERNELVERSION", 0);
67 sym->flags |= SYMBOL_AUTO;
68 p = getenv("KERNELVERSION");
70 sym_add_default(sym, p);
72 sym = sym_lookup("UNAME_RELEASE", 0);
74 sym->flags |= SYMBOL_AUTO;
75 sym_add_default(sym, uts.release);
78 enum symbol_type sym_get_type(struct symbol *sym)
80 enum symbol_type type = sym->type;
82 if (type == S_TRISTATE) {
83 if (sym_is_choice_value(sym) && sym->visible == yes)
85 else if (modules_val == no)
91 const char *sym_type_name(enum symbol_type type)
112 struct property *sym_get_choice_prop(struct symbol *sym)
114 struct property *prop;
116 for_all_choices(sym, prop)
121 struct property *sym_get_default_prop(struct symbol *sym)
123 struct property *prop;
125 for_all_defaults(sym, prop) {
126 prop->visible.tri = expr_calc_value(prop->visible.expr);
127 if (prop->visible.tri != no)
133 struct property *sym_get_range_prop(struct symbol *sym)
135 struct property *prop;
137 for_all_properties(sym, prop, P_RANGE) {
138 prop->visible.tri = expr_calc_value(prop->visible.expr);
139 if (prop->visible.tri != no)
145 static int sym_get_range_val(struct symbol *sym, int base)
158 return strtol(sym->curr.val, NULL, base);
161 static void sym_validate_range(struct symbol *sym)
163 struct property *prop;
177 prop = sym_get_range_prop(sym);
180 val = strtol(sym->curr.val, NULL, base);
181 val2 = sym_get_range_val(prop->expr->left.sym, base);
183 val2 = sym_get_range_val(prop->expr->right.sym, base);
187 if (sym->type == S_INT)
188 sprintf(str, "%d", val2);
190 sprintf(str, "0x%x", val2);
191 sym->curr.val = strdup(str);
194 static void sym_calc_visibility(struct symbol *sym)
196 struct property *prop;
199 /* any prompt visible? */
201 for_all_prompts(sym, prop) {
202 prop->visible.tri = expr_calc_value(prop->visible.expr);
203 tri = E_OR(tri, prop->visible.tri);
205 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
207 if (sym->visible != tri) {
209 sym_set_changed(sym);
211 if (sym_is_choice_value(sym))
214 if (sym->rev_dep.expr)
215 tri = expr_calc_value(sym->rev_dep.expr);
216 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
218 if (sym->rev_dep.tri != tri) {
219 sym->rev_dep.tri = tri;
220 sym_set_changed(sym);
224 static struct symbol *sym_calc_choice(struct symbol *sym)
226 struct symbol *def_sym;
227 struct property *prop;
230 /* is the user choice visible? */
231 def_sym = sym->def[S_DEF_USER].val;
233 sym_calc_visibility(def_sym);
234 if (def_sym->visible != no)
238 /* any of the defaults visible? */
239 for_all_defaults(sym, prop) {
240 prop->visible.tri = expr_calc_value(prop->visible.expr);
241 if (prop->visible.tri == no)
243 def_sym = prop_get_symbol(prop);
244 sym_calc_visibility(def_sym);
245 if (def_sym->visible != no)
249 /* just get the first visible value */
250 prop = sym_get_choice_prop(sym);
251 for (e = prop->expr; e; e = e->left.expr) {
252 def_sym = e->right.sym;
253 sym_calc_visibility(def_sym);
254 if (def_sym->visible != no)
258 /* no choice? reset tristate value */
263 void sym_calc_value(struct symbol *sym)
265 struct symbol_value newval, oldval;
266 struct property *prop;
272 if (sym->flags & SYMBOL_VALID)
274 sym->flags |= SYMBOL_VALID;
282 newval = symbol_empty.curr;
286 newval = symbol_no.curr;
289 sym->curr.val = sym->name;
293 if (!sym_is_choice_value(sym))
294 sym->flags &= ~SYMBOL_WRITE;
296 sym_calc_visibility(sym);
298 /* set default if recursively called */
301 switch (sym_get_type(sym)) {
304 if (sym_is_choice_value(sym) && sym->visible == yes) {
305 prop = sym_get_choice_prop(sym);
306 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
307 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
308 sym->flags |= SYMBOL_WRITE;
309 if (sym_has_value(sym))
310 newval.tri = sym->def[S_DEF_USER].tri;
311 else if (!sym_is_choice(sym)) {
312 prop = sym_get_default_prop(sym);
314 newval.tri = expr_calc_value(prop->expr);
316 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
317 } else if (!sym_is_choice(sym)) {
318 prop = sym_get_default_prop(sym);
320 sym->flags |= SYMBOL_WRITE;
321 newval.tri = expr_calc_value(prop->expr);
324 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
330 if (sym->visible != no) {
331 sym->flags |= SYMBOL_WRITE;
332 if (sym_has_value(sym)) {
333 newval.val = sym->def[S_DEF_USER].val;
337 prop = sym_get_default_prop(sym);
339 struct symbol *ds = prop_get_symbol(prop);
341 sym->flags |= SYMBOL_WRITE;
343 newval.val = ds->curr.val;
352 if (sym_is_choice(sym) && newval.tri == yes)
353 sym->curr.val = sym_calc_choice(sym);
354 sym_validate_range(sym);
356 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
357 sym_set_changed(sym);
358 if (modules_sym == sym) {
359 sym_set_all_changed();
360 modules_val = modules_sym->curr.tri;
364 if (sym_is_choice(sym)) {
365 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
366 prop = sym_get_choice_prop(sym);
367 for (e = prop->expr; e; e = e->left.expr) {
368 e->right.sym->flags |= flags;
369 if (flags & SYMBOL_CHANGED)
370 sym_set_changed(e->right.sym);
375 void sym_clear_all_valid(void)
380 for_all_symbols(i, sym)
381 sym->flags &= ~SYMBOL_VALID;
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 struct symbol *sym_check_deps(struct symbol *sym)
793 struct property *prop;
795 if (sym->flags & SYMBOL_CHECK) {
796 printf("Warning! Found recursive dependency: %s", sym->name);
799 if (sym->flags & SYMBOL_CHECKED)
802 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
803 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
807 for (prop = sym->prop; prop; prop = prop->next) {
808 if (prop->type == P_CHOICE || prop->type == P_SELECT)
810 sym2 = sym_check_expr_deps(prop->visible.expr);
813 if (prop->type != P_DEFAULT || sym_is_choice(sym))
815 sym2 = sym_check_expr_deps(prop->expr);
821 printf(" %s", sym->name);
827 sym->flags &= ~SYMBOL_CHECK;
831 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
833 struct property *prop;
834 struct property **propp;
836 prop = malloc(sizeof(*prop));
837 memset(prop, 0, sizeof(*prop));
840 prop->file = current_file;
841 prop->lineno = zconf_lineno();
843 /* append property to the prop list of symbol */
845 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
853 struct symbol *prop_get_symbol(struct property *prop)
855 if (prop->expr && (prop->expr->type == E_SYMBOL ||
856 prop->expr->type == E_CHOICE))
857 return prop->expr->left.sym;
861 const char *prop_get_type_name(enum prop_type type)