fix prerequisites due to libc changes
[buildroot.git] / package / config / confdata.c
blob7f06da4f16da3dc8a8da59cae5a7051b8c5990bc
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>
14 #include <libgen.h>
16 #define LKC_DIRECT_LINK
17 #include "lkc.h"
19 static void conf_warning(const char *fmt, ...)
20 __attribute__ ((format (printf, 1, 2)));
22 static const char *conf_filename;
23 static int conf_lineno, conf_warnings, conf_unsaved;
25 const char conf_defname[] = ".defconfig";
27 static void conf_warning(const char *fmt, ...)
29 va_list ap;
30 va_start(ap, fmt);
31 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
32 vfprintf(stderr, fmt, ap);
33 fprintf(stderr, "\n");
34 va_end(ap);
35 conf_warnings++;
38 const char *conf_get_configname(void)
40 char *name = getenv("BUILDROOT_CONFIG");
42 return name ? name : ".config";
45 static char *conf_expand_value(const char *in)
47 struct symbol *sym;
48 const char *src;
49 static char res_value[SYMBOL_MAXLENGTH];
50 char *dst, name[SYMBOL_MAXLENGTH];
52 res_value[0] = 0;
53 dst = name;
54 while ((src = strchr(in, '$'))) {
55 strncat(res_value, in, src - in);
56 src++;
57 dst = name;
58 while (isalnum(*src) || *src == '_')
59 *dst++ = *src++;
60 *dst = 0;
61 sym = sym_lookup(name, 0);
62 sym_calc_value(sym);
63 strcat(res_value, sym_get_string_value(sym));
64 in = src;
66 strcat(res_value, in);
68 return res_value;
71 char *conf_get_default_confname(void)
73 struct stat buf;
74 static char fullname[PATH_MAX+1];
75 char *env, *name;
77 name = conf_expand_value(conf_defname);
78 env = getenv(SRCTREE);
79 if (env) {
80 sprintf(fullname, "%s/%s", env, name);
81 if (!stat(fullname, &buf))
82 return fullname;
84 return name;
87 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
89 char *p2;
91 switch (sym->type) {
92 case S_TRISTATE:
93 if (p[0] == 'm') {
94 sym->def[def].tri = mod;
95 sym->flags |= def_flags;
96 break;
98 case S_BOOLEAN:
99 if (p[0] == 'y') {
100 sym->def[def].tri = yes;
101 sym->flags |= def_flags;
102 break;
104 if (p[0] == 'n') {
105 sym->def[def].tri = no;
106 sym->flags |= def_flags;
107 break;
109 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
110 break;
111 case S_OTHER:
112 if (*p != '"') {
113 for (p2 = p; *p2 && !isspace(*p2); p2++)
115 sym->type = S_STRING;
116 goto done;
118 case S_STRING:
119 if (*p++ != '"')
120 break;
121 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
122 if (*p2 == '"') {
123 *p2 = 0;
124 break;
126 memmove(p2, p2 + 1, strlen(p2));
128 if (!p2) {
129 conf_warning("invalid string found");
130 return 1;
132 case S_INT:
133 case S_HEX:
134 done:
135 if (sym_string_valid(sym, p)) {
136 sym->def[def].val = strdup(p);
137 sym->flags |= def_flags;
138 } else {
139 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
140 return 1;
142 break;
143 default:
146 return 0;
149 int conf_read_simple(const char *name, int def)
151 FILE *in = NULL;
152 char line[1024];
153 char *p, *p2;
154 struct symbol *sym;
155 int i, def_flags;
157 if (name) {
158 in = zconf_fopen(name);
159 } else {
160 struct property *prop;
162 name = conf_get_configname();
163 in = zconf_fopen(name);
164 if (in)
165 goto load;
166 sym_add_change_count(1);
167 if (!sym_defconfig_list)
168 return 1;
170 for_all_defaults(sym_defconfig_list, prop) {
171 if (expr_calc_value(prop->visible.expr) == no ||
172 prop->expr->type != E_SYMBOL)
173 continue;
174 name = conf_expand_value(prop->expr->left.sym->name);
175 in = zconf_fopen(name);
176 if (in) {
177 printf(_("#\n"
178 "# using defaults found in %s\n"
179 "#\n"), name);
180 goto load;
184 if (!in)
185 return 1;
187 load:
188 conf_filename = name;
189 conf_lineno = 0;
190 conf_warnings = 0;
191 conf_unsaved = 0;
193 def_flags = SYMBOL_DEF << def;
194 for_all_symbols(i, sym) {
195 sym->flags |= SYMBOL_CHANGED;
196 sym->flags &= ~(def_flags|SYMBOL_VALID);
197 if (sym_is_choice(sym))
198 sym->flags |= def_flags;
199 switch (sym->type) {
200 case S_INT:
201 case S_HEX:
202 case S_STRING:
203 if (sym->def[def].val)
204 free(sym->def[def].val);
205 default:
206 sym->def[def].val = NULL;
207 sym->def[def].tri = no;
211 while (fgets(line, sizeof(line), in)) {
212 conf_lineno++;
213 sym = NULL;
214 switch (line[0]) {
215 case '#':
216 if (line[1]!=' ')
217 continue;
218 p = strchr(line + 2, ' ');
219 if (!p)
220 continue;
221 *p++ = 0;
222 if (strncmp(p, "is not set", 10))
223 continue;
224 if (def == S_DEF_USER) {
225 sym = sym_find(line + 2);
226 if (!sym) {
227 conf_warning("trying to assign nonexistent symbol %s", line + 2);
228 break;
230 } else {
231 sym = sym_lookup(line + 2, 0);
232 if (sym->type == S_UNKNOWN)
233 sym->type = S_BOOLEAN;
235 if (sym->flags & def_flags) {
236 conf_warning("trying to reassign symbol %s", sym->name);
237 break;
239 switch (sym->type) {
240 case S_BOOLEAN:
241 case S_TRISTATE:
242 sym->def[def].tri = no;
243 sym->flags |= def_flags;
244 break;
245 default:
248 break;
249 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
250 p = strchr(line, '=');
251 if (!p)
252 continue;
253 *p++ = 0;
254 p2 = strchr(p, '\n');
255 if (p2) {
256 *p2-- = 0;
257 if (*p2 == '\r')
258 *p2 = 0;
260 if (def == S_DEF_USER) {
261 sym = sym_find(line);
262 if (!sym) {
263 conf_warning("trying to assign nonexistent symbol %s", line);
264 break;
266 } else {
267 sym = sym_lookup(line, 0);
268 if (sym->type == S_UNKNOWN)
269 sym->type = S_OTHER;
271 if (sym->flags & def_flags) {
272 conf_warning("trying to reassign symbol %s", sym->name);
273 break;
275 if (conf_set_sym_val(sym, def, def_flags, p))
276 continue;
277 break;
278 case '\r':
279 case '\n':
280 break;
281 default:
282 conf_warning("unexpected data");
283 continue;
285 if (sym && sym_is_choice_value(sym)) {
286 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
287 switch (sym->def[def].tri) {
288 case no:
289 break;
290 case mod:
291 if (cs->def[def].tri == yes) {
292 conf_warning("%s creates inconsistent choice state", sym->name);
293 cs->flags &= ~def_flags;
295 break;
296 case yes:
297 if (cs->def[def].tri != no) {
298 conf_warning("%s creates inconsistent choice state", sym->name);
299 cs->flags &= ~def_flags;
300 } else
301 cs->def[def].val = sym;
302 break;
304 cs->def[def].tri = E_OR(cs->def[def].tri, sym->def[def].tri);
307 fclose(in);
309 if (modules_sym)
310 sym_calc_value(modules_sym);
311 return 0;
314 int conf_read(const char *name)
316 struct symbol *sym;
317 struct property *prop;
318 struct expr *e;
319 int i, flags;
321 sym_set_change_count(0);
323 if (conf_read_simple(name, S_DEF_USER))
324 return 1;
326 for_all_symbols(i, sym) {
327 sym_calc_value(sym);
328 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
329 goto sym_ok;
330 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
331 /* check that calculated value agrees with saved value */
332 switch (sym->type) {
333 case S_BOOLEAN:
334 case S_TRISTATE:
335 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
336 break;
337 if (!sym_is_choice(sym))
338 goto sym_ok;
339 default:
340 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
341 goto sym_ok;
342 break;
344 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
345 /* no previous value and not saved */
346 goto sym_ok;
347 conf_unsaved++;
348 /* maybe print value in verbose mode... */
349 sym_ok:
350 if (!sym_is_choice(sym))
351 continue;
352 /* The choice symbol only has a set value (and thus is not new)
353 * if all its visible childs have values.
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 for_all_symbols(i, sym) {
364 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
365 /* Reset values of generates values, so they'll appear
366 * as new, if they should become visible, but that
367 * doesn't quite work if the Kconfig and the saved
368 * configuration disagree.
370 if (sym->visible == no && !conf_unsaved)
371 sym->flags &= ~SYMBOL_DEF_USER;
372 switch (sym->type) {
373 case S_STRING:
374 case S_INT:
375 case S_HEX:
376 /* Reset a string value if it's out of range */
377 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
378 break;
379 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
380 conf_unsaved++;
381 break;
382 default:
383 break;
388 sym_add_change_count(conf_warnings || conf_unsaved);
390 return 0;
393 int conf_write(const char *name)
395 FILE *out;
396 struct symbol *sym;
397 struct menu *menu;
398 const char *basename;
399 char dirname[128], tmpname[128], newname[128];
400 int type, l;
401 const char *str;
402 time_t now;
403 int use_timestamp = 1;
404 char *env;
406 dirname[0] = 0;
407 if (name && name[0]) {
408 struct stat st;
409 char *slash;
411 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
412 strcpy(dirname, name);
413 strcat(dirname, "/");
414 basename = conf_get_configname();
415 } else if ((slash = strrchr(name, '/'))) {
416 int size = slash - name + 1;
417 memcpy(dirname, name, size);
418 dirname[size] = 0;
419 if (slash[1])
420 basename = slash + 1;
421 else
422 basename = conf_get_configname();
423 } else
424 basename = name;
425 } else
426 basename = conf_get_configname();
428 sprintf(newname, "%s%s", dirname, basename);
429 env = getenv("KCONFIG_OVERWRITECONFIG");
430 if (!env || !*env) {
431 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
432 out = fopen(tmpname, "w");
433 } else {
434 *tmpname = 0;
435 out = fopen(newname, "w");
437 if (!out)
438 return 1;
440 sym = sym_lookup("BR2_VERSION", 0);
441 sym_calc_value(sym);
442 time(&now);
443 env = getenv("KCONFIG_NOTIMESTAMP");
444 if (env && *env)
445 use_timestamp = 0;
447 fprintf(out, _("#\n"
448 "# Automatically generated make config: don't edit\n"
449 "%s%s"
450 "#\n"),
451 use_timestamp ? "# " : "",
452 use_timestamp ? ctime(&now) : "");
454 if (!conf_get_changed())
455 sym_clear_all_valid();
457 menu = rootmenu.list;
458 while (menu) {
459 sym = menu->sym;
460 if (!sym) {
461 if (!menu_is_visible(menu))
462 goto next;
463 str = menu_get_prompt(menu);
464 fprintf(out, "\n"
465 "#\n"
466 "# %s\n"
467 "#\n", str);
468 } else if (!(sym->flags & SYMBOL_CHOICE)) {
469 sym_calc_value(sym);
470 if (!(sym->flags & SYMBOL_WRITE))
471 goto next;
472 sym->flags &= ~SYMBOL_WRITE;
473 type = sym->type;
474 if (type == S_TRISTATE) {
475 sym_calc_value(modules_sym);
476 if (modules_sym->curr.tri == no)
477 type = S_BOOLEAN;
479 switch (type) {
480 case S_BOOLEAN:
481 case S_TRISTATE:
482 switch (sym_get_tristate_value(sym)) {
483 case no:
484 fprintf(out, "# %s is not set\n", sym->name);
485 break;
486 case mod:
487 fprintf(out, "%s=m\n", sym->name);
488 break;
489 case yes:
490 fprintf(out, "%s=y\n", sym->name);
491 break;
493 break;
494 case S_STRING:
495 str = sym_get_string_value(sym);
496 fprintf(out, "%s=\"", sym->name);
497 while (1) {
498 l = strcspn(str, "\"\\");
499 if (l) {
500 fwrite(str, l, 1, out);
501 str += l;
503 if (!*str)
504 break;
505 fprintf(out, "\\%c", *str++);
507 fputs("\"\n", out);
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, "%s=%s\n", sym->name, str);
513 break;
515 case S_INT:
516 str = sym_get_string_value(sym);
517 fprintf(out, "%s=%s\n", sym->name, str);
518 break;
522 next:
523 if (menu->list) {
524 menu = menu->list;
525 continue;
527 if (menu->next)
528 menu = menu->next;
529 else while ((menu = menu->parent)) {
530 if (menu->next) {
531 menu = menu->next;
532 break;
536 fclose(out);
538 if (*tmpname) {
539 strcat(dirname, basename);
540 strcat(dirname, ".old");
541 rename(newname, dirname);
542 if (rename(tmpname, newname))
543 return 1;
546 printf(_("#\n"
547 "# configuration written to %s\n"
548 "#\n"), newname);
550 sym_set_change_count(0);
552 return 0;
555 int conf_split_config(void)
557 char *name, path[128], *opwd, *dir, *_name;
558 char *s, *d, c;
559 struct symbol *sym;
560 struct stat sb;
561 int res, i, fd;
563 name = getenv("KCONFIG_AUTOCONFIG");
564 if (!name)
565 name = "include/config/auto.conf";
566 conf_read_simple(name, S_DEF_AUTO);
568 opwd = malloc(256);
569 _name = strdup(name);
570 if (opwd == NULL || _name == NULL)
571 return 1;
572 opwd = getcwd(opwd, 256);
573 dir = dirname(_name);
574 if (dir == NULL) {
575 res = 1;
576 goto err;
578 if (chdir(dir)) {
579 res = 1;
580 goto err;
583 res = 0;
584 for_all_symbols(i, sym) {
585 sym_calc_value(sym);
586 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
587 continue;
588 if (sym->flags & SYMBOL_WRITE) {
589 if (sym->flags & SYMBOL_DEF_AUTO) {
591 * symbol has old and new value,
592 * so compare them...
594 switch (sym->type) {
595 case S_BOOLEAN:
596 case S_TRISTATE:
597 if (sym_get_tristate_value(sym) ==
598 sym->def[S_DEF_AUTO].tri)
599 continue;
600 break;
601 case S_STRING:
602 case S_HEX:
603 case S_INT:
604 if (!strcmp(sym_get_string_value(sym),
605 sym->def[S_DEF_AUTO].val))
606 continue;
607 break;
608 default:
609 break;
611 } else {
613 * If there is no old value, only 'no' (unset)
614 * is allowed as new value.
616 switch (sym->type) {
617 case S_BOOLEAN:
618 case S_TRISTATE:
619 if (sym_get_tristate_value(sym) == no)
620 continue;
621 break;
622 default:
623 break;
626 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
627 /* There is neither an old nor a new value. */
628 continue;
629 /* else
630 * There is an old value, but no new value ('no' (unset)
631 * isn't saved in auto.conf, so the old value is always
632 * different from 'no').
635 /* Replace all '_' and append ".h" */
636 s = sym->name;
637 d = path;
638 while ((c = *s++)) {
639 c = tolower(c);
640 *d++ = (c == '_') ? '/' : c;
642 strcpy(d, ".h");
644 /* Assume directory path already exists. */
645 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
646 if (fd == -1) {
647 if (errno != ENOENT) {
648 res = 1;
649 break;
652 * Create directory components,
653 * unless they exist already.
655 d = path;
656 while ((d = strchr(d, '/'))) {
657 *d = 0;
658 if (stat(path, &sb) && mkdir(path, 0755)) {
659 res = 1;
660 goto out;
662 *d++ = '/';
664 /* Try it again. */
665 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
666 if (fd == -1) {
667 res = 1;
668 break;
671 close(fd);
673 out:
674 if (chdir(opwd))
675 res = 1;
676 err:
677 free(opwd);
678 free(_name);
679 return res;
682 int conf_write_autoconf(void)
684 struct symbol *sym;
685 const char *str;
686 char *name;
687 FILE *out, *out_h;
688 time_t now;
689 int i, l;
691 sym_clear_all_valid();
693 file_write_dep(".config.cmd");
695 if (conf_split_config())
696 return 1;
698 out = fopen(".tmpconfig", "w");
699 if (!out)
700 return 1;
702 out_h = fopen(".tmpconfig.h", "w");
703 if (!out_h) {
704 fclose(out);
705 return 1;
708 sym = sym_lookup("BR2_VERSION", 0);
709 sym_calc_value(sym);
710 time(&now);
711 fprintf(out, "#\n"
712 "# Automatically generated make config: don't edit\n"
713 "# %s"
714 "#\n",
715 ctime(&now));
716 fprintf(out_h, "/*\n"
717 " * Automatically generated C config: don't edit\n"
718 " * %s"
719 " */\n",
720 ctime(&now));
722 for_all_symbols(i, sym) {
723 sym_calc_value(sym);
724 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
725 continue;
726 switch (sym->type) {
727 case S_BOOLEAN:
728 case S_TRISTATE:
729 switch (sym_get_tristate_value(sym)) {
730 case no:
731 break;
732 case mod:
733 fprintf(out, "%s=m\n", sym->name);
734 fprintf(out_h, "#define %s_MODULE 1\n", sym->name);
735 break;
736 case yes:
737 fprintf(out, "%s=y\n", sym->name);
738 fprintf(out_h, "#define %s 1\n", sym->name);
739 break;
741 break;
742 case S_STRING:
743 str = sym_get_string_value(sym);
744 fprintf(out, "%s=\"", sym->name);
745 fprintf(out_h, "#define %s \"", sym->name);
746 while (1) {
747 l = strcspn(str, "\"\\");
748 if (l) {
749 fwrite(str, l, 1, out);
750 fwrite(str, l, 1, out_h);
751 str += l;
753 if (!*str)
754 break;
755 fprintf(out, "\\%c", *str);
756 fprintf(out_h, "\\%c", *str);
757 str++;
759 fputs("\"\n", out);
760 fputs("\"\n", out_h);
761 break;
762 case S_HEX:
763 str = sym_get_string_value(sym);
764 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
765 fprintf(out, "%s=%s\n", sym->name, str);
766 fprintf(out_h, "#define %s 0x%s\n", sym->name, str);
767 break;
769 case S_INT:
770 str = sym_get_string_value(sym);
771 fprintf(out, "%s=%s\n", sym->name, str);
772 fprintf(out_h, "#define %s %s\n", sym->name, str);
773 break;
774 default:
775 break;
778 fclose(out);
779 fclose(out_h);
781 name = getenv("KCONFIG_AUTOHEADER");
782 if (!name)
783 name = "include/linux/autoconf.h";
784 if (rename(".tmpconfig.h", name))
785 return 1;
786 name = getenv("KCONFIG_AUTOCONFIG");
787 if (!name)
788 name = "include/config/auto.conf";
790 * This must be the last step, kbuild has a dependency on auto.conf
791 * and this marks the successful completion of the previous steps.
793 if (rename(".tmpconfig", name))
794 return 1;
796 return 0;
799 static int sym_change_count;
800 static void (*conf_changed_callback)(void);
802 void sym_set_change_count(int count)
804 int _sym_change_count = sym_change_count;
805 sym_change_count = count;
806 if (conf_changed_callback &&
807 (bool)_sym_change_count != (bool)count)
808 conf_changed_callback();
811 void sym_add_change_count(int count)
813 sym_set_change_count(count + sym_change_count);
816 bool conf_get_changed(void)
818 return sym_change_count;
821 void conf_set_changed_callback(void (*fn)(void))
823 conf_changed_callback = fn;