options.c 1.1 KB
Newer Older
J
Jonathan Calmels 已提交
1
/*
J
Jonathan Calmels 已提交
2
 * Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved.
J
Jonathan Calmels 已提交
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
 */

#include <stdlib.h>
#include <string.h>

#include "nvc_internal.h"

#include "error.h"
#include "options.h"
#include "xfuncs.h"

int32_t
options_parse(struct error *err, const char *str, const struct option *opts, size_t nopts)
{
        char buf[NVC_ARG_MAX];
        char *ptr = buf;
        int32_t flags = 0;
        char *opt;
        size_t i;

        if (strlen(str) >= sizeof(buf)) {
                error_setx(err, "too many options");
                return (-1);
        }
        strcpy(buf, str);

        while ((opt = strsep(&ptr, " ")) != NULL) {
                if (*opt == '\0')
                        continue;
                for (i = 0; i < nopts; ++i) {
J
Jonathan Calmels 已提交
33
                        if (str_equal(opt, opts[i].name)) {
J
Jonathan Calmels 已提交
34 35 36 37 38 39 40 41 42 43 44
                                flags |= opts[i].value;
                                break;
                        }
                }
                if (i == nopts) {
                        error_setx(err, "invalid option: %s", opt);
                        return (-1);
                }
        }
        return (flags);
}