blob: 7c9a88e91cfad95f8fa2e350c2dcccb92981d36f [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
Roman Zippel93449082008-01-14 04:50:54 +010079struct property *sym_get_env_prop(struct symbol *sym)
80{
81 struct property *prop;
82
83 for_all_properties(sym, prop, P_ENV)
84 return prop;
85 return NULL;
86}
87
Michal Marekad8d40c2015-02-24 16:37:13 +010088static struct property *sym_get_default_prop(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 struct property *prop;
91
92 for_all_defaults(sym, prop) {
93 prop->visible.tri = expr_calc_value(prop->visible.expr);
94 if (prop->visible.tri != no)
95 return prop;
96 }
97 return NULL;
98}
99
Trevor Keith4356f482009-09-18 12:49:23 -0700100static struct property *sym_get_range_prop(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct property *prop;
103
104 for_all_properties(sym, prop, P_RANGE) {
105 prop->visible.tri = expr_calc_value(prop->visible.expr);
106 if (prop->visible.tri != no)
107 return prop;
108 }
109 return NULL;
110}
111
Kees Cook129784a2013-07-18 11:32:01 -0700112static long long sym_get_range_val(struct symbol *sym, int base)
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800113{
114 sym_calc_value(sym);
115 switch (sym->type) {
116 case S_INT:
117 base = 10;
118 break;
119 case S_HEX:
120 base = 16;
121 break;
122 default:
123 break;
124 }
Kees Cook129784a2013-07-18 11:32:01 -0700125 return strtoll(sym->curr.val, NULL, base);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800126}
127
128static void sym_validate_range(struct symbol *sym)
129{
130 struct property *prop;
Kees Cook129784a2013-07-18 11:32:01 -0700131 int base;
132 long long val, val2;
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800133 char str[64];
134
135 switch (sym->type) {
136 case S_INT:
137 base = 10;
138 break;
139 case S_HEX:
140 base = 16;
141 break;
142 default:
143 return;
144 }
145 prop = sym_get_range_prop(sym);
146 if (!prop)
147 return;
Kees Cook129784a2013-07-18 11:32:01 -0700148 val = strtoll(sym->curr.val, NULL, base);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800149 val2 = sym_get_range_val(prop->expr->left.sym, base);
150 if (val >= val2) {
151 val2 = sym_get_range_val(prop->expr->right.sym, base);
152 if (val <= val2)
153 return;
154 }
155 if (sym->type == S_INT)
Kees Cook129784a2013-07-18 11:32:01 -0700156 sprintf(str, "%lld", val2);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800157 else
Kees Cook129784a2013-07-18 11:32:01 -0700158 sprintf(str, "0x%llx", val2);
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900159 sym->curr.val = xstrdup(str);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800160}
161
Michal Marekad8d40c2015-02-24 16:37:13 +0100162static void sym_set_changed(struct symbol *sym)
163{
164 struct property *prop;
165
166 sym->flags |= SYMBOL_CHANGED;
167 for (prop = sym->prop; prop; prop = prop->next) {
168 if (prop->menu)
169 prop->menu->flags |= MENU_CHANGED;
170 }
171}
172
173static void sym_set_all_changed(void)
174{
175 struct symbol *sym;
176 int i;
177
178 for_all_symbols(i, sym)
179 sym_set_changed(sym);
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static void sym_calc_visibility(struct symbol *sym)
183{
184 struct property *prop;
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200185 struct symbol *choice_sym = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 tristate tri;
187
188 /* any prompt visible? */
189 tri = no;
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200190
191 if (sym_is_choice_value(sym))
192 choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 for_all_prompts(sym, prop) {
195 prop->visible.tri = expr_calc_value(prop->visible.expr);
Dirk Goudersfa64e5f2016-04-29 10:24:52 +0200196 /*
197 * Tristate choice_values with visibility 'mod' are
198 * not visible if the corresponding choice's value is
199 * 'yes'.
200 */
201 if (choice_sym && sym->type == S_TRISTATE &&
202 prop->visible.tri == mod && choice_sym->curr.tri == yes)
203 prop->visible.tri = no;
204
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100205 tri = EXPR_OR(tri, prop->visible.tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
208 tri = yes;
209 if (sym->visible != tri) {
210 sym->visible = tri;
211 sym_set_changed(sym);
212 }
213 if (sym_is_choice_value(sym))
214 return;
Catalin Marinas246cf9c2010-06-08 17:25:57 +0100215 /* defaulting to "yes" if no explicit "depends on" are given */
216 tri = yes;
217 if (sym->dir_dep.expr)
218 tri = expr_calc_value(sym->dir_dep.expr);
Masahiro Yamadaf622f822018-03-13 18:56:07 +0900219 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
Catalin Marinas246cf9c2010-06-08 17:25:57 +0100220 tri = yes;
221 if (sym->dir_dep.tri != tri) {
222 sym->dir_dep.tri = tri;
223 sym_set_changed(sym);
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 tri = no;
226 if (sym->rev_dep.expr)
227 tri = expr_calc_value(sym->rev_dep.expr);
228 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
229 tri = yes;
230 if (sym->rev_dep.tri != tri) {
231 sym->rev_dep.tri = tri;
232 sym_set_changed(sym);
233 }
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500234 tri = no;
235 if (sym->implied.expr && sym->dir_dep.tri != no)
236 tri = expr_calc_value(sym->implied.expr);
237 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
238 tri = yes;
239 if (sym->implied.tri != tri) {
240 sym->implied.tri = tri;
241 sym_set_changed(sym);
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Sam Ravnborgc2521472010-07-31 23:35:32 +0200245/*
246 * Find the default symbol for a choice.
247 * First try the default values for the choice symbol
248 * Next locate the first visible choice value
249 * Return NULL if none was found
250 */
251struct symbol *sym_choice_default(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct symbol *def_sym;
254 struct property *prop;
255 struct expr *e;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* any of the defaults visible? */
258 for_all_defaults(sym, prop) {
259 prop->visible.tri = expr_calc_value(prop->visible.expr);
260 if (prop->visible.tri == no)
261 continue;
262 def_sym = prop_get_symbol(prop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (def_sym->visible != no)
264 return def_sym;
265 }
266
267 /* just get the first visible value */
268 prop = sym_get_choice_prop(sym);
Jan Beulich0a28c472010-06-30 13:11:01 +0100269 expr_list_for_each_sym(prop->expr, e, def_sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (def_sym->visible != no)
271 return def_sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Sam Ravnborgc2521472010-07-31 23:35:32 +0200273 /* failed to locate any defaults */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return NULL;
275}
276
Sam Ravnborgc2521472010-07-31 23:35:32 +0200277static struct symbol *sym_calc_choice(struct symbol *sym)
278{
279 struct symbol *def_sym;
280 struct property *prop;
281 struct expr *e;
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500282 int flags;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200283
284 /* first calculate all choice values' visibilities */
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500285 flags = sym->flags;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200286 prop = sym_get_choice_prop(sym);
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500287 expr_list_for_each_sym(prop->expr, e, def_sym) {
Sam Ravnborgc2521472010-07-31 23:35:32 +0200288 sym_calc_visibility(def_sym);
Arnaud Lacombe5d095982012-01-23 17:29:05 -0500289 if (def_sym->visible != no)
290 flags &= def_sym->flags;
291 }
292
293 sym->flags &= flags | ~SYMBOL_DEF_USER;
Sam Ravnborgc2521472010-07-31 23:35:32 +0200294
295 /* is the user choice visible? */
296 def_sym = sym->def[S_DEF_USER].val;
297 if (def_sym && def_sym->visible != no)
298 return def_sym;
299
300 def_sym = sym_choice_default(sym);
301
302 if (def_sym == NULL)
303 /* no choice? reset tristate value */
304 sym->curr.tri = no;
305
306 return def_sym;
307}
308
Masahiro Yamadaf8f69dc2018-03-13 18:56:08 +0900309static void sym_warn_unmet_dep(struct symbol *sym)
310{
311 struct gstr gs = str_new();
312
313 str_printf(&gs,
314 "\nWARNING: unmet direct dependencies detected for %s\n",
315 sym->name);
316 str_printf(&gs,
317 " Depends on [%c]: ",
318 sym->dir_dep.tri == mod ? 'm' : 'n');
319 expr_gstr_print(sym->dir_dep.expr, &gs);
320 str_printf(&gs, "\n");
321
322 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes,
323 " Selected by [y]:\n");
324 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod,
325 " Selected by [m]:\n");
326
327 fputs(str_get(&gs), stderr);
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330void sym_calc_value(struct symbol *sym)
331{
332 struct symbol_value newval, oldval;
333 struct property *prop;
334 struct expr *e;
335
336 if (!sym)
337 return;
338
339 if (sym->flags & SYMBOL_VALID)
340 return;
Arve Hjønnevågfbe98bb2013-06-06 20:37:00 -0700341
342 if (sym_is_choice_value(sym) &&
343 sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
344 sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
345 prop = sym_get_choice_prop(sym);
346 sym_calc_value(prop_get_symbol(prop));
347 }
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 sym->flags |= SYMBOL_VALID;
350
351 oldval = sym->curr;
352
353 switch (sym->type) {
354 case S_INT:
355 case S_HEX:
356 case S_STRING:
357 newval = symbol_empty.curr;
358 break;
359 case S_BOOLEAN:
360 case S_TRISTATE:
361 newval = symbol_no.curr;
362 break;
363 default:
364 sym->curr.val = sym->name;
365 sym->curr.tri = no;
366 return;
367 }
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900368 sym->flags &= ~SYMBOL_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 sym_calc_visibility(sym);
371
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900372 if (sym->visible != no)
373 sym->flags |= SYMBOL_WRITE;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* set default if recursively called */
376 sym->curr = newval;
377
378 switch (sym_get_type(sym)) {
379 case S_BOOLEAN:
380 case S_TRISTATE:
381 if (sym_is_choice_value(sym) && sym->visible == yes) {
382 prop = sym_get_choice_prop(sym);
383 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
Roman Zippel587c9062008-02-11 21:13:47 +0100384 } else {
385 if (sym->visible != no) {
386 /* if the symbol is visible use the user value
387 * if available, otherwise try the default value
388 */
Roman Zippel587c9062008-02-11 21:13:47 +0100389 if (sym_has_value(sym)) {
390 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
391 sym->visible);
392 goto calc_newval;
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Roman Zippel587c9062008-02-11 21:13:47 +0100395 if (sym->rev_dep.tri != no)
396 sym->flags |= SYMBOL_WRITE;
397 if (!sym_is_choice(sym)) {
398 prop = sym_get_default_prop(sym);
399 if (prop) {
Roman Zippel587c9062008-02-11 21:13:47 +0100400 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
401 prop->visible.tri);
Ulf Magnussonf467c562018-02-23 12:49:01 +0100402 if (newval.tri != no)
403 sym->flags |= SYMBOL_WRITE;
Roman Zippel587c9062008-02-11 21:13:47 +0100404 }
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500405 if (sym->implied.tri != no) {
406 sym->flags |= SYMBOL_WRITE;
407 newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
408 }
Roman Zippel587c9062008-02-11 21:13:47 +0100409 }
410 calc_newval:
Masahiro Yamadaf8f69dc2018-03-13 18:56:08 +0900411 if (sym->dir_dep.tri < sym->rev_dep.tri)
412 sym_warn_unmet_dep(sym);
Roman Zippel587c9062008-02-11 21:13:47 +0100413 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500415 if (newval.tri == mod &&
416 (sym_get_type(sym) == S_BOOLEAN || sym->implied.tri == yes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 newval.tri = yes;
418 break;
419 case S_STRING:
420 case S_HEX:
421 case S_INT:
Masahiro Yamadacb67ab22018-02-06 09:34:42 +0900422 if (sym->visible != no && sym_has_value(sym)) {
423 newval.val = sym->def[S_DEF_USER].val;
424 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 prop = sym_get_default_prop(sym);
427 if (prop) {
428 struct symbol *ds = prop_get_symbol(prop);
429 if (ds) {
430 sym->flags |= SYMBOL_WRITE;
431 sym_calc_value(ds);
432 newval.val = ds->curr.val;
433 }
434 }
435 break;
436 default:
437 ;
438 }
439
440 sym->curr = newval;
441 if (sym_is_choice(sym) && newval.tri == yes)
442 sym->curr.val = sym_calc_choice(sym);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800443 sym_validate_range(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Roman Zippelface4372006-06-08 22:12:45 -0700445 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 sym_set_changed(sym);
Roman Zippelface4372006-06-08 22:12:45 -0700447 if (modules_sym == sym) {
448 sym_set_all_changed();
449 modules_val = modules_sym->curr.tri;
450 }
451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (sym_is_choice(sym)) {
Roman Zippel7a962922008-01-14 04:50:23 +0100454 struct symbol *choice_sym;
Roman Zippel7a962922008-01-14 04:50:23 +0100455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 prop = sym_get_choice_prop(sym);
Roman Zippel7a962922008-01-14 04:50:23 +0100457 expr_list_for_each_sym(prop->expr, e, choice_sym) {
Jan Beulich0a28c472010-06-30 13:11:01 +0100458 if ((sym->flags & SYMBOL_WRITE) &&
459 choice_sym->visible != no)
460 choice_sym->flags |= SYMBOL_WRITE;
461 if (sym->flags & SYMBOL_CHANGED)
Roman Zippel7a962922008-01-14 04:50:23 +0100462 sym_set_changed(choice_sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464 }
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100465
466 if (sym->flags & SYMBOL_AUTO)
467 sym->flags &= ~SYMBOL_WRITE;
Arve Hjønnevågfbe98bb2013-06-06 20:37:00 -0700468
469 if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
470 set_all_choice_values(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473void sym_clear_all_valid(void)
474{
475 struct symbol *sym;
476 int i;
477
478 for_all_symbols(i, sym)
479 sym->flags &= ~SYMBOL_VALID;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800480 sym_add_change_count(1);
Markus Elfring35ffd082015-07-07 21:48:23 +0200481 sym_calc_value(modules_sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484bool sym_tristate_within_range(struct symbol *sym, tristate val)
485{
486 int type = sym_get_type(sym);
487
488 if (sym->visible == no)
489 return false;
490
491 if (type != S_BOOLEAN && type != S_TRISTATE)
492 return false;
493
494 if (type == S_BOOLEAN && val == mod)
495 return false;
496 if (sym->visible <= sym->rev_dep.tri)
497 return false;
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500498 if (sym->implied.tri == yes && val == mod)
499 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (sym_is_choice_value(sym) && sym->visible == yes)
501 return val == yes;
502 return val >= sym->rev_dep.tri && val <= sym->visible;
503}
504
505bool sym_set_tristate_value(struct symbol *sym, tristate val)
506{
507 tristate oldval = sym_get_tristate_value(sym);
508
509 if (oldval != val && !sym_tristate_within_range(sym, val))
510 return false;
511
Roman Zippel669bfad2006-06-08 22:12:42 -0700512 if (!(sym->flags & SYMBOL_DEF_USER)) {
513 sym->flags |= SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 sym_set_changed(sym);
515 }
Roman Zippel3f23ca22005-11-08 21:34:48 -0800516 /*
517 * setting a choice value also resets the new flag of the choice
518 * symbol and all other choice values.
519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (sym_is_choice_value(sym) && val == yes) {
521 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel3f23ca22005-11-08 21:34:48 -0800522 struct property *prop;
523 struct expr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Roman Zippel0c1822e2006-06-08 22:12:41 -0700525 cs->def[S_DEF_USER].val = sym;
Roman Zippel669bfad2006-06-08 22:12:42 -0700526 cs->flags |= SYMBOL_DEF_USER;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800527 prop = sym_get_choice_prop(cs);
528 for (e = prop->expr; e; e = e->left.expr) {
529 if (e->right.sym->visible != no)
Roman Zippel669bfad2006-06-08 22:12:42 -0700530 e->right.sym->flags |= SYMBOL_DEF_USER;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
Roman Zippel0c1822e2006-06-08 22:12:41 -0700534 sym->def[S_DEF_USER].tri = val;
Roman Zippelface4372006-06-08 22:12:45 -0700535 if (oldval != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 sym_clear_all_valid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 return true;
539}
540
541tristate sym_toggle_tristate_value(struct symbol *sym)
542{
543 tristate oldval, newval;
544
545 oldval = newval = sym_get_tristate_value(sym);
546 do {
547 switch (newval) {
548 case no:
549 newval = mod;
550 break;
551 case mod:
552 newval = yes;
553 break;
554 case yes:
555 newval = no;
556 break;
557 }
558 if (sym_set_tristate_value(sym, newval))
559 break;
560 } while (oldval != newval);
561 return newval;
562}
563
564bool sym_string_valid(struct symbol *sym, const char *str)
565{
566 signed char ch;
567
568 switch (sym->type) {
569 case S_STRING:
570 return true;
571 case S_INT:
572 ch = *str++;
573 if (ch == '-')
574 ch = *str++;
575 if (!isdigit(ch))
576 return false;
577 if (ch == '0' && *str != 0)
578 return false;
579 while ((ch = *str++)) {
580 if (!isdigit(ch))
581 return false;
582 }
583 return true;
584 case S_HEX:
585 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
586 str += 2;
587 ch = *str++;
588 do {
589 if (!isxdigit(ch))
590 return false;
591 } while ((ch = *str++));
592 return true;
593 case S_BOOLEAN:
594 case S_TRISTATE:
595 switch (str[0]) {
596 case 'y': case 'Y':
597 case 'm': case 'M':
598 case 'n': case 'N':
599 return true;
600 }
601 return false;
602 default:
603 return false;
604 }
605}
606
607bool sym_string_within_range(struct symbol *sym, const char *str)
608{
609 struct property *prop;
Kees Cook129784a2013-07-18 11:32:01 -0700610 long long val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 switch (sym->type) {
613 case S_STRING:
614 return sym_string_valid(sym, str);
615 case S_INT:
616 if (!sym_string_valid(sym, str))
617 return false;
618 prop = sym_get_range_prop(sym);
619 if (!prop)
620 return true;
Kees Cook129784a2013-07-18 11:32:01 -0700621 val = strtoll(str, NULL, 10);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800622 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
623 val <= sym_get_range_val(prop->expr->right.sym, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 case S_HEX:
625 if (!sym_string_valid(sym, str))
626 return false;
627 prop = sym_get_range_prop(sym);
628 if (!prop)
629 return true;
Kees Cook129784a2013-07-18 11:32:01 -0700630 val = strtoll(str, NULL, 16);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800631 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
632 val <= sym_get_range_val(prop->expr->right.sym, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 case S_BOOLEAN:
634 case S_TRISTATE:
635 switch (str[0]) {
636 case 'y': case 'Y':
637 return sym_tristate_within_range(sym, yes);
638 case 'm': case 'M':
639 return sym_tristate_within_range(sym, mod);
640 case 'n': case 'N':
641 return sym_tristate_within_range(sym, no);
642 }
643 return false;
644 default:
645 return false;
646 }
647}
648
649bool sym_set_string_value(struct symbol *sym, const char *newval)
650{
651 const char *oldval;
652 char *val;
653 int size;
654
655 switch (sym->type) {
656 case S_BOOLEAN:
657 case S_TRISTATE:
658 switch (newval[0]) {
659 case 'y': case 'Y':
660 return sym_set_tristate_value(sym, yes);
661 case 'm': case 'M':
662 return sym_set_tristate_value(sym, mod);
663 case 'n': case 'N':
664 return sym_set_tristate_value(sym, no);
665 }
666 return false;
667 default:
668 ;
669 }
670
671 if (!sym_string_within_range(sym, newval))
672 return false;
673
Roman Zippel669bfad2006-06-08 22:12:42 -0700674 if (!(sym->flags & SYMBOL_DEF_USER)) {
675 sym->flags |= SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 sym_set_changed(sym);
677 }
678
Roman Zippel0c1822e2006-06-08 22:12:41 -0700679 oldval = sym->def[S_DEF_USER].val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 size = strlen(newval) + 1;
681 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
682 size += 2;
Alan Cox177acf72012-11-06 14:32:08 +0000683 sym->def[S_DEF_USER].val = val = xmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 *val++ = '0';
685 *val++ = 'x';
686 } else if (!oldval || strcmp(oldval, newval))
Alan Cox177acf72012-11-06 14:32:08 +0000687 sym->def[S_DEF_USER].val = val = xmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 else
689 return true;
690
691 strcpy(val, newval);
692 free((void *)oldval);
693 sym_clear_all_valid();
694
695 return true;
696}
697
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200698/*
699 * Find the default value associated to a symbol.
700 * For tristate symbol handle the modules=n case
701 * in which case "m" becomes "y".
702 * If the symbol does not have any default then fallback
703 * to the fixed default values.
704 */
705const char *sym_get_string_default(struct symbol *sym)
706{
707 struct property *prop;
708 struct symbol *ds;
709 const char *str;
710 tristate val;
711
712 sym_calc_visibility(sym);
713 sym_calc_value(modules_sym);
714 val = symbol_no.curr.tri;
715 str = symbol_empty.curr.val;
716
717 /* If symbol has a default value look it up */
718 prop = sym_get_default_prop(sym);
719 if (prop != NULL) {
720 switch (sym->type) {
721 case S_BOOLEAN:
722 case S_TRISTATE:
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -0500723 /* The visibility may limit the value from yes => mod */
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200724 val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
725 break;
726 default:
727 /*
728 * The following fails to handle the situation
729 * where a default value is further limited by
730 * the valid range.
731 */
732 ds = prop_get_symbol(prop);
733 if (ds != NULL) {
734 sym_calc_value(ds);
735 str = (const char *)ds->curr.val;
736 }
737 }
738 }
739
740 /* Handle select statements */
741 val = EXPR_OR(val, sym->rev_dep.tri);
742
743 /* transpose mod to yes if modules are not enabled */
744 if (val == mod)
745 if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
746 val = yes;
747
748 /* transpose mod to yes if type is bool */
749 if (sym->type == S_BOOLEAN && val == mod)
750 val = yes;
751
Nicolas Pitre237e3ad2016-11-11 00:10:05 -0500752 /* adjust the default value if this symbol is implied by another */
753 if (val < sym->implied.tri)
754 val = sym->implied.tri;
755
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200756 switch (sym->type) {
757 case S_BOOLEAN:
758 case S_TRISTATE:
759 switch (val) {
760 case no: return "n";
761 case mod: return "m";
762 case yes: return "y";
763 }
764 case S_INT:
765 case S_HEX:
766 return str;
767 case S_STRING:
768 return str;
769 case S_OTHER:
770 case S_UNKNOWN:
771 break;
772 }
773 return "";
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776const char *sym_get_string_value(struct symbol *sym)
777{
778 tristate val;
779
780 switch (sym->type) {
781 case S_BOOLEAN:
782 case S_TRISTATE:
783 val = sym_get_tristate_value(sym);
784 switch (val) {
785 case no:
786 return "n";
787 case mod:
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400788 sym_calc_value(modules_sym);
789 return (modules_sym->curr.tri == no) ? "n" : "m";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 case yes:
791 return "y";
792 }
793 break;
794 default:
795 ;
796 }
797 return (const char *)sym->curr.val;
798}
799
800bool sym_is_changable(struct symbol *sym)
801{
802 return sym->visible > sym->rev_dep.tri;
803}
804
Andi Kleene66f25d2010-01-13 17:02:44 +0100805static unsigned strhash(const char *s)
806{
807 /* fnv32 hash */
808 unsigned hash = 2166136261U;
809 for (; *s; s++)
810 hash = (hash ^ *s) * 0x01000193;
811 return hash;
812}
813
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100814struct symbol *sym_lookup(const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 struct symbol *symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 char *new_name;
Andi Kleene66f25d2010-01-13 17:02:44 +0100818 int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (name) {
821 if (name[0] && !name[1]) {
822 switch (name[0]) {
823 case 'y': return &symbol_yes;
824 case 'm': return &symbol_mod;
825 case 'n': return &symbol_no;
826 }
827 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100828 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100831 if (symbol->name &&
832 !strcmp(symbol->name, name) &&
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100833 (flags ? symbol->flags & flags
834 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
835 return symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Masahiro Yamadacd81fc82018-02-17 03:38:31 +0900837 new_name = xstrdup(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 } else {
839 new_name = NULL;
Andi Kleene66f25d2010-01-13 17:02:44 +0100840 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
Alan Cox177acf72012-11-06 14:32:08 +0000843 symbol = xmalloc(sizeof(*symbol));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 memset(symbol, 0, sizeof(*symbol));
845 symbol->name = new_name;
846 symbol->type = S_UNKNOWN;
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100847 symbol->flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 symbol->next = symbol_hash[hash];
850 symbol_hash[hash] = symbol;
851
852 return symbol;
853}
854
855struct symbol *sym_find(const char *name)
856{
857 struct symbol *symbol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 int hash = 0;
859
860 if (!name)
861 return NULL;
862
863 if (name[0] && !name[1]) {
864 switch (name[0]) {
865 case 'y': return &symbol_yes;
866 case 'm': return &symbol_mod;
867 case 'n': return &symbol_no;
868 }
869 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100870 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100873 if (symbol->name &&
874 !strcmp(symbol->name, name) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 !(symbol->flags & SYMBOL_CONST))
876 break;
877 }
878
879 return symbol;
880}
881
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400882const char *sym_escape_string_value(const char *in)
883{
884 const char *p;
885 size_t reslen;
886 char *res;
887 size_t l;
888
889 reslen = strlen(in) + strlen("\"\"") + 1;
890
891 p = in;
892 for (;;) {
893 l = strcspn(p, "\"\\");
894 p += l;
895
896 if (p[0] == '\0')
897 break;
898
899 reslen++;
900 p++;
901 }
902
Alan Cox177acf72012-11-06 14:32:08 +0000903 res = xmalloc(reslen);
Arnaud Lacombee54e6922011-05-15 23:42:09 -0400904 res[0] = '\0';
905
906 strcat(res, "\"");
907
908 p = in;
909 for (;;) {
910 l = strcspn(p, "\"\\");
911 strncat(res, p, l);
912 p += l;
913
914 if (p[0] == '\0')
915 break;
916
917 strcat(res, "\\");
918 strncat(res, p++, 1);
919 }
920
921 strcat(res, "\"");
922 return res;
923}
924
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200925struct sym_match {
926 struct symbol *sym;
927 off_t so, eo;
928};
929
930/* Compare matched symbols as thus:
931 * - first, symbols that match exactly
932 * - then, alphabetical sort
933 */
Yann E. MORIN803b3512013-07-16 20:28:51 +0200934static int sym_rel_comp(const void *sym1, const void *sym2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200935{
Yann E. MORIN508382a2013-07-16 20:39:42 +0200936 const struct sym_match *s1 = sym1;
937 const struct sym_match *s2 = sym2;
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200938 int exact1, exact2;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200939
940 /* Exact match:
941 * - if matched length on symbol s1 is the length of that symbol,
942 * then this symbol should come first;
943 * - if matched length on symbol s2 is the length of that symbol,
944 * then this symbol should come first.
945 * Note: since the search can be a regexp, both symbols may match
946 * exactly; if this is the case, we can't decide which comes first,
947 * and we fallback to sorting alphabetically.
948 */
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200949 exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
950 exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
951 if (exact1 && !exact2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200952 return -1;
Yann E. MORIN26e933e2013-07-13 15:09:43 +0200953 if (!exact1 && exact2)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200954 return 1;
955
956 /* As a fallback, sort symbols alphabetically */
957 return strcmp(s1->sym->name, s2->sym->name);
958}
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960struct symbol **sym_re_search(const char *pattern)
961{
962 struct symbol *sym, **sym_arr = NULL;
Yann E. MORIN508382a2013-07-16 20:39:42 +0200963 struct sym_match *sym_match_arr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int i, cnt, size;
965 regex_t re;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200966 regmatch_t match[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 cnt = size = 0;
969 /* Skip if empty */
970 if (strlen(pattern) == 0)
971 return NULL;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200972 if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return NULL;
974
975 for_all_symbols(i, sym) {
976 if (sym->flags & SYMBOL_CONST || !sym->name)
977 continue;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200978 if (regexec(&re, sym->name, 1, match, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 continue;
Yann E. MORIN1407f972013-07-16 20:32:33 +0200980 if (cnt >= size) {
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200981 void *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 size += 16;
Yann E. MORIN508382a2013-07-16 20:39:42 +0200983 tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
Yann E. MORIN803b3512013-07-16 20:28:51 +0200984 if (!tmp)
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200985 goto sym_re_search_free;
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200986 sym_match_arr = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Li Zefanda6df872010-03-19 14:57:47 +0800988 sym_calc_value(sym);
Yann E. MORIN803b3512013-07-16 20:28:51 +0200989 /* As regexec returned 0, we know we have a match, so
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200990 * we can use match[0].rm_[se]o without further checks
991 */
Yann E. MORIN508382a2013-07-16 20:39:42 +0200992 sym_match_arr[cnt].so = match[0].rm_so;
993 sym_match_arr[cnt].eo = match[0].rm_eo;
994 sym_match_arr[cnt++].sym = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200996 if (sym_match_arr) {
Yann E. MORIN508382a2013-07-16 20:39:42 +0200997 qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
Heinrich Schuchardt88127da2017-11-08 22:09:59 +0100998 sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
Yann E. MORIN193b40a2013-05-06 14:57:47 +0200999 if (!sym_arr)
1000 goto sym_re_search_free;
1001 for (i = 0; i < cnt; i++)
Yann E. MORIN508382a2013-07-16 20:39:42 +02001002 sym_arr[i] = sym_match_arr[i].sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 sym_arr[cnt] = NULL;
Yann E. MORIN193b40a2013-05-06 14:57:47 +02001004 }
1005sym_re_search_free:
Yann E. MORIN508382a2013-07-16 20:39:42 +02001006 /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
1007 free(sym_match_arr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 regfree(&re);
1009
1010 return sym_arr;
1011}
1012
Roman Zippeld595cea2010-07-31 23:35:30 +02001013/*
1014 * When we check for recursive dependencies we use a stack to save
1015 * current state so we can print out relevant info to user.
1016 * The entries are located on the call stack so no need to free memory.
Martin Walch8d9dfe82013-10-03 17:28:14 +02001017 * Note insert() remove() must always match to properly clear the stack.
Roman Zippeld595cea2010-07-31 23:35:30 +02001018 */
1019static struct dep_stack {
1020 struct dep_stack *prev, *next;
1021 struct symbol *sym;
1022 struct property *prop;
1023 struct expr *expr;
1024} *check_top;
1025
1026static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
1027{
1028 memset(stack, 0, sizeof(*stack));
1029 if (check_top)
1030 check_top->next = stack;
1031 stack->prev = check_top;
1032 stack->sym = sym;
1033 check_top = stack;
1034}
1035
1036static void dep_stack_remove(void)
1037{
1038 check_top = check_top->prev;
1039 if (check_top)
1040 check_top->next = NULL;
1041}
1042
1043/*
1044 * Called when we have detected a recursive dependency.
1045 * check_top point to the top of the stact so we use
1046 * the ->prev pointer to locate the bottom of the stack.
1047 */
1048static void sym_check_print_recursive(struct symbol *last_sym)
1049{
1050 struct dep_stack *stack;
1051 struct symbol *sym, *next_sym;
1052 struct menu *menu = NULL;
1053 struct property *prop;
1054 struct dep_stack cv_stack;
1055
1056 if (sym_is_choice_value(last_sym)) {
1057 dep_stack_insert(&cv_stack, last_sym);
1058 last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1059 }
1060
1061 for (stack = check_top; stack != NULL; stack = stack->prev)
1062 if (stack->sym == last_sym)
1063 break;
1064 if (!stack) {
1065 fprintf(stderr, "unexpected recursive dependency error\n");
1066 return;
1067 }
1068
1069 for (; stack; stack = stack->next) {
1070 sym = stack->sym;
1071 next_sym = stack->next ? stack->next->sym : last_sym;
1072 prop = stack->prop;
Sam Ravnborg3643f842010-08-14 14:40:00 +02001073 if (prop == NULL)
1074 prop = stack->sym->prop;
Roman Zippeld595cea2010-07-31 23:35:30 +02001075
1076 /* for choice values find the menu entry (used below) */
1077 if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1078 for (prop = sym->prop; prop; prop = prop->next) {
1079 menu = prop->menu;
1080 if (prop->menu)
1081 break;
1082 }
1083 }
1084 if (stack->sym == last_sym)
1085 fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1086 prop->file->name, prop->lineno);
Masahiro Yamadae3b03bf2017-12-16 00:28:42 +09001087
Roman Zippeld595cea2010-07-31 23:35:30 +02001088 if (stack->expr) {
1089 fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1090 prop->file->name, prop->lineno,
1091 sym->name ? sym->name : "<choice>",
1092 prop_get_type_name(prop->type),
1093 next_sym->name ? next_sym->name : "<choice>");
1094 } else if (stack->prop) {
1095 fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1096 prop->file->name, prop->lineno,
1097 sym->name ? sym->name : "<choice>",
1098 next_sym->name ? next_sym->name : "<choice>");
1099 } else if (sym_is_choice(sym)) {
1100 fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1101 menu->file->name, menu->lineno,
1102 sym->name ? sym->name : "<choice>",
1103 next_sym->name ? next_sym->name : "<choice>");
1104 } else if (sym_is_choice_value(sym)) {
1105 fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1106 menu->file->name, menu->lineno,
1107 sym->name ? sym->name : "<choice>",
1108 next_sym->name ? next_sym->name : "<choice>");
1109 } else {
1110 fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1111 prop->file->name, prop->lineno,
1112 sym->name ? sym->name : "<choice>",
1113 next_sym->name ? next_sym->name : "<choice>");
1114 }
1115 }
1116
Masahiro Yamadae3b03bf2017-12-16 00:28:42 +09001117 fprintf(stderr,
1118 "For a resolution refer to Documentation/kbuild/kconfig-language.txt\n"
1119 "subsection \"Kconfig recursive dependency limitations\"\n"
1120 "\n");
1121
Roman Zippeld595cea2010-07-31 23:35:30 +02001122 if (check_top == &cv_stack)
1123 dep_stack_remove();
1124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126static struct symbol *sym_check_expr_deps(struct expr *e)
1127{
1128 struct symbol *sym;
1129
1130 if (!e)
1131 return NULL;
1132 switch (e->type) {
1133 case E_OR:
1134 case E_AND:
1135 sym = sym_check_expr_deps(e->left.expr);
1136 if (sym)
1137 return sym;
1138 return sym_check_expr_deps(e->right.expr);
1139 case E_NOT:
1140 return sym_check_expr_deps(e->left.expr);
1141 case E_EQUAL:
Jan Beulich31847b62015-06-15 13:00:21 +01001142 case E_GEQ:
1143 case E_GTH:
1144 case E_LEQ:
1145 case E_LTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 case E_UNEQUAL:
1147 sym = sym_check_deps(e->left.sym);
1148 if (sym)
1149 return sym;
1150 return sym_check_deps(e->right.sym);
1151 case E_SYMBOL:
1152 return sym_check_deps(e->left.sym);
1153 default:
1154 break;
1155 }
Masahiro Yamada9e3e10c2018-02-06 09:34:41 +09001156 fprintf(stderr, "Oops! How to check %d?\n", e->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return NULL;
1158}
1159
Sam Ravnborg5447d342007-05-06 09:20:10 +02001160/* return NULL when dependencies are OK */
Roman Zippel48981172008-02-29 05:10:24 +01001161static struct symbol *sym_check_sym_deps(struct symbol *sym)
1162{
1163 struct symbol *sym2;
1164 struct property *prop;
Roman Zippeld595cea2010-07-31 23:35:30 +02001165 struct dep_stack stack;
1166
1167 dep_stack_insert(&stack, sym);
Roman Zippel48981172008-02-29 05:10:24 +01001168
1169 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1170 if (sym2)
Roman Zippeld595cea2010-07-31 23:35:30 +02001171 goto out;
Roman Zippel48981172008-02-29 05:10:24 +01001172
1173 for (prop = sym->prop; prop; prop = prop->next) {
1174 if (prop->type == P_CHOICE || prop->type == P_SELECT)
1175 continue;
Roman Zippeld595cea2010-07-31 23:35:30 +02001176 stack.prop = prop;
Roman Zippel48981172008-02-29 05:10:24 +01001177 sym2 = sym_check_expr_deps(prop->visible.expr);
1178 if (sym2)
1179 break;
1180 if (prop->type != P_DEFAULT || sym_is_choice(sym))
1181 continue;
Roman Zippeld595cea2010-07-31 23:35:30 +02001182 stack.expr = prop->expr;
Roman Zippel48981172008-02-29 05:10:24 +01001183 sym2 = sym_check_expr_deps(prop->expr);
1184 if (sym2)
1185 break;
Roman Zippeld595cea2010-07-31 23:35:30 +02001186 stack.expr = NULL;
Roman Zippel48981172008-02-29 05:10:24 +01001187 }
1188
Roman Zippeld595cea2010-07-31 23:35:30 +02001189out:
1190 dep_stack_remove();
1191
Roman Zippel48981172008-02-29 05:10:24 +01001192 return sym2;
1193}
1194
1195static struct symbol *sym_check_choice_deps(struct symbol *choice)
1196{
1197 struct symbol *sym, *sym2;
1198 struct property *prop;
1199 struct expr *e;
Roman Zippeld595cea2010-07-31 23:35:30 +02001200 struct dep_stack stack;
1201
1202 dep_stack_insert(&stack, choice);
Roman Zippel48981172008-02-29 05:10:24 +01001203
1204 prop = sym_get_choice_prop(choice);
1205 expr_list_for_each_sym(prop->expr, e, sym)
1206 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1207
1208 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1209 sym2 = sym_check_sym_deps(choice);
1210 choice->flags &= ~SYMBOL_CHECK;
1211 if (sym2)
1212 goto out;
1213
1214 expr_list_for_each_sym(prop->expr, e, sym) {
1215 sym2 = sym_check_sym_deps(sym);
Roman Zippeld595cea2010-07-31 23:35:30 +02001216 if (sym2)
Roman Zippel48981172008-02-29 05:10:24 +01001217 break;
Roman Zippel48981172008-02-29 05:10:24 +01001218 }
1219out:
1220 expr_list_for_each_sym(prop->expr, e, sym)
1221 sym->flags &= ~SYMBOL_CHECK;
1222
1223 if (sym2 && sym_is_choice_value(sym2) &&
1224 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1225 sym2 = choice;
1226
Roman Zippeld595cea2010-07-31 23:35:30 +02001227 dep_stack_remove();
1228
Roman Zippel48981172008-02-29 05:10:24 +01001229 return sym2;
1230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232struct symbol *sym_check_deps(struct symbol *sym)
1233{
1234 struct symbol *sym2;
1235 struct property *prop;
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (sym->flags & SYMBOL_CHECK) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001238 sym_check_print_recursive(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return sym;
1240 }
David Gibson3f04e7d2005-11-08 21:34:46 -08001241 if (sym->flags & SYMBOL_CHECKED)
1242 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Roman Zippel48981172008-02-29 05:10:24 +01001244 if (sym_is_choice_value(sym)) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001245 struct dep_stack stack;
1246
Roman Zippel48981172008-02-29 05:10:24 +01001247 /* for choice groups start the check with main choice symbol */
Roman Zippeld595cea2010-07-31 23:35:30 +02001248 dep_stack_insert(&stack, sym);
Roman Zippel48981172008-02-29 05:10:24 +01001249 prop = sym_get_choice_prop(sym);
1250 sym2 = sym_check_deps(prop_get_symbol(prop));
Roman Zippeld595cea2010-07-31 23:35:30 +02001251 dep_stack_remove();
Roman Zippel48981172008-02-29 05:10:24 +01001252 } else if (sym_is_choice(sym)) {
1253 sym2 = sym_check_choice_deps(sym);
1254 } else {
1255 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1256 sym2 = sym_check_sym_deps(sym);
1257 sym->flags &= ~SYMBOL_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
Roman Zippel48981172008-02-29 05:10:24 +01001259
Roman Zippeld595cea2010-07-31 23:35:30 +02001260 if (sym2 && sym2 == sym)
1261 sym2 = NULL;
Roman Zippel48981172008-02-29 05:10:24 +01001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return sym2;
1264}
1265
1266struct property *prop_alloc(enum prop_type type, struct symbol *sym)
1267{
1268 struct property *prop;
1269 struct property **propp;
1270
Alan Cox177acf72012-11-06 14:32:08 +00001271 prop = xmalloc(sizeof(*prop));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 memset(prop, 0, sizeof(*prop));
1273 prop->type = type;
1274 prop->sym = sym;
1275 prop->file = current_file;
1276 prop->lineno = zconf_lineno();
1277
1278 /* append property to the prop list of symbol */
1279 if (sym) {
1280 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
1281 ;
1282 *propp = prop;
1283 }
1284
1285 return prop;
1286}
1287
1288struct symbol *prop_get_symbol(struct property *prop)
1289{
1290 if (prop->expr && (prop->expr->type == E_SYMBOL ||
Roman Zippel7a962922008-01-14 04:50:23 +01001291 prop->expr->type == E_LIST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return prop->expr->left.sym;
1293 return NULL;
1294}
1295
1296const char *prop_get_type_name(enum prop_type type)
1297{
1298 switch (type) {
1299 case P_PROMPT:
1300 return "prompt";
Roman Zippel93449082008-01-14 04:50:54 +01001301 case P_ENV:
1302 return "env";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 case P_COMMENT:
1304 return "comment";
1305 case P_MENU:
1306 return "menu";
1307 case P_DEFAULT:
1308 return "default";
1309 case P_CHOICE:
1310 return "choice";
1311 case P_SELECT:
1312 return "select";
Nicolas Pitre237e3ad2016-11-11 00:10:05 -05001313 case P_IMPLY:
1314 return "imply";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 case P_RANGE:
1316 return "range";
Sam Ravnborg59e89e32010-07-31 23:35:29 +02001317 case P_SYMBOL:
1318 return "symbol";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 case P_UNKNOWN:
1320 break;
1321 }
1322 return "unknown";
1323}