Welcome to little lamb

Code » limb » release » tree

[release] / src / liblimb / loadopt.h / loadopt_auto_noconfig.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <errno.h>
#include <stddef.h> /* offsetof() */
#include <limb/bytestr.h>
#include <limb/output.h>
#include "loadopt.h"

int
loadopt_auto_noconfig(int idx, const struct option *options, struct loadopt *ctx)
{
    if (!ctx->po.arg) {
        for (int i = 0; options[i].longopt; ++i)
            if (!(get_optflags(ctx->optflags, i) & OPT_SET))
                add_optflags(ctx->optflags, i, OPT_SKIP);
    } else {
        for (char *s = strtok((char *) ctx->po.arg, ","); s; s = strtok(NULL, ",")) {
            size_t l= strlen(s);
            int first = -1, i;
            i = byte_get_match_full(&first, s, l, options,
                                    offsetof(struct option, longopt), sizeof(*options));
            if (i < 0) {
                char buf[2] = { options[idx].shortopt, 0 };
                warn("invalid argument to option --", options[idx].longopt,
                     (*buf) ? "/-" : "", buf, ": ", ctx->po.arg);
                if (first >= 0)
                    list_matches_full(obuffer_2, OLVL_NORMAL, "did you mean ",
                                      "--", " or ", " ?", s, l, first, options,
                                      offsetof(struct option, longopt), sizeof(*options));
                return (errno = EINVAL, 0);
            }
            add_optflags(ctx->optflags, i, OPT_SKIP);
        }
    }

    return 1;
}