2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
14 #define LKC_DIRECT_LINK
17 static void conf_warning(const char *fmt
, ...)
18 __attribute__ ((format (printf
, 1, 2)));
20 static const char *conf_filename
;
21 static int conf_lineno
, conf_warnings
, conf_unsaved
;
23 const char conf_def_filename
[] = ".config";
25 const char conf_defname
[] = "arch/$ARCH/defconfig";
27 const char *conf_confnames
[] = {
29 "/lib/modules/$UNAME_RELEASE/.config",
31 "/boot/config-$UNAME_RELEASE",
36 static void conf_warning(const char *fmt
, ...)
40 fprintf(stderr
, "%s:%d:warning: ", conf_filename
, conf_lineno
);
41 vfprintf(stderr
, fmt
, ap
);
42 fprintf(stderr
, "\n");
47 static char *conf_expand_value(const char *in
)
51 static char res_value
[SYMBOL_MAXLENGTH
];
52 char *dst
, name
[SYMBOL_MAXLENGTH
];
56 while ((src
= strchr(in
, '$'))) {
57 strncat(res_value
, in
, src
- in
);
60 while (isalnum(*src
) || *src
== '_')
63 sym
= sym_lookup(name
, 0);
65 strcat(res_value
, sym_get_string_value(sym
));
68 strcat(res_value
, in
);
73 char *conf_get_default_confname(void)
76 static char fullname
[PATH_MAX
+1];
79 name
= conf_expand_value(conf_defname
);
80 env
= getenv(SRCTREE
);
82 sprintf(fullname
, "%s/%s", env
, name
);
83 if (!stat(fullname
, &buf
))
89 int conf_read_simple(const char *name
)
98 in
= zconf_fopen(name
);
100 const char **names
= conf_confnames
;
104 in
= zconf_fopen(name
);
108 while ((name
= *names
++)) {
109 name
= conf_expand_value(name
);
110 in
= zconf_fopen(name
);
113 "# using defaults found in %s\n"
123 conf_filename
= name
;
128 for_all_symbols(i
, sym
) {
129 sym
->flags
|= SYMBOL_NEW
| SYMBOL_CHANGED
;
130 if (sym_is_choice(sym
))
131 sym
->flags
&= ~SYMBOL_NEW
;
132 sym
->flags
&= ~SYMBOL_VALID
;
140 sym
->user
.val
= NULL
;
145 while (fgets(line
, sizeof(line
), in
)) {
150 if (memcmp(line
+ 2, "CONFIG_", 7))
152 p
= strchr(line
+ 9, ' ');
156 if (strncmp(p
, "is not set", 10))
158 sym
= sym_find(line
+ 9);
160 conf_warning("trying to assign nonexistent symbol %s", line
+ 9);
162 } else if (!(sym
->flags
& SYMBOL_NEW
)) {
163 conf_warning("trying to reassign symbol %s", sym
->name
);
170 sym
->flags
&= ~SYMBOL_NEW
;
177 if (memcmp(line
, "CONFIG_", 7)) {
178 conf_warning("unexpected data");
181 p
= strchr(line
+ 7, '=');
185 p2
= strchr(p
, '\n');
188 sym
= sym_find(line
+ 7);
190 conf_warning("trying to assign nonexistent symbol %s", line
+ 7);
192 } else if (!(sym
->flags
& SYMBOL_NEW
)) {
193 conf_warning("trying to reassign symbol %s", sym
->name
);
200 sym
->flags
&= ~SYMBOL_NEW
;
206 sym
->flags
&= ~SYMBOL_NEW
;
211 sym
->flags
&= ~SYMBOL_NEW
;
214 conf_warning("symbol value '%s' invalid for %s", p
, sym
->name
);
219 for (p2
= p
; (p2
= strpbrk(p2
, "\"\\")); p2
++) {
224 memmove(p2
, p2
+ 1, strlen(p2
));
227 conf_warning("invalid string found");
232 if (sym_string_valid(sym
, p
)) {
233 sym
->user
.val
= strdup(p
);
234 sym
->flags
&= ~SYMBOL_NEW
;
236 conf_warning("symbol value '%s' invalid for %s", p
, sym
->name
);
247 conf_warning("unexpected data");
250 if (sym
&& sym_is_choice_value(sym
)) {
251 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
252 switch (sym
->user
.tri
) {
256 if (cs
->user
.tri
== yes
) {
257 conf_warning("%s creates inconsistent choice state", sym
->name
);
258 cs
->flags
|= SYMBOL_NEW
;
262 if (cs
->user
.tri
!= no
) {
263 conf_warning("%s creates inconsistent choice state", sym
->name
);
264 cs
->flags
|= SYMBOL_NEW
;
269 cs
->user
.tri
= E_OR(cs
->user
.tri
, sym
->user
.tri
);
275 sym_calc_value(modules_sym
);
279 int conf_read(const char *name
)
282 struct property
*prop
;
286 sym_change_count
= 0;
288 if (conf_read_simple(name
))
291 for_all_symbols(i
, sym
) {
293 if (sym_is_choice(sym
) || (sym
->flags
& SYMBOL_AUTO
))
295 if (sym_has_value(sym
) && (sym
->flags
& SYMBOL_WRITE
)) {
296 /* check that calculated value agrees with saved value */
300 if (sym
->user
.tri
!= sym_get_tristate_value(sym
))
302 if (!sym_is_choice(sym
))
305 if (!strcmp(sym
->curr
.val
, sym
->user
.val
))
309 } else if (!sym_has_value(sym
) && !(sym
->flags
& SYMBOL_WRITE
))
310 /* no previous value and not saved */
313 /* maybe print value in verbose mode... */
315 if (sym_has_value(sym
) && !sym_is_choice_value(sym
)) {
316 if (sym
->visible
== no
)
317 sym
->flags
|= SYMBOL_NEW
;
322 if (!sym_string_within_range(sym
, sym
->user
.val
)) {
323 sym
->flags
|= SYMBOL_NEW
;
324 sym
->flags
&= ~SYMBOL_VALID
;
330 if (!sym_is_choice(sym
))
332 prop
= sym_get_choice_prop(sym
);
333 for (e
= prop
->expr
; e
; e
= e
->left
.expr
)
334 if (e
->right
.sym
->visible
!= no
)
335 sym
->flags
|= e
->right
.sym
->flags
& SYMBOL_NEW
;
338 sym_change_count
+= conf_warnings
|| conf_unsaved
;
343 int conf_write(const char *name
)
348 const char *basename
;
349 char dirname
[128], tmpname
[128], newname
[128];
353 int use_timestamp
= 1;
357 if (name
&& name
[0]) {
361 if (!stat(name
, &st
) && S_ISDIR(st
.st_mode
)) {
362 strcpy(dirname
, name
);
363 strcat(dirname
, "/");
364 basename
= conf_def_filename
;
365 } else if ((slash
= strrchr(name
, '/'))) {
366 int size
= slash
- name
+ 1;
367 memcpy(dirname
, name
, size
);
370 basename
= slash
+ 1;
372 basename
= conf_def_filename
;
376 basename
= conf_def_filename
;
378 sprintf(newname
, "%s.tmpconfig.%d", dirname
, (int)getpid());
379 out
= fopen(newname
, "w");
382 sym
= sym_lookup("KERNELVERSION", 0);
385 env
= getenv("KCONFIG_NOTIMESTAMP");
390 "# Automatically generated make config: don't edit\n"
391 "# Linux kernel version: %s\n"
394 sym_get_string_value(sym
),
395 use_timestamp
? "# " : "",
396 use_timestamp
? ctime(&now
) : "");
398 if (!sym_change_count
)
399 sym_clear_all_valid();
401 menu
= rootmenu
.list
;
405 if (!menu_is_visible(menu
))
407 str
= menu_get_prompt(menu
);
412 } else if (!(sym
->flags
& SYMBOL_CHOICE
)) {
414 if (!(sym
->flags
& SYMBOL_WRITE
))
416 sym
->flags
&= ~SYMBOL_WRITE
;
418 if (type
== S_TRISTATE
) {
419 sym_calc_value(modules_sym
);
420 if (modules_sym
->curr
.tri
== no
)
426 switch (sym_get_tristate_value(sym
)) {
428 fprintf(out
, "# CONFIG_%s is not set\n", sym
->name
);
431 fprintf(out
, "CONFIG_%s=m\n", sym
->name
);
434 fprintf(out
, "CONFIG_%s=y\n", sym
->name
);
439 str
= sym_get_string_value(sym
);
440 fprintf(out
, "CONFIG_%s=\"", sym
->name
);
442 l
= strcspn(str
, "\"\\");
444 fwrite(str
, l
, 1, out
);
449 fprintf(out
, "\\%c", *str
++);
454 str
= sym_get_string_value(sym
);
455 if (str
[0] != '0' || (str
[1] != 'x' && str
[1] != 'X')) {
456 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
460 str
= sym_get_string_value(sym
);
461 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
473 else while ((menu
= menu
->parent
)) {
481 if (!name
|| basename
!= conf_def_filename
) {
483 name
= conf_def_filename
;
484 sprintf(tmpname
, "%s.old", name
);
485 rename(name
, tmpname
);
487 sprintf(tmpname
, "%s%s", dirname
, basename
);
488 if (rename(newname
, tmpname
))
492 "# configuration written to %s\n"
495 sym_change_count
= 0;
500 int conf_write_autoconf(void)
509 file_write_dep("include/config/auto.conf.cmd");
511 out
= fopen(".tmpconfig", "w");
515 out_h
= fopen(".tmpconfig.h", "w");
521 sym
= sym_lookup("KERNELVERSION", 0);
525 "# Automatically generated make config: don't edit\n"
526 "# Linux kernel version: %s\n"
529 sym_get_string_value(sym
), ctime(&now
));
530 fprintf(out_h
, "/*\n"
531 " * Automatically generated C config: don't edit\n"
532 " * Linux kernel version: %s\n"
535 "#define AUTOCONF_INCLUDED\n",
536 sym_get_string_value(sym
), ctime(&now
));
538 sym_clear_all_valid();
540 for_all_symbols(i
, sym
) {
542 if (!(sym
->flags
& SYMBOL_WRITE
) || !sym
->name
)
547 switch (sym_get_tristate_value(sym
)) {
551 fprintf(out
, "CONFIG_%s=m\n", sym
->name
);
552 fprintf(out_h
, "#define CONFIG_%s_MODULE 1\n", sym
->name
);
555 fprintf(out
, "CONFIG_%s=y\n", sym
->name
);
556 fprintf(out_h
, "#define CONFIG_%s 1\n", sym
->name
);
561 str
= sym_get_string_value(sym
);
562 fprintf(out
, "CONFIG_%s=\"", sym
->name
);
563 fprintf(out_h
, "#define CONFIG_%s \"", sym
->name
);
565 l
= strcspn(str
, "\"\\");
567 fwrite(str
, l
, 1, out
);
568 fwrite(str
, l
, 1, out_h
);
573 fprintf(out
, "\\%c", *str
);
574 fprintf(out_h
, "\\%c", *str
);
578 fputs("\"\n", out_h
);
581 str
= sym_get_string_value(sym
);
582 if (str
[0] != '0' || (str
[1] != 'x' && str
[1] != 'X')) {
583 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
584 fprintf(out_h
, "#define CONFIG_%s 0x%s\n", sym
->name
, str
);
588 str
= sym_get_string_value(sym
);
589 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
590 fprintf(out_h
, "#define CONFIG_%s %s\n", sym
->name
, str
);
599 name
= getenv("KCONFIG_AUTOHEADER");
601 name
= "include/linux/autoconf.h";
602 if (rename(".tmpconfig.h", name
))
604 name
= getenv("KCONFIG_AUTOCONFIG");
606 name
= "include/config/auto.conf";
608 * This must be the last step, kbuild has a dependency on auto.conf
609 * and this marks the successful completion of the previous steps.
611 if (rename(".tmpconfig", name
))