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