blob: 273d73888f9d4553f3574d9a8474df66533085bc [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>
Roman Zippel2e3646e2006-06-08 22:12:42 -07008#include <fcntl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <time.h>
13#include <unistd.h>
14
15#define LKC_DIRECT_LINK
16#include "lkc.h"
17
Roman Zippelc1a0f5e2005-11-08 21:34:54 -080018static void conf_warning(const char *fmt, ...)
19 __attribute__ ((format (printf, 1, 2)));
20
21static const char *conf_filename;
22static int conf_lineno, conf_warnings, conf_unsaved;
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024const char conf_defname[] = "arch/$ARCH/defconfig";
25
Roman Zippelc1a0f5e2005-11-08 21:34:54 -080026static void conf_warning(const char *fmt, ...)
27{
28 va_list ap;
29 va_start(ap, fmt);
30 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
31 vfprintf(stderr, fmt, ap);
32 fprintf(stderr, "\n");
33 va_end(ap);
34 conf_warnings++;
35}
36
Roman Zippel14cdd3c2006-06-08 22:12:51 -070037const char *conf_get_configname(void)
38{
39 char *name = getenv("KCONFIG_CONFIG");
40
41 return name ? name : ".config";
42}
43
J.A. Magallon48b9d032005-06-25 14:59:22 -070044static char *conf_expand_value(const char *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct symbol *sym;
J.A. Magallon48b9d032005-06-25 14:59:22 -070047 const char *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 static char res_value[SYMBOL_MAXLENGTH];
49 char *dst, name[SYMBOL_MAXLENGTH];
50
51 res_value[0] = 0;
52 dst = name;
53 while ((src = strchr(in, '$'))) {
54 strncat(res_value, in, src - in);
55 src++;
56 dst = name;
57 while (isalnum(*src) || *src == '_')
58 *dst++ = *src++;
59 *dst = 0;
60 sym = sym_lookup(name, 0);
61 sym_calc_value(sym);
62 strcat(res_value, sym_get_string_value(sym));
63 in = src;
64 }
65 strcat(res_value, in);
66
67 return res_value;
68}
69
70char *conf_get_default_confname(void)
71{
72 struct stat buf;
73 static char fullname[PATH_MAX+1];
74 char *env, *name;
75
76 name = conf_expand_value(conf_defname);
77 env = getenv(SRCTREE);
78 if (env) {
79 sprintf(fullname, "%s/%s", env, name);
80 if (!stat(fullname, &buf))
81 return fullname;
82 }
83 return name;
84}
85
Sam Ravnborg9c900a92007-11-10 20:01:56 +010086static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
87{
88 char *p2;
89
90 switch (sym->type) {
91 case S_TRISTATE:
92 if (p[0] == 'm') {
93 sym->def[def].tri = mod;
94 sym->flags |= def_flags;
95 break;
96 }
97 case S_BOOLEAN:
98 if (p[0] == 'y') {
99 sym->def[def].tri = yes;
100 sym->flags |= def_flags;
101 break;
102 }
103 if (p[0] == 'n') {
104 sym->def[def].tri = no;
105 sym->flags |= def_flags;
106 break;
107 }
108 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
109 break;
110 case S_OTHER:
111 if (*p != '"') {
112 for (p2 = p; *p2 && !isspace(*p2); p2++)
113 ;
114 sym->type = S_STRING;
115 goto done;
116 }
117 case S_STRING:
118 if (*p++ != '"')
119 break;
120 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
121 if (*p2 == '"') {
122 *p2 = 0;
123 break;
124 }
125 memmove(p2, p2 + 1, strlen(p2));
126 }
127 if (!p2) {
128 conf_warning("invalid string found");
129 return 1;
130 }
131 case S_INT:
132 case S_HEX:
133 done:
134 if (sym_string_valid(sym, p)) {
135 sym->def[def].val = strdup(p);
136 sym->flags |= def_flags;
137 } else {
138 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
139 return 1;
140 }
141 break;
142 default:
143 ;
144 }
145 return 0;
146}
147
Roman Zippel669bfad2006-06-08 22:12:42 -0700148int conf_read_simple(const char *name, int def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 FILE *in = NULL;
151 char line[1024];
152 char *p, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct symbol *sym;
Roman Zippel669bfad2006-06-08 22:12:42 -0700154 int i, def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (name) {
157 in = zconf_fopen(name);
158 } else {
Roman Zippelface4372006-06-08 22:12:45 -0700159 struct property *prop;
160
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700161 name = conf_get_configname();
Roman Zippelddc97ca2006-06-08 22:12:38 -0700162 in = zconf_fopen(name);
163 if (in)
164 goto load;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800165 sym_add_change_count(1);
Roman Zippelface4372006-06-08 22:12:45 -0700166 if (!sym_defconfig_list)
167 return 1;
168
169 for_all_defaults(sym_defconfig_list, prop) {
170 if (expr_calc_value(prop->visible.expr) == no ||
171 prop->expr->type != E_SYMBOL)
172 continue;
173 name = conf_expand_value(prop->expr->left.sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 in = zconf_fopen(name);
175 if (in) {
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700176 printf(_("#\n"
Roman Zippelddc97ca2006-06-08 22:12:38 -0700177 "# using defaults found in %s\n"
178 "#\n"), name);
179 goto load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181 }
182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (!in)
184 return 1;
185
Roman Zippelddc97ca2006-06-08 22:12:38 -0700186load:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800187 conf_filename = name;
188 conf_lineno = 0;
189 conf_warnings = 0;
190 conf_unsaved = 0;
191
Roman Zippel669bfad2006-06-08 22:12:42 -0700192 def_flags = SYMBOL_DEF << def;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 for_all_symbols(i, sym) {
Roman Zippel669bfad2006-06-08 22:12:42 -0700194 sym->flags |= SYMBOL_CHANGED;
195 sym->flags &= ~(def_flags|SYMBOL_VALID);
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800196 if (sym_is_choice(sym))
Roman Zippel669bfad2006-06-08 22:12:42 -0700197 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 switch (sym->type) {
199 case S_INT:
200 case S_HEX:
201 case S_STRING:
Roman Zippel669bfad2006-06-08 22:12:42 -0700202 if (sym->def[def].val)
203 free(sym->def[def].val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 default:
Roman Zippel669bfad2006-06-08 22:12:42 -0700205 sym->def[def].val = NULL;
206 sym->def[def].tri = no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208 }
209
210 while (fgets(line, sizeof(line), in)) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800211 conf_lineno++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 sym = NULL;
213 switch (line[0]) {
214 case '#':
215 if (memcmp(line + 2, "CONFIG_", 7))
216 continue;
217 p = strchr(line + 9, ' ');
218 if (!p)
219 continue;
220 *p++ = 0;
221 if (strncmp(p, "is not set", 10))
222 continue;
Roman Zippel669bfad2006-06-08 22:12:42 -0700223 if (def == S_DEF_USER) {
224 sym = sym_find(line + 9);
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200225 if (!sym) {
226 sym_add_change_count(1);
Roman Zippel669bfad2006-06-08 22:12:42 -0700227 break;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200228 }
Roman Zippel669bfad2006-06-08 22:12:42 -0700229 } else {
230 sym = sym_lookup(line + 9, 0);
231 if (sym->type == S_UNKNOWN)
232 sym->type = S_BOOLEAN;
233 }
234 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100235 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237 switch (sym->type) {
238 case S_BOOLEAN:
239 case S_TRISTATE:
Roman Zippel669bfad2006-06-08 22:12:42 -0700240 sym->def[def].tri = no;
241 sym->flags |= def_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 break;
243 default:
244 ;
245 }
246 break;
247 case 'C':
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800248 if (memcmp(line, "CONFIG_", 7)) {
249 conf_warning("unexpected data");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 continue;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 p = strchr(line + 7, '=');
253 if (!p)
254 continue;
255 *p++ = 0;
256 p2 = strchr(p, '\n');
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600257 if (p2) {
258 *p2-- = 0;
259 if (*p2 == '\r')
260 *p2 = 0;
261 }
Roman Zippel669bfad2006-06-08 22:12:42 -0700262 if (def == S_DEF_USER) {
263 sym = sym_find(line + 7);
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200264 if (!sym) {
265 sym_add_change_count(1);
Roman Zippel669bfad2006-06-08 22:12:42 -0700266 break;
zippel@linux-m68k.org661b0682008-09-29 05:27:11 +0200267 }
Roman Zippel669bfad2006-06-08 22:12:42 -0700268 } else {
269 sym = sym_lookup(line + 7, 0);
270 if (sym->type == S_UNKNOWN)
271 sym->type = S_OTHER;
272 }
273 if (sym->flags & def_flags) {
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100274 conf_warning("override: reassigning to symbol %s", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
Sam Ravnborg9c900a92007-11-10 20:01:56 +0100276 if (conf_set_sym_val(sym, def, def_flags, p))
277 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
Matthew Wilcoxd3660a82006-07-13 12:54:07 -0600279 case '\r':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 case '\n':
281 break;
282 default:
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800283 conf_warning("unexpected data");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 continue;
285 }
286 if (sym && sym_is_choice_value(sym)) {
287 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
Roman Zippel669bfad2006-06-08 22:12:42 -0700288 switch (sym->def[def].tri) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 case no:
290 break;
291 case mod:
Roman Zippel669bfad2006-06-08 22:12:42 -0700292 if (cs->def[def].tri == yes) {
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800293 conf_warning("%s creates inconsistent choice state", sym->name);
Roman Zippel669bfad2006-06-08 22:12:42 -0700294 cs->flags &= ~def_flags;
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 case yes:
Jan Engelhardtd84876f2008-01-03 23:33:44 +0100298 if (cs->def[def].tri != no)
299 conf_warning("override: %s changes choice state", sym->name);
300 cs->def[def].val = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
302 }
Sam Ravnborgd6ee3572008-01-07 21:09:55 +0100303 cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305 }
306 fclose(in);
307
308 if (modules_sym)
309 sym_calc_value(modules_sym);
Roman Zippel90389162005-11-08 21:34:49 -0800310 return 0;
311}
312
313int conf_read(const char *name)
314{
Roman Zippel7a962922008-01-14 04:50:23 +0100315 struct symbol *sym, *choice_sym;
Roman Zippel90389162005-11-08 21:34:49 -0800316 struct property *prop;
317 struct expr *e;
Roman Zippel669bfad2006-06-08 22:12:42 -0700318 int i, flags;
Roman Zippel90389162005-11-08 21:34:49 -0800319
Karsten Wiesebfc10002006-12-13 00:34:07 -0800320 sym_set_change_count(0);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700321
Roman Zippel669bfad2006-06-08 22:12:42 -0700322 if (conf_read_simple(name, S_DEF_USER))
Roman Zippel90389162005-11-08 21:34:49 -0800323 return 1;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 for_all_symbols(i, sym) {
326 sym_calc_value(sym);
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800327 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
328 goto sym_ok;
329 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
330 /* check that calculated value agrees with saved value */
331 switch (sym->type) {
332 case S_BOOLEAN:
333 case S_TRISTATE:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700334 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800335 break;
336 if (!sym_is_choice(sym))
337 goto sym_ok;
338 default:
Roman Zippel0c1822e2006-06-08 22:12:41 -0700339 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
Roman Zippelc1a0f5e2005-11-08 21:34:54 -0800340 goto sym_ok;
341 break;
342 }
343 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
344 /* no previous value and not saved */
345 goto sym_ok;
346 conf_unsaved++;
347 /* maybe print value in verbose mode... */
348 sym_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (!sym_is_choice(sym))
350 continue;
Roman Zippeld8982ba2007-07-09 11:43:58 -0700351 /* The choice symbol only has a set value (and thus is not new)
352 * if all its visible childs have values.
353 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 prop = sym_get_choice_prop(sym);
Roman Zippel669bfad2006-06-08 22:12:42 -0700355 flags = sym->flags;
Roman Zippel7a962922008-01-14 04:50:23 +0100356 expr_list_for_each_sym(prop->expr, e, choice_sym)
357 if (choice_sym->visible != no)
358 flags &= choice_sym->flags;
Roman Zippel002d27b2006-07-13 13:22:38 +0200359 sym->flags &= flags | ~SYMBOL_DEF_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
Roman Zippeld8982ba2007-07-09 11:43:58 -0700362 for_all_symbols(i, sym) {
363 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
364 /* Reset values of generates values, so they'll appear
365 * as new, if they should become visible, but that
366 * doesn't quite work if the Kconfig and the saved
367 * configuration disagree.
368 */
369 if (sym->visible == no && !conf_unsaved)
370 sym->flags &= ~SYMBOL_DEF_USER;
371 switch (sym->type) {
372 case S_STRING:
373 case S_INT:
374 case S_HEX:
375 /* Reset a string value if it's out of range */
376 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
377 break;
378 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
379 conf_unsaved++;
380 break;
381 default:
382 break;
383 }
384 }
385 }
386
Karsten Wiesebfc10002006-12-13 00:34:07 -0800387 sym_add_change_count(conf_warnings || conf_unsaved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 return 0;
390}
391
392int conf_write(const char *name)
393{
Roman Zippelc955cca2006-06-08 22:12:39 -0700394 FILE *out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct symbol *sym;
396 struct menu *menu;
397 const char *basename;
398 char dirname[128], tmpname[128], newname[128];
399 int type, l;
400 const char *str;
401 time_t now;
402 int use_timestamp = 1;
403 char *env;
404
405 dirname[0] = 0;
406 if (name && name[0]) {
407 struct stat st;
408 char *slash;
409
410 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
411 strcpy(dirname, name);
412 strcat(dirname, "/");
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700413 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 } else if ((slash = strrchr(name, '/'))) {
415 int size = slash - name + 1;
416 memcpy(dirname, name, size);
417 dirname[size] = 0;
418 if (slash[1])
419 basename = slash + 1;
420 else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700421 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 } else
423 basename = name;
424 } else
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700425 basename = conf_get_configname();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700427 sprintf(newname, "%s%s", dirname, basename);
428 env = getenv("KCONFIG_OVERWRITECONFIG");
429 if (!env || !*env) {
430 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
431 out = fopen(tmpname, "w");
432 } else {
433 *tmpname = 0;
434 out = fopen(newname, "w");
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!out)
437 return 1;
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700438
Sam Ravnborg2244cbd2006-01-16 12:12:12 +0100439 sym = sym_lookup("KERNELVERSION", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 sym_calc_value(sym);
441 time(&now);
442 env = getenv("KCONFIG_NOTIMESTAMP");
443 if (env && *env)
444 use_timestamp = 0;
445
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700446 fprintf(out, _("#\n"
447 "# Automatically generated make config: don't edit\n"
448 "# Linux kernel version: %s\n"
449 "%s%s"
450 "#\n"),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 sym_get_string_value(sym),
452 use_timestamp ? "# " : "",
453 use_timestamp ? ctime(&now) : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Karsten Wieseb3214292006-12-13 00:34:06 -0800455 if (!conf_get_changed())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 sym_clear_all_valid();
457
458 menu = rootmenu.list;
459 while (menu) {
460 sym = menu->sym;
461 if (!sym) {
462 if (!menu_is_visible(menu))
463 goto next;
464 str = menu_get_prompt(menu);
465 fprintf(out, "\n"
466 "#\n"
467 "# %s\n"
468 "#\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 } else if (!(sym->flags & SYMBOL_CHOICE)) {
470 sym_calc_value(sym);
471 if (!(sym->flags & SYMBOL_WRITE))
472 goto next;
473 sym->flags &= ~SYMBOL_WRITE;
474 type = sym->type;
475 if (type == S_TRISTATE) {
476 sym_calc_value(modules_sym);
477 if (modules_sym->curr.tri == no)
478 type = S_BOOLEAN;
479 }
480 switch (type) {
481 case S_BOOLEAN:
482 case S_TRISTATE:
483 switch (sym_get_tristate_value(sym)) {
484 case no:
485 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 case mod:
488 fprintf(out, "CONFIG_%s=m\n", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 break;
490 case yes:
491 fprintf(out, "CONFIG_%s=y\n", sym->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
493 }
494 break;
495 case S_STRING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 str = sym_get_string_value(sym);
497 fprintf(out, "CONFIG_%s=\"", sym->name);
Roman Zippelc955cca2006-06-08 22:12:39 -0700498 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 l = strcspn(str, "\"\\");
500 if (l) {
501 fwrite(str, l, 1, out);
Roman Zippelc955cca2006-06-08 22:12:39 -0700502 str += l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Roman Zippelc955cca2006-06-08 22:12:39 -0700504 if (!*str)
505 break;
506 fprintf(out, "\\%c", *str++);
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 fputs("\"\n", out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
510 case S_HEX:
511 str = sym_get_string_value(sym);
512 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
513 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
515 }
516 case S_INT:
517 str = sym_get_string_value(sym);
518 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 break;
520 }
521 }
522
523 next:
524 if (menu->list) {
525 menu = menu->list;
526 continue;
527 }
528 if (menu->next)
529 menu = menu->next;
530 else while ((menu = menu->parent)) {
531 if (menu->next) {
532 menu = menu->next;
533 break;
534 }
535 }
536 }
537 fclose(out);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700538
539 if (*tmpname) {
Sam Ravnborg9a3d0fe2006-10-01 11:48:53 +0200540 strcat(dirname, basename);
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700541 strcat(dirname, ".old");
542 rename(newname, dirname);
543 if (rename(tmpname, newname))
544 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Roman Zippelddc97ca2006-06-08 22:12:38 -0700547 printf(_("#\n"
548 "# configuration written to %s\n"
Roman Zippel14cdd3c2006-06-08 22:12:51 -0700549 "#\n"), newname);
Roman Zippelddc97ca2006-06-08 22:12:38 -0700550
Karsten Wiesebfc10002006-12-13 00:34:07 -0800551 sym_set_change_count(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 return 0;
554}
Roman Zippelc955cca2006-06-08 22:12:39 -0700555
Roman Zippel2e3646e2006-06-08 22:12:42 -0700556int conf_split_config(void)
557{
558 char *name, path[128];
559 char *s, *d, c;
560 struct symbol *sym;
561 struct stat sb;
562 int res, i, fd;
563
564 name = getenv("KCONFIG_AUTOCONFIG");
565 if (!name)
566 name = "include/config/auto.conf";
567 conf_read_simple(name, S_DEF_AUTO);
568
569 if (chdir("include/config"))
570 return 1;
571
572 res = 0;
573 for_all_symbols(i, sym) {
574 sym_calc_value(sym);
575 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
576 continue;
577 if (sym->flags & SYMBOL_WRITE) {
578 if (sym->flags & SYMBOL_DEF_AUTO) {
579 /*
580 * symbol has old and new value,
581 * so compare them...
582 */
583 switch (sym->type) {
584 case S_BOOLEAN:
585 case S_TRISTATE:
586 if (sym_get_tristate_value(sym) ==
587 sym->def[S_DEF_AUTO].tri)
588 continue;
589 break;
590 case S_STRING:
591 case S_HEX:
592 case S_INT:
593 if (!strcmp(sym_get_string_value(sym),
594 sym->def[S_DEF_AUTO].val))
595 continue;
596 break;
597 default:
598 break;
599 }
600 } else {
601 /*
602 * If there is no old value, only 'no' (unset)
603 * is allowed as new value.
604 */
605 switch (sym->type) {
606 case S_BOOLEAN:
607 case S_TRISTATE:
608 if (sym_get_tristate_value(sym) == no)
609 continue;
610 break;
611 default:
612 break;
613 }
614 }
615 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
616 /* There is neither an old nor a new value. */
617 continue;
618 /* else
619 * There is an old value, but no new value ('no' (unset)
620 * isn't saved in auto.conf, so the old value is always
621 * different from 'no').
622 */
623
624 /* Replace all '_' and append ".h" */
625 s = sym->name;
626 d = path;
627 while ((c = *s++)) {
628 c = tolower(c);
629 *d++ = (c == '_') ? '/' : c;
630 }
631 strcpy(d, ".h");
632
633 /* Assume directory path already exists. */
634 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
635 if (fd == -1) {
636 if (errno != ENOENT) {
637 res = 1;
638 break;
639 }
640 /*
641 * Create directory components,
642 * unless they exist already.
643 */
644 d = path;
645 while ((d = strchr(d, '/'))) {
646 *d = 0;
647 if (stat(path, &sb) && mkdir(path, 0755)) {
648 res = 1;
649 goto out;
650 }
651 *d++ = '/';
652 }
653 /* Try it again. */
654 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
655 if (fd == -1) {
656 res = 1;
657 break;
658 }
659 }
660 close(fd);
661 }
662out:
663 if (chdir("../.."))
664 return 1;
665
666 return res;
667}
668
Roman Zippelc955cca2006-06-08 22:12:39 -0700669int conf_write_autoconf(void)
670{
671 struct symbol *sym;
672 const char *str;
673 char *name;
674 FILE *out, *out_h;
675 time_t now;
676 int i, l;
677
Roman Zippel2e3646e2006-06-08 22:12:42 -0700678 sym_clear_all_valid();
679
Roman Zippelc955cca2006-06-08 22:12:39 -0700680 file_write_dep("include/config/auto.conf.cmd");
681
Roman Zippel2e3646e2006-06-08 22:12:42 -0700682 if (conf_split_config())
683 return 1;
684
Roman Zippelc955cca2006-06-08 22:12:39 -0700685 out = fopen(".tmpconfig", "w");
686 if (!out)
687 return 1;
688
689 out_h = fopen(".tmpconfig.h", "w");
690 if (!out_h) {
691 fclose(out);
692 return 1;
693 }
694
695 sym = sym_lookup("KERNELVERSION", 0);
696 sym_calc_value(sym);
697 time(&now);
698 fprintf(out, "#\n"
699 "# Automatically generated make config: don't edit\n"
700 "# Linux kernel version: %s\n"
701 "# %s"
702 "#\n",
703 sym_get_string_value(sym), ctime(&now));
704 fprintf(out_h, "/*\n"
705 " * Automatically generated C config: don't edit\n"
706 " * Linux kernel version: %s\n"
707 " * %s"
708 " */\n"
709 "#define AUTOCONF_INCLUDED\n",
710 sym_get_string_value(sym), ctime(&now));
711
Roman Zippelc955cca2006-06-08 22:12:39 -0700712 for_all_symbols(i, sym) {
713 sym_calc_value(sym);
714 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
715 continue;
716 switch (sym->type) {
717 case S_BOOLEAN:
718 case S_TRISTATE:
719 switch (sym_get_tristate_value(sym)) {
720 case no:
721 break;
722 case mod:
723 fprintf(out, "CONFIG_%s=m\n", sym->name);
724 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
725 break;
726 case yes:
727 fprintf(out, "CONFIG_%s=y\n", sym->name);
728 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
729 break;
730 }
731 break;
732 case S_STRING:
733 str = sym_get_string_value(sym);
734 fprintf(out, "CONFIG_%s=\"", sym->name);
735 fprintf(out_h, "#define CONFIG_%s \"", sym->name);
736 while (1) {
737 l = strcspn(str, "\"\\");
738 if (l) {
739 fwrite(str, l, 1, out);
740 fwrite(str, l, 1, out_h);
741 str += l;
742 }
743 if (!*str)
744 break;
745 fprintf(out, "\\%c", *str);
746 fprintf(out_h, "\\%c", *str);
747 str++;
748 }
749 fputs("\"\n", out);
750 fputs("\"\n", out_h);
751 break;
752 case S_HEX:
753 str = sym_get_string_value(sym);
754 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
755 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
756 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
757 break;
758 }
759 case S_INT:
760 str = sym_get_string_value(sym);
761 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
762 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
763 break;
764 default:
765 break;
766 }
767 }
768 fclose(out);
769 fclose(out_h);
770
771 name = getenv("KCONFIG_AUTOHEADER");
772 if (!name)
773 name = "include/linux/autoconf.h";
774 if (rename(".tmpconfig.h", name))
775 return 1;
776 name = getenv("KCONFIG_AUTOCONFIG");
777 if (!name)
778 name = "include/config/auto.conf";
779 /*
780 * This must be the last step, kbuild has a dependency on auto.conf
781 * and this marks the successful completion of the previous steps.
782 */
783 if (rename(".tmpconfig", name))
784 return 1;
785
786 return 0;
787}
Karsten Wieseb3214292006-12-13 00:34:06 -0800788
Karsten Wiesebfc10002006-12-13 00:34:07 -0800789static int sym_change_count;
Karsten Wiese3b354c52006-12-13 00:34:08 -0800790static void (*conf_changed_callback)(void);
Karsten Wiesebfc10002006-12-13 00:34:07 -0800791
792void sym_set_change_count(int count)
793{
Karsten Wiese3b354c52006-12-13 00:34:08 -0800794 int _sym_change_count = sym_change_count;
Karsten Wiesebfc10002006-12-13 00:34:07 -0800795 sym_change_count = count;
Karsten Wiese3b354c52006-12-13 00:34:08 -0800796 if (conf_changed_callback &&
797 (bool)_sym_change_count != (bool)count)
798 conf_changed_callback();
Karsten Wiesebfc10002006-12-13 00:34:07 -0800799}
800
801void sym_add_change_count(int count)
802{
Karsten Wiese3b354c52006-12-13 00:34:08 -0800803 sym_set_change_count(count + sym_change_count);
Karsten Wiesebfc10002006-12-13 00:34:07 -0800804}
805
Karsten Wieseb3214292006-12-13 00:34:06 -0800806bool conf_get_changed(void)
807{
808 return sym_change_count;
809}
Karsten Wiese3b354c52006-12-13 00:34:08 -0800810
811void conf_set_changed_callback(void (*fn)(void))
812{
813 conf_changed_callback = fn;
814}
Roman Zippeldc7862e2008-05-06 04:55:55 +0200815
816
817void conf_set_all_new_symbols(enum conf_def_mode mode)
818{
819 struct symbol *sym, *csym;
820 struct property *prop;
821 struct expr *e;
822 int i, cnt, def;
823
824 for_all_symbols(i, sym) {
825 if (sym_has_value(sym))
826 continue;
827 switch (sym_get_type(sym)) {
828 case S_BOOLEAN:
829 case S_TRISTATE:
830 switch (mode) {
831 case def_yes:
832 sym->def[S_DEF_USER].tri = yes;
833 break;
834 case def_mod:
835 sym->def[S_DEF_USER].tri = mod;
836 break;
837 case def_no:
838 sym->def[S_DEF_USER].tri = no;
839 break;
840 case def_random:
841 sym->def[S_DEF_USER].tri = (tristate)(rand() % 3);
842 break;
843 default:
844 continue;
845 }
Sam Ravnborg184832c2009-03-15 11:05:12 +0100846 if (!(sym_is_choice(sym) && mode == def_random))
Roman Zippeldc7862e2008-05-06 04:55:55 +0200847 sym->flags |= SYMBOL_DEF_USER;
848 break;
849 default:
850 break;
851 }
852
853 }
854
Al Viroce97e132008-10-26 05:12:34 +0000855 sym_clear_all_valid();
Roman Zippeldc7862e2008-05-06 04:55:55 +0200856
857 if (mode != def_random)
858 return;
Sam Ravnborg184832c2009-03-15 11:05:12 +0100859 /*
860 * We have different type of choice blocks.
861 * If curr.tri equal to mod then we can select several
862 * choice symbols in one block.
863 * In this case we do nothing.
864 * If curr.tri equal yes then only one symbol can be
865 * selected in a choice block and we set it to yes,
866 * and the rest to no.
867 */
Roman Zippeldc7862e2008-05-06 04:55:55 +0200868 for_all_symbols(i, csym) {
869 if (sym_has_value(csym) || !sym_is_choice(csym))
870 continue;
871
872 sym_calc_value(csym);
Sam Ravnborg184832c2009-03-15 11:05:12 +0100873
874 if (csym->curr.tri != yes)
875 continue;
876
Roman Zippeldc7862e2008-05-06 04:55:55 +0200877 prop = sym_get_choice_prop(csym);
Sam Ravnborg184832c2009-03-15 11:05:12 +0100878
879 /* count entries in choice block */
880 cnt = 0;
881 expr_list_for_each_sym(prop->expr, e, sym)
882 cnt++;
883
884 /*
885 * find a random value and set it to yes,
886 * set the rest to no so we have only one set
887 */
888 def = (rand() % cnt);
889
890 cnt = 0;
891 expr_list_for_each_sym(prop->expr, e, sym) {
892 if (def == cnt++) {
893 sym->def[S_DEF_USER].tri = yes;
894 csym->def[S_DEF_USER].val = sym;
Roman Zippeldc7862e2008-05-06 04:55:55 +0200895 }
Sam Ravnborg184832c2009-03-15 11:05:12 +0100896 else {
897 sym->def[S_DEF_USER].tri = no;
898 }
Roman Zippeldc7862e2008-05-06 04:55:55 +0200899 }
900 csym->flags |= SYMBOL_DEF_USER;
Sam Ravnborg184832c2009-03-15 11:05:12 +0100901 /* clear VALID to get value calculated */
902 csym->flags &= ~(SYMBOL_VALID);
Roman Zippeldc7862e2008-05-06 04:55:55 +0200903 }
904}