blob: ca16ab4367dd04fc0ea644d0f7213c286f9bed1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
5
6#include <sys/stat.h>
7#include <ctype.h>
Arnaud Lacombe94bedec2010-08-17 01:40:20 -04008#include <errno.h>
Roman Zippel2e3646e2006-06-08 22:12:42 -07009#include <fcntl.h>
Arnaud Lacombe10a4b272011-06-01 16:00:46 -040010#include <stdarg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <time.h>
15#include <unistd.h>
16
17#define LKC_DIRECT_LINK
18#include "lkc.h"
19
Roman Zippelc1a0f5e2005-11-08 21:34:54 -080020static void conf_warning(const char *fmt, ...)
21 __attribute__ ((format (printf, 1, 2)));
22
Michal Marek42368c32010-08-17 10:21:19 +020023static void conf_message(const char *fmt, ...)
24 __attribute__ ((format (printf, 1, 2)));
25
Roman Zippelc1a0f5e2005-11-08 21:34:54 -080026static const char *conf_filename;
27static int conf_lineno, conf_warnings, conf_unsaved;
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029const char conf_defname[] = "arch/$ARCH/defconfig";
30
Roman Zippelc1a0f5e2005-11-08 21:34:54 -080031static void conf_warning(const char *fmt, ...)
32{
33 va_list ap;
34 va_start(ap, fmt);
35 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
36 vfprintf(stderr, fmt, ap);
37 fprintf(stderr, "\n");
38 va_end(ap);
39 conf_warnings++;
40}
41
Michal Marek42368c32010-08-17 10:21:19 +020042static void conf_default_message_callback(const char *fmt, va_list ap)
43{
44 printf("#\n# ");
45 vprintf(fmt, ap);
46 printf("\n#\n");
47}
48
49static void (*conf_message_callback) (const char *fmt, va_list ap) =
50 conf_default_message_callback;
51void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap))
52{
53 conf_message_callback = fn;
54}
55
56static void conf_message(const char *fmt, ...)
57{
58 va_list ap;
59
60 va_start(ap, fmt);
61 if (conf_message_callback)
62 conf_message_callback(fmt, ap);
63}
64
Roman Zippel14cdd3c2006-06-08 22:12:51 -070065const char *conf_get_configname(void)
66{
67 char *name = getenv("KCONFIG_CONFIG");
68
69 return name ? name : ".config";
70}
71
Markus Heidelberg12122f62009-05-18 01:36:54 +020072const char *conf_get_autoconfig_name(void)
73{
74 char *name = getenv("KCONFIG_AUTOCONFIG");
75
76 return name ? name : "include/config/auto.conf";
77}
78
J.A. Magallon48b9d032005-06-25 14:59:22 -070079static char *conf_expand_value(const char *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 struct symbol *sym;
J.A. Magallon48b9d032005-06-25 14:59:22 -070082 const char *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 static char res_value[SYMBOL_MAXLENGTH];
84 char *dst, name[SYMBOL_MAXLENGTH];
85
86 res_value[0] = 0;
87 dst = name;
88 while ((src = strchr(in, '$'))) {
89 strncat(res_value, in, src - in);
90 src++;
91 dst = name;
92 while (isalnum(*src) || *src == '_')
93 *dst++ = *src++;
94 *dst = 0;
95 sym = sym_lookup(name, 0);
96 sym_calc_value(sym);
97 strcat(res_value, sym_get_string_value(sym));
98 in = src;
99 }
100 strcat(res_value, in);
101
102 return res_value;
103}
104
105char *conf_get_default_confname(void)
106{
107 struct stat buf;
108 static char fullname[PATH_MAX+1];
109 char *env, *name;
110
111 name = conf_expand_value(conf_defname);
112 env = getenv(SRCTREE);
113 if (env) {
114 sprintf(fullname, "%s/%s", env, name);
115 if (!stat(fullname, &buf))
116 return fullname;
117 }
118 return name;
119}
120
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100121static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
122{
123 char *p2;
124
125 switch (sym->type) {
126 case S_TRISTATE:
127 if (p[0] == 'm') {
128 sym->def[def].tri = mod;
129 sym->flags |= def_flags;
130 break;
131 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400132 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100133 case S_BOOLEAN:
134 if (p[0] == 'y') {
135 sym->def[def].tri = yes;
136 sym->flags |= def_flags;
137 break;
138 }
139 if (p[0] == 'n') {
140 sym->def[def].tri = no;
141 sym->flags |= def_flags;
142 break;
143 }
144 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
Arnaud Lacombe75f14682011-05-31 12:31:57 -0400145 return 1;
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100146 case S_OTHER:
147 if (*p != '"') {
148 for (p2 = p; *p2 && !isspace(*p2); p2++)
149 ;
150 sym->type = S_STRING;
151 goto done;
152 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400153 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100154 case S_STRING:
155 if (*p++ != '"')
156 break;
157 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
158 if (*p2 == '"') {
159 *p2 = 0;
160 break;
161 }
162 memmove(p2, p2 + 1, strlen(p2));
163 }
164 if (!p2) {
165 conf_warning("invalid string found");
166 return 1;
167 }
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400168 /* fall through */
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100169 case S_INT:
170 case S_HEX:
171 done:
172 if (sym_string_valid(sym, p)) {
173 sym->def[def].val = strdup(p);
174 sym->flags |= def_flags;
175 } else {
176 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
177 return 1;
178 }
179 break;
180 default:
181 ;
182 }
183 return 0;
184}
185
Roman Zippel669bfad92006-06-08 22:12:42 -0700186int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 FILE *in = NULL;
189 char line[1024];
190 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct symbol *sym;
Roman Zippel669bfad92006-06-08 22:12:42 -0700192 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (name) {
195 in = zconf_fopen(name);
196 } else {
Roman Zippelface4372006-06-08 22:12:45 -0700197 struct property *prop;
198
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700199 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700200 in = zconf_fopen(name);
201 if (in)
202 goto load;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800203 sym_add_change_count(1);
Ulf Magnussonac1ffde2010-07-27 21:57:43 +0200204 if (!sym_defconfig_list) {
205 if (modules_sym)
206 sym_calc_value(modules_sym);
Roman Zippelface4372006-06-08 22:12:45 -0700207 return 1;
Ulf Magnussonac1ffde2010-07-27 21:57:43 +0200208 }
Roman Zippelface4372006-06-08 22:12:45 -0700209
210 for_all_defaults(sym_defconfig_list, prop) {
211 if (expr_calc_value(prop->visible.expr) == no ||
212 prop->expr->type != E_SYMBOL)
213 continue;
214 name = conf_expand_value(prop->expr->left.sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 in = zconf_fopen(name);
216 if (in) {
Michal Marek42368c32010-08-17 10:21:19 +0200217 conf_message(_("using defaults found in %s"),
218 name);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700219 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221 }
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (!in)
224 return 1;
225
Roman Zippelddc97ca2006-06-08 22:12:38 -0700226load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800227 conf_filename = name;
228 conf_lineno = 0;
229 conf_warnings = 0;
230 conf_unsaved = 0;
231
Roman Zippel669bfad92006-06-08 22:12:42 -0700232 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 for_all_symbols(i, sym) {
Roman Zippel669bfad92006-06-08 22:12:42 -0700234 sym->flags |= SYMBOL_CHANGED;
235 sym->flags &= ~(def_flags|SYMBOL_VALID);
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800236 if (sym_is_choice(sym))
Roman Zippel669bfad92006-06-08 22:12:42 -0700237 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 switch (sym->type) {
239 case S_INT:
240 case S_HEX:
241 case S_STRING:
Roman Zippel669bfad92006-06-08 22:12:42 -0700242 if (sym->def[def].val)
243 free(sym->def[def].val);
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400244 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 default:
Roman Zippel669bfad92006-06-08 22:12:42 -0700246 sym->def[def].val = NULL;
247 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 }
250
251 while (fgets(line, sizeof(line), in)) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800252 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 sym = NULL;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400254 if (line[0] == '#') {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400255 if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 continue;
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400257 p = strchr(line + 2 + strlen(CONFIG_), ' ');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (!p)
259 continue;
260 *p++ = 0;
261 if (strncmp(p, "is not set", 10))
262 continue;
Roman Zippel669bfad92006-06-08 22:12:42 -0700263 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400264 sym = sym_find(line + 2 + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200265 if (!sym) {
266 sym_add_change_count(1);
Naohiro Aota8bea7542010-10-01 04:23:17 +0900267 goto setsym;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200268 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700269 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400270 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700271 if (sym->type == S_UNKNOWN)
272 sym->type = S_BOOLEAN;
273 }
274 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100275 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277 switch (sym->type) {
278 case S_BOOLEAN:
279 case S_TRISTATE:
Roman Zippel669bfad92006-06-08 22:12:42 -0700280 sym->def[def].tri = no;
281 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 default:
284 ;
285 }
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400286 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
287 p = strchr(line + strlen(CONFIG_), '=');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!p)
289 continue;
290 *p++ = 0;
291 p2 = strchr(p, '\n');
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600292 if (p2) {
293 *p2-- = 0;
294 if (*p2 == '\r')
295 *p2 = 0;
296 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700297 if (def == S_DEF_USER) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400298 sym = sym_find(line + strlen(CONFIG_));
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200299 if (!sym) {
300 sym_add_change_count(1);
Naohiro Aota8bea7542010-10-01 04:23:17 +0900301 goto setsym;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200302 }
Roman Zippel669bfad92006-06-08 22:12:42 -0700303 } else {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400304 sym = sym_lookup(line + strlen(CONFIG_), 0);
Roman Zippel669bfad92006-06-08 22:12:42 -0700305 if (sym->type == S_UNKNOWN)
306 sym->type = S_OTHER;
307 }
308 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100309 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100311 if (conf_set_sym_val(sym, def, def_flags, p))
312 continue;
Arnaud Lacombe8baefd32010-08-24 00:14:47 -0400313 } else {
314 if (line[0] != '\r' && line[0] != '\n')
315 conf_warning("unexpected data");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 continue;
317 }
Naohiro Aota8bea7542010-10-01 04:23:17 +0900318setsym:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (sym && sym_is_choice_value(sym)) {
320 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad92006-06-08 22:12:42 -0700321 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 case no:
323 break;
324 case mod:
Roman Zippel669bfad92006-06-08 22:12:42 -0700325 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800326 conf_warning("%s creates inconsistent choice state", sym->name);
Roman Zippel669bfad92006-06-08 22:12:42 -0700327 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
330 case yes:
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100331 if (cs->def[def].tri != no)
332 conf_warning("override: %s changes choice state", sym->name);
333 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 break;
335 }
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100336 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 }
339 fclose(in);
340
341 if (modules_sym)
342 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800343 return 0;
344}
345
346int conf_read(const char *name)
347{
Roman Zippel7a962922008-01-14 04:50:23 +0100348 struct symbol *sym, *choice_sym;
Roman Zippel90389162005-11-08 21:34:49 -0800349 struct property *prop;
350 struct expr *e;
Roman Zippel669bfad92006-06-08 22:12:42 -0700351 int i, flags;
Roman Zippel90389162005-11-08 21:34:49 -0800352
Karsten Wiesebfc10002006-12-13 00:34:07 -0800353 sym_set_change_count(0);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700354
Roman Zippel669bfad92006-06-08 22:12:42 -0700355 if (conf_read_simple(name, S_DEF_USER))
Roman Zippel90389162005-11-08 21:34:49 -0800356 return 1;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 for_all_symbols(i, sym) {
359 sym_calc_value(sym);
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800360 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
361 goto sym_ok;
362 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
363 /* check that calculated value agrees with saved value */
364 switch (sym->type) {
365 case S_BOOLEAN:
366 case S_TRISTATE:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700367 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800368 break;
369 if (!sym_is_choice(sym))
370 goto sym_ok;
Arnaud Lacombed8fc3202011-05-31 12:30:26 -0400371 /* fall through */
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800372 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700373 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800374 goto sym_ok;
375 break;
376 }
377 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
378 /* no previous value and not saved */
379 goto sym_ok;
380 conf_unsaved++;
381 /* maybe print value in verbose mode... */
382 sym_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (!sym_is_choice(sym))
384 continue;
Roman Zippeld8982ba2007-07-09 11:43:58 -0700385 /* The choice symbol only has a set value (and thus is not new)
386 * if all its visible childs have values.
387 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 prop = sym_get_choice_prop(sym);
Roman Zippel669bfad92006-06-08 22:12:42 -0700389 flags = sym->flags;
Roman Zippel7a962922008-01-14 04:50:23 +0100390 expr_list_for_each_sym(prop->expr, e, choice_sym)
391 if (choice_sym->visible != no)
392 flags &= choice_sym->flags;
Roman Zippel002d27b2006-07-13 13:22:38 +0200393 sym->flags &= flags | ~SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
Roman Zippeld8982ba2007-07-09 11:43:58 -0700396 for_all_symbols(i, sym) {
397 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
398 /* Reset values of generates values, so they'll appear
399 * as new, if they should become visible, but that
400 * doesn't quite work if the Kconfig and the saved
401 * configuration disagree.
402 */
403 if (sym->visible == no && !conf_unsaved)
404 sym->flags &= ~SYMBOL_DEF_USER;
405 switch (sym->type) {
406 case S_STRING:
407 case S_INT:
408 case S_HEX:
409 /* Reset a string value if it's out of range */
410 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
411 break;
412 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
413 conf_unsaved++;
414 break;
415 default:
416 break;
417 }
418 }
419 }
420
Karsten Wiesebfc10002006-12-13 00:34:07 -0800421 sym_add_change_count(conf_warnings || conf_unsaved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 return 0;
424}
425
Sam Ravnborg49192f22010-07-31 23:35:33 +0200426/* Write a S_STRING */
427static void conf_write_string(bool headerfile, const char *name,
428 const char *str, FILE *out)
429{
430 int l;
431 if (headerfile)
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400432 fprintf(out, "#define %s%s \"", CONFIG_, name);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200433 else
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400434 fprintf(out, "%s%s=\"", CONFIG_, name);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200435
436 while (1) {
437 l = strcspn(str, "\"\\");
438 if (l) {
Jean Sacrenbf5e3272010-08-04 16:01:02 -0600439 xfwrite(str, l, 1, out);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200440 str += l;
441 }
442 if (!*str)
443 break;
444 fprintf(out, "\\%c", *str++);
445 }
446 fputs("\"\n", out);
447}
448
Arnaud Lacombe0dce6312010-12-05 01:33:16 -0500449static void conf_write_symbol(struct symbol *sym, FILE *out, bool write_no)
Sam Ravnborg49192f22010-07-31 23:35:33 +0200450{
451 const char *str;
452
Arnaud Lacombe0dce6312010-12-05 01:33:16 -0500453 switch (sym->type) {
Sam Ravnborg49192f22010-07-31 23:35:33 +0200454 case S_BOOLEAN:
455 case S_TRISTATE:
456 switch (sym_get_tristate_value(sym)) {
457 case no:
458 if (write_no)
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400459 fprintf(out, "# %s%s is not set\n",
460 CONFIG_, sym->name);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200461 break;
462 case mod:
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400463 fprintf(out, "%s%s=m\n", CONFIG_, sym->name);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200464 break;
465 case yes:
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400466 fprintf(out, "%s%s=y\n", CONFIG_, sym->name);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200467 break;
468 }
469 break;
470 case S_STRING:
471 conf_write_string(false, sym->name, sym_get_string_value(sym), out);
472 break;
473 case S_HEX:
474 case S_INT:
475 str = sym_get_string_value(sym);
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400476 fprintf(out, "%s%s=%s\n", CONFIG_, sym->name, str);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200477 break;
478 case S_OTHER:
479 case S_UNKNOWN:
480 break;
481 }
482}
483
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200484/*
485 * Write out a minimal config.
486 * All values that has default values are skipped as this is redundant.
487 */
488int conf_write_defconfig(const char *filename)
489{
490 struct symbol *sym;
491 struct menu *menu;
492 FILE *out;
493
494 out = fopen(filename, "w");
495 if (!out)
496 return 1;
497
498 sym_clear_all_valid();
499
500 /* Traverse all menus to find all relevant symbols */
501 menu = rootmenu.list;
502
503 while (menu != NULL)
504 {
505 sym = menu->sym;
506 if (sym == NULL) {
507 if (!menu_is_visible(menu))
508 goto next_menu;
509 } else if (!sym_is_choice(sym)) {
510 sym_calc_value(sym);
511 if (!(sym->flags & SYMBOL_WRITE))
512 goto next_menu;
513 sym->flags &= ~SYMBOL_WRITE;
514 /* If we cannot change the symbol - skip */
515 if (!sym_is_changable(sym))
516 goto next_menu;
517 /* If symbol equals to default value - skip */
518 if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
519 goto next_menu;
520
521 /*
522 * If symbol is a choice value and equals to the
523 * default for a choice - skip.
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200524 * But only if value is bool and equal to "y" and
525 * choice is not "optional".
526 * (If choice is "optional" then all values can be "n")
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200527 */
528 if (sym_is_choice_value(sym)) {
529 struct symbol *cs;
530 struct symbol *ds;
531
532 cs = prop_get_symbol(sym_get_choice_prop(sym));
533 ds = sym_choice_default(cs);
Sam Ravnborg84062dd2010-08-14 23:22:16 +0200534 if (!sym_is_optional(cs) && sym == ds) {
Sam Ravnborg801690c2010-08-12 09:11:51 +0200535 if ((sym->type == S_BOOLEAN) &&
536 sym_get_tristate_value(sym) == yes)
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200537 goto next_menu;
538 }
539 }
Arnaud Lacombe0dce6312010-12-05 01:33:16 -0500540 conf_write_symbol(sym, out, true);
Sam Ravnborg7cf3d732010-07-31 23:35:34 +0200541 }
542next_menu:
543 if (menu->list != NULL) {
544 menu = menu->list;
545 }
546 else if (menu->next != NULL) {
547 menu = menu->next;
548 } else {
549 while ((menu = menu->parent)) {
550 if (menu->next != NULL) {
551 menu = menu->next;
552 break;
553 }
554 }
555 }
556 }
557 fclose(out);
558 return 0;
559}
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561int conf_write(const char *name)
562{
Roman Zippelc955cca2006-06-08 22:12:39 -0700563 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 struct symbol *sym;
565 struct menu *menu;
566 const char *basename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 const char *str;
Will Newton1408b152010-09-22 15:59:13 +0100568 char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 char *env;
570
571 dirname[0] = 0;
572 if (name && name[0]) {
573 struct stat st;
574 char *slash;
575
576 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
577 strcpy(dirname, name);
578 strcat(dirname, "/");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700579 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 } else if ((slash = strrchr(name, '/'))) {
581 int size = slash - name + 1;
582 memcpy(dirname, name, size);
583 dirname[size] = 0;
584 if (slash[1])
585 basename = slash + 1;
586 else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700587 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 } else
589 basename = name;
590 } else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700591 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700593 sprintf(newname, "%s%s", dirname, basename);
594 env = getenv("KCONFIG_OVERWRITECONFIG");
595 if (!env || !*env) {
596 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
597 out = fopen(tmpname, "w");
598 } else {
599 *tmpname = 0;
600 out = fopen(newname, "w");
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!out)
603 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700604
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700605 fprintf(out, _("#\n"
606 "# Automatically generated make config: don't edit\n"
Arnaud Lacombe09548282010-08-18 01:57:13 -0400607 "# %s\n"
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700608 "#\n"),
Arnaud Lacombebdebd482011-05-15 23:22:56 -0400609 rootmenu.prompt->text);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Karsten Wieseb3214292006-12-13 00:34:06 -0800611 if (!conf_get_changed())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 sym_clear_all_valid();
613
614 menu = rootmenu.list;
615 while (menu) {
616 sym = menu->sym;
617 if (!sym) {
618 if (!menu_is_visible(menu))
619 goto next;
620 str = menu_get_prompt(menu);
621 fprintf(out, "\n"
622 "#\n"
623 "# %s\n"
624 "#\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 } else if (!(sym->flags & SYMBOL_CHOICE)) {
626 sym_calc_value(sym);
627 if (!(sym->flags & SYMBOL_WRITE))
628 goto next;
629 sym->flags &= ~SYMBOL_WRITE;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200630 /* Write config symbol to file */
Arnaud Lacombe0dce6312010-12-05 01:33:16 -0500631 conf_write_symbol(sym, out, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
Sam Ravnborg49192f22010-07-31 23:35:33 +0200634next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (menu->list) {
636 menu = menu->list;
637 continue;
638 }
639 if (menu->next)
640 menu = menu->next;
641 else while ((menu = menu->parent)) {
642 if (menu->next) {
643 menu = menu->next;
644 break;
645 }
646 }
647 }
648 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700649
650 if (*tmpname) {
Sam Ravnborg9a3d0fe2006-10-01 11:48:53 +0200651 strcat(dirname, basename);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700652 strcat(dirname, ".old");
653 rename(newname, dirname);
654 if (rename(tmpname, newname))
655 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Michal Marek42368c32010-08-17 10:21:19 +0200658 conf_message(_("configuration written to %s"), newname);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700659
Karsten Wiesebfc10002006-12-13 00:34:07 -0800660 sym_set_change_count(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 return 0;
663}
Roman Zippelc955cca2006-06-08 22:12:39 -0700664
Trevor Keith4356f482009-09-18 12:49:23 -0700665static int conf_split_config(void)
Roman Zippel2e3646e2006-06-08 22:12:42 -0700666{
Markus Heidelberg12122f62009-05-18 01:36:54 +0200667 const char *name;
Will Newton1408b152010-09-22 15:59:13 +0100668 char path[PATH_MAX+1];
Roman Zippel2e3646e2006-06-08 22:12:42 -0700669 char *s, *d, c;
670 struct symbol *sym;
671 struct stat sb;
672 int res, i, fd;
673
Markus Heidelberg12122f62009-05-18 01:36:54 +0200674 name = conf_get_autoconfig_name();
Roman Zippel2e3646e2006-06-08 22:12:42 -0700675 conf_read_simple(name, S_DEF_AUTO);
676
677 if (chdir("include/config"))
678 return 1;
679
680 res = 0;
681 for_all_symbols(i, sym) {
682 sym_calc_value(sym);
683 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
684 continue;
685 if (sym->flags & SYMBOL_WRITE) {
686 if (sym->flags & SYMBOL_DEF_AUTO) {
687 /*
688 * symbol has old and new value,
689 * so compare them...
690 */
691 switch (sym->type) {
692 case S_BOOLEAN:
693 case S_TRISTATE:
694 if (sym_get_tristate_value(sym) ==
695 sym->def[S_DEF_AUTO].tri)
696 continue;
697 break;
698 case S_STRING:
699 case S_HEX:
700 case S_INT:
701 if (!strcmp(sym_get_string_value(sym),
702 sym->def[S_DEF_AUTO].val))
703 continue;
704 break;
705 default:
706 break;
707 }
708 } else {
709 /*
710 * If there is no old value, only 'no' (unset)
711 * is allowed as new value.
712 */
713 switch (sym->type) {
714 case S_BOOLEAN:
715 case S_TRISTATE:
716 if (sym_get_tristate_value(sym) == no)
717 continue;
718 break;
719 default:
720 break;
721 }
722 }
723 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
724 /* There is neither an old nor a new value. */
725 continue;
726 /* else
727 * There is an old value, but no new value ('no' (unset)
728 * isn't saved in auto.conf, so the old value is always
729 * different from 'no').
730 */
731
732 /* Replace all '_' and append ".h" */
733 s = sym->name;
734 d = path;
735 while ((c = *s++)) {
736 c = tolower(c);
737 *d++ = (c == '_') ? '/' : c;
738 }
739 strcpy(d, ".h");
740
741 /* Assume directory path already exists. */
742 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
743 if (fd == -1) {
744 if (errno != ENOENT) {
745 res = 1;
746 break;
747 }
748 /*
749 * Create directory components,
750 * unless they exist already.
751 */
752 d = path;
753 while ((d = strchr(d, '/'))) {
754 *d = 0;
755 if (stat(path, &sb) && mkdir(path, 0755)) {
756 res = 1;
757 goto out;
758 }
759 *d++ = '/';
760 }
761 /* Try it again. */
762 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
763 if (fd == -1) {
764 res = 1;
765 break;
766 }
767 }
768 close(fd);
769 }
770out:
771 if (chdir("../.."))
772 return 1;
773
774 return res;
775}
776
Roman Zippelc955cca2006-06-08 22:12:39 -0700777int conf_write_autoconf(void)
778{
779 struct symbol *sym;
780 const char *str;
Markus Heidelberg12122f62009-05-18 01:36:54 +0200781 const char *name;
Michal Marekbc081dd2009-12-07 16:38:33 +0100782 FILE *out, *tristate, *out_h;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200783 int i;
Roman Zippelc955cca2006-06-08 22:12:39 -0700784
Roman Zippel2e3646e2006-06-08 22:12:42 -0700785 sym_clear_all_valid();
786
Roman Zippelc955cca2006-06-08 22:12:39 -0700787 file_write_dep("include/config/auto.conf.cmd");
788
Roman Zippel2e3646e2006-06-08 22:12:42 -0700789 if (conf_split_config())
790 return 1;
791
Roman Zippelc955cca2006-06-08 22:12:39 -0700792 out = fopen(".tmpconfig", "w");
793 if (!out)
794 return 1;
795
Michal Marekbc081dd2009-12-07 16:38:33 +0100796 tristate = fopen(".tmpconfig_tristate", "w");
797 if (!tristate) {
798 fclose(out);
799 return 1;
800 }
801
Roman Zippelc955cca2006-06-08 22:12:39 -0700802 out_h = fopen(".tmpconfig.h", "w");
803 if (!out_h) {
804 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +0100805 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -0700806 return 1;
807 }
808
Roman Zippelc955cca2006-06-08 22:12:39 -0700809 fprintf(out, "#\n"
810 "# Automatically generated make config: don't edit\n"
Arnaud Lacombe09548282010-08-18 01:57:13 -0400811 "# %s\n"
Roman Zippelc955cca2006-06-08 22:12:39 -0700812 "#\n",
Michal Marekc33724a2009-04-28 15:05:20 +0200813 rootmenu.prompt->text);
Michal Marekbc081dd2009-12-07 16:38:33 +0100814 fprintf(tristate, "#\n"
815 "# Automatically generated - do not edit\n"
816 "\n");
Roman Zippelc955cca2006-06-08 22:12:39 -0700817 fprintf(out_h, "/*\n"
818 " * Automatically generated C config: don't edit\n"
Arnaud Lacombe09548282010-08-18 01:57:13 -0400819 " * %s\n"
Arnaud Lacombe6e71fab2010-12-05 01:31:57 -0500820 " */\n",
Michal Marekc33724a2009-04-28 15:05:20 +0200821 rootmenu.prompt->text);
Roman Zippelc955cca2006-06-08 22:12:39 -0700822
Roman Zippelc955cca2006-06-08 22:12:39 -0700823 for_all_symbols(i, sym) {
824 sym_calc_value(sym);
825 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
826 continue;
Sam Ravnborg49192f22010-07-31 23:35:33 +0200827
828 /* write symbol to config file */
Arnaud Lacombe0dce6312010-12-05 01:33:16 -0500829 conf_write_symbol(sym, out, false);
Sam Ravnborg49192f22010-07-31 23:35:33 +0200830
831 /* update autoconf and tristate files */
Roman Zippelc955cca2006-06-08 22:12:39 -0700832 switch (sym->type) {
833 case S_BOOLEAN:
834 case S_TRISTATE:
835 switch (sym_get_tristate_value(sym)) {
836 case no:
837 break;
838 case mod:
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400839 fprintf(tristate, "%s%s=M\n",
840 CONFIG_, sym->name);
841 fprintf(out_h, "#define %s%s_MODULE 1\n",
842 CONFIG_, sym->name);
Roman Zippelc955cca2006-06-08 22:12:39 -0700843 break;
844 case yes:
Michal Marekbc081dd2009-12-07 16:38:33 +0100845 if (sym->type == S_TRISTATE)
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400846 fprintf(tristate,"%s%s=Y\n",
847 CONFIG_, sym->name);
848 fprintf(out_h, "#define %s%s 1\n",
849 CONFIG_, sym->name);
Roman Zippelc955cca2006-06-08 22:12:39 -0700850 break;
851 }
852 break;
853 case S_STRING:
Sam Ravnborg49192f22010-07-31 23:35:33 +0200854 conf_write_string(true, sym->name, sym_get_string_value(sym), out_h);
Roman Zippelc955cca2006-06-08 22:12:39 -0700855 break;
856 case S_HEX:
857 str = sym_get_string_value(sym);
858 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400859 fprintf(out_h, "#define %s%s 0x%s\n",
860 CONFIG_, sym->name, str);
Roman Zippelc955cca2006-06-08 22:12:39 -0700861 break;
862 }
863 case S_INT:
864 str = sym_get_string_value(sym);
Arnaud Lacombeffb59572010-08-14 23:57:43 -0400865 fprintf(out_h, "#define %s%s %s\n",
866 CONFIG_, sym->name, str);
Roman Zippelc955cca2006-06-08 22:12:39 -0700867 break;
868 default:
869 break;
870 }
871 }
872 fclose(out);
Michal Marekbc081dd2009-12-07 16:38:33 +0100873 fclose(tristate);
Roman Zippelc955cca2006-06-08 22:12:39 -0700874 fclose(out_h);
875
876 name = getenv("KCONFIG_AUTOHEADER");
877 if (!name)
Sam Ravnborg264a2682009-10-18 00:49:24 +0200878 name = "include/generated/autoconf.h";
Roman Zippelc955cca2006-06-08 22:12:39 -0700879 if (rename(".tmpconfig.h", name))
880 return 1;
Michal Marekbc081dd2009-12-07 16:38:33 +0100881 name = getenv("KCONFIG_TRISTATE");
882 if (!name)
883 name = "include/config/tristate.conf";
884 if (rename(".tmpconfig_tristate", name))
885 return 1;
Markus Heidelberg12122f62009-05-18 01:36:54 +0200886 name = conf_get_autoconfig_name();
Roman Zippelc955cca2006-06-08 22:12:39 -0700887 /*
888 * This must be the last step, kbuild has a dependency on auto.conf
889 * and this marks the successful completion of the previous steps.
890 */
891 if (rename(".tmpconfig", name))
892 return 1;
893
894 return 0;
895}
Karsten Wieseb3214292006-12-13 00:34:06 -0800896
Karsten Wiesebfc10002006-12-13 00:34:07 -0800897static int sym_change_count;
Karsten Wiese3b354c52006-12-13 00:34:08 -0800898static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -0800899
900void sym_set_change_count(int count)
901{
Karsten Wiese3b354c52006-12-13 00:34:08 -0800902 int _sym_change_count = sym_change_count;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800903 sym_change_count = count;
Karsten Wiese3b354c52006-12-13 00:34:08 -0800904 if (conf_changed_callback &&
905 (bool)_sym_change_count != (bool)count)
906 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -0800907}
908
909void sym_add_change_count(int count)
910{
Karsten Wiese3b354c52006-12-13 00:34:08 -0800911 sym_set_change_count(count + sym_change_count);
Karsten Wiesebfc10002006-12-13 00:34:07 -0800912}
913
Karsten Wieseb3214292006-12-13 00:34:06 -0800914bool conf_get_changed(void)
915{
916 return sym_change_count;
917}
Karsten Wiese3b354c52006-12-13 00:34:08 -0800918
919void conf_set_changed_callback(void (*fn)(void))
920{
921 conf_changed_callback = fn;
922}
Roman Zippeldc7862e2008-05-06 04:55:55 +0200923
Sam Ravnborga64b44e2010-08-12 09:11:52 +0200924static void randomize_choice_values(struct symbol *csym)
925{
926 struct property *prop;
927 struct symbol *sym;
928 struct expr *e;
929 int cnt, def;
930
931 /*
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -0500932 * If choice is mod then we may have more items selected
Sam Ravnborga64b44e2010-08-12 09:11:52 +0200933 * and if no then no-one.
934 * In both cases stop.
935 */
936 if (csym->curr.tri != yes)
937 return;
938
939 prop = sym_get_choice_prop(csym);
940
941 /* count entries in choice block */
942 cnt = 0;
943 expr_list_for_each_sym(prop->expr, e, sym)
944 cnt++;
945
946 /*
947 * find a random value and set it to yes,
948 * set the rest to no so we have only one set
949 */
950 def = (rand() % cnt);
951
952 cnt = 0;
953 expr_list_for_each_sym(prop->expr, e, sym) {
954 if (def == cnt++) {
955 sym->def[S_DEF_USER].tri = yes;
956 csym->def[S_DEF_USER].val = sym;
957 }
958 else {
959 sym->def[S_DEF_USER].tri = no;
960 }
961 }
962 csym->flags |= SYMBOL_DEF_USER;
963 /* clear VALID to get value calculated */
964 csym->flags &= ~(SYMBOL_VALID);
965}
966
967static void set_all_choice_values(struct symbol *csym)
968{
969 struct property *prop;
970 struct symbol *sym;
971 struct expr *e;
972
973 prop = sym_get_choice_prop(csym);
974
975 /*
976 * Set all non-assinged choice values to no
977 */
978 expr_list_for_each_sym(prop->expr, e, sym) {
979 if (!sym_has_value(sym))
980 sym->def[S_DEF_USER].tri = no;
981 }
982 csym->flags |= SYMBOL_DEF_USER;
983 /* clear VALID to get value calculated */
984 csym->flags &= ~(SYMBOL_VALID);
985}
Roman Zippeldc7862e2008-05-06 04:55:55 +0200986
987void conf_set_all_new_symbols(enum conf_def_mode mode)
988{
989 struct symbol *sym, *csym;
Sam Ravnborga64b44e2010-08-12 09:11:52 +0200990 int i, cnt;
Roman Zippeldc7862e2008-05-06 04:55:55 +0200991
992 for_all_symbols(i, sym) {
993 if (sym_has_value(sym))
994 continue;
995 switch (sym_get_type(sym)) {
996 case S_BOOLEAN:
997 case S_TRISTATE:
998 switch (mode) {
999 case def_yes:
1000 sym->def[S_DEF_USER].tri = yes;
1001 break;
1002 case def_mod:
1003 sym->def[S_DEF_USER].tri = mod;
1004 break;
1005 case def_no:
1006 sym->def[S_DEF_USER].tri = no;
1007 break;
1008 case def_random:
Peter Korsgaard1244b412010-07-22 14:24:57 +02001009 cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
1010 sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
Roman Zippeldc7862e2008-05-06 04:55:55 +02001011 break;
1012 default:
1013 continue;
1014 }
Sam Ravnborg184832c2009-03-15 11:05:12 +01001015 if (!(sym_is_choice(sym) && mode == def_random))
Roman Zippeldc7862e2008-05-06 04:55:55 +02001016 sym->flags |= SYMBOL_DEF_USER;
1017 break;
1018 default:
1019 break;
1020 }
1021
1022 }
1023
Al Viroce97e132008-10-26 05:12:34 +00001024 sym_clear_all_valid();
Roman Zippeldc7862e2008-05-06 04:55:55 +02001025
Sam Ravnborg184832c2009-03-15 11:05:12 +01001026 /*
1027 * We have different type of choice blocks.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001028 * If curr.tri equals to mod then we can select several
Sam Ravnborg184832c2009-03-15 11:05:12 +01001029 * choice symbols in one block.
1030 * In this case we do nothing.
Arnaud Lacombe579fb8e2010-12-05 01:41:16 -05001031 * If curr.tri equals yes then only one symbol can be
Sam Ravnborg184832c2009-03-15 11:05:12 +01001032 * selected in a choice block and we set it to yes,
1033 * and the rest to no.
1034 */
Roman Zippeldc7862e2008-05-06 04:55:55 +02001035 for_all_symbols(i, csym) {
1036 if (sym_has_value(csym) || !sym_is_choice(csym))
1037 continue;
1038
1039 sym_calc_value(csym);
Sam Ravnborga64b44e2010-08-12 09:11:52 +02001040 if (mode == def_random)
1041 randomize_choice_values(csym);
1042 else
1043 set_all_choice_values(csym);
Roman Zippeldc7862e2008-05-06 04:55:55 +02001044 }
1045}