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
[] = "scripts/defconfig";
27 const char *conf_confnames
[] = {
32 static void conf_warning(const char *fmt
, ...)
36 fprintf(stderr
, "%s:%d:warning: ", conf_filename
, conf_lineno
);
37 vfprintf(stderr
, fmt
, ap
);
38 fprintf(stderr
, "\n");
43 static char *conf_expand_value(const char *in
)
47 static char res_value
[SYMBOL_MAXLENGTH
];
48 char *dst
, name
[SYMBOL_MAXLENGTH
];
52 while ((src
= strchr(in
, '$'))) {
53 strncat(res_value
, in
, src
- in
);
56 while (isalnum(*src
) || *src
== '_')
59 sym
= sym_lookup(name
, 0);
61 strcat(res_value
, sym_get_string_value(sym
));
64 strcat(res_value
, in
);
69 char *conf_get_default_confname(void)
72 static char fullname
[PATH_MAX
+1];
75 name
= conf_expand_value(conf_defname
);
76 env
= getenv(SRCTREE
);
78 sprintf(fullname
, "%s/%s", env
, name
);
79 if (!stat(fullname
, &buf
))
85 int conf_read_simple(const char *name
)
94 in
= zconf_fopen(name
);
96 const char **names
= conf_confnames
;
97 while ((name
= *names
++)) {
98 name
= conf_expand_value(name
);
99 in
= zconf_fopen(name
);
102 "# using defaults found in %s\n"
111 conf_filename
= name
;
116 for_all_symbols(i
, sym
) {
117 sym
->flags
|= SYMBOL_NEW
| SYMBOL_CHANGED
;
118 if (sym_is_choice(sym
))
119 sym
->flags
&= ~SYMBOL_NEW
;
120 sym
->flags
&= ~SYMBOL_VALID
;
128 sym
->user
.val
= NULL
;
133 while (fgets(line
, sizeof(line
), in
)) {
138 if (memcmp(line
+ 2, "CONFIG_", 7))
140 p
= strchr(line
+ 9, ' ');
144 if (strncmp(p
, "is not set", 10))
146 sym
= sym_find(line
+ 9);
148 conf_warning("trying to assign nonexistent symbol %s", line
+ 9);
150 } else if (!(sym
->flags
& SYMBOL_NEW
)) {
151 conf_warning("trying to reassign symbol %s", sym
->name
);
158 sym
->flags
&= ~SYMBOL_NEW
;
165 if (memcmp(line
, "CONFIG_", 7)) {
166 conf_warning("unexpected data");
169 p
= strchr(line
+ 7, '=');
173 p2
= strchr(p
, '\n');
176 sym
= sym_find(line
+ 7);
178 conf_warning("trying to assign nonexistent symbol %s", line
+ 7);
180 } else if (!(sym
->flags
& SYMBOL_NEW
)) {
181 conf_warning("trying to reassign symbol %s", sym
->name
);
188 sym
->flags
&= ~SYMBOL_NEW
;
194 sym
->flags
&= ~SYMBOL_NEW
;
199 sym
->flags
&= ~SYMBOL_NEW
;
202 conf_warning("symbol value '%s' invalid for %s", p
, sym
->name
);
207 for (p2
= p
; (p2
= strpbrk(p2
, "\"\\")); p2
++) {
212 memmove(p2
, p2
+ 1, strlen(p2
));
215 conf_warning("invalid string found");
220 if (sym_string_valid(sym
, p
)) {
221 sym
->user
.val
= strdup(p
);
222 sym
->flags
&= ~SYMBOL_NEW
;
224 conf_warning("symbol value '%s' invalid for %s", p
, sym
->name
);
235 conf_warning("unexpected data");
238 if (sym
&& sym_is_choice_value(sym
)) {
239 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
240 switch (sym
->user
.tri
) {
244 if (cs
->user
.tri
== yes
) {
245 conf_warning("%s creates inconsistent choice state", sym
->name
);
246 cs
->flags
|= SYMBOL_NEW
;
250 if (cs
->user
.tri
!= no
) {
251 conf_warning("%s creates inconsistent choice state", sym
->name
);
252 cs
->flags
|= SYMBOL_NEW
;
257 cs
->user
.tri
= E_OR(cs
->user
.tri
, sym
->user
.tri
);
263 sym_calc_value(modules_sym
);
267 int conf_read(const char *name
)
270 struct property
*prop
;
274 if (conf_read_simple(name
))
277 for_all_symbols(i
, sym
) {
279 if (sym_is_choice(sym
) || (sym
->flags
& SYMBOL_AUTO
))
281 if (sym_has_value(sym
) && (sym
->flags
& SYMBOL_WRITE
)) {
282 /* check that calculated value agrees with saved value */
286 if (sym
->user
.tri
!= sym_get_tristate_value(sym
))
288 if (!sym_is_choice(sym
))
291 if (!strcmp(sym
->curr
.val
, sym
->user
.val
))
295 } else if (!sym_has_value(sym
) && !(sym
->flags
& SYMBOL_WRITE
))
296 /* no previous value and not saved */
299 /* maybe print value in verbose mode... */
301 if (sym_has_value(sym
) && !sym_is_choice_value(sym
)) {
302 if (sym
->visible
== no
)
303 sym
->flags
|= SYMBOL_NEW
;
308 if (!sym_string_within_range(sym
, sym
->user
.val
)) {
309 sym
->flags
|= SYMBOL_NEW
;
310 sym
->flags
&= ~SYMBOL_VALID
;
316 if (!sym_is_choice(sym
))
318 prop
= sym_get_choice_prop(sym
);
319 for (e
= prop
->expr
; e
; e
= e
->left
.expr
)
320 if (e
->right
.sym
->visible
!= no
)
321 sym
->flags
|= e
->right
.sym
->flags
& SYMBOL_NEW
;
324 sym_change_count
= conf_warnings
|| conf_unsaved
;
329 int conf_write(const char *name
)
334 const char *basename
;
335 char dirname
[128], tmpname
[128], newname
[128];
339 int use_timestamp
= 1;
343 if (name
&& name
[0]) {
347 if (!stat(name
, &st
) && S_ISDIR(st
.st_mode
)) {
348 strcpy(dirname
, name
);
349 strcat(dirname
, "/");
350 basename
= conf_def_filename
;
351 } else if ((slash
= strrchr(name
, '/'))) {
352 int size
= slash
- name
+ 1;
353 memcpy(dirname
, name
, size
);
356 basename
= slash
+ 1;
358 basename
= conf_def_filename
;
362 basename
= conf_def_filename
;
364 sprintf(newname
, "%s.tmpconfig.%d", dirname
, (int)getpid());
365 out
= fopen(newname
, "w");
370 out_h
= fopen(".tmpconfig.h", "w");
373 file_write_dep(NULL
);
375 sym
= sym_lookup("KERNELVERSION", 0);
378 env
= getenv("KCONFIG_NOTIMESTAMP");
383 "# Automatically generated make config: don't edit\n"
384 "# Buildrom version: %s\n"
387 sym_get_string_value(sym
),
388 use_timestamp
? "# " : "",
389 use_timestamp
? ctime(&now
) : "");
391 char buf
[sizeof("#define AUTOCONF_TIMESTAMP "
392 "\"YYYY-MM-DD HH:MM:SS some_timezone\"\n")];
396 strftime(buf
, sizeof(buf
), "#define AUTOCONF_TIMESTAMP "
397 "\"%Y-%m-%d %H:%M:%S %Z\"\n", localtime(&now
));
398 /* if user has Factory timezone or some other odd install, the
399 * %Z above will overflow the string leaving us with undefined
400 * results ... so let's try again without the timezone.
403 strftime(buf
, sizeof(buf
), "#define AUTOCONF_TIMESTAMP "
404 "\"%Y-%m-%d %H:%M:%S\"\n", localtime(&now
));
406 fprintf(out_h
, "/*\n"
407 " * Automatically generated C config: don't edit\n"
408 " * Buildrom version: %s\n"
412 sym_get_string_value(sym
),
415 if (!sym_change_count
)
416 sym_clear_all_valid();
418 menu
= rootmenu
.list
;
422 if (!menu_is_visible(menu
))
424 str
= menu_get_prompt(menu
);
434 } else if (!(sym
->flags
& SYMBOL_CHOICE
)) {
436 /* bbox: we want to all syms
437 if (!(sym->flags & SYMBOL_WRITE))
440 sym
->flags
&= ~SYMBOL_WRITE
;
442 if (type
== S_TRISTATE
) {
443 sym_calc_value(modules_sym
);
444 if (modules_sym
->curr
.tri
== no
)
450 switch (sym_get_tristate_value(sym
)) {
452 fprintf(out
, "# CONFIG_%s is not set\n", sym
->name
);
454 fprintf(out_h
, "#undef CONFIG_%s\n", sym
->name
);
456 fprintf(out_h
, "#define ENABLE_%s 0\n", sym
->name
);
457 fprintf(out_h
, "#define USE_%s(...)\n", sym
->name
);
458 fprintf(out_h
, "#define SKIP_%s(...) __VA_ARGS__\n", sym
->name
);
462 fprintf(out
, "CONFIG_%s=m\n", sym
->name
);
464 fprintf(out_h
, "#define CONFIG_%s_MODULE 1\n", sym
->name
);
467 fprintf(out
, "CONFIG_%s=y\n", sym
->name
);
469 fprintf(out_h
, "#define CONFIG_%s 1\n", sym
->name
);
471 fprintf(out_h
, "#define ENABLE_%s 1\n", sym
->name
);
472 fprintf(out_h
, "#define USE_%s(...) __VA_ARGS__\n", sym
->name
);
473 fprintf(out_h
, "#define SKIP_%s(...)\n", sym
->name
);
480 str
= sym_get_string_value(sym
);
481 fprintf(out
, "CONFIG_%s=\"", sym
->name
);
483 fprintf(out_h
, "#define CONFIG_%s \"", sym
->name
);
485 l
= strcspn(str
, "\"\\");
487 fwrite(str
, l
, 1, out
);
489 fwrite(str
, l
, 1, out_h
);
492 while (*str
== '\\' || *str
== '"') {
493 fprintf(out
, "\\%c", *str
);
495 fprintf(out_h
, "\\%c", *str
);
501 fputs("\"\n", out_h
);
503 fprintf(out_h
, "#define ENABLE_%s 1\n", sym
->name
);
504 fprintf(out_h
, "#define USE_%s(...) __VA_ARGS__\n", sym
->name
);
505 fprintf(out_h
, "#define SKIP_%s(...)\n", sym
->name
);
509 str
= sym_get_string_value(sym
);
510 if (str
[0] != '0' || (str
[1] != 'x' && str
[1] != 'X')) {
511 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
513 fprintf(out_h
, "#define CONFIG_%s 0x%s\n", sym
->name
, str
);
515 fprintf(out_h
, "#define ENABLE_%s 1\n", sym
->name
);
516 fprintf(out_h
, "#define USE_%s(...) __VA_ARGS__\n", sym
->name
);
517 fprintf(out_h
, "#define SKIP_%s(...)\n", sym
->name
);
522 str
= sym_get_string_value(sym
);
523 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
525 fprintf(out_h
, "#define CONFIG_%s %s\n", sym
->name
, str
);
527 fprintf(out_h
, "#define ENABLE_%s 1\n", sym
->name
);
528 fprintf(out_h
, "#define USE_%s(...) __VA_ARGS__\n", sym
->name
);
529 fprintf(out_h
, "#define SKIP_%s(...)\n", sym
->name
);
542 else while ((menu
= menu
->parent
)) {
552 rename(".tmpconfig.h", "include/autoconf.h");
554 if (!name
|| basename
!= conf_def_filename
) {
556 name
= conf_def_filename
;
557 sprintf(tmpname
, "%s.old", name
);
558 rename(name
, tmpname
);
560 sprintf(tmpname
, "%s%s", dirname
, basename
);
561 if (rename(newname
, tmpname
))
564 sym_change_count
= 0;