Initial commit of vpp code.
[vpp.git] / vlib / vlib / cli.h
1 /*
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
45 struct vlib_cli_command_t;
46
47 typedef struct {
48   u32 min_char;
49
50   /* Indexed by name[position] - min_char. */
51   uword ** bitmaps;
52 } vlib_cli_parse_position_t;
53
54 typedef struct {
55   u8 * name;
56
57   u32 index;
58 } vlib_cli_sub_command_t;
59
60 typedef struct {
61   u8 * name;
62
63   u32 rule_index;
64
65   u32 command_index;
66 } vlib_cli_sub_rule_t;
67
68 typedef struct {
69   char * name;
70   char * short_help;
71   char * long_help;
72
73   /* Number of bytes in parsed data.  Zero for vector. */
74   uword data_size;
75
76   unformat_function_t * unformat_function;
77
78   /* Opaque for unformat function. */
79   uword unformat_function_arg[2];
80 } vlib_cli_parse_rule_t;
81
82 /* CLI command callback function. */
83 typedef clib_error_t * (vlib_cli_command_function_t)
84   (struct vlib_main_t * vm,
85    unformat_input_t * input,
86    struct vlib_cli_command_t * cmd);
87
88 typedef struct vlib_cli_command_t {
89   /* Command path (e.g. "show something").
90      Spaces delimit elements of path. */
91   char * path;
92
93   /* Short/long help strings. */
94   char * short_help;
95   char * long_help;
96
97   /* Callback function. */
98   vlib_cli_command_function_t * function;
99
100   /* Opaque. */
101   uword function_arg;
102
103   /* Known MP-safe? */
104   uword is_mp_safe;
105
106   /* Sub commands for this command. */
107   vlib_cli_sub_command_t * sub_commands;
108
109   /* Hash table mapping name (e.g. last path element) to sub command index. */
110   uword * sub_command_index_by_name;
111
112   /* bitmap[p][c][i] says whether sub-command i has character
113      c in position p. */
114   vlib_cli_parse_position_t * sub_command_positions;
115
116   /* Hash table mapping name (e.g. last path element) to sub rule index. */
117   uword * sub_rule_index_by_name;
118
119   /* Vector of possible parse rules for this path. */
120   vlib_cli_sub_rule_t * sub_rules;
121
122   /* List of CLI commands, built by constructors */
123   struct vlib_cli_command_t * next_cli_command;
124
125 } vlib_cli_command_t;
126
127 typedef void (vlib_cli_output_function_t) (uword arg,
128                                            u8 * buffer,
129                                            uword buffer_bytes);
130 typedef struct {
131   /* Current output function. */
132   vlib_cli_output_function_t * output_function;
133
134   /* Opaque data for output function. */
135   uword output_function_arg;
136
137   /* Vector of all known commands. */
138   vlib_cli_command_t * commands;
139
140   /* Hash table mapping normalized path to index into all_commands. */
141   uword * command_index_by_path;
142
143   /* Vector of all known parse rules. */
144   vlib_cli_parse_rule_t * parse_rules;
145
146   /* Hash table mapping parse rule name to index into parse_rule vector. */
147   uword * parse_rule_index_by_name;
148
149   /* Data parsed for rules. */
150   void ** parse_rule_data;
151
152   /* registration list added by constructors */
153   vlib_cli_command_t *cli_command_registrations;
154 } vlib_cli_main_t;
155
156 #define VLIB_CLI_COMMAND(x,...)                                         \
157     __VA_ARGS__ vlib_cli_command_t x;                                   \
158 static void __vlib_cli_command_registration_##x (void)                  \
159     __attribute__((__constructor__)) ;                                  \
160 static void __vlib_cli_command_registration_##x (void)                  \
161 {                                                                       \
162     vlib_main_t * vm = vlib_get_main();                                 \
163     vlib_cli_main_t *cm = &vm->cli_main;                                \
164     x.next_cli_command = cm->cli_command_registrations;                 \
165     cm->cli_command_registrations = &x;                                 \
166 }                                                                       \
167 __VA_ARGS__ vlib_cli_command_t x 
168
169
170 #define VLIB_CLI_PARSE_RULE(x) \
171   vlib_cli_parse_rule_t x 
172
173 /* Output to current CLI connection. */
174 void vlib_cli_output (struct vlib_main_t * vm, char * fmt, ...);
175
176 /* Process CLI input. */
177 void vlib_cli_input (struct vlib_main_t * vm,
178                      unformat_input_t * input,
179                      vlib_cli_output_function_t * function,
180                      uword function_arg);
181
182 clib_error_t * vlib_cli_register (struct vlib_main_t * vm,
183                                   vlib_cli_command_t * c);
184 clib_error_t * vlib_cli_register_parse_rule (struct vlib_main_t * vm,
185                                              vlib_cli_parse_rule_t * c);
186
187 #endif /* included_vlib_cli_h */