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