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