mb/google/cherry: Add NOR-Flash support
[coreboot.git] / util / kconfig / symbol.c
blobaf494391bb414cf286395389d81772eda9a973e5
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 #ifndef __MINGW32__
11 #include <sys/utsname.h>
12 #endif
14 #include "lkc.h"
16 struct symbol symbol_yes = {
17 .name = "y",
18 .curr = { "y", yes },
19 .flags = SYMBOL_CONST|SYMBOL_VALID,
20 }, symbol_mod = {
21 .name = "m",
22 .curr = { "m", mod },
23 .flags = SYMBOL_CONST|SYMBOL_VALID,
24 }, symbol_no = {
25 .name = "n",
26 .curr = { "n", no },
27 .flags = SYMBOL_CONST|SYMBOL_VALID,
28 }, symbol_empty = {
29 .name = "",
30 .curr = { "", no },
31 .flags = SYMBOL_VALID,
34 struct symbol *sym_defconfig_list;
35 struct symbol *modules_sym;
36 tristate modules_val;
38 struct expr *sym_env_list;
40 static void sym_add_default(struct symbol *sym, const char *def)
42 struct property *prop = prop_alloc(P_DEFAULT, sym);
44 prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
47 void sym_init(void)
49 struct symbol *sym;
50 #ifndef __MINGW32__
51 struct utsname uts;
52 #endif
53 static bool inited = false;
55 if (inited)
56 return;
57 inited = true;
59 #ifndef __MINGW32__
60 uname(&uts);
61 #endif
63 sym = sym_lookup("UNAME_RELEASE", 0);
64 sym->type = S_STRING;
65 sym->flags |= SYMBOL_AUTO;
66 #ifndef __MINGW32__
67 sym_add_default(sym, uts.release);
68 #else
69 sym_add_default(sym, "mingw-unknown");
70 #endif
73 enum symbol_type sym_get_type(struct symbol *sym)
75 enum symbol_type type = sym->type;
77 if (type == S_TRISTATE) {
78 if (sym_is_choice_value(sym) && sym->visible == yes)
79 type = S_BOOLEAN;
80 else if (modules_val == no)
81 type = S_BOOLEAN;
83 return type;
86 const char *sym_type_name(enum symbol_type type)
88 switch (type) {
89 case S_BOOLEAN:
90 return "boolean";
91 case S_TRISTATE:
92 return "tristate";
93 case S_INT:
94 return "integer";
95 case S_HEX:
96 return "hex";
97 case S_STRING:
98 return "string";
99 case S_UNKNOWN:
100 return "unknown";
101 case S_OTHER:
102 break;
104 return "???";
107 struct property *sym_get_choice_prop(struct symbol *sym)
109 struct property *prop;
111 for_all_choices(sym, prop)
112 return prop;
113 return NULL;
116 struct property *sym_get_env_prop(struct symbol *sym)
118 struct property *prop;
120 for_all_properties(sym, prop, P_ENV)
121 return prop;
122 return NULL;
125 struct property *sym_get_default_prop(struct symbol *sym)
127 struct property *prop;
129 for_all_defaults(sym, prop) {
130 prop->visible.tri = expr_calc_value(prop->visible.expr);
131 if (prop->visible.tri != no)
132 return prop;
134 return NULL;
137 static struct property *sym_get_range_prop(struct symbol *sym)
139 struct property *prop;
141 for_all_properties(sym, prop, P_RANGE) {
142 prop->visible.tri = expr_calc_value(prop->visible.expr);
143 if (prop->visible.tri != no)
144 return prop;
146 return NULL;
149 static long long sym_get_range_val(struct symbol *sym, int base)
151 sym_calc_value(sym);
152 switch (sym->type) {
153 case S_INT:
154 base = 10;
155 break;
156 case S_HEX:
157 base = 16;
158 break;
159 default:
160 break;
162 return strtoll(sym->curr.val, NULL, base);
165 static void sym_validate_range(struct symbol *sym)
167 struct property *prop;
168 int base;
169 long long val, val2;
170 char str[64];
172 switch (sym->type) {
173 case S_INT:
174 base = 10;
175 break;
176 case S_HEX:
177 base = 16;
178 break;
179 default:
180 return;
182 prop = sym_get_range_prop(sym);
183 if (!prop)
184 return;
185 val = strtoll(sym->curr.val, NULL, base);
186 val2 = sym_get_range_val(prop->expr->left.sym, base);
187 if (val >= val2) {
188 val2 = sym_get_range_val(prop->expr->right.sym, base);
189 if (val <= val2)
190 return;
192 if (sym->type == S_INT)
193 sprintf(str, "%lld", val2);
194 else
195 sprintf(str, "0x%llx", val2);
196 sym->curr.val = strdup(str);
199 static void sym_calc_visibility(struct symbol *sym)
201 struct property *prop;
202 tristate tri;
204 /* any prompt visible? */
205 tri = no;
206 for_all_prompts(sym, prop) {
207 prop->visible.tri = expr_calc_value(prop->visible.expr);
208 tri = EXPR_OR(tri, prop->visible.tri);
210 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
211 tri = yes;
212 if (sym->visible != tri) {
213 sym->visible = tri;
214 sym_set_changed(sym);
216 if (sym_is_choice_value(sym))
217 return;
218 /* defaulting to "yes" if no explicit "depends on" are given */
219 tri = yes;
220 if (sym->dir_dep.expr)
221 tri = expr_calc_value(sym->dir_dep.expr);
222 if (tri == mod)
223 tri = yes;
224 if (sym->dir_dep.tri != tri) {
225 sym->dir_dep.tri = tri;
226 sym_set_changed(sym);
228 tri = no;
229 if (sym->rev_dep.expr)
230 tri = expr_calc_value(sym->rev_dep.expr);
231 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
232 tri = yes;
233 if (sym->rev_dep.tri != tri) {
234 sym->rev_dep.tri = tri;
235 sym_set_changed(sym);
240 * Find the default symbol for a choice.
241 * First try the default values for the choice symbol
242 * Next locate the first visible choice value
243 * Return NULL if none was found
245 struct symbol *sym_choice_default(struct symbol *sym)
247 struct symbol *def_sym;
248 struct property *prop;
249 struct expr *e;
251 /* any of the defaults visible? */
252 for_all_defaults(sym, prop) {
253 prop->visible.tri = expr_calc_value(prop->visible.expr);
254 if (prop->visible.tri == no)
255 continue;
256 def_sym = prop_get_symbol(prop);
257 if (def_sym->visible != no)
258 return def_sym;
261 /* just get the first visible value */
262 prop = sym_get_choice_prop(sym);
263 expr_list_for_each_sym(prop->expr, e, def_sym)
264 if (def_sym->visible != no)
265 return def_sym;
267 /* failed to locate any defaults */
268 return NULL;
271 static struct symbol *sym_calc_choice(struct symbol *sym)
273 struct symbol *def_sym;
274 struct property *prop;
275 struct expr *e;
276 int flags;
278 /* first calculate all choice values' visibilities */
279 flags = sym->flags;
280 prop = sym_get_choice_prop(sym);
281 expr_list_for_each_sym(prop->expr, e, def_sym) {
282 sym_calc_visibility(def_sym);
283 if (def_sym->visible != no)
284 flags &= def_sym->flags;
287 sym->flags &= flags | ~SYMBOL_DEF_USER;
289 /* is the user choice visible? */
290 def_sym = sym->def[S_DEF_USER].val;
291 if (def_sym && def_sym->visible != no)
292 return def_sym;
294 def_sym = sym_choice_default(sym);
296 if (def_sym == NULL)
297 /* no choice? reset tristate value */
298 sym->curr.tri = no;
300 return def_sym;
303 void sym_calc_value(struct symbol *sym)
305 struct symbol_value newval, oldval;
306 struct property *prop;
307 struct expr *e;
309 if (!sym)
310 return;
312 if (sym->flags & SYMBOL_VALID)
313 return;
315 if (sym_is_choice_value(sym) &&
316 sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
317 sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
318 prop = sym_get_choice_prop(sym);
319 sym_calc_value(prop_get_symbol(prop));
322 sym->flags |= SYMBOL_VALID;
324 oldval = sym->curr;
326 switch (sym->type) {
327 case S_INT:
328 case S_HEX:
329 case S_STRING:
330 newval = symbol_empty.curr;
331 break;
332 case S_BOOLEAN:
333 case S_TRISTATE:
334 newval = symbol_no.curr;
335 break;
336 default:
337 sym->curr.val = sym->name;
338 sym->curr.tri = no;
339 return;
341 if (!sym_is_choice_value(sym))
342 sym->flags &= ~SYMBOL_WRITE;
344 sym_calc_visibility(sym);
346 /* set default if recursively called */
347 sym->curr = newval;
349 switch (sym_get_type(sym)) {
350 case S_BOOLEAN:
351 case S_TRISTATE:
352 if (sym_is_choice_value(sym) && sym->visible == yes) {
353 prop = sym_get_choice_prop(sym);
354 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
355 } else {
356 if (sym->visible != no) {
357 /* if the symbol is visible use the user value
358 * if available, otherwise try the default value
360 sym->flags |= SYMBOL_WRITE;
361 if (sym_has_value(sym)) {
362 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
363 sym->visible);
364 goto calc_newval;
367 if (sym->rev_dep.tri != no)
368 sym->flags |= SYMBOL_WRITE;
369 if (!sym_is_choice(sym)) {
370 prop = sym_get_default_prop(sym);
371 if (prop) {
372 sym->flags |= SYMBOL_WRITE;
373 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
374 prop->visible.tri);
377 calc_newval:
378 if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
379 struct expr *e;
380 e = expr_simplify_unmet_dep(sym->rev_dep.expr,
381 sym->dir_dep.expr);
382 fprintf(stderr, "warning: (");
383 expr_fprint(e, stderr);
384 fprintf(stderr, ") selects %s which has unmet direct dependencies (",
385 sym->name);
386 expr_fprint(sym->dir_dep.expr, stderr);
387 fprintf(stderr, ")\n");
388 kconfig_warnings++;
389 expr_free(e);
391 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
393 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
394 newval.tri = yes;
395 break;
396 case S_STRING:
397 case S_HEX:
398 case S_INT:
399 if (sym->visible != no) {
400 sym->flags |= SYMBOL_WRITE;
401 if (sym_has_value(sym)) {
402 newval.val = sym->def[S_DEF_USER].val;
403 break;
406 prop = sym_get_default_prop(sym);
407 if (prop) {
408 struct symbol *ds = prop_get_symbol(prop);
409 if (ds) {
410 sym->flags |= SYMBOL_WRITE;
411 sym_calc_value(ds);
412 newval.val = ds->curr.val;
415 break;
416 default:
420 sym->curr = newval;
421 if (sym_is_choice(sym) && newval.tri == yes)
422 sym->curr.val = sym_calc_choice(sym);
423 sym_validate_range(sym);
425 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
426 sym_set_changed(sym);
427 if (modules_sym == sym) {
428 sym_set_all_changed();
429 modules_val = modules_sym->curr.tri;
433 if (sym_is_choice(sym)) {
434 struct symbol *choice_sym;
436 prop = sym_get_choice_prop(sym);
437 expr_list_for_each_sym(prop->expr, e, choice_sym) {
438 if ((sym->flags & SYMBOL_WRITE) &&
439 choice_sym->visible != no)
440 choice_sym->flags |= SYMBOL_WRITE;
441 if (sym->flags & SYMBOL_CHANGED)
442 sym_set_changed(choice_sym);
446 if (sym->flags & SYMBOL_AUTO)
447 sym->flags &= ~SYMBOL_WRITE;
449 if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
450 set_all_choice_values(sym);
453 void sym_clear_all_valid(void)
455 struct symbol *sym;
456 int i;
458 for_all_symbols(i, sym)
459 sym->flags &= ~SYMBOL_VALID;
460 sym_add_change_count(1);
461 if (modules_sym)
462 sym_calc_value(modules_sym);
465 void sym_set_changed(struct symbol *sym)
467 struct property *prop;
469 sym->flags |= SYMBOL_CHANGED;
470 for (prop = sym->prop; prop; prop = prop->next) {
471 if (prop->menu)
472 prop->menu->flags |= MENU_CHANGED;
476 void sym_set_all_changed(void)
478 struct symbol *sym;
479 int i;
481 for_all_symbols(i, sym)
482 sym_set_changed(sym);
485 bool sym_tristate_within_range(struct symbol *sym, tristate val)
487 int type = sym_get_type(sym);
489 if (sym->visible == no)
490 return false;
492 if (type != S_BOOLEAN && type != S_TRISTATE)
493 return false;
495 if (type == S_BOOLEAN && val == mod)
496 return false;
497 if (sym->visible <= sym->rev_dep.tri)
498 return false;
499 if (sym_is_choice_value(sym) && sym->visible == yes)
500 return val == yes;
501 return val >= sym->rev_dep.tri && val <= sym->visible;
504 bool sym_set_tristate_value(struct symbol *sym, tristate val)
506 tristate oldval = sym_get_tristate_value(sym);
508 if (oldval != val && !sym_tristate_within_range(sym, val))
509 return false;
511 if (!(sym->flags & SYMBOL_DEF_USER)) {
512 sym->flags |= SYMBOL_DEF_USER;
513 sym_set_changed(sym);
516 * setting a choice value also resets the new flag of the choice
517 * symbol and all other choice values.
519 if (sym_is_choice_value(sym) && val == yes) {
520 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
521 struct property *prop;
522 struct expr *e;
524 cs->def[S_DEF_USER].val = sym;
525 cs->flags |= SYMBOL_DEF_USER;
526 prop = sym_get_choice_prop(cs);
527 for (e = prop->expr; e; e = e->left.expr) {
528 if (e->right.sym->visible != no)
529 e->right.sym->flags |= SYMBOL_DEF_USER;
533 sym->def[S_DEF_USER].tri = val;
534 if (oldval != val)
535 sym_clear_all_valid();
537 return true;
540 tristate sym_toggle_tristate_value(struct symbol *sym)
542 tristate oldval, newval;
544 oldval = newval = sym_get_tristate_value(sym);
545 do {
546 switch (newval) {
547 case no:
548 newval = mod;
549 break;
550 case mod:
551 newval = yes;
552 break;
553 case yes:
554 newval = no;
555 break;
557 if (sym_set_tristate_value(sym, newval))
558 break;
559 } while (oldval != newval);
560 return newval;
563 bool sym_string_valid(struct symbol *sym, const char *str)
565 signed char ch;
567 switch (sym->type) {
568 case S_STRING:
569 return true;
570 case S_INT:
571 ch = *str++;
572 if (ch == '-')
573 ch = *str++;
574 if (!isdigit(ch))
575 return false;
576 if (ch == '0' && *str != 0)
577 return false;
578 while ((ch = *str++)) {
579 if (!isdigit(ch))
580 return false;
582 return true;
583 case S_HEX:
584 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
585 str += 2;
586 ch = *str++;
587 do {
588 if (!isxdigit(ch))
589 return false;
590 } while ((ch = *str++));
591 return true;
592 case S_BOOLEAN:
593 case S_TRISTATE:
594 switch (str[0]) {
595 case 'y': case 'Y':
596 case 'm': case 'M':
597 case 'n': case 'N':
598 return true;
600 return false;
601 default:
602 return false;
606 bool sym_string_within_range(struct symbol *sym, const char *str)
608 struct property *prop;
609 long long val;
611 switch (sym->type) {
612 case S_STRING:
613 return sym_string_valid(sym, str);
614 case S_INT:
615 if (!sym_string_valid(sym, str))
616 return false;
617 prop = sym_get_range_prop(sym);
618 if (!prop)
619 return true;
620 val = strtoll(str, NULL, 10);
621 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
622 val <= sym_get_range_val(prop->expr->right.sym, 10);
623 case S_HEX:
624 if (!sym_string_valid(sym, str))
625 return false;
626 prop = sym_get_range_prop(sym);
627 if (!prop)
628 return true;
629 val = strtoll(str, NULL, 16);
630 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
631 val <= sym_get_range_val(prop->expr->right.sym, 16);
632 case S_BOOLEAN:
633 case S_TRISTATE:
634 switch (str[0]) {
635 case 'y': case 'Y':
636 return sym_tristate_within_range(sym, yes);
637 case 'm': case 'M':
638 return sym_tristate_within_range(sym, mod);
639 case 'n': case 'N':
640 return sym_tristate_within_range(sym, no);
642 return false;
643 default:
644 return false;
648 bool sym_set_string_value(struct symbol *sym, const char *newval)
650 const char *oldval;
651 char *val;
652 int size;
654 switch (sym->type) {
655 case S_BOOLEAN:
656 case S_TRISTATE:
657 switch (newval[0]) {
658 case 'y': case 'Y':
659 return sym_set_tristate_value(sym, yes);
660 case 'm': case 'M':
661 return sym_set_tristate_value(sym, mod);
662 case 'n': case 'N':
663 return sym_set_tristate_value(sym, no);
665 return false;
666 default:
670 if (!sym_string_within_range(sym, newval))
671 return false;
673 if (!(sym->flags & SYMBOL_DEF_USER)) {
674 sym->flags |= SYMBOL_DEF_USER;
675 sym_set_changed(sym);
678 oldval = sym->def[S_DEF_USER].val;
679 size = strlen(newval) + 1;
680 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
681 size += 2;
682 sym->def[S_DEF_USER].val = val = xmalloc(size);
683 *val++ = '0';
684 *val++ = 'x';
685 } else if (!oldval || strcmp(oldval, newval))
686 sym->def[S_DEF_USER].val = val = xmalloc(size);
687 else
688 return true;
690 strcpy(val, newval);
691 free((void *)oldval);
692 sym_clear_all_valid();
694 return true;
698 * Find the default value associated to a symbol.
699 * For tristate symbol handle the modules=n case
700 * in which case "m" becomes "y".
701 * If the symbol does not have any default then fallback
702 * to the fixed default values.
704 const char *sym_get_string_default(struct symbol *sym)
706 struct property *prop;
707 struct symbol *ds;
708 const char *str;
709 tristate val;
711 sym_calc_visibility(sym);
712 sym_calc_value(modules_sym);
713 val = symbol_no.curr.tri;
714 str = symbol_empty.curr.val;
716 /* If symbol has a default value look it up */
717 prop = sym_get_default_prop(sym);
718 if (prop != NULL) {
719 switch (sym->type) {
720 case S_BOOLEAN:
721 case S_TRISTATE:
722 /* The visibility may limit the value from yes => mod */
723 val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
724 break;
725 default:
727 * The following fails to handle the situation
728 * where a default value is further limited by
729 * the valid range.
731 ds = prop_get_symbol(prop);
732 if (ds != NULL) {
733 sym_calc_value(ds);
734 str = (const char *)ds->curr.val;
739 /* Handle select statements */
740 val = EXPR_OR(val, sym->rev_dep.tri);
742 /* transpose mod to yes if modules are not enabled */
743 if (val == mod)
744 if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
745 val = yes;
747 /* transpose mod to yes if type is bool */
748 if (sym->type == S_BOOLEAN && val == mod)
749 val = yes;
751 switch (sym->type) {
752 case S_BOOLEAN:
753 case S_TRISTATE:
754 switch (val) {
755 case no: return "n";
756 case mod: return "m";
757 case yes: return "y";
759 case S_INT:
760 case S_HEX:
761 return str;
762 case S_STRING:
763 return str;
764 case S_OTHER:
765 case S_UNKNOWN:
766 break;
768 return "";
771 const char *sym_get_string_value(struct symbol *sym)
773 tristate val;
775 switch (sym->type) {
776 case S_BOOLEAN:
777 case S_TRISTATE:
778 val = sym_get_tristate_value(sym);
779 switch (val) {
780 case no:
781 return "n";
782 case mod:
783 sym_calc_value(modules_sym);
784 return (modules_sym->curr.tri == no) ? "n" : "m";
785 case yes:
786 return "y";
788 break;
789 default:
792 return (const char *)sym->curr.val;
795 bool sym_is_changable(struct symbol *sym)
797 return sym->visible > sym->rev_dep.tri;
800 static unsigned strhash(const char *s)
802 /* fnv32 hash */
803 unsigned hash = 2166136261U;
804 for (; *s; s++)
805 hash = (hash ^ *s) * 0x01000193;
806 return hash;
809 struct symbol *sym_lookup(const char *name, int flags)
811 struct symbol *symbol;
812 char *new_name;
813 int hash;
815 if (name) {
816 if (name[0] && !name[1]) {
817 switch (name[0]) {
818 case 'y': return &symbol_yes;
819 case 'm': return &symbol_mod;
820 case 'n': return &symbol_no;
823 hash = strhash(name) % SYMBOL_HASHSIZE;
825 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
826 if (symbol->name &&
827 !strcmp(symbol->name, name) &&
828 (flags ? symbol->flags & flags
829 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
830 return symbol;
832 new_name = strdup(name);
833 } else {
834 new_name = NULL;
835 hash = 0;
838 symbol = xmalloc(sizeof(*symbol));
839 memset(symbol, 0, sizeof(*symbol));
840 symbol->name = new_name;
841 symbol->type = S_UNKNOWN;
842 symbol->flags |= flags;
844 symbol->next = symbol_hash[hash];
845 symbol_hash[hash] = symbol;
847 return symbol;
850 struct symbol *sym_find(const char *name)
852 struct symbol *symbol = NULL;
853 int hash = 0;
855 if (!name)
856 return NULL;
858 if (name[0] && !name[1]) {
859 switch (name[0]) {
860 case 'y': return &symbol_yes;
861 case 'm': return &symbol_mod;
862 case 'n': return &symbol_no;
865 hash = strhash(name) % SYMBOL_HASHSIZE;
867 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
868 if (symbol->name &&
869 !strcmp(symbol->name, name) &&
870 !(symbol->flags & SYMBOL_CONST))
871 break;
874 return symbol;
878 * Expand symbol's names embedded in the string given in argument. Symbols'
879 * name to be expanded shall be prefixed by a '$'. Unknown symbol expands to
880 * the empty string.
882 const char *sym_expand_string_value(const char *in)
884 const char *src;
885 char *res;
886 size_t reslen;
888 reslen = strlen(in) + 1;
889 res = xmalloc(reslen);
890 res[0] = '\0';
892 while ((src = strchr(in, '$'))) {
893 char *p, name[SYMBOL_MAXLENGTH];
894 const char *symval = "";
895 struct symbol *sym;
896 size_t newlen;
898 strncat(res, in, src - in);
899 src++;
901 p = name;
902 while (isalnum(*src) || *src == '_')
903 *p++ = *src++;
904 *p = '\0';
906 sym = sym_find(name);
907 if (sym != NULL) {
908 sym_calc_value(sym);
909 symval = sym_get_string_value(sym);
912 newlen = strlen(res) + strlen(symval) + strlen(src) + 1;
913 if (newlen > reslen) {
914 reslen = newlen;
915 res = realloc(res, reslen);
918 strcat(res, symval);
919 in = src;
921 strcat(res, in);
923 return res;
926 const char *sym_escape_string_value(const char *in)
928 const char *p;
929 size_t reslen;
930 char *res;
931 size_t l;
933 reslen = strlen(in) + strlen("\"\"") + 1;
935 p = in;
936 for (;;) {
937 l = strcspn(p, "\"\\");
938 p += l;
940 if (p[0] == '\0')
941 break;
943 reslen++;
944 p++;
947 res = xmalloc(reslen);
948 res[0] = '\0';
950 strcat(res, "\"");
952 p = in;
953 for (;;) {
954 l = strcspn(p, "\"\\");
955 strncat(res, p, l);
956 p += l;
958 if (p[0] == '\0')
959 break;
961 strcat(res, "\\");
962 strncat(res, p++, 1);
965 strcat(res, "\"");
966 return res;
969 struct sym_match {
970 struct symbol *sym;
971 off_t so, eo;
974 /* Compare matched symbols as thus:
975 * - first, symbols that match exactly
976 * - then, alphabetical sort
978 static int sym_rel_comp(const void *sym1, const void *sym2)
980 const struct sym_match *s1 = sym1;
981 const struct sym_match *s2 = sym2;
982 int exact1, exact2;
984 /* Exact match:
985 * - if matched length on symbol s1 is the length of that symbol,
986 * then this symbol should come first;
987 * - if matched length on symbol s2 is the length of that symbol,
988 * then this symbol should come first.
989 * Note: since the search can be a regexp, both symbols may match
990 * exactly; if this is the case, we can't decide which comes first,
991 * and we fallback to sorting alphabetically.
993 exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
994 exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
995 if (exact1 && !exact2)
996 return -1;
997 if (!exact1 && exact2)
998 return 1;
1000 /* As a fallback, sort symbols alphabetically */
1001 return strcmp(s1->sym->name, s2->sym->name);
1004 struct symbol **sym_re_search(const char *pattern)
1006 struct symbol *sym, **sym_arr = NULL;
1007 struct sym_match *sym_match_arr = NULL;
1008 int i, cnt, size;
1009 regex_t re;
1010 regmatch_t match[1];
1012 cnt = size = 0;
1013 /* Skip if empty */
1014 if (strlen(pattern) == 0)
1015 return NULL;
1016 if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
1017 return NULL;
1019 for_all_symbols(i, sym) {
1020 if (sym->flags & SYMBOL_CONST || !sym->name)
1021 continue;
1022 if (regexec(&re, sym->name, 1, match, 0))
1023 continue;
1024 if (cnt >= size) {
1025 void *tmp;
1026 size += 16;
1027 tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
1028 if (!tmp)
1029 goto sym_re_search_free;
1030 sym_match_arr = tmp;
1032 sym_calc_value(sym);
1033 /* As regexec returned 0, we know we have a match, so
1034 * we can use match[0].rm_[se]o without further checks
1036 sym_match_arr[cnt].so = match[0].rm_so;
1037 sym_match_arr[cnt].eo = match[0].rm_eo;
1038 sym_match_arr[cnt++].sym = sym;
1040 if (sym_match_arr) {
1041 qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
1042 sym_arr = malloc((cnt+1) * sizeof(struct symbol));
1043 if (!sym_arr)
1044 goto sym_re_search_free;
1045 for (i = 0; i < cnt; i++)
1046 sym_arr[i] = sym_match_arr[i].sym;
1047 sym_arr[cnt] = NULL;
1049 sym_re_search_free:
1050 /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
1051 free(sym_match_arr);
1052 regfree(&re);
1054 return sym_arr;
1058 * When we check for recursive dependencies we use a stack to save
1059 * current state so we can print out relevant info to user.
1060 * The entries are located on the call stack so no need to free memory.
1061 * Note insert() remove() must always match to properly clear the stack.
1063 static struct dep_stack {
1064 struct dep_stack *prev, *next;
1065 struct symbol *sym;
1066 struct property *prop;
1067 struct expr *expr;
1068 } *check_top;
1070 static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
1072 memset(stack, 0, sizeof(*stack));
1073 if (check_top)
1074 check_top->next = stack;
1075 stack->prev = check_top;
1076 stack->sym = sym;
1077 check_top = stack;
1080 static void dep_stack_remove(void)
1082 check_top = check_top->prev;
1083 if (check_top)
1084 check_top->next = NULL;
1088 * Called when we have detected a recursive dependency.
1089 * check_top point to the top of the stact so we use
1090 * the ->prev pointer to locate the bottom of the stack.
1092 static void sym_check_print_recursive(struct symbol *last_sym)
1094 struct dep_stack *stack;
1095 struct symbol *sym, *next_sym;
1096 struct menu *menu = NULL;
1097 struct property *prop;
1098 struct dep_stack cv_stack;
1100 if (sym_is_choice_value(last_sym)) {
1101 dep_stack_insert(&cv_stack, last_sym);
1102 last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1105 for (stack = check_top; stack != NULL; stack = stack->prev)
1106 if (stack->sym == last_sym)
1107 break;
1108 if (!stack) {
1109 fprintf(stderr, "unexpected recursive dependency error\n");
1110 return;
1113 for (; stack; stack = stack->next) {
1114 sym = stack->sym;
1115 next_sym = stack->next ? stack->next->sym : last_sym;
1116 prop = stack->prop;
1117 if (prop == NULL)
1118 prop = stack->sym->prop;
1120 /* for choice values find the menu entry (used below) */
1121 if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1122 for (prop = sym->prop; prop; prop = prop->next) {
1123 menu = prop->menu;
1124 if (prop->menu)
1125 break;
1128 if (stack->sym == last_sym)
1129 fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1130 prop->file->name, prop->lineno);
1131 if (stack->expr) {
1132 fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1133 prop->file->name, prop->lineno,
1134 sym->name ? sym->name : "<choice>",
1135 prop_get_type_name(prop->type),
1136 next_sym->name ? next_sym->name : "<choice>");
1137 } else if (stack->prop) {
1138 fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1139 prop->file->name, prop->lineno,
1140 sym->name ? sym->name : "<choice>",
1141 next_sym->name ? next_sym->name : "<choice>");
1142 } else if (sym_is_choice(sym)) {
1143 fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1144 menu->file->name, menu->lineno,
1145 sym->name ? sym->name : "<choice>",
1146 next_sym->name ? next_sym->name : "<choice>");
1147 } else if (sym_is_choice_value(sym)) {
1148 fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1149 menu->file->name, menu->lineno,
1150 sym->name ? sym->name : "<choice>",
1151 next_sym->name ? next_sym->name : "<choice>");
1152 } else {
1153 fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1154 prop->file->name, prop->lineno,
1155 sym->name ? sym->name : "<choice>",
1156 next_sym->name ? next_sym->name : "<choice>");
1160 if (check_top == &cv_stack)
1161 dep_stack_remove();
1164 static struct symbol *sym_check_expr_deps(struct expr *e)
1166 struct symbol *sym;
1168 if (!e)
1169 return NULL;
1170 switch (e->type) {
1171 case E_OR:
1172 case E_AND:
1173 sym = sym_check_expr_deps(e->left.expr);
1174 if (sym)
1175 return sym;
1176 return sym_check_expr_deps(e->right.expr);
1177 case E_NOT:
1178 return sym_check_expr_deps(e->left.expr);
1179 case E_EQUAL:
1180 case E_UNEQUAL:
1181 sym = sym_check_deps(e->left.sym);
1182 if (sym)
1183 return sym;
1184 return sym_check_deps(e->right.sym);
1185 case E_SYMBOL:
1186 return sym_check_deps(e->left.sym);
1187 default:
1188 break;
1190 printf("Oops! How to check %d?\n", e->type);
1191 return NULL;
1194 /* return NULL when dependencies are OK */
1195 static struct symbol *sym_check_sym_deps(struct symbol *sym)
1197 struct symbol *sym2;
1198 struct property *prop;
1199 struct dep_stack stack;
1201 dep_stack_insert(&stack, sym);
1203 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1204 if (sym2)
1205 goto out;
1207 for (prop = sym->prop; prop; prop = prop->next) {
1208 if (prop->type == P_CHOICE || prop->type == P_SELECT)
1209 continue;
1210 stack.prop = prop;
1211 sym2 = sym_check_expr_deps(prop->visible.expr);
1212 if (sym2)
1213 break;
1214 if (prop->type != P_DEFAULT || sym_is_choice(sym))
1215 continue;
1216 stack.expr = prop->expr;
1217 sym2 = sym_check_expr_deps(prop->expr);
1218 if (sym2)
1219 break;
1220 stack.expr = NULL;
1223 out:
1224 dep_stack_remove();
1226 return sym2;
1229 static struct symbol *sym_check_choice_deps(struct symbol *choice)
1231 struct symbol *sym, *sym2;
1232 struct property *prop;
1233 struct expr *e;
1234 struct dep_stack stack;
1236 dep_stack_insert(&stack, choice);
1238 prop = sym_get_choice_prop(choice);
1239 expr_list_for_each_sym(prop->expr, e, sym)
1240 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1242 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1243 sym2 = sym_check_sym_deps(choice);
1244 choice->flags &= ~SYMBOL_CHECK;
1245 if (sym2)
1246 goto out;
1248 expr_list_for_each_sym(prop->expr, e, sym) {
1249 sym2 = sym_check_sym_deps(sym);
1250 if (sym2)
1251 break;
1253 out:
1254 expr_list_for_each_sym(prop->expr, e, sym)
1255 sym->flags &= ~SYMBOL_CHECK;
1257 if (sym2 && sym_is_choice_value(sym2) &&
1258 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1259 sym2 = choice;
1261 dep_stack_remove();
1263 return sym2;
1266 struct symbol *sym_check_deps(struct symbol *sym)
1268 struct symbol *sym2;
1269 struct property *prop;
1271 if (sym->flags & SYMBOL_CHECK) {
1272 sym_check_print_recursive(sym);
1273 return sym;
1275 if (sym->flags & SYMBOL_CHECKED)
1276 return NULL;
1278 if (sym_is_choice_value(sym)) {
1279 struct dep_stack stack;
1281 /* for choice groups start the check with main choice symbol */
1282 dep_stack_insert(&stack, sym);
1283 prop = sym_get_choice_prop(sym);
1284 sym2 = sym_check_deps(prop_get_symbol(prop));
1285 dep_stack_remove();
1286 } else if (sym_is_choice(sym)) {
1287 sym2 = sym_check_choice_deps(sym);
1288 } else {
1289 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1290 sym2 = sym_check_sym_deps(sym);
1291 sym->flags &= ~SYMBOL_CHECK;
1294 if (sym2 && sym2 == sym)
1295 sym2 = NULL;
1297 return sym2;
1300 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
1302 struct property *prop;
1303 struct property **propp;
1305 prop = xmalloc(sizeof(*prop));
1306 memset(prop, 0, sizeof(*prop));
1307 prop->type = type;
1308 prop->sym = sym;
1309 prop->file = current_file;
1310 prop->lineno = zconf_lineno();
1312 /* append property to the prop list of symbol */
1313 if (sym) {
1314 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
1316 *propp = prop;
1319 return prop;
1322 struct symbol *prop_get_symbol(struct property *prop)
1324 if (prop->expr && (prop->expr->type == E_SYMBOL ||
1325 prop->expr->type == E_LIST))
1326 return prop->expr->left.sym;
1327 return NULL;
1330 const char *prop_get_type_name(enum prop_type type)
1332 switch (type) {
1333 case P_PROMPT:
1334 return "prompt";
1335 case P_ENV:
1336 return "env";
1337 case P_COMMENT:
1338 return "comment";
1339 case P_MENU:
1340 return "menu";
1341 case P_DEFAULT:
1342 return "default";
1343 case P_CHOICE:
1344 return "choice";
1345 case P_SELECT:
1346 return "select";
1347 case P_RANGE:
1348 return "range";
1349 case P_SYMBOL:
1350 return "symbol";
1351 case P_UNKNOWN:
1352 break;
1354 return "unknown";
1357 static void prop_add_env(const char *env)
1359 struct symbol *sym, *sym2;
1360 struct property *prop;
1361 char *p;
1363 sym = current_entry->sym;
1364 sym->flags |= SYMBOL_AUTO;
1365 for_all_properties(sym, prop, P_ENV) {
1366 sym2 = prop_get_symbol(prop);
1367 if (strcmp(sym2->name, env))
1368 menu_warn(current_entry, "redefining environment symbol from %s",
1369 sym2->name);
1370 return;
1373 prop = prop_alloc(P_ENV, sym);
1374 prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
1376 sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
1377 sym_env_list->right.sym = sym;
1379 p = getenv(env);
1380 if (p)
1381 sym_add_default(sym, p);
1382 else
1383 menu_warn(current_entry, "environment variable %s undefined", env);