aboutsummaryrefslogtreecommitdiffstats
path: root/android/config.c
blob: 2c37b03cb92157924a4030307988126089c97ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include "android/config.h"
#include "android/utils/path.h"

AConfig*
aconfig_node(const char *name, const char *value)
{
    AConfig *n;

    n = (AConfig*) calloc(sizeof(AConfig), 1);
    n->name = name ? name : "";
    n->value = value ? value : "";

    return n;
}

static AConfig*
_aconfig_find(AConfig *root, const char *name, int create)
{
    AConfig *node;

    for(node = root->first_child; node; node = node->next) {
        if(!strcmp(node->name, name)) return node;
    }

    if(create) {
        node = (AConfig*) calloc(sizeof(AConfig), 1);
        node->name = name;
        node->value = "";

        if(root->last_child) {
            root->last_child->next = node;
        } else {
            root->first_child = node;
        }
        root->last_child = node;
    }

    return node;
}

AConfig*
aconfig_find(AConfig *root, const char *name)
{
    return _aconfig_find(root, name, 0);
}

int
aconfig_bool(AConfig *root, const char *name, int _default)
{
    AConfig *n = _aconfig_find(root, name, 0);
    if(n == 0) {
        return _default;
    } else {
        switch(n->value[0]){
        case 'y':
        case 'Y':
        case '1':
            return 1;
        default:
            return 0;
        }
    }
}

unsigned
aconfig_unsigned(AConfig *root, const char *name, unsigned _default)
{
    AConfig *n = _aconfig_find(root, name, 0);
    if(n == 0) {
        return _default;
    } else {
        return strtoul(n->value, 0, 0);
    }
}

int
aconfig_int(AConfig *root, const char *name, int _default)
{
    AConfig *n = _aconfig_find(root, name, 0);
    if(n == 0) {
        return _default;
    } else {
        return strtol(n->value, 0, 0);
    }
}


const char*
aconfig_str(AConfig *root, const char *name, const char *_default)
{
    AConfig *n = _aconfig_find(root, name, 0);
    if(n == 0) {
        return _default;
    } else {
        return n->value;
    }
}

void
aconfig_set(AConfig *root, const char *name, const char *value)
{
    AConfig *node = _aconfig_find(root, name, 1);
    node->value = value;
}

#define T_EOF 0
#define T_TEXT 1
#define T_DOT 2
#define T_OBRACE 3
#define T_CBRACE 4

typedef struct
{
    char *data;
    char *text;
    int len;
    char next;
} cstate;


static int _lex(cstate *cs, int value)
{
    char c;
    char *s;
    char *data;

    data = cs->data;

    if(cs->next != 0) {
        c = cs->next;
        cs->next = 0;
        goto got_c;
    }

restart:
    for(;;) {
        c = *data++;
    got_c:
        if(isspace(c)) continue;

        switch(c) {
        case 0:
            return T_EOF;

        /* a sharp sign (#) starts a line comment and everything
         * behind that is ignored until the end of line
         */
        case '#':
            for(;;) {
                switch(*data) {
                case 0:
                    cs->data = data;
                    return T_EOF;
                case '\n':
                    cs->data = data + 1;
                    goto restart;
                default:
                    data++;
                }
            }
            break;

        case '.':
            cs->data = data;
            return T_DOT;

        case '{':
            cs->data = data;
            return T_OBRACE;

        case '}':
            cs->data = data;
            return T_CBRACE;

        default:
            s = data - 1;

            if(value) {
               /* if we're looking for a value, then take anything
                * until the end of line. note that sharp signs do
                * not start comments then. the result will be stripped
                * from trailing whitespace.
                */
                for(;;) {
                    if(*data == 0) {
                        cs->data = data;
                        break;
                    }
                    if(*data == '\n') {
                        cs->data = data + 1;
                        *data-- = 0;
                        break;
                    }
                    data++;
                }

                    /* strip trailing whitespace */
                while(data > s){
                    if(!isspace(*data)) break;
                    *data-- = 0;
                }

                goto got_text;
            } else {
               /* looking for a key name. we stop at whitspace,
                * EOF, of T_DOT/T_OBRACE/T_CBRACE characters.
                * note that the name can include sharp signs
                */
                for(;;) {
                    if(isspace(*data)) {
                        *data = 0;
                        cs->data = data + 1;
                        goto got_text;
                    }
                    switch(*data) {
                    case 0:
                        cs->data = data;
                        goto got_text;
                    case '.':
                    case '{':
                    case '}':
                        cs->next = *data;
                        *data = 0;
                        cs->data = data + 1;
                        goto got_text;
                    default:
                        data++;
                    }
                }
            }
        }
    }

got_text:
    cs->text = s;
    return T_TEXT;
}

#if 0
char *TOKENNAMES[] = { "EOF", "TEXT", "DOT", "OBRACE", "CBRACE" };

static int lex(cstate *cs, int value)
{
    int tok = _lex(cs, value);
    printf("TOKEN(%d) %s %s\n", value, TOKENNAMES[tok],
           tok == T_TEXT ? cs->text : "");
    return tok;
}
#else
#define lex(cs,v) _lex(cs,v)
#endif

static int parse_expr(cstate *cs, AConfig *node);

static int
parse_block(cstate *cs, AConfig *node)
{
    for(;;){
        switch(lex(cs, 0)){
        case T_TEXT:
            if(parse_expr(cs, node)) return -1;
            continue;

        case T_CBRACE:
            return 0;

        default:
            return -1;
        }
    }
}

static int
parse_expr(cstate *cs, AConfig *node)
{
        /* last token was T_TEXT */
    node = _aconfig_find(node, cs->text, 1);

    for(;;) {
        switch(lex(cs, 1)) {
        case T_DOT:
            if(lex(cs, 0) != T_TEXT) return -1;
            node = _aconfig_find(node, cs->text, 1);
            continue;

        case T_TEXT:
            node->value = cs->text;
            return 0;

        case T_OBRACE:
            return parse_block(cs, node);

        default:
            return -1;
        }
    }
}

void
aconfig_load(AConfig *root, char *data)
{
    if(data != 0) {
        cstate cs;
        cs.data = data;
        cs.next = 0;

        for(;;) {
            switch(lex(&cs, 0)){
            case T_TEXT:
                if(parse_expr(&cs, root)) return;
                break;
            default:
                return;
            }
        }
    }
}

int
aconfig_load_file(AConfig *root, const char *fn)
{
    char *data;
    data = path_load_file(fn, NULL);
    if (data == NULL)
        return -1;

    aconfig_load(root, data);
    return 0;
}


typedef struct
{
    char   buff[1024];
    char*  p;
    char*  end;
    int    fd;
} Writer;

static int
writer_init( Writer*  w, const char*  fn )
{
    w->p   = w->buff;
    w->end = w->buff + sizeof(w->buff);

    w->fd  = creat( fn, 0755 );
    if (w->fd < 0)
        return -1;

#ifdef _WIN32
    _setmode( w->fd, _O_BINARY );
#endif
    return 0;
}

static void
writer_write( Writer*  w, const char*  src, int  len )
{
    while (len > 0) {
        int  avail = w->end - w->p;

        if (avail > len)
            avail = len;

        memcpy( w->p, src, avail );
        src += avail;
        len -= avail;

        w->p += avail;
        if (w->p == w->end) {
            int ret;
            do {
                ret = write( w->fd, w->buff, w->p - w->buff );
            } while (ret < 0 && errno == EINTR);
            if (ret < 0)
                break;
            w->p = w->buff;
        }
    }
}

static void
writer_done( Writer*  w )
{
    if (w->p > w->buff) {
        int ret;
        do {
            ret = write( w->fd, w->buff, w->p - w->buff );
        } while (ret < 0 && errno == EINTR);
    }
    close( w->fd );
}

static void
writer_margin( Writer*  w, int  margin)
{
    static const char  spaces[10] = "          ";
    while (margin >= 10) {
        writer_write(w,spaces,10);
        margin -= 10;
    }
    if (margin > 0)
        writer_write(w,spaces,margin);
}

static void
writer_c(Writer*  w, char  c)
{
    writer_write(w, &c, 1);
}

static void
writer_str(Writer*  w, const char*  str)
{
    writer_write(w, str, strlen(str));
}

static void
writer_node(Writer*  w, AConfig*  node, int  margin)
{
    writer_margin(w,margin);
    writer_str(w, node->name);
    writer_c(w,' ');

    if (node->value[0]) {
        writer_str(w, node->value);
        writer_c(w,'\n');
    }
    else
    {
        AConfig*  child;

        writer_c(w, '{');
        writer_c(w, '\n');

        for (child = node->first_child; child; child = child->next)
            writer_node(w,child,margin+4);

        writer_margin(w,margin);
        writer_c(w,'}');
        writer_c(w,'\n');
    }
}

int
aconfig_save_file(AConfig *root, const char *fn)
{
    AConfig*  child;
    Writer    w[1];

    if (writer_init(w,fn) < 0)
        return -1;

    for (child = root->first_child; child; child = child->next)
        writer_node(w,child,0);

    writer_done(w);
    return 0;
}