Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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> |
Dave Barach | 3d0e0d1 | 2021-02-17 10:25:18 -0500 | [diff] [blame] | 44 | #include <vlib/log.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | |
| 46 | struct vlib_cli_command_t; |
| 47 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 48 | typedef struct |
| 49 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | u32 min_char; |
| 51 | |
| 52 | /* Indexed by name[position] - min_char. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 53 | uword **bitmaps; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | } vlib_cli_parse_position_t; |
| 55 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 56 | typedef struct |
| 57 | { |
| 58 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | |
| 60 | u32 index; |
| 61 | } vlib_cli_sub_command_t; |
| 62 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 63 | typedef struct |
| 64 | { |
| 65 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
| 67 | u32 rule_index; |
| 68 | |
| 69 | u32 command_index; |
| 70 | } vlib_cli_sub_rule_t; |
| 71 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 72 | typedef struct |
| 73 | { |
| 74 | char *name; |
| 75 | char *short_help; |
| 76 | char *long_help; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | |
| 78 | /* Number of bytes in parsed data. Zero for vector. */ |
| 79 | uword data_size; |
| 80 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 81 | unformat_function_t *unformat_function; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
| 83 | /* Opaque for unformat function. */ |
| 84 | uword unformat_function_arg[2]; |
| 85 | } vlib_cli_parse_rule_t; |
| 86 | |
| 87 | /* CLI command callback function. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 88 | typedef clib_error_t *(vlib_cli_command_function_t) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | (struct vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 90 | unformat_input_t * input, struct vlib_cli_command_t * cmd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 92 | typedef struct vlib_cli_command_t |
| 93 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | /* Command path (e.g. "show something"). |
| 95 | Spaces delimit elements of path. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 96 | char *path; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | |
| 98 | /* Short/long help strings. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 99 | char *short_help; |
| 100 | char *long_help; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 101 | |
| 102 | /* Callback function. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 103 | vlib_cli_command_function_t *function; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
| 105 | /* Opaque. */ |
| 106 | uword function_arg; |
| 107 | |
| 108 | /* Known MP-safe? */ |
| 109 | uword is_mp_safe; |
| 110 | |
| 111 | /* Sub commands for this command. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 112 | vlib_cli_sub_command_t *sub_commands; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | |
| 114 | /* Hash table mapping name (e.g. last path element) to sub command index. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 115 | uword *sub_command_index_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
| 117 | /* bitmap[p][c][i] says whether sub-command i has character |
| 118 | c in position p. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 119 | vlib_cli_parse_position_t *sub_command_positions; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | |
| 121 | /* Hash table mapping name (e.g. last path element) to sub rule index. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 122 | uword *sub_rule_index_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 123 | |
| 124 | /* Vector of possible parse rules for this path. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 125 | vlib_cli_sub_rule_t *sub_rules; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
| 127 | /* List of CLI commands, built by constructors */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 128 | struct vlib_cli_command_t *next_cli_command; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Dave Barach | a1f5a95 | 2019-11-01 11:24:43 -0400 | [diff] [blame] | 130 | /* Hit counter */ |
| 131 | u32 hit_counter; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | } vlib_cli_command_t; |
| 133 | |
| 134 | typedef void (vlib_cli_output_function_t) (uword arg, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 135 | u8 * buffer, uword buffer_bytes); |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 136 | typedef struct vlib_cli_main_t |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 137 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | /* Vector of all known commands. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 139 | vlib_cli_command_t *commands; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
| 141 | /* Hash table mapping normalized path to index into all_commands. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 142 | uword *command_index_by_path; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | /* registration list added by constructors */ |
| 145 | vlib_cli_command_t *cli_command_registrations; |
Dave Barach | a1f5a95 | 2019-11-01 11:24:43 -0400 | [diff] [blame] | 146 | |
| 147 | /* index vector, to sort commands, etc. */ |
| 148 | u32 *sort_vector; |
| 149 | |
Tom Seidenberg | 6c81f5a | 2020-07-10 15:49:03 +0000 | [diff] [blame] | 150 | |
| 151 | /* performance counter callback */ |
| 152 | void (**perf_counter_cbs) |
| 153 | (struct vlib_cli_main_t *, u32 id, int before_or_after); |
| 154 | void (**perf_counter_cbs_tmp) |
| 155 | (struct vlib_cli_main_t *, u32 id, int before_or_after); |
Dave Barach | 3d0e0d1 | 2021-02-17 10:25:18 -0500 | [diff] [blame] | 156 | |
| 157 | /* cli log */ |
| 158 | vlib_log_class_t log; |
| 159 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | } vlib_cli_main_t; |
| 161 | |
Damjan Marion | 6e36351 | 2018-08-10 22:39:11 +0200 | [diff] [blame] | 162 | #ifndef CLIB_MARCH_VARIANT |
Damjan Marion | fd8deb4 | 2021-03-06 12:26:28 +0100 | [diff] [blame] | 163 | #define VLIB_CLI_COMMAND(x, ...) \ |
| 164 | __VA_ARGS__ vlib_cli_command_t x; \ |
| 165 | static void __vlib_cli_command_registration_##x (void) \ |
| 166 | __attribute__ ((__constructor__)); \ |
| 167 | static void __vlib_cli_command_registration_##x (void) \ |
| 168 | { \ |
| 169 | vlib_global_main_t *vgm = vlib_get_global_main (); \ |
| 170 | vlib_cli_main_t *cm = &vgm->cli_main; \ |
| 171 | x.next_cli_command = cm->cli_command_registrations; \ |
| 172 | cm->cli_command_registrations = &x; \ |
| 173 | } \ |
| 174 | static void __vlib_cli_command_unregistration_##x (void) \ |
| 175 | __attribute__ ((__destructor__)); \ |
| 176 | static void __vlib_cli_command_unregistration_##x (void) \ |
| 177 | { \ |
| 178 | vlib_global_main_t *vgm = vlib_get_global_main (); \ |
| 179 | vlib_cli_main_t *cm = &vgm->cli_main; \ |
| 180 | VLIB_REMOVE_FROM_LINKED_LIST (cm->cli_command_registrations, &x, \ |
| 181 | next_cli_command); \ |
| 182 | } \ |
| 183 | __VA_ARGS__ vlib_cli_command_t x |
Damjan Marion | 6e36351 | 2018-08-10 22:39:11 +0200 | [diff] [blame] | 184 | #else |
| 185 | /* create unused pointer to silence compiler warnings and get whole |
| 186 | function optimized out */ |
| 187 | #define VLIB_CLI_COMMAND(x,...) \ |
| 188 | static __clib_unused vlib_cli_command_t __clib_unused_##x |
| 189 | #endif |
| 190 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | #define VLIB_CLI_PARSE_RULE(x) \ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 192 | vlib_cli_parse_rule_t x |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 193 | /* Output to current CLI connection. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 194 | void vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | |
| 196 | /* Process CLI input. */ |
Ole Troan | 72d8758 | 2019-05-10 12:01:10 +0200 | [diff] [blame] | 197 | int vlib_cli_input (struct vlib_main_t *vm, |
| 198 | unformat_input_t * input, |
| 199 | vlib_cli_output_function_t * function, |
| 200 | uword function_arg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 202 | clib_error_t *vlib_cli_register (struct vlib_main_t *vm, |
| 203 | vlib_cli_command_t * c); |
| 204 | clib_error_t *vlib_cli_register_parse_rule (struct vlib_main_t *vm, |
| 205 | vlib_cli_parse_rule_t * c); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | |
Damjan Marion | c68b4cb | 2016-05-25 20:11:33 +0200 | [diff] [blame] | 207 | uword unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args); |
| 208 | |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 209 | /* Return an vector of strings consisting of possible auto-completions |
| 210 | * for a given input string */ |
| 211 | u8 **vlib_cli_get_possible_completions (u8 * input_str); |
| 212 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | #endif /* included_vlib_cli_h */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * fd.io coding-style-patch-verification: ON |
| 217 | * |
| 218 | * Local Variables: |
| 219 | * eval: (c-set-style "gnu") |
| 220 | * End: |
| 221 | */ |