blob: 29bff43adc5e021d46f05727b3d3709d80720dde [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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_YES|SYMBOL_VALID,
19}, symbol_mod = {
20 .name = "m",
21 .curr = { "m", mod },
22 .flags = SYMBOL_MOD|SYMBOL_VALID,
23}, symbol_no = {
24 .name = "n",
25 .curr = { "n", no },
26 .flags = SYMBOL_NO|SYMBOL_VALID,
27}, symbol_empty = {
28 .name = "",
29 .curr = { "", no },
30 .flags = SYMBOL_VALID,
31};
32
33int sym_change_count;
34struct symbol *modules_sym;
35tristate modules_val;
36
37void sym_add_default(struct symbol *sym, const char *def)
38{
39 struct property *prop = prop_alloc(P_DEFAULT, sym);
40
41 prop->expr = expr_alloc_symbol(sym_lookup(def, 1));
42}
43
44void sym_init(void)
45{
46 struct symbol *sym;
47 struct utsname uts;
48 char *p;
49 static bool inited = false;
50
51 if (inited)
52 return;
53 inited = true;
54
55 uname(&uts);
56
57 sym = sym_lookup("ARCH", 0);
58 sym->type = S_STRING;
59 sym->flags |= SYMBOL_AUTO;
60 p = getenv("ARCH");
61 if (p)
62 sym_add_default(sym, p);
63
64 sym = sym_lookup("KERNELRELEASE", 0);
65 sym->type = S_STRING;
66 sym->flags |= SYMBOL_AUTO;
67 p = getenv("KERNELRELEASE");
68 if (p)
69 sym_add_default(sym, p);
70
71 sym = sym_lookup("UNAME_RELEASE", 0);
72 sym->type = S_STRING;
73 sym->flags |= SYMBOL_AUTO;
74 sym_add_default(sym, uts.release);
75}
76
77enum symbol_type sym_get_type(struct symbol *sym)
78{
79 enum symbol_type type = sym->type;
80
81 if (type == S_TRISTATE) {
82 if (sym_is_choice_value(sym) && sym->visible == yes)
83 type = S_BOOLEAN;
84 else if (modules_val == no)
85 type = S_BOOLEAN;
86 }
87 return type;
88}
89
90const char *sym_type_name(enum symbol_type type)
91{
92 switch (type) {
93 case S_BOOLEAN:
94 return "boolean";
95 case S_TRISTATE:
96 return "tristate";
97 case S_INT:
98 return "integer";
99 case S_HEX:
100 return "hex";
101 case S_STRING:
102 return "string";
103 case S_UNKNOWN:
104 return "unknown";
105 case S_OTHER:
106 break;
107 }
108 return "???";
109}
110
111struct property *sym_get_choice_prop(struct symbol *sym)
112{
113 struct property *prop;
114
115 for_all_choices(sym, prop)
116 return prop;
117 return NULL;
118}
119
120struct property *sym_get_default_prop(struct symbol *sym)
121{
122 struct property *prop;
123
124 for_all_defaults(sym, prop) {
125 prop->visible.tri = expr_calc_value(prop->visible.expr);
126 if (prop->visible.tri != no)
127 return prop;
128 }
129 return NULL;
130}
131
132struct property *sym_get_range_prop(struct symbol *sym)
133{
134 struct property *prop;
135
136 for_all_properties(sym, prop, P_RANGE) {
137 prop->visible.tri = expr_calc_value(prop->visible.expr);
138 if (prop->visible.tri != no)
139 return prop;
140 }
141 return NULL;
142}
143
144static void sym_calc_visibility(struct symbol *sym)
145{
146 struct property *prop;
147 tristate tri;
148
149 /* any prompt visible? */
150 tri = no;
151 for_all_prompts(sym, prop) {
152 prop->visible.tri = expr_calc_value(prop->visible.expr);
153 tri = E_OR(tri, prop->visible.tri);
154 }
155 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
156 tri = yes;
157 if (sym->visible != tri) {
158 sym->visible = tri;
159 sym_set_changed(sym);
160 }
161 if (sym_is_choice_value(sym))
162 return;
163 tri = no;
164 if (sym->rev_dep.expr)
165 tri = expr_calc_value(sym->rev_dep.expr);
166 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
167 tri = yes;
168 if (sym->rev_dep.tri != tri) {
169 sym->rev_dep.tri = tri;
170 sym_set_changed(sym);
171 }
172}
173
174static struct symbol *sym_calc_choice(struct symbol *sym)
175{
176 struct symbol *def_sym;
177 struct property *prop;
178 struct expr *e;
179
180 /* is the user choice visible? */
181 def_sym = sym->user.val;
182 if (def_sym) {
183 sym_calc_visibility(def_sym);
184 if (def_sym->visible != no)
185 return def_sym;
186 }
187
188 /* any of the defaults visible? */
189 for_all_defaults(sym, prop) {
190 prop->visible.tri = expr_calc_value(prop->visible.expr);
191 if (prop->visible.tri == no)
192 continue;
193 def_sym = prop_get_symbol(prop);
194 sym_calc_visibility(def_sym);
195 if (def_sym->visible != no)
196 return def_sym;
197 }
198
199 /* just get the first visible value */
200 prop = sym_get_choice_prop(sym);
201 for (e = prop->expr; e; e = e->left.expr) {
202 def_sym = e->right.sym;
203 sym_calc_visibility(def_sym);
204 if (def_sym->visible != no)
205 return def_sym;
206 }
207
208 /* no choice? reset tristate value */
209 sym->curr.tri = no;
210 return NULL;
211}
212
213void sym_calc_value(struct symbol *sym)
214{
215 struct symbol_value newval, oldval;
216 struct property *prop;
217 struct expr *e;
218
219 if (!sym)
220 return;
221
222 if (sym->flags & SYMBOL_VALID)
223 return;
224 sym->flags |= SYMBOL_VALID;
225
226 oldval = sym->curr;
227
228 switch (sym->type) {
229 case S_INT:
230 case S_HEX:
231 case S_STRING:
232 newval = symbol_empty.curr;
233 break;
234 case S_BOOLEAN:
235 case S_TRISTATE:
236 newval = symbol_no.curr;
237 break;
238 default:
239 sym->curr.val = sym->name;
240 sym->curr.tri = no;
241 return;
242 }
243 if (!sym_is_choice_value(sym))
244 sym->flags &= ~SYMBOL_WRITE;
245
246 sym_calc_visibility(sym);
247
248 /* set default if recursively called */
249 sym->curr = newval;
250
251 switch (sym_get_type(sym)) {
252 case S_BOOLEAN:
253 case S_TRISTATE:
254 if (sym_is_choice_value(sym) && sym->visible == yes) {
255 prop = sym_get_choice_prop(sym);
256 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
257 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
258 sym->flags |= SYMBOL_WRITE;
259 if (sym_has_value(sym))
260 newval.tri = sym->user.tri;
261 else if (!sym_is_choice(sym)) {
262 prop = sym_get_default_prop(sym);
263 if (prop)
264 newval.tri = expr_calc_value(prop->expr);
265 }
266 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
267 } else if (!sym_is_choice(sym)) {
268 prop = sym_get_default_prop(sym);
269 if (prop) {
270 sym->flags |= SYMBOL_WRITE;
271 newval.tri = expr_calc_value(prop->expr);
272 }
273 }
274 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
275 newval.tri = yes;
276 break;
277 case S_STRING:
278 case S_HEX:
279 case S_INT:
280 if (sym->visible != no) {
281 sym->flags |= SYMBOL_WRITE;
282 if (sym_has_value(sym)) {
283 newval.val = sym->user.val;
284 break;
285 }
286 }
287 prop = sym_get_default_prop(sym);
288 if (prop) {
289 struct symbol *ds = prop_get_symbol(prop);
290 if (ds) {
291 sym->flags |= SYMBOL_WRITE;
292 sym_calc_value(ds);
293 newval.val = ds->curr.val;
294 }
295 }
296 break;
297 default:
298 ;
299 }
300
301 sym->curr = newval;
302 if (sym_is_choice(sym) && newval.tri == yes)
303 sym->curr.val = sym_calc_choice(sym);
304
305 if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
306 sym_set_changed(sym);
307 if (modules_sym == sym)
308 modules_val = modules_sym->curr.tri;
309
310 if (sym_is_choice(sym)) {
311 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
312 prop = sym_get_choice_prop(sym);
313 for (e = prop->expr; e; e = e->left.expr) {
314 e->right.sym->flags |= flags;
315 if (flags & SYMBOL_CHANGED)
316 sym_set_changed(e->right.sym);
317 }
318 }
319}
320
321void sym_clear_all_valid(void)
322{
323 struct symbol *sym;
324 int i;
325
326 for_all_symbols(i, sym)
327 sym->flags &= ~SYMBOL_VALID;
328 sym_change_count++;
329 if (modules_sym)
330 sym_calc_value(modules_sym);
331}
332
333void sym_set_changed(struct symbol *sym)
334{
335 struct property *prop;
336
337 sym->flags |= SYMBOL_CHANGED;
338 for (prop = sym->prop; prop; prop = prop->next) {
339 if (prop->menu)
340 prop->menu->flags |= MENU_CHANGED;
341 }
342}
343
344void sym_set_all_changed(void)
345{
346 struct symbol *sym;
347 int i;
348
349 for_all_symbols(i, sym)
350 sym_set_changed(sym);
351}
352
353bool sym_tristate_within_range(struct symbol *sym, tristate val)
354{
355 int type = sym_get_type(sym);
356
357 if (sym->visible == no)
358 return false;
359
360 if (type != S_BOOLEAN && type != S_TRISTATE)
361 return false;
362
363 if (type == S_BOOLEAN && val == mod)
364 return false;
365 if (sym->visible <= sym->rev_dep.tri)
366 return false;
367 if (sym_is_choice_value(sym) && sym->visible == yes)
368 return val == yes;
369 return val >= sym->rev_dep.tri && val <= sym->visible;
370}
371
372bool sym_set_tristate_value(struct symbol *sym, tristate val)
373{
374 tristate oldval = sym_get_tristate_value(sym);
375
376 if (oldval != val && !sym_tristate_within_range(sym, val))
377 return false;
378
379 if (sym->flags & SYMBOL_NEW) {
380 sym->flags &= ~SYMBOL_NEW;
381 sym_set_changed(sym);
382 }
Roman Zippel3f23ca22005-11-08 21:34:48 -0800383 /*
384 * setting a choice value also resets the new flag of the choice
385 * symbol and all other choice values.
386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (sym_is_choice_value(sym) && val == yes) {
388 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel3f23ca22005-11-08 21:34:48 -0800389 struct property *prop;
390 struct expr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 cs->user.val = sym;
393 cs->flags &= ~SYMBOL_NEW;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800394 prop = sym_get_choice_prop(cs);
395 for (e = prop->expr; e; e = e->left.expr) {
396 if (e->right.sym->visible != no)
397 e->right.sym->flags &= ~SYMBOL_NEW;
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 sym->user.tri = val;
402 if (oldval != val) {
403 sym_clear_all_valid();
404 if (sym == modules_sym)
405 sym_set_all_changed();
406 }
407
408 return true;
409}
410
411tristate sym_toggle_tristate_value(struct symbol *sym)
412{
413 tristate oldval, newval;
414
415 oldval = newval = sym_get_tristate_value(sym);
416 do {
417 switch (newval) {
418 case no:
419 newval = mod;
420 break;
421 case mod:
422 newval = yes;
423 break;
424 case yes:
425 newval = no;
426 break;
427 }
428 if (sym_set_tristate_value(sym, newval))
429 break;
430 } while (oldval != newval);
431 return newval;
432}
433
434bool sym_string_valid(struct symbol *sym, const char *str)
435{
436 signed char ch;
437
438 switch (sym->type) {
439 case S_STRING:
440 return true;
441 case S_INT:
442 ch = *str++;
443 if (ch == '-')
444 ch = *str++;
445 if (!isdigit(ch))
446 return false;
447 if (ch == '0' && *str != 0)
448 return false;
449 while ((ch = *str++)) {
450 if (!isdigit(ch))
451 return false;
452 }
453 return true;
454 case S_HEX:
455 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
456 str += 2;
457 ch = *str++;
458 do {
459 if (!isxdigit(ch))
460 return false;
461 } while ((ch = *str++));
462 return true;
463 case S_BOOLEAN:
464 case S_TRISTATE:
465 switch (str[0]) {
466 case 'y': case 'Y':
467 case 'm': case 'M':
468 case 'n': case 'N':
469 return true;
470 }
471 return false;
472 default:
473 return false;
474 }
475}
476
477bool sym_string_within_range(struct symbol *sym, const char *str)
478{
479 struct property *prop;
480 int val;
481
482 switch (sym->type) {
483 case S_STRING:
484 return sym_string_valid(sym, str);
485 case S_INT:
486 if (!sym_string_valid(sym, str))
487 return false;
488 prop = sym_get_range_prop(sym);
489 if (!prop)
490 return true;
491 val = strtol(str, NULL, 10);
492 return val >= strtol(prop->expr->left.sym->name, NULL, 10) &&
493 val <= strtol(prop->expr->right.sym->name, NULL, 10);
494 case S_HEX:
495 if (!sym_string_valid(sym, str))
496 return false;
497 prop = sym_get_range_prop(sym);
498 if (!prop)
499 return true;
500 val = strtol(str, NULL, 16);
501 return val >= strtol(prop->expr->left.sym->name, NULL, 16) &&
502 val <= strtol(prop->expr->right.sym->name, NULL, 16);
503 case S_BOOLEAN:
504 case S_TRISTATE:
505 switch (str[0]) {
506 case 'y': case 'Y':
507 return sym_tristate_within_range(sym, yes);
508 case 'm': case 'M':
509 return sym_tristate_within_range(sym, mod);
510 case 'n': case 'N':
511 return sym_tristate_within_range(sym, no);
512 }
513 return false;
514 default:
515 return false;
516 }
517}
518
519bool sym_set_string_value(struct symbol *sym, const char *newval)
520{
521 const char *oldval;
522 char *val;
523 int size;
524
525 switch (sym->type) {
526 case S_BOOLEAN:
527 case S_TRISTATE:
528 switch (newval[0]) {
529 case 'y': case 'Y':
530 return sym_set_tristate_value(sym, yes);
531 case 'm': case 'M':
532 return sym_set_tristate_value(sym, mod);
533 case 'n': case 'N':
534 return sym_set_tristate_value(sym, no);
535 }
536 return false;
537 default:
538 ;
539 }
540
541 if (!sym_string_within_range(sym, newval))
542 return false;
543
544 if (sym->flags & SYMBOL_NEW) {
545 sym->flags &= ~SYMBOL_NEW;
546 sym_set_changed(sym);
547 }
548
549 oldval = sym->user.val;
550 size = strlen(newval) + 1;
551 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
552 size += 2;
553 sym->user.val = val = malloc(size);
554 *val++ = '0';
555 *val++ = 'x';
556 } else if (!oldval || strcmp(oldval, newval))
557 sym->user.val = val = malloc(size);
558 else
559 return true;
560
561 strcpy(val, newval);
562 free((void *)oldval);
563 sym_clear_all_valid();
564
565 return true;
566}
567
568const char *sym_get_string_value(struct symbol *sym)
569{
570 tristate val;
571
572 switch (sym->type) {
573 case S_BOOLEAN:
574 case S_TRISTATE:
575 val = sym_get_tristate_value(sym);
576 switch (val) {
577 case no:
578 return "n";
579 case mod:
580 return "m";
581 case yes:
582 return "y";
583 }
584 break;
585 default:
586 ;
587 }
588 return (const char *)sym->curr.val;
589}
590
591bool sym_is_changable(struct symbol *sym)
592{
593 return sym->visible > sym->rev_dep.tri;
594}
595
596struct symbol *sym_lookup(const char *name, int isconst)
597{
598 struct symbol *symbol;
599 const char *ptr;
600 char *new_name;
601 int hash = 0;
602
603 if (name) {
604 if (name[0] && !name[1]) {
605 switch (name[0]) {
606 case 'y': return &symbol_yes;
607 case 'm': return &symbol_mod;
608 case 'n': return &symbol_no;
609 }
610 }
611 for (ptr = name; *ptr; ptr++)
612 hash += *ptr;
613 hash &= 0xff;
614
615 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
616 if (!strcmp(symbol->name, name)) {
617 if ((isconst && symbol->flags & SYMBOL_CONST) ||
618 (!isconst && !(symbol->flags & SYMBOL_CONST)))
619 return symbol;
620 }
621 }
622 new_name = strdup(name);
623 } else {
624 new_name = NULL;
625 hash = 256;
626 }
627
628 symbol = malloc(sizeof(*symbol));
629 memset(symbol, 0, sizeof(*symbol));
630 symbol->name = new_name;
631 symbol->type = S_UNKNOWN;
632 symbol->flags = SYMBOL_NEW;
633 if (isconst)
634 symbol->flags |= SYMBOL_CONST;
635
636 symbol->next = symbol_hash[hash];
637 symbol_hash[hash] = symbol;
638
639 return symbol;
640}
641
642struct symbol *sym_find(const char *name)
643{
644 struct symbol *symbol = NULL;
645 const char *ptr;
646 int hash = 0;
647
648 if (!name)
649 return NULL;
650
651 if (name[0] && !name[1]) {
652 switch (name[0]) {
653 case 'y': return &symbol_yes;
654 case 'm': return &symbol_mod;
655 case 'n': return &symbol_no;
656 }
657 }
658 for (ptr = name; *ptr; ptr++)
659 hash += *ptr;
660 hash &= 0xff;
661
662 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
663 if (!strcmp(symbol->name, name) &&
664 !(symbol->flags & SYMBOL_CONST))
665 break;
666 }
667
668 return symbol;
669}
670
671struct symbol **sym_re_search(const char *pattern)
672{
673 struct symbol *sym, **sym_arr = NULL;
674 int i, cnt, size;
675 regex_t re;
676
677 cnt = size = 0;
678 /* Skip if empty */
679 if (strlen(pattern) == 0)
680 return NULL;
681 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
682 return NULL;
683
684 for_all_symbols(i, sym) {
685 if (sym->flags & SYMBOL_CONST || !sym->name)
686 continue;
687 if (regexec(&re, sym->name, 0, NULL, 0))
688 continue;
689 if (cnt + 1 >= size) {
690 void *tmp = sym_arr;
691 size += 16;
692 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
693 if (!sym_arr) {
694 free(tmp);
695 return NULL;
696 }
697 }
698 sym_arr[cnt++] = sym;
699 }
700 if (sym_arr)
701 sym_arr[cnt] = NULL;
702 regfree(&re);
703
704 return sym_arr;
705}
706
707
708struct symbol *sym_check_deps(struct symbol *sym);
709
710static struct symbol *sym_check_expr_deps(struct expr *e)
711{
712 struct symbol *sym;
713
714 if (!e)
715 return NULL;
716 switch (e->type) {
717 case E_OR:
718 case E_AND:
719 sym = sym_check_expr_deps(e->left.expr);
720 if (sym)
721 return sym;
722 return sym_check_expr_deps(e->right.expr);
723 case E_NOT:
724 return sym_check_expr_deps(e->left.expr);
725 case E_EQUAL:
726 case E_UNEQUAL:
727 sym = sym_check_deps(e->left.sym);
728 if (sym)
729 return sym;
730 return sym_check_deps(e->right.sym);
731 case E_SYMBOL:
732 return sym_check_deps(e->left.sym);
733 default:
734 break;
735 }
736 printf("Oops! How to check %d?\n", e->type);
737 return NULL;
738}
739
740struct symbol *sym_check_deps(struct symbol *sym)
741{
742 struct symbol *sym2;
743 struct property *prop;
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (sym->flags & SYMBOL_CHECK) {
746 printf("Warning! Found recursive dependency: %s", sym->name);
747 return sym;
748 }
David Gibson3f04e7d2005-11-08 21:34:46 -0800749 if (sym->flags & SYMBOL_CHECKED)
750 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
753 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
754 if (sym2)
755 goto out;
756
757 for (prop = sym->prop; prop; prop = prop->next) {
758 if (prop->type == P_CHOICE || prop->type == P_SELECT)
759 continue;
760 sym2 = sym_check_expr_deps(prop->visible.expr);
761 if (sym2)
762 goto out;
763 if (prop->type != P_DEFAULT || sym_is_choice(sym))
764 continue;
765 sym2 = sym_check_expr_deps(prop->expr);
766 if (sym2)
767 goto out;
768 }
769out:
David Gibson3f04e7d2005-11-08 21:34:46 -0800770 if (sym2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 printf(" %s", sym->name);
David Gibson3f04e7d2005-11-08 21:34:46 -0800772 if (sym2 == sym) {
773 printf("\n");
774 sym2 = NULL;
775 }
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 sym->flags &= ~SYMBOL_CHECK;
778 return sym2;
779}
780
781struct property *prop_alloc(enum prop_type type, struct symbol *sym)
782{
783 struct property *prop;
784 struct property **propp;
785
786 prop = malloc(sizeof(*prop));
787 memset(prop, 0, sizeof(*prop));
788 prop->type = type;
789 prop->sym = sym;
790 prop->file = current_file;
791 prop->lineno = zconf_lineno();
792
793 /* append property to the prop list of symbol */
794 if (sym) {
795 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
796 ;
797 *propp = prop;
798 }
799
800 return prop;
801}
802
803struct symbol *prop_get_symbol(struct property *prop)
804{
805 if (prop->expr && (prop->expr->type == E_SYMBOL ||
806 prop->expr->type == E_CHOICE))
807 return prop->expr->left.sym;
808 return NULL;
809}
810
811const char *prop_get_type_name(enum prop_type type)
812{
813 switch (type) {
814 case P_PROMPT:
815 return "prompt";
816 case P_COMMENT:
817 return "comment";
818 case P_MENU:
819 return "menu";
820 case P_DEFAULT:
821 return "default";
822 case P_CHOICE:
823 return "choice";
824 case P_SELECT:
825 return "select";
826 case P_RANGE:
827 return "range";
828 case P_UNKNOWN:
829 break;
830 }
831 return "unknown";
832}