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