blob: 0a013ab3ae27c99e1852b4d7b62706f3327e1c1b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
5
6#include <ctype.h>
7#include <stdlib.h>
8#include <string.h>
9#include <regex.h>
10#include <sys/utsname.h>
11
12#define LKC_DIRECT_LINK
13#include "lkc.h"
14
15struct symbol symbol_yes = {
16 .name = "y",
17 .curr = { "y", yes },
Roman Zippelc0e150ac2006-06-08 22:12:40 -070018 .flags = SYMBOL_CONST|SYMBOL_VALID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070019}, symbol_mod = {
20 .name = "m",
21 .curr = { "m", mod },
Roman Zippelc0e150ac2006-06-08 22:12:40 -070022 .flags = SYMBOL_CONST|SYMBOL_VALID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070023}, symbol_no = {
24 .name = "n",
25 .curr = { "n", no },
Roman Zippelc0e150ac2006-06-08 22:12:40 -070026 .flags = SYMBOL_CONST|SYMBOL_VALID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}, symbol_empty = {
28 .name = "",
29 .curr = { "", no },
30 .flags = SYMBOL_VALID,
31};
32
Roman Zippelface4372006-06-08 22:12:45 -070033struct symbol *sym_defconfig_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034struct symbol *modules_sym;
35tristate modules_val;
36
Roman Zippel93449082008-01-14 04:50:54 +010037struct expr *sym_env_list;
38
Trevor Keith4356f482009-09-18 12:49:23 -070039static void sym_add_default(struct symbol *sym, const char *def)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 struct property *prop = prop_alloc(P_DEFAULT, sym);
42
Roman Zippel5a1aa8a2008-02-29 05:11:50 +010043 prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46void sym_init(void)
47{
48 struct symbol *sym;
49 struct utsname uts;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 static bool inited = false;
51
52 if (inited)
53 return;
54 inited = true;
55
56 uname(&uts);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 sym = sym_lookup("UNAME_RELEASE", 0);
59 sym->type = S_STRING;
60 sym->flags |= SYMBOL_AUTO;
61 sym_add_default(sym, uts.release);
62}
63
64enum symbol_type sym_get_type(struct symbol *sym)
65{
66 enum symbol_type type = sym->type;
67
68 if (type == S_TRISTATE) {
69 if (sym_is_choice_value(sym) && sym->visible == yes)
70 type = S_BOOLEAN;
71 else if (modules_val == no)
72 type = S_BOOLEAN;
73 }
74 return type;
75}
76
77const char *sym_type_name(enum symbol_type type)
78{
79 switch (type) {
80 case S_BOOLEAN:
81 return "boolean";
82 case S_TRISTATE:
83 return "tristate";
84 case S_INT:
85 return "integer";
86 case S_HEX:
87 return "hex";
88 case S_STRING:
89 return "string";
90 case S_UNKNOWN:
91 return "unknown";
92 case S_OTHER:
93 break;
94 }
95 return "???";
96}
97
98struct property *sym_get_choice_prop(struct symbol *sym)
99{
100 struct property *prop;
101
102 for_all_choices(sym, prop)
103 return prop;
104 return NULL;
105}
106
Roman Zippel93449082008-01-14 04:50:54 +0100107struct property *sym_get_env_prop(struct symbol *sym)
108{
109 struct property *prop;
110
111 for_all_properties(sym, prop, P_ENV)
112 return prop;
113 return NULL;
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116struct property *sym_get_default_prop(struct symbol *sym)
117{
118 struct property *prop;
119
120 for_all_defaults(sym, prop) {
121 prop->visible.tri = expr_calc_value(prop->visible.expr);
122 if (prop->visible.tri != no)
123 return prop;
124 }
125 return NULL;
126}
127
Trevor Keith4356f482009-09-18 12:49:23 -0700128static struct property *sym_get_range_prop(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct property *prop;
131
132 for_all_properties(sym, prop, P_RANGE) {
133 prop->visible.tri = expr_calc_value(prop->visible.expr);
134 if (prop->visible.tri != no)
135 return prop;
136 }
137 return NULL;
138}
139
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800140static int sym_get_range_val(struct symbol *sym, int base)
141{
142 sym_calc_value(sym);
143 switch (sym->type) {
144 case S_INT:
145 base = 10;
146 break;
147 case S_HEX:
148 base = 16;
149 break;
150 default:
151 break;
152 }
153 return strtol(sym->curr.val, NULL, base);
154}
155
156static void sym_validate_range(struct symbol *sym)
157{
158 struct property *prop;
159 int base, val, val2;
160 char str[64];
161
162 switch (sym->type) {
163 case S_INT:
164 base = 10;
165 break;
166 case S_HEX:
167 base = 16;
168 break;
169 default:
170 return;
171 }
172 prop = sym_get_range_prop(sym);
173 if (!prop)
174 return;
175 val = strtol(sym->curr.val, NULL, base);
176 val2 = sym_get_range_val(prop->expr->left.sym, base);
177 if (val >= val2) {
178 val2 = sym_get_range_val(prop->expr->right.sym, base);
179 if (val <= val2)
180 return;
181 }
182 if (sym->type == S_INT)
183 sprintf(str, "%d", val2);
184 else
185 sprintf(str, "0x%x", val2);
186 sym->curr.val = strdup(str);
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static void sym_calc_visibility(struct symbol *sym)
190{
191 struct property *prop;
192 tristate tri;
193
194 /* any prompt visible? */
195 tri = no;
196 for_all_prompts(sym, prop) {
197 prop->visible.tri = expr_calc_value(prop->visible.expr);
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100198 tri = EXPR_OR(tri, prop->visible.tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
201 tri = yes;
202 if (sym->visible != tri) {
203 sym->visible = tri;
204 sym_set_changed(sym);
205 }
206 if (sym_is_choice_value(sym))
207 return;
Catalin Marinas246cf9c2010-06-08 17:25:57 +0100208 /* defaulting to "yes" if no explicit "depends on" are given */
209 tri = yes;
210 if (sym->dir_dep.expr)
211 tri = expr_calc_value(sym->dir_dep.expr);
212 if (tri == mod)
213 tri = yes;
214 if (sym->dir_dep.tri != tri) {
215 sym->dir_dep.tri = tri;
216 sym_set_changed(sym);
217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 tri = no;
219 if (sym->rev_dep.expr)
220 tri = expr_calc_value(sym->rev_dep.expr);
221 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
222 tri = yes;
223 if (sym->rev_dep.tri != tri) {
224 sym->rev_dep.tri = tri;
225 sym_set_changed(sym);
226 }
227}
228
Sam Ravnborgc2521472010-07-31 23:35:32 +0200229/*
230 * Find the default symbol for a choice.
231 * First try the default values for the choice symbol
232 * Next locate the first visible choice value
233 * Return NULL if none was found
234 */
235struct symbol *sym_choice_default(struct symbol *sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct symbol *def_sym;
238 struct property *prop;
239 struct expr *e;
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* any of the defaults visible? */
242 for_all_defaults(sym, prop) {
243 prop->visible.tri = expr_calc_value(prop->visible.expr);
244 if (prop->visible.tri == no)
245 continue;
246 def_sym = prop_get_symbol(prop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (def_sym->visible != no)
248 return def_sym;
249 }
250
251 /* just get the first visible value */
252 prop = sym_get_choice_prop(sym);
Jan Beulich0a28c472010-06-30 13:11:01 +0100253 expr_list_for_each_sym(prop->expr, e, def_sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (def_sym->visible != no)
255 return def_sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Sam Ravnborgc2521472010-07-31 23:35:32 +0200257 /* failed to locate any defaults */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return NULL;
259}
260
Sam Ravnborgc2521472010-07-31 23:35:32 +0200261static struct symbol *sym_calc_choice(struct symbol *sym)
262{
263 struct symbol *def_sym;
264 struct property *prop;
265 struct expr *e;
266
267 /* first calculate all choice values' visibilities */
268 prop = sym_get_choice_prop(sym);
269 expr_list_for_each_sym(prop->expr, e, def_sym)
270 sym_calc_visibility(def_sym);
271
272 /* is the user choice visible? */
273 def_sym = sym->def[S_DEF_USER].val;
274 if (def_sym && def_sym->visible != no)
275 return def_sym;
276
277 def_sym = sym_choice_default(sym);
278
279 if (def_sym == NULL)
280 /* no choice? reset tristate value */
281 sym->curr.tri = no;
282
283 return def_sym;
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286void sym_calc_value(struct symbol *sym)
287{
288 struct symbol_value newval, oldval;
289 struct property *prop;
290 struct expr *e;
291
292 if (!sym)
293 return;
294
295 if (sym->flags & SYMBOL_VALID)
296 return;
297 sym->flags |= SYMBOL_VALID;
298
299 oldval = sym->curr;
300
301 switch (sym->type) {
302 case S_INT:
303 case S_HEX:
304 case S_STRING:
305 newval = symbol_empty.curr;
306 break;
307 case S_BOOLEAN:
308 case S_TRISTATE:
309 newval = symbol_no.curr;
310 break;
311 default:
312 sym->curr.val = sym->name;
313 sym->curr.tri = no;
314 return;
315 }
316 if (!sym_is_choice_value(sym))
317 sym->flags &= ~SYMBOL_WRITE;
318
319 sym_calc_visibility(sym);
320
321 /* set default if recursively called */
322 sym->curr = newval;
323
324 switch (sym_get_type(sym)) {
325 case S_BOOLEAN:
326 case S_TRISTATE:
327 if (sym_is_choice_value(sym) && sym->visible == yes) {
328 prop = sym_get_choice_prop(sym);
329 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
Roman Zippel587c9062008-02-11 21:13:47 +0100330 } else {
331 if (sym->visible != no) {
332 /* if the symbol is visible use the user value
333 * if available, otherwise try the default value
334 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 sym->flags |= SYMBOL_WRITE;
Roman Zippel587c9062008-02-11 21:13:47 +0100336 if (sym_has_value(sym)) {
337 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
338 sym->visible);
339 goto calc_newval;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Roman Zippel587c9062008-02-11 21:13:47 +0100342 if (sym->rev_dep.tri != no)
343 sym->flags |= SYMBOL_WRITE;
344 if (!sym_is_choice(sym)) {
345 prop = sym_get_default_prop(sym);
346 if (prop) {
347 sym->flags |= SYMBOL_WRITE;
348 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
349 prop->visible.tri);
350 }
351 }
352 calc_newval:
Catalin Marinas246cf9c2010-06-08 17:25:57 +0100353 if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
354 fprintf(stderr, "warning: (");
355 expr_fprint(sym->rev_dep.expr, stderr);
356 fprintf(stderr, ") selects %s which has unmet direct dependencies (",
357 sym->name);
358 expr_fprint(sym->dir_dep.expr, stderr);
359 fprintf(stderr, ")\n");
360 }
Roman Zippel587c9062008-02-11 21:13:47 +0100361 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
364 newval.tri = yes;
365 break;
366 case S_STRING:
367 case S_HEX:
368 case S_INT:
369 if (sym->visible != no) {
370 sym->flags |= SYMBOL_WRITE;
371 if (sym_has_value(sym)) {
Roman Zippel0c1822e2006-06-08 22:12:41 -0700372 newval.val = sym->def[S_DEF_USER].val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
374 }
375 }
376 prop = sym_get_default_prop(sym);
377 if (prop) {
378 struct symbol *ds = prop_get_symbol(prop);
379 if (ds) {
380 sym->flags |= SYMBOL_WRITE;
381 sym_calc_value(ds);
382 newval.val = ds->curr.val;
383 }
384 }
385 break;
386 default:
387 ;
388 }
389
390 sym->curr = newval;
391 if (sym_is_choice(sym) && newval.tri == yes)
392 sym->curr.val = sym_calc_choice(sym);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800393 sym_validate_range(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Roman Zippelface4372006-06-08 22:12:45 -0700395 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 sym_set_changed(sym);
Roman Zippelface4372006-06-08 22:12:45 -0700397 if (modules_sym == sym) {
398 sym_set_all_changed();
399 modules_val = modules_sym->curr.tri;
400 }
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (sym_is_choice(sym)) {
Roman Zippel7a962922008-01-14 04:50:23 +0100404 struct symbol *choice_sym;
Roman Zippel7a962922008-01-14 04:50:23 +0100405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 prop = sym_get_choice_prop(sym);
Roman Zippel7a962922008-01-14 04:50:23 +0100407 expr_list_for_each_sym(prop->expr, e, choice_sym) {
Jan Beulich0a28c472010-06-30 13:11:01 +0100408 if ((sym->flags & SYMBOL_WRITE) &&
409 choice_sym->visible != no)
410 choice_sym->flags |= SYMBOL_WRITE;
411 if (sym->flags & SYMBOL_CHANGED)
Roman Zippel7a962922008-01-14 04:50:23 +0100412 sym_set_changed(choice_sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100415
416 if (sym->flags & SYMBOL_AUTO)
417 sym->flags &= ~SYMBOL_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420void sym_clear_all_valid(void)
421{
422 struct symbol *sym;
423 int i;
424
425 for_all_symbols(i, sym)
426 sym->flags &= ~SYMBOL_VALID;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800427 sym_add_change_count(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (modules_sym)
429 sym_calc_value(modules_sym);
430}
431
432void sym_set_changed(struct symbol *sym)
433{
434 struct property *prop;
435
436 sym->flags |= SYMBOL_CHANGED;
437 for (prop = sym->prop; prop; prop = prop->next) {
438 if (prop->menu)
439 prop->menu->flags |= MENU_CHANGED;
440 }
441}
442
443void sym_set_all_changed(void)
444{
445 struct symbol *sym;
446 int i;
447
448 for_all_symbols(i, sym)
449 sym_set_changed(sym);
450}
451
452bool sym_tristate_within_range(struct symbol *sym, tristate val)
453{
454 int type = sym_get_type(sym);
455
456 if (sym->visible == no)
457 return false;
458
459 if (type != S_BOOLEAN && type != S_TRISTATE)
460 return false;
461
462 if (type == S_BOOLEAN && val == mod)
463 return false;
464 if (sym->visible <= sym->rev_dep.tri)
465 return false;
466 if (sym_is_choice_value(sym) && sym->visible == yes)
467 return val == yes;
468 return val >= sym->rev_dep.tri && val <= sym->visible;
469}
470
471bool sym_set_tristate_value(struct symbol *sym, tristate val)
472{
473 tristate oldval = sym_get_tristate_value(sym);
474
475 if (oldval != val && !sym_tristate_within_range(sym, val))
476 return false;
477
Roman Zippel669bfad2006-06-08 22:12:42 -0700478 if (!(sym->flags & SYMBOL_DEF_USER)) {
479 sym->flags |= SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 sym_set_changed(sym);
481 }
Roman Zippel3f23ca22005-11-08 21:34:48 -0800482 /*
483 * setting a choice value also resets the new flag of the choice
484 * symbol and all other choice values.
485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (sym_is_choice_value(sym) && val == yes) {
487 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel3f23ca22005-11-08 21:34:48 -0800488 struct property *prop;
489 struct expr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Roman Zippel0c1822e2006-06-08 22:12:41 -0700491 cs->def[S_DEF_USER].val = sym;
Roman Zippel669bfad2006-06-08 22:12:42 -0700492 cs->flags |= SYMBOL_DEF_USER;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800493 prop = sym_get_choice_prop(cs);
494 for (e = prop->expr; e; e = e->left.expr) {
495 if (e->right.sym->visible != no)
Roman Zippel669bfad2006-06-08 22:12:42 -0700496 e->right.sym->flags |= SYMBOL_DEF_USER;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
Roman Zippel0c1822e2006-06-08 22:12:41 -0700500 sym->def[S_DEF_USER].tri = val;
Roman Zippelface4372006-06-08 22:12:45 -0700501 if (oldval != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 sym_clear_all_valid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 return true;
505}
506
507tristate sym_toggle_tristate_value(struct symbol *sym)
508{
509 tristate oldval, newval;
510
511 oldval = newval = sym_get_tristate_value(sym);
512 do {
513 switch (newval) {
514 case no:
515 newval = mod;
516 break;
517 case mod:
518 newval = yes;
519 break;
520 case yes:
521 newval = no;
522 break;
523 }
524 if (sym_set_tristate_value(sym, newval))
525 break;
526 } while (oldval != newval);
527 return newval;
528}
529
530bool sym_string_valid(struct symbol *sym, const char *str)
531{
532 signed char ch;
533
534 switch (sym->type) {
535 case S_STRING:
536 return true;
537 case S_INT:
538 ch = *str++;
539 if (ch == '-')
540 ch = *str++;
541 if (!isdigit(ch))
542 return false;
543 if (ch == '0' && *str != 0)
544 return false;
545 while ((ch = *str++)) {
546 if (!isdigit(ch))
547 return false;
548 }
549 return true;
550 case S_HEX:
551 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
552 str += 2;
553 ch = *str++;
554 do {
555 if (!isxdigit(ch))
556 return false;
557 } while ((ch = *str++));
558 return true;
559 case S_BOOLEAN:
560 case S_TRISTATE:
561 switch (str[0]) {
562 case 'y': case 'Y':
563 case 'm': case 'M':
564 case 'n': case 'N':
565 return true;
566 }
567 return false;
568 default:
569 return false;
570 }
571}
572
573bool sym_string_within_range(struct symbol *sym, const char *str)
574{
575 struct property *prop;
576 int val;
577
578 switch (sym->type) {
579 case S_STRING:
580 return sym_string_valid(sym, str);
581 case S_INT:
582 if (!sym_string_valid(sym, str))
583 return false;
584 prop = sym_get_range_prop(sym);
585 if (!prop)
586 return true;
587 val = strtol(str, NULL, 10);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800588 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
589 val <= sym_get_range_val(prop->expr->right.sym, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 case S_HEX:
591 if (!sym_string_valid(sym, str))
592 return false;
593 prop = sym_get_range_prop(sym);
594 if (!prop)
595 return true;
596 val = strtol(str, NULL, 16);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800597 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
598 val <= sym_get_range_val(prop->expr->right.sym, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 case S_BOOLEAN:
600 case S_TRISTATE:
601 switch (str[0]) {
602 case 'y': case 'Y':
603 return sym_tristate_within_range(sym, yes);
604 case 'm': case 'M':
605 return sym_tristate_within_range(sym, mod);
606 case 'n': case 'N':
607 return sym_tristate_within_range(sym, no);
608 }
609 return false;
610 default:
611 return false;
612 }
613}
614
615bool sym_set_string_value(struct symbol *sym, const char *newval)
616{
617 const char *oldval;
618 char *val;
619 int size;
620
621 switch (sym->type) {
622 case S_BOOLEAN:
623 case S_TRISTATE:
624 switch (newval[0]) {
625 case 'y': case 'Y':
626 return sym_set_tristate_value(sym, yes);
627 case 'm': case 'M':
628 return sym_set_tristate_value(sym, mod);
629 case 'n': case 'N':
630 return sym_set_tristate_value(sym, no);
631 }
632 return false;
633 default:
634 ;
635 }
636
637 if (!sym_string_within_range(sym, newval))
638 return false;
639
Roman Zippel669bfad2006-06-08 22:12:42 -0700640 if (!(sym->flags & SYMBOL_DEF_USER)) {
641 sym->flags |= SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 sym_set_changed(sym);
643 }
644
Roman Zippel0c1822e2006-06-08 22:12:41 -0700645 oldval = sym->def[S_DEF_USER].val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 size = strlen(newval) + 1;
647 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
648 size += 2;
Roman Zippel0c1822e2006-06-08 22:12:41 -0700649 sym->def[S_DEF_USER].val = val = malloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 *val++ = '0';
651 *val++ = 'x';
652 } else if (!oldval || strcmp(oldval, newval))
Roman Zippel0c1822e2006-06-08 22:12:41 -0700653 sym->def[S_DEF_USER].val = val = malloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 else
655 return true;
656
657 strcpy(val, newval);
658 free((void *)oldval);
659 sym_clear_all_valid();
660
661 return true;
662}
663
664const char *sym_get_string_value(struct symbol *sym)
665{
666 tristate val;
667
668 switch (sym->type) {
669 case S_BOOLEAN:
670 case S_TRISTATE:
671 val = sym_get_tristate_value(sym);
672 switch (val) {
673 case no:
674 return "n";
675 case mod:
676 return "m";
677 case yes:
678 return "y";
679 }
680 break;
681 default:
682 ;
683 }
684 return (const char *)sym->curr.val;
685}
686
687bool sym_is_changable(struct symbol *sym)
688{
689 return sym->visible > sym->rev_dep.tri;
690}
691
Andi Kleene66f25d2010-01-13 17:02:44 +0100692static unsigned strhash(const char *s)
693{
694 /* fnv32 hash */
695 unsigned hash = 2166136261U;
696 for (; *s; s++)
697 hash = (hash ^ *s) * 0x01000193;
698 return hash;
699}
700
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100701struct symbol *sym_lookup(const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct symbol *symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 char *new_name;
Andi Kleene66f25d2010-01-13 17:02:44 +0100705 int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (name) {
708 if (name[0] && !name[1]) {
709 switch (name[0]) {
710 case 'y': return &symbol_yes;
711 case 'm': return &symbol_mod;
712 case 'n': return &symbol_no;
713 }
714 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100715 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100718 if (symbol->name &&
719 !strcmp(symbol->name, name) &&
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100720 (flags ? symbol->flags & flags
721 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
722 return symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724 new_name = strdup(name);
725 } else {
726 new_name = NULL;
Andi Kleene66f25d2010-01-13 17:02:44 +0100727 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729
730 symbol = malloc(sizeof(*symbol));
731 memset(symbol, 0, sizeof(*symbol));
732 symbol->name = new_name;
733 symbol->type = S_UNKNOWN;
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100734 symbol->flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 symbol->next = symbol_hash[hash];
737 symbol_hash[hash] = symbol;
738
739 return symbol;
740}
741
742struct symbol *sym_find(const char *name)
743{
744 struct symbol *symbol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int hash = 0;
746
747 if (!name)
748 return NULL;
749
750 if (name[0] && !name[1]) {
751 switch (name[0]) {
752 case 'y': return &symbol_yes;
753 case 'm': return &symbol_mod;
754 case 'n': return &symbol_no;
755 }
756 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100757 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100760 if (symbol->name &&
761 !strcmp(symbol->name, name) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 !(symbol->flags & SYMBOL_CONST))
763 break;
764 }
765
766 return symbol;
767}
768
769struct symbol **sym_re_search(const char *pattern)
770{
771 struct symbol *sym, **sym_arr = NULL;
772 int i, cnt, size;
773 regex_t re;
774
775 cnt = size = 0;
776 /* Skip if empty */
777 if (strlen(pattern) == 0)
778 return NULL;
779 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
780 return NULL;
781
782 for_all_symbols(i, sym) {
783 if (sym->flags & SYMBOL_CONST || !sym->name)
784 continue;
785 if (regexec(&re, sym->name, 0, NULL, 0))
786 continue;
787 if (cnt + 1 >= size) {
788 void *tmp = sym_arr;
789 size += 16;
790 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
791 if (!sym_arr) {
792 free(tmp);
793 return NULL;
794 }
795 }
Li Zefanda6df872010-03-19 14:57:47 +0800796 sym_calc_value(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 sym_arr[cnt++] = sym;
798 }
799 if (sym_arr)
800 sym_arr[cnt] = NULL;
801 regfree(&re);
802
803 return sym_arr;
804}
805
Roman Zippeld595cea2010-07-31 23:35:30 +0200806/*
807 * When we check for recursive dependencies we use a stack to save
808 * current state so we can print out relevant info to user.
809 * The entries are located on the call stack so no need to free memory.
810 * Note inser() remove() must always match to properly clear the stack.
811 */
812static struct dep_stack {
813 struct dep_stack *prev, *next;
814 struct symbol *sym;
815 struct property *prop;
816 struct expr *expr;
817} *check_top;
818
819static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
820{
821 memset(stack, 0, sizeof(*stack));
822 if (check_top)
823 check_top->next = stack;
824 stack->prev = check_top;
825 stack->sym = sym;
826 check_top = stack;
827}
828
829static void dep_stack_remove(void)
830{
831 check_top = check_top->prev;
832 if (check_top)
833 check_top->next = NULL;
834}
835
836/*
837 * Called when we have detected a recursive dependency.
838 * check_top point to the top of the stact so we use
839 * the ->prev pointer to locate the bottom of the stack.
840 */
841static void sym_check_print_recursive(struct symbol *last_sym)
842{
843 struct dep_stack *stack;
844 struct symbol *sym, *next_sym;
845 struct menu *menu = NULL;
846 struct property *prop;
847 struct dep_stack cv_stack;
848
849 if (sym_is_choice_value(last_sym)) {
850 dep_stack_insert(&cv_stack, last_sym);
851 last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
852 }
853
854 for (stack = check_top; stack != NULL; stack = stack->prev)
855 if (stack->sym == last_sym)
856 break;
857 if (!stack) {
858 fprintf(stderr, "unexpected recursive dependency error\n");
859 return;
860 }
861
862 for (; stack; stack = stack->next) {
863 sym = stack->sym;
864 next_sym = stack->next ? stack->next->sym : last_sym;
865 prop = stack->prop;
866
867 /* for choice values find the menu entry (used below) */
868 if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
869 for (prop = sym->prop; prop; prop = prop->next) {
870 menu = prop->menu;
871 if (prop->menu)
872 break;
873 }
874 }
875 if (stack->sym == last_sym)
876 fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
877 prop->file->name, prop->lineno);
878 if (stack->expr) {
879 fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
880 prop->file->name, prop->lineno,
881 sym->name ? sym->name : "<choice>",
882 prop_get_type_name(prop->type),
883 next_sym->name ? next_sym->name : "<choice>");
884 } else if (stack->prop) {
885 fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
886 prop->file->name, prop->lineno,
887 sym->name ? sym->name : "<choice>",
888 next_sym->name ? next_sym->name : "<choice>");
889 } else if (sym_is_choice(sym)) {
890 fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
891 menu->file->name, menu->lineno,
892 sym->name ? sym->name : "<choice>",
893 next_sym->name ? next_sym->name : "<choice>");
894 } else if (sym_is_choice_value(sym)) {
895 fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
896 menu->file->name, menu->lineno,
897 sym->name ? sym->name : "<choice>",
898 next_sym->name ? next_sym->name : "<choice>");
899 } else {
900 fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
901 prop->file->name, prop->lineno,
902 sym->name ? sym->name : "<choice>",
903 next_sym->name ? next_sym->name : "<choice>");
904 }
905 }
906
907 if (check_top == &cv_stack)
908 dep_stack_remove();
909}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911static struct symbol *sym_check_expr_deps(struct expr *e)
912{
913 struct symbol *sym;
914
915 if (!e)
916 return NULL;
917 switch (e->type) {
918 case E_OR:
919 case E_AND:
920 sym = sym_check_expr_deps(e->left.expr);
921 if (sym)
922 return sym;
923 return sym_check_expr_deps(e->right.expr);
924 case E_NOT:
925 return sym_check_expr_deps(e->left.expr);
926 case E_EQUAL:
927 case E_UNEQUAL:
928 sym = sym_check_deps(e->left.sym);
929 if (sym)
930 return sym;
931 return sym_check_deps(e->right.sym);
932 case E_SYMBOL:
933 return sym_check_deps(e->left.sym);
934 default:
935 break;
936 }
937 printf("Oops! How to check %d?\n", e->type);
938 return NULL;
939}
940
Sam Ravnborg5447d342007-05-06 09:20:10 +0200941/* return NULL when dependencies are OK */
Roman Zippel48981172008-02-29 05:10:24 +0100942static struct symbol *sym_check_sym_deps(struct symbol *sym)
943{
944 struct symbol *sym2;
945 struct property *prop;
Roman Zippeld595cea2010-07-31 23:35:30 +0200946 struct dep_stack stack;
947
948 dep_stack_insert(&stack, sym);
Roman Zippel48981172008-02-29 05:10:24 +0100949
950 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
951 if (sym2)
Roman Zippeld595cea2010-07-31 23:35:30 +0200952 goto out;
Roman Zippel48981172008-02-29 05:10:24 +0100953
954 for (prop = sym->prop; prop; prop = prop->next) {
955 if (prop->type == P_CHOICE || prop->type == P_SELECT)
956 continue;
Roman Zippeld595cea2010-07-31 23:35:30 +0200957 stack.prop = prop;
Roman Zippel48981172008-02-29 05:10:24 +0100958 sym2 = sym_check_expr_deps(prop->visible.expr);
959 if (sym2)
960 break;
961 if (prop->type != P_DEFAULT || sym_is_choice(sym))
962 continue;
Roman Zippeld595cea2010-07-31 23:35:30 +0200963 stack.expr = prop->expr;
Roman Zippel48981172008-02-29 05:10:24 +0100964 sym2 = sym_check_expr_deps(prop->expr);
965 if (sym2)
966 break;
Roman Zippeld595cea2010-07-31 23:35:30 +0200967 stack.expr = NULL;
Roman Zippel48981172008-02-29 05:10:24 +0100968 }
969
Roman Zippeld595cea2010-07-31 23:35:30 +0200970out:
971 dep_stack_remove();
972
Roman Zippel48981172008-02-29 05:10:24 +0100973 return sym2;
974}
975
976static struct symbol *sym_check_choice_deps(struct symbol *choice)
977{
978 struct symbol *sym, *sym2;
979 struct property *prop;
980 struct expr *e;
Roman Zippeld595cea2010-07-31 23:35:30 +0200981 struct dep_stack stack;
982
983 dep_stack_insert(&stack, choice);
Roman Zippel48981172008-02-29 05:10:24 +0100984
985 prop = sym_get_choice_prop(choice);
986 expr_list_for_each_sym(prop->expr, e, sym)
987 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
988
989 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
990 sym2 = sym_check_sym_deps(choice);
991 choice->flags &= ~SYMBOL_CHECK;
992 if (sym2)
993 goto out;
994
995 expr_list_for_each_sym(prop->expr, e, sym) {
996 sym2 = sym_check_sym_deps(sym);
Roman Zippeld595cea2010-07-31 23:35:30 +0200997 if (sym2)
Roman Zippel48981172008-02-29 05:10:24 +0100998 break;
Roman Zippel48981172008-02-29 05:10:24 +0100999 }
1000out:
1001 expr_list_for_each_sym(prop->expr, e, sym)
1002 sym->flags &= ~SYMBOL_CHECK;
1003
1004 if (sym2 && sym_is_choice_value(sym2) &&
1005 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1006 sym2 = choice;
1007
Roman Zippeld595cea2010-07-31 23:35:30 +02001008 dep_stack_remove();
1009
Roman Zippel48981172008-02-29 05:10:24 +01001010 return sym2;
1011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013struct symbol *sym_check_deps(struct symbol *sym)
1014{
1015 struct symbol *sym2;
1016 struct property *prop;
1017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (sym->flags & SYMBOL_CHECK) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001019 sym_check_print_recursive(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return sym;
1021 }
David Gibson3f04e7d2005-11-08 21:34:46 -08001022 if (sym->flags & SYMBOL_CHECKED)
1023 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Roman Zippel48981172008-02-29 05:10:24 +01001025 if (sym_is_choice_value(sym)) {
Roman Zippeld595cea2010-07-31 23:35:30 +02001026 struct dep_stack stack;
1027
Roman Zippel48981172008-02-29 05:10:24 +01001028 /* for choice groups start the check with main choice symbol */
Roman Zippeld595cea2010-07-31 23:35:30 +02001029 dep_stack_insert(&stack, sym);
Roman Zippel48981172008-02-29 05:10:24 +01001030 prop = sym_get_choice_prop(sym);
1031 sym2 = sym_check_deps(prop_get_symbol(prop));
Roman Zippeld595cea2010-07-31 23:35:30 +02001032 dep_stack_remove();
Roman Zippel48981172008-02-29 05:10:24 +01001033 } else if (sym_is_choice(sym)) {
1034 sym2 = sym_check_choice_deps(sym);
1035 } else {
1036 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1037 sym2 = sym_check_sym_deps(sym);
1038 sym->flags &= ~SYMBOL_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
Roman Zippel48981172008-02-29 05:10:24 +01001040
Roman Zippeld595cea2010-07-31 23:35:30 +02001041 if (sym2 && sym2 == sym)
1042 sym2 = NULL;
Roman Zippel48981172008-02-29 05:10:24 +01001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return sym2;
1045}
1046
1047struct property *prop_alloc(enum prop_type type, struct symbol *sym)
1048{
1049 struct property *prop;
1050 struct property **propp;
1051
1052 prop = malloc(sizeof(*prop));
1053 memset(prop, 0, sizeof(*prop));
1054 prop->type = type;
1055 prop->sym = sym;
1056 prop->file = current_file;
1057 prop->lineno = zconf_lineno();
1058
1059 /* append property to the prop list of symbol */
1060 if (sym) {
1061 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
1062 ;
1063 *propp = prop;
1064 }
1065
1066 return prop;
1067}
1068
1069struct symbol *prop_get_symbol(struct property *prop)
1070{
1071 if (prop->expr && (prop->expr->type == E_SYMBOL ||
Roman Zippel7a962922008-01-14 04:50:23 +01001072 prop->expr->type == E_LIST))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return prop->expr->left.sym;
1074 return NULL;
1075}
1076
1077const char *prop_get_type_name(enum prop_type type)
1078{
1079 switch (type) {
1080 case P_PROMPT:
1081 return "prompt";
Roman Zippel93449082008-01-14 04:50:54 +01001082 case P_ENV:
1083 return "env";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 case P_COMMENT:
1085 return "comment";
1086 case P_MENU:
1087 return "menu";
1088 case P_DEFAULT:
1089 return "default";
1090 case P_CHOICE:
1091 return "choice";
1092 case P_SELECT:
1093 return "select";
1094 case P_RANGE:
1095 return "range";
Sam Ravnborg59e89e32010-07-31 23:35:29 +02001096 case P_SYMBOL:
1097 return "symbol";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 case P_UNKNOWN:
1099 break;
1100 }
1101 return "unknown";
1102}
Roman Zippel93449082008-01-14 04:50:54 +01001103
Trevor Keith4356f482009-09-18 12:49:23 -07001104static void prop_add_env(const char *env)
Roman Zippel93449082008-01-14 04:50:54 +01001105{
1106 struct symbol *sym, *sym2;
1107 struct property *prop;
1108 char *p;
1109
1110 sym = current_entry->sym;
1111 sym->flags |= SYMBOL_AUTO;
1112 for_all_properties(sym, prop, P_ENV) {
1113 sym2 = prop_get_symbol(prop);
1114 if (strcmp(sym2->name, env))
1115 menu_warn(current_entry, "redefining environment symbol from %s",
1116 sym2->name);
1117 return;
1118 }
1119
1120 prop = prop_alloc(P_ENV, sym);
Roman Zippel5a1aa8a2008-02-29 05:11:50 +01001121 prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
Roman Zippel93449082008-01-14 04:50:54 +01001122
1123 sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
1124 sym_env_list->right.sym = sym;
1125
1126 p = getenv(env);
1127 if (p)
1128 sym_add_default(sym, p);
1129 else
1130 menu_warn(current_entry, "environment variable %s undefined", env);
1131}