[PATCH] chardev: GPIO for SCx200 & PC-8736x: migrate file-ops to common module
[linux-2.6/kmemtrace.git] / scripts / kconfig / confdata.c
blob2ee48c377b66b5e3f01ba752ad1944f303e22ad9
1 /*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
6 #include <sys/stat.h>
7 #include <ctype.h>
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include <unistd.h>
15 #define LKC_DIRECT_LINK
16 #include "lkc.h"
18 static void conf_warning(const char *fmt, ...)
19 __attribute__ ((format (printf, 1, 2)));
21 static const char *conf_filename;
22 static int conf_lineno, conf_warnings, conf_unsaved;
24 const char conf_defname[] = "arch/$ARCH/defconfig";
26 static void conf_warning(const char *fmt, ...)
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++;
37 const char *conf_get_configname(void)
39 char *name = getenv("KCONFIG_CONFIG");
41 return name ? name : ".config";
44 static char *conf_expand_value(const char *in)
46 struct symbol *sym;
47 const char *src;
48 static char res_value[SYMBOL_MAXLENGTH];
49 char *dst, name[SYMBOL_MAXLENGTH];
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;
65 strcat(res_value, in);
67 return res_value;
70 char *conf_get_default_confname(void)
72 struct stat buf;
73 static char fullname[PATH_MAX+1];
74 char *env, *name;
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;
83 return name;
86 int conf_read_simple(const char *name, int def)
88 FILE *in = NULL;
89 char line[1024];
90 char *p, *p2;
91 struct symbol *sym;
92 int i, def_flags;
94 if (name) {
95 in = zconf_fopen(name);
96 } else {
97 struct property *prop;
99 name = conf_get_configname();
100 in = zconf_fopen(name);
101 if (in)
102 goto load;
103 sym_change_count++;
104 if (!sym_defconfig_list)
105 return 1;
107 for_all_defaults(sym_defconfig_list, prop) {
108 if (expr_calc_value(prop->visible.expr) == no ||
109 prop->expr->type != E_SYMBOL)
110 continue;
111 name = conf_expand_value(prop->expr->left.sym->name);
112 in = zconf_fopen(name);
113 if (in) {
114 printf(_("#\n"
115 "# using defaults found in %s\n"
116 "#\n"), name);
117 goto load;
121 if (!in)
122 return 1;
124 load:
125 conf_filename = name;
126 conf_lineno = 0;
127 conf_warnings = 0;
128 conf_unsaved = 0;
130 def_flags = SYMBOL_DEF << def;
131 for_all_symbols(i, sym) {
132 sym->flags |= SYMBOL_CHANGED;
133 sym->flags &= ~(def_flags|SYMBOL_VALID);
134 if (sym_is_choice(sym))
135 sym->flags |= def_flags;
136 switch (sym->type) {
137 case S_INT:
138 case S_HEX:
139 case S_STRING:
140 if (sym->def[def].val)
141 free(sym->def[def].val);
142 default:
143 sym->def[def].val = NULL;
144 sym->def[def].tri = no;
148 while (fgets(line, sizeof(line), in)) {
149 conf_lineno++;
150 sym = NULL;
151 switch (line[0]) {
152 case '#':
153 if (memcmp(line + 2, "CONFIG_", 7))
154 continue;
155 p = strchr(line + 9, ' ');
156 if (!p)
157 continue;
158 *p++ = 0;
159 if (strncmp(p, "is not set", 10))
160 continue;
161 if (def == S_DEF_USER) {
162 sym = sym_find(line + 9);
163 if (!sym) {
164 conf_warning("trying to assign nonexistent symbol %s", line + 9);
165 break;
167 } else {
168 sym = sym_lookup(line + 9, 0);
169 if (sym->type == S_UNKNOWN)
170 sym->type = S_BOOLEAN;
172 if (sym->flags & def_flags) {
173 conf_warning("trying to reassign symbol %s", sym->name);
174 break;
176 switch (sym->type) {
177 case S_BOOLEAN:
178 case S_TRISTATE:
179 sym->def[def].tri = no;
180 sym->flags |= def_flags;
181 break;
182 default:
185 break;
186 case 'C':
187 if (memcmp(line, "CONFIG_", 7)) {
188 conf_warning("unexpected data");
189 continue;
191 p = strchr(line + 7, '=');
192 if (!p)
193 continue;
194 *p++ = 0;
195 p2 = strchr(p, '\n');
196 if (p2)
197 *p2 = 0;
198 if (def == S_DEF_USER) {
199 sym = sym_find(line + 7);
200 if (!sym) {
201 conf_warning("trying to assign nonexistent symbol %s", line + 7);
202 break;
204 } else {
205 sym = sym_lookup(line + 7, 0);
206 if (sym->type == S_UNKNOWN)
207 sym->type = S_OTHER;
209 if (sym->flags & def_flags) {
210 conf_warning("trying to reassign symbol %s", sym->name);
211 break;
213 switch (sym->type) {
214 case S_TRISTATE:
215 if (p[0] == 'm') {
216 sym->def[def].tri = mod;
217 sym->flags |= def_flags;
218 break;
220 case S_BOOLEAN:
221 if (p[0] == 'y') {
222 sym->def[def].tri = yes;
223 sym->flags |= def_flags;
224 break;
226 if (p[0] == 'n') {
227 sym->def[def].tri = no;
228 sym->flags |= def_flags;
229 break;
231 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
232 break;
233 case S_OTHER:
234 if (*p != '"') {
235 for (p2 = p; *p2 && !isspace(*p2); p2++)
237 sym->type = S_STRING;
238 goto done;
240 case S_STRING:
241 if (*p++ != '"')
242 break;
243 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
244 if (*p2 == '"') {
245 *p2 = 0;
246 break;
248 memmove(p2, p2 + 1, strlen(p2));
250 if (!p2) {
251 conf_warning("invalid string found");
252 continue;
254 case S_INT:
255 case S_HEX:
256 done:
257 if (sym_string_valid(sym, p)) {
258 sym->def[def].val = strdup(p);
259 sym->flags |= def_flags;
260 } else {
261 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
262 continue;
264 break;
265 default:
268 break;
269 case '\n':
270 break;
271 default:
272 conf_warning("unexpected data");
273 continue;
275 if (sym && sym_is_choice_value(sym)) {
276 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
277 switch (sym->def[def].tri) {
278 case no:
279 break;
280 case mod:
281 if (cs->def[def].tri == yes) {
282 conf_warning("%s creates inconsistent choice state", sym->name);
283 cs->flags &= ~def_flags;
285 break;
286 case yes:
287 if (cs->def[def].tri != no) {
288 conf_warning("%s creates inconsistent choice state", sym->name);
289 cs->flags &= ~def_flags;
290 } else
291 cs->def[def].val = sym;
292 break;
294 cs->def[def].tri = E_OR(cs->def[def].tri, sym->def[def].tri);
297 fclose(in);
299 if (modules_sym)
300 sym_calc_value(modules_sym);
301 return 0;
304 int conf_read(const char *name)
306 struct symbol *sym;
307 struct property *prop;
308 struct expr *e;
309 int i, flags;
311 sym_change_count = 0;
313 if (conf_read_simple(name, S_DEF_USER))
314 return 1;
316 for_all_symbols(i, sym) {
317 sym_calc_value(sym);
318 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
319 goto sym_ok;
320 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
321 /* check that calculated value agrees with saved value */
322 switch (sym->type) {
323 case S_BOOLEAN:
324 case S_TRISTATE:
325 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
326 break;
327 if (!sym_is_choice(sym))
328 goto sym_ok;
329 default:
330 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
331 goto sym_ok;
332 break;
334 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
335 /* no previous value and not saved */
336 goto sym_ok;
337 conf_unsaved++;
338 /* maybe print value in verbose mode... */
339 sym_ok:
340 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
341 if (sym->visible == no)
342 sym->flags &= ~SYMBOL_DEF_USER;
343 switch (sym->type) {
344 case S_STRING:
345 case S_INT:
346 case S_HEX:
347 if (!sym_string_within_range(sym, sym->def[S_DEF_USER].val))
348 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
349 default:
350 break;
353 if (!sym_is_choice(sym))
354 continue;
355 prop = sym_get_choice_prop(sym);
356 flags = sym->flags;
357 for (e = prop->expr; e; e = e->left.expr)
358 if (e->right.sym->visible != no)
359 flags &= e->right.sym->flags;
360 sym->flags |= flags & SYMBOL_DEF_USER;
363 sym_change_count += conf_warnings || conf_unsaved;
365 return 0;
368 int conf_write(const char *name)
370 FILE *out;
371 struct symbol *sym;
372 struct menu *menu;
373 const char *basename;
374 char dirname[128], tmpname[128], newname[128];
375 int type, l;
376 const char *str;
377 time_t now;
378 int use_timestamp = 1;
379 char *env;
381 dirname[0] = 0;
382 if (name && name[0]) {
383 struct stat st;
384 char *slash;
386 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
387 strcpy(dirname, name);
388 strcat(dirname, "/");
389 basename = conf_get_configname();
390 } else if ((slash = strrchr(name, '/'))) {
391 int size = slash - name + 1;
392 memcpy(dirname, name, size);
393 dirname[size] = 0;
394 if (slash[1])
395 basename = slash + 1;
396 else
397 basename = conf_get_configname();
398 } else
399 basename = name;
400 } else
401 basename = conf_get_configname();
403 sprintf(newname, "%s%s", dirname, basename);
404 env = getenv("KCONFIG_OVERWRITECONFIG");
405 if (!env || !*env) {
406 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
407 out = fopen(tmpname, "w");
408 } else {
409 *tmpname = 0;
410 out = fopen(newname, "w");
412 if (!out)
413 return 1;
415 sym = sym_lookup("KERNELVERSION", 0);
416 sym_calc_value(sym);
417 time(&now);
418 env = getenv("KCONFIG_NOTIMESTAMP");
419 if (env && *env)
420 use_timestamp = 0;
422 fprintf(out, _("#\n"
423 "# Automatically generated make config: don't edit\n"
424 "# Linux kernel version: %s\n"
425 "%s%s"
426 "#\n"),
427 sym_get_string_value(sym),
428 use_timestamp ? "# " : "",
429 use_timestamp ? ctime(&now) : "");
431 if (!sym_change_count)
432 sym_clear_all_valid();
434 menu = rootmenu.list;
435 while (menu) {
436 sym = menu->sym;
437 if (!sym) {
438 if (!menu_is_visible(menu))
439 goto next;
440 str = menu_get_prompt(menu);
441 fprintf(out, "\n"
442 "#\n"
443 "# %s\n"
444 "#\n", str);
445 } else if (!(sym->flags & SYMBOL_CHOICE)) {
446 sym_calc_value(sym);
447 if (!(sym->flags & SYMBOL_WRITE))
448 goto next;
449 sym->flags &= ~SYMBOL_WRITE;
450 type = sym->type;
451 if (type == S_TRISTATE) {
452 sym_calc_value(modules_sym);
453 if (modules_sym->curr.tri == no)
454 type = S_BOOLEAN;
456 switch (type) {
457 case S_BOOLEAN:
458 case S_TRISTATE:
459 switch (sym_get_tristate_value(sym)) {
460 case no:
461 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
462 break;
463 case mod:
464 fprintf(out, "CONFIG_%s=m\n", sym->name);
465 break;
466 case yes:
467 fprintf(out, "CONFIG_%s=y\n", sym->name);
468 break;
470 break;
471 case S_STRING:
472 str = sym_get_string_value(sym);
473 fprintf(out, "CONFIG_%s=\"", sym->name);
474 while (1) {
475 l = strcspn(str, "\"\\");
476 if (l) {
477 fwrite(str, l, 1, out);
478 str += l;
480 if (!*str)
481 break;
482 fprintf(out, "\\%c", *str++);
484 fputs("\"\n", out);
485 break;
486 case S_HEX:
487 str = sym_get_string_value(sym);
488 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
489 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
490 break;
492 case S_INT:
493 str = sym_get_string_value(sym);
494 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
495 break;
499 next:
500 if (menu->list) {
501 menu = menu->list;
502 continue;
504 if (menu->next)
505 menu = menu->next;
506 else while ((menu = menu->parent)) {
507 if (menu->next) {
508 menu = menu->next;
509 break;
513 fclose(out);
515 if (*tmpname) {
516 strcat(dirname, name ? name : conf_get_configname());
517 strcat(dirname, ".old");
518 rename(newname, dirname);
519 if (rename(tmpname, newname))
520 return 1;
523 printf(_("#\n"
524 "# configuration written to %s\n"
525 "#\n"), newname);
527 sym_change_count = 0;
529 return 0;
532 int conf_split_config(void)
534 char *name, path[128];
535 char *s, *d, c;
536 struct symbol *sym;
537 struct stat sb;
538 int res, i, fd;
540 name = getenv("KCONFIG_AUTOCONFIG");
541 if (!name)
542 name = "include/config/auto.conf";
543 conf_read_simple(name, S_DEF_AUTO);
545 if (chdir("include/config"))
546 return 1;
548 res = 0;
549 for_all_symbols(i, sym) {
550 sym_calc_value(sym);
551 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
552 continue;
553 if (sym->flags & SYMBOL_WRITE) {
554 if (sym->flags & SYMBOL_DEF_AUTO) {
556 * symbol has old and new value,
557 * so compare them...
559 switch (sym->type) {
560 case S_BOOLEAN:
561 case S_TRISTATE:
562 if (sym_get_tristate_value(sym) ==
563 sym->def[S_DEF_AUTO].tri)
564 continue;
565 break;
566 case S_STRING:
567 case S_HEX:
568 case S_INT:
569 if (!strcmp(sym_get_string_value(sym),
570 sym->def[S_DEF_AUTO].val))
571 continue;
572 break;
573 default:
574 break;
576 } else {
578 * If there is no old value, only 'no' (unset)
579 * is allowed as new value.
581 switch (sym->type) {
582 case S_BOOLEAN:
583 case S_TRISTATE:
584 if (sym_get_tristate_value(sym) == no)
585 continue;
586 break;
587 default:
588 break;
591 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
592 /* There is neither an old nor a new value. */
593 continue;
594 /* else
595 * There is an old value, but no new value ('no' (unset)
596 * isn't saved in auto.conf, so the old value is always
597 * different from 'no').
600 /* Replace all '_' and append ".h" */
601 s = sym->name;
602 d = path;
603 while ((c = *s++)) {
604 c = tolower(c);
605 *d++ = (c == '_') ? '/' : c;
607 strcpy(d, ".h");
609 /* Assume directory path already exists. */
610 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
611 if (fd == -1) {
612 if (errno != ENOENT) {
613 res = 1;
614 break;
617 * Create directory components,
618 * unless they exist already.
620 d = path;
621 while ((d = strchr(d, '/'))) {
622 *d = 0;
623 if (stat(path, &sb) && mkdir(path, 0755)) {
624 res = 1;
625 goto out;
627 *d++ = '/';
629 /* Try it again. */
630 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
631 if (fd == -1) {
632 res = 1;
633 break;
636 close(fd);
638 out:
639 if (chdir("../.."))
640 return 1;
642 return res;
645 int conf_write_autoconf(void)
647 struct symbol *sym;
648 const char *str;
649 char *name;
650 FILE *out, *out_h;
651 time_t now;
652 int i, l;
654 sym_clear_all_valid();
656 file_write_dep("include/config/auto.conf.cmd");
658 if (conf_split_config())
659 return 1;
661 out = fopen(".tmpconfig", "w");
662 if (!out)
663 return 1;
665 out_h = fopen(".tmpconfig.h", "w");
666 if (!out_h) {
667 fclose(out);
668 return 1;
671 sym = sym_lookup("KERNELVERSION", 0);
672 sym_calc_value(sym);
673 time(&now);
674 fprintf(out, "#\n"
675 "# Automatically generated make config: don't edit\n"
676 "# Linux kernel version: %s\n"
677 "# %s"
678 "#\n",
679 sym_get_string_value(sym), ctime(&now));
680 fprintf(out_h, "/*\n"
681 " * Automatically generated C config: don't edit\n"
682 " * Linux kernel version: %s\n"
683 " * %s"
684 " */\n"
685 "#define AUTOCONF_INCLUDED\n",
686 sym_get_string_value(sym), ctime(&now));
688 for_all_symbols(i, sym) {
689 sym_calc_value(sym);
690 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
691 continue;
692 switch (sym->type) {
693 case S_BOOLEAN:
694 case S_TRISTATE:
695 switch (sym_get_tristate_value(sym)) {
696 case no:
697 break;
698 case mod:
699 fprintf(out, "CONFIG_%s=m\n", sym->name);
700 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
701 break;
702 case yes:
703 fprintf(out, "CONFIG_%s=y\n", sym->name);
704 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
705 break;
707 break;
708 case S_STRING:
709 str = sym_get_string_value(sym);
710 fprintf(out, "CONFIG_%s=\"", sym->name);
711 fprintf(out_h, "#define CONFIG_%s \"", sym->name);
712 while (1) {
713 l = strcspn(str, "\"\\");
714 if (l) {
715 fwrite(str, l, 1, out);
716 fwrite(str, l, 1, out_h);
717 str += l;
719 if (!*str)
720 break;
721 fprintf(out, "\\%c", *str);
722 fprintf(out_h, "\\%c", *str);
723 str++;
725 fputs("\"\n", out);
726 fputs("\"\n", out_h);
727 break;
728 case S_HEX:
729 str = sym_get_string_value(sym);
730 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
731 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
732 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
733 break;
735 case S_INT:
736 str = sym_get_string_value(sym);
737 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
738 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
739 break;
740 default:
741 break;
744 fclose(out);
745 fclose(out_h);
747 name = getenv("KCONFIG_AUTOHEADER");
748 if (!name)
749 name = "include/linux/autoconf.h";
750 if (rename(".tmpconfig.h", name))
751 return 1;
752 name = getenv("KCONFIG_AUTOCONFIG");
753 if (!name)
754 name = "include/config/auto.conf";
756 * This must be the last step, kbuild has a dependency on auto.conf
757 * and this marks the successful completion of the previous steps.
759 if (rename(".tmpconfig", name))
760 return 1;
762 return 0;