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_YES|SYMBOL_VALID,
22 .flags = SYMBOL_MOD|SYMBOL_VALID,
26 .flags = SYMBOL_NO|SYMBOL_VALID,
30 .flags = SYMBOL_VALID,
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);
61 sym_add_default(sym, p);
63 sym = sym_lookup("OPENWRTVERSION", 0);
65 sym->flags |= SYMBOL_AUTO;
66 p = getenv("OPENWRTVERSION");
68 sym_add_default(sym, p);
70 sym = sym_lookup("UNAME_RELEASE", 0);
72 sym->flags |= SYMBOL_AUTO;
73 sym_add_default(sym, uts.release);
76 enum symbol_type sym_get_type(struct symbol *sym)
78 enum symbol_type type = sym->type;
80 if (type == S_TRISTATE) {
81 if (sym_is_choice_value(sym) && sym->visible == yes)
83 /* tristate always enabled */
85 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_default_prop(struct symbol *sym)
124 struct property *prop;
126 for_all_defaults(sym, prop) {
127 prop->visible.tri = expr_calc_value(prop->visible.expr);
128 if (prop->visible.tri != no)
134 struct property *sym_get_range_prop(struct symbol *sym)
136 struct property *prop;
138 for_all_properties(sym, prop, P_RANGE) {
139 prop->visible.tri = expr_calc_value(prop->visible.expr);
140 if (prop->visible.tri != no)
146 static int sym_get_range_val(struct symbol *sym, int base)
159 return strtol(sym->curr.val, NULL, base);
162 static void sym_validate_range(struct symbol *sym)
164 struct property *prop;
178 prop = sym_get_range_prop(sym);
181 val = strtol(sym->curr.val, NULL, base);
182 val2 = sym_get_range_val(prop->expr->left.sym, base);
184 val2 = sym_get_range_val(prop->expr->right.sym, base);
188 if (sym->type == S_INT)
189 sprintf(str, "%d", val2);
191 sprintf(str, "0x%x", val2);
192 sym->curr.val = strdup(str);
195 static void sym_calc_visibility(struct symbol *sym)
197 struct property *prop;
201 /* any prompt visible? */
203 for_all_prompts(sym, prop) {
204 prop->visible.tri = expr_calc_value(prop->visible.expr);
205 tri = E_OR(tri, prop->visible.tri);
207 if (tri == mod && (sym->type != S_TRISTATE))
209 if (sym->rev_dep_inv.expr && (expr_calc_value(sym->rev_dep_inv.expr) == yes)) {
213 if (sym->visible != tri) {
215 sym_set_changed(sym);
217 if (sym_is_choice_value(sym) || deselected)
220 if (sym->rev_dep.expr)
221 tri = expr_calc_value(sym->rev_dep.expr);
222 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
224 if (sym->rev_dep.tri != tri) {
225 sym->rev_dep.tri = tri;
226 sym_set_changed(sym);
230 static struct symbol *sym_calc_choice(struct symbol *sym)
232 struct symbol *def_sym;
233 struct property *prop;
236 /* is the user choice visible? */
237 def_sym = sym->user.val;
239 sym_calc_visibility(def_sym);
240 if (def_sym->visible != no)
244 /* any of the defaults visible? */
245 for_all_defaults(sym, prop) {
246 prop->visible.tri = expr_calc_value(prop->visible.expr);
247 if (prop->visible.tri == no)
249 def_sym = prop_get_symbol(prop);
250 sym_calc_visibility(def_sym);
251 if (def_sym->visible != no)
255 /* just get the first visible value */
256 prop = sym_get_choice_prop(sym);
257 for (e = prop->expr; e; e = e->left.expr) {
258 def_sym = e->right.sym;
259 sym_calc_visibility(def_sym);
260 if (def_sym->visible != no)
264 /* no choice? reset tristate value */
269 void sym_set_changed(struct symbol *sym)
271 struct property *prop;
273 sym->flags |= SYMBOL_CHANGED;
274 for (prop = sym->prop; prop; prop = prop->next) {
276 prop->menu->flags |= MENU_CHANGED;
280 void sym_set_all_changed(void)
285 for_all_symbols(i, sym)
286 sym_set_changed(sym);
289 void sym_calc_value(struct symbol *sym)
291 struct symbol_value newval, oldval;
292 struct property *prop;
298 if (sym->flags & SYMBOL_VALID)
300 sym->flags |= SYMBOL_VALID;
308 newval = symbol_empty.curr;
312 newval = symbol_no.curr;
315 sym->curr.val = sym->name;
319 if (!sym_is_choice_value(sym))
320 sym->flags &= ~SYMBOL_WRITE;
322 sym_calc_visibility(sym);
324 /* set default if recursively called */
327 switch (sym_get_type(sym)) {
330 if (sym_is_choice_value(sym) && sym->visible == yes) {
331 prop = sym_get_choice_prop(sym);
332 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
333 } else if (sym->rev_dep_inv.expr && (expr_calc_value(sym->rev_dep_inv.expr) == yes)) {
335 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
336 sym->flags |= SYMBOL_WRITE;
337 if (sym_has_value(sym))
338 newval.tri = sym->user.tri;
339 else if (!sym_is_choice(sym)) {
340 prop = sym_get_default_prop(sym);
342 newval.tri = expr_calc_value(prop->expr);
344 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
345 } else if (!sym_is_choice(sym)) {
346 prop = sym_get_default_prop(sym);
348 sym->flags |= SYMBOL_WRITE;
349 newval.tri = expr_calc_value(prop->expr);
352 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
358 if (sym->visible != no) {
359 sym->flags |= SYMBOL_WRITE;
360 if (sym_has_value(sym)) {
361 newval.val = sym->user.val;
365 prop = sym_get_default_prop(sym);
367 struct symbol *ds = prop_get_symbol(prop);
369 sym->flags |= SYMBOL_WRITE;
371 newval.val = ds->curr.val;
380 if (sym_is_choice(sym) && newval.tri == yes)
381 sym->curr.val = sym_calc_choice(sym);
382 sym_validate_range(sym);
384 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
385 sym->flags &= ~SYMBOL_VALID;
386 sym_set_changed(sym);
387 if (modules_sym == sym) {
388 sym_set_all_changed();
389 modules_val = modules_sym->curr.tri;
393 if (sym_is_choice(sym)) {
394 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
395 prop = sym_get_choice_prop(sym);
396 for (e = prop->expr; e; e = e->left.expr) {
397 e->right.sym->flags |= flags;
398 if (flags & SYMBOL_CHANGED)
399 sym_set_changed(e->right.sym);
403 if (sym->flags & SYMBOL_AUTO)
404 sym->flags &= ~SYMBOL_WRITE;
407 void sym_clear_all_valid(void)
412 for_all_symbols(i, sym)
413 sym->flags &= ~SYMBOL_VALID;
416 sym_calc_value(modules_sym);
419 bool sym_tristate_within_range(struct symbol *sym, tristate val)
421 int type = sym_get_type(sym);
423 if (sym->visible == no)
426 if (type != S_BOOLEAN && type != S_TRISTATE)
429 if (type == S_BOOLEAN && val == mod)
431 if (sym->visible <= sym->rev_dep.tri)
433 if (sym_is_choice_value(sym) && sym->visible == yes)
435 return val >= sym->rev_dep.tri && val <= sym->visible;
438 bool sym_set_tristate_value(struct symbol *sym, tristate val)
440 tristate oldval = sym_get_tristate_value(sym);
442 if (oldval != val && !sym_tristate_within_range(sym, val))
445 if (sym->flags & SYMBOL_NEW) {
446 sym->flags &= ~SYMBOL_NEW;
447 sym_set_changed(sym);
450 * setting a choice value also resets the new flag of the choice
451 * symbol and all other choice values.
453 if (sym_is_choice_value(sym) && val == yes) {
454 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
455 struct property *prop;
459 cs->flags &= ~SYMBOL_NEW;
460 prop = sym_get_choice_prop(cs);
461 for (e = prop->expr; e; e = e->left.expr) {
462 if (e->right.sym->visible != no)
463 e->right.sym->flags &= ~SYMBOL_NEW;
469 sym_clear_all_valid();
475 tristate sym_toggle_tristate_value(struct symbol *sym)
477 tristate oldval, newval;
479 oldval = newval = sym_get_tristate_value(sym);
492 if (sym_set_tristate_value(sym, newval))
494 } while (oldval != newval);
498 bool sym_string_valid(struct symbol *sym, const char *str)
511 if (ch == '0' && *str != 0)
513 while ((ch = *str++)) {
519 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
525 } while ((ch = *str++));
541 bool sym_string_within_range(struct symbol *sym, const char *str)
543 struct property *prop;
548 return sym_string_valid(sym, str);
550 if (!sym_string_valid(sym, str))
552 prop = sym_get_range_prop(sym);
555 val = strtol(str, NULL, 10);
556 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
557 val <= sym_get_range_val(prop->expr->right.sym, 10);
559 if (!sym_string_valid(sym, str))
561 prop = sym_get_range_prop(sym);
564 val = strtol(str, NULL, 16);
565 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
566 val <= sym_get_range_val(prop->expr->right.sym, 16);
571 return sym_tristate_within_range(sym, yes);
573 return sym_tristate_within_range(sym, mod);
575 return sym_tristate_within_range(sym, no);
583 bool sym_set_string_value(struct symbol *sym, const char *newval)
594 return sym_set_tristate_value(sym, yes);
596 return sym_set_tristate_value(sym, mod);
598 return sym_set_tristate_value(sym, no);
605 if (!sym_string_within_range(sym, newval))
608 if (sym->flags & SYMBOL_NEW) {
609 sym->flags &= ~SYMBOL_NEW;
610 sym_set_changed(sym);
613 oldval = sym->user.val;
614 size = strlen(newval) + 1;
615 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
617 sym->user.val = val = malloc(size);
620 } else if (!oldval || strcmp(oldval, newval))
621 sym->user.val = val = malloc(size);
626 free((void *)oldval);
627 sym_clear_all_valid();
632 const char *sym_get_string_value(struct symbol *sym)
639 val = sym_get_tristate_value(sym);
652 return (const char *)sym->curr.val;
655 bool sym_is_changable(struct symbol *sym)
657 return sym->visible > sym->rev_dep.tri;
660 struct symbol *sym_lookup(const char *name, int isconst)
662 struct symbol *symbol;
668 if (name[0] && !name[1]) {
670 case 'y': return &symbol_yes;
671 case 'm': return &symbol_mod;
672 case 'n': return &symbol_no;
675 for (ptr = name; *ptr; ptr++)
679 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
680 if (!strcmp(symbol->name, name)) {
681 if ((isconst && symbol->flags & SYMBOL_CONST) ||
682 (!isconst && !(symbol->flags & SYMBOL_CONST)))
686 new_name = strdup(name);
692 symbol = malloc(sizeof(*symbol));
693 memset(symbol, 0, sizeof(*symbol));
694 symbol->name = new_name;
695 symbol->type = S_UNKNOWN;
696 symbol->flags = SYMBOL_NEW;
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 *));
763 sym_arr[cnt++] = sym;
773 struct symbol *sym_check_deps(struct symbol *sym);
775 static struct symbol *sym_check_expr_deps(struct expr *e)
784 sym = sym_check_expr_deps(e->left.expr);
787 return sym_check_expr_deps(e->right.expr);
789 return sym_check_expr_deps(e->left.expr);
792 sym = sym_check_deps(e->left.sym);
795 return sym_check_deps(e->right.sym);
797 return sym_check_deps(e->left.sym);
801 printf("Oops! How to check %d?\n", e->type);
805 struct symbol *sym_check_deps(struct symbol *sym)
808 struct property *prop;
810 if (sym->flags & SYMBOL_CHECK) {
811 printf("Warning! Found recursive dependency: %s", sym->name);
814 if (sym->flags & SYMBOL_CHECKED)
817 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
818 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
822 for (prop = sym->prop; prop; prop = prop->next) {
823 if (prop->type == P_CHOICE || prop->type == P_SELECT || prop->type == P_DESELECT)
825 sym2 = sym_check_expr_deps(prop->visible.expr);
828 if (prop->type != P_DEFAULT || sym_is_choice(sym))
830 sym2 = sym_check_expr_deps(prop->expr);
836 printf(" %s", sym->name);
842 sym->flags &= ~SYMBOL_CHECK;
846 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
848 struct property *prop;
849 struct property **propp;
851 prop = malloc(sizeof(*prop));
852 memset(prop, 0, sizeof(*prop));
855 prop->file = current_file;
856 prop->lineno = zconf_lineno();
858 /* append property to the prop list of symbol */
860 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
868 struct symbol *prop_get_symbol(struct property *prop)
870 if (prop->expr && (prop->expr->type == E_SYMBOL ||
871 prop->expr->type == E_CHOICE))
872 return prop->expr->left.sym;
876 const char *prop_get_type_name(enum prop_type type)