Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / command.h / getcommandordie.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 <limb/bytestr.h>
#include <limb/command.h>
#include <limb/output.h>

struct command *
getcommandordie(int exit, const char *usage, const char *arg)
{
    const char *names[n_commands + 1];
    for (unsigned i = 0; i < n_commands; ++i)
        names[i] = commands[i]->name;
    names[n_commands] = NULL;

    size_t alen = strlen(arg);
    int r, first = -1;
    r = byte_get_match(&first, arg, alen, names);
    if (r < 0) {
        warn("unknown command: ", arg);
        /* more than 1 partial match? */
        if (first >= 0)
            list_matches(obuffer_2, OLVL_NORMAL, "did you mean ", NULL,
                         " or ", " ?", arg, alen, first, names);
        dieusage(exit, usage);
    }

    return commands[r];
}