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);
59 sym->flags |= SYMBOL_AUTO;
62 sym_add_default(sym, p);
64 sym = sym_lookup("OPENWRTVERSION", 0);
66 sym->flags |= SYMBOL_AUTO;
67 p = getenv("OPENWRTVERSION");
69 sym_add_default(sym, p);
71 sym = sym_lookup("UNAME_RELEASE", 0);
73 sym->flags |= SYMBOL_AUTO;
74 sym_add_default(sym, uts.release);
77 enum symbol_type sym_get_type(struct symbol *sym)
79 enum symbol_type type = sym->type;
81 if (type == S_TRISTATE) {
82 if (sym_is_choice_value(sym) && sym->visible == yes)
84 /* tristate always enabled */
86 else if (modules_val == no)
93 const char *sym_type_name(enum symbol_type type)
114 struct property *sym_get_choice_prop(struct symbol *sym)
116 struct property *prop;
118 for_all_choices(sym, prop)
123 struct property *sym_get_default_prop(struct symbol *sym)
125 struct property *prop;
127 for_all_defaults(sym, prop) {
128 prop->visible.tri = expr_calc_value(prop->visible.expr);
129 if (prop->visible.tri != no)
135 struct property *sym_get_range_prop(struct symbol *sym)
137 struct property *prop;
139 for_all_properties(sym, prop, P_RANGE) {
140 prop->visible.tri = expr_calc_value(prop->visible.expr);
141 if (prop->visible.tri != no)
147 static int sym_get_range_val(struct symbol *sym, int base)
160 return strtol(sym->curr.val, NULL, base);
163 static void sym_validate_range(struct symbol *sym)
165 struct property *prop;
179 prop = sym_get_range_prop(sym);
182 val = strtol(sym->curr.val, NULL, base);
183 val2 = sym_get_range_val(prop->expr->left.sym, base);
185 val2 = sym_get_range_val(prop->expr->right.sym, base);
189 if (sym->type == S_INT)
190 sprintf(str, "%d", val2);
192 sprintf(str, "0x%x", val2);
193 sym->curr.val = strdup(str);
196 static void sym_calc_visibility(struct symbol *sym)
198 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) {
210 if (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))
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_calc_value(struct symbol *sym)
271 struct symbol_value newval, oldval;
272 struct property *prop;
278 if (sym->flags & SYMBOL_VALID)
280 sym->flags |= SYMBOL_VALID;
288 newval = symbol_empty.curr;
292 newval = symbol_no.curr;
295 sym->curr.val = sym->name;
299 if (!sym_is_choice_value(sym))
300 sym->flags &= ~SYMBOL_WRITE;
302 sym_calc_visibility(sym);
304 /* set default if recursively called */
307 switch (sym_get_type(sym)) {
310 if (sym_is_choice_value(sym) && sym->visible == yes) {
311 prop = sym_get_choice_prop(sym);
312 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
313 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
314 sym->flags |= SYMBOL_WRITE;
315 if (sym_has_value(sym))
316 newval.tri = sym->user.tri;
317 else if (!sym_is_choice(sym)) {
318 prop = sym_get_default_prop(sym);
320 newval.tri = expr_calc_value(prop->expr);
322 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
323 } else if (!sym_is_choice(sym)) {
324 prop = sym_get_default_prop(sym);
326 sym->flags |= SYMBOL_WRITE;
327 newval.tri = expr_calc_value(prop->expr);
330 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
336 if (sym->visible != no) {
337 sym->flags |= SYMBOL_WRITE;
338 if (sym_has_value(sym)) {
339 newval.val = sym->user.val;
343 prop = sym_get_default_prop(sym);
345 struct symbol *ds = prop_get_symbol(prop);
347 sym->flags |= SYMBOL_WRITE;
349 newval.val = ds->curr.val;
358 if (sym_is_choice(sym) && newval.tri == yes)
359 sym->curr.val = sym_calc_choice(sym);
360 sym_validate_range(sym);
362 if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
363 sym_set_changed(sym);
365 if (modules_sym == sym)
366 modules_val = modules_sym->curr.tri;
368 if (sym_is_choice(sym)) {
369 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
370 prop = sym_get_choice_prop(sym);
371 for (e = prop->expr; e; e = e->left.expr) {
372 e->right.sym->flags |= flags;
373 if (flags & SYMBOL_CHANGED)
374 sym_set_changed(e->right.sym);
379 void sym_clear_all_valid(void)
384 for_all_symbols(i, sym)
385 sym->flags &= ~SYMBOL_VALID;
388 sym_calc_value(modules_sym);
391 void sym_set_changed(struct symbol *sym)
393 struct property *prop;
395 sym->flags |= SYMBOL_CHANGED;
396 for (prop = sym->prop; prop; prop = prop->next) {
398 prop->menu->flags |= MENU_CHANGED;
402 void sym_set_all_changed(void)
407 for_all_symbols(i, sym)
408 sym_set_changed(sym);
411 bool sym_tristate_within_range(struct symbol *sym, tristate val)
413 int type = sym_get_type(sym);
415 if (sym->visible == no)
418 if (type != S_BOOLEAN && type != S_TRISTATE)
421 if (type == S_BOOLEAN && val == mod)
423 if (sym->visible <= sym->rev_dep.tri)
425 if (sym_is_choice_value(sym) && sym->visible == yes)
427 return val >= sym->rev_dep.tri && val <= sym->visible;
430 bool sym_set_tristate_value(struct symbol *sym, tristate val)
432 tristate oldval = sym_get_tristate_value(sym);
434 if (oldval != val && !sym_tristate_within_range(sym, val))
437 if (sym->flags & SYMBOL_NEW) {
438 sym->flags &= ~SYMBOL_NEW;
439 sym_set_changed(sym);
442 * setting a choice value also resets the new flag of the choice
443 * symbol and all other choice values.
445 if (sym_is_choice_value(sym) && val == yes) {
446 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
447 struct property *prop;
451 cs->flags &= ~SYMBOL_NEW;
452 prop = sym_get_choice_prop(cs);
453 for (e = prop->expr; e; e = e->left.expr) {
454 if (e->right.sym->visible != no)
455 e->right.sym->flags &= ~SYMBOL_NEW;
461 sym_clear_all_valid();
462 if (sym == modules_sym)
463 sym_set_all_changed();
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_NEW) {
603 sym->flags &= ~SYMBOL_NEW;
604 sym_set_changed(sym);
607 oldval = sym->user.val;
608 size = strlen(newval) + 1;
609 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
611 sym->user.val = val = malloc(size);
614 } else if (!oldval || strcmp(oldval, newval))
615 sym->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 isconst)
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 if ((isconst && symbol->flags & SYMBOL_CONST) ||
676 (!isconst && !(symbol->flags & SYMBOL_CONST)))
680 new_name = strdup(name);
686 symbol = malloc(sizeof(*symbol));
687 memset(symbol, 0, sizeof(*symbol));
688 symbol->name = new_name;
689 symbol->type = S_UNKNOWN;
690 symbol->flags = SYMBOL_NEW;
692 symbol->flags |= SYMBOL_CONST;
694 symbol->next = symbol_hash[hash];
695 symbol_hash[hash] = symbol;
700 struct symbol *sym_find(const char *name)
702 struct symbol *symbol = NULL;
709 if (name[0] && !name[1]) {
711 case 'y': return &symbol_yes;
712 case 'm': return &symbol_mod;
713 case 'n': return &symbol_no;
716 for (ptr = name; *ptr; ptr++)
720 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
721 if (!strcmp(symbol->name, name) &&
722 !(symbol->flags & SYMBOL_CONST))
729 struct symbol **sym_re_search(const char *pattern)
731 struct symbol *sym, **sym_arr = NULL;
737 if (strlen(pattern) == 0)
739 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
742 for_all_symbols(i, sym) {
743 if (sym->flags & SYMBOL_CONST || !sym->name)
745 if (regexec(&re, sym->name, 0, NULL, 0))
747 if (cnt + 1 >= size) {
750 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
756 sym_arr[cnt++] = sym;
766 struct symbol *sym_check_deps(struct symbol *sym);
768 static struct symbol *sym_check_expr_deps(struct expr *e)
777 sym = sym_check_expr_deps(e->left.expr);
780 return sym_check_expr_deps(e->right.expr);
782 return sym_check_expr_deps(e->left.expr);
785 sym = sym_check_deps(e->left.sym);
788 return sym_check_deps(e->right.sym);
790 return sym_check_deps(e->left.sym);
794 printf("Oops! How to check %d?\n", e->type);
798 struct symbol *sym_check_deps(struct symbol *sym)
801 struct property *prop;
803 if (sym->flags & SYMBOL_CHECK) {
804 printf("Warning! Found recursive dependency: %s", sym->name);
807 if (sym->flags & SYMBOL_CHECKED)
810 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
811 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
815 for (prop = sym->prop; prop; prop = prop->next) {
816 if (prop->type == P_CHOICE || prop->type == P_SELECT || prop->type == P_DESELECT)
818 sym2 = sym_check_expr_deps(prop->visible.expr);
821 if (prop->type != P_DEFAULT || sym_is_choice(sym))
823 sym2 = sym_check_expr_deps(prop->expr);
829 printf(" %s", sym->name);
835 sym->flags &= ~SYMBOL_CHECK;
839 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
841 struct property *prop;
842 struct property **propp;
844 prop = malloc(sizeof(*prop));
845 memset(prop, 0, sizeof(*prop));
848 prop->file = current_file;
849 prop->lineno = zconf_lineno();
851 /* append property to the prop list of symbol */
853 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
861 struct symbol *prop_get_symbol(struct property *prop)
863 if (prop->expr && (prop->expr->type == E_SYMBOL ||
864 prop->expr->type == E_CHOICE))
865 return prop->expr->left.sym;
869 const char *prop_get_type_name(enum prop_type type)