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