Chameleon

Chameleon Svn Source Tree

Root/branches/chucko/i386/config/confdata.c

1/*
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 <errno.h>
9#include <fcntl.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <time.h>
14#include <unistd.h>
15
16#define LKC_DIRECT_LINK
17#include "lkc.h"
18
19#define max(a, b) ({\
20typeof(a) _a = a;\
21typeof(b) _b = b;\
22_a > _b ? _a : _b; })
23
24#define min(a, b) ({\
25typeof(a) _a = a;\
26typeof(b) _b = b;\
27_a < _b ? _a : _b; })
28
29static void conf_warning(const char *fmt, ...)
30__attribute__ ((format (printf, 1, 2)));
31
32static void conf_message(const char *fmt, ...)
33__attribute__ ((format (printf, 1, 2)));
34
35static const char *conf_filename;
36static int conf_lineno, conf_warnings, conf_unsaved;
37
38const char conf_defname[] = "config/defconfig";
39
40static void conf_warning(const char *fmt, ...)
41{
42va_list ap;
43va_start(ap, fmt);
44fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
45vfprintf(stderr, fmt, ap);
46fprintf(stderr, "\n");
47va_end(ap);
48conf_warnings++;
49}
50
51static void conf_default_message_callback(const char *fmt, va_list ap)
52{
53printf("#\n# ");
54vprintf(fmt, ap);
55printf("\n#\n");
56}
57
58static void (*conf_message_callback) (const char *fmt, va_list ap) =
59conf_default_message_callback;
60void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap))
61{
62conf_message_callback = fn;
63}
64
65static void conf_message(const char *fmt, ...)
66{
67va_list ap;
68
69va_start(ap, fmt);
70if (conf_message_callback)
71conf_message_callback(fmt, ap);
72}
73
74const char *conf_get_configname(void)
75{
76char *name = getenv("KCONFIG_CONFIG");
77
78return name ? name : ".config";
79}
80
81const char *conf_get_autoconfig_name(void)
82{
83char *name = getenv("KCONFIG_AUTOCONFIG");
84
85return name ? name : "auto.conf";
86}
87
88/* TODO: figure out if symbols are always null-terminated */
89static char *conf_expand_value(const char *in)
90{
91 static char res_value[SYMBOL_MAXLENGTH + 1];
92 char name[SYMBOL_MAXLENGTH];
93 size_t res_rem = SYMBOL_MAXLENGTH;
94 char *res_ptr = res_value;
95 const char *src;
96 *res_ptr = 0;
97 res_ptr[SYMBOL_MAXLENGTH] = 0;
98
99 while ((src = strchr(in, '$'))) {
100 struct symbol *sym;
101 const char *symval;
102 char *name_ptr = name;
103 size_t n = min(res_rem, src - in);
104
105 res_ptr = stpncpy(res_ptr, in, n);
106 if (!(res_rem -= n))
107 return res_value; /* buffer full, quit now */
108 src++;
109
110 *name_ptr = 0;
111 while (isalnum(*src) || *src == '_')
112 *name_ptr++ = *src++;
113 *name_ptr = 0;
114
115 sym = sym_lookup(name, 0);
116 sym_calc_value(sym);
117 symval = sym_get_string_value(sym);
118 n = min(res_rem, strlen(symval));
119
120 res_ptr = stpncpy(res_ptr, symval, n);
121 if (!(res_rem -= n))
122 return res_value; /* buffer full, quit now */
123
124 in = src;
125 }
126
127 strncpy(res_ptr, in, res_rem + 1);
128 return res_value;
129}
130
131char *conf_get_default_confname(void)
132{
133struct stat buf;
134static char fullname[PATH_MAX+1];
135char *env, *name;
136
137name = conf_expand_value(conf_defname);
138env = getenv(SRCTREE);
139if (env) {
140 snprintf(fullname, PATH_MAX+1, "%s/%s", env, name);
141if (!stat(fullname, &buf))
142return fullname;
143}
144return name;
145}
146
147static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
148{
149char *p2;
150
151switch (sym->type) {
152case S_TRISTATE:
153if (p[0] == 'm') {
154sym->def[def].tri = mod;
155sym->flags |= def_flags;
156break;
157}
158case S_BOOLEAN:
159if (p[0] == 'y') {
160sym->def[def].tri = yes;
161sym->flags |= def_flags;
162break;
163}
164if (p[0] == 'n') {
165sym->def[def].tri = no;
166sym->flags |= def_flags;
167break;
168}
169conf_warning("symbol value '%s' invalid for %s", p, sym->name);
170break;
171case S_OTHER:
172if (*p != '"') {
173for (p2 = p; *p2 && !isspace(*p2); p2++)
174;
175sym->type = S_STRING;
176goto done;
177}
178case S_STRING:
179if (*p++ != '"')
180break;
181for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
182if (*p2 == '"') {
183*p2 = 0;
184break;
185}
186memmove(p2, p2 + 1, strlen(p2));
187}
188if (!p2) {
189conf_warning("invalid string found");
190return 1;
191}
192case S_INT:
193case S_HEX:
194done:
195if (sym_string_valid(sym, p)) {
196sym->def[def].val = strdup(p);
197sym->flags |= def_flags;
198} else {
199conf_warning("symbol value '%s' invalid for %s", p, sym->name);
200return 1;
201}
202break;
203default:
204;
205}
206return 0;
207}
208
209int conf_read_simple(const char *name, int def)
210{
211FILE *in = NULL;
212char line[1024];
213char *p, *p2;
214struct symbol *sym;
215int i, def_flags;
216
217if (name) {
218in = zconf_fopen(name);
219} else {
220struct property *prop;
221
222name = conf_get_configname();
223in = zconf_fopen(name);
224if (in)
225goto load;
226sym_add_change_count(1);
227if (!sym_defconfig_list) {
228if (modules_sym)
229sym_calc_value(modules_sym);
230return 1;
231}
232
233for_all_defaults(sym_defconfig_list, prop) {
234if (expr_calc_value(prop->visible.expr) == no ||
235 prop->expr->type != E_SYMBOL)
236continue;
237name = conf_expand_value(prop->expr->left.sym->name);
238in = zconf_fopen(name);
239if (in) {
240conf_message(_("using defaults found in %s"),
241 name);
242goto load;
243}
244}
245}
246if (!in)
247return 1;
248
249load:
250conf_filename = name;
251conf_lineno = 0;
252conf_warnings = 0;
253conf_unsaved = 0;
254
255def_flags = SYMBOL_DEF << def;
256for_all_symbols(i, sym) {
257sym->flags |= SYMBOL_CHANGED;
258sym->flags &= ~(def_flags|SYMBOL_VALID);
259if (sym_is_choice(sym))
260sym->flags |= def_flags;
261switch (sym->type) {
262case S_INT:
263case S_HEX:
264case S_STRING:
265if (sym->def[def].val)
266free(sym->def[def].val);
267default:
268sym->def[def].val = NULL;
269sym->def[def].tri = no;
270}
271}
272
273while (fgets(line, sizeof(line), in)) {
274conf_lineno++;
275sym = NULL;
276if (line[0] == '#') {
277if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
278continue;
279p = strchr(line + 2 + strlen(CONFIG_), ' ');
280if (!p)
281continue;
282*p++ = 0;
283if (strncmp(p, "is not set", 10))
284continue;
285if (def == S_DEF_USER) {
286sym = sym_find(line + 2 + strlen(CONFIG_));
287if (!sym) {
288sym_add_change_count(1);
289goto setsym;
290}
291} else {
292sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
293if (sym->type == S_UNKNOWN)
294sym->type = S_BOOLEAN;
295}
296if (sym->flags & def_flags) {
297conf_warning("override: reassigning to symbol %s", sym->name);
298}
299switch (sym->type) {
300case S_BOOLEAN:
301case S_TRISTATE:
302sym->def[def].tri = no;
303sym->flags |= def_flags;
304break;
305default:
306;
307}
308} else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
309p = strchr(line + strlen(CONFIG_), '=');
310if (!p)
311continue;
312*p++ = 0;
313p2 = strchr(p, '\n');
314if (p2) {
315*p2-- = 0;
316if (*p2 == '\r')
317*p2 = 0;
318}
319if (def == S_DEF_USER) {
320sym = sym_find(line + strlen(CONFIG_));
321if (!sym) {
322sym_add_change_count(1);
323goto setsym;
324}
325} else {
326sym = sym_lookup(line + strlen(CONFIG_), 0);
327if (sym->type == S_UNKNOWN)
328sym->type = S_OTHER;
329}
330if (sym->flags & def_flags) {
331conf_warning("override: reassigning to symbol %s", sym->name);
332}
333if (conf_set_sym_val(sym, def, def_flags, p))
334continue;
335} else {
336if (line[0] != '\r' && line[0] != '\n')
337conf_warning("unexpected data");
338continue;
339}
340setsym:
341if (sym && sym_is_choice_value(sym)) {
342struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
343switch (sym->def[def].tri) {
344case no:
345break;
346case mod:
347if (cs->def[def].tri == yes) {
348conf_warning("%s creates inconsistent choice state", sym->name);
349cs->flags &= ~def_flags;
350}
351break;
352case yes:
353if (cs->def[def].tri != no)
354conf_warning("override: %s changes choice state", sym->name);
355cs->def[def].val = sym;
356break;
357}
358cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
359}
360}
361fclose(in);
362
363if (modules_sym)
364sym_calc_value(modules_sym);
365return 0;
366}
367
368int conf_read(const char *name)
369{
370struct symbol *sym, *choice_sym;
371struct property *prop;
372struct expr *e;
373int i, flags;
374
375sym_set_change_count(0);
376
377if (conf_read_simple(name, S_DEF_USER))
378return 1;
379
380for_all_symbols(i, sym) {
381sym_calc_value(sym);
382if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
383goto sym_ok;
384if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
385/* check that calculated value agrees with saved value */
386switch (sym->type) {
387case S_BOOLEAN:
388case S_TRISTATE:
389if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
390break;
391if (!sym_is_choice(sym))
392goto sym_ok;
393default:
394if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
395goto sym_ok;
396break;
397}
398} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
399/* no previous value and not saved */
400goto sym_ok;
401conf_unsaved++;
402/* maybe print value in verbose mode... */
403sym_ok:
404if (!sym_is_choice(sym))
405continue;
406/* The choice symbol only has a set value (and thus is not new)
407 * if all its visible childs have values.
408 */
409prop = sym_get_choice_prop(sym);
410flags = sym->flags;
411expr_list_for_each_sym(prop->expr, e, choice_sym)
412if (choice_sym->visible != no)
413flags &= choice_sym->flags;
414sym->flags &= flags | ~SYMBOL_DEF_USER;
415}
416
417for_all_symbols(i, sym) {
418if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
419/* Reset values of generates values, so they'll appear
420 * as new, if they should become visible, but that
421 * doesn't quite work if the Kconfig and the saved
422 * configuration disagree.
423 */
424if (sym->visible == no && !conf_unsaved)
425sym->flags &= ~SYMBOL_DEF_USER;
426switch (sym->type) {
427case S_STRING:
428case S_INT:
429case S_HEX:
430/* Reset a string value if it's out of range */
431if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
432break;
433sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
434conf_unsaved++;
435break;
436default:
437break;
438}
439}
440}
441
442sym_add_change_count(conf_warnings || conf_unsaved);
443
444return 0;
445}
446
447/* Write a S_STRING */
448static void conf_write_string(bool headerfile, const char *name,
449 const char *str, FILE *out)
450{
451int l;
452if (headerfile)
453fprintf(out, "#define %s%s \"", CONFIG_, name);
454else
455fprintf(out, "%s%s=\"", CONFIG_, name);
456
457while (1) {
458l = strcspn(str, "\"\\");
459if (l) {
460xfwrite(str, l, 1, out);
461str += l;
462}
463if (!*str)
464break;
465fprintf(out, "\\%c", *str++);
466}
467fputs("\"\n", out);
468}
469
470static void conf_write_symbol(struct symbol *sym, FILE *out, bool write_no)
471{
472const char *str;
473
474switch (sym->type) {
475case S_BOOLEAN:
476case S_TRISTATE:
477switch (sym_get_tristate_value(sym)) {
478case no:
479if (write_no)
480fprintf(out, "# %s%s is not set\n",
481 CONFIG_, sym->name);
482break;
483case mod:
484fprintf(out, "%s%s=m\n", CONFIG_, sym->name);
485break;
486case yes:
487fprintf(out, "%s%s=y\n", CONFIG_, sym->name);
488break;
489}
490break;
491case S_STRING:
492conf_write_string(false, sym->name, sym_get_string_value(sym), out);
493break;
494case S_HEX:
495case S_INT:
496str = sym_get_string_value(sym);
497fprintf(out, "%s%s=%s\n", CONFIG_, sym->name, str);
498break;
499case S_OTHER:
500case S_UNKNOWN:
501break;
502}
503}
504
505/*
506 * Write out a minimal config.
507 * All values that has default values are skipped as this is redundant.
508 */
509int conf_write_defconfig(const char *filename)
510{
511struct symbol *sym;
512struct menu *menu;
513FILE *out;
514
515out = fopen(filename, "w");
516if (!out)
517return 1;
518
519sym_clear_all_valid();
520
521/* Traverse all menus to find all relevant symbols */
522menu = rootmenu.list;
523
524while (menu != NULL)
525{
526sym = menu->sym;
527if (sym == NULL) {
528if (!menu_is_visible(menu))
529goto next_menu;
530} else if (!sym_is_choice(sym)) {
531sym_calc_value(sym);
532if (!(sym->flags & SYMBOL_WRITE))
533goto next_menu;
534sym->flags &= ~SYMBOL_WRITE;
535/* If we cannot change the symbol - skip */
536if (!sym_is_changable(sym))
537goto next_menu;
538/* If symbol equals to default value - skip */
539if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
540goto next_menu;
541
542/*
543 * If symbol is a choice value and equals to the
544 * default for a choice - skip.
545 * But only if value is bool and equal to "y" and
546 * choice is not "optional".
547 * (If choice is "optional" then all values can be "n")
548 */
549if (sym_is_choice_value(sym)) {
550struct symbol *cs;
551struct symbol *ds;
552
553cs = prop_get_symbol(sym_get_choice_prop(sym));
554ds = sym_choice_default(cs);
555if (!sym_is_optional(cs) && sym == ds) {
556if ((sym->type == S_BOOLEAN) &&
557 sym_get_tristate_value(sym) == yes)
558goto next_menu;
559}
560}
561conf_write_symbol(sym, out, true);
562}
563next_menu:
564if (menu->list != NULL) {
565menu = menu->list;
566}
567else if (menu->next != NULL) {
568menu = menu->next;
569} else {
570while ((menu = menu->parent)) {
571if (menu->next != NULL) {
572menu = menu->next;
573break;
574}
575}
576}
577}
578fclose(out);
579return 0;
580}
581
582int conf_write(const char *name)
583{
584FILE *out;
585struct symbol *sym;
586struct menu *menu;
587const char *basename;
588const char *str;
589char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
590time_t now;
591int use_timestamp = 1;
592char *env;
593
594dirname[0] = 0;
595if (name && name[0]) {
596struct stat st;
597char *slash;
598
599if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
600 /* FIXME: add length check */
601 strcpy(stpcpy(dirname, name), "/");
602 basename = conf_get_configname();
603} else if ((slash = strrchr(name, '/'))) {
604size_t size = slash - name + 1;
605memcpy(dirname, name, size);
606dirname[size] = 0;
607if (slash[1])
608basename = slash + 1;
609else
610basename = conf_get_configname();
611} else
612basename = name;
613} else
614basename = conf_get_configname();
615
616snprintf(newname, PATH_MAX+1, "%s%s", dirname, basename);
617env = getenv("KCONFIG_OVERWRITECONFIG");
618if (!env || !*env) {
619 snprintf(tmpname, PATH_MAX+1, "%s.tmpconfig.%d", dirname, (int)getpid());
620 out = fopen(tmpname, "w");
621} else {
622*tmpname = 0;
623out = fopen(newname, "w");
624}
625if (!out)
626return 1;
627
628time(&now);
629env = getenv("KCONFIG_NOTIMESTAMP");
630if (env && *env)
631use_timestamp = 0;
632
633fprintf(out, _("#\n"
634 "# Automatically generated make config: don't edit\n"
635 "# %s\n"
636 "%s%s"
637 "#\n"),
638 rootmenu.prompt->text,
639 use_timestamp ? "# " : "",
640 use_timestamp ? ctime(&now) : "");
641
642if (!conf_get_changed())
643sym_clear_all_valid();
644
645menu = rootmenu.list;
646while (menu) {
647sym = menu->sym;
648if (!sym) {
649if (!menu_is_visible(menu))
650goto next;
651str = menu_get_prompt(menu);
652fprintf(out, "\n"
653 "#\n"
654 "# %s\n"
655 "#\n", str);
656} else if (!(sym->flags & SYMBOL_CHOICE)) {
657sym_calc_value(sym);
658if (!(sym->flags & SYMBOL_WRITE))
659goto next;
660sym->flags &= ~SYMBOL_WRITE;
661/* Write config symbol to file */
662conf_write_symbol(sym, out, true);
663}
664
665next:
666if (menu->list) {
667menu = menu->list;
668continue;
669}
670if (menu->next)
671menu = menu->next;
672else while ((menu = menu->parent)) {
673if (menu->next) {
674menu = menu->next;
675break;
676}
677}
678}
679fclose(out);
680
681if (*tmpname) {
682strcat(dirname, basename);
683strcat(dirname, ".old");
684rename(newname, dirname);
685if (rename(tmpname, newname))
686return 1;
687}
688
689//conf_message(_("configuration written to %s"), newname);
690
691sym_set_change_count(0);
692
693return 0;
694}
695#if 0
696
697static int conf_split_config(void)
698{
699const char *name;
700char path[PATH_MAX+1];
701char *s, *d, c;
702struct symbol *sym;
703struct stat sb;
704int res, i, fd;
705
706name = conf_get_autoconfig_name();
707conf_read_simple(name, S_DEF_AUTO);
708
709//if (chdir("include/config"))
710//return 1;
711
712res = 0;
713for_all_symbols(i, sym) {
714sym_calc_value(sym);
715if ((sym->flags & SYMBOL_AUTO) || !sym->name)
716continue;
717if (sym->flags & SYMBOL_WRITE) {
718if (sym->flags & SYMBOL_DEF_AUTO) {
719/*
720 * symbol has old and new value,
721 * so compare them...
722 */
723switch (sym->type) {
724case S_BOOLEAN:
725case S_TRISTATE:
726if (sym_get_tristate_value(sym) ==
727 sym->def[S_DEF_AUTO].tri)
728continue;
729break;
730case S_STRING:
731case S_HEX:
732case S_INT:
733if (!strcmp(sym_get_string_value(sym),
734 sym->def[S_DEF_AUTO].val))
735continue;
736break;
737default:
738break;
739}
740} else {
741/*
742 * If there is no old value, only 'no' (unset)
743 * is allowed as new value.
744 */
745switch (sym->type) {
746case S_BOOLEAN:
747case S_TRISTATE:
748if (sym_get_tristate_value(sym) == no)
749continue;
750break;
751default:
752break;
753}
754}
755} else if (!(sym->flags & SYMBOL_DEF_AUTO))
756/* There is neither an old nor a new value. */
757continue;
758/* else
759 *There is an old value, but no new value ('no' (unset)
760 *isn't saved in auto.conf, so the old value is always
761 *different from 'no').
762 */
763
764/* Replace all '_' and append ".h" */
765s = sym->name;
766d = path;
767while ((c = *s++)) {
768c = tolower(c);
769*d++ = (c == '_') ? '/' : c;
770}
771strcpy(d, ".h");
772
773/* Assume directory path already exists. */
774fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
775if (fd == -1) {
776if (errno != ENOENT) {
777res = 1;
778break;
779}
780/*
781 * Create directory components,
782 * unless they exist already.
783 */
784d = path;
785while ((d = strchr(d, '/'))) {
786*d = 0;
787if (stat(path, &sb) && mkdir(path, 0755)) {
788res = 1;
789goto out;
790}
791*d++ = '/';
792}
793/* Try it again. */
794fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
795if (fd == -1) {
796res = 1;
797break;
798}
799}
800close(fd);
801}
802out:
803if (chdir("../.."))
804return 1;
805
806return res;
807}
808#endif
809int conf_write_autoconf(void)
810{
811struct symbol *sym;
812const char *str;
813const char *name;
814FILE *out, *out_h, *out_inc;
815time_t now;
816int i;
817
818sym_clear_all_valid();
819
820/*if (conf_split_config())
821{
822 printf("ERR: conf_split_config");
823 return 1;
824 }*/
825
826out = fopen(".tmpconfig", "w");
827if (!out)
828 {
829 printf("ERR: .tmpconfig");
830return 1;
831 }
832
833out_h = fopen(".tmpconfig.h", "w");
834if (!out_h) {
835fclose(out);
836return 1;
837}
838
839 out_inc = fopen(".tmpconfig.inc", "w");
840if (!out_inc) {
841fclose(out);
842 fclose(out_h);
843return 1;
844}
845
846
847time(&now);
848fprintf(out, "#\n"
849 "# Automatically generated make config: don't edit\n"
850 "# %s\n"
851 "# %s"
852 "#\n",
853 rootmenu.prompt->text, ctime(&now));
854
855fprintf(out_h, "//\n"
856 "// Automatically generated make config: don't edit\n"
857 "// %s\n"
858 "// %s"
859 "// \n",
860 rootmenu.prompt->text, ctime(&now));
861
862 fprintf(out_inc, ";\n"
863 "; Automatically generated make config: don't edit\n"
864 "; %s\n"
865 "; %s"
866 ";\n",
867 rootmenu.prompt->text, ctime(&now));
868
869
870 fprintf(out_h, "#define CONFIG_IS_BUILTIN 1\n");
871 fprintf(out_h, "#define CONFIG_IS_MODULE 2\n");
872
873
874for_all_symbols(i, sym) {
875sym_calc_value(sym);
876if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
877continue;
878
879/* write symbol to config file */
880conf_write_symbol(sym, out, false);
881
882/* update autoconf and tristate files */
883switch (sym->type) {
884case S_BOOLEAN:
885case S_TRISTATE:
886switch (sym_get_tristate_value(sym)) {
887case no:
888 fprintf(out_inc, "%s%s EQU 0\n",
889 CONFIG_, sym->name);
890
891break;
892case mod:
893fprintf(out_inc, "%s%s EQU 1\n",
894 CONFIG_, sym->name);
895 fprintf(out_h, "#define %s%s CONFIG_IS_MODULE\n",
896 CONFIG_, sym->name);
897
898break;
899case yes:
900 fprintf(out_inc, "%s%s EQU 1\n",
901 CONFIG_, sym->name);
902fprintf(out_h, "#define %s%s CONFIG_IS_BUILTIN\n",
903 CONFIG_, sym->name);
904break;
905}
906break;
907case S_STRING:
908conf_write_string(true, sym->name, sym_get_string_value(sym), out_h);
909break;
910case S_HEX:
911str = sym_get_string_value(sym);
912if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
913 fprintf(out_inc, "%s%s EQU 0x%s\n",
914 CONFIG_, sym->name, str);
915fprintf(out_h, "#define %s%s 0x%s\n",
916 CONFIG_, sym->name, str);
917break;
918}
919case S_INT:
920str = sym_get_string_value(sym);
921 fprintf(out_inc, "%s%s EQU %s\n",
922 CONFIG_, sym->name, str);
923fprintf(out_h, "#define %s%s %s\n",
924 CONFIG_, sym->name, str);
925break;
926default:
927break;
928}
929}
930fclose(out);
931fclose(out_h);
932fclose(out_inc);
933
934name = getenv("CCONFIG_AUTOHEADER");
935if (!name) name = "autoconf.h";
936if (rename(".tmpconfig.h", name))
937return 1;
938
939 name = getenv("CCONFIG_AUTOINC");
940if (!name) name = "autoconf.inc";
941if (rename(".tmpconfig.inc", name))
942return 1;
943
944
945name = conf_get_autoconfig_name();
946/*
947 * This must be the last step, kbuild has a dependency on auto.conf
948 * and this marks the successful completion of the previous steps.
949 */
950if (rename(".tmpconfig", name))
951 {
952 printf("rename");
953return 1;
954 }
955return 0;
956}
957
958static int sym_change_count;
959static void (*conf_changed_callback)(void);
960
961void sym_set_change_count(int count)
962{
963int _sym_change_count = sym_change_count;
964sym_change_count = count;
965if (conf_changed_callback &&
966 (bool)_sym_change_count != (bool)count)
967conf_changed_callback();
968}
969
970void sym_add_change_count(int count)
971{
972sym_set_change_count(count + sym_change_count);
973}
974
975bool conf_get_changed(void)
976{
977return sym_change_count;
978}
979
980void conf_set_changed_callback(void (*fn)(void))
981{
982conf_changed_callback = fn;
983}
984
985static void randomize_choice_values(struct symbol *csym)
986{
987struct property *prop;
988struct symbol *sym;
989struct expr *e;
990int cnt, def;
991
992/*
993 * If choice is mod then we may have more items selected
994 * and if no then no-one.
995 * In both cases stop.
996 */
997if (csym->curr.tri != yes)
998return;
999
1000prop = sym_get_choice_prop(csym);
1001
1002/* count entries in choice block */
1003cnt = 0;
1004expr_list_for_each_sym(prop->expr, e, sym)
1005cnt++;
1006
1007/*
1008 * find a random value and set it to yes,
1009 * set the rest to no so we have only one set
1010 */
1011def = (rand() % cnt);
1012
1013cnt = 0;
1014expr_list_for_each_sym(prop->expr, e, sym) {
1015if (def == cnt++) {
1016sym->def[S_DEF_USER].tri = yes;
1017csym->def[S_DEF_USER].val = sym;
1018}
1019else {
1020sym->def[S_DEF_USER].tri = no;
1021}
1022}
1023csym->flags |= SYMBOL_DEF_USER;
1024/* clear VALID to get value calculated */
1025csym->flags &= ~(SYMBOL_VALID);
1026}
1027
1028static void set_all_choice_values(struct symbol *csym)
1029{
1030struct property *prop;
1031struct symbol *sym;
1032struct expr *e;
1033
1034prop = sym_get_choice_prop(csym);
1035
1036/*
1037 * Set all non-assinged choice values to no
1038 */
1039expr_list_for_each_sym(prop->expr, e, sym) {
1040if (!sym_has_value(sym))
1041sym->def[S_DEF_USER].tri = no;
1042}
1043csym->flags |= SYMBOL_DEF_USER;
1044/* clear VALID to get value calculated */
1045csym->flags &= ~(SYMBOL_VALID);
1046}
1047
1048void conf_set_all_new_symbols(enum conf_def_mode mode)
1049{
1050struct symbol *sym, *csym;
1051int i, cnt;
1052
1053for_all_symbols(i, sym) {
1054if (sym_has_value(sym))
1055continue;
1056switch (sym_get_type(sym)) {
1057case S_BOOLEAN:
1058case S_TRISTATE:
1059switch (mode) {
1060case def_yes:
1061sym->def[S_DEF_USER].tri = yes;
1062break;
1063case def_mod:
1064sym->def[S_DEF_USER].tri = mod;
1065break;
1066case def_no:
1067sym->def[S_DEF_USER].tri = no;
1068break;
1069case def_random:
1070cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
1071sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
1072break;
1073default:
1074continue;
1075}
1076if (!(sym_is_choice(sym) && mode == def_random))
1077sym->flags |= SYMBOL_DEF_USER;
1078break;
1079default:
1080break;
1081}
1082
1083}
1084
1085sym_clear_all_valid();
1086
1087/*
1088 * We have different type of choice blocks.
1089 * If curr.tri equals to mod then we can select several
1090 * choice symbols in one block.
1091 * In this case we do nothing.
1092 * If curr.tri equals yes then only one symbol can be
1093 * selected in a choice block and we set it to yes,
1094 * and the rest to no.
1095 */
1096for_all_symbols(i, csym) {
1097if (sym_has_value(csym) || !sym_is_choice(csym))
1098continue;
1099
1100sym_calc_value(csym);
1101if (mode == def_random)
1102randomize_choice_values(csym);
1103else
1104set_all_choice_values(csym);
1105}
1106}
1107

Archive Download this file

Revision: 2336