move common unpacking code to unpack.mk, replace zcat with $(ZCAT) for systems that...
[openwrt/svn-archive/archive.git] / scripts / 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 <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <unistd.h>
13
14 #define LKC_DIRECT_LINK
15 #include "lkc.h"
16
17 static void conf_warning(const char *fmt, ...)
18 __attribute__ ((format (printf, 1, 2)));
19
20 static const char *conf_filename;
21 static int conf_lineno, conf_warnings, conf_unsaved;
22
23 const char conf_def_filename[] = ".config";
24
25 const char conf_defname[] = "scripts/config/defconfig";
26
27 const char *conf_confnames[] = {
28 ".config",
29 conf_defname,
30 NULL,
31 };
32
33 static void conf_warning(const char *fmt, ...)
34 {
35 va_list ap;
36 va_start(ap, fmt);
37 fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
38 vfprintf(stderr, fmt, ap);
39 fprintf(stderr, "\n");
40 va_end(ap);
41 conf_warnings++;
42 }
43
44 static char *conf_expand_value(const char *in)
45 {
46 struct symbol *sym;
47 const char *src;
48 static char res_value[SYMBOL_MAXLENGTH];
49 char *dst, name[SYMBOL_MAXLENGTH];
50
51 res_value[0] = 0;
52 dst = name;
53 while ((src = strchr(in, '$'))) {
54 strncat(res_value, in, src - in);
55 src++;
56 dst = name;
57 while (isalnum(*src) || *src == '_')
58 *dst++ = *src++;
59 *dst = 0;
60 sym = sym_lookup(name, 0);
61 sym_calc_value(sym);
62 strcat(res_value, sym_get_string_value(sym));
63 in = src;
64 }
65 strcat(res_value, in);
66
67 return res_value;
68 }
69
70 char *conf_get_default_confname(void)
71 {
72 struct stat buf;
73 static char fullname[PATH_MAX+1];
74 char *env, *name;
75
76 name = conf_expand_value(conf_defname);
77 env = getenv(SRCTREE);
78 if (env) {
79 sprintf(fullname, "%s/%s", env, name);
80 if (!stat(fullname, &buf))
81 return fullname;
82 }
83 return name;
84 }
85
86 void conf_reset(void)
87 {
88 struct symbol *sym;
89 int i;
90
91 for_all_symbols(i, sym) {
92 sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
93 if (sym_is_choice(sym))
94 sym->flags &= ~SYMBOL_NEW;
95 sym->flags &= ~SYMBOL_VALID;
96 switch (sym->type) {
97 case S_INT:
98 case S_HEX:
99 case S_STRING:
100 if (sym->user.val)
101 free(sym->user.val);
102 default:
103 sym->user.val = NULL;
104 sym->user.tri = no;
105 }
106 }
107 }
108
109 int conf_read_simple(const char *name)
110 {
111 FILE *in = NULL;
112 char line[1024];
113 char *p, *p2;
114 struct symbol *sym;
115 int i;
116
117 if (name) {
118 in = zconf_fopen(name);
119 } else {
120 const char **names = conf_confnames;
121 while ((name = *names++)) {
122 name = conf_expand_value(name);
123 in = zconf_fopen(name);
124 if (in) {
125 printf(_("#\n"
126 "# using defaults found in %s\n"
127 "#\n"), name);
128 break;
129 }
130 }
131 }
132 if (!in)
133 return 1;
134
135 conf_filename = name;
136 conf_lineno = 0;
137 conf_warnings = 0;
138 conf_unsaved = 0;
139
140 for_all_symbols(i, sym) {
141 sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
142 if (sym_is_choice(sym))
143 sym->flags &= ~SYMBOL_NEW;
144 sym->flags &= ~SYMBOL_VALID;
145 switch (sym->type) {
146 case S_INT:
147 case S_HEX:
148 case S_STRING:
149 if (sym->user.val)
150 free(sym->user.val);
151 default:
152 sym->user.val = NULL;
153 sym->user.tri = no;
154 }
155 }
156
157 while (fgets(line, sizeof(line), in)) {
158 conf_lineno++;
159 sym = NULL;
160 switch (line[0]) {
161 case '#':
162 if (memcmp(line + 2, "CONFIG_", 7))
163 continue;
164 p = strchr(line + 9, ' ');
165 if (!p)
166 continue;
167 *p++ = 0;
168 if (strncmp(p, "is not set", 10))
169 continue;
170 sym = sym_find(line + 9);
171 if (!sym) {
172 conf_warning("trying to assign nonexistent symbol %s", line + 9);
173 break;
174 } else if (!(sym->flags & SYMBOL_NEW)) {
175 conf_warning("trying to reassign symbol %s", sym->name);
176 break;
177 }
178 switch (sym->type) {
179 case S_BOOLEAN:
180 case S_TRISTATE:
181 sym->user.tri = no;
182 sym->flags &= ~SYMBOL_NEW;
183 break;
184 default:
185 ;
186 }
187 break;
188 case 'C':
189 if (memcmp(line, "CONFIG_", 7)) {
190 conf_warning("unexpected data");
191 continue;
192 }
193 p = strchr(line + 7, '=');
194 if (!p)
195 continue;
196 *p++ = 0;
197 p2 = strchr(p, '\n');
198 if (p2)
199 *p2 = 0;
200 sym = sym_find(line + 7);
201 if (!sym) {
202 conf_warning("trying to assign nonexistent symbol %s", line + 7);
203 break;
204 } else if (!(sym->flags & SYMBOL_NEW)) {
205 conf_warning("trying to reassign symbol %s", sym->name);
206 break;
207 }
208 switch (sym->type) {
209 case S_TRISTATE:
210 if (p[0] == 'm') {
211 sym->user.tri = mod;
212 sym->flags &= ~SYMBOL_NEW;
213 break;
214 }
215 case S_BOOLEAN:
216 if (p[0] == 'y') {
217 sym->user.tri = yes;
218 sym->flags &= ~SYMBOL_NEW;
219 break;
220 }
221 if (p[0] == 'n') {
222 sym->user.tri = no;
223 sym->flags &= ~SYMBOL_NEW;
224 break;
225 }
226 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
227 break;
228 case S_STRING:
229 if (*p++ != '"')
230 break;
231 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
232 if (*p2 == '"') {
233 *p2 = 0;
234 break;
235 }
236 memmove(p2, p2 + 1, strlen(p2));
237 }
238 if (!p2) {
239 conf_warning("invalid string found");
240 continue;
241 }
242 case S_INT:
243 case S_HEX:
244 if (sym_string_valid(sym, p)) {
245 sym->user.val = strdup(p);
246 sym->flags &= ~SYMBOL_NEW;
247 } else {
248 conf_warning("symbol value '%s' invalid for %s", p, sym->name);
249 continue;
250 }
251 break;
252 default:
253 ;
254 }
255 break;
256 case '\n':
257 break;
258 default:
259 conf_warning("unexpected data");
260 continue;
261 }
262 if (sym && sym_is_choice_value(sym)) {
263 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
264 switch (sym->user.tri) {
265 case no:
266 break;
267 case mod:
268 if (cs->user.tri == yes) {
269 conf_warning("%s creates inconsistent choice state", sym->name);
270 cs->flags |= SYMBOL_NEW;
271 }
272 break;
273 case yes:
274 if (cs->user.tri != no) {
275 conf_warning("%s creates inconsistent choice state", sym->name);
276 cs->flags |= SYMBOL_NEW;
277 } else
278 cs->user.val = sym;
279 break;
280 }
281 cs->user.tri = E_OR(cs->user.tri, sym->user.tri);
282 }
283 }
284 fclose(in);
285
286 if (modules_sym)
287 sym_calc_value(modules_sym);
288 return 0;
289 }
290
291 int conf_read(const char *name)
292 {
293 struct symbol *sym;
294 struct property *prop;
295 struct expr *e;
296 int i;
297
298 if (conf_read_simple(name))
299 return 1;
300
301 for_all_symbols(i, sym) {
302 sym_calc_value(sym);
303 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
304 goto sym_ok;
305 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
306 /* check that calculated value agrees with saved value */
307 switch (sym->type) {
308 case S_BOOLEAN:
309 case S_TRISTATE:
310 if (sym->user.tri != sym_get_tristate_value(sym))
311 break;
312 if (!sym_is_choice(sym))
313 goto sym_ok;
314 default:
315 if (!strcmp(sym->curr.val, sym->user.val))
316 goto sym_ok;
317 break;
318 }
319 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
320 /* no previous value and not saved */
321 goto sym_ok;
322 conf_unsaved++;
323 /* maybe print value in verbose mode... */
324 sym_ok:
325 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
326 if (sym->visible == no)
327 sym->flags |= SYMBOL_NEW;
328 switch (sym->type) {
329 case S_STRING:
330 case S_INT:
331 case S_HEX:
332 if (!sym_string_within_range(sym, sym->user.val)) {
333 sym->flags |= SYMBOL_NEW;
334 sym->flags &= ~SYMBOL_VALID;
335 }
336 default:
337 break;
338 }
339 }
340 if (!sym_is_choice(sym))
341 continue;
342 prop = sym_get_choice_prop(sym);
343 for (e = prop->expr; e; e = e->left.expr)
344 if (e->right.sym->visible != no)
345 sym->flags |= e->right.sym->flags & SYMBOL_NEW;
346 }
347
348 sym_change_count = conf_warnings && conf_unsaved;
349
350 return 0;
351 }
352
353 int conf_write(const char *name)
354 {
355 FILE *out, *out_h;
356 struct symbol *sym;
357 struct menu *menu;
358 const char *basename;
359 char dirname[128], tmpname[128], newname[128];
360 int type, l;
361 const char *str;
362 time_t now;
363 int use_timestamp = 1;
364 char *env;
365
366 dirname[0] = 0;
367 if (name && name[0]) {
368 struct stat st;
369 char *slash;
370
371 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
372 strcpy(dirname, name);
373 strcat(dirname, "/");
374 basename = conf_def_filename;
375 } else if ((slash = strrchr(name, '/'))) {
376 int size = slash - name + 1;
377 memcpy(dirname, name, size);
378 dirname[size] = 0;
379 if (slash[1])
380 basename = slash + 1;
381 else
382 basename = conf_def_filename;
383 } else
384 basename = name;
385 } else
386 basename = conf_def_filename;
387
388 sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
389 out = fopen(newname, "w");
390 if (!out)
391 return 1;
392 out_h = NULL;
393 if (!name) {
394 out_h = fopen(".tmpconfig.h", "w");
395 if (!out_h)
396 return 1;
397 }
398 sym = sym_lookup("OPENWRTVERSION", 0);
399 sym_calc_value(sym);
400 time(&now);
401 env = getenv("KCONFIG_NOTIMESTAMP");
402 if (env && *env)
403 use_timestamp = 0;
404
405 fprintf(out, _("#\n"
406 "# Automatically generated make config: don't edit\n"
407 "# OpenWrt version: %s\n"
408 "%s%s"
409 "#\n"),
410 sym_get_string_value(sym),
411 use_timestamp ? "# " : "",
412 use_timestamp ? ctime(&now) : "");
413 if (out_h)
414 fprintf(out_h, "/*\n"
415 " * Automatically generated C config: don't edit\n"
416 " * OpenWrt version: %s\n"
417 "%s%s"
418 " */\n"
419 "#define AUTOCONF_INCLUDED\n",
420 sym_get_string_value(sym),
421 use_timestamp ? " * " : "",
422 use_timestamp ? ctime(&now) : "");
423
424 if (!sym_change_count)
425 sym_clear_all_valid();
426
427 menu = rootmenu.list;
428 while (menu) {
429 sym = menu->sym;
430 if (!sym) {
431 if (!menu_is_visible(menu))
432 goto next;
433 str = menu_get_prompt(menu);
434 fprintf(out, "\n"
435 "#\n"
436 "# %s\n"
437 "#\n", str);
438 if (out_h)
439 fprintf(out_h, "\n"
440 "/*\n"
441 " * %s\n"
442 " */\n", str);
443 } else if (!(sym->flags & SYMBOL_CHOICE)) {
444 sym_calc_value(sym);
445 if (!(sym->flags & SYMBOL_WRITE))
446 goto next;
447 sym->flags &= ~SYMBOL_WRITE;
448 type = sym->type;
449 if (type == S_TRISTATE) {
450 sym_calc_value(modules_sym);
451 /* tristate always enabled */
452 #if 0
453 if (modules_sym->curr.tri == no)
454 type = S_BOOLEAN;
455 #endif
456 }
457 switch (type) {
458 case S_BOOLEAN:
459 case S_TRISTATE:
460 switch (sym_get_tristate_value(sym)) {
461 case no:
462 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
463 if (out_h)
464 fprintf(out_h, "#undef CONFIG_%s\n", sym->name);
465 break;
466 case mod:
467 fprintf(out, "CONFIG_%s=m\n", sym->name);
468 if (out_h)
469 fprintf(out_h, "#define CONFIG_%s_MODULE 1\n", sym->name);
470 break;
471 case yes:
472 fprintf(out, "CONFIG_%s=y\n", sym->name);
473 if (out_h)
474 fprintf(out_h, "#define CONFIG_%s 1\n", sym->name);
475 break;
476 }
477 break;
478 case S_STRING:
479 // fix me
480 str = sym_get_string_value(sym);
481 fprintf(out, "CONFIG_%s=\"", sym->name);
482 if (out_h)
483 fprintf(out_h, "#define CONFIG_%s \"", sym->name);
484 do {
485 l = strcspn(str, "\"\\");
486 if (l) {
487 fwrite(str, l, 1, out);
488 if (out_h)
489 fwrite(str, l, 1, out_h);
490 }
491 str += l;
492 while (*str == '\\' || *str == '"') {
493 fprintf(out, "\\%c", *str);
494 if (out_h)
495 fprintf(out_h, "\\%c", *str);
496 str++;
497 }
498 } while (*str);
499 fputs("\"\n", out);
500 if (out_h)
501 fputs("\"\n", out_h);
502 break;
503 case S_HEX:
504 str = sym_get_string_value(sym);
505 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
506 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
507 if (out_h)
508 fprintf(out_h, "#define CONFIG_%s 0x%s\n", sym->name, str);
509 break;
510 }
511 case S_INT:
512 str = sym_get_string_value(sym);
513 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
514 if (out_h)
515 fprintf(out_h, "#define CONFIG_%s %s\n", sym->name, str);
516 break;
517 }
518 }
519
520 next:
521 if (menu->list) {
522 menu = menu->list;
523 continue;
524 }
525 if (menu->next)
526 menu = menu->next;
527 else while ((menu = menu->parent)) {
528 if (menu->next) {
529 menu = menu->next;
530 break;
531 }
532 }
533 }
534 fclose(out);
535 if (out_h) {
536 fclose(out_h);
537 rename(".tmpconfig.h", "include/linux/autoconf.h");
538 file_write_dep(NULL);
539 }
540 if (!name || basename != conf_def_filename) {
541 if (!name)
542 name = conf_def_filename;
543 sprintf(tmpname, "%s.old", name);
544 rename(name, tmpname);
545 }
546 sprintf(tmpname, "%s%s", dirname, basename);
547 if (rename(newname, tmpname))
548 return 1;
549
550 sym_change_count = 0;
551
552 return 0;
553 }