blob: fc76e0e88027211be58da87a511bcf51a5793bc0 [file] [log] [blame]
Patrick Georgi0588d192009-08-12 15:00:51 +00001/*
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>
Patrick Georgid5208402014-04-11 20:24:06 +02008#include <errno.h>
Patrick Georgi0588d192009-08-12 15:00:51 +00009#include <fcntl.h>
Patrick Georgid5208402014-04-11 20:24:06 +020010#include <stdarg.h>
Patrick Georgi0588d192009-08-12 15:00:51 +000011#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <time.h>
15#include <unistd.h>
16
Patrick Georgi0588d192009-08-12 15:00:51 +000017#include "lkc.h"
18
19static void conf_warning(const char *fmt, ...)
20 __attribute__ ((format (printf, 1, 2)));
21
Patrick Georgid5208402014-04-11 20:24:06 +020022static void conf_message(const char *fmt, ...)
23 __attribute__ ((format (printf, 1, 2)));
24
Patrick Georgi0588d192009-08-12 15:00:51 +000025static const char *conf_filename;
26static int conf_lineno, conf_warnings, conf_unsaved;
27
28const char conf_defname[] = "arch/$ARCH/defconfig";
29
zbao2f3fd262015-09-26 06:20:53 -040030#ifdef __MINGW32__
31#define mkdir(_n,_p) mkdir((_n))
32#endif
33
Patrick Georgi0588d192009-08-12 15:00:51 +000034static void conf_warning(const char *fmt, ...)
35{
36 va_list ap;
37 va_start(ap, fmt);
38 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
39 vfprintf(stderr, fmt, ap);
40 fprintf(stderr, "\n");
41 va_end(ap);
42 conf_warnings++;
43}
44
Martin Roth00cbc7f2016-09-21 14:27:26 -060045static void conf_notice(const char *fmt, ...)
46{
47 va_list ap;
48 va_start(ap, fmt);
49 fprintf(stderr, "%s:%d:notice: ", conf_filename, conf_lineno);
50 vfprintf(stderr, fmt, ap);
51 fprintf(stderr, "\n");
52 va_end(ap);
53}
54
Patrick Georgid5208402014-04-11 20:24:06 +020055static void conf_default_message_callback(const char *fmt, va_list ap)
56{
57 printf("#\n# ");
58 vprintf(fmt, ap);
59 printf("\n#\n");
60}
61
62static void (*conf_message_callback) (const char *fmt, va_list ap) =
63 conf_default_message_callback;
64void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap))
65{
66 conf_message_callback = fn;
67}
68
69static void conf_message(const char *fmt, ...)
70{
71 va_list ap;
72
73 va_start(ap, fmt);
74 if (conf_message_callback)
75 conf_message_callback(fmt, ap);
Stefan Reinauercce66622015-04-06 01:14:21 +020076 va_end(ap);
Patrick Georgid5208402014-04-11 20:24:06 +020077}
78
Patrick Georgi0588d192009-08-12 15:00:51 +000079const char *conf_get_configname(void)
80{
81 char *name = getenv("KCONFIG_CONFIG");
82
83 return name ? name : ".config";
84}
85
Patrick Georgid5208402014-04-11 20:24:06 +020086const char *conf_get_autoconfig_name(void)
87{
88 char *name = getenv("KCONFIG_AUTOCONFIG");
89
90 return name ? name : "include/config/auto.conf";
91}
92
Patrick Georgi0588d192009-08-12 15:00:51 +000093static char *conf_expand_value(const char *in)
94{
95 struct symbol *sym;
96 const char *src;
97 static char res_value[SYMBOL_MAXLENGTH];
98 char *dst, name[SYMBOL_MAXLENGTH];
99
100 res_value[0] = 0;
101 dst = name;
102 while ((src = strchr(in, '$'))) {
103 strncat(res_value, in, src - in);
104 src++;
105 dst = name;
106 while (isalnum(*src) || *src == '_')
107 *dst++ = *src++;
108 *dst = 0;
109 sym = sym_lookup(name, 0);
110 sym_calc_value(sym);
111 strcat(res_value, sym_get_string_value(sym));
112 in = src;
113 }
114 strcat(res_value, in);
115
116 return res_value;
117}
118
119char *conf_get_default_confname(void)
120{
121 struct stat buf;
122 static char fullname[PATH_MAX+1];
123 char *env, *name;
124
125 name = conf_expand_value(conf_defname);
126 env = getenv(SRCTREE);
127 if (env) {
128 sprintf(fullname, "%s/%s", env, name);
129 if (!stat(fullname, &buf))
130 return fullname;
131 }
132 return name;
133}
134
135static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
136{
137 char *p2;
138
139 switch (sym->type) {
140 case S_TRISTATE:
141 if (p[0] == 'm') {
142 sym->def[def].tri = mod;
143 sym->flags |= def_flags;
144 break;
145 }
Patrick Georgid5208402014-04-11 20:24:06 +0200146 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000147 case S_BOOLEAN:
148 if (p[0] == 'y') {
149 sym->def[def].tri = yes;
150 sym->flags |= def_flags;
151 break;
152 }
153 if (p[0] == 'n') {
154 sym->def[def].tri = no;
155 sym->flags |= def_flags;
156 break;
157 }
Patrick Georgid5208402014-04-11 20:24:06 +0200158 if (def != S_DEF_AUTO)
159 conf_warning("symbol value '%s' invalid for %s",
160 p, sym->name);
161 return 1;
Patrick Georgi0588d192009-08-12 15:00:51 +0000162 case S_OTHER:
163 if (*p != '"') {
164 for (p2 = p; *p2 && !isspace(*p2); p2++)
165 ;
166 sym->type = S_STRING;
167 goto done;
168 }
Patrick Georgid5208402014-04-11 20:24:06 +0200169 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000170 case S_STRING:
171 if (*p++ != '"')
172 break;
173 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
174 if (*p2 == '"') {
175 *p2 = 0;
176 break;
177 }
178 memmove(p2, p2 + 1, strlen(p2));
179 }
180 if (!p2) {
Patrick Georgid5208402014-04-11 20:24:06 +0200181 if (def != S_DEF_AUTO)
182 conf_warning("invalid string found");
Patrick Georgi0588d192009-08-12 15:00:51 +0000183 return 1;
184 }
Patrick Georgid5208402014-04-11 20:24:06 +0200185 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000186 case S_INT:
187 case S_HEX:
188 done:
189 if (sym_string_valid(sym, p)) {
190 sym->def[def].val = strdup(p);
191 sym->flags |= def_flags;
192 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200193 if (def != S_DEF_AUTO)
194 conf_warning("symbol value '%s' invalid for %s",
195 p, sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000196 return 1;
197 }
198 break;
199 default:
200 ;
201 }
202 return 0;
203}
204
Patrick Georgid5208402014-04-11 20:24:06 +0200205#define LINE_GROWTH 16
206static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
207{
208 char *nline;
209 size_t new_size = slen + 1;
210 if (new_size > *n) {
211 new_size += LINE_GROWTH - 1;
212 new_size *= 2;
213 nline = realloc(*lineptr, new_size);
214 if (!nline)
215 return -1;
216
217 *lineptr = nline;
218 *n = new_size;
219 }
220
221 (*lineptr)[slen] = c;
222
223 return 0;
224}
225
226static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
227{
228 char *line = *lineptr;
229 size_t slen = 0;
230
231 for (;;) {
232 int c = getc(stream);
233
234 switch (c) {
235 case '\n':
236 if (add_byte(c, &line, slen, n) < 0)
237 goto e_out;
238 slen++;
239 /* fall through */
240 case EOF:
241 if (add_byte('\0', &line, slen, n) < 0)
242 goto e_out;
243 *lineptr = line;
244 if (slen == 0)
245 return -1;
246 return slen;
247 default:
248 if (add_byte(c, &line, slen, n) < 0)
249 goto e_out;
250 slen++;
251 }
252 }
253
254e_out:
255 line[slen-1] = '\0';
256 *lineptr = line;
257 return -1;
258}
259
Patrick Georgi0588d192009-08-12 15:00:51 +0000260int conf_read_simple(const char *name, int def)
261{
262 FILE *in = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +0200263 char *line = NULL;
264 size_t line_asize = 0;
Patrick Georgi0588d192009-08-12 15:00:51 +0000265 char *p, *p2;
266 struct symbol *sym;
267 int i, def_flags;
268
269 if (name) {
270 in = zconf_fopen(name);
271 } else {
272 struct property *prop;
273
274 name = conf_get_configname();
275 in = zconf_fopen(name);
276 if (in)
277 goto load;
278 sym_add_change_count(1);
Patrick Georgid5208402014-04-11 20:24:06 +0200279 if (!sym_defconfig_list) {
280 if (modules_sym)
281 sym_calc_value(modules_sym);
Patrick Georgi0588d192009-08-12 15:00:51 +0000282 return 1;
Patrick Georgid5208402014-04-11 20:24:06 +0200283 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000284
285 for_all_defaults(sym_defconfig_list, prop) {
286 if (expr_calc_value(prop->visible.expr) == no ||
287 prop->expr->type != E_SYMBOL)
288 continue;
289 name = conf_expand_value(prop->expr->left.sym->name);
290 in = zconf_fopen(name);
291 if (in) {
Patrick Georgid5208402014-04-11 20:24:06 +0200292 conf_message(_("using defaults found in %s"),
293 name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000294 goto load;
295 }
296 }
297 }
298 if (!in)
299 return 1;
300
301load:
302 conf_filename = name;
303 conf_lineno = 0;
304 conf_warnings = 0;
305 conf_unsaved = 0;
306
307 def_flags = SYMBOL_DEF << def;
308 for_all_symbols(i, sym) {
309 sym->flags |= SYMBOL_CHANGED;
310 sym->flags &= ~(def_flags|SYMBOL_VALID);
311 if (sym_is_choice(sym))
312 sym->flags |= def_flags;
313 switch (sym->type) {
314 case S_INT:
315 case S_HEX:
316 case S_STRING:
317 if (sym->def[def].val)
318 free(sym->def[def].val);
Patrick Georgid5208402014-04-11 20:24:06 +0200319 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000320 default:
321 sym->def[def].val = NULL;
322 sym->def[def].tri = no;
323 }
324 }
325
Patrick Georgid5208402014-04-11 20:24:06 +0200326 while (compat_getline(&line, &line_asize, in) != -1) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000327 conf_lineno++;
328 sym = NULL;
Patrick Georgid5208402014-04-11 20:24:06 +0200329 if (line[0] == '#') {
330 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Patrick Georgi0588d192009-08-12 15:00:51 +0000331 continue;
Patrick Georgid5208402014-04-11 20:24:06 +0200332 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Patrick Georgi0588d192009-08-12 15:00:51 +0000333 if (!p)
334 continue;
335 *p++ = 0;
336 if (strncmp(p, "is not set", 10))
337 continue;
338 if (def == S_DEF_USER) {
Patrick Georgid5208402014-04-11 20:24:06 +0200339 sym = sym_find(line + 2 + strlen(CONFIG_));
Patrick Georgi0588d192009-08-12 15:00:51 +0000340 if (!sym) {
Patrick Georgid5208402014-04-11 20:24:06 +0200341 sym_add_change_count(1);
342 goto setsym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000343 }
344 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200345 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Patrick Georgi0588d192009-08-12 15:00:51 +0000346 if (sym->type == S_UNKNOWN)
347 sym->type = S_BOOLEAN;
348 }
349 if (sym->flags & def_flags) {
Martin Roth00cbc7f2016-09-21 14:27:26 -0600350 conf_notice("override: reassigning to symbol %s", sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000351 }
352 switch (sym->type) {
353 case S_BOOLEAN:
354 case S_TRISTATE:
355 sym->def[def].tri = no;
356 sym->flags |= def_flags;
357 break;
358 default:
359 ;
360 }
Patrick Georgid5208402014-04-11 20:24:06 +0200361 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
362 p = strchr(line + strlen(CONFIG_), '=');
Patrick Georgi0588d192009-08-12 15:00:51 +0000363 if (!p)
364 continue;
365 *p++ = 0;
366 p2 = strchr(p, '\n');
367 if (p2) {
368 *p2-- = 0;
369 if (*p2 == '\r')
370 *p2 = 0;
371 }
372 if (def == S_DEF_USER) {
Patrick Georgid5208402014-04-11 20:24:06 +0200373 sym = sym_find(line + strlen(CONFIG_));
Patrick Georgi0588d192009-08-12 15:00:51 +0000374 if (!sym) {
Martin Rothc37c7c82016-02-10 16:06:00 -0700375 conf_message(
376 "ignoring nonexistent symbol %s",
377 line + strlen(CONFIG_));
Patrick Georgid5208402014-04-11 20:24:06 +0200378 sym_add_change_count(1);
379 goto setsym;
Patrick Georgi0588d192009-08-12 15:00:51 +0000380 }
381 } else {
Patrick Georgid5208402014-04-11 20:24:06 +0200382 sym = sym_lookup(line + strlen(CONFIG_), 0);
Patrick Georgi0588d192009-08-12 15:00:51 +0000383 if (sym->type == S_UNKNOWN)
384 sym->type = S_OTHER;
385 }
386 if (sym->flags & def_flags) {
Martin Roth00cbc7f2016-09-21 14:27:26 -0600387 conf_notice("override: reassigning to symbol %s", sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000388 }
389 if (conf_set_sym_val(sym, def, def_flags, p))
390 continue;
Patrick Georgid5208402014-04-11 20:24:06 +0200391 } else {
392 if (line[0] != '\r' && line[0] != '\n')
393 conf_warning("unexpected data");
Patrick Georgi0588d192009-08-12 15:00:51 +0000394 continue;
395 }
Patrick Georgid5208402014-04-11 20:24:06 +0200396setsym:
Patrick Georgi0588d192009-08-12 15:00:51 +0000397 if (sym && sym_is_choice_value(sym)) {
398 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
399 switch (sym->def[def].tri) {
400 case no:
401 break;
402 case mod:
403 if (cs->def[def].tri == yes) {
404 conf_warning("%s creates inconsistent choice state", sym->name);
405 cs->flags &= ~def_flags;
406 }
407 break;
408 case yes:
409 if (cs->def[def].tri != no)
Martin Roth00cbc7f2016-09-21 14:27:26 -0600410 conf_notice("override: %s changes choice state", sym->name);
Patrick Georgi0588d192009-08-12 15:00:51 +0000411 cs->def[def].val = sym;
412 break;
413 }
414 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
415 }
416 }
Patrick Georgid5208402014-04-11 20:24:06 +0200417 free(line);
Patrick Georgi0588d192009-08-12 15:00:51 +0000418 fclose(in);
419
420 if (modules_sym)
421 sym_calc_value(modules_sym);
Stefan Reinauerf5342032015-07-17 17:26:48 -0700422
Stefan Reinauer57a31312015-08-20 11:19:34 -0700423 kconfig_warnings += conf_warnings;
Stefan Reinauerf5342032015-07-17 17:26:48 -0700424
Patrick Georgi0588d192009-08-12 15:00:51 +0000425 return 0;
426}
427
428int conf_read(const char *name)
429{
Patrick Georgid5208402014-04-11 20:24:06 +0200430 struct symbol *sym;
431 int i;
Patrick Georgi0588d192009-08-12 15:00:51 +0000432
433 sym_set_change_count(0);
434
435 if (conf_read_simple(name, S_DEF_USER))
436 return 1;
437
438 for_all_symbols(i, sym) {
439 sym_calc_value(sym);
440 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
Patrick Georgid5208402014-04-11 20:24:06 +0200441 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000442 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
443 /* check that calculated value agrees with saved value */
444 switch (sym->type) {
445 case S_BOOLEAN:
446 case S_TRISTATE:
447 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
448 break;
449 if (!sym_is_choice(sym))
Patrick Georgid5208402014-04-11 20:24:06 +0200450 continue;
451 /* fall through */
Patrick Georgi0588d192009-08-12 15:00:51 +0000452 default:
453 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Patrick Georgid5208402014-04-11 20:24:06 +0200454 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000455 break;
456 }
457 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
458 /* no previous value and not saved */
Patrick Georgid5208402014-04-11 20:24:06 +0200459 continue;
Patrick Georgi0588d192009-08-12 15:00:51 +0000460 conf_unsaved++;
461 /* maybe print value in verbose mode... */
Patrick Georgi0588d192009-08-12 15:00:51 +0000462 }
463
464 for_all_symbols(i, sym) {
465 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
466 /* Reset values of generates values, so they'll appear
467 * as new, if they should become visible, but that
468 * doesn't quite work if the Kconfig and the saved
469 * configuration disagree.
470 */
471 if (sym->visible == no && !conf_unsaved)
472 sym->flags &= ~SYMBOL_DEF_USER;
473 switch (sym->type) {
474 case S_STRING:
475 case S_INT:
476 case S_HEX:
477 /* Reset a string value if it's out of range */
478 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
479 break;
480 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
481 conf_unsaved++;
482 break;
483 default:
484 break;
485 }
486 }
487 }
488
489 sym_add_change_count(conf_warnings || conf_unsaved);
490
491 return 0;
492}
493
Patrick Georgid5208402014-04-11 20:24:06 +0200494/*
495 * Kconfig configuration printer
496 *
497 * This printer is used when generating the resulting configuration after
498 * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
499 * passing a non-NULL argument to the printer.
500 *
501 */
502static void
503kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
504{
505
506 switch (sym->type) {
507 case S_BOOLEAN:
508 case S_TRISTATE:
509 if (*value == 'n') {
510 bool skip_unset = (arg != NULL);
511
512 if (!skip_unset)
513 fprintf(fp, "# %s%s is not set\n",
514 CONFIG_, sym->name);
515 return;
516 }
517 break;
518 default:
519 break;
520 }
521
522 fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
523}
524
525static void
526kconfig_print_comment(FILE *fp, const char *value, void *arg)
527{
528 const char *p = value;
529 size_t l;
530
531 for (;;) {
532 l = strcspn(p, "\n");
533 fprintf(fp, "#");
534 if (l) {
535 fprintf(fp, " ");
536 xfwrite(p, l, 1, fp);
537 p += l;
538 }
539 fprintf(fp, "\n");
540 if (*p++ == '\0')
541 break;
542 }
543}
544
545static struct conf_printer kconfig_printer_cb =
546{
547 .print_symbol = kconfig_print_symbol,
548 .print_comment = kconfig_print_comment,
549};
550
551/*
552 * Header printer
553 *
554 * This printer is used when generating the `include/generated/autoconf.h' file.
555 */
556static void
557header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
558{
559
560 switch (sym->type) {
561 case S_BOOLEAN:
562 case S_TRISTATE: {
563 const char *suffix = "";
564
565 switch (*value) {
566 case 'n':
567 if (getenv("KCONFIG_NEGATIVES")) {
568 fprintf(fp, "#define %s%s%s 0\n",
569 CONFIG_, sym->name, suffix);
570 }
571 break;
572 case 'm':
573 suffix = "_MODULE";
574 /* fall through */
575 default:
576 fprintf(fp, "#define %s%s%s 1\n",
577 CONFIG_, sym->name, suffix);
578 }
579 break;
580 }
581 case S_HEX: {
582 const char *prefix = "";
583
584 if (!value || (value[0] == '\0')) {
585 value = "0";
586 } else
587 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
588 prefix = "0x";
589 fprintf(fp, "#define %s%s %s%s\n",
590 CONFIG_, sym->name, prefix, value);
591 break;
592 }
593 case S_INT:
594 if (!value || (value[0] == '\0')) {
595 value = "0";
596 }
597 /* fall through */
598 case S_STRING:
599 fprintf(fp, "#define %s%s %s\n",
600 CONFIG_, sym->name, value);
601 break;
602 default:
603 break;
604 }
605
606}
607
608static void
609header_print_comment(FILE *fp, const char *value, void *arg)
610{
611 const char *p = value;
612 size_t l;
613
614 fprintf(fp, "/*\n");
615 for (;;) {
616 l = strcspn(p, "\n");
617 fprintf(fp, " *");
618 if (l) {
619 fprintf(fp, " ");
620 xfwrite(p, l, 1, fp);
621 p += l;
622 }
623 fprintf(fp, "\n");
624 if (*p++ == '\0')
625 break;
626 }
627 fprintf(fp, " */\n");
628}
629
630static struct conf_printer header_printer_cb =
631{
632 .print_symbol = header_print_symbol,
633 .print_comment = header_print_comment,
634};
635
636/*
637 * Tristate printer
638 *
639 * This printer is used when generating the `include/config/tristate.conf' file.
640 */
641static void
642tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
643{
644
645 if (sym->type == S_TRISTATE && (*value != 'n' || arg == NULL))
646 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
647}
648
649static struct conf_printer tristate_printer_cb =
650{
651 .print_symbol = tristate_print_symbol,
652 .print_comment = kconfig_print_comment,
653};
654
655static void conf_write_symbol(FILE *fp, struct symbol *sym,
656 struct conf_printer *printer, void *printer_arg)
657{
658 const char *str;
659
660 switch (sym->type) {
661 case S_OTHER:
662 case S_UNKNOWN:
663 break;
664 case S_STRING:
665 str = sym_get_string_value(sym);
666 str = sym_escape_string_value(str);
667 printer->print_symbol(fp, sym, str, printer_arg);
668 free((void *)str);
669 break;
670 default:
671 str = sym_get_string_value(sym);
672 printer->print_symbol(fp, sym, str, printer_arg);
673 }
674}
675
676static void
677conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
678{
679 char buf[256];
680
681 snprintf(buf, sizeof(buf),
682 "\n"
683 "Automatically generated file; DO NOT EDIT.\n"
684 "%s\n",
685 rootmenu.prompt->text);
686
687 printer->print_comment(fp, buf, printer_arg);
688}
689
690/*
691 * Write out a minimal config.
692 * All values that has default values are skipped as this is redundant.
693 */
694int conf_write_defconfig(const char *filename)
695{
696 struct symbol *sym;
697 struct menu *menu;
698 FILE *out;
699
700 out = fopen(filename, "w");
701 if (!out)
702 return 1;
703
704 sym_clear_all_valid();
705
706 /* Traverse all menus to find all relevant symbols */
707 menu = rootmenu.list;
708
709 while (menu != NULL)
710 {
711 sym = menu->sym;
712 if (sym == NULL) {
713 if (!menu_is_visible(menu))
714 goto next_menu;
715 } else if (!sym_is_choice(sym)) {
716 sym_calc_value(sym);
717 if (!(sym->flags & SYMBOL_WRITE))
718 goto next_menu;
719 sym->flags &= ~SYMBOL_WRITE;
720 /* If we cannot change the symbol - skip */
721 if (!sym_is_changable(sym))
722 goto next_menu;
723 /* If symbol equals to default value - skip */
724 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
725 goto next_menu;
726
727 /*
728 * If symbol is a choice value and equals to the
729 * default for a choice - skip.
730 * But only if value is bool and equal to "y" and
731 * choice is not "optional".
732 * (If choice is "optional" then all values can be "n")
733 */
734 if (sym_is_choice_value(sym)) {
735 struct symbol *cs;
736 struct symbol *ds;
737
738 cs = prop_get_symbol(sym_get_choice_prop(sym));
739 ds = sym_choice_default(cs);
740 if (!sym_is_optional(cs) && sym == ds) {
741 if ((sym->type == S_BOOLEAN) &&
742 sym_get_tristate_value(sym) == yes)
743 goto next_menu;
744 }
745 }
746 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
747 }
748next_menu:
749 if (menu->list != NULL) {
750 menu = menu->list;
751 }
752 else if (menu->next != NULL) {
753 menu = menu->next;
754 } else {
755 while ((menu = menu->parent)) {
756 if (menu->next != NULL) {
757 menu = menu->next;
758 break;
759 }
760 }
761 }
762 }
763 fclose(out);
764 return 0;
765}
766
Raul E Rangel7b2dedd2019-07-25 15:49:52 -0600767
768int conf_mktemp(const char *path, char *tmpfile)
769{
770 if (snprintf(tmpfile, PATH_MAX, "%s.tmp.XXXXXX", path) >= PATH_MAX) {
771 errno = EOVERFLOW;
772 return -1;
773 }
774 return mkstemp(tmpfile);
775}
776
Patrick Georgi0588d192009-08-12 15:00:51 +0000777int conf_write(const char *name)
778{
779 FILE *out;
780 struct symbol *sym;
781 struct menu *menu;
Raul E Rangeld2f90a02019-07-25 16:00:50 -0600782 const char *basename = NULL;
Patrick Georgi0588d192009-08-12 15:00:51 +0000783 const char *str;
Patrick Georgid5208402014-04-11 20:24:06 +0200784 char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
Patrick Georgi0588d192009-08-12 15:00:51 +0000785 char *env;
786
787 dirname[0] = 0;
788 if (name && name[0]) {
789 struct stat st;
Patrick Georgi0588d192009-08-12 15:00:51 +0000790
791 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
792 strcpy(dirname, name);
793 strcat(dirname, "/");
794 basename = conf_get_configname();
Raul E Rangeld2f90a02019-07-25 16:00:50 -0600795 }
796 } else {
797 name = conf_get_configname();
798 }
799
800 if (!basename) {
801 char *slash = strrchr(name, '/');
802
803 if (slash) {
Patrick Georgi0588d192009-08-12 15:00:51 +0000804 int size = slash - name + 1;
805 memcpy(dirname, name, size);
806 dirname[size] = 0;
807 if (slash[1])
808 basename = slash + 1;
809 else
810 basename = conf_get_configname();
Raul E Rangeld2f90a02019-07-25 16:00:50 -0600811 } else {
Patrick Georgi0588d192009-08-12 15:00:51 +0000812 basename = name;
Raul E Rangeld2f90a02019-07-25 16:00:50 -0600813 }
814 }
Patrick Georgi0588d192009-08-12 15:00:51 +0000815
816 sprintf(newname, "%s%s", dirname, basename);
817 env = getenv("KCONFIG_OVERWRITECONFIG");
818 if (!env || !*env) {
Raul E Rangel7b2dedd2019-07-25 15:49:52 -0600819 conf_mktemp(newname, tmpname);
Patrick Georgi0588d192009-08-12 15:00:51 +0000820 out = fopen(tmpname, "w");
821 } else {
822 *tmpname = 0;
823 out = fopen(newname, "w");
824 }
825 if (!out)
826 return 1;
827
Patrick Georgid5208402014-04-11 20:24:06 +0200828 conf_write_heading(out, &kconfig_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +0000829
830 if (!conf_get_changed())
831 sym_clear_all_valid();
832
833 menu = rootmenu.list;
834 while (menu) {
835 sym = menu->sym;
836 if (!sym) {
837 if (!menu_is_visible(menu))
838 goto next;
839 str = menu_get_prompt(menu);
840 fprintf(out, "\n"
841 "#\n"
842 "# %s\n"
843 "#\n", str);
844 } else if (!(sym->flags & SYMBOL_CHOICE)) {
845 sym_calc_value(sym);
846 if (!(sym->flags & SYMBOL_WRITE))
847 goto next;
848 sym->flags &= ~SYMBOL_WRITE;
Patrick Georgid5208402014-04-11 20:24:06 +0200849
850 conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +0000851 }
852
Patrick Georgid5208402014-04-11 20:24:06 +0200853next:
Patrick Georgi0588d192009-08-12 15:00:51 +0000854 if (menu->list) {
855 menu = menu->list;
856 continue;
857 }
858 if (menu->next)
859 menu = menu->next;
860 else while ((menu = menu->parent)) {
861 if (menu->next) {
862 menu = menu->next;
863 break;
864 }
865 }
866 }
867 fclose(out);
868
869 if (*tmpname) {
870 strcat(dirname, basename);
871 strcat(dirname, ".old");
872 rename(newname, dirname);
873 if (rename(tmpname, newname))
874 return 1;
875 }
876
Patrick Georgid5208402014-04-11 20:24:06 +0200877 conf_message(_("configuration written to %s"), newname);
Patrick Georgi0588d192009-08-12 15:00:51 +0000878
879 sym_set_change_count(0);
880
881 return 0;
882}
883
Patrick Georgid5208402014-04-11 20:24:06 +0200884static int conf_split_config(void)
Patrick Georgi0588d192009-08-12 15:00:51 +0000885{
Patrick Georgid5208402014-04-11 20:24:06 +0200886 const char *name;
887 char path[PATH_MAX+1];
888 char pwd[PATH_MAX+1];
Patrick Georgi0588d192009-08-12 15:00:51 +0000889 char *s, *d, c;
890 struct symbol *sym;
891 struct stat sb;
892 int res, i, fd;
893
Patrick Georgid5208402014-04-11 20:24:06 +0200894 name = conf_get_autoconfig_name();
Patrick Georgi0588d192009-08-12 15:00:51 +0000895 conf_read_simple(name, S_DEF_AUTO);
896
Patrick Georgid5208402014-04-11 20:24:06 +0200897 getcwd(pwd, sizeof(pwd));
898 name = getenv("KCONFIG_SPLITCONFIG");
899 if (!name)
900 name = "include/config";
901 if (chdir(name))
Patrick Georgi0588d192009-08-12 15:00:51 +0000902 return 1;
903
904 res = 0;
905 for_all_symbols(i, sym) {
906 sym_calc_value(sym);
907 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
908 continue;
909 if (sym->flags & SYMBOL_WRITE) {
910 if (sym->flags & SYMBOL_DEF_AUTO) {
911 /*
912 * symbol has old and new value,
913 * so compare them...
914 */
915 switch (sym->type) {
916 case S_BOOLEAN:
917 case S_TRISTATE:
918 if (sym_get_tristate_value(sym) ==
919 sym->def[S_DEF_AUTO].tri)
920 continue;
921 break;
922 case S_STRING:
923 case S_HEX:
924 case S_INT:
925 if (!strcmp(sym_get_string_value(sym),
926 sym->def[S_DEF_AUTO].val))
927 continue;
928 break;
929 default:
930 break;
931 }
932 } else {
933 /*
934 * If there is no old value, only 'no' (unset)
935 * is allowed as new value.
936 */
937 switch (sym->type) {
938 case S_BOOLEAN:
939 case S_TRISTATE:
940 if (sym_get_tristate_value(sym) == no)
941 continue;
942 break;
943 default:
944 break;
945 }
946 }
947 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
948 /* There is neither an old nor a new value. */
949 continue;
950 /* else
951 * There is an old value, but no new value ('no' (unset)
952 * isn't saved in auto.conf, so the old value is always
953 * different from 'no').
954 */
955
956 /* Replace all '_' and append ".h" */
957 s = sym->name;
958 d = path;
959 while ((c = *s++)) {
960 c = tolower(c);
961 *d++ = (c == '_') ? '/' : c;
962 }
963 strcpy(d, ".h");
964
965 /* Assume directory path already exists. */
966 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
967 if (fd == -1) {
968 if (errno != ENOENT) {
969 res = 1;
970 break;
971 }
972 /*
973 * Create directory components,
974 * unless they exist already.
975 */
976 d = path;
977 while ((d = strchr(d, '/'))) {
978 *d = 0;
979 if (stat(path, &sb) && mkdir(path, 0755)) {
980 res = 1;
981 goto out;
982 }
983 *d++ = '/';
984 }
985 /* Try it again. */
986 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
987 if (fd == -1) {
988 res = 1;
989 break;
990 }
991 }
992 close(fd);
993 }
994out:
Patrick Georgid5208402014-04-11 20:24:06 +0200995 if (chdir(pwd))
Patrick Georgi0588d192009-08-12 15:00:51 +0000996 return 1;
997
998 return res;
999}
1000
1001int conf_write_autoconf(void)
1002{
1003 struct symbol *sym;
Patrick Georgid5208402014-04-11 20:24:06 +02001004 FILE *out, *tristate, *out_h;
1005 int i;
1006 int print_negatives;
1007
1008 print_negatives = getenv("KCONFIG_NEGATIVES") != NULL;
Patrick Georgi0588d192009-08-12 15:00:51 +00001009
1010 sym_clear_all_valid();
1011
Patrick Georgid5208402014-04-11 20:24:06 +02001012 char *dep_file = getenv("KCONFIG_DEPENDENCIES");
1013 if (!dep_file)
1014 dep_file = "include/config/auto.conf.cmd";
1015 file_write_dep(dep_file);
Vadim Bendeburyc302d202011-10-24 14:06:23 -07001016
Patrick Georgi0588d192009-08-12 15:00:51 +00001017 if (conf_split_config())
1018 return 1;
Patrick Georgi0588d192009-08-12 15:00:51 +00001019
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001020 char tmpconfig_name[PATH_MAX];
1021 const char *config_name = conf_get_autoconfig_name();
1022
1023 i = conf_mktemp(config_name, tmpconfig_name);
1024 if (i == -1)
1025 goto error_auto_conf_cmd_tmp;
Patrick Georgid5208402014-04-11 20:24:06 +02001026 out = fdopen(i, "w");
Patrick Georgi0588d192009-08-12 15:00:51 +00001027 if (!out)
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001028 goto error_auto_conf_cmd_open;
Patrick Georgi0588d192009-08-12 15:00:51 +00001029
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001030 char tmpconfig_triname[PATH_MAX];
1031 const char *config_triname = getenv("KCONFIG_TRISTATE");
1032 if (!config_triname)
1033 config_triname = "include/config/tristate.conf";
1034
1035 i = conf_mktemp(config_triname, tmpconfig_triname);
1036 if (i == -1)
1037 goto error_tristate_tmp;
1038
Patrick Georgid5208402014-04-11 20:24:06 +02001039 tristate = fdopen(i, "w");
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001040 if (!tristate)
1041 goto error_tristate_open;
Patrick Georgi0588d192009-08-12 15:00:51 +00001042
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001043 char tmpconfig_h[PATH_MAX];
1044 const char *config_h = getenv("KCONFIG_AUTOHEADER");
1045 if (!config_h)
1046 config_h = "include/generated/autoconf.h";
1047
1048 i = conf_mktemp(config_h, tmpconfig_h);
1049 if (i == -1)
1050 goto error_auto_conf_h_tmp;
1051
Patrick Georgid5208402014-04-11 20:24:06 +02001052 out_h = fdopen(i, "w");
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001053 if (!out_h)
1054 goto error_auto_conf_h_open;
Patrick Georgid5208402014-04-11 20:24:06 +02001055
1056 conf_write_heading(out, &kconfig_printer_cb, NULL);
1057
1058 conf_write_heading(tristate, &tristate_printer_cb, NULL);
1059
1060 conf_write_heading(out_h, &header_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +00001061
1062 for_all_symbols(i, sym) {
1063 sym_calc_value(sym);
Stefan Reinauerebc93def2011-04-18 02:07:16 +00001064 if (!sym->name)
Patrick Georgi0588d192009-08-12 15:00:51 +00001065 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001066
1067 if (!(sym->flags & SYMBOL_WRITE) && !print_negatives)
Stefan Reinauerebc93def2011-04-18 02:07:16 +00001068 continue;
Patrick Georgid5208402014-04-11 20:24:06 +02001069
1070 /* these are safe to write out, so do it all the time */
1071 if (!(sym->flags & SYMBOL_WRITE) &&
1072 !(sym->type == S_BOOLEAN ||
1073 sym->type == S_HEX ||
1074 sym->type == S_INT))
1075 continue;
1076
1077 /* write symbol to auto.conf, tristate and header files */
1078 conf_write_symbol(out, sym, &kconfig_printer_cb, print_negatives?NULL:(void *)1);
1079
1080 conf_write_symbol(tristate, sym, &tristate_printer_cb, print_negatives?NULL:(void *)1);
1081
1082 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
Patrick Georgi0588d192009-08-12 15:00:51 +00001083 }
1084 fclose(out);
Patrick Georgid5208402014-04-11 20:24:06 +02001085 fclose(tristate);
Patrick Georgi0588d192009-08-12 15:00:51 +00001086 fclose(out_h);
1087
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001088 if (rename(tmpconfig_h, config_h))
Patrick Georgid5208402014-04-11 20:24:06 +02001089 return 1;
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001090
1091 if (rename(tmpconfig_triname, config_triname))
Patrick Georgid5208402014-04-11 20:24:06 +02001092 return 1;
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001093
Patrick Georgi0588d192009-08-12 15:00:51 +00001094 /*
1095 * This must be the last step, kbuild has a dependency on auto.conf
1096 * and this marks the successful completion of the previous steps.
1097 */
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001098 if (rename(tmpconfig_name, config_name))
Patrick Georgi0588d192009-08-12 15:00:51 +00001099 return 1;
1100
Patrick Georgi0588d192009-08-12 15:00:51 +00001101 return 0;
Raul E Rangel7b2dedd2019-07-25 15:49:52 -06001102
1103error_auto_conf_h_open:
1104 unlink(tmpconfig_h);
1105
1106error_auto_conf_h_tmp:
1107 fclose(tristate);
1108
1109error_tristate_open:
1110 unlink(tmpconfig_triname);
1111
1112error_tristate_tmp:
1113 fclose(out);
1114
1115error_auto_conf_cmd_open:
1116 unlink(tmpconfig_name);
1117
1118error_auto_conf_cmd_tmp:
1119 return 1;
Patrick Georgi0588d192009-08-12 15:00:51 +00001120}
1121
1122static int sym_change_count;
1123static void (*conf_changed_callback)(void);
1124
1125void sym_set_change_count(int count)
1126{
1127 int _sym_change_count = sym_change_count;
1128 sym_change_count = count;
1129 if (conf_changed_callback &&
1130 (bool)_sym_change_count != (bool)count)
1131 conf_changed_callback();
1132}
1133
1134void sym_add_change_count(int count)
1135{
1136 sym_set_change_count(count + sym_change_count);
1137}
1138
1139bool conf_get_changed(void)
1140{
1141 return sym_change_count;
1142}
1143
1144void conf_set_changed_callback(void (*fn)(void))
1145{
1146 conf_changed_callback = fn;
1147}
Patrick Georgid5208402014-04-11 20:24:06 +02001148
1149static bool randomize_choice_values(struct symbol *csym)
1150{
1151 struct property *prop;
1152 struct symbol *sym;
1153 struct expr *e;
1154 int cnt, def;
1155
1156 /*
1157 * If choice is mod then we may have more items selected
1158 * and if no then no-one.
1159 * In both cases stop.
1160 */
1161 if (csym->curr.tri != yes)
1162 return false;
1163
1164 prop = sym_get_choice_prop(csym);
1165
1166 /* count entries in choice block */
1167 cnt = 0;
1168 expr_list_for_each_sym(prop->expr, e, sym)
1169 cnt++;
1170
1171 /*
1172 * find a random value and set it to yes,
1173 * set the rest to no so we have only one set
1174 */
1175 def = (rand() % cnt);
1176
1177 cnt = 0;
1178 expr_list_for_each_sym(prop->expr, e, sym) {
1179 if (def == cnt++) {
1180 sym->def[S_DEF_USER].tri = yes;
1181 csym->def[S_DEF_USER].val = sym;
1182 }
1183 else {
1184 sym->def[S_DEF_USER].tri = no;
1185 }
1186 sym->flags |= SYMBOL_DEF_USER;
1187 /* clear VALID to get value calculated */
1188 sym->flags &= ~SYMBOL_VALID;
1189 }
1190 csym->flags |= SYMBOL_DEF_USER;
1191 /* clear VALID to get value calculated */
1192 csym->flags &= ~(SYMBOL_VALID);
1193
1194 return true;
1195}
1196
1197void set_all_choice_values(struct symbol *csym)
1198{
1199 struct property *prop;
1200 struct symbol *sym;
1201 struct expr *e;
1202
1203 prop = sym_get_choice_prop(csym);
1204
1205 /*
1206 * Set all non-assinged choice values to no
1207 */
1208 expr_list_for_each_sym(prop->expr, e, sym) {
1209 if (!sym_has_value(sym))
1210 sym->def[S_DEF_USER].tri = no;
1211 }
1212 csym->flags |= SYMBOL_DEF_USER;
1213 /* clear VALID to get value calculated */
1214 csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1215}
1216
1217bool conf_set_all_new_symbols(enum conf_def_mode mode)
1218{
1219 struct symbol *sym, *csym;
1220 int i, cnt, pby, pty, ptm; /* pby: probability of boolean = y
1221 * pty: probability of tristate = y
1222 * ptm: probability of tristate = m
1223 */
1224
1225 pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1226 * below, otherwise gcc whines about
1227 * -Wmaybe-uninitialized */
1228 if (mode == def_random) {
1229 int n, p[3];
1230 char *env = getenv("KCONFIG_PROBABILITY");
1231 n = 0;
1232 while( env && *env ) {
1233 char *endp;
1234 int tmp = strtol( env, &endp, 10 );
1235 if( tmp >= 0 && tmp <= 100 ) {
1236 p[n++] = tmp;
1237 } else {
1238 errno = ERANGE;
1239 perror( "KCONFIG_PROBABILITY" );
1240 exit( 1 );
1241 }
1242 env = (*endp == ':') ? endp+1 : endp;
1243 if( n >=3 ) {
1244 break;
1245 }
1246 }
1247 switch( n ) {
1248 case 1:
1249 pby = p[0]; ptm = pby/2; pty = pby-ptm;
1250 break;
1251 case 2:
1252 pty = p[0]; ptm = p[1]; pby = pty + ptm;
1253 break;
1254 case 3:
1255 pby = p[0]; pty = p[1]; ptm = p[2];
1256 break;
1257 }
1258
1259 if( pty+ptm > 100 ) {
1260 errno = ERANGE;
1261 perror( "KCONFIG_PROBABILITY" );
1262 exit( 1 );
1263 }
1264 }
1265 bool has_changed = false;
1266
1267 for_all_symbols(i, sym) {
1268 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1269 continue;
1270 switch (sym_get_type(sym)) {
1271 case S_BOOLEAN:
1272 case S_TRISTATE:
1273 has_changed = true;
1274 switch (mode) {
1275 case def_yes:
1276 sym->def[S_DEF_USER].tri = yes;
1277 break;
1278 case def_mod:
1279 sym->def[S_DEF_USER].tri = mod;
1280 break;
1281 case def_no:
1282 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1283 sym->def[S_DEF_USER].tri = yes;
1284 else
1285 sym->def[S_DEF_USER].tri = no;
1286 break;
1287 case def_random:
1288 sym->def[S_DEF_USER].tri = no;
1289 cnt = rand() % 100;
1290 if (sym->type == S_TRISTATE) {
1291 if (cnt < pty)
1292 sym->def[S_DEF_USER].tri = yes;
1293 else if (cnt < (pty+ptm))
1294 sym->def[S_DEF_USER].tri = mod;
1295 } else if (cnt < pby)
1296 sym->def[S_DEF_USER].tri = yes;
1297 break;
1298 default:
1299 continue;
1300 }
1301 if (!(sym_is_choice(sym) && mode == def_random))
1302 sym->flags |= SYMBOL_DEF_USER;
1303 break;
1304 default:
1305 break;
1306 }
1307
1308 }
1309
1310 sym_clear_all_valid();
1311
1312 /*
1313 * We have different type of choice blocks.
1314 * If curr.tri equals to mod then we can select several
1315 * choice symbols in one block.
1316 * In this case we do nothing.
1317 * If curr.tri equals yes then only one symbol can be
1318 * selected in a choice block and we set it to yes,
1319 * and the rest to no.
1320 */
1321 if (mode != def_random) {
1322 for_all_symbols(i, csym) {
1323 if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1324 sym_is_choice_value(csym))
1325 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1326 }
1327 }
1328
1329 for_all_symbols(i, csym) {
1330 if (sym_has_value(csym) || !sym_is_choice(csym))
1331 continue;
1332
1333 sym_calc_value(csym);
1334 if (mode == def_random)
1335 has_changed = randomize_choice_values(csym);
1336 else {
1337 set_all_choice_values(csym);
1338 has_changed = true;
1339 }
1340 }
1341
1342 return has_changed;
1343}