Linux 4.18.10
[linux/fpc-iii.git] / scripts / kconfig / symbol.c
blob7c9a88e91cfad95f8fa2e350c2dcccb92981d36f
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 #include "lkc.h"
14 struct symbol symbol_yes = {
15 .name = "y",
16 .curr = { "y", yes },
17 .flags = SYMBOL_CONST|SYMBOL_VALID,
18 }, symbol_mod = {
19 .name = "m",
20 .curr = { "m", mod },
21 .flags = SYMBOL_CONST|SYMBOL_VALID,
22 }, symbol_no = {
23 .name = "n",
24 .curr = { "n", no },
25 .flags = SYMBOL_CONST|SYMBOL_VALID,
26 }, symbol_empty = {
27 .name = "",
28 .curr = { "", no },
29 .flags = SYMBOL_VALID,
32 struct symbol *sym_defconfig_list;
33 struct symbol *modules_sym;
34 tristate modules_val;
36 enum symbol_type sym_get_type(struct symbol *sym)
38 enum symbol_type type = sym->type;
40 if (type == S_TRISTATE) {
41 if (sym_is_choice_value(sym) && sym->visible == yes)
42 type = S_BOOLEAN;
43 else if (modules_val == no)
44 type = S_BOOLEAN;
46 return type;
49 const char *sym_type_name(enum symbol_type type)
51 switch (type) {
52 case S_BOOLEAN:
53 return "bool";
54 case S_TRISTATE:
55 return "tristate";
56 case S_INT:
57 return "integer";
58 case S_HEX:
59 return "hex";
60 case S_STRING:
61 return "string";
62 case S_UNKNOWN:
63 return "unknown";
64 case S_OTHER:
65 break;
67 return "???";
70 struct property *sym_get_choice_prop(struct symbol *sym)
72 struct property *prop;
74 for_all_choices(sym, prop)
75 return prop;
76 return NULL;
79 struct property *sym_get_env_prop(struct symbol *sym)
81 struct property *prop;
83 for_all_properties(sym, prop, P_ENV)
84 return prop;
85 return NULL;
88 static struct property *sym_get_default_prop(struct symbol *sym)
90 struct property *prop;
92 for_all_defaults(sym, prop) {
93 prop->visible.tri = expr_calc_value(prop->visible.expr);
94 if (prop->visible.tri != no)
95 return prop;
97 return NULL;
100 static struct property *sym_get_range_prop(struct symbol *sym)
102 struct property *prop;
104 for_all_properties(sym, prop, P_RANGE) {
105 prop->visible.tri = expr_calc_value(prop->visible.expr);
106 if (prop->visible.tri != no)
107 return prop;
109 return NULL;
112 static long long sym_get_range_val(struct symbol *sym, int base)
114 sym_calc_value(sym);
115 switch (sym->type) {
116 case S_INT:
117 base = 10;
118 break;
119 case S_HEX:
120 base = 16;
121 break;
122 default:
123 break;
125 return strtoll(sym->curr.val, NULL, base);
128 static void sym_validate_range(struct symbol *sym)
130 struct property *prop;
131 int base;
132 long long val, val2;
133 char str[64];
135 switch (sym->type) {
136 case S_INT:
137 base = 10;
138 break;
139 case S_HEX:
140 base = 16;
141 break;
142 default:
143 return;
145 prop = sym_get_range_prop(sym);
146 if (!prop)
147 return;
148 val = strtoll(sym->curr.val, NULL, base);
149 val2 = sym_get_range_val(prop->expr->left.sym, base);
150 if (val >= val2) {
151 val2 = sym_get_range_val(prop->expr->right.sym, base);
152 if (val <= val2)
153 return;
155 if (sym->type == S_INT)
156 sprintf(str, "%lld", val2);
157 else
158 sprintf(str, "0x%llx", val2);
159 sym->curr.val = xstrdup(str);
162 static void sym_set_changed(struct symbol *sym)
164 struct property *prop;
166 sym->flags |= SYMBOL_CHANGED;
167 for (prop = sym->prop; prop; prop = prop->next) {
168 if (prop->menu)
169 prop->menu->flags |= MENU_CHANGED;
173 static void sym_set_all_changed(void)
175 struct symbol *sym;
176 int i;
178 for_all_symbols(i, sym)
179 sym_set_changed(sym);
182 static void sym_calc_visibility(struct symbol *sym)
184 struct property *prop;
185 struct symbol *choice_sym = NULL;
186 tristate tri;
188 /* any prompt visible? */
189 tri = no;
191 if (sym_is_choice_value(sym))
192 choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
194 for_all_prompts(sym, prop) {
195 prop->visible.tri = expr_calc_value(prop->visible.expr);
197 * Tristate choice_values with visibility 'mod' are
198 * not visible if the corresponding choice's value is
199 * 'yes'.
201 if (choice_sym && sym->type == S_TRISTATE &&
202 prop->visible.tri == mod && choice_sym->curr.tri == yes)
203 prop->visible.tri = no;
205 tri = EXPR_OR(tri, prop->visible.tri);
207 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
208 tri = yes;
209 if (sym->visible != tri) {
210 sym->visible = tri;
211 sym_set_changed(sym);
213 if (sym_is_choice_value(sym))
214 return;
215 /* defaulting to "yes" if no explicit "depends on" are given */
216 tri = yes;
217 if (sym->dir_dep.expr)
218 tri = expr_calc_value(sym->dir_dep.expr);
219 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
220 tri = yes;
221 if (sym->dir_dep.tri != tri) {
222 sym->dir_dep.tri = tri;
223 sym_set_changed(sym);
225 tri = no;
226 if (sym->rev_dep.expr)
227 tri = expr_calc_value(sym->rev_dep.expr);
228 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
229 tri = yes;
230 if (sym->rev_dep.tri != tri) {
231 sym->rev_dep.tri = tri;
232 sym_set_changed(sym);
234 tri = no;
235 if (sym->implied.expr && sym->dir_dep.tri != no)
236 tri = expr_calc_value(sym->implied.expr);
237 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
238 tri = yes;
239 if (sym->implied.tri != tri) {
240 sym->implied.tri = tri;
241 sym_set_changed(sym);
246 * Find the default symbol for a choice.
247 * First try the default values for the choice symbol
248 * Next locate the first visible choice value
249 * Return NULL if none was found
251 struct symbol *sym_choice_default(struct symbol *sym)
253 struct symbol *def_sym;
254 struct property *prop;
255 struct expr *e;
257 /* any of the defaults visible? */
258 for_all_defaults(sym, prop) {
259 prop->visible.tri = expr_calc_value(prop->visible.expr);
260 if (prop->visible.tri == no)
261 continue;
262 def_sym = prop_get_symbol(prop);
263 if (def_sym->visible != no)
264 return def_sym;
267 /* just get the first visible value */
268 prop = sym_get_choice_prop(sym);
269 expr_list_for_each_sym(prop->expr, e, def_sym)
270 if (def_sym->visible != no)
271 return def_sym;
273 /* failed to locate any defaults */
274 return NULL;
277 static struct symbol *sym_calc_choice(struct symbol *sym)
279 struct symbol *def_sym;
280 struct property *prop;
281 struct expr *e;
282 int flags;
284 /* first calculate all choice values' visibilities */
285 flags = sym->flags;
286 prop = sym_get_choice_prop(sym);
287 expr_list_for_each_sym(prop->expr, e, def_sym) {
288 sym_calc_visibility(def_sym);
289 if (def_sym->visible != no)
290 flags &= def_sym->flags;
293 sym->flags &= flags | ~SYMBOL_DEF_USER;
295 /* is the user choice visible? */
296 def_sym = sym->def[S_DEF_USER].val;
297 if (def_sym && def_sym->visible != no)
298 return def_sym;
300 def_sym = sym_choice_default(sym);
302 if (def_sym == NULL)
303 /* no choice? reset tristate value */
304 sym->curr.tri = no;
306 return def_sym;
309 static void sym_warn_unmet_dep(struct symbol *sym)
311 struct gstr gs = str_new();
313 str_printf(&gs,
314 "\nWARNING: unmet direct dependencies detected for %s\n",
315 sym->name);
316 str_printf(&gs,
317 " Depends on [%c]: ",
318 sym->dir_dep.tri == mod ? 'm' : 'n');
319 expr_gstr_print(sym->dir_dep.expr, &gs);
320 str_printf(&gs, "\n");
322 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes,
323 " Selected by [y]:\n");
324 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod,
325 " Selected by [m]:\n");
327 fputs(str_get(&gs), stderr);
330 void sym_calc_value(struct symbol *sym)
332 struct symbol_value newval, oldval;
333 struct property *prop;
334 struct expr *e;
336 if (!sym)
337 return;
339 if (sym->flags & SYMBOL_VALID)
340 return;
342 if (sym_is_choice_value(sym) &&
343 sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
344 sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
345 prop = sym_get_choice_prop(sym);
346 sym_calc_value(prop_get_symbol(prop));
349 sym->flags |= SYMBOL_VALID;
351 oldval = sym->curr;
353 switch (sym->type) {
354 case S_INT:
355 case S_HEX:
356 case S_STRING:
357 newval = symbol_empty.curr;
358 break;
359 case S_BOOLEAN:
360 case S_TRISTATE:
361 newval = symbol_no.curr;
362 break;
363 default:
364 sym->curr.val = sym->name;
365 sym->curr.tri = no;
366 return;
368 sym->flags &= ~SYMBOL_WRITE;
370 sym_calc_visibility(sym);
372 if (sym->visible != no)
373 sym->flags |= SYMBOL_WRITE;
375 /* set default if recursively called */
376 sym->curr = newval;
378 switch (sym_get_type(sym)) {
379 case S_BOOLEAN:
380 case S_TRISTATE:
381 if (sym_is_choice_value(sym) && sym->visible == yes) {
382 prop = sym_get_choice_prop(sym);
383 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
384 } else {
385 if (sym->visible != no) {
386 /* if the symbol is visible use the user value
387 * if available, otherwise try the default value
389 if (sym_has_value(sym)) {
390 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
391 sym->visible);
392 goto calc_newval;
395 if (sym->rev_dep.tri != no)
396 sym->flags |= SYMBOL_WRITE;
397 if (!sym_is_choice(sym)) {
398 prop = sym_get_default_prop(sym);
399 if (prop) {
400 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
401 prop->visible.tri);
402 if (newval.tri != no)
403 sym->flags |= SYMBOL_WRITE;
405 if (sym->implied.tri != no) {
406 sym->flags |= SYMBOL_WRITE;
407 newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
410 calc_newval:
411 if (sym->dir_dep.tri < sym->rev_dep.tri)
412 sym_warn_unmet_dep(sym);
413 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
415 if (newval.tri == mod &&
416 (sym_get_type(sym) == S_BOOLEAN || sym->implied.tri == yes))
417 newval.tri = yes;
418 break;
419 case S_STRING:
420 case S_HEX:
421 case S_INT:
422 if (sym->visible != no && sym_has_value(sym)) {
423 newval.val = sym->def[S_DEF_USER].val;
424 break;
426 prop = sym_get_default_prop(sym);
427 if (prop) {
428 struct symbol *ds = prop_get_symbol(prop);
429 if (ds) {
430 sym->flags |= SYMBOL_WRITE;
431 sym_calc_value(ds);
432 newval.val = ds->curr.val;
435 break;
436 default:
440 sym->curr = newval;
441 if (sym_is_choice(sym) && newval.tri == yes)
442 sym->curr.val = sym_calc_choice(sym);
443 sym_validate_range(sym);
445 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
446 sym_set_changed(sym);
447 if (modules_sym == sym) {
448 sym_set_all_changed();
449 modules_val = modules_sym->curr.tri;
453 if (sym_is_choice(sym)) {
454 struct symbol *choice_sym;
456 prop = sym_get_choice_prop(sym);
457 expr_list_for_each_sym(prop->expr, e, choice_sym) {
458 if ((sym->flags & SYMBOL_WRITE) &&
459 choice_sym->visible != no)
460 choice_sym->flags |= SYMBOL_WRITE;
461 if (sym->flags & SYMBOL_CHANGED)
462 sym_set_changed(choice_sym);
466 if (sym->flags & SYMBOL_AUTO)
467 sym->flags &= ~SYMBOL_WRITE;
469 if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
470 set_all_choice_values(sym);
473 void sym_clear_all_valid(void)
475 struct symbol *sym;
476 int i;
478 for_all_symbols(i, sym)
479 sym->flags &= ~SYMBOL_VALID;
480 sym_add_change_count(1);
481 sym_calc_value(modules_sym);
484 bool sym_tristate_within_range(struct symbol *sym, tristate val)
486 int type = sym_get_type(sym);
488 if (sym->visible == no)
489 return false;
491 if (type != S_BOOLEAN && type != S_TRISTATE)
492 return false;
494 if (type == S_BOOLEAN && val == mod)
495 return false;
496 if (sym->visible <= sym->rev_dep.tri)
497 return false;
498 if (sym->implied.tri == yes && val == mod)
499 return false;
500 if (sym_is_choice_value(sym) && sym->visible == yes)
501 return val == yes;
502 return val >= sym->rev_dep.tri && val <= sym->visible;
505 bool sym_set_tristate_value(struct symbol *sym, tristate val)
507 tristate oldval = sym_get_tristate_value(sym);
509 if (oldval != val && !sym_tristate_within_range(sym, val))
510 return false;
512 if (!(sym->flags & SYMBOL_DEF_USER)) {
513 sym->flags |= SYMBOL_DEF_USER;
514 sym_set_changed(sym);
517 * setting a choice value also resets the new flag of the choice
518 * symbol and all other choice values.
520 if (sym_is_choice_value(sym) && val == yes) {
521 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
522 struct property *prop;
523 struct expr *e;
525 cs->def[S_DEF_USER].val = sym;
526 cs->flags |= SYMBOL_DEF_USER;
527 prop = sym_get_choice_prop(cs);
528 for (e = prop->expr; e; e = e->left.expr) {
529 if (e->right.sym->visible != no)
530 e->right.sym->flags |= SYMBOL_DEF_USER;
534 sym->def[S_DEF_USER].tri = val;
535 if (oldval != val)
536 sym_clear_all_valid();
538 return true;
541 tristate sym_toggle_tristate_value(struct symbol *sym)
543 tristate oldval, newval;
545 oldval = newval = sym_get_tristate_value(sym);
546 do {
547 switch (newval) {
548 case no:
549 newval = mod;
550 break;
551 case mod:
552 newval = yes;
553 break;
554 case yes:
555 newval = no;
556 break;
558 if (sym_set_tristate_value(sym, newval))
559 break;
560 } while (oldval != newval);
561 return newval;
564 bool sym_string_valid(struct symbol *sym, const char *str)
566 signed char ch;
568 switch (sym->type) {
569 case S_STRING:
570 return true;
571 case S_INT:
572 ch = *str++;
573 if (ch == '-')
574 ch = *str++;
575 if (!isdigit(ch))
576 return false;
577 if (ch == '0' && *str != 0)
578 return false;
579 while ((ch = *str++)) {
580 if (!isdigit(ch))
581 return false;
583 return true;
584 case S_HEX:
585 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
586 str += 2;
587 ch = *str++;
588 do {
589 if (!isxdigit(ch))
590 return false;
591 } while ((ch = *str++));
592 return true;
593 case S_BOOLEAN:
594 case S_TRISTATE:
595 switch (str[0]) {
596 case 'y': case 'Y':
597 case 'm': case 'M':
598 case 'n': case 'N':
599 return true;
601 return false;
602 default:
603 return false;
607 bool sym_string_within_range(struct symbol *sym, const char *str)
609 struct property *prop;
610 long long val;
612 switch (sym->type) {
613 case S_STRING:
614 return sym_string_valid(sym, str);
615 case S_INT:
616 if (!sym_string_valid(sym, str))
617 return false;
618 prop = sym_get_range_prop(sym);
619 if (!prop)
620 return true;
621 val = strtoll(str, NULL, 10);
622 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
623 val <= sym_get_range_val(prop->expr->right.sym, 10);
624 case S_HEX:
625 if (!sym_string_valid(sym, str))
626 return false;
627 prop = sym_get_range_prop(sym);
628 if (!prop)
629 return true;
630 val = strtoll(str, NULL, 16);
631 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
632 val <= sym_get_range_val(prop->expr->right.sym, 16);
633 case S_BOOLEAN:
634 case S_TRISTATE:
635 switch (str[0]) {
636 case 'y': case 'Y':
637 return sym_tristate_within_range(sym, yes);
638 case 'm': case 'M':
639 return sym_tristate_within_range(sym, mod);
640 case 'n': case 'N':
641 return sym_tristate_within_range(sym, no);
643 return false;
644 default:
645 return false;
649 bool sym_set_string_value(struct symbol *sym, const char *newval)
651 const char *oldval;
652 char *val;
653 int size;
655 switch (sym->type) {
656 case S_BOOLEAN:
657 case S_TRISTATE:
658 switch (newval[0]) {
659 case 'y': case 'Y':
660 return sym_set_tristate_value(sym, yes);
661 case 'm': case 'M':
662 return sym_set_tristate_value(sym, mod);
663 case 'n': case 'N':
664 return sym_set_tristate_value(sym, no);
666 return false;
667 default:
671 if (!sym_string_within_range(sym, newval))
672 return false;
674 if (!(sym->flags & SYMBOL_DEF_USER)) {
675 sym->flags |= SYMBOL_DEF_USER;
676 sym_set_changed(sym);
679 oldval = sym->def[S_DEF_USER].val;
680 size = strlen(newval) + 1;
681 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
682 size += 2;
683 sym->def[S_DEF_USER].val = val = xmalloc(size);
684 *val++ = '0';
685 *val++ = 'x';
686 } else if (!oldval || strcmp(oldval, newval))
687 sym->def[S_DEF_USER].val = val = xmalloc(size);
688 else
689 return true;
691 strcpy(val, newval);
692 free((void *)oldval);
693 sym_clear_all_valid();
695 return true;
699 * Find the default value associated to a symbol.
700 * For tristate symbol handle the modules=n case
701 * in which case "m" becomes "y".
702 * If the symbol does not have any default then fallback
703 * to the fixed default values.
705 const char *sym_get_string_default(struct symbol *sym)
707 struct property *prop;
708 struct symbol *ds;
709 const char *str;
710 tristate val;
712 sym_calc_visibility(sym);
713 sym_calc_value(modules_sym);
714 val = symbol_no.curr.tri;
715 str = symbol_empty.curr.val;
717 /* If symbol has a default value look it up */
718 prop = sym_get_default_prop(sym);
719 if (prop != NULL) {
720 switch (sym->type) {
721 case S_BOOLEAN:
722 case S_TRISTATE:
723 /* The visibility may limit the value from yes => mod */
724 val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
725 break;
726 default:
728 * The following fails to handle the situation
729 * where a default value is further limited by
730 * the valid range.
732 ds = prop_get_symbol(prop);
733 if (ds != NULL) {
734 sym_calc_value(ds);
735 str = (const char *)ds->curr.val;
740 /* Handle select statements */
741 val = EXPR_OR(val, sym->rev_dep.tri);
743 /* transpose mod to yes if modules are not enabled */
744 if (val == mod)
745 if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
746 val = yes;
748 /* transpose mod to yes if type is bool */
749 if (sym->type == S_BOOLEAN && val == mod)
750 val = yes;
752 /* adjust the default value if this symbol is implied by another */
753 if (val < sym->implied.tri)
754 val = sym->implied.tri;
756 switch (sym->type) {
757 case S_BOOLEAN:
758 case S_TRISTATE:
759 switch (val) {
760 case no: return "n";
761 case mod: return "m";
762 case yes: return "y";
764 case S_INT:
765 case S_HEX:
766 return str;
767 case S_STRING:
768 return str;
769 case S_OTHER:
770 case S_UNKNOWN:
771 break;
773 return "";
776 const char *sym_get_string_value(struct symbol *sym)
778 tristate val;
780 switch (sym->type) {
781 case S_BOOLEAN:
782 case S_TRISTATE:
783 val = sym_get_tristate_value(sym);
784 switch (val) {
785 case no:
786 return "n";
787 case mod:
788 sym_calc_value(modules_sym);
789 return (modules_sym->curr.tri == no) ? "n" : "m";
790 case yes:
791 return "y";
793 break;
794 default:
797 return (const char *)sym->curr.val;
800 bool sym_is_changable(struct symbol *sym)
802 return sym->visible > sym->rev_dep.tri;
805 static unsigned strhash(const char *s)
807 /* fnv32 hash */
808 unsigned hash = 2166136261U;
809 for (; *s; s++)
810 hash = (hash ^ *s) * 0x01000193;
811 return hash;
814 struct symbol *sym_lookup(const char *name, int flags)
816 struct symbol *symbol;
817 char *new_name;
818 int hash;
820 if (name) {
821 if (name[0] && !name[1]) {
822 switch (name[0]) {
823 case 'y': return &symbol_yes;
824 case 'm': return &symbol_mod;
825 case 'n': return &symbol_no;
828 hash = strhash(name) % SYMBOL_HASHSIZE;
830 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
831 if (symbol->name &&
832 !strcmp(symbol->name, name) &&
833 (flags ? symbol->flags & flags
834 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
835 return symbol;
837 new_name = xstrdup(name);
838 } else {
839 new_name = NULL;
840 hash = 0;
843 symbol = xmalloc(sizeof(*symbol));
844 memset(symbol, 0, sizeof(*symbol));
845 symbol->name = new_name;
846 symbol->type = S_UNKNOWN;
847 symbol->flags |= flags;
849 symbol->next = symbol_hash[hash];
850 symbol_hash[hash] = symbol;
852 return symbol;
855 struct symbol *sym_find(const char *name)
857 struct symbol *symbol = NULL;
858 int hash = 0;
860 if (!name)
861 return NULL;
863 if (name[0] && !name[1]) {
864 switch (name[0]) {
865 case 'y': return &symbol_yes;
866 case 'm': return &symbol_mod;
867 case 'n': return &symbol_no;
870 hash = strhash(name) % SYMBOL_HASHSIZE;
872 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
873 if (symbol->name &&
874 !strcmp(symbol->name, name) &&
875 !(symbol->flags & SYMBOL_CONST))
876 break;
879 return symbol;
882 const char *sym_escape_string_value(const char *in)
884 const char *p;
885 size_t reslen;
886 char *res;
887 size_t l;
889 reslen = strlen(in) + strlen("\"\"") + 1;
891 p = in;
892 for (;;) {
893 l = strcspn(p, "\"\\");
894 p += l;
896 if (p[0] == '\0')
897 break;
899 reslen++;
900 p++;
903 res = xmalloc(reslen);
904 res[0] = '\0';
906 strcat(res, "\"");
908 p = in;
909 for (;;) {
910 l = strcspn(p, "\"\\");
911 strncat(res, p, l);
912 p += l;
914 if (p[0] == '\0')
915 break;
917 strcat(res, "\\");
918 strncat(res, p++, 1);
921 strcat(res, "\"");
922 return res;
925 struct sym_match {
926 struct symbol *sym;
927 off_t so, eo;
930 /* Compare matched symbols as thus:
931 * - first, symbols that match exactly
932 * - then, alphabetical sort
934 static int sym_rel_comp(const void *sym1, const void *sym2)
936 const struct sym_match *s1 = sym1;
937 const struct sym_match *s2 = sym2;
938 int exact1, exact2;
940 /* Exact match:
941 * - if matched length on symbol s1 is the length of that symbol,
942 * then this symbol should come first;
943 * - if matched length on symbol s2 is the length of that symbol,
944 * then this symbol should come first.
945 * Note: since the search can be a regexp, both symbols may match
946 * exactly; if this is the case, we can't decide which comes first,
947 * and we fallback to sorting alphabetically.
949 exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
950 exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
951 if (exact1 && !exact2)
952 return -1;
953 if (!exact1 && exact2)
954 return 1;
956 /* As a fallback, sort symbols alphabetically */
957 return strcmp(s1->sym->name, s2->sym->name);
960 struct symbol **sym_re_search(const char *pattern)
962 struct symbol *sym, **sym_arr = NULL;
963 struct sym_match *sym_match_arr = NULL;
964 int i, cnt, size;
965 regex_t re;
966 regmatch_t match[1];
968 cnt = size = 0;
969 /* Skip if empty */
970 if (strlen(pattern) == 0)
971 return NULL;
972 if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
973 return NULL;
975 for_all_symbols(i, sym) {
976 if (sym->flags & SYMBOL_CONST || !sym->name)
977 continue;
978 if (regexec(&re, sym->name, 1, match, 0))
979 continue;
980 if (cnt >= size) {
981 void *tmp;
982 size += 16;
983 tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
984 if (!tmp)
985 goto sym_re_search_free;
986 sym_match_arr = tmp;
988 sym_calc_value(sym);
989 /* As regexec returned 0, we know we have a match, so
990 * we can use match[0].rm_[se]o without further checks
992 sym_match_arr[cnt].so = match[0].rm_so;
993 sym_match_arr[cnt].eo = match[0].rm_eo;
994 sym_match_arr[cnt++].sym = sym;
996 if (sym_match_arr) {
997 qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
998 sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
999 if (!sym_arr)
1000 goto sym_re_search_free;
1001 for (i = 0; i < cnt; i++)
1002 sym_arr[i] = sym_match_arr[i].sym;
1003 sym_arr[cnt] = NULL;
1005 sym_re_search_free:
1006 /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
1007 free(sym_match_arr);
1008 regfree(&re);
1010 return sym_arr;
1014 * When we check for recursive dependencies we use a stack to save
1015 * current state so we can print out relevant info to user.
1016 * The entries are located on the call stack so no need to free memory.
1017 * Note insert() remove() must always match to properly clear the stack.
1019 static struct dep_stack {
1020 struct dep_stack *prev, *next;
1021 struct symbol *sym;
1022 struct property *prop;
1023 struct expr *expr;
1024 } *check_top;
1026 static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
1028 memset(stack, 0, sizeof(*stack));
1029 if (check_top)
1030 check_top->next = stack;
1031 stack->prev = check_top;
1032 stack->sym = sym;
1033 check_top = stack;
1036 static void dep_stack_remove(void)
1038 check_top = check_top->prev;
1039 if (check_top)
1040 check_top->next = NULL;
1044 * Called when we have detected a recursive dependency.
1045 * check_top point to the top of the stact so we use
1046 * the ->prev pointer to locate the bottom of the stack.
1048 static void sym_check_print_recursive(struct symbol *last_sym)
1050 struct dep_stack *stack;
1051 struct symbol *sym, *next_sym;
1052 struct menu *menu = NULL;
1053 struct property *prop;
1054 struct dep_stack cv_stack;
1056 if (sym_is_choice_value(last_sym)) {
1057 dep_stack_insert(&cv_stack, last_sym);
1058 last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1061 for (stack = check_top; stack != NULL; stack = stack->prev)
1062 if (stack->sym == last_sym)
1063 break;
1064 if (!stack) {
1065 fprintf(stderr, "unexpected recursive dependency error\n");
1066 return;
1069 for (; stack; stack = stack->next) {
1070 sym = stack->sym;
1071 next_sym = stack->next ? stack->next->sym : last_sym;
1072 prop = stack->prop;
1073 if (prop == NULL)
1074 prop = stack->sym->prop;
1076 /* for choice values find the menu entry (used below) */
1077 if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1078 for (prop = sym->prop; prop; prop = prop->next) {
1079 menu = prop->menu;
1080 if (prop->menu)
1081 break;
1084 if (stack->sym == last_sym)
1085 fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1086 prop->file->name, prop->lineno);
1088 if (stack->expr) {
1089 fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1090 prop->file->name, prop->lineno,
1091 sym->name ? sym->name : "<choice>",
1092 prop_get_type_name(prop->type),
1093 next_sym->name ? next_sym->name : "<choice>");
1094 } else if (stack->prop) {
1095 fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1096 prop->file->name, prop->lineno,
1097 sym->name ? sym->name : "<choice>",
1098 next_sym->name ? next_sym->name : "<choice>");
1099 } else if (sym_is_choice(sym)) {
1100 fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1101 menu->file->name, menu->lineno,
1102 sym->name ? sym->name : "<choice>",
1103 next_sym->name ? next_sym->name : "<choice>");
1104 } else if (sym_is_choice_value(sym)) {
1105 fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1106 menu->file->name, menu->lineno,
1107 sym->name ? sym->name : "<choice>",
1108 next_sym->name ? next_sym->name : "<choice>");
1109 } else {
1110 fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1111 prop->file->name, prop->lineno,
1112 sym->name ? sym->name : "<choice>",
1113 next_sym->name ? next_sym->name : "<choice>");
1117 fprintf(stderr,
1118 "For a resolution refer to Documentation/kbuild/kconfig-language.txt\n"
1119 "subsection \"Kconfig recursive dependency limitations\"\n"
1120 "\n");
1122 if (check_top == &cv_stack)
1123 dep_stack_remove();
1126 static struct symbol *sym_check_expr_deps(struct expr *e)
1128 struct symbol *sym;
1130 if (!e)
1131 return NULL;
1132 switch (e->type) {
1133 case E_OR:
1134 case E_AND:
1135 sym = sym_check_expr_deps(e->left.expr);
1136 if (sym)
1137 return sym;
1138 return sym_check_expr_deps(e->right.expr);
1139 case E_NOT:
1140 return sym_check_expr_deps(e->left.expr);
1141 case E_EQUAL:
1142 case E_GEQ:
1143 case E_GTH:
1144 case E_LEQ:
1145 case E_LTH:
1146 case E_UNEQUAL:
1147 sym = sym_check_deps(e->left.sym);
1148 if (sym)
1149 return sym;
1150 return sym_check_deps(e->right.sym);
1151 case E_SYMBOL:
1152 return sym_check_deps(e->left.sym);
1153 default:
1154 break;
1156 fprintf(stderr, "Oops! How to check %d?\n", e->type);
1157 return NULL;
1160 /* return NULL when dependencies are OK */
1161 static struct symbol *sym_check_sym_deps(struct symbol *sym)
1163 struct symbol *sym2;
1164 struct property *prop;
1165 struct dep_stack stack;
1167 dep_stack_insert(&stack, sym);
1169 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1170 if (sym2)
1171 goto out;
1173 for (prop = sym->prop; prop; prop = prop->next) {
1174 if (prop->type == P_CHOICE || prop->type == P_SELECT)
1175 continue;
1176 stack.prop = prop;
1177 sym2 = sym_check_expr_deps(prop->visible.expr);
1178 if (sym2)
1179 break;
1180 if (prop->type != P_DEFAULT || sym_is_choice(sym))
1181 continue;
1182 stack.expr = prop->expr;
1183 sym2 = sym_check_expr_deps(prop->expr);
1184 if (sym2)
1185 break;
1186 stack.expr = NULL;
1189 out:
1190 dep_stack_remove();
1192 return sym2;
1195 static struct symbol *sym_check_choice_deps(struct symbol *choice)
1197 struct symbol *sym, *sym2;
1198 struct property *prop;
1199 struct expr *e;
1200 struct dep_stack stack;
1202 dep_stack_insert(&stack, choice);
1204 prop = sym_get_choice_prop(choice);
1205 expr_list_for_each_sym(prop->expr, e, sym)
1206 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1208 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1209 sym2 = sym_check_sym_deps(choice);
1210 choice->flags &= ~SYMBOL_CHECK;
1211 if (sym2)
1212 goto out;
1214 expr_list_for_each_sym(prop->expr, e, sym) {
1215 sym2 = sym_check_sym_deps(sym);
1216 if (sym2)
1217 break;
1219 out:
1220 expr_list_for_each_sym(prop->expr, e, sym)
1221 sym->flags &= ~SYMBOL_CHECK;
1223 if (sym2 && sym_is_choice_value(sym2) &&
1224 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1225 sym2 = choice;
1227 dep_stack_remove();
1229 return sym2;
1232 struct symbol *sym_check_deps(struct symbol *sym)
1234 struct symbol *sym2;
1235 struct property *prop;
1237 if (sym->flags & SYMBOL_CHECK) {
1238 sym_check_print_recursive(sym);
1239 return sym;
1241 if (sym->flags & SYMBOL_CHECKED)
1242 return NULL;
1244 if (sym_is_choice_value(sym)) {
1245 struct dep_stack stack;
1247 /* for choice groups start the check with main choice symbol */
1248 dep_stack_insert(&stack, sym);
1249 prop = sym_get_choice_prop(sym);
1250 sym2 = sym_check_deps(prop_get_symbol(prop));
1251 dep_stack_remove();
1252 } else if (sym_is_choice(sym)) {
1253 sym2 = sym_check_choice_deps(sym);
1254 } else {
1255 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1256 sym2 = sym_check_sym_deps(sym);
1257 sym->flags &= ~SYMBOL_CHECK;
1260 if (sym2 && sym2 == sym)
1261 sym2 = NULL;
1263 return sym2;
1266 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
1268 struct property *prop;
1269 struct property **propp;
1271 prop = xmalloc(sizeof(*prop));
1272 memset(prop, 0, sizeof(*prop));
1273 prop->type = type;
1274 prop->sym = sym;
1275 prop->file = current_file;
1276 prop->lineno = zconf_lineno();
1278 /* append property to the prop list of symbol */
1279 if (sym) {
1280 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
1282 *propp = prop;
1285 return prop;
1288 struct symbol *prop_get_symbol(struct property *prop)
1290 if (prop->expr && (prop->expr->type == E_SYMBOL ||
1291 prop->expr->type == E_LIST))
1292 return prop->expr->left.sym;
1293 return NULL;
1296 const char *prop_get_type_name(enum prop_type type)
1298 switch (type) {
1299 case P_PROMPT:
1300 return "prompt";
1301 case P_ENV:
1302 return "env";
1303 case P_COMMENT:
1304 return "comment";
1305 case P_MENU:
1306 return "menu";
1307 case P_DEFAULT:
1308 return "default";
1309 case P_CHOICE:
1310 return "choice";
1311 case P_SELECT:
1312 return "select";
1313 case P_IMPLY:
1314 return "imply";
1315 case P_RANGE:
1316 return "range";
1317 case P_SYMBOL:
1318 return "symbol";
1319 case P_UNKNOWN:
1320 break;
1322 return "unknown";