OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [extra/] [config/] [confdata.c] - Blame information for rev 1771

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*
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 <unistd.h>
12
 
13
#define LKC_DIRECT_LINK
14
#include "lkc.h"
15
 
16
const char conf_def_filename[] = ".config";
17
 
18
const char conf_defname[] = "extra/Configs/Config.default";
19
 
20
const char *conf_confnames[] = {
21
        ".config",
22
        conf_defname,
23
        NULL,
24
};
25
 
26
static char *conf_expand_value(const char *in)
27
{
28
        struct symbol *sym;
29
        const char *src;
30
        static char res_value[SYMBOL_MAXLENGTH];
31
        char *dst, name[SYMBOL_MAXLENGTH];
32
 
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;
46
        }
47
        strcat(res_value, in);
48
 
49
        return res_value;
50
}
51
 
52
char *conf_get_default_confname(void)
53
{
54
        struct stat buf;
55
        static char fullname[4096+1];
56
        char *env, *name;
57
 
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;
64
        }
65
        return name;
66
}
67
 
68
int conf_read(const char *name)
69
{
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;
78
 
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;
91
                        }
92
                }
93
        }
94
 
95
        if (!in)
96
                return 1;
97
 
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;
110
                }
111
        }
112
 
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;
130
                        }
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:
138
                                ;
139
                        }
140
                        break;
141
 
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;
154
                        }
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;
161
                                }
162
                        case S_BOOLEAN:
163
                                if (p[0] == 'y') {
164
                                        sym->user.tri = yes;
165
                                        sym->flags &= ~SYMBOL_NEW;
166
                                        break;
167
                                }
168
                                if (p[0] == 'n') {
169
                                        sym->user.tri = no;
170
                                        sym->flags &= ~SYMBOL_NEW;
171
                                        break;
172
                                }
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;
181
                                        }
182
                                        memmove(p2, p2 + 1, strlen(p2));
183
                                }
184
                                if (!p2) {
185
                                        fprintf(stderr, "%s:%d: invalid string found\n", name, lineno);
186
                                        exit(1);
187
                                }
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);
196
                                }
197
                                break;
198
                        default:
199
                                ;
200
                        }
201
                        break;
202
                case '\n':
203
                        break;
204
                default:
205
                        continue;
206
                }
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;
221
                        }
222
                        cs->user.tri = E_OR(cs->user.tri, sym->user.tri);
223
                        cs->flags &= ~SYMBOL_NEW;
224
                }
225
        }
226
        fclose(in);
227
 
228
        for_all_symbols(i, sym) {
229
                sym_calc_value(sym);
230
                if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
231
                        if (sym->visible == no)
232
                                sym->flags |= SYMBOL_NEW;
233
                        switch (sym->type) {
234
                        case S_STRING:
235
                        case S_INT:
236
                        case S_HEX:
237
                                if (!sym_string_within_range(sym, sym->user.val))
238
                                        sym->flags |= SYMBOL_NEW;
239
                        default:
240
                                break;
241
                        }
242
                }
243
                if (!sym_is_choice(sym))
244
                        continue;
245
                prop = sym_get_choice_prop(sym);
246
                for (e = prop->expr; e; e = e->left.expr)
247
                        if (e->right.sym->visible != no)
248
                                sym->flags |= e->right.sym->flags & SYMBOL_NEW;
249
        }
250
 
251
        sym_change_count = 1;
252
 
253
        return 0;
254
}
255
 
256
int conf_write(const char *name)
257
{
258
        FILE *out, *out_h;
259
        struct symbol *sym;
260
        struct menu *menu;
261
        const char *basename;
262
        char dirname[128], tmpname[128], newname[128];
263
        int type, l;
264
        const char *str;
265
 
266
        dirname[0] = 0;
267
        if (name && name[0]) {
268
                char *slash = strrchr(name, '/');
269
                if (slash) {
270
                        int size = slash - name + 1;
271
                        memcpy(dirname, name, size);
272
                        dirname[size] = 0;
273
                        if (slash[1])
274
                                basename = slash + 1;
275
                        else
276
                                basename = conf_def_filename;
277
                } else
278
                        basename = name;
279
        } else
280
                basename = conf_def_filename;
281
 
282
        sprintf(newname, "%s.tmpconfig.%d", dirname, getpid());
283
        out = fopen(newname, "w");
284
        if (!out)
285
                return 1;
286
        out_h = NULL;
287
        if (!name) {
288
                out_h = fopen(".tmpconfig.h", "w");
289
                if (!out_h)
290
                        return 1;
291
        }
292
        fprintf(out, "#\n"
293
                     "# Automatically generated make config: don't edit\n"
294
                     "#\n");
295
        if (out_h)
296
                fprintf(out_h, "/*\n"
297
                             " * Automatically generated C config: don't edit\n"
298
                             " */\n"
299
                             "#if !defined __FEATURES_H && !defined __need_uClibc_config_h\n"
300
                             "#error Never include <bits/uClibc_config.h> directly; use <features.h> instead\n"
301
                             "#endif\n"
302
                             "#define AUTOCONF_INCLUDED\n\n"
303
                             "/*\n"
304
                             " * Version Number\n"
305
                             " */\n"
306
                             "#define __UCLIBC_MAJOR__ %s\n"
307
                             "#define __UCLIBC_MINOR__ %s\n"
308
                             "#define __UCLIBC_SUBLEVEL__ %s\n",
309
                             getenv("MAJOR_VERSION"),
310
                             getenv("MINOR_VERSION"),
311
                             getenv("SUBLEVEL"));
312
 
313
        if (!sym_change_count)
314
                sym_clear_all_valid();
315
 
316
        menu = rootmenu.list;
317
        while (menu) {
318
                sym = menu->sym;
319
                if (!sym) {
320
                        if (!menu_is_visible(menu))
321
                                goto next;
322
                        str = menu_get_prompt(menu);
323
                        fprintf(out, "\n"
324
                                     "#\n"
325
                                     "# %s\n"
326
                                     "#\n", str);
327
                        if (out_h)
328
                                fprintf(out_h, "\n"
329
                                               "/*\n"
330
                                               " * %s\n"
331
                                               " */\n", str);
332
                } else if (!(sym->flags & SYMBOL_CHOICE)) {
333
                        sym_calc_value(sym);
334
                        if (!(sym->flags & SYMBOL_WRITE))
335
                                goto next;
336
                        sym->flags &= ~SYMBOL_WRITE;
337
                        type = sym->type;
338
                        if (type == S_TRISTATE) {
339
                                sym_calc_value(modules_sym);
340
                                if (modules_sym->curr.tri == no)
341
                                        type = S_BOOLEAN;
342
                        }
343
                        switch (type) {
344
                        case S_BOOLEAN:
345
                        case S_TRISTATE:
346
                                switch (sym_get_tristate_value(sym)) {
347
                                case no:
348
                                        fprintf(out, "# %s is not set\n", sym->name);
349
                                        if (out_h)
350
                                                fprintf(out_h, "#undef __%s__\n", sym->name);
351
                                        break;
352
                                case mod:
353
                                        fprintf(out, "%s=m\n", sym->name);
354
                                        if (out_h)
355
                                                fprintf(out_h, "#define %s_MODULE 1\n", sym->name);
356
                                        break;
357
                                case yes:
358
                                        fprintf(out, "%s=y\n", sym->name);
359
                                        if (out_h)
360
                                                fprintf(out_h, "#define __%s__ 1\n", sym->name);
361
                                        break;
362
                                }
363
                                break;
364
                        case S_STRING:
365
                                // fix me
366
                                str = sym_get_string_value(sym);
367
                                fprintf(out, "%s=\"", sym->name);
368
                                if (out_h)
369
                                        fprintf(out_h, "#define __%s__ \"", sym->name);
370
                                do {
371
                                        l = strcspn(str, "\"\\");
372
                                        if (l) {
373
                                                fwrite(str, l, 1, out);
374
                                                if (out_h)
375
                                                        fwrite(str, l, 1, out_h);
376
                                        }
377
                                        str += l;
378
                                        while (*str == '\\' || *str == '"') {
379
                                                fprintf(out, "\\%c", *str);
380
                                                if (out_h)
381
                                                        fprintf(out_h, "\\%c", *str);
382
                                                str++;
383
                                        }
384
                                } while (*str);
385
                                fputs("\"\n", out);
386
                                if (out_h)
387
                                        fputs("\"\n", out_h);
388
                                break;
389
                        case S_HEX:
390
                                str = sym_get_string_value(sym);
391
                                if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
392
                                        fprintf(out, "%s=%s\n", sym->name, str);
393
                                        if (out_h)
394
                                                fprintf(out_h, "#define __%s__ 0x%s\n", sym->name, str);
395
                                        break;
396
                                }
397
                        case S_INT:
398
                                str = sym_get_string_value(sym);
399
                                fprintf(out, "%s=%s\n", sym->name, str);
400
                                if (out_h)
401
                                        fprintf(out_h, "#define __%s__ %s\n", sym->name, str);
402
                                break;
403
                        }
404
                }
405
 
406
        next:
407
                if (menu->list) {
408
                        menu = menu->list;
409
                        continue;
410
                }
411
                if (menu->next)
412
                        menu = menu->next;
413
                else while ((menu = menu->parent)) {
414
                        if (menu->next) {
415
                                menu = menu->next;
416
                                break;
417
                        }
418
                }
419
        }
420
        fclose(out);
421
        if (out_h) {
422
                fclose(out_h);
423
                rename(".tmpconfig.h", "include/bits/uClibc_config.h");
424
                file_write_dep(NULL);
425
        }
426
        if (!name || basename != conf_def_filename) {
427
                if (!name)
428
                        name = conf_def_filename;
429
                sprintf(tmpname, "%s.old", name);
430
                rename(name, tmpname);
431
        }
432
        sprintf(tmpname, "%s%s", dirname, basename);
433
        if (rename(newname, tmpname))
434
                return 1;
435
 
436
        sym_change_count = 0;
437
 
438
        return 0;
439
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.