xref: /freebsd/sbin/nvmecontrol/comnd.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1f634b4c1SWarner Losh /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 
28f634b4c1SWarner Losh #ifndef	COMND_H
29f634b4c1SWarner Losh #define	COMND_H
30f634b4c1SWarner Losh 
31f634b4c1SWarner Losh #include <sys/queue.h>
32f634b4c1SWarner Losh #include <sys/linker_set.h>
33f634b4c1SWarner Losh 
34f634b4c1SWarner Losh /*
35f634b4c1SWarner Losh  * Regularized parsing of simple arguments built on top of getopt_long.
36f634b4c1SWarner Losh  */
37f634b4c1SWarner Losh 
38f634b4c1SWarner Losh typedef enum arg_type {
39f634b4c1SWarner Losh 	arg_none = 0,
40f634b4c1SWarner Losh 	arg_uint8,
41f634b4c1SWarner Losh 	arg_uint16,
42f634b4c1SWarner Losh 	arg_uint32,
43f634b4c1SWarner Losh 	arg_uint64,
44f634b4c1SWarner Losh 	arg_size,
45f634b4c1SWarner Losh 	arg_string,
46f634b4c1SWarner Losh 	arg_path,
47f634b4c1SWarner Losh } arg_type;
48f634b4c1SWarner Losh 
49f634b4c1SWarner Losh // XXX need to change to offsetof for opts and args.
50f634b4c1SWarner Losh // we then need to allocate ctx and pass that into the cmd
51f634b4c1SWarner Losh // stuff. this will be a little tricky and we may need to expand
52f634b4c1SWarner Losh // arg_type stuff.
53f634b4c1SWarner Losh 
54f634b4c1SWarner Losh struct opts {
55f634b4c1SWarner Losh 	const char	*long_arg;
56f634b4c1SWarner Losh 	int		short_arg;
57f634b4c1SWarner Losh 	arg_type	at;
58f634b4c1SWarner Losh 	void 		*ptr;			//  XXXX change to offset of
59f634b4c1SWarner Losh 	const char	*descr;
60f634b4c1SWarner Losh };
61f634b4c1SWarner Losh 
62f634b4c1SWarner Losh // XXX TDB: subcommand vs actual argument. maybe with subcmd?
63f634b4c1SWarner Losh // XXX TBD: do we need parsing callback functions?
64f634b4c1SWarner Losh struct args {
65f634b4c1SWarner Losh 	arg_type	at;
66f634b4c1SWarner Losh 	void 		*ptr;			//  XXXX change to offset of
67f634b4c1SWarner Losh 	const char	*descr;
68f634b4c1SWarner Losh };
69f634b4c1SWarner Losh 
70f634b4c1SWarner Losh typedef void (cmd_load_cb_t)(void *, void *);
71f634b4c1SWarner Losh struct cmd;
72f634b4c1SWarner Losh typedef void (cmd_fn_t)(const struct cmd *nf, int argc, char *argv[]);
73f634b4c1SWarner Losh 
74f634b4c1SWarner Losh struct cmd  {
75f634b4c1SWarner Losh 	SLIST_ENTRY(cmd)	link;
76f634b4c1SWarner Losh 	const char		*name;
77f634b4c1SWarner Losh 	cmd_fn_t		*fn;
78f634b4c1SWarner Losh 	size_t			ctx_size;
79f634b4c1SWarner Losh 	const struct opts	*opts;
80f634b4c1SWarner Losh 	const struct args	*args;
81f634b4c1SWarner Losh 	const char		*descr;
82f634b4c1SWarner Losh 	SLIST_HEAD(,cmd)	subcmd;
83f634b4c1SWarner Losh 	struct cmd		*parent;
84f634b4c1SWarner Losh };
85f634b4c1SWarner Losh 
86f634b4c1SWarner Losh void cmd_register(struct cmd *, struct cmd *);
87f634b4c1SWarner Losh #define CMD_COMMAND(c)							\
88f634b4c1SWarner Losh     static void cmd_register_##c(void) __attribute__((constructor));	\
89f634b4c1SWarner Losh     static void cmd_register_##c(void) { cmd_register(NULL, &c); }
90f634b4c1SWarner Losh #define CMD_SUBCOMMAND(c,sc)						\
91f634b4c1SWarner Losh     static void cmd_register_##c_##sc(void) __attribute__((constructor)); \
92f634b4c1SWarner Losh     static void cmd_register_##c_##sc(void) { cmd_register(&c, &sc); }
93f634b4c1SWarner Losh 
94f634b4c1SWarner Losh int arg_parse(int argc, char * const *argv, const struct cmd *f);
95f634b4c1SWarner Losh void arg_help(int argc, char * const *argv, const struct cmd *f);
96f634b4c1SWarner Losh void cmd_init(void);
978e103108SScott Long void cmd_load_dir(const char *dir, cmd_load_cb_t *cb, void *argp);
98f634b4c1SWarner Losh int cmd_dispatch(int argc, char *argv[], const struct cmd *);
99f634b4c1SWarner Losh 
100f634b4c1SWarner Losh #endif /* COMND_H */
101