10c874100SMasahiro Yamada // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
41da177e4SLinus Torvalds */
51da177e4SLinus Torvalds
678cb0907SBoris Kolpackov #include <sys/types.h>
71da177e4SLinus Torvalds #include <ctype.h>
81da177e4SLinus Torvalds #include <stdlib.h>
91da177e4SLinus Torvalds #include <string.h>
101da177e4SLinus Torvalds #include <regex.h>
111da177e4SLinus Torvalds
121da177e4SLinus Torvalds #include "lkc.h"
131da177e4SLinus Torvalds
141da177e4SLinus Torvalds struct symbol symbol_yes = {
151da177e4SLinus Torvalds .name = "y",
16*2b6e818fSMasahiro Yamada .type = S_TRISTATE,
171da177e4SLinus Torvalds .curr = { "y", yes },
18c0e150acSRoman Zippel .flags = SYMBOL_CONST|SYMBOL_VALID,
19d41809ffSMasahiro Yamada };
20d41809ffSMasahiro Yamada
21d41809ffSMasahiro Yamada struct symbol symbol_mod = {
221da177e4SLinus Torvalds .name = "m",
23*2b6e818fSMasahiro Yamada .type = S_TRISTATE,
241da177e4SLinus Torvalds .curr = { "m", mod },
25c0e150acSRoman Zippel .flags = SYMBOL_CONST|SYMBOL_VALID,
26d41809ffSMasahiro Yamada };
27d41809ffSMasahiro Yamada
28d41809ffSMasahiro Yamada struct symbol symbol_no = {
291da177e4SLinus Torvalds .name = "n",
30*2b6e818fSMasahiro Yamada .type = S_TRISTATE,
311da177e4SLinus Torvalds .curr = { "n", no },
32c0e150acSRoman Zippel .flags = SYMBOL_CONST|SYMBOL_VALID,
33d41809ffSMasahiro Yamada };
34d41809ffSMasahiro Yamada
35d41809ffSMasahiro Yamada static struct symbol symbol_empty = {
361da177e4SLinus Torvalds .name = "",
371da177e4SLinus Torvalds .curr = { "", no },
381da177e4SLinus Torvalds .flags = SYMBOL_VALID,
391da177e4SLinus Torvalds };
401da177e4SLinus Torvalds
411da177e4SLinus Torvalds struct symbol *modules_sym;
42d41809ffSMasahiro Yamada static tristate modules_val;
431da177e4SLinus Torvalds
sym_get_type(struct symbol * sym)441da177e4SLinus Torvalds enum symbol_type sym_get_type(struct symbol *sym)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds enum symbol_type type = sym->type;
471da177e4SLinus Torvalds
481da177e4SLinus Torvalds if (type == S_TRISTATE) {
491da177e4SLinus Torvalds if (sym_is_choice_value(sym) && sym->visible == yes)
501da177e4SLinus Torvalds type = S_BOOLEAN;
511da177e4SLinus Torvalds else if (modules_val == no)
521da177e4SLinus Torvalds type = S_BOOLEAN;
531da177e4SLinus Torvalds }
541da177e4SLinus Torvalds return type;
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds
sym_type_name(enum symbol_type type)571da177e4SLinus Torvalds const char *sym_type_name(enum symbol_type type)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds switch (type) {
601da177e4SLinus Torvalds case S_BOOLEAN:
61b92d804aSMasahiro Yamada return "bool";
621da177e4SLinus Torvalds case S_TRISTATE:
631da177e4SLinus Torvalds return "tristate";
641da177e4SLinus Torvalds case S_INT:
651da177e4SLinus Torvalds return "integer";
661da177e4SLinus Torvalds case S_HEX:
671da177e4SLinus Torvalds return "hex";
681da177e4SLinus Torvalds case S_STRING:
691da177e4SLinus Torvalds return "string";
701da177e4SLinus Torvalds case S_UNKNOWN:
711da177e4SLinus Torvalds return "unknown";
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds return "???";
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds
sym_get_choice_prop(struct symbol * sym)761da177e4SLinus Torvalds struct property *sym_get_choice_prop(struct symbol *sym)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds struct property *prop;
791da177e4SLinus Torvalds
801da177e4SLinus Torvalds for_all_choices(sym, prop)
811da177e4SLinus Torvalds return prop;
821da177e4SLinus Torvalds return NULL;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds
sym_get_default_prop(struct symbol * sym)85ad8d40cdSMichal Marek static struct property *sym_get_default_prop(struct symbol *sym)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds struct property *prop;
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds for_all_defaults(sym, prop) {
901da177e4SLinus Torvalds prop->visible.tri = expr_calc_value(prop->visible.expr);
911da177e4SLinus Torvalds if (prop->visible.tri != no)
921da177e4SLinus Torvalds return prop;
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds return NULL;
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds
sym_get_range_prop(struct symbol * sym)97558e78e3SMasahiro Yamada struct property *sym_get_range_prop(struct symbol *sym)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds struct property *prop;
1001da177e4SLinus Torvalds
1011da177e4SLinus Torvalds for_all_properties(sym, prop, P_RANGE) {
1021da177e4SLinus Torvalds prop->visible.tri = expr_calc_value(prop->visible.expr);
1031da177e4SLinus Torvalds if (prop->visible.tri != no)
1041da177e4SLinus Torvalds return prop;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds return NULL;
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds
sym_get_range_val(struct symbol * sym,int base)109129784abSKees Cook static long long sym_get_range_val(struct symbol *sym, int base)
1104cf3cbe2SRoman Zippel {
1114cf3cbe2SRoman Zippel sym_calc_value(sym);
1124cf3cbe2SRoman Zippel switch (sym->type) {
1134cf3cbe2SRoman Zippel case S_INT:
1144cf3cbe2SRoman Zippel base = 10;
1154cf3cbe2SRoman Zippel break;
1164cf3cbe2SRoman Zippel case S_HEX:
1174cf3cbe2SRoman Zippel base = 16;
1184cf3cbe2SRoman Zippel break;
1194cf3cbe2SRoman Zippel default:
1204cf3cbe2SRoman Zippel break;
1214cf3cbe2SRoman Zippel }
122129784abSKees Cook return strtoll(sym->curr.val, NULL, base);
1234cf3cbe2SRoman Zippel }
1244cf3cbe2SRoman Zippel
sym_validate_range(struct symbol * sym)1254cf3cbe2SRoman Zippel static void sym_validate_range(struct symbol *sym)
1264cf3cbe2SRoman Zippel {
1274cf3cbe2SRoman Zippel struct property *prop;
128b97debd0SMasahiro Yamada struct symbol *range_sym;
129129784abSKees Cook int base;
130129784abSKees Cook long long val, val2;
1314cf3cbe2SRoman Zippel
1324cf3cbe2SRoman Zippel switch (sym->type) {
1334cf3cbe2SRoman Zippel case S_INT:
1344cf3cbe2SRoman Zippel base = 10;
1354cf3cbe2SRoman Zippel break;
1364cf3cbe2SRoman Zippel case S_HEX:
1374cf3cbe2SRoman Zippel base = 16;
1384cf3cbe2SRoman Zippel break;
1394cf3cbe2SRoman Zippel default:
1404cf3cbe2SRoman Zippel return;
1414cf3cbe2SRoman Zippel }
1424cf3cbe2SRoman Zippel prop = sym_get_range_prop(sym);
1434cf3cbe2SRoman Zippel if (!prop)
1444cf3cbe2SRoman Zippel return;
145129784abSKees Cook val = strtoll(sym->curr.val, NULL, base);
146b97debd0SMasahiro Yamada range_sym = prop->expr->left.sym;
147b97debd0SMasahiro Yamada val2 = sym_get_range_val(range_sym, base);
1484cf3cbe2SRoman Zippel if (val >= val2) {
149b97debd0SMasahiro Yamada range_sym = prop->expr->right.sym;
150b97debd0SMasahiro Yamada val2 = sym_get_range_val(range_sym, base);
1514cf3cbe2SRoman Zippel if (val <= val2)
1524cf3cbe2SRoman Zippel return;
1534cf3cbe2SRoman Zippel }
154b97debd0SMasahiro Yamada sym->curr.val = range_sym->curr.val;
1554cf3cbe2SRoman Zippel }
1564cf3cbe2SRoman Zippel
sym_set_changed(struct symbol * sym)157ad8d40cdSMichal Marek static void sym_set_changed(struct symbol *sym)
158ad8d40cdSMichal Marek {
159ad8d40cdSMichal Marek struct property *prop;
160ad8d40cdSMichal Marek
161ad8d40cdSMichal Marek sym->flags |= SYMBOL_CHANGED;
162ad8d40cdSMichal Marek for (prop = sym->prop; prop; prop = prop->next) {
163ad8d40cdSMichal Marek if (prop->menu)
164ad8d40cdSMichal Marek prop->menu->flags |= MENU_CHANGED;
165ad8d40cdSMichal Marek }
166ad8d40cdSMichal Marek }
167ad8d40cdSMichal Marek
sym_set_all_changed(void)168ad8d40cdSMichal Marek static void sym_set_all_changed(void)
169ad8d40cdSMichal Marek {
170ad8d40cdSMichal Marek struct symbol *sym;
171ad8d40cdSMichal Marek int i;
172ad8d40cdSMichal Marek
173ad8d40cdSMichal Marek for_all_symbols(i, sym)
174ad8d40cdSMichal Marek sym_set_changed(sym);
175ad8d40cdSMichal Marek }
176ad8d40cdSMichal Marek
sym_calc_visibility(struct symbol * sym)1771da177e4SLinus Torvalds static void sym_calc_visibility(struct symbol *sym)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds struct property *prop;
180fa64e5f6SDirk Gouders struct symbol *choice_sym = NULL;
1811da177e4SLinus Torvalds tristate tri;
1821da177e4SLinus Torvalds
1831da177e4SLinus Torvalds /* any prompt visible? */
1841da177e4SLinus Torvalds tri = no;
185fa64e5f6SDirk Gouders
186fa64e5f6SDirk Gouders if (sym_is_choice_value(sym))
187fa64e5f6SDirk Gouders choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
188fa64e5f6SDirk Gouders
1891da177e4SLinus Torvalds for_all_prompts(sym, prop) {
1901da177e4SLinus Torvalds prop->visible.tri = expr_calc_value(prop->visible.expr);
191fa64e5f6SDirk Gouders /*
192fa64e5f6SDirk Gouders * Tristate choice_values with visibility 'mod' are
193fa64e5f6SDirk Gouders * not visible if the corresponding choice's value is
194fa64e5f6SDirk Gouders * 'yes'.
195fa64e5f6SDirk Gouders */
196fa64e5f6SDirk Gouders if (choice_sym && sym->type == S_TRISTATE &&
197fa64e5f6SDirk Gouders prop->visible.tri == mod && choice_sym->curr.tri == yes)
198fa64e5f6SDirk Gouders prop->visible.tri = no;
199fa64e5f6SDirk Gouders
200d6ee3576SSam Ravnborg tri = EXPR_OR(tri, prop->visible.tri);
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
2031da177e4SLinus Torvalds tri = yes;
2041da177e4SLinus Torvalds if (sym->visible != tri) {
2051da177e4SLinus Torvalds sym->visible = tri;
2061da177e4SLinus Torvalds sym_set_changed(sym);
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds if (sym_is_choice_value(sym))
2091da177e4SLinus Torvalds return;
210246cf9c2SCatalin Marinas /* defaulting to "yes" if no explicit "depends on" are given */
211246cf9c2SCatalin Marinas tri = yes;
212246cf9c2SCatalin Marinas if (sym->dir_dep.expr)
213246cf9c2SCatalin Marinas tri = expr_calc_value(sym->dir_dep.expr);
214f622f827SMasahiro Yamada if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
215246cf9c2SCatalin Marinas tri = yes;
216246cf9c2SCatalin Marinas if (sym->dir_dep.tri != tri) {
217246cf9c2SCatalin Marinas sym->dir_dep.tri = tri;
218246cf9c2SCatalin Marinas sym_set_changed(sym);
219246cf9c2SCatalin Marinas }
2201da177e4SLinus Torvalds tri = no;
2211da177e4SLinus Torvalds if (sym->rev_dep.expr)
2221da177e4SLinus Torvalds tri = expr_calc_value(sym->rev_dep.expr);
2231da177e4SLinus Torvalds if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
2241da177e4SLinus Torvalds tri = yes;
2251da177e4SLinus Torvalds if (sym->rev_dep.tri != tri) {
2261da177e4SLinus Torvalds sym->rev_dep.tri = tri;
2271da177e4SLinus Torvalds sym_set_changed(sym);
2281da177e4SLinus Torvalds }
229237e3ad0SNicolas Pitre tri = no;
2303a9dd3ecSMasahiro Yamada if (sym->implied.expr)
231237e3ad0SNicolas Pitre tri = expr_calc_value(sym->implied.expr);
232237e3ad0SNicolas Pitre if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
233237e3ad0SNicolas Pitre tri = yes;
234237e3ad0SNicolas Pitre if (sym->implied.tri != tri) {
235237e3ad0SNicolas Pitre sym->implied.tri = tri;
236237e3ad0SNicolas Pitre sym_set_changed(sym);
237237e3ad0SNicolas Pitre }
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds
240c252147dSSam Ravnborg /*
241c252147dSSam Ravnborg * Find the default symbol for a choice.
242c252147dSSam Ravnborg * First try the default values for the choice symbol
243c252147dSSam Ravnborg * Next locate the first visible choice value
244c252147dSSam Ravnborg * Return NULL if none was found
245c252147dSSam Ravnborg */
sym_choice_default(struct symbol * sym)246c252147dSSam Ravnborg struct symbol *sym_choice_default(struct symbol *sym)
2471da177e4SLinus Torvalds {
2481da177e4SLinus Torvalds struct symbol *def_sym;
2491da177e4SLinus Torvalds struct property *prop;
2501da177e4SLinus Torvalds struct expr *e;
2511da177e4SLinus Torvalds
2521da177e4SLinus Torvalds /* any of the defaults visible? */
2531da177e4SLinus Torvalds for_all_defaults(sym, prop) {
2541da177e4SLinus Torvalds prop->visible.tri = expr_calc_value(prop->visible.expr);
2551da177e4SLinus Torvalds if (prop->visible.tri == no)
2561da177e4SLinus Torvalds continue;
2571da177e4SLinus Torvalds def_sym = prop_get_symbol(prop);
2581da177e4SLinus Torvalds if (def_sym->visible != no)
2591da177e4SLinus Torvalds return def_sym;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds
2621da177e4SLinus Torvalds /* just get the first visible value */
2631da177e4SLinus Torvalds prop = sym_get_choice_prop(sym);
2640a28c47bSJan Beulich expr_list_for_each_sym(prop->expr, e, def_sym)
2651da177e4SLinus Torvalds if (def_sym->visible != no)
2661da177e4SLinus Torvalds return def_sym;
2671da177e4SLinus Torvalds
268c252147dSSam Ravnborg /* failed to locate any defaults */
269c252147dSSam Ravnborg return NULL;
270c252147dSSam Ravnborg }
271c252147dSSam Ravnborg
sym_calc_choice(struct symbol * sym)272c252147dSSam Ravnborg static struct symbol *sym_calc_choice(struct symbol *sym)
273c252147dSSam Ravnborg {
274c252147dSSam Ravnborg struct symbol *def_sym;
275c252147dSSam Ravnborg struct property *prop;
276c252147dSSam Ravnborg struct expr *e;
2775d09598dSArnaud Lacombe int flags;
278c252147dSSam Ravnborg
279c252147dSSam Ravnborg /* first calculate all choice values' visibilities */
2805d09598dSArnaud Lacombe flags = sym->flags;
281c252147dSSam Ravnborg prop = sym_get_choice_prop(sym);
2825d09598dSArnaud Lacombe expr_list_for_each_sym(prop->expr, e, def_sym) {
283c252147dSSam Ravnborg sym_calc_visibility(def_sym);
2845d09598dSArnaud Lacombe if (def_sym->visible != no)
2855d09598dSArnaud Lacombe flags &= def_sym->flags;
2865d09598dSArnaud Lacombe }
2875d09598dSArnaud Lacombe
2885d09598dSArnaud Lacombe sym->flags &= flags | ~SYMBOL_DEF_USER;
289c252147dSSam Ravnborg
290c252147dSSam Ravnborg /* is the user choice visible? */
291c252147dSSam Ravnborg def_sym = sym->def[S_DEF_USER].val;
292c252147dSSam Ravnborg if (def_sym && def_sym->visible != no)
293c252147dSSam Ravnborg return def_sym;
294c252147dSSam Ravnborg
295c252147dSSam Ravnborg def_sym = sym_choice_default(sym);
296c252147dSSam Ravnborg
297c252147dSSam Ravnborg if (def_sym == NULL)
2981da177e4SLinus Torvalds /* no choice? reset tristate value */
2991da177e4SLinus Torvalds sym->curr.tri = no;
300c252147dSSam Ravnborg
301c252147dSSam Ravnborg return def_sym;
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds
sym_warn_unmet_dep(struct symbol * sym)304f8f69dc0SMasahiro Yamada static void sym_warn_unmet_dep(struct symbol *sym)
305f8f69dc0SMasahiro Yamada {
306f8f69dc0SMasahiro Yamada struct gstr gs = str_new();
307f8f69dc0SMasahiro Yamada
308f8f69dc0SMasahiro Yamada str_printf(&gs,
309f8f69dc0SMasahiro Yamada "\nWARNING: unmet direct dependencies detected for %s\n",
310f8f69dc0SMasahiro Yamada sym->name);
311f8f69dc0SMasahiro Yamada str_printf(&gs,
312f8f69dc0SMasahiro Yamada " Depends on [%c]: ",
313f8f69dc0SMasahiro Yamada sym->dir_dep.tri == mod ? 'm' : 'n');
314f8f69dc0SMasahiro Yamada expr_gstr_print(sym->dir_dep.expr, &gs);
315f8f69dc0SMasahiro Yamada str_printf(&gs, "\n");
316f8f69dc0SMasahiro Yamada
317f8f69dc0SMasahiro Yamada expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes,
318f8f69dc0SMasahiro Yamada " Selected by [y]:\n");
319f8f69dc0SMasahiro Yamada expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod,
320f8f69dc0SMasahiro Yamada " Selected by [m]:\n");
321f8f69dc0SMasahiro Yamada
322f8f69dc0SMasahiro Yamada fputs(str_get(&gs), stderr);
323f8f69dc0SMasahiro Yamada }
324f8f69dc0SMasahiro Yamada
sym_calc_value(struct symbol * sym)3251da177e4SLinus Torvalds void sym_calc_value(struct symbol *sym)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds struct symbol_value newval, oldval;
3281da177e4SLinus Torvalds struct property *prop;
3291da177e4SLinus Torvalds struct expr *e;
3301da177e4SLinus Torvalds
3311da177e4SLinus Torvalds if (!sym)
3321da177e4SLinus Torvalds return;
3331da177e4SLinus Torvalds
3341da177e4SLinus Torvalds if (sym->flags & SYMBOL_VALID)
3351da177e4SLinus Torvalds return;
336fbe98bb9SArve Hjønnevåg
337fbe98bb9SArve Hjønnevåg if (sym_is_choice_value(sym) &&
338fbe98bb9SArve Hjønnevåg sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
339fbe98bb9SArve Hjønnevåg sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
340fbe98bb9SArve Hjønnevåg prop = sym_get_choice_prop(sym);
341fbe98bb9SArve Hjønnevåg sym_calc_value(prop_get_symbol(prop));
342fbe98bb9SArve Hjønnevåg }
343fbe98bb9SArve Hjønnevåg
3441da177e4SLinus Torvalds sym->flags |= SYMBOL_VALID;
3451da177e4SLinus Torvalds
3461da177e4SLinus Torvalds oldval = sym->curr;
3471da177e4SLinus Torvalds
3481da177e4SLinus Torvalds switch (sym->type) {
3491da177e4SLinus Torvalds case S_INT:
3501da177e4SLinus Torvalds case S_HEX:
3511da177e4SLinus Torvalds case S_STRING:
3521da177e4SLinus Torvalds newval = symbol_empty.curr;
3531da177e4SLinus Torvalds break;
3541da177e4SLinus Torvalds case S_BOOLEAN:
3551da177e4SLinus Torvalds case S_TRISTATE:
3561da177e4SLinus Torvalds newval = symbol_no.curr;
3571da177e4SLinus Torvalds break;
3581da177e4SLinus Torvalds default:
3591da177e4SLinus Torvalds sym->curr.val = sym->name;
3601da177e4SLinus Torvalds sym->curr.tri = no;
3611da177e4SLinus Torvalds return;
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds sym->flags &= ~SYMBOL_WRITE;
3641da177e4SLinus Torvalds
3651da177e4SLinus Torvalds sym_calc_visibility(sym);
3661da177e4SLinus Torvalds
367cb67ab2cSMasahiro Yamada if (sym->visible != no)
368cb67ab2cSMasahiro Yamada sym->flags |= SYMBOL_WRITE;
369cb67ab2cSMasahiro Yamada
3701da177e4SLinus Torvalds /* set default if recursively called */
3711da177e4SLinus Torvalds sym->curr = newval;
3721da177e4SLinus Torvalds
3731da177e4SLinus Torvalds switch (sym_get_type(sym)) {
3741da177e4SLinus Torvalds case S_BOOLEAN:
3751da177e4SLinus Torvalds case S_TRISTATE:
3761da177e4SLinus Torvalds if (sym_is_choice_value(sym) && sym->visible == yes) {
3771da177e4SLinus Torvalds prop = sym_get_choice_prop(sym);
3781da177e4SLinus Torvalds newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
379587c9061SRoman Zippel } else {
380587c9061SRoman Zippel if (sym->visible != no) {
381587c9061SRoman Zippel /* if the symbol is visible use the user value
382587c9061SRoman Zippel * if available, otherwise try the default value
383587c9061SRoman Zippel */
384587c9061SRoman Zippel if (sym_has_value(sym)) {
385587c9061SRoman Zippel newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
386587c9061SRoman Zippel sym->visible);
387587c9061SRoman Zippel goto calc_newval;
3881da177e4SLinus Torvalds }
389587c9061SRoman Zippel }
390587c9061SRoman Zippel if (sym->rev_dep.tri != no)
391587c9061SRoman Zippel sym->flags |= SYMBOL_WRITE;
392587c9061SRoman Zippel if (!sym_is_choice(sym)) {
3931da177e4SLinus Torvalds prop = sym_get_default_prop(sym);
3941da177e4SLinus Torvalds if (prop) {
395587c9061SRoman Zippel newval.tri = EXPR_AND(expr_calc_value(prop->expr),
396587c9061SRoman Zippel prop->visible.tri);
397f467c564SUlf Magnusson if (newval.tri != no)
398f467c564SUlf Magnusson sym->flags |= SYMBOL_WRITE;
3991da177e4SLinus Torvalds }
400237e3ad0SNicolas Pitre if (sym->implied.tri != no) {
401237e3ad0SNicolas Pitre sym->flags |= SYMBOL_WRITE;
402237e3ad0SNicolas Pitre newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
4033a9dd3ecSMasahiro Yamada newval.tri = EXPR_AND(newval.tri,
4043a9dd3ecSMasahiro Yamada sym->dir_dep.tri);
405237e3ad0SNicolas Pitre }
4061da177e4SLinus Torvalds }
407587c9061SRoman Zippel calc_newval:
408f8f69dc0SMasahiro Yamada if (sym->dir_dep.tri < sym->rev_dep.tri)
409f8f69dc0SMasahiro Yamada sym_warn_unmet_dep(sym);
410587c9061SRoman Zippel newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
411587c9061SRoman Zippel }
412def2fbffSMasahiro Yamada if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
4131da177e4SLinus Torvalds newval.tri = yes;
4141da177e4SLinus Torvalds break;
4151da177e4SLinus Torvalds case S_STRING:
4161da177e4SLinus Torvalds case S_HEX:
4171da177e4SLinus Torvalds case S_INT:
418cb67ab2cSMasahiro Yamada if (sym->visible != no && sym_has_value(sym)) {
4190c1822e6SRoman Zippel newval.val = sym->def[S_DEF_USER].val;
4201da177e4SLinus Torvalds break;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds prop = sym_get_default_prop(sym);
4231da177e4SLinus Torvalds if (prop) {
4241da177e4SLinus Torvalds struct symbol *ds = prop_get_symbol(prop);
4251da177e4SLinus Torvalds if (ds) {
4261da177e4SLinus Torvalds sym->flags |= SYMBOL_WRITE;
4271da177e4SLinus Torvalds sym_calc_value(ds);
4281da177e4SLinus Torvalds newval.val = ds->curr.val;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds }
4311da177e4SLinus Torvalds break;
4321da177e4SLinus Torvalds default:
4331da177e4SLinus Torvalds ;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds
4361da177e4SLinus Torvalds sym->curr = newval;
4371da177e4SLinus Torvalds if (sym_is_choice(sym) && newval.tri == yes)
4381da177e4SLinus Torvalds sym->curr.val = sym_calc_choice(sym);
4394cf3cbe2SRoman Zippel sym_validate_range(sym);
4401da177e4SLinus Torvalds
441face4374SRoman Zippel if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
4421da177e4SLinus Torvalds sym_set_changed(sym);
443face4374SRoman Zippel if (modules_sym == sym) {
444face4374SRoman Zippel sym_set_all_changed();
4451da177e4SLinus Torvalds modules_val = modules_sym->curr.tri;
446face4374SRoman Zippel }
447face4374SRoman Zippel }
4481da177e4SLinus Torvalds
4491da177e4SLinus Torvalds if (sym_is_choice(sym)) {
4507a962923SRoman Zippel struct symbol *choice_sym;
4517a962923SRoman Zippel
4521da177e4SLinus Torvalds prop = sym_get_choice_prop(sym);
4537a962923SRoman Zippel expr_list_for_each_sym(prop->expr, e, choice_sym) {
4540a28c47bSJan Beulich if ((sym->flags & SYMBOL_WRITE) &&
4550a28c47bSJan Beulich choice_sym->visible != no)
4560a28c47bSJan Beulich choice_sym->flags |= SYMBOL_WRITE;
4570a28c47bSJan Beulich if (sym->flags & SYMBOL_CHANGED)
4587a962923SRoman Zippel sym_set_changed(choice_sym);
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds }
4615a1aa8a1SRoman Zippel
462693359f7SDirk Gouders if (sym->flags & SYMBOL_NO_WRITE)
4635a1aa8a1SRoman Zippel sym->flags &= ~SYMBOL_WRITE;
464fbe98bb9SArve Hjønnevåg
465fbe98bb9SArve Hjønnevåg if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
466fbe98bb9SArve Hjønnevåg set_all_choice_values(sym);
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds
sym_clear_all_valid(void)4691da177e4SLinus Torvalds void sym_clear_all_valid(void)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds struct symbol *sym;
4721da177e4SLinus Torvalds int i;
4731da177e4SLinus Torvalds
4741da177e4SLinus Torvalds for_all_symbols(i, sym)
4751da177e4SLinus Torvalds sym->flags &= ~SYMBOL_VALID;
4765ee54659SMasahiro Yamada conf_set_changed(true);
4771da177e4SLinus Torvalds sym_calc_value(modules_sym);
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds
sym_tristate_within_range(struct symbol * sym,tristate val)4801da177e4SLinus Torvalds bool sym_tristate_within_range(struct symbol *sym, tristate val)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds int type = sym_get_type(sym);
4831da177e4SLinus Torvalds
4841da177e4SLinus Torvalds if (sym->visible == no)
4851da177e4SLinus Torvalds return false;
4861da177e4SLinus Torvalds
4871da177e4SLinus Torvalds if (type != S_BOOLEAN && type != S_TRISTATE)
4881da177e4SLinus Torvalds return false;
4891da177e4SLinus Torvalds
4901da177e4SLinus Torvalds if (type == S_BOOLEAN && val == mod)
4911da177e4SLinus Torvalds return false;
4921da177e4SLinus Torvalds if (sym->visible <= sym->rev_dep.tri)
4931da177e4SLinus Torvalds return false;
4941da177e4SLinus Torvalds if (sym_is_choice_value(sym) && sym->visible == yes)
4951da177e4SLinus Torvalds return val == yes;
4961da177e4SLinus Torvalds return val >= sym->rev_dep.tri && val <= sym->visible;
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds
sym_set_tristate_value(struct symbol * sym,tristate val)4991da177e4SLinus Torvalds bool sym_set_tristate_value(struct symbol *sym, tristate val)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds tristate oldval = sym_get_tristate_value(sym);
5021da177e4SLinus Torvalds
5031da177e4SLinus Torvalds if (oldval != val && !sym_tristate_within_range(sym, val))
5041da177e4SLinus Torvalds return false;
5051da177e4SLinus Torvalds
506669bfad9SRoman Zippel if (!(sym->flags & SYMBOL_DEF_USER)) {
507669bfad9SRoman Zippel sym->flags |= SYMBOL_DEF_USER;
5081da177e4SLinus Torvalds sym_set_changed(sym);
5091da177e4SLinus Torvalds }
5103f23ca2bSRoman Zippel /*
5113f23ca2bSRoman Zippel * setting a choice value also resets the new flag of the choice
5123f23ca2bSRoman Zippel * symbol and all other choice values.
5133f23ca2bSRoman Zippel */
5141da177e4SLinus Torvalds if (sym_is_choice_value(sym) && val == yes) {
5151da177e4SLinus Torvalds struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
5163f23ca2bSRoman Zippel struct property *prop;
5173f23ca2bSRoman Zippel struct expr *e;
5181da177e4SLinus Torvalds
5190c1822e6SRoman Zippel cs->def[S_DEF_USER].val = sym;
520669bfad9SRoman Zippel cs->flags |= SYMBOL_DEF_USER;
5213f23ca2bSRoman Zippel prop = sym_get_choice_prop(cs);
5223f23ca2bSRoman Zippel for (e = prop->expr; e; e = e->left.expr) {
5233f23ca2bSRoman Zippel if (e->right.sym->visible != no)
524669bfad9SRoman Zippel e->right.sym->flags |= SYMBOL_DEF_USER;
5253f23ca2bSRoman Zippel }
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds
5280c1822e6SRoman Zippel sym->def[S_DEF_USER].tri = val;
529face4374SRoman Zippel if (oldval != val)
5301da177e4SLinus Torvalds sym_clear_all_valid();
5311da177e4SLinus Torvalds
5321da177e4SLinus Torvalds return true;
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds
sym_toggle_tristate_value(struct symbol * sym)5351da177e4SLinus Torvalds tristate sym_toggle_tristate_value(struct symbol *sym)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds tristate oldval, newval;
5381da177e4SLinus Torvalds
5391da177e4SLinus Torvalds oldval = newval = sym_get_tristate_value(sym);
5401da177e4SLinus Torvalds do {
5411da177e4SLinus Torvalds switch (newval) {
5421da177e4SLinus Torvalds case no:
5431da177e4SLinus Torvalds newval = mod;
5441da177e4SLinus Torvalds break;
5451da177e4SLinus Torvalds case mod:
5461da177e4SLinus Torvalds newval = yes;
5471da177e4SLinus Torvalds break;
5481da177e4SLinus Torvalds case yes:
5491da177e4SLinus Torvalds newval = no;
5501da177e4SLinus Torvalds break;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds if (sym_set_tristate_value(sym, newval))
5531da177e4SLinus Torvalds break;
5541da177e4SLinus Torvalds } while (oldval != newval);
5551da177e4SLinus Torvalds return newval;
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds
sym_string_valid(struct symbol * sym,const char * str)5581da177e4SLinus Torvalds bool sym_string_valid(struct symbol *sym, const char *str)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds signed char ch;
5611da177e4SLinus Torvalds
5621da177e4SLinus Torvalds switch (sym->type) {
5631da177e4SLinus Torvalds case S_STRING:
5641da177e4SLinus Torvalds return true;
5651da177e4SLinus Torvalds case S_INT:
5661da177e4SLinus Torvalds ch = *str++;
5671da177e4SLinus Torvalds if (ch == '-')
5681da177e4SLinus Torvalds ch = *str++;
5691da177e4SLinus Torvalds if (!isdigit(ch))
5701da177e4SLinus Torvalds return false;
5711da177e4SLinus Torvalds if (ch == '0' && *str != 0)
5721da177e4SLinus Torvalds return false;
5731da177e4SLinus Torvalds while ((ch = *str++)) {
5741da177e4SLinus Torvalds if (!isdigit(ch))
5751da177e4SLinus Torvalds return false;
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds return true;
5781da177e4SLinus Torvalds case S_HEX:
5791da177e4SLinus Torvalds if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
5801da177e4SLinus Torvalds str += 2;
5811da177e4SLinus Torvalds ch = *str++;
5821da177e4SLinus Torvalds do {
5831da177e4SLinus Torvalds if (!isxdigit(ch))
5841da177e4SLinus Torvalds return false;
5851da177e4SLinus Torvalds } while ((ch = *str++));
5861da177e4SLinus Torvalds return true;
5871da177e4SLinus Torvalds case S_BOOLEAN:
5881da177e4SLinus Torvalds case S_TRISTATE:
5891da177e4SLinus Torvalds switch (str[0]) {
5901da177e4SLinus Torvalds case 'y': case 'Y':
5911da177e4SLinus Torvalds case 'm': case 'M':
5921da177e4SLinus Torvalds case 'n': case 'N':
5931da177e4SLinus Torvalds return true;
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds return false;
5961da177e4SLinus Torvalds default:
5971da177e4SLinus Torvalds return false;
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds
sym_string_within_range(struct symbol * sym,const char * str)6011da177e4SLinus Torvalds bool sym_string_within_range(struct symbol *sym, const char *str)
6021da177e4SLinus Torvalds {
6031da177e4SLinus Torvalds struct property *prop;
604129784abSKees Cook long long val;
6051da177e4SLinus Torvalds
6061da177e4SLinus Torvalds switch (sym->type) {
6071da177e4SLinus Torvalds case S_STRING:
6081da177e4SLinus Torvalds return sym_string_valid(sym, str);
6091da177e4SLinus Torvalds case S_INT:
6101da177e4SLinus Torvalds if (!sym_string_valid(sym, str))
6111da177e4SLinus Torvalds return false;
6121da177e4SLinus Torvalds prop = sym_get_range_prop(sym);
6131da177e4SLinus Torvalds if (!prop)
6141da177e4SLinus Torvalds return true;
615129784abSKees Cook val = strtoll(str, NULL, 10);
6164cf3cbe2SRoman Zippel return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
6174cf3cbe2SRoman Zippel val <= sym_get_range_val(prop->expr->right.sym, 10);
6181da177e4SLinus Torvalds case S_HEX:
6191da177e4SLinus Torvalds if (!sym_string_valid(sym, str))
6201da177e4SLinus Torvalds return false;
6211da177e4SLinus Torvalds prop = sym_get_range_prop(sym);
6221da177e4SLinus Torvalds if (!prop)
6231da177e4SLinus Torvalds return true;
624129784abSKees Cook val = strtoll(str, NULL, 16);
6254cf3cbe2SRoman Zippel return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
6264cf3cbe2SRoman Zippel val <= sym_get_range_val(prop->expr->right.sym, 16);
6271da177e4SLinus Torvalds case S_BOOLEAN:
6281da177e4SLinus Torvalds case S_TRISTATE:
6291da177e4SLinus Torvalds switch (str[0]) {
6301da177e4SLinus Torvalds case 'y': case 'Y':
6311da177e4SLinus Torvalds return sym_tristate_within_range(sym, yes);
6321da177e4SLinus Torvalds case 'm': case 'M':
6331da177e4SLinus Torvalds return sym_tristate_within_range(sym, mod);
6341da177e4SLinus Torvalds case 'n': case 'N':
6351da177e4SLinus Torvalds return sym_tristate_within_range(sym, no);
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds return false;
6381da177e4SLinus Torvalds default:
6391da177e4SLinus Torvalds return false;
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds
sym_set_string_value(struct symbol * sym,const char * newval)6431da177e4SLinus Torvalds bool sym_set_string_value(struct symbol *sym, const char *newval)
6441da177e4SLinus Torvalds {
6451da177e4SLinus Torvalds const char *oldval;
6461da177e4SLinus Torvalds char *val;
6471da177e4SLinus Torvalds int size;
6481da177e4SLinus Torvalds
6491da177e4SLinus Torvalds switch (sym->type) {
6501da177e4SLinus Torvalds case S_BOOLEAN:
6511da177e4SLinus Torvalds case S_TRISTATE:
6521da177e4SLinus Torvalds switch (newval[0]) {
6531da177e4SLinus Torvalds case 'y': case 'Y':
6541da177e4SLinus Torvalds return sym_set_tristate_value(sym, yes);
6551da177e4SLinus Torvalds case 'm': case 'M':
6561da177e4SLinus Torvalds return sym_set_tristate_value(sym, mod);
6571da177e4SLinus Torvalds case 'n': case 'N':
6581da177e4SLinus Torvalds return sym_set_tristate_value(sym, no);
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds return false;
6611da177e4SLinus Torvalds default:
6621da177e4SLinus Torvalds ;
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds
6651da177e4SLinus Torvalds if (!sym_string_within_range(sym, newval))
6661da177e4SLinus Torvalds return false;
6671da177e4SLinus Torvalds
668669bfad9SRoman Zippel if (!(sym->flags & SYMBOL_DEF_USER)) {
669669bfad9SRoman Zippel sym->flags |= SYMBOL_DEF_USER;
6701da177e4SLinus Torvalds sym_set_changed(sym);
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds
6730c1822e6SRoman Zippel oldval = sym->def[S_DEF_USER].val;
6741da177e4SLinus Torvalds size = strlen(newval) + 1;
6751da177e4SLinus Torvalds if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
6761da177e4SLinus Torvalds size += 2;
677177acf78SAlan Cox sym->def[S_DEF_USER].val = val = xmalloc(size);
6781da177e4SLinus Torvalds *val++ = '0';
6791da177e4SLinus Torvalds *val++ = 'x';
6801da177e4SLinus Torvalds } else if (!oldval || strcmp(oldval, newval))
681177acf78SAlan Cox sym->def[S_DEF_USER].val = val = xmalloc(size);
6821da177e4SLinus Torvalds else
6831da177e4SLinus Torvalds return true;
6841da177e4SLinus Torvalds
6851da177e4SLinus Torvalds strcpy(val, newval);
6861da177e4SLinus Torvalds free((void *)oldval);
6871da177e4SLinus Torvalds sym_clear_all_valid();
6881da177e4SLinus Torvalds
6891da177e4SLinus Torvalds return true;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds
6927cf3d73bSSam Ravnborg /*
6937cf3d73bSSam Ravnborg * Find the default value associated to a symbol.
6947cf3d73bSSam Ravnborg * For tristate symbol handle the modules=n case
6957cf3d73bSSam Ravnborg * in which case "m" becomes "y".
6967cf3d73bSSam Ravnborg * If the symbol does not have any default then fallback
6977cf3d73bSSam Ravnborg * to the fixed default values.
6987cf3d73bSSam Ravnborg */
sym_get_string_default(struct symbol * sym)6997cf3d73bSSam Ravnborg const char *sym_get_string_default(struct symbol *sym)
7007cf3d73bSSam Ravnborg {
7017cf3d73bSSam Ravnborg struct property *prop;
7027cf3d73bSSam Ravnborg struct symbol *ds;
7037cf3d73bSSam Ravnborg const char *str;
7047cf3d73bSSam Ravnborg tristate val;
7057cf3d73bSSam Ravnborg
7067cf3d73bSSam Ravnborg sym_calc_visibility(sym);
7077cf3d73bSSam Ravnborg sym_calc_value(modules_sym);
7087cf3d73bSSam Ravnborg val = symbol_no.curr.tri;
7097cf3d73bSSam Ravnborg str = symbol_empty.curr.val;
7107cf3d73bSSam Ravnborg
7117cf3d73bSSam Ravnborg /* If symbol has a default value look it up */
7127cf3d73bSSam Ravnborg prop = sym_get_default_prop(sym);
7137cf3d73bSSam Ravnborg if (prop != NULL) {
7147cf3d73bSSam Ravnborg switch (sym->type) {
7157cf3d73bSSam Ravnborg case S_BOOLEAN:
7167cf3d73bSSam Ravnborg case S_TRISTATE:
717579fb8e7SArnaud Lacombe /* The visibility may limit the value from yes => mod */
7187cf3d73bSSam Ravnborg val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
7197cf3d73bSSam Ravnborg break;
7207cf3d73bSSam Ravnborg default:
7217cf3d73bSSam Ravnborg /*
7227cf3d73bSSam Ravnborg * The following fails to handle the situation
7237cf3d73bSSam Ravnborg * where a default value is further limited by
7247cf3d73bSSam Ravnborg * the valid range.
7257cf3d73bSSam Ravnborg */
7267cf3d73bSSam Ravnborg ds = prop_get_symbol(prop);
7277cf3d73bSSam Ravnborg if (ds != NULL) {
7287cf3d73bSSam Ravnborg sym_calc_value(ds);
7297cf3d73bSSam Ravnborg str = (const char *)ds->curr.val;
7307cf3d73bSSam Ravnborg }
7317cf3d73bSSam Ravnborg }
7327cf3d73bSSam Ravnborg }
7337cf3d73bSSam Ravnborg
7347cf3d73bSSam Ravnborg /* Handle select statements */
7357cf3d73bSSam Ravnborg val = EXPR_OR(val, sym->rev_dep.tri);
7367cf3d73bSSam Ravnborg
7377cf3d73bSSam Ravnborg /* transpose mod to yes if modules are not enabled */
7387cf3d73bSSam Ravnborg if (val == mod)
7397cf3d73bSSam Ravnborg if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
7407cf3d73bSSam Ravnborg val = yes;
7417cf3d73bSSam Ravnborg
7427cf3d73bSSam Ravnborg /* transpose mod to yes if type is bool */
7437cf3d73bSSam Ravnborg if (sym->type == S_BOOLEAN && val == mod)
7447cf3d73bSSam Ravnborg val = yes;
7457cf3d73bSSam Ravnborg
746237e3ad0SNicolas Pitre /* adjust the default value if this symbol is implied by another */
747237e3ad0SNicolas Pitre if (val < sym->implied.tri)
748237e3ad0SNicolas Pitre val = sym->implied.tri;
749237e3ad0SNicolas Pitre
7507cf3d73bSSam Ravnborg switch (sym->type) {
7517cf3d73bSSam Ravnborg case S_BOOLEAN:
7527cf3d73bSSam Ravnborg case S_TRISTATE:
7537cf3d73bSSam Ravnborg switch (val) {
7547cf3d73bSSam Ravnborg case no: return "n";
7557cf3d73bSSam Ravnborg case mod: return "m";
7567cf3d73bSSam Ravnborg case yes: return "y";
7577cf3d73bSSam Ravnborg }
7587cf3d73bSSam Ravnborg case S_INT:
7597cf3d73bSSam Ravnborg case S_HEX:
7607cf3d73bSSam Ravnborg return str;
7617cf3d73bSSam Ravnborg case S_STRING:
7627cf3d73bSSam Ravnborg return str;
7637cf3d73bSSam Ravnborg case S_UNKNOWN:
7647cf3d73bSSam Ravnborg break;
7657cf3d73bSSam Ravnborg }
7667cf3d73bSSam Ravnborg return "";
7677cf3d73bSSam Ravnborg }
7687cf3d73bSSam Ravnborg
sym_get_string_value(struct symbol * sym)7691da177e4SLinus Torvalds const char *sym_get_string_value(struct symbol *sym)
7701da177e4SLinus Torvalds {
7711da177e4SLinus Torvalds tristate val;
7721da177e4SLinus Torvalds
7731da177e4SLinus Torvalds switch (sym->type) {
7741da177e4SLinus Torvalds case S_BOOLEAN:
7751da177e4SLinus Torvalds case S_TRISTATE:
7761da177e4SLinus Torvalds val = sym_get_tristate_value(sym);
7771da177e4SLinus Torvalds switch (val) {
7781da177e4SLinus Torvalds case no:
7791da177e4SLinus Torvalds return "n";
7801da177e4SLinus Torvalds case mod:
781*2b6e818fSMasahiro Yamada return "m";
7821da177e4SLinus Torvalds case yes:
7831da177e4SLinus Torvalds return "y";
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds break;
7861da177e4SLinus Torvalds default:
7871da177e4SLinus Torvalds ;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds return (const char *)sym->curr.val;
7901da177e4SLinus Torvalds }
7911da177e4SLinus Torvalds
sym_is_changeable(struct symbol * sym)792baa23ec8SMarco Ammon bool sym_is_changeable(struct symbol *sym)
7931da177e4SLinus Torvalds {
7941da177e4SLinus Torvalds return sym->visible > sym->rev_dep.tri;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds
strhash(const char * s)797e66f25d7SAndi Kleen static unsigned strhash(const char *s)
798e66f25d7SAndi Kleen {
799e66f25d7SAndi Kleen /* fnv32 hash */
800e66f25d7SAndi Kleen unsigned hash = 2166136261U;
801e66f25d7SAndi Kleen for (; *s; s++)
802e66f25d7SAndi Kleen hash = (hash ^ *s) * 0x01000193;
803e66f25d7SAndi Kleen return hash;
804e66f25d7SAndi Kleen }
805e66f25d7SAndi Kleen
sym_lookup(const char * name,int flags)8065a1aa8a1SRoman Zippel struct symbol *sym_lookup(const char *name, int flags)
8071da177e4SLinus Torvalds {
8081da177e4SLinus Torvalds struct symbol *symbol;
8091da177e4SLinus Torvalds char *new_name;
810e66f25d7SAndi Kleen int hash;
8111da177e4SLinus Torvalds
8121da177e4SLinus Torvalds if (name) {
8131da177e4SLinus Torvalds if (name[0] && !name[1]) {
8141da177e4SLinus Torvalds switch (name[0]) {
8151da177e4SLinus Torvalds case 'y': return &symbol_yes;
8161da177e4SLinus Torvalds case 'm': return &symbol_mod;
8171da177e4SLinus Torvalds case 'n': return &symbol_no;
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds }
820e66f25d7SAndi Kleen hash = strhash(name) % SYMBOL_HASHSIZE;
8211da177e4SLinus Torvalds
8221da177e4SLinus Torvalds for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
823e66f25d7SAndi Kleen if (symbol->name &&
824e66f25d7SAndi Kleen !strcmp(symbol->name, name) &&
8255a1aa8a1SRoman Zippel (flags ? symbol->flags & flags
8265a1aa8a1SRoman Zippel : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
8271da177e4SLinus Torvalds return symbol;
8281da177e4SLinus Torvalds }
829cd81fc82SMasahiro Yamada new_name = xstrdup(name);
8301da177e4SLinus Torvalds } else {
8311da177e4SLinus Torvalds new_name = NULL;
832e66f25d7SAndi Kleen hash = 0;
8331da177e4SLinus Torvalds }
8341da177e4SLinus Torvalds
835177acf78SAlan Cox symbol = xmalloc(sizeof(*symbol));
8361da177e4SLinus Torvalds memset(symbol, 0, sizeof(*symbol));
8371da177e4SLinus Torvalds symbol->name = new_name;
8381da177e4SLinus Torvalds symbol->type = S_UNKNOWN;
839cfc6eea9SMasahiro Yamada symbol->flags = flags;
8401da177e4SLinus Torvalds
8411da177e4SLinus Torvalds symbol->next = symbol_hash[hash];
8421da177e4SLinus Torvalds symbol_hash[hash] = symbol;
8431da177e4SLinus Torvalds
8441da177e4SLinus Torvalds return symbol;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds
sym_find(const char * name)8471da177e4SLinus Torvalds struct symbol *sym_find(const char *name)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds struct symbol *symbol = NULL;
8501da177e4SLinus Torvalds int hash = 0;
8511da177e4SLinus Torvalds
8521da177e4SLinus Torvalds if (!name)
8531da177e4SLinus Torvalds return NULL;
8541da177e4SLinus Torvalds
8551da177e4SLinus Torvalds if (name[0] && !name[1]) {
8561da177e4SLinus Torvalds switch (name[0]) {
8571da177e4SLinus Torvalds case 'y': return &symbol_yes;
8581da177e4SLinus Torvalds case 'm': return &symbol_mod;
8591da177e4SLinus Torvalds case 'n': return &symbol_no;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds }
862e66f25d7SAndi Kleen hash = strhash(name) % SYMBOL_HASHSIZE;
8631da177e4SLinus Torvalds
8641da177e4SLinus Torvalds for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
865e66f25d7SAndi Kleen if (symbol->name &&
866e66f25d7SAndi Kleen !strcmp(symbol->name, name) &&
8671da177e4SLinus Torvalds !(symbol->flags & SYMBOL_CONST))
8681da177e4SLinus Torvalds break;
8691da177e4SLinus Torvalds }
8701da177e4SLinus Torvalds
8711da177e4SLinus Torvalds return symbol;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds
874193b40aeSYann E. MORIN struct sym_match {
875193b40aeSYann E. MORIN struct symbol *sym;
876193b40aeSYann E. MORIN off_t so, eo;
877193b40aeSYann E. MORIN };
878193b40aeSYann E. MORIN
879193b40aeSYann E. MORIN /* Compare matched symbols as thus:
880193b40aeSYann E. MORIN * - first, symbols that match exactly
881193b40aeSYann E. MORIN * - then, alphabetical sort
882193b40aeSYann E. MORIN */
sym_rel_comp(const void * sym1,const void * sym2)883193b40aeSYann E. MORIN static int sym_rel_comp(const void *sym1, const void *sym2)
884193b40aeSYann E. MORIN {
885508382a0SYann E. MORIN const struct sym_match *s1 = sym1;
886508382a0SYann E. MORIN const struct sym_match *s2 = sym2;
88726e933e3SYann E. MORIN int exact1, exact2;
888193b40aeSYann E. MORIN
889193b40aeSYann E. MORIN /* Exact match:
890193b40aeSYann E. MORIN * - if matched length on symbol s1 is the length of that symbol,
891193b40aeSYann E. MORIN * then this symbol should come first;
892193b40aeSYann E. MORIN * - if matched length on symbol s2 is the length of that symbol,
893193b40aeSYann E. MORIN * then this symbol should come first.
894193b40aeSYann E. MORIN * Note: since the search can be a regexp, both symbols may match
895193b40aeSYann E. MORIN * exactly; if this is the case, we can't decide which comes first,
896193b40aeSYann E. MORIN * and we fallback to sorting alphabetically.
897193b40aeSYann E. MORIN */
89826e933e3SYann E. MORIN exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
89926e933e3SYann E. MORIN exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
90026e933e3SYann E. MORIN if (exact1 && !exact2)
901193b40aeSYann E. MORIN return -1;
90226e933e3SYann E. MORIN if (!exact1 && exact2)
903193b40aeSYann E. MORIN return 1;
904193b40aeSYann E. MORIN
905193b40aeSYann E. MORIN /* As a fallback, sort symbols alphabetically */
906193b40aeSYann E. MORIN return strcmp(s1->sym->name, s2->sym->name);
907193b40aeSYann E. MORIN }
908193b40aeSYann E. MORIN
sym_re_search(const char * pattern)9091da177e4SLinus Torvalds struct symbol **sym_re_search(const char *pattern)
9101da177e4SLinus Torvalds {
9111da177e4SLinus Torvalds struct symbol *sym, **sym_arr = NULL;
912508382a0SYann E. MORIN struct sym_match *sym_match_arr = NULL;
9131da177e4SLinus Torvalds int i, cnt, size;
9141da177e4SLinus Torvalds regex_t re;
915193b40aeSYann E. MORIN regmatch_t match[1];
9161da177e4SLinus Torvalds
9171da177e4SLinus Torvalds cnt = size = 0;
9181da177e4SLinus Torvalds /* Skip if empty */
9191da177e4SLinus Torvalds if (strlen(pattern) == 0)
9201da177e4SLinus Torvalds return NULL;
921193b40aeSYann E. MORIN if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
9221da177e4SLinus Torvalds return NULL;
9231da177e4SLinus Torvalds
9241da177e4SLinus Torvalds for_all_symbols(i, sym) {
9251da177e4SLinus Torvalds if (sym->flags & SYMBOL_CONST || !sym->name)
9261da177e4SLinus Torvalds continue;
927193b40aeSYann E. MORIN if (regexec(&re, sym->name, 1, match, 0))
9281da177e4SLinus Torvalds continue;
9291407f97aSYann E. MORIN if (cnt >= size) {
930193b40aeSYann E. MORIN void *tmp;
9311da177e4SLinus Torvalds size += 16;
932508382a0SYann E. MORIN tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
933803b3519SYann E. MORIN if (!tmp)
934193b40aeSYann E. MORIN goto sym_re_search_free;
935193b40aeSYann E. MORIN sym_match_arr = tmp;
9361da177e4SLinus Torvalds }
937da6df879SLi Zefan sym_calc_value(sym);
938803b3519SYann E. MORIN /* As regexec returned 0, we know we have a match, so
939193b40aeSYann E. MORIN * we can use match[0].rm_[se]o without further checks
940193b40aeSYann E. MORIN */
941508382a0SYann E. MORIN sym_match_arr[cnt].so = match[0].rm_so;
942508382a0SYann E. MORIN sym_match_arr[cnt].eo = match[0].rm_eo;
943508382a0SYann E. MORIN sym_match_arr[cnt++].sym = sym;
9441da177e4SLinus Torvalds }
945193b40aeSYann E. MORIN if (sym_match_arr) {
946508382a0SYann E. MORIN qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
94788127daeSHeinrich Schuchardt sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
948193b40aeSYann E. MORIN if (!sym_arr)
949193b40aeSYann E. MORIN goto sym_re_search_free;
950193b40aeSYann E. MORIN for (i = 0; i < cnt; i++)
951508382a0SYann E. MORIN sym_arr[i] = sym_match_arr[i].sym;
9521da177e4SLinus Torvalds sym_arr[cnt] = NULL;
953193b40aeSYann E. MORIN }
954193b40aeSYann E. MORIN sym_re_search_free:
955508382a0SYann E. MORIN /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
956193b40aeSYann E. MORIN free(sym_match_arr);
9571da177e4SLinus Torvalds regfree(&re);
9581da177e4SLinus Torvalds
9591da177e4SLinus Torvalds return sym_arr;
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds
962d595cea6SRoman Zippel /*
963d595cea6SRoman Zippel * When we check for recursive dependencies we use a stack to save
964d595cea6SRoman Zippel * current state so we can print out relevant info to user.
965d595cea6SRoman Zippel * The entries are located on the call stack so no need to free memory.
9668d9dfe82SMartin Walch * Note insert() remove() must always match to properly clear the stack.
967d595cea6SRoman Zippel */
968d595cea6SRoman Zippel static struct dep_stack {
969d595cea6SRoman Zippel struct dep_stack *prev, *next;
970d595cea6SRoman Zippel struct symbol *sym;
971d595cea6SRoman Zippel struct property *prop;
972f498926cSMasahiro Yamada struct expr **expr;
973d595cea6SRoman Zippel } *check_top;
974d595cea6SRoman Zippel
dep_stack_insert(struct dep_stack * stack,struct symbol * sym)975d595cea6SRoman Zippel static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
976d595cea6SRoman Zippel {
977d595cea6SRoman Zippel memset(stack, 0, sizeof(*stack));
978d595cea6SRoman Zippel if (check_top)
979d595cea6SRoman Zippel check_top->next = stack;
980d595cea6SRoman Zippel stack->prev = check_top;
981d595cea6SRoman Zippel stack->sym = sym;
982d595cea6SRoman Zippel check_top = stack;
983d595cea6SRoman Zippel }
984d595cea6SRoman Zippel
dep_stack_remove(void)985d595cea6SRoman Zippel static void dep_stack_remove(void)
986d595cea6SRoman Zippel {
987d595cea6SRoman Zippel check_top = check_top->prev;
988d595cea6SRoman Zippel if (check_top)
989d595cea6SRoman Zippel check_top->next = NULL;
990d595cea6SRoman Zippel }
991d595cea6SRoman Zippel
992d595cea6SRoman Zippel /*
993d595cea6SRoman Zippel * Called when we have detected a recursive dependency.
994d595cea6SRoman Zippel * check_top point to the top of the stact so we use
995d595cea6SRoman Zippel * the ->prev pointer to locate the bottom of the stack.
996d595cea6SRoman Zippel */
sym_check_print_recursive(struct symbol * last_sym)997d595cea6SRoman Zippel static void sym_check_print_recursive(struct symbol *last_sym)
998d595cea6SRoman Zippel {
999d595cea6SRoman Zippel struct dep_stack *stack;
1000d595cea6SRoman Zippel struct symbol *sym, *next_sym;
1001d595cea6SRoman Zippel struct menu *menu = NULL;
1002d595cea6SRoman Zippel struct property *prop;
1003d595cea6SRoman Zippel struct dep_stack cv_stack;
1004d595cea6SRoman Zippel
1005d595cea6SRoman Zippel if (sym_is_choice_value(last_sym)) {
1006d595cea6SRoman Zippel dep_stack_insert(&cv_stack, last_sym);
1007d595cea6SRoman Zippel last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1008d595cea6SRoman Zippel }
1009d595cea6SRoman Zippel
1010d595cea6SRoman Zippel for (stack = check_top; stack != NULL; stack = stack->prev)
1011d595cea6SRoman Zippel if (stack->sym == last_sym)
1012d595cea6SRoman Zippel break;
1013d595cea6SRoman Zippel if (!stack) {
1014d595cea6SRoman Zippel fprintf(stderr, "unexpected recursive dependency error\n");
1015d595cea6SRoman Zippel return;
1016d595cea6SRoman Zippel }
1017d595cea6SRoman Zippel
1018d595cea6SRoman Zippel for (; stack; stack = stack->next) {
1019d595cea6SRoman Zippel sym = stack->sym;
1020d595cea6SRoman Zippel next_sym = stack->next ? stack->next->sym : last_sym;
1021d595cea6SRoman Zippel prop = stack->prop;
10223643f849SSam Ravnborg if (prop == NULL)
10233643f849SSam Ravnborg prop = stack->sym->prop;
1024d595cea6SRoman Zippel
1025d595cea6SRoman Zippel /* for choice values find the menu entry (used below) */
1026d595cea6SRoman Zippel if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1027d595cea6SRoman Zippel for (prop = sym->prop; prop; prop = prop->next) {
1028d595cea6SRoman Zippel menu = prop->menu;
1029d595cea6SRoman Zippel if (prop->menu)
1030d595cea6SRoman Zippel break;
1031d595cea6SRoman Zippel }
1032d595cea6SRoman Zippel }
1033d595cea6SRoman Zippel if (stack->sym == last_sym)
1034d595cea6SRoman Zippel fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1035d595cea6SRoman Zippel prop->file->name, prop->lineno);
1036e3b03bf2SMasahiro Yamada
1037f498926cSMasahiro Yamada if (sym_is_choice(sym)) {
1038d595cea6SRoman Zippel fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1039d595cea6SRoman Zippel menu->file->name, menu->lineno,
1040d595cea6SRoman Zippel sym->name ? sym->name : "<choice>",
1041d595cea6SRoman Zippel next_sym->name ? next_sym->name : "<choice>");
1042d595cea6SRoman Zippel } else if (sym_is_choice_value(sym)) {
1043d595cea6SRoman Zippel fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1044d595cea6SRoman Zippel menu->file->name, menu->lineno,
1045d595cea6SRoman Zippel sym->name ? sym->name : "<choice>",
1046d595cea6SRoman Zippel next_sym->name ? next_sym->name : "<choice>");
1047f498926cSMasahiro Yamada } else if (stack->expr == &sym->dir_dep.expr) {
1048f498926cSMasahiro Yamada fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1049d595cea6SRoman Zippel prop->file->name, prop->lineno,
1050d595cea6SRoman Zippel sym->name ? sym->name : "<choice>",
1051d595cea6SRoman Zippel next_sym->name ? next_sym->name : "<choice>");
1052f498926cSMasahiro Yamada } else if (stack->expr == &sym->rev_dep.expr) {
1053f498926cSMasahiro Yamada fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1054f498926cSMasahiro Yamada prop->file->name, prop->lineno,
1055f498926cSMasahiro Yamada sym->name ? sym->name : "<choice>",
1056f498926cSMasahiro Yamada next_sym->name ? next_sym->name : "<choice>");
1057f498926cSMasahiro Yamada } else if (stack->expr == &sym->implied.expr) {
1058f498926cSMasahiro Yamada fprintf(stderr, "%s:%d:\tsymbol %s is implied by %s\n",
1059f498926cSMasahiro Yamada prop->file->name, prop->lineno,
1060f498926cSMasahiro Yamada sym->name ? sym->name : "<choice>",
1061f498926cSMasahiro Yamada next_sym->name ? next_sym->name : "<choice>");
1062f498926cSMasahiro Yamada } else if (stack->expr) {
1063f498926cSMasahiro Yamada fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1064f498926cSMasahiro Yamada prop->file->name, prop->lineno,
1065f498926cSMasahiro Yamada sym->name ? sym->name : "<choice>",
1066f498926cSMasahiro Yamada prop_get_type_name(prop->type),
1067f498926cSMasahiro Yamada next_sym->name ? next_sym->name : "<choice>");
1068f498926cSMasahiro Yamada } else {
1069f498926cSMasahiro Yamada fprintf(stderr, "%s:%d:\tsymbol %s %s is visible depending on %s\n",
1070f498926cSMasahiro Yamada prop->file->name, prop->lineno,
1071f498926cSMasahiro Yamada sym->name ? sym->name : "<choice>",
1072f498926cSMasahiro Yamada prop_get_type_name(prop->type),
1073f498926cSMasahiro Yamada next_sym->name ? next_sym->name : "<choice>");
1074d595cea6SRoman Zippel }
1075d595cea6SRoman Zippel }
1076d595cea6SRoman Zippel
1077e3b03bf2SMasahiro Yamada fprintf(stderr,
1078cd238effSMauro Carvalho Chehab "For a resolution refer to Documentation/kbuild/kconfig-language.rst\n"
1079e3b03bf2SMasahiro Yamada "subsection \"Kconfig recursive dependency limitations\"\n"
1080e3b03bf2SMasahiro Yamada "\n");
1081e3b03bf2SMasahiro Yamada
1082d595cea6SRoman Zippel if (check_top == &cv_stack)
1083d595cea6SRoman Zippel dep_stack_remove();
1084d595cea6SRoman Zippel }
10851da177e4SLinus Torvalds
sym_check_expr_deps(struct expr * e)10861da177e4SLinus Torvalds static struct symbol *sym_check_expr_deps(struct expr *e)
10871da177e4SLinus Torvalds {
10881da177e4SLinus Torvalds struct symbol *sym;
10891da177e4SLinus Torvalds
10901da177e4SLinus Torvalds if (!e)
10911da177e4SLinus Torvalds return NULL;
10921da177e4SLinus Torvalds switch (e->type) {
10931da177e4SLinus Torvalds case E_OR:
10941da177e4SLinus Torvalds case E_AND:
10951da177e4SLinus Torvalds sym = sym_check_expr_deps(e->left.expr);
10961da177e4SLinus Torvalds if (sym)
10971da177e4SLinus Torvalds return sym;
10981da177e4SLinus Torvalds return sym_check_expr_deps(e->right.expr);
10991da177e4SLinus Torvalds case E_NOT:
11001da177e4SLinus Torvalds return sym_check_expr_deps(e->left.expr);
11011da177e4SLinus Torvalds case E_EQUAL:
110231847b67SJan Beulich case E_GEQ:
110331847b67SJan Beulich case E_GTH:
110431847b67SJan Beulich case E_LEQ:
110531847b67SJan Beulich case E_LTH:
11061da177e4SLinus Torvalds case E_UNEQUAL:
11071da177e4SLinus Torvalds sym = sym_check_deps(e->left.sym);
11081da177e4SLinus Torvalds if (sym)
11091da177e4SLinus Torvalds return sym;
11101da177e4SLinus Torvalds return sym_check_deps(e->right.sym);
11111da177e4SLinus Torvalds case E_SYMBOL:
11121da177e4SLinus Torvalds return sym_check_deps(e->left.sym);
11131da177e4SLinus Torvalds default:
11141da177e4SLinus Torvalds break;
11151da177e4SLinus Torvalds }
11169e3e10c7SMasahiro Yamada fprintf(stderr, "Oops! How to check %d?\n", e->type);
11171da177e4SLinus Torvalds return NULL;
11181da177e4SLinus Torvalds }
11191da177e4SLinus Torvalds
11205447d34bSSam Ravnborg /* return NULL when dependencies are OK */
sym_check_sym_deps(struct symbol * sym)112148981178SRoman Zippel static struct symbol *sym_check_sym_deps(struct symbol *sym)
112248981178SRoman Zippel {
112348981178SRoman Zippel struct symbol *sym2;
112448981178SRoman Zippel struct property *prop;
1125d595cea6SRoman Zippel struct dep_stack stack;
1126d595cea6SRoman Zippel
1127d595cea6SRoman Zippel dep_stack_insert(&stack, sym);
112848981178SRoman Zippel
1129f498926cSMasahiro Yamada stack.expr = &sym->dir_dep.expr;
1130f498926cSMasahiro Yamada sym2 = sym_check_expr_deps(sym->dir_dep.expr);
1131f498926cSMasahiro Yamada if (sym2)
1132f498926cSMasahiro Yamada goto out;
1133f498926cSMasahiro Yamada
1134f498926cSMasahiro Yamada stack.expr = &sym->rev_dep.expr;
113548981178SRoman Zippel sym2 = sym_check_expr_deps(sym->rev_dep.expr);
113648981178SRoman Zippel if (sym2)
1137d595cea6SRoman Zippel goto out;
113848981178SRoman Zippel
1139f498926cSMasahiro Yamada stack.expr = &sym->implied.expr;
11405e8c5299SMasahiro Yamada sym2 = sym_check_expr_deps(sym->implied.expr);
11415e8c5299SMasahiro Yamada if (sym2)
11425e8c5299SMasahiro Yamada goto out;
11435e8c5299SMasahiro Yamada
1144f498926cSMasahiro Yamada stack.expr = NULL;
1145f498926cSMasahiro Yamada
114648981178SRoman Zippel for (prop = sym->prop; prop; prop = prop->next) {
11475e8c5299SMasahiro Yamada if (prop->type == P_CHOICE || prop->type == P_SELECT ||
11485e8c5299SMasahiro Yamada prop->type == P_IMPLY)
114948981178SRoman Zippel continue;
1150d595cea6SRoman Zippel stack.prop = prop;
115148981178SRoman Zippel sym2 = sym_check_expr_deps(prop->visible.expr);
115248981178SRoman Zippel if (sym2)
115348981178SRoman Zippel break;
115448981178SRoman Zippel if (prop->type != P_DEFAULT || sym_is_choice(sym))
115548981178SRoman Zippel continue;
1156f498926cSMasahiro Yamada stack.expr = &prop->expr;
115748981178SRoman Zippel sym2 = sym_check_expr_deps(prop->expr);
115848981178SRoman Zippel if (sym2)
115948981178SRoman Zippel break;
1160d595cea6SRoman Zippel stack.expr = NULL;
116148981178SRoman Zippel }
116248981178SRoman Zippel
1163d595cea6SRoman Zippel out:
1164d595cea6SRoman Zippel dep_stack_remove();
1165d595cea6SRoman Zippel
116648981178SRoman Zippel return sym2;
116748981178SRoman Zippel }
116848981178SRoman Zippel
sym_check_choice_deps(struct symbol * choice)116948981178SRoman Zippel static struct symbol *sym_check_choice_deps(struct symbol *choice)
117048981178SRoman Zippel {
117148981178SRoman Zippel struct symbol *sym, *sym2;
117248981178SRoman Zippel struct property *prop;
117348981178SRoman Zippel struct expr *e;
1174d595cea6SRoman Zippel struct dep_stack stack;
1175d595cea6SRoman Zippel
1176d595cea6SRoman Zippel dep_stack_insert(&stack, choice);
117748981178SRoman Zippel
117848981178SRoman Zippel prop = sym_get_choice_prop(choice);
117948981178SRoman Zippel expr_list_for_each_sym(prop->expr, e, sym)
118048981178SRoman Zippel sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
118148981178SRoman Zippel
118248981178SRoman Zippel choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
118348981178SRoman Zippel sym2 = sym_check_sym_deps(choice);
118448981178SRoman Zippel choice->flags &= ~SYMBOL_CHECK;
118548981178SRoman Zippel if (sym2)
118648981178SRoman Zippel goto out;
118748981178SRoman Zippel
118848981178SRoman Zippel expr_list_for_each_sym(prop->expr, e, sym) {
118948981178SRoman Zippel sym2 = sym_check_sym_deps(sym);
1190d595cea6SRoman Zippel if (sym2)
119148981178SRoman Zippel break;
119248981178SRoman Zippel }
119348981178SRoman Zippel out:
119448981178SRoman Zippel expr_list_for_each_sym(prop->expr, e, sym)
119548981178SRoman Zippel sym->flags &= ~SYMBOL_CHECK;
119648981178SRoman Zippel
119748981178SRoman Zippel if (sym2 && sym_is_choice_value(sym2) &&
119848981178SRoman Zippel prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
119948981178SRoman Zippel sym2 = choice;
120048981178SRoman Zippel
1201d595cea6SRoman Zippel dep_stack_remove();
1202d595cea6SRoman Zippel
120348981178SRoman Zippel return sym2;
120448981178SRoman Zippel }
120548981178SRoman Zippel
sym_check_deps(struct symbol * sym)12061da177e4SLinus Torvalds struct symbol *sym_check_deps(struct symbol *sym)
12071da177e4SLinus Torvalds {
12081da177e4SLinus Torvalds struct symbol *sym2;
12091da177e4SLinus Torvalds struct property *prop;
12101da177e4SLinus Torvalds
12111da177e4SLinus Torvalds if (sym->flags & SYMBOL_CHECK) {
1212d595cea6SRoman Zippel sym_check_print_recursive(sym);
12131da177e4SLinus Torvalds return sym;
12141da177e4SLinus Torvalds }
12153f04e7ddSDavid Gibson if (sym->flags & SYMBOL_CHECKED)
12163f04e7ddSDavid Gibson return NULL;
12171da177e4SLinus Torvalds
121848981178SRoman Zippel if (sym_is_choice_value(sym)) {
1219d595cea6SRoman Zippel struct dep_stack stack;
1220d595cea6SRoman Zippel
122148981178SRoman Zippel /* for choice groups start the check with main choice symbol */
1222d595cea6SRoman Zippel dep_stack_insert(&stack, sym);
122348981178SRoman Zippel prop = sym_get_choice_prop(sym);
122448981178SRoman Zippel sym2 = sym_check_deps(prop_get_symbol(prop));
1225d595cea6SRoman Zippel dep_stack_remove();
122648981178SRoman Zippel } else if (sym_is_choice(sym)) {
122748981178SRoman Zippel sym2 = sym_check_choice_deps(sym);
122848981178SRoman Zippel } else {
12291da177e4SLinus Torvalds sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
123048981178SRoman Zippel sym2 = sym_check_sym_deps(sym);
12311da177e4SLinus Torvalds sym->flags &= ~SYMBOL_CHECK;
123248981178SRoman Zippel }
123348981178SRoman Zippel
12341da177e4SLinus Torvalds return sym2;
12351da177e4SLinus Torvalds }
12361da177e4SLinus Torvalds
prop_get_symbol(struct property * prop)12371da177e4SLinus Torvalds struct symbol *prop_get_symbol(struct property *prop)
12381da177e4SLinus Torvalds {
12391da177e4SLinus Torvalds if (prop->expr && (prop->expr->type == E_SYMBOL ||
12407a962923SRoman Zippel prop->expr->type == E_LIST))
12411da177e4SLinus Torvalds return prop->expr->left.sym;
12421da177e4SLinus Torvalds return NULL;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds
prop_get_type_name(enum prop_type type)12451da177e4SLinus Torvalds const char *prop_get_type_name(enum prop_type type)
12461da177e4SLinus Torvalds {
12471da177e4SLinus Torvalds switch (type) {
12481da177e4SLinus Torvalds case P_PROMPT:
12491da177e4SLinus Torvalds return "prompt";
12501da177e4SLinus Torvalds case P_COMMENT:
12511da177e4SLinus Torvalds return "comment";
12521da177e4SLinus Torvalds case P_MENU:
12531da177e4SLinus Torvalds return "menu";
12541da177e4SLinus Torvalds case P_DEFAULT:
12551da177e4SLinus Torvalds return "default";
12561da177e4SLinus Torvalds case P_CHOICE:
12571da177e4SLinus Torvalds return "choice";
12581da177e4SLinus Torvalds case P_SELECT:
12591da177e4SLinus Torvalds return "select";
1260237e3ad0SNicolas Pitre case P_IMPLY:
1261237e3ad0SNicolas Pitre return "imply";
12621da177e4SLinus Torvalds case P_RANGE:
12631da177e4SLinus Torvalds return "range";
126459e89e3dSSam Ravnborg case P_SYMBOL:
126559e89e3dSSam Ravnborg return "symbol";
12661da177e4SLinus Torvalds case P_UNKNOWN:
12671da177e4SLinus Torvalds break;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds return "unknown";
12701da177e4SLinus Torvalds }
1271