Tcl build fix
[gumsense-br.git] / package / config / confdata.c
blob144b15c04372b6b908b1fa1e2e7b2913a72c63c5
1 /*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
6 #include <sys/stat.h>
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
13 #define LKC_DIRECT_LINK
14 #include "lkc.h"
16 const char conf_def_filename[] = ".config";
18 const char conf_defname[] = ".defconfig";
20 const char *conf_confnames[] = {
21 ".config",
22 conf_defname,
23 NULL,
26 static char *conf_expand_value(const signed char *in)
28 struct symbol *sym;
29 const signed char *src;
30 static char res_value[SYMBOL_MAXLENGTH];
31 char *dst, name[SYMBOL_MAXLENGTH];
33 res_value[0] = 0;
34 dst = name;
35 while ((src = strchr(in, '$'))) {
36 strncat(res_value, in, src - in);
37 src++;
38 dst = name;
39 while (isalnum(*src) || *src == '_')
40 *dst++ = *src++;
41 *dst = 0;
42 sym = sym_lookup(name, 0);
43 sym_calc_value(sym);
44 strcat(res_value, sym_get_string_value(sym));
45 in = src;
47 strcat(res_value, in);
49 return res_value;
52 char *conf_get_default_confname(void)
54 struct stat buf;
55 static char fullname[PATH_MAX+1];
56 char *env, *name;
58 name = conf_expand_value(conf_defname);
59 env = getenv(SRCTREE);
60 if (env) {
61 sprintf(fullname, "%s/%s", env, name);
62 if (!stat(fullname, &buf))
63 return fullname;
65 return name;
68 int conf_read(const char *name)
70 FILE *in = NULL;
71 char line[1024];
72 char *p, *p2;
73 int lineno = 0;
74 struct symbol *sym;
75 struct property *prop;
76 struct expr *e;
77 int i;
79 if (name) {
80 in = zconf_fopen(name);
81 } else {
82 const char **names = conf_confnames;
83 while ((name = *names++)) {
84 name = conf_expand_value(name);
85 in = zconf_fopen(name);
86 if (in) {
87 printf("#\n"
88 "# using defaults found in %s\n"
89 "#\n", name);
90 break;
95 if (!in)
96 return 1;
98 for_all_symbols(i, sym) {
99 sym->flags |= SYMBOL_NEW | SYMBOL_CHANGED;
100 sym->flags &= ~SYMBOL_VALID;
101 switch (sym->type) {
102 case S_INT:
103 case S_HEX:
104 case S_STRING:
105 if (sym->user.val)
106 free(sym->user.val);
107 default:
108 sym->user.val = NULL;
109 sym->user.tri = no;
113 while (fgets(line, sizeof(line), in)) {
114 lineno++;
115 sym = NULL;
116 switch (line[0]) {
117 case '#':
118 if (line[1]!=' ')
119 continue;
120 p = strchr(line + 2, ' ');
121 if (!p)
122 continue;
123 *p++ = 0;
124 if (strncmp(p, "is not set", 10))
125 continue;
126 sym = sym_find(line + 2);
127 if (!sym) {
128 fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line + 2);
129 break;
131 switch (sym->type) {
132 case S_BOOLEAN:
133 case S_TRISTATE:
134 sym->user.tri = no;
135 sym->flags &= ~SYMBOL_NEW;
136 break;
137 default:
140 break;
142 case 'A' ... 'Z':
143 p = strchr(line, '=');
144 if (!p)
145 continue;
146 *p++ = 0;
147 p2 = strchr(p, '\n');
148 if (p2)
149 *p2 = 0;
150 sym = sym_find(line);
151 if (!sym) {
152 fprintf(stderr, "%s:%d: trying to assign nonexistent symbol %s\n", name, lineno, line);
153 break;
155 switch (sym->type) {
156 case S_TRISTATE:
157 if (p[0] == 'm') {
158 sym->user.tri = mod;
159 sym->flags &= ~SYMBOL_NEW;
160 break;
162 case S_BOOLEAN:
163 if (p[0] == 'y') {
164 sym->user.tri = yes;
165 sym->flags &= ~SYMBOL_NEW;
166 break;
168 if (p[0] == 'n') {
169 sym->user.tri = no;
170 sym->flags &= ~SYMBOL_NEW;
171 break;
173 break;
174 case S_STRING:
175 if (*p++ != '"')
176 break;
177 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
178 if (*p2 == '"') {
179 *p2 = 0;
180 break;
182 memmove(p2, p2 + 1, strlen(p2));
184 if (!p2) {
185 fprintf(stderr, "%s:%d: invalid string found\n", name, lineno);
186 exit(1);
188 case S_INT:
189 case S_HEX:
190 if (sym_string_valid(sym, p)) {
191 sym->user.val = strdup(p);
192 sym->flags &= ~SYMBOL_NEW;
193 } else {
194 fprintf(stderr, "%s:%d: symbol value '%s' invalid for %s\n", name, lineno, p, sym->name);
195 exit(1);
197 break;
198 default:
201 break;
202 case '\n':
203 break;
204 default:
205 continue;
207 if (sym && sym_is_choice_value(sym)) {
208 struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
209 switch (sym->user.tri) {
210 case no:
211 break;
212 case mod:
213 if (cs->user.tri == yes)
214 /* warn? */;
215 break;
216 case yes:
217 if (cs->user.tri != no)
218 /* warn? */;
219 cs->user.val = sym;
220 break;
222 cs->user.tri = E_OR(cs->user.tri, sym->user.tri);
223 cs->flags &= ~SYMBOL_NEW;
226 fclose(in);
228 if (modules_sym)
229 sym_calc_value(modules_sym);
230 for_all_symbols(i, sym) {
231 sym_calc_value(sym);
232 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
233 if (sym->visible == no)
234 sym->flags |= SYMBOL_NEW;
235 switch (sym->type) {
236 case S_STRING:
237 case S_INT:
238 case S_HEX:
239 if (!sym_string_within_range(sym, sym->user.val))
240 sym->flags |= SYMBOL_NEW;
241 default:
242 break;
245 if (!sym_is_choice(sym))
246 continue;
247 prop = sym_get_choice_prop(sym);
248 for (e = prop->expr; e; e = e->left.expr)
249 if (e->right.sym->visible != no)
250 sym->flags |= e->right.sym->flags & SYMBOL_NEW;
253 sym_change_count = 1;
255 return 0;
258 int conf_write(const char *name)
260 FILE *out;
261 struct symbol *sym;
262 struct menu *menu;
263 const char *basename;
264 char dirname[128], tmpname[128], newname[128];
265 int type, l;
266 const char *str;
268 dirname[0] = 0;
269 if (name && name[0]) {
270 struct stat st;
271 char *slash;
273 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
274 strcpy(dirname, name);
275 strcat(dirname, "/");
276 basename = conf_def_filename;
277 } else if ((slash = strrchr(name, '/'))) {
278 int size = slash - name + 1;
279 memcpy(dirname, name, size);
280 dirname[size] = 0;
281 if (slash[1])
282 basename = slash + 1;
283 else
284 basename = conf_def_filename;
285 } else
286 basename = name;
287 } else
288 basename = conf_def_filename;
290 sprintf(newname, "%s.tmpconfig.%d", dirname, (int)getpid());
291 out = fopen(newname, "w");
292 if (!out)
293 return 1;
294 fprintf(out, "#\n"
295 "# Automatically generated make config: don't edit\n"
296 "#\n");
298 if (!sym_change_count)
299 sym_clear_all_valid();
301 menu = rootmenu.list;
302 while (menu) {
303 sym = menu->sym;
304 if (!sym) {
305 if (!menu_is_visible(menu))
306 goto next;
307 str = menu_get_prompt(menu);
308 fprintf(out, "\n"
309 "#\n"
310 "# %s\n"
311 "#\n", str);
312 } else if (!(sym->flags & SYMBOL_CHOICE)) {
313 sym_calc_value(sym);
314 if (!(sym->flags & SYMBOL_WRITE))
315 goto next;
316 sym->flags &= ~SYMBOL_WRITE;
317 type = sym->type;
318 if (type == S_TRISTATE) {
319 sym_calc_value(modules_sym);
320 if (modules_sym->curr.tri == no)
321 type = S_BOOLEAN;
323 switch (type) {
324 case S_BOOLEAN:
325 case S_TRISTATE:
326 switch (sym_get_tristate_value(sym)) {
327 case no:
328 fprintf(out, "# %s is not set\n", sym->name);
329 break;
330 case mod:
331 fprintf(out, "%s=m\n", sym->name);
332 break;
333 case yes:
334 fprintf(out, "%s=y\n", sym->name);
335 break;
337 break;
338 case S_STRING:
339 // fix me
340 str = sym_get_string_value(sym);
341 fprintf(out, "%s=\"", sym->name);
342 do {
343 l = strcspn(str, "\"\\");
344 if (l) {
345 fwrite(str, l, 1, out);
347 str += l;
348 while (*str == '\\' || *str == '"') {
349 fprintf(out, "\\%c", *str);
350 str++;
352 } while (*str);
353 fputs("\"\n", out);
354 break;
355 case S_HEX:
356 str = sym_get_string_value(sym);
357 if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
358 fprintf(out, "%s=%s\n", sym->name, str);
359 break;
361 case S_INT:
362 str = sym_get_string_value(sym);
363 fprintf(out, "%s=%s\n", sym->name, str);
364 break;
368 next:
369 if (menu->list) {
370 menu = menu->list;
371 continue;
373 if (menu->next)
374 menu = menu->next;
375 else while ((menu = menu->parent)) {
376 if (menu->next) {
377 menu = menu->next;
378 break;
382 fclose(out);
383 file_write_dep(NULL);
384 if (!name || basename != conf_def_filename) {
385 if (!name)
386 name = conf_def_filename;
387 sprintf(tmpname, "%s.old", name);
388 rename(name, tmpname);
390 sprintf(tmpname, "%s%s", dirname, basename);
391 if (rename(newname, tmpname))
392 return 1;
394 sym_change_count = 0;
396 return 0;