kconfig: add helper to set config symbol from environment variable
[linux-2.6.git] / scripts / kconfig / confdata.c
1 /*
2  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3  * Released under the terms of the GNU GPL v2.0.
4  */
5
6 #include <sys/stat.h>
7 #include <ctype.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include <unistd.h>
14
15 #define LKC_DIRECT_LINK
16 #include "lkc.h"
17
18 static void conf_warning(const char *fmt, ...)
19         __attribute__ ((format (printf, 1, 2)));
20
21 static const char *conf_filename;
22 static int conf_lineno, conf_warnings, conf_unsaved;
23
24 const char conf_defname[] = "arch/$ARCH/defconfig";
25
26 static void conf_warning(const char *fmt, ...)
27 {
28         va_list ap;
29         va_start(ap, fmt);
30         fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
31         vfprintf(stderr, fmt, ap);
32         fprintf(stderr, "\n");
33         va_end(ap);
34         conf_warnings++;
35 }
36
37 const char *conf_get_configname(void)
38 {
39         char *name = getenv("KCONFIG_CONFIG");
40
41         return name ? name : ".config";
42 }
43
44 static char *conf_expand_value(const char *in)
45 {
46         struct symbol *sym;
47         const char *src;
48         static char res_value[SYMBOL_MAXLENGTH];
49         char *dst, name[SYMBOL_MAXLENGTH];
50
51         res_value[0] = 0;
52         dst = name;
53         while ((src = strchr(in, '$'))) {
54                 strncat(res_value, in, src - in);
55                 src++;
56                 dst = name;
57                 while (isalnum(*src) || *src == '_')
58                         *dst++ = *src++;
59                 *dst = 0;
60                 sym = sym_lookup(name, 0);
61                 sym_calc_value(sym);
62                 strcat(res_value, sym_get_string_value(sym));
63                 in = src;
64         }
65         strcat(res_value, in);
66
67         return res_value;
68 }
69
70 char *conf_get_default_confname(void)
71 {
72         struct stat buf;
73         static char fullname[PATH_MAX+1];
74         char *env, *name;
75
76         name = conf_expand_value(conf_defname);
77         env = getenv(SRCTREE);
78         if (env) {
79                 sprintf(fullname, "%s/%s", env, name);
80                 if (!stat(fullname, &buf))
81                         return fullname;
82         }
83         return name;
84 }
85
86 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
87 {
88         char *p2;
89
90         switch (sym->type) {
91         case S_TRISTATE:
92                 if (p[0] == 'm') {
93                         sym->def[def].tri = mod;
94                         sym->flags |= def_flags;
95                         break;
96                 }
97         case S_BOOLEAN:
98                 if (p[0] == 'y') {
99                         sym->def[def].tri = yes;
100                         sym->flags |= def_flags;
101                         break;
102                 }
103                 if (p[0] == 'n') {
104                         sym->def[def].tri = no;
105                         sym->flags |= def_flags;
106                         break;
107                 }
108                 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
109                 break;
110         case S_OTHER:
111                 if (*p != '"') {
112                         for (p2 = p; *p2 && !isspace(*p2); p2++)
113                                 ;
114                         sym->type = S_STRING;
115                         goto done;
116                 }
117         case S_STRING:
118                 if (*p++ != '"')
119                         break;
120                 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
121                         if (*p2 == '"') {
122                                 *p2 = 0;
123                                 break;
124                         }
125                         memmove(p2, p2 + 1, strlen(p2));
126                 }
127                 if (!p2) {
128                         conf_warning("invalid string found");
129                         return 1;
130                 }
131         case S_INT:
132         case S_HEX:
133         done:
134                 if (sym_string_valid(sym, p)) {
135                         sym->def[def].val = strdup(p);
136                         sym->flags |= def_flags;
137                 } else {
138                         conf_warning("symbol value '%s' invalid for %s", p, sym->name);
139                         return 1;
140                 }
141                 break;
142         default:
143                 ;
144         }
145         return 0;
146 }
147
148 /* Read an environment variable and assign the value to the symbol */
149 int conf_set_env_sym(const char *env, const char *symname, int def)
150 {
151         struct symbol *sym;
152         char *p;
153         int def_flags;
154
155         p = getenv(env);
156         if (p) {
157                 char warning[200];
158                 sprintf(warning, "Environment variable (%s = \"%s\")", env, p);
159                 conf_filename = warning;
160                 def_flags = SYMBOL_DEF << def;
161                 if (def == S_DEF_USER) {
162                         sym = sym_find(symname);
163                         if (!sym)
164                                 return 1;
165                 } else {
166                         sym = sym_lookup(symname, 0);
167                         if (sym->type == S_UNKNOWN)
168                                 sym->type = S_OTHER;
169                 }
170                 conf_set_sym_val(sym, def, def_flags, p);
171         }
172         return 0;
173 }
174
175 int conf_read_simple(const char *name, int def)
176 {
177         FILE *in = NULL;
178         char line[1024];
179         char *p, *p2;
180         struct symbol *sym;
181         int i, def_flags;
182
183         if (name) {
184                 in = zconf_fopen(name);
185         } else {
186                 struct property *prop;
187
188                 name = conf_get_configname();
189                 in = zconf_fopen(name);
190                 if (in)
191                         goto load;
192                 sym_add_change_count(1);
193                 if (!sym_defconfig_list)
194                         return 1;
195
196                 for_all_defaults(sym_defconfig_list, prop) {
197                         if (expr_calc_value(prop->visible.expr) == no ||
198                             prop->expr->type != E_SYMBOL)
199                                 continue;
200                         name = conf_expand_value(prop->expr->left.sym->name);
201                         in = zconf_fopen(name);
202                         if (in) {
203                                 printf(_("#\n"
204                                          "# using defaults found in %s\n"
205                                          "#\n"), name);
206                                 goto load;
207                         }
208                 }
209         }
210         if (!in)
211                 return 1;
212
213 load:
214         conf_filename = name;
215         conf_lineno = 0;
216         conf_warnings = 0;
217         conf_unsaved = 0;
218
219         def_flags = SYMBOL_DEF << def;
220         for_all_symbols(i, sym) {
221                 sym->flags |= SYMBOL_CHANGED;
222                 sym->flags &= ~(def_flags|SYMBOL_VALID);
223                 if (sym_is_choice(sym))
224                         sym->flags |= def_flags;
225                 switch (sym->type) {
226                 case S_INT:
227                 case S_HEX:
228                 case S_STRING:
229                         if (sym->def[def].val)
230                                 free(sym->def[def].val);
231                 default:
232                         sym->def[def].val = NULL;
233                         sym->def[def].tri = no;
234                 }
235         }
236
237         while (fgets(line, sizeof(line), in)) {
238                 conf_lineno++;
239                 sym = NULL;
240                 switch (line[0]) {
241                 case '#':
242                         if (memcmp(line + 2, "CONFIG_", 7))
243                                 continue;
244                         p = strchr(line + 9, ' ');
245                         if (!p)
246                                 continue;
247                         *p++ = 0;
248                         if (strncmp(p, "is not set", 10))
249                                 continue;
250                         if (def == S_DEF_USER) {
251                                 sym = sym_find(line + 9);
252                                 if (!sym) {
253                                         conf_warning("trying to assign nonexistent symbol %s", line + 9);
254                                         break;
255                                 }
256                         } else {
257                                 sym = sym_lookup(line + 9, 0);
258                                 if (sym->type == S_UNKNOWN)
259                                         sym->type = S_BOOLEAN;
260                         }
261                         if (sym->flags & def_flags) {
262                                 conf_warning("trying to reassign symbol %s", sym->name);
263                                 break;
264                         }
265                         switch (sym->type) {
266                         case S_BOOLEAN:
267                         case S_TRISTATE:
268                                 sym->def[def].tri = no;
269                                 sym->flags |= def_flags;
270                                 break;
271                         default:
272                                 ;
273                         }
274                         break;
275                 case 'C':
276                         if (memcmp(line, "CONFIG_", 7)) {
277                                 conf_warning("unexpected data");
278                                 continue;
279                         }
280                         p = strchr(line + 7, '=');
281                         if (!p)
282                                 continue;
283                         *p++ = 0;
284                         p2 = strchr(p, '\n');
285                         if (p2) {
286                                 *p2-- = 0;
287                                 if (*p2 == '\r')
288                                         *p2 = 0;
289                         }
290                         if (def == S_DEF_USER) {
291                                 sym = sym_find(line + 7);
292                                 if (!sym) {
293                                         conf_warning("trying to assign nonexistent symbol %s", line + 7);
294                                         break;
295                                 }
296                         } else {
297                                 sym = sym_lookup(line + 7, 0);
298                                 if (sym->type == S_UNKNOWN)
299                                         sym->type = S_OTHER;
300                         }
301                         if (sym->flags & def_flags) {
302                                 conf_warning("trying to reassign symbol %s", sym->name);
303                                 break;
304                         }
305                         if (conf_set_sym_val(sym, def, def_flags, p))
306                                 continue;
307                         break;
308                 case '\r':
309                 case '\n':
310                         break;
311                 default:
312                         conf_warning("unexpected data");
313                         continue;
314                 }
315                 if (sym && sym_is_choice_value(sym)) {
316                         struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
317                         switch (sym->def[def].tri) {
318                         case no:
319                                 break;
320                         case mod:
321                                 if (cs->def[def].tri == yes) {
322                                         conf_warning("%s creates inconsistent choice state", sym->name);
323                                         cs->flags &= ~def_flags;
324                                 }
325                                 break;
326                         case yes:
327                                 if (cs->def[def].tri != no) {
328                                         conf_warning("%s creates inconsistent choice state", sym->name);
329                                         cs->flags &= ~def_flags;
330                                 } else
331                                         cs->def[def].val = sym;
332                                 break;
333                         }
334                         cs->def[def].tri = E_OR(cs->def[def].tri, sym->def[def].tri);
335                 }
336         }
337         fclose(in);
338
339         if (modules_sym)
340                 sym_calc_value(modules_sym);
341         return 0;
342 }
343
344 int conf_read(const char *name)
345 {
346         struct symbol *sym;
347         struct property *prop;
348         struct expr *e;
349         int i, flags;
350
351         sym_set_change_count(0);
352
353         if (conf_read_simple(name, S_DEF_USER))
354                 return 1;
355
356         for_all_symbols(i, sym) {
357                 sym_calc_value(sym);
358                 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
359                         goto sym_ok;
360                 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
361                         /* check that calculated value agrees with saved value */
362                         switch (sym->type) {
363                         case S_BOOLEAN:
364                         case S_TRISTATE:
365                                 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
366                                         break;
367                                 if (!sym_is_choice(sym))
368                                         goto sym_ok;
369                         default:
370                                 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
371                                         goto sym_ok;
372                                 break;
373                         }
374                 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
375                         /* no previous value and not saved */
376                         goto sym_ok;
377                 conf_unsaved++;
378                 /* maybe print value in verbose mode... */
379         sym_ok:
380                 if (!sym_is_choice(sym))
381                         continue;
382                 /* The choice symbol only has a set value (and thus is not new)
383                  * if all its visible childs have values.
384                  */
385                 prop = sym_get_choice_prop(sym);
386                 flags = sym->flags;
387                 for (e = prop->expr; e; e = e->left.expr)
388                         if (e->right.sym->visible != no)
389                                 flags &= e->right.sym->flags;
390                 sym->flags &= flags | ~SYMBOL_DEF_USER;
391         }
392
393         for_all_symbols(i, sym) {
394                 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
395                         /* Reset values of generates values, so they'll appear
396                          * as new, if they should become visible, but that
397                          * doesn't quite work if the Kconfig and the saved
398                          * configuration disagree.
399                          */
400                         if (sym->visible == no && !conf_unsaved)
401                                 sym->flags &= ~SYMBOL_DEF_USER;
402                         switch (sym->type) {
403                         case S_STRING:
404                         case S_INT:
405                         case S_HEX:
406                                 /* Reset a string value if it's out of range */
407                                 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
408                                         break;
409                                 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
410                                 conf_unsaved++;
411                                 break;
412                         default:
413                                 break;
414                         }
415                 }
416         }
417
418         sym_add_change_count(conf_warnings || conf_unsaved);
419
420         return 0;
421 }
422
423 int conf_write(const char *name)
424 {
425         FILE *out;
426         struct symbol *sym;
427         struct menu *menu;
428         const char *basename;
429         char dirname[128], tmpname[128], newname[128];
430         int type, l;
431         const char *str;
432         time_t now;
433         int use_timestamp = 1;
434         char *env;
435
436         dirname[0] = 0;
437         if (name && name[0]) {
438                 struct stat st;
439                 char *slash;
440
441                 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
442                         strcpy(dirname, name);
443                         strcat(dirname, "/");
444                         basename = conf_get_configname();
445                 } else if ((slash = strrchr(name, '/'))) {
446                         int size = slash - name + 1;
447                         memcpy(dirname, name, size);
448                         dirname[size] = 0;
449                         if (slash[1])
450                                 basename = slash + 1;
451                         else
452                                 basename = conf_get_configname();
453                 } else
454                         basename = name;
455         } else
456                 basename = conf_get_configname();
457
458         sprintf(newname, "%s%s", dirname, basename);
459         env = getenv("KCONFIG_OVERWRITECONFIG");
460         if (!env || !*env) {
461                 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
462                 out = fopen(tmpname, "w");
463         } else {
464                 *tmpname = 0;
465                 out = fopen(newname, "w");
466         }
467         if (!out)
468                 return 1;
469
470         sym = sym_lookup("KERNELVERSION", 0);
471         sym_calc_value(sym);
472         time(&now);
473         env = getenv("KCONFIG_NOTIMESTAMP");
474         if (env && *env)
475                 use_timestamp = 0;
476
477         fprintf(out, _("#\n"
478                        "# Automatically generated make config: don't edit\n"
479                        "# Linux kernel version: %s\n"
480                        "%s%s"
481                        "#\n"),
482                      sym_get_string_value(sym),
483                      use_timestamp ? "# " : "",
484                      use_timestamp ? ctime(&now) : "");
485
486         if (!conf_get_changed())
487                 sym_clear_all_valid();
488
489         menu = rootmenu.list;
490         while (menu) {
491                 sym = menu->sym;
492                 if (!sym) {
493                         if (!menu_is_visible(menu))
494                                 goto next;
495                         str = menu_get_prompt(menu);
496                         fprintf(out, "\n"
497                                      "#\n"
498                                      "# %s\n"
499                                      "#\n", str);
500                 } else if (!(sym->flags & SYMBOL_CHOICE)) {
501                         sym_calc_value(sym);
502                         if (!(sym->flags & SYMBOL_WRITE))
503                                 goto next;
504                         sym->flags &= ~SYMBOL_WRITE;
505                         type = sym->type;
506                         if (type == S_TRISTATE) {
507                                 sym_calc_value(modules_sym);
508                                 if (modules_sym->curr.tri == no)
509                                         type = S_BOOLEAN;
510                         }
511                         switch (type) {
512                         case S_BOOLEAN:
513                         case S_TRISTATE:
514                                 switch (sym_get_tristate_value(sym)) {
515                                 case no:
516                                         fprintf(out, "# CONFIG_%s is not set\n", sym->name);
517                                         break;
518                                 case mod:
519                                         fprintf(out, "CONFIG_%s=m\n", sym->name);
520                                         break;
521                                 case yes:
522                                         fprintf(out, "CONFIG_%s=y\n", sym->name);
523                                         break;
524                                 }
525                                 break;
526                         case S_STRING:
527                                 str = sym_get_string_value(sym);
528                                 fprintf(out, "CONFIG_%s=\"", sym->name);
529                                 while (1) {
530                                         l = strcspn(str, "\"\\");
531                                         if (l) {
532                                                 fwrite(str, l, 1, out);
533                                                 str += l;
534                                         }
535                                         if (!*str)
536                                                 break;
537                                         fprintf(out, "\\%c", *str++);
538                                 }
539                                 fputs("\"\n", out);
540                                 break;
541                         case S_HEX:
542                                 str = sym_get_string_value(sym);
543                                 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
544                                         fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
545                                         break;
546                                 }
547                         case S_INT:
548                                 str = sym_get_string_value(sym);
549                                 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
550                                 break;
551                         }
552                 }
553
554         next:
555                 if (menu->list) {
556                         menu = menu->list;
557                         continue;
558                 }
559                 if (menu->next)
560                         menu = menu->next;
561                 else while ((menu = menu->parent)) {
562                         if (menu->next) {
563                                 menu = menu->next;
564                                 break;
565                         }
566                 }
567         }
568         fclose(out);
569
570         if (*tmpname) {
571                 strcat(dirname, basename);
572                 strcat(dirname, ".old");
573                 rename(newname, dirname);
574                 if (rename(tmpname, newname))
575                         return 1;
576         }
577
578         printf(_("#\n"
579                  "# configuration written to %s\n"
580                  "#\n"), newname);
581
582         sym_set_change_count(0);
583
584         return 0;
585 }
586
587 int conf_split_config(void)
588 {
589         char *name, path[128];
590         char *s, *d, c;
591         struct symbol *sym;
592         struct stat sb;
593         int res, i, fd;
594
595         name = getenv("KCONFIG_AUTOCONFIG");
596         if (!name)
597                 name = "include/config/auto.conf";
598         conf_read_simple(name, S_DEF_AUTO);
599
600         if (chdir("include/config"))
601                 return 1;
602
603         res = 0;
604         for_all_symbols(i, sym) {
605                 sym_calc_value(sym);
606                 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
607                         continue;
608                 if (sym->flags & SYMBOL_WRITE) {
609                         if (sym->flags & SYMBOL_DEF_AUTO) {
610                                 /*
611                                  * symbol has old and new value,
612                                  * so compare them...
613                                  */
614                                 switch (sym->type) {
615                                 case S_BOOLEAN:
616                                 case S_TRISTATE:
617                                         if (sym_get_tristate_value(sym) ==
618                                             sym->def[S_DEF_AUTO].tri)
619                                                 continue;
620                                         break;
621                                 case S_STRING:
622                                 case S_HEX:
623                                 case S_INT:
624                                         if (!strcmp(sym_get_string_value(sym),
625                                                     sym->def[S_DEF_AUTO].val))
626                                                 continue;
627                                         break;
628                                 default:
629                                         break;
630                                 }
631                         } else {
632                                 /*
633                                  * If there is no old value, only 'no' (unset)
634                                  * is allowed as new value.
635                                  */
636                                 switch (sym->type) {
637                                 case S_BOOLEAN:
638                                 case S_TRISTATE:
639                                         if (sym_get_tristate_value(sym) == no)
640                                                 continue;
641                                         break;
642                                 default:
643                                         break;
644                                 }
645                         }
646                 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
647                         /* There is neither an old nor a new value. */
648                         continue;
649                 /* else
650                  *      There is an old value, but no new value ('no' (unset)
651                  *      isn't saved in auto.conf, so the old value is always
652                  *      different from 'no').
653                  */
654
655                 /* Replace all '_' and append ".h" */
656                 s = sym->name;
657                 d = path;
658                 while ((c = *s++)) {
659                         c = tolower(c);
660                         *d++ = (c == '_') ? '/' : c;
661                 }
662                 strcpy(d, ".h");
663
664                 /* Assume directory path already exists. */
665                 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
666                 if (fd == -1) {
667                         if (errno != ENOENT) {
668                                 res = 1;
669                                 break;
670                         }
671                         /*
672                          * Create directory components,
673                          * unless they exist already.
674                          */
675                         d = path;
676                         while ((d = strchr(d, '/'))) {
677                                 *d = 0;
678                                 if (stat(path, &sb) && mkdir(path, 0755)) {
679                                         res = 1;
680                                         goto out;
681                                 }
682                                 *d++ = '/';
683                         }
684                         /* Try it again. */
685                         fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
686                         if (fd == -1) {
687                                 res = 1;
688                                 break;
689                         }
690                 }
691                 close(fd);
692         }
693 out:
694         if (chdir("../.."))
695                 return 1;
696
697         return res;
698 }
699
700 int conf_write_autoconf(void)
701 {
702         struct symbol *sym;
703         const char *str;
704         char *name;
705         FILE *out, *out_h;
706         time_t now;
707         int i, l;
708
709         sym_clear_all_valid();
710
711         file_write_dep("include/config/auto.conf.cmd");
712
713         if (conf_split_config())
714                 return 1;
715
716         out = fopen(".tmpconfig", "w");
717         if (!out)
718                 return 1;
719
720         out_h = fopen(".tmpconfig.h", "w");
721         if (!out_h) {
722                 fclose(out);
723                 return 1;
724         }
725
726         sym = sym_lookup("KERNELVERSION", 0);
727         sym_calc_value(sym);
728         time(&now);
729         fprintf(out, "#\n"
730                      "# Automatically generated make config: don't edit\n"
731                      "# Linux kernel version: %s\n"
732                      "# %s"
733                      "#\n",
734                      sym_get_string_value(sym), ctime(&now));
735         fprintf(out_h, "/*\n"
736                        " * Automatically generated C config: don't edit\n"
737                        " * Linux kernel version: %s\n"
738                        " * %s"
739                        " */\n"
740                        "#define AUTOCONF_INCLUDED\n",
741                        sym_get_string_value(sym), ctime(&now));
742
743         for_all_symbols(i, sym) {
744                 sym_calc_value(sym);
745                 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
746                         continue;
747                 switch (sym->type) {
748                 case S_BOOLEAN:
749                 case S_TRISTATE:
750                         switch (sym_get_tristate_value(sym)) {
751                         case no:
752                                 break;
753                         case mod:
754                                 fprintf(out, "CONFIG_%s=m\n", sym->name);
755                                 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
756                                 break;
757                         case yes:
758                                 fprintf(out, "CONFIG_%s=y\n", sym->name);
759                                 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
760                                 break;
761                         }
762                         break;
763                 case S_STRING:
764                         str = sym_get_string_value(sym);
765                         fprintf(out, "CONFIG_%s=\"", sym->name);
766                         fprintf(out_h, "#define CONFIG_%s \"", sym->name);
767                         while (1) {
768                                 l = strcspn(str, "\"\\");
769                                 if (l) {
770                                         fwrite(str, l, 1, out);
771                                         fwrite(str, l, 1, out_h);
772                                         str += l;
773                                 }
774                                 if (!*str)
775                                         break;
776                                 fprintf(out, "\\%c", *str);
777                                 fprintf(out_h, "\\%c", *str);
778                                 str++;
779                         }
780                         fputs("\"\n", out);
781                         fputs("\"\n", out_h);
782                         break;
783                 case S_HEX:
784                         str = sym_get_string_value(sym);
785                         if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
786                                 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
787                                 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
788                                 break;
789                         }
790                 case S_INT:
791                         str = sym_get_string_value(sym);
792                         fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
793                         fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
794                         break;
795                 default:
796                         break;
797                 }
798         }
799         fclose(out);
800         fclose(out_h);
801
802         name = getenv("KCONFIG_AUTOHEADER");
803         if (!name)
804                 name = "include/linux/autoconf.h";
805         if (rename(".tmpconfig.h", name))
806                 return 1;
807         name = getenv("KCONFIG_AUTOCONFIG");
808         if (!name)
809                 name = "include/config/auto.conf";
810         /*
811          * This must be the last step, kbuild has a dependency on auto.conf
812          * and this marks the successful completion of the previous steps.
813          */
814         if (rename(".tmpconfig", name))
815                 return 1;
816
817         return 0;
818 }
819
820 static int sym_change_count;
821 static void (*conf_changed_callback)(void);
822
823 void sym_set_change_count(int count)
824 {
825         int _sym_change_count = sym_change_count;
826         sym_change_count = count;
827         if (conf_changed_callback &&
828             (bool)_sym_change_count != (bool)count)
829                 conf_changed_callback();
830 }
831
832 void sym_add_change_count(int count)
833 {
834         sym_set_change_count(count + sym_change_count);
835 }
836
837 bool conf_get_changed(void)
838 {
839         return sym_change_count;
840 }
841
842 void conf_set_changed_callback(void (*fn)(void))
843 {
844         conf_changed_callback = fn;
845 }