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
[] = "/dev/null"; //bbox
27 const char *conf_confnames
[] = {
33 static void conf_warning(const char *fmt
, ...)
37 fprintf(stderr
, "%s:%d:warning: ", conf_filename
, conf_lineno
);
38 vfprintf(stderr
, fmt
, ap
);
39 fprintf(stderr
, "\n");
44 static char *conf_expand_value(const char *in
)
48 static char res_value
[SYMBOL_MAXLENGTH
];
49 char *dst
, name
[SYMBOL_MAXLENGTH
];
53 while ((src
= strchr(in
, '$'))) {
54 strncat(res_value
, in
, src
- in
);
57 while (isalnum((unsigned char)*src
) || *src
== '_')
60 sym
= sym_lookup(name
, 0);
62 strcat(res_value
, sym_get_string_value(sym
));
65 strcat(res_value
, in
);
70 char *conf_get_default_confname(void)
73 static char *fullname
= NULL
;
76 name
= conf_expand_value(conf_defname
);
77 env
= getenv(SRCTREE
);
79 fullname
= realloc(fullname
, strlen(env
) + strlen(name
) + 2);
80 sprintf(fullname
, "%s/%s", env
, name
);
81 if (!stat(fullname
, &buf
))
87 int conf_read_simple(const char *name
)
96 in
= zconf_fopen(name
);
98 const char **names
= conf_confnames
;
99 while ((name
= *names
++)) {
100 name
= conf_expand_value(name
);
101 in
= zconf_fopen(name
);
104 "# using defaults found in %s\n"
113 conf_filename
= name
;
118 for_all_symbols(i
, sym
) {
119 sym
->flags
|= SYMBOL_NEW
| SYMBOL_CHANGED
;
120 if (sym_is_choice(sym
))
121 sym
->flags
&= ~SYMBOL_NEW
;
122 sym
->flags
&= ~SYMBOL_VALID
;
129 sym
->user
.val
= NULL
;
134 while (fgets(line
, sizeof(line
), in
)) {
139 if (memcmp(line
+ 2, "CONFIG_", 7))
141 p
= strchr(line
+ 9, ' ');
145 if (strncmp(p
, "is not set", 10))
147 sym
= sym_find(line
+ 9);
149 conf_warning("trying to assign nonexistent symbol %s", line
+ 9);
151 } else if (!(sym
->flags
& SYMBOL_NEW
)) {
152 conf_warning("trying to reassign symbol %s", sym
->name
);
159 sym
->flags
&= ~SYMBOL_NEW
;
166 if (memcmp(line
, "CONFIG_", 7)) {
167 conf_warning("unexpected data");
170 p
= strchr(line
+ 7, '=');
174 p2
= strchr(p
, '\n');
177 sym
= sym_find(line
+ 7);
179 conf_warning("trying to assign nonexistent symbol %s", line
+ 7);
181 } else if (!(sym
->flags
& SYMBOL_NEW
)) {
182 conf_warning("trying to reassign symbol %s", sym
->name
);
189 sym
->flags
&= ~SYMBOL_NEW
;
195 sym
->flags
&= ~SYMBOL_NEW
;
200 sym
->flags
&= ~SYMBOL_NEW
;
203 conf_warning("symbol value '%s' invalid for %s", p
, sym
->name
);
208 for (p2
= p
; (p2
= strpbrk(p2
, "\"\\")); p2
++) {
213 memmove(p2
, p2
+ 1, strlen(p2
));
216 conf_warning("invalid string found");
221 if (sym_string_valid(sym
, p
)) {
222 sym
->user
.val
= strdup(p
);
223 sym
->flags
&= ~SYMBOL_NEW
;
226 conf_warning("symbol value '%s' invalid for %s", p
, sym
->name
);
237 conf_warning("unexpected data");
240 if (sym
&& sym_is_choice_value(sym
)) {
241 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
242 switch (sym
->user
.tri
) {
246 if (cs
->user
.tri
== yes
) {
247 conf_warning("%s creates inconsistent choice state", sym
->name
);
248 cs
->flags
|= SYMBOL_NEW
;
252 if (cs
->user
.tri
!= no
) {
253 conf_warning("%s creates inconsistent choice state", sym
->name
);
254 cs
->flags
|= SYMBOL_NEW
;
259 cs
->user
.tri
= E_OR(cs
->user
.tri
, sym
->user
.tri
);
265 sym_calc_value(modules_sym
);
269 int conf_read(const char *name
)
272 struct property
*prop
;
276 if (conf_read_simple(name
))
279 for_all_symbols(i
, sym
) {
281 if (sym_is_choice(sym
) || (sym
->flags
& SYMBOL_AUTO
))
283 if (sym_has_value(sym
) && (sym
->flags
& SYMBOL_WRITE
)) {
284 /* check that calculated value agrees with saved value */
288 if (sym
->user
.tri
!= sym_get_tristate_value(sym
))
290 if (!sym_is_choice(sym
))
293 if (!strcmp(sym
->curr
.val
, sym
->user
.val
))
297 } else if (!sym_has_value(sym
) && !(sym
->flags
& SYMBOL_WRITE
))
298 /* no previous value and not saved */
301 /* maybe print value in verbose mode... */
303 if (sym_has_value(sym
) && !sym_is_choice_value(sym
)) {
304 if (sym
->visible
== no
)
305 sym
->flags
|= SYMBOL_NEW
;
310 if (!sym_string_within_range(sym
, sym
->user
.val
)) {
311 sym
->flags
|= SYMBOL_NEW
;
312 sym
->flags
&= ~SYMBOL_VALID
;
318 if (!sym_is_choice(sym
))
320 prop
= sym_get_choice_prop(sym
);
321 for (e
= prop
->expr
; e
; e
= e
->left
.expr
)
322 if (e
->right
.sym
->visible
!= no
)
323 sym
->flags
|= e
->right
.sym
->flags
& SYMBOL_NEW
;
326 sym_change_count
= conf_warnings
|| conf_unsaved
;
331 int conf_write(const char *name
)
336 const char *basename
;
343 int use_timestamp
= 1;
345 char *source_date_epoch
;
346 struct tm
*build_time
;
349 if (name
&& name
[0]) {
353 if (!stat(name
, &st
) && S_ISDIR(st
.st_mode
)) {
354 strcpy(dirname
, name
);
355 strcat(dirname
, "/");
356 basename
= conf_def_filename
;
357 } else if ((slash
= strrchr(name
, '/'))) {
358 int size
= slash
- name
+ 1;
359 memcpy(dirname
, name
, size
);
362 basename
= slash
+ 1;
364 basename
= conf_def_filename
;
368 basename
= conf_def_filename
;
370 sprintf(newname
, "%s.tmpconfig.%d", dirname
, (int)getpid());
371 out
= fopen(newname
, "w");
376 out_h
= fopen(".tmpconfig.h", "w");
379 file_write_dep(NULL
);
381 sym
= sym_lookup("KERNELVERSION", 0);
384 source_date_epoch
= getenv("SOURCE_DATE_EPOCH");
385 if (source_date_epoch
&& *source_date_epoch
) {
386 now
= strtoull(source_date_epoch
, NULL
, 10);
387 build_time
= gmtime(&now
);
390 build_time
= localtime(&now
);
393 env
= getenv("KCONFIG_NOTIMESTAMP");
398 "# Automatically generated make config: don't edit\n"
399 "# Busybox version: %s\n"
402 sym_get_string_value(sym
),
403 use_timestamp
? "# " : "",
404 use_timestamp
? ctime(&now
) : "");
406 char buf
[sizeof("#define AUTOCONF_TIMESTAMP "
407 "\"YYYY-MM-DD HH:MM:SS some_timezone\"\n")];
411 strftime(buf
, sizeof(buf
), "#define AUTOCONF_TIMESTAMP "
412 "\"%Y-%m-%d %H:%M:%S %Z\"\n", build_time
);
413 /* if user has Factory timezone or some other odd install, the
414 * %Z above will overflow the string leaving us with undefined
415 * results ... so let's try again without the timezone.
418 strftime(buf
, sizeof(buf
), "#define AUTOCONF_TIMESTAMP "
419 "\"%Y-%m-%d %H:%M:%S\"\n", build_time
);
421 strcpy(buf
, "#define AUTOCONF_TIMESTAMP \"\"\n");
423 fprintf(out_h
, "/*\n"
424 " * Automatically generated C config: don't edit\n"
425 " * Busybox version: %s\n"
429 sym_get_string_value(sym
),
432 if (!sym_change_count
)
433 sym_clear_all_valid();
435 menu
= rootmenu
.list
;
439 if (!menu_is_visible(menu
))
441 str
= menu_get_prompt(menu
);
451 } else if (!(sym
->flags
& SYMBOL_CHOICE
)) {
453 /* bbox: we want to see all syms
454 if (!(sym->flags & SYMBOL_WRITE))
457 sym
->flags
&= ~SYMBOL_WRITE
;
459 if (type
== S_TRISTATE
) {
460 sym_calc_value(modules_sym
);
461 if (modules_sym
->curr
.tri
== no
)
467 switch (sym_get_tristate_value(sym
)) {
469 fprintf(out
, "# CONFIG_%s is not set\n", sym
->name
);
471 fprintf(out_h
, "#undef CONFIG_%s\n", sym
->name
);
473 fprintf(out_h
, "#define ENABLE_%s 0\n", sym
->name
);
474 fprintf(out_h
, "#define IF_%s(...)\n", sym
->name
);
475 fprintf(out_h
, "#define IF_NOT_%s(...) __VA_ARGS__\n", sym
->name
);
479 fprintf(out
, "CONFIG_%s=m\n", sym
->name
);
481 fprintf(out_h
, "#define CONFIG_%s_MODULE 1\n", sym
->name
);
484 fprintf(out
, "CONFIG_%s=y\n", sym
->name
);
486 fprintf(out_h
, "#define CONFIG_%s 1\n", sym
->name
);
488 fprintf(out_h
, "#define ENABLE_%s 1\n", sym
->name
);
489 fprintf(out_h
, "#ifdef MAKE_SUID\n");
490 fprintf(out_h
, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym
->name
, sym
->name
);
491 fprintf(out_h
, "#else\n");
492 fprintf(out_h
, "# define IF_%s(...) __VA_ARGS__\n", sym
->name
);
493 fprintf(out_h
, "#endif\n");
494 fprintf(out_h
, "#define IF_NOT_%s(...)\n", sym
->name
);
501 str
= sym_get_string_value(sym
);
502 fprintf(out
, "CONFIG_%s=\"", sym
->name
);
504 fprintf(out_h
, "#define CONFIG_%s \"", sym
->name
);
506 l
= strcspn(str
, "\"\\");
508 fwrite(str
, l
, 1, out
);
510 fwrite(str
, l
, 1, out_h
);
513 while (*str
== '\\' || *str
== '"') {
514 fprintf(out
, "\\%c", *str
);
516 fprintf(out_h
, "\\%c", *str
);
522 fputs("\"\n", out_h
);
524 fprintf(out_h
, "#define ENABLE_%s 1\n", sym
->name
);
525 fprintf(out_h
, "#ifdef MAKE_SUID\n");
526 fprintf(out_h
, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym
->name
, sym
->name
);
527 fprintf(out_h
, "#else\n");
528 fprintf(out_h
, "# define IF_%s(...) __VA_ARGS__\n", sym
->name
);
529 fprintf(out_h
, "#endif\n");
530 fprintf(out_h
, "#define IF_NOT_%s(...)\n", sym
->name
);
534 str
= sym_get_string_value(sym
);
535 if (str
[0] != '0' || (str
[1] != 'x' && str
[1] != 'X')) {
536 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
538 fprintf(out_h
, "#define CONFIG_%s 0x%s\n", sym
->name
, str
);
540 fprintf(out_h
, "#define ENABLE_%s 1\n", sym
->name
);
541 fprintf(out_h
, "#ifdef MAKE_SUID\n");
542 fprintf(out_h
, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym
->name
, sym
->name
);
543 fprintf(out_h
, "#else\n");
544 fprintf(out_h
, "# define IF_%s(...) __VA_ARGS__\n", sym
->name
);
545 fprintf(out_h
, "#endif\n");
546 fprintf(out_h
, "#define IF_NOT_%s(...)\n", sym
->name
);
551 str
= sym_get_string_value(sym
);
554 fprintf(out
, "CONFIG_%s=%s\n", sym
->name
, str
);
556 fprintf(out_h
, "#define CONFIG_%s %s\n", sym
->name
, str
);
558 fprintf(out_h
, "#define ENABLE_%s 1\n", sym
->name
);
559 fprintf(out_h
, "#ifdef MAKE_SUID\n");
560 fprintf(out_h
, "# define IF_%s(...) __VA_ARGS__ \"CONFIG_%s\"\n", sym
->name
, sym
->name
);
561 fprintf(out_h
, "#else\n");
562 fprintf(out_h
, "# define IF_%s(...) __VA_ARGS__\n", sym
->name
);
563 fprintf(out_h
, "#endif\n");
564 fprintf(out_h
, "#define IF_NOT_%s(...)\n", sym
->name
);
577 else while ((menu
= menu
->parent
)) {
587 rename(".tmpconfig.h", "include/autoconf.h");
589 if (!name
|| basename
!= conf_def_filename
) {
591 name
= conf_def_filename
;
592 sprintf(tmpname
, "%s.old", name
);
593 rename(name
, tmpname
);
595 sprintf(tmpname
, "%s%s", dirname
, basename
);
596 if (rename(newname
, tmpname
))
599 sym_change_count
= 0;