blob: 2b848d5d0a95bf3e65fc4ad6ed8fbb60740937f4 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
5
6#include <sys/stat.h>
7#include <ctype.h>
8#include <fcntl.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <time.h>
13#include <unistd.h>
14#include <libgen.h>
15
16#define LKC_DIRECT_LINK
17#include "lkc.h"
18
19static void conf_warning(const char *fmt, ...)
20 __attribute__ ((format (printf, 1, 2)));
21
22static const char *conf_filename;
23static int conf_lineno, conf_warnings, conf_unsaved;
24
25const char conf_defname[] = "extra/Configs/defconfigs/$ARCH";
26
27static void conf_warning(const char *fmt, ...)
28{
29 va_list ap;
30 va_start(ap, fmt);
31 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
32 vfprintf(stderr, fmt, ap);
33 fprintf(stderr, "\n");
34 va_end(ap);
35 conf_warnings++;
36}
37
38const char *conf_get_configname(void)
39{
40 char *name = getenv("KCONFIG_CONFIG");
41
42 return name ? name : ".config";
43}
44
45static char *conf_expand_value(const char *in)
46{
47 struct symbol *sym;
48 const char *src;
49 static char res_value[SYMBOL_MAXLENGTH];
50 char *dst, name[SYMBOL_MAXLENGTH];
51
52 res_value[0] = 0;
53 dst = name;
54 while ((src = strchr(in, '$'))) {
55 strncat(res_value, in, src - in);
56 src++;
57 dst = name;
58 while (isalnum(*src) || *src == '_')
59 *dst++ = *src++;
60 *dst = 0;
61 sym = sym_lookup(name, 0);
62 sym_calc_value(sym);
63 strcat(res_value, sym_get_string_value(sym));
64 in = src;
65 }
66 strcat(res_value, in);
67
68 return res_value;
69}
70
71char *conf_get_default_confname(void)
72{
73 struct stat buf;
74 static char fullname[PATH_MAX+1];
75 char *env, *name;
76
77 name = conf_expand_value(conf_defname);
78 env = getenv(SRCTREE);
79 if (env) {
80 sprintf(fullname, "%s/%s", env, name);
81 if (!stat(fullname, &buf))
82 return fullname;
83 }
84 return name;
85}
86
87static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
88{
89 char *p2;
90
91 switch (sym->type) {
92 case S_TRISTATE:
93 if (p[0] == 'm') {
94 sym->def[def].tri = mod;
95 sym->flags |= def_flags;
96 break;
97 }
98 case S_BOOLEAN:
99 if (p[0] == 'y') {
100 sym->def[def].tri = yes;
101 sym->flags |= def_flags;
102 break;
103 }
104 if (p[0] == 'n') {
105 sym->def[def].tri = no;
106 sym->flags |= def_flags;
107 break;
108 }
109 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
110 break;
111 case S_OTHER:
112 if (*p != '"') {
113 for (p2 = p; *p2 && !isspace(*p2); p2++)
114 ;
115 sym->type = S_STRING;
116 goto done;
117 }
118 case S_STRING:
119 if (*p++ != '"')
120 break;
121 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
122 if (*p2 == '"') {
123 *p2 = 0;
124 break;
125 }
126 memmove(p2, p2 + 1, strlen(p2));
127 }
128 if (!p2) {
129 conf_warning("invalid string found");
130 return 1;
131 }
132 case S_INT:
133 case S_HEX:
134 done:
135 if (sym_string_valid(sym, p)) {
136 sym->def[def].val = strdup(p);
137 sym->flags |= def_flags;
138 } else {
139 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
140 return 1;
141 }
142 break;
143 default:
144 ;
145 }
146 return 0;
147}
148
149int conf_read_simple(const char *name, int def)
150{
151 FILE *in = NULL;
152 char line[1024];
153 char *p, *p2;
154 struct symbol *sym;
155 int i, def_flags;
156
157 if (name) {
158 in = zconf_fopen(name);
159 } else {
160 struct property *prop;
161
162 name = conf_get_configname();
163 in = zconf_fopen(name);
164 if (in)
165 goto load;
166 sym_add_change_count(1);
167 if (!sym_defconfig_list)
168 return 1;
169
170 for_all_defaults(sym_defconfig_list, prop) {
171 if (expr_calc_value(prop->visible.expr) == no ||
172 prop->expr->type != E_SYMBOL)
173 continue;
174 name = conf_expand_value(prop->expr->left.sym->name);
175 in = zconf_fopen(name);
176 if (in) {
177 printf(_("#\n"
178 "# using defaults found in %s\n"
179 "#\n"), name);
180 goto load;
181 }
182 }
183 }
184 if (!in)
185 return 1;
186
187load:
188 conf_filename = name;
189 conf_lineno = 0;
190 conf_warnings = 0;
191 conf_unsaved = 0;
192
193 def_flags = SYMBOL_DEF << def;
194 for_all_symbols(i, sym) {
195 sym->flags |= SYMBOL_CHANGED;
196 sym->flags &= ~(def_flags|SYMBOL_VALID);
197 if (sym_is_choice(sym))
198 sym->flags |= def_flags;
199 switch (sym->type) {
200 case S_INT:
201 case S_HEX:
202 case S_STRING:
203 if (sym->def[def].val)
204 free(sym->def[def].val);
205 default:
206 sym->def[def].val = NULL;
207 sym->def[def].tri = no;
208 }
209 }
210
211 while (fgets(line, sizeof(line), in)) {
212 conf_lineno++;
213 sym = NULL;
214 switch (line[0]) {
215 case '#':
216 if (line[1] != ' ')
217 continue;
218 p = strchr(line + 2, ' ');
219 if (!p)
220 continue;
221 *p++ = 0;
222 if (strncmp(p, "is not set", 10))
223 continue;
224 if (def == S_DEF_USER) {
225 sym = sym_find(line + 2);
226 if (!sym) {
227 sym_add_change_count(1);
228 break;
229 }
230 } else {
231 sym = sym_lookup(line + 2, 0);
232 if (sym->type == S_UNKNOWN)
233 sym->type = S_BOOLEAN;
234 }
235 if (sym->flags & def_flags) {
236 conf_warning("override: reassigning to symbol %s", sym->name);
237 }
238 switch (sym->type) {
239 case S_BOOLEAN:
240 case S_TRISTATE:
241 sym->def[def].tri = no;
242 sym->flags |= def_flags;
243 break;
244 default:
245 ;
246 }
247 break;
248 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
249 p = strchr(line, '=');
250 if (!p) {
251 conf_warning("unexpected data '%s'", line);
252 continue;
253 }
254 *p++ = 0;
255 p2 = strchr(p, '\n');
256 if (p2) {
257 *p2-- = 0;
258 if (*p2 == '\r')
259 *p2 = 0;
260 }
261 if (def == S_DEF_USER) {
262 sym = sym_find(line);
263 if (!sym) {
264 sym_add_change_count(1);
265 break;
266 }
267 } else {
268 sym = sym_lookup(line, 0);
269 if (sym->type == S_UNKNOWN)
270 sym->type = S_OTHER;
271 }
272 if (sym->flags & def_flags) {
273 conf_warning("override: reassigning to symbol %s", sym->name);
274 }
275 if (conf_set_sym_val(sym, def, def_flags, p))
276 continue;
277 break;
278 case '\r':
279 case '\n':
280 break;
281 default:
282 conf_warning("unexpected data");
283 continue;
284 }
285 if (sym && sym_is_choice_value(sym)) {
286 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
287 switch (sym->def[def].tri) {
288 case no:
289 break;
290 case mod:
291 if (cs->def[def].tri == yes) {
292 conf_warning("%s creates inconsistent choice state", sym->name);
293 cs->flags &= ~def_flags;
294 }
295 break;
296 case yes:
297 if (cs->def[def].tri != no)
298 conf_warning("override: %s changes choice state", sym->name);
299 cs->def[def].val = sym;
300 break;
301 }
302 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
303 }
304 }
305 fclose(in);
306
307 if (modules_sym)
308 sym_calc_value(modules_sym);
309 return 0;
310}
311
312int conf_read(const char *name)
313{
314 struct symbol *sym, *choice_sym;
315 struct property *prop;
316 struct expr *e;
317 int i, flags;
318
319 sym_set_change_count(0);
320
321 if (conf_read_simple(name, S_DEF_USER))
322 return 1;
323
324 for_all_symbols(i, sym) {
325 sym_calc_value(sym);
326 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
327 goto sym_ok;
328 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
329 /* check that calculated value agrees with saved value */
330 switch (sym->type) {
331 case S_BOOLEAN:
332 case S_TRISTATE:
333 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
334 break;
335 if (!sym_is_choice(sym))
336 goto sym_ok;
337 default:
338 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
339 goto sym_ok;
340 break;
341 }
342 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
343 /* no previous value and not saved */
344 goto sym_ok;
345 conf_unsaved++;
346 /* maybe print value in verbose mode... */
347 sym_ok:
348 if (!sym_is_choice(sym))
349 continue;
350 /* The choice symbol only has a set value (and thus is not new)
351 * if all its visible childs have values.
352 */
353 prop = sym_get_choice_prop(sym);
354 flags = sym->flags;
355 expr_list_for_each_sym(prop->expr, e, choice_sym)
356 if (choice_sym->visible != no)
357 flags &= choice_sym->flags;
358 sym->flags &= flags | ~SYMBOL_DEF_USER;
359 }
360
361 for_all_symbols(i, sym) {
362 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
363 /* Reset values of generates values, so they'll appear
364 * as new, if they should become visible, but that
365 * doesn't quite work if the Kconfig and the saved
366 * configuration disagree.
367 */
368 if (sym->visible == no && !conf_unsaved)
369 sym->flags &= ~SYMBOL_DEF_USER;
370 switch (sym->type) {
371 case S_STRING:
372 case S_INT:
373 case S_HEX:
374 /* Reset a string value if it's out of range */
375 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
376 break;
377 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
378 conf_unsaved++;
379 break;
380 default:
381 break;
382 }
383 }
384 }
385
386 sym_add_change_count(conf_warnings || conf_unsaved);
387
388 return 0;
389}
390
391int conf_write(const char *name)
392{
393 FILE *out;
394 struct symbol *sym;
395 struct menu *menu;
396 const char *basename;
397 char dirname[128], tmpname[128], newname[128];
398 int type, l;
399 const char *str;
400 time_t now;
401 int use_timestamp = 1;
402 char *env;
403
404 dirname[0] = 0;
405 if (name == NULL)
406 name = conf_get_configname();
407 if (name && name[0]) {
408 struct stat st;
409 char *slash;
410
411 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
412 strcpy(dirname, name);
413 strcat(dirname, "/");
414 basename = conf_get_configname();
415 } else if ((slash = strrchr(name, '/'))) {
416 int size = slash - name + 1;
417 memcpy(dirname, name, size);
418 dirname[size] = 0;
419 if (slash[1])
420 basename = slash + 1;
421 else
422 basename = conf_get_configname();
423 } else
424 basename = name;
425 } else
426 basename = conf_get_configname();
427
428 sprintf(newname, "%s%s", dirname, basename);
429 env = getenv("KCONFIG_OVERWRITECONFIG");
430 if (!env || !*env) {
431 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
432 out = fopen(tmpname, "w");
433 } else {
434 *tmpname = 0;
435 out = fopen(newname, "w");
436 }
437 if (!out)
438 return 1;
439
440 sym = sym_lookup("VERSION", 0);
441 sym_calc_value(sym);
442 time(&now);
443 env = getenv("KCONFIG_NOTIMESTAMP");
444 if (env && *env)
445 use_timestamp = 0;
446
447 fprintf(out, _("#\n"
448 "# Automatically generated make config: don't edit\n"
449 "# Version: %s\n"
450 "%s%s"
451 "#\n"),
452 sym_get_string_value(sym),
453 use_timestamp ? "# " : "",
454 use_timestamp ? ctime(&now) : "");
455
456 if (!conf_get_changed())
457 sym_clear_all_valid();
458
459 menu = rootmenu.list;
460 while (menu) {
461 sym = menu->sym;
462 if (!sym) {
463 if (!menu_is_visible(menu))
464 goto next;
465 str = menu_get_prompt(menu);
466 fprintf(out, "\n"
467 "#\n"
468 "# %s\n"
469 "#\n", str);
470 } else if (!(sym->flags & SYMBOL_CHOICE)) {
471 sym_calc_value(sym);
472 if (!(sym->flags & SYMBOL_WRITE))
473 goto next;
474 sym->flags &= ~SYMBOL_WRITE;
475 type = sym->type;
476 if (type == S_TRISTATE) {
477 sym_calc_value(modules_sym);
478 if (modules_sym->curr.tri == no)
479 type = S_BOOLEAN;
480 }
481 switch (type) {
482 case S_BOOLEAN:
483 case S_TRISTATE:
484 switch (sym_get_tristate_value(sym)) {
485 case no:
486 fprintf(out, "# %s is not set\n", sym->name);
487 break;
488 case mod:
489 fprintf(out, "%s=m\n", sym->name);
490 break;
491 case yes:
492 fprintf(out, "%s=y\n", sym->name);
493 break;
494 }
495 break;
496 case S_STRING:
497 str = sym_get_string_value(sym);
498 fprintf(out, "%s=\"", sym->name);
499 while (1) {
500 l = strcspn(str, "\"\\");
501 if (l) {
502 fwrite(str, l, 1, out);
503 str += l;
504 }
505 if (!*str)
506 break;
507 fprintf(out, "\\%c", *str++);
508 }
509 fputs("\"\n", out);
510 break;
511 case S_HEX:
512 str = sym_get_string_value(sym);
513 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
514 fprintf(out, "%s=%s\n", sym->name, str);
515 break;
516 }
517 case S_INT:
518 str = sym_get_string_value(sym);
519 fprintf(out, "%s=%s\n", sym->name, str);
520 break;
521 }
522 }
523
524 next:
525 if (menu->list) {
526 menu = menu->list;
527 continue;
528 }
529 if (menu->next)
530 menu = menu->next;
531 else while ((menu = menu->parent)) {
532 if (menu->next) {
533 menu = menu->next;
534 break;
535 }
536 }
537 }
538 fclose(out);
539
540 if (*tmpname) {
541 strcat(dirname, basename);
542 strcat(dirname, ".old");
543 rename(newname, dirname);
544 if (rename(tmpname, newname))
545 return 1;
546 }
547
548 printf(_("#\n"
549 "# configuration written to %s\n"
550 "#\n"), newname);
551
552 sym_set_change_count(0);
553
554 return 0;
555}
556
557int conf_split_config(void)
558{
559 char *name, path[128], opwd[512];
560 char *s, *d, c;
561 struct symbol *sym;
562 struct stat sb;
563 int res, i, fd;
564
565 name = getenv("KCONFIG_AUTOCONFIG");
566 if (!name)
567 name = "include/config/auto.conf";
568 conf_read_simple(name, S_DEF_AUTO);
569
570 if (getcwd(opwd, sizeof(opwd)) == NULL)
571 return 1;
572 if (chdir(dirname(strdup(name))))
573 return 1;
574
575 res = 0;
576 for_all_symbols(i, sym) {
577 sym_calc_value(sym);
578 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
579 continue;
580 if (sym->flags & SYMBOL_WRITE) {
581 if (sym->flags & SYMBOL_DEF_AUTO) {
582 /*
583 * symbol has old and new value,
584 * so compare them...
585 */
586 switch (sym->type) {
587 case S_BOOLEAN:
588 case S_TRISTATE:
589 if (sym_get_tristate_value(sym) ==
590 sym->def[S_DEF_AUTO].tri)
591 continue;
592 break;
593 case S_STRING:
594 case S_HEX:
595 case S_INT:
596 if (!strcmp(sym_get_string_value(sym),
597 sym->def[S_DEF_AUTO].val))
598 continue;
599 break;
600 default:
601 break;
602 }
603 } else {
604 /*
605 * If there is no old value, only 'no' (unset)
606 * is allowed as new value.
607 */
608 switch (sym->type) {
609 case S_BOOLEAN:
610 case S_TRISTATE:
611 if (sym_get_tristate_value(sym) == no)
612 continue;
613 break;
614 default:
615 break;
616 }
617 }
618 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
619 /* There is neither an old nor a new value. */
620 continue;
621 /* else
622 * There is an old value, but no new value ('no' (unset)
623 * isn't saved in auto.conf, so the old value is always
624 * different from 'no').
625 */
626
627 /* Replace all '_' and append ".h" */
628 s = sym->name;
629 d = path;
630 while ((c = *s++)) {
631 c = tolower(c);
632 *d++ = (c == '_') ? '/' : c;
633 }
634 strcpy(d, ".h");
635
636 /* Assume directory path already exists. */
637 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
638 if (fd == -1) {
639 if (errno != ENOENT) {
640 res = 1;
641 break;
642 }
643 /*
644 * Create directory components,
645 * unless they exist already.
646 */
647 d = path;
648 while ((d = strchr(d, '/'))) {
649 *d = 0;
650 if (stat(path, &sb) && mkdir(path, 0755)) {
651 res = 1;
652 goto out;
653 }
654 *d++ = '/';
655 }
656 /* Try it again. */
657 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
658 if (fd == -1) {
659 res = 1;
660 break;
661 }
662 }
663 close(fd);
664 }
665out:
666 if (chdir(opwd))
667 return 1;
668
669 return res;
670}
671
672int conf_write_autoconf(void)
673{
674 char opwd[512];
675 struct symbol *sym;
676 const char *str;
677 char *name;
678 FILE *out, *out_h;
679 time_t now;
680 int i, l;
681
682 if (getcwd(opwd, sizeof(opwd)) == NULL)
683 return 1;
684 if (chdir(dirname(strdup(conf_get_configname()))))
685 return 1;
686 sym_clear_all_valid();
687
688 file_write_dep("include/config/auto.conf.cmd");
689
690 if (conf_split_config())
691 return 1;
692
693 out = fopen(".tmpconfig", "w");
694 if (!out)
695 return 1;
696
697 out_h = fopen(".tmpconfig.h", "w");
698 if (!out_h) {
699 fclose(out);
700 return 1;
701 }
702
703 sym = sym_lookup("VERSION", 0);
704 sym_calc_value(sym);
705 time(&now);
706 fprintf(out, "#\n"
707 "# Automatically generated make config: don't edit\n"
708 "# Version: %s\n"
709 "# %s"
710 "#\n",
711 sym_get_string_value(sym), ctime(&now));
712 fprintf(out_h, "/*\n"
713 " * Automatically generated C config: don't edit\n"
714 " * Version: %s\n"
715 " * %s"
716 " */\n"
717 "#define AUTOCONF_INCLUDED\n",
718 sym_get_string_value(sym), ctime(&now));
719
720 for_all_symbols(i, sym) {
721 sym_calc_value(sym);
722 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
723 continue;
724 switch (sym->type) {
725 case S_BOOLEAN:
726 case S_TRISTATE:
727 switch (sym_get_tristate_value(sym)) {
728 case no:
729 break;
730 case mod:
731 fprintf(out, "CONFIG_%s=m\n", sym->name);
732 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
733 break;
734 case yes:
735 fprintf(out, "%s=y\n", sym->name);
736 fprintf(out_h, "#define %s 1\n", sym->name);
737 break;
738 }
739 break;
740 case S_STRING:
741 str = sym_get_string_value(sym);
742 fprintf(out, "%s=\"", sym->name);
743 fprintf(out_h, "#define %s \"", sym->name);
744 while (1) {
745 l = strcspn(str, "\"\\");
746 if (l) {
747 fwrite(str, l, 1, out);
748 fwrite(str, l, 1, out_h);
749 str += l;
750 }
751 if (!*str)
752 break;
753 fprintf(out, "\\%c", *str);
754 fprintf(out_h, "\\%c", *str);
755 str++;
756 }
757 fputs("\"\n", out);
758 fputs("\"\n", out_h);
759 break;
760 case S_HEX:
761 str = sym_get_string_value(sym);
762 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
763 fprintf(out, "%s=%s\n", sym->name, str);
764 fprintf(out_h, "#define %s 0x%s\n", sym->name, str);
765 break;
766 }
767 case S_INT:
768 str = sym_get_string_value(sym);
769 fprintf(out, "%s=%s\n", sym->name, str);
770 fprintf(out_h, "#define %s %s\n", sym->name, str);
771 break;
772 default:
773 break;
774 }
775 }
776 fclose(out);
777 fclose(out_h);
778
779 name = getenv("KCONFIG_AUTOHEADER");
780 if (!name)
781 name = "include/config/autoconf.h";
782 if (rename(".tmpconfig.h", name))
783 return 1;
784 name = getenv("KCONFIG_AUTOCONFIG");
785 if (!name)
786 name = "include/config/auto.conf";
787 /*
788 * This must be the last step, kbuild has a dependency on auto.conf
789 * and this marks the successful completion of the previous steps.
790 */
791 if (rename(".tmpconfig", name))
792 return 1;
793 chdir(opwd);
794 return 0;
795}
796
797static int sym_change_count;
798static void (*conf_changed_callback)(void);
799
800void sym_set_change_count(int count)
801{
802 int _sym_change_count = sym_change_count;
803 sym_change_count = count;
804 if (conf_changed_callback &&
805 (bool)_sym_change_count != (bool)count)
806 conf_changed_callback();
807}
808
809void sym_add_change_count(int count)
810{
811 sym_set_change_count(count + sym_change_count);
812}
813
814bool conf_get_changed(void)
815{
816 return sym_change_count;
817}
818
819void conf_set_changed_callback(void (*fn)(void))
820{
821 conf_changed_callback = fn;
822}
823
824
825void conf_set_all_new_symbols(enum conf_def_mode mode)
826{
827 struct symbol *sym, *csym;
828 struct property *prop;
829 struct expr *e;
830 int i, cnt, def;
831
832 for_all_symbols(i, sym) {
833 if (sym_has_value(sym))
834 continue;
835 switch (sym_get_type(sym)) {
836 case S_BOOLEAN:
837 case S_TRISTATE:
838 switch (mode) {
839 case def_yes:
840 sym->def[S_DEF_USER].tri = yes;
841 break;
842 case def_mod:
843 sym->def[S_DEF_USER].tri = mod;
844 break;
845 case def_no:
846 sym->def[S_DEF_USER].tri = no;
847 break;
848 case def_random:
849 sym->def[S_DEF_USER].tri = (tristate)(rand() % 3);
850 break;
851 default:
852 continue;
853 }
854 if (!(sym_is_choice(sym) && mode == def_random))
855 sym->flags |= SYMBOL_DEF_USER;
856 break;
857 default:
858 break;
859 }
860
861 }
862
863 sym_clear_all_valid();
864
865 if (mode != def_random)
866 return;
867 /*
868 * We have different type of choice blocks.
869 * If curr.tri equal to mod then we can select several
870 * choice symbols in one block.
871 * In this case we do nothing.
872 * If curr.tri equal yes then only one symbol can be
873 * selected in a choice block and we set it to yes,
874 * and the rest to no.
875 */
876 for_all_symbols(i, csym) {
877 if (sym_has_value(csym) || !sym_is_choice(csym))
878 continue;
879
880 sym_calc_value(csym);
881
882 if (csym->curr.tri != yes)
883 continue;
884
885 prop = sym_get_choice_prop(csym);
886
887 /* count entries in choice block */
888 cnt = 0;
889 expr_list_for_each_sym(prop->expr, e, sym)
890 cnt++;
891
892 /*
893 * find a random value and set it to yes,
894 * set the rest to no so we have only one set
895 */
896 def = (rand() % cnt);
897
898 cnt = 0;
899 expr_list_for_each_sym(prop->expr, e, sym) {
900 if (def == cnt++) {
901 sym->def[S_DEF_USER].tri = yes;
902 csym->def[S_DEF_USER].val = sym;
903 }
904 else {
905 sym->def[S_DEF_USER].tri = no;
906 }
907 }
908 csym->flags |= SYMBOL_DEF_USER;
909 /* clear VALID to get value calculated */
910 csym->flags &= ~(SYMBOL_VALID);
911 }
912}