xref: /linux/tools/net/ynl/ynltool/main.h (revision 6dfafbd0299a60bfb5d5e277fdf100037c7ded07)
1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3 /* Copyright Meta Platforms, Inc. and affiliates */
4 
5 #ifndef __YNLTOOL_H
6 #define __YNLTOOL_H
7 
8 #ifndef _GNU_SOURCE
9 #define _GNU_SOURCE
10 #endif
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #include <string.h>
16 
17 #include "json_writer.h"
18 
19 #define NEXT_ARG()	({ argc--; argv++; if (argc < 0) usage(); })
20 #define NEXT_ARGP()	({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
21 #define BAD_ARG()	({ p_err("what is '%s'?", *argv); -1; })
22 #define GET_ARG()	({ argc--; *argv++; })
23 #define REQ_ARGS(cnt)							\
24 	({								\
25 		int _cnt = (cnt);					\
26 		bool _res;						\
27 									\
28 		if (argc < _cnt) {					\
29 			p_err("'%s' needs at least %d arguments, %d found", \
30 			      argv[-1], _cnt, argc);			\
31 			_res = false;					\
32 		} else {						\
33 			_res = true;					\
34 		}							\
35 		_res;							\
36 	})
37 
38 #define HELP_SPEC_OPTIONS						\
39 	"OPTIONS := { {-j|--json} [{-p|--pretty}] }"
40 
41 extern const char *bin_name;
42 
43 extern json_writer_t *json_wtr;
44 extern bool json_output;
45 extern bool pretty_output;
46 
47 void __attribute__((format(printf, 1, 2))) p_err(const char *fmt, ...);
48 void __attribute__((format(printf, 1, 2))) p_info(const char *fmt, ...);
49 
50 bool is_prefix(const char *pfx, const char *str);
51 int detect_common_prefix(const char *arg, ...);
52 void usage(void) __attribute__((noreturn));
53 
54 struct cmd {
55 	const char *cmd;
56 	int (*func)(int argc, char **argv);
57 };
58 
59 int cmd_select(const struct cmd *cmds, int argc, char **argv,
60 	       int (*help)(int argc, char **argv));
61 
62 /* subcommands */
63 int do_page_pool(int argc, char **argv);
64 int do_qstats(int argc, char **argv);
65 
66 #endif /* __YNLTOOL_H */
67