NFS: missing spaces in KERN_WARNING
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / scripts / kconfig / symbol.c
blob3929e5b35e79db7dc0d03083aabaa988e10e1992
1 /*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
6 #include <ctype.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <regex.h>
10 #include <sys/utsname.h>
12 #define LKC_DIRECT_LINK
13 #include "lkc.h"
15 struct symbol symbol_yes = {
16 .name = "y",
17 .curr = { "y", yes },
18 .flags = SYMBOL_CONST|SYMBOL_VALID,
19 }, symbol_mod = {
20 .name = "m",
21 .curr = { "m", mod },
22 .flags = SYMBOL_CONST|SYMBOL_VALID,
23 }, symbol_no = {
24 .name = "n",
25 .curr = { "n", no },
26 .flags = SYMBOL_CONST|SYMBOL_VALID,
27 }, symbol_empty = {
28 .name = "",
29 .curr = { "", no },
30 .flags = SYMBOL_VALID,
33 struct symbol *sym_defconfig_list;
34 struct symbol *modules_sym;
35 tristate modules_val;
37 struct expr *sym_env_list;
39 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, 1));
46 void sym_init(void)
48 struct symbol *sym;
49 struct utsname uts;
50 static bool inited = false;
52 if (inited)
53 return;
54 inited = true;
56 uname(&uts);
58 sym = sym_lookup("UNAME_RELEASE", 0);
59 sym->type = S_STRING;
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)
70 type = S_BOOLEAN;
71 else if (modules_val == no)
72 type = S_BOOLEAN;
74 return type;
77 const char *sym_type_name(enum symbol_type type)
79 switch (type) {
80 case S_BOOLEAN:
81 return "boolean";
82 case S_TRISTATE:
83 return "tristate";
84 case S_INT:
85 return "integer";
86 case S_HEX:
87 return "hex";
88 case S_STRING:
89 return "string";
90 case S_UNKNOWN:
91 return "unknown";
92 case S_OTHER:
93 break;
95 return "???";
98 struct property *sym_get_choice_prop(struct symbol *sym)
100 struct property *prop;
102 for_all_choices(sym, prop)
103 return prop;
104 return NULL;
107 struct property *sym_get_env_prop(struct symbol *sym)
109 struct property *prop;
111 for_all_properties(sym, prop, P_ENV)
112 return prop;
113 return NULL;
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)
123 return prop;
125 return NULL;
128 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)
135 return prop;
137 return NULL;
140 static int sym_get_range_val(struct symbol *sym, int base)
142 sym_calc_value(sym);
143 switch (sym->type) {
144 case S_INT:
145 base = 10;
146 break;
147 case S_HEX:
148 base = 16;
149 break;
150 default:
151 break;
153 return strtol(sym->curr.val, NULL, base);
156 static void sym_validate_range(struct symbol *sym)
158 struct property *prop;
159 int base, val, val2;
160 char str[64];
162 switch (sym->type) {
163 case S_INT:
164 base = 10;
165 break;
166 case S_HEX:
167 base = 16;
168 break;
169 default:
170 return;
172 prop = sym_get_range_prop(sym);
173 if (!prop)
174 return;
175 val = strtol(sym->curr.val, NULL, base);
176 val2 = sym_get_range_val(prop->expr->left.sym, base);
177 if (val >= val2) {
178 val2 = sym_get_range_val(prop->expr->right.sym, base);
179 if (val <= val2)
180 return;
182 if (sym->type == S_INT)
183 sprintf(str, "%d", val2);
184 else
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;
192 tristate tri;
194 /* any prompt visible? */
195 tri = no;
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))
201 tri = yes;
202 if (sym->visible != tri) {
203 sym->visible = tri;
204 sym_set_changed(sym);
206 if (sym_is_choice_value(sym))
207 return;
208 tri = no;
209 if (sym->rev_dep.expr)
210 tri = expr_calc_value(sym->rev_dep.expr);
211 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
212 tri = yes;
213 if (sym->rev_dep.tri != tri) {
214 sym->rev_dep.tri = tri;
215 sym_set_changed(sym);
219 static struct symbol *sym_calc_choice(struct symbol *sym)
221 struct symbol *def_sym;
222 struct property *prop;
223 struct expr *e;
225 /* is the user choice visible? */
226 def_sym = sym->def[S_DEF_USER].val;
227 if (def_sym) {
228 sym_calc_visibility(def_sym);
229 if (def_sym->visible != no)
230 return def_sym;
233 /* any of the defaults visible? */
234 for_all_defaults(sym, prop) {
235 prop->visible.tri = expr_calc_value(prop->visible.expr);
236 if (prop->visible.tri == no)
237 continue;
238 def_sym = prop_get_symbol(prop);
239 sym_calc_visibility(def_sym);
240 if (def_sym->visible != no)
241 return def_sym;
244 /* just get the first visible value */
245 prop = sym_get_choice_prop(sym);
246 expr_list_for_each_sym(prop->expr, e, def_sym) {
247 sym_calc_visibility(def_sym);
248 if (def_sym->visible != no)
249 return def_sym;
252 /* no choice? reset tristate value */
253 sym->curr.tri = no;
254 return NULL;
257 void sym_calc_value(struct symbol *sym)
259 struct symbol_value newval, oldval;
260 struct property *prop;
261 struct expr *e;
263 if (!sym)
264 return;
266 if (sym->flags & SYMBOL_VALID)
267 return;
268 sym->flags |= SYMBOL_VALID;
270 oldval = sym->curr;
272 switch (sym->type) {
273 case S_INT:
274 case S_HEX:
275 case S_STRING:
276 newval = symbol_empty.curr;
277 break;
278 case S_BOOLEAN:
279 case S_TRISTATE:
280 newval = symbol_no.curr;
281 break;
282 default:
283 sym->curr.val = sym->name;
284 sym->curr.tri = no;
285 return;
287 if (!sym_is_choice_value(sym))
288 sym->flags &= ~SYMBOL_WRITE;
290 sym_calc_visibility(sym);
292 /* set default if recursively called */
293 sym->curr = newval;
295 switch (sym_get_type(sym)) {
296 case S_BOOLEAN:
297 case S_TRISTATE:
298 if (sym_is_choice_value(sym) && sym->visible == yes) {
299 prop = sym_get_choice_prop(sym);
300 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
301 } else if (EXPR_OR(sym->visible, sym->rev_dep.tri) != no) {
302 sym->flags |= SYMBOL_WRITE;
303 if (sym_has_value(sym))
304 newval.tri = sym->def[S_DEF_USER].tri;
305 else if (!sym_is_choice(sym)) {
306 prop = sym_get_default_prop(sym);
307 if (prop)
308 newval.tri = expr_calc_value(prop->expr);
310 newval.tri = EXPR_OR(EXPR_AND(newval.tri, sym->visible), sym->rev_dep.tri);
311 } else if (!sym_is_choice(sym)) {
312 prop = sym_get_default_prop(sym);
313 if (prop) {
314 sym->flags |= SYMBOL_WRITE;
315 newval.tri = expr_calc_value(prop->expr);
318 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
319 newval.tri = yes;
320 break;
321 case S_STRING:
322 case S_HEX:
323 case S_INT:
324 if (sym->visible != no) {
325 sym->flags |= SYMBOL_WRITE;
326 if (sym_has_value(sym)) {
327 newval.val = sym->def[S_DEF_USER].val;
328 break;
331 prop = sym_get_default_prop(sym);
332 if (prop) {
333 struct symbol *ds = prop_get_symbol(prop);
334 if (ds) {
335 sym->flags |= SYMBOL_WRITE;
336 sym_calc_value(ds);
337 newval.val = ds->curr.val;
340 break;
341 default:
345 if (sym->flags & SYMBOL_AUTO)
346 sym->flags &= ~SYMBOL_WRITE;
348 sym->curr = newval;
349 if (sym_is_choice(sym) && newval.tri == yes)
350 sym->curr.val = sym_calc_choice(sym);
351 sym_validate_range(sym);
353 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
354 sym_set_changed(sym);
355 if (modules_sym == sym) {
356 sym_set_all_changed();
357 modules_val = modules_sym->curr.tri;
361 if (sym_is_choice(sym)) {
362 struct symbol *choice_sym;
363 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
365 prop = sym_get_choice_prop(sym);
366 expr_list_for_each_sym(prop->expr, e, choice_sym) {
367 choice_sym->flags |= flags;
368 if (flags & SYMBOL_CHANGED)
369 sym_set_changed(choice_sym);
374 void sym_clear_all_valid(void)
376 struct symbol *sym;
377 int i;
379 for_all_symbols(i, sym)
380 sym->flags &= ~SYMBOL_VALID;
381 sym_add_change_count(1);
382 if (modules_sym)
383 sym_calc_value(modules_sym);
386 void sym_set_changed(struct symbol *sym)
388 struct property *prop;
390 sym->flags |= SYMBOL_CHANGED;
391 for (prop = sym->prop; prop; prop = prop->next) {
392 if (prop->menu)
393 prop->menu->flags |= MENU_CHANGED;
397 void sym_set_all_changed(void)
399 struct symbol *sym;
400 int i;
402 for_all_symbols(i, sym)
403 sym_set_changed(sym);
406 bool sym_tristate_within_range(struct symbol *sym, tristate val)
408 int type = sym_get_type(sym);
410 if (sym->visible == no)
411 return false;
413 if (type != S_BOOLEAN && type != S_TRISTATE)
414 return false;
416 if (type == S_BOOLEAN && val == mod)
417 return false;
418 if (sym->visible <= sym->rev_dep.tri)
419 return false;
420 if (sym_is_choice_value(sym) && sym->visible == yes)
421 return val == yes;
422 return val >= sym->rev_dep.tri && val <= sym->visible;
425 bool sym_set_tristate_value(struct symbol *sym, tristate val)
427 tristate oldval = sym_get_tristate_value(sym);
429 if (oldval != val && !sym_tristate_within_range(sym, val))
430 return false;
432 if (!(sym->flags & SYMBOL_DEF_USER)) {
433 sym->flags |= SYMBOL_DEF_USER;
434 sym_set_changed(sym);
437 * setting a choice value also resets the new flag of the choice
438 * symbol and all other choice values.
440 if (sym_is_choice_value(sym) && val == yes) {
441 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
442 struct property *prop;
443 struct expr *e;
445 cs->def[S_DEF_USER].val = sym;
446 cs->flags |= SYMBOL_DEF_USER;
447 prop = sym_get_choice_prop(cs);
448 for (e = prop->expr; e; e = e->left.expr) {
449 if (e->right.sym->visible != no)
450 e->right.sym->flags |= SYMBOL_DEF_USER;
454 sym->def[S_DEF_USER].tri = val;
455 if (oldval != val)
456 sym_clear_all_valid();
458 return true;
461 tristate sym_toggle_tristate_value(struct symbol *sym)
463 tristate oldval, newval;
465 oldval = newval = sym_get_tristate_value(sym);
466 do {
467 switch (newval) {
468 case no:
469 newval = mod;
470 break;
471 case mod:
472 newval = yes;
473 break;
474 case yes:
475 newval = no;
476 break;
478 if (sym_set_tristate_value(sym, newval))
479 break;
480 } while (oldval != newval);
481 return newval;
484 bool sym_string_valid(struct symbol *sym, const char *str)
486 signed char ch;
488 switch (sym->type) {
489 case S_STRING:
490 return true;
491 case S_INT:
492 ch = *str++;
493 if (ch == '-')
494 ch = *str++;
495 if (!isdigit(ch))
496 return false;
497 if (ch == '0' && *str != 0)
498 return false;
499 while ((ch = *str++)) {
500 if (!isdigit(ch))
501 return false;
503 return true;
504 case S_HEX:
505 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
506 str += 2;
507 ch = *str++;
508 do {
509 if (!isxdigit(ch))
510 return false;
511 } while ((ch = *str++));
512 return true;
513 case S_BOOLEAN:
514 case S_TRISTATE:
515 switch (str[0]) {
516 case 'y': case 'Y':
517 case 'm': case 'M':
518 case 'n': case 'N':
519 return true;
521 return false;
522 default:
523 return false;
527 bool sym_string_within_range(struct symbol *sym, const char *str)
529 struct property *prop;
530 int val;
532 switch (sym->type) {
533 case S_STRING:
534 return sym_string_valid(sym, str);
535 case S_INT:
536 if (!sym_string_valid(sym, str))
537 return false;
538 prop = sym_get_range_prop(sym);
539 if (!prop)
540 return true;
541 val = strtol(str, NULL, 10);
542 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
543 val <= sym_get_range_val(prop->expr->right.sym, 10);
544 case S_HEX:
545 if (!sym_string_valid(sym, str))
546 return false;
547 prop = sym_get_range_prop(sym);
548 if (!prop)
549 return true;
550 val = strtol(str, NULL, 16);
551 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
552 val <= sym_get_range_val(prop->expr->right.sym, 16);
553 case S_BOOLEAN:
554 case S_TRISTATE:
555 switch (str[0]) {
556 case 'y': case 'Y':
557 return sym_tristate_within_range(sym, yes);
558 case 'm': case 'M':
559 return sym_tristate_within_range(sym, mod);
560 case 'n': case 'N':
561 return sym_tristate_within_range(sym, no);
563 return false;
564 default:
565 return false;
569 bool sym_set_string_value(struct symbol *sym, const char *newval)
571 const char *oldval;
572 char *val;
573 int size;
575 switch (sym->type) {
576 case S_BOOLEAN:
577 case S_TRISTATE:
578 switch (newval[0]) {
579 case 'y': case 'Y':
580 return sym_set_tristate_value(sym, yes);
581 case 'm': case 'M':
582 return sym_set_tristate_value(sym, mod);
583 case 'n': case 'N':
584 return sym_set_tristate_value(sym, no);
586 return false;
587 default:
591 if (!sym_string_within_range(sym, newval))
592 return false;
594 if (!(sym->flags & SYMBOL_DEF_USER)) {
595 sym->flags |= SYMBOL_DEF_USER;
596 sym_set_changed(sym);
599 oldval = sym->def[S_DEF_USER].val;
600 size = strlen(newval) + 1;
601 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
602 size += 2;
603 sym->def[S_DEF_USER].val = val = malloc(size);
604 *val++ = '0';
605 *val++ = 'x';
606 } else if (!oldval || strcmp(oldval, newval))
607 sym->def[S_DEF_USER].val = val = malloc(size);
608 else
609 return true;
611 strcpy(val, newval);
612 free((void *)oldval);
613 sym_clear_all_valid();
615 return true;
618 const char *sym_get_string_value(struct symbol *sym)
620 tristate val;
622 switch (sym->type) {
623 case S_BOOLEAN:
624 case S_TRISTATE:
625 val = sym_get_tristate_value(sym);
626 switch (val) {
627 case no:
628 return "n";
629 case mod:
630 return "m";
631 case yes:
632 return "y";
634 break;
635 default:
638 return (const char *)sym->curr.val;
641 bool sym_is_changable(struct symbol *sym)
643 return sym->visible > sym->rev_dep.tri;
646 struct symbol *sym_lookup(const char *name, int isconst)
648 struct symbol *symbol;
649 const char *ptr;
650 char *new_name;
651 int hash = 0;
653 if (name) {
654 if (name[0] && !name[1]) {
655 switch (name[0]) {
656 case 'y': return &symbol_yes;
657 case 'm': return &symbol_mod;
658 case 'n': return &symbol_no;
661 for (ptr = name; *ptr; ptr++)
662 hash += *ptr;
663 hash &= 0xff;
665 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
666 if (!strcmp(symbol->name, name)) {
667 if ((isconst && symbol->flags & SYMBOL_CONST) ||
668 (!isconst && !(symbol->flags & SYMBOL_CONST)))
669 return symbol;
672 new_name = strdup(name);
673 } else {
674 new_name = NULL;
675 hash = 256;
678 symbol = malloc(sizeof(*symbol));
679 memset(symbol, 0, sizeof(*symbol));
680 symbol->name = new_name;
681 symbol->type = S_UNKNOWN;
682 if (isconst)
683 symbol->flags |= SYMBOL_CONST;
685 symbol->next = symbol_hash[hash];
686 symbol_hash[hash] = symbol;
688 return symbol;
691 struct symbol *sym_find(const char *name)
693 struct symbol *symbol = NULL;
694 const char *ptr;
695 int hash = 0;
697 if (!name)
698 return NULL;
700 if (name[0] && !name[1]) {
701 switch (name[0]) {
702 case 'y': return &symbol_yes;
703 case 'm': return &symbol_mod;
704 case 'n': return &symbol_no;
707 for (ptr = name; *ptr; ptr++)
708 hash += *ptr;
709 hash &= 0xff;
711 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
712 if (!strcmp(symbol->name, name) &&
713 !(symbol->flags & SYMBOL_CONST))
714 break;
717 return symbol;
720 struct symbol **sym_re_search(const char *pattern)
722 struct symbol *sym, **sym_arr = NULL;
723 int i, cnt, size;
724 regex_t re;
726 cnt = size = 0;
727 /* Skip if empty */
728 if (strlen(pattern) == 0)
729 return NULL;
730 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
731 return NULL;
733 for_all_symbols(i, sym) {
734 if (sym->flags & SYMBOL_CONST || !sym->name)
735 continue;
736 if (regexec(&re, sym->name, 0, NULL, 0))
737 continue;
738 if (cnt + 1 >= size) {
739 void *tmp = sym_arr;
740 size += 16;
741 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
742 if (!sym_arr) {
743 free(tmp);
744 return NULL;
747 sym_arr[cnt++] = sym;
749 if (sym_arr)
750 sym_arr[cnt] = NULL;
751 regfree(&re);
753 return sym_arr;
757 struct symbol *sym_check_deps(struct symbol *sym);
759 static struct symbol *sym_check_expr_deps(struct expr *e)
761 struct symbol *sym;
763 if (!e)
764 return NULL;
765 switch (e->type) {
766 case E_OR:
767 case E_AND:
768 sym = sym_check_expr_deps(e->left.expr);
769 if (sym)
770 return sym;
771 return sym_check_expr_deps(e->right.expr);
772 case E_NOT:
773 return sym_check_expr_deps(e->left.expr);
774 case E_EQUAL:
775 case E_UNEQUAL:
776 sym = sym_check_deps(e->left.sym);
777 if (sym)
778 return sym;
779 return sym_check_deps(e->right.sym);
780 case E_SYMBOL:
781 return sym_check_deps(e->left.sym);
782 default:
783 break;
785 printf("Oops! How to check %d?\n", e->type);
786 return NULL;
789 /* return NULL when dependencies are OK */
790 struct symbol *sym_check_deps(struct symbol *sym)
792 struct symbol *sym2;
793 struct property *prop;
795 if (sym->flags & SYMBOL_CHECK) {
796 fprintf(stderr, "%s:%d:error: found recursive dependency: %s",
797 sym->prop->file->name, sym->prop->lineno, sym->name);
798 return sym;
800 if (sym->flags & SYMBOL_CHECKED)
801 return NULL;
803 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
804 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
805 if (sym2)
806 goto out;
808 for (prop = sym->prop; prop; prop = prop->next) {
809 if (prop->type == P_CHOICE || prop->type == P_SELECT)
810 continue;
811 sym2 = sym_check_expr_deps(prop->visible.expr);
812 if (sym2)
813 goto out;
814 if (prop->type != P_DEFAULT || sym_is_choice(sym))
815 continue;
816 sym2 = sym_check_expr_deps(prop->expr);
817 if (sym2)
818 goto out;
820 out:
821 if (sym2)
822 fprintf(stderr, " -> %s%s", sym->name, sym2 == sym? "\n": "");
823 sym->flags &= ~SYMBOL_CHECK;
824 return sym2;
827 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
829 struct property *prop;
830 struct property **propp;
832 prop = malloc(sizeof(*prop));
833 memset(prop, 0, sizeof(*prop));
834 prop->type = type;
835 prop->sym = sym;
836 prop->file = current_file;
837 prop->lineno = zconf_lineno();
839 /* append property to the prop list of symbol */
840 if (sym) {
841 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
843 *propp = prop;
846 return prop;
849 struct symbol *prop_get_symbol(struct property *prop)
851 if (prop->expr && (prop->expr->type == E_SYMBOL ||
852 prop->expr->type == E_LIST))
853 return prop->expr->left.sym;
854 return NULL;
857 const char *prop_get_type_name(enum prop_type type)
859 switch (type) {
860 case P_PROMPT:
861 return "prompt";
862 case P_ENV:
863 return "env";
864 case P_COMMENT:
865 return "comment";
866 case P_MENU:
867 return "menu";
868 case P_DEFAULT:
869 return "default";
870 case P_CHOICE:
871 return "choice";
872 case P_SELECT:
873 return "select";
874 case P_RANGE:
875 return "range";
876 case P_UNKNOWN:
877 break;
879 return "unknown";
882 void prop_add_env(const char *env)
884 struct symbol *sym, *sym2;
885 struct property *prop;
886 char *p;
888 sym = current_entry->sym;
889 sym->flags |= SYMBOL_AUTO;
890 for_all_properties(sym, prop, P_ENV) {
891 sym2 = prop_get_symbol(prop);
892 if (strcmp(sym2->name, env))
893 menu_warn(current_entry, "redefining environment symbol from %s",
894 sym2->name);
895 return;
898 prop = prop_alloc(P_ENV, sym);
899 prop->expr = expr_alloc_symbol(sym_lookup(env, 1));
901 sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
902 sym_env_list->right.sym = sym;
904 p = getenv(env);
905 if (p)
906 sym_add_default(sym, p);
907 else
908 menu_warn(current_entry, "environment variable %s undefined", env);