Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
15 | struct symbol symbol_yes = { | |
16 | .name = "y", | |
17 | .curr = { "y", yes }, | |
c0e150ac | 18 | .flags = SYMBOL_CONST|SYMBOL_VALID, |
1da177e4 LT |
19 | }, symbol_mod = { |
20 | .name = "m", | |
21 | .curr = { "m", mod }, | |
c0e150ac | 22 | .flags = SYMBOL_CONST|SYMBOL_VALID, |
1da177e4 LT |
23 | }, symbol_no = { |
24 | .name = "n", | |
25 | .curr = { "n", no }, | |
c0e150ac | 26 | .flags = SYMBOL_CONST|SYMBOL_VALID, |
1da177e4 LT |
27 | }, symbol_empty = { |
28 | .name = "", | |
29 | .curr = { "", no }, | |
30 | .flags = SYMBOL_VALID, | |
31 | }; | |
32 | ||
face4374 | 33 | struct symbol *sym_defconfig_list; |
1da177e4 LT |
34 | struct symbol *modules_sym; |
35 | tristate modules_val; | |
36 | ||
37 | void sym_add_default(struct symbol *sym, const char *def) | |
38 | { | |
39 | struct property *prop = prop_alloc(P_DEFAULT, sym); | |
40 | ||
41 | prop->expr = expr_alloc_symbol(sym_lookup(def, 1)); | |
42 | } | |
43 | ||
44 | void sym_init(void) | |
45 | { | |
46 | struct symbol *sym; | |
47 | struct utsname uts; | |
48 | char *p; | |
49 | static bool inited = false; | |
50 | ||
51 | if (inited) | |
52 | return; | |
53 | inited = true; | |
54 | ||
55 | uname(&uts); | |
56 | ||
57 | sym = sym_lookup("ARCH", 0); | |
58 | sym->type = S_STRING; | |
59 | sym->flags |= SYMBOL_AUTO; | |
60 | p = getenv("ARCH"); | |
61 | if (p) | |
62 | sym_add_default(sym, p); | |
63 | ||
2244cbd8 | 64 | sym = sym_lookup("KERNELVERSION", 0); |
1da177e4 LT |
65 | sym->type = S_STRING; |
66 | sym->flags |= SYMBOL_AUTO; | |
2244cbd8 | 67 | p = getenv("KERNELVERSION"); |
1da177e4 LT |
68 | if (p) |
69 | sym_add_default(sym, p); | |
70 | ||
71 | sym = sym_lookup("UNAME_RELEASE", 0); | |
72 | sym->type = S_STRING; | |
73 | sym->flags |= SYMBOL_AUTO; | |
74 | sym_add_default(sym, uts.release); | |
75 | } | |
76 | ||
77 | enum symbol_type sym_get_type(struct symbol *sym) | |
78 | { | |
79 | enum symbol_type type = sym->type; | |
80 | ||
81 | if (type == S_TRISTATE) { | |
82 | if (sym_is_choice_value(sym) && sym->visible == yes) | |
83 | type = S_BOOLEAN; | |
84 | else if (modules_val == no) | |
85 | type = S_BOOLEAN; | |
86 | } | |
87 | return type; | |
88 | } | |
89 | ||
90 | const char *sym_type_name(enum symbol_type type) | |
91 | { | |
92 | switch (type) { | |
93 | case S_BOOLEAN: | |
94 | return "boolean"; | |
95 | case S_TRISTATE: | |
96 | return "tristate"; | |
97 | case S_INT: | |
98 | return "integer"; | |
99 | case S_HEX: | |
100 | return "hex"; | |
101 | case S_STRING: | |
102 | return "string"; | |
103 | case S_UNKNOWN: | |
104 | return "unknown"; | |
105 | case S_OTHER: | |
106 | break; | |
107 | } | |
108 | return "???"; | |
109 | } | |
110 | ||
111 | struct property *sym_get_choice_prop(struct symbol *sym) | |
112 | { | |
113 | struct property *prop; | |
114 | ||
115 | for_all_choices(sym, prop) | |
116 | return prop; | |
117 | return NULL; | |
118 | } | |
119 | ||
120 | struct property *sym_get_default_prop(struct symbol *sym) | |
121 | { | |
122 | struct property *prop; | |
123 | ||
124 | for_all_defaults(sym, prop) { | |
125 | prop->visible.tri = expr_calc_value(prop->visible.expr); | |
126 | if (prop->visible.tri != no) | |
127 | return prop; | |
128 | } | |
129 | return NULL; | |
130 | } | |
131 | ||
132 | struct property *sym_get_range_prop(struct symbol *sym) | |
133 | { | |
134 | struct property *prop; | |
135 | ||
136 | for_all_properties(sym, prop, P_RANGE) { | |
137 | prop->visible.tri = expr_calc_value(prop->visible.expr); | |
138 | if (prop->visible.tri != no) | |
139 | return prop; | |
140 | } | |
141 | return NULL; | |
142 | } | |
143 | ||
4cf3cbe2 RZ |
144 | static int sym_get_range_val(struct symbol *sym, int base) |
145 | { | |
146 | sym_calc_value(sym); | |
147 | switch (sym->type) { | |
148 | case S_INT: | |
149 | base = 10; | |
150 | break; | |
151 | case S_HEX: | |
152 | base = 16; | |
153 | break; | |
154 | default: | |
155 | break; | |
156 | } | |
157 | return strtol(sym->curr.val, NULL, base); | |
158 | } | |
159 | ||
160 | static void sym_validate_range(struct symbol *sym) | |
161 | { | |
162 | struct property *prop; | |
163 | int base, val, val2; | |
164 | char str[64]; | |
165 | ||
166 | switch (sym->type) { | |
167 | case S_INT: | |
168 | base = 10; | |
169 | break; | |
170 | case S_HEX: | |
171 | base = 16; | |
172 | break; | |
173 | default: | |
174 | return; | |
175 | } | |
176 | prop = sym_get_range_prop(sym); | |
177 | if (!prop) | |
178 | return; | |
179 | val = strtol(sym->curr.val, NULL, base); | |
180 | val2 = sym_get_range_val(prop->expr->left.sym, base); | |
181 | if (val >= val2) { | |
182 | val2 = sym_get_range_val(prop->expr->right.sym, base); | |
183 | if (val <= val2) | |
184 | return; | |
185 | } | |
186 | if (sym->type == S_INT) | |
187 | sprintf(str, "%d", val2); | |
188 | else | |
189 | sprintf(str, "0x%x", val2); | |
190 | sym->curr.val = strdup(str); | |
191 | } | |
192 | ||
1da177e4 LT |
193 | static void sym_calc_visibility(struct symbol *sym) |
194 | { | |
195 | struct property *prop; | |
196 | tristate tri; | |
197 | ||
198 | /* any prompt visible? */ | |
199 | tri = no; | |
200 | for_all_prompts(sym, prop) { | |
201 | prop->visible.tri = expr_calc_value(prop->visible.expr); | |
d6ee3576 | 202 | tri = EXPR_OR(tri, prop->visible.tri); |
1da177e4 LT |
203 | } |
204 | if (tri == mod && (sym->type != S_TRISTATE || modules_val == no)) | |
205 | tri = yes; | |
206 | if (sym->visible != tri) { | |
207 | sym->visible = tri; | |
208 | sym_set_changed(sym); | |
209 | } | |
210 | if (sym_is_choice_value(sym)) | |
211 | return; | |
212 | tri = no; | |
213 | if (sym->rev_dep.expr) | |
214 | tri = expr_calc_value(sym->rev_dep.expr); | |
215 | if (tri == mod && sym_get_type(sym) == S_BOOLEAN) | |
216 | tri = yes; | |
217 | if (sym->rev_dep.tri != tri) { | |
218 | sym->rev_dep.tri = tri; | |
219 | sym_set_changed(sym); | |
220 | } | |
221 | } | |
222 | ||
223 | static struct symbol *sym_calc_choice(struct symbol *sym) | |
224 | { | |
225 | struct symbol *def_sym; | |
226 | struct property *prop; | |
227 | struct expr *e; | |
228 | ||
229 | /* is the user choice visible? */ | |
0c1822e6 | 230 | def_sym = sym->def[S_DEF_USER].val; |
1da177e4 LT |
231 | if (def_sym) { |
232 | sym_calc_visibility(def_sym); | |
233 | if (def_sym->visible != no) | |
234 | return def_sym; | |
235 | } | |
236 | ||
237 | /* any of the defaults visible? */ | |
238 | for_all_defaults(sym, prop) { | |
239 | prop->visible.tri = expr_calc_value(prop->visible.expr); | |
240 | if (prop->visible.tri == no) | |
241 | continue; | |
242 | def_sym = prop_get_symbol(prop); | |
243 | sym_calc_visibility(def_sym); | |
244 | if (def_sym->visible != no) | |
245 | return def_sym; | |
246 | } | |
247 | ||
248 | /* just get the first visible value */ | |
249 | prop = sym_get_choice_prop(sym); | |
7a962923 | 250 | expr_list_for_each_sym(prop->expr, e, def_sym) { |
1da177e4 LT |
251 | sym_calc_visibility(def_sym); |
252 | if (def_sym->visible != no) | |
253 | return def_sym; | |
254 | } | |
255 | ||
256 | /* no choice? reset tristate value */ | |
257 | sym->curr.tri = no; | |
258 | return NULL; | |
259 | } | |
260 | ||
261 | void sym_calc_value(struct symbol *sym) | |
262 | { | |
263 | struct symbol_value newval, oldval; | |
264 | struct property *prop; | |
265 | struct expr *e; | |
266 | ||
267 | if (!sym) | |
268 | return; | |
269 | ||
270 | if (sym->flags & SYMBOL_VALID) | |
271 | return; | |
272 | sym->flags |= SYMBOL_VALID; | |
273 | ||
274 | oldval = sym->curr; | |
275 | ||
276 | switch (sym->type) { | |
277 | case S_INT: | |
278 | case S_HEX: | |
279 | case S_STRING: | |
280 | newval = symbol_empty.curr; | |
281 | break; | |
282 | case S_BOOLEAN: | |
283 | case S_TRISTATE: | |
284 | newval = symbol_no.curr; | |
285 | break; | |
286 | default: | |
287 | sym->curr.val = sym->name; | |
288 | sym->curr.tri = no; | |
289 | return; | |
290 | } | |
291 | if (!sym_is_choice_value(sym)) | |
292 | sym->flags &= ~SYMBOL_WRITE; | |
293 | ||
294 | sym_calc_visibility(sym); | |
295 | ||
296 | /* set default if recursively called */ | |
297 | sym->curr = newval; | |
298 | ||
299 | switch (sym_get_type(sym)) { | |
300 | case S_BOOLEAN: | |
301 | case S_TRISTATE: | |
302 | if (sym_is_choice_value(sym) && sym->visible == yes) { | |
303 | prop = sym_get_choice_prop(sym); | |
304 | newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no; | |
d6ee3576 | 305 | } else if (EXPR_OR(sym->visible, sym->rev_dep.tri) != no) { |
1da177e4 LT |
306 | sym->flags |= SYMBOL_WRITE; |
307 | if (sym_has_value(sym)) | |
0c1822e6 | 308 | newval.tri = sym->def[S_DEF_USER].tri; |
1da177e4 LT |
309 | else if (!sym_is_choice(sym)) { |
310 | prop = sym_get_default_prop(sym); | |
311 | if (prop) | |
312 | newval.tri = expr_calc_value(prop->expr); | |
313 | } | |
d6ee3576 | 314 | newval.tri = EXPR_OR(EXPR_AND(newval.tri, sym->visible), sym->rev_dep.tri); |
1da177e4 LT |
315 | } else if (!sym_is_choice(sym)) { |
316 | prop = sym_get_default_prop(sym); | |
317 | if (prop) { | |
318 | sym->flags |= SYMBOL_WRITE; | |
319 | newval.tri = expr_calc_value(prop->expr); | |
320 | } | |
321 | } | |
322 | if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN) | |
323 | newval.tri = yes; | |
324 | break; | |
325 | case S_STRING: | |
326 | case S_HEX: | |
327 | case S_INT: | |
328 | if (sym->visible != no) { | |
329 | sym->flags |= SYMBOL_WRITE; | |
330 | if (sym_has_value(sym)) { | |
0c1822e6 | 331 | newval.val = sym->def[S_DEF_USER].val; |
1da177e4 LT |
332 | break; |
333 | } | |
334 | } | |
335 | prop = sym_get_default_prop(sym); | |
336 | if (prop) { | |
337 | struct symbol *ds = prop_get_symbol(prop); | |
338 | if (ds) { | |
339 | sym->flags |= SYMBOL_WRITE; | |
340 | sym_calc_value(ds); | |
341 | newval.val = ds->curr.val; | |
342 | } | |
343 | } | |
344 | break; | |
345 | default: | |
346 | ; | |
347 | } | |
348 | ||
349 | sym->curr = newval; | |
350 | if (sym_is_choice(sym) && newval.tri == yes) | |
351 | sym->curr.val = sym_calc_choice(sym); | |
4cf3cbe2 | 352 | sym_validate_range(sym); |
1da177e4 | 353 | |
face4374 | 354 | if (memcmp(&oldval, &sym->curr, sizeof(oldval))) { |
1da177e4 | 355 | sym_set_changed(sym); |
face4374 RZ |
356 | if (modules_sym == sym) { |
357 | sym_set_all_changed(); | |
358 | modules_val = modules_sym->curr.tri; | |
359 | } | |
360 | } | |
1da177e4 LT |
361 | |
362 | if (sym_is_choice(sym)) { | |
7a962923 | 363 | struct symbol *choice_sym; |
1da177e4 | 364 | int flags = sym->flags & (SYMBOL_CHANGED | SYMBOL_WRITE); |
7a962923 | 365 | |
1da177e4 | 366 | prop = sym_get_choice_prop(sym); |
7a962923 RZ |
367 | expr_list_for_each_sym(prop->expr, e, choice_sym) { |
368 | choice_sym->flags |= flags; | |
1da177e4 | 369 | if (flags & SYMBOL_CHANGED) |
7a962923 | 370 | sym_set_changed(choice_sym); |
1da177e4 LT |
371 | } |
372 | } | |
373 | } | |
374 | ||
375 | void sym_clear_all_valid(void) | |
376 | { | |
377 | struct symbol *sym; | |
378 | int i; | |
379 | ||
380 | for_all_symbols(i, sym) | |
381 | sym->flags &= ~SYMBOL_VALID; | |
bfc10001 | 382 | sym_add_change_count(1); |
1da177e4 LT |
383 | if (modules_sym) |
384 | sym_calc_value(modules_sym); | |
385 | } | |
386 | ||
387 | void sym_set_changed(struct symbol *sym) | |
388 | { | |
389 | struct property *prop; | |
390 | ||
391 | sym->flags |= SYMBOL_CHANGED; | |
392 | for (prop = sym->prop; prop; prop = prop->next) { | |
393 | if (prop->menu) | |
394 | prop->menu->flags |= MENU_CHANGED; | |
395 | } | |
396 | } | |
397 | ||
398 | void sym_set_all_changed(void) | |
399 | { | |
400 | struct symbol *sym; | |
401 | int i; | |
402 | ||
403 | for_all_symbols(i, sym) | |
404 | sym_set_changed(sym); | |
405 | } | |
406 | ||
407 | bool sym_tristate_within_range(struct symbol *sym, tristate val) | |
408 | { | |
409 | int type = sym_get_type(sym); | |
410 | ||
411 | if (sym->visible == no) | |
412 | return false; | |
413 | ||
414 | if (type != S_BOOLEAN && type != S_TRISTATE) | |
415 | return false; | |
416 | ||
417 | if (type == S_BOOLEAN && val == mod) | |
418 | return false; | |
419 | if (sym->visible <= sym->rev_dep.tri) | |
420 | return false; | |
421 | if (sym_is_choice_value(sym) && sym->visible == yes) | |
422 | return val == yes; | |
423 | return val >= sym->rev_dep.tri && val <= sym->visible; | |
424 | } | |
425 | ||
426 | bool sym_set_tristate_value(struct symbol *sym, tristate val) | |
427 | { | |
428 | tristate oldval = sym_get_tristate_value(sym); | |
429 | ||
430 | if (oldval != val && !sym_tristate_within_range(sym, val)) | |
431 | return false; | |
432 | ||
669bfad9 RZ |
433 | if (!(sym->flags & SYMBOL_DEF_USER)) { |
434 | sym->flags |= SYMBOL_DEF_USER; | |
1da177e4 LT |
435 | sym_set_changed(sym); |
436 | } | |
3f23ca2b RZ |
437 | /* |
438 | * setting a choice value also resets the new flag of the choice | |
439 | * symbol and all other choice values. | |
440 | */ | |
1da177e4 LT |
441 | if (sym_is_choice_value(sym) && val == yes) { |
442 | struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym)); | |
3f23ca2b RZ |
443 | struct property *prop; |
444 | struct expr *e; | |
1da177e4 | 445 | |
0c1822e6 | 446 | cs->def[S_DEF_USER].val = sym; |
669bfad9 | 447 | cs->flags |= SYMBOL_DEF_USER; |
3f23ca2b RZ |
448 | prop = sym_get_choice_prop(cs); |
449 | for (e = prop->expr; e; e = e->left.expr) { | |
450 | if (e->right.sym->visible != no) | |
669bfad9 | 451 | e->right.sym->flags |= SYMBOL_DEF_USER; |
3f23ca2b | 452 | } |
1da177e4 LT |
453 | } |
454 | ||
0c1822e6 | 455 | sym->def[S_DEF_USER].tri = val; |
face4374 | 456 | if (oldval != val) |
1da177e4 | 457 | sym_clear_all_valid(); |
1da177e4 LT |
458 | |
459 | return true; | |
460 | } | |
461 | ||
462 | tristate sym_toggle_tristate_value(struct symbol *sym) | |
463 | { | |
464 | tristate oldval, newval; | |
465 | ||
466 | oldval = newval = sym_get_tristate_value(sym); | |
467 | do { | |
468 | switch (newval) { | |
469 | case no: | |
470 | newval = mod; | |
471 | break; | |
472 | case mod: | |
473 | newval = yes; | |
474 | break; | |
475 | case yes: | |
476 | newval = no; | |
477 | break; | |
478 | } | |
479 | if (sym_set_tristate_value(sym, newval)) | |
480 | break; | |
481 | } while (oldval != newval); | |
482 | return newval; | |
483 | } | |
484 | ||
485 | bool sym_string_valid(struct symbol *sym, const char *str) | |
486 | { | |
487 | signed char ch; | |
488 | ||
489 | switch (sym->type) { | |
490 | case S_STRING: | |
491 | return true; | |
492 | case S_INT: | |
493 | ch = *str++; | |
494 | if (ch == '-') | |
495 | ch = *str++; | |
496 | if (!isdigit(ch)) | |
497 | return false; | |
498 | if (ch == '0' && *str != 0) | |
499 | return false; | |
500 | while ((ch = *str++)) { | |
501 | if (!isdigit(ch)) | |
502 | return false; | |
503 | } | |
504 | return true; | |
505 | case S_HEX: | |
506 | if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) | |
507 | str += 2; | |
508 | ch = *str++; | |
509 | do { | |
510 | if (!isxdigit(ch)) | |
511 | return false; | |
512 | } while ((ch = *str++)); | |
513 | return true; | |
514 | case S_BOOLEAN: | |
515 | case S_TRISTATE: | |
516 | switch (str[0]) { | |
517 | case 'y': case 'Y': | |
518 | case 'm': case 'M': | |
519 | case 'n': case 'N': | |
520 | return true; | |
521 | } | |
522 | return false; | |
523 | default: | |
524 | return false; | |
525 | } | |
526 | } | |
527 | ||
528 | bool sym_string_within_range(struct symbol *sym, const char *str) | |
529 | { | |
530 | struct property *prop; | |
531 | int val; | |
532 | ||
533 | switch (sym->type) { | |
534 | case S_STRING: | |
535 | return sym_string_valid(sym, str); | |
536 | case S_INT: | |
537 | if (!sym_string_valid(sym, str)) | |
538 | return false; | |
539 | prop = sym_get_range_prop(sym); | |
540 | if (!prop) | |
541 | return true; | |
542 | val = strtol(str, NULL, 10); | |
4cf3cbe2 RZ |
543 | return val >= sym_get_range_val(prop->expr->left.sym, 10) && |
544 | val <= sym_get_range_val(prop->expr->right.sym, 10); | |
1da177e4 LT |
545 | case S_HEX: |
546 | if (!sym_string_valid(sym, str)) | |
547 | return false; | |
548 | prop = sym_get_range_prop(sym); | |
549 | if (!prop) | |
550 | return true; | |
551 | val = strtol(str, NULL, 16); | |
4cf3cbe2 RZ |
552 | return val >= sym_get_range_val(prop->expr->left.sym, 16) && |
553 | val <= sym_get_range_val(prop->expr->right.sym, 16); | |
1da177e4 LT |
554 | case S_BOOLEAN: |
555 | case S_TRISTATE: | |
556 | switch (str[0]) { | |
557 | case 'y': case 'Y': | |
558 | return sym_tristate_within_range(sym, yes); | |
559 | case 'm': case 'M': | |
560 | return sym_tristate_within_range(sym, mod); | |
561 | case 'n': case 'N': | |
562 | return sym_tristate_within_range(sym, no); | |
563 | } | |
564 | return false; | |
565 | default: | |
566 | return false; | |
567 | } | |
568 | } | |
569 | ||
570 | bool sym_set_string_value(struct symbol *sym, const char *newval) | |
571 | { | |
572 | const char *oldval; | |
573 | char *val; | |
574 | int size; | |
575 | ||
576 | switch (sym->type) { | |
577 | case S_BOOLEAN: | |
578 | case S_TRISTATE: | |
579 | switch (newval[0]) { | |
580 | case 'y': case 'Y': | |
581 | return sym_set_tristate_value(sym, yes); | |
582 | case 'm': case 'M': | |
583 | return sym_set_tristate_value(sym, mod); | |
584 | case 'n': case 'N': | |
585 | return sym_set_tristate_value(sym, no); | |
586 | } | |
587 | return false; | |
588 | default: | |
589 | ; | |
590 | } | |
591 | ||
592 | if (!sym_string_within_range(sym, newval)) | |
593 | return false; | |
594 | ||
669bfad9 RZ |
595 | if (!(sym->flags & SYMBOL_DEF_USER)) { |
596 | sym->flags |= SYMBOL_DEF_USER; | |
1da177e4 LT |
597 | sym_set_changed(sym); |
598 | } | |
599 | ||
0c1822e6 | 600 | oldval = sym->def[S_DEF_USER].val; |
1da177e4 LT |
601 | size = strlen(newval) + 1; |
602 | if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) { | |
603 | size += 2; | |
0c1822e6 | 604 | sym->def[S_DEF_USER].val = val = malloc(size); |
1da177e4 LT |
605 | *val++ = '0'; |
606 | *val++ = 'x'; | |
607 | } else if (!oldval || strcmp(oldval, newval)) | |
0c1822e6 | 608 | sym->def[S_DEF_USER].val = val = malloc(size); |
1da177e4 LT |
609 | else |
610 | return true; | |
611 | ||
612 | strcpy(val, newval); | |
613 | free((void *)oldval); | |
614 | sym_clear_all_valid(); | |
615 | ||
616 | return true; | |
617 | } | |
618 | ||
619 | const char *sym_get_string_value(struct symbol *sym) | |
620 | { | |
621 | tristate val; | |
622 | ||
623 | switch (sym->type) { | |
624 | case S_BOOLEAN: | |
625 | case S_TRISTATE: | |
626 | val = sym_get_tristate_value(sym); | |
627 | switch (val) { | |
628 | case no: | |
629 | return "n"; | |
630 | case mod: | |
631 | return "m"; | |
632 | case yes: | |
633 | return "y"; | |
634 | } | |
635 | break; | |
636 | default: | |
637 | ; | |
638 | } | |
639 | return (const char *)sym->curr.val; | |
640 | } | |
641 | ||
642 | bool sym_is_changable(struct symbol *sym) | |
643 | { | |
644 | return sym->visible > sym->rev_dep.tri; | |
645 | } | |
646 | ||
647 | struct symbol *sym_lookup(const char *name, int isconst) | |
648 | { | |
649 | struct symbol *symbol; | |
650 | const char *ptr; | |
651 | char *new_name; | |
652 | int hash = 0; | |
653 | ||
654 | if (name) { | |
655 | if (name[0] && !name[1]) { | |
656 | switch (name[0]) { | |
657 | case 'y': return &symbol_yes; | |
658 | case 'm': return &symbol_mod; | |
659 | case 'n': return &symbol_no; | |
660 | } | |
661 | } | |
662 | for (ptr = name; *ptr; ptr++) | |
663 | hash += *ptr; | |
664 | hash &= 0xff; | |
665 | ||
666 | for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) { | |
667 | if (!strcmp(symbol->name, name)) { | |
668 | if ((isconst && symbol->flags & SYMBOL_CONST) || | |
669 | (!isconst && !(symbol->flags & SYMBOL_CONST))) | |
670 | return symbol; | |
671 | } | |
672 | } | |
673 | new_name = strdup(name); | |
674 | } else { | |
675 | new_name = NULL; | |
676 | hash = 256; | |
677 | } | |
678 | ||
679 | symbol = malloc(sizeof(*symbol)); | |
680 | memset(symbol, 0, sizeof(*symbol)); | |
681 | symbol->name = new_name; | |
682 | symbol->type = S_UNKNOWN; | |
1da177e4 LT |
683 | if (isconst) |
684 | symbol->flags |= SYMBOL_CONST; | |
685 | ||
686 | symbol->next = symbol_hash[hash]; | |
687 | symbol_hash[hash] = symbol; | |
688 | ||
689 | return symbol; | |
690 | } | |
691 | ||
692 | struct symbol *sym_find(const char *name) | |
693 | { | |
694 | struct symbol *symbol = NULL; | |
695 | const char *ptr; | |
696 | int hash = 0; | |
697 | ||
698 | if (!name) | |
699 | return NULL; | |
700 | ||
701 | if (name[0] && !name[1]) { | |
702 | switch (name[0]) { | |
703 | case 'y': return &symbol_yes; | |
704 | case 'm': return &symbol_mod; | |
705 | case 'n': return &symbol_no; | |
706 | } | |
707 | } | |
708 | for (ptr = name; *ptr; ptr++) | |
709 | hash += *ptr; | |
710 | hash &= 0xff; | |
711 | ||
712 | for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) { | |
713 | if (!strcmp(symbol->name, name) && | |
714 | !(symbol->flags & SYMBOL_CONST)) | |
715 | break; | |
716 | } | |
717 | ||
718 | return symbol; | |
719 | } | |
720 | ||
721 | struct symbol **sym_re_search(const char *pattern) | |
722 | { | |
723 | struct symbol *sym, **sym_arr = NULL; | |
724 | int i, cnt, size; | |
725 | regex_t re; | |
726 | ||
727 | cnt = size = 0; | |
728 | /* Skip if empty */ | |
729 | if (strlen(pattern) == 0) | |
730 | return NULL; | |
731 | if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB|REG_ICASE)) | |
732 | return NULL; | |
733 | ||
734 | for_all_symbols(i, sym) { | |
735 | if (sym->flags & SYMBOL_CONST || !sym->name) | |
736 | continue; | |
737 | if (regexec(&re, sym->name, 0, NULL, 0)) | |
738 | continue; | |
739 | if (cnt + 1 >= size) { | |
740 | void *tmp = sym_arr; | |
741 | size += 16; | |
742 | sym_arr = realloc(sym_arr, size * sizeof(struct symbol *)); | |
743 | if (!sym_arr) { | |
744 | free(tmp); | |
745 | return NULL; | |
746 | } | |
747 | } | |
748 | sym_arr[cnt++] = sym; | |
749 | } | |
750 | if (sym_arr) | |
751 | sym_arr[cnt] = NULL; | |
752 | regfree(&re); | |
753 | ||
754 | return sym_arr; | |
755 | } | |
756 | ||
757 | ||
758 | struct symbol *sym_check_deps(struct symbol *sym); | |
759 | ||
760 | static struct symbol *sym_check_expr_deps(struct expr *e) | |
761 | { | |
762 | struct symbol *sym; | |
763 | ||
764 | if (!e) | |
765 | return NULL; | |
766 | switch (e->type) { | |
767 | case E_OR: | |
768 | case E_AND: | |
769 | sym = sym_check_expr_deps(e->left.expr); | |
770 | if (sym) | |
771 | return sym; | |
772 | return sym_check_expr_deps(e->right.expr); | |
773 | case E_NOT: | |
774 | return sym_check_expr_deps(e->left.expr); | |
775 | case E_EQUAL: | |
776 | case E_UNEQUAL: | |
777 | sym = sym_check_deps(e->left.sym); | |
778 | if (sym) | |
779 | return sym; | |
780 | return sym_check_deps(e->right.sym); | |
781 | case E_SYMBOL: | |
782 | return sym_check_deps(e->left.sym); | |
783 | default: | |
784 | break; | |
785 | } | |
786 | printf("Oops! How to check %d?\n", e->type); | |
787 | return NULL; | |
788 | } | |
789 | ||
5447d34b | 790 | /* return NULL when dependencies are OK */ |
1da177e4 LT |
791 | struct symbol *sym_check_deps(struct symbol *sym) |
792 | { | |
793 | struct symbol *sym2; | |
794 | struct property *prop; | |
795 | ||
1da177e4 | 796 | if (sym->flags & SYMBOL_CHECK) { |
5447d34b SR |
797 | fprintf(stderr, "%s:%d:error: found recursive dependency: %s", |
798 | sym->prop->file->name, sym->prop->lineno, sym->name); | |
1da177e4 LT |
799 | return sym; |
800 | } | |
3f04e7dd DG |
801 | if (sym->flags & SYMBOL_CHECKED) |
802 | return NULL; | |
1da177e4 LT |
803 | |
804 | sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED); | |
805 | sym2 = sym_check_expr_deps(sym->rev_dep.expr); | |
806 | if (sym2) | |
807 | goto out; | |
808 | ||
809 | for (prop = sym->prop; prop; prop = prop->next) { | |
810 | if (prop->type == P_CHOICE || prop->type == P_SELECT) | |
811 | continue; | |
812 | sym2 = sym_check_expr_deps(prop->visible.expr); | |
813 | if (sym2) | |
814 | goto out; | |
815 | if (prop->type != P_DEFAULT || sym_is_choice(sym)) | |
816 | continue; | |
817 | sym2 = sym_check_expr_deps(prop->expr); | |
818 | if (sym2) | |
819 | goto out; | |
820 | } | |
821 | out: | |
5447d34b SR |
822 | if (sym2) |
823 | fprintf(stderr, " -> %s%s", sym->name, sym2 == sym? "\n": ""); | |
1da177e4 LT |
824 | sym->flags &= ~SYMBOL_CHECK; |
825 | return sym2; | |
826 | } | |
827 | ||
828 | struct property *prop_alloc(enum prop_type type, struct symbol *sym) | |
829 | { | |
830 | struct property *prop; | |
831 | struct property **propp; | |
832 | ||
833 | prop = malloc(sizeof(*prop)); | |
834 | memset(prop, 0, sizeof(*prop)); | |
835 | prop->type = type; | |
836 | prop->sym = sym; | |
837 | prop->file = current_file; | |
838 | prop->lineno = zconf_lineno(); | |
839 | ||
840 | /* append property to the prop list of symbol */ | |
841 | if (sym) { | |
842 | for (propp = &sym->prop; *propp; propp = &(*propp)->next) | |
843 | ; | |
844 | *propp = prop; | |
845 | } | |
846 | ||
847 | return prop; | |
848 | } | |
849 | ||
850 | struct symbol *prop_get_symbol(struct property *prop) | |
851 | { | |
852 | if (prop->expr && (prop->expr->type == E_SYMBOL || | |
7a962923 | 853 | prop->expr->type == E_LIST)) |
1da177e4 LT |
854 | return prop->expr->left.sym; |
855 | return NULL; | |
856 | } | |
857 | ||
858 | const char *prop_get_type_name(enum prop_type type) | |
859 | { | |
860 | switch (type) { | |
861 | case P_PROMPT: | |
862 | return "prompt"; | |
863 | case P_COMMENT: | |
864 | return "comment"; | |
865 | case P_MENU: | |
866 | return "menu"; | |
867 | case P_DEFAULT: | |
868 | return "default"; | |
869 | case P_CHOICE: | |
870 | return "choice"; | |
871 | case P_SELECT: | |
872 | return "select"; | |
873 | case P_RANGE: | |
874 | return "range"; | |
875 | case P_UNKNOWN: | |
876 | break; | |
877 | } | |
878 | return "unknown"; | |
879 | } |