blob: b101ef3c377a6b98d76e8d93a3c5be1c45d8fb7f [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
6#include <ctype.h>
7#include <stdlib.h>
8#include <string.h>
9#include <regex.h>
10#include <sys/utsname.h>
11
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070018}, symbol_mod = {
19 .name = "m",
20 .curr = { "m", mod },
Roman Zippelc0e150ac2006-06-08 22:12:40 -070021 .flags = SYMBOL_CONST|SYMBOL_VALID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070022}, symbol_no = {
23 .name = "n",
24 .curr = { "n", no },
Roman Zippelc0e150ac2006-06-08 22:12:40 -070025 .flags = SYMBOL_CONST|SYMBOL_VALID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070026}, symbol_empty = {
27 .name = "",
28 .curr = { "", no },
29 .flags = SYMBOL_VALID,
30};
31
Roman Zippelface4372006-06-08 22:12:45 -070032struct symbol *sym_defconfig_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033struct symbol *modules_sym;
34tristate modules_val;
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036enum 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:
Masahiro Yamadab92d8042017-12-16 00:38:02 +090053 return "bool";
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 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";
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65 return "???";
66}
67
68struct 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
Michal Marekad8d40c2015-02-24 16:37:13 +010077static struct property *sym_get_default_prop(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
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
Masahiro Yamada558e78e2018-12-21 17:33:04 +090089struct property *sym_get_range_prop(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
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
Kees Cook129784a2013-07-18 11:32:01 -0700101static long long sym_get_range_val(struct symbol *sym, int base)
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800102{
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 }
Kees Cook129784a2013-07-18 11:32:01 -0700114 return strtoll(sym->curr.val, NULL, base);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800115}
116
117static void sym_validate_range(struct symbol *sym)
118{
119 struct property *prop;
Kees Cook129784a2013-07-18 11:32:01 -0700120 int base;
121 long long val, val2;
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800122 char str[64];
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;
Kees Cook129784a2013-07-18 11:32:01 -0700137 val = strtoll(sym->curr.val, NULL, base);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800138 val2 = sym_get_range_val(prop->expr->left.sym, base);
139 if (val >= val2) {
140 val2 = sym_get_range_val(prop->expr->right.sym, base);
141 if (val <= val2)
142 return;
143 }
144 if (sym->type == S_INT)
Kees Cook129784a2013-07-18 11:32:01 -0700145 sprintf(str, "%lld", val2);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800146 else
Kees Cook129784a2013-07-18 11:32:01 -0700147 sprintf(str, "0x%llx", val2);
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900148 sym->curr.val = xstrdup(str);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800149}
150
Michal Marekad8d40c2015-02-24 16:37:13 +0100151static void sym_set_changed(struct symbol *sym)
152{
153 struct property *prop;
154
155 sym->flags |= SYMBOL_CHANGED;
156 for (prop = sym->prop; prop; prop = prop->next) {
157 if (prop->menu)
158 prop->menu->flags |= MENU_CHANGED;
159 }
160}
161
162static void sym_set_all_changed(void)
163{
164 struct symbol *sym;
165 int i;
166
167 for_all_symbols(i, sym)
168 sym_set_changed(sym);
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static void sym_calc_visibility(struct symbol *sym)
172{
173 struct property *prop;
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200174 struct symbol *choice_sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 tristate tri;
176
177 /* any prompt visible? */
178 tri = no;
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200179
180 if (sym_is_choice_value(sym))
181 choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 for_all_prompts(sym, prop) {
184 prop->visible.tri = expr_calc_value(prop->visible.expr);
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200185 /*
186 * Tristate choice_values with visibility 'mod' are
187 * not visible if the corresponding choice's value is
188 * 'yes'.
189 */
190 if (choice_sym && sym->type == S_TRISTATE &&
191 prop->visible.tri == mod && choice_sym->curr.tri == yes)
192 prop->visible.tri = no;
193
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100194 tri = EXPR_OR(tri, prop->visible.tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
197 tri = yes;
198 if (sym->visible != tri) {
199 sym->visible = tri;
200 sym_set_changed(sym);
201 }
202 if (sym_is_choice_value(sym))
203 return;
Catalin Marinas246cf9c2010-06-08 17:25:57 +0100204 /* defaulting to "yes" if no explicit "depends on" are given */
205 tri = yes;
206 if (sym->dir_dep.expr)
207 tri = expr_calc_value(sym->dir_dep.expr);
Masahiro Yamadaf622f822018-03-13 18:56:07 +0900208 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
Catalin Marinas246cf9c2010-06-08 17:25:57 +0100209 tri = yes;
210 if (sym->dir_dep.tri != tri) {
211 sym->dir_dep.tri = tri;
212 sym_set_changed(sym);
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 tri = no;
215 if (sym->rev_dep.expr)
216 tri = expr_calc_value(sym->rev_dep.expr);
217 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
218 tri = yes;
219 if (sym->rev_dep.tri != tri) {
220 sym->rev_dep.tri = tri;
221 sym_set_changed(sym);
222 }
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500223 tri = no;
224 if (sym->implied.expr && sym->dir_dep.tri != no)
225 tri = expr_calc_value(sym->implied.expr);
226 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
227 tri = yes;
228 if (sym->implied.tri != tri) {
229 sym->implied.tri = tri;
230 sym_set_changed(sym);
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Sam Ravnborgc2521472010-07-31 23:35:32 +0200234/*
235 * Find the default symbol for a choice.
236 * First try the default values for the choice symbol
237 * Next locate the first visible choice value
238 * Return NULL if none was found
239 */
240struct symbol *sym_choice_default(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct symbol *def_sym;
243 struct property *prop;
244 struct expr *e;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* any of the defaults visible? */
247 for_all_defaults(sym, prop) {
248 prop->visible.tri = expr_calc_value(prop->visible.expr);
249 if (prop->visible.tri == no)
250 continue;
251 def_sym = prop_get_symbol(prop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (def_sym->visible != no)
253 return def_sym;
254 }
255
256 /* just get the first visible value */
257 prop = sym_get_choice_prop(sym);
Jan Beulich0a28c472010-06-30 13:11:01 +0100258 expr_list_for_each_sym(prop->expr, e, def_sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (def_sym->visible != no)
260 return def_sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Sam Ravnborgc2521472010-07-31 23:35:32 +0200262 /* failed to locate any defaults */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return NULL;
264}
265
Sam Ravnborgc2521472010-07-31 23:35:32 +0200266static struct symbol *sym_calc_choice(struct symbol *sym)
267{
268 struct symbol *def_sym;
269 struct property *prop;
270 struct expr *e;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500271 int flags;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200272
273 /* first calculate all choice values' visibilities */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500274 flags = sym->flags;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200275 prop = sym_get_choice_prop(sym);
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500276 expr_list_for_each_sym(prop->expr, e, def_sym) {
Sam Ravnborgc2521472010-07-31 23:35:32 +0200277 sym_calc_visibility(def_sym);
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500278 if (def_sym->visible != no)
279 flags &= def_sym->flags;
280 }
281
282 sym->flags &= flags | ~SYMBOL_DEF_USER;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200283
284 /* is the user choice visible? */
285 def_sym = sym->def[S_DEF_USER].val;
286 if (def_sym && def_sym->visible != no)
287 return def_sym;
288
289 def_sym = sym_choice_default(sym);
290
291 if (def_sym == NULL)
292 /* no choice? reset tristate value */
293 sym->curr.tri = no;
294
295 return def_sym;
296}
297
Masahiro Yamadaf8f69dc2018-03-13 18:56:08 +0900298static void sym_warn_unmet_dep(struct symbol *sym)
299{
300 struct gstr gs = str_new();
301
302 str_printf(&gs,
303 "\nWARNING: unmet direct dependencies detected for %s\n",
304 sym->name);
305 str_printf(&gs,
306 " Depends on [%c]: ",
307 sym->dir_dep.tri == mod ? 'm' : 'n');
308 expr_gstr_print(sym->dir_dep.expr, &gs);
309 str_printf(&gs, "\n");
310
311 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes,
312 " Selected by [y]:\n");
313 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod,
314 " Selected by [m]:\n");
315
316 fputs(str_get(&gs), stderr);
317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319void sym_calc_value(struct symbol *sym)
320{
321 struct symbol_value newval, oldval;
322 struct property *prop;
323 struct expr *e;
324
325 if (!sym)
326 return;
327
328 if (sym->flags & SYMBOL_VALID)
329 return;
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -0700330
331 if (sym_is_choice_value(sym) &&
332 sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
333 sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
334 prop = sym_get_choice_prop(sym);
335 sym_calc_value(prop_get_symbol(prop));
336 }
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 sym->flags |= SYMBOL_VALID;
339
340 oldval = sym->curr;
341
342 switch (sym->type) {
343 case S_INT:
344 case S_HEX:
345 case S_STRING:
346 newval = symbol_empty.curr;
347 break;
348 case S_BOOLEAN:
349 case S_TRISTATE:
350 newval = symbol_no.curr;
351 break;
352 default:
353 sym->curr.val = sym->name;
354 sym->curr.tri = no;
355 return;
356 }
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900357 sym->flags &= ~SYMBOL_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 sym_calc_visibility(sym);
360
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900361 if (sym->visible != no)
362 sym->flags |= SYMBOL_WRITE;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /* set default if recursively called */
365 sym->curr = newval;
366
367 switch (sym_get_type(sym)) {
368 case S_BOOLEAN:
369 case S_TRISTATE:
370 if (sym_is_choice_value(sym) && sym->visible == yes) {
371 prop = sym_get_choice_prop(sym);
372 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
Roman Zippel587c9062008-02-11 21:13:47 +0100373 } else {
374 if (sym->visible != no) {
375 /* if the symbol is visible use the user value
376 * if available, otherwise try the default value
377 */
Roman Zippel587c9062008-02-11 21:13:47 +0100378 if (sym_has_value(sym)) {
379 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
380 sym->visible);
381 goto calc_newval;
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Roman Zippel587c9062008-02-11 21:13:47 +0100384 if (sym->rev_dep.tri != no)
385 sym->flags |= SYMBOL_WRITE;
386 if (!sym_is_choice(sym)) {
387 prop = sym_get_default_prop(sym);
388 if (prop) {
Roman Zippel587c9062008-02-11 21:13:47 +0100389 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
390 prop->visible.tri);
Ulf Magnussonf467c562018-02-23 12:49:01 +0100391 if (newval.tri != no)
392 sym->flags |= SYMBOL_WRITE;
Roman Zippel587c9062008-02-11 21:13:47 +0100393 }
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500394 if (sym->implied.tri != no) {
395 sym->flags |= SYMBOL_WRITE;
396 newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
397 }
Roman Zippel587c9062008-02-11 21:13:47 +0100398 }
399 calc_newval:
Masahiro Yamadaf8f69dc2018-03-13 18:56:08 +0900400 if (sym->dir_dep.tri < sym->rev_dep.tri)
401 sym_warn_unmet_dep(sym);
Roman Zippel587c9062008-02-11 21:13:47 +0100402 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Masahiro Yamadadef2fbf2020-03-02 15:23:39 +0900404 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 newval.tri = yes;
406 break;
407 case S_STRING:
408 case S_HEX:
409 case S_INT:
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900410 if (sym->visible != no && sym_has_value(sym)) {
411 newval.val = sym->def[S_DEF_USER].val;
412 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 prop = sym_get_default_prop(sym);
415 if (prop) {
416 struct symbol *ds = prop_get_symbol(prop);
417 if (ds) {
418 sym->flags |= SYMBOL_WRITE;
419 sym_calc_value(ds);
420 newval.val = ds->curr.val;
421 }
422 }
423 break;
424 default:
425 ;
426 }
427
428 sym->curr = newval;
429 if (sym_is_choice(sym) && newval.tri == yes)
430 sym->curr.val = sym_calc_choice(sym);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800431 sym_validate_range(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Roman Zippelface4372006-06-08 22:12:45 -0700433 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 sym_set_changed(sym);
Roman Zippelface4372006-06-08 22:12:45 -0700435 if (modules_sym == sym) {
436 sym_set_all_changed();
437 modules_val = modules_sym->curr.tri;
438 }
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (sym_is_choice(sym)) {
Roman Zippel7a962922008-01-14 04:50:23 +0100442 struct symbol *choice_sym;
Roman Zippel7a962922008-01-14 04:50:23 +0100443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 prop = sym_get_choice_prop(sym);
Roman Zippel7a962922008-01-14 04:50:23 +0100445 expr_list_for_each_sym(prop->expr, e, choice_sym) {
Jan Beulich0a28c472010-06-30 13:11:01 +0100446 if ((sym->flags & SYMBOL_WRITE) &&
447 choice_sym->visible != no)
448 choice_sym->flags |= SYMBOL_WRITE;
449 if (sym->flags & SYMBOL_CHANGED)
Roman Zippel7a962922008-01-14 04:50:23 +0100450 sym_set_changed(choice_sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 }
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100453
Dirk Gouders693359f2018-07-03 14:43:31 +0200454 if (sym->flags & SYMBOL_NO_WRITE)
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100455 sym->flags &= ~SYMBOL_WRITE;
Arve Hjønnevågfbe98bb92013-06-06 20:37:00 -0700456
457 if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
458 set_all_choice_values(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461void sym_clear_all_valid(void)
462{
463 struct symbol *sym;
464 int i;
465
466 for_all_symbols(i, sym)
467 sym->flags &= ~SYMBOL_VALID;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800468 sym_add_change_count(1);
Markus Elfring35ffd082015-07-07 21:48:23 +0200469 sym_calc_value(modules_sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472bool sym_tristate_within_range(struct symbol *sym, tristate val)
473{
474 int type = sym_get_type(sym);
475
476 if (sym->visible == no)
477 return false;
478
479 if (type != S_BOOLEAN && type != S_TRISTATE)
480 return false;
481
482 if (type == S_BOOLEAN && val == mod)
483 return false;
484 if (sym->visible <= sym->rev_dep.tri)
485 return false;
486 if (sym_is_choice_value(sym) && sym->visible == yes)
487 return val == yes;
488 return val >= sym->rev_dep.tri && val <= sym->visible;
489}
490
491bool sym_set_tristate_value(struct symbol *sym, tristate val)
492{
493 tristate oldval = sym_get_tristate_value(sym);
494
495 if (oldval != val && !sym_tristate_within_range(sym, val))
496 return false;
497
Roman Zippel669bfad92006-06-08 22:12:42 -0700498 if (!(sym->flags & SYMBOL_DEF_USER)) {
499 sym->flags |= SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 sym_set_changed(sym);
501 }
Roman Zippel3f23ca22005-11-08 21:34:48 -0800502 /*
503 * setting a choice value also resets the new flag of the choice
504 * symbol and all other choice values.
505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (sym_is_choice_value(sym) && val == yes) {
507 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel3f23ca22005-11-08 21:34:48 -0800508 struct property *prop;
509 struct expr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Roman Zippel0c1822e2006-06-08 22:12:41 -0700511 cs->def[S_DEF_USER].val = sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700512 cs->flags |= SYMBOL_DEF_USER;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800513 prop = sym_get_choice_prop(cs);
514 for (e = prop->expr; e; e = e->left.expr) {
515 if (e->right.sym->visible != no)
Roman Zippel669bfad92006-06-08 22:12:42 -0700516 e->right.sym->flags |= SYMBOL_DEF_USER;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519
Roman Zippel0c1822e2006-06-08 22:12:41 -0700520 sym->def[S_DEF_USER].tri = val;
Roman Zippelface4372006-06-08 22:12:45 -0700521 if (oldval != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 sym_clear_all_valid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 return true;
525}
526
527tristate sym_toggle_tristate_value(struct symbol *sym)
528{
529 tristate oldval, newval;
530
531 oldval = newval = sym_get_tristate_value(sym);
532 do {
533 switch (newval) {
534 case no:
535 newval = mod;
536 break;
537 case mod:
538 newval = yes;
539 break;
540 case yes:
541 newval = no;
542 break;
543 }
544 if (sym_set_tristate_value(sym, newval))
545 break;
546 } while (oldval != newval);
547 return newval;
548}
549
550bool sym_string_valid(struct symbol *sym, const char *str)
551{
552 signed char ch;
553
554 switch (sym->type) {
555 case S_STRING:
556 return true;
557 case S_INT:
558 ch = *str++;
559 if (ch == '-')
560 ch = *str++;
561 if (!isdigit(ch))
562 return false;
563 if (ch == '0' && *str != 0)
564 return false;
565 while ((ch = *str++)) {
566 if (!isdigit(ch))
567 return false;
568 }
569 return true;
570 case S_HEX:
571 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
572 str += 2;
573 ch = *str++;
574 do {
575 if (!isxdigit(ch))
576 return false;
577 } while ((ch = *str++));
578 return true;
579 case S_BOOLEAN:
580 case S_TRISTATE:
581 switch (str[0]) {
582 case 'y': case 'Y':
583 case 'm': case 'M':
584 case 'n': case 'N':
585 return true;
586 }
587 return false;
588 default:
589 return false;
590 }
591}
592
593bool sym_string_within_range(struct symbol *sym, const char *str)
594{
595 struct property *prop;
Kees Cook129784a2013-07-18 11:32:01 -0700596 long long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 switch (sym->type) {
599 case S_STRING:
600 return sym_string_valid(sym, str);
601 case S_INT:
602 if (!sym_string_valid(sym, str))
603 return false;
604 prop = sym_get_range_prop(sym);
605 if (!prop)
606 return true;
Kees Cook129784a2013-07-18 11:32:01 -0700607 val = strtoll(str, NULL, 10);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800608 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
609 val <= sym_get_range_val(prop->expr->right.sym, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 case S_HEX:
611 if (!sym_string_valid(sym, str))
612 return false;
613 prop = sym_get_range_prop(sym);
614 if (!prop)
615 return true;
Kees Cook129784a2013-07-18 11:32:01 -0700616 val = strtoll(str, NULL, 16);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800617 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
618 val <= sym_get_range_val(prop->expr->right.sym, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 case S_BOOLEAN:
620 case S_TRISTATE:
621 switch (str[0]) {
622 case 'y': case 'Y':
623 return sym_tristate_within_range(sym, yes);
624 case 'm': case 'M':
625 return sym_tristate_within_range(sym, mod);
626 case 'n': case 'N':
627 return sym_tristate_within_range(sym, no);
628 }
629 return false;
630 default:
631 return false;
632 }
633}
634
635bool sym_set_string_value(struct symbol *sym, const char *newval)
636{
637 const char *oldval;
638 char *val;
639 int size;
640
641 switch (sym->type) {
642 case S_BOOLEAN:
643 case S_TRISTATE:
644 switch (newval[0]) {
645 case 'y': case 'Y':
646 return sym_set_tristate_value(sym, yes);
647 case 'm': case 'M':
648 return sym_set_tristate_value(sym, mod);
649 case 'n': case 'N':
650 return sym_set_tristate_value(sym, no);
651 }
652 return false;
653 default:
654 ;
655 }
656
657 if (!sym_string_within_range(sym, newval))
658 return false;
659
Roman Zippel669bfad92006-06-08 22:12:42 -0700660 if (!(sym->flags & SYMBOL_DEF_USER)) {
661 sym->flags |= SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 sym_set_changed(sym);
663 }
664
Roman Zippel0c1822e2006-06-08 22:12:41 -0700665 oldval = sym->def[S_DEF_USER].val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 size = strlen(newval) + 1;
667 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
668 size += 2;
Alan Cox177acf72012-11-06 14:32:08 +0000669 sym->def[S_DEF_USER].val = val = xmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 *val++ = '0';
671 *val++ = 'x';
672 } else if (!oldval || strcmp(oldval, newval))
Alan Cox177acf72012-11-06 14:32:08 +0000673 sym->def[S_DEF_USER].val = val = xmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 else
675 return true;
676
677 strcpy(val, newval);
678 free((void *)oldval);
679 sym_clear_all_valid();
680
681 return true;
682}
683
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200684/*
685 * Find the default value associated to a symbol.
686 * For tristate symbol handle the modules=n case
687 * in which case "m" becomes "y".
688 * If the symbol does not have any default then fallback
689 * to the fixed default values.
690 */
691const char *sym_get_string_default(struct symbol *sym)
692{
693 struct property *prop;
694 struct symbol *ds;
695 const char *str;
696 tristate val;
697
698 sym_calc_visibility(sym);
699 sym_calc_value(modules_sym);
700 val = symbol_no.curr.tri;
701 str = symbol_empty.curr.val;
702
703 /* If symbol has a default value look it up */
704 prop = sym_get_default_prop(sym);
705 if (prop != NULL) {
706 switch (sym->type) {
707 case S_BOOLEAN:
708 case S_TRISTATE:
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -0500709 /* The visibility may limit the value from yes => mod */
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200710 val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
711 break;
712 default:
713 /*
714 * The following fails to handle the situation
715 * where a default value is further limited by
716 * the valid range.
717 */
718 ds = prop_get_symbol(prop);
719 if (ds != NULL) {
720 sym_calc_value(ds);
721 str = (const char *)ds->curr.val;
722 }
723 }
724 }
725
726 /* Handle select statements */
727 val = EXPR_OR(val, sym->rev_dep.tri);
728
729 /* transpose mod to yes if modules are not enabled */
730 if (val == mod)
731 if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
732 val = yes;
733
734 /* transpose mod to yes if type is bool */
735 if (sym->type == S_BOOLEAN && val == mod)
736 val = yes;
737
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500738 /* adjust the default value if this symbol is implied by another */
739 if (val < sym->implied.tri)
740 val = sym->implied.tri;
741
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200742 switch (sym->type) {
743 case S_BOOLEAN:
744 case S_TRISTATE:
745 switch (val) {
746 case no: return "n";
747 case mod: return "m";
748 case yes: return "y";
749 }
750 case S_INT:
751 case S_HEX:
752 return str;
753 case S_STRING:
754 return str;
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200755 case S_UNKNOWN:
756 break;
757 }
758 return "";
759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761const char *sym_get_string_value(struct symbol *sym)
762{
763 tristate val;
764
765 switch (sym->type) {
766 case S_BOOLEAN:
767 case S_TRISTATE:
768 val = sym_get_tristate_value(sym);
769 switch (val) {
770 case no:
771 return "n";
772 case mod:
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400773 sym_calc_value(modules_sym);
774 return (modules_sym->curr.tri == no) ? "n" : "m";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 case yes:
776 return "y";
777 }
778 break;
779 default:
780 ;
781 }
782 return (const char *)sym->curr.val;
783}
784
Marco Ammonbaa23ec2019-07-04 12:50:41 +0200785bool sym_is_changeable(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 return sym->visible > sym->rev_dep.tri;
788}
789
Andi Kleene66f25d2010-01-13 17:02:44 +0100790static unsigned strhash(const char *s)
791{
792 /* fnv32 hash */
793 unsigned hash = 2166136261U;
794 for (; *s; s++)
795 hash = (hash ^ *s) * 0x01000193;
796 return hash;
797}
798
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100799struct symbol *sym_lookup(const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct symbol *symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 char *new_name;
Andi Kleene66f25d2010-01-13 17:02:44 +0100803 int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (name) {
806 if (name[0] && !name[1]) {
807 switch (name[0]) {
808 case 'y': return &symbol_yes;
809 case 'm': return &symbol_mod;
810 case 'n': return &symbol_no;
811 }
812 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100813 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100816 if (symbol->name &&
817 !strcmp(symbol->name, name) &&
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100818 (flags ? symbol->flags & flags
819 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
820 return symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900822 new_name = xstrdup(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 } else {
824 new_name = NULL;
Andi Kleene66f25d2010-01-13 17:02:44 +0100825 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827
Alan Cox177acf72012-11-06 14:32:08 +0000828 symbol = xmalloc(sizeof(*symbol));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 memset(symbol, 0, sizeof(*symbol));
830 symbol->name = new_name;
831 symbol->type = S_UNKNOWN;
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100832 symbol->flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 symbol->next = symbol_hash[hash];
835 symbol_hash[hash] = symbol;
836
837 return symbol;
838}
839
840struct symbol *sym_find(const char *name)
841{
842 struct symbol *symbol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 int hash = 0;
844
845 if (!name)
846 return NULL;
847
848 if (name[0] && !name[1]) {
849 switch (name[0]) {
850 case 'y': return &symbol_yes;
851 case 'm': return &symbol_mod;
852 case 'n': return &symbol_no;
853 }
854 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100855 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100858 if (symbol->name &&
859 !strcmp(symbol->name, name) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 !(symbol->flags & SYMBOL_CONST))
861 break;
862 }
863
864 return symbol;
865}
866
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400867const char *sym_escape_string_value(const char *in)
868{
869 const char *p;
870 size_t reslen;
871 char *res;
872 size_t l;
873
874 reslen = strlen(in) + strlen("\"\"") + 1;
875
876 p = in;
877 for (;;) {
878 l = strcspn(p, "\"\\");
879 p += l;
880
881 if (p[0] == '\0')
882 break;
883
884 reslen++;
885 p++;
886 }
887
Alan Cox177acf72012-11-06 14:32:08 +0000888 res = xmalloc(reslen);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400889 res[0] = '\0';
890
891 strcat(res, "\"");
892
893 p = in;
894 for (;;) {
895 l = strcspn(p, "\"\\");
896 strncat(res, p, l);
897 p += l;
898
899 if (p[0] == '\0')
900 break;
901
902 strcat(res, "\\");
903 strncat(res, p++, 1);
904 }
905
906 strcat(res, "\"");
907 return res;
908}
909
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200910struct sym_match {
911 struct symbol *sym;
912 off_t so, eo;
913};
914
915/* Compare matched symbols as thus:
916 * - first, symbols that match exactly
917 * - then, alphabetical sort
918 */
Yann E. MORIN803b3512013-07-16 20:28:51 +0200919static int sym_rel_comp(const void *sym1, const void *sym2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200920{
Yann E. MORIN508382a2013-07-16 20:39:42 +0200921 const struct sym_match *s1 = sym1;
922 const struct sym_match *s2 = sym2;
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200923 int exact1, exact2;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200924
925 /* Exact match:
926 * - if matched length on symbol s1 is the length of that symbol,
927 * then this symbol should come first;
928 * - if matched length on symbol s2 is the length of that symbol,
929 * then this symbol should come first.
930 * Note: since the search can be a regexp, both symbols may match
931 * exactly; if this is the case, we can't decide which comes first,
932 * and we fallback to sorting alphabetically.
933 */
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200934 exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
935 exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
936 if (exact1 && !exact2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200937 return -1;
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200938 if (!exact1 && exact2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200939 return 1;
940
941 /* As a fallback, sort symbols alphabetically */
942 return strcmp(s1->sym->name, s2->sym->name);
943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945struct symbol **sym_re_search(const char *pattern)
946{
947 struct symbol *sym, **sym_arr = NULL;
Yann E. MORIN508382a2013-07-16 20:39:42 +0200948 struct sym_match *sym_match_arr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 int i, cnt, size;
950 regex_t re;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200951 regmatch_t match[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 cnt = size = 0;
954 /* Skip if empty */
955 if (strlen(pattern) == 0)
956 return NULL;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200957 if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return NULL;
959
960 for_all_symbols(i, sym) {
961 if (sym->flags & SYMBOL_CONST || !sym->name)
962 continue;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200963 if (regexec(&re, sym->name, 1, match, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 continue;
Yann E. MORIN1407f97a2013-07-16 20:32:33 +0200965 if (cnt >= size) {
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200966 void *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 size += 16;
Yann E. MORIN508382a2013-07-16 20:39:42 +0200968 tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
Yann E. MORIN803b3512013-07-16 20:28:51 +0200969 if (!tmp)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200970 goto sym_re_search_free;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200971 sym_match_arr = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Li Zefanda6df872010-03-19 14:57:47 +0800973 sym_calc_value(sym);
Yann E. MORIN803b3512013-07-16 20:28:51 +0200974 /* As regexec returned 0, we know we have a match, so
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200975 * we can use match[0].rm_[se]o without further checks
976 */
Yann E. MORIN508382a2013-07-16 20:39:42 +0200977 sym_match_arr[cnt].so = match[0].rm_so;
978 sym_match_arr[cnt].eo = match[0].rm_eo;
979 sym_match_arr[cnt++].sym = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200981 if (sym_match_arr) {
Yann E. MORIN508382a2013-07-16 20:39:42 +0200982 qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
Heinrich Schuchardt88127da2017-11-08 22:09:59 +0100983 sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200984 if (!sym_arr)
985 goto sym_re_search_free;
986 for (i = 0; i < cnt; i++)
Yann E. MORIN508382a2013-07-16 20:39:42 +0200987 sym_arr[i] = sym_match_arr[i].sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 sym_arr[cnt] = NULL;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200989 }
990sym_re_search_free:
Yann E. MORIN508382a2013-07-16 20:39:42 +0200991 /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
992 free(sym_match_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 regfree(&re);
994
995 return sym_arr;
996}
997
Roman Zippeld595cea2010-07-31 23:35:30 +0200998/*
999 * When we check for recursive dependencies we use a stack to save
1000 * current state so we can print out relevant info to user.
1001 * The entries are located on the call stack so no need to free memory.
Martin Walch8d9dfe82013-10-03 17:28:14 +02001002 * Note insert() remove() must always match to properly clear the stack.
Roman Zippeld595cea2010-07-31 23:35:30 +02001003 */
1004static struct dep_stack {
1005 struct dep_stack *prev, *next;
1006 struct symbol *sym;
1007 struct property *prop;
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001008 struct expr **expr;
Roman Zippeld595cea2010-07-31 23:35:30 +02001009} *check_top;
1010
1011static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
1012{
1013 memset(stack, 0, sizeof(*stack));
1014 if (check_top)
1015 check_top->next = stack;
1016 stack->prev = check_top;
1017 stack->sym = sym;
1018 check_top = stack;
1019}
1020
1021static void dep_stack_remove(void)
1022{
1023 check_top = check_top->prev;
1024 if (check_top)
1025 check_top->next = NULL;
1026}
1027
1028/*
1029 * Called when we have detected a recursive dependency.
1030 * check_top point to the top of the stact so we use
1031 * the ->prev pointer to locate the bottom of the stack.
1032 */
1033static void sym_check_print_recursive(struct symbol *last_sym)
1034{
1035 struct dep_stack *stack;
1036 struct symbol *sym, *next_sym;
1037 struct menu *menu = NULL;
1038 struct property *prop;
1039 struct dep_stack cv_stack;
1040
1041 if (sym_is_choice_value(last_sym)) {
1042 dep_stack_insert(&cv_stack, last_sym);
1043 last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1044 }
1045
1046 for (stack = check_top; stack != NULL; stack = stack->prev)
1047 if (stack->sym == last_sym)
1048 break;
1049 if (!stack) {
1050 fprintf(stderr, "unexpected recursive dependency error\n");
1051 return;
1052 }
1053
1054 for (; stack; stack = stack->next) {
1055 sym = stack->sym;
1056 next_sym = stack->next ? stack->next->sym : last_sym;
1057 prop = stack->prop;
Sam Ravnborg3643f842010-08-14 14:40:00 +02001058 if (prop == NULL)
1059 prop = stack->sym->prop;
Roman Zippeld595cea2010-07-31 23:35:30 +02001060
1061 /* for choice values find the menu entry (used below) */
1062 if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1063 for (prop = sym->prop; prop; prop = prop->next) {
1064 menu = prop->menu;
1065 if (prop->menu)
1066 break;
1067 }
1068 }
1069 if (stack->sym == last_sym)
1070 fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1071 prop->file->name, prop->lineno);
Masahiro Yamadae3b03bf2017-12-16 00:28:42 +09001072
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001073 if (sym_is_choice(sym)) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001074 fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1075 menu->file->name, menu->lineno,
1076 sym->name ? sym->name : "<choice>",
1077 next_sym->name ? next_sym->name : "<choice>");
1078 } else if (sym_is_choice_value(sym)) {
1079 fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1080 menu->file->name, menu->lineno,
1081 sym->name ? sym->name : "<choice>",
1082 next_sym->name ? next_sym->name : "<choice>");
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001083 } else if (stack->expr == &sym->dir_dep.expr) {
1084 fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
Roman Zippeld595cea2010-07-31 23:35:30 +02001085 prop->file->name, prop->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->rev_dep.expr) {
1089 fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1090 prop->file->name, prop->lineno,
1091 sym->name ? sym->name : "<choice>",
1092 next_sym->name ? next_sym->name : "<choice>");
1093 } else if (stack->expr == &sym->implied.expr) {
1094 fprintf(stderr, "%s:%d:\tsymbol %s is implied 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) {
1099 fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1100 prop->file->name, prop->lineno,
1101 sym->name ? sym->name : "<choice>",
1102 prop_get_type_name(prop->type),
1103 next_sym->name ? next_sym->name : "<choice>");
1104 } else {
1105 fprintf(stderr, "%s:%d:\tsymbol %s %s is visible depending on %s\n",
1106 prop->file->name, prop->lineno,
1107 sym->name ? sym->name : "<choice>",
1108 prop_get_type_name(prop->type),
1109 next_sym->name ? next_sym->name : "<choice>");
Roman Zippeld595cea2010-07-31 23:35:30 +02001110 }
1111 }
1112
Masahiro Yamadae3b03bf2017-12-16 00:28:42 +09001113 fprintf(stderr,
Mauro Carvalho Chehabcd238ef2019-06-12 14:52:48 -03001114 "For a resolution refer to Documentation/kbuild/kconfig-language.rst\n"
Masahiro Yamadae3b03bf2017-12-16 00:28:42 +09001115 "subsection \"Kconfig recursive dependency limitations\"\n"
1116 "\n");
1117
Roman Zippeld595cea2010-07-31 23:35:30 +02001118 if (check_top == &cv_stack)
1119 dep_stack_remove();
1120}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122static struct symbol *sym_check_expr_deps(struct expr *e)
1123{
1124 struct symbol *sym;
1125
1126 if (!e)
1127 return NULL;
1128 switch (e->type) {
1129 case E_OR:
1130 case E_AND:
1131 sym = sym_check_expr_deps(e->left.expr);
1132 if (sym)
1133 return sym;
1134 return sym_check_expr_deps(e->right.expr);
1135 case E_NOT:
1136 return sym_check_expr_deps(e->left.expr);
1137 case E_EQUAL:
Jan Beulich31847b62015-06-15 13:00:21 +01001138 case E_GEQ:
1139 case E_GTH:
1140 case E_LEQ:
1141 case E_LTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 case E_UNEQUAL:
1143 sym = sym_check_deps(e->left.sym);
1144 if (sym)
1145 return sym;
1146 return sym_check_deps(e->right.sym);
1147 case E_SYMBOL:
1148 return sym_check_deps(e->left.sym);
1149 default:
1150 break;
1151 }
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +09001152 fprintf(stderr, "Oops! How to check %d?\n", e->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return NULL;
1154}
1155
Sam Ravnborg5447d342007-05-06 09:20:10 +02001156/* return NULL when dependencies are OK */
Roman Zippel48981172008-02-29 05:10:24 +01001157static struct symbol *sym_check_sym_deps(struct symbol *sym)
1158{
1159 struct symbol *sym2;
1160 struct property *prop;
Roman Zippeld595cea2010-07-31 23:35:30 +02001161 struct dep_stack stack;
1162
1163 dep_stack_insert(&stack, sym);
Roman Zippel48981172008-02-29 05:10:24 +01001164
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001165 stack.expr = &sym->dir_dep.expr;
1166 sym2 = sym_check_expr_deps(sym->dir_dep.expr);
1167 if (sym2)
1168 goto out;
1169
1170 stack.expr = &sym->rev_dep.expr;
Roman Zippel48981172008-02-29 05:10:24 +01001171 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1172 if (sym2)
Roman Zippeld595cea2010-07-31 23:35:30 +02001173 goto out;
Roman Zippel48981172008-02-29 05:10:24 +01001174
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001175 stack.expr = &sym->implied.expr;
Masahiro Yamada5e8c5292018-08-15 14:59:44 +09001176 sym2 = sym_check_expr_deps(sym->implied.expr);
1177 if (sym2)
1178 goto out;
1179
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001180 stack.expr = NULL;
1181
Roman Zippel48981172008-02-29 05:10:24 +01001182 for (prop = sym->prop; prop; prop = prop->next) {
Masahiro Yamada5e8c5292018-08-15 14:59:44 +09001183 if (prop->type == P_CHOICE || prop->type == P_SELECT ||
1184 prop->type == P_IMPLY)
Roman Zippel48981172008-02-29 05:10:24 +01001185 continue;
Roman Zippeld595cea2010-07-31 23:35:30 +02001186 stack.prop = prop;
Roman Zippel48981172008-02-29 05:10:24 +01001187 sym2 = sym_check_expr_deps(prop->visible.expr);
1188 if (sym2)
1189 break;
1190 if (prop->type != P_DEFAULT || sym_is_choice(sym))
1191 continue;
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001192 stack.expr = &prop->expr;
Roman Zippel48981172008-02-29 05:10:24 +01001193 sym2 = sym_check_expr_deps(prop->expr);
1194 if (sym2)
1195 break;
Roman Zippeld595cea2010-07-31 23:35:30 +02001196 stack.expr = NULL;
Roman Zippel48981172008-02-29 05:10:24 +01001197 }
1198
Roman Zippeld595cea2010-07-31 23:35:30 +02001199out:
1200 dep_stack_remove();
1201
Roman Zippel48981172008-02-29 05:10:24 +01001202 return sym2;
1203}
1204
1205static struct symbol *sym_check_choice_deps(struct symbol *choice)
1206{
1207 struct symbol *sym, *sym2;
1208 struct property *prop;
1209 struct expr *e;
Roman Zippeld595cea2010-07-31 23:35:30 +02001210 struct dep_stack stack;
1211
1212 dep_stack_insert(&stack, choice);
Roman Zippel48981172008-02-29 05:10:24 +01001213
1214 prop = sym_get_choice_prop(choice);
1215 expr_list_for_each_sym(prop->expr, e, sym)
1216 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1217
1218 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1219 sym2 = sym_check_sym_deps(choice);
1220 choice->flags &= ~SYMBOL_CHECK;
1221 if (sym2)
1222 goto out;
1223
1224 expr_list_for_each_sym(prop->expr, e, sym) {
1225 sym2 = sym_check_sym_deps(sym);
Roman Zippeld595cea2010-07-31 23:35:30 +02001226 if (sym2)
Roman Zippel48981172008-02-29 05:10:24 +01001227 break;
Roman Zippel48981172008-02-29 05:10:24 +01001228 }
1229out:
1230 expr_list_for_each_sym(prop->expr, e, sym)
1231 sym->flags &= ~SYMBOL_CHECK;
1232
1233 if (sym2 && sym_is_choice_value(sym2) &&
1234 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1235 sym2 = choice;
1236
Roman Zippeld595cea2010-07-31 23:35:30 +02001237 dep_stack_remove();
1238
Roman Zippel48981172008-02-29 05:10:24 +01001239 return sym2;
1240}
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242struct symbol *sym_check_deps(struct symbol *sym)
1243{
1244 struct symbol *sym2;
1245 struct property *prop;
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (sym->flags & SYMBOL_CHECK) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001248 sym_check_print_recursive(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return sym;
1250 }
David Gibson3f04e7d2005-11-08 21:34:46 -08001251 if (sym->flags & SYMBOL_CHECKED)
1252 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Roman Zippel48981172008-02-29 05:10:24 +01001254 if (sym_is_choice_value(sym)) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001255 struct dep_stack stack;
1256
Roman Zippel48981172008-02-29 05:10:24 +01001257 /* for choice groups start the check with main choice symbol */
Roman Zippeld595cea2010-07-31 23:35:30 +02001258 dep_stack_insert(&stack, sym);
Roman Zippel48981172008-02-29 05:10:24 +01001259 prop = sym_get_choice_prop(sym);
1260 sym2 = sym_check_deps(prop_get_symbol(prop));
Roman Zippeld595cea2010-07-31 23:35:30 +02001261 dep_stack_remove();
Roman Zippel48981172008-02-29 05:10:24 +01001262 } else if (sym_is_choice(sym)) {
1263 sym2 = sym_check_choice_deps(sym);
1264 } else {
1265 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1266 sym2 = sym_check_sym_deps(sym);
1267 sym->flags &= ~SYMBOL_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
Roman Zippel48981172008-02-29 05:10:24 +01001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return sym2;
1271}
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273struct symbol *prop_get_symbol(struct property *prop)
1274{
1275 if (prop->expr && (prop->expr->type == E_SYMBOL ||
Roman Zippel7a962922008-01-14 04:50:23 +01001276 prop->expr->type == E_LIST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return prop->expr->left.sym;
1278 return NULL;
1279}
1280
1281const char *prop_get_type_name(enum prop_type type)
1282{
1283 switch (type) {
1284 case P_PROMPT:
1285 return "prompt";
1286 case P_COMMENT:
1287 return "comment";
1288 case P_MENU:
1289 return "menu";
1290 case P_DEFAULT:
1291 return "default";
1292 case P_CHOICE:
1293 return "choice";
1294 case P_SELECT:
1295 return "select";
Nicolas Pitre237e3ad2016-11-11 00:10:05 -05001296 case P_IMPLY:
1297 return "imply";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 case P_RANGE:
1299 return "range";
Sam Ravnborg59e89e32010-07-31 23:35:29 +02001300 case P_SYMBOL:
1301 return "symbol";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 case P_UNKNOWN:
1303 break;
1304 }
1305 return "unknown";
1306}