blob: 7f8013dcef0020e5f8ebf856c0aec0ee3ce8912f [file] [log] [blame]
Masahiro Yamada0c874102018-12-18 21:13:35 +09001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Boris Kolpackov78cb0902020-11-23 11:38:18 +02006#include <sys/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <ctype.h>
8#include <stdlib.h>
9#include <string.h>
10#include <regex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "lkc.h"
13
14struct symbol symbol_yes = {
15 .name = "y",
16 .curr = { "y", yes },
Roman Zippelc0e150ac2006-06-08 22:12:40 -070017 .flags = SYMBOL_CONST|SYMBOL_VALID,
Masahiro Yamadad41809f2020-07-29 12:18:37 +090018};
19
20struct symbol symbol_mod = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 .name = "m",
22 .curr = { "m", mod },
Roman Zippelc0e150ac2006-06-08 22:12:40 -070023 .flags = SYMBOL_CONST|SYMBOL_VALID,
Masahiro Yamadad41809f2020-07-29 12:18:37 +090024};
25
26struct symbol symbol_no = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 .name = "n",
28 .curr = { "n", no },
Roman Zippelc0e150ac2006-06-08 22:12:40 -070029 .flags = SYMBOL_CONST|SYMBOL_VALID,
Masahiro Yamadad41809f2020-07-29 12:18:37 +090030};
31
32static struct symbol symbol_empty = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 .name = "",
34 .curr = { "", no },
35 .flags = SYMBOL_VALID,
36};
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct symbol *modules_sym;
Masahiro Yamadad41809f2020-07-29 12:18:37 +090039static tristate modules_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041enum symbol_type sym_get_type(struct symbol *sym)
42{
43 enum symbol_type type = sym->type;
44
45 if (type == S_TRISTATE) {
46 if (sym_is_choice_value(sym) && sym->visible == yes)
47 type = S_BOOLEAN;
48 else if (modules_val == no)
49 type = S_BOOLEAN;
50 }
51 return type;
52}
53
54const char *sym_type_name(enum symbol_type type)
55{
56 switch (type) {
57 case S_BOOLEAN:
Masahiro Yamadab92d8042017-12-16 00:38:02 +090058 return "bool";
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 case S_TRISTATE:
60 return "tristate";
61 case S_INT:
62 return "integer";
63 case S_HEX:
64 return "hex";
65 case S_STRING:
66 return "string";
67 case S_UNKNOWN:
68 return "unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70 return "???";
71}
72
73struct property *sym_get_choice_prop(struct symbol *sym)
74{
75 struct property *prop;
76
77 for_all_choices(sym, prop)
78 return prop;
79 return NULL;
80}
81
Michal Marekad8d40c2015-02-24 16:37:13 +010082static struct property *sym_get_default_prop(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 struct property *prop;
85
86 for_all_defaults(sym, prop) {
87 prop->visible.tri = expr_calc_value(prop->visible.expr);
88 if (prop->visible.tri != no)
89 return prop;
90 }
91 return NULL;
92}
93
Masahiro Yamada558e78e2018-12-21 17:33:04 +090094struct property *sym_get_range_prop(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct property *prop;
97
98 for_all_properties(sym, prop, P_RANGE) {
99 prop->visible.tri = expr_calc_value(prop->visible.expr);
100 if (prop->visible.tri != no)
101 return prop;
102 }
103 return NULL;
104}
105
Kees Cook129784a2013-07-18 11:32:01 -0700106static long long sym_get_range_val(struct symbol *sym, int base)
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800107{
108 sym_calc_value(sym);
109 switch (sym->type) {
110 case S_INT:
111 base = 10;
112 break;
113 case S_HEX:
114 base = 16;
115 break;
116 default:
117 break;
118 }
Kees Cook129784a2013-07-18 11:32:01 -0700119 return strtoll(sym->curr.val, NULL, base);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800120}
121
122static void sym_validate_range(struct symbol *sym)
123{
124 struct property *prop;
Masahiro Yamada94e5ed12023-11-15 13:16:53 +0900125 struct symbol *range_sym;
Kees Cook129784a2013-07-18 11:32:01 -0700126 int base;
127 long long val, val2;
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800128
129 switch (sym->type) {
130 case S_INT:
131 base = 10;
132 break;
133 case S_HEX:
134 base = 16;
135 break;
136 default:
137 return;
138 }
139 prop = sym_get_range_prop(sym);
140 if (!prop)
141 return;
Kees Cook129784a2013-07-18 11:32:01 -0700142 val = strtoll(sym->curr.val, NULL, base);
Masahiro Yamada94e5ed12023-11-15 13:16:53 +0900143 range_sym = prop->expr->left.sym;
144 val2 = sym_get_range_val(range_sym, base);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800145 if (val >= val2) {
Masahiro Yamada94e5ed12023-11-15 13:16:53 +0900146 range_sym = prop->expr->right.sym;
147 val2 = sym_get_range_val(range_sym, base);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800148 if (val <= val2)
149 return;
150 }
Masahiro Yamada94e5ed12023-11-15 13:16:53 +0900151 sym->curr.val = range_sym->curr.val;
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800152}
153
Michal Marekad8d40c2015-02-24 16:37:13 +0100154static void sym_set_changed(struct symbol *sym)
155{
156 struct property *prop;
157
158 sym->flags |= SYMBOL_CHANGED;
159 for (prop = sym->prop; prop; prop = prop->next) {
160 if (prop->menu)
161 prop->menu->flags |= MENU_CHANGED;
162 }
163}
164
165static void sym_set_all_changed(void)
166{
167 struct symbol *sym;
168 int i;
169
170 for_all_symbols(i, sym)
171 sym_set_changed(sym);
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static void sym_calc_visibility(struct symbol *sym)
175{
176 struct property *prop;
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200177 struct symbol *choice_sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 tristate tri;
179
180 /* any prompt visible? */
181 tri = no;
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200182
183 if (sym_is_choice_value(sym))
184 choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 for_all_prompts(sym, prop) {
187 prop->visible.tri = expr_calc_value(prop->visible.expr);
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200188 /*
189 * Tristate choice_values with visibility 'mod' are
190 * not visible if the corresponding choice's value is
191 * 'yes'.
192 */
193 if (choice_sym && sym->type == S_TRISTATE &&
194 prop->visible.tri == mod && choice_sym->curr.tri == yes)
195 prop->visible.tri = no;
196
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100197 tri = EXPR_OR(tri, prop->visible.tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
200 tri = yes;
201 if (sym->visible != tri) {
202 sym->visible = tri;
203 sym_set_changed(sym);
204 }
205 if (sym_is_choice_value(sym))
206 return;
Catalin Marinas246cf9c2010-06-08 17:25:57 +0100207 /* defaulting to "yes" if no explicit "depends on" are given */
208 tri = yes;
209 if (sym->dir_dep.expr)
210 tri = expr_calc_value(sym->dir_dep.expr);
Masahiro Yamadaf622f822018-03-13 18:56:07 +0900211 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
Catalin Marinas246cf9c2010-06-08 17:25:57 +0100212 tri = yes;
213 if (sym->dir_dep.tri != tri) {
214 sym->dir_dep.tri = tri;
215 sym_set_changed(sym);
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 tri = no;
218 if (sym->rev_dep.expr)
219 tri = expr_calc_value(sym->rev_dep.expr);
220 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
221 tri = yes;
222 if (sym->rev_dep.tri != tri) {
223 sym->rev_dep.tri = tri;
224 sym_set_changed(sym);
225 }
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500226 tri = no;
Masahiro Yamada3a9dd3e2020-03-02 15:23:40 +0900227 if (sym->implied.expr)
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500228 tri = expr_calc_value(sym->implied.expr);
229 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
230 tri = yes;
231 if (sym->implied.tri != tri) {
232 sym->implied.tri = tri;
233 sym_set_changed(sym);
234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Sam Ravnborgc2521472010-07-31 23:35:32 +0200237/*
238 * Find the default symbol for a choice.
239 * First try the default values for the choice symbol
240 * Next locate the first visible choice value
241 * Return NULL if none was found
242 */
243struct symbol *sym_choice_default(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct symbol *def_sym;
246 struct property *prop;
247 struct expr *e;
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* any of the defaults visible? */
250 for_all_defaults(sym, prop) {
251 prop->visible.tri = expr_calc_value(prop->visible.expr);
252 if (prop->visible.tri == no)
253 continue;
254 def_sym = prop_get_symbol(prop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (def_sym->visible != no)
256 return def_sym;
257 }
258
259 /* just get the first visible value */
260 prop = sym_get_choice_prop(sym);
Jan Beulich0a28c472010-06-30 13:11:01 +0100261 expr_list_for_each_sym(prop->expr, e, def_sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (def_sym->visible != no)
263 return def_sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Sam Ravnborgc2521472010-07-31 23:35:32 +0200265 /* failed to locate any defaults */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return NULL;
267}
268
Sam Ravnborgc2521472010-07-31 23:35:32 +0200269static struct symbol *sym_calc_choice(struct symbol *sym)
270{
271 struct symbol *def_sym;
272 struct property *prop;
273 struct expr *e;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500274 int flags;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200275
276 /* first calculate all choice values' visibilities */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500277 flags = sym->flags;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200278 prop = sym_get_choice_prop(sym);
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500279 expr_list_for_each_sym(prop->expr, e, def_sym) {
Sam Ravnborgc2521472010-07-31 23:35:32 +0200280 sym_calc_visibility(def_sym);
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500281 if (def_sym->visible != no)
282 flags &= def_sym->flags;
283 }
284
285 sym->flags &= flags | ~SYMBOL_DEF_USER;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200286
287 /* is the user choice visible? */
288 def_sym = sym->def[S_DEF_USER].val;
289 if (def_sym && def_sym->visible != no)
290 return def_sym;
291
292 def_sym = sym_choice_default(sym);
293
294 if (def_sym == NULL)
295 /* no choice? reset tristate value */
296 sym->curr.tri = no;
297
298 return def_sym;
299}
300
Masahiro Yamadaf8f69dc2018-03-13 18:56:08 +0900301static void sym_warn_unmet_dep(struct symbol *sym)
302{
303 struct gstr gs = str_new();
304
305 str_printf(&gs,
306 "\nWARNING: unmet direct dependencies detected for %s\n",
307 sym->name);
308 str_printf(&gs,
309 " Depends on [%c]: ",
310 sym->dir_dep.tri == mod ? 'm' : 'n');
311 expr_gstr_print(sym->dir_dep.expr, &gs);
312 str_printf(&gs, "\n");
313
314 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes,
315 " Selected by [y]:\n");
316 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod,
317 " Selected by [m]:\n");
318
319 fputs(str_get(&gs), stderr);
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322void sym_calc_value(struct symbol *sym)
323{
324 struct symbol_value newval, oldval;
325 struct property *prop;
326 struct expr *e;
327
328 if (!sym)
329 return;
330
331 if (sym->flags & SYMBOL_VALID)
332 return;
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -0700333
334 if (sym_is_choice_value(sym) &&
335 sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
336 sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
337 prop = sym_get_choice_prop(sym);
338 sym_calc_value(prop_get_symbol(prop));
339 }
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 sym->flags |= SYMBOL_VALID;
342
343 oldval = sym->curr;
344
345 switch (sym->type) {
346 case S_INT:
347 case S_HEX:
348 case S_STRING:
349 newval = symbol_empty.curr;
350 break;
351 case S_BOOLEAN:
352 case S_TRISTATE:
353 newval = symbol_no.curr;
354 break;
355 default:
356 sym->curr.val = sym->name;
357 sym->curr.tri = no;
358 return;
359 }
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900360 sym->flags &= ~SYMBOL_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 sym_calc_visibility(sym);
363
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900364 if (sym->visible != no)
365 sym->flags |= SYMBOL_WRITE;
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /* set default if recursively called */
368 sym->curr = newval;
369
370 switch (sym_get_type(sym)) {
371 case S_BOOLEAN:
372 case S_TRISTATE:
373 if (sym_is_choice_value(sym) && sym->visible == yes) {
374 prop = sym_get_choice_prop(sym);
375 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
Roman Zippel587c9062008-02-11 21:13:47 +0100376 } else {
377 if (sym->visible != no) {
378 /* if the symbol is visible use the user value
379 * if available, otherwise try the default value
380 */
Roman Zippel587c9062008-02-11 21:13:47 +0100381 if (sym_has_value(sym)) {
382 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
383 sym->visible);
384 goto calc_newval;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Roman Zippel587c9062008-02-11 21:13:47 +0100387 if (sym->rev_dep.tri != no)
388 sym->flags |= SYMBOL_WRITE;
389 if (!sym_is_choice(sym)) {
390 prop = sym_get_default_prop(sym);
391 if (prop) {
Roman Zippel587c9062008-02-11 21:13:47 +0100392 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
393 prop->visible.tri);
Ulf Magnussonf467c562018-02-23 12:49:01 +0100394 if (newval.tri != no)
395 sym->flags |= SYMBOL_WRITE;
Roman Zippel587c9062008-02-11 21:13:47 +0100396 }
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500397 if (sym->implied.tri != no) {
398 sym->flags |= SYMBOL_WRITE;
399 newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
Masahiro Yamada3a9dd3e2020-03-02 15:23:40 +0900400 newval.tri = EXPR_AND(newval.tri,
401 sym->dir_dep.tri);
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500402 }
Roman Zippel587c9062008-02-11 21:13:47 +0100403 }
404 calc_newval:
Masahiro Yamadaf8f69dc2018-03-13 18:56:08 +0900405 if (sym->dir_dep.tri < sym->rev_dep.tri)
406 sym_warn_unmet_dep(sym);
Roman Zippel587c9062008-02-11 21:13:47 +0100407 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Masahiro Yamadadef2fbf2020-03-02 15:23:39 +0900409 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 newval.tri = yes;
411 break;
412 case S_STRING:
413 case S_HEX:
414 case S_INT:
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900415 if (sym->visible != no && sym_has_value(sym)) {
416 newval.val = sym->def[S_DEF_USER].val;
417 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 prop = sym_get_default_prop(sym);
420 if (prop) {
421 struct symbol *ds = prop_get_symbol(prop);
422 if (ds) {
423 sym->flags |= SYMBOL_WRITE;
424 sym_calc_value(ds);
425 newval.val = ds->curr.val;
426 }
427 }
428 break;
429 default:
430 ;
431 }
432
433 sym->curr = newval;
434 if (sym_is_choice(sym) && newval.tri == yes)
435 sym->curr.val = sym_calc_choice(sym);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800436 sym_validate_range(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Roman Zippelface4372006-06-08 22:12:45 -0700438 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 sym_set_changed(sym);
Roman Zippelface4372006-06-08 22:12:45 -0700440 if (modules_sym == sym) {
441 sym_set_all_changed();
442 modules_val = modules_sym->curr.tri;
443 }
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (sym_is_choice(sym)) {
Roman Zippel7a962922008-01-14 04:50:23 +0100447 struct symbol *choice_sym;
Roman Zippel7a962922008-01-14 04:50:23 +0100448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 prop = sym_get_choice_prop(sym);
Roman Zippel7a962922008-01-14 04:50:23 +0100450 expr_list_for_each_sym(prop->expr, e, choice_sym) {
Jan Beulich0a28c472010-06-30 13:11:01 +0100451 if ((sym->flags & SYMBOL_WRITE) &&
452 choice_sym->visible != no)
453 choice_sym->flags |= SYMBOL_WRITE;
454 if (sym->flags & SYMBOL_CHANGED)
Roman Zippel7a962922008-01-14 04:50:23 +0100455 sym_set_changed(choice_sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 }
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100458
Dirk Gouders693359f2018-07-03 14:43:31 +0200459 if (sym->flags & SYMBOL_NO_WRITE)
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100460 sym->flags &= ~SYMBOL_WRITE;
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -0700461
462 if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
463 set_all_choice_values(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466void sym_clear_all_valid(void)
467{
468 struct symbol *sym;
469 int i;
470
471 for_all_symbols(i, sym)
472 sym->flags &= ~SYMBOL_VALID;
Masahiro Yamada5ee54652021-04-10 15:57:22 +0900473 conf_set_changed(true);
Markus Elfring35ffd082015-07-07 21:48:23 +0200474 sym_calc_value(modules_sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477bool sym_tristate_within_range(struct symbol *sym, tristate val)
478{
479 int type = sym_get_type(sym);
480
481 if (sym->visible == no)
482 return false;
483
484 if (type != S_BOOLEAN && type != S_TRISTATE)
485 return false;
486
487 if (type == S_BOOLEAN && val == mod)
488 return false;
489 if (sym->visible <= sym->rev_dep.tri)
490 return false;
491 if (sym_is_choice_value(sym) && sym->visible == yes)
492 return val == yes;
493 return val >= sym->rev_dep.tri && val <= sym->visible;
494}
495
496bool sym_set_tristate_value(struct symbol *sym, tristate val)
497{
498 tristate oldval = sym_get_tristate_value(sym);
499
500 if (oldval != val && !sym_tristate_within_range(sym, val))
501 return false;
502
Roman Zippel669bfad92006-06-08 22:12:42 -0700503 if (!(sym->flags & SYMBOL_DEF_USER)) {
504 sym->flags |= SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 sym_set_changed(sym);
506 }
Roman Zippel3f23ca22005-11-08 21:34:48 -0800507 /*
508 * setting a choice value also resets the new flag of the choice
509 * symbol and all other choice values.
510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (sym_is_choice_value(sym) && val == yes) {
512 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel3f23ca22005-11-08 21:34:48 -0800513 struct property *prop;
514 struct expr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Roman Zippel0c1822e2006-06-08 22:12:41 -0700516 cs->def[S_DEF_USER].val = sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700517 cs->flags |= SYMBOL_DEF_USER;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800518 prop = sym_get_choice_prop(cs);
519 for (e = prop->expr; e; e = e->left.expr) {
520 if (e->right.sym->visible != no)
Roman Zippel669bfad92006-06-08 22:12:42 -0700521 e->right.sym->flags |= SYMBOL_DEF_USER;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Roman Zippel0c1822e2006-06-08 22:12:41 -0700525 sym->def[S_DEF_USER].tri = val;
Roman Zippelface4372006-06-08 22:12:45 -0700526 if (oldval != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 sym_clear_all_valid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 return true;
530}
531
532tristate sym_toggle_tristate_value(struct symbol *sym)
533{
534 tristate oldval, newval;
535
536 oldval = newval = sym_get_tristate_value(sym);
537 do {
538 switch (newval) {
539 case no:
540 newval = mod;
541 break;
542 case mod:
543 newval = yes;
544 break;
545 case yes:
546 newval = no;
547 break;
548 }
549 if (sym_set_tristate_value(sym, newval))
550 break;
551 } while (oldval != newval);
552 return newval;
553}
554
555bool sym_string_valid(struct symbol *sym, const char *str)
556{
557 signed char ch;
558
559 switch (sym->type) {
560 case S_STRING:
561 return true;
562 case S_INT:
563 ch = *str++;
564 if (ch == '-')
565 ch = *str++;
566 if (!isdigit(ch))
567 return false;
568 if (ch == '0' && *str != 0)
569 return false;
570 while ((ch = *str++)) {
571 if (!isdigit(ch))
572 return false;
573 }
574 return true;
575 case S_HEX:
576 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
577 str += 2;
578 ch = *str++;
579 do {
580 if (!isxdigit(ch))
581 return false;
582 } while ((ch = *str++));
583 return true;
584 case S_BOOLEAN:
585 case S_TRISTATE:
586 switch (str[0]) {
587 case 'y': case 'Y':
588 case 'm': case 'M':
589 case 'n': case 'N':
590 return true;
591 }
592 return false;
593 default:
594 return false;
595 }
596}
597
598bool sym_string_within_range(struct symbol *sym, const char *str)
599{
600 struct property *prop;
Kees Cook129784a2013-07-18 11:32:01 -0700601 long long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 switch (sym->type) {
604 case S_STRING:
605 return sym_string_valid(sym, str);
606 case S_INT:
607 if (!sym_string_valid(sym, str))
608 return false;
609 prop = sym_get_range_prop(sym);
610 if (!prop)
611 return true;
Kees Cook129784a2013-07-18 11:32:01 -0700612 val = strtoll(str, NULL, 10);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800613 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
614 val <= sym_get_range_val(prop->expr->right.sym, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 case S_HEX:
616 if (!sym_string_valid(sym, str))
617 return false;
618 prop = sym_get_range_prop(sym);
619 if (!prop)
620 return true;
Kees Cook129784a2013-07-18 11:32:01 -0700621 val = strtoll(str, NULL, 16);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800622 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
623 val <= sym_get_range_val(prop->expr->right.sym, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 case S_BOOLEAN:
625 case S_TRISTATE:
626 switch (str[0]) {
627 case 'y': case 'Y':
628 return sym_tristate_within_range(sym, yes);
629 case 'm': case 'M':
630 return sym_tristate_within_range(sym, mod);
631 case 'n': case 'N':
632 return sym_tristate_within_range(sym, no);
633 }
634 return false;
635 default:
636 return false;
637 }
638}
639
640bool sym_set_string_value(struct symbol *sym, const char *newval)
641{
642 const char *oldval;
643 char *val;
644 int size;
645
646 switch (sym->type) {
647 case S_BOOLEAN:
648 case S_TRISTATE:
649 switch (newval[0]) {
650 case 'y': case 'Y':
651 return sym_set_tristate_value(sym, yes);
652 case 'm': case 'M':
653 return sym_set_tristate_value(sym, mod);
654 case 'n': case 'N':
655 return sym_set_tristate_value(sym, no);
656 }
657 return false;
658 default:
659 ;
660 }
661
662 if (!sym_string_within_range(sym, newval))
663 return false;
664
Roman Zippel669bfad92006-06-08 22:12:42 -0700665 if (!(sym->flags & SYMBOL_DEF_USER)) {
666 sym->flags |= SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 sym_set_changed(sym);
668 }
669
Roman Zippel0c1822e2006-06-08 22:12:41 -0700670 oldval = sym->def[S_DEF_USER].val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 size = strlen(newval) + 1;
672 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
673 size += 2;
Alan Cox177acf72012-11-06 14:32:08 +0000674 sym->def[S_DEF_USER].val = val = xmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *val++ = '0';
676 *val++ = 'x';
677 } else if (!oldval || strcmp(oldval, newval))
Alan Cox177acf72012-11-06 14:32:08 +0000678 sym->def[S_DEF_USER].val = val = xmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 else
680 return true;
681
682 strcpy(val, newval);
683 free((void *)oldval);
684 sym_clear_all_valid();
685
686 return true;
687}
688
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200689/*
690 * Find the default value associated to a symbol.
691 * For tristate symbol handle the modules=n case
692 * in which case "m" becomes "y".
693 * If the symbol does not have any default then fallback
694 * to the fixed default values.
695 */
696const char *sym_get_string_default(struct symbol *sym)
697{
698 struct property *prop;
699 struct symbol *ds;
700 const char *str;
701 tristate val;
702
703 sym_calc_visibility(sym);
704 sym_calc_value(modules_sym);
705 val = symbol_no.curr.tri;
706 str = symbol_empty.curr.val;
707
708 /* If symbol has a default value look it up */
709 prop = sym_get_default_prop(sym);
710 if (prop != NULL) {
711 switch (sym->type) {
712 case S_BOOLEAN:
713 case S_TRISTATE:
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -0500714 /* The visibility may limit the value from yes => mod */
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200715 val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
716 break;
717 default:
718 /*
719 * The following fails to handle the situation
720 * where a default value is further limited by
721 * the valid range.
722 */
723 ds = prop_get_symbol(prop);
724 if (ds != NULL) {
725 sym_calc_value(ds);
726 str = (const char *)ds->curr.val;
727 }
728 }
729 }
730
731 /* Handle select statements */
732 val = EXPR_OR(val, sym->rev_dep.tri);
733
734 /* transpose mod to yes if modules are not enabled */
735 if (val == mod)
736 if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
737 val = yes;
738
739 /* transpose mod to yes if type is bool */
740 if (sym->type == S_BOOLEAN && val == mod)
741 val = yes;
742
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500743 /* adjust the default value if this symbol is implied by another */
744 if (val < sym->implied.tri)
745 val = sym->implied.tri;
746
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200747 switch (sym->type) {
748 case S_BOOLEAN:
749 case S_TRISTATE:
750 switch (val) {
751 case no: return "n";
752 case mod: return "m";
753 case yes: return "y";
754 }
755 case S_INT:
756 case S_HEX:
757 return str;
758 case S_STRING:
759 return str;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200760 case S_UNKNOWN:
761 break;
762 }
763 return "";
764}
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766const char *sym_get_string_value(struct symbol *sym)
767{
768 tristate val;
769
770 switch (sym->type) {
771 case S_BOOLEAN:
772 case S_TRISTATE:
773 val = sym_get_tristate_value(sym);
774 switch (val) {
775 case no:
776 return "n";
777 case mod:
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400778 sym_calc_value(modules_sym);
779 return (modules_sym->curr.tri == no) ? "n" : "m";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 case yes:
781 return "y";
782 }
783 break;
784 default:
785 ;
786 }
787 return (const char *)sym->curr.val;
788}
789
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200790bool sym_is_changeable(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 return sym->visible > sym->rev_dep.tri;
793}
794
Andi Kleene66f25d2010-01-13 17:02:44 +0100795static unsigned strhash(const char *s)
796{
797 /* fnv32 hash */
798 unsigned hash = 2166136261U;
799 for (; *s; s++)
800 hash = (hash ^ *s) * 0x01000193;
801 return hash;
802}
803
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100804struct symbol *sym_lookup(const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 struct symbol *symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 char *new_name;
Andi Kleene66f25d2010-01-13 17:02:44 +0100808 int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if (name) {
811 if (name[0] && !name[1]) {
812 switch (name[0]) {
813 case 'y': return &symbol_yes;
814 case 'm': return &symbol_mod;
815 case 'n': return &symbol_no;
816 }
817 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100818 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100821 if (symbol->name &&
822 !strcmp(symbol->name, name) &&
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100823 (flags ? symbol->flags & flags
824 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
825 return symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900827 new_name = xstrdup(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 } else {
829 new_name = NULL;
Andi Kleene66f25d2010-01-13 17:02:44 +0100830 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
Alan Cox177acf72012-11-06 14:32:08 +0000833 symbol = xmalloc(sizeof(*symbol));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 memset(symbol, 0, sizeof(*symbol));
835 symbol->name = new_name;
836 symbol->type = S_UNKNOWN;
Masahiro Yamadacfc6eea2020-04-14 00:33:20 +0900837 symbol->flags = flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 symbol->next = symbol_hash[hash];
840 symbol_hash[hash] = symbol;
841
842 return symbol;
843}
844
845struct symbol *sym_find(const char *name)
846{
847 struct symbol *symbol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 int hash = 0;
849
850 if (!name)
851 return NULL;
852
853 if (name[0] && !name[1]) {
854 switch (name[0]) {
855 case 'y': return &symbol_yes;
856 case 'm': return &symbol_mod;
857 case 'n': return &symbol_no;
858 }
859 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100860 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100863 if (symbol->name &&
864 !strcmp(symbol->name, name) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 !(symbol->flags & SYMBOL_CONST))
866 break;
867 }
868
869 return symbol;
870}
871
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400872const char *sym_escape_string_value(const char *in)
873{
874 const char *p;
875 size_t reslen;
876 char *res;
877 size_t l;
878
879 reslen = strlen(in) + strlen("\"\"") + 1;
880
881 p = in;
882 for (;;) {
883 l = strcspn(p, "\"\\");
884 p += l;
885
886 if (p[0] == '\0')
887 break;
888
889 reslen++;
890 p++;
891 }
892
Alan Cox177acf72012-11-06 14:32:08 +0000893 res = xmalloc(reslen);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400894 res[0] = '\0';
895
896 strcat(res, "\"");
897
898 p = in;
899 for (;;) {
900 l = strcspn(p, "\"\\");
901 strncat(res, p, l);
902 p += l;
903
904 if (p[0] == '\0')
905 break;
906
907 strcat(res, "\\");
908 strncat(res, p++, 1);
909 }
910
911 strcat(res, "\"");
912 return res;
913}
914
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200915struct sym_match {
916 struct symbol *sym;
917 off_t so, eo;
918};
919
920/* Compare matched symbols as thus:
921 * - first, symbols that match exactly
922 * - then, alphabetical sort
923 */
Yann E. MORIN803b3512013-07-16 20:28:51 +0200924static int sym_rel_comp(const void *sym1, const void *sym2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200925{
Yann E. MORIN508382a2013-07-16 20:39:42 +0200926 const struct sym_match *s1 = sym1;
927 const struct sym_match *s2 = sym2;
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200928 int exact1, exact2;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200929
930 /* Exact match:
931 * - if matched length on symbol s1 is the length of that symbol,
932 * then this symbol should come first;
933 * - if matched length on symbol s2 is the length of that symbol,
934 * then this symbol should come first.
935 * Note: since the search can be a regexp, both symbols may match
936 * exactly; if this is the case, we can't decide which comes first,
937 * and we fallback to sorting alphabetically.
938 */
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200939 exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
940 exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
941 if (exact1 && !exact2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200942 return -1;
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200943 if (!exact1 && exact2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200944 return 1;
945
946 /* As a fallback, sort symbols alphabetically */
947 return strcmp(s1->sym->name, s2->sym->name);
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950struct symbol **sym_re_search(const char *pattern)
951{
952 struct symbol *sym, **sym_arr = NULL;
Yann E. MORIN508382a2013-07-16 20:39:42 +0200953 struct sym_match *sym_match_arr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int i, cnt, size;
955 regex_t re;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200956 regmatch_t match[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 cnt = size = 0;
959 /* Skip if empty */
960 if (strlen(pattern) == 0)
961 return NULL;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200962 if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return NULL;
964
965 for_all_symbols(i, sym) {
966 if (sym->flags & SYMBOL_CONST || !sym->name)
967 continue;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200968 if (regexec(&re, sym->name, 1, match, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 continue;
Yann E. MORIN1407f97a2013-07-16 20:32:33 +0200970 if (cnt >= size) {
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200971 void *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 size += 16;
Yann E. MORIN508382a2013-07-16 20:39:42 +0200973 tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
Yann E. MORIN803b3512013-07-16 20:28:51 +0200974 if (!tmp)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200975 goto sym_re_search_free;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200976 sym_match_arr = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Li Zefanda6df872010-03-19 14:57:47 +0800978 sym_calc_value(sym);
Yann E. MORIN803b3512013-07-16 20:28:51 +0200979 /* As regexec returned 0, we know we have a match, so
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200980 * we can use match[0].rm_[se]o without further checks
981 */
Yann E. MORIN508382a2013-07-16 20:39:42 +0200982 sym_match_arr[cnt].so = match[0].rm_so;
983 sym_match_arr[cnt].eo = match[0].rm_eo;
984 sym_match_arr[cnt++].sym = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200986 if (sym_match_arr) {
Yann E. MORIN508382a2013-07-16 20:39:42 +0200987 qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
Heinrich Schuchardt88127da2017-11-08 22:09:59 +0100988 sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200989 if (!sym_arr)
990 goto sym_re_search_free;
991 for (i = 0; i < cnt; i++)
Yann E. MORIN508382a2013-07-16 20:39:42 +0200992 sym_arr[i] = sym_match_arr[i].sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 sym_arr[cnt] = NULL;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200994 }
995sym_re_search_free:
Yann E. MORIN508382a2013-07-16 20:39:42 +0200996 /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
997 free(sym_match_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 regfree(&re);
999
1000 return sym_arr;
1001}
1002
Roman Zippeld595cea2010-07-31 23:35:30 +02001003/*
1004 * When we check for recursive dependencies we use a stack to save
1005 * current state so we can print out relevant info to user.
1006 * The entries are located on the call stack so no need to free memory.
Martin Walch8d9dfe82013-10-03 17:28:14 +02001007 * Note insert() remove() must always match to properly clear the stack.
Roman Zippeld595cea2010-07-31 23:35:30 +02001008 */
1009static struct dep_stack {
1010 struct dep_stack *prev, *next;
1011 struct symbol *sym;
1012 struct property *prop;
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001013 struct expr **expr;
Roman Zippeld595cea2010-07-31 23:35:30 +02001014} *check_top;
1015
1016static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
1017{
1018 memset(stack, 0, sizeof(*stack));
1019 if (check_top)
1020 check_top->next = stack;
1021 stack->prev = check_top;
1022 stack->sym = sym;
1023 check_top = stack;
1024}
1025
1026static void dep_stack_remove(void)
1027{
1028 check_top = check_top->prev;
1029 if (check_top)
1030 check_top->next = NULL;
1031}
1032
1033/*
1034 * Called when we have detected a recursive dependency.
1035 * check_top point to the top of the stact so we use
1036 * the ->prev pointer to locate the bottom of the stack.
1037 */
1038static void sym_check_print_recursive(struct symbol *last_sym)
1039{
1040 struct dep_stack *stack;
1041 struct symbol *sym, *next_sym;
1042 struct menu *menu = NULL;
1043 struct property *prop;
1044 struct dep_stack cv_stack;
1045
1046 if (sym_is_choice_value(last_sym)) {
1047 dep_stack_insert(&cv_stack, last_sym);
1048 last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1049 }
1050
1051 for (stack = check_top; stack != NULL; stack = stack->prev)
1052 if (stack->sym == last_sym)
1053 break;
1054 if (!stack) {
1055 fprintf(stderr, "unexpected recursive dependency error\n");
1056 return;
1057 }
1058
1059 for (; stack; stack = stack->next) {
1060 sym = stack->sym;
1061 next_sym = stack->next ? stack->next->sym : last_sym;
1062 prop = stack->prop;
Sam Ravnborg3643f842010-08-14 14:40:00 +02001063 if (prop == NULL)
1064 prop = stack->sym->prop;
Roman Zippeld595cea2010-07-31 23:35:30 +02001065
1066 /* for choice values find the menu entry (used below) */
1067 if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1068 for (prop = sym->prop; prop; prop = prop->next) {
1069 menu = prop->menu;
1070 if (prop->menu)
1071 break;
1072 }
1073 }
1074 if (stack->sym == last_sym)
1075 fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1076 prop->file->name, prop->lineno);
Masahiro Yamadae3b03bf2017-12-16 00:28:42 +09001077
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001078 if (sym_is_choice(sym)) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001079 fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1080 menu->file->name, menu->lineno,
1081 sym->name ? sym->name : "<choice>",
1082 next_sym->name ? next_sym->name : "<choice>");
1083 } else if (sym_is_choice_value(sym)) {
1084 fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1085 menu->file->name, menu->lineno,
1086 sym->name ? sym->name : "<choice>",
1087 next_sym->name ? next_sym->name : "<choice>");
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001088 } else if (stack->expr == &sym->dir_dep.expr) {
1089 fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
Roman Zippeld595cea2010-07-31 23:35:30 +02001090 prop->file->name, prop->lineno,
1091 sym->name ? sym->name : "<choice>",
1092 next_sym->name ? next_sym->name : "<choice>");
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001093 } else if (stack->expr == &sym->rev_dep.expr) {
1094 fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1095 prop->file->name, prop->lineno,
1096 sym->name ? sym->name : "<choice>",
1097 next_sym->name ? next_sym->name : "<choice>");
1098 } else if (stack->expr == &sym->implied.expr) {
1099 fprintf(stderr, "%s:%d:\tsymbol %s is implied by %s\n",
1100 prop->file->name, prop->lineno,
1101 sym->name ? sym->name : "<choice>",
1102 next_sym->name ? next_sym->name : "<choice>");
1103 } else if (stack->expr) {
1104 fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1105 prop->file->name, prop->lineno,
1106 sym->name ? sym->name : "<choice>",
1107 prop_get_type_name(prop->type),
1108 next_sym->name ? next_sym->name : "<choice>");
1109 } else {
1110 fprintf(stderr, "%s:%d:\tsymbol %s %s is visible depending on %s\n",
1111 prop->file->name, prop->lineno,
1112 sym->name ? sym->name : "<choice>",
1113 prop_get_type_name(prop->type),
1114 next_sym->name ? next_sym->name : "<choice>");
Roman Zippeld595cea2010-07-31 23:35:30 +02001115 }
1116 }
1117
Masahiro Yamadae3b03bf2017-12-16 00:28:42 +09001118 fprintf(stderr,
Mauro Carvalho Chehabcd238ef2019-06-12 14:52:48 -03001119 "For a resolution refer to Documentation/kbuild/kconfig-language.rst\n"
Masahiro Yamadae3b03bf2017-12-16 00:28:42 +09001120 "subsection \"Kconfig recursive dependency limitations\"\n"
1121 "\n");
1122
Roman Zippeld595cea2010-07-31 23:35:30 +02001123 if (check_top == &cv_stack)
1124 dep_stack_remove();
1125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127static struct symbol *sym_check_expr_deps(struct expr *e)
1128{
1129 struct symbol *sym;
1130
1131 if (!e)
1132 return NULL;
1133 switch (e->type) {
1134 case E_OR:
1135 case E_AND:
1136 sym = sym_check_expr_deps(e->left.expr);
1137 if (sym)
1138 return sym;
1139 return sym_check_expr_deps(e->right.expr);
1140 case E_NOT:
1141 return sym_check_expr_deps(e->left.expr);
1142 case E_EQUAL:
Jan Beulich31847b62015-06-15 13:00:21 +01001143 case E_GEQ:
1144 case E_GTH:
1145 case E_LEQ:
1146 case E_LTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 case E_UNEQUAL:
1148 sym = sym_check_deps(e->left.sym);
1149 if (sym)
1150 return sym;
1151 return sym_check_deps(e->right.sym);
1152 case E_SYMBOL:
1153 return sym_check_deps(e->left.sym);
1154 default:
1155 break;
1156 }
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +09001157 fprintf(stderr, "Oops! How to check %d?\n", e->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return NULL;
1159}
1160
Sam Ravnborg5447d342007-05-06 09:20:10 +02001161/* return NULL when dependencies are OK */
Roman Zippel48981172008-02-29 05:10:24 +01001162static struct symbol *sym_check_sym_deps(struct symbol *sym)
1163{
1164 struct symbol *sym2;
1165 struct property *prop;
Roman Zippeld595cea2010-07-31 23:35:30 +02001166 struct dep_stack stack;
1167
1168 dep_stack_insert(&stack, sym);
Roman Zippel48981172008-02-29 05:10:24 +01001169
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001170 stack.expr = &sym->dir_dep.expr;
1171 sym2 = sym_check_expr_deps(sym->dir_dep.expr);
1172 if (sym2)
1173 goto out;
1174
1175 stack.expr = &sym->rev_dep.expr;
Roman Zippel48981172008-02-29 05:10:24 +01001176 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1177 if (sym2)
Roman Zippeld595cea2010-07-31 23:35:30 +02001178 goto out;
Roman Zippel48981172008-02-29 05:10:24 +01001179
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001180 stack.expr = &sym->implied.expr;
Masahiro Yamada5e8c5292018-08-15 14:59:44 +09001181 sym2 = sym_check_expr_deps(sym->implied.expr);
1182 if (sym2)
1183 goto out;
1184
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001185 stack.expr = NULL;
1186
Roman Zippel48981172008-02-29 05:10:24 +01001187 for (prop = sym->prop; prop; prop = prop->next) {
Masahiro Yamada5e8c5292018-08-15 14:59:44 +09001188 if (prop->type == P_CHOICE || prop->type == P_SELECT ||
1189 prop->type == P_IMPLY)
Roman Zippel48981172008-02-29 05:10:24 +01001190 continue;
Roman Zippeld595cea2010-07-31 23:35:30 +02001191 stack.prop = prop;
Roman Zippel48981172008-02-29 05:10:24 +01001192 sym2 = sym_check_expr_deps(prop->visible.expr);
1193 if (sym2)
1194 break;
1195 if (prop->type != P_DEFAULT || sym_is_choice(sym))
1196 continue;
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001197 stack.expr = &prop->expr;
Roman Zippel48981172008-02-29 05:10:24 +01001198 sym2 = sym_check_expr_deps(prop->expr);
1199 if (sym2)
1200 break;
Roman Zippeld595cea2010-07-31 23:35:30 +02001201 stack.expr = NULL;
Roman Zippel48981172008-02-29 05:10:24 +01001202 }
1203
Roman Zippeld595cea2010-07-31 23:35:30 +02001204out:
1205 dep_stack_remove();
1206
Roman Zippel48981172008-02-29 05:10:24 +01001207 return sym2;
1208}
1209
1210static struct symbol *sym_check_choice_deps(struct symbol *choice)
1211{
1212 struct symbol *sym, *sym2;
1213 struct property *prop;
1214 struct expr *e;
Roman Zippeld595cea2010-07-31 23:35:30 +02001215 struct dep_stack stack;
1216
1217 dep_stack_insert(&stack, choice);
Roman Zippel48981172008-02-29 05:10:24 +01001218
1219 prop = sym_get_choice_prop(choice);
1220 expr_list_for_each_sym(prop->expr, e, sym)
1221 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1222
1223 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1224 sym2 = sym_check_sym_deps(choice);
1225 choice->flags &= ~SYMBOL_CHECK;
1226 if (sym2)
1227 goto out;
1228
1229 expr_list_for_each_sym(prop->expr, e, sym) {
1230 sym2 = sym_check_sym_deps(sym);
Roman Zippeld595cea2010-07-31 23:35:30 +02001231 if (sym2)
Roman Zippel48981172008-02-29 05:10:24 +01001232 break;
Roman Zippel48981172008-02-29 05:10:24 +01001233 }
1234out:
1235 expr_list_for_each_sym(prop->expr, e, sym)
1236 sym->flags &= ~SYMBOL_CHECK;
1237
1238 if (sym2 && sym_is_choice_value(sym2) &&
1239 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1240 sym2 = choice;
1241
Roman Zippeld595cea2010-07-31 23:35:30 +02001242 dep_stack_remove();
1243
Roman Zippel48981172008-02-29 05:10:24 +01001244 return sym2;
1245}
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247struct symbol *sym_check_deps(struct symbol *sym)
1248{
1249 struct symbol *sym2;
1250 struct property *prop;
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (sym->flags & SYMBOL_CHECK) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001253 sym_check_print_recursive(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return sym;
1255 }
David Gibson3f04e7d2005-11-08 21:34:46 -08001256 if (sym->flags & SYMBOL_CHECKED)
1257 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Roman Zippel48981172008-02-29 05:10:24 +01001259 if (sym_is_choice_value(sym)) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001260 struct dep_stack stack;
1261
Roman Zippel48981172008-02-29 05:10:24 +01001262 /* for choice groups start the check with main choice symbol */
Roman Zippeld595cea2010-07-31 23:35:30 +02001263 dep_stack_insert(&stack, sym);
Roman Zippel48981172008-02-29 05:10:24 +01001264 prop = sym_get_choice_prop(sym);
1265 sym2 = sym_check_deps(prop_get_symbol(prop));
Roman Zippeld595cea2010-07-31 23:35:30 +02001266 dep_stack_remove();
Roman Zippel48981172008-02-29 05:10:24 +01001267 } else if (sym_is_choice(sym)) {
1268 sym2 = sym_check_choice_deps(sym);
1269 } else {
1270 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1271 sym2 = sym_check_sym_deps(sym);
1272 sym->flags &= ~SYMBOL_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Roman Zippel48981172008-02-29 05:10:24 +01001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return sym2;
1276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278struct symbol *prop_get_symbol(struct property *prop)
1279{
1280 if (prop->expr && (prop->expr->type == E_SYMBOL ||
Roman Zippel7a962922008-01-14 04:50:23 +01001281 prop->expr->type == E_LIST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return prop->expr->left.sym;
1283 return NULL;
1284}
1285
1286const char *prop_get_type_name(enum prop_type type)
1287{
1288 switch (type) {
1289 case P_PROMPT:
1290 return "prompt";
1291 case P_COMMENT:
1292 return "comment";
1293 case P_MENU:
1294 return "menu";
1295 case P_DEFAULT:
1296 return "default";
1297 case P_CHOICE:
1298 return "choice";
1299 case P_SELECT:
1300 return "select";
Nicolas Pitre237e3ad2016-11-11 00:10:05 -05001301 case P_IMPLY:
1302 return "imply";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 case P_RANGE:
1304 return "range";
Sam Ravnborg59e89e32010-07-31 23:35:29 +02001305 case P_SYMBOL:
1306 return "symbol";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 case P_UNKNOWN:
1308 break;
1309 }
1310 return "unknown";
1311}