Chameleon

Chameleon Svn Source Tree

Root/branches/chucko/i386/config/symbol.c

1/*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
5
6#include <ctype.h>
7#include <stdlib.h>
8#include <string.h>
9#include <regex.h>
10#include <sys/utsname.h>
11
12#define LKC_DIRECT_LINK
13#include "lkc.h"
14
15struct 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,
31};
32
33struct symbol *sym_defconfig_list;
34struct symbol *modules_sym;
35tristate modules_val;
36
37struct expr *sym_env_list;
38
39static void sym_add_default(struct symbol *sym, const char *def)
40{
41struct property *prop = prop_alloc(P_DEFAULT, sym);
42
43prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
44}
45
46void sym_init(void)
47{
48struct symbol *sym;
49struct utsname uts;
50static bool inited = false;
51
52if (inited)
53return;
54inited = true;
55
56uname(&uts);
57
58sym = sym_lookup("UNAME_RELEASE", 0);
59sym->type = S_STRING;
60sym->flags |= SYMBOL_AUTO;
61sym_add_default(sym, uts.release);
62}
63
64enum symbol_type sym_get_type(struct symbol *sym)
65{
66enum symbol_type type = sym->type;
67
68if (type == S_TRISTATE) {
69if (sym_is_choice_value(sym) && sym->visible == yes)
70type = S_BOOLEAN;
71else if (modules_val == no)
72type = S_BOOLEAN;
73}
74return type;
75}
76
77const char *sym_type_name(enum symbol_type type)
78{
79switch (type) {
80case S_BOOLEAN:
81return "boolean";
82case S_TRISTATE:
83return "tristate";
84case S_INT:
85return "integer";
86case S_HEX:
87return "hex";
88case S_STRING:
89return "string";
90case S_UNKNOWN:
91return "unknown";
92case S_OTHER:
93break;
94}
95return "???";
96}
97
98struct property *sym_get_choice_prop(struct symbol *sym)
99{
100struct property *prop;
101
102for_all_choices(sym, prop)
103return prop;
104return NULL;
105}
106
107struct property *sym_get_env_prop(struct symbol *sym)
108{
109struct property *prop;
110
111for_all_properties(sym, prop, P_ENV)
112return prop;
113return NULL;
114}
115
116struct property *sym_get_default_prop(struct symbol *sym)
117{
118struct property *prop;
119
120for_all_defaults(sym, prop) {
121prop->visible.tri = expr_calc_value(prop->visible.expr);
122if (prop->visible.tri != no)
123return prop;
124}
125return NULL;
126}
127
128static struct property *sym_get_range_prop(struct symbol *sym)
129{
130struct property *prop;
131
132for_all_properties(sym, prop, P_RANGE) {
133prop->visible.tri = expr_calc_value(prop->visible.expr);
134if (prop->visible.tri != no)
135return prop;
136}
137return NULL;
138}
139
140static int sym_get_range_val(struct symbol *sym, int base)
141{
142sym_calc_value(sym);
143switch (sym->type) {
144case S_INT:
145base = 10;
146break;
147case S_HEX:
148base = 16;
149break;
150default:
151break;
152}
153return strtol(sym->curr.val, NULL, base);
154}
155
156static void sym_validate_range(struct symbol *sym)
157{
158struct property *prop;
159int base, val, val2;
160char str[64];
161
162switch (sym->type) {
163case S_INT:
164base = 10;
165break;
166case S_HEX:
167base = 16;
168break;
169default:
170return;
171}
172prop = sym_get_range_prop(sym);
173if (!prop)
174return;
175val = strtol(sym->curr.val, NULL, base);
176val2 = sym_get_range_val(prop->expr->left.sym, base);
177if (val >= val2) {
178val2 = sym_get_range_val(prop->expr->right.sym, base);
179if (val <= val2)
180return;
181}
182if (sym->type == S_INT)
183sprintf(str, "%d", val2);
184else
185sprintf(str, "0x%x", val2);
186sym->curr.val = strdup(str);
187}
188
189static void sym_calc_visibility(struct symbol *sym)
190{
191struct property *prop;
192tristate tri;
193
194/* any prompt visible? */
195tri = no;
196for_all_prompts(sym, prop) {
197prop->visible.tri = expr_calc_value(prop->visible.expr);
198tri = EXPR_OR(tri, prop->visible.tri);
199}
200if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
201tri = yes;
202if (sym->visible != tri) {
203sym->visible = tri;
204sym_set_changed(sym);
205}
206if (sym_is_choice_value(sym))
207return;
208/* defaulting to "yes" if no explicit "depends on" are given */
209tri = yes;
210if (sym->dir_dep.expr)
211tri = expr_calc_value(sym->dir_dep.expr);
212if (tri == mod)
213tri = yes;
214if (sym->dir_dep.tri != tri) {
215sym->dir_dep.tri = tri;
216sym_set_changed(sym);
217}
218tri = no;
219if (sym->rev_dep.expr)
220tri = expr_calc_value(sym->rev_dep.expr);
221if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
222tri = yes;
223if (sym->rev_dep.tri != tri) {
224sym->rev_dep.tri = tri;
225sym_set_changed(sym);
226}
227}
228
229/*
230 * Find the default symbol for a choice.
231 * First try the default values for the choice symbol
232 * Next locate the first visible choice value
233 * Return NULL if none was found
234 */
235struct symbol *sym_choice_default(struct symbol *sym)
236{
237struct symbol *def_sym;
238struct property *prop;
239struct expr *e;
240
241/* any of the defaults visible? */
242for_all_defaults(sym, prop) {
243prop->visible.tri = expr_calc_value(prop->visible.expr);
244if (prop->visible.tri == no)
245continue;
246def_sym = prop_get_symbol(prop);
247if (def_sym->visible != no)
248return def_sym;
249}
250
251/* just get the first visible value */
252prop = sym_get_choice_prop(sym);
253expr_list_for_each_sym(prop->expr, e, def_sym)
254if (def_sym->visible != no)
255return def_sym;
256
257/* failed to locate any defaults */
258return NULL;
259}
260
261static struct symbol *sym_calc_choice(struct symbol *sym)
262{
263struct symbol *def_sym;
264struct property *prop;
265struct expr *e;
266
267/* first calculate all choice values' visibilities */
268prop = sym_get_choice_prop(sym);
269expr_list_for_each_sym(prop->expr, e, def_sym)
270sym_calc_visibility(def_sym);
271
272/* is the user choice visible? */
273def_sym = sym->def[S_DEF_USER].val;
274if (def_sym && def_sym->visible != no)
275return def_sym;
276
277def_sym = sym_choice_default(sym);
278
279if (def_sym == NULL)
280/* no choice? reset tristate value */
281sym->curr.tri = no;
282
283return def_sym;
284}
285
286void sym_calc_value(struct symbol *sym)
287{
288struct symbol_value newval, oldval;
289struct property *prop;
290struct expr *e;
291
292if (!sym)
293return;
294
295if (sym->flags & SYMBOL_VALID)
296return;
297sym->flags |= SYMBOL_VALID;
298
299oldval = sym->curr;
300
301switch (sym->type) {
302case S_INT:
303case S_HEX:
304case S_STRING:
305newval = symbol_empty.curr;
306break;
307case S_BOOLEAN:
308case S_TRISTATE:
309newval = symbol_no.curr;
310break;
311default:
312sym->curr.val = sym->name;
313sym->curr.tri = no;
314return;
315}
316if (!sym_is_choice_value(sym))
317sym->flags &= ~SYMBOL_WRITE;
318
319sym_calc_visibility(sym);
320
321/* set default if recursively called */
322sym->curr = newval;
323
324switch (sym_get_type(sym)) {
325case S_BOOLEAN:
326case S_TRISTATE:
327if (sym_is_choice_value(sym) && sym->visible == yes) {
328prop = sym_get_choice_prop(sym);
329newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
330} else {
331if (sym->visible != no) {
332/* if the symbol is visible use the user value
333 * if available, otherwise try the default value
334 */
335sym->flags |= SYMBOL_WRITE;
336if (sym_has_value(sym)) {
337newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
338 sym->visible);
339goto calc_newval;
340}
341}
342if (sym->rev_dep.tri != no)
343sym->flags |= SYMBOL_WRITE;
344if (!sym_is_choice(sym)) {
345prop = sym_get_default_prop(sym);
346if (prop) {
347sym->flags |= SYMBOL_WRITE;
348newval.tri = EXPR_AND(expr_calc_value(prop->expr),
349 prop->visible.tri);
350}
351}
352calc_newval:
353if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
354struct expr *e;
355e = expr_simplify_unmet_dep(sym->rev_dep.expr,
356 sym->dir_dep.expr);
357fprintf(stderr, "warning: (");
358expr_fprint(e, stderr);
359fprintf(stderr, ") selects %s which has unmet direct dependencies (",
360sym->name);
361expr_fprint(sym->dir_dep.expr, stderr);
362fprintf(stderr, ")\n");
363expr_free(e);
364}
365newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
366}
367if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
368newval.tri = yes;
369break;
370case S_STRING:
371case S_HEX:
372case S_INT:
373if (sym->visible != no) {
374sym->flags |= SYMBOL_WRITE;
375if (sym_has_value(sym)) {
376newval.val = sym->def[S_DEF_USER].val;
377break;
378}
379}
380prop = sym_get_default_prop(sym);
381if (prop) {
382struct symbol *ds = prop_get_symbol(prop);
383if (ds) {
384sym->flags |= SYMBOL_WRITE;
385sym_calc_value(ds);
386newval.val = ds->curr.val;
387}
388}
389break;
390default:
391;
392}
393
394sym->curr = newval;
395if (sym_is_choice(sym) && newval.tri == yes)
396sym->curr.val = sym_calc_choice(sym);
397sym_validate_range(sym);
398
399if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
400sym_set_changed(sym);
401if (modules_sym == sym) {
402sym_set_all_changed();
403modules_val = modules_sym->curr.tri;
404}
405}
406
407if (sym_is_choice(sym)) {
408struct symbol *choice_sym;
409
410prop = sym_get_choice_prop(sym);
411expr_list_for_each_sym(prop->expr, e, choice_sym) {
412if ((sym->flags & SYMBOL_WRITE) &&
413 choice_sym->visible != no)
414choice_sym->flags |= SYMBOL_WRITE;
415if (sym->flags & SYMBOL_CHANGED)
416sym_set_changed(choice_sym);
417}
418}
419
420if (sym->flags & SYMBOL_AUTO)
421sym->flags &= ~SYMBOL_WRITE;
422}
423
424void sym_clear_all_valid(void)
425{
426struct symbol *sym;
427int i;
428
429for_all_symbols(i, sym)
430sym->flags &= ~SYMBOL_VALID;
431sym_add_change_count(1);
432if (modules_sym)
433sym_calc_value(modules_sym);
434}
435
436void sym_set_changed(struct symbol *sym)
437{
438struct property *prop;
439
440sym->flags |= SYMBOL_CHANGED;
441for (prop = sym->prop; prop; prop = prop->next) {
442if (prop->menu)
443prop->menu->flags |= MENU_CHANGED;
444}
445}
446
447void sym_set_all_changed(void)
448{
449struct symbol *sym;
450int i;
451
452for_all_symbols(i, sym)
453sym_set_changed(sym);
454}
455
456bool sym_tristate_within_range(struct symbol *sym, tristate val)
457{
458int type = sym_get_type(sym);
459
460if (sym->visible == no)
461return false;
462
463if (type != S_BOOLEAN && type != S_TRISTATE)
464return false;
465
466if (type == S_BOOLEAN && val == mod)
467return false;
468if (sym->visible <= sym->rev_dep.tri)
469return false;
470if (sym_is_choice_value(sym) && sym->visible == yes)
471return val == yes;
472return val >= sym->rev_dep.tri && val <= sym->visible;
473}
474
475bool sym_set_tristate_value(struct symbol *sym, tristate val)
476{
477tristate oldval = sym_get_tristate_value(sym);
478
479if (oldval != val && !sym_tristate_within_range(sym, val))
480return false;
481
482if (!(sym->flags & SYMBOL_DEF_USER)) {
483sym->flags |= SYMBOL_DEF_USER;
484sym_set_changed(sym);
485}
486/*
487 * setting a choice value also resets the new flag of the choice
488 * symbol and all other choice values.
489 */
490if (sym_is_choice_value(sym) && val == yes) {
491struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
492struct property *prop;
493struct expr *e;
494
495cs->def[S_DEF_USER].val = sym;
496cs->flags |= SYMBOL_DEF_USER;
497prop = sym_get_choice_prop(cs);
498for (e = prop->expr; e; e = e->left.expr) {
499if (e->right.sym->visible != no)
500e->right.sym->flags |= SYMBOL_DEF_USER;
501}
502}
503
504sym->def[S_DEF_USER].tri = val;
505if (oldval != val)
506sym_clear_all_valid();
507
508return true;
509}
510
511tristate sym_toggle_tristate_value(struct symbol *sym)
512{
513tristate oldval, newval;
514
515oldval = newval = sym_get_tristate_value(sym);
516do {
517switch (newval) {
518case no:
519newval = mod;
520break;
521case mod:
522newval = yes;
523break;
524case yes:
525newval = no;
526break;
527}
528if (sym_set_tristate_value(sym, newval))
529break;
530} while (oldval != newval);
531return newval;
532}
533
534bool sym_string_valid(struct symbol *sym, const char *str)
535{
536signed char ch;
537
538switch (sym->type) {
539case S_STRING:
540return true;
541case S_INT:
542ch = *str++;
543if (ch == '-')
544ch = *str++;
545if (!isdigit(ch))
546return false;
547if (ch == '0' && *str != 0)
548return false;
549while ((ch = *str++)) {
550if (!isdigit(ch))
551return false;
552}
553return true;
554case S_HEX:
555if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
556str += 2;
557ch = *str++;
558do {
559if (!isxdigit(ch))
560return false;
561} while ((ch = *str++));
562return true;
563case S_BOOLEAN:
564case S_TRISTATE:
565switch (str[0]) {
566case 'y': case 'Y':
567case 'm': case 'M':
568case 'n': case 'N':
569return true;
570}
571return false;
572default:
573return false;
574}
575}
576
577bool sym_string_within_range(struct symbol *sym, const char *str)
578{
579struct property *prop;
580int val;
581
582switch (sym->type) {
583case S_STRING:
584return sym_string_valid(sym, str);
585case S_INT:
586if (!sym_string_valid(sym, str))
587return false;
588prop = sym_get_range_prop(sym);
589if (!prop)
590return true;
591val = strtol(str, NULL, 10);
592return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
593 val <= sym_get_range_val(prop->expr->right.sym, 10);
594case S_HEX:
595if (!sym_string_valid(sym, str))
596return false;
597prop = sym_get_range_prop(sym);
598if (!prop)
599return true;
600val = strtol(str, NULL, 16);
601return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
602 val <= sym_get_range_val(prop->expr->right.sym, 16);
603case S_BOOLEAN:
604case S_TRISTATE:
605switch (str[0]) {
606case 'y': case 'Y':
607return sym_tristate_within_range(sym, yes);
608case 'm': case 'M':
609return sym_tristate_within_range(sym, mod);
610case 'n': case 'N':
611return sym_tristate_within_range(sym, no);
612}
613return false;
614default:
615return false;
616}
617}
618
619bool sym_set_string_value(struct symbol *sym, const char *newval)
620{
621const char *oldval;
622char *val;
623int size;
624
625switch (sym->type) {
626case S_BOOLEAN:
627case S_TRISTATE:
628switch (newval[0]) {
629case 'y': case 'Y':
630return sym_set_tristate_value(sym, yes);
631case 'm': case 'M':
632return sym_set_tristate_value(sym, mod);
633case 'n': case 'N':
634return sym_set_tristate_value(sym, no);
635}
636return false;
637default:
638;
639}
640
641if (!sym_string_within_range(sym, newval))
642return false;
643
644if (!(sym->flags & SYMBOL_DEF_USER)) {
645sym->flags |= SYMBOL_DEF_USER;
646sym_set_changed(sym);
647}
648
649oldval = sym->def[S_DEF_USER].val;
650size = strlen(newval) + 1;
651if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
652size += 2;
653sym->def[S_DEF_USER].val = val = malloc(size);
654*val++ = '0';
655*val++ = 'x';
656} else if (!oldval || strcmp(oldval, newval))
657sym->def[S_DEF_USER].val = val = malloc(size);
658else
659return true;
660
661strcpy(val, newval);
662free((void *)oldval);
663sym_clear_all_valid();
664
665return true;
666}
667
668/*
669 * Find the default value associated to a symbol.
670 * For tristate symbol handle the modules=n case
671 * in which case "m" becomes "y".
672 * If the symbol does not have any default then fallback
673 * to the fixed default values.
674 */
675const char *sym_get_string_default(struct symbol *sym)
676{
677struct property *prop;
678struct symbol *ds;
679const char *str;
680tristate val;
681
682sym_calc_visibility(sym);
683sym_calc_value(modules_sym);
684val = symbol_no.curr.tri;
685str = symbol_empty.curr.val;
686
687/* If symbol has a default value look it up */
688prop = sym_get_default_prop(sym);
689if (prop != NULL) {
690switch (sym->type) {
691case S_BOOLEAN:
692case S_TRISTATE:
693/* The visibility may limit the value from yes => mod */
694val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
695break;
696default:
697/*
698 * The following fails to handle the situation
699 * where a default value is further limited by
700 * the valid range.
701 */
702ds = prop_get_symbol(prop);
703if (ds != NULL) {
704sym_calc_value(ds);
705str = (const char *)ds->curr.val;
706}
707}
708}
709
710/* Handle select statements */
711val = EXPR_OR(val, sym->rev_dep.tri);
712
713/* transpose mod to yes if modules are not enabled */
714if (val == mod)
715if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
716val = yes;
717
718/* transpose mod to yes if type is bool */
719if (sym->type == S_BOOLEAN && val == mod)
720val = yes;
721
722switch (sym->type) {
723case S_BOOLEAN:
724case S_TRISTATE:
725switch (val) {
726case no: return "n";
727case mod: return "m";
728case yes: return "y";
729}
730case S_INT:
731case S_HEX:
732return str;
733case S_STRING:
734return str;
735case S_OTHER:
736case S_UNKNOWN:
737break;
738}
739return "";
740}
741
742const char *sym_get_string_value(struct symbol *sym)
743{
744tristate val;
745
746switch (sym->type) {
747case S_BOOLEAN:
748case S_TRISTATE:
749val = sym_get_tristate_value(sym);
750switch (val) {
751case no:
752return "n";
753case mod:
754return "m";
755case yes:
756return "y";
757}
758break;
759default:
760;
761}
762return (const char *)sym->curr.val;
763}
764
765bool sym_is_changable(struct symbol *sym)
766{
767return sym->visible > sym->rev_dep.tri;
768}
769
770static unsigned strhash(const char *s)
771{
772/* fnv32 hash */
773unsigned hash = 2166136261U;
774for (; *s; s++)
775hash = (hash ^ *s) * 0x01000193;
776return hash;
777}
778
779struct symbol *sym_lookup(const char *name, int flags)
780{
781struct symbol *symbol;
782char *new_name;
783int hash;
784
785if (name) {
786if (name[0] && !name[1]) {
787switch (name[0]) {
788case 'y': return &symbol_yes;
789case 'm': return &symbol_mod;
790case 'n': return &symbol_no;
791}
792}
793hash = strhash(name) % SYMBOL_HASHSIZE;
794
795for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
796if (symbol->name &&
797 !strcmp(symbol->name, name) &&
798 (flags ? symbol->flags & flags
799 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
800return symbol;
801}
802new_name = strdup(name);
803} else {
804new_name = NULL;
805hash = 0;
806}
807
808symbol = malloc(sizeof(*symbol));
809memset(symbol, 0, sizeof(*symbol));
810symbol->name = new_name;
811symbol->type = S_UNKNOWN;
812symbol->flags |= flags;
813
814symbol->next = symbol_hash[hash];
815symbol_hash[hash] = symbol;
816
817return symbol;
818}
819
820struct symbol *sym_find(const char *name)
821{
822struct symbol *symbol = NULL;
823int hash = 0;
824
825if (!name)
826return NULL;
827
828if (name[0] && !name[1]) {
829switch (name[0]) {
830case 'y': return &symbol_yes;
831case 'm': return &symbol_mod;
832case 'n': return &symbol_no;
833}
834}
835hash = strhash(name) % SYMBOL_HASHSIZE;
836
837for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
838if (symbol->name &&
839 !strcmp(symbol->name, name) &&
840 !(symbol->flags & SYMBOL_CONST))
841break;
842}
843
844return symbol;
845}
846
847/*
848 * Expand symbol's names embedded in the string given in argument. Symbols'
849 * name to be expanded shall be prefixed by a '$'. Unknown symbol expands to
850 * the empty string.
851 */
852const char *sym_expand_string_value(const char *in)
853{
854const char *src;
855char *res;
856size_t reslen;
857
858reslen = strlen(in) + 1;
859res = malloc(reslen);
860res[0] = '\0';
861
862while ((src = strchr(in, '$'))) {
863char *p, name[SYMBOL_MAXLENGTH];
864const char *symval = "";
865struct symbol *sym;
866size_t newlen;
867
868strncat(res, in, src - in);
869src++;
870
871p = name;
872while (isalnum(*src) || *src == '_')
873*p++ = *src++;
874*p = '\0';
875
876sym = sym_find(name);
877if (sym != NULL) {
878sym_calc_value(sym);
879symval = sym_get_string_value(sym);
880}
881
882newlen = strlen(res) + strlen(symval) + strlen(src) + 1;
883if (newlen > reslen) {
884char* newres = NULL;
885if (!(newres = realloc(res, newlen))) {
886/* TODO: handle error gracefully - for now, punt */
887break;
888}
889res = newres;
890reslen = newlen;
891}
892
893strcat(res, symval);
894in = src;
895}
896strcat(res, in);
897
898return res;
899}
900
901struct symbol **sym_re_search(const char *pattern)
902{
903struct symbol *sym, **sym_arr = NULL;
904int i, cnt, size;
905regex_t re;
906
907cnt = size = 0;
908/* Skip if empty */
909if (strlen(pattern) == 0)
910return NULL;
911if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
912return NULL;
913
914for_all_symbols(i, sym) {
915if (sym->flags & SYMBOL_CONST || !sym->name)
916continue;
917if (regexec(&re, sym->name, 0, NULL, 0))
918continue;
919if (cnt + 1 >= size) {
920void *tmp = sym_arr;
921size += 16;
922sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
923if (!sym_arr) {
924free(tmp);
925return NULL;
926}
927}
928sym_calc_value(sym);
929sym_arr[cnt++] = sym;
930}
931if (sym_arr)
932sym_arr[cnt] = NULL;
933regfree(&re);
934
935return sym_arr;
936}
937
938/*
939 * When we check for recursive dependencies we use a stack to save
940 * current state so we can print out relevant info to user.
941 * The entries are located on the call stack so no need to free memory.
942 * Note inser() remove() must always match to properly clear the stack.
943 */
944static struct dep_stack {
945struct dep_stack *prev, *next;
946struct symbol *sym;
947struct property *prop;
948struct expr *expr;
949} *check_top;
950
951static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
952{
953memset(stack, 0, sizeof(*stack));
954if (check_top)
955check_top->next = stack;
956stack->prev = check_top;
957stack->sym = sym;
958check_top = stack;
959}
960
961static void dep_stack_remove(void)
962{
963check_top = check_top->prev;
964if (check_top)
965check_top->next = NULL;
966}
967
968/*
969 * Called when we have detected a recursive dependency.
970 * check_top point to the top of the stact so we use
971 * the ->prev pointer to locate the bottom of the stack.
972 */
973static void sym_check_print_recursive(struct symbol *last_sym)
974{
975struct dep_stack *stack;
976struct symbol *sym, *next_sym;
977struct menu *menu = NULL;
978struct property *prop;
979struct dep_stack cv_stack;
980
981if (sym_is_choice_value(last_sym)) {
982dep_stack_insert(&cv_stack, last_sym);
983last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
984}
985
986for (stack = check_top; stack != NULL; stack = stack->prev)
987if (stack->sym == last_sym)
988break;
989if (!stack) {
990fprintf(stderr, "unexpected recursive dependency error\n");
991return;
992}
993
994for (; stack; stack = stack->next) {
995sym = stack->sym;
996next_sym = stack->next ? stack->next->sym : last_sym;
997prop = stack->prop;
998if (prop == NULL)
999prop = stack->sym->prop;
1000
1001/* for choice values find the menu entry (used below) */
1002if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1003for (prop = sym->prop; prop; prop = prop->next) {
1004menu = prop->menu;
1005if (prop->menu)
1006break;
1007}
1008}
1009if (stack->sym == last_sym)
1010fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1011prop->file->name, prop->lineno);
1012if (stack->expr) {
1013fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1014prop->file->name, prop->lineno,
1015sym->name ? sym->name : "<choice>",
1016prop_get_type_name(prop->type),
1017next_sym->name ? next_sym->name : "<choice>");
1018} else if (stack->prop) {
1019fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1020prop->file->name, prop->lineno,
1021sym->name ? sym->name : "<choice>",
1022next_sym->name ? next_sym->name : "<choice>");
1023} else if (sym_is_choice(sym)) {
1024fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1025menu->file->name, menu->lineno,
1026sym->name ? sym->name : "<choice>",
1027next_sym->name ? next_sym->name : "<choice>");
1028} else if (sym_is_choice_value(sym)) {
1029fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1030menu->file->name, menu->lineno,
1031sym->name ? sym->name : "<choice>",
1032next_sym->name ? next_sym->name : "<choice>");
1033} else {
1034fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1035prop->file->name, prop->lineno,
1036sym->name ? sym->name : "<choice>",
1037next_sym->name ? next_sym->name : "<choice>");
1038}
1039}
1040
1041if (check_top == &cv_stack)
1042dep_stack_remove();
1043}
1044
1045static struct symbol *sym_check_expr_deps(struct expr *e)
1046{
1047struct symbol *sym;
1048
1049if (!e)
1050return NULL;
1051switch (e->type) {
1052case E_OR:
1053case E_AND:
1054sym = sym_check_expr_deps(e->left.expr);
1055if (sym)
1056return sym;
1057return sym_check_expr_deps(e->right.expr);
1058case E_NOT:
1059return sym_check_expr_deps(e->left.expr);
1060case E_EQUAL:
1061case E_UNEQUAL:
1062sym = sym_check_deps(e->left.sym);
1063if (sym)
1064return sym;
1065return sym_check_deps(e->right.sym);
1066case E_SYMBOL:
1067return sym_check_deps(e->left.sym);
1068default:
1069break;
1070}
1071printf("Oops! How to check %d?\n", e->type);
1072return NULL;
1073}
1074
1075/* return NULL when dependencies are OK */
1076static struct symbol *sym_check_sym_deps(struct symbol *sym)
1077{
1078struct symbol *sym2;
1079struct property *prop;
1080struct dep_stack stack;
1081
1082dep_stack_insert(&stack, sym);
1083
1084sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1085if (sym2)
1086goto out;
1087
1088for (prop = sym->prop; prop; prop = prop->next) {
1089if (prop->type == P_CHOICE || prop->type == P_SELECT)
1090continue;
1091stack.prop = prop;
1092sym2 = sym_check_expr_deps(prop->visible.expr);
1093if (sym2)
1094break;
1095if (prop->type != P_DEFAULT || sym_is_choice(sym))
1096continue;
1097stack.expr = prop->expr;
1098sym2 = sym_check_expr_deps(prop->expr);
1099if (sym2)
1100break;
1101stack.expr = NULL;
1102}
1103
1104out:
1105dep_stack_remove();
1106
1107return sym2;
1108}
1109
1110static struct symbol *sym_check_choice_deps(struct symbol *choice)
1111{
1112struct symbol *sym, *sym2;
1113struct property *prop;
1114struct expr *e;
1115struct dep_stack stack;
1116
1117dep_stack_insert(&stack, choice);
1118
1119prop = sym_get_choice_prop(choice);
1120expr_list_for_each_sym(prop->expr, e, sym)
1121sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1122
1123choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1124sym2 = sym_check_sym_deps(choice);
1125choice->flags &= ~SYMBOL_CHECK;
1126if (sym2)
1127goto out;
1128
1129expr_list_for_each_sym(prop->expr, e, sym) {
1130sym2 = sym_check_sym_deps(sym);
1131if (sym2)
1132break;
1133}
1134out:
1135expr_list_for_each_sym(prop->expr, e, sym)
1136sym->flags &= ~SYMBOL_CHECK;
1137
1138if (sym2 && sym_is_choice_value(sym2) &&
1139 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1140sym2 = choice;
1141
1142dep_stack_remove();
1143
1144return sym2;
1145}
1146
1147struct symbol *sym_check_deps(struct symbol *sym)
1148{
1149struct symbol *sym2;
1150struct property *prop;
1151
1152if (sym->flags & SYMBOL_CHECK) {
1153sym_check_print_recursive(sym);
1154return sym;
1155}
1156if (sym->flags & SYMBOL_CHECKED)
1157return NULL;
1158
1159if (sym_is_choice_value(sym)) {
1160struct dep_stack stack;
1161
1162/* for choice groups start the check with main choice symbol */
1163dep_stack_insert(&stack, sym);
1164prop = sym_get_choice_prop(sym);
1165sym2 = sym_check_deps(prop_get_symbol(prop));
1166dep_stack_remove();
1167} else if (sym_is_choice(sym)) {
1168sym2 = sym_check_choice_deps(sym);
1169} else {
1170sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1171sym2 = sym_check_sym_deps(sym);
1172sym->flags &= ~SYMBOL_CHECK;
1173}
1174
1175if (sym2 && sym2 == sym)
1176sym2 = NULL;
1177
1178return sym2;
1179}
1180
1181struct property *prop_alloc(enum prop_type type, struct symbol *sym)
1182{
1183struct property *prop;
1184struct property **propp;
1185
1186prop = malloc(sizeof(*prop));
1187memset(prop, 0, sizeof(*prop));
1188prop->type = type;
1189prop->sym = sym;
1190prop->file = current_file;
1191prop->lineno = zconf_lineno();
1192
1193/* append property to the prop list of symbol */
1194if (sym) {
1195for (propp = &sym->prop; *propp; propp = &(*propp)->next)
1196;
1197*propp = prop;
1198}
1199
1200return prop;
1201}
1202
1203struct symbol *prop_get_symbol(struct property *prop)
1204{
1205if (prop->expr && (prop->expr->type == E_SYMBOL ||
1206 prop->expr->type == E_LIST))
1207return prop->expr->left.sym;
1208return NULL;
1209}
1210
1211const char *prop_get_type_name(enum prop_type type)
1212{
1213switch (type) {
1214case P_PROMPT:
1215return "prompt";
1216case P_ENV:
1217return "env";
1218case P_COMMENT:
1219return "comment";
1220case P_MENU:
1221return "menu";
1222case P_DEFAULT:
1223return "default";
1224case P_CHOICE:
1225return "choice";
1226case P_SELECT:
1227return "select";
1228case P_RANGE:
1229return "range";
1230case P_SYMBOL:
1231return "symbol";
1232case P_UNKNOWN:
1233break;
1234}
1235return "unknown";
1236}
1237

Archive Download this file

Revision: 2329