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