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[] = "arch/$ARCH/defconfig";
20 const char *conf_confnames[] = {
22 "/lib/modules/$UNAME_RELEASE/.config",
24 "/boot/config-$UNAME_RELEASE",
29 static char *conf_expand_value(const char *in)
33 static char res_value[SYMBOL_MAXLENGTH];
34 char *dst, name[SYMBOL_MAXLENGTH];
38 while ((src = strchr(in, '$'))) {
39 strncat(res_value, in, src - in);
42 while (isalnum(*src) || *src == '_')
45 sym = sym_lookup(name, 0);
47 strcat(res_value, sym_get_string_value(sym));
50 strcat(res_value, in);
55 char *conf_get_default_confname(void)
58 static char fullname[PATH_MAX+1];
61 name = conf_expand_value(conf_defname);
62 env = getenv(SRCTREE);
64 sprintf(fullname, "%s/%s", env, name);
65 if (!stat(fullname, &buf))
71 int conf_read(const char *name)
78 struct property *prop;
83 in = zconf_fopen(name);
85 const char **names = conf_confnames;
86 while ((name = *names++)) {
87 name = conf_expand_value(name);
88 in = zconf_fopen(name);
91 "# using defaults found in %s\n"
101 for_all_symbols(i, sym) {
102 sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
103 sym->flags &= ~SYMBOL_VALID;
111 sym->user.val = NULL;
116 while (fgets(line, sizeof(line), in)) {
121 if (memcmp(line + 2, "CONFIG_", 7))
123 p = strchr(line + 9, ' ');
127 if (strncmp(p, "is not set", 10))
129 sym = sym_find(line + 9);
131 fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line + 9);
138 sym->flags &= ~SYMBOL_NEW;
145 if (memcmp(line, "CONFIG_", 7))
147 p = strchr(line + 7, '=');
151 p2 = strchr(p, '\n');
154 sym = sym_find(line + 7);
156 fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line + 7);
163 sym->flags &= ~SYMBOL_NEW;
169 sym->flags &= ~SYMBOL_NEW;
174 sym->flags &= ~SYMBOL_NEW;
181 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
186 memmove(p2, p2 + 1, strlen(p2));
189 fprintf(stderr, "%s:%d: invalid string found\n", name, lineno);
194 if (sym_string_valid(sym, p)) {
195 sym->user.val = strdup(p);
196 sym->flags &= ~SYMBOL_NEW;
198 fprintf(stderr, "%s:%d: symbol value '%s' invalid for %s\n", name, lineno, p, sym->name);
211 if (sym && sym_is_choice_value(sym)) {
212 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
213 switch (sym->user.tri) {
217 if (cs->user.tri == yes)
221 if (cs->user.tri != no)
226 cs->user.tri = E_OR(cs->user.tri, sym->user.tri);
227 cs->flags &= ~SYMBOL_NEW;
233 sym_calc_value(modules_sym);
234 for_all_symbols(i, sym) {
236 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
237 if (sym->visible == no)
238 sym->flags |= SYMBOL_NEW;
243 if (!sym_string_within_range(sym, sym->user.val))
244 sym->flags |= SYMBOL_NEW;
249 if (!sym_is_choice(sym))
251 prop = sym_get_choice_prop(sym);
252 for (e = prop->expr; e; e = e->left.expr)
253 if (e->right.sym->visible != no)
254 sym->flags |= e->right.sym->flags & SYMBOL_NEW;
257 sym_change_count = 1;
262 int conf_write(const char *name)
267 const char *basename;
268 char dirname[128], tmpname[128], newname[128];
273 if (name && name[0]) {
277 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
278 strcpy(dirname, name);
279 strcat(dirname, "/");
280 basename = conf_def_filename;
281 } else if ((slash = strrchr(name, '/'))) {
282 int size = slash - name + 1;
283 memcpy(dirname, name, size);
286 basename = slash + 1;
288 basename = conf_def_filename;
292 basename = conf_def_filename;
294 sprintf(newname, "%s.tmpconfig.%d", dirname, getpid());
295 out = fopen(newname, "w");
300 out_h = fopen(".tmpconfig.h", "w");
305 "# Automatically generated make config: don't edit\n"
308 fprintf(out_h, "/*\n"
309 " * Automatically generated C config: don't edit\n"
311 "#define AUTOCONF_INCLUDED\n");
313 if (!sym_change_count)
314 sym_clear_all_valid();
316 menu = rootmenu.list;
320 if (!menu_is_visible(menu))
322 str = menu_get_prompt(menu);
332 } else if (!(sym->flags & SYMBOL_CHOICE)) {
334 if (!(sym->flags & SYMBOL_WRITE))
336 sym->flags &= ~SYMBOL_WRITE;
338 if (type == S_TRISTATE) {
339 sym_calc_value(modules_sym);
340 if (modules_sym->curr.tri == no)
346 switch (sym_get_tristate_value(sym)) {
348 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
350 fprintf(out_h, "#undef CONFIG_%s\n", sym->name);
353 fprintf(out, "CONFIG_%s=m\n", sym->name);
355 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
358 fprintf(out, "CONFIG_%s=y\n", sym->name);
360 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
366 str = sym_get_string_value(sym);
367 fprintf(out, "CONFIG_%s=\"", sym->name);
369 fprintf(out_h, "#define CONFIG_%s \"", sym->name);
371 l = strcspn(str, "\"\\");
373 fwrite(str, l, 1, out);
375 fwrite(str, l, 1, out_h);
378 while (*str == '\\' || *str == '"') {
379 fprintf(out, "\\%c", *str);
381 fprintf(out_h, "\\%c", *str);
387 fputs("\"\n", out_h);
390 str = sym_get_string_value(sym);
391 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
392 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
394 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
398 str = sym_get_string_value(sym);
399 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
401 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
413 else while ((menu = menu->parent)) {
423 rename(".tmpconfig.h", "include/linux/autoconf.h");
424 file_write_dep(NULL);
426 if (!name || basename != conf_def_filename) {
428 name = conf_def_filename;
429 sprintf(tmpname, "%s.old", name);
430 rename(name, tmpname);
432 sprintf(tmpname, "%s%s", dirname, basename);
433 if (rename(newname, tmpname))
436 sym_change_count = 0;