]> git.ipfire.org Git - thirdparty/linux.git/blob - scripts/kconfig/confdata.c
Merge tag 'tag-chrome-platform-for-v5.18' of git://git.kernel.org/pub/scm/linux/kerne...
[thirdparty/linux.git] / scripts / kconfig / confdata.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4 */
5
6 #include <sys/mman.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <ctype.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <limits.h>
13 #include <stdarg.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <time.h>
19 #include <unistd.h>
20
21 #include "lkc.h"
22
23 /* return true if 'path' exists, false otherwise */
24 static bool is_present(const char *path)
25 {
26 struct stat st;
27
28 return !stat(path, &st);
29 }
30
31 /* return true if 'path' exists and it is a directory, false otherwise */
32 static bool is_dir(const char *path)
33 {
34 struct stat st;
35
36 if (stat(path, &st))
37 return false;
38
39 return S_ISDIR(st.st_mode);
40 }
41
42 /* return true if the given two files are the same, false otherwise */
43 static bool is_same(const char *file1, const char *file2)
44 {
45 int fd1, fd2;
46 struct stat st1, st2;
47 void *map1, *map2;
48 bool ret = false;
49
50 fd1 = open(file1, O_RDONLY);
51 if (fd1 < 0)
52 return ret;
53
54 fd2 = open(file2, O_RDONLY);
55 if (fd2 < 0)
56 goto close1;
57
58 ret = fstat(fd1, &st1);
59 if (ret)
60 goto close2;
61 ret = fstat(fd2, &st2);
62 if (ret)
63 goto close2;
64
65 if (st1.st_size != st2.st_size)
66 goto close2;
67
68 map1 = mmap(NULL, st1.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
69 if (map1 == MAP_FAILED)
70 goto close2;
71
72 map2 = mmap(NULL, st2.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
73 if (map2 == MAP_FAILED)
74 goto close2;
75
76 if (bcmp(map1, map2, st1.st_size))
77 goto close2;
78
79 ret = true;
80 close2:
81 close(fd2);
82 close1:
83 close(fd1);
84
85 return ret;
86 }
87
88 /*
89 * Create the parent directory of the given path.
90 *
91 * For example, if 'include/config/auto.conf' is given, create 'include/config'.
92 */
93 static int make_parent_dir(const char *path)
94 {
95 char tmp[PATH_MAX + 1];
96 char *p;
97
98 strncpy(tmp, path, sizeof(tmp));
99 tmp[sizeof(tmp) - 1] = 0;
100
101 /* Remove the base name. Just return if nothing is left */
102 p = strrchr(tmp, '/');
103 if (!p)
104 return 0;
105 *(p + 1) = 0;
106
107 /* Just in case it is an absolute path */
108 p = tmp;
109 while (*p == '/')
110 p++;
111
112 while ((p = strchr(p, '/'))) {
113 *p = 0;
114
115 /* skip if the directory exists */
116 if (!is_dir(tmp) && mkdir(tmp, 0755))
117 return -1;
118
119 *p = '/';
120 while (*p == '/')
121 p++;
122 }
123
124 return 0;
125 }
126
127 static char depfile_path[PATH_MAX];
128 static size_t depfile_prefix_len;
129
130 /* touch depfile for symbol 'name' */
131 static int conf_touch_dep(const char *name)
132 {
133 int fd;
134
135 /* check overflow: prefix + name + '\0' must fit in buffer. */
136 if (depfile_prefix_len + strlen(name) + 1 > sizeof(depfile_path))
137 return -1;
138
139 strcpy(depfile_path + depfile_prefix_len, name);
140
141 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
142 if (fd == -1)
143 return -1;
144 close(fd);
145
146 return 0;
147 }
148
149 static void conf_warning(const char *fmt, ...)
150 __attribute__ ((format (printf, 1, 2)));
151
152 static void conf_message(const char *fmt, ...)
153 __attribute__ ((format (printf, 1, 2)));
154
155 static const char *conf_filename;
156 static int conf_lineno, conf_warnings;
157
158 static void conf_warning(const char *fmt, ...)
159 {
160 va_list ap;
161 va_start(ap, fmt);
162 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
163 vfprintf(stderr, fmt, ap);
164 fprintf(stderr, "\n");
165 va_end(ap);
166 conf_warnings++;
167 }
168
169 static void conf_default_message_callback(const char *s)
170 {
171 printf("#\n# ");
172 printf("%s", s);
173 printf("\n#\n");
174 }
175
176 static void (*conf_message_callback)(const char *s) =
177 conf_default_message_callback;
178 void conf_set_message_callback(void (*fn)(const char *s))
179 {
180 conf_message_callback = fn;
181 }
182
183 static void conf_message(const char *fmt, ...)
184 {
185 va_list ap;
186 char buf[4096];
187
188 if (!conf_message_callback)
189 return;
190
191 va_start(ap, fmt);
192
193 vsnprintf(buf, sizeof(buf), fmt, ap);
194 conf_message_callback(buf);
195 va_end(ap);
196 }
197
198 const char *conf_get_configname(void)
199 {
200 char *name = getenv("KCONFIG_CONFIG");
201
202 return name ? name : ".config";
203 }
204
205 static const char *conf_get_autoconfig_name(void)
206 {
207 char *name = getenv("KCONFIG_AUTOCONFIG");
208
209 return name ? name : "include/config/auto.conf";
210 }
211
212 static const char *conf_get_autoheader_name(void)
213 {
214 char *name = getenv("KCONFIG_AUTOHEADER");
215
216 return name ? name : "include/generated/autoconf.h";
217 }
218
219 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
220 {
221 char *p2;
222
223 switch (sym->type) {
224 case S_TRISTATE:
225 if (p[0] == 'm') {
226 sym->def[def].tri = mod;
227 sym->flags |= def_flags;
228 break;
229 }
230 /* fall through */
231 case S_BOOLEAN:
232 if (p[0] == 'y') {
233 sym->def[def].tri = yes;
234 sym->flags |= def_flags;
235 break;
236 }
237 if (p[0] == 'n') {
238 sym->def[def].tri = no;
239 sym->flags |= def_flags;
240 break;
241 }
242 if (def != S_DEF_AUTO)
243 conf_warning("symbol value '%s' invalid for %s",
244 p, sym->name);
245 return 1;
246 case S_STRING:
247 /* No escaping for S_DEF_AUTO (include/config/auto.conf) */
248 if (def != S_DEF_AUTO) {
249 if (*p++ != '"')
250 break;
251 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
252 if (*p2 == '"') {
253 *p2 = 0;
254 break;
255 }
256 memmove(p2, p2 + 1, strlen(p2));
257 }
258 if (!p2) {
259 conf_warning("invalid string found");
260 return 1;
261 }
262 }
263 /* fall through */
264 case S_INT:
265 case S_HEX:
266 if (sym_string_valid(sym, p)) {
267 sym->def[def].val = xstrdup(p);
268 sym->flags |= def_flags;
269 } else {
270 if (def != S_DEF_AUTO)
271 conf_warning("symbol value '%s' invalid for %s",
272 p, sym->name);
273 return 1;
274 }
275 break;
276 default:
277 ;
278 }
279 return 0;
280 }
281
282 #define LINE_GROWTH 16
283 static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
284 {
285 char *nline;
286 size_t new_size = slen + 1;
287 if (new_size > *n) {
288 new_size += LINE_GROWTH - 1;
289 new_size *= 2;
290 nline = xrealloc(*lineptr, new_size);
291 if (!nline)
292 return -1;
293
294 *lineptr = nline;
295 *n = new_size;
296 }
297
298 (*lineptr)[slen] = c;
299
300 return 0;
301 }
302
303 static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
304 {
305 char *line = *lineptr;
306 size_t slen = 0;
307
308 for (;;) {
309 int c = getc(stream);
310
311 switch (c) {
312 case '\n':
313 if (add_byte(c, &line, slen, n) < 0)
314 goto e_out;
315 slen++;
316 /* fall through */
317 case EOF:
318 if (add_byte('\0', &line, slen, n) < 0)
319 goto e_out;
320 *lineptr = line;
321 if (slen == 0)
322 return -1;
323 return slen;
324 default:
325 if (add_byte(c, &line, slen, n) < 0)
326 goto e_out;
327 slen++;
328 }
329 }
330
331 e_out:
332 line[slen-1] = '\0';
333 *lineptr = line;
334 return -1;
335 }
336
337 int conf_read_simple(const char *name, int def)
338 {
339 FILE *in = NULL;
340 char *line = NULL;
341 size_t line_asize = 0;
342 char *p, *p2;
343 struct symbol *sym;
344 int i, def_flags;
345
346 if (name) {
347 in = zconf_fopen(name);
348 } else {
349 char *env;
350
351 name = conf_get_configname();
352 in = zconf_fopen(name);
353 if (in)
354 goto load;
355 conf_set_changed(true);
356
357 env = getenv("KCONFIG_DEFCONFIG_LIST");
358 if (!env)
359 return 1;
360
361 while (1) {
362 bool is_last;
363
364 while (isspace(*env))
365 env++;
366
367 if (!*env)
368 break;
369
370 p = env;
371 while (*p && !isspace(*p))
372 p++;
373
374 is_last = (*p == '\0');
375
376 *p = '\0';
377
378 in = zconf_fopen(env);
379 if (in) {
380 conf_message("using defaults found in %s",
381 env);
382 goto load;
383 }
384
385 if (is_last)
386 break;
387
388 env = p + 1;
389 }
390 }
391 if (!in)
392 return 1;
393
394 load:
395 conf_filename = name;
396 conf_lineno = 0;
397 conf_warnings = 0;
398
399 def_flags = SYMBOL_DEF << def;
400 for_all_symbols(i, sym) {
401 sym->flags |= SYMBOL_CHANGED;
402 sym->flags &= ~(def_flags|SYMBOL_VALID);
403 if (sym_is_choice(sym))
404 sym->flags |= def_flags;
405 switch (sym->type) {
406 case S_INT:
407 case S_HEX:
408 case S_STRING:
409 if (sym->def[def].val)
410 free(sym->def[def].val);
411 /* fall through */
412 default:
413 sym->def[def].val = NULL;
414 sym->def[def].tri = no;
415 }
416 }
417
418 while (compat_getline(&line, &line_asize, in) != -1) {
419 conf_lineno++;
420 sym = NULL;
421 if (line[0] == '#') {
422 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
423 continue;
424 p = strchr(line + 2 + strlen(CONFIG_), ' ');
425 if (!p)
426 continue;
427 *p++ = 0;
428 if (strncmp(p, "is not set", 10))
429 continue;
430 if (def == S_DEF_USER) {
431 sym = sym_find(line + 2 + strlen(CONFIG_));
432 if (!sym) {
433 conf_set_changed(true);
434 continue;
435 }
436 } else {
437 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
438 if (sym->type == S_UNKNOWN)
439 sym->type = S_BOOLEAN;
440 }
441 if (sym->flags & def_flags) {
442 conf_warning("override: reassigning to symbol %s", sym->name);
443 }
444 switch (sym->type) {
445 case S_BOOLEAN:
446 case S_TRISTATE:
447 sym->def[def].tri = no;
448 sym->flags |= def_flags;
449 break;
450 default:
451 ;
452 }
453 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
454 p = strchr(line + strlen(CONFIG_), '=');
455 if (!p)
456 continue;
457 *p++ = 0;
458 p2 = strchr(p, '\n');
459 if (p2) {
460 *p2-- = 0;
461 if (*p2 == '\r')
462 *p2 = 0;
463 }
464
465 sym = sym_find(line + strlen(CONFIG_));
466 if (!sym) {
467 if (def == S_DEF_AUTO)
468 /*
469 * Reading from include/config/auto.conf
470 * If CONFIG_FOO previously existed in
471 * auto.conf but it is missing now,
472 * include/config/FOO must be touched.
473 */
474 conf_touch_dep(line + strlen(CONFIG_));
475 else
476 conf_set_changed(true);
477 continue;
478 }
479
480 if (sym->flags & def_flags) {
481 conf_warning("override: reassigning to symbol %s", sym->name);
482 }
483 if (conf_set_sym_val(sym, def, def_flags, p))
484 continue;
485 } else {
486 if (line[0] != '\r' && line[0] != '\n')
487 conf_warning("unexpected data: %.*s",
488 (int)strcspn(line, "\r\n"), line);
489
490 continue;
491 }
492
493 if (sym && sym_is_choice_value(sym)) {
494 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
495 switch (sym->def[def].tri) {
496 case no:
497 break;
498 case mod:
499 if (cs->def[def].tri == yes) {
500 conf_warning("%s creates inconsistent choice state", sym->name);
501 cs->flags &= ~def_flags;
502 }
503 break;
504 case yes:
505 if (cs->def[def].tri != no)
506 conf_warning("override: %s changes choice state", sym->name);
507 cs->def[def].val = sym;
508 break;
509 }
510 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
511 }
512 }
513 free(line);
514 fclose(in);
515 return 0;
516 }
517
518 int conf_read(const char *name)
519 {
520 struct symbol *sym;
521 int conf_unsaved = 0;
522 int i;
523
524 conf_set_changed(false);
525
526 if (conf_read_simple(name, S_DEF_USER)) {
527 sym_calc_value(modules_sym);
528 return 1;
529 }
530
531 sym_calc_value(modules_sym);
532
533 for_all_symbols(i, sym) {
534 sym_calc_value(sym);
535 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
536 continue;
537 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
538 /* check that calculated value agrees with saved value */
539 switch (sym->type) {
540 case S_BOOLEAN:
541 case S_TRISTATE:
542 if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
543 continue;
544 break;
545 default:
546 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
547 continue;
548 break;
549 }
550 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
551 /* no previous value and not saved */
552 continue;
553 conf_unsaved++;
554 /* maybe print value in verbose mode... */
555 }
556
557 for_all_symbols(i, sym) {
558 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
559 /* Reset values of generates values, so they'll appear
560 * as new, if they should become visible, but that
561 * doesn't quite work if the Kconfig and the saved
562 * configuration disagree.
563 */
564 if (sym->visible == no && !conf_unsaved)
565 sym->flags &= ~SYMBOL_DEF_USER;
566 switch (sym->type) {
567 case S_STRING:
568 case S_INT:
569 case S_HEX:
570 /* Reset a string value if it's out of range */
571 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
572 break;
573 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
574 conf_unsaved++;
575 break;
576 default:
577 break;
578 }
579 }
580 }
581
582 if (conf_warnings || conf_unsaved)
583 conf_set_changed(true);
584
585 return 0;
586 }
587
588 struct comment_style {
589 const char *decoration;
590 const char *prefix;
591 const char *postfix;
592 };
593
594 static const struct comment_style comment_style_pound = {
595 .decoration = "#",
596 .prefix = "#",
597 .postfix = "#",
598 };
599
600 static const struct comment_style comment_style_c = {
601 .decoration = " *",
602 .prefix = "/*",
603 .postfix = " */",
604 };
605
606 static void conf_write_heading(FILE *fp, const struct comment_style *cs)
607 {
608 fprintf(fp, "%s\n", cs->prefix);
609
610 fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
611 cs->decoration);
612
613 fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
614
615 fprintf(fp, "%s\n", cs->postfix);
616 }
617
618 /* The returned pointer must be freed on the caller side */
619 static char *escape_string_value(const char *in)
620 {
621 const char *p;
622 char *out;
623 size_t len;
624
625 len = strlen(in) + strlen("\"\"") + 1;
626
627 p = in;
628 while (1) {
629 p += strcspn(p, "\"\\");
630
631 if (p[0] == '\0')
632 break;
633
634 len++;
635 p++;
636 }
637
638 out = xmalloc(len);
639 out[0] = '\0';
640
641 strcat(out, "\"");
642
643 p = in;
644 while (1) {
645 len = strcspn(p, "\"\\");
646 strncat(out, p, len);
647 p += len;
648
649 if (p[0] == '\0')
650 break;
651
652 strcat(out, "\\");
653 strncat(out, p++, 1);
654 }
655
656 strcat(out, "\"");
657
658 return out;
659 }
660
661 /*
662 * Kconfig configuration printer
663 *
664 * This printer is used when generating the resulting configuration after
665 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
666 * passing a non-NULL argument to the printer.
667 */
668 enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
669
670 static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
671 bool escape_string)
672 {
673 const char *val;
674 char *escaped = NULL;
675
676 if (sym->type == S_UNKNOWN)
677 return;
678
679 val = sym_get_string_value(sym);
680
681 if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
682 output_n != OUTPUT_N && *val == 'n') {
683 if (output_n == OUTPUT_N_AS_UNSET)
684 fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
685 return;
686 }
687
688 if (sym->type == S_STRING && escape_string) {
689 escaped = escape_string_value(val);
690 val = escaped;
691 }
692
693 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
694
695 free(escaped);
696 }
697
698 static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
699 {
700 __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
701 }
702
703 static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
704 {
705 __print_symbol(fp, sym, OUTPUT_N_NONE, false);
706 }
707
708 void print_symbol_for_listconfig(struct symbol *sym)
709 {
710 __print_symbol(stdout, sym, OUTPUT_N, true);
711 }
712
713 static void print_symbol_for_c(FILE *fp, struct symbol *sym)
714 {
715 const char *val;
716 const char *sym_suffix = "";
717 const char *val_prefix = "";
718 char *escaped = NULL;
719
720 if (sym->type == S_UNKNOWN)
721 return;
722
723 val = sym_get_string_value(sym);
724
725 switch (sym->type) {
726 case S_BOOLEAN:
727 case S_TRISTATE:
728 switch (*val) {
729 case 'n':
730 return;
731 case 'm':
732 sym_suffix = "_MODULE";
733 /* fall through */
734 default:
735 val = "1";
736 }
737 break;
738 case S_HEX:
739 if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
740 val_prefix = "0x";
741 break;
742 case S_STRING:
743 escaped = escape_string_value(val);
744 val = escaped;
745 default:
746 break;
747 }
748
749 fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
750 val_prefix, val);
751
752 free(escaped);
753 }
754
755 /*
756 * Write out a minimal config.
757 * All values that has default values are skipped as this is redundant.
758 */
759 int conf_write_defconfig(const char *filename)
760 {
761 struct symbol *sym;
762 struct menu *menu;
763 FILE *out;
764
765 out = fopen(filename, "w");
766 if (!out)
767 return 1;
768
769 sym_clear_all_valid();
770
771 /* Traverse all menus to find all relevant symbols */
772 menu = rootmenu.list;
773
774 while (menu != NULL)
775 {
776 sym = menu->sym;
777 if (sym == NULL) {
778 if (!menu_is_visible(menu))
779 goto next_menu;
780 } else if (!sym_is_choice(sym)) {
781 sym_calc_value(sym);
782 if (!(sym->flags & SYMBOL_WRITE))
783 goto next_menu;
784 sym->flags &= ~SYMBOL_WRITE;
785 /* If we cannot change the symbol - skip */
786 if (!sym_is_changeable(sym))
787 goto next_menu;
788 /* If symbol equals to default value - skip */
789 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
790 goto next_menu;
791
792 /*
793 * If symbol is a choice value and equals to the
794 * default for a choice - skip.
795 * But only if value is bool and equal to "y" and
796 * choice is not "optional".
797 * (If choice is "optional" then all values can be "n")
798 */
799 if (sym_is_choice_value(sym)) {
800 struct symbol *cs;
801 struct symbol *ds;
802
803 cs = prop_get_symbol(sym_get_choice_prop(sym));
804 ds = sym_choice_default(cs);
805 if (!sym_is_optional(cs) && sym == ds) {
806 if ((sym->type == S_BOOLEAN) &&
807 sym_get_tristate_value(sym) == yes)
808 goto next_menu;
809 }
810 }
811 print_symbol_for_dotconfig(out, sym);
812 }
813 next_menu:
814 if (menu->list != NULL) {
815 menu = menu->list;
816 }
817 else if (menu->next != NULL) {
818 menu = menu->next;
819 } else {
820 while ((menu = menu->parent)) {
821 if (menu->next != NULL) {
822 menu = menu->next;
823 break;
824 }
825 }
826 }
827 }
828 fclose(out);
829 return 0;
830 }
831
832 int conf_write(const char *name)
833 {
834 FILE *out;
835 struct symbol *sym;
836 struct menu *menu;
837 const char *str;
838 char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
839 char *env;
840 int i;
841 bool need_newline = false;
842
843 if (!name)
844 name = conf_get_configname();
845
846 if (!*name) {
847 fprintf(stderr, "config name is empty\n");
848 return -1;
849 }
850
851 if (is_dir(name)) {
852 fprintf(stderr, "%s: Is a directory\n", name);
853 return -1;
854 }
855
856 if (make_parent_dir(name))
857 return -1;
858
859 env = getenv("KCONFIG_OVERWRITECONFIG");
860 if (env && *env) {
861 *tmpname = 0;
862 out = fopen(name, "w");
863 } else {
864 snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
865 name, (int)getpid());
866 out = fopen(tmpname, "w");
867 }
868 if (!out)
869 return 1;
870
871 conf_write_heading(out, &comment_style_pound);
872
873 if (!conf_get_changed())
874 sym_clear_all_valid();
875
876 menu = rootmenu.list;
877 while (menu) {
878 sym = menu->sym;
879 if (!sym) {
880 if (!menu_is_visible(menu))
881 goto next;
882 str = menu_get_prompt(menu);
883 fprintf(out, "\n"
884 "#\n"
885 "# %s\n"
886 "#\n", str);
887 need_newline = false;
888 } else if (!(sym->flags & SYMBOL_CHOICE) &&
889 !(sym->flags & SYMBOL_WRITTEN)) {
890 sym_calc_value(sym);
891 if (!(sym->flags & SYMBOL_WRITE))
892 goto next;
893 if (need_newline) {
894 fprintf(out, "\n");
895 need_newline = false;
896 }
897 sym->flags |= SYMBOL_WRITTEN;
898 print_symbol_for_dotconfig(out, sym);
899 }
900
901 next:
902 if (menu->list) {
903 menu = menu->list;
904 continue;
905 }
906
907 end_check:
908 if (!menu->sym && menu_is_visible(menu) && menu != &rootmenu &&
909 menu->prompt->type == P_MENU) {
910 fprintf(out, "# end of %s\n", menu_get_prompt(menu));
911 need_newline = true;
912 }
913
914 if (menu->next) {
915 menu = menu->next;
916 } else {
917 menu = menu->parent;
918 if (menu)
919 goto end_check;
920 }
921 }
922 fclose(out);
923
924 for_all_symbols(i, sym)
925 sym->flags &= ~SYMBOL_WRITTEN;
926
927 if (*tmpname) {
928 if (is_same(name, tmpname)) {
929 conf_message("No change to %s", name);
930 unlink(tmpname);
931 conf_set_changed(false);
932 return 0;
933 }
934
935 snprintf(oldname, sizeof(oldname), "%s.old", name);
936 rename(name, oldname);
937 if (rename(tmpname, name))
938 return 1;
939 }
940
941 conf_message("configuration written to %s", name);
942
943 conf_set_changed(false);
944
945 return 0;
946 }
947
948 /* write a dependency file as used by kbuild to track dependencies */
949 static int conf_write_autoconf_cmd(const char *autoconf_name)
950 {
951 char name[PATH_MAX], tmp[PATH_MAX];
952 struct file *file;
953 FILE *out;
954 int ret;
955
956 ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
957 if (ret >= sizeof(name)) /* check truncation */
958 return -1;
959
960 if (make_parent_dir(name))
961 return -1;
962
963 ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
964 if (ret >= sizeof(tmp)) /* check truncation */
965 return -1;
966
967 out = fopen(tmp, "w");
968 if (!out) {
969 perror("fopen");
970 return -1;
971 }
972
973 fprintf(out, "deps_config := \\\n");
974 for (file = file_list; file; file = file->next)
975 fprintf(out, "\t%s \\\n", file->name);
976
977 fprintf(out, "\n%s: $(deps_config)\n\n", autoconf_name);
978
979 env_write_dep(out, autoconf_name);
980
981 fprintf(out, "\n$(deps_config): ;\n");
982
983 fflush(out);
984 ret = ferror(out); /* error check for all fprintf() calls */
985 fclose(out);
986 if (ret)
987 return -1;
988
989 if (rename(tmp, name)) {
990 perror("rename");
991 return -1;
992 }
993
994 return 0;
995 }
996
997 static int conf_touch_deps(void)
998 {
999 const char *name, *tmp;
1000 struct symbol *sym;
1001 int res, i;
1002
1003 name = conf_get_autoconfig_name();
1004 tmp = strrchr(name, '/');
1005 depfile_prefix_len = tmp ? tmp - name + 1 : 0;
1006 if (depfile_prefix_len + 1 > sizeof(depfile_path))
1007 return -1;
1008
1009 strncpy(depfile_path, name, depfile_prefix_len);
1010 depfile_path[depfile_prefix_len] = 0;
1011
1012 conf_read_simple(name, S_DEF_AUTO);
1013 sym_calc_value(modules_sym);
1014
1015 for_all_symbols(i, sym) {
1016 sym_calc_value(sym);
1017 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
1018 continue;
1019 if (sym->flags & SYMBOL_WRITE) {
1020 if (sym->flags & SYMBOL_DEF_AUTO) {
1021 /*
1022 * symbol has old and new value,
1023 * so compare them...
1024 */
1025 switch (sym->type) {
1026 case S_BOOLEAN:
1027 case S_TRISTATE:
1028 if (sym_get_tristate_value(sym) ==
1029 sym->def[S_DEF_AUTO].tri)
1030 continue;
1031 break;
1032 case S_STRING:
1033 case S_HEX:
1034 case S_INT:
1035 if (!strcmp(sym_get_string_value(sym),
1036 sym->def[S_DEF_AUTO].val))
1037 continue;
1038 break;
1039 default:
1040 break;
1041 }
1042 } else {
1043 /*
1044 * If there is no old value, only 'no' (unset)
1045 * is allowed as new value.
1046 */
1047 switch (sym->type) {
1048 case S_BOOLEAN:
1049 case S_TRISTATE:
1050 if (sym_get_tristate_value(sym) == no)
1051 continue;
1052 break;
1053 default:
1054 break;
1055 }
1056 }
1057 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1058 /* There is neither an old nor a new value. */
1059 continue;
1060 /* else
1061 * There is an old value, but no new value ('no' (unset)
1062 * isn't saved in auto.conf, so the old value is always
1063 * different from 'no').
1064 */
1065
1066 res = conf_touch_dep(sym->name);
1067 if (res)
1068 return res;
1069 }
1070
1071 return 0;
1072 }
1073
1074 static int __conf_write_autoconf(const char *filename,
1075 void (*print_symbol)(FILE *, struct symbol *),
1076 const struct comment_style *comment_style)
1077 {
1078 char tmp[PATH_MAX];
1079 FILE *file;
1080 struct symbol *sym;
1081 int ret, i;
1082
1083 if (make_parent_dir(filename))
1084 return -1;
1085
1086 ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
1087 if (ret >= sizeof(tmp)) /* check truncation */
1088 return -1;
1089
1090 file = fopen(tmp, "w");
1091 if (!file) {
1092 perror("fopen");
1093 return -1;
1094 }
1095
1096 conf_write_heading(file, comment_style);
1097
1098 for_all_symbols(i, sym)
1099 if ((sym->flags & SYMBOL_WRITE) && sym->name)
1100 print_symbol(file, sym);
1101
1102 fflush(file);
1103 /* check possible errors in conf_write_heading() and print_symbol() */
1104 ret = ferror(file);
1105 fclose(file);
1106 if (ret)
1107 return -1;
1108
1109 if (rename(tmp, filename)) {
1110 perror("rename");
1111 return -1;
1112 }
1113
1114 return 0;
1115 }
1116
1117 int conf_write_autoconf(int overwrite)
1118 {
1119 struct symbol *sym;
1120 const char *autoconf_name = conf_get_autoconfig_name();
1121 int ret, i;
1122
1123 if (!overwrite && is_present(autoconf_name))
1124 return 0;
1125
1126 ret = conf_write_autoconf_cmd(autoconf_name);
1127 if (ret)
1128 return -1;
1129
1130 if (conf_touch_deps())
1131 return 1;
1132
1133 for_all_symbols(i, sym)
1134 sym_calc_value(sym);
1135
1136 ret = __conf_write_autoconf(conf_get_autoheader_name(),
1137 print_symbol_for_c,
1138 &comment_style_c);
1139 if (ret)
1140 return ret;
1141
1142 /*
1143 * Create include/config/auto.conf. This must be the last step because
1144 * Kbuild has a dependency on auto.conf and this marks the successful
1145 * completion of the previous steps.
1146 */
1147 ret = __conf_write_autoconf(conf_get_autoconfig_name(),
1148 print_symbol_for_autoconf,
1149 &comment_style_pound);
1150 if (ret)
1151 return ret;
1152
1153 return 0;
1154 }
1155
1156 static bool conf_changed;
1157 static void (*conf_changed_callback)(void);
1158
1159 void conf_set_changed(bool val)
1160 {
1161 if (conf_changed_callback && conf_changed != val)
1162 conf_changed_callback();
1163
1164 conf_changed = val;
1165 }
1166
1167 bool conf_get_changed(void)
1168 {
1169 return conf_changed;
1170 }
1171
1172 void conf_set_changed_callback(void (*fn)(void))
1173 {
1174 conf_changed_callback = fn;
1175 }
1176
1177 void set_all_choice_values(struct symbol *csym)
1178 {
1179 struct property *prop;
1180 struct symbol *sym;
1181 struct expr *e;
1182
1183 prop = sym_get_choice_prop(csym);
1184
1185 /*
1186 * Set all non-assinged choice values to no
1187 */
1188 expr_list_for_each_sym(prop->expr, e, sym) {
1189 if (!sym_has_value(sym))
1190 sym->def[S_DEF_USER].tri = no;
1191 }
1192 csym->flags |= SYMBOL_DEF_USER;
1193 /* clear VALID to get value calculated */
1194 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1195 }