blob: af494391bb414cf286395389d81772eda9a973e5 [file] [log] [blame]
Patrick Georgi0588d192009-08-12 15:00:51 +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 <regex.h>
zbaod38b22f2015-09-26 06:22:11 -040010#ifndef __MINGW32__
Patrick Georgi0588d192009-08-12 15:00:51 +000011#include <sys/utsname.h>
zbaod38b22f2015-09-26 06:22:11 -040012#endif
Patrick Georgi0588d192009-08-12 15:00:51 +000013
Patrick Georgi0588d192009-08-12 15:00:51 +000014#include "lkc.h"
15
16struct symbol symbol_yes = {
17 .name = "y",
18 .curr = { "y", yes },
19 .flags = SYMBOL_CONST|SYMBOL_VALID,
20}, symbol_mod = {
21 .name = "m",
22 .curr = { "m", mod },
23 .flags = SYMBOL_CONST|SYMBOL_VALID,
24}, symbol_no = {
25 .name = "n",
26 .curr = { "n", no },
27 .flags = SYMBOL_CONST|SYMBOL_VALID,
28}, symbol_empty = {
29 .name = "",
30 .curr = { "", no },
31 .flags = SYMBOL_VALID,
32};
33
34struct symbol *sym_defconfig_list;
35struct symbol *modules_sym;
36tristate modules_val;
37
38struct expr *sym_env_list;
39
Patrick Georgid5208402014-04-11 20:24:06 +020040static void sym_add_default(struct symbol *sym, const char *def)
Patrick Georgi0588d192009-08-12 15:00:51 +000041{
42 struct property *prop = prop_alloc(P_DEFAULT, sym);
43
Roman Zippel543aa7b2008-02-29 05:11:50 +010044 prop->expr = expr_alloc_symbol(sym_lookup(def, SYMBOL_CONST));
Patrick Georgi0588d192009-08-12 15:00:51 +000045}
46
47void sym_init(void)
48{
49 struct symbol *sym;
zbaod38b22f2015-09-26 06:22:11 -040050#ifndef __MINGW32__
Patrick Georgi0588d192009-08-12 15:00:51 +000051 struct utsname uts;
zbaod38b22f2015-09-26 06:22:11 -040052#endif
Patrick Georgi0588d192009-08-12 15:00:51 +000053 static bool inited = false;
54
55 if (inited)
56 return;
57 inited = true;
58
zbaod38b22f2015-09-26 06:22:11 -040059#ifndef __MINGW32__
Patrick Georgi0588d192009-08-12 15:00:51 +000060 uname(&uts);
zbaod38b22f2015-09-26 06:22:11 -040061#endif
Patrick Georgi0588d192009-08-12 15:00:51 +000062
63 sym = sym_lookup("UNAME_RELEASE", 0);
64 sym->type = S_STRING;
65 sym->flags |= SYMBOL_AUTO;
zbaod38b22f2015-09-26 06:22:11 -040066#ifndef __MINGW32__
Patrick Georgi0588d192009-08-12 15:00:51 +000067 sym_add_default(sym, uts.release);
zbaod38b22f2015-09-26 06:22:11 -040068#else
69 sym_add_default(sym, "mingw-unknown");
70#endif
Patrick Georgi0588d192009-08-12 15:00:51 +000071}
72
73enum symbol_type sym_get_type(struct symbol *sym)
74{
75 enum symbol_type type = sym->type;
76
77 if (type == S_TRISTATE) {
78 if (sym_is_choice_value(sym) && sym->visible == yes)
79 type = S_BOOLEAN;
80 else if (modules_val == no)
81 type = S_BOOLEAN;
82 }
83 return type;
84}
85
86const char *sym_type_name(enum symbol_type type)
87{
88 switch (type) {
89 case S_BOOLEAN:
90 return "boolean";
91 case S_TRISTATE:
92 return "tristate";
93 case S_INT:
94 return "integer";
95 case S_HEX:
96 return "hex";
97 case S_STRING:
98 return "string";
99 case S_UNKNOWN:
100 return "unknown";
101 case S_OTHER:
102 break;
103 }
104 return "???";
105}
106
107struct property *sym_get_choice_prop(struct symbol *sym)
108{
109 struct property *prop;
110
111 for_all_choices(sym, prop)
112 return prop;
113 return NULL;
114}
115
116struct property *sym_get_env_prop(struct symbol *sym)
117{
118 struct property *prop;
119
120 for_all_properties(sym, prop, P_ENV)
121 return prop;
122 return NULL;
123}
124
125struct property *sym_get_default_prop(struct symbol *sym)
126{
127 struct property *prop;
128
129 for_all_defaults(sym, prop) {
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
Patrick Georgid5208402014-04-11 20:24:06 +0200137static struct property *sym_get_range_prop(struct symbol *sym)
Patrick Georgi0588d192009-08-12 15:00:51 +0000138{
139 struct property *prop;
140
141 for_all_properties(sym, prop, P_RANGE) {
142 prop->visible.tri = expr_calc_value(prop->visible.expr);
143 if (prop->visible.tri != no)
144 return prop;
145 }
146 return NULL;
147}
148
Patrick Georgid5208402014-04-11 20:24:06 +0200149static long long sym_get_range_val(struct symbol *sym, int base)
Patrick Georgi0588d192009-08-12 15:00:51 +0000150{
151 sym_calc_value(sym);
152 switch (sym->type) {
153 case S_INT:
154 base = 10;
155 break;
156 case S_HEX:
157 base = 16;
158 break;
159 default:
160 break;
161 }
Patrick Georgid5208402014-04-11 20:24:06 +0200162 return strtoll(sym->curr.val, NULL, base);
Patrick Georgi0588d192009-08-12 15:00:51 +0000163}
164
165static void sym_validate_range(struct symbol *sym)
166{
167 struct property *prop;
Patrick Georgid5208402014-04-11 20:24:06 +0200168 int base;
169 long long val, val2;
Patrick Georgi0588d192009-08-12 15:00:51 +0000170 char str[64];
171
172 switch (sym->type) {
173 case S_INT:
174 base = 10;
175 break;
176 case S_HEX:
177 base = 16;
178 break;
179 default:
180 return;
181 }
182 prop = sym_get_range_prop(sym);
183 if (!prop)
184 return;
Patrick Georgid5208402014-04-11 20:24:06 +0200185 val = strtoll(sym->curr.val, NULL, base);
Patrick Georgi0588d192009-08-12 15:00:51 +0000186 val2 = sym_get_range_val(prop->expr->left.sym, base);
187 if (val >= val2) {
188 val2 = sym_get_range_val(prop->expr->right.sym, base);
189 if (val <= val2)
190 return;
191 }
192 if (sym->type == S_INT)
Patrick Georgid5208402014-04-11 20:24:06 +0200193 sprintf(str, "%lld", val2);
Patrick Georgi0588d192009-08-12 15:00:51 +0000194 else
Patrick Georgid5208402014-04-11 20:24:06 +0200195 sprintf(str, "0x%llx", val2);
Patrick Georgi0588d192009-08-12 15:00:51 +0000196 sym->curr.val = strdup(str);
197}
198
199static void sym_calc_visibility(struct symbol *sym)
200{
201 struct property *prop;
202 tristate tri;
203
204 /* any prompt visible? */
205 tri = no;
206 for_all_prompts(sym, prop) {
207 prop->visible.tri = expr_calc_value(prop->visible.expr);
208 tri = EXPR_OR(tri, prop->visible.tri);
209 }
210 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no))
211 tri = yes;
212 if (sym->visible != tri) {
213 sym->visible = tri;
214 sym_set_changed(sym);
215 }
216 if (sym_is_choice_value(sym))
217 return;
Patrick Georgid5208402014-04-11 20:24:06 +0200218 /* defaulting to "yes" if no explicit "depends on" are given */
219 tri = yes;
220 if (sym->dir_dep.expr)
221 tri = expr_calc_value(sym->dir_dep.expr);
222 if (tri == mod)
223 tri = yes;
224 if (sym->dir_dep.tri != tri) {
225 sym->dir_dep.tri = tri;
226 sym_set_changed(sym);
227 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000228 tri = no;
229 if (sym->rev_dep.expr)
230 tri = expr_calc_value(sym->rev_dep.expr);
231 if (tri == mod && sym_get_type(sym) == S_BOOLEAN)
232 tri = yes;
233 if (sym->rev_dep.tri != tri) {
234 sym->rev_dep.tri = tri;
235 sym_set_changed(sym);
236 }
237}
238
Patrick Georgid5208402014-04-11 20:24:06 +0200239/*
240 * Find the default symbol for a choice.
241 * First try the default values for the choice symbol
242 * Next locate the first visible choice value
243 * Return NULL if none was found
244 */
245struct symbol *sym_choice_default(struct symbol *sym)
Patrick Georgi0588d192009-08-12 15:00:51 +0000246{
247 struct symbol *def_sym;
248 struct property *prop;
249 struct expr *e;
250
Patrick Georgi0588d192009-08-12 15:00:51 +0000251 /* any of the defaults visible? */
252 for_all_defaults(sym, prop) {
253 prop->visible.tri = expr_calc_value(prop->visible.expr);
254 if (prop->visible.tri == no)
255 continue;
256 def_sym = prop_get_symbol(prop);
Patrick Georgi0588d192009-08-12 15:00:51 +0000257 if (def_sym->visible != no)
258 return def_sym;
259 }
260
261 /* just get the first visible value */
262 prop = sym_get_choice_prop(sym);
Patrick Georgid5208402014-04-11 20:24:06 +0200263 expr_list_for_each_sym(prop->expr, e, def_sym)
264 if (def_sym->visible != no)
265 return def_sym;
266
267 /* failed to locate any defaults */
268 return NULL;
269}
270
271static struct symbol *sym_calc_choice(struct symbol *sym)
272{
273 struct symbol *def_sym;
274 struct property *prop;
275 struct expr *e;
276 int flags;
277
278 /* first calculate all choice values' visibilities */
279 flags = sym->flags;
280 prop = sym_get_choice_prop(sym);
Patrick Georgi0588d192009-08-12 15:00:51 +0000281 expr_list_for_each_sym(prop->expr, e, def_sym) {
282 sym_calc_visibility(def_sym);
283 if (def_sym->visible != no)
Patrick Georgid5208402014-04-11 20:24:06 +0200284 flags &= def_sym->flags;
Patrick Georgi0588d192009-08-12 15:00:51 +0000285 }
286
Patrick Georgid5208402014-04-11 20:24:06 +0200287 sym->flags &= flags | ~SYMBOL_DEF_USER;
288
289 /* is the user choice visible? */
290 def_sym = sym->def[S_DEF_USER].val;
291 if (def_sym && def_sym->visible != no)
292 return def_sym;
293
294 def_sym = sym_choice_default(sym);
295
296 if (def_sym == NULL)
297 /* no choice? reset tristate value */
298 sym->curr.tri = no;
299
300 return def_sym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000301}
302
303void sym_calc_value(struct symbol *sym)
304{
305 struct symbol_value newval, oldval;
306 struct property *prop;
307 struct expr *e;
308
309 if (!sym)
310 return;
311
312 if (sym->flags & SYMBOL_VALID)
313 return;
Patrick Georgid5208402014-04-11 20:24:06 +0200314
315 if (sym_is_choice_value(sym) &&
316 sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES) {
317 sym->flags &= ~SYMBOL_NEED_SET_CHOICE_VALUES;
318 prop = sym_get_choice_prop(sym);
319 sym_calc_value(prop_get_symbol(prop));
320 }
321
Patrick Georgi0588d192009-08-12 15:00:51 +0000322 sym->flags |= SYMBOL_VALID;
323
324 oldval = sym->curr;
325
326 switch (sym->type) {
327 case S_INT:
328 case S_HEX:
329 case S_STRING:
330 newval = symbol_empty.curr;
331 break;
332 case S_BOOLEAN:
333 case S_TRISTATE:
334 newval = symbol_no.curr;
335 break;
336 default:
337 sym->curr.val = sym->name;
338 sym->curr.tri = no;
339 return;
340 }
341 if (!sym_is_choice_value(sym))
342 sym->flags &= ~SYMBOL_WRITE;
343
344 sym_calc_visibility(sym);
345
346 /* set default if recursively called */
347 sym->curr = newval;
348
349 switch (sym_get_type(sym)) {
350 case S_BOOLEAN:
351 case S_TRISTATE:
352 if (sym_is_choice_value(sym) && sym->visible == yes) {
353 prop = sym_get_choice_prop(sym);
354 newval.tri = (prop_get_symbol(prop)->curr.val == sym) ? yes : no;
355 } else {
356 if (sym->visible != no) {
357 /* if the symbol is visible use the user value
358 * if available, otherwise try the default value
359 */
360 sym->flags |= SYMBOL_WRITE;
361 if (sym_has_value(sym)) {
362 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri,
363 sym->visible);
364 goto calc_newval;
365 }
366 }
367 if (sym->rev_dep.tri != no)
368 sym->flags |= SYMBOL_WRITE;
369 if (!sym_is_choice(sym)) {
370 prop = sym_get_default_prop(sym);
371 if (prop) {
372 sym->flags |= SYMBOL_WRITE;
373 newval.tri = EXPR_AND(expr_calc_value(prop->expr),
374 prop->visible.tri);
375 }
376 }
377 calc_newval:
Patrick Georgid5208402014-04-11 20:24:06 +0200378 if (sym->dir_dep.tri == no && sym->rev_dep.tri != no) {
379 struct expr *e;
380 e = expr_simplify_unmet_dep(sym->rev_dep.expr,
381 sym->dir_dep.expr);
382 fprintf(stderr, "warning: (");
383 expr_fprint(e, stderr);
384 fprintf(stderr, ") selects %s which has unmet direct dependencies (",
385 sym->name);
386 expr_fprint(sym->dir_dep.expr, stderr);
387 fprintf(stderr, ")\n");
Stefan Reinauer57a31312015-08-20 11:19:34 -0700388 kconfig_warnings++;
Patrick Georgid5208402014-04-11 20:24:06 +0200389 expr_free(e);
390 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000391 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri);
392 }
393 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN)
394 newval.tri = yes;
395 break;
396 case S_STRING:
397 case S_HEX:
398 case S_INT:
399 if (sym->visible != no) {
400 sym->flags |= SYMBOL_WRITE;
401 if (sym_has_value(sym)) {
402 newval.val = sym->def[S_DEF_USER].val;
403 break;
404 }
405 }
406 prop = sym_get_default_prop(sym);
407 if (prop) {
408 struct symbol *ds = prop_get_symbol(prop);
409 if (ds) {
410 sym->flags |= SYMBOL_WRITE;
411 sym_calc_value(ds);
412 newval.val = ds->curr.val;
413 }
414 }
415 break;
416 default:
417 ;
418 }
419
Patrick Georgi0588d192009-08-12 15:00:51 +0000420 sym->curr = newval;
421 if (sym_is_choice(sym) && newval.tri == yes)
422 sym->curr.val = sym_calc_choice(sym);
423 sym_validate_range(sym);
424
425 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) {
426 sym_set_changed(sym);
427 if (modules_sym == sym) {
428 sym_set_all_changed();
429 modules_val = modules_sym->curr.tri;
430 }
431 }
432
433 if (sym_is_choice(sym)) {
434 struct symbol *choice_sym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000435
436 prop = sym_get_choice_prop(sym);
437 expr_list_for_each_sym(prop->expr, e, choice_sym) {
Patrick Georgid5208402014-04-11 20:24:06 +0200438 if ((sym->flags & SYMBOL_WRITE) &&
439 choice_sym->visible != no)
440 choice_sym->flags |= SYMBOL_WRITE;
441 if (sym->flags & SYMBOL_CHANGED)
Patrick Georgi0588d192009-08-12 15:00:51 +0000442 sym_set_changed(choice_sym);
443 }
444 }
Roman Zippel543aa7b2008-02-29 05:11:50 +0100445
446 if (sym->flags & SYMBOL_AUTO)
447 sym->flags &= ~SYMBOL_WRITE;
Patrick Georgid5208402014-04-11 20:24:06 +0200448
449 if (sym->flags & SYMBOL_NEED_SET_CHOICE_VALUES)
450 set_all_choice_values(sym);
Patrick Georgi0588d192009-08-12 15:00:51 +0000451}
452
453void sym_clear_all_valid(void)
454{
455 struct symbol *sym;
456 int i;
457
458 for_all_symbols(i, sym)
459 sym->flags &= ~SYMBOL_VALID;
460 sym_add_change_count(1);
461 if (modules_sym)
462 sym_calc_value(modules_sym);
463}
464
465void sym_set_changed(struct symbol *sym)
466{
467 struct property *prop;
468
469 sym->flags |= SYMBOL_CHANGED;
470 for (prop = sym->prop; prop; prop = prop->next) {
471 if (prop->menu)
472 prop->menu->flags |= MENU_CHANGED;
473 }
474}
475
476void sym_set_all_changed(void)
477{
478 struct symbol *sym;
479 int i;
480
481 for_all_symbols(i, sym)
482 sym_set_changed(sym);
483}
484
485bool sym_tristate_within_range(struct symbol *sym, tristate val)
486{
487 int type = sym_get_type(sym);
488
489 if (sym->visible == no)
490 return false;
491
492 if (type != S_BOOLEAN && type != S_TRISTATE)
493 return false;
494
495 if (type == S_BOOLEAN && val == mod)
496 return false;
497 if (sym->visible <= sym->rev_dep.tri)
498 return false;
499 if (sym_is_choice_value(sym) && sym->visible == yes)
500 return val == yes;
501 return val >= sym->rev_dep.tri && val <= sym->visible;
502}
503
504bool sym_set_tristate_value(struct symbol *sym, tristate val)
505{
506 tristate oldval = sym_get_tristate_value(sym);
507
508 if (oldval != val && !sym_tristate_within_range(sym, val))
509 return false;
510
511 if (!(sym->flags & SYMBOL_DEF_USER)) {
512 sym->flags |= SYMBOL_DEF_USER;
513 sym_set_changed(sym);
514 }
515 /*
516 * setting a choice value also resets the new flag of the choice
517 * symbol and all other choice values.
518 */
519 if (sym_is_choice_value(sym) && val == yes) {
520 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
521 struct property *prop;
522 struct expr *e;
523
524 cs->def[S_DEF_USER].val = sym;
525 cs->flags |= SYMBOL_DEF_USER;
526 prop = sym_get_choice_prop(cs);
527 for (e = prop->expr; e; e = e->left.expr) {
528 if (e->right.sym->visible != no)
529 e->right.sym->flags |= SYMBOL_DEF_USER;
530 }
531 }
532
533 sym->def[S_DEF_USER].tri = val;
534 if (oldval != val)
535 sym_clear_all_valid();
536
537 return true;
538}
539
540tristate sym_toggle_tristate_value(struct symbol *sym)
541{
542 tristate oldval, newval;
543
544 oldval = newval = sym_get_tristate_value(sym);
545 do {
546 switch (newval) {
547 case no:
548 newval = mod;
549 break;
550 case mod:
551 newval = yes;
552 break;
553 case yes:
554 newval = no;
555 break;
556 }
557 if (sym_set_tristate_value(sym, newval))
558 break;
559 } while (oldval != newval);
560 return newval;
561}
562
563bool sym_string_valid(struct symbol *sym, const char *str)
564{
565 signed char ch;
566
567 switch (sym->type) {
568 case S_STRING:
569 return true;
570 case S_INT:
571 ch = *str++;
572 if (ch == '-')
573 ch = *str++;
574 if (!isdigit(ch))
575 return false;
576 if (ch == '0' && *str != 0)
577 return false;
578 while ((ch = *str++)) {
579 if (!isdigit(ch))
580 return false;
581 }
582 return true;
583 case S_HEX:
584 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
585 str += 2;
586 ch = *str++;
587 do {
588 if (!isxdigit(ch))
589 return false;
590 } while ((ch = *str++));
591 return true;
592 case S_BOOLEAN:
593 case S_TRISTATE:
594 switch (str[0]) {
595 case 'y': case 'Y':
596 case 'm': case 'M':
597 case 'n': case 'N':
598 return true;
599 }
600 return false;
601 default:
602 return false;
603 }
604}
605
606bool sym_string_within_range(struct symbol *sym, const char *str)
607{
608 struct property *prop;
Patrick Georgid5208402014-04-11 20:24:06 +0200609 long long val;
Patrick Georgi0588d192009-08-12 15:00:51 +0000610
611 switch (sym->type) {
612 case S_STRING:
613 return sym_string_valid(sym, str);
614 case S_INT:
615 if (!sym_string_valid(sym, str))
616 return false;
617 prop = sym_get_range_prop(sym);
618 if (!prop)
619 return true;
Patrick Georgid5208402014-04-11 20:24:06 +0200620 val = strtoll(str, NULL, 10);
Patrick Georgi0588d192009-08-12 15:00:51 +0000621 return val >= sym_get_range_val(prop->expr->left.sym, 10) &&
622 val <= sym_get_range_val(prop->expr->right.sym, 10);
623 case S_HEX:
624 if (!sym_string_valid(sym, str))
625 return false;
626 prop = sym_get_range_prop(sym);
627 if (!prop)
628 return true;
Patrick Georgid5208402014-04-11 20:24:06 +0200629 val = strtoll(str, NULL, 16);
Patrick Georgi0588d192009-08-12 15:00:51 +0000630 return val >= sym_get_range_val(prop->expr->left.sym, 16) &&
631 val <= sym_get_range_val(prop->expr->right.sym, 16);
632 case S_BOOLEAN:
633 case S_TRISTATE:
634 switch (str[0]) {
635 case 'y': case 'Y':
636 return sym_tristate_within_range(sym, yes);
637 case 'm': case 'M':
638 return sym_tristate_within_range(sym, mod);
639 case 'n': case 'N':
640 return sym_tristate_within_range(sym, no);
641 }
642 return false;
643 default:
644 return false;
645 }
646}
647
648bool sym_set_string_value(struct symbol *sym, const char *newval)
649{
650 const char *oldval;
651 char *val;
652 int size;
653
654 switch (sym->type) {
655 case S_BOOLEAN:
656 case S_TRISTATE:
657 switch (newval[0]) {
658 case 'y': case 'Y':
659 return sym_set_tristate_value(sym, yes);
660 case 'm': case 'M':
661 return sym_set_tristate_value(sym, mod);
662 case 'n': case 'N':
663 return sym_set_tristate_value(sym, no);
664 }
665 return false;
666 default:
667 ;
668 }
669
670 if (!sym_string_within_range(sym, newval))
671 return false;
672
673 if (!(sym->flags & SYMBOL_DEF_USER)) {
674 sym->flags |= SYMBOL_DEF_USER;
675 sym_set_changed(sym);
676 }
677
678 oldval = sym->def[S_DEF_USER].val;
679 size = strlen(newval) + 1;
680 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
681 size += 2;
Patrick Georgid5208402014-04-11 20:24:06 +0200682 sym->def[S_DEF_USER].val = val = xmalloc(size);
Patrick Georgi0588d192009-08-12 15:00:51 +0000683 *val++ = '0';
684 *val++ = 'x';
685 } else if (!oldval || strcmp(oldval, newval))
Patrick Georgid5208402014-04-11 20:24:06 +0200686 sym->def[S_DEF_USER].val = val = xmalloc(size);
Patrick Georgi0588d192009-08-12 15:00:51 +0000687 else
688 return true;
689
690 strcpy(val, newval);
691 free((void *)oldval);
692 sym_clear_all_valid();
693
694 return true;
695}
696
Patrick Georgid5208402014-04-11 20:24:06 +0200697/*
698 * Find the default value associated to a symbol.
699 * For tristate symbol handle the modules=n case
700 * in which case "m" becomes "y".
701 * If the symbol does not have any default then fallback
702 * to the fixed default values.
703 */
704const char *sym_get_string_default(struct symbol *sym)
705{
706 struct property *prop;
707 struct symbol *ds;
708 const char *str;
709 tristate val;
710
711 sym_calc_visibility(sym);
712 sym_calc_value(modules_sym);
713 val = symbol_no.curr.tri;
714 str = symbol_empty.curr.val;
715
716 /* If symbol has a default value look it up */
717 prop = sym_get_default_prop(sym);
718 if (prop != NULL) {
719 switch (sym->type) {
720 case S_BOOLEAN:
721 case S_TRISTATE:
722 /* The visibility may limit the value from yes => mod */
723 val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri);
724 break;
725 default:
726 /*
727 * The following fails to handle the situation
728 * where a default value is further limited by
729 * the valid range.
730 */
731 ds = prop_get_symbol(prop);
732 if (ds != NULL) {
733 sym_calc_value(ds);
734 str = (const char *)ds->curr.val;
735 }
736 }
737 }
738
739 /* Handle select statements */
740 val = EXPR_OR(val, sym->rev_dep.tri);
741
742 /* transpose mod to yes if modules are not enabled */
743 if (val == mod)
744 if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
745 val = yes;
746
747 /* transpose mod to yes if type is bool */
748 if (sym->type == S_BOOLEAN && val == mod)
749 val = yes;
750
751 switch (sym->type) {
752 case S_BOOLEAN:
753 case S_TRISTATE:
754 switch (val) {
755 case no: return "n";
756 case mod: return "m";
757 case yes: return "y";
758 }
759 case S_INT:
760 case S_HEX:
761 return str;
762 case S_STRING:
763 return str;
764 case S_OTHER:
765 case S_UNKNOWN:
766 break;
767 }
768 return "";
769}
770
Patrick Georgi0588d192009-08-12 15:00:51 +0000771const char *sym_get_string_value(struct symbol *sym)
772{
773 tristate val;
774
775 switch (sym->type) {
776 case S_BOOLEAN:
777 case S_TRISTATE:
778 val = sym_get_tristate_value(sym);
779 switch (val) {
780 case no:
781 return "n";
782 case mod:
Patrick Georgid5208402014-04-11 20:24:06 +0200783 sym_calc_value(modules_sym);
784 return (modules_sym->curr.tri == no) ? "n" : "m";
Patrick Georgi0588d192009-08-12 15:00:51 +0000785 case yes:
786 return "y";
787 }
788 break;
789 default:
790 ;
791 }
792 return (const char *)sym->curr.val;
793}
794
795bool sym_is_changable(struct symbol *sym)
796{
797 return sym->visible > sym->rev_dep.tri;
798}
799
Patrick Georgid5208402014-04-11 20:24:06 +0200800static unsigned strhash(const char *s)
801{
802 /* fnv32 hash */
803 unsigned hash = 2166136261U;
804 for (; *s; s++)
805 hash = (hash ^ *s) * 0x01000193;
806 return hash;
807}
808
Roman Zippel543aa7b2008-02-29 05:11:50 +0100809struct symbol *sym_lookup(const char *name, int flags)
Patrick Georgi0588d192009-08-12 15:00:51 +0000810{
811 struct symbol *symbol;
Patrick Georgi0588d192009-08-12 15:00:51 +0000812 char *new_name;
Patrick Georgid5208402014-04-11 20:24:06 +0200813 int hash;
Patrick Georgi0588d192009-08-12 15:00:51 +0000814
815 if (name) {
816 if (name[0] && !name[1]) {
817 switch (name[0]) {
818 case 'y': return &symbol_yes;
819 case 'm': return &symbol_mod;
820 case 'n': return &symbol_no;
821 }
822 }
Patrick Georgid5208402014-04-11 20:24:06 +0200823 hash = strhash(name) % SYMBOL_HASHSIZE;
Patrick Georgi0588d192009-08-12 15:00:51 +0000824
825 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Patrick Georgid5208402014-04-11 20:24:06 +0200826 if (symbol->name &&
827 !strcmp(symbol->name, name) &&
Roman Zippel543aa7b2008-02-29 05:11:50 +0100828 (flags ? symbol->flags & flags
829 : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
830 return symbol;
Patrick Georgi0588d192009-08-12 15:00:51 +0000831 }
832 new_name = strdup(name);
833 } else {
834 new_name = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +0200835 hash = 0;
Patrick Georgi0588d192009-08-12 15:00:51 +0000836 }
837
Patrick Georgid5208402014-04-11 20:24:06 +0200838 symbol = xmalloc(sizeof(*symbol));
Patrick Georgi0588d192009-08-12 15:00:51 +0000839 memset(symbol, 0, sizeof(*symbol));
840 symbol->name = new_name;
841 symbol->type = S_UNKNOWN;
Roman Zippel543aa7b2008-02-29 05:11:50 +0100842 symbol->flags |= flags;
Patrick Georgi0588d192009-08-12 15:00:51 +0000843
844 symbol->next = symbol_hash[hash];
845 symbol_hash[hash] = symbol;
846
847 return symbol;
848}
849
850struct symbol *sym_find(const char *name)
851{
852 struct symbol *symbol = NULL;
Patrick Georgi0588d192009-08-12 15:00:51 +0000853 int hash = 0;
854
855 if (!name)
856 return NULL;
857
858 if (name[0] && !name[1]) {
859 switch (name[0]) {
860 case 'y': return &symbol_yes;
861 case 'm': return &symbol_mod;
862 case 'n': return &symbol_no;
863 }
864 }
Patrick Georgid5208402014-04-11 20:24:06 +0200865 hash = strhash(name) % SYMBOL_HASHSIZE;
Patrick Georgi0588d192009-08-12 15:00:51 +0000866
867 for (symbol = symbol_hash[hash]; symbol; symbol = symbol->next) {
Patrick Georgid5208402014-04-11 20:24:06 +0200868 if (symbol->name &&
869 !strcmp(symbol->name, name) &&
Patrick Georgi0588d192009-08-12 15:00:51 +0000870 !(symbol->flags & SYMBOL_CONST))
871 break;
872 }
873
874 return symbol;
875}
876
Patrick Georgid5208402014-04-11 20:24:06 +0200877/*
878 * Expand symbol's names embedded in the string given in argument. Symbols'
879 * name to be expanded shall be prefixed by a '$'. Unknown symbol expands to
880 * the empty string.
881 */
882const char *sym_expand_string_value(const char *in)
883{
884 const char *src;
885 char *res;
886 size_t reslen;
887
888 reslen = strlen(in) + 1;
889 res = xmalloc(reslen);
890 res[0] = '\0';
891
892 while ((src = strchr(in, '$'))) {
893 char *p, name[SYMBOL_MAXLENGTH];
894 const char *symval = "";
895 struct symbol *sym;
896 size_t newlen;
897
898 strncat(res, in, src - in);
899 src++;
900
901 p = name;
902 while (isalnum(*src) || *src == '_')
903 *p++ = *src++;
904 *p = '\0';
905
906 sym = sym_find(name);
907 if (sym != NULL) {
908 sym_calc_value(sym);
909 symval = sym_get_string_value(sym);
910 }
911
912 newlen = strlen(res) + strlen(symval) + strlen(src) + 1;
913 if (newlen > reslen) {
914 reslen = newlen;
915 res = realloc(res, reslen);
916 }
917
918 strcat(res, symval);
919 in = src;
920 }
921 strcat(res, in);
922
923 return res;
924}
925
926const char *sym_escape_string_value(const char *in)
927{
928 const char *p;
929 size_t reslen;
930 char *res;
931 size_t l;
932
933 reslen = strlen(in) + strlen("\"\"") + 1;
934
935 p = in;
936 for (;;) {
937 l = strcspn(p, "\"\\");
938 p += l;
939
940 if (p[0] == '\0')
941 break;
942
943 reslen++;
944 p++;
945 }
946
947 res = xmalloc(reslen);
948 res[0] = '\0';
949
950 strcat(res, "\"");
951
952 p = in;
953 for (;;) {
954 l = strcspn(p, "\"\\");
955 strncat(res, p, l);
956 p += l;
957
958 if (p[0] == '\0')
959 break;
960
961 strcat(res, "\\");
962 strncat(res, p++, 1);
963 }
964
965 strcat(res, "\"");
966 return res;
967}
968
969struct sym_match {
970 struct symbol *sym;
971 off_t so, eo;
972};
973
974/* Compare matched symbols as thus:
975 * - first, symbols that match exactly
976 * - then, alphabetical sort
977 */
978static int sym_rel_comp(const void *sym1, const void *sym2)
979{
980 const struct sym_match *s1 = sym1;
981 const struct sym_match *s2 = sym2;
982 int exact1, exact2;
983
984 /* Exact match:
985 * - if matched length on symbol s1 is the length of that symbol,
986 * then this symbol should come first;
987 * - if matched length on symbol s2 is the length of that symbol,
988 * then this symbol should come first.
989 * Note: since the search can be a regexp, both symbols may match
990 * exactly; if this is the case, we can't decide which comes first,
991 * and we fallback to sorting alphabetically.
992 */
993 exact1 = (s1->eo - s1->so) == strlen(s1->sym->name);
994 exact2 = (s2->eo - s2->so) == strlen(s2->sym->name);
995 if (exact1 && !exact2)
996 return -1;
997 if (!exact1 && exact2)
998 return 1;
999
1000 /* As a fallback, sort symbols alphabetically */
1001 return strcmp(s1->sym->name, s2->sym->name);
1002}
1003
Patrick Georgi0588d192009-08-12 15:00:51 +00001004struct symbol **sym_re_search(const char *pattern)
1005{
1006 struct symbol *sym, **sym_arr = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +02001007 struct sym_match *sym_match_arr = NULL;
Patrick Georgi0588d192009-08-12 15:00:51 +00001008 int i, cnt, size;
1009 regex_t re;
Patrick Georgid5208402014-04-11 20:24:06 +02001010 regmatch_t match[1];
Patrick Georgi0588d192009-08-12 15:00:51 +00001011
1012 cnt = size = 0;
1013 /* Skip if empty */
1014 if (strlen(pattern) == 0)
1015 return NULL;
Patrick Georgid5208402014-04-11 20:24:06 +02001016 if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE))
Patrick Georgi0588d192009-08-12 15:00:51 +00001017 return NULL;
1018
1019 for_all_symbols(i, sym) {
1020 if (sym->flags & SYMBOL_CONST || !sym->name)
1021 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001022 if (regexec(&re, sym->name, 1, match, 0))
Patrick Georgi0588d192009-08-12 15:00:51 +00001023 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001024 if (cnt >= size) {
1025 void *tmp;
Patrick Georgi0588d192009-08-12 15:00:51 +00001026 size += 16;
Patrick Georgid5208402014-04-11 20:24:06 +02001027 tmp = realloc(sym_match_arr, size * sizeof(struct sym_match));
1028 if (!tmp)
1029 goto sym_re_search_free;
1030 sym_match_arr = tmp;
Patrick Georgi0588d192009-08-12 15:00:51 +00001031 }
Patrick Georgid5208402014-04-11 20:24:06 +02001032 sym_calc_value(sym);
1033 /* As regexec returned 0, we know we have a match, so
1034 * we can use match[0].rm_[se]o without further checks
1035 */
1036 sym_match_arr[cnt].so = match[0].rm_so;
1037 sym_match_arr[cnt].eo = match[0].rm_eo;
1038 sym_match_arr[cnt++].sym = sym;
Patrick Georgi0588d192009-08-12 15:00:51 +00001039 }
Patrick Georgid5208402014-04-11 20:24:06 +02001040 if (sym_match_arr) {
1041 qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp);
1042 sym_arr = malloc((cnt+1) * sizeof(struct symbol));
1043 if (!sym_arr)
1044 goto sym_re_search_free;
1045 for (i = 0; i < cnt; i++)
1046 sym_arr[i] = sym_match_arr[i].sym;
Patrick Georgi0588d192009-08-12 15:00:51 +00001047 sym_arr[cnt] = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +02001048 }
1049sym_re_search_free:
1050 /* sym_match_arr can be NULL if no match, but free(NULL) is OK */
1051 free(sym_match_arr);
Patrick Georgi0588d192009-08-12 15:00:51 +00001052 regfree(&re);
1053
1054 return sym_arr;
1055}
1056
Patrick Georgid5208402014-04-11 20:24:06 +02001057/*
1058 * When we check for recursive dependencies we use a stack to save
1059 * current state so we can print out relevant info to user.
1060 * The entries are located on the call stack so no need to free memory.
1061 * Note insert() remove() must always match to properly clear the stack.
1062 */
1063static struct dep_stack {
1064 struct dep_stack *prev, *next;
1065 struct symbol *sym;
1066 struct property *prop;
1067 struct expr *expr;
1068} *check_top;
1069
1070static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym)
1071{
1072 memset(stack, 0, sizeof(*stack));
1073 if (check_top)
1074 check_top->next = stack;
1075 stack->prev = check_top;
1076 stack->sym = sym;
1077 check_top = stack;
1078}
1079
1080static void dep_stack_remove(void)
1081{
1082 check_top = check_top->prev;
1083 if (check_top)
1084 check_top->next = NULL;
1085}
1086
1087/*
1088 * Called when we have detected a recursive dependency.
1089 * check_top point to the top of the stact so we use
1090 * the ->prev pointer to locate the bottom of the stack.
1091 */
1092static void sym_check_print_recursive(struct symbol *last_sym)
1093{
1094 struct dep_stack *stack;
1095 struct symbol *sym, *next_sym;
1096 struct menu *menu = NULL;
1097 struct property *prop;
1098 struct dep_stack cv_stack;
1099
1100 if (sym_is_choice_value(last_sym)) {
1101 dep_stack_insert(&cv_stack, last_sym);
1102 last_sym = prop_get_symbol(sym_get_choice_prop(last_sym));
1103 }
1104
1105 for (stack = check_top; stack != NULL; stack = stack->prev)
1106 if (stack->sym == last_sym)
1107 break;
1108 if (!stack) {
1109 fprintf(stderr, "unexpected recursive dependency error\n");
1110 return;
1111 }
1112
1113 for (; stack; stack = stack->next) {
1114 sym = stack->sym;
1115 next_sym = stack->next ? stack->next->sym : last_sym;
1116 prop = stack->prop;
1117 if (prop == NULL)
1118 prop = stack->sym->prop;
1119
1120 /* for choice values find the menu entry (used below) */
1121 if (sym_is_choice(sym) || sym_is_choice_value(sym)) {
1122 for (prop = sym->prop; prop; prop = prop->next) {
1123 menu = prop->menu;
1124 if (prop->menu)
1125 break;
1126 }
1127 }
1128 if (stack->sym == last_sym)
1129 fprintf(stderr, "%s:%d:error: recursive dependency detected!\n",
1130 prop->file->name, prop->lineno);
1131 if (stack->expr) {
1132 fprintf(stderr, "%s:%d:\tsymbol %s %s value contains %s\n",
1133 prop->file->name, prop->lineno,
1134 sym->name ? sym->name : "<choice>",
1135 prop_get_type_name(prop->type),
1136 next_sym->name ? next_sym->name : "<choice>");
1137 } else if (stack->prop) {
1138 fprintf(stderr, "%s:%d:\tsymbol %s depends on %s\n",
1139 prop->file->name, prop->lineno,
1140 sym->name ? sym->name : "<choice>",
1141 next_sym->name ? next_sym->name : "<choice>");
1142 } else if (sym_is_choice(sym)) {
1143 fprintf(stderr, "%s:%d:\tchoice %s contains symbol %s\n",
1144 menu->file->name, menu->lineno,
1145 sym->name ? sym->name : "<choice>",
1146 next_sym->name ? next_sym->name : "<choice>");
1147 } else if (sym_is_choice_value(sym)) {
1148 fprintf(stderr, "%s:%d:\tsymbol %s is part of choice %s\n",
1149 menu->file->name, menu->lineno,
1150 sym->name ? sym->name : "<choice>",
1151 next_sym->name ? next_sym->name : "<choice>");
1152 } else {
1153 fprintf(stderr, "%s:%d:\tsymbol %s is selected by %s\n",
1154 prop->file->name, prop->lineno,
1155 sym->name ? sym->name : "<choice>",
1156 next_sym->name ? next_sym->name : "<choice>");
1157 }
1158 }
1159
1160 if (check_top == &cv_stack)
1161 dep_stack_remove();
1162}
Patrick Georgi0588d192009-08-12 15:00:51 +00001163
Patrick Georgi0588d192009-08-12 15:00:51 +00001164static struct symbol *sym_check_expr_deps(struct expr *e)
1165{
1166 struct symbol *sym;
1167
1168 if (!e)
1169 return NULL;
1170 switch (e->type) {
1171 case E_OR:
1172 case E_AND:
1173 sym = sym_check_expr_deps(e->left.expr);
1174 if (sym)
1175 return sym;
1176 return sym_check_expr_deps(e->right.expr);
1177 case E_NOT:
1178 return sym_check_expr_deps(e->left.expr);
1179 case E_EQUAL:
1180 case E_UNEQUAL:
1181 sym = sym_check_deps(e->left.sym);
1182 if (sym)
1183 return sym;
1184 return sym_check_deps(e->right.sym);
1185 case E_SYMBOL:
1186 return sym_check_deps(e->left.sym);
1187 default:
1188 break;
1189 }
1190 printf("Oops! How to check %d?\n", e->type);
1191 return NULL;
1192}
1193
1194/* return NULL when dependencies are OK */
Roman Zippel330bb6a2008-02-29 05:10:24 +01001195static struct symbol *sym_check_sym_deps(struct symbol *sym)
1196{
1197 struct symbol *sym2;
1198 struct property *prop;
Patrick Georgid5208402014-04-11 20:24:06 +02001199 struct dep_stack stack;
1200
1201 dep_stack_insert(&stack, sym);
Roman Zippel330bb6a2008-02-29 05:10:24 +01001202
1203 sym2 = sym_check_expr_deps(sym->rev_dep.expr);
1204 if (sym2)
Patrick Georgid5208402014-04-11 20:24:06 +02001205 goto out;
Roman Zippel330bb6a2008-02-29 05:10:24 +01001206
1207 for (prop = sym->prop; prop; prop = prop->next) {
1208 if (prop->type == P_CHOICE || prop->type == P_SELECT)
1209 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001210 stack.prop = prop;
Roman Zippel330bb6a2008-02-29 05:10:24 +01001211 sym2 = sym_check_expr_deps(prop->visible.expr);
1212 if (sym2)
1213 break;
1214 if (prop->type != P_DEFAULT || sym_is_choice(sym))
1215 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001216 stack.expr = prop->expr;
Roman Zippel330bb6a2008-02-29 05:10:24 +01001217 sym2 = sym_check_expr_deps(prop->expr);
1218 if (sym2)
1219 break;
Patrick Georgid5208402014-04-11 20:24:06 +02001220 stack.expr = NULL;
Roman Zippel330bb6a2008-02-29 05:10:24 +01001221 }
1222
Patrick Georgid5208402014-04-11 20:24:06 +02001223out:
1224 dep_stack_remove();
1225
Roman Zippel330bb6a2008-02-29 05:10:24 +01001226 return sym2;
1227}
1228
1229static struct symbol *sym_check_choice_deps(struct symbol *choice)
1230{
1231 struct symbol *sym, *sym2;
1232 struct property *prop;
1233 struct expr *e;
Patrick Georgid5208402014-04-11 20:24:06 +02001234 struct dep_stack stack;
1235
1236 dep_stack_insert(&stack, choice);
Roman Zippel330bb6a2008-02-29 05:10:24 +01001237
1238 prop = sym_get_choice_prop(choice);
1239 expr_list_for_each_sym(prop->expr, e, sym)
1240 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1241
1242 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1243 sym2 = sym_check_sym_deps(choice);
1244 choice->flags &= ~SYMBOL_CHECK;
1245 if (sym2)
1246 goto out;
1247
1248 expr_list_for_each_sym(prop->expr, e, sym) {
1249 sym2 = sym_check_sym_deps(sym);
Patrick Georgid5208402014-04-11 20:24:06 +02001250 if (sym2)
Roman Zippel330bb6a2008-02-29 05:10:24 +01001251 break;
Roman Zippel330bb6a2008-02-29 05:10:24 +01001252 }
1253out:
1254 expr_list_for_each_sym(prop->expr, e, sym)
1255 sym->flags &= ~SYMBOL_CHECK;
1256
1257 if (sym2 && sym_is_choice_value(sym2) &&
1258 prop_get_symbol(sym_get_choice_prop(sym2)) == choice)
1259 sym2 = choice;
1260
Patrick Georgid5208402014-04-11 20:24:06 +02001261 dep_stack_remove();
1262
Roman Zippel330bb6a2008-02-29 05:10:24 +01001263 return sym2;
1264}
1265
Patrick Georgi0588d192009-08-12 15:00:51 +00001266struct symbol *sym_check_deps(struct symbol *sym)
1267{
1268 struct symbol *sym2;
1269 struct property *prop;
1270
1271 if (sym->flags & SYMBOL_CHECK) {
Patrick Georgid5208402014-04-11 20:24:06 +02001272 sym_check_print_recursive(sym);
Patrick Georgi0588d192009-08-12 15:00:51 +00001273 return sym;
1274 }
1275 if (sym->flags & SYMBOL_CHECKED)
1276 return NULL;
1277
Roman Zippel330bb6a2008-02-29 05:10:24 +01001278 if (sym_is_choice_value(sym)) {
Patrick Georgid5208402014-04-11 20:24:06 +02001279 struct dep_stack stack;
1280
Roman Zippel330bb6a2008-02-29 05:10:24 +01001281 /* for choice groups start the check with main choice symbol */
Patrick Georgid5208402014-04-11 20:24:06 +02001282 dep_stack_insert(&stack, sym);
Roman Zippel330bb6a2008-02-29 05:10:24 +01001283 prop = sym_get_choice_prop(sym);
1284 sym2 = sym_check_deps(prop_get_symbol(prop));
Patrick Georgid5208402014-04-11 20:24:06 +02001285 dep_stack_remove();
Roman Zippel330bb6a2008-02-29 05:10:24 +01001286 } else if (sym_is_choice(sym)) {
1287 sym2 = sym_check_choice_deps(sym);
1288 } else {
1289 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED);
1290 sym2 = sym_check_sym_deps(sym);
1291 sym->flags &= ~SYMBOL_CHECK;
Patrick Georgi0588d192009-08-12 15:00:51 +00001292 }
Roman Zippel330bb6a2008-02-29 05:10:24 +01001293
Patrick Georgid5208402014-04-11 20:24:06 +02001294 if (sym2 && sym2 == sym)
1295 sym2 = NULL;
Roman Zippel330bb6a2008-02-29 05:10:24 +01001296
Patrick Georgi0588d192009-08-12 15:00:51 +00001297 return sym2;
1298}
1299
1300struct property *prop_alloc(enum prop_type type, struct symbol *sym)
1301{
1302 struct property *prop;
1303 struct property **propp;
1304
Patrick Georgid5208402014-04-11 20:24:06 +02001305 prop = xmalloc(sizeof(*prop));
Patrick Georgi0588d192009-08-12 15:00:51 +00001306 memset(prop, 0, sizeof(*prop));
1307 prop->type = type;
1308 prop->sym = sym;
1309 prop->file = current_file;
1310 prop->lineno = zconf_lineno();
1311
1312 /* append property to the prop list of symbol */
1313 if (sym) {
1314 for (propp = &sym->prop; *propp; propp = &(*propp)->next)
1315 ;
1316 *propp = prop;
1317 }
1318
1319 return prop;
1320}
1321
1322struct symbol *prop_get_symbol(struct property *prop)
1323{
1324 if (prop->expr && (prop->expr->type == E_SYMBOL ||
1325 prop->expr->type == E_LIST))
1326 return prop->expr->left.sym;
1327 return NULL;
1328}
1329
1330const char *prop_get_type_name(enum prop_type type)
1331{
1332 switch (type) {
1333 case P_PROMPT:
1334 return "prompt";
1335 case P_ENV:
1336 return "env";
1337 case P_COMMENT:
1338 return "comment";
1339 case P_MENU:
1340 return "menu";
1341 case P_DEFAULT:
1342 return "default";
1343 case P_CHOICE:
1344 return "choice";
1345 case P_SELECT:
1346 return "select";
1347 case P_RANGE:
1348 return "range";
Patrick Georgid5208402014-04-11 20:24:06 +02001349 case P_SYMBOL:
1350 return "symbol";
Patrick Georgi0588d192009-08-12 15:00:51 +00001351 case P_UNKNOWN:
1352 break;
1353 }
1354 return "unknown";
1355}
1356
Patrick Georgid5208402014-04-11 20:24:06 +02001357static void prop_add_env(const char *env)
Patrick Georgi0588d192009-08-12 15:00:51 +00001358{
1359 struct symbol *sym, *sym2;
1360 struct property *prop;
1361 char *p;
1362
1363 sym = current_entry->sym;
1364 sym->flags |= SYMBOL_AUTO;
1365 for_all_properties(sym, prop, P_ENV) {
1366 sym2 = prop_get_symbol(prop);
1367 if (strcmp(sym2->name, env))
1368 menu_warn(current_entry, "redefining environment symbol from %s",
1369 sym2->name);
1370 return;
1371 }
1372
1373 prop = prop_alloc(P_ENV, sym);
Roman Zippel543aa7b2008-02-29 05:11:50 +01001374 prop->expr = expr_alloc_symbol(sym_lookup(env, SYMBOL_CONST));
Patrick Georgi0588d192009-08-12 15:00:51 +00001375
1376 sym_env_list = expr_alloc_one(E_LIST, sym_env_list);
1377 sym_env_list->right.sym = sym;
1378
1379 p = getenv(env);
1380 if (p)
1381 sym_add_default(sym, p);
1382 else
1383 menu_warn(current_entry, "environment variable %s undefined", env);
1384}