2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
13 #define LKC_DIRECT_LINK
18 const char conf_def_filename
[] = ".config";
20 const char conf_defname
[] = "Config.default";
22 const char *conf_confnames
[] = {
28 static char *conf_expand_value(const char *in
)
32 static char res_value
[SYMBOL_MAXLENGTH
];
33 char *dst
, name
[SYMBOL_MAXLENGTH
];
37 while ((src
= strchr(in
, '$'))) {
38 strncat(res_value
, in
, src
- in
);
41 while (isalnum(*src
) || *src
== '_')
44 sym
= sym_lookup(name
, 0);
46 strcat(res_value
, sym_get_string_value(sym
));
49 strcat(res_value
, in
);
54 char *conf_get_default_confname(void)
57 static char fullname
[PATH_MAX
+1];
60 name
= conf_expand_value(conf_defname
);
61 env
= getenv(SRCTREE
);
63 sprintf(fullname
, "%s/%s", env
, name
);
64 if (!stat(fullname
, &buf
))
70 int conf_read(const char *name
)
77 struct property
*prop
;
82 in
= zconf_fopen(name
);
84 const char **names
= conf_confnames
;
85 while ((name
= *names
++)) {
86 name
= conf_expand_value(name
);
87 in
= zconf_fopen(name
);
90 "# using defaults found in %s\n"
100 for_all_symbols(i
, sym
) {
101 sym
->flags
|= SYMBOL_NEW
| SYMBOL_CHANGED
;
102 sym
->flags
&= ~SYMBOL_VALID
;
110 sym
->user
.val
= NULL
;
115 while (fgets(line
, sizeof(line
), in
)) {
122 p
= strchr(line
+ 2, ' ');
126 if (strncmp(p
, "is not set", 10))
128 sym
= sym_find(line
+ 2);
130 fprintf(stderr
, "%s:%d: trying to assign nonexistent symbol %s\n", name
, lineno
, line
+ 2);
137 sym
->flags
&= ~SYMBOL_NEW
;
145 p
= strchr(line
, '=');
149 p2
= strchr(p
, '\n');
152 sym
= sym_find(line
);
154 fprintf(stderr
, "%s:%d: trying to assign nonexistent symbol %s\n", name
, lineno
, line
);
161 sym
->flags
&= ~SYMBOL_NEW
;
167 sym
->flags
&= ~SYMBOL_NEW
;
172 sym
->flags
&= ~SYMBOL_NEW
;
179 for (p2
= p
; (p2
= strpbrk(p2
, "\"\\")); p2
++) {
184 memmove(p2
, p2
+ 1, strlen(p2
));
187 fprintf(stderr
, "%s:%d: invalid string found\n", name
, lineno
);
192 if (sym_string_valid(sym
, p
)) {
193 sym
->user
.val
= strdup(p
);
194 sym
->flags
&= ~SYMBOL_NEW
;
196 fprintf(stderr
, "%s:%d: symbol value '%s' invalid for %s\n", name
, lineno
, p
, sym
->name
);
209 if (sym
&& sym_is_choice_value(sym
)) {
210 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
211 switch (sym
->user
.tri
) {
215 if (cs
->user
.tri
== yes
)
219 if (cs
->user
.tri
!= no
)
224 cs
->user
.tri
= E_OR(cs
->user
.tri
, sym
->user
.tri
);
225 cs
->flags
&= ~SYMBOL_NEW
;
231 sym_calc_value(modules_sym
);
232 for_all_symbols(i
, sym
) {
234 if (sym_has_value(sym
) && !sym_is_choice_value(sym
)) {
235 if (sym
->visible
== no
)
236 sym
->flags
|= SYMBOL_NEW
;
241 if (!sym_string_within_range(sym
, sym
->user
.val
))
242 sym
->flags
|= SYMBOL_NEW
;
247 if (!sym_is_choice(sym
))
249 prop
= sym_get_choice_prop(sym
);
250 for (e
= prop
->expr
; e
; e
= e
->left
.expr
)
251 if (e
->right
.sym
->visible
!= no
)
252 sym
->flags
|= e
->right
.sym
->flags
& SYMBOL_NEW
;
255 sym_change_count
= 1;
260 int conf_write(const char *name
)
265 const char *basename
;
266 char dirname
[128], tmpname
[128], newname
[128];
271 if (name
&& name
[0]) {
275 if (!stat(name
, &st
) && S_ISDIR(st
.st_mode
)) {
276 strcpy(dirname
, name
);
277 strcat(dirname
, "/");
278 basename
= conf_def_filename
;
279 } else if ((slash
= strrchr(name
, '/'))) {
280 int size
= slash
- name
+ 1;
281 memcpy(dirname
, name
, size
);
284 basename
= slash
+ 1;
286 basename
= conf_def_filename
;
290 basename
= conf_def_filename
;
292 sprintf(newname
, "%s.tmpconfig.%d", dirname
, getpid());
293 out
= fopen(newname
, "w");
298 out_h
= fopen(".tmpconfig.h", "w");
303 "# Automatically generated make config: don't edit\n"
306 fprintf(out_h
, "/*\n"
307 " * Automatically generated header file: don't edit\n"
309 "#define AUTOCONF_INCLUDED\n\n"
310 "/* Version Number */\n"
311 "#define BB_VER \"%s\"\n"
312 "#define BB_BT \"%s\"\n",
314 getenv("BUILDTIME"));
315 if (getenv("EXTRA_VERSION"))
316 fprintf(out_h
, "#define BB_EXTRA_VERSION \"%s\"\n",
317 getenv("EXTRA_VERSION"));
318 fprintf(out_h
, "\n");
321 if (!sym_change_count
)
322 sym_clear_all_valid();
324 menu
= rootmenu
.list
;
328 if (!menu_is_visible(menu
))
330 str
= menu_get_prompt(menu
);
340 } else if (!(sym
->flags
& SYMBOL_CHOICE
)) {
342 if (!(sym
->flags
& SYMBOL_WRITE
) && !output_mode
)
344 sym
->flags
&= ~SYMBOL_WRITE
;
346 if (type
== S_TRISTATE
) {
347 sym_calc_value(modules_sym
);
348 if (modules_sym
->curr
.tri
== no
)
354 switch (sym_get_tristate_value(sym
)) {
356 fprintf(out
, "# %s is not set\n", sym
->name
);
358 fprintf(out_h
, "#undef %s\n", sym
->name
);
361 fprintf(out
, "%s=m\n", sym
->name
);
364 fprintf(out_h
, "#define %s_MODULE 1\n", sym
->name
);
368 fprintf(out
, "%s=y\n", sym
->name
);
370 fprintf(out_h
, "#define %s 1\n", sym
->name
);
376 str
= sym_get_string_value(sym
);
377 fprintf(out
, "%s=\"", sym
->name
);
379 fprintf(out_h
, "#define %s \"", sym
->name
);
381 l
= strcspn(str
, "\"\\");
383 fwrite(str
, l
, 1, out
);
385 fwrite(str
, l
, 1, out_h
);
388 while (*str
== '\\' || *str
== '"') {
389 fprintf(out
, "\\%c", *str
);
391 fprintf(out_h
, "\\%c", *str
);
397 fputs("\"\n", out_h
);
400 str
= sym_get_string_value(sym
);
401 if (str
[0] != '0' || (str
[1] != 'x' && str
[1] != 'X')) {
402 fprintf(out
, "%s=%s\n", sym
->name
, str
);
404 fprintf(out_h
, "#define %s 0x%s\n", sym
->name
, str
);
408 str
= sym_get_string_value(sym
);
409 fprintf(out
, "%s=%s\n", sym
->name
, str
);
411 fprintf(out_h
, "#define %s %s\n", sym
->name
, str
);
423 else while ((menu
= menu
->parent
)) {
433 rename(".tmpconfig.h", "include/config.h");
434 file_write_dep(NULL
);
436 if (!name
|| basename
!= conf_def_filename
) {
438 name
= conf_def_filename
;
439 sprintf(tmpname
, "%s.old", name
);
440 rename(name
, tmpname
);
442 sprintf(tmpname
, "%s%s", dirname
, basename
);
443 if (rename(newname
, tmpname
))
446 sym_change_count
= 0;