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
;
101 while ((name
= *names
++)) {
102 name
= conf_expand_value(name
);
103 in
= zconf_fopen(name
);
106 "# using defaults found in %s\n"
115 conf_filename
= name
;
120 for_all_symbols(i
, sym
) {
121 sym
->flags
|= SYMBOL_NEW
| SYMBOL_CHANGED
;
122 if (sym_is_choice(sym
))
123 sym
->flags
&= ~SYMBOL_NEW
;
124 sym
->flags
&= ~SYMBOL_VALID
;
132 sym
->user
.val
= NULL
;
137 while (fgets(line
, sizeof(line
), in
)) {
142 if (memcmp(line
+ 2, "CONFIG_", 7))
144 p
= strchr(line
+ 9, ' ');
148 if (strncmp(p
, "is not set", 10))
150 sym
= sym_find(line
+ 9);
152 conf_warning("trying to assign nonexistent symbol %s", line
+ 9);
154 } else if (!(sym
->flags
& SYMBOL_NEW
)) {
155 conf_warning("trying to reassign symbol %s", sym
->name
);
162 sym
->flags
&= ~SYMBOL_NEW
;
169 if (memcmp(line
, "CONFIG_", 7)) {
170 conf_warning("unexpected data");
173 p
= strchr(line
+ 7, '=');
177 p2
= strchr(p
, '\n');
180 sym
= sym_find(line
+ 7);
182 conf_warning("trying to assign nonexistent symbol %s", line
+ 7);
184 } else if (!(sym
->flags
& SYMBOL_NEW
)) {
185 conf_warning("trying to reassign symbol %s", sym
->name
);
192 sym
->flags
&= ~SYMBOL_NEW
;
198 sym
->flags
&= ~SYMBOL_NEW
;
203 sym
->flags
&= ~SYMBOL_NEW
;
206 conf_warning("symbol value '%s' invalid for %s", p
, sym
->name
);
211 for (p2
= p
; (p2
= strpbrk(p2
, "\"\\")); p2
++) {
216 memmove(p2
, p2
+ 1, strlen(p2
));
219 conf_warning("invalid string found");
224 if (sym_string_valid(sym
, p
)) {
225 sym
->user
.val
= strdup(p
);
226 sym
->flags
&= ~SYMBOL_NEW
;
228 conf_warning("symbol value '%s' invalid for %s", p
, sym
->name
);
239 conf_warning("unexpected data");
242 if (sym
&& sym_is_choice_value(sym
)) {
243 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
244 switch (sym
->user
.tri
) {
248 if (cs
->user
.tri
== yes
) {
249 conf_warning("%s creates inconsistent choice state", sym
->name
);
250 cs
->flags
|= SYMBOL_NEW
;
254 if (cs
->user
.tri
!= no
) {
255 conf_warning("%s creates inconsistent choice state", sym
->name
);
256 cs
->flags
|= SYMBOL_NEW
;
261 cs
->user
.tri
= E_OR(cs
->user
.tri
, sym
->user
.tri
);
267 sym_calc_value(modules_sym
);
271 int conf_read(const char *name
)
274 struct property
*prop
;
278 if (conf_read_simple(name
))
281 for_all_symbols(i
, sym
) {
283 if (sym_is_choice(sym
) || (sym
->flags
& SYMBOL_AUTO
))
285 if (sym_has_value(sym
) && (sym
->flags
& SYMBOL_WRITE
)) {
286 /* check that calculated value agrees with saved value */
290 if (sym
->user
.tri
!= sym_get_tristate_value(sym
))
292 if (!sym_is_choice(sym
))
295 if (!strcmp(sym
->curr
.val
, sym
->user
.val
))
299 } else if (!sym_has_value(sym
) && !(sym
->flags
& SYMBOL_WRITE
))
300 /* no previous value and not saved */
303 /* maybe print value in verbose mode... */
305 if (sym_has_value(sym
) && !sym_is_choice_value(sym
)) {
306 if (sym
->visible
== no
)
307 sym
->flags
|= SYMBOL_NEW
;
312 if (!sym_string_within_range(sym
, sym
->user
.val
)) {
313 sym
->flags
|= SYMBOL_NEW
;
314 sym
->flags
&= ~SYMBOL_VALID
;
320 if (!sym_is_choice(sym
))
322 prop
= sym_get_choice_prop(sym
);
323 for (e
= prop
->expr
; e
; e
= e
->left
.expr
)
324 if (e
->right
.sym
->visible
!= no
)
325 sym
->flags
|= e
->right
.sym
->flags
& SYMBOL_NEW
;
328 sym_change_count
= conf_warnings
&& conf_unsaved
;
333 int conf_write(const char *name
)
338 const char *basename
;
339 char dirname
[128], tmpname
[128], newname
[128];
343 int use_timestamp
= 1;
347 if (name
&& name
[0]) {
351 if (!stat(name
, &st
) && S_ISDIR(st
.st_mode
)) {
352 strcpy(dirname
, name
);
353 strcat(dirname
, "/");
354 basename
= conf_def_filename
;
355 } else if ((slash
= strrchr(name
, '/'))) {
356 int size
= slash
- name
+ 1;
357 memcpy(dirname
, name
, size
);
360 basename
= slash
+ 1;
362 basename
= conf_def_filename
;
366 basename
= conf_def_filename
;
368 sprintf(newname
, "%s.tmpconfig.%d", dirname
, (int)getpid());
369 out
= fopen(newname
, "w");
374 out_h
= fopen(".tmpconfig.h", "w");
378 sym
= sym_lookup("KERNELRELEASE", 0);
381 env
= getenv("KCONFIG_NOTIMESTAMP");
386 "# Automatically generated make config: don't edit\n"
387 "# Linux kernel version: %s\n"
390 sym_get_string_value(sym
),
391 use_timestamp
? "# " : "",
392 use_timestamp
? ctime(&now
) : "");
394 fprintf(out_h
, "/*\n"
395 " * Automatically generated C config: don't edit\n"
396 " * Linux kernel version: %s\n"
399 "#define AUTOCONF_INCLUDED\n",
400 sym_get_string_value(sym
),
401 use_timestamp
? " * " : "",
402 use_timestamp
? ctime(&now
) : "");
404 if (!sym_change_count
)
405 sym_clear_all_valid();
407 menu
= rootmenu
.list
;
411 if (!menu_is_visible(menu
))
413 str
= menu_get_prompt(menu
);
423 } else if (!(sym
->flags
& SYMBOL_CHOICE
)) {
425 if (!(sym
->flags
& SYMBOL_WRITE
))
427 sym
->flags
&= ~SYMBOL_WRITE
;
429 if (type
== S_TRISTATE
) {
430 sym_calc_value(modules_sym
);
431 if (modules_sym
->curr
.tri
== no
)
437 switch (sym_get_tristate_value(sym
)) {
439 fprintf(out
, "# CONFIG_%s is not set\n", sym
->name
);
441 fprintf(out_h
, "#undef CONFIG_%s\n", sym
->name
);
444 fprintf(out
, "CONFIG_%s=m\n", sym
->name
);
446 fprintf(out_h
, "#define CONFIG_%s_MODULE 1\n", sym
->name
);
449 fprintf(out
, "CONFIG_%s=y\n", sym
->name
);
451 fprintf(out_h
, "#define CONFIG_%s 1\n", sym
->name
);
457 str
= sym_get_string_value(sym
);
458 fprintf(out
, "CONFIG_%s=\"", sym
->name
);
460 fprintf(out_h
, "#define CONFIG_%s \"", sym
->name
);
462 l
= strcspn(str
, "\"\\");
464 fwrite(str
, l
, 1, out
);
466 fwrite(str
, l
, 1, out_h
);
469 while (*str
== '\\' || *str
== '"') {
470 fprintf(out
, "\\%c", *str
);
472 fprintf(out_h
, "\\%c", *str
);
478 fputs("\"\n", out_h
);
481 str
= sym_get_string_value(sym
);
482 if (str
[0] != '0' || (str
[1] != 'x' && str
[1] != 'X')) {
483 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
485 fprintf(out_h
, "#define CONFIG_%s 0x%s\n", sym
->name
, str
);
489 str
= sym_get_string_value(sym
);
490 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
492 fprintf(out_h
, "#define CONFIG_%s %s\n", sym
->name
, str
);
504 else while ((menu
= menu
->parent
)) {
514 rename(".tmpconfig.h", "include/linux/autoconf.h");
515 file_write_dep(NULL
);
517 if (!name
|| basename
!= conf_def_filename
) {
519 name
= conf_def_filename
;
520 sprintf(tmpname
, "%s.old", name
);
521 rename(name
, tmpname
);
523 sprintf(tmpname
, "%s%s", dirname
, basename
);
524 if (rename(newname
, tmpname
))
527 sym_change_count
= 0;