summaryrefslogtreecommitdiffstats
path: root/freebsd/sbin/nvmecontrol/comnd.h
blob: 0f06cd298567ab25a455b4bdd9cc22c10d7b182b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2019 Netflix, Inc
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#ifndef	COMND_H
#define	COMND_H

#include <sys/queue.h>
#include <sys/linker_set.h>

/*
 * Regularized parsing of simple arguments built on top of getopt_long.
 */

typedef enum arg_type {
	arg_none = 0,
	arg_uint8,
	arg_uint16,
	arg_uint32,
	arg_uint64,
	arg_size,
	arg_string,
	arg_path,
} arg_type;

// XXX need to change to offsetof for opts and args.
// we then need to allocate ctx and pass that into the cmd
// stuff. this will be a little tricky and we may need to expand
// arg_type stuff.

struct opts {
	const char	*long_arg;
	int		short_arg;
	arg_type	at;
	void 		*ptr;			//  XXXX change to offset of
	const char	*descr;
};

// XXX TDB: subcommand vs actual argument. maybe with subcmd?
// XXX TBD: do we need parsing callback functions?
struct args {
	arg_type	at;
	void 		*ptr;			//  XXXX change to offset of
	const char	*descr;
};

typedef void (cmd_load_cb_t)(void *, void *);
struct cmd;
typedef void (cmd_fn_t)(const struct cmd *nf, int argc, char *argv[]);

struct cmd  {
	SLIST_ENTRY(cmd)	link;
	const char		*name;
	cmd_fn_t		*fn;
	size_t			ctx_size;
	const struct opts	*opts;
	const struct args	*args;
	const char		*descr;
	SLIST_HEAD(,cmd)	subcmd;
	struct cmd		*parent;
};

void cmd_register(struct cmd *, struct cmd *);
#ifndef __rtems__
#define CMD_COMMAND(c)							\
    static void cmd_register_##c(void) __attribute__((constructor));	\
    static void cmd_register_##c(void) { cmd_register(NULL, &c); }
#define CMD_SUBCOMMAND(c,sc)						\
    static void cmd_register_##c_##sc(void) __attribute__((constructor)); \
    static void cmd_register_##c_##sc(void) { cmd_register(&c, &sc); }
#else /* __rtems__ */
#define CMD_COMMAND(c)							\
    void cmd_register_##c(void) { cmd_register(NULL, &c); }
#define CMD_SUBCOMMAND(c,sc)						\
    void cmd_register_##c##_##sc(void) { cmd_register(&c, &sc); }
void cmd_register_admin_pass_cmd(void);
void cmd_register_devlist_cmd(void);
void cmd_register_firmware_cmd(void);
void cmd_register_format_cmd(void);
void cmd_register_identify_cmd(void);
void cmd_register_io_pass_cmd(void);
void cmd_register_logpage_cmd(void);
void cmd_register_ns_cmd(void);
void cmd_register_ns_cmd_active_cmd(void);
void cmd_register_ns_cmd_allocated_cmd(void);
void cmd_register_ns_cmd_attach_cmd(void);
void cmd_register_ns_cmd_attached_cmd(void);
void cmd_register_ns_cmd_controllers_cmd(void);
void cmd_register_ns_cmd_create_cmd(void);
void cmd_register_ns_cmd_delete_cmd(void);
void cmd_register_ns_cmd_detach_cmd(void);
void cmd_register_ns_cmd_identify_cmd(void);
void cmd_register_nsid_cmd(void);
void cmd_register_perftest_cmd(void);
void cmd_register_power_cmd(void);
void cmd_register_reset_cmd(void);
void cmd_register_resv_cmd(void);
void cmd_register_resv_cmd_acquire_cmd(void);
void cmd_register_resv_cmd_register_cmd(void);
void cmd_register_resv_cmd_release_cmd(void);
void cmd_register_resv_cmd_report_cmd(void);
void cmd_register_sanitize_cmd(void);
void cmd_register_wdc_cmd(void);
void cmd_register_wdc_cmd_cap_diag_cmd(void);
#endif /* __rtems__ */

int arg_parse(int argc, char * const *argv, const struct cmd *f);
void arg_help(int argc, char * const *argv, const struct cmd *f);
void cmd_init(void);
void cmd_load_dir(const char *dir, cmd_load_cb_t *cb, void *argp);
int cmd_dispatch(int argc, char *argv[], const struct cmd *);

#endif /* COMND_H */