Debian Codegraph @

config/confdata.c

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