Blame view

scripts/kconfig/symbol.c 28.6 KB
0c8741001   Masahiro Yamada   kconfig: convert ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
  /*
   * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
7
8
9
10
   */
  
  #include <ctype.h>
  #include <stdlib.h>
  #include <string.h>
  #include <regex.h>
  #include <sys/utsname.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
15
  #include "lkc.h"
  
  struct symbol symbol_yes = {
  	.name = "y",
  	.curr = { "y", yes },
c0e150acd   Roman Zippel   kconfig: remove S...
16
  	.flags = SYMBOL_CONST|SYMBOL_VALID,
d41809ff7   Masahiro Yamada   kconfig: add 'sta...
17
18
19
  };
  
  struct symbol symbol_mod = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
  	.name = "m",
  	.curr = { "m", mod },
c0e150acd   Roman Zippel   kconfig: remove S...
22
  	.flags = SYMBOL_CONST|SYMBOL_VALID,
d41809ff7   Masahiro Yamada   kconfig: add 'sta...
23
24
25
  };
  
  struct symbol symbol_no = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
  	.name = "n",
  	.curr = { "n", no },
c0e150acd   Roman Zippel   kconfig: remove S...
28
  	.flags = SYMBOL_CONST|SYMBOL_VALID,
d41809ff7   Masahiro Yamada   kconfig: add 'sta...
29
30
31
  };
  
  static struct symbol symbol_empty = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
  	.name = "",
  	.curr = { "", no },
  	.flags = SYMBOL_VALID,
  };
face4374e   Roman Zippel   kconfig: add defc...
36
  struct symbol *sym_defconfig_list;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
  struct symbol *modules_sym;
d41809ff7   Masahiro Yamada   kconfig: add 'sta...
38
  static tristate modules_val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  enum symbol_type sym_get_type(struct symbol *sym)
  {
  	enum symbol_type type = sym->type;
  
  	if (type == S_TRISTATE) {
  		if (sym_is_choice_value(sym) && sym->visible == yes)
  			type = S_BOOLEAN;
  		else if (modules_val == no)
  			type = S_BOOLEAN;
  	}
  	return type;
  }
  
  const char *sym_type_name(enum symbol_type type)
  {
  	switch (type) {
  	case S_BOOLEAN:
b92d804a5   Masahiro Yamada   kconfig: drop 'bo...
57
  		return "bool";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
59
60
61
62
63
64
65
66
67
  	case S_TRISTATE:
  		return "tristate";
  	case S_INT:
  		return "integer";
  	case S_HEX:
  		return "hex";
  	case S_STRING:
  		return "string";
  	case S_UNKNOWN:
  		return "unknown";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68
69
70
71
72
73
74
75
76
77
78
  	}
  	return "???";
  }
  
  struct property *sym_get_choice_prop(struct symbol *sym)
  {
  	struct property *prop;
  
  	for_all_choices(sym, prop)
  		return prop;
  	return NULL;
93449082e   Roman Zippel   kconfig: environm...
79
  }
ad8d40cda   Michal Marek   kconfig: Remove u...
80
  static struct property *sym_get_default_prop(struct symbol *sym)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
84
85
86
87
88
89
90
  {
  	struct property *prop;
  
  	for_all_defaults(sym, prop) {
  		prop->visible.tri = expr_calc_value(prop->visible.expr);
  		if (prop->visible.tri != no)
  			return prop;
  	}
  	return NULL;
  }
558e78e3c   Masahiro Yamada   kconfig: split so...
91
  struct property *sym_get_range_prop(struct symbol *sym)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
93
94
95
96
97
98
99
100
101
  {
  	struct property *prop;
  
  	for_all_properties(sym, prop, P_RANGE) {
  		prop->visible.tri = expr_calc_value(prop->visible.expr);
  		if (prop->visible.tri != no)
  			return prop;
  	}
  	return NULL;
  }
129784abc   Kees Cook   kconfig: switch t...
102
  static long long sym_get_range_val(struct symbol *sym, int base)
4cf3cbe2a   Roman Zippel   [PATCH] kconfig: ...
103
104
105
106
107
108
109
110
111
112
113
114
  {
  	sym_calc_value(sym);
  	switch (sym->type) {
  	case S_INT:
  		base = 10;
  		break;
  	case S_HEX:
  		base = 16;
  		break;
  	default:
  		break;
  	}
129784abc   Kees Cook   kconfig: switch t...
115
  	return strtoll(sym->curr.val, NULL, base);
4cf3cbe2a   Roman Zippel   [PATCH] kconfig: ...
116
117
118
119
120
  }
  
  static void sym_validate_range(struct symbol *sym)
  {
  	struct property *prop;
129784abc   Kees Cook   kconfig: switch t...
121
122
  	int base;
  	long long val, val2;
4cf3cbe2a   Roman Zippel   [PATCH] kconfig: ...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  	char str[64];
  
  	switch (sym->type) {
  	case S_INT:
  		base = 10;
  		break;
  	case S_HEX:
  		base = 16;
  		break;
  	default:
  		return;
  	}
  	prop = sym_get_range_prop(sym);
  	if (!prop)
  		return;
129784abc   Kees Cook   kconfig: switch t...
138
  	val = strtoll(sym->curr.val, NULL, base);
4cf3cbe2a   Roman Zippel   [PATCH] kconfig: ...
139
140
141
142
143
144
145
  	val2 = sym_get_range_val(prop->expr->left.sym, base);
  	if (val >= val2) {
  		val2 = sym_get_range_val(prop->expr->right.sym, base);
  		if (val <= val2)
  			return;
  	}
  	if (sym->type == S_INT)
129784abc   Kees Cook   kconfig: switch t...
146
  		sprintf(str, "%lld", val2);
4cf3cbe2a   Roman Zippel   [PATCH] kconfig: ...
147
  	else
129784abc   Kees Cook   kconfig: switch t...
148
  		sprintf(str, "0x%llx", val2);
cd81fc82b   Masahiro Yamada   kconfig: add xstr...
149
  	sym->curr.val = xstrdup(str);
4cf3cbe2a   Roman Zippel   [PATCH] kconfig: ...
150
  }
ad8d40cda   Michal Marek   kconfig: Remove u...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
  static void sym_set_changed(struct symbol *sym)
  {
  	struct property *prop;
  
  	sym->flags |= SYMBOL_CHANGED;
  	for (prop = sym->prop; prop; prop = prop->next) {
  		if (prop->menu)
  			prop->menu->flags |= MENU_CHANGED;
  	}
  }
  
  static void sym_set_all_changed(void)
  {
  	struct symbol *sym;
  	int i;
  
  	for_all_symbols(i, sym)
  		sym_set_changed(sym);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
171
172
  static void sym_calc_visibility(struct symbol *sym)
  {
  	struct property *prop;
fa64e5f6a   Dirk Gouders   kconfig/symbol.c:...
173
  	struct symbol *choice_sym = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
176
177
  	tristate tri;
  
  	/* any prompt visible? */
  	tri = no;
fa64e5f6a   Dirk Gouders   kconfig/symbol.c:...
178
179
180
  
  	if (sym_is_choice_value(sym))
  		choice_sym = prop_get_symbol(sym_get_choice_prop(sym));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
  	for_all_prompts(sym, prop) {
  		prop->visible.tri = expr_calc_value(prop->visible.expr);
fa64e5f6a   Dirk Gouders   kconfig/symbol.c:...
183
184
185
186
187
188
189
190
  		/*
  		 * Tristate choice_values with visibility 'mod' are
  		 * not visible if the corresponding choice's value is
  		 * 'yes'.
  		 */
  		if (choice_sym && sym->type == S_TRISTATE &&
  		    prop->visible.tri == mod && choice_sym->curr.tri == yes)
  			prop->visible.tri = no;
d6ee35764   Sam Ravnborg   kconfig: rename E...
191
  		tri = EXPR_OR(tri, prop->visible.tri);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192
193
194
195
196
197
198
199
200
  	}
  	if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
  		tri = yes;
  	if (sym->visible != tri) {
  		sym->visible = tri;
  		sym_set_changed(sym);
  	}
  	if (sym_is_choice_value(sym))
  		return;
246cf9c26   Catalin Marinas   kbuild: Warn on s...
201
202
203
204
  	/* defaulting to "yes" if no explicit "depends on" are given */
  	tri = yes;
  	if (sym->dir_dep.expr)
  		tri = expr_calc_value(sym->dir_dep.expr);
f622f8279   Masahiro Yamada   kconfig: warn unm...
205
  	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
246cf9c26   Catalin Marinas   kbuild: Warn on s...
206
207
208
209
210
  		tri = yes;
  	if (sym->dir_dep.tri != tri) {
  		sym->dir_dep.tri = tri;
  		sym_set_changed(sym);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
212
213
214
215
216
217
218
219
  	tri = no;
  	if (sym->rev_dep.expr)
  		tri = expr_calc_value(sym->rev_dep.expr);
  	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
  		tri = yes;
  	if (sym->rev_dep.tri != tri) {
  		sym->rev_dep.tri = tri;
  		sym_set_changed(sym);
  	}
237e3ad0f   Nicolas Pitre   Kconfig: Introduc...
220
  	tri = no;
3a9dd3ecb   Masahiro Yamada   kconfig: make 'im...
221
  	if (sym->implied.expr)
237e3ad0f   Nicolas Pitre   Kconfig: Introduc...
222
223
224
225
226
227
228
  		tri = expr_calc_value(sym->implied.expr);
  	if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
  		tri = yes;
  	if (sym->implied.tri != tri) {
  		sym->implied.tri = tri;
  		sym_set_changed(sym);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
  }
c252147de   Sam Ravnborg   kconfig: refactor...
230
231
232
233
234
235
236
  /*
   * Find the default symbol for a choice.
   * First try the default values for the choice symbol
   * Next locate the first visible choice value
   * Return NULL if none was found
   */
  struct symbol *sym_choice_default(struct symbol *sym)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
239
240
  {
  	struct symbol *def_sym;
  	struct property *prop;
  	struct expr *e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
243
244
245
246
  	/* any of the defaults visible? */
  	for_all_defaults(sym, prop) {
  		prop->visible.tri = expr_calc_value(prop->visible.expr);
  		if (prop->visible.tri == no)
  			continue;
  		def_sym = prop_get_symbol(prop);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
248
249
250
251
252
  		if (def_sym->visible != no)
  			return def_sym;
  	}
  
  	/* just get the first visible value */
  	prop = sym_get_choice_prop(sym);
0a28c47b8   Jan Beulich   kconfig: Don't wr...
253
  	expr_list_for_each_sym(prop->expr, e, def_sym)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
  		if (def_sym->visible != no)
  			return def_sym;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
256

c252147de   Sam Ravnborg   kconfig: refactor...
257
  	/* failed to locate any defaults */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
259
  	return NULL;
  }
c252147de   Sam Ravnborg   kconfig: refactor...
260
261
262
263
264
  static struct symbol *sym_calc_choice(struct symbol *sym)
  {
  	struct symbol *def_sym;
  	struct property *prop;
  	struct expr *e;
5d09598d4   Arnaud Lacombe   kconfig: fix new ...
265
  	int flags;
c252147de   Sam Ravnborg   kconfig: refactor...
266
267
  
  	/* first calculate all choice values' visibilities */
5d09598d4   Arnaud Lacombe   kconfig: fix new ...
268
  	flags = sym->flags;
c252147de   Sam Ravnborg   kconfig: refactor...
269
  	prop = sym_get_choice_prop(sym);
5d09598d4   Arnaud Lacombe   kconfig: fix new ...
270
  	expr_list_for_each_sym(prop->expr, e, def_sym) {
c252147de   Sam Ravnborg   kconfig: refactor...
271
  		sym_calc_visibility(def_sym);
5d09598d4   Arnaud Lacombe   kconfig: fix new ...
272
273
274
275
276
  		if (def_sym->visible != no)
  			flags &= def_sym->flags;
  	}
  
  	sym->flags &= flags | ~SYMBOL_DEF_USER;
c252147de   Sam Ravnborg   kconfig: refactor...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  
  	/* is the user choice visible? */
  	def_sym = sym->def[S_DEF_USER].val;
  	if (def_sym && def_sym->visible != no)
  		return def_sym;
  
  	def_sym = sym_choice_default(sym);
  
  	if (def_sym == NULL)
  		/* no choice? reset tristate value */
  		sym->curr.tri = no;
  
  	return def_sym;
  }
f8f69dc0b   Masahiro Yamada   kconfig: make unm...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  static void sym_warn_unmet_dep(struct symbol *sym)
  {
  	struct gstr gs = str_new();
  
  	str_printf(&gs,
  		   "
  WARNING: unmet direct dependencies detected for %s
  ",
  		   sym->name);
  	str_printf(&gs,
  		   "  Depends on [%c]: ",
  		   sym->dir_dep.tri == mod ? 'm' : 'n');
  	expr_gstr_print(sym->dir_dep.expr, &gs);
  	str_printf(&gs, "
  ");
  
  	expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes,
  			       "  Selected by [y]:
  ");
  	expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod,
  			       "  Selected by [m]:
  ");
  
  	fputs(str_get(&gs), stderr);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
316
317
318
319
320
321
322
323
324
325
326
  void sym_calc_value(struct symbol *sym)
  {
  	struct symbol_value newval, oldval;
  	struct property *prop;
  	struct expr *e;
  
  	if (!sym)
  		return;
  
  	if (sym->flags & SYMBOL_VALID)
  		return;
fbe98bb9e   Arve HjønnevÃ¥g   kconfig: Fix defc...
327
328
329
330
331
332
333
  
  	if (sym_is_choice_value(sym) &&
  	    sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
  		sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
  		prop = sym_get_choice_prop(sym);
  		sym_calc_value(prop_get_symbol(prop));
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
  	sym->flags |= SYMBOL_VALID;
  
  	oldval = sym->curr;
  
  	switch (sym->type) {
  	case S_INT:
  	case S_HEX:
  	case S_STRING:
  		newval = symbol_empty.curr;
  		break;
  	case S_BOOLEAN:
  	case S_TRISTATE:
  		newval = symbol_no.curr;
  		break;
  	default:
  		sym->curr.val = sym->name;
  		sym->curr.tri = no;
  		return;
  	}
cb67ab2cd   Masahiro Yamada   kconfig: do not w...
353
  	sym->flags &= ~SYMBOL_WRITE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
354
355
  
  	sym_calc_visibility(sym);
cb67ab2cd   Masahiro Yamada   kconfig: do not w...
356
357
  	if (sym->visible != no)
  		sym->flags |= SYMBOL_WRITE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
359
360
361
362
363
364
365
366
  	/* set default if recursively called */
  	sym->curr = newval;
  
  	switch (sym_get_type(sym)) {
  	case S_BOOLEAN:
  	case S_TRISTATE:
  		if (sym_is_choice_value(sym) && sym->visible == yes) {
  			prop = sym_get_choice_prop(sym);
  			newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
587c90616   Roman Zippel   kconfig: fix sele...
367
368
369
370
371
  		} else {
  			if (sym->visible != no) {
  				/* if the symbol is visible use the user value
  				 * if available, otherwise try the default value
  				 */
587c90616   Roman Zippel   kconfig: fix sele...
372
373
374
375
376
  				if (sym_has_value(sym)) {
  					newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
  							      sym->visible);
  					goto calc_newval;
  				}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377
  			}
587c90616   Roman Zippel   kconfig: fix sele...
378
  			if (sym->rev_dep.tri != no)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
  				sym->flags |= SYMBOL_WRITE;
587c90616   Roman Zippel   kconfig: fix sele...
380
381
382
  			if (!sym_is_choice(sym)) {
  				prop = sym_get_default_prop(sym);
  				if (prop) {
587c90616   Roman Zippel   kconfig: fix sele...
383
384
  					newval.tri = EXPR_AND(expr_calc_value(prop->expr),
  							      prop->visible.tri);
f467c5640   Ulf Magnusson   kconfig: only wri...
385
386
  					if (newval.tri != no)
  						sym->flags |= SYMBOL_WRITE;
587c90616   Roman Zippel   kconfig: fix sele...
387
  				}
237e3ad0f   Nicolas Pitre   Kconfig: Introduc...
388
389
390
  				if (sym->implied.tri != no) {
  					sym->flags |= SYMBOL_WRITE;
  					newval.tri = EXPR_OR(newval.tri, sym->implied.tri);
3a9dd3ecb   Masahiro Yamada   kconfig: make 'im...
391
392
  					newval.tri = EXPR_AND(newval.tri,
  							      sym->dir_dep.tri);
237e3ad0f   Nicolas Pitre   Kconfig: Introduc...
393
  				}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
  			}
587c90616   Roman Zippel   kconfig: fix sele...
395
  		calc_newval:
f8f69dc0b   Masahiro Yamada   kconfig: make unm...
396
397
  			if (sym->dir_dep.tri < sym->rev_dep.tri)
  				sym_warn_unmet_dep(sym);
587c90616   Roman Zippel   kconfig: fix sele...
398
  			newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
399
  		}
def2fbffe   Masahiro Yamada   kconfig: allow sy...
400
  		if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
401
402
403
404
405
  			newval.tri = yes;
  		break;
  	case S_STRING:
  	case S_HEX:
  	case S_INT:
cb67ab2cd   Masahiro Yamada   kconfig: do not w...
406
407
408
  		if (sym->visible != no && sym_has_value(sym)) {
  			newval.val = sym->def[S_DEF_USER].val;
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
  		}
  		prop = sym_get_default_prop(sym);
  		if (prop) {
  			struct symbol *ds = prop_get_symbol(prop);
  			if (ds) {
  				sym->flags |= SYMBOL_WRITE;
  				sym_calc_value(ds);
  				newval.val = ds->curr.val;
  			}
  		}
  		break;
  	default:
  		;
  	}
  
  	sym->curr = newval;
  	if (sym_is_choice(sym) && newval.tri == yes)
  		sym->curr.val = sym_calc_choice(sym);
4cf3cbe2a   Roman Zippel   [PATCH] kconfig: ...
427
  	sym_validate_range(sym);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
428

face4374e   Roman Zippel   kconfig: add defc...
429
  	if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
430
  		sym_set_changed(sym);
face4374e   Roman Zippel   kconfig: add defc...
431
432
433
434
435
  		if (modules_sym == sym) {
  			sym_set_all_changed();
  			modules_val = modules_sym->curr.tri;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
436
437
  
  	if (sym_is_choice(sym)) {
7a9629233   Roman Zippel   kconfig: explicit...
438
  		struct symbol *choice_sym;
7a9629233   Roman Zippel   kconfig: explicit...
439

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
  		prop = sym_get_choice_prop(sym);
7a9629233   Roman Zippel   kconfig: explicit...
441
  		expr_list_for_each_sym(prop->expr, e, choice_sym) {
0a28c47b8   Jan Beulich   kconfig: Don't wr...
442
443
444
445
  			if ((sym->flags & SYMBOL_WRITE) &&
  			    choice_sym->visible != no)
  				choice_sym->flags |= SYMBOL_WRITE;
  			if (sym->flags & SYMBOL_CHANGED)
7a9629233   Roman Zippel   kconfig: explicit...
446
  				sym_set_changed(choice_sym);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447
448
  		}
  	}
5a1aa8a1a   Roman Zippel   kconfig: add name...
449

693359f7a   Dirk Gouders   kconfig: rename S...
450
  	if (sym->flags & SYMBOL_NO_WRITE)
5a1aa8a1a   Roman Zippel   kconfig: add name...
451
  		sym->flags &= ~SYMBOL_WRITE;
fbe98bb9e   Arve HjønnevÃ¥g   kconfig: Fix defc...
452
453
454
  
  	if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
  		set_all_choice_values(sym);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
456
457
458
459
460
461
462
463
  }
  
  void sym_clear_all_valid(void)
  {
  	struct symbol *sym;
  	int i;
  
  	for_all_symbols(i, sym)
  		sym->flags &= ~SYMBOL_VALID;
bfc10001b   Karsten Wiese   [PATCH] kconfig: ...
464
  	sym_add_change_count(1);
35ffd08d9   Markus Elfring   kconfig: Delete u...
465
  	sym_calc_value(modules_sym);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
  bool sym_tristate_within_range(struct symbol *sym, tristate val)
  {
  	int type = sym_get_type(sym);
  
  	if (sym->visible == no)
  		return false;
  
  	if (type != S_BOOLEAN && type != S_TRISTATE)
  		return false;
  
  	if (type == S_BOOLEAN && val == mod)
  		return false;
  	if (sym->visible <= sym->rev_dep.tri)
  		return false;
  	if (sym_is_choice_value(sym) && sym->visible == yes)
  		return val == yes;
  	return val >= sym->rev_dep.tri && val <= sym->visible;
  }
  
  bool sym_set_tristate_value(struct symbol *sym, tristate val)
  {
  	tristate oldval = sym_get_tristate_value(sym);
  
  	if (oldval != val && !sym_tristate_within_range(sym, val))
  		return false;
669bfad90   Roman Zippel   kconfig: allow lo...
492
493
  	if (!(sym->flags & SYMBOL_DEF_USER)) {
  		sym->flags |= SYMBOL_DEF_USER;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494
495
  		sym_set_changed(sym);
  	}
3f23ca2b3   Roman Zippel   [PATCH] kconfig: ...
496
497
498
499
  	/*
  	 * setting a choice value also resets the new flag of the choice
  	 * symbol and all other choice values.
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
501
  	if (sym_is_choice_value(sym) && val == yes) {
  		struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
3f23ca2b3   Roman Zippel   [PATCH] kconfig: ...
502
503
  		struct property *prop;
  		struct expr *e;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
504

0c1822e69   Roman Zippel   kconfig: allow mu...
505
  		cs->def[S_DEF_USER].val = sym;
669bfad90   Roman Zippel   kconfig: allow lo...
506
  		cs->flags |= SYMBOL_DEF_USER;
3f23ca2b3   Roman Zippel   [PATCH] kconfig: ...
507
508
509
  		prop = sym_get_choice_prop(cs);
  		for (e = prop->expr; e; e = e->left.expr) {
  			if (e->right.sym->visible != no)
669bfad90   Roman Zippel   kconfig: allow lo...
510
  				e->right.sym->flags |= SYMBOL_DEF_USER;
3f23ca2b3   Roman Zippel   [PATCH] kconfig: ...
511
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
512
  	}
0c1822e69   Roman Zippel   kconfig: allow mu...
513
  	sym->def[S_DEF_USER].tri = val;
face4374e   Roman Zippel   kconfig: add defc...
514
  	if (oldval != val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
  		sym_clear_all_valid();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
  
  	return true;
  }
  
  tristate sym_toggle_tristate_value(struct symbol *sym)
  {
  	tristate oldval, newval;
  
  	oldval = newval = sym_get_tristate_value(sym);
  	do {
  		switch (newval) {
  		case no:
  			newval = mod;
  			break;
  		case mod:
  			newval = yes;
  			break;
  		case yes:
  			newval = no;
  			break;
  		}
  		if (sym_set_tristate_value(sym, newval))
  			break;
  	} while (oldval != newval);
  	return newval;
  }
  
  bool sym_string_valid(struct symbol *sym, const char *str)
  {
  	signed char ch;
  
  	switch (sym->type) {
  	case S_STRING:
  		return true;
  	case S_INT:
  		ch = *str++;
  		if (ch == '-')
  			ch = *str++;
  		if (!isdigit(ch))
  			return false;
  		if (ch == '0' && *str != 0)
  			return false;
  		while ((ch = *str++)) {
  			if (!isdigit(ch))
  				return false;
  		}
  		return true;
  	case S_HEX:
  		if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
  			str += 2;
  		ch = *str++;
  		do {
  			if (!isxdigit(ch))
  				return false;
  		} while ((ch = *str++));
  		return true;
  	case S_BOOLEAN:
  	case S_TRISTATE:
  		switch (str[0]) {
  		case 'y': case 'Y':
  		case 'm': case 'M':
  		case 'n': case 'N':
  			return true;
  		}
  		return false;
  	default:
  		return false;
  	}
  }
  
  bool sym_string_within_range(struct symbol *sym, const char *str)
  {
  	struct property *prop;
129784abc   Kees Cook   kconfig: switch t...
589
  	long long val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
590
591
592
593
594
595
596
597
598
599
  
  	switch (sym->type) {
  	case S_STRING:
  		return sym_string_valid(sym, str);
  	case S_INT:
  		if (!sym_string_valid(sym, str))
  			return false;
  		prop = sym_get_range_prop(sym);
  		if (!prop)
  			return true;
129784abc   Kees Cook   kconfig: switch t...
600
  		val = strtoll(str, NULL, 10);
4cf3cbe2a   Roman Zippel   [PATCH] kconfig: ...
601
602
  		return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
  		       val <= sym_get_range_val(prop->expr->right.sym, 10);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
603
604
605
606
607
608
  	case S_HEX:
  		if (!sym_string_valid(sym, str))
  			return false;
  		prop = sym_get_range_prop(sym);
  		if (!prop)
  			return true;
129784abc   Kees Cook   kconfig: switch t...
609
  		val = strtoll(str, NULL, 16);
4cf3cbe2a   Roman Zippel   [PATCH] kconfig: ...
610
611
  		return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
  		       val <= sym_get_range_val(prop->expr->right.sym, 16);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
  	case S_BOOLEAN:
  	case S_TRISTATE:
  		switch (str[0]) {
  		case 'y': case 'Y':
  			return sym_tristate_within_range(sym, yes);
  		case 'm': case 'M':
  			return sym_tristate_within_range(sym, mod);
  		case 'n': case 'N':
  			return sym_tristate_within_range(sym, no);
  		}
  		return false;
  	default:
  		return false;
  	}
  }
  
  bool sym_set_string_value(struct symbol *sym, const char *newval)
  {
  	const char *oldval;
  	char *val;
  	int size;
  
  	switch (sym->type) {
  	case S_BOOLEAN:
  	case S_TRISTATE:
  		switch (newval[0]) {
  		case 'y': case 'Y':
  			return sym_set_tristate_value(sym, yes);
  		case 'm': case 'M':
  			return sym_set_tristate_value(sym, mod);
  		case 'n': case 'N':
  			return sym_set_tristate_value(sym, no);
  		}
  		return false;
  	default:
  		;
  	}
  
  	if (!sym_string_within_range(sym, newval))
  		return false;
669bfad90   Roman Zippel   kconfig: allow lo...
652
653
  	if (!(sym->flags & SYMBOL_DEF_USER)) {
  		sym->flags |= SYMBOL_DEF_USER;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
654
655
  		sym_set_changed(sym);
  	}
0c1822e69   Roman Zippel   kconfig: allow mu...
656
  	oldval = sym->def[S_DEF_USER].val;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657
658
659
  	size = strlen(newval) + 1;
  	if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
  		size += 2;
177acf784   Alan Cox   kconfig: Fix mall...
660
  		sym->def[S_DEF_USER].val = val = xmalloc(size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
661
662
663
  		*val++ = '0';
  		*val++ = 'x';
  	} else if (!oldval || strcmp(oldval, newval))
177acf784   Alan Cox   kconfig: Fix mall...
664
  		sym->def[S_DEF_USER].val = val = xmalloc(size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
665
666
667
668
669
670
671
672
673
  	else
  		return true;
  
  	strcpy(val, newval);
  	free((void *)oldval);
  	sym_clear_all_valid();
  
  	return true;
  }
7cf3d73b4   Sam Ravnborg   kconfig: add save...
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
  /*
   * Find the default value associated to a symbol.
   * For tristate symbol handle the modules=n case
   * in which case "m" becomes "y".
   * If the symbol does not have any default then fallback
   * to the fixed default values.
   */
  const char *sym_get_string_default(struct symbol *sym)
  {
  	struct property *prop;
  	struct symbol *ds;
  	const char *str;
  	tristate val;
  
  	sym_calc_visibility(sym);
  	sym_calc_value(modules_sym);
  	val = symbol_no.curr.tri;
  	str = symbol_empty.curr.val;
  
  	/* If symbol has a default value look it up */
  	prop = sym_get_default_prop(sym);
  	if (prop != NULL) {
  		switch (sym->type) {
  		case S_BOOLEAN:
  		case S_TRISTATE:
579fb8e74   Arnaud Lacombe   kconfig: fix typos
699
  			/* The visibility may limit the value from yes => mod */
7cf3d73b4   Sam Ravnborg   kconfig: add save...
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
  			val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
  			break;
  		default:
  			/*
  			 * The following fails to handle the situation
  			 * where a default value is further limited by
  			 * the valid range.
  			 */
  			ds = prop_get_symbol(prop);
  			if (ds != NULL) {
  				sym_calc_value(ds);
  				str = (const char *)ds->curr.val;
  			}
  		}
  	}
  
  	/* Handle select statements */
  	val = EXPR_OR(val, sym->rev_dep.tri);
  
  	/* transpose mod to yes if modules are not enabled */
  	if (val == mod)
  		if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
  			val = yes;
  
  	/* transpose mod to yes if type is bool */
  	if (sym->type == S_BOOLEAN && val == mod)
  		val = yes;
237e3ad0f   Nicolas Pitre   Kconfig: Introduc...
727
728
729
  	/* adjust the default value if this symbol is implied by another */
  	if (val < sym->implied.tri)
  		val = sym->implied.tri;
7cf3d73b4   Sam Ravnborg   kconfig: add save...
730
731
732
733
734
735
736
737
738
739
740
741
742
  	switch (sym->type) {
  	case S_BOOLEAN:
  	case S_TRISTATE:
  		switch (val) {
  		case no: return "n";
  		case mod: return "m";
  		case yes: return "y";
  		}
  	case S_INT:
  	case S_HEX:
  		return str;
  	case S_STRING:
  		return str;
7cf3d73b4   Sam Ravnborg   kconfig: add save...
743
744
745
746
747
  	case S_UNKNOWN:
  		break;
  	}
  	return "";
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
748
749
750
751
752
753
754
755
756
757
758
759
  const char *sym_get_string_value(struct symbol *sym)
  {
  	tristate val;
  
  	switch (sym->type) {
  	case S_BOOLEAN:
  	case S_TRISTATE:
  		val = sym_get_tristate_value(sym);
  		switch (val) {
  		case no:
  			return "n";
  		case mod:
e54e692ba   Arnaud Lacombe   kconfig: introduc...
760
761
  			sym_calc_value(modules_sym);
  			return (modules_sym->curr.tri == no) ? "n" : "m";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
763
764
765
766
767
768
769
770
  		case yes:
  			return "y";
  		}
  		break;
  	default:
  		;
  	}
  	return (const char *)sym->curr.val;
  }
baa23ec86   Marco Ammon   kconfig: Fix spel...
771
  bool sym_is_changeable(struct symbol *sym)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
772
773
774
  {
  	return sym->visible > sym->rev_dep.tri;
  }
e66f25d7d   Andi Kleen   Improve kconfig s...
775
776
777
778
779
780
781
782
  static unsigned strhash(const char *s)
  {
  	/* fnv32 hash */
  	unsigned hash = 2166136261U;
  	for (; *s; s++)
  		hash = (hash ^ *s) * 0x01000193;
  	return hash;
  }
5a1aa8a1a   Roman Zippel   kconfig: add name...
783
  struct symbol *sym_lookup(const char *name, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784
785
  {
  	struct symbol *symbol;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
786
  	char *new_name;
e66f25d7d   Andi Kleen   Improve kconfig s...
787
  	int hash;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
789
790
791
792
793
794
795
796
  
  	if (name) {
  		if (name[0] && !name[1]) {
  			switch (name[0]) {
  			case 'y': return &symbol_yes;
  			case 'm': return &symbol_mod;
  			case 'n': return &symbol_no;
  			}
  		}
e66f25d7d   Andi Kleen   Improve kconfig s...
797
  		hash = strhash(name) % SYMBOL_HASHSIZE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
798
799
  
  		for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
e66f25d7d   Andi Kleen   Improve kconfig s...
800
801
  			if (symbol->name &&
  			    !strcmp(symbol->name, name) &&
5a1aa8a1a   Roman Zippel   kconfig: add name...
802
803
804
  			    (flags ? symbol->flags & flags
  				   : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
  				return symbol;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
805
  		}
cd81fc82b   Masahiro Yamada   kconfig: add xstr...
806
  		new_name = xstrdup(name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
807
808
  	} else {
  		new_name = NULL;
e66f25d7d   Andi Kleen   Improve kconfig s...
809
  		hash = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
810
  	}
177acf784   Alan Cox   kconfig: Fix mall...
811
  	symbol = xmalloc(sizeof(*symbol));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812
813
814
  	memset(symbol, 0, sizeof(*symbol));
  	symbol->name = new_name;
  	symbol->type = S_UNKNOWN;
cfc6eea9f   Masahiro Yamada   kconfig: do not u...
815
  	symbol->flags = flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
816
817
818
819
820
821
822
823
824
825
  
  	symbol->next = symbol_hash[hash];
  	symbol_hash[hash] = symbol;
  
  	return symbol;
  }
  
  struct symbol *sym_find(const char *name)
  {
  	struct symbol *symbol = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
826
827
828
829
830
831
832
833
834
835
836
837
  	int hash = 0;
  
  	if (!name)
  		return NULL;
  
  	if (name[0] && !name[1]) {
  		switch (name[0]) {
  		case 'y': return &symbol_yes;
  		case 'm': return &symbol_mod;
  		case 'n': return &symbol_no;
  		}
  	}
e66f25d7d   Andi Kleen   Improve kconfig s...
838
  	hash = strhash(name) % SYMBOL_HASHSIZE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
839
840
  
  	for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
e66f25d7d   Andi Kleen   Improve kconfig s...
841
842
  		if (symbol->name &&
  		    !strcmp(symbol->name, name) &&
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
843
844
845
846
847
848
  		    !(symbol->flags & SYMBOL_CONST))
  				break;
  	}
  
  	return symbol;
  }
e54e692ba   Arnaud Lacombe   kconfig: introduc...
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
  const char *sym_escape_string_value(const char *in)
  {
  	const char *p;
  	size_t reslen;
  	char *res;
  	size_t l;
  
  	reslen = strlen(in) + strlen("\"\"") + 1;
  
  	p = in;
  	for (;;) {
  		l = strcspn(p, "\"\\");
  		p += l;
  
  		if (p[0] == '\0')
  			break;
  
  		reslen++;
  		p++;
  	}
177acf784   Alan Cox   kconfig: Fix mall...
869
  	res = xmalloc(reslen);
e54e692ba   Arnaud Lacombe   kconfig: introduc...
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
  	res[0] = '\0';
  
  	strcat(res, "\"");
  
  	p = in;
  	for (;;) {
  		l = strcspn(p, "\"\\");
  		strncat(res, p, l);
  		p += l;
  
  		if (p[0] == '\0')
  			break;
  
  		strcat(res, "\\");
  		strncat(res, p++, 1);
  	}
  
  	strcat(res, "\"");
  	return res;
  }
193b40aeb   Yann E. MORIN   kconfig: sort fou...
890
891
892
893
894
895
896
897
898
  struct sym_match {
  	struct symbol	*sym;
  	off_t		so, eo;
  };
  
  /* Compare matched symbols as thus:
   * - first, symbols that match exactly
   * - then, alphabetical sort
   */
803b35198   Yann E. MORIN   kconfig: minor st...
899
  static int sym_rel_comp(const void *sym1, const void *sym2)
193b40aeb   Yann E. MORIN   kconfig: sort fou...
900
  {
508382a04   Yann E. MORIN   kconfig: simplify...
901
902
  	const struct sym_match *s1 = sym1;
  	const struct sym_match *s2 = sym2;
26e933e3c   Yann E. MORIN   kconfig: avoid mu...
903
  	int exact1, exact2;
193b40aeb   Yann E. MORIN   kconfig: sort fou...
904
905
906
907
908
909
910
911
912
913
  
  	/* Exact match:
  	 * - if matched length on symbol s1 is the length of that symbol,
  	 *   then this symbol should come first;
  	 * - if matched length on symbol s2 is the length of that symbol,
  	 *   then this symbol should come first.
  	 * Note: since the search can be a regexp, both symbols may match
  	 * exactly; if this is the case, we can't decide which comes first,
  	 * and we fallback to sorting alphabetically.
  	 */
26e933e3c   Yann E. MORIN   kconfig: avoid mu...
914
915
916
  	exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
  	exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
  	if (exact1 && !exact2)
193b40aeb   Yann E. MORIN   kconfig: sort fou...
917
  		return -1;
26e933e3c   Yann E. MORIN   kconfig: avoid mu...
918
  	if (!exact1 && exact2)
193b40aeb   Yann E. MORIN   kconfig: sort fou...
919
920
921
922
923
  		return 1;
  
  	/* As a fallback, sort symbols alphabetically */
  	return strcmp(s1->sym->name, s2->sym->name);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
926
  struct symbol **sym_re_search(const char *pattern)
  {
  	struct symbol *sym, **sym_arr = NULL;
508382a04   Yann E. MORIN   kconfig: simplify...
927
  	struct sym_match *sym_match_arr = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
928
929
  	int i, cnt, size;
  	regex_t re;
193b40aeb   Yann E. MORIN   kconfig: sort fou...
930
  	regmatch_t match[1];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
931
932
933
934
935
  
  	cnt = size = 0;
  	/* Skip if empty */
  	if (strlen(pattern) == 0)
  		return NULL;
193b40aeb   Yann E. MORIN   kconfig: sort fou...
936
  	if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
937
938
939
940
941
  		return NULL;
  
  	for_all_symbols(i, sym) {
  		if (sym->flags & SYMBOL_CONST || !sym->name)
  			continue;
193b40aeb   Yann E. MORIN   kconfig: sort fou...
942
  		if (regexec(&re, sym->name, 1, match, 0))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
943
  			continue;
1407f97ae   Yann E. MORIN   kconfig: don't al...
944
  		if (cnt >= size) {
193b40aeb   Yann E. MORIN   kconfig: sort fou...
945
  			void *tmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
946
  			size += 16;
508382a04   Yann E. MORIN   kconfig: simplify...
947
  			tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
803b35198   Yann E. MORIN   kconfig: minor st...
948
  			if (!tmp)
193b40aeb   Yann E. MORIN   kconfig: sort fou...
949
  				goto sym_re_search_free;
193b40aeb   Yann E. MORIN   kconfig: sort fou...
950
  			sym_match_arr = tmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
951
  		}
da6df879b   Li Zefan   kconfig: recalc s...
952
  		sym_calc_value(sym);
803b35198   Yann E. MORIN   kconfig: minor st...
953
  		/* As regexec returned 0, we know we have a match, so
193b40aeb   Yann E. MORIN   kconfig: sort fou...
954
955
  		 * we can use match[0].rm_[se]o without further checks
  		 */
508382a04   Yann E. MORIN   kconfig: simplify...
956
957
958
  		sym_match_arr[cnt].so = match[0].rm_so;
  		sym_match_arr[cnt].eo = match[0].rm_eo;
  		sym_match_arr[cnt++].sym = sym;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
959
  	}
193b40aeb   Yann E. MORIN   kconfig: sort fou...
960
  	if (sym_match_arr) {
508382a04   Yann E. MORIN   kconfig: simplify...
961
  		qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
88127dae6   Heinrich Schuchardt   kconfig/symbol.c:...
962
  		sym_arr = malloc((cnt+1) * sizeof(struct symbol *));
193b40aeb   Yann E. MORIN   kconfig: sort fou...
963
964
965
  		if (!sym_arr)
  			goto sym_re_search_free;
  		for (i = 0; i < cnt; i++)
508382a04   Yann E. MORIN   kconfig: simplify...
966
  			sym_arr[i] = sym_match_arr[i].sym;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
967
  		sym_arr[cnt] = NULL;
193b40aeb   Yann E. MORIN   kconfig: sort fou...
968
969
  	}
  sym_re_search_free:
508382a04   Yann E. MORIN   kconfig: simplify...
970
971
  	/* sym_match_arr can be NULL if no match, but free(NULL) is OK */
  	free(sym_match_arr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
972
973
974
975
  	regfree(&re);
  
  	return sym_arr;
  }
d595cea62   Roman Zippel   kconfig: print mo...
976
977
978
979
  /*
   * When we check for recursive dependencies we use a stack to save
   * current state so we can print out relevant info to user.
   * The entries are located on the call stack so no need to free memory.
8d9dfe827   Martin Walch   kconfig: fix triv...
980
   * Note insert() remove() must always match to properly clear the stack.
d595cea62   Roman Zippel   kconfig: print mo...
981
982
983
984
985
   */
  static struct dep_stack {
  	struct dep_stack *prev, *next;
  	struct symbol *sym;
  	struct property *prop;
f498926c4   Masahiro Yamada   kconfig: improve ...
986
  	struct expr **expr;
d595cea62   Roman Zippel   kconfig: print mo...
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
  } *check_top;
  
  static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
  {
  	memset(stack, 0, sizeof(*stack));
  	if (check_top)
  		check_top->next = stack;
  	stack->prev = check_top;
  	stack->sym = sym;
  	check_top = stack;
  }
  
  static void dep_stack_remove(void)
  {
  	check_top = check_top->prev;
  	if (check_top)
  		check_top->next = NULL;
  }
  
  /*
   * Called when we have detected a recursive dependency.
   * check_top point to the top of the stact so we use
   * the ->prev pointer to locate the bottom of the stack.
   */
  static void sym_check_print_recursive(struct symbol *last_sym)
  {
  	struct dep_stack *stack;
  	struct symbol *sym, *next_sym;
  	struct menu *menu = NULL;
  	struct property *prop;
  	struct dep_stack cv_stack;
  
  	if (sym_is_choice_value(last_sym)) {
  		dep_stack_insert(&cv_stack, last_sym);
  		last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
  	}
  
  	for (stack = check_top; stack != NULL; stack = stack->prev)
  		if (stack->sym == last_sym)
  			break;
  	if (!stack) {
  		fprintf(stderr, "unexpected recursive dependency error
  ");
  		return;
  	}
  
  	for (; stack; stack = stack->next) {
  		sym = stack->sym;
  		next_sym = stack->next ? stack->next->sym : last_sym;
  		prop = stack->prop;
3643f849d   Sam Ravnborg   kconfig: fix segf...
1037
1038
  		if (prop == NULL)
  			prop = stack->sym->prop;
d595cea62   Roman Zippel   kconfig: print mo...
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
  
  		/* for choice values find the menu entry (used below) */
  		if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
  			for (prop = sym->prop; prop; prop = prop->next) {
  				menu = prop->menu;
  				if (prop->menu)
  					break;
  			}
  		}
  		if (stack->sym == last_sym)
  			fprintf(stderr, "%s:%d:error: recursive dependency detected!
  ",
  				prop->file->name, prop->lineno);
e3b03bf29   Masahiro Yamada   kconfig: display ...
1052

f498926c4   Masahiro Yamada   kconfig: improve ...
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
  		if (sym_is_choice(sym)) {
  			fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s
  ",
  				menu->file->name, menu->lineno,
  				sym->name ? sym->name : "<choice>",
  				next_sym->name ? next_sym->name : "<choice>");
  		} else if (sym_is_choice_value(sym)) {
  			fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s
  ",
  				menu->file->name, menu->lineno,
d595cea62   Roman Zippel   kconfig: print mo...
1063
  				sym->name ? sym->name : "<choice>",
d595cea62   Roman Zippel   kconfig: print mo...
1064
  				next_sym->name ? next_sym->name : "<choice>");
f498926c4   Masahiro Yamada   kconfig: improve ...
1065
  		} else if (stack->expr == &sym->dir_dep.expr) {
d595cea62   Roman Zippel   kconfig: print mo...
1066
1067
1068
1069
1070
  			fprintf(stderr, "%s:%d:\tsymbol %s depends on %s
  ",
  				prop->file->name, prop->lineno,
  				sym->name ? sym->name : "<choice>",
  				next_sym->name ? next_sym->name : "<choice>");
f498926c4   Masahiro Yamada   kconfig: improve ...
1071
1072
1073
1074
  		} else if (stack->expr == &sym->rev_dep.expr) {
  			fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s
  ",
  				prop->file->name, prop->lineno,
d595cea62   Roman Zippel   kconfig: print mo...
1075
1076
  				sym->name ? sym->name : "<choice>",
  				next_sym->name ? next_sym->name : "<choice>");
f498926c4   Masahiro Yamada   kconfig: improve ...
1077
1078
1079
1080
  		} else if (stack->expr == &sym->implied.expr) {
  			fprintf(stderr, "%s:%d:\tsymbol %s is implied by %s
  ",
  				prop->file->name, prop->lineno,
d595cea62   Roman Zippel   kconfig: print mo...
1081
1082
  				sym->name ? sym->name : "<choice>",
  				next_sym->name ? next_sym->name : "<choice>");
f498926c4   Masahiro Yamada   kconfig: improve ...
1083
1084
1085
1086
1087
1088
1089
  		} else if (stack->expr) {
  			fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s
  ",
  				prop->file->name, prop->lineno,
  				sym->name ? sym->name : "<choice>",
  				prop_get_type_name(prop->type),
  				next_sym->name ? next_sym->name : "<choice>");
d595cea62   Roman Zippel   kconfig: print mo...
1090
  		} else {
f498926c4   Masahiro Yamada   kconfig: improve ...
1091
1092
  			fprintf(stderr, "%s:%d:\tsymbol %s %s is visible depending on %s
  ",
d595cea62   Roman Zippel   kconfig: print mo...
1093
1094
  				prop->file->name, prop->lineno,
  				sym->name ? sym->name : "<choice>",
f498926c4   Masahiro Yamada   kconfig: improve ...
1095
  				prop_get_type_name(prop->type),
d595cea62   Roman Zippel   kconfig: print mo...
1096
1097
1098
  				next_sym->name ? next_sym->name : "<choice>");
  		}
  	}
e3b03bf29   Masahiro Yamada   kconfig: display ...
1099
  	fprintf(stderr,
cd238effe   Mauro Carvalho Chehab   docs: kbuild: con...
1100
1101
  		"For a resolution refer to Documentation/kbuild/kconfig-language.rst
  "
e3b03bf29   Masahiro Yamada   kconfig: display ...
1102
1103
1104
1105
  		"subsection \"Kconfig recursive dependency limitations\"
  "
  		"
  ");
d595cea62   Roman Zippel   kconfig: print mo...
1106
1107
1108
  	if (check_top == &cv_stack)
  		dep_stack_remove();
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1109

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
  static struct symbol *sym_check_expr_deps(struct expr *e)
  {
  	struct symbol *sym;
  
  	if (!e)
  		return NULL;
  	switch (e->type) {
  	case E_OR:
  	case E_AND:
  		sym = sym_check_expr_deps(e->left.expr);
  		if (sym)
  			return sym;
  		return sym_check_expr_deps(e->right.expr);
  	case E_NOT:
  		return sym_check_expr_deps(e->left.expr);
  	case E_EQUAL:
31847b67b   Jan Beulich   kconfig: allow us...
1126
1127
1128
1129
  	case E_GEQ:
  	case E_GTH:
  	case E_LEQ:
  	case E_LTH:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
  	case E_UNEQUAL:
  		sym = sym_check_deps(e->left.sym);
  		if (sym)
  			return sym;
  		return sym_check_deps(e->right.sym);
  	case E_SYMBOL:
  		return sym_check_deps(e->left.sym);
  	default:
  		break;
  	}
9e3e10c72   Masahiro Yamada   kconfig: send err...
1140
1141
  	fprintf(stderr, "Oops! How to check %d?
  ", e->type);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1142
1143
  	return NULL;
  }
5447d34b0   Sam Ravnborg   kconfig: error ou...
1144
  /* return NULL when dependencies are OK */
489811788   Roman Zippel   kconfig: fix choi...
1145
  static struct symbol *sym_check_sym_deps(struct symbol *sym)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1146
1147
1148
  {
  	struct symbol *sym2;
  	struct property *prop;
d595cea62   Roman Zippel   kconfig: print mo...
1149
1150
1151
  	struct dep_stack stack;
  
  	dep_stack_insert(&stack, sym);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1152

f498926c4   Masahiro Yamada   kconfig: improve ...
1153
1154
1155
1156
1157
1158
  	stack.expr = &sym->dir_dep.expr;
  	sym2 = sym_check_expr_deps(sym->dir_dep.expr);
  	if (sym2)
  		goto out;
  
  	stack.expr = &sym->rev_dep.expr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1159
1160
  	sym2 = sym_check_expr_deps(sym->rev_dep.expr);
  	if (sym2)
d595cea62   Roman Zippel   kconfig: print mo...
1161
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1162

f498926c4   Masahiro Yamada   kconfig: improve ...
1163
  	stack.expr = &sym->implied.expr;
5e8c5299d   Masahiro Yamada   kconfig: report r...
1164
1165
1166
  	sym2 = sym_check_expr_deps(sym->implied.expr);
  	if (sym2)
  		goto out;
f498926c4   Masahiro Yamada   kconfig: improve ...
1167
  	stack.expr = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1168
  	for (prop = sym->prop; prop; prop = prop->next) {
5e8c5299d   Masahiro Yamada   kconfig: report r...
1169
1170
  		if (prop->type == P_CHOICE || prop->type == P_SELECT ||
  		    prop->type == P_IMPLY)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1171
  			continue;
d595cea62   Roman Zippel   kconfig: print mo...
1172
  		stack.prop = prop;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1173
1174
  		sym2 = sym_check_expr_deps(prop->visible.expr);
  		if (sym2)
489811788   Roman Zippel   kconfig: fix choi...
1175
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1176
1177
  		if (prop->type != P_DEFAULT || sym_is_choice(sym))
  			continue;
f498926c4   Masahiro Yamada   kconfig: improve ...
1178
  		stack.expr = &prop->expr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1179
1180
  		sym2 = sym_check_expr_deps(prop->expr);
  		if (sym2)
489811788   Roman Zippel   kconfig: fix choi...
1181
  			break;
d595cea62   Roman Zippel   kconfig: print mo...
1182
  		stack.expr = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1183
  	}
489811788   Roman Zippel   kconfig: fix choi...
1184

d595cea62   Roman Zippel   kconfig: print mo...
1185
1186
  out:
  	dep_stack_remove();
489811788   Roman Zippel   kconfig: fix choi...
1187
1188
1189
1190
1191
1192
1193
1194
  	return sym2;
  }
  
  static struct symbol *sym_check_choice_deps(struct symbol *choice)
  {
  	struct symbol *sym, *sym2;
  	struct property *prop;
  	struct expr *e;
d595cea62   Roman Zippel   kconfig: print mo...
1195
1196
1197
  	struct dep_stack stack;
  
  	dep_stack_insert(&stack, choice);
489811788   Roman Zippel   kconfig: fix choi...
1198
1199
1200
1201
1202
1203
1204
1205
  
  	prop = sym_get_choice_prop(choice);
  	expr_list_for_each_sym(prop->expr, e, sym)
  		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  
  	choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  	sym2 = sym_check_sym_deps(choice);
  	choice->flags &= ~SYMBOL_CHECK;
5447d34b0   Sam Ravnborg   kconfig: error ou...
1206
  	if (sym2)
489811788   Roman Zippel   kconfig: fix choi...
1207
1208
1209
1210
  		goto out;
  
  	expr_list_for_each_sym(prop->expr, e, sym) {
  		sym2 = sym_check_sym_deps(sym);
d595cea62   Roman Zippel   kconfig: print mo...
1211
  		if (sym2)
489811788   Roman Zippel   kconfig: fix choi...
1212
  			break;
489811788   Roman Zippel   kconfig: fix choi...
1213
1214
1215
1216
1217
1218
1219
1220
  	}
  out:
  	expr_list_for_each_sym(prop->expr, e, sym)
  		sym->flags &= ~SYMBOL_CHECK;
  
  	if (sym2 && sym_is_choice_value(sym2) &&
  	    prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
  		sym2 = choice;
d595cea62   Roman Zippel   kconfig: print mo...
1221
  	dep_stack_remove();
489811788   Roman Zippel   kconfig: fix choi...
1222
1223
1224
1225
1226
1227
1228
1229
1230
  	return sym2;
  }
  
  struct symbol *sym_check_deps(struct symbol *sym)
  {
  	struct symbol *sym2;
  	struct property *prop;
  
  	if (sym->flags & SYMBOL_CHECK) {
d595cea62   Roman Zippel   kconfig: print mo...
1231
  		sym_check_print_recursive(sym);
489811788   Roman Zippel   kconfig: fix choi...
1232
1233
1234
1235
1236
1237
  		return sym;
  	}
  	if (sym->flags & SYMBOL_CHECKED)
  		return NULL;
  
  	if (sym_is_choice_value(sym)) {
d595cea62   Roman Zippel   kconfig: print mo...
1238
  		struct dep_stack stack;
489811788   Roman Zippel   kconfig: fix choi...
1239
  		/* for choice groups start the check with main choice symbol */
d595cea62   Roman Zippel   kconfig: print mo...
1240
  		dep_stack_insert(&stack, sym);
489811788   Roman Zippel   kconfig: fix choi...
1241
1242
  		prop = sym_get_choice_prop(sym);
  		sym2 = sym_check_deps(prop_get_symbol(prop));
d595cea62   Roman Zippel   kconfig: print mo...
1243
  		dep_stack_remove();
489811788   Roman Zippel   kconfig: fix choi...
1244
1245
1246
1247
1248
1249
1250
  	} else if (sym_is_choice(sym)) {
  		sym2 = sym_check_choice_deps(sym);
  	} else {
  		sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
  		sym2 = sym_check_sym_deps(sym);
  		sym->flags &= ~SYMBOL_CHECK;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1251
1252
  	return sym2;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1253
1254
1255
  struct symbol *prop_get_symbol(struct property *prop)
  {
  	if (prop->expr && (prop->expr->type == E_SYMBOL ||
7a9629233   Roman Zippel   kconfig: explicit...
1256
  			   prop->expr->type == E_LIST))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
  		return prop->expr->left.sym;
  	return NULL;
  }
  
  const char *prop_get_type_name(enum prop_type type)
  {
  	switch (type) {
  	case P_PROMPT:
  		return "prompt";
  	case P_COMMENT:
  		return "comment";
  	case P_MENU:
  		return "menu";
  	case P_DEFAULT:
  		return "default";
  	case P_CHOICE:
  		return "choice";
  	case P_SELECT:
  		return "select";
237e3ad0f   Nicolas Pitre   Kconfig: Introduc...
1276
1277
  	case P_IMPLY:
  		return "imply";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1278
1279
  	case P_RANGE:
  		return "range";
59e89e3dd   Sam Ravnborg   kconfig: save loc...
1280
1281
  	case P_SYMBOL:
  		return "symbol";
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1282
1283
1284
1285
1286
  	case P_UNKNOWN:
  		break;
  	}
  	return "unknown";
  }