python-pathvalidate: bump version to 0.14.1
[buildroot-gz.git] / support / kconfig / confdata.c
blob2371fa81794645866d867aac6f15d8b9c6a637c3
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 "lkc.h"
20 static void conf_warning(const char *fmt, ...)
21 __attribute__ ((format (printf, 1, 2)));
23 static void conf_message(const char *fmt, ...)
24 __attribute__ ((format (printf, 1, 2)));
26 static const char *conf_filename;
27 static int conf_lineno, conf_warnings, conf_unsaved;
29 const char conf_defname[] = ".defconfig";
31 static void conf_warning(const char *fmt, ...)
33 va_list ap;
34 va_start(ap, fmt);
35 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
36 vfprintf(stderr, fmt, ap);
37 fprintf(stderr, "\n");
38 va_end(ap);
39 conf_warnings++;
42 static void conf_default_message_callback(const char *fmt, va_list ap)
44 printf("#\n# ");
45 vprintf(fmt, ap);
46 printf("\n#\n");
49 static void (*conf_message_callback) (const char *fmt, va_list ap) =
50 conf_default_message_callback;
51 void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap))
53 conf_message_callback = fn;
56 static void conf_message(const char *fmt, ...)
58 va_list ap;
60 va_start(ap, fmt);
61 if (conf_message_callback)
62 conf_message_callback(fmt, ap);
65 const char *conf_get_configname(void)
67 char *name = getenv("BR2_CONFIG");
69 return name ? name : ".config";
72 const char *conf_get_autoconfig_name(void)
74 return getenv("KCONFIG_AUTOCONFIG");
77 static char *conf_expand_value(const char *in)
79 struct symbol *sym;
80 const char *src;
81 static char res_value[SYMBOL_MAXLENGTH];
82 char *dst, name[SYMBOL_MAXLENGTH];
84 res_value[0] = 0;
85 dst = name;
86 while ((src = strchr(in, '$'))) {
87 strncat(res_value, in, src - in);
88 src++;
89 dst = name;
90 while (isalnum(*src) || *src == '_')
91 *dst++ = *src++;
92 *dst = 0;
93 sym = sym_lookup(name, 0);
94 sym_calc_value(sym);
95 strcat(res_value, sym_get_string_value(sym));
96 in = src;
98 strcat(res_value, in);
100 return res_value;
103 char *conf_get_default_confname(void)
105 struct stat buf;
106 static char fullname[PATH_MAX+1];
107 char *env, *name;
109 name = conf_expand_value(conf_defname);
110 env = getenv(SRCTREE);
111 if (env) {
112 sprintf(fullname, "%s/%s", env, name);
113 if (!stat(fullname, &buf))
114 return fullname;
116 return name;
119 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
121 char *p2;
123 switch (sym->type) {
124 case S_TRISTATE:
125 if (p[0] == 'm') {
126 sym->def[def].tri = mod;
127 sym->flags |= def_flags;
128 break;
130 /* fall through */
131 case S_BOOLEAN:
132 if (p[0] == 'y') {
133 sym->def[def].tri = yes;
134 sym->flags |= def_flags;
135 break;
137 if (p[0] == 'n') {
138 sym->def[def].tri = no;
139 sym->flags |= def_flags;
140 break;
142 if (def != S_DEF_AUTO)
143 conf_warning("symbol value '%s' invalid for %s",
144 p, sym->name);
145 return 1;
146 case S_OTHER:
147 if (*p != '"') {
148 for (p2 = p; *p2 && !isspace(*p2); p2++)
150 sym->type = S_STRING;
151 goto done;
153 /* fall through */
154 case S_STRING:
155 if (*p++ != '"')
156 break;
157 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
158 if (*p2 == '"') {
159 *p2 = 0;
160 break;
162 memmove(p2, p2 + 1, strlen(p2));
164 if (!p2) {
165 if (def != S_DEF_AUTO)
166 conf_warning("invalid string found");
167 return 1;
169 /* fall through */
170 case S_INT:
171 case S_HEX:
172 done:
173 if (sym_string_valid(sym, p)) {
174 sym->def[def].val = strdup(p);
175 sym->flags |= def_flags;
176 } else {
177 if (def != S_DEF_AUTO)
178 conf_warning("symbol value '%s' invalid for %s",
179 p, sym->name);
180 return 1;
182 break;
183 default:
186 return 0;
189 #define LINE_GROWTH 16
190 static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
192 char *nline;
193 size_t new_size = slen + 1;
194 if (new_size > *n) {
195 new_size += LINE_GROWTH - 1;
196 new_size *= 2;
197 nline = realloc(*lineptr, new_size);
198 if (!nline)
199 return -1;
201 *lineptr = nline;
202 *n = new_size;
205 (*lineptr)[slen] = c;
207 return 0;
210 static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
212 char *line = *lineptr;
213 size_t slen = 0;
215 for (;;) {
216 int c = getc(stream);
218 switch (c) {
219 case '\n':
220 if (add_byte(c, &line, slen, n) < 0)
221 goto e_out;
222 slen++;
223 /* fall through */
224 case EOF:
225 if (add_byte('\0', &line, slen, n) < 0)
226 goto e_out;
227 *lineptr = line;
228 if (slen == 0)
229 return -1;
230 return slen;
231 default:
232 if (add_byte(c, &line, slen, n) < 0)
233 goto e_out;
234 slen++;
238 e_out:
239 line[slen-1] = '\0';
240 *lineptr = line;
241 return -1;
244 int conf_read_simple(const char *name, int def)
246 FILE *in = NULL;
247 char *line = NULL;
248 size_t line_asize = 0;
249 char *p, *p2;
250 struct symbol *sym;
251 int i, def_flags;
253 if (name) {
254 in = zconf_fopen(name);
255 } else {
256 struct property *prop;
258 name = conf_get_configname();
259 in = zconf_fopen(name);
260 if (in)
261 goto load;
262 sym_add_change_count(1);
263 if (!sym_defconfig_list) {
264 if (modules_sym)
265 sym_calc_value(modules_sym);
266 return 1;
269 for_all_defaults(sym_defconfig_list, prop) {
270 if (expr_calc_value(prop->visible.expr) == no ||
271 prop->expr->type != E_SYMBOL)
272 continue;
273 name = conf_expand_value(prop->expr->left.sym->name);
274 in = zconf_fopen(name);
275 if (in) {
276 conf_message(_("using defaults found in %s"),
277 name);
278 goto load;
282 if (!in)
283 return 1;
285 load:
286 conf_filename = name;
287 conf_lineno = 0;
288 conf_warnings = 0;
289 conf_unsaved = 0;
291 def_flags = SYMBOL_DEF << def;
292 for_all_symbols(i, sym) {
293 sym->flags |= SYMBOL_CHANGED;
294 sym->flags &= ~(def_flags|SYMBOL_VALID);
295 if (sym_is_choice(sym))
296 sym->flags |= def_flags;
297 switch (sym->type) {
298 case S_INT:
299 case S_HEX:
300 case S_STRING:
301 if (sym->def[def].val)
302 free(sym->def[def].val);
303 /* fall through */
304 default:
305 sym->def[def].val = NULL;
306 sym->def[def].tri = no;
310 while (compat_getline(&line, &line_asize, in) != -1) {
311 conf_lineno++;
312 sym = NULL;
313 if (line[0] == '#') {
314 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
315 continue;
316 p = strchr(line + 2 + strlen(CONFIG_), ' ');
317 if (!p)
318 continue;
319 *p++ = 0;
320 if (strncmp(p, "is not set", 10))
321 continue;
322 if (def == S_DEF_USER) {
323 sym = sym_find(line + 2 + strlen(CONFIG_));
324 if (!sym) {
325 sym_add_change_count(1);
326 goto setsym;
328 } else {
329 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
330 if (sym->type == S_UNKNOWN)
331 sym->type = S_BOOLEAN;
333 if (sym->flags & def_flags) {
334 conf_warning("override: reassigning to symbol %s", sym->name);
336 switch (sym->type) {
337 case S_BOOLEAN:
338 case S_TRISTATE:
339 sym->def[def].tri = no;
340 sym->flags |= def_flags;
341 break;
342 default:
345 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
346 p = strchr(line + strlen(CONFIG_), '=');
347 if (!p)
348 continue;
349 *p++ = 0;
350 p2 = strchr(p, '\n');
351 if (p2) {
352 *p2-- = 0;
353 if (*p2 == '\r')
354 *p2 = 0;
356 if (def == S_DEF_USER) {
357 sym = sym_find(line + strlen(CONFIG_));
358 if (!sym) {
359 sym_add_change_count(1);
360 goto setsym;
362 } else {
363 sym = sym_lookup(line + strlen(CONFIG_), 0);
364 if (sym->type == S_UNKNOWN)
365 sym->type = S_OTHER;
367 if (sym->flags & def_flags) {
368 conf_warning("override: reassigning to symbol %s", sym->name);
370 if (conf_set_sym_val(sym, def, def_flags, p))
371 continue;
372 } else {
373 if (line[0] != '\r' && line[0] != '\n')
374 conf_warning("unexpected data");
375 continue;
377 setsym:
378 if (sym && sym_is_choice_value(sym)) {
379 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
380 switch (sym->def[def].tri) {
381 case no:
382 break;
383 case mod:
384 if (cs->def[def].tri == yes) {
385 conf_warning("%s creates inconsistent choice state", sym->name);
386 cs->flags &= ~def_flags;
388 break;
389 case yes:
390 if (cs->def[def].tri != no)
391 conf_warning("override: %s changes choice state", sym->name);
392 cs->def[def].val = sym;
393 break;
395 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
398 free(line);
399 fclose(in);
401 if (modules_sym)
402 sym_calc_value(modules_sym);
403 return 0;
406 int conf_read(const char *name)
408 struct symbol *sym;
409 int i;
411 sym_set_change_count(0);
413 if (conf_read_simple(name, S_DEF_USER))
414 return 1;
416 for_all_symbols(i, sym) {
417 sym_calc_value(sym);
418 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
419 continue;
420 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
421 /* check that calculated value agrees with saved value */
422 switch (sym->type) {
423 case S_BOOLEAN:
424 case S_TRISTATE:
425 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
426 break;
427 if (!sym_is_choice(sym))
428 continue;
429 /* fall through */
430 default:
431 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
432 continue;
433 break;
435 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
436 /* no previous value and not saved */
437 continue;
438 conf_unsaved++;
439 /* maybe print value in verbose mode... */
442 for_all_symbols(i, sym) {
443 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
444 /* Reset values of generates values, so they'll appear
445 * as new, if they should become visible, but that
446 * doesn't quite work if the Kconfig and the saved
447 * configuration disagree.
449 if (sym->visible == no && !conf_unsaved)
450 sym->flags &= ~SYMBOL_DEF_USER;
451 switch (sym->type) {
452 case S_STRING:
453 case S_INT:
454 case S_HEX:
455 /* Reset a string value if it's out of range */
456 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
457 break;
458 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
459 conf_unsaved++;
460 break;
461 default:
462 break;
467 sym_add_change_count(conf_warnings || conf_unsaved);
469 return 0;
473 * Kconfig configuration printer
475 * This printer is used when generating the resulting configuration after
476 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
477 * passing a non-NULL argument to the printer.
480 static void
481 kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
484 switch (sym->type) {
485 case S_BOOLEAN:
486 case S_TRISTATE:
487 if (*value == 'n') {
488 bool skip_unset = (arg != NULL);
490 if (!skip_unset)
491 fprintf(fp, "# %s%s is not set\n",
492 CONFIG_, sym->name);
493 return;
495 break;
496 default:
497 break;
500 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
503 static void
504 kconfig_print_comment(FILE *fp, const char *value, void *arg)
506 const char *p = value;
507 size_t l;
509 for (;;) {
510 l = strcspn(p, "\n");
511 fprintf(fp, "#");
512 if (l) {
513 fprintf(fp, " ");
514 xfwrite(p, l, 1, fp);
515 p += l;
517 fprintf(fp, "\n");
518 if (*p++ == '\0')
519 break;
523 static struct conf_printer kconfig_printer_cb =
525 .print_symbol = kconfig_print_symbol,
526 .print_comment = kconfig_print_comment,
530 * Header printer
532 * This printer is used when generating the `include/generated/autoconf.h' file.
534 static void
535 header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
538 switch (sym->type) {
539 case S_BOOLEAN:
540 case S_TRISTATE: {
541 const char *suffix = "";
543 switch (*value) {
544 case 'n':
545 break;
546 case 'm':
547 suffix = "_MODULE";
548 /* fall through */
549 default:
550 fprintf(fp, "#define %s%s%s 1\n",
551 CONFIG_, sym->name, suffix);
553 break;
555 case S_HEX: {
556 const char *prefix = "";
558 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
559 prefix = "0x";
560 fprintf(fp, "#define %s%s %s%s\n",
561 CONFIG_, sym->name, prefix, value);
562 break;
564 case S_STRING:
565 case S_INT:
566 fprintf(fp, "#define %s%s %s\n",
567 CONFIG_, sym->name, value);
568 break;
569 default:
570 break;
575 static void
576 header_print_comment(FILE *fp, const char *value, void *arg)
578 const char *p = value;
579 size_t l;
581 fprintf(fp, "/*\n");
582 for (;;) {
583 l = strcspn(p, "\n");
584 fprintf(fp, " *");
585 if (l) {
586 fprintf(fp, " ");
587 xfwrite(p, l, 1, fp);
588 p += l;
590 fprintf(fp, "\n");
591 if (*p++ == '\0')
592 break;
594 fprintf(fp, " */\n");
597 static struct conf_printer header_printer_cb =
599 .print_symbol = header_print_symbol,
600 .print_comment = header_print_comment,
604 * Tristate printer
606 * This printer is used when generating the `include/config/tristate.conf' file.
608 static void
609 tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
612 if (sym->type == S_TRISTATE && *value != 'n')
613 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
616 static struct conf_printer tristate_printer_cb =
618 .print_symbol = tristate_print_symbol,
619 .print_comment = kconfig_print_comment,
622 static void conf_write_symbol(FILE *fp, struct symbol *sym,
623 struct conf_printer *printer, void *printer_arg)
625 const char *str;
627 switch (sym->type) {
628 case S_OTHER:
629 case S_UNKNOWN:
630 break;
631 case S_STRING:
632 str = sym_get_string_value(sym);
633 str = sym_escape_string_value(str);
634 printer->print_symbol(fp, sym, str, printer_arg);
635 free((void *)str);
636 break;
637 default:
638 str = sym_get_string_value(sym);
639 printer->print_symbol(fp, sym, str, printer_arg);
643 static void
644 conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
646 char buf[256];
648 snprintf(buf, sizeof(buf),
649 "\n"
650 "Automatically generated file; DO NOT EDIT.\n"
651 "%s\n",
652 rootmenu.prompt->text);
654 printer->print_comment(fp, buf, printer_arg);
658 * Write out a minimal config.
659 * All values that has default values are skipped as this is redundant.
661 int conf_write_defconfig(const char *filename)
663 struct symbol *sym;
664 struct menu *menu;
665 FILE *out;
667 out = fopen(filename, "w");
668 if (!out)
669 return 1;
671 sym_clear_all_valid();
673 /* Traverse all menus to find all relevant symbols */
674 menu = rootmenu.list;
676 while (menu != NULL)
678 sym = menu->sym;
679 if (sym == NULL) {
680 if (!menu_is_visible(menu))
681 goto next_menu;
682 } else if (!sym_is_choice(sym)) {
683 sym_calc_value(sym);
684 if (!(sym->flags & SYMBOL_WRITE))
685 goto next_menu;
686 sym->flags &= ~SYMBOL_WRITE;
687 /* If we cannot change the symbol - skip */
688 if (!sym_is_changable(sym))
689 goto next_menu;
690 /* If symbol equals to default value - skip */
691 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
692 goto next_menu;
695 * If symbol is a choice value and equals to the
696 * default for a choice - skip.
697 * But only if value is bool and equal to "y" and
698 * choice is not "optional".
699 * (If choice is "optional" then all values can be "n")
701 if (sym_is_choice_value(sym)) {
702 struct symbol *cs;
703 struct symbol *ds;
705 cs = prop_get_symbol(sym_get_choice_prop(sym));
706 ds = sym_choice_default(cs);
707 if (!sym_is_optional(cs) && sym == ds) {
708 if ((sym->type == S_BOOLEAN) &&
709 sym_get_tristate_value(sym) == yes)
710 goto next_menu;
713 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
715 next_menu:
716 if (menu->list != NULL) {
717 menu = menu->list;
719 else if (menu->next != NULL) {
720 menu = menu->next;
721 } else {
722 while ((menu = menu->parent)) {
723 if (menu->next != NULL) {
724 menu = menu->next;
725 break;
730 fclose(out);
731 return 0;
734 int conf_write(const char *name)
736 FILE *out;
737 struct symbol *sym;
738 struct menu *menu;
739 const char *basename;
740 const char *str;
741 char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
742 char *env;
744 if (!name)
745 name = conf_get_configname();
747 dirname[0] = 0;
748 if (name && name[0]) {
749 struct stat st;
750 char *slash;
752 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
753 strcpy(dirname, name);
754 strcat(dirname, "/");
755 basename = conf_get_configname();
756 } else if ((slash = strrchr(name, '/'))) {
757 int size = slash - name + 1;
758 memcpy(dirname, name, size);
759 dirname[size] = 0;
760 if (slash[1])
761 basename = slash + 1;
762 else
763 basename = conf_get_configname();
764 } else
765 basename = name;
766 } else
767 basename = conf_get_configname();
769 sprintf(newname, "%s%s", dirname, basename);
770 env = getenv("KCONFIG_OVERWRITECONFIG");
771 if (!env || !*env) {
772 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
773 out = fopen(tmpname, "w");
774 } else {
775 *tmpname = 0;
776 out = fopen(newname, "w");
778 if (!out)
779 return 1;
781 conf_write_heading(out, &kconfig_printer_cb, NULL);
783 if (!conf_get_changed())
784 sym_clear_all_valid();
786 menu = rootmenu.list;
787 while (menu) {
788 sym = menu->sym;
789 if (!sym) {
790 if (!menu_is_visible(menu))
791 goto next;
792 str = menu_get_prompt(menu);
793 fprintf(out, "\n"
794 "#\n"
795 "# %s\n"
796 "#\n", str);
797 } else if (!(sym->flags & SYMBOL_CHOICE)) {
798 sym_calc_value(sym);
799 if (!(sym->flags & SYMBOL_WRITE))
800 goto next;
801 sym->flags &= ~SYMBOL_WRITE;
803 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
806 next:
807 if (menu->list) {
808 menu = menu->list;
809 continue;
811 if (menu->next)
812 menu = menu->next;
813 else while ((menu = menu->parent)) {
814 if (menu->next) {
815 menu = menu->next;
816 break;
820 fclose(out);
822 if (*tmpname) {
823 strcat(dirname, basename);
824 strcat(dirname, ".old");
825 rename(newname, dirname);
826 if (rename(tmpname, newname))
827 return 1;
830 conf_message(_("configuration written to %s"), newname);
832 sym_set_change_count(0);
834 return 0;
837 static int conf_split_config(void)
839 const char *name;
840 char path[PATH_MAX+1];
841 char *opwd, *dir, *_name;
842 char *s, *d, c;
843 struct symbol *sym;
844 struct stat sb;
845 int res, i, fd;
847 name = conf_get_autoconfig_name();
848 conf_read_simple(name, S_DEF_AUTO);
850 opwd = malloc(256);
851 _name = strdup(name);
852 if (opwd == NULL || _name == NULL)
853 return 1;
854 opwd = getcwd(opwd, 256);
855 dir = dirname(_name);
856 if (dir == NULL) {
857 res = 1;
858 goto err;
860 if (chdir(dir)) {
861 res = 1;
862 goto err;
865 res = 0;
866 for_all_symbols(i, sym) {
867 sym_calc_value(sym);
868 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
869 continue;
870 if (sym->flags & SYMBOL_WRITE) {
871 if (sym->flags & SYMBOL_DEF_AUTO) {
873 * symbol has old and new value,
874 * so compare them...
876 switch (sym->type) {
877 case S_BOOLEAN:
878 case S_TRISTATE:
879 if (sym_get_tristate_value(sym) ==
880 sym->def[S_DEF_AUTO].tri)
881 continue;
882 break;
883 case S_STRING:
884 case S_HEX:
885 case S_INT:
886 if (!strcmp(sym_get_string_value(sym),
887 sym->def[S_DEF_AUTO].val))
888 continue;
889 break;
890 default:
891 break;
893 } else {
895 * If there is no old value, only 'no' (unset)
896 * is allowed as new value.
898 switch (sym->type) {
899 case S_BOOLEAN:
900 case S_TRISTATE:
901 if (sym_get_tristate_value(sym) == no)
902 continue;
903 break;
904 default:
905 break;
908 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
909 /* There is neither an old nor a new value. */
910 continue;
911 /* else
912 * There is an old value, but no new value ('no' (unset)
913 * isn't saved in auto.conf, so the old value is always
914 * different from 'no').
917 /* Replace all '_' and append ".h" */
918 s = sym->name;
919 d = path;
920 while ((c = *s++)) {
921 c = tolower(c);
922 *d++ = (c == '_') ? '/' : c;
924 strcpy(d, ".h");
926 /* Assume directory path already exists. */
927 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
928 if (fd == -1) {
929 if (errno != ENOENT) {
930 res = 1;
931 break;
934 * Create directory components,
935 * unless they exist already.
937 d = path;
938 while ((d = strchr(d, '/'))) {
939 *d = 0;
940 if (stat(path, &sb) && mkdir(path, 0755)) {
941 res = 1;
942 goto out;
944 *d++ = '/';
946 /* Try it again. */
947 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
948 if (fd == -1) {
949 res = 1;
950 break;
953 close(fd);
955 out:
956 if (chdir(opwd))
957 res = 1;
958 err:
959 free(opwd);
960 free(_name);
961 return res;
964 int conf_write_autoconf(void)
966 struct symbol *sym;
967 const char *name;
968 FILE *out, *tristate, *out_h;
969 int i;
970 char dir[PATH_MAX+1], buf[PATH_MAX+1];
971 char *s;
973 strcpy(dir, conf_get_configname());
974 s = strrchr(dir, '/');
975 if (s)
976 s[1] = 0;
977 else
978 dir[0] = 0;
980 sym_clear_all_valid();
982 sprintf(buf, "%s.config.cmd", dir);
983 file_write_dep(buf);
985 if (conf_split_config())
986 return 1;
988 sprintf(buf, "%s.tmpconfig", dir);
989 out = fopen(buf, "w");
990 if (!out)
991 return 1;
993 sprintf(buf, "%s.tmpconfig_tristate", dir);
994 tristate = fopen(buf, "w");
995 if (!tristate) {
996 fclose(out);
997 return 1;
1000 sprintf(buf, "%s.tmpconfig.h", dir);
1001 out_h = fopen(buf, "w");
1002 if (!out_h) {
1003 fclose(out);
1004 fclose(tristate);
1005 return 1;
1008 conf_write_heading(out, &kconfig_printer_cb, NULL);
1010 conf_write_heading(tristate, &tristate_printer_cb, NULL);
1012 conf_write_heading(out_h, &header_printer_cb, NULL);
1014 for_all_symbols(i, sym) {
1015 sym_calc_value(sym);
1016 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
1017 continue;
1019 /* write symbol to auto.conf, tristate and header files */
1020 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
1022 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1024 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
1026 fclose(out);
1027 fclose(tristate);
1028 fclose(out_h);
1030 name = getenv("KCONFIG_AUTOHEADER");
1031 if (!name)
1032 name = "include/generated/autoconf.h";
1033 sprintf(buf, "%s.tmpconfig.h", dir);
1034 if (rename(buf, name))
1035 return 1;
1036 name = getenv("KCONFIG_TRISTATE");
1037 if (!name)
1038 name = "include/config/tristate.conf";
1039 sprintf(buf, "%s.tmpconfig_tristate", dir);
1040 if (rename(buf, name))
1041 return 1;
1042 name = conf_get_autoconfig_name();
1044 * This must be the last step, kbuild has a dependency on auto.conf
1045 * and this marks the successful completion of the previous steps.
1047 sprintf(buf, "%s.tmpconfig", dir);
1048 if (rename(buf, name))
1049 return 1;
1051 return 0;
1054 static int sym_change_count;
1055 static void (*conf_changed_callback)(void);
1057 void sym_set_change_count(int count)
1059 int _sym_change_count = sym_change_count;
1060 sym_change_count = count;
1061 if (conf_changed_callback &&
1062 (bool)_sym_change_count != (bool)count)
1063 conf_changed_callback();
1066 void sym_add_change_count(int count)
1068 sym_set_change_count(count + sym_change_count);
1071 bool conf_get_changed(void)
1073 return sym_change_count;
1076 void conf_set_changed_callback(void (*fn)(void))
1078 conf_changed_callback = fn;
1081 static bool randomize_choice_values(struct symbol *csym)
1083 struct property *prop;
1084 struct symbol *sym;
1085 struct expr *e;
1086 int cnt, def;
1089 * If choice is mod then we may have more items selected
1090 * and if no then no-one.
1091 * In both cases stop.
1093 if (csym->curr.tri != yes)
1094 return false;
1096 prop = sym_get_choice_prop(csym);
1098 /* count entries in choice block */
1099 cnt = 0;
1100 expr_list_for_each_sym(prop->expr, e, sym)
1101 cnt++;
1104 * find a random value and set it to yes,
1105 * set the rest to no so we have only one set
1107 def = (rand() % cnt);
1109 cnt = 0;
1110 expr_list_for_each_sym(prop->expr, e, sym) {
1111 if (def == cnt++) {
1112 sym->def[S_DEF_USER].tri = yes;
1113 csym->def[S_DEF_USER].val = sym;
1115 else {
1116 sym->def[S_DEF_USER].tri = no;
1118 sym->flags |= SYMBOL_DEF_USER;
1119 /* clear VALID to get value calculated */
1120 sym->flags &= ~SYMBOL_VALID;
1122 csym->flags |= SYMBOL_DEF_USER;
1123 /* clear VALID to get value calculated */
1124 csym->flags &= ~(SYMBOL_VALID);
1126 return true;
1129 void set_all_choice_values(struct symbol *csym)
1131 struct property *prop;
1132 struct symbol *sym;
1133 struct expr *e;
1135 prop = sym_get_choice_prop(csym);
1138 * Set all non-assinged choice values to no
1140 expr_list_for_each_sym(prop->expr, e, sym) {
1141 if (!sym_has_value(sym))
1142 sym->def[S_DEF_USER].tri = no;
1144 csym->flags |= SYMBOL_DEF_USER;
1145 /* clear VALID to get value calculated */
1146 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1149 bool conf_set_all_new_symbols(enum conf_def_mode mode)
1151 struct symbol *sym, *csym;
1152 int i, cnt, pby, pty, ptm; /* pby: probability of boolean = y
1153 * pty: probability of tristate = y
1154 * ptm: probability of tristate = m
1157 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1158 * below, otherwise gcc whines about
1159 * -Wmaybe-uninitialized */
1160 if (mode == def_random) {
1161 int n, p[3];
1162 char *env = getenv("KCONFIG_PROBABILITY");
1163 n = 0;
1164 while( env && *env ) {
1165 char *endp;
1166 int tmp = strtol( env, &endp, 10 );
1167 if( tmp >= 0 && tmp <= 100 ) {
1168 p[n++] = tmp;
1169 } else {
1170 errno = ERANGE;
1171 perror( "KCONFIG_PROBABILITY" );
1172 exit( 1 );
1174 env = (*endp == ':') ? endp+1 : endp;
1175 if( n >=3 ) {
1176 break;
1179 switch( n ) {
1180 case 1:
1181 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1182 break;
1183 case 2:
1184 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1185 break;
1186 case 3:
1187 pby = p[0]; pty = p[1]; ptm = p[2];
1188 break;
1191 if( pty+ptm > 100 ) {
1192 errno = ERANGE;
1193 perror( "KCONFIG_PROBABILITY" );
1194 exit( 1 );
1197 bool has_changed = false;
1199 for_all_symbols(i, sym) {
1200 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1201 continue;
1202 switch (sym_get_type(sym)) {
1203 case S_BOOLEAN:
1204 case S_TRISTATE:
1205 has_changed = true;
1206 switch (mode) {
1207 case def_yes:
1208 sym->def[S_DEF_USER].tri = yes;
1209 break;
1210 case def_mod:
1211 sym->def[S_DEF_USER].tri = mod;
1212 break;
1213 case def_no:
1214 sym->def[S_DEF_USER].tri = no;
1215 break;
1216 case def_random:
1217 sym->def[S_DEF_USER].tri = no;
1218 cnt = rand() % 100;
1219 if (sym->type == S_TRISTATE) {
1220 if (cnt < pty)
1221 sym->def[S_DEF_USER].tri = yes;
1222 else if (cnt < (pty+ptm))
1223 sym->def[S_DEF_USER].tri = mod;
1224 } else if (cnt < pby)
1225 sym->def[S_DEF_USER].tri = yes;
1226 break;
1227 default:
1228 continue;
1230 if (!(sym_is_choice(sym) && mode == def_random))
1231 sym->flags |= SYMBOL_DEF_USER;
1232 break;
1233 default:
1234 break;
1239 sym_clear_all_valid();
1242 * We have different type of choice blocks.
1243 * If curr.tri equals to mod then we can select several
1244 * choice symbols in one block.
1245 * In this case we do nothing.
1246 * If curr.tri equals yes then only one symbol can be
1247 * selected in a choice block and we set it to yes,
1248 * and the rest to no.
1250 if (mode != def_random) {
1251 for_all_symbols(i, csym) {
1252 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1253 sym_is_choice_value(csym))
1254 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1258 for_all_symbols(i, csym) {
1259 if (sym_has_value(csym) || !sym_is_choice(csym))
1260 continue;
1262 sym_calc_value(csym);
1263 if (mode == def_random)
1264 has_changed = randomize_choice_values(csym);
1265 else {
1266 set_all_choice_values(csym);
1267 has_changed = true;
1271 return has_changed;