20230220
[build-config.git] / src / bconfig / conf.dir / confdata.c
blobd371b7e2572c17cbd060d60725f87432412bd468
1 /*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
6 #include <sys/stat.h>
7 #include <ctype.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15 #include <unistd.h>
16 #include <libgen.h>
18 #include "lkglob.h"
20 struct conf_printer {
21 void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
22 void (*print_comment)(FILE *, const char *, void *);
25 static void conf_warning(const char *fmt, ...)
26 __attribute__ ((format (printf, 1, 2)));
28 static void conf_message(const char *fmt, ...)
29 __attribute__ ((format (printf, 1, 2)));
31 static const char *conf_filename;
32 static int conf_lineno, conf_warnings;
34 const char conf_defname[] = ".defconfig";
36 static void conf_warning(const char *fmt, ...)
38 va_list ap;
39 va_start(ap, fmt);
40 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
41 vfprintf(stderr, fmt, ap);
42 fprintf(stderr, "\n");
43 va_end(ap);
44 conf_warnings++;
47 static void conf_default_message_callback(const char *fmt, va_list ap)
49 printf("#\n# ");
50 vprintf(fmt, ap);
51 printf("\n#\n");
54 static void (*conf_message_callback) (const char *fmt, va_list ap) =
55 conf_default_message_callback;
56 void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap))
58 conf_message_callback = fn;
61 static void conf_message(const char *fmt, ...)
63 va_list ap;
65 va_start(ap, fmt);
66 if (conf_message_callback)
67 conf_message_callback(fmt, ap);
68 va_end(ap);
71 const char *conf_get_configname(void)
73 char *name = getenv("BR2_CONFIG");
75 return name ? name : ".config";
78 const char *conf_get_autoconfig_name(void)
80 return getenv("KCONFIG_AUTOCONFIG");
83 static char *conf_expand_value(const char *in)
85 struct symbol *sym;
86 const char *src;
87 static char res_value[SYMBOL_MAXLENGTH];
88 char *dst, name[SYMBOL_MAXLENGTH];
90 res_value[0] = 0;
91 dst = name;
92 while ((src = strchr(in, '$'))) {
93 strncat(res_value, in, src - in);
94 src++;
95 dst = name;
96 while (isalnum(*src) || *src == '_')
97 *dst++ = *src++;
98 *dst = 0;
99 sym = sym_lookup(name, 0);
100 sym_calc_value(sym);
101 strcat(res_value, sym_get_string_value(sym));
102 in = src;
104 strcat(res_value, in);
106 return res_value;
109 char *conf_get_default_confname(void)
111 struct stat buf;
112 static char fullname[PATH_MAX+1];
113 char *env, *name;
115 name = conf_expand_value(conf_defname);
116 env = getenv(SRCTREE);
117 if (env) {
118 sprintf(fullname, "%s/%s", env, name);
119 if (!stat(fullname, &buf))
120 return fullname;
122 return name;
125 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
127 char *p2;
129 switch (sym->type) {
130 case S_TRISTATE:
131 if (p[0] == 'm') {
132 sym->def[def].tri = mod;
133 sym->flags |= def_flags;
134 break;
136 /* fall through */
137 case S_BOOLEAN:
138 if (p[0] == 'y') {
139 sym->def[def].tri = yes;
140 sym->flags |= def_flags;
141 break;
143 if (p[0] == 'n') {
144 sym->def[def].tri = no;
145 sym->flags |= def_flags;
146 break;
148 if (def != S_DEF_AUTO)
149 conf_warning("symbol value '%s' invalid for %s",
150 p, sym->name);
151 return 1;
152 case S_OTHER:
153 if (*p != '"') {
154 for (p2 = p; *p2 && !isspace(*p2); p2++)
156 sym->type = S_STRING;
157 goto done;
159 /* fall through */
160 case S_STRING:
161 if (*p++ != '"')
162 break;
163 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
164 if (*p2 == '"') {
165 *p2 = 0;
166 break;
168 memmove(p2, p2 + 1, strlen(p2));
170 if (!p2) {
171 if (def != S_DEF_AUTO)
172 conf_warning("invalid string found");
173 return 1;
175 /* fall through */
176 case S_INT:
177 case S_HEX:
178 done:
179 if (sym_string_valid(sym, p)) {
180 sym->def[def].val = xstrdup(p);
181 sym->flags |= def_flags;
182 } else {
183 if (def != S_DEF_AUTO)
184 conf_warning("symbol value '%s' invalid for %s",
185 p, sym->name);
186 return 1;
188 break;
189 default:
192 return 0;
195 #define LINE_GROWTH 16
196 static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
198 char *nline;
199 size_t new_size = slen + 1;
200 if (new_size > *n) {
201 new_size += LINE_GROWTH - 1;
202 new_size *= 2;
203 nline = xrealloc(*lineptr, new_size);
204 if (!nline)
205 return -1;
207 *lineptr = nline;
208 *n = new_size;
211 (*lineptr)[slen] = c;
213 return 0;
216 static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
218 char *line = *lineptr;
219 size_t slen = 0;
221 for (;;) {
222 int c = getc(stream);
224 switch (c) {
225 case '\n':
226 if (add_byte(c, &line, slen, n) < 0)
227 goto e_out;
228 slen++;
229 /* fall through */
230 case EOF:
231 if (add_byte('\0', &line, slen, n) < 0)
232 goto e_out;
233 *lineptr = line;
234 if (slen == 0)
235 return -1;
236 return slen;
237 default:
238 if (add_byte(c, &line, slen, n) < 0)
239 goto e_out;
240 slen++;
244 e_out:
245 line[slen-1] = '\0';
246 *lineptr = line;
247 return -1;
250 int conf_read_simple(const char *name, int def)
252 FILE *in = NULL;
253 char *line = NULL;
254 size_t line_asize = 0;
255 char *p, *p2;
256 struct symbol *sym;
257 int i, def_flags;
259 if (name) {
260 in = zconf_fopen(name);
261 } else {
262 struct property *prop;
264 name = conf_get_configname();
265 in = zconf_fopen(name);
266 if (in)
267 goto load;
268 sym_add_change_count(1);
269 if (!sym_defconfig_list)
270 return 1;
272 for_all_defaults(sym_defconfig_list, prop) {
273 if (expr_calc_value(prop->visible.expr) == no ||
274 prop->expr->type != E_SYMBOL)
275 continue;
276 name = conf_expand_value(prop->expr->left.sym->name);
277 in = zconf_fopen(name);
278 if (in) {
279 conf_message(_("using defaults found in %s"),
280 name);
281 goto load;
285 if (!in)
286 return 1;
288 load:
289 conf_filename = name;
290 conf_lineno = 0;
291 conf_warnings = 0;
293 def_flags = SYMBOL_DEF << def;
294 for_all_symbols(i, sym) {
295 sym->flags |= SYMBOL_CHANGED;
296 sym->flags &= ~(def_flags|SYMBOL_VALID);
297 if (sym_is_choice(sym))
298 sym->flags |= def_flags;
299 switch (sym->type) {
300 case S_INT:
301 case S_HEX:
302 case S_STRING:
303 if (sym->def[def].val)
304 free(sym->def[def].val);
305 /* fall through */
306 default:
307 sym->def[def].val = NULL;
308 sym->def[def].tri = no;
312 while (compat_getline(&line, &line_asize, in) != -1) {
313 conf_lineno++;
314 sym = NULL;
315 if (line[0] == '#') {
316 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
317 continue;
318 p = strchr(line + 2 + strlen(CONFIG_), ' ');
319 if (!p)
320 continue;
321 *p++ = 0;
322 if (strncmp(p, "is not set", 10))
323 continue;
324 if (def == S_DEF_USER) {
325 sym = sym_find(line + 2 + strlen(CONFIG_));
326 if (!sym) {
327 sym_add_change_count(1);
328 goto setsym;
330 } else {
331 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
332 if (sym->type == S_UNKNOWN)
333 sym->type = S_BOOLEAN;
335 if (sym->flags & def_flags) {
336 conf_warning("override: reassigning to symbol %s", sym->name);
338 switch (sym->type) {
339 case S_BOOLEAN:
340 case S_TRISTATE:
341 sym->def[def].tri = no;
342 sym->flags |= def_flags;
343 break;
344 default:
347 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
348 p = strchr(line + strlen(CONFIG_), '=');
349 if (!p)
350 continue;
351 *p++ = 0;
352 p2 = strchr(p, '\n');
353 if (p2) {
354 *p2-- = 0;
355 if (*p2 == '\r')
356 *p2 = 0;
358 if (def == S_DEF_USER) {
359 sym = sym_find(line + strlen(CONFIG_));
360 if (!sym) {
361 sym_add_change_count(1);
362 goto setsym;
364 } else {
365 sym = sym_lookup(line + strlen(CONFIG_), 0);
366 if (sym->type == S_UNKNOWN)
367 sym->type = S_OTHER;
369 if (sym->flags & def_flags) {
370 conf_warning("override: reassigning to symbol %s", sym->name);
372 if (conf_set_sym_val(sym, def, def_flags, p))
373 continue;
374 } else {
375 if (line[0] != '\r' && line[0] != '\n')
376 conf_warning("unexpected data: %.*s",
377 (int)strcspn(line, "\r\n"), line);
379 continue;
381 setsym:
382 if (sym && sym_is_choice_value(sym)) {
383 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
384 switch (sym->def[def].tri) {
385 case no:
386 break;
387 case mod:
388 if (cs->def[def].tri == yes) {
389 conf_warning("%s creates inconsistent choice state", sym->name);
390 cs->flags &= ~def_flags;
392 break;
393 case yes:
394 if (cs->def[def].tri != no)
395 conf_warning("override: %s changes choice state", sym->name);
396 cs->def[def].val = sym;
397 break;
399 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
402 free(line);
403 fclose(in);
404 return 0;
407 int conf_read(const char *name)
409 struct symbol *sym;
410 int conf_unsaved = 0;
411 int i;
413 sym_set_change_count(0);
415 if (conf_read_simple(name, S_DEF_USER)) {
416 sym_calc_value(modules_sym);
417 return 1;
420 sym_calc_value(modules_sym);
422 for_all_symbols(i, sym) {
423 sym_calc_value(sym);
424 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
425 continue;
426 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
427 /* check that calculated value agrees with saved value */
428 switch (sym->type) {
429 case S_BOOLEAN:
430 case S_TRISTATE:
431 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
432 break;
433 if (!sym_is_choice(sym))
434 continue;
435 /* fall through */
436 default:
437 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
438 continue;
439 break;
441 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
442 /* no previous value and not saved */
443 continue;
444 conf_unsaved++;
445 /* maybe print value in verbose mode... */
448 for_all_symbols(i, sym) {
449 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
450 /* Reset values of generates values, so they'll appear
451 * as new, if they should become visible, but that
452 * doesn't quite work if the Kconfig and the saved
453 * configuration disagree.
455 if (sym->visible == no && !conf_unsaved)
456 sym->flags &= ~SYMBOL_DEF_USER;
457 switch (sym->type) {
458 case S_STRING:
459 case S_INT:
460 case S_HEX:
461 /* Reset a string value if it's out of range */
462 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
463 break;
464 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
465 conf_unsaved++;
466 break;
467 default:
468 break;
473 sym_add_change_count(conf_warnings || conf_unsaved);
475 return 0;
479 * Kconfig configuration printer
481 * This printer is used when generating the resulting configuration after
482 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
483 * passing a non-NULL argument to the printer.
486 static void
487 kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
490 switch (sym->type) {
491 case S_BOOLEAN:
492 case S_TRISTATE:
493 if (*value == 'n') {
494 bool skip_unset = (arg != NULL);
496 if (!skip_unset)
497 fprintf(fp, "# %s%s is not set\n",
498 CONFIG_, sym->name);
499 return;
501 break;
502 default:
503 break;
506 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
509 static void
510 kconfig_print_comment(FILE *fp, const char *value, void *arg)
512 const char *p = value;
513 size_t l;
515 for (;;) {
516 l = strcspn(p, "\n");
517 fprintf(fp, "#");
518 if (l) {
519 fprintf(fp, " ");
520 xfwrite(p, l, 1, fp);
521 p += l;
523 fprintf(fp, "\n");
524 if (*p++ == '\0')
525 break;
529 static struct conf_printer kconfig_printer_cb =
531 .print_symbol = kconfig_print_symbol,
532 .print_comment = kconfig_print_comment,
536 * Header printer
538 * This printer is used when generating the `include/generated/autoconf.h' file.
540 static void
541 header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
544 switch (sym->type) {
545 case S_BOOLEAN:
546 case S_TRISTATE: {
547 const char *suffix = "";
549 switch (*value) {
550 case 'n':
551 break;
552 case 'm':
553 suffix = "_MODULE";
554 /* fall through */
555 default:
556 fprintf(fp, "#define %s%s%s 1\n",
557 CONFIG_, sym->name, suffix);
559 break;
561 case S_HEX: {
562 const char *prefix = "";
564 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
565 prefix = "0x";
566 fprintf(fp, "#define %s%s %s%s\n",
567 CONFIG_, sym->name, prefix, value);
568 break;
570 case S_STRING:
571 case S_INT:
572 fprintf(fp, "#define %s%s %s\n",
573 CONFIG_, sym->name, value);
574 break;
575 default:
576 break;
581 static void
582 header_print_comment(FILE *fp, const char *value, void *arg)
584 const char *p = value;
585 size_t l;
587 fprintf(fp, "/*\n");
588 for (;;) {
589 l = strcspn(p, "\n");
590 fprintf(fp, " *");
591 if (l) {
592 fprintf(fp, " ");
593 xfwrite(p, l, 1, fp);
594 p += l;
596 fprintf(fp, "\n");
597 if (*p++ == '\0')
598 break;
600 fprintf(fp, " */\n");
603 static struct conf_printer header_printer_cb =
605 .print_symbol = header_print_symbol,
606 .print_comment = header_print_comment,
610 * Tristate printer
612 * This printer is used when generating the `include/config/tristate.conf' file.
614 static void
615 tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
618 if (sym->type == S_TRISTATE && *value != 'n')
619 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
622 static struct conf_printer tristate_printer_cb =
624 .print_symbol = tristate_print_symbol,
625 .print_comment = kconfig_print_comment,
628 static void conf_write_symbol(FILE *fp, struct symbol *sym,
629 struct conf_printer *printer, void *printer_arg)
631 const char *str;
633 switch (sym->type) {
634 case S_OTHER:
635 case S_UNKNOWN:
636 break;
637 case S_STRING:
638 str = sym_get_string_value(sym);
639 str = sym_escape_string_value(str);
640 printer->print_symbol(fp, sym, str, printer_arg);
641 free((void *)str);
642 break;
643 default:
644 str = sym_get_string_value(sym);
645 printer->print_symbol(fp, sym, str, printer_arg);
649 static void
650 conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
652 char buf[256];
654 snprintf(buf, sizeof(buf),
655 "\n"
656 "Automatically generated file; DO NOT EDIT.\n"
657 "%s\n",
658 rootmenu.prompt->text);
660 printer->print_comment(fp, buf, printer_arg);
664 * Write out a minimal config.
665 * All values that has default values are skipped as this is redundant.
667 int conf_write_defconfig(const char *filename)
669 struct symbol *sym;
670 struct menu *menu;
671 FILE *out;
673 out = fopen(filename, "w");
674 if (!out)
675 return 1;
677 sym_clear_all_valid();
679 /* Traverse all menus to find all relevant symbols */
680 menu = rootmenu.list;
682 while (menu != NULL)
684 sym = menu->sym;
685 if (sym == NULL) {
686 if (!menu_is_visible(menu))
687 goto next_menu;
688 } else if (!sym_is_choice(sym)) {
689 sym_calc_value(sym);
690 if (!(sym->flags & SYMBOL_WRITE))
691 goto next_menu;
692 sym->flags &= ~SYMBOL_WRITE;
693 /* If we cannot change the symbol - skip */
694 if (!sym_is_changable(sym))
695 goto next_menu;
696 /* If symbol equals to default value - skip */
697 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
698 goto next_menu;
701 * If symbol is a choice value and equals to the
702 * default for a choice - skip.
703 * But only if value is bool and equal to "y" and
704 * choice is not "optional".
705 * (If choice is "optional" then all values can be "n")
707 if (sym_is_choice_value(sym)) {
708 struct symbol *cs;
709 struct symbol *ds;
711 cs = prop_get_symbol(sym_get_choice_prop(sym));
712 ds = sym_choice_default(cs);
713 if (!sym_is_optional(cs) && sym == ds) {
714 if ((sym->type == S_BOOLEAN) &&
715 sym_get_tristate_value(sym) == yes)
716 goto next_menu;
719 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
721 next_menu:
722 if (menu->list != NULL) {
723 menu = menu->list;
725 else if (menu->next != NULL) {
726 menu = menu->next;
727 } else {
728 while ((menu = menu->parent)) {
729 if (menu->next != NULL) {
730 menu = menu->next;
731 break;
736 fclose(out);
737 return 0;
740 int conf_write(const char *name)
742 FILE *out;
743 struct symbol *sym;
744 struct menu *menu;
745 const char *basename;
746 const char *str;
747 char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
748 char *env;
750 if (!name)
751 name = conf_get_configname();
753 dirname[0] = 0;
754 if (name && name[0]) {
755 struct stat st;
756 char *slash;
758 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
759 strcpy(dirname, name);
760 strcat(dirname, "/");
761 basename = conf_get_configname();
762 } else if ((slash = strrchr(name, '/'))) {
763 int size = slash - name + 1;
764 memcpy(dirname, name, size);
765 dirname[size] = 0;
766 if (slash[1])
767 basename = slash + 1;
768 else
769 basename = conf_get_configname();
770 } else
771 basename = name;
772 } else
773 basename = conf_get_configname();
775 sprintf(newname, "%s%s", dirname, basename);
776 env = getenv("KCONFIG_OVERWRITECONFIG");
777 if (!env || !*env) {
778 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
779 out = fopen(tmpname, "w");
780 } else {
781 *tmpname = 0;
782 out = fopen(newname, "w");
784 if (!out)
785 return 1;
787 conf_write_heading(out, &kconfig_printer_cb, NULL);
789 if (!conf_get_changed())
790 sym_clear_all_valid();
792 menu = rootmenu.list;
793 while (menu) {
794 sym = menu->sym;
795 if (!sym) {
796 if (!menu_is_visible(menu))
797 goto next;
798 str = menu_get_prompt(menu);
799 fprintf(out, "\n"
800 "#\n"
801 "# %s\n"
802 "#\n", str);
803 } else if (!(sym->flags & SYMBOL_CHOICE)) {
804 sym_calc_value(sym);
805 if (!(sym->flags & SYMBOL_WRITE))
806 goto next;
807 sym->flags &= ~SYMBOL_WRITE;
809 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
812 next:
813 if (menu->list) {
814 menu = menu->list;
815 continue;
817 if (menu->next)
818 menu = menu->next;
819 else while ((menu = menu->parent)) {
820 if (menu->next) {
821 menu = menu->next;
822 break;
826 fclose(out);
828 if (*tmpname) {
829 strcat(dirname, basename);
830 strcat(dirname, ".old");
831 rename(newname, dirname);
832 if (rename(tmpname, newname))
833 return 1;
836 conf_message(_("configuration written to %s"), newname);
838 sym_set_change_count(0);
840 return 0;
843 static int conf_split_config(void)
845 const char *name;
846 char path[PATH_MAX+1];
847 char *opwd, *dir, *_name;
848 char *s, *d, c;
849 struct symbol *sym;
850 struct stat sb;
851 int res, i, fd;
853 name = conf_get_autoconfig_name();
854 conf_read_simple(name, S_DEF_AUTO);
855 sym_calc_value(modules_sym);
857 opwd = malloc(256);
858 _name = strdup(name);
859 if (opwd == NULL || _name == NULL)
860 return 1;
861 opwd = getcwd(opwd, 256);
862 dir = dirname(_name);
863 if (dir == NULL) {
864 res = 1;
865 goto err;
867 if (chdir(dir)) {
868 res = 1;
869 goto err;
872 res = 0;
873 for_all_symbols(i, sym) {
874 sym_calc_value(sym);
875 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
876 continue;
877 if (sym->flags & SYMBOL_WRITE) {
878 if (sym->flags & SYMBOL_DEF_AUTO) {
880 * symbol has old and new value,
881 * so compare them...
883 switch (sym->type) {
884 case S_BOOLEAN:
885 case S_TRISTATE:
886 if (sym_get_tristate_value(sym) ==
887 sym->def[S_DEF_AUTO].tri)
888 continue;
889 break;
890 case S_STRING:
891 case S_HEX:
892 case S_INT:
893 if (!strcmp(sym_get_string_value(sym),
894 sym->def[S_DEF_AUTO].val))
895 continue;
896 break;
897 default:
898 break;
900 } else {
902 * If there is no old value, only 'no' (unset)
903 * is allowed as new value.
905 switch (sym->type) {
906 case S_BOOLEAN:
907 case S_TRISTATE:
908 if (sym_get_tristate_value(sym) == no)
909 continue;
910 break;
911 default:
912 break;
915 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
916 /* There is neither an old nor a new value. */
917 continue;
918 /* else
919 * There is an old value, but no new value ('no' (unset)
920 * isn't saved in auto.conf, so the old value is always
921 * different from 'no').
924 /* Replace all '_' and append ".h" */
925 s = sym->name;
926 d = path;
927 while ((c = *s++)) {
928 c = tolower(c);
929 *d++ = (c == '_') ? '/' : c;
931 strcpy(d, ".h");
933 /* Assume directory path already exists. */
934 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
935 if (fd == -1) {
936 if (errno != ENOENT) {
937 res = 1;
938 break;
941 * Create directory components,
942 * unless they exist already.
944 d = path;
945 while ((d = strchr(d, '/'))) {
946 *d = 0;
947 if (stat(path, &sb) && mkdir(path, 0755)) {
948 res = 1;
949 goto out;
951 *d++ = '/';
953 /* Try it again. */
954 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
955 if (fd == -1) {
956 res = 1;
957 break;
960 close(fd);
962 out:
963 if (chdir(opwd))
964 res = 1;
965 err:
966 free(opwd);
967 free(_name);
968 return res;
971 int conf_write_autoconf(void)
973 struct symbol *sym;
974 const char *name;
975 FILE *out, *tristate, *out_h;
976 int i;
977 char dir[PATH_MAX+1], buf[PATH_MAX+1];
978 char *s;
980 strcpy(dir, conf_get_configname());
981 s = strrchr(dir, '/');
982 if (s)
983 s[1] = 0;
984 else
985 dir[0] = 0;
987 sym_clear_all_valid();
989 sprintf(buf, "%s.config.cmd", dir);
990 file_write_dep(buf);
992 if (conf_split_config())
993 return 1;
995 sprintf(buf, "%s.tmpconfig", dir);
996 out = fopen(buf, "w");
997 if (!out)
998 return 1;
1000 sprintf(buf, "%s.tmpconfig_tristate", dir);
1001 tristate = fopen(buf, "w");
1002 if (!tristate) {
1003 fclose(out);
1004 return 1;
1007 sprintf(buf, "%s.tmpconfig.h", dir);
1008 out_h = fopen(buf, "w");
1009 if (!out_h) {
1010 fclose(out);
1011 fclose(tristate);
1012 return 1;
1015 conf_write_heading(out, &kconfig_printer_cb, NULL);
1017 conf_write_heading(tristate, &tristate_printer_cb, NULL);
1019 conf_write_heading(out_h, &header_printer_cb, NULL);
1021 for_all_symbols(i, sym) {
1022 sym_calc_value(sym);
1023 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
1024 continue;
1026 /* write symbol to auto.conf, tristate and header files */
1027 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
1029 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1031 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
1033 fclose(out);
1034 fclose(tristate);
1035 fclose(out_h);
1037 name = getenv("KCONFIG_AUTOHEADER");
1038 if (!name)
1039 name = "include/generated/autoconf.h";
1040 sprintf(buf, "%s.tmpconfig.h", dir);
1041 if (rename(buf, name))
1042 return 1;
1043 name = getenv("KCONFIG_TRISTATE");
1044 if (!name)
1045 name = "include/config/tristate.conf";
1046 sprintf(buf, "%s.tmpconfig_tristate", dir);
1047 if (rename(buf, name))
1048 return 1;
1049 name = conf_get_autoconfig_name();
1051 * This must be the last step, kbuild has a dependency on auto.conf
1052 * and this marks the successful completion of the previous steps.
1054 sprintf(buf, "%s.tmpconfig", dir);
1055 if (rename(buf, name))
1056 return 1;
1058 return 0;
1061 static int sym_change_count;
1062 static void (*conf_changed_callback)(void);
1064 void sym_set_change_count(int count)
1066 int _sym_change_count = sym_change_count;
1067 sym_change_count = count;
1068 if (conf_changed_callback &&
1069 (bool)_sym_change_count != (bool)count)
1070 conf_changed_callback();
1073 void sym_add_change_count(int count)
1075 sym_set_change_count(count + sym_change_count);
1078 bool conf_get_changed(void)
1080 return sym_change_count;
1083 void conf_set_changed_callback(void (*fn)(void))
1085 conf_changed_callback = fn;
1088 static bool randomize_choice_values(struct symbol *csym)
1090 struct property *prop;
1091 struct symbol *sym;
1092 struct expr *e;
1093 int cnt, def;
1096 * If choice is mod then we may have more items selected
1097 * and if no then no-one.
1098 * In both cases stop.
1100 if (csym->curr.tri != yes)
1101 return false;
1103 prop = sym_get_choice_prop(csym);
1105 /* count entries in choice block */
1106 cnt = 0;
1107 expr_list_for_each_sym(prop->expr, e, sym)
1108 cnt++;
1111 * find a random value and set it to yes,
1112 * set the rest to no so we have only one set
1114 def = (rand() % cnt);
1116 cnt = 0;
1117 expr_list_for_each_sym(prop->expr, e, sym) {
1118 if (def == cnt++) {
1119 sym->def[S_DEF_USER].tri = yes;
1120 csym->def[S_DEF_USER].val = sym;
1122 else {
1123 sym->def[S_DEF_USER].tri = no;
1125 sym->flags |= SYMBOL_DEF_USER;
1126 /* clear VALID to get value calculated */
1127 sym->flags &= ~SYMBOL_VALID;
1129 csym->flags |= SYMBOL_DEF_USER;
1130 /* clear VALID to get value calculated */
1131 csym->flags &= ~(SYMBOL_VALID);
1133 return true;
1136 void set_all_choice_values(struct symbol *csym)
1138 struct property *prop;
1139 struct symbol *sym;
1140 struct expr *e;
1142 prop = sym_get_choice_prop(csym);
1145 * Set all non-assinged choice values to no
1147 expr_list_for_each_sym(prop->expr, e, sym) {
1148 if (!sym_has_value(sym))
1149 sym->def[S_DEF_USER].tri = no;
1151 csym->flags |= SYMBOL_DEF_USER;
1152 /* clear VALID to get value calculated */
1153 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1156 bool conf_set_all_new_symbols(enum conf_def_mode mode)
1158 struct symbol *sym, *csym;
1159 int i, cnt, pby, pty, ptm; /* pby: probability of bool = y
1160 * pty: probability of tristate = y
1161 * ptm: probability of tristate = m
1164 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1165 * below, otherwise gcc whines about
1166 * -Wmaybe-uninitialized */
1167 if (mode == def_random) {
1168 int n, p[3];
1169 char *env = getenv("KCONFIG_PROBABILITY");
1170 n = 0;
1171 while( env && *env ) {
1172 char *endp;
1173 int tmp = strtol( env, &endp, 10 );
1174 if( tmp >= 0 && tmp <= 100 ) {
1175 p[n++] = tmp;
1176 } else {
1177 errno = ERANGE;
1178 perror( "KCONFIG_PROBABILITY" );
1179 exit( 1 );
1181 env = (*endp == ':') ? endp+1 : endp;
1182 if( n >=3 ) {
1183 break;
1186 switch( n ) {
1187 case 1:
1188 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1189 break;
1190 case 2:
1191 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1192 break;
1193 case 3:
1194 pby = p[0]; pty = p[1]; ptm = p[2];
1195 break;
1198 if( pty+ptm > 100 ) {
1199 errno = ERANGE;
1200 perror( "KCONFIG_PROBABILITY" );
1201 exit( 1 );
1204 bool has_changed = false;
1206 for_all_symbols(i, sym) {
1207 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1208 continue;
1209 switch (sym_get_type(sym)) {
1210 case S_BOOLEAN:
1211 case S_TRISTATE:
1212 has_changed = true;
1213 switch (mode) {
1214 case def_yes:
1215 sym->def[S_DEF_USER].tri = yes;
1216 break;
1217 case def_mod:
1218 sym->def[S_DEF_USER].tri = mod;
1219 break;
1220 case def_no:
1221 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1222 sym->def[S_DEF_USER].tri = yes;
1223 else
1224 sym->def[S_DEF_USER].tri = no;
1225 break;
1226 case def_random:
1227 sym->def[S_DEF_USER].tri = no;
1228 cnt = rand() % 100;
1229 if (sym->type == S_TRISTATE) {
1230 if (cnt < pty)
1231 sym->def[S_DEF_USER].tri = yes;
1232 else if (cnt < (pty+ptm))
1233 sym->def[S_DEF_USER].tri = mod;
1234 } else if (cnt < pby)
1235 sym->def[S_DEF_USER].tri = yes;
1236 break;
1237 default:
1238 continue;
1240 if (!(sym_is_choice(sym) && mode == def_random))
1241 sym->flags |= SYMBOL_DEF_USER;
1242 break;
1243 default:
1244 break;
1249 sym_clear_all_valid();
1252 * We have different type of choice blocks.
1253 * If curr.tri equals to mod then we can select several
1254 * choice symbols in one block.
1255 * In this case we do nothing.
1256 * If curr.tri equals yes then only one symbol can be
1257 * selected in a choice block and we set it to yes,
1258 * and the rest to no.
1260 if (mode != def_random) {
1261 for_all_symbols(i, csym) {
1262 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1263 sym_is_choice_value(csym))
1264 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1268 for_all_symbols(i, csym) {
1269 if (sym_has_value(csym) || !sym_is_choice(csym))
1270 continue;
1272 sym_calc_value(csym);
1273 if (mode == def_random)
1274 has_changed = randomize_choice_values(csym);
1275 else {
1276 set_all_choice_values(csym);
1277 has_changed = true;
1281 return has_changed;