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 /* tristate always enabled */
209 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
211 if (tri == mod && (sym->type != S_TRISTATE))
214 if (sym->visible != tri) {
216 sym_set_changed(sym);
218 if (sym_is_choice_value(sym))
221 if (sym->rev_dep.expr)
222 tri = expr_calc_value(sym->rev_dep.expr);
223 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
225 if (sym->rev_dep.tri != tri) {
226 sym->rev_dep.tri = tri;
227 sym_set_changed(sym);
231 static struct symbol *sym_calc_choice(struct symbol *sym)
233 struct symbol *def_sym;
234 struct property *prop;
237 /* is the user choice visible? */
238 def_sym = sym->user.val;
240 sym_calc_visibility(def_sym);
241 if (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 sym_calc_visibility(def_sym);
252 if (def_sym->visible != no)
256 /* just get the first visible value */
257 prop = sym_get_choice_prop(sym);
258 for (e = prop->expr; e; e = e->left.expr) {
259 def_sym = e->right.sym;
260 sym_calc_visibility(def_sym);
261 if (def_sym->visible != no)
265 /* no choice? reset tristate value */
270 void sym_calc_value(struct symbol *sym)
272 struct symbol_value newval, oldval;
273 struct property *prop;
279 if (sym->flags & SYMBOL_VALID)
281 sym->flags |= SYMBOL_VALID;
289 newval = symbol_empty.curr;
293 newval = symbol_no.curr;
296 sym->curr.val = sym->name;
300 if (!sym_is_choice_value(sym))
301 sym->flags &= ~SYMBOL_WRITE;
303 sym_calc_visibility(sym);
305 /* set default if recursively called */
308 switch (sym_get_type(sym)) {
311 if (sym_is_choice_value(sym) && sym->visible == yes) {
312 prop = sym_get_choice_prop(sym);
313 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
314 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
315 sym->flags |= SYMBOL_WRITE;
316 if (sym_has_value(sym))
317 newval.tri = sym->user.tri;
318 else if (!sym_is_choice(sym)) {
319 prop = sym_get_default_prop(sym);
321 newval.tri = expr_calc_value(prop->expr);
323 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
324 } else if (!sym_is_choice(sym)) {
325 prop = sym_get_default_prop(sym);
327 sym->flags |= SYMBOL_WRITE;
328 newval.tri = expr_calc_value(prop->expr);
331 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
337 if (sym->visible != no) {
338 sym->flags |= SYMBOL_WRITE;
339 if (sym_has_value(sym)) {
340 newval.val = sym->user.val;
344 prop = sym_get_default_prop(sym);
346 struct symbol *ds = prop_get_symbol(prop);
348 sym->flags |= SYMBOL_WRITE;
350 newval.val = ds->curr.val;
359 if (sym_is_choice(sym) && newval.tri == yes)
360 sym->curr.val = sym_calc_choice(sym);
361 sym_validate_range(sym);
363 if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
364 sym_set_changed(sym);
366 if (modules_sym == sym)
367 modules_val = modules_sym->curr.tri;
369 if (sym_is_choice(sym)) {
370 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
371 prop = sym_get_choice_prop(sym);
372 for (e = prop->expr; e; e = e->left.expr) {
373 e->right.sym->flags |= flags;
374 if (flags & SYMBOL_CHANGED)
375 sym_set_changed(e->right.sym);
380 void sym_clear_all_valid(void)
385 for_all_symbols(i, sym)
386 sym->flags &= ~SYMBOL_VALID;
389 sym_calc_value(modules_sym);
392 void sym_set_changed(struct symbol *sym)
394 struct property *prop;
396 sym->flags |= SYMBOL_CHANGED;
397 for (prop = sym->prop; prop; prop = prop->next) {
399 prop->menu->flags |= MENU_CHANGED;
403 void sym_set_all_changed(void)
408 for_all_symbols(i, sym)
409 sym_set_changed(sym);
412 bool sym_tristate_within_range(struct symbol *sym, tristate val)
414 int type = sym_get_type(sym);
416 if (sym->visible == no)
419 if (type != S_BOOLEAN && type != S_TRISTATE)
422 if (type == S_BOOLEAN && val == mod)
424 if (sym->visible <= sym->rev_dep.tri)
426 if (sym_is_choice_value(sym) && sym->visible == yes)
428 return val >= sym->rev_dep.tri && val <= sym->visible;
431 bool sym_set_tristate_value(struct symbol *sym, tristate val)
433 tristate oldval = sym_get_tristate_value(sym);
435 if (oldval != val && !sym_tristate_within_range(sym, val))
438 if (sym->flags & SYMBOL_NEW) {
439 sym->flags &= ~SYMBOL_NEW;
440 sym_set_changed(sym);
443 * setting a choice value also resets the new flag of the choice
444 * symbol and all other choice values.
446 if (sym_is_choice_value(sym) && val == yes) {
447 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
448 struct property *prop;
452 cs->flags &= ~SYMBOL_NEW;
453 prop = sym_get_choice_prop(cs);
454 for (e = prop->expr; e; e = e->left.expr) {
455 if (e->right.sym->visible != no)
456 e->right.sym->flags &= ~SYMBOL_NEW;
462 sym_clear_all_valid();
463 if (sym == modules_sym)
464 sym_set_all_changed();
470 tristate sym_toggle_tristate_value(struct symbol *sym)
472 tristate oldval, newval;
474 oldval = newval = sym_get_tristate_value(sym);
487 if (sym_set_tristate_value(sym, newval))
489 } while (oldval != newval);
493 bool sym_string_valid(struct symbol *sym, const char *str)
506 if (ch == '0' && *str != 0)
508 while ((ch = *str++)) {
514 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
520 } while ((ch = *str++));
536 bool sym_string_within_range(struct symbol *sym, const char *str)
538 struct property *prop;
543 return sym_string_valid(sym, str);
545 if (!sym_string_valid(sym, str))
547 prop = sym_get_range_prop(sym);
550 val = strtol(str, NULL, 10);
551 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
552 val <= sym_get_range_val(prop->expr->right.sym, 10);
554 if (!sym_string_valid(sym, str))
556 prop = sym_get_range_prop(sym);
559 val = strtol(str, NULL, 16);
560 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
561 val <= sym_get_range_val(prop->expr->right.sym, 16);
566 return sym_tristate_within_range(sym, yes);
568 return sym_tristate_within_range(sym, mod);
570 return sym_tristate_within_range(sym, no);
578 bool sym_set_string_value(struct symbol *sym, const char *newval)
589 return sym_set_tristate_value(sym, yes);
591 return sym_set_tristate_value(sym, mod);
593 return sym_set_tristate_value(sym, no);
600 if (!sym_string_within_range(sym, newval))
603 if (sym->flags & SYMBOL_NEW) {
604 sym->flags &= ~SYMBOL_NEW;
605 sym_set_changed(sym);
608 oldval = sym->user.val;
609 size = strlen(newval) + 1;
610 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
612 sym->user.val = val = malloc(size);
615 } else if (!oldval || strcmp(oldval, newval))
616 sym->user.val = val = malloc(size);
621 free((void *)oldval);
622 sym_clear_all_valid();
627 const char *sym_get_string_value(struct symbol *sym)
634 val = sym_get_tristate_value(sym);
647 return (const char *)sym->curr.val;
650 bool sym_is_changable(struct symbol *sym)
652 return sym->visible > sym->rev_dep.tri;
655 struct symbol *sym_lookup(const char *name, int isconst)
657 struct symbol *symbol;
663 if (name[0] && !name[1]) {
665 case 'y': return &symbol_yes;
666 case 'm': return &symbol_mod;
667 case 'n': return &symbol_no;
670 for (ptr = name; *ptr; ptr++)
674 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
675 if (!strcmp(symbol->name, name)) {
676 if ((isconst && symbol->flags & SYMBOL_CONST) ||
677 (!isconst && !(symbol->flags & SYMBOL_CONST)))
681 new_name = strdup(name);
687 symbol = malloc(sizeof(*symbol));
688 memset(symbol, 0, sizeof(*symbol));
689 symbol->name = new_name;
690 symbol->type = S_UNKNOWN;
691 symbol->flags = SYMBOL_NEW;
693 symbol->flags |= SYMBOL_CONST;
695 symbol->next = symbol_hash[hash];
696 symbol_hash[hash] = symbol;
701 struct symbol *sym_find(const char *name)
703 struct symbol *symbol = NULL;
710 if (name[0] && !name[1]) {
712 case 'y': return &symbol_yes;
713 case 'm': return &symbol_mod;
714 case 'n': return &symbol_no;
717 for (ptr = name; *ptr; ptr++)
721 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
722 if (!strcmp(symbol->name, name) &&
723 !(symbol->flags & SYMBOL_CONST))
730 struct symbol **sym_re_search(const char *pattern)
732 struct symbol *sym, **sym_arr = NULL;
738 if (strlen(pattern) == 0)
740 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
743 for_all_symbols(i, sym) {
744 if (sym->flags & SYMBOL_CONST || !sym->name)
746 if (regexec(&re, sym->name, 0, NULL, 0))
748 if (cnt + 1 >= size) {
751 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
757 sym_arr[cnt++] = sym;
767 struct symbol *sym_check_deps(struct symbol *sym);
769 static struct symbol *sym_check_expr_deps(struct expr *e)
778 sym = sym_check_expr_deps(e->left.expr);
781 return sym_check_expr_deps(e->right.expr);
783 return sym_check_expr_deps(e->left.expr);
786 sym = sym_check_deps(e->left.sym);
789 return sym_check_deps(e->right.sym);
791 return sym_check_deps(e->left.sym);
795 printf("Oops! How to check %d?\n", e->type);
799 struct symbol *sym_check_deps(struct symbol *sym)
802 struct property *prop;
804 if (sym->flags & SYMBOL_CHECK) {
805 printf("Warning! Found recursive dependency: %s", sym->name);
808 if (sym->flags & SYMBOL_CHECKED)
811 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
812 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
816 for (prop = sym->prop; prop; prop = prop->next) {
817 if (prop->type == P_CHOICE || prop->type == P_SELECT)
819 sym2 = sym_check_expr_deps(prop->visible.expr);
822 if (prop->type != P_DEFAULT || sym_is_choice(sym))
824 sym2 = sym_check_expr_deps(prop->expr);
830 printf(" %s", sym->name);
836 sym->flags &= ~SYMBOL_CHECK;
840 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
842 struct property *prop;
843 struct property **propp;
845 prop = malloc(sizeof(*prop));
846 memset(prop, 0, sizeof(*prop));
849 prop->file = current_file;
850 prop->lineno = zconf_lineno();
852 /* append property to the prop list of symbol */
854 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
862 struct symbol *prop_get_symbol(struct property *prop)
864 if (prop->expr && (prop->expr->type == E_SYMBOL ||
865 prop->expr->type == E_CHOICE))
866 return prop->expr->left.sym;
870 const char *prop_get_type_name(enum prop_type type)