add initial version of a package feeds management script
[openwrt.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;
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 sym = sym_lookup("OPENWRTVERSION", 0);
393 sym_calc_value(sym);
394 time(&now);
395 env = getenv("KCONFIG_NOTIMESTAMP");
396 if (env && *env)
397 use_timestamp = 0;
398
399 fprintf(out, _("#\n"
400 "# Automatically generated make config: don't edit\n"
401 "# OpenWrt version: %s\n"
402 "%s%s"
403 "#\n"),
404 sym_get_string_value(sym),
405 use_timestamp ? "# " : "",
406 use_timestamp ? ctime(&now) : "");
407
408 if (!sym_change_count)
409 sym_clear_all_valid();
410
411 menu = rootmenu.list;
412 while (menu) {
413 sym = menu->sym;
414 if (!sym) {
415 if (!menu_is_visible(menu))
416 goto next;
417 str = menu_get_prompt(menu);
418 fprintf(out, "\n"
419 "#\n"
420 "# %s\n"
421 "#\n", str);
422 } else if (!(sym->flags & SYMBOL_CHOICE)) {
423 sym_calc_value(sym);
424 if (!(sym->flags & SYMBOL_WRITE))
425 goto next;
426 sym->flags &= ~SYMBOL_WRITE;
427 type = sym->type;
428 if (type == S_TRISTATE) {
429 sym_calc_value(modules_sym);
430 /* tristate always enabled */
431 #if 0
432 if (modules_sym->curr.tri == no)
433 type = S_BOOLEAN;
434 #endif
435 }
436 switch (type) {
437 case S_BOOLEAN:
438 case S_TRISTATE:
439 switch (sym_get_tristate_value(sym)) {
440 case no:
441 fprintf(out, "# CONFIG_%s is not set\n", sym->name);
442 break;
443 case mod:
444 fprintf(out, "CONFIG_%s=m\n", sym->name);
445 break;
446 case yes:
447 fprintf(out, "CONFIG_%s=y\n", sym->name);
448 break;
449 }
450 break;
451 case S_STRING:
452 // fix me
453 str = sym_get_string_value(sym);
454 fprintf(out, "CONFIG_%s=\"", sym->name);
455 do {
456 l = strcspn(str, "\"\\");
457 if (l) {
458 fwrite(str, l, 1, out);
459 }
460 str += l;
461 while (*str == '\\' || *str == '"') {
462 fprintf(out, "\\%c", *str);
463 str++;
464 }
465 } while (*str);
466 fputs("\"\n", out);
467 break;
468 case S_HEX:
469 str = sym_get_string_value(sym);
470 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
471 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
472 break;
473 }
474 case S_INT:
475 str = sym_get_string_value(sym);
476 fprintf(out, "CONFIG_%s=%s\n", sym->name, str);
477 break;
478 }
479 }
480
481 next:
482 if (menu->list) {
483 menu = menu->list;
484 continue;
485 }
486 if (menu->next)
487 menu = menu->next;
488 else while ((menu = menu->parent)) {
489 if (menu->next) {
490 menu = menu->next;
491 break;
492 }
493 }
494 }
495 fclose(out);
496 if (!name || basename != conf_def_filename) {
497 if (!name)
498 name = conf_def_filename;
499 sprintf(tmpname, "%s.old", name);
500 rename(name, tmpname);
501 }
502 sprintf(tmpname, "%s%s", dirname, basename);
503 if (rename(newname, tmpname))
504 return 1;
505
506 sym_change_count = 0;
507
508 return 0;
509 }
This page took 0.075786 seconds and 5 git commands to generate.