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