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
16 const char conf_def_filename
[] = ".config";
18 const char conf_defname
[] = ".defconfig";
20 const char *conf_confnames
[] = {
26 static char *conf_expand_value(const signed char *in
)
29 const signed char *src
;
30 static char res_value
[SYMBOL_MAXLENGTH
];
31 char *dst
, name
[SYMBOL_MAXLENGTH
];
35 while ((src
= strchr(in
, '$'))) {
36 strncat(res_value
, in
, src
- in
);
39 while (isalnum(*src
) || *src
== '_')
42 sym
= sym_lookup(name
, 0);
44 strcat(res_value
, sym_get_string_value(sym
));
47 strcat(res_value
, in
);
52 char *conf_get_default_confname(void)
55 static char fullname
[PATH_MAX
+1];
58 name
= conf_expand_value(conf_defname
);
59 env
= getenv(SRCTREE
);
61 sprintf(fullname
, "%s/%s", env
, name
);
62 if (!stat(fullname
, &buf
))
68 int conf_read(const char *name
)
75 struct property
*prop
;
80 in
= zconf_fopen(name
);
82 const char **names
= conf_confnames
;
83 while ((name
= *names
++)) {
84 name
= conf_expand_value(name
);
85 in
= zconf_fopen(name
);
88 "# using defaults found in %s\n"
98 for_all_symbols(i
, sym
) {
99 sym
->flags
|= SYMBOL_NEW
| SYMBOL_CHANGED
;
100 sym
->flags
&= ~SYMBOL_VALID
;
108 sym
->user
.val
= NULL
;
113 while (fgets(line
, sizeof(line
), in
)) {
120 p
= strchr(line
+ 2, ' ');
124 if (strncmp(p
, "is not set", 10))
126 sym
= sym_find(line
+ 2);
128 fprintf(stderr
, "%s:%d: trying to assign nonexistent symbol %s\n", name
, lineno
, line
+ 2);
135 sym
->flags
&= ~SYMBOL_NEW
;
143 p
= strchr(line
, '=');
147 p2
= strchr(p
, '\n');
150 sym
= sym_find(line
);
152 fprintf(stderr
, "%s:%d: trying to assign nonexistent symbol %s\n", name
, lineno
, line
);
159 sym
->flags
&= ~SYMBOL_NEW
;
165 sym
->flags
&= ~SYMBOL_NEW
;
170 sym
->flags
&= ~SYMBOL_NEW
;
177 for (p2
= p
; (p2
= strpbrk(p2
, "\"\\")); p2
++) {
182 memmove(p2
, p2
+ 1, strlen(p2
));
185 fprintf(stderr
, "%s:%d: invalid string found\n", name
, lineno
);
190 if (sym_string_valid(sym
, p
)) {
191 sym
->user
.val
= strdup(p
);
192 sym
->flags
&= ~SYMBOL_NEW
;
194 fprintf(stderr
, "%s:%d: symbol value '%s' invalid for %s\n", name
, lineno
, p
, sym
->name
);
207 if (sym
&& sym_is_choice_value(sym
)) {
208 struct symbol
*cs
= prop_get_symbol(sym_get_choice_prop(sym
));
209 switch (sym
->user
.tri
) {
213 if (cs
->user
.tri
== yes
)
217 if (cs
->user
.tri
!= no
)
222 cs
->user
.tri
= E_OR(cs
->user
.tri
, sym
->user
.tri
);
223 cs
->flags
&= ~SYMBOL_NEW
;
229 sym_calc_value(modules_sym
);
230 for_all_symbols(i
, sym
) {
232 if (sym_has_value(sym
) && !sym_is_choice_value(sym
)) {
233 if (sym
->visible
== no
)
234 sym
->flags
|= SYMBOL_NEW
;
239 if (!sym_string_within_range(sym
, sym
->user
.val
))
240 sym
->flags
|= SYMBOL_NEW
;
245 if (!sym_is_choice(sym
))
247 prop
= sym_get_choice_prop(sym
);
248 for (e
= prop
->expr
; e
; e
= e
->left
.expr
)
249 if (e
->right
.sym
->visible
!= no
)
250 sym
->flags
|= e
->right
.sym
->flags
& SYMBOL_NEW
;
253 sym_change_count
= 1;
258 int conf_write(const char *name
)
263 const char *basename
;
264 char dirname
[128], tmpname
[128], newname
[128];
269 if (name
&& name
[0]) {
273 if (!stat(name
, &st
) && S_ISDIR(st
.st_mode
)) {
274 strcpy(dirname
, name
);
275 strcat(dirname
, "/");
276 basename
= conf_def_filename
;
277 } else if ((slash
= strrchr(name
, '/'))) {
278 int size
= slash
- name
+ 1;
279 memcpy(dirname
, name
, size
);
282 basename
= slash
+ 1;
284 basename
= conf_def_filename
;
288 basename
= conf_def_filename
;
290 sprintf(newname
, "%s.tmpconfig.%d", dirname
, (int)getpid());
291 out
= fopen(newname
, "w");
295 "# Automatically generated make config: don't edit\n"
298 if (!sym_change_count
)
299 sym_clear_all_valid();
301 menu
= rootmenu
.list
;
305 if (!menu_is_visible(menu
))
307 str
= menu_get_prompt(menu
);
312 } else if (!(sym
->flags
& SYMBOL_CHOICE
)) {
314 if (!(sym
->flags
& SYMBOL_WRITE
))
316 sym
->flags
&= ~SYMBOL_WRITE
;
318 if (type
== S_TRISTATE
) {
319 sym_calc_value(modules_sym
);
320 if (modules_sym
->curr
.tri
== no
)
326 switch (sym_get_tristate_value(sym
)) {
328 fprintf(out
, "# %s is not set\n", sym
->name
);
331 fprintf(out
, "%s=m\n", sym
->name
);
334 fprintf(out
, "%s=y\n", sym
->name
);
340 str
= sym_get_string_value(sym
);
341 fprintf(out
, "%s=\"", sym
->name
);
343 l
= strcspn(str
, "\"\\");
345 fwrite(str
, l
, 1, out
);
348 while (*str
== '\\' || *str
== '"') {
349 fprintf(out
, "\\%c", *str
);
356 str
= sym_get_string_value(sym
);
357 if (str
[0] != '0' || (str
[1] != 'x' && str
[1] != 'X')) {
358 fprintf(out
, "%s=%s\n", sym
->name
, str
);
362 str
= sym_get_string_value(sym
);
363 fprintf(out
, "%s=%s\n", sym
->name
, str
);
375 else while ((menu
= menu
->parent
)) {
383 file_write_dep(NULL
);
384 if (!name
|| basename
!= conf_def_filename
) {
386 name
= conf_def_filename
;
387 sprintf(tmpname
, "%s.old", name
);
388 rename(name
, tmpname
);
390 sprintf(tmpname
, "%s%s", dirname
, basename
);
391 if (rename(newname
, tmpname
))
394 sym_change_count
= 0;