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