blob: ea629728ac1713ee4f59744a66bbee9c8dc84444 [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>
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +00009#include <regex.h>
Eric Andersenc9f20d92002-12-05 08:41:41 +000010#include <sys/utsname.h>
11
12#define LKC_DIRECT_LINK
13#include "lkc.h"
14
15struct symbol symbol_yes = {
Eric Andersen837f0582004-07-15 06:01:05 +000016 .name = "y",
17 .curr = { "y", yes },
18 .flags = SYMBOL_YES|SYMBOL_VALID,
Eric Andersenc9f20d92002-12-05 08:41:41 +000019}, symbol_mod = {
Eric Andersen837f0582004-07-15 06:01:05 +000020 .name = "m",
21 .curr = { "m", mod },
22 .flags = SYMBOL_MOD|SYMBOL_VALID,
Eric Andersenc9f20d92002-12-05 08:41:41 +000023}, symbol_no = {
Eric Andersen837f0582004-07-15 06:01:05 +000024 .name = "n",
25 .curr = { "n", no },
26 .flags = SYMBOL_NO|SYMBOL_VALID,
Eric Andersenc9f20d92002-12-05 08:41:41 +000027}, symbol_empty = {
Eric Andersen837f0582004-07-15 06:01:05 +000028 .name = "",
29 .curr = { "", no },
30 .flags = SYMBOL_VALID,
Eric Andersenc9f20d92002-12-05 08:41:41 +000031};
32
33int sym_change_count;
34struct symbol *modules_sym;
Eric Andersen837f0582004-07-15 06:01:05 +000035tristate modules_val;
Eric Andersenc9f20d92002-12-05 08:41:41 +000036
37void sym_add_default(struct symbol *sym, const char *def)
38{
Eric Andersen72d8e442003-08-05 02:18:25 +000039 struct property *prop = prop_alloc(P_DEFAULT, sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +000040
Eric Andersen72d8e442003-08-05 02:18:25 +000041 prop->expr = expr_alloc_symbol(sym_lookup(def, 1));
Eric Andersenc9f20d92002-12-05 08:41:41 +000042}
43
44void sym_init(void)
45{
46 struct symbol *sym;
Eric Andersenc9f20d92002-12-05 08:41:41 +000047 char *p;
48 static bool inited = false;
49
50 if (inited)
51 return;
52 inited = true;
53
Eric Andersenc9f20d92002-12-05 08:41:41 +000054 sym = sym_lookup("VERSION", 0);
55 sym->type = S_STRING;
56 sym->flags |= SYMBOL_AUTO;
57 p = getenv("VERSION");
58 if (p)
59 sym_add_default(sym, p);
60
Eric Andersenc9f20d92002-12-05 08:41:41 +000061 sym = sym_lookup("TARGET_ARCH", 0);
62 sym->type = S_STRING;
63 sym->flags |= SYMBOL_AUTO;
64 p = getenv("TARGET_ARCH");
65 if (p)
66 sym_add_default(sym, p);
Eric Andersen72d8e442003-08-05 02:18:25 +000067
Eric Andersenc9f20d92002-12-05 08:41:41 +000068}
69
Eric Andersen72d8e442003-08-05 02:18:25 +000070enum symbol_type sym_get_type(struct symbol *sym)
Eric Andersenc9f20d92002-12-05 08:41:41 +000071{
Eric Andersen72d8e442003-08-05 02:18:25 +000072 enum symbol_type type = sym->type;
73
Eric Andersenc9f20d92002-12-05 08:41:41 +000074 if (type == S_TRISTATE) {
75 if (sym_is_choice_value(sym) && sym->visible == yes)
76 type = S_BOOLEAN;
Eric Andersen837f0582004-07-15 06:01:05 +000077 else if (modules_val == no)
78 type = S_BOOLEAN;
Eric Andersenc9f20d92002-12-05 08:41:41 +000079 }
80 return type;
81}
82
Eric Andersen72d8e442003-08-05 02:18:25 +000083const char *sym_type_name(enum symbol_type type)
Eric Andersenc9f20d92002-12-05 08:41:41 +000084{
85 switch (type) {
86 case S_BOOLEAN:
87 return "boolean";
88 case S_TRISTATE:
89 return "tristate";
90 case S_INT:
91 return "integer";
92 case S_HEX:
93 return "hex";
94 case S_STRING:
95 return "string";
96 case S_UNKNOWN:
97 return "unknown";
Eric Andersen72d8e442003-08-05 02:18:25 +000098 case S_OTHER:
99 break;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000100 }
101 return "???";
102}
103
104struct property *sym_get_choice_prop(struct symbol *sym)
105{
106 struct property *prop;
107
108 for_all_choices(sym, prop)
109 return prop;
110 return NULL;
111}
112
113struct property *sym_get_default_prop(struct symbol *sym)
114{
115 struct property *prop;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000116
117 for_all_defaults(sym, prop) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000118 prop->visible.tri = expr_calc_value(prop->visible.expr);
119 if (prop->visible.tri != no)
Eric Andersenc9f20d92002-12-05 08:41:41 +0000120 return prop;
121 }
122 return NULL;
123}
124
Eric Andersen72d8e442003-08-05 02:18:25 +0000125struct property *sym_get_range_prop(struct symbol *sym)
Eric Andersenc9f20d92002-12-05 08:41:41 +0000126{
127 struct property *prop;
Eric Andersen72d8e442003-08-05 02:18:25 +0000128
129 for_all_properties(sym, prop, P_RANGE) {
130 prop->visible.tri = expr_calc_value(prop->visible.expr);
131 if (prop->visible.tri != no)
132 return prop;
133 }
134 return NULL;
135}
136
137static void sym_calc_visibility(struct symbol *sym)
138{
139 struct property *prop;
140 tristate tri;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000141
142 /* any prompt visible? */
Eric Andersen72d8e442003-08-05 02:18:25 +0000143 tri = no;
144 for_all_prompts(sym, prop) {
145 prop->visible.tri = expr_calc_value(prop->visible.expr);
146 tri = E_OR(tri, prop->visible.tri);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000147 }
Eric Andersen837f0582004-07-15 06:01:05 +0000148 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
149 tri = yes;
Eric Andersen72d8e442003-08-05 02:18:25 +0000150 if (sym->visible != tri) {
151 sym->visible = tri;
152 sym_set_changed(sym);
153 }
154 if (sym_is_choice_value(sym))
155 return;
156 tri = no;
157 if (sym->rev_dep.expr)
158 tri = expr_calc_value(sym->rev_dep.expr);
Eric Andersen837f0582004-07-15 06:01:05 +0000159 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
160 tri = yes;
Eric Andersen72d8e442003-08-05 02:18:25 +0000161 if (sym->rev_dep.tri != tri) {
162 sym->rev_dep.tri = tri;
163 sym_set_changed(sym);
164 }
165}
166
167static struct symbol *sym_calc_choice(struct symbol *sym)
168{
169 struct symbol *def_sym;
170 struct property *prop;
171 struct expr *e;
172
173 /* is the user choice visible? */
174 def_sym = sym->user.val;
175 if (def_sym) {
176 sym_calc_visibility(def_sym);
177 if (def_sym->visible != no)
178 return def_sym;
179 }
180
181 /* any of the defaults visible? */
182 for_all_defaults(sym, prop) {
183 prop->visible.tri = expr_calc_value(prop->visible.expr);
184 if (prop->visible.tri == no)
185 continue;
186 def_sym = prop_get_symbol(prop);
187 sym_calc_visibility(def_sym);
188 if (def_sym->visible != no)
189 return def_sym;
190 }
191
192 /* just get the first visible value */
193 prop = sym_get_choice_prop(sym);
194 for (e = prop->expr; e; e = e->left.expr) {
195 def_sym = e->right.sym;
196 sym_calc_visibility(def_sym);
197 if (def_sym->visible != no)
198 return def_sym;
199 }
200
201 /* no choice? reset tristate value */
202 sym->curr.tri = no;
203 return NULL;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000204}
205
206void sym_calc_value(struct symbol *sym)
207{
208 struct symbol_value newval, oldval;
Eric Andersen72d8e442003-08-05 02:18:25 +0000209 struct property *prop;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000210 struct expr *e;
211
Eric Andersen72d8e442003-08-05 02:18:25 +0000212 if (!sym)
213 return;
214
Eric Andersenc9f20d92002-12-05 08:41:41 +0000215 if (sym->flags & SYMBOL_VALID)
216 return;
Eric Andersen72d8e442003-08-05 02:18:25 +0000217 sym->flags |= SYMBOL_VALID;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000218
219 oldval = sym->curr;
220
221 switch (sym->type) {
222 case S_INT:
223 case S_HEX:
224 case S_STRING:
225 newval = symbol_empty.curr;
226 break;
227 case S_BOOLEAN:
228 case S_TRISTATE:
229 newval = symbol_no.curr;
230 break;
231 default:
Eric Andersen72d8e442003-08-05 02:18:25 +0000232 sym->curr.val = sym->name;
233 sym->curr.tri = no;
234 return;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000235 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000236 if (!sym_is_choice_value(sym))
237 sym->flags &= ~SYMBOL_WRITE;
238
239 sym_calc_visibility(sym);
240
241 /* set default if recursively called */
242 sym->curr = newval;
243
Eric Andersen72d8e442003-08-05 02:18:25 +0000244 switch (sym_get_type(sym)) {
245 case S_BOOLEAN:
246 case S_TRISTATE:
Eric Andersenc9f20d92002-12-05 08:41:41 +0000247 if (sym_is_choice_value(sym) && sym->visible == yes) {
248 prop = sym_get_choice_prop(sym);
Eric Andersen72d8e442003-08-05 02:18:25 +0000249 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
250 } else if (E_OR(sym->visible, sym->rev_dep.tri) != no) {
251 sym->flags |= SYMBOL_WRITE;
252 if (sym_has_value(sym))
253 newval.tri = sym->user.tri;
254 else if (!sym_is_choice(sym)) {
255 prop = sym_get_default_prop(sym);
256 if (prop)
257 newval.tri = expr_calc_value(prop->expr);
258 }
259 newval.tri = E_OR(E_AND(newval.tri, sym->visible), sym->rev_dep.tri);
260 } else if (!sym_is_choice(sym)) {
261 prop = sym_get_default_prop(sym);
262 if (prop) {
263 sym->flags |= SYMBOL_WRITE;
264 newval.tri = expr_calc_value(prop->expr);
265 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000266 }
Eric Andersen837f0582004-07-15 06:01:05 +0000267 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
268 newval.tri = yes;
Eric Andersen72d8e442003-08-05 02:18:25 +0000269 break;
270 case S_STRING:
271 case S_HEX:
272 case S_INT:
273 if (sym->visible != no) {
274 sym->flags |= SYMBOL_WRITE;
275 if (sym_has_value(sym)) {
276 newval.val = sym->user.val;
277 break;
278 }
279 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000280 prop = sym_get_default_prop(sym);
281 if (prop) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000282 struct symbol *ds = prop_get_symbol(prop);
283 if (ds) {
284 sym->flags |= SYMBOL_WRITE;
285 sym_calc_value(ds);
286 newval.val = ds->curr.val;
287 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000288 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000289 break;
Eric Andersen72d8e442003-08-05 02:18:25 +0000290 default:
291 ;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000292 }
293
Eric Andersenc9f20d92002-12-05 08:41:41 +0000294 sym->curr = newval;
Eric Andersen72d8e442003-08-05 02:18:25 +0000295 if (sym_is_choice(sym) && newval.tri == yes)
296 sym->curr.val = sym_calc_choice(sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000297
Eric Andersen72d8e442003-08-05 02:18:25 +0000298 if (memcmp(&oldval, &sym->curr, sizeof(oldval)))
299 sym_set_changed(sym);
Eric Andersen837f0582004-07-15 06:01:05 +0000300 if (modules_sym == sym)
301 modules_val = modules_sym->curr.tri;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000302
303 if (sym_is_choice(sym)) {
304 int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE);
305 prop = sym_get_choice_prop(sym);
Eric Andersen72d8e442003-08-05 02:18:25 +0000306 for (e = prop->expr; e; e = e->left.expr) {
Eric Andersenc9f20d92002-12-05 08:41:41 +0000307 e->right.sym->flags |= flags;
Eric Andersen72d8e442003-08-05 02:18:25 +0000308 if (flags & SYMBOL_CHANGED)
309 sym_set_changed(e->right.sym);
310 }
Eric Andersenc9f20d92002-12-05 08:41:41 +0000311 }
312}
313
314void sym_clear_all_valid(void)
315{
316 struct symbol *sym;
317 int i;
318
319 for_all_symbols(i, sym)
320 sym->flags &= ~SYMBOL_VALID;
321 sym_change_count++;
Eric Andersen837f0582004-07-15 06:01:05 +0000322 if (modules_sym)
323 sym_calc_value(modules_sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000324}
325
Eric Andersen72d8e442003-08-05 02:18:25 +0000326void sym_set_changed(struct symbol *sym)
327{
328 struct property *prop;
329
330 sym->flags |= SYMBOL_CHANGED;
331 for (prop = sym->prop; prop; prop = prop->next) {
332 if (prop->menu)
333 prop->menu->flags |= MENU_CHANGED;
334 }
335}
336
Eric Andersenc9f20d92002-12-05 08:41:41 +0000337void sym_set_all_changed(void)
338{
339 struct symbol *sym;
340 int i;
341
342 for_all_symbols(i, sym)
Eric Andersen72d8e442003-08-05 02:18:25 +0000343 sym_set_changed(sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000344}
345
346bool sym_tristate_within_range(struct symbol *sym, tristate val)
347{
348 int type = sym_get_type(sym);
349
350 if (sym->visible == no)
351 return false;
352
353 if (type != S_BOOLEAN && type != S_TRISTATE)
354 return false;
355
Eric Andersen72d8e442003-08-05 02:18:25 +0000356 if (type == S_BOOLEAN && val == mod)
357 return false;
358 if (sym->visible <= sym->rev_dep.tri)
359 return false;
360 if (sym_is_choice_value(sym) && sym->visible == yes)
361 return val == yes;
362 return val >= sym->rev_dep.tri && val <= sym->visible;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000363}
364
365bool sym_set_tristate_value(struct symbol *sym, tristate val)
366{
367 tristate oldval = sym_get_tristate_value(sym);
368
369 if (oldval != val && !sym_tristate_within_range(sym, val))
370 return false;
371
372 if (sym->flags & SYMBOL_NEW) {
373 sym->flags &= ~SYMBOL_NEW;
Eric Andersen72d8e442003-08-05 02:18:25 +0000374 sym_set_changed(sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000375 }
376 if (sym_is_choice_value(sym) && val == yes) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000377 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Eric Andersenc9f20d92002-12-05 08:41:41 +0000378
Eric Andersen72d8e442003-08-05 02:18:25 +0000379 cs->user.val = sym;
380 cs->flags &= ~SYMBOL_NEW;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000381 }
382
Eric Andersen72d8e442003-08-05 02:18:25 +0000383 sym->user.tri = val;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000384 if (oldval != val) {
385 sym_clear_all_valid();
386 if (sym == modules_sym)
387 sym_set_all_changed();
388 }
389
390 return true;
391}
392
393tristate sym_toggle_tristate_value(struct symbol *sym)
394{
395 tristate oldval, newval;
396
397 oldval = newval = sym_get_tristate_value(sym);
398 do {
399 switch (newval) {
400 case no:
401 newval = mod;
402 break;
403 case mod:
404 newval = yes;
405 break;
406 case yes:
407 newval = no;
408 break;
409 }
410 if (sym_set_tristate_value(sym, newval))
411 break;
412 } while (oldval != newval);
413 return newval;
414}
415
416bool sym_string_valid(struct symbol *sym, const char *str)
417{
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +0000418 signed char ch;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000419
420 switch (sym->type) {
421 case S_STRING:
422 return true;
423 case S_INT:
424 ch = *str++;
425 if (ch == '-')
426 ch = *str++;
Eric Andersen72d8e442003-08-05 02:18:25 +0000427 if (!isdigit(ch))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000428 return false;
429 if (ch == '0' && *str != 0)
430 return false;
431 while ((ch = *str++)) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000432 if (!isdigit(ch))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000433 return false;
434 }
435 return true;
436 case S_HEX:
437 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
438 str += 2;
439 ch = *str++;
440 do {
Eric Andersen72d8e442003-08-05 02:18:25 +0000441 if (!isxdigit(ch))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000442 return false;
443 } while ((ch = *str++));
444 return true;
445 case S_BOOLEAN:
446 case S_TRISTATE:
447 switch (str[0]) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000448 case 'y': case 'Y':
449 case 'm': case 'M':
450 case 'n': case 'N':
451 return true;
452 }
453 return false;
454 default:
455 return false;
456 }
457}
458
459bool sym_string_within_range(struct symbol *sym, const char *str)
460{
461 struct property *prop;
462 int val;
463
464 switch (sym->type) {
465 case S_STRING:
466 return sym_string_valid(sym, str);
467 case S_INT:
468 if (!sym_string_valid(sym, str))
469 return false;
470 prop = sym_get_range_prop(sym);
471 if (!prop)
472 return true;
473 val = strtol(str, NULL, 10);
474 return val >= strtol(prop->expr->left.sym->name, NULL, 10) &&
475 val <= strtol(prop->expr->right.sym->name, NULL, 10);
476 case S_HEX:
477 if (!sym_string_valid(sym, str))
478 return false;
479 prop = sym_get_range_prop(sym);
480 if (!prop)
481 return true;
482 val = strtol(str, NULL, 16);
483 return val >= strtol(prop->expr->left.sym->name, NULL, 16) &&
484 val <= strtol(prop->expr->right.sym->name, NULL, 16);
485 case S_BOOLEAN:
486 case S_TRISTATE:
487 switch (str[0]) {
488 case 'y': case 'Y':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000489 return sym_tristate_within_range(sym, yes);
Eric Andersen72d8e442003-08-05 02:18:25 +0000490 case 'm': case 'M':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000491 return sym_tristate_within_range(sym, mod);
Eric Andersen72d8e442003-08-05 02:18:25 +0000492 case 'n': case 'N':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000493 return sym_tristate_within_range(sym, no);
494 }
495 return false;
496 default:
497 return false;
498 }
499}
500
501bool sym_set_string_value(struct symbol *sym, const char *newval)
502{
503 const char *oldval;
504 char *val;
505 int size;
506
507 switch (sym->type) {
508 case S_BOOLEAN:
509 case S_TRISTATE:
510 switch (newval[0]) {
Eric Andersen72d8e442003-08-05 02:18:25 +0000511 case 'y': case 'Y':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000512 return sym_set_tristate_value(sym, yes);
Eric Andersen72d8e442003-08-05 02:18:25 +0000513 case 'm': case 'M':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000514 return sym_set_tristate_value(sym, mod);
Eric Andersen72d8e442003-08-05 02:18:25 +0000515 case 'n': case 'N':
Eric Andersenc9f20d92002-12-05 08:41:41 +0000516 return sym_set_tristate_value(sym, no);
517 }
518 return false;
519 default:
520 ;
521 }
522
Eric Andersen72d8e442003-08-05 02:18:25 +0000523 if (!sym_string_within_range(sym, newval))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000524 return false;
525
526 if (sym->flags & SYMBOL_NEW) {
527 sym->flags &= ~SYMBOL_NEW;
Eric Andersen72d8e442003-08-05 02:18:25 +0000528 sym_set_changed(sym);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000529 }
530
Eric Andersen72d8e442003-08-05 02:18:25 +0000531 oldval = sym->user.val;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000532 size = strlen(newval) + 1;
533 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
534 size += 2;
Eric Andersen72d8e442003-08-05 02:18:25 +0000535 sym->user.val = val = malloc(size);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000536 *val++ = '0';
537 *val++ = 'x';
538 } else if (!oldval || strcmp(oldval, newval))
Eric Andersen72d8e442003-08-05 02:18:25 +0000539 sym->user.val = val = malloc(size);
Eric Andersenc9f20d92002-12-05 08:41:41 +0000540 else
541 return true;
542
543 strcpy(val, newval);
544 free((void *)oldval);
545 sym_clear_all_valid();
546
547 return true;
548}
549
550const char *sym_get_string_value(struct symbol *sym)
551{
552 tristate val;
553
554 switch (sym->type) {
555 case S_BOOLEAN:
556 case S_TRISTATE:
557 val = sym_get_tristate_value(sym);
558 switch (val) {
559 case no:
560 return "n";
561 case mod:
562 return "m";
563 case yes:
564 return "y";
565 }
566 break;
567 default:
568 ;
569 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000570 return (const char *)sym->curr.val;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000571}
572
573bool sym_is_changable(struct symbol *sym)
574{
Eric Andersen72d8e442003-08-05 02:18:25 +0000575 return sym->visible > sym->rev_dep.tri;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000576}
577
578struct symbol *sym_lookup(const char *name, int isconst)
579{
580 struct symbol *symbol;
581 const char *ptr;
582 char *new_name;
583 int hash = 0;
584
Eric Andersenc9f20d92002-12-05 08:41:41 +0000585 if (name) {
586 if (name[0] && !name[1]) {
587 switch (name[0]) {
588 case 'y': return &symbol_yes;
589 case 'm': return &symbol_mod;
590 case 'n': return &symbol_no;
591 }
592 }
593 for (ptr = name; *ptr; ptr++)
594 hash += *ptr;
595 hash &= 0xff;
596
597 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
598 if (!strcmp(symbol->name, name)) {
599 if ((isconst && symbol->flags & SYMBOL_CONST) ||
Eric Andersen72d8e442003-08-05 02:18:25 +0000600 (!isconst && !(symbol->flags & SYMBOL_CONST)))
Eric Andersenc9f20d92002-12-05 08:41:41 +0000601 return symbol;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000602 }
603 }
604 new_name = strdup(name);
605 } else {
606 new_name = NULL;
607 hash = 256;
608 }
609
610 symbol = malloc(sizeof(*symbol));
611 memset(symbol, 0, sizeof(*symbol));
612 symbol->name = new_name;
613 symbol->type = S_UNKNOWN;
614 symbol->flags = SYMBOL_NEW;
615 if (isconst)
616 symbol->flags |= SYMBOL_CONST;
617
618 symbol->next = symbol_hash[hash];
619 symbol_hash[hash] = symbol;
620
Eric Andersenc9f20d92002-12-05 08:41:41 +0000621 return symbol;
622}
623
624struct symbol *sym_find(const char *name)
625{
626 struct symbol *symbol = NULL;
627 const char *ptr;
628 int hash = 0;
629
630 if (!name)
631 return NULL;
632
633 if (name[0] && !name[1]) {
634 switch (name[0]) {
635 case 'y': return &symbol_yes;
636 case 'm': return &symbol_mod;
637 case 'n': return &symbol_no;
638 }
639 }
640 for (ptr = name; *ptr; ptr++)
641 hash += *ptr;
642 hash &= 0xff;
643
644 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
645 if (!strcmp(symbol->name, name) &&
646 !(symbol->flags & SYMBOL_CONST))
647 break;
648 }
649
650 return symbol;
651}
652
Peter Kjellerstedtbae38db2005-04-19 09:55:06 +0000653struct symbol **sym_re_search(const char *pattern)
654{
655 struct symbol *sym, **sym_arr = NULL;
656 int i, cnt, size;
657 regex_t re;
658
659 cnt = size = 0;
660 /* Skip if empty */
661 if (strlen(pattern) == 0)
662 return NULL;
663 if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE))
664 return NULL;
665
666 for_all_symbols(i, sym) {
667 if (sym->flags & SYMBOL_CONST || !sym->name)
668 continue;
669 if (regexec(&re, sym->name, 0, NULL, 0))
670 continue;
671 if (cnt + 1 >= size) {
672 void *tmp = sym_arr;
673 size += 16;
674 sym_arr = realloc(sym_arr, size * sizeof(struct symbol *));
675 if (!sym_arr) {
676 free(tmp);
677 return NULL;
678 }
679 }
680 sym_arr[cnt++] = sym;
681 }
682 if (sym_arr)
683 sym_arr[cnt] = NULL;
684 regfree(&re);
685
686 return sym_arr;
687}
688
689
Eric Andersen72d8e442003-08-05 02:18:25 +0000690struct symbol *sym_check_deps(struct symbol *sym);
691
692static struct symbol *sym_check_expr_deps(struct expr *e)
693{
694 struct symbol *sym;
695
696 if (!e)
697 return NULL;
698 switch (e->type) {
699 case E_OR:
700 case E_AND:
701 sym = sym_check_expr_deps(e->left.expr);
702 if (sym)
703 return sym;
704 return sym_check_expr_deps(e->right.expr);
705 case E_NOT:
706 return sym_check_expr_deps(e->left.expr);
707 case E_EQUAL:
708 case E_UNEQUAL:
709 sym = sym_check_deps(e->left.sym);
710 if (sym)
711 return sym;
712 return sym_check_deps(e->right.sym);
713 case E_SYMBOL:
714 return sym_check_deps(e->left.sym);
715 default:
716 break;
717 }
718 printf("Oops! How to check %d?\n", e->type);
719 return NULL;
720}
721
722struct symbol *sym_check_deps(struct symbol *sym)
723{
724 struct symbol *sym2;
725 struct property *prop;
726
727 if (sym->flags & SYMBOL_CHECK_DONE)
728 return NULL;
729 if (sym->flags & SYMBOL_CHECK) {
730 printf("Warning! Found recursive dependency: %s", sym->name);
731 return sym;
732 }
733
734 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
735 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
736 if (sym2)
737 goto out;
738
739 for (prop = sym->prop; prop; prop = prop->next) {
Eric Andersen837f0582004-07-15 06:01:05 +0000740 if (prop->type == P_CHOICE || prop->type == P_SELECT)
Eric Andersen72d8e442003-08-05 02:18:25 +0000741 continue;
742 sym2 = sym_check_expr_deps(prop->visible.expr);
743 if (sym2)
744 goto out;
745 if (prop->type != P_DEFAULT || sym_is_choice(sym))
746 continue;
747 sym2 = sym_check_expr_deps(prop->expr);
748 if (sym2)
749 goto out;
750 }
751out:
752 if (sym2)
753 printf(" %s", sym->name);
754 sym->flags &= ~SYMBOL_CHECK;
755 return sym2;
756}
757
758struct property *prop_alloc(enum prop_type type, struct symbol *sym)
759{
760 struct property *prop;
761 struct property **propp;
762
763 prop = malloc(sizeof(*prop));
764 memset(prop, 0, sizeof(*prop));
765 prop->type = type;
766 prop->sym = sym;
767 prop->file = current_file;
768 prop->lineno = zconf_lineno();
769
770 /* append property to the prop list of symbol */
771 if (sym) {
772 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
773 ;
774 *propp = prop;
775 }
776
777 return prop;
778}
779
780struct symbol *prop_get_symbol(struct property *prop)
781{
782 if (prop->expr && (prop->expr->type == E_SYMBOL ||
783 prop->expr->type == E_CHOICE))
784 return prop->expr->left.sym;
785 return NULL;
786}
787
Eric Andersenc9f20d92002-12-05 08:41:41 +0000788const char *prop_get_type_name(enum prop_type type)
789{
790 switch (type) {
791 case P_PROMPT:
792 return "prompt";
793 case P_COMMENT:
794 return "comment";
795 case P_MENU:
796 return "menu";
Eric Andersenc9f20d92002-12-05 08:41:41 +0000797 case P_DEFAULT:
798 return "default";
799 case P_CHOICE:
800 return "choice";
Eric Andersen72d8e442003-08-05 02:18:25 +0000801 case P_SELECT:
802 return "select";
803 case P_RANGE:
804 return "range";
805 case P_UNKNOWN:
806 break;
Eric Andersenc9f20d92002-12-05 08:41:41 +0000807 }
Eric Andersen72d8e442003-08-05 02:18:25 +0000808 return "unknown";
Eric Andersenc9f20d92002-12-05 08:41:41 +0000809}