blob: 009c7e82cf72836dce1fe3b67714fdc997e21a88 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 * cli.h: command line interface
17 *
18 * Copyright (c) 2008 Eliot Dresselhaus
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
27 *
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
30 *
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 */
39
40#ifndef included_vlib_cli_h
41#define included_vlib_cli_h
42
43#include <vppinfra/format.h>
44
45struct vlib_cli_command_t;
46
Dave Barach9b8ffd92016-07-08 08:13:45 -040047typedef struct
48{
Ed Warnickecb9cada2015-12-08 15:45:58 -070049 u32 min_char;
50
51 /* Indexed by name[position] - min_char. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040052 uword **bitmaps;
Ed Warnickecb9cada2015-12-08 15:45:58 -070053} vlib_cli_parse_position_t;
54
Dave Barach9b8ffd92016-07-08 08:13:45 -040055typedef struct
56{
57 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070058
59 u32 index;
60} vlib_cli_sub_command_t;
61
Dave Barach9b8ffd92016-07-08 08:13:45 -040062typedef struct
63{
64 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070065
66 u32 rule_index;
67
68 u32 command_index;
69} vlib_cli_sub_rule_t;
70
Dave Barach9b8ffd92016-07-08 08:13:45 -040071typedef struct
72{
73 char *name;
74 char *short_help;
75 char *long_help;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
77 /* Number of bytes in parsed data. Zero for vector. */
78 uword data_size;
79
Dave Barach9b8ffd92016-07-08 08:13:45 -040080 unformat_function_t *unformat_function;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
82 /* Opaque for unformat function. */
83 uword unformat_function_arg[2];
84} vlib_cli_parse_rule_t;
85
86/* CLI command callback function. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040087typedef clib_error_t *(vlib_cli_command_function_t)
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 (struct vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -040089 unformat_input_t * input, struct vlib_cli_command_t * cmd);
Ed Warnickecb9cada2015-12-08 15:45:58 -070090
Dave Barach9b8ffd92016-07-08 08:13:45 -040091typedef struct vlib_cli_command_t
92{
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 /* Command path (e.g. "show something").
94 Spaces delimit elements of path. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040095 char *path;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096
97 /* Short/long help strings. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040098 char *short_help;
99 char *long_help;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
101 /* Callback function. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400102 vlib_cli_command_function_t *function;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103
104 /* Opaque. */
105 uword function_arg;
106
107 /* Known MP-safe? */
108 uword is_mp_safe;
109
110 /* Sub commands for this command. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400111 vlib_cli_sub_command_t *sub_commands;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112
113 /* Hash table mapping name (e.g. last path element) to sub command index. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400114 uword *sub_command_index_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
116 /* bitmap[p][c][i] says whether sub-command i has character
117 c in position p. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400118 vlib_cli_parse_position_t *sub_command_positions;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119
120 /* Hash table mapping name (e.g. last path element) to sub rule index. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400121 uword *sub_rule_index_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122
123 /* Vector of possible parse rules for this path. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400124 vlib_cli_sub_rule_t *sub_rules;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
126 /* List of CLI commands, built by constructors */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400127 struct vlib_cli_command_t *next_cli_command;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128
129} vlib_cli_command_t;
130
131typedef void (vlib_cli_output_function_t) (uword arg,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400132 u8 * buffer, uword buffer_bytes);
133typedef struct
134{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 /* Vector of all known commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400136 vlib_cli_command_t *commands;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137
138 /* Hash table mapping normalized path to index into all_commands. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400139 uword *command_index_by_path;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700140
141 /* Vector of all known parse rules. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400142 vlib_cli_parse_rule_t *parse_rules;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143
144 /* Hash table mapping parse rule name to index into parse_rule vector. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400145 uword *parse_rule_index_by_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
147 /* Data parsed for rules. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400148 void **parse_rule_data;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
150 /* registration list added by constructors */
151 vlib_cli_command_t *cli_command_registrations;
152} vlib_cli_main_t;
153
154#define VLIB_CLI_COMMAND(x,...) \
155 __VA_ARGS__ vlib_cli_command_t x; \
156static void __vlib_cli_command_registration_##x (void) \
157 __attribute__((__constructor__)) ; \
158static void __vlib_cli_command_registration_##x (void) \
159{ \
160 vlib_main_t * vm = vlib_get_main(); \
161 vlib_cli_main_t *cm = &vm->cli_main; \
162 x.next_cli_command = cm->cli_command_registrations; \
163 cm->cli_command_registrations = &x; \
164} \
Dave Barach9b8ffd92016-07-08 08:13:45 -0400165__VA_ARGS__ vlib_cli_command_t x
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166#define VLIB_CLI_PARSE_RULE(x) \
Dave Barach9b8ffd92016-07-08 08:13:45 -0400167 vlib_cli_parse_rule_t x
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168/* Output to current CLI connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400169void vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
171/* Process CLI input. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400172void vlib_cli_input (struct vlib_main_t *vm,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173 unformat_input_t * input,
174 vlib_cli_output_function_t * function,
175 uword function_arg);
176
Dave Barach9b8ffd92016-07-08 08:13:45 -0400177clib_error_t *vlib_cli_register (struct vlib_main_t *vm,
178 vlib_cli_command_t * c);
179clib_error_t *vlib_cli_register_parse_rule (struct vlib_main_t *vm,
180 vlib_cli_parse_rule_t * c);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181
Damjan Marionc68b4cb2016-05-25 20:11:33 +0200182uword unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args);
183
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184#endif /* included_vlib_cli_h */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400185
186/*
187 * fd.io coding-style-patch-verification: ON
188 *
189 * Local Variables:
190 * eval: (c-set-style "gnu")
191 * End:
192 */