blob: 79bb4a38e4d24bc255c1921ea0a71b384015e19e [file] [log] [blame]
Denis Vlasenko7d219aa2006-10-05 10:17:08 +00001/*
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>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <time.h>
12#include <unistd.h>
13
14#define LKC_DIRECT_LINK
15#include "lkc.h"
16
17static void conf_warning(const char *fmt, ...)
18 __attribute__ ((format (printf, 1, 2)));
19
20static const char *conf_filename;
21static int conf_lineno, conf_warnings, conf_unsaved;
22
23const char conf_def_filename[] = ".config";
24
Denis Vlasenkoa3310522006-10-05 18:26:35 +000025const char conf_defname[] = "scripts/defconfig";
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000026
27const char *conf_confnames[] = {
Denis Vlasenkod67a6062007-06-21 12:57:29 +000028 conf_def_filename,
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000029 conf_defname,
30 NULL,
31};
32
33static void conf_warning(const char *fmt, ...)
34{
35 va_list ap;
36 va_start(ap, fmt);
37 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
38 vfprintf(stderr, fmt, ap);
39 fprintf(stderr, "\n");
40 va_end(ap);
41 conf_warnings++;
42}
43
44static char *conf_expand_value(const char *in)
45{
46 struct symbol *sym;
47 const char *src;
48 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
86int conf_read_simple(const char *name)
87{
88 FILE *in = NULL;
89 char line[1024];
90 char *p, *p2;
91 struct symbol *sym;
92 int i;
93
94 if (name) {
95 in = zconf_fopen(name);
96 } else {
97 const char **names = conf_confnames;
98 while ((name = *names++)) {
99 name = conf_expand_value(name);
100 in = zconf_fopen(name);
101 if (in) {
102 printf(_("#\n"
103 "# using defaults found in %s\n"
104 "#\n"), name);
105 break;
106 }
107 }
108 }
109 if (!in)
110 return 1;
111
112 conf_filename = name;
113 conf_lineno = 0;
114 conf_warnings = 0;
115 conf_unsaved = 0;
116
117 for_all_symbols(i, sym) {
118 sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
119 if (sym_is_choice(sym))
120 sym->flags &= ~SYMBOL_NEW;
121 sym->flags &= ~SYMBOL_VALID;
122 switch (sym->type) {
123 case S_INT:
124 case S_HEX:
125 case S_STRING:
126 if (sym->user.val)
127 free(sym->user.val);
128 default:
129 sym->user.val = NULL;
130 sym->user.tri = no;
131 }
132 }
133
134 while (fgets(line, sizeof(line), in)) {
135 conf_lineno++;
136 sym = NULL;
137 switch (line[0]) {
138 case '#':
139 if (memcmp(line + 2, "CONFIG_", 7))
140 continue;
141 p = strchr(line + 9, ' ');
142 if (!p)
143 continue;
144 *p++ = 0;
145 if (strncmp(p, "is not set", 10))
146 continue;
147 sym = sym_find(line + 9);
148 if (!sym) {
149 conf_warning("trying to assign nonexistent symbol %s", line + 9);
150 break;
151 } else if (!(sym->flags & SYMBOL_NEW)) {
152 conf_warning("trying to reassign symbol %s", sym->name);
153 break;
154 }
155 switch (sym->type) {
156 case S_BOOLEAN:
157 case S_TRISTATE:
158 sym->user.tri = no;
159 sym->flags &= ~SYMBOL_NEW;
160 break;
161 default:
162 ;
163 }
164 break;
165 case 'C':
166 if (memcmp(line, "CONFIG_", 7)) {
167 conf_warning("unexpected data");
168 continue;
169 }
170 p = strchr(line + 7, '=');
171 if (!p)
172 continue;
173 *p++ = 0;
174 p2 = strchr(p, '\n');
175 if (p2)
176 *p2 = 0;
177 sym = sym_find(line + 7);
178 if (!sym) {
179 conf_warning("trying to assign nonexistent symbol %s", line + 7);
180 break;
181 } else if (!(sym->flags & SYMBOL_NEW)) {
182 conf_warning("trying to reassign symbol %s", sym->name);
183 break;
184 }
185 switch (sym->type) {
186 case S_TRISTATE:
187 if (p[0] == 'm') {
188 sym->user.tri = mod;
189 sym->flags &= ~SYMBOL_NEW;
190 break;
191 }
192 case S_BOOLEAN:
193 if (p[0] == 'y') {
194 sym->user.tri = yes;
195 sym->flags &= ~SYMBOL_NEW;
196 break;
197 }
198 if (p[0] == 'n') {
199 sym->user.tri = no;
200 sym->flags &= ~SYMBOL_NEW;
201 break;
202 }
203 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
204 break;
205 case S_STRING:
206 if (*p++ != '"')
207 break;
208 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
209 if (*p2 == '"') {
210 *p2 = 0;
211 break;
212 }
213 memmove(p2, p2 + 1, strlen(p2));
214 }
215 if (!p2) {
216 conf_warning("invalid string found");
217 continue;
218 }
219 case S_INT:
220 case S_HEX:
221 if (sym_string_valid(sym, p)) {
222 sym->user.val = strdup(p);
223 sym->flags &= ~SYMBOL_NEW;
224 } else {
225 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
226 continue;
227 }
228 break;
229 default:
230 ;
231 }
232 break;
233 case '\n':
234 break;
235 default:
236 conf_warning("unexpected data");
237 continue;
238 }
239 if (sym && sym_is_choice_value(sym)) {
240 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
241 switch (sym->user.tri) {
242 case no:
243 break;
244 case mod:
245 if (cs->user.tri == yes) {
246 conf_warning("%s creates inconsistent choice state", sym->name);
247 cs->flags |= SYMBOL_NEW;
248 }
249 break;
250 case yes:
251 if (cs->user.tri != no) {
252 conf_warning("%s creates inconsistent choice state", sym->name);
253 cs->flags |= SYMBOL_NEW;
254 } else
255 cs->user.val = sym;
256 break;
257 }
258 cs->user.tri = E_OR(cs->user.tri, sym->user.tri);
259 }
260 }
261 fclose(in);
262
263 if (modules_sym)
264 sym_calc_value(modules_sym);
265 return 0;
266}
267
268int conf_read(const char *name)
269{
270 struct symbol *sym;
271 struct property *prop;
272 struct expr *e;
273 int i;
274
275 if (conf_read_simple(name))
276 return 1;
277
278 for_all_symbols(i, sym) {
279 sym_calc_value(sym);
280 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
281 goto sym_ok;
282 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
283 /* check that calculated value agrees with saved value */
284 switch (sym->type) {
285 case S_BOOLEAN:
286 case S_TRISTATE:
287 if (sym->user.tri != sym_get_tristate_value(sym))
288 break;
289 if (!sym_is_choice(sym))
290 goto sym_ok;
291 default:
292 if (!strcmp(sym->curr.val, sym->user.val))
293 goto sym_ok;
294 break;
295 }
296 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
297 /* no previous value and not saved */
298 goto sym_ok;
299 conf_unsaved++;
300 /* maybe print value in verbose mode... */
301 sym_ok:
302 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
303 if (sym->visible == no)
304 sym->flags |= SYMBOL_NEW;
305 switch (sym->type) {
306 case S_STRING:
307 case S_INT:
308 case S_HEX:
309 if (!sym_string_within_range(sym, sym->user.val)) {
310 sym->flags |= SYMBOL_NEW;
311 sym->flags &= ~SYMBOL_VALID;
312 }
313 default:
314 break;
315 }
316 }
317 if (!sym_is_choice(sym))
318 continue;
319 prop = sym_get_choice_prop(sym);
320 for (e = prop->expr; e; e = e->left.expr)
321 if (e->right.sym->visible != no)
322 sym->flags |= e->right.sym->flags & SYMBOL_NEW;
323 }
324
325 sym_change_count = conf_warnings || conf_unsaved;
326
327 return 0;
328}
329
330int conf_write(const char *name)
331{
332 FILE *out, *out_h;
333 struct symbol *sym;
334 struct menu *menu;
335 const char *basename;
336 char dirname[128], tmpname[128], newname[128];
337 int type, l;
338 const char *str;
339 time_t now;
340 int use_timestamp = 1;
341 char *env;
342
343 dirname[0] = 0;
344 if (name && name[0]) {
345 struct stat st;
346 char *slash;
347
348 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
349 strcpy(dirname, name);
350 strcat(dirname, "/");
351 basename = conf_def_filename;
352 } else if ((slash = strrchr(name, '/'))) {
353 int size = slash - name + 1;
354 memcpy(dirname, name, size);
355 dirname[size] = 0;
356 if (slash[1])
357 basename = slash + 1;
358 else
359 basename = conf_def_filename;
360 } else
361 basename = name;
362 } else
363 basename = conf_def_filename;
364
365 sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
366 out = fopen(newname, "w");
367 if (!out)
368 return 1;
369 out_h = NULL;
370 if (!name) {
371 out_h = fopen(".tmpconfig.h", "w");
372 if (!out_h)
373 return 1;
374 file_write_dep(NULL);
375 }
376 sym = sym_lookup("KERNELVERSION", 0);
377 sym_calc_value(sym);
378 time(&now);
379 env = getenv("KCONFIG_NOTIMESTAMP");
380 if (env && *env)
381 use_timestamp = 0;
382
383 fprintf(out, _("#\n"
384 "# Automatically generated make config: don't edit\n"
Bernhard Reutner-Fischer9e8df932007-01-17 19:36:01 +0000385 "# Busybox version: %s\n"
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000386 "%s%s"
387 "#\n"),
388 sym_get_string_value(sym),
389 use_timestamp ? "# " : "",
390 use_timestamp ? ctime(&now) : "");
391 if (out_h) {
392 char buf[sizeof("#define AUTOCONF_TIMESTAMP "
393 "\"YYYY-MM-DD HH:MM:SS some_timezone\"\n")];
394 buf[0] = '\0';
Mike Frysinger68ffb9a2006-12-30 19:43:35 +0000395 if (use_timestamp) {
396 size_t ret = \
397 strftime(buf, sizeof(buf), "#define AUTOCONF_TIMESTAMP "
398 "\"%Y-%m-%d %H:%M:%S %Z\"\n", localtime(&now));
399 /* if user has Factory timezone or some other odd install, the
400 * %Z above will overflow the string leaving us with undefined
401 * results ... so let's try again without the timezone.
402 */
403 if (ret == 0)
404 strftime(buf, sizeof(buf), "#define AUTOCONF_TIMESTAMP "
405 "\"%Y-%m-%d %H:%M:%S\"\n", localtime(&now));
406 }
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000407 fprintf(out_h, "/*\n"
408 " * Automatically generated C config: don't edit\n"
Bernhard Reutner-Fischer9e8df932007-01-17 19:36:01 +0000409 " * Busybox version: %s\n"
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000410 " */\n"
411 "%s"
Mike Frysinger1b3e8172006-12-30 19:46:38 +0000412 "\n",
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000413 sym_get_string_value(sym),
414 buf);
415 }
416 if (!sym_change_count)
417 sym_clear_all_valid();
418
419 menu = rootmenu.list;
420 while (menu) {
421 sym = menu->sym;
422 if (!sym) {
423 if (!menu_is_visible(menu))
424 goto next;
425 str = menu_get_prompt(menu);
426 fprintf(out, "\n"
427 "#\n"
428 "# %s\n"
429 "#\n", str);
430 if (out_h)
431 fprintf(out_h, "\n"
432 "/*\n"
433 " * %s\n"
434 " */\n", str);
435 } else if (!(sym->flags & SYMBOL_CHOICE)) {
436 sym_calc_value(sym);
Denis Vlasenkob79b87a2006-10-19 22:17:44 +0000437/* bbox: we want to all syms
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000438 if (!(sym->flags & SYMBOL_WRITE))
439 goto next;
Denis Vlasenkob79b87a2006-10-19 22:17:44 +0000440*/
Denis Vlasenko7d219aa2006-10-05 10:17:08 +0000441 sym->flags &= ~SYMBOL_WRITE;
442 type = sym->type;
443 if (type == S_TRISTATE) {
444 sym_calc_value(modules_sym);
445 if (modules_sym->curr.tri == no)
446 type = S_BOOLEAN;
447 }
448 switch (type) {
449 case S_BOOLEAN:
450 case S_TRISTATE:
451 switch (sym_get_tristate_value(sym)) {
452 case no:
453 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
454 if (out_h) {
455 fprintf(out_h, "#undef CONFIG_%s\n", sym->name);
456 /* bbox */
457 fprintf(out_h, "#define ENABLE_%s 0\n", sym->name);
458 fprintf(out_h, "#define USE_%s(...)\n", sym->name);
459 fprintf(out_h, "#define SKIP_%s(...) __VA_ARGS__\n", sym->name);
460 }
461 break;
462 case mod:
463 fprintf(out, "CONFIG_%s=m\n", sym->name);
464 if (out_h)
465 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
466 break;
467 case yes:
468 fprintf(out, "CONFIG_%s=y\n", sym->name);
469 if (out_h) {
470 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
471 /* bbox */
472 fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
473 fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
474 fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
475 }
476 break;
477 }
478 break;
479 case S_STRING:
480 // fix me
481 str = sym_get_string_value(sym);
482 fprintf(out, "CONFIG_%s=\"", sym->name);
483 if (out_h)
484 fprintf(out_h, "#define CONFIG_%s \"", sym->name);
485 do {
486 l = strcspn(str, "\"\\");
487 if (l) {
488 fwrite(str, l, 1, out);
489 if (out_h)
490 fwrite(str, l, 1, out_h);
491 }
492 str += l;
493 while (*str == '\\' || *str == '"') {
494 fprintf(out, "\\%c", *str);
495 if (out_h)
496 fprintf(out_h, "\\%c", *str);
497 str++;
498 }
499 } while (*str);
500 fputs("\"\n", out);
501 if (out_h) {
502 fputs("\"\n", out_h);
503 /* bbox */
504 fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
505 fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
506 fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
507 }
508 break;
509 case S_HEX:
510 str = sym_get_string_value(sym);
511 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
512 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
513 if (out_h) {
514 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
515 /* bbox */
516 fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
517 fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
518 fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
519 }
520 break;
521 }
522 case S_INT:
523 str = sym_get_string_value(sym);
524 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
525 if (out_h) {
526 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
527 /* bbox */
528 fprintf(out_h, "#define ENABLE_%s 1\n", sym->name);
529 fprintf(out_h, "#define USE_%s(...) __VA_ARGS__\n", sym->name);
530 fprintf(out_h, "#define SKIP_%s(...)\n", sym->name);
531 }
532 break;
533 }
534 }
535
536 next:
537 if (menu->list) {
538 menu = menu->list;
539 continue;
540 }
541 if (menu->next)
542 menu = menu->next;
543 else while ((menu = menu->parent)) {
544 if (menu->next) {
545 menu = menu->next;
546 break;
547 }
548 }
549 }
550 fclose(out);
551 if (out_h) {
552 fclose(out_h);
553 rename(".tmpconfig.h", "include/autoconf.h");
554 }
555 if (!name || basename != conf_def_filename) {
556 if (!name)
557 name = conf_def_filename;
558 sprintf(tmpname, "%s.old", name);
559 rename(name, tmpname);
560 }
561 sprintf(tmpname, "%s%s", dirname, basename);
562 if (rename(newname, tmpname))
563 return 1;
564
565 sym_change_count = 0;
566
567 return 0;
568}