blob: 703b9b899ee9c6fd0b2f2de9411d8be5b4413cbe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
5
6#include <ctype.h>
7#include <stdlib.h>
8#include <string.h>
9#include <regex.h>
10#include <sys/utsname.h>
11
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";
64 case S_OTHER:
65 break;
66 }
67 return "???";
68}
69
70struct property *sym_get_choice_prop(struct symbol *sym)
71{
72 struct property *prop;
73
74 for_all_choices(sym, prop)
75 return prop;
76 return NULL;
77}
78
Michal Marekad8d40c2015-02-24 16:37:13 +010079static struct property *sym_get_default_prop(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 struct property *prop;
82
83 for_all_defaults(sym, prop) {
84 prop->visible.tri = expr_calc_value(prop->visible.expr);
85 if (prop->visible.tri != no)
86 return prop;
87 }
88 return NULL;
89}
90
Trevor Keith4356f482009-09-18 12:49:23 -070091static struct property *sym_get_range_prop(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct property *prop;
94
95 for_all_properties(sym, prop, P_RANGE) {
96 prop->visible.tri = expr_calc_value(prop->visible.expr);
97 if (prop->visible.tri != no)
98 return prop;
99 }
100 return NULL;
101}
102
Kees Cook129784a2013-07-18 11:32:01 -0700103static long long sym_get_range_val(struct symbol *sym, int base)
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800104{
105 sym_calc_value(sym);
106 switch (sym->type) {
107 case S_INT:
108 base = 10;
109 break;
110 case S_HEX:
111 base = 16;
112 break;
113 default:
114 break;
115 }
Kees Cook129784a2013-07-18 11:32:01 -0700116 return strtoll(sym->curr.val, NULL, base);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800117}
118
119static void sym_validate_range(struct symbol *sym)
120{
121 struct property *prop;
Kees Cook129784a2013-07-18 11:32:01 -0700122 int base;
123 long long val, val2;
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800124 char str[64];
125
126 switch (sym->type) {
127 case S_INT:
128 base = 10;
129 break;
130 case S_HEX:
131 base = 16;
132 break;
133 default:
134 return;
135 }
136 prop = sym_get_range_prop(sym);
137 if (!prop)
138 return;
Kees Cook129784a2013-07-18 11:32:01 -0700139 val = strtoll(sym->curr.val, NULL, base);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800140 val2 = sym_get_range_val(prop->expr->left.sym, base);
141 if (val >= val2) {
142 val2 = sym_get_range_val(prop->expr->right.sym, base);
143 if (val <= val2)
144 return;
145 }
146 if (sym->type == S_INT)
Kees Cook129784a2013-07-18 11:32:01 -0700147 sprintf(str, "%lld", val2);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800148 else
Kees Cook129784a2013-07-18 11:32:01 -0700149 sprintf(str, "0x%llx", val2);
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900150 sym->curr.val = xstrdup(str);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800151}
152
Michal Marekad8d40c2015-02-24 16:37:13 +0100153static void sym_set_changed(struct symbol *sym)
154{
155 struct property *prop;
156
157 sym->flags |= SYMBOL_CHANGED;
158 for (prop = sym->prop; prop; prop = prop->next) {
159 if (prop->menu)
160 prop->menu->flags |= MENU_CHANGED;
161 }
162}
163
164static void sym_set_all_changed(void)
165{
166 struct symbol *sym;
167 int i;
168
169 for_all_symbols(i, sym)
170 sym_set_changed(sym);
171}
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static void sym_calc_visibility(struct symbol *sym)
174{
175 struct property *prop;
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200176 struct symbol *choice_sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 tristate tri;
178
179 /* any prompt visible? */
180 tri = no;
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200181
182 if (sym_is_choice_value(sym))
183 choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 for_all_prompts(sym, prop) {
186 prop->visible.tri = expr_calc_value(prop->visible.expr);
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200187 /*
188 * Tristate choice_values with visibility 'mod' are
189 * not visible if the corresponding choice's value is
190 * 'yes'.
191 */
192 if (choice_sym && sym->type == S_TRISTATE &&
193 prop->visible.tri == mod && choice_sym->curr.tri == yes)
194 prop->visible.tri = no;
195
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100196 tri = EXPR_OR(tri, prop->visible.tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
199 tri = yes;
200 if (sym->visible != tri) {
201 sym->visible = tri;
202 sym_set_changed(sym);
203 }
204 if (sym_is_choice_value(sym))
205 return;
Catalin Marinas246cf9c2010-06-08 17:25:57 +0100206 /* defaulting to "yes" if no explicit "depends on" are given */
207 tri = yes;
208 if (sym->dir_dep.expr)
209 tri = expr_calc_value(sym->dir_dep.expr);
Masahiro Yamadaf622f822018-03-13 18:56:07 +0900210 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
Catalin Marinas246cf9c2010-06-08 17:25:57 +0100211 tri = yes;
212 if (sym->dir_dep.tri != tri) {
213 sym->dir_dep.tri = tri;
214 sym_set_changed(sym);
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 tri = no;
217 if (sym->rev_dep.expr)
218 tri = expr_calc_value(sym->rev_dep.expr);
219 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
220 tri = yes;
221 if (sym->rev_dep.tri != tri) {
222 sym->rev_dep.tri = tri;
223 sym_set_changed(sym);
224 }
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500225 tri = no;
226 if (sym->implied.expr && sym->dir_dep.tri != no)
227 tri = expr_calc_value(sym->implied.expr);
228 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
229 tri = yes;
230 if (sym->implied.tri != tri) {
231 sym->implied.tri = tri;
232 sym_set_changed(sym);
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Sam Ravnborgc2521472010-07-31 23:35:32 +0200236/*
237 * Find the default symbol for a choice.
238 * First try the default values for the choice symbol
239 * Next locate the first visible choice value
240 * Return NULL if none was found
241 */
242struct symbol *sym_choice_default(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct symbol *def_sym;
245 struct property *prop;
246 struct expr *e;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* any of the defaults visible? */
249 for_all_defaults(sym, prop) {
250 prop->visible.tri = expr_calc_value(prop->visible.expr);
251 if (prop->visible.tri == no)
252 continue;
253 def_sym = prop_get_symbol(prop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (def_sym->visible != no)
255 return def_sym;
256 }
257
258 /* just get the first visible value */
259 prop = sym_get_choice_prop(sym);
Jan Beulich0a28c472010-06-30 13:11:01 +0100260 expr_list_for_each_sym(prop->expr, e, def_sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (def_sym->visible != no)
262 return def_sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Sam Ravnborgc2521472010-07-31 23:35:32 +0200264 /* failed to locate any defaults */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return NULL;
266}
267
Sam Ravnborgc2521472010-07-31 23:35:32 +0200268static struct symbol *sym_calc_choice(struct symbol *sym)
269{
270 struct symbol *def_sym;
271 struct property *prop;
272 struct expr *e;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500273 int flags;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200274
275 /* first calculate all choice values' visibilities */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500276 flags = sym->flags;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200277 prop = sym_get_choice_prop(sym);
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500278 expr_list_for_each_sym(prop->expr, e, def_sym) {
Sam Ravnborgc2521472010-07-31 23:35:32 +0200279 sym_calc_visibility(def_sym);
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500280 if (def_sym->visible != no)
281 flags &= def_sym->flags;
282 }
283
284 sym->flags &= flags | ~SYMBOL_DEF_USER;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200285
286 /* is the user choice visible? */
287 def_sym = sym->def[S_DEF_USER].val;
288 if (def_sym && def_sym->visible != no)
289 return def_sym;
290
291 def_sym = sym_choice_default(sym);
292
293 if (def_sym == NULL)
294 /* no choice? reset tristate value */
295 sym->curr.tri = no;
296
297 return def_sym;
298}
299
Masahiro Yamadaf8f69dc2018-03-13 18:56:08 +0900300static void sym_warn_unmet_dep(struct symbol *sym)
301{
302 struct gstr gs = str_new();
303
304 str_printf(&gs,
305 "\nWARNING: unmet direct dependencies detected for %s\n",
306 sym->name);
307 str_printf(&gs,
308 " Depends on [%c]: ",
309 sym->dir_dep.tri == mod ? 'm' : 'n');
310 expr_gstr_print(sym->dir_dep.expr, &gs);
311 str_printf(&gs, "\n");
312
313 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes,
314 " Selected by [y]:\n");
315 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod,
316 " Selected by [m]:\n");
317
318 fputs(str_get(&gs), stderr);
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321void sym_calc_value(struct symbol *sym)
322{
323 struct symbol_value newval, oldval;
324 struct property *prop;
325 struct expr *e;
326
327 if (!sym)
328 return;
329
330 if (sym->flags & SYMBOL_VALID)
331 return;
Arve Hjønnevågfbe98bb2013-06-06 20:37:00 -0700332
333 if (sym_is_choice_value(sym) &&
334 sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
335 sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
336 prop = sym_get_choice_prop(sym);
337 sym_calc_value(prop_get_symbol(prop));
338 }
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sym->flags |= SYMBOL_VALID;
341
342 oldval = sym->curr;
343
344 switch (sym->type) {
345 case S_INT:
346 case S_HEX:
347 case S_STRING:
348 newval = symbol_empty.curr;
349 break;
350 case S_BOOLEAN:
351 case S_TRISTATE:
352 newval = symbol_no.curr;
353 break;
354 default:
355 sym->curr.val = sym->name;
356 sym->curr.tri = no;
357 return;
358 }
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900359 sym->flags &= ~SYMBOL_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 sym_calc_visibility(sym);
362
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900363 if (sym->visible != no)
364 sym->flags |= SYMBOL_WRITE;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* set default if recursively called */
367 sym->curr = newval;
368
369 switch (sym_get_type(sym)) {
370 case S_BOOLEAN:
371 case S_TRISTATE:
372 if (sym_is_choice_value(sym) && sym->visible == yes) {
373 prop = sym_get_choice_prop(sym);
374 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
Roman Zippel587c9062008-02-11 21:13:47 +0100375 } else {
376 if (sym->visible != no) {
377 /* if the symbol is visible use the user value
378 * if available, otherwise try the default value
379 */
Roman Zippel587c9062008-02-11 21:13:47 +0100380 if (sym_has_value(sym)) {
381 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
382 sym->visible);
383 goto calc_newval;
384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Roman Zippel587c9062008-02-11 21:13:47 +0100386 if (sym->rev_dep.tri != no)
387 sym->flags |= SYMBOL_WRITE;
388 if (!sym_is_choice(sym)) {
389 prop = sym_get_default_prop(sym);
390 if (prop) {
Roman Zippel587c9062008-02-11 21:13:47 +0100391 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
392 prop->visible.tri);
Ulf Magnussonf467c562018-02-23 12:49:01 +0100393 if (newval.tri != no)
394 sym->flags |= SYMBOL_WRITE;
Roman Zippel587c9062008-02-11 21:13:47 +0100395 }
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500396 if (sym->implied.tri != no) {
397 sym->flags |= SYMBOL_WRITE;
398 newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
399 }
Roman Zippel587c9062008-02-11 21:13:47 +0100400 }
401 calc_newval:
Masahiro Yamadaf8f69dc2018-03-13 18:56:08 +0900402 if (sym->dir_dep.tri < sym->rev_dep.tri)
403 sym_warn_unmet_dep(sym);
Roman Zippel587c9062008-02-11 21:13:47 +0100404 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500406 if (newval.tri == mod &&
407 (sym_get_type(sym) == S_BOOLEAN || sym->implied.tri == yes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 newval.tri = yes;
409 break;
410 case S_STRING:
411 case S_HEX:
412 case S_INT:
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900413 if (sym->visible != no && sym_has_value(sym)) {
414 newval.val = sym->def[S_DEF_USER].val;
415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 prop = sym_get_default_prop(sym);
418 if (prop) {
419 struct symbol *ds = prop_get_symbol(prop);
420 if (ds) {
421 sym->flags |= SYMBOL_WRITE;
422 sym_calc_value(ds);
423 newval.val = ds->curr.val;
424 }
425 }
426 break;
427 default:
428 ;
429 }
430
431 sym->curr = newval;
432 if (sym_is_choice(sym) && newval.tri == yes)
433 sym->curr.val = sym_calc_choice(sym);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800434 sym_validate_range(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Roman Zippelface4372006-06-08 22:12:45 -0700436 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 sym_set_changed(sym);
Roman Zippelface4372006-06-08 22:12:45 -0700438 if (modules_sym == sym) {
439 sym_set_all_changed();
440 modules_val = modules_sym->curr.tri;
441 }
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if (sym_is_choice(sym)) {
Roman Zippel7a962922008-01-14 04:50:23 +0100445 struct symbol *choice_sym;
Roman Zippel7a962922008-01-14 04:50:23 +0100446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 prop = sym_get_choice_prop(sym);
Roman Zippel7a962922008-01-14 04:50:23 +0100448 expr_list_for_each_sym(prop->expr, e, choice_sym) {
Jan Beulich0a28c472010-06-30 13:11:01 +0100449 if ((sym->flags & SYMBOL_WRITE) &&
450 choice_sym->visible != no)
451 choice_sym->flags |= SYMBOL_WRITE;
452 if (sym->flags & SYMBOL_CHANGED)
Roman Zippel7a962922008-01-14 04:50:23 +0100453 sym_set_changed(choice_sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455 }
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100456
Dirk Gouders693359f2018-07-03 14:43:31 +0200457 if (sym->flags & SYMBOL_NO_WRITE)
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100458 sym->flags &= ~SYMBOL_WRITE;
Arve Hjønnevågfbe98bb2013-06-06 20:37:00 -0700459
460 if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
461 set_all_choice_values(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464void sym_clear_all_valid(void)
465{
466 struct symbol *sym;
467 int i;
468
469 for_all_symbols(i, sym)
470 sym->flags &= ~SYMBOL_VALID;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800471 sym_add_change_count(1);
Markus Elfring35ffd082015-07-07 21:48:23 +0200472 sym_calc_value(modules_sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475bool sym_tristate_within_range(struct symbol *sym, tristate val)
476{
477 int type = sym_get_type(sym);
478
479 if (sym->visible == no)
480 return false;
481
482 if (type != S_BOOLEAN && type != S_TRISTATE)
483 return false;
484
485 if (type == S_BOOLEAN && val == mod)
486 return false;
487 if (sym->visible <= sym->rev_dep.tri)
488 return false;
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500489 if (sym->implied.tri == yes && val == mod)
490 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 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 Zippel669bfad2006-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 Zippel669bfad2006-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 Zippel669bfad2006-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 Zippel669bfad2006-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;
760 case S_OTHER:
761 case S_UNKNOWN:
762 break;
763 }
764 return "";
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767const char *sym_get_string_value(struct symbol *sym)
768{
769 tristate val;
770
771 switch (sym->type) {
772 case S_BOOLEAN:
773 case S_TRISTATE:
774 val = sym_get_tristate_value(sym);
775 switch (val) {
776 case no:
777 return "n";
778 case mod:
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400779 sym_calc_value(modules_sym);
780 return (modules_sym->curr.tri == no) ? "n" : "m";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 case yes:
782 return "y";
783 }
784 break;
785 default:
786 ;
787 }
788 return (const char *)sym->curr.val;
789}
790
791bool sym_is_changable(struct symbol *sym)
792{
793 return sym->visible > sym->rev_dep.tri;
794}
795
Andi Kleene66f25d2010-01-13 17:02:44 +0100796static unsigned strhash(const char *s)
797{
798 /* fnv32 hash */
799 unsigned hash = 2166136261U;
800 for (; *s; s++)
801 hash = (hash ^ *s) * 0x01000193;
802 return hash;
803}
804
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100805struct symbol *sym_lookup(const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 struct symbol *symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 char *new_name;
Andi Kleene66f25d2010-01-13 17:02:44 +0100809 int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (name) {
812 if (name[0] && !name[1]) {
813 switch (name[0]) {
814 case 'y': return &symbol_yes;
815 case 'm': return &symbol_mod;
816 case 'n': return &symbol_no;
817 }
818 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100819 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100822 if (symbol->name &&
823 !strcmp(symbol->name, name) &&
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100824 (flags ? symbol->flags & flags
825 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
826 return symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900828 new_name = xstrdup(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 } else {
830 new_name = NULL;
Andi Kleene66f25d2010-01-13 17:02:44 +0100831 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
Alan Cox177acf72012-11-06 14:32:08 +0000834 symbol = xmalloc(sizeof(*symbol));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 memset(symbol, 0, sizeof(*symbol));
836 symbol->name = new_name;
837 symbol->type = S_UNKNOWN;
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100838 symbol->flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 symbol->next = symbol_hash[hash];
841 symbol_hash[hash] = symbol;
842
843 return symbol;
844}
845
846struct symbol *sym_find(const char *name)
847{
848 struct symbol *symbol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 int hash = 0;
850
851 if (!name)
852 return NULL;
853
854 if (name[0] && !name[1]) {
855 switch (name[0]) {
856 case 'y': return &symbol_yes;
857 case 'm': return &symbol_mod;
858 case 'n': return &symbol_no;
859 }
860 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100861 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100864 if (symbol->name &&
865 !strcmp(symbol->name, name) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 !(symbol->flags & SYMBOL_CONST))
867 break;
868 }
869
870 return symbol;
871}
872
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400873const char *sym_escape_string_value(const char *in)
874{
875 const char *p;
876 size_t reslen;
877 char *res;
878 size_t l;
879
880 reslen = strlen(in) + strlen("\"\"") + 1;
881
882 p = in;
883 for (;;) {
884 l = strcspn(p, "\"\\");
885 p += l;
886
887 if (p[0] == '\0')
888 break;
889
890 reslen++;
891 p++;
892 }
893
Alan Cox177acf72012-11-06 14:32:08 +0000894 res = xmalloc(reslen);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400895 res[0] = '\0';
896
897 strcat(res, "\"");
898
899 p = in;
900 for (;;) {
901 l = strcspn(p, "\"\\");
902 strncat(res, p, l);
903 p += l;
904
905 if (p[0] == '\0')
906 break;
907
908 strcat(res, "\\");
909 strncat(res, p++, 1);
910 }
911
912 strcat(res, "\"");
913 return res;
914}
915
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200916struct sym_match {
917 struct symbol *sym;
918 off_t so, eo;
919};
920
921/* Compare matched symbols as thus:
922 * - first, symbols that match exactly
923 * - then, alphabetical sort
924 */
Yann E. MORIN803b3512013-07-16 20:28:51 +0200925static int sym_rel_comp(const void *sym1, const void *sym2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200926{
Yann E. MORIN508382a2013-07-16 20:39:42 +0200927 const struct sym_match *s1 = sym1;
928 const struct sym_match *s2 = sym2;
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200929 int exact1, exact2;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200930
931 /* Exact match:
932 * - if matched length on symbol s1 is the length of that symbol,
933 * then this symbol should come first;
934 * - if matched length on symbol s2 is the length of that symbol,
935 * then this symbol should come first.
936 * Note: since the search can be a regexp, both symbols may match
937 * exactly; if this is the case, we can't decide which comes first,
938 * and we fallback to sorting alphabetically.
939 */
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200940 exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
941 exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
942 if (exact1 && !exact2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200943 return -1;
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200944 if (!exact1 && exact2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200945 return 1;
946
947 /* As a fallback, sort symbols alphabetically */
948 return strcmp(s1->sym->name, s2->sym->name);
949}
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951struct symbol **sym_re_search(const char *pattern)
952{
953 struct symbol *sym, **sym_arr = NULL;
Yann E. MORIN508382a2013-07-16 20:39:42 +0200954 struct sym_match *sym_match_arr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int i, cnt, size;
956 regex_t re;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200957 regmatch_t match[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 cnt = size = 0;
960 /* Skip if empty */
961 if (strlen(pattern) == 0)
962 return NULL;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200963 if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return NULL;
965
966 for_all_symbols(i, sym) {
967 if (sym->flags & SYMBOL_CONST || !sym->name)
968 continue;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200969 if (regexec(&re, sym->name, 1, match, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 continue;
Yann E. MORIN1407f972013-07-16 20:32:33 +0200971 if (cnt >= size) {
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200972 void *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 size += 16;
Yann E. MORIN508382a2013-07-16 20:39:42 +0200974 tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
Yann E. MORIN803b3512013-07-16 20:28:51 +0200975 if (!tmp)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200976 goto sym_re_search_free;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200977 sym_match_arr = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Li Zefanda6df872010-03-19 14:57:47 +0800979 sym_calc_value(sym);
Yann E. MORIN803b3512013-07-16 20:28:51 +0200980 /* As regexec returned 0, we know we have a match, so
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200981 * we can use match[0].rm_[se]o without further checks
982 */
Yann E. MORIN508382a2013-07-16 20:39:42 +0200983 sym_match_arr[cnt].so = match[0].rm_so;
984 sym_match_arr[cnt].eo = match[0].rm_eo;
985 sym_match_arr[cnt++].sym = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200987 if (sym_match_arr) {
Yann E. MORIN508382a2013-07-16 20:39:42 +0200988 qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
Heinrich Schuchardt88127da2017-11-08 22:09:59 +0100989 sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200990 if (!sym_arr)
991 goto sym_re_search_free;
992 for (i = 0; i < cnt; i++)
Yann E. MORIN508382a2013-07-16 20:39:42 +0200993 sym_arr[i] = sym_match_arr[i].sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 sym_arr[cnt] = NULL;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200995 }
996sym_re_search_free:
Yann E. MORIN508382a2013-07-16 20:39:42 +0200997 /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
998 free(sym_match_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 regfree(&re);
1000
1001 return sym_arr;
1002}
1003
Roman Zippeld595cea2010-07-31 23:35:30 +02001004/*
1005 * When we check for recursive dependencies we use a stack to save
1006 * current state so we can print out relevant info to user.
1007 * The entries are located on the call stack so no need to free memory.
Martin Walch8d9dfe82013-10-03 17:28:14 +02001008 * Note insert() remove() must always match to properly clear the stack.
Roman Zippeld595cea2010-07-31 23:35:30 +02001009 */
1010static struct dep_stack {
1011 struct dep_stack *prev, *next;
1012 struct symbol *sym;
1013 struct property *prop;
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001014 struct expr **expr;
Roman Zippeld595cea2010-07-31 23:35:30 +02001015} *check_top;
1016
1017static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
1018{
1019 memset(stack, 0, sizeof(*stack));
1020 if (check_top)
1021 check_top->next = stack;
1022 stack->prev = check_top;
1023 stack->sym = sym;
1024 check_top = stack;
1025}
1026
1027static void dep_stack_remove(void)
1028{
1029 check_top = check_top->prev;
1030 if (check_top)
1031 check_top->next = NULL;
1032}
1033
1034/*
1035 * Called when we have detected a recursive dependency.
1036 * check_top point to the top of the stact so we use
1037 * the ->prev pointer to locate the bottom of the stack.
1038 */
1039static void sym_check_print_recursive(struct symbol *last_sym)
1040{
1041 struct dep_stack *stack;
1042 struct symbol *sym, *next_sym;
1043 struct menu *menu = NULL;
1044 struct property *prop;
1045 struct dep_stack cv_stack;
1046
1047 if (sym_is_choice_value(last_sym)) {
1048 dep_stack_insert(&cv_stack, last_sym);
1049 last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1050 }
1051
1052 for (stack = check_top; stack != NULL; stack = stack->prev)
1053 if (stack->sym == last_sym)
1054 break;
1055 if (!stack) {
1056 fprintf(stderr, "unexpected recursive dependency error\n");
1057 return;
1058 }
1059
1060 for (; stack; stack = stack->next) {
1061 sym = stack->sym;
1062 next_sym = stack->next ? stack->next->sym : last_sym;
1063 prop = stack->prop;
Sam Ravnborg3643f842010-08-14 14:40:00 +02001064 if (prop == NULL)
1065 prop = stack->sym->prop;
Roman Zippeld595cea2010-07-31 23:35:30 +02001066
1067 /* for choice values find the menu entry (used below) */
1068 if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1069 for (prop = sym->prop; prop; prop = prop->next) {
1070 menu = prop->menu;
1071 if (prop->menu)
1072 break;
1073 }
1074 }
1075 if (stack->sym == last_sym)
1076 fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1077 prop->file->name, prop->lineno);
Masahiro Yamadae3b03bf2017-12-16 00:28:42 +09001078
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001079 if (sym_is_choice(sym)) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001080 fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1081 menu->file->name, menu->lineno,
1082 sym->name ? sym->name : "<choice>",
1083 next_sym->name ? next_sym->name : "<choice>");
1084 } else if (sym_is_choice_value(sym)) {
1085 fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1086 menu->file->name, menu->lineno,
1087 sym->name ? sym->name : "<choice>",
1088 next_sym->name ? next_sym->name : "<choice>");
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001089 } else if (stack->expr == &sym->dir_dep.expr) {
1090 fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
Roman Zippeld595cea2010-07-31 23:35:30 +02001091 prop->file->name, prop->lineno,
1092 sym->name ? sym->name : "<choice>",
1093 next_sym->name ? next_sym->name : "<choice>");
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001094 } else if (stack->expr == &sym->rev_dep.expr) {
1095 fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1096 prop->file->name, prop->lineno,
1097 sym->name ? sym->name : "<choice>",
1098 next_sym->name ? next_sym->name : "<choice>");
1099 } else if (stack->expr == &sym->implied.expr) {
1100 fprintf(stderr, "%s:%d:\tsymbol %s is implied by %s\n",
1101 prop->file->name, prop->lineno,
1102 sym->name ? sym->name : "<choice>",
1103 next_sym->name ? next_sym->name : "<choice>");
1104 } else if (stack->expr) {
1105 fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1106 prop->file->name, prop->lineno,
1107 sym->name ? sym->name : "<choice>",
1108 prop_get_type_name(prop->type),
1109 next_sym->name ? next_sym->name : "<choice>");
1110 } else {
1111 fprintf(stderr, "%s:%d:\tsymbol %s %s is visible depending on %s\n",
1112 prop->file->name, prop->lineno,
1113 sym->name ? sym->name : "<choice>",
1114 prop_get_type_name(prop->type),
1115 next_sym->name ? next_sym->name : "<choice>");
Roman Zippeld595cea2010-07-31 23:35:30 +02001116 }
1117 }
1118
Masahiro Yamadae3b03bf2017-12-16 00:28:42 +09001119 fprintf(stderr,
1120 "For a resolution refer to Documentation/kbuild/kconfig-language.txt\n"
1121 "subsection \"Kconfig recursive dependency limitations\"\n"
1122 "\n");
1123
Roman Zippeld595cea2010-07-31 23:35:30 +02001124 if (check_top == &cv_stack)
1125 dep_stack_remove();
1126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128static struct symbol *sym_check_expr_deps(struct expr *e)
1129{
1130 struct symbol *sym;
1131
1132 if (!e)
1133 return NULL;
1134 switch (e->type) {
1135 case E_OR:
1136 case E_AND:
1137 sym = sym_check_expr_deps(e->left.expr);
1138 if (sym)
1139 return sym;
1140 return sym_check_expr_deps(e->right.expr);
1141 case E_NOT:
1142 return sym_check_expr_deps(e->left.expr);
1143 case E_EQUAL:
Jan Beulich31847b62015-06-15 13:00:21 +01001144 case E_GEQ:
1145 case E_GTH:
1146 case E_LEQ:
1147 case E_LTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 case E_UNEQUAL:
1149 sym = sym_check_deps(e->left.sym);
1150 if (sym)
1151 return sym;
1152 return sym_check_deps(e->right.sym);
1153 case E_SYMBOL:
1154 return sym_check_deps(e->left.sym);
1155 default:
1156 break;
1157 }
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +09001158 fprintf(stderr, "Oops! How to check %d?\n", e->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return NULL;
1160}
1161
Sam Ravnborg5447d342007-05-06 09:20:10 +02001162/* return NULL when dependencies are OK */
Roman Zippel48981172008-02-29 05:10:24 +01001163static struct symbol *sym_check_sym_deps(struct symbol *sym)
1164{
1165 struct symbol *sym2;
1166 struct property *prop;
Roman Zippeld595cea2010-07-31 23:35:30 +02001167 struct dep_stack stack;
1168
1169 dep_stack_insert(&stack, sym);
Roman Zippel48981172008-02-29 05:10:24 +01001170
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001171 stack.expr = &sym->dir_dep.expr;
1172 sym2 = sym_check_expr_deps(sym->dir_dep.expr);
1173 if (sym2)
1174 goto out;
1175
1176 stack.expr = &sym->rev_dep.expr;
Roman Zippel48981172008-02-29 05:10:24 +01001177 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1178 if (sym2)
Roman Zippeld595cea2010-07-31 23:35:30 +02001179 goto out;
Roman Zippel48981172008-02-29 05:10:24 +01001180
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001181 stack.expr = &sym->implied.expr;
Masahiro Yamada5e8c5292018-08-15 14:59:44 +09001182 sym2 = sym_check_expr_deps(sym->implied.expr);
1183 if (sym2)
1184 goto out;
1185
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001186 stack.expr = NULL;
1187
Roman Zippel48981172008-02-29 05:10:24 +01001188 for (prop = sym->prop; prop; prop = prop->next) {
Masahiro Yamada5e8c5292018-08-15 14:59:44 +09001189 if (prop->type == P_CHOICE || prop->type == P_SELECT ||
1190 prop->type == P_IMPLY)
Roman Zippel48981172008-02-29 05:10:24 +01001191 continue;
Roman Zippeld595cea2010-07-31 23:35:30 +02001192 stack.prop = prop;
Roman Zippel48981172008-02-29 05:10:24 +01001193 sym2 = sym_check_expr_deps(prop->visible.expr);
1194 if (sym2)
1195 break;
1196 if (prop->type != P_DEFAULT || sym_is_choice(sym))
1197 continue;
Masahiro Yamadaf4989262018-08-15 14:59:45 +09001198 stack.expr = &prop->expr;
Roman Zippel48981172008-02-29 05:10:24 +01001199 sym2 = sym_check_expr_deps(prop->expr);
1200 if (sym2)
1201 break;
Roman Zippeld595cea2010-07-31 23:35:30 +02001202 stack.expr = NULL;
Roman Zippel48981172008-02-29 05:10:24 +01001203 }
1204
Roman Zippeld595cea2010-07-31 23:35:30 +02001205out:
1206 dep_stack_remove();
1207
Roman Zippel48981172008-02-29 05:10:24 +01001208 return sym2;
1209}
1210
1211static struct symbol *sym_check_choice_deps(struct symbol *choice)
1212{
1213 struct symbol *sym, *sym2;
1214 struct property *prop;
1215 struct expr *e;
Roman Zippeld595cea2010-07-31 23:35:30 +02001216 struct dep_stack stack;
1217
1218 dep_stack_insert(&stack, choice);
Roman Zippel48981172008-02-29 05:10:24 +01001219
1220 prop = sym_get_choice_prop(choice);
1221 expr_list_for_each_sym(prop->expr, e, sym)
1222 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1223
1224 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1225 sym2 = sym_check_sym_deps(choice);
1226 choice->flags &= ~SYMBOL_CHECK;
1227 if (sym2)
1228 goto out;
1229
1230 expr_list_for_each_sym(prop->expr, e, sym) {
1231 sym2 = sym_check_sym_deps(sym);
Roman Zippeld595cea2010-07-31 23:35:30 +02001232 if (sym2)
Roman Zippel48981172008-02-29 05:10:24 +01001233 break;
Roman Zippel48981172008-02-29 05:10:24 +01001234 }
1235out:
1236 expr_list_for_each_sym(prop->expr, e, sym)
1237 sym->flags &= ~SYMBOL_CHECK;
1238
1239 if (sym2 && sym_is_choice_value(sym2) &&
1240 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1241 sym2 = choice;
1242
Roman Zippeld595cea2010-07-31 23:35:30 +02001243 dep_stack_remove();
1244
Roman Zippel48981172008-02-29 05:10:24 +01001245 return sym2;
1246}
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248struct symbol *sym_check_deps(struct symbol *sym)
1249{
1250 struct symbol *sym2;
1251 struct property *prop;
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (sym->flags & SYMBOL_CHECK) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001254 sym_check_print_recursive(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return sym;
1256 }
David Gibson3f04e7d2005-11-08 21:34:46 -08001257 if (sym->flags & SYMBOL_CHECKED)
1258 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Roman Zippel48981172008-02-29 05:10:24 +01001260 if (sym_is_choice_value(sym)) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001261 struct dep_stack stack;
1262
Roman Zippel48981172008-02-29 05:10:24 +01001263 /* for choice groups start the check with main choice symbol */
Roman Zippeld595cea2010-07-31 23:35:30 +02001264 dep_stack_insert(&stack, sym);
Roman Zippel48981172008-02-29 05:10:24 +01001265 prop = sym_get_choice_prop(sym);
1266 sym2 = sym_check_deps(prop_get_symbol(prop));
Roman Zippeld595cea2010-07-31 23:35:30 +02001267 dep_stack_remove();
Roman Zippel48981172008-02-29 05:10:24 +01001268 } else if (sym_is_choice(sym)) {
1269 sym2 = sym_check_choice_deps(sym);
1270 } else {
1271 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1272 sym2 = sym_check_sym_deps(sym);
1273 sym->flags &= ~SYMBOL_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Roman Zippel48981172008-02-29 05:10:24 +01001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return sym2;
1277}
1278
1279struct property *prop_alloc(enum prop_type type, struct symbol *sym)
1280{
1281 struct property *prop;
1282 struct property **propp;
1283
Alan Cox177acf72012-11-06 14:32:08 +00001284 prop = xmalloc(sizeof(*prop));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 memset(prop, 0, sizeof(*prop));
1286 prop->type = type;
1287 prop->sym = sym;
1288 prop->file = current_file;
1289 prop->lineno = zconf_lineno();
1290
1291 /* append property to the prop list of symbol */
1292 if (sym) {
1293 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
1294 ;
1295 *propp = prop;
1296 }
1297
1298 return prop;
1299}
1300
1301struct symbol *prop_get_symbol(struct property *prop)
1302{
1303 if (prop->expr && (prop->expr->type == E_SYMBOL ||
Roman Zippel7a962922008-01-14 04:50:23 +01001304 prop->expr->type == E_LIST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return prop->expr->left.sym;
1306 return NULL;
1307}
1308
1309const char *prop_get_type_name(enum prop_type type)
1310{
1311 switch (type) {
1312 case P_PROMPT:
1313 return "prompt";
1314 case P_COMMENT:
1315 return "comment";
1316 case P_MENU:
1317 return "menu";
1318 case P_DEFAULT:
1319 return "default";
1320 case P_CHOICE:
1321 return "choice";
1322 case P_SELECT:
1323 return "select";
Nicolas Pitre237e3ad2016-11-11 00:10:05 -05001324 case P_IMPLY:
1325 return "imply";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 case P_RANGE:
1327 return "range";
Sam Ravnborg59e89e32010-07-31 23:35:29 +02001328 case P_SYMBOL:
1329 return "symbol";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 case P_UNKNOWN:
1331 break;
1332 }
1333 return "unknown";
1334}