blob: a9fae9c13afb8c863d7ca8aaae37467e302b2981 [file] [log] [blame]
Eric Andersenc9f20d92002-12-05 08:41:41 +00001/*
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 <sys/utsname.h>
10
11#define LKC_DIRECT_LINK
12#include "lkc.h"
13
14struct symbol symbol_yes = {
Eric Andersen837f0582004-07-15 06:01:05 +000015 .name = "y",
16 .curr = { "y", yes },
17 .flags = SYMBOL_YES|SYMBOL_VALID,
Eric Andersenc9f20d92002-12-05 08:41:41 +000018}, symbol_mod = {
Eric Andersen837f0582004-07-15 06:01:05 +000019 .name = "m",
20 .curr = { "m", mod },
21 .flags = SYMBOL_MOD|SYMBOL_VALID,
Eric Andersenc9f20d92002-12-05 08:41:41 +000022}, symbol_no = {
Eric Andersen837f0582004-07-15 06:01:05 +000023 .name = "n",
24 .curr = { "n", no },
25 .flags = SYMBOL_NO|SYMBOL_VALID,
Eric Andersenc9f20d92002-12-05 08:41:41 +000026}, symbol_empty = {
Eric Andersen837f0582004-07-15 06:01:05 +000027 .name = "",
28 .curr = { "", no },
29 .flags = SYMBOL_VALID,
Eric Andersenc9f20d92002-12-05 08:41:41 +000030};
31
32int sym_change_count;
33struct symbol *modules_sym;
Eric Andersen837f0582004-07-15 06:01:05 +000034tristate modules_val;
Eric Andersenc9f20d92002-12-05 08:41:41 +000035
36void sym_add_default(struct symbol *sym, const char *def)
37{
Eric Andersen72d8e442003-08-05 02:18:25 +000038 struct property *prop = prop_alloc(P_DEFAULT, sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +000039
Eric Andersen72d8e442003-08-05 02:18:25 +000040 prop->expr = expr_alloc_symbol(sym_lookup(def, 1));
Eric Andersenc9f20d92002-12-05 08:41:41 +000041}
42
43void sym_init(void)
44{
45 struct symbol *sym;
Eric Andersenc9f20d92002-12-05 08:41:41 +000046 char *p;
47 static bool inited = false;
48
49 if (inited)
50 return;
51 inited = true;
52
Eric Andersenc9f20d92002-12-05 08:41:41 +000053 sym = sym_lookup("VERSION", 0);
54 sym->type = S_STRING;
55 sym->flags |= SYMBOL_AUTO;
56 p = getenv("VERSION");
57 if (p)
58 sym_add_default(sym, p);
59
Eric Andersenc9f20d92002-12-05 08:41:41 +000060 sym = sym_lookup("TARGET_ARCH", 0);
61 sym->type = S_STRING;
62 sym->flags |= SYMBOL_AUTO;
63 p = getenv("TARGET_ARCH");
64 if (p)
65 sym_add_default(sym, p);
Eric Andersen72d8e442003-08-05 02:18:25 +000066
Eric Andersenc9f20d92002-12-05 08:41:41 +000067}
68
Eric Andersen72d8e442003-08-05 02:18:25 +000069enum symbol_type sym_get_type(struct symbol *sym)
Eric Andersenc9f20d92002-12-05 08:41:41 +000070{
Eric Andersen72d8e442003-08-05 02:18:25 +000071 enum symbol_type type = sym->type;
72
Eric Andersenc9f20d92002-12-05 08:41:41 +000073 if (type == S_TRISTATE) {
74 if (sym_is_choice_value(sym) && sym->visible == yes)
75 type = S_BOOLEAN;
Eric Andersen837f0582004-07-15 06:01:05 +000076 else if (modules_val == no)
77 type = S_BOOLEAN;
Eric Andersenc9f20d92002-12-05 08:41:41 +000078 }
79 return type;
80}
81
Eric Andersen72d8e442003-08-05 02:18:25 +000082const char *sym_type_name(enum symbol_type type)
Eric Andersenc9f20d92002-12-05 08:41:41 +000083{
84 switch (type) {
85 case S_BOOLEAN:
86 return "boolean";
87 case S_TRISTATE:
88 return "tristate";
89 case S_INT:
90 return "integer";
91 case S_HEX:
92 return "hex";
93 case S_STRING:
94 return "string";
95 case S_UNKNOWN:
96 return "unknown";
Eric Andersen72d8e442003-08-05 02:18:25 +000097 case S_OTHER:
98 break;
Eric Andersenc9f20d92002-12-05 08:41:41 +000099 }
100 return "???";
101}
102
103struct property *sym_get_choice_prop(struct symbol *sym)
104{
105 struct property *prop;
106
107 for_all_choices(sym, prop)
108 return prop;
109 return NULL;
110}
111
112struct property *sym_get_default_prop(struct symbol *sym)
113{
114 struct property *prop;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000115
116 for_all_defaults(sym, prop) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000117 prop->visible.tri = expr_calc_value(prop->visible.expr);
118 if (prop->visible.tri != no)
Eric Andersenc9f20d92002-12-05 08:41:41 +0000119 return prop;
120 }
121 return NULL;
122}
123
Eric Andersen72d8e442003-08-05 02:18:25 +0000124struct property *sym_get_range_prop(struct symbol *sym)
Eric Andersenc9f20d92002-12-05 08:41:41 +0000125{
126 struct property *prop;
Eric Andersen72d8e442003-08-05 02:18:25 +0000127
128 for_all_properties(sym, prop, P_RANGE) {
129 prop->visible.tri = expr_calc_value(prop->visible.expr);
130 if (prop->visible.tri != no)
131 return prop;
132 }
133 return NULL;
134}
135
136static void sym_calc_visibility(struct symbol *sym)
137{
138 struct property *prop;
139 tristate tri;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000140
141 /* any prompt visible? */
Eric Andersen72d8e442003-08-05 02:18:25 +0000142 tri = no;
143 for_all_prompts(sym, prop) {
144 prop->visible.tri = expr_calc_value(prop->visible.expr);
145 tri = E_OR(tri, prop->visible.tri);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000146 }
Eric Andersen837f0582004-07-15 06:01:05 +0000147 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
148 tri = yes;
Eric Andersen72d8e442003-08-05 02:18:25 +0000149 if (sym->visible != tri) {
150 sym->visible = tri;
151 sym_set_changed(sym);
152 }
153 if (sym_is_choice_value(sym))
154 return;
155 tri = no;
156 if (sym->rev_dep.expr)
157 tri = expr_calc_value(sym->rev_dep.expr);
Eric Andersen837f0582004-07-15 06:01:05 +0000158 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
159 tri = yes;
Eric Andersen72d8e442003-08-05 02:18:25 +0000160 if (sym->rev_dep.tri != tri) {
161 sym->rev_dep.tri = tri;
162 sym_set_changed(sym);
163 }
164}
165
166static struct symbol *sym_calc_choice(struct symbol *sym)
167{
168 struct symbol *def_sym;
169 struct property *prop;
170 struct expr *e;
171
172 /* is the user choice visible? */
173 def_sym = sym->user.val;
174 if (def_sym) {
175 sym_calc_visibility(def_sym);
176 if (def_sym->visible != no)
177 return def_sym;
178 }
179
180 /* any of the defaults visible? */
181 for_all_defaults(sym, prop) {
182 prop->visible.tri = expr_calc_value(prop->visible.expr);
183 if (prop->visible.tri == no)
184 continue;
185 def_sym = prop_get_symbol(prop);
186 sym_calc_visibility(def_sym);
187 if (def_sym->visible != no)
188 return def_sym;
189 }
190
191 /* just get the first visible value */
192 prop = sym_get_choice_prop(sym);
193 for (e = prop->expr; e; e = e->left.expr) {
194 def_sym = e->right.sym;
195 sym_calc_visibility(def_sym);
196 if (def_sym->visible != no)
197 return def_sym;
198 }
199
200 /* no choice? reset tristate value */
201 sym->curr.tri = no;
202 return NULL;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000203}
204
205void sym_calc_value(struct symbol *sym)
206{
207 struct symbol_value newval, oldval;
Eric Andersen72d8e442003-08-05 02:18:25 +0000208 struct property *prop;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000209 struct expr *e;
210
Eric Andersen72d8e442003-08-05 02:18:25 +0000211 if (!sym)
212 return;
213
Eric Andersenc9f20d92002-12-05 08:41:41 +0000214 if (sym->flags & SYMBOL_VALID)
215 return;
Eric Andersen72d8e442003-08-05 02:18:25 +0000216 sym->flags |= SYMBOL_VALID;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000217
218 oldval = sym->curr;
219
220 switch (sym->type) {
221 case S_INT:
222 case S_HEX:
223 case S_STRING:
224 newval = symbol_empty.curr;
225 break;
226 case S_BOOLEAN:
227 case S_TRISTATE:
228 newval = symbol_no.curr;
229 break;
230 default:
Eric Andersen72d8e442003-08-05 02:18:25 +0000231 sym->curr.val = sym->name;
232 sym->curr.tri = no;
233 return;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000234 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000235 if (!sym_is_choice_value(sym))
236 sym->flags &= ~SYMBOL_WRITE;
237
238 sym_calc_visibility(sym);
239
240 /* set default if recursively called */
241 sym->curr = newval;
242
Eric Andersen72d8e442003-08-05 02:18:25 +0000243 switch (sym_get_type(sym)) {
244 case S_BOOLEAN:
245 case S_TRISTATE:
Eric Andersenc9f20d92002-12-05 08:41:41 +0000246 if (sym_is_choice_value(sym) && sym->visible == yes) {
247 prop = sym_get_choice_prop(sym);
Eric Andersen72d8e442003-08-05 02:18:25 +0000248 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
249 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
250 sym->flags |= SYMBOL_WRITE;
251 if (sym_has_value(sym))
252 newval.tri = sym->user.tri;
253 else if (!sym_is_choice(sym)) {
254 prop = sym_get_default_prop(sym);
255 if (prop)
256 newval.tri = expr_calc_value(prop->expr);
257 }
258 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
259 } else if (!sym_is_choice(sym)) {
260 prop = sym_get_default_prop(sym);
261 if (prop) {
262 sym->flags |= SYMBOL_WRITE;
263 newval.tri = expr_calc_value(prop->expr);
264 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000265 }
Eric Andersen837f0582004-07-15 06:01:05 +0000266 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
267 newval.tri = yes;
Eric Andersen72d8e442003-08-05 02:18:25 +0000268 break;
269 case S_STRING:
270 case S_HEX:
271 case S_INT:
272 if (sym->visible != no) {
273 sym->flags |= SYMBOL_WRITE;
274 if (sym_has_value(sym)) {
275 newval.val = sym->user.val;
276 break;
277 }
278 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000279 prop = sym_get_default_prop(sym);
280 if (prop) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000281 struct symbol *ds = prop_get_symbol(prop);
282 if (ds) {
283 sym->flags |= SYMBOL_WRITE;
284 sym_calc_value(ds);
285 newval.val = ds->curr.val;
286 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000287 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000288 break;
Eric Andersen72d8e442003-08-05 02:18:25 +0000289 default:
290 ;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000291 }
292
Eric Andersenc9f20d92002-12-05 08:41:41 +0000293 sym->curr = newval;
Eric Andersen72d8e442003-08-05 02:18:25 +0000294 if (sym_is_choice(sym) && newval.tri == yes)
295 sym->curr.val = sym_calc_choice(sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000296
Eric Andersen72d8e442003-08-05 02:18:25 +0000297 if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
298 sym_set_changed(sym);
Eric Andersen837f0582004-07-15 06:01:05 +0000299 if (modules_sym == sym)
300 modules_val = modules_sym->curr.tri;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000301
302 if (sym_is_choice(sym)) {
303 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
304 prop = sym_get_choice_prop(sym);
Eric Andersen72d8e442003-08-05 02:18:25 +0000305 for (e = prop->expr; e; e = e->left.expr) {
Eric Andersenc9f20d92002-12-05 08:41:41 +0000306 e->right.sym->flags |= flags;
Eric Andersen72d8e442003-08-05 02:18:25 +0000307 if (flags & SYMBOL_CHANGED)
308 sym_set_changed(e->right.sym);
309 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000310 }
311}
312
313void sym_clear_all_valid(void)
314{
315 struct symbol *sym;
316 int i;
317
318 for_all_symbols(i, sym)
319 sym->flags &= ~SYMBOL_VALID;
320 sym_change_count++;
Eric Andersen837f0582004-07-15 06:01:05 +0000321 if (modules_sym)
322 sym_calc_value(modules_sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000323}
324
Eric Andersen72d8e442003-08-05 02:18:25 +0000325void sym_set_changed(struct symbol *sym)
326{
327 struct property *prop;
328
329 sym->flags |= SYMBOL_CHANGED;
330 for (prop = sym->prop; prop; prop = prop->next) {
331 if (prop->menu)
332 prop->menu->flags |= MENU_CHANGED;
333 }
334}
335
Eric Andersenc9f20d92002-12-05 08:41:41 +0000336void sym_set_all_changed(void)
337{
338 struct symbol *sym;
339 int i;
340
341 for_all_symbols(i, sym)
Eric Andersen72d8e442003-08-05 02:18:25 +0000342 sym_set_changed(sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000343}
344
345bool sym_tristate_within_range(struct symbol *sym, tristate val)
346{
347 int type = sym_get_type(sym);
348
349 if (sym->visible == no)
350 return false;
351
352 if (type != S_BOOLEAN && type != S_TRISTATE)
353 return false;
354
Eric Andersen72d8e442003-08-05 02:18:25 +0000355 if (type == S_BOOLEAN && val == mod)
356 return false;
357 if (sym->visible <= sym->rev_dep.tri)
358 return false;
359 if (sym_is_choice_value(sym) && sym->visible == yes)
360 return val == yes;
361 return val >= sym->rev_dep.tri && val <= sym->visible;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000362}
363
364bool sym_set_tristate_value(struct symbol *sym, tristate val)
365{
366 tristate oldval = sym_get_tristate_value(sym);
367
368 if (oldval != val && !sym_tristate_within_range(sym, val))
369 return false;
370
371 if (sym->flags & SYMBOL_NEW) {
372 sym->flags &= ~SYMBOL_NEW;
Eric Andersen72d8e442003-08-05 02:18:25 +0000373 sym_set_changed(sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000374 }
375 if (sym_is_choice_value(sym) && val == yes) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000376 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Eric Andersenc9f20d92002-12-05 08:41:41 +0000377
Eric Andersen72d8e442003-08-05 02:18:25 +0000378 cs->user.val = sym;
379 cs->flags &= ~SYMBOL_NEW;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000380 }
381
Eric Andersen72d8e442003-08-05 02:18:25 +0000382 sym->user.tri = val;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000383 if (oldval != val) {
384 sym_clear_all_valid();
385 if (sym == modules_sym)
386 sym_set_all_changed();
387 }
388
389 return true;
390}
391
392tristate sym_toggle_tristate_value(struct symbol *sym)
393{
394 tristate oldval, newval;
395
396 oldval = newval = sym_get_tristate_value(sym);
397 do {
398 switch (newval) {
399 case no:
400 newval = mod;
401 break;
402 case mod:
403 newval = yes;
404 break;
405 case yes:
406 newval = no;
407 break;
408 }
409 if (sym_set_tristate_value(sym, newval))
410 break;
411 } while (oldval != newval);
412 return newval;
413}
414
415bool sym_string_valid(struct symbol *sym, const char *str)
416{
417 char ch;
418
419 switch (sym->type) {
420 case S_STRING:
421 return true;
422 case S_INT:
423 ch = *str++;
424 if (ch == '-')
425 ch = *str++;
Eric Andersen72d8e442003-08-05 02:18:25 +0000426 if (!isdigit(ch))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000427 return false;
428 if (ch == '0' && *str != 0)
429 return false;
430 while ((ch = *str++)) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000431 if (!isdigit(ch))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000432 return false;
433 }
434 return true;
435 case S_HEX:
436 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
437 str += 2;
438 ch = *str++;
439 do {
Eric Andersen72d8e442003-08-05 02:18:25 +0000440 if (!isxdigit(ch))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000441 return false;
442 } while ((ch = *str++));
443 return true;
444 case S_BOOLEAN:
445 case S_TRISTATE:
446 switch (str[0]) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000447 case 'y': case 'Y':
448 case 'm': case 'M':
449 case 'n': case 'N':
450 return true;
451 }
452 return false;
453 default:
454 return false;
455 }
456}
457
458bool sym_string_within_range(struct symbol *sym, const char *str)
459{
460 struct property *prop;
461 int val;
462
463 switch (sym->type) {
464 case S_STRING:
465 return sym_string_valid(sym, str);
466 case S_INT:
467 if (!sym_string_valid(sym, str))
468 return false;
469 prop = sym_get_range_prop(sym);
470 if (!prop)
471 return true;
472 val = strtol(str, NULL, 10);
473 return val >= strtol(prop->expr->left.sym->name, NULL, 10) &&
474 val <= strtol(prop->expr->right.sym->name, NULL, 10);
475 case S_HEX:
476 if (!sym_string_valid(sym, str))
477 return false;
478 prop = sym_get_range_prop(sym);
479 if (!prop)
480 return true;
481 val = strtol(str, NULL, 16);
482 return val >= strtol(prop->expr->left.sym->name, NULL, 16) &&
483 val <= strtol(prop->expr->right.sym->name, NULL, 16);
484 case S_BOOLEAN:
485 case S_TRISTATE:
486 switch (str[0]) {
487 case 'y': case 'Y':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000488 return sym_tristate_within_range(sym, yes);
Eric Andersen72d8e442003-08-05 02:18:25 +0000489 case 'm': case 'M':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000490 return sym_tristate_within_range(sym, mod);
Eric Andersen72d8e442003-08-05 02:18:25 +0000491 case 'n': case 'N':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000492 return sym_tristate_within_range(sym, no);
493 }
494 return false;
495 default:
496 return false;
497 }
498}
499
500bool sym_set_string_value(struct symbol *sym, const char *newval)
501{
502 const char *oldval;
503 char *val;
504 int size;
505
506 switch (sym->type) {
507 case S_BOOLEAN:
508 case S_TRISTATE:
509 switch (newval[0]) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000510 case 'y': case 'Y':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000511 return sym_set_tristate_value(sym, yes);
Eric Andersen72d8e442003-08-05 02:18:25 +0000512 case 'm': case 'M':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000513 return sym_set_tristate_value(sym, mod);
Eric Andersen72d8e442003-08-05 02:18:25 +0000514 case 'n': case 'N':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000515 return sym_set_tristate_value(sym, no);
516 }
517 return false;
518 default:
519 ;
520 }
521
Eric Andersen72d8e442003-08-05 02:18:25 +0000522 if (!sym_string_within_range(sym, newval))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000523 return false;
524
525 if (sym->flags & SYMBOL_NEW) {
526 sym->flags &= ~SYMBOL_NEW;
Eric Andersen72d8e442003-08-05 02:18:25 +0000527 sym_set_changed(sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000528 }
529
Eric Andersen72d8e442003-08-05 02:18:25 +0000530 oldval = sym->user.val;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000531 size = strlen(newval) + 1;
532 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
533 size += 2;
Eric Andersen72d8e442003-08-05 02:18:25 +0000534 sym->user.val = val = malloc(size);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000535 *val++ = '0';
536 *val++ = 'x';
537 } else if (!oldval || strcmp(oldval, newval))
Eric Andersen72d8e442003-08-05 02:18:25 +0000538 sym->user.val = val = malloc(size);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000539 else
540 return true;
541
542 strcpy(val, newval);
543 free((void *)oldval);
544 sym_clear_all_valid();
545
546 return true;
547}
548
549const char *sym_get_string_value(struct symbol *sym)
550{
551 tristate val;
552
553 switch (sym->type) {
554 case S_BOOLEAN:
555 case S_TRISTATE:
556 val = sym_get_tristate_value(sym);
557 switch (val) {
558 case no:
559 return "n";
560 case mod:
561 return "m";
562 case yes:
563 return "y";
564 }
565 break;
566 default:
567 ;
568 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000569 return (const char *)sym->curr.val;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000570}
571
572bool sym_is_changable(struct symbol *sym)
573{
Eric Andersen72d8e442003-08-05 02:18:25 +0000574 return sym->visible > sym->rev_dep.tri;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000575}
576
577struct symbol *sym_lookup(const char *name, int isconst)
578{
579 struct symbol *symbol;
580 const char *ptr;
581 char *new_name;
582 int hash = 0;
583
Eric Andersenc9f20d92002-12-05 08:41:41 +0000584 if (name) {
585 if (name[0] && !name[1]) {
586 switch (name[0]) {
587 case 'y': return &symbol_yes;
588 case 'm': return &symbol_mod;
589 case 'n': return &symbol_no;
590 }
591 }
592 for (ptr = name; *ptr; ptr++)
593 hash += *ptr;
594 hash &= 0xff;
595
596 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
597 if (!strcmp(symbol->name, name)) {
598 if ((isconst && symbol->flags & SYMBOL_CONST) ||
Eric Andersen72d8e442003-08-05 02:18:25 +0000599 (!isconst && !(symbol->flags & SYMBOL_CONST)))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000600 return symbol;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000601 }
602 }
603 new_name = strdup(name);
604 } else {
605 new_name = NULL;
606 hash = 256;
607 }
608
609 symbol = malloc(sizeof(*symbol));
610 memset(symbol, 0, sizeof(*symbol));
611 symbol->name = new_name;
612 symbol->type = S_UNKNOWN;
613 symbol->flags = SYMBOL_NEW;
614 if (isconst)
615 symbol->flags |= SYMBOL_CONST;
616
617 symbol->next = symbol_hash[hash];
618 symbol_hash[hash] = symbol;
619
Eric Andersenc9f20d92002-12-05 08:41:41 +0000620 return symbol;
621}
622
623struct symbol *sym_find(const char *name)
624{
625 struct symbol *symbol = NULL;
626 const char *ptr;
627 int hash = 0;
628
629 if (!name)
630 return NULL;
631
632 if (name[0] && !name[1]) {
633 switch (name[0]) {
634 case 'y': return &symbol_yes;
635 case 'm': return &symbol_mod;
636 case 'n': return &symbol_no;
637 }
638 }
639 for (ptr = name; *ptr; ptr++)
640 hash += *ptr;
641 hash &= 0xff;
642
643 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
644 if (!strcmp(symbol->name, name) &&
645 !(symbol->flags & SYMBOL_CONST))
646 break;
647 }
648
649 return symbol;
650}
651
Eric Andersen72d8e442003-08-05 02:18:25 +0000652struct symbol *sym_check_deps(struct symbol *sym);
653
654static struct symbol *sym_check_expr_deps(struct expr *e)
655{
656 struct symbol *sym;
657
658 if (!e)
659 return NULL;
660 switch (e->type) {
661 case E_OR:
662 case E_AND:
663 sym = sym_check_expr_deps(e->left.expr);
664 if (sym)
665 return sym;
666 return sym_check_expr_deps(e->right.expr);
667 case E_NOT:
668 return sym_check_expr_deps(e->left.expr);
669 case E_EQUAL:
670 case E_UNEQUAL:
671 sym = sym_check_deps(e->left.sym);
672 if (sym)
673 return sym;
674 return sym_check_deps(e->right.sym);
675 case E_SYMBOL:
676 return sym_check_deps(e->left.sym);
677 default:
678 break;
679 }
680 printf("Oops! How to check %d?\n", e->type);
681 return NULL;
682}
683
684struct symbol *sym_check_deps(struct symbol *sym)
685{
686 struct symbol *sym2;
687 struct property *prop;
688
689 if (sym->flags & SYMBOL_CHECK_DONE)
690 return NULL;
691 if (sym->flags & SYMBOL_CHECK) {
692 printf("Warning! Found recursive dependency: %s", sym->name);
693 return sym;
694 }
695
696 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
697 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
698 if (sym2)
699 goto out;
700
701 for (prop = sym->prop; prop; prop = prop->next) {
Eric Andersen837f0582004-07-15 06:01:05 +0000702 if (prop->type == P_CHOICE || prop->type == P_SELECT)
Eric Andersen72d8e442003-08-05 02:18:25 +0000703 continue;
704 sym2 = sym_check_expr_deps(prop->visible.expr);
705 if (sym2)
706 goto out;
707 if (prop->type != P_DEFAULT || sym_is_choice(sym))
708 continue;
709 sym2 = sym_check_expr_deps(prop->expr);
710 if (sym2)
711 goto out;
712 }
713out:
714 if (sym2)
715 printf(" %s", sym->name);
716 sym->flags &= ~SYMBOL_CHECK;
717 return sym2;
718}
719
720struct property *prop_alloc(enum prop_type type, struct symbol *sym)
721{
722 struct property *prop;
723 struct property **propp;
724
725 prop = malloc(sizeof(*prop));
726 memset(prop, 0, sizeof(*prop));
727 prop->type = type;
728 prop->sym = sym;
729 prop->file = current_file;
730 prop->lineno = zconf_lineno();
731
732 /* append property to the prop list of symbol */
733 if (sym) {
734 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
735 ;
736 *propp = prop;
737 }
738
739 return prop;
740}
741
742struct symbol *prop_get_symbol(struct property *prop)
743{
744 if (prop->expr && (prop->expr->type == E_SYMBOL ||
745 prop->expr->type == E_CHOICE))
746 return prop->expr->left.sym;
747 return NULL;
748}
749
Eric Andersenc9f20d92002-12-05 08:41:41 +0000750const char *prop_get_type_name(enum prop_type type)
751{
752 switch (type) {
753 case P_PROMPT:
754 return "prompt";
755 case P_COMMENT:
756 return "comment";
757 case P_MENU:
758 return "menu";
Eric Andersenc9f20d92002-12-05 08:41:41 +0000759 case P_DEFAULT:
760 return "default";
761 case P_CHOICE:
762 return "choice";
Eric Andersen72d8e442003-08-05 02:18:25 +0000763 case P_SELECT:
764 return "select";
765 case P_RANGE:
766 return "range";
767 case P_UNKNOWN:
768 break;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000769 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000770 return "unknown";
Eric Andersenc9f20d92002-12-05 08:41:41 +0000771}