blob: 2f778df206f5b4e19b959c9044ab78afd5de40a6 [file] [log] [blame]
Masahiro Yamada0a9064f2014-07-30 14:08:13 +09001/*
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
19static void conf_warning(const char *fmt, ...)
20 __attribute__ ((format (printf, 1, 2)));
21
22static void conf_message(const char *fmt, ...)
23 __attribute__ ((format (printf, 1, 2)));
24
25static const char *conf_filename;
26static int conf_lineno, conf_warnings, conf_unsaved;
27
28const char conf_defname[] = "arch/$ARCH/defconfig";
29
30static 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
41static 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
48static void (*conf_message_callback) (const char *fmt, va_list ap) =
49 conf_default_message_callback;
50void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap))
51{
52 conf_message_callback = fn;
53}
54
55static 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
64const char *conf_get_configname(void)
65{
66 char *name = getenv("KCONFIG_CONFIG");
67
68 return name ? name : ".config";
69}
70
71const char *conf_get_autoconfig_name(void)
72{
73 char *name = getenv("KCONFIG_AUTOCONFIG");
74
75 return name ? name : "include/config/auto.conf";
76}
77
78static 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
104char *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
120static 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 if (def != S_DEF_AUTO)
144 conf_warning("symbol value '%s' invalid for %s",
145 p, sym->name);
146 return 1;
147 case S_OTHER:
148 if (*p != '"') {
149 for (p2 = p; *p2 && !isspace(*p2); p2++)
150 ;
151 sym->type = S_STRING;
152 goto done;
153 }
154 /* fall through */
155 case S_STRING:
156 if (*p++ != '"')
157 break;
Stefan Roese20c20822015-05-29 11:47:32 +0200158 /* Last char has to be a '"' */
159 if (p[strlen(p) - 1] != '"') {
Masahiro Yamada0a9064f2014-07-30 14:08:13 +0900160 if (def != S_DEF_AUTO)
161 conf_warning("invalid string found");
162 return 1;
163 }
Stefan Roese20c20822015-05-29 11:47:32 +0200164 /* Overwrite '"' with \0 for string termination */
165 p[strlen(p) - 1] = 0;
Masahiro Yamada0a9064f2014-07-30 14:08:13 +0900166 /* fall through */
167 case S_INT:
168 case S_HEX:
169 done:
170 if (sym_string_valid(sym, p)) {
171 sym->def[def].val = strdup(p);
172 sym->flags |= def_flags;
173 } else {
174 if (def != S_DEF_AUTO)
175 conf_warning("symbol value '%s' invalid for %s",
176 p, sym->name);
177 return 1;
178 }
179 break;
180 default:
181 ;
182 }
183 return 0;
184}
185
186#define LINE_GROWTH 16
187static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
188{
189 char *nline;
190 size_t new_size = slen + 1;
191 if (new_size > *n) {
192 new_size += LINE_GROWTH - 1;
193 new_size *= 2;
194 nline = realloc(*lineptr, new_size);
195 if (!nline)
196 return -1;
197
198 *lineptr = nline;
199 *n = new_size;
200 }
201
202 (*lineptr)[slen] = c;
203
204 return 0;
205}
206
207static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
208{
209 char *line = *lineptr;
210 size_t slen = 0;
211
212 for (;;) {
213 int c = getc(stream);
214
215 switch (c) {
216 case '\n':
217 if (add_byte(c, &line, slen, n) < 0)
218 goto e_out;
219 slen++;
220 /* fall through */
221 case EOF:
222 if (add_byte('\0', &line, slen, n) < 0)
223 goto e_out;
224 *lineptr = line;
225 if (slen == 0)
226 return -1;
227 return slen;
228 default:
229 if (add_byte(c, &line, slen, n) < 0)
230 goto e_out;
231 slen++;
232 }
233 }
234
235e_out:
236 line[slen-1] = '\0';
237 *lineptr = line;
238 return -1;
239}
240
241int conf_read_simple(const char *name, int def)
242{
243 FILE *in = NULL;
244 char *line = NULL;
245 size_t line_asize = 0;
246 char *p, *p2;
247 struct symbol *sym;
248 int i, def_flags;
249
250 if (name) {
251 in = zconf_fopen(name);
252 } else {
253 struct property *prop;
254
255 name = conf_get_configname();
256 in = zconf_fopen(name);
257 if (in)
258 goto load;
259 sym_add_change_count(1);
260 if (!sym_defconfig_list) {
261 if (modules_sym)
262 sym_calc_value(modules_sym);
263 return 1;
264 }
265
266 for_all_defaults(sym_defconfig_list, prop) {
267 if (expr_calc_value(prop->visible.expr) == no ||
268 prop->expr->type != E_SYMBOL)
269 continue;
270 name = conf_expand_value(prop->expr->left.sym->name);
271 in = zconf_fopen(name);
272 if (in) {
273 conf_message(_("using defaults found in %s"),
274 name);
275 goto load;
276 }
277 }
278 }
279 if (!in)
280 return 1;
281
282load:
283 conf_filename = name;
284 conf_lineno = 0;
285 conf_warnings = 0;
286 conf_unsaved = 0;
287
288 def_flags = SYMBOL_DEF << def;
289 for_all_symbols(i, sym) {
290 sym->flags |= SYMBOL_CHANGED;
291 sym->flags &= ~(def_flags|SYMBOL_VALID);
292 if (sym_is_choice(sym))
293 sym->flags |= def_flags;
294 switch (sym->type) {
295 case S_INT:
296 case S_HEX:
297 case S_STRING:
298 if (sym->def[def].val)
299 free(sym->def[def].val);
300 /* fall through */
301 default:
302 sym->def[def].val = NULL;
303 sym->def[def].tri = no;
304 }
305 }
306
307 while (compat_getline(&line, &line_asize, in) != -1) {
308 conf_lineno++;
309 sym = NULL;
310 if (line[0] == '#') {
311 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
312 continue;
313 p = strchr(line + 2 + strlen(CONFIG_), ' ');
314 if (!p)
315 continue;
316 *p++ = 0;
317 if (strncmp(p, "is not set", 10))
318 continue;
319 if (def == S_DEF_USER) {
320 sym = sym_find(line + 2 + strlen(CONFIG_));
321 if (!sym) {
322 sym_add_change_count(1);
323 goto setsym;
324 }
325 } else {
326 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
327 if (sym->type == S_UNKNOWN)
328 sym->type = S_BOOLEAN;
329 }
330 if (sym->flags & def_flags) {
331 conf_warning("override: reassigning to symbol %s", sym->name);
332 }
333 switch (sym->type) {
334 case S_BOOLEAN:
335 case S_TRISTATE:
336 sym->def[def].tri = no;
337 sym->flags |= def_flags;
338 break;
339 default:
340 ;
341 }
342 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
343 p = strchr(line + strlen(CONFIG_), '=');
344 if (!p)
345 continue;
346 *p++ = 0;
347 p2 = strchr(p, '\n');
348 if (p2) {
349 *p2-- = 0;
350 if (*p2 == '\r')
351 *p2 = 0;
352 }
353 if (def == S_DEF_USER) {
354 sym = sym_find(line + strlen(CONFIG_));
355 if (!sym) {
356 sym_add_change_count(1);
357 goto setsym;
358 }
359 } else {
360 sym = sym_lookup(line + strlen(CONFIG_), 0);
361 if (sym->type == S_UNKNOWN)
362 sym->type = S_OTHER;
363 }
364 if (sym->flags & def_flags) {
365 conf_warning("override: reassigning to symbol %s", sym->name);
366 }
367 if (conf_set_sym_val(sym, def, def_flags, p))
368 continue;
369 } else {
370 if (line[0] != '\r' && line[0] != '\n')
371 conf_warning("unexpected data");
372 continue;
373 }
374setsym:
375 if (sym && sym_is_choice_value(sym)) {
376 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
377 switch (sym->def[def].tri) {
378 case no:
379 break;
380 case mod:
381 if (cs->def[def].tri == yes) {
382 conf_warning("%s creates inconsistent choice state", sym->name);
383 cs->flags &= ~def_flags;
384 }
385 break;
386 case yes:
387 if (cs->def[def].tri != no)
388 conf_warning("override: %s changes choice state", sym->name);
389 cs->def[def].val = sym;
390 break;
391 }
392 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
393 }
394 }
395 free(line);
396 fclose(in);
397
398 if (modules_sym)
399 sym_calc_value(modules_sym);
400 return 0;
401}
402
403int conf_read(const char *name)
404{
405 struct symbol *sym;
406 int i;
407
408 sym_set_change_count(0);
409
410 if (conf_read_simple(name, S_DEF_USER))
411 return 1;
412
413 for_all_symbols(i, sym) {
414 sym_calc_value(sym);
415 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
416 continue;
417 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
418 /* check that calculated value agrees with saved value */
419 switch (sym->type) {
420 case S_BOOLEAN:
421 case S_TRISTATE:
422 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
423 break;
424 if (!sym_is_choice(sym))
425 continue;
426 /* fall through */
427 default:
428 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
429 continue;
430 break;
431 }
432 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
433 /* no previous value and not saved */
434 continue;
435 conf_unsaved++;
436 /* maybe print value in verbose mode... */
437 }
438
439 for_all_symbols(i, sym) {
440 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
441 /* Reset values of generates values, so they'll appear
442 * as new, if they should become visible, but that
443 * doesn't quite work if the Kconfig and the saved
444 * configuration disagree.
445 */
446 if (sym->visible == no && !conf_unsaved)
447 sym->flags &= ~SYMBOL_DEF_USER;
448 switch (sym->type) {
449 case S_STRING:
450 case S_INT:
451 case S_HEX:
452 /* Reset a string value if it's out of range */
453 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
454 break;
455 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
456 conf_unsaved++;
457 break;
458 default:
459 break;
460 }
461 }
462 }
463
464 sym_add_change_count(conf_warnings || conf_unsaved);
465
466 return 0;
467}
468
469/*
470 * Kconfig configuration printer
471 *
472 * This printer is used when generating the resulting configuration after
473 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
474 * passing a non-NULL argument to the printer.
475 *
476 */
477static void
478kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
479{
480
481 switch (sym->type) {
482 case S_BOOLEAN:
483 case S_TRISTATE:
484 if (*value == 'n') {
485 bool skip_unset = (arg != NULL);
486
487 if (!skip_unset)
488 fprintf(fp, "# %s%s is not set\n",
489 CONFIG_, sym->name);
490 return;
491 }
492 break;
493 default:
494 break;
495 }
496
497 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
498}
499
500static void
501kconfig_print_comment(FILE *fp, const char *value, void *arg)
502{
503 const char *p = value;
504 size_t l;
505
506 for (;;) {
507 l = strcspn(p, "\n");
508 fprintf(fp, "#");
509 if (l) {
510 fprintf(fp, " ");
511 xfwrite(p, l, 1, fp);
512 p += l;
513 }
514 fprintf(fp, "\n");
515 if (*p++ == '\0')
516 break;
517 }
518}
519
520static struct conf_printer kconfig_printer_cb =
521{
522 .print_symbol = kconfig_print_symbol,
523 .print_comment = kconfig_print_comment,
524};
525
526/*
527 * Header printer
528 *
529 * This printer is used when generating the `include/generated/autoconf.h' file.
530 */
531static void
532header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
533{
534
535 switch (sym->type) {
536 case S_BOOLEAN:
537 case S_TRISTATE: {
538 const char *suffix = "";
539
540 switch (*value) {
541 case 'n':
542 break;
543 case 'm':
544 suffix = "_MODULE";
545 /* fall through */
546 default:
547 fprintf(fp, "#define %s%s%s 1\n",
548 CONFIG_, sym->name, suffix);
549 }
550 break;
551 }
552 case S_HEX: {
553 const char *prefix = "";
554
555 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
556 prefix = "0x";
557 fprintf(fp, "#define %s%s %s%s\n",
558 CONFIG_, sym->name, prefix, value);
559 break;
560 }
561 case S_STRING:
562 case S_INT:
563 fprintf(fp, "#define %s%s %s\n",
564 CONFIG_, sym->name, value);
565 break;
566 default:
567 break;
568 }
569
570}
571
572static void
573header_print_comment(FILE *fp, const char *value, void *arg)
574{
575 const char *p = value;
576 size_t l;
577
578 fprintf(fp, "/*\n");
579 for (;;) {
580 l = strcspn(p, "\n");
581 fprintf(fp, " *");
582 if (l) {
583 fprintf(fp, " ");
584 xfwrite(p, l, 1, fp);
585 p += l;
586 }
587 fprintf(fp, "\n");
588 if (*p++ == '\0')
589 break;
590 }
591 fprintf(fp, " */\n");
592}
593
594static struct conf_printer header_printer_cb =
595{
596 .print_symbol = header_print_symbol,
597 .print_comment = header_print_comment,
598};
599
600/*
601 * Tristate printer
602 *
603 * This printer is used when generating the `include/config/tristate.conf' file.
604 */
605static void
606tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
607{
608
609 if (sym->type == S_TRISTATE && *value != 'n')
610 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
611}
612
613static struct conf_printer tristate_printer_cb =
614{
615 .print_symbol = tristate_print_symbol,
616 .print_comment = kconfig_print_comment,
617};
618
619static void conf_write_symbol(FILE *fp, struct symbol *sym,
620 struct conf_printer *printer, void *printer_arg)
621{
622 const char *str;
Stefan Roese20c20822015-05-29 11:47:32 +0200623 char *str2;
Masahiro Yamada0a9064f2014-07-30 14:08:13 +0900624
625 switch (sym->type) {
626 case S_OTHER:
627 case S_UNKNOWN:
628 break;
629 case S_STRING:
630 str = sym_get_string_value(sym);
Stefan Roese20c20822015-05-29 11:47:32 +0200631 str2 = xmalloc(strlen(str) + 3);
632 sprintf(str2, "\"%s\"", str);
633 printer->print_symbol(fp, sym, str2, printer_arg);
634 free((void *)str2);
Masahiro Yamada0a9064f2014-07-30 14:08:13 +0900635 break;
636 default:
637 str = sym_get_string_value(sym);
638 printer->print_symbol(fp, sym, str, printer_arg);
639 }
640}
641
642static void
643conf_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 */
660int 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 }
714next_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
733int 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
802next:
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
833static 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 }
938out:
939 if (chdir("../.."))
940 return 1;
941
942 return res;
943}
944
945int conf_write_autoconf(void)
946{
947 struct symbol *sym;
948 const char *name;
949 FILE *out, *tristate, *out_h;
950 int i;
951
Masahiro Yamada0a9064f2014-07-30 14:08:13 +0900952 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
1019static int sym_change_count;
1020static void (*conf_changed_callback)(void);
1021
1022void 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
1031void sym_add_change_count(int count)
1032{
1033 sym_set_change_count(count + sym_change_count);
1034}
1035
1036bool conf_get_changed(void)
1037{
1038 return sym_change_count;
1039}
1040
1041void conf_set_changed_callback(void (*fn)(void))
1042{
1043 conf_changed_callback = fn;
1044}
1045
1046static bool 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 false;
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 sym->flags |= SYMBOL_DEF_USER;
1084 /* clear VALID to get value calculated */
1085 sym->flags &= ~SYMBOL_VALID;
1086 }
1087 csym->flags |= SYMBOL_DEF_USER;
1088 /* clear VALID to get value calculated */
1089 csym->flags &= ~(SYMBOL_VALID);
1090
1091 return true;
1092}
1093
1094void set_all_choice_values(struct symbol *csym)
1095{
1096 struct property *prop;
1097 struct symbol *sym;
1098 struct expr *e;
1099
1100 prop = sym_get_choice_prop(csym);
1101
1102 /*
1103 * Set all non-assinged choice values to no
1104 */
1105 expr_list_for_each_sym(prop->expr, e, sym) {
1106 if (!sym_has_value(sym))
1107 sym->def[S_DEF_USER].tri = no;
1108 }
1109 csym->flags |= SYMBOL_DEF_USER;
1110 /* clear VALID to get value calculated */
1111 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1112}
1113
1114bool conf_set_all_new_symbols(enum conf_def_mode mode)
1115{
1116 struct symbol *sym, *csym;
1117 int i, cnt, pby, pty, ptm; /* pby: probability of boolean = y
1118 * pty: probability of tristate = y
1119 * ptm: probability of tristate = m
1120 */
1121
1122 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1123 * below, otherwise gcc whines about
1124 * -Wmaybe-uninitialized */
1125 if (mode == def_random) {
1126 int n, p[3];
1127 char *env = getenv("KCONFIG_PROBABILITY");
1128 n = 0;
1129 while( env && *env ) {
1130 char *endp;
1131 int tmp = strtol( env, &endp, 10 );
1132 if( tmp >= 0 && tmp <= 100 ) {
1133 p[n++] = tmp;
1134 } else {
1135 errno = ERANGE;
1136 perror( "KCONFIG_PROBABILITY" );
1137 exit( 1 );
1138 }
1139 env = (*endp == ':') ? endp+1 : endp;
1140 if( n >=3 ) {
1141 break;
1142 }
1143 }
1144 switch( n ) {
1145 case 1:
1146 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1147 break;
1148 case 2:
1149 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1150 break;
1151 case 3:
1152 pby = p[0]; pty = p[1]; ptm = p[2];
1153 break;
1154 }
1155
1156 if( pty+ptm > 100 ) {
1157 errno = ERANGE;
1158 perror( "KCONFIG_PROBABILITY" );
1159 exit( 1 );
1160 }
1161 }
1162 bool has_changed = false;
1163
1164 for_all_symbols(i, sym) {
1165 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1166 continue;
1167 switch (sym_get_type(sym)) {
1168 case S_BOOLEAN:
1169 case S_TRISTATE:
1170 has_changed = true;
1171 switch (mode) {
1172 case def_yes:
1173 sym->def[S_DEF_USER].tri = yes;
1174 break;
1175 case def_mod:
1176 sym->def[S_DEF_USER].tri = mod;
1177 break;
1178 case def_no:
1179 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1180 sym->def[S_DEF_USER].tri = yes;
1181 else
1182 sym->def[S_DEF_USER].tri = no;
1183 break;
1184 case def_random:
1185 sym->def[S_DEF_USER].tri = no;
1186 cnt = rand() % 100;
1187 if (sym->type == S_TRISTATE) {
1188 if (cnt < pty)
1189 sym->def[S_DEF_USER].tri = yes;
1190 else if (cnt < (pty+ptm))
1191 sym->def[S_DEF_USER].tri = mod;
1192 } else if (cnt < pby)
1193 sym->def[S_DEF_USER].tri = yes;
1194 break;
1195 default:
1196 continue;
1197 }
1198 if (!(sym_is_choice(sym) && mode == def_random))
1199 sym->flags |= SYMBOL_DEF_USER;
1200 break;
1201 default:
1202 break;
1203 }
1204
1205 }
1206
1207 sym_clear_all_valid();
1208
1209 /*
1210 * We have different type of choice blocks.
1211 * If curr.tri equals to mod then we can select several
1212 * choice symbols in one block.
1213 * In this case we do nothing.
1214 * If curr.tri equals yes then only one symbol can be
1215 * selected in a choice block and we set it to yes,
1216 * and the rest to no.
1217 */
1218 if (mode != def_random) {
1219 for_all_symbols(i, csym) {
1220 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1221 sym_is_choice_value(csym))
1222 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1223 }
1224 }
1225
1226 for_all_symbols(i, csym) {
1227 if (sym_has_value(csym) || !sym_is_choice(csym))
1228 continue;
1229
1230 sym_calc_value(csym);
1231 if (mode == def_random)
1232 has_changed = randomize_choice_values(csym);
1233 else {
1234 set_all_choice_values(csym);
1235 has_changed = true;
1236 }
1237 }
1238
1239 return has_changed;
1240}