aboutsummaryrefslogtreecommitdiff
path: root/src/options.c
blob: 4b14bf1762c55ad0b56eddec95ccd736a2597ec6 (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
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define POTD_LOGFILE "/tmp/potd.log"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>

#include "options.h"
#include "log.h"

typedef enum opt_type {
    OT_INVALID = 0, OT_NOARG, OT_L, OT_LL, OT_STR,
    OT_PATH
} opt_type;

struct opt_list;

typedef union opt_ptr {
    long int l;
    long long int ll;
    const char *str;
    char *str_dup;
    struct opt_list *list;
} opt_ptr;

typedef struct opt_list {
    opt_ptr value;
    struct opt_list *next;
} opt_list;

struct opt {
    opt_type type;
    opt_ptr value;
    opt_ptr def_value;
    int used;
    int is_list;

    const char *arg_name;
    const char *short_help;
    const char *help;
};

#define OPT(type, def_value, arg, short_help, help) \
    { type, {0}, {def_value}, 0, 0, arg, short_help, help }
#define OPT_LIST(type, def_values, arg, short_help, help) \
    { type, {0}, {def_values}, 0, 1, arg, short_help, help }
#define OPT_NOARG(arg, short_help, help) \
    OPT(OT_NOARG, .ll = 0, arg, short_help, help)
static struct opt options[OPT_MAX+1] = {
    OPT_NOARG("log-to-file", "short_help", "help"),
    OPT(OT_PATH, .str = POTD_LOGFILE, "log-file", "short help", "help"),
    OPT(OT_STR, .ll = NOTICE, "log-level", "short help", "help"),
    OPT_NOARG("daemon", "short help", "help"),
    OPT_LIST(OT_STR, .ll = 0, "redirect", "short_help", "help"),
    OPT_LIST(OT_STR, .ll = 0, "protocol", "short_help", "help"),
    OPT_LIST(OT_STR, .ll = 0, "jail", "short_help", "help"),

    OPT(OT_INVALID, .ll = 0, NULL, NULL, NULL)
};

static int opt_convert(opt_type t, opt_ptr *d);
static int setopt_list(struct opt *o, const char *optarg);
static int setopt(struct opt *o, const char *optarg);


static int opt_convert(opt_type t, opt_ptr *d)
{
    char *endptr = NULL;

    switch (t) {
        case OT_L:
            d->l = strtol(optarg, &endptr, 10);
            break;
        case OT_LL:
            d->ll = strtoll(optarg, &endptr, 10);
            break;
        case OT_STR:
            d->str_dup = strdup(optarg);
            break;
        case OT_PATH:
            d->str_dup = realpath(optarg, NULL);
            break;
        case OT_NOARG:
        case OT_INVALID:
            return 1;
    }

    if (endptr && *endptr != 0)
        return 1;

    return 0;
}

static int setopt_list(struct opt *o, const char *optarg)
{
    opt_list **l;

    assert(o && o->type != OT_INVALID);

    if (!optarg || o->type == OT_NOARG || !o->is_list)
        return 1;

    l = &o->value.list;
    while (*l) l = &(*l)->next;
    *l = (opt_list *) calloc(1, sizeof **l);

    if (opt_convert(o->type, &(*l)->value))
        return 1;

    o->used = 1;

    return 0;
}

static int setopt(struct opt *o, const char *optarg)
{
    assert(o && o->type != OT_INVALID);
    if (o->used && !o->is_list)
        return 1;
    if (!optarg || o->type == OT_NOARG)
        goto noarg;

    if (opt_convert(o->type, &o->value))
        return 1;

noarg:
    o->used = 1;

    return 0;
}

int parse_cmdline(int argc, char **argv)
{
    int rc, i, option, option_index;
    struct option *o = (struct option *) calloc(OPT_MAX+1, sizeof *o);

    assert(o);
    for (i = 0; i < OPT_MAX; ++i) {
        o[i].name = options[i].arg_name;
        if (options[i].def_value.ll)
            o[i].has_arg = required_argument;
        else
            o[i].has_arg =
                (options[i].type == OT_NOARG ? no_argument : required_argument);
    }

    rc = 0;
    while (1) {
        option_index = -1;
        option = getopt_long_only(argc, argv, "", o, &option_index);

        if (option_index == -1 && option != -1) {
            rc = 1;
            continue;
        }
        if (option == -1)
            break;

        if (!option) {
            if (!setopt_list(&options[option_index], optarg)) {
            } else if (setopt(&options[option_index], optarg)) {
                rc = 1;
                goto error;
            }
        } else {
            fprintf(stderr, "%s: unknown option '%c' [0x%X]\n",
                argv[0], option, option);
        }
    }

error:
    free(o);

    return rc;
}

int getopt_used(opt_name on)
{
    return options[on].used;
}

char *
getopt_str(opt_name on)
{
    char *str;

    assert(options[on].type == OT_STR ||
        options[on].type == OT_PATH);
    assert(getopt_used(on) || options[on].def_value.str);
    str = options[on].value.str_dup;
    if (!str)
        str = options[on].def_value.str_dup;
    assert(str);

    return str;
}

char *
getopt_strlist(opt_name on, opt_list **ol)
{
    opt_list *o;

    assert(options[on].is_list && ol);
    assert(options[on].type == OT_STR ||
        options[on].type == OT_PATH);
    assert(getopt_used(on) && !options[on].def_value.str);

    if (*ol) {
        o = (*ol)->next;
    } else {
        o = options[on].value.list;
    }
    *ol = o;

    return (o ? o->value.str_dup : NULL);
}