]> bbs.cooldavid.org Git - net-next-2.6.git/blob - scripts/kconfig/symbol.c
kconfig: Don't write invisible choice values
[net-next-2.6.git] / scripts / kconfig / 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
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,
31 };
32
33 struct symbol *sym_defconfig_list;
34 struct symbol *modules_sym;
35 tristate modules_val;
36
37 struct expr *sym_env_list;
38
39 static void sym_add_default(struct symbol *sym, const char *def)
40 {
41         struct property *prop = prop_alloc(P_DEFAULT, sym);
42
43         prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
44 }
45
46 void sym_init(void)
47 {
48         struct symbol *sym;
49         struct utsname uts;
50         static bool inited = false;
51
52         if (inited)
53                 return;
54         inited = true;
55
56         uname(&uts);
57
58         sym = sym_lookup("UNAME_RELEASE", 0);
59         sym->type = S_STRING;
60         sym->flags |= SYMBOL_AUTO;
61         sym_add_default(sym, uts.release);
62 }
63
64 enum symbol_type sym_get_type(struct symbol *sym)
65 {
66         enum symbol_type type = sym->type;
67
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;
73         }
74         return type;
75 }
76
77 const char *sym_type_name(enum symbol_type type)
78 {
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;
94         }
95         return "???";
96 }
97
98 struct property *sym_get_choice_prop(struct symbol *sym)
99 {
100         struct property *prop;
101
102         for_all_choices(sym, prop)
103                 return prop;
104         return NULL;
105 }
106
107 struct property *sym_get_env_prop(struct symbol *sym)
108 {
109         struct property *prop;
110
111         for_all_properties(sym, prop, P_ENV)
112                 return prop;
113         return NULL;
114 }
115
116 struct property *sym_get_default_prop(struct symbol *sym)
117 {
118         struct property *prop;
119
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;
124         }
125         return NULL;
126 }
127
128 static struct property *sym_get_range_prop(struct symbol *sym)
129 {
130         struct property *prop;
131
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;
136         }
137         return NULL;
138 }
139
140 static int sym_get_range_val(struct symbol *sym, int base)
141 {
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;
152         }
153         return strtol(sym->curr.val, NULL, base);
154 }
155
156 static void sym_validate_range(struct symbol *sym)
157 {
158         struct property *prop;
159         int base, val, val2;
160         char str[64];
161
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;
171         }
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;
181         }
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);
187 }
188
189 static void sym_calc_visibility(struct symbol *sym)
190 {
191         struct property *prop;
192         tristate tri;
193
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);
199         }
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);
205         }
206         if (sym_is_choice_value(sym))
207                 return;
208         /* defaulting to "yes" if no explicit "depends on" are given */
209         tri = yes;
210         if (sym->dir_dep.expr)
211                 tri = expr_calc_value(sym->dir_dep.expr);
212         if (tri == mod)
213                 tri = yes;
214         if (sym->dir_dep.tri != tri) {
215                 sym->dir_dep.tri = tri;
216                 sym_set_changed(sym);
217         }
218         tri = no;
219         if (sym->rev_dep.expr)
220                 tri = expr_calc_value(sym->rev_dep.expr);
221         if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
222                 tri = yes;
223         if (sym->rev_dep.tri != tri) {
224                 sym->rev_dep.tri = tri;
225                 sym_set_changed(sym);
226         }
227 }
228
229 static struct symbol *sym_calc_choice(struct symbol *sym)
230 {
231         struct symbol *def_sym;
232         struct property *prop;
233         struct expr *e;
234
235         /* first calculate all choice values' visibilities */
236         prop = sym_get_choice_prop(sym);
237         expr_list_for_each_sym(prop->expr, e, def_sym)
238                 sym_calc_visibility(def_sym);
239
240         /* is the user choice visible? */
241         def_sym = sym->def[S_DEF_USER].val;
242         if (def_sym && def_sym->visible != no)
243                 return def_sym;
244
245         /* any of the defaults visible? */
246         for_all_defaults(sym, prop) {
247                 prop->visible.tri = expr_calc_value(prop->visible.expr);
248                 if (prop->visible.tri == no)
249                         continue;
250                 def_sym = prop_get_symbol(prop);
251                 if (def_sym->visible != no)
252                         return def_sym;
253         }
254
255         /* just get the first visible value */
256         prop = sym_get_choice_prop(sym);
257         expr_list_for_each_sym(prop->expr, e, def_sym)
258                 if (def_sym->visible != no)
259                         return def_sym;
260
261         /* no choice? reset tristate value */
262         sym->curr.tri = no;
263         return NULL;
264 }
265
266 void sym_calc_value(struct symbol *sym)
267 {
268         struct symbol_value newval, oldval;
269         struct property *prop;
270         struct expr *e;
271
272         if (!sym)
273                 return;
274
275         if (sym->flags & SYMBOL_VALID)
276                 return;
277         sym->flags |= SYMBOL_VALID;
278
279         oldval = sym->curr;
280
281         switch (sym->type) {
282         case S_INT:
283         case S_HEX:
284         case S_STRING:
285                 newval = symbol_empty.curr;
286                 break;
287         case S_BOOLEAN:
288         case S_TRISTATE:
289                 newval = symbol_no.curr;
290                 break;
291         default:
292                 sym->curr.val = sym->name;
293                 sym->curr.tri = no;
294                 return;
295         }
296         if (!sym_is_choice_value(sym))
297                 sym->flags &= ~SYMBOL_WRITE;
298
299         sym_calc_visibility(sym);
300
301         /* set default if recursively called */
302         sym->curr = newval;
303
304         switch (sym_get_type(sym)) {
305         case S_BOOLEAN:
306         case S_TRISTATE:
307                 if (sym_is_choice_value(sym) && sym->visible == yes) {
308                         prop = sym_get_choice_prop(sym);
309                         newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
310                 } else {
311                         if (sym->visible != no) {
312                                 /* if the symbol is visible use the user value
313                                  * if available, otherwise try the default value
314                                  */
315                                 sym->flags |= SYMBOL_WRITE;
316                                 if (sym_has_value(sym)) {
317                                         newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
318                                                               sym->visible);
319                                         goto calc_newval;
320                                 }
321                         }
322                         if (sym->rev_dep.tri != no)
323                                 sym->flags |= SYMBOL_WRITE;
324                         if (!sym_is_choice(sym)) {
325                                 prop = sym_get_default_prop(sym);
326                                 if (prop) {
327                                         sym->flags |= SYMBOL_WRITE;
328                                         newval.tri = EXPR_AND(expr_calc_value(prop->expr),
329                                                               prop->visible.tri);
330                                 }
331                         }
332                 calc_newval:
333                         if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
334                                 fprintf(stderr, "warning: (");
335                                 expr_fprint(sym->rev_dep.expr, stderr);
336                                 fprintf(stderr, ") selects %s which has unmet direct dependencies (",
337                                         sym->name);
338                                 expr_fprint(sym->dir_dep.expr, stderr);
339                                 fprintf(stderr, ")\n");
340                         }
341                         newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
342                 }
343                 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
344                         newval.tri = yes;
345                 break;
346         case S_STRING:
347         case S_HEX:
348         case S_INT:
349                 if (sym->visible != no) {
350                         sym->flags |= SYMBOL_WRITE;
351                         if (sym_has_value(sym)) {
352                                 newval.val = sym->def[S_DEF_USER].val;
353                                 break;
354                         }
355                 }
356                 prop = sym_get_default_prop(sym);
357                 if (prop) {
358                         struct symbol *ds = prop_get_symbol(prop);
359                         if (ds) {
360                                 sym->flags |= SYMBOL_WRITE;
361                                 sym_calc_value(ds);
362                                 newval.val = ds->curr.val;
363                         }
364                 }
365                 break;
366         default:
367                 ;
368         }
369
370         sym->curr = newval;
371         if (sym_is_choice(sym) && newval.tri == yes)
372                 sym->curr.val = sym_calc_choice(sym);
373         sym_validate_range(sym);
374
375         if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
376                 sym_set_changed(sym);
377                 if (modules_sym == sym) {
378                         sym_set_all_changed();
379                         modules_val = modules_sym->curr.tri;
380                 }
381         }
382
383         if (sym_is_choice(sym)) {
384                 struct symbol *choice_sym;
385
386                 prop = sym_get_choice_prop(sym);
387                 expr_list_for_each_sym(prop->expr, e, choice_sym) {
388                         if ((sym->flags & SYMBOL_WRITE) &&
389                             choice_sym->visible != no)
390                                 choice_sym->flags |= SYMBOL_WRITE;
391                         if (sym->flags & SYMBOL_CHANGED)
392                                 sym_set_changed(choice_sym);
393                 }
394         }
395
396         if (sym->flags & SYMBOL_AUTO)
397                 sym->flags &= ~SYMBOL_WRITE;
398 }
399
400 void sym_clear_all_valid(void)
401 {
402         struct symbol *sym;
403         int i;
404
405         for_all_symbols(i, sym)
406                 sym->flags &= ~SYMBOL_VALID;
407         sym_add_change_count(1);
408         if (modules_sym)
409                 sym_calc_value(modules_sym);
410 }
411
412 void sym_set_changed(struct symbol *sym)
413 {
414         struct property *prop;
415
416         sym->flags |= SYMBOL_CHANGED;
417         for (prop = sym->prop; prop; prop = prop->next) {
418                 if (prop->menu)
419                         prop->menu->flags |= MENU_CHANGED;
420         }
421 }
422
423 void sym_set_all_changed(void)
424 {
425         struct symbol *sym;
426         int i;
427
428         for_all_symbols(i, sym)
429                 sym_set_changed(sym);
430 }
431
432 bool sym_tristate_within_range(struct symbol *sym, tristate val)
433 {
434         int type = sym_get_type(sym);
435
436         if (sym->visible == no)
437                 return false;
438
439         if (type != S_BOOLEAN && type != S_TRISTATE)
440                 return false;
441
442         if (type == S_BOOLEAN && val == mod)
443                 return false;
444         if (sym->visible <= sym->rev_dep.tri)
445                 return false;
446         if (sym_is_choice_value(sym) && sym->visible == yes)
447                 return val == yes;
448         return val >= sym->rev_dep.tri && val <= sym->visible;
449 }
450
451 bool sym_set_tristate_value(struct symbol *sym, tristate val)
452 {
453         tristate oldval = sym_get_tristate_value(sym);
454
455         if (oldval != val && !sym_tristate_within_range(sym, val))
456                 return false;
457
458         if (!(sym->flags & SYMBOL_DEF_USER)) {
459                 sym->flags |= SYMBOL_DEF_USER;
460                 sym_set_changed(sym);
461         }
462         /*
463          * setting a choice value also resets the new flag of the choice
464          * symbol and all other choice values.
465          */
466         if (sym_is_choice_value(sym) && val == yes) {
467                 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
468                 struct property *prop;
469                 struct expr *e;
470
471                 cs->def[S_DEF_USER].val = sym;
472                 cs->flags |= SYMBOL_DEF_USER;
473                 prop = sym_get_choice_prop(cs);
474                 for (e = prop->expr; e; e = e->left.expr) {
475                         if (e->right.sym->visible != no)
476                                 e->right.sym->flags |= SYMBOL_DEF_USER;
477                 }
478         }
479
480         sym->def[S_DEF_USER].tri = val;
481         if (oldval != val)
482                 sym_clear_all_valid();
483
484         return true;
485 }
486
487 tristate sym_toggle_tristate_value(struct symbol *sym)
488 {
489         tristate oldval, newval;
490
491         oldval = newval = sym_get_tristate_value(sym);
492         do {
493                 switch (newval) {
494                 case no:
495                         newval = mod;
496                         break;
497                 case mod:
498                         newval = yes;
499                         break;
500                 case yes:
501                         newval = no;
502                         break;
503                 }
504                 if (sym_set_tristate_value(sym, newval))
505                         break;
506         } while (oldval != newval);
507         return newval;
508 }
509
510 bool sym_string_valid(struct symbol *sym, const char *str)
511 {
512         signed char ch;
513
514         switch (sym->type) {
515         case S_STRING:
516                 return true;
517         case S_INT:
518                 ch = *str++;
519                 if (ch == '-')
520                         ch = *str++;
521                 if (!isdigit(ch))
522                         return false;
523                 if (ch == '0' && *str != 0)
524                         return false;
525                 while ((ch = *str++)) {
526                         if (!isdigit(ch))
527                                 return false;
528                 }
529                 return true;
530         case S_HEX:
531                 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
532                         str += 2;
533                 ch = *str++;
534                 do {
535                         if (!isxdigit(ch))
536                                 return false;
537                 } while ((ch = *str++));
538                 return true;
539         case S_BOOLEAN:
540         case S_TRISTATE:
541                 switch (str[0]) {
542                 case 'y': case 'Y':
543                 case 'm': case 'M':
544                 case 'n': case 'N':
545                         return true;
546                 }
547                 return false;
548         default:
549                 return false;
550         }
551 }
552
553 bool sym_string_within_range(struct symbol *sym, const char *str)
554 {
555         struct property *prop;
556         int val;
557
558         switch (sym->type) {
559         case S_STRING:
560                 return sym_string_valid(sym, str);
561         case S_INT:
562                 if (!sym_string_valid(sym, str))
563                         return false;
564                 prop = sym_get_range_prop(sym);
565                 if (!prop)
566                         return true;
567                 val = strtol(str, NULL, 10);
568                 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
569                        val <= sym_get_range_val(prop->expr->right.sym, 10);
570         case S_HEX:
571                 if (!sym_string_valid(sym, str))
572                         return false;
573                 prop = sym_get_range_prop(sym);
574                 if (!prop)
575                         return true;
576                 val = strtol(str, NULL, 16);
577                 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
578                        val <= sym_get_range_val(prop->expr->right.sym, 16);
579         case S_BOOLEAN:
580         case S_TRISTATE:
581                 switch (str[0]) {
582                 case 'y': case 'Y':
583                         return sym_tristate_within_range(sym, yes);
584                 case 'm': case 'M':
585                         return sym_tristate_within_range(sym, mod);
586                 case 'n': case 'N':
587                         return sym_tristate_within_range(sym, no);
588                 }
589                 return false;
590         default:
591                 return false;
592         }
593 }
594
595 bool sym_set_string_value(struct symbol *sym, const char *newval)
596 {
597         const char *oldval;
598         char *val;
599         int size;
600
601         switch (sym->type) {
602         case S_BOOLEAN:
603         case S_TRISTATE:
604                 switch (newval[0]) {
605                 case 'y': case 'Y':
606                         return sym_set_tristate_value(sym, yes);
607                 case 'm': case 'M':
608                         return sym_set_tristate_value(sym, mod);
609                 case 'n': case 'N':
610                         return sym_set_tristate_value(sym, no);
611                 }
612                 return false;
613         default:
614                 ;
615         }
616
617         if (!sym_string_within_range(sym, newval))
618                 return false;
619
620         if (!(sym->flags & SYMBOL_DEF_USER)) {
621                 sym->flags |= SYMBOL_DEF_USER;
622                 sym_set_changed(sym);
623         }
624
625         oldval = sym->def[S_DEF_USER].val;
626         size = strlen(newval) + 1;
627         if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
628                 size += 2;
629                 sym->def[S_DEF_USER].val = val = malloc(size);
630                 *val++ = '0';
631                 *val++ = 'x';
632         } else if (!oldval || strcmp(oldval, newval))
633                 sym->def[S_DEF_USER].val = val = malloc(size);
634         else
635                 return true;
636
637         strcpy(val, newval);
638         free((void *)oldval);
639         sym_clear_all_valid();
640
641         return true;
642 }
643
644 const char *sym_get_string_value(struct symbol *sym)
645 {
646         tristate val;
647
648         switch (sym->type) {
649         case S_BOOLEAN:
650         case S_TRISTATE:
651                 val = sym_get_tristate_value(sym);
652                 switch (val) {
653                 case no:
654                         return "n";
655                 case mod:
656                         return "m";
657                 case yes:
658                         return "y";
659                 }
660                 break;
661         default:
662                 ;
663         }
664         return (const char *)sym->curr.val;
665 }
666
667 bool sym_is_changable(struct symbol *sym)
668 {
669         return sym->visible > sym->rev_dep.tri;
670 }
671
672 static unsigned strhash(const char *s)
673 {
674         /* fnv32 hash */
675         unsigned hash = 2166136261U;
676         for (; *s; s++)
677                 hash = (hash ^ *s) * 0x01000193;
678         return hash;
679 }
680
681 struct symbol *sym_lookup(const char *name, int flags)
682 {
683         struct symbol *symbol;
684         char *new_name;
685         int hash;
686
687         if (name) {
688                 if (name[0] && !name[1]) {
689                         switch (name[0]) {
690                         case 'y': return &symbol_yes;
691                         case 'm': return &symbol_mod;
692                         case 'n': return &symbol_no;
693                         }
694                 }
695                 hash = strhash(name) % SYMBOL_HASHSIZE;
696
697                 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
698                         if (symbol->name &&
699                             !strcmp(symbol->name, name) &&
700                             (flags ? symbol->flags & flags
701                                    : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
702                                 return symbol;
703                 }
704                 new_name = strdup(name);
705         } else {
706                 new_name = NULL;
707                 hash = 0;
708         }
709
710         symbol = malloc(sizeof(*symbol));
711         memset(symbol, 0, sizeof(*symbol));
712         symbol->name = new_name;
713         symbol->type = S_UNKNOWN;
714         symbol->flags |= flags;
715
716         symbol->next = symbol_hash[hash];
717         symbol_hash[hash] = symbol;
718
719         return symbol;
720 }
721
722 struct symbol *sym_find(const char *name)
723 {
724         struct symbol *symbol = NULL;
725         int hash = 0;
726
727         if (!name)
728                 return NULL;
729
730         if (name[0] && !name[1]) {
731                 switch (name[0]) {
732                 case 'y': return &symbol_yes;
733                 case 'm': return &symbol_mod;
734                 case 'n': return &symbol_no;
735                 }
736         }
737         hash = strhash(name) % SYMBOL_HASHSIZE;
738
739         for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
740                 if (symbol->name &&
741                     !strcmp(symbol->name, name) &&
742                     !(symbol->flags & SYMBOL_CONST))
743                                 break;
744         }
745
746         return symbol;
747 }
748
749 struct symbol **sym_re_search(const char *pattern)
750 {
751         struct symbol *sym, **sym_arr = NULL;
752         int i, cnt, size;
753         regex_t re;
754
755         cnt = size = 0;
756         /* Skip if empty */
757         if (strlen(pattern) == 0)
758                 return NULL;
759         if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
760                 return NULL;
761
762         for_all_symbols(i, sym) {
763                 if (sym->flags & SYMBOL_CONST || !sym->name)
764                         continue;
765                 if (regexec(&re, sym->name, 0, NULL, 0))
766                         continue;
767                 if (cnt + 1 >= size) {
768                         void *tmp = sym_arr;
769                         size += 16;
770                         sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
771                         if (!sym_arr) {
772                                 free(tmp);
773                                 return NULL;
774                         }
775                 }
776                 sym_calc_value(sym);
777                 sym_arr[cnt++] = sym;
778         }
779         if (sym_arr)
780                 sym_arr[cnt] = NULL;
781         regfree(&re);
782
783         return sym_arr;
784 }
785
786
787 static struct symbol *sym_check_expr_deps(struct expr *e)
788 {
789         struct symbol *sym;
790
791         if (!e)
792                 return NULL;
793         switch (e->type) {
794         case E_OR:
795         case E_AND:
796                 sym = sym_check_expr_deps(e->left.expr);
797                 if (sym)
798                         return sym;
799                 return sym_check_expr_deps(e->right.expr);
800         case E_NOT:
801                 return sym_check_expr_deps(e->left.expr);
802         case E_EQUAL:
803         case E_UNEQUAL:
804                 sym = sym_check_deps(e->left.sym);
805                 if (sym)
806                         return sym;
807                 return sym_check_deps(e->right.sym);
808         case E_SYMBOL:
809                 return sym_check_deps(e->left.sym);
810         default:
811                 break;
812         }
813         printf("Oops! How to check %d?\n", e->type);
814         return NULL;
815 }
816
817 /* return NULL when dependencies are OK */
818 static struct symbol *sym_check_sym_deps(struct symbol *sym)
819 {
820         struct symbol *sym2;
821         struct property *prop;
822
823         sym2 = sym_check_expr_deps(sym->rev_dep.expr);
824         if (sym2)
825                 return sym2;
826
827         for (prop = sym->prop; prop; prop = prop->next) {
828                 if (prop->type == P_CHOICE || prop->type == P_SELECT)
829                         continue;
830                 sym2 = sym_check_expr_deps(prop->visible.expr);
831                 if (sym2)
832                         break;
833                 if (prop->type != P_DEFAULT || sym_is_choice(sym))
834                         continue;
835                 sym2 = sym_check_expr_deps(prop->expr);
836                 if (sym2)
837                         break;
838         }
839
840         return sym2;
841 }
842
843 static struct symbol *sym_check_choice_deps(struct symbol *choice)
844 {
845         struct symbol *sym, *sym2;
846         struct property *prop;
847         struct expr *e;
848
849         prop = sym_get_choice_prop(choice);
850         expr_list_for_each_sym(prop->expr, e, sym)
851                 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
852
853         choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
854         sym2 = sym_check_sym_deps(choice);
855         choice->flags &= ~SYMBOL_CHECK;
856         if (sym2)
857                 goto out;
858
859         expr_list_for_each_sym(prop->expr, e, sym) {
860                 sym2 = sym_check_sym_deps(sym);
861                 if (sym2) {
862                         fprintf(stderr, " -> %s", sym->name);
863                         break;
864                 }
865         }
866 out:
867         expr_list_for_each_sym(prop->expr, e, sym)
868                 sym->flags &= ~SYMBOL_CHECK;
869
870         if (sym2 && sym_is_choice_value(sym2) &&
871             prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
872                 sym2 = choice;
873
874         return sym2;
875 }
876
877 struct symbol *sym_check_deps(struct symbol *sym)
878 {
879         struct symbol *sym2;
880         struct property *prop;
881
882         if (sym->flags & SYMBOL_CHECK) {
883                 fprintf(stderr, "%s:%d:error: found recursive dependency: %s",
884                         sym->prop->file->name, sym->prop->lineno,
885                         sym->name ? sym->name : "<choice>");
886                 return sym;
887         }
888         if (sym->flags & SYMBOL_CHECKED)
889                 return NULL;
890
891         if (sym_is_choice_value(sym)) {
892                 /* for choice groups start the check with main choice symbol */
893                 prop = sym_get_choice_prop(sym);
894                 sym2 = sym_check_deps(prop_get_symbol(prop));
895         } else if (sym_is_choice(sym)) {
896                 sym2 = sym_check_choice_deps(sym);
897         } else {
898                 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
899                 sym2 = sym_check_sym_deps(sym);
900                 sym->flags &= ~SYMBOL_CHECK;
901         }
902
903         if (sym2) {
904                 fprintf(stderr, " -> %s", sym->name ? sym->name : "<choice>");
905                 if (sym2 == sym) {
906                         fprintf(stderr, "\n");
907                         zconfnerrs++;
908                         sym2 = NULL;
909                 }
910         }
911
912         return sym2;
913 }
914
915 struct property *prop_alloc(enum prop_type type, struct symbol *sym)
916 {
917         struct property *prop;
918         struct property **propp;
919
920         prop = malloc(sizeof(*prop));
921         memset(prop, 0, sizeof(*prop));
922         prop->type = type;
923         prop->sym = sym;
924         prop->file = current_file;
925         prop->lineno = zconf_lineno();
926
927         /* append property to the prop list of symbol */
928         if (sym) {
929                 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
930                         ;
931                 *propp = prop;
932         }
933
934         return prop;
935 }
936
937 struct symbol *prop_get_symbol(struct property *prop)
938 {
939         if (prop->expr && (prop->expr->type == E_SYMBOL ||
940                            prop->expr->type == E_LIST))
941                 return prop->expr->left.sym;
942         return NULL;
943 }
944
945 const char *prop_get_type_name(enum prop_type type)
946 {
947         switch (type) {
948         case P_PROMPT:
949                 return "prompt";
950         case P_ENV:
951                 return "env";
952         case P_COMMENT:
953                 return "comment";
954         case P_MENU:
955                 return "menu";
956         case P_DEFAULT:
957                 return "default";
958         case P_CHOICE:
959                 return "choice";
960         case P_SELECT:
961                 return "select";
962         case P_RANGE:
963                 return "range";
964         case P_UNKNOWN:
965                 break;
966         }
967         return "unknown";
968 }
969
970 static void prop_add_env(const char *env)
971 {
972         struct symbol *sym, *sym2;
973         struct property *prop;
974         char *p;
975
976         sym = current_entry->sym;
977         sym->flags |= SYMBOL_AUTO;
978         for_all_properties(sym, prop, P_ENV) {
979                 sym2 = prop_get_symbol(prop);
980                 if (strcmp(sym2->name, env))
981                         menu_warn(current_entry, "redefining environment symbol from %s",
982                                   sym2->name);
983                 return;
984         }
985
986         prop = prop_alloc(P_ENV, sym);
987         prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
988
989         sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
990         sym_env_list->right.sym = sym;
991
992         p = getenv(env);
993         if (p)
994                 sym_add_default(sym, p);
995         else
996                 menu_warn(current_entry, "environment variable %s undefined", env);
997 }