aboutsummaryrefslogtreecommitdiff
path: root/src/util/profile/prof_parse.c
blob: c581fb722247e8265c3fa94e16431086afb859b0 (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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "prof_int.h"

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <errno.h>
#include <ctype.h>
#ifndef _WIN32
#include <dirent.h>
#endif

#define SECTION_SEP_CHAR '/'

#define STATE_INIT_COMMENT      1
#define STATE_STD_LINE          2
#define STATE_GET_OBRACE        3

struct parse_state {
    int state;
    int group_level;
    int discard;                /* group_level of a final-flagged section */
    struct profile_node *root_section;
    struct profile_node *current_section;
};

static errcode_t parse_file(FILE *f, struct parse_state *state,
                            char **ret_modspec);

static char *skip_over_blanks(char *cp)
{
    while (*cp && isspace((int) (*cp)))
        cp++;
    return cp;
}

static void strip_line(char *line)
{
    char *p = line + strlen(line);
    while (p > line && (p[-1] == '\n' || p[-1] == '\r'))
        *--p = 0;
}

static void parse_quoted_string(char *str)
{
    char *to, *from;

    for (to = from = str; *from && *from != '"'; to++, from++) {
        if (*from == '\\' && *(from + 1) != '\0') {
            from++;
            switch (*from) {
            case 'n':
                *to = '\n';
                break;
            case 't':
                *to = '\t';
                break;
            case 'b':
                *to = '\b';
                break;
            default:
                *to = *from;
            }
            continue;
        }
        *to = *from;
    }
    *to = '\0';
}


static errcode_t parse_std_line(char *line, struct parse_state *state)
{
    char    *cp, ch, *tag, *value;
    char    *p;
    errcode_t retval;
    struct profile_node     *node;
    int do_subsection = 0;

    if (*line == 0)
        return 0;
    cp = skip_over_blanks(line);
    if (cp[0] == ';' || cp[0] == '#')
        return 0;
    strip_line(cp);
    ch = *cp;
    if (ch == 0)
        return 0;
    if (ch == '[') {
        if (state->group_level > 1)
            return PROF_SECTION_NOTOP;
        cp++;
        p = strchr(cp, ']');
        if (p == NULL)
            return PROF_SECTION_SYNTAX;
        *p = '\0';
        retval = profile_add_node(state->root_section, cp, NULL, 0,
                                  &state->current_section);
        if (retval)
            return retval;
        state->group_level = 1;
        /* If we previously saw this section name with the final flag,
         * discard values until the next top-level section. */
        state->discard = profile_is_node_final(state->current_section) ?
            1 : 0;

        /*
         * Finish off the rest of the line.
         */
        cp = p+1;
        if (*cp == '*') {
            profile_make_node_final(state->current_section);
            cp++;
        }
        /*
         * A space after ']' should not be fatal
         */
        cp = skip_over_blanks(cp);
        if (*cp)
            return PROF_SECTION_SYNTAX;
        return 0;
    }
    if (ch == '}') {
        if (state->group_level == 0)
            return PROF_EXTRA_CBRACE;
        if (*(cp+1) == '*')
            profile_make_node_final(state->current_section);
        retval = profile_get_node_parent(state->current_section,
                                         &state->current_section);
        if (retval)
            return retval;
        state->group_level--;
        /* Check if we are done discarding values from a subsection. */
        if (state->group_level < state->discard)
            state->discard = 0;
        return 0;
    }
    /*
     * Parse the relations
     */
    tag = cp;
    cp = strchr(cp, '=');
    if (!cp)
        return PROF_RELATION_SYNTAX;
    if (cp == tag)
        return PROF_RELATION_SYNTAX;
    *cp = '\0';
    p = tag;
    /* Look for whitespace on left-hand side.  */
    while (p < cp && !isspace((int)*p))
        p++;
    if (p < cp) {
        /* Found some sort of whitespace.  */
        *p++ = 0;
        /* If we have more non-whitespace, it's an error.  */
        while (p < cp) {
            if (!isspace((int)*p))
                return PROF_RELATION_SYNTAX;
            p++;
        }
    }
    cp = skip_over_blanks(cp+1);
    value = cp;
    if (value[0] == '"') {
        value++;
        parse_quoted_string(value);
    } else if (value[0] == 0) {
        do_subsection++;
        state->state = STATE_GET_OBRACE;
    } else if (value[0] == '{' && *(skip_over_blanks(value+1)) == 0)
        do_subsection++;
    else {
        cp = value + strlen(value) - 1;
        while ((cp > value) && isspace((int) (*cp)))
            *cp-- = 0;
    }
    if (do_subsection) {
        p = strchr(tag, '*');
        if (p)
            *p = '\0';
        state->group_level++;
        if (!state->discard) {
            retval = profile_add_node(state->current_section, tag, NULL, 0,
                                      &state->current_section);
            if (retval)
                return retval;
            /* If we previously saw this subsection with the final flag,
             * discard values until the subsection is done. */
            if (profile_is_node_final(state->current_section))
                state->discard = state->group_level;
            if (p)
                profile_make_node_final(state->current_section);
        }
        return 0;
    }
    p = strchr(tag, '*');
    if (p)
        *p = '\0';
    if (!state->discard) {
        profile_add_node(state->current_section, tag, value, 1, &node);
        if (p && node)
            profile_make_node_final(node);
    }
    return 0;
}

/* Open and parse an included profile file. */
static errcode_t parse_include_file(const char *filename,
                                    struct profile_node *root_section)
{
    FILE    *fp;
    errcode_t retval = 0;
    struct parse_state state;

    /* Create a new state so that fragments are syntactically independent but
     * share a root section. */
    state.state = STATE_INIT_COMMENT;
    state.group_level = state.discard = 0;
    state.root_section = root_section;
    state.current_section = NULL;

    fp = fopen(filename, "r");
    if (fp == NULL)
        return PROF_FAIL_INCLUDE_FILE;
    retval = parse_file(fp, &state, NULL);
    fclose(fp);
    return retval;
}

/* Return non-zero if filename contains only alphanumeric characters, dashes,
 * and underscores, or if the filename ends in ".conf" and is not a dotfile. */
static int valid_name(const char *filename)
{
    const char *p;
    size_t len = strlen(filename);

    /* Ignore dotfiles, which might be editor or filesystem artifacts. */
    if (*filename == '.')
        return 0;

    if (len >= 5 && !strcmp(filename + len - 5, ".conf"))
        return 1;

    for (p = filename; *p != '\0'; p++) {
        if (!isalnum((unsigned char)*p) && *p != '-' && *p != '_')
            return 0;
    }
    return 1;
}

/*
 * Include files within dirname.  Only files with names ending in ".conf", or
 * consisting entirely of alphanumeric characters, dashes, and underscores are
 * included.  This restriction avoids including editor backup files, .rpmsave
 * files, and the like.  Files are processed in alphanumeric order.
 */
static errcode_t parse_include_dir(const char *dirname,
                                   struct profile_node *root_section)
{
    errcode_t retval = 0;
    char **fnames, *pathname;
    int i;

    if (k5_dir_filenames(dirname, &fnames) != 0)
        return PROF_FAIL_INCLUDE_DIR;

    for (i = 0; fnames != NULL && fnames[i] != NULL; i++) {
        if (!valid_name(fnames[i]))
            continue;
        if (asprintf(&pathname, "%s/%s", dirname, fnames[i]) < 0) {
            retval = ENOMEM;
            break;
        }
        retval = parse_include_file(pathname, root_section);
        free(pathname);
        if (retval)
            break;
    }
    k5_free_filenames(fnames);
    return retval;
}

static errcode_t parse_line(char *line, struct parse_state *state,
                            char **ret_modspec)
{
    char    *cp;

    if (strncmp(line, "include", 7) == 0 && isspace(line[7])) {
        cp = skip_over_blanks(line + 7);
        strip_line(cp);
        return parse_include_file(cp, state->root_section);
    }
    if (strncmp(line, "includedir", 10) == 0 && isspace(line[10])) {
        cp = skip_over_blanks(line + 10);
        strip_line(cp);
        return parse_include_dir(cp, state->root_section);
    }
    switch (state->state) {
    case STATE_INIT_COMMENT:
        if (strncmp(line, "module", 6) == 0 && isspace(line[6])) {
            /*
             * If we are expecting a module declaration, fill in *ret_modspec
             * and return PROF_MODULE, which will cause parsing to abort and
             * the module to be loaded instead.  If we aren't expecting a
             * module declaration, return PROF_MODULE without filling in
             * *ret_modspec, which will be treated as an ordinary error.
             */
            if (ret_modspec) {
                cp = skip_over_blanks(line + 6);
                strip_line(cp);
                *ret_modspec = strdup(cp);
                if (!*ret_modspec)
                    return ENOMEM;
            }
            return PROF_MODULE;
        }
        if (line[0] != '[')
            return 0;
        state->state = STATE_STD_LINE;
    case STATE_STD_LINE:
        return parse_std_line(line, state);
    case STATE_GET_OBRACE:
        cp = skip_over_blanks(line);
        if (*cp != '{')
            return PROF_MISSING_OBRACE;
        state->state = STATE_STD_LINE;
    }
    return 0;
}

static errcode_t parse_file(FILE *f, struct parse_state *state,
                            char **ret_modspec)
{
#define BUF_SIZE        2048
    char *bptr;
    errcode_t retval;

    bptr = malloc (BUF_SIZE);
    if (!bptr)
        return ENOMEM;

    while (!feof(f)) {
        if (fgets(bptr, BUF_SIZE, f) == NULL)
            break;
#ifndef PROFILE_SUPPORTS_FOREIGN_NEWLINES
        retval = parse_line(bptr, state, ret_modspec);
        if (retval) {
            free (bptr);
            return retval;
        }
#else
        {
            char *p, *end;

            if (strlen(bptr) >= BUF_SIZE - 1) {
                /* The string may have foreign newlines and
                   gotten chopped off on a non-newline
                   boundary.  Seek backwards to the last known
                   newline.  */
                long offset;
                char *c = bptr + strlen (bptr);
                for (offset = 0; offset > -BUF_SIZE; offset--) {
                    if (*c == '\r' || *c == '\n') {
                        *c = '\0';
                        fseek (f, offset, SEEK_CUR);
                        break;
                    }
                    c--;
                }
            }

            /* First change all newlines to \n */
            for (p = bptr; *p != '\0'; p++) {
                if (*p == '\r')
                    *p = '\n';
            }
            /* Then parse all lines */
            p = bptr;
            end = bptr + strlen (bptr);
            while (p < end) {
                char* newline;
                char* newp;

                newline = strchr (p, '\n');
                if (newline != NULL)
                    *newline = '\0';

                /* parse_line modifies contents of p */
                newp = p + strlen (p) + 1;
                retval = parse_line (p, state, ret_modspec);
                if (retval) {
                    free (bptr);
                    return retval;
                }

                p = newp;
            }
        }
#endif
    }

    free (bptr);
    return 0;
}

errcode_t profile_parse_file(FILE *f, struct profile_node **root,
                             char **ret_modspec)
{
    struct parse_state state;
    errcode_t retval;

    *root = NULL;

    /* Initialize parsing state with a new root node. */
    state.state = STATE_INIT_COMMENT;
    state.group_level = state.discard = 0;
    state.current_section = NULL;
    retval = profile_create_node("(root)", 0, &state.root_section);
    if (retval)
        return retval;

    retval = parse_file(f, &state, ret_modspec);
    if (retval) {
        profile_free_node(state.root_section);
        return retval;
    }
    *root = state.root_section;
    return 0;
}

errcode_t profile_process_directory(const char *dirname,
                                    struct profile_node **root)
{
    errcode_t retval;
    struct profile_node *node;

    *root = NULL;
    retval = profile_create_node("(root)", 0, &node);
    if (retval)
        return retval;
    retval = parse_include_dir(dirname, node);
    if (retval) {
        profile_free_node(node);
        return retval;
    }
    *root = node;
    return 0;
}

/*
 * Return TRUE if the string begins or ends with whitespace
 */
static int need_double_quotes(char *str)
{
    if (!str)
        return 0;
    if (str[0] == '\0')
        return 1;
    if (isspace((int) (*str)) ||isspace((int) (*(str + strlen(str) - 1))))
        return 1;
    if (strchr(str, '\n') || strchr(str, '\t') || strchr(str, '\b'))
        return 1;
    return 0;
}

/*
 * Output a string with double quotes, doing appropriate backquoting
 * of characters as necessary.
 */
static void output_quoted_string(char *str, void (*cb)(const char *,void *),
                                 void *data)
{
    char    ch;
    char buf[2];

    cb("\"", data);
    if (!str) {
        cb("\"", data);
        return;
    }
    buf[1] = 0;
    while ((ch = *str++)) {
        switch (ch) {
        case '\\':
            cb("\\\\", data);
            break;
        case '\n':
            cb("\\n", data);
            break;
        case '\t':
            cb("\\t", data);
            break;
        case '\b':
            cb("\\b", data);
            break;
        default:
            /* This would be a lot faster if we scanned
               forward for the next "interesting"
               character.  */
            buf[0] = ch;
            cb(buf, data);
            break;
        }
    }
    cb("\"", data);
}



#if defined(_WIN32)
#define EOL "\r\n"
#endif

#ifndef EOL
#define EOL "\n"
#endif

/* Errors should be returned, not ignored!  */
static void dump_profile(struct profile_node *root, int level,
                         void (*cb)(const char *, void *), void *data)
{
    int i, final;
    struct profile_node *p;
    void *iter;
    long retval;
    char *name, *value;

    iter = 0;
    do {
        retval = profile_find_node_relation(root, 0, &iter,
                                            &name, &value, &final);
        if (retval)
            break;
        for (i=0; i < level; i++)
            cb("\t", data);
        cb(name, data);
        cb(final ? "*" : "", data);
        cb(" = ", data);
        if (need_double_quotes(value))
            output_quoted_string(value, cb, data);
        else
            cb(value, data);
        cb(EOL, data);
    } while (iter != 0);

    iter = 0;
    do {
        retval = profile_find_node_subsection(root, 0, &iter,
                                              &name, &p);
        if (retval)
            break;
        if (level == 0) { /* [xxx] */
            cb("[", data);
            cb(name, data);
            cb("]", data);
            cb(profile_is_node_final(p) ? "*" : "", data);
            cb(EOL, data);
            dump_profile(p, level+1, cb, data);
            cb(EOL, data);
        } else {        /* xxx = { ... } */
            for (i=0; i < level; i++)
                cb("\t", data);
            cb(name, data);
            cb(" = {", data);
            cb(EOL, data);
            dump_profile(p, level+1, cb, data);
            for (i=0; i < level; i++)
                cb("\t", data);
            cb("}", data);
            cb(profile_is_node_final(p) ? "*" : "", data);
            cb(EOL, data);
        }
    } while (iter != 0);
}

static void dump_profile_to_file_cb(const char *str, void *data)
{
    fputs(str, data);
}

errcode_t profile_write_tree_file(struct profile_node *root, FILE *dstfile)
{
    dump_profile(root, 0, dump_profile_to_file_cb, dstfile);
    return 0;
}

struct prof_buf {
    char *base;
    size_t cur, max;
    int err;
};

static void add_data_to_buffer(struct prof_buf *b, const void *d, size_t len)
{
    if (b->err)
        return;
    if (b->max - b->cur < len) {
        size_t newsize;
        char *newptr;

        newsize = b->max + (b->max >> 1) + len + 1024;
        newptr = realloc(b->base, newsize);
        if (newptr == NULL) {
            b->err = 1;
            return;
        }
        b->base = newptr;
        b->max = newsize;
    }
    memcpy(b->base + b->cur, d, len);
    b->cur += len;          /* ignore overflow */
}

static void dump_profile_to_buffer_cb(const char *str, void *data)
{
    add_data_to_buffer((struct prof_buf *)data, str, strlen(str));
}

errcode_t profile_write_tree_to_buffer(struct profile_node *root,
                                       char **buf)
{
    struct prof_buf prof_buf = { 0, 0, 0, 0 };

    dump_profile(root, 0, dump_profile_to_buffer_cb, &prof_buf);
    if (prof_buf.err) {
        *buf = NULL;
        return ENOMEM;
    }
    add_data_to_buffer(&prof_buf, "", 1); /* append nul */
    if (prof_buf.max - prof_buf.cur > (prof_buf.max >> 3)) {
        char *newptr = realloc(prof_buf.base, prof_buf.cur);
        if (newptr)
            prof_buf.base = newptr;
    }
    *buf = prof_buf.base;
    return 0;
}