1f634b4c1SWarner Losh /*- 2f634b4c1SWarner Losh * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3f634b4c1SWarner Losh * 4f634b4c1SWarner Losh * Copyright (c) 2019 Netflix, Inc 5f634b4c1SWarner Losh * 6f634b4c1SWarner Losh * Redistribution and use in source and binary forms, with or without 7f634b4c1SWarner Losh * modification, are permitted provided that the following conditions 8f634b4c1SWarner Losh * are met: 9f634b4c1SWarner Losh * 1. Redistributions of source code must retain the above copyright 10f634b4c1SWarner Losh * notice, this list of conditions and the following disclaimer. 11f634b4c1SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 12f634b4c1SWarner Losh * notice, this list of conditions and the following disclaimer in the 13f634b4c1SWarner Losh * documentation and/or other materials provided with the distribution. 14f634b4c1SWarner Losh * 15f634b4c1SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16f634b4c1SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17f634b4c1SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18f634b4c1SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19f634b4c1SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20f634b4c1SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21f634b4c1SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22f634b4c1SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23f634b4c1SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24f634b4c1SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25f634b4c1SWarner Losh * SUCH DAMAGE. 26f634b4c1SWarner Losh * 27f634b4c1SWarner Losh * $FreeBSD$ 28f634b4c1SWarner Losh */ 29f634b4c1SWarner Losh 30f634b4c1SWarner Losh #ifndef COMND_H 31f634b4c1SWarner Losh #define COMND_H 32f634b4c1SWarner Losh 33f634b4c1SWarner Losh #include <sys/queue.h> 34f634b4c1SWarner Losh #include <sys/linker_set.h> 35f634b4c1SWarner Losh 36f634b4c1SWarner Losh /* 37f634b4c1SWarner Losh * Regularized parsing of simple arguments built on top of getopt_long. 38f634b4c1SWarner Losh */ 39f634b4c1SWarner Losh 40f634b4c1SWarner Losh typedef enum arg_type { 41f634b4c1SWarner Losh arg_none = 0, 42f634b4c1SWarner Losh arg_uint8, 43f634b4c1SWarner Losh arg_uint16, 44f634b4c1SWarner Losh arg_uint32, 45f634b4c1SWarner Losh arg_uint64, 46f634b4c1SWarner Losh arg_size, 47f634b4c1SWarner Losh arg_string, 48f634b4c1SWarner Losh arg_path, 49f634b4c1SWarner Losh } arg_type; 50f634b4c1SWarner Losh 51f634b4c1SWarner Losh // XXX need to change to offsetof for opts and args. 52f634b4c1SWarner Losh // we then need to allocate ctx and pass that into the cmd 53f634b4c1SWarner Losh // stuff. this will be a little tricky and we may need to expand 54f634b4c1SWarner Losh // arg_type stuff. 55f634b4c1SWarner Losh 56f634b4c1SWarner Losh struct opts { 57f634b4c1SWarner Losh const char *long_arg; 58f634b4c1SWarner Losh int short_arg; 59f634b4c1SWarner Losh arg_type at; 60f634b4c1SWarner Losh void *ptr; // XXXX change to offset of 61f634b4c1SWarner Losh const char *descr; 62f634b4c1SWarner Losh }; 63f634b4c1SWarner Losh 64f634b4c1SWarner Losh // XXX TDB: subcommand vs actual argument. maybe with subcmd? 65f634b4c1SWarner Losh // XXX TBD: do we need parsing callback functions? 66f634b4c1SWarner Losh struct args { 67f634b4c1SWarner Losh arg_type at; 68f634b4c1SWarner Losh void *ptr; // XXXX change to offset of 69f634b4c1SWarner Losh const char *descr; 70f634b4c1SWarner Losh }; 71f634b4c1SWarner Losh 72f634b4c1SWarner Losh typedef void (cmd_load_cb_t)(void *, void *); 73f634b4c1SWarner Losh struct cmd; 74f634b4c1SWarner Losh typedef void (cmd_fn_t)(const struct cmd *nf, int argc, char *argv[]); 75f634b4c1SWarner Losh 76f634b4c1SWarner Losh struct cmd { 77f634b4c1SWarner Losh SLIST_ENTRY(cmd) link; 78f634b4c1SWarner Losh const char *name; 79f634b4c1SWarner Losh cmd_fn_t *fn; 80f634b4c1SWarner Losh size_t ctx_size; 81f634b4c1SWarner Losh const struct opts *opts; 82f634b4c1SWarner Losh const struct args *args; 83f634b4c1SWarner Losh const char *descr; 84f634b4c1SWarner Losh SLIST_HEAD(,cmd) subcmd; 85f634b4c1SWarner Losh struct cmd *parent; 86f634b4c1SWarner Losh }; 87f634b4c1SWarner Losh 88f634b4c1SWarner Losh void cmd_register(struct cmd *, struct cmd *); 89f634b4c1SWarner Losh #define CMD_COMMAND(c) \ 90f634b4c1SWarner Losh static void cmd_register_##c(void) __attribute__((constructor)); \ 91f634b4c1SWarner Losh static void cmd_register_##c(void) { cmd_register(NULL, &c); } 92f634b4c1SWarner Losh #define CMD_SUBCOMMAND(c,sc) \ 93f634b4c1SWarner Losh static void cmd_register_##c_##sc(void) __attribute__((constructor)); \ 94f634b4c1SWarner Losh static void cmd_register_##c_##sc(void) { cmd_register(&c, &sc); } 95f634b4c1SWarner Losh 96f634b4c1SWarner Losh int arg_parse(int argc, char * const *argv, const struct cmd *f); 97f634b4c1SWarner Losh void arg_help(int argc, char * const *argv, const struct cmd *f); 98f634b4c1SWarner Losh void cmd_init(void); 99*8e103108SScott Long void cmd_load_dir(const char *dir, cmd_load_cb_t *cb, void *argp); 100f634b4c1SWarner Losh int cmd_dispatch(int argc, char *argv[], const struct cmd *); 101f634b4c1SWarner Losh 102f634b4c1SWarner Losh #endif /* COMND_H */ 103