aboutsummaryrefslogtreecommitdiff
path: root/src/prc1/arg.c
blob: f55d3220a6c5c3e1876f141030bda4c28ac99af3 (plain) (blame)
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
#include "arg.h"
#include "dbg.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct arg
{
    const char *s;
    int (*f)(struct args *, const char *);

    enum
    {
        NOVAL,
        SPACEVAL = 1,
        EQVAL = 1 << 1,
        JOINVAL = 1 << 2
    } v;
};

static int debug(struct args *a, const char *arg)
{
    dbgen++;
    return 0;
}

static int target(struct args *a, const char *arg)
{
    if (!strcmp(arg, "qbe"))
        a->tgt = QBE;
    else
    {
        fprintf(stderr, "invalid or unsupported target \"%s\"", arg);
        return -1;
    }

    return 0;
}

static int inc(struct args *a, const char *arg)
{
    struct arg_input *i = &a->incs;
    size_t n = i->n + 1;
    const char **v = realloc(i->v, n * sizeof *v);

    if (!v)
    {
        perror("realloc(3)");
        return -1;
    }

    v[i->n++] = arg;
    i->v = v;
    return 0;
}

static int opt(struct args *args, const struct arg *a, const char *arg)
{
    const struct arg *prev = args->arg;
    const char *v = arg + strlen(a->s);

    if (prev && prev->v)
    {
        fprintf(stderr, "%s: option '%s' requires a value\n", args->name,
            prev->s);
        return -1;
    }
    else if (*v)
    {
        if (!(a->v & JOINVAL))
        {
            fprintf(stderr, "%s: unrecognized command-line option '%s'\n",
                args->name, arg);
            return -1;
        }
    }
    else if (a->v & SPACEVAL)
    {
        args->arg = a;
        return 0;
    }
    else
        v = NULL;

    return a->f(args, v);
}

int arg(struct args *a, const char *value)
{
    static const struct arg args[] =
    {
        {"-d", debug},
        {"-t", target},
        {"-I", inc, SPACEVAL | JOINVAL}
    };

    const struct arg *prev = a->arg;

    for (size_t i = 0; i < sizeof args / sizeof *args; i++)
    {
        const struct arg *arg = &args[i];

        if (!strncmp(value, arg->s, strlen(arg->s)))
            return opt(a, arg, value);
    }

    if (prev)
    {
        if (*value != '-' && prev->f(a, value))
            return -1;

        a->arg = NULL;
    }
    else if (a->src)
    {
        fprintf(stderr, "%s: only one source file allowed\n", a->name);
        return -1;
    }
    else
        a->src = value;

    return 0;
}

void arg_defaults(struct args *a)
{
    a->tgt = QBE;
}

static void free_input(struct arg_input *i)
{
    free(i->v);
}

void arg_free(struct args *a)
{
    free_input(&a->incs);
}