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