summaryrefslogtreecommitdiffstats
path: root/src/opt.c
blob: 01949e6bd74261c5ecea050941e52b81dc4458fd (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
/*
 * Copyright 2019-2021 OARC, Inc.
 * Copyright 2017-2018 Akamai Technologies
 * Copyright 2006-2016 Nominum, Inc.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "config.h"

#include "opt.h"

#include "log.h"
#include "util.h"
#include "result.h"

#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <netinet/in.h>

#define MAX_OPTS 64
#define LINE_LENGTH 80

typedef struct {
    char           c;
    perf_opttype_t type;
    const char*    desc;
    const char*    help;
    const char*    defval;
    char           defvalbuf[512];
    union {
        void*         valp;
        char**        stringp;
        bool*         boolp;
        unsigned int* uintp;
        uint64_t*     uint64p;
        double*       doublep;
        in_port_t*    portp;
    } u;
} opt_t;

typedef struct long_opt long_opt_t;
struct long_opt {
    long_opt_t*    next;
    const char*    name;
    perf_opttype_t type;
    const char*    desc;
    const char*    help;
    const char*    defval;
    char           defvalbuf[512];
    union {
        void*         valp;
        char**        stringp;
        bool*         boolp;
        unsigned int* uintp;
        uint64_t*     uint64p;
        double*       doublep;
        in_port_t*    portp;
    } u;
};

static opt_t        opts[MAX_OPTS];
static long_opt_t*  longopts = 0;
static unsigned int nopts;
static char         optstr[MAX_OPTS * 2 + 2 + 1] = { 0 };
extern const char*  progname;

void perf_opt_add(char c, perf_opttype_t type, const char* desc, const char* help,
    const char* defval, void* valp)
{
    opt_t* opt;

    if (nopts == MAX_OPTS) {
        perf_log_fatal("too many defined options");
        return;
    }
    opt       = &opts[nopts++];
    opt->c    = c;
    opt->type = type;
    opt->desc = desc;
    opt->help = help;
    if (defval != NULL) {
        if (strlen(defval) > sizeof(opt->defvalbuf) - 1) {
            perf_log_fatal("perf_opt_add(): defval too large");
            return;
        }
        strncpy(opt->defvalbuf, defval, sizeof(opt->defvalbuf));
        opt->defvalbuf[sizeof(opt->defvalbuf) - 1] = 0;
        opt->defval                                = opt->defvalbuf;
    } else {
        opt->defval = NULL;
    }
    opt->u.valp = valp;

    char newoptstr[sizeof(optstr) + 2];
    snprintf(newoptstr, sizeof(newoptstr), "%s%c%s", optstr, c, (type == perf_opt_boolean ? "" : ":"));
    memcpy(optstr, newoptstr, sizeof(optstr) - 1);
    optstr[sizeof(optstr) - 1] = 0;
}

void perf_long_opt_add(const char* name, perf_opttype_t type, const char* desc, const char* help, const char* defval, void* valp)
{
    long_opt_t* opt = calloc(1, sizeof(long_opt_t));

    if (!opt) {
        perf_log_fatal("perf_long_opt_add(): out of memory");
        return;
    }

    opt->name = name;
    opt->type = type;
    opt->desc = desc;
    opt->help = help;
    if (defval != NULL) {
        if (strlen(defval) > sizeof(opt->defvalbuf) - 1) {
            perf_log_fatal("perf_opt_add(): defval too large");
            free(opt); // fix clang scan-build
            return;
        }
        strncpy(opt->defvalbuf, defval, sizeof(opt->defvalbuf));
        opt->defvalbuf[sizeof(opt->defvalbuf) - 1] = 0;
        opt->defval                                = opt->defvalbuf;
    } else {
        opt->defval = NULL;
    }
    opt->u.valp = valp;

    opt->next = longopts;
    longopts  = opt;
}

void perf_opt_usage(void)
{
    unsigned int prefix_len, position, arg_len, i, j;

    prefix_len = fprintf(stderr, "Usage: %s", progname);
    position   = prefix_len;
    for (i = 0; i < nopts; i++) {
        arg_len = 6;
        if (opts[i].desc != NULL)
            arg_len += strlen(opts[i].desc) + 1;
        if (LINE_LENGTH - position - 1 < arg_len) {
            fprintf(stderr, "\n");
            for (j = 0; j < prefix_len; j++)
                fprintf(stderr, " ");
            position = prefix_len;
        }
        fprintf(stderr, " [-%c", opts[i].c);
        if (opts[i].desc != NULL)
            fprintf(stderr, " %s", opts[i].desc);
        fprintf(stderr, "]");
        position += arg_len;
    }
    fprintf(stderr, "\n");

    for (i = 0; i < nopts; i++) {
        fprintf(stderr, "  -%c %s", opts[i].c, opts[i].help);
        if (opts[i].defval)
            fprintf(stderr, " (default: %s)", opts[i].defval);
        fprintf(stderr, "\n");
    }
}

static uint32_t
parse_uint(const char* desc, const char* str,
    unsigned int min, unsigned int max)
{
    unsigned long int val;
    uint32_t          ret;
    char*             endptr = 0;

    errno = 0;
    val   = strtoul(str, &endptr, 10);
    if (!errno && str && *str && endptr && !*endptr && val <= UINT32_MAX) {
        ret = (uint32_t)val;
        if (ret >= min && ret <= max) {
            return ret;
        }
    }

    fprintf(stderr, "invalid %s: %s\n", desc, str);
    perf_opt_usage();
    exit(1);
}

static double
parse_double(const char* desc, const char* str)
{
    const char* s;
    char        c;
    bool        seen_dot = false;

    s = str;
    while (*s != 0) {
        c = *s++;
        if (c == '.') {
            if (seen_dot)
                goto fail;
            seen_dot = true;
        } else if (c < '0' || c > '9') {
            goto fail;
        }
    }

    return atof(str);

fail:
    fprintf(stderr, "invalid %s: %s\n", desc, str);
    perf_opt_usage();
    exit(1);
}

static uint64_t
parse_timeval(const char* desc, const char* str)
{
    return MILLION * parse_double(desc, str);
}

static int perf_opt_long_parse(char* optarg)
{
    ssize_t optlen;
    char*   arg;

    // TODO: Allow boolean not to have =/value
    if (!(arg = strchr(optarg, '='))) {
        return -1;
    }
    optlen = arg - optarg;
    if (optlen < 1) {
        return -1;
    }
    arg++;

    long_opt_t* opt = longopts;
    while (opt) {
        if (!strncmp(optarg, opt->name, optlen)) {
            switch (opt->type) {
            case perf_opt_string:
                *opt->u.stringp = arg;
                break;
            case perf_opt_boolean:
                *opt->u.boolp = true;
                break;
            case perf_opt_uint:
                *opt->u.uintp = parse_uint(opt->desc, arg, 1, 0xFFFFFFFF);
                break;
            case perf_opt_zpint:
                *opt->u.uintp = parse_uint(opt->desc, arg, 0, 0xFFFFFFFF);
                break;
            case perf_opt_timeval:
                *opt->u.uint64p = parse_timeval(opt->desc, arg);
                break;
            case perf_opt_double:
                *opt->u.doublep = parse_double(opt->desc, arg);
                break;
            case perf_opt_port:
                *opt->u.portp = parse_uint(opt->desc, arg, 0, 0xFFFF);
                break;
            }
            return 0;
        }
        opt = opt->next;
    }

    return -1;
}

void perf_long_opt_usage(void)
{
    fprintf(stderr, "Usage: %s ... -O <name>=<value> ...\n\nAvailable long options:\n", progname);
    long_opt_t* opt = longopts;
    while (opt) {
        fprintf(stderr, "  %s: %s", opt->name, opt->help);
        if (opt->defval) {
            fprintf(stderr, " (default: %s)", opt->defval);
        }
        fprintf(stderr, "\n");

        opt = opt->next;
    }
}

void perf_opt_parse(int argc, char** argv)
{
    int          c;
    opt_t*       opt;
    unsigned int i;

    perf_opt_add('h', perf_opt_boolean, NULL, "print this help", NULL, NULL);
    perf_opt_add('H', perf_opt_boolean, NULL, "print long options help", NULL, NULL);
    perf_opt_add('O', perf_opt_string, NULL, "set long options: <name>=<value>", NULL, NULL);

    while ((c = getopt(argc, argv, optstr)) != -1) {
        for (i = 0; i < nopts; i++) {
            if (opts[i].c == c)
                break;
        }
        if (i == nopts) {
            perf_opt_usage();
            exit(1);
        }
        if (c == 'h') {
            perf_opt_usage();
            exit(0);
        }
        if (c == 'H') {
            perf_long_opt_usage();
            exit(0);
        }
        if (c == 'O') {
            if (perf_opt_long_parse(optarg)) {
                fprintf(stderr, "invalid long option: %s\n", optarg);
                perf_opt_usage();
                exit(1);
            }
            continue;
        }
        opt = &opts[i];
        switch (opt->type) {
        case perf_opt_string:
            *opt->u.stringp = optarg;
            break;
        case perf_opt_boolean:
            *opt->u.boolp = true;
            break;
        case perf_opt_uint:
            *opt->u.uintp = parse_uint(opt->desc, optarg,
                1, 0xFFFFFFFF);
            break;
        case perf_opt_zpint:
            *opt->u.uintp = parse_uint(opt->desc, optarg,
                0, 0xFFFFFFFF);
            break;
        case perf_opt_timeval:
            *opt->u.uint64p = parse_timeval(opt->desc, optarg);
            break;
        case perf_opt_double:
            *opt->u.doublep = parse_double(opt->desc, optarg);
            break;
        case perf_opt_port:
            *opt->u.portp = parse_uint(opt->desc, optarg,
                0, 0xFFFF);
            break;
        }
    }
    if (optind != argc) {
        fprintf(stderr, "unexpected argument %s\n", argv[optind]);
        perf_opt_usage();
        exit(1);
    }
}

perf_suppress_t perf_opt_parse_suppress(const char* val)
{
    perf_suppress_t s = { false, false, false };

    while (val && *val) {
        const char* next = strchr(val, ',');
        int         len;
        if (next) {
            len = next - val;
            next += 1;
        } else {
            len  = strlen(val);
            next = 0;
        }

        if (!strncmp(val, "timeouts", len)) {
            s.timeouts = true;
        } else if (!strncmp(val, "congestion", len)) {
            s.congestion = true;
        } else if (!strncmp(val, "sendfailed", len)) {
            s.sendfailed = true;
        } else if (!strncmp(val, "sockready", len)) {
            s.sockready = true;
        } else {
            fprintf(stderr, "unknown message type to suppress: %.*s\n", len, val);
            perf_opt_usage();
            exit(1);
        }

        val = next;
    }

    return s;
}