blob: c127fa342f1da0e024698d1911260895c9dce71c [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
229static struct symbol *sym_calc_choice(struct symbol *sym)
230{
231 struct symbol *def_sym;
232 struct property *prop;
233 struct expr *e;
234
Jan Beulich0a28c472010-06-30 13:11:01 +0100235 /* first calculate all choice values' visibilities */
236 prop = sym_get_choice_prop(sym);
237 expr_list_for_each_sym(prop->expr, e, def_sym)
238 sym_calc_visibility(def_sym);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* is the user choice visible? */
Roman Zippel0c1822e2006-06-08 22:12:41 -0700241 def_sym = sym->def[S_DEF_USER].val;
Jan Beulich0a28c472010-06-30 13:11:01 +0100242 if (def_sym && def_sym->visible != no)
243 return def_sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* any of the defaults visible? */
246 for_all_defaults(sym, prop) {
247 prop->visible.tri = expr_calc_value(prop->visible.expr);
248 if (prop->visible.tri == no)
249 continue;
250 def_sym = prop_get_symbol(prop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (def_sym->visible != no)
252 return def_sym;
253 }
254
255 /* just get the first visible value */
256 prop = sym_get_choice_prop(sym);
Jan Beulich0a28c472010-06-30 13:11:01 +0100257 expr_list_for_each_sym(prop->expr, e, def_sym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (def_sym->visible != no)
259 return def_sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /* no choice? reset tristate value */
262 sym->curr.tri = no;
263 return NULL;
264}
265
266void sym_calc_value(struct symbol *sym)
267{
268 struct symbol_value newval, oldval;
269 struct property *prop;
270 struct expr *e;
271
272 if (!sym)
273 return;
274
275 if (sym->flags & SYMBOL_VALID)
276 return;
277 sym->flags |= SYMBOL_VALID;
278
279 oldval = sym->curr;
280
281 switch (sym->type) {
282 case S_INT:
283 case S_HEX:
284 case S_STRING:
285 newval = symbol_empty.curr;
286 break;
287 case S_BOOLEAN:
288 case S_TRISTATE:
289 newval = symbol_no.curr;
290 break;
291 default:
292 sym->curr.val = sym->name;
293 sym->curr.tri = no;
294 return;
295 }
296 if (!sym_is_choice_value(sym))
297 sym->flags &= ~SYMBOL_WRITE;
298
299 sym_calc_visibility(sym);
300
301 /* set default if recursively called */
302 sym->curr = newval;
303
304 switch (sym_get_type(sym)) {
305 case S_BOOLEAN:
306 case S_TRISTATE:
307 if (sym_is_choice_value(sym) && sym->visible == yes) {
308 prop = sym_get_choice_prop(sym);
309 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
Roman Zippel587c9062008-02-11 21:13:47 +0100310 } else {
311 if (sym->visible != no) {
312 /* if the symbol is visible use the user value
313 * if available, otherwise try the default value
314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 sym->flags |= SYMBOL_WRITE;
Roman Zippel587c9062008-02-11 21:13:47 +0100316 if (sym_has_value(sym)) {
317 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
318 sym->visible);
319 goto calc_newval;
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
Roman Zippel587c9062008-02-11 21:13:47 +0100322 if (sym->rev_dep.tri != no)
323 sym->flags |= SYMBOL_WRITE;
324 if (!sym_is_choice(sym)) {
325 prop = sym_get_default_prop(sym);
326 if (prop) {
327 sym->flags |= SYMBOL_WRITE;
328 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
329 prop->visible.tri);
330 }
331 }
332 calc_newval:
Catalin Marinas246cf9c2010-06-08 17:25:57 +0100333 if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
334 fprintf(stderr, "warning: (");
335 expr_fprint(sym->rev_dep.expr, stderr);
336 fprintf(stderr, ") selects %s which has unmet direct dependencies (",
337 sym->name);
338 expr_fprint(sym->dir_dep.expr, stderr);
339 fprintf(stderr, ")\n");
340 }
Roman Zippel587c9062008-02-11 21:13:47 +0100341 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
344 newval.tri = yes;
345 break;
346 case S_STRING:
347 case S_HEX:
348 case S_INT:
349 if (sym->visible != no) {
350 sym->flags |= SYMBOL_WRITE;
351 if (sym_has_value(sym)) {
Roman Zippel0c1822e2006-06-08 22:12:41 -0700352 newval.val = sym->def[S_DEF_USER].val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 }
355 }
356 prop = sym_get_default_prop(sym);
357 if (prop) {
358 struct symbol *ds = prop_get_symbol(prop);
359 if (ds) {
360 sym->flags |= SYMBOL_WRITE;
361 sym_calc_value(ds);
362 newval.val = ds->curr.val;
363 }
364 }
365 break;
366 default:
367 ;
368 }
369
370 sym->curr = newval;
371 if (sym_is_choice(sym) && newval.tri == yes)
372 sym->curr.val = sym_calc_choice(sym);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800373 sym_validate_range(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Roman Zippelface4372006-06-08 22:12:45 -0700375 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 sym_set_changed(sym);
Roman Zippelface4372006-06-08 22:12:45 -0700377 if (modules_sym == sym) {
378 sym_set_all_changed();
379 modules_val = modules_sym->curr.tri;
380 }
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if (sym_is_choice(sym)) {
Roman Zippel7a962922008-01-14 04:50:23 +0100384 struct symbol *choice_sym;
Roman Zippel7a962922008-01-14 04:50:23 +0100385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 prop = sym_get_choice_prop(sym);
Roman Zippel7a962922008-01-14 04:50:23 +0100387 expr_list_for_each_sym(prop->expr, e, choice_sym) {
Jan Beulich0a28c472010-06-30 13:11:01 +0100388 if ((sym->flags & SYMBOL_WRITE) &&
389 choice_sym->visible != no)
390 choice_sym->flags |= SYMBOL_WRITE;
391 if (sym->flags & SYMBOL_CHANGED)
Roman Zippel7a962922008-01-14 04:50:23 +0100392 sym_set_changed(choice_sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 }
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100395
396 if (sym->flags & SYMBOL_AUTO)
397 sym->flags &= ~SYMBOL_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400void sym_clear_all_valid(void)
401{
402 struct symbol *sym;
403 int i;
404
405 for_all_symbols(i, sym)
406 sym->flags &= ~SYMBOL_VALID;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800407 sym_add_change_count(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (modules_sym)
409 sym_calc_value(modules_sym);
410}
411
412void sym_set_changed(struct symbol *sym)
413{
414 struct property *prop;
415
416 sym->flags |= SYMBOL_CHANGED;
417 for (prop = sym->prop; prop; prop = prop->next) {
418 if (prop->menu)
419 prop->menu->flags |= MENU_CHANGED;
420 }
421}
422
423void sym_set_all_changed(void)
424{
425 struct symbol *sym;
426 int i;
427
428 for_all_symbols(i, sym)
429 sym_set_changed(sym);
430}
431
432bool sym_tristate_within_range(struct symbol *sym, tristate val)
433{
434 int type = sym_get_type(sym);
435
436 if (sym->visible == no)
437 return false;
438
439 if (type != S_BOOLEAN && type != S_TRISTATE)
440 return false;
441
442 if (type == S_BOOLEAN && val == mod)
443 return false;
444 if (sym->visible <= sym->rev_dep.tri)
445 return false;
446 if (sym_is_choice_value(sym) && sym->visible == yes)
447 return val == yes;
448 return val >= sym->rev_dep.tri && val <= sym->visible;
449}
450
451bool sym_set_tristate_value(struct symbol *sym, tristate val)
452{
453 tristate oldval = sym_get_tristate_value(sym);
454
455 if (oldval != val && !sym_tristate_within_range(sym, val))
456 return false;
457
Roman Zippel669bfad2006-06-08 22:12:42 -0700458 if (!(sym->flags & SYMBOL_DEF_USER)) {
459 sym->flags |= SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 sym_set_changed(sym);
461 }
Roman Zippel3f23ca22005-11-08 21:34:48 -0800462 /*
463 * setting a choice value also resets the new flag of the choice
464 * symbol and all other choice values.
465 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (sym_is_choice_value(sym) && val == yes) {
467 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel3f23ca22005-11-08 21:34:48 -0800468 struct property *prop;
469 struct expr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Roman Zippel0c1822e2006-06-08 22:12:41 -0700471 cs->def[S_DEF_USER].val = sym;
Roman Zippel669bfad2006-06-08 22:12:42 -0700472 cs->flags |= SYMBOL_DEF_USER;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800473 prop = sym_get_choice_prop(cs);
474 for (e = prop->expr; e; e = e->left.expr) {
475 if (e->right.sym->visible != no)
Roman Zippel669bfad2006-06-08 22:12:42 -0700476 e->right.sym->flags |= SYMBOL_DEF_USER;
Roman Zippel3f23ca22005-11-08 21:34:48 -0800477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479
Roman Zippel0c1822e2006-06-08 22:12:41 -0700480 sym->def[S_DEF_USER].tri = val;
Roman Zippelface4372006-06-08 22:12:45 -0700481 if (oldval != val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 sym_clear_all_valid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 return true;
485}
486
487tristate sym_toggle_tristate_value(struct symbol *sym)
488{
489 tristate oldval, newval;
490
491 oldval = newval = sym_get_tristate_value(sym);
492 do {
493 switch (newval) {
494 case no:
495 newval = mod;
496 break;
497 case mod:
498 newval = yes;
499 break;
500 case yes:
501 newval = no;
502 break;
503 }
504 if (sym_set_tristate_value(sym, newval))
505 break;
506 } while (oldval != newval);
507 return newval;
508}
509
510bool sym_string_valid(struct symbol *sym, const char *str)
511{
512 signed char ch;
513
514 switch (sym->type) {
515 case S_STRING:
516 return true;
517 case S_INT:
518 ch = *str++;
519 if (ch == '-')
520 ch = *str++;
521 if (!isdigit(ch))
522 return false;
523 if (ch == '0' && *str != 0)
524 return false;
525 while ((ch = *str++)) {
526 if (!isdigit(ch))
527 return false;
528 }
529 return true;
530 case S_HEX:
531 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
532 str += 2;
533 ch = *str++;
534 do {
535 if (!isxdigit(ch))
536 return false;
537 } while ((ch = *str++));
538 return true;
539 case S_BOOLEAN:
540 case S_TRISTATE:
541 switch (str[0]) {
542 case 'y': case 'Y':
543 case 'm': case 'M':
544 case 'n': case 'N':
545 return true;
546 }
547 return false;
548 default:
549 return false;
550 }
551}
552
553bool sym_string_within_range(struct symbol *sym, const char *str)
554{
555 struct property *prop;
556 int val;
557
558 switch (sym->type) {
559 case S_STRING:
560 return sym_string_valid(sym, str);
561 case S_INT:
562 if (!sym_string_valid(sym, str))
563 return false;
564 prop = sym_get_range_prop(sym);
565 if (!prop)
566 return true;
567 val = strtol(str, NULL, 10);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800568 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
569 val <= sym_get_range_val(prop->expr->right.sym, 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 case S_HEX:
571 if (!sym_string_valid(sym, str))
572 return false;
573 prop = sym_get_range_prop(sym);
574 if (!prop)
575 return true;
576 val = strtol(str, NULL, 16);
Roman Zippel4cf3cbe2005-11-08 21:34:49 -0800577 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
578 val <= sym_get_range_val(prop->expr->right.sym, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 case S_BOOLEAN:
580 case S_TRISTATE:
581 switch (str[0]) {
582 case 'y': case 'Y':
583 return sym_tristate_within_range(sym, yes);
584 case 'm': case 'M':
585 return sym_tristate_within_range(sym, mod);
586 case 'n': case 'N':
587 return sym_tristate_within_range(sym, no);
588 }
589 return false;
590 default:
591 return false;
592 }
593}
594
595bool sym_set_string_value(struct symbol *sym, const char *newval)
596{
597 const char *oldval;
598 char *val;
599 int size;
600
601 switch (sym->type) {
602 case S_BOOLEAN:
603 case S_TRISTATE:
604 switch (newval[0]) {
605 case 'y': case 'Y':
606 return sym_set_tristate_value(sym, yes);
607 case 'm': case 'M':
608 return sym_set_tristate_value(sym, mod);
609 case 'n': case 'N':
610 return sym_set_tristate_value(sym, no);
611 }
612 return false;
613 default:
614 ;
615 }
616
617 if (!sym_string_within_range(sym, newval))
618 return false;
619
Roman Zippel669bfad2006-06-08 22:12:42 -0700620 if (!(sym->flags & SYMBOL_DEF_USER)) {
621 sym->flags |= SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 sym_set_changed(sym);
623 }
624
Roman Zippel0c1822e2006-06-08 22:12:41 -0700625 oldval = sym->def[S_DEF_USER].val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 size = strlen(newval) + 1;
627 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
628 size += 2;
Roman Zippel0c1822e2006-06-08 22:12:41 -0700629 sym->def[S_DEF_USER].val = val = malloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 *val++ = '0';
631 *val++ = 'x';
632 } else if (!oldval || strcmp(oldval, newval))
Roman Zippel0c1822e2006-06-08 22:12:41 -0700633 sym->def[S_DEF_USER].val = val = malloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 else
635 return true;
636
637 strcpy(val, newval);
638 free((void *)oldval);
639 sym_clear_all_valid();
640
641 return true;
642}
643
644const char *sym_get_string_value(struct symbol *sym)
645{
646 tristate val;
647
648 switch (sym->type) {
649 case S_BOOLEAN:
650 case S_TRISTATE:
651 val = sym_get_tristate_value(sym);
652 switch (val) {
653 case no:
654 return "n";
655 case mod:
656 return "m";
657 case yes:
658 return "y";
659 }
660 break;
661 default:
662 ;
663 }
664 return (const char *)sym->curr.val;
665}
666
667bool sym_is_changable(struct symbol *sym)
668{
669 return sym->visible > sym->rev_dep.tri;
670}
671
Andi Kleene66f25d2010-01-13 17:02:44 +0100672static unsigned strhash(const char *s)
673{
674 /* fnv32 hash */
675 unsigned hash = 2166136261U;
676 for (; *s; s++)
677 hash = (hash ^ *s) * 0x01000193;
678 return hash;
679}
680
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100681struct symbol *sym_lookup(const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 struct symbol *symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 char *new_name;
Andi Kleene66f25d2010-01-13 17:02:44 +0100685 int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (name) {
688 if (name[0] && !name[1]) {
689 switch (name[0]) {
690 case 'y': return &symbol_yes;
691 case 'm': return &symbol_mod;
692 case 'n': return &symbol_no;
693 }
694 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100695 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100698 if (symbol->name &&
699 !strcmp(symbol->name, name) &&
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100700 (flags ? symbol->flags & flags
701 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
702 return symbol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704 new_name = strdup(name);
705 } else {
706 new_name = NULL;
Andi Kleene66f25d2010-01-13 17:02:44 +0100707 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
710 symbol = malloc(sizeof(*symbol));
711 memset(symbol, 0, sizeof(*symbol));
712 symbol->name = new_name;
713 symbol->type = S_UNKNOWN;
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100714 symbol->flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 symbol->next = symbol_hash[hash];
717 symbol_hash[hash] = symbol;
718
719 return symbol;
720}
721
722struct symbol *sym_find(const char *name)
723{
724 struct symbol *symbol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int hash = 0;
726
727 if (!name)
728 return NULL;
729
730 if (name[0] && !name[1]) {
731 switch (name[0]) {
732 case 'y': return &symbol_yes;
733 case 'm': return &symbol_mod;
734 case 'n': return &symbol_no;
735 }
736 }
Andi Kleene66f25d2010-01-13 17:02:44 +0100737 hash = strhash(name) % SYMBOL_HASHSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Andi Kleene66f25d2010-01-13 17:02:44 +0100740 if (symbol->name &&
741 !strcmp(symbol->name, name) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 !(symbol->flags & SYMBOL_CONST))
743 break;
744 }
745
746 return symbol;
747}
748
749struct symbol **sym_re_search(const char *pattern)
750{
751 struct symbol *sym, **sym_arr = NULL;
752 int i, cnt, size;
753 regex_t re;
754
755 cnt = size = 0;
756 /* Skip if empty */
757 if (strlen(pattern) == 0)
758 return NULL;
759 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
760 return NULL;
761
762 for_all_symbols(i, sym) {
763 if (sym->flags & SYMBOL_CONST || !sym->name)
764 continue;
765 if (regexec(&re, sym->name, 0, NULL, 0))
766 continue;
767 if (cnt + 1 >= size) {
768 void *tmp = sym_arr;
769 size += 16;
770 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
771 if (!sym_arr) {
772 free(tmp);
773 return NULL;
774 }
775 }
Li Zefanda6df872010-03-19 14:57:47 +0800776 sym_calc_value(sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 sym_arr[cnt++] = sym;
778 }
779 if (sym_arr)
780 sym_arr[cnt] = NULL;
781 regfree(&re);
782
783 return sym_arr;
784}
785
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787static struct symbol *sym_check_expr_deps(struct expr *e)
788{
789 struct symbol *sym;
790
791 if (!e)
792 return NULL;
793 switch (e->type) {
794 case E_OR:
795 case E_AND:
796 sym = sym_check_expr_deps(e->left.expr);
797 if (sym)
798 return sym;
799 return sym_check_expr_deps(e->right.expr);
800 case E_NOT:
801 return sym_check_expr_deps(e->left.expr);
802 case E_EQUAL:
803 case E_UNEQUAL:
804 sym = sym_check_deps(e->left.sym);
805 if (sym)
806 return sym;
807 return sym_check_deps(e->right.sym);
808 case E_SYMBOL:
809 return sym_check_deps(e->left.sym);
810 default:
811 break;
812 }
813 printf("Oops! How to check %d?\n", e->type);
814 return NULL;
815}
816
Sam Ravnborg5447d342007-05-06 09:20:10 +0200817/* return NULL when dependencies are OK */
Roman Zippel48981172008-02-29 05:10:24 +0100818static struct symbol *sym_check_sym_deps(struct symbol *sym)
819{
820 struct symbol *sym2;
821 struct property *prop;
822
823 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
824 if (sym2)
825 return sym2;
826
827 for (prop = sym->prop; prop; prop = prop->next) {
828 if (prop->type == P_CHOICE || prop->type == P_SELECT)
829 continue;
830 sym2 = sym_check_expr_deps(prop->visible.expr);
831 if (sym2)
832 break;
833 if (prop->type != P_DEFAULT || sym_is_choice(sym))
834 continue;
835 sym2 = sym_check_expr_deps(prop->expr);
836 if (sym2)
837 break;
838 }
839
840 return sym2;
841}
842
843static struct symbol *sym_check_choice_deps(struct symbol *choice)
844{
845 struct symbol *sym, *sym2;
846 struct property *prop;
847 struct expr *e;
848
849 prop = sym_get_choice_prop(choice);
850 expr_list_for_each_sym(prop->expr, e, sym)
851 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
852
853 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
854 sym2 = sym_check_sym_deps(choice);
855 choice->flags &= ~SYMBOL_CHECK;
856 if (sym2)
857 goto out;
858
859 expr_list_for_each_sym(prop->expr, e, sym) {
860 sym2 = sym_check_sym_deps(sym);
861 if (sym2) {
862 fprintf(stderr, " -> %s", sym->name);
863 break;
864 }
865 }
866out:
867 expr_list_for_each_sym(prop->expr, e, sym)
868 sym->flags &= ~SYMBOL_CHECK;
869
870 if (sym2 && sym_is_choice_value(sym2) &&
871 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
872 sym2 = choice;
873
874 return sym2;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877struct symbol *sym_check_deps(struct symbol *sym)
878{
879 struct symbol *sym2;
880 struct property *prop;
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (sym->flags & SYMBOL_CHECK) {
Sam Ravnborg5447d342007-05-06 09:20:10 +0200883 fprintf(stderr, "%s:%d:error: found recursive dependency: %s",
Roman Zippel48981172008-02-29 05:10:24 +0100884 sym->prop->file->name, sym->prop->lineno,
885 sym->name ? sym->name : "<choice>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return sym;
887 }
David Gibson3f04e7d2005-11-08 21:34:46 -0800888 if (sym->flags & SYMBOL_CHECKED)
889 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Roman Zippel48981172008-02-29 05:10:24 +0100891 if (sym_is_choice_value(sym)) {
892 /* for choice groups start the check with main choice symbol */
893 prop = sym_get_choice_prop(sym);
894 sym2 = sym_check_deps(prop_get_symbol(prop));
895 } else if (sym_is_choice(sym)) {
896 sym2 = sym_check_choice_deps(sym);
897 } else {
898 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
899 sym2 = sym_check_sym_deps(sym);
900 sym->flags &= ~SYMBOL_CHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
Roman Zippel48981172008-02-29 05:10:24 +0100902
903 if (sym2) {
904 fprintf(stderr, " -> %s", sym->name ? sym->name : "<choice>");
905 if (sym2 == sym) {
906 fprintf(stderr, "\n");
907 zconfnerrs++;
908 sym2 = NULL;
909 }
910 }
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return sym2;
913}
914
915struct property *prop_alloc(enum prop_type type, struct symbol *sym)
916{
917 struct property *prop;
918 struct property **propp;
919
920 prop = malloc(sizeof(*prop));
921 memset(prop, 0, sizeof(*prop));
922 prop->type = type;
923 prop->sym = sym;
924 prop->file = current_file;
925 prop->lineno = zconf_lineno();
926
927 /* append property to the prop list of symbol */
928 if (sym) {
929 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
930 ;
931 *propp = prop;
932 }
933
934 return prop;
935}
936
937struct symbol *prop_get_symbol(struct property *prop)
938{
939 if (prop->expr && (prop->expr->type == E_SYMBOL ||
Roman Zippel7a962922008-01-14 04:50:23 +0100940 prop->expr->type == E_LIST))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return prop->expr->left.sym;
942 return NULL;
943}
944
945const char *prop_get_type_name(enum prop_type type)
946{
947 switch (type) {
948 case P_PROMPT:
949 return "prompt";
Roman Zippel93449082008-01-14 04:50:54 +0100950 case P_ENV:
951 return "env";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 case P_COMMENT:
953 return "comment";
954 case P_MENU:
955 return "menu";
956 case P_DEFAULT:
957 return "default";
958 case P_CHOICE:
959 return "choice";
960 case P_SELECT:
961 return "select";
962 case P_RANGE:
963 return "range";
964 case P_UNKNOWN:
965 break;
966 }
967 return "unknown";
968}
Roman Zippel93449082008-01-14 04:50:54 +0100969
Trevor Keith4356f482009-09-18 12:49:23 -0700970static void prop_add_env(const char *env)
Roman Zippel93449082008-01-14 04:50:54 +0100971{
972 struct symbol *sym, *sym2;
973 struct property *prop;
974 char *p;
975
976 sym = current_entry->sym;
977 sym->flags |= SYMBOL_AUTO;
978 for_all_properties(sym, prop, P_ENV) {
979 sym2 = prop_get_symbol(prop);
980 if (strcmp(sym2->name, env))
981 menu_warn(current_entry, "redefining environment symbol from %s",
982 sym2->name);
983 return;
984 }
985
986 prop = prop_alloc(P_ENV, sym);
Roman Zippel5a1aa8a2008-02-29 05:11:50 +0100987 prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
Roman Zippel93449082008-01-14 04:50:54 +0100988
989 sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
990 sym_env_list->right.sym = sym;
991
992 p = getenv(env);
993 if (p)
994 sym_add_default(sym, p);
995 else
996 menu_warn(current_entry, "environment variable %s undefined", env);
997}