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.c: Unix stdin/socket CLI. |
| 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 | */ |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 39 | /** |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 40 | * @file |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 41 | * @brief Unix stdin/socket command line interface. |
| 42 | * Provides a command line interface so humans can interact with VPP. |
| 43 | * This is predominantly a debugging and testing mechanism. |
| 44 | */ |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 45 | /*? %%clicmd:group_label Command line session %% ?*/ |
| 46 | /*? %%syscfg:group_label Command line session %% ?*/ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | |
| 48 | #include <vlib/vlib.h> |
| 49 | #include <vlib/unix/unix.h> |
| 50 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 51 | #include <ctype.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | #include <fcntl.h> |
| 53 | #include <sys/stat.h> |
| 54 | #include <termios.h> |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 55 | #include <signal.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | #include <unistd.h> |
| 57 | #include <arpa/telnet.h> |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 58 | #include <sys/ioctl.h> |
Damjan Marion | f661638 | 2017-06-21 12:01:37 +0200 | [diff] [blame] | 59 | #include <sys/types.h> |
| 60 | #include <unistd.h> |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 61 | #include <limits.h> |
Chris Luke | a9cf6af | 2018-09-05 21:00:52 -0400 | [diff] [blame] | 62 | #include <netinet/tcp.h> |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 63 | #include <math.h> |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 64 | #include <vppinfra/macros.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 66 | /** ANSI escape code. */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 67 | #define ESC "\x1b" |
| 68 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 69 | /** ANSI Control Sequence Introducer. */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 70 | #define CSI ESC "[" |
| 71 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 72 | /** ANSI clear screen. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 73 | #define ANSI_CLEAR CSI "2J" CSI "1;1H" |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 74 | /** ANSI reset color settings. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 75 | #define ANSI_RESET CSI "0m" |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 76 | /** ANSI Start bold text. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 77 | #define ANSI_BOLD CSI "1m" |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 78 | /** ANSI Stop bold text. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 79 | #define ANSI_DIM CSI "2m" |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 80 | /** ANSI Start dark red text. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 81 | #define ANSI_DRED ANSI_DIM CSI "31m" |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 82 | /** ANSI Start bright red text. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 83 | #define ANSI_BRED ANSI_BOLD CSI "31m" |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 84 | /** ANSI clear line cursor is on. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 85 | #define ANSI_CLEARLINE CSI "2K" |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 86 | /** ANSI scroll screen down one line. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 87 | #define ANSI_SCROLLDN CSI "1T" |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 88 | /** ANSI save cursor position. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 89 | #define ANSI_SAVECURSOR CSI "s" |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 90 | /** ANSI restore cursor position if previously saved. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 91 | #define ANSI_RESTCURSOR CSI "u" |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 92 | |
| 93 | /** Maximum depth into a byte stream from which to compile a Telnet |
Chris Luke | 2d8bf30 | 2017-11-12 22:26:37 -0500 | [diff] [blame] | 94 | * protocol message. This is a safety measure. */ |
Vladimir Isaev | b59095f | 2020-08-11 17:15:58 +0300 | [diff] [blame] | 95 | #define UNIX_CLI_MAX_DEPTH_TELNET 32 |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 96 | |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 97 | /** Maximum terminal width we will accept */ |
| 98 | #define UNIX_CLI_MAX_TERMINAL_WIDTH 512 |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 99 | /** Maximum terminal height we will accept */ |
| 100 | #define UNIX_CLI_MAX_TERMINAL_HEIGHT 512 |
Chris Luke | 2d8bf30 | 2017-11-12 22:26:37 -0500 | [diff] [blame] | 101 | /** Default terminal height */ |
| 102 | #define UNIX_CLI_DEFAULT_TERMINAL_HEIGHT 24 |
| 103 | /** Default terminal width */ |
| 104 | #define UNIX_CLI_DEFAULT_TERMINAL_WIDTH 80 |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 105 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 106 | /** A CLI banner line. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 107 | typedef struct |
| 108 | { |
| 109 | u8 *line; /**< The line to print. */ |
| 110 | u32 length; /**< The length of the line without terminating NUL. */ |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 111 | } unix_cli_banner_t; |
| 112 | |
| 113 | #define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 } |
| 114 | /** Plain welcome banner. */ |
| 115 | static unix_cli_banner_t unix_cli_banner[] = { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 116 | _(" _______ _ _ _____ ___ \n"), |
| 117 | _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"), |
| 118 | _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"), |
| 119 | _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"), |
| 120 | _("\n") |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 121 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 122 | |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 123 | /** ANSI color welcome banner. */ |
| 124 | static unix_cli_banner_t unix_cli_banner_color[] = { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 125 | _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"), |
| 126 | _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"), |
| 127 | _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"), |
| 128 | _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"), |
| 129 | _("\n") |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 130 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 131 | |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 132 | #undef _ |
| 133 | |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 134 | /** Pager line index */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 135 | typedef struct |
| 136 | { |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 137 | /** Index into pager_vector */ |
| 138 | u32 line; |
| 139 | |
| 140 | /** Offset of the string in the line */ |
| 141 | u32 offset; |
| 142 | |
| 143 | /** Length of the string in the line */ |
| 144 | u32 length; |
| 145 | } unix_cli_pager_index_t; |
| 146 | |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 147 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 148 | /** Unix CLI session. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 149 | typedef struct |
| 150 | { |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 151 | /** The file index held by unix.c */ |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 152 | u32 clib_file_index; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 154 | /** Vector of output pending write to file descriptor. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 155 | u8 *output_vector; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 156 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 157 | /** Vector of input saved by Unix input node to be processed by |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 158 | CLI process. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 159 | u8 *input_vector; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 161 | /** This session has command history. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 162 | u8 has_history; |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 163 | /** Array of vectors of commands in the history. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 164 | u8 **command_history; |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 165 | /** The command currently pointed at by the history cursor. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 166 | u8 *current_command; |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 167 | /** How far from the end of the history array the user has browsed. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | i32 excursion; |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 169 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 170 | /** Maximum number of history entries this session will store. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 171 | u32 history_limit; |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 172 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 173 | /** Current command line counter */ |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 174 | u32 command_number; |
| 175 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 176 | /** The string being searched for in the history. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 177 | u8 *search_key; |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 178 | /** If non-zero then the CLI is searching in the history array. |
| 179 | * - @c -1 means search backwards. |
| 180 | * - @c 1 means search forwards. |
| 181 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | int search_mode; |
| 183 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 184 | /** Position of the insert cursor on the current input line */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 185 | u32 cursor; |
| 186 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 187 | /** Line mode or char mode */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 188 | u8 line_mode; |
| 189 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 190 | /** Set if the CRLF mode wants CR + LF */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 191 | u8 crlf_mode; |
| 192 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 193 | /** Can we do ANSI output? */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 194 | u8 ansi_capable; |
| 195 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 196 | /** Has the session started? */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 197 | u8 started; |
| 198 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 199 | /** Disable the pager? */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 200 | u8 no_pager; |
| 201 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 202 | /** Whether the session is interactive or not. |
| 203 | * Controls things like initial banner, the CLI prompt etc. */ |
| 204 | u8 is_interactive; |
| 205 | |
| 206 | /** Whether the session is attached to a socket. */ |
| 207 | u8 is_socket; |
| 208 | |
| 209 | /** If EPIPE has been detected, prevent further write-related |
| 210 | * activity on the descriptor. |
| 211 | */ |
| 212 | u8 has_epipe; |
| 213 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 214 | /** Pager buffer */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 215 | u8 **pager_vector; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 216 | |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 217 | /** Index of line fragments in the pager buffer */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 218 | unix_cli_pager_index_t *pager_index; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 219 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 220 | /** Line number of top of page */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 221 | u32 pager_start; |
| 222 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 223 | /** Terminal width */ |
| 224 | u32 width; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 225 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 226 | /** Terminal height */ |
| 227 | u32 height; |
| 228 | |
| 229 | /** Process node identifier */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | u32 process_node_index; |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 231 | |
| 232 | /** The current direction of cursor travel. |
| 233 | * This is important since when advancing left-to-right, at the |
| 234 | * right hand edge of the console the terminal typically defers |
| 235 | * wrapping the cursor to the next line until a character is |
| 236 | * actually displayed. |
| 237 | * This messes up our heuristic for whether to use ANSI to return |
| 238 | * the cursor to the end of the line and instead we have to |
| 239 | * nudge the cursor to the next line. |
| 240 | * A Value of @c 0 means we're advancing left-to-right; @c 1 means |
| 241 | * the opposite. |
| 242 | */ |
| 243 | u8 cursor_direction; |
| 244 | |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 245 | /** Macro tables for this session */ |
| 246 | clib_macro_main_t macro_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | } unix_cli_file_t; |
| 248 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 249 | /** Resets the pager buffer and other data. |
| 250 | * @param f The CLI session whose pager needs to be reset. |
| 251 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 252 | always_inline void |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 253 | unix_cli_pager_reset (unix_cli_file_t * f) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 254 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 255 | u8 **p; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 256 | |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 257 | f->pager_start = 0; |
| 258 | |
| 259 | vec_free (f->pager_index); |
| 260 | f->pager_index = 0; |
| 261 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 262 | vec_foreach (p, f->pager_vector) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 263 | { |
| 264 | vec_free (*p); |
| 265 | } |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 266 | vec_free (f->pager_vector); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 267 | f->pager_vector = 0; |
| 268 | } |
| 269 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 270 | /** Release storage used by a CLI session. |
| 271 | * @param f The CLI session whose storage needs to be released. |
| 272 | */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 273 | always_inline void |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | unix_cli_file_free (unix_cli_file_t * f) |
| 275 | { |
| 276 | vec_free (f->output_vector); |
| 277 | vec_free (f->input_vector); |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 278 | unix_cli_pager_reset (f); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 281 | /** CLI actions */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 282 | typedef enum |
| 283 | { |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 284 | UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */ |
| 285 | UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */ |
| 286 | UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */ |
| 287 | UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */ |
| 288 | UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */ |
| 289 | UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */ |
| 290 | UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */ |
| 291 | UNIX_CLI_PARSE_ACTION_LEFT, /**< Left arrow */ |
| 292 | UNIX_CLI_PARSE_ACTION_RIGHT, /**< Right arrow */ |
| 293 | UNIX_CLI_PARSE_ACTION_HOME, /**< Home key (jump to start of line) */ |
| 294 | UNIX_CLI_PARSE_ACTION_END, /**< End key (jump to end of line) */ |
| 295 | UNIX_CLI_PARSE_ACTION_WORDLEFT, /**< Jump cursor to start of left word */ |
| 296 | UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */ |
| 297 | UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */ |
| 298 | UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */ |
Hiroki Shirokura | 67e4df1 | 2019-08-16 11:30:34 +0000 | [diff] [blame] | 299 | UNIX_CLI_PARSE_ACTION_ERASEWORDLEFT, /**< Erase word left */ |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 300 | UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */ |
| 301 | UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */ |
| 302 | UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */ |
| 303 | UNIX_CLI_PARSE_ACTION_YANK, /**< Undo last erase action */ |
| 304 | UNIX_CLI_PARSE_ACTION_TELNETIAC, /**< Telnet control code */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 305 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 306 | UNIX_CLI_PARSE_ACTION_PAGER_CRLF, /**< Enter pressed (CR, CRLF, LF, etc) */ |
| 307 | UNIX_CLI_PARSE_ACTION_PAGER_QUIT, /**< Exit the pager session */ |
| 308 | UNIX_CLI_PARSE_ACTION_PAGER_NEXT, /**< Scroll to next page */ |
| 309 | UNIX_CLI_PARSE_ACTION_PAGER_DN, /**< Scroll to next line */ |
| 310 | UNIX_CLI_PARSE_ACTION_PAGER_UP, /**< Scroll to previous line */ |
| 311 | UNIX_CLI_PARSE_ACTION_PAGER_TOP, /**< Scroll to first line */ |
| 312 | UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM, /**< Scroll to last line */ |
| 313 | UNIX_CLI_PARSE_ACTION_PAGER_PGDN, /**< Scroll to next page */ |
| 314 | UNIX_CLI_PARSE_ACTION_PAGER_PGUP, /**< Scroll to previous page */ |
| 315 | UNIX_CLI_PARSE_ACTION_PAGER_REDRAW, /**< Clear and redraw the page on the terminal */ |
| 316 | UNIX_CLI_PARSE_ACTION_PAGER_SEARCH, /**< Search the pager buffer */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 317 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 318 | UNIX_CLI_PARSE_ACTION_PARTIALMATCH, /**< Action parser found a partial match */ |
| 319 | UNIX_CLI_PARSE_ACTION_NOMATCH /**< Action parser did not find any match */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 320 | } unix_cli_parse_action_t; |
| 321 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 322 | /** @brief Mapping of input buffer strings to action values. |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 323 | * @note This won't work as a hash since we need to be able to do |
| 324 | * partial matches on the string. |
| 325 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 326 | typedef struct |
| 327 | { |
| 328 | u8 *input; /**< Input string to match. */ |
| 329 | u32 len; /**< Length of input without final NUL. */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 330 | unix_cli_parse_action_t action; /**< Action to take when matched. */ |
| 331 | } unix_cli_parse_actions_t; |
| 332 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 333 | /** @brief Given a capital ASCII letter character return a @c NUL terminated |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 334 | * string with the control code for that letter. |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 335 | * |
| 336 | * @param c An ASCII character. |
| 337 | * @return A @c NUL terminated string of type @c u8[]. |
| 338 | * |
| 339 | * @par Example |
| 340 | * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[]. |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 341 | */ |
| 342 | #define CTL(c) (u8[]){ (c) - '@', 0 } |
| 343 | |
| 344 | #define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) } |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 345 | /** |
| 346 | * Patterns to match on a CLI input stream. |
| 347 | * @showinitializer |
| 348 | */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 349 | static unix_cli_parse_actions_t unix_cli_parse_strings[] = { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 350 | /* Line handling */ |
| 351 | _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */ |
| 352 | _("\n", UNIX_CLI_PARSE_ACTION_CRLF), |
| 353 | _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */ |
| 354 | _("\r", UNIX_CLI_PARSE_ACTION_CRLF), |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 355 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 356 | /* Unix shell control codes */ |
| 357 | _(CTL ('B'), UNIX_CLI_PARSE_ACTION_LEFT), |
| 358 | _(CTL ('F'), UNIX_CLI_PARSE_ACTION_RIGHT), |
| 359 | _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP), |
| 360 | _(CTL ('N'), UNIX_CLI_PARSE_ACTION_DOWN), |
| 361 | _(CTL ('A'), UNIX_CLI_PARSE_ACTION_HOME), |
| 362 | _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END), |
| 363 | _(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT), |
| 364 | _(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT), |
| 365 | _(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT), |
Hiroki Shirokura | 67e4df1 | 2019-08-16 11:30:34 +0000 | [diff] [blame] | 366 | _(CTL ('W'), UNIX_CLI_PARSE_ACTION_ERASEWORDLEFT), |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 367 | _(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK), |
| 368 | _(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR), |
| 369 | _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */ |
| 370 | _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */ |
| 371 | _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */ |
| 372 | _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */ |
| 373 | _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 374 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 375 | /* VT100 Normal mode - Broadest support */ |
| 376 | _(CSI "A", UNIX_CLI_PARSE_ACTION_UP), |
| 377 | _(CSI "B", UNIX_CLI_PARSE_ACTION_DOWN), |
| 378 | _(CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT), |
| 379 | _(CSI "D", UNIX_CLI_PARSE_ACTION_LEFT), |
| 380 | _(CSI "H", UNIX_CLI_PARSE_ACTION_HOME), |
| 381 | _(CSI "F", UNIX_CLI_PARSE_ACTION_END), |
| 382 | _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */ |
| 383 | _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */ |
| 384 | _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 385 | |
| 386 | /* VT100 Application mode - Some Gnome Terminal functions use these */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 387 | _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP), |
| 388 | _(ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN), |
| 389 | _(ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT), |
| 390 | _(ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT), |
| 391 | _(ESC "OH", UNIX_CLI_PARSE_ACTION_HOME), |
| 392 | _(ESC "OF", UNIX_CLI_PARSE_ACTION_END), |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 393 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 394 | /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */ |
| 395 | _(CSI "1~", UNIX_CLI_PARSE_ACTION_HOME), |
| 396 | _(CSI "4~", UNIX_CLI_PARSE_ACTION_END), |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 397 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 398 | /* Emacs-ish history search */ |
| 399 | _(CTL ('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH), |
| 400 | _(CTL ('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH), |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 401 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 402 | /* Other protocol things */ |
| 403 | _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */ |
| 404 | _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */ |
| 405 | _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH) |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 406 | }; |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 407 | |
| 408 | /** |
| 409 | * Patterns to match when a CLI session is in the pager. |
| 410 | * @showinitializer |
| 411 | */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 412 | static unix_cli_parse_actions_t unix_cli_parse_pager[] = { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 413 | /* Line handling */ |
| 414 | _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */ |
| 415 | _("\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), |
| 416 | _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */ |
| 417 | _("\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 418 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 419 | /* Pager commands */ |
| 420 | _(" ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT), |
| 421 | _("q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT), |
| 422 | _(CTL ('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW), |
| 423 | _(CTL ('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW), |
| 424 | _("/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH), |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 425 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 426 | /* VT100 */ |
| 427 | _(CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP), |
| 428 | _(CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN), |
| 429 | _(CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP), |
| 430 | _(CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM), |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 431 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 432 | /* VT100 Application mode */ |
| 433 | _(ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP), |
| 434 | _(ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN), |
| 435 | _(ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP), |
| 436 | _(ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM), |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 437 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 438 | /* ANSI X3.41-1974 */ |
| 439 | _(CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP), |
| 440 | _(CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM), |
| 441 | _(CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP), |
| 442 | _(CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN), |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 443 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 444 | /* Other protocol things */ |
| 445 | _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */ |
| 446 | _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */ |
| 447 | _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 448 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 449 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 450 | #undef _ |
| 451 | |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 452 | /** CLI session telnet negotiation timer events. */ |
| 453 | typedef enum |
| 454 | { |
| 455 | UNIX_CLI_NEW_SESSION_EVENT_ADD, /**< Add a CLI session to the new session list */ |
| 456 | } unix_cli_timeout_event_type_t; |
| 457 | |
| 458 | /** Each new session is stored on a list with a deadline after which |
| 459 | * a prompt is issued, in case the session TELNET negotiation fails to |
| 460 | * complete. */ |
| 461 | typedef struct |
| 462 | { |
| 463 | uword cf_index; /**< Session index of the new session. */ |
| 464 | f64 deadline; /**< Deadline after which the new session must have a prompt. */ |
| 465 | } unix_cli_new_session_t; |
| 466 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 467 | /** CLI global state. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 468 | typedef struct |
| 469 | { |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 470 | /** Prompt string for CLI. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 471 | u8 *cli_prompt; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 473 | /** Vec pool of CLI sessions. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 474 | unix_cli_file_t *cli_file_pool; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 475 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 476 | /** Vec pool of unused session indices. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 477 | u32 *unused_cli_process_node_indices; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 479 | /** The session index of the stdin cli */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 480 | u32 stdin_cli_file_index; |
| 481 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 482 | /** File pool index of current input. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 483 | u32 current_input_file_index; |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 484 | |
| 485 | /** New session process node identifier */ |
| 486 | u32 new_session_process_node_index; |
| 487 | |
| 488 | /** List of new sessions */ |
| 489 | unix_cli_new_session_t *new_sessions; |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 490 | |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 491 | /** system default macro table */ |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 492 | clib_macro_main_t macro_main; |
| 493 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 494 | } unix_cli_main_t; |
| 495 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 496 | /** CLI global state */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 497 | static unix_cli_main_t unix_cli_main; |
| 498 | |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 499 | /** Return the macro main / tables we should use for this session |
| 500 | */ |
| 501 | static clib_macro_main_t * |
| 502 | get_macro_main (void) |
| 503 | { |
| 504 | unix_cli_main_t *cm = &unix_cli_main; |
| 505 | vlib_main_t *vm = vlib_get_main (); |
| 506 | vlib_process_t *cp = vlib_get_current_process (vm); |
| 507 | unix_cli_file_t *cf; |
| 508 | |
| 509 | if (pool_is_free_index (cm->cli_file_pool, cp->output_function_arg)) |
| 510 | return (&cm->macro_main); |
| 511 | |
| 512 | cf = pool_elt_at_index (cm->cli_file_pool, cp->output_function_arg); |
| 513 | |
| 514 | return (&cf->macro_main); |
| 515 | } |
| 516 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 517 | /** |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 518 | * @brief Search for a byte sequence in the action list. |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 519 | * |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 520 | * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with |
| 521 | * the bytes in @a input of maximum length @a ilen bytes. |
| 522 | * When a match is made @a *matched indicates how many bytes were matched. |
| 523 | * Returns a value from the enum @ref unix_cli_parse_action_t to indicate |
| 524 | * whether no match was found, a partial match was found or a complete |
| 525 | * match was found and what action, if any, should be taken. |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 526 | * |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 527 | * @param[in] a Actions list to search within. |
| 528 | * @param[in] input String fragment to search for. |
| 529 | * @param[in] ilen Length of the string in 'input'. |
| 530 | * @param[out] matched Pointer to an integer that will contain the number |
| 531 | * of bytes matched when a complete match is found. |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 532 | * |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 533 | * @return Action from @ref unix_cli_parse_action_t that the string fragment |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 534 | * matches. |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 535 | * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the |
| 536 | * whole input string matches the start of at least one action. |
| 537 | * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 538 | * match at all. |
| 539 | */ |
| 540 | static unix_cli_parse_action_t |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 541 | unix_cli_match_action (unix_cli_parse_actions_t * a, |
| 542 | u8 * input, u32 ilen, i32 * matched) |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 543 | { |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 544 | u8 partial = 0; |
| 545 | |
| 546 | while (a->input) |
| 547 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 548 | if (ilen >= a->len) |
| 549 | { |
| 550 | /* see if the start of the input buffer exactly matches the current |
| 551 | * action string. */ |
| 552 | if (memcmp (input, a->input, a->len) == 0) |
| 553 | { |
| 554 | *matched = a->len; |
| 555 | return a->action; |
| 556 | } |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | /* if the first ilen characters match, flag this as a partial - |
| 561 | * meaning keep collecting bytes in case of a future match */ |
| 562 | if (memcmp (input, a->input, ilen) == 0) |
| 563 | partial = 1; |
| 564 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 565 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 566 | /* check next action */ |
| 567 | a++; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | return partial ? |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 571 | UNIX_CLI_PARSE_ACTION_PARTIALMATCH : UNIX_CLI_PARSE_ACTION_NOMATCH; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 575 | /** Add bytes to the output vector and then flagg the I/O system that bytes |
| 576 | * are available to be sent. |
| 577 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 578 | static void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 579 | unix_cli_add_pending_output (clib_file_t * uf, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 580 | unix_cli_file_t * cf, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 581 | u8 * buffer, uword buffer_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 582 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 583 | clib_file_main_t *fm = &file_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 584 | |
| 585 | vec_add (cf->output_vector, buffer, buffer_bytes); |
| 586 | if (vec_len (cf->output_vector) > 0) |
| 587 | { |
| 588 | int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); |
| 589 | uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 590 | if (!skip_update) |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 591 | fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 595 | /** Delete all bytes from the output vector and flag the I/O system |
| 596 | * that no more bytes are available to be sent. |
| 597 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 598 | static void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 599 | unix_cli_del_pending_output (clib_file_t * uf, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 600 | unix_cli_file_t * cf, uword n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 601 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 602 | clib_file_main_t *fm = &file_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 603 | |
| 604 | vec_delete (cf->output_vector, n_bytes, 0); |
| 605 | if (vec_len (cf->output_vector) <= 0) |
| 606 | { |
| 607 | int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); |
| 608 | uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 609 | if (!skip_update) |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 610 | fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 614 | /** @brief A bit like strchr with a buffer length limit. |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 615 | * Search a buffer for the first instance of a character up to the limit of |
| 616 | * the buffer length. If found then return the position of that character. |
| 617 | * |
| 618 | * The key departure from strchr is that if the character is not found then |
| 619 | * return the buffer length. |
| 620 | * |
| 621 | * @param chr The byte value to search for. |
| 622 | * @param str The buffer in which to search for the value. |
| 623 | * @param len The depth into the buffer to search. |
| 624 | * |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 625 | * @return The index of the first occurrence of \c chr. If \c chr is not |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 626 | * found then \c len instead. |
| 627 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 628 | always_inline word |
| 629 | unix_vlib_findchr (u8 chr, u8 * str, word len) |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 630 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 631 | word i = 0; |
| 632 | for (i = 0; i < len; i++, str++) |
| 633 | { |
| 634 | if (*str == chr) |
| 635 | return i; |
| 636 | } |
| 637 | return len; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 638 | } |
| 639 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 640 | /** @brief Send a buffer to the CLI stream if possible, enqueue it otherwise. |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 641 | * Attempts to write given buffer to the file descriptor of the given |
| 642 | * Unix CLI session. If that session already has data in the output buffer |
| 643 | * or if the write attempt tells us to try again later then the given buffer |
| 644 | * is appended to the pending output buffer instead. |
| 645 | * |
| 646 | * This is typically called only from \c unix_vlib_cli_output_cooked since |
| 647 | * that is where CRLF handling occurs or from places where we explicitly do |
| 648 | * not want cooked handling. |
| 649 | * |
| 650 | * @param cf Unix CLI session of the desired stream to write to. |
| 651 | * @param uf The Unix file structure of the desired stream to write to. |
| 652 | * @param buffer Pointer to the buffer that needs to be written. |
| 653 | * @param buffer_bytes The number of bytes from \c buffer to write. |
| 654 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 655 | static void |
| 656 | unix_vlib_cli_output_raw (unix_cli_file_t * cf, |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 657 | clib_file_t * uf, u8 * buffer, uword buffer_bytes) |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 658 | { |
| 659 | int n = 0; |
| 660 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 661 | if (cf->has_epipe) /* don't try writing anything */ |
| 662 | return; |
| 663 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 664 | if (vec_len (cf->output_vector) == 0) |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 665 | { |
| 666 | if (cf->is_socket) |
| 667 | /* If it's a socket we use MSG_NOSIGNAL to prevent SIGPIPE */ |
| 668 | n = send (uf->file_descriptor, buffer, buffer_bytes, MSG_NOSIGNAL); |
| 669 | else |
| 670 | n = write (uf->file_descriptor, buffer, buffer_bytes); |
| 671 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 672 | |
| 673 | if (n < 0 && errno != EAGAIN) |
| 674 | { |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 675 | if (errno == EPIPE) |
| 676 | { |
| 677 | /* connection closed on us */ |
| 678 | unix_main_t *um = &unix_main; |
| 679 | cf->has_epipe = 1; |
| 680 | vlib_process_signal_event (um->vlib_main, cf->process_node_index, |
| 681 | UNIX_CLI_PROCESS_EVENT_QUIT, |
| 682 | uf->private_data); |
| 683 | } |
| 684 | else |
| 685 | { |
| 686 | clib_unix_warning ("write"); |
| 687 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 688 | } |
| 689 | else if ((word) n < (word) buffer_bytes) |
| 690 | { |
| 691 | /* We got EAGAIN or we already have stuff in the buffer; |
| 692 | * queue up whatever didn't get sent for later. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 693 | if (n < 0) |
| 694 | n = 0; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 695 | unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n); |
| 696 | } |
| 697 | } |
| 698 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 699 | /** @brief Process a buffer for CRLF handling before outputting it to the CLI. |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 700 | * |
| 701 | * @param cf Unix CLI session of the desired stream to write to. |
| 702 | * @param uf The Unix file structure of the desired stream to write to. |
| 703 | * @param buffer Pointer to the buffer that needs to be written. |
| 704 | * @param buffer_bytes The number of bytes from \c buffer to write. |
| 705 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 706 | static void |
| 707 | unix_vlib_cli_output_cooked (unix_cli_file_t * cf, |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 708 | clib_file_t * uf, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 709 | u8 * buffer, uword buffer_bytes) |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 710 | { |
| 711 | word end = 0, start = 0; |
| 712 | |
| 713 | while (end < buffer_bytes) |
| 714 | { |
| 715 | if (cf->crlf_mode) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 716 | { |
| 717 | /* iterate the line on \n's so we can insert a \r before it */ |
| 718 | end = unix_vlib_findchr ('\n', |
| 719 | buffer + start, |
| 720 | buffer_bytes - start) + start; |
| 721 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 722 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 723 | { |
| 724 | /* otherwise just send the whole buffer */ |
| 725 | end = buffer_bytes; |
| 726 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 727 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 728 | unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 729 | |
| 730 | if (cf->crlf_mode) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 731 | { |
| 732 | if (end < buffer_bytes) |
| 733 | { |
| 734 | unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2); |
| 735 | end++; /* skip the \n that we already sent */ |
| 736 | } |
| 737 | start = end; |
| 738 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 739 | } |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 740 | |
| 741 | /* Use the last character to determine the last direction of the cursor. */ |
| 742 | if (buffer_bytes > 0) |
| 743 | cf->cursor_direction = (buffer[buffer_bytes - 1] == (u8) '\b'); |
| 744 | } |
| 745 | |
| 746 | /** @brief Moves the terminal cursor one character to the left, with |
| 747 | * special handling when it reaches the left edge of the terminal window. |
| 748 | * |
| 749 | * Ordinarily we can simply send a '\b' to move the cursor left, however |
| 750 | * most terminals will not reverse-wrap to the end of the previous line |
| 751 | * if the cursor is in the left-most column. To counter this we must |
| 752 | * check the cursor position + prompt length modulo terminal width and |
| 753 | * if available use some other means, such as ANSI terminal escape |
| 754 | * sequences, to move the cursor. |
| 755 | * |
| 756 | * @param cf Unix CLI session of the desired stream to write to. |
| 757 | * @param uf The Unix file structure of the desired stream to write to. |
| 758 | */ |
| 759 | static void |
| 760 | unix_vlib_cli_output_cursor_left (unix_cli_file_t * cf, clib_file_t * uf) |
| 761 | { |
| 762 | unix_cli_main_t *cm = &unix_cli_main; |
| 763 | static u8 *ansi = 0; /* assumes no reentry */ |
| 764 | u32 position; |
| 765 | |
| 766 | if (!cf->is_interactive || !cf->ansi_capable || !cf->width) |
| 767 | { |
| 768 | /* No special handling for dumb terminals */ |
| 769 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); |
| 770 | return; |
| 771 | } |
| 772 | |
| 773 | position = ((u32) vec_len (cm->cli_prompt) + cf->cursor) % cf->width; |
| 774 | |
| 775 | if (position != 0) |
| 776 | { |
| 777 | /* No special handling required if we're not at the left edge */ |
| 778 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | if (!cf->cursor_direction) |
| 783 | { |
| 784 | /* Special handling for when we are at the left edge but |
| 785 | * the cursor was going left-to-right, but in this situation |
| 786 | * xterm-like terminals actually hide the cursor off the right |
| 787 | * edge. A \b here seems to jump one char too many, so let's |
| 788 | * force the cursor onto the next line instead. |
| 789 | */ |
| 790 | if (cf->cursor < vec_len (cf->current_command)) |
| 791 | unix_vlib_cli_output_cooked (cf, uf, &cf->current_command[cf->cursor], |
| 792 | 1); |
| 793 | else |
| 794 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
| 795 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1); |
| 796 | } |
| 797 | |
| 798 | /* Relocate the cursor at the right hand edge one line above */ |
| 799 | ansi = format (ansi, CSI "A" CSI "%dC", cf->width - 1); |
| 800 | unix_vlib_cli_output_cooked (cf, uf, ansi, vec_len (ansi)); |
| 801 | vec_reset_length (ansi); /* keep the vec around for next time */ |
| 802 | cf->cursor_direction = 1; /* going backwards now */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 803 | } |
| 804 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 805 | /** @brief Output the CLI prompt */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 806 | static void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 807 | unix_cli_cli_prompt (unix_cli_file_t * cf, clib_file_t * uf) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 808 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 809 | unix_cli_main_t *cm = &unix_cli_main; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 810 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 811 | if (cf->is_interactive) /* Only interactive sessions get a prompt */ |
| 812 | unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt, |
| 813 | vec_len (cm->cli_prompt)); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 814 | } |
| 815 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 816 | /** @brief Output a pager prompt and show number of buffered lines */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 817 | static void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 818 | unix_cli_pager_prompt (unix_cli_file_t * cf, clib_file_t * uf) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 819 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 820 | u8 *prompt; |
Chris Luke | 270b6de | 2016-05-19 14:23:25 -0400 | [diff] [blame] | 821 | u32 h; |
| 822 | |
| 823 | h = cf->pager_start + (cf->height - 1); |
| 824 | if (h > vec_len (cf->pager_index)) |
| 825 | h = vec_len (cf->pager_index); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 826 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 827 | prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s", |
| 828 | cf->ansi_capable ? ANSI_BOLD : "", |
| 829 | cf->pager_start + 1, |
| 830 | h, |
| 831 | vec_len (cf->pager_index), |
| 832 | cf->ansi_capable ? ANSI_RESET : ""); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 833 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 834 | unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt)); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 835 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 836 | vec_free (prompt); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 837 | } |
| 838 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 839 | /** @brief Output a pager "skipping" message */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 840 | static void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 841 | unix_cli_pager_message (unix_cli_file_t * cf, clib_file_t * uf, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 842 | char *message, char *postfix) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 843 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 844 | u8 *prompt; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 845 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 846 | prompt = format (0, "\r%s-- %s --%s%s", |
| 847 | cf->ansi_capable ? ANSI_BOLD : "", |
| 848 | message, cf->ansi_capable ? ANSI_RESET : "", postfix); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 849 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 850 | unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt)); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 851 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 852 | vec_free (prompt); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 853 | } |
| 854 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 855 | /** @brief Erase the printed pager prompt */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 856 | static void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 857 | unix_cli_pager_prompt_erase (unix_cli_file_t * cf, clib_file_t * uf) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 858 | { |
| 859 | if (cf->ansi_capable) |
| 860 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 861 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1); |
| 862 | unix_vlib_cli_output_cooked (cf, uf, |
| 863 | (u8 *) ANSI_CLEARLINE, |
| 864 | sizeof (ANSI_CLEARLINE) - 1); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 865 | } |
| 866 | else |
| 867 | { |
| 868 | int i; |
| 869 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 870 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1); |
| 871 | for (i = 0; i < cf->width - 1; i++) |
| 872 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
| 873 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 877 | /** @brief Uses an ANSI escape sequence to move the cursor */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 878 | static void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 879 | unix_cli_ansi_cursor (unix_cli_file_t * cf, clib_file_t * uf, u16 x, u16 y) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 880 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 881 | u8 *str; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 882 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 883 | str = format (0, "%s%d;%dH", CSI, y, x); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 884 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 885 | unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str)); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 886 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 887 | vec_free (str); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 888 | } |
| 889 | |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 890 | /** Redraw the currently displayed page of text. |
| 891 | * @param cf CLI session to redraw the pager buffer of. |
| 892 | * @param uf Unix file of the CLI session. |
| 893 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 894 | static void |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 895 | unix_cli_pager_redraw (unix_cli_file_t * cf, clib_file_t * uf) |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 896 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 897 | unix_cli_pager_index_t *pi = NULL; |
| 898 | u8 *line = NULL; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 899 | word i; |
| 900 | |
| 901 | /* No active pager? Do nothing. */ |
| 902 | if (!vec_len (cf->pager_index)) |
| 903 | return; |
| 904 | |
| 905 | if (cf->ansi_capable) |
| 906 | { |
| 907 | /* If we have ANSI, send the clear screen sequence */ |
| 908 | unix_vlib_cli_output_cooked (cf, uf, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 909 | (u8 *) ANSI_CLEAR, |
| 910 | sizeof (ANSI_CLEAR) - 1); |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 911 | } |
| 912 | else |
| 913 | { |
| 914 | /* Otherwise make sure we're on a blank line */ |
| 915 | unix_cli_pager_prompt_erase (cf, uf); |
| 916 | } |
| 917 | |
| 918 | /* (Re-)send the current page of content */ |
| 919 | for (i = 0; i < cf->height - 1 && |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 920 | i + cf->pager_start < vec_len (cf->pager_index); i++) |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 921 | { |
| 922 | pi = &cf->pager_index[cf->pager_start + i]; |
| 923 | line = cf->pager_vector[pi->line] + pi->offset; |
| 924 | |
| 925 | unix_vlib_cli_output_cooked (cf, uf, line, pi->length); |
| 926 | } |
| 927 | /* if the last line didn't end in newline, add a newline */ |
| 928 | if (pi && line[pi->length - 1] != '\n') |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 929 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 930 | |
| 931 | unix_cli_pager_prompt (cf, uf); |
| 932 | } |
| 933 | |
| 934 | /** @brief Process and add a line to the pager index. |
| 935 | * In normal operation this function will take the given character string |
| 936 | * found in @c line and with length @c len_or_index and iterates the over the |
| 937 | * contents, adding each line of text discovered within it to the |
| 938 | * pager index. Lines are identified by newlines ("<code>\\n</code>") and by |
| 939 | * strings longer than the width of the terminal. |
| 940 | * |
| 941 | * If instead @c line is @c NULL then @c len_or_index is taken to mean the |
| 942 | * index of an existing line in the pager buffer; this simply means that the |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 943 | * input line does not need to be cloned since we already have it. This is |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 944 | * typical if we are reindexing the pager buffer. |
| 945 | * |
| 946 | * @param cf The CLI session whose pager we are adding to. |
| 947 | * @param line The string of text to be indexed into the pager buffer. |
| 948 | * If @c line is @c NULL then the mode of operation |
| 949 | * changes slightly; see the description above. |
| 950 | * @param len_or_index If @c line is a pointer to a string then this parameter |
| 951 | * indicates the length of that string; Otherwise this |
| 952 | * value provides the index in the pager buffer of an |
| 953 | * existing string to be indexed. |
| 954 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 955 | static void |
| 956 | unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index) |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 957 | { |
Neale Ranns | 9c2c243 | 2017-12-05 13:34:36 -0800 | [diff] [blame] | 958 | u8 *p = NULL; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 959 | word i, j, k; |
| 960 | word line_index, len; |
| 961 | u32 width = cf->width; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 962 | unix_cli_pager_index_t *pi; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 963 | |
| 964 | if (line == NULL) |
| 965 | { |
| 966 | /* Use a line already in the pager buffer */ |
| 967 | line_index = len_or_index; |
Neale Ranns | 9c2c243 | 2017-12-05 13:34:36 -0800 | [diff] [blame] | 968 | if (cf->pager_vector != NULL) |
| 969 | p = cf->pager_vector[line_index]; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 970 | len = vec_len (p); |
| 971 | } |
| 972 | else |
| 973 | { |
| 974 | len = len_or_index; |
| 975 | /* Add a copy of the raw string to the pager buffer */ |
| 976 | p = vec_new (u8, len); |
| 977 | clib_memcpy (p, line, len); |
| 978 | |
| 979 | /* store in pager buffer */ |
| 980 | line_index = vec_len (cf->pager_vector); |
| 981 | vec_add1 (cf->pager_vector, p); |
| 982 | } |
| 983 | |
| 984 | i = 0; |
| 985 | while (i < len) |
| 986 | { |
| 987 | /* Find the next line, or run to terminal width, or run to EOL */ |
| 988 | int l = len - i; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 989 | j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width); |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 990 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 991 | if (j < l && p[j] == '\n') /* incl \n */ |
| 992 | j++; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 993 | |
| 994 | /* Add the line to the index */ |
| 995 | k = vec_len (cf->pager_index); |
| 996 | vec_validate (cf->pager_index, k); |
| 997 | pi = &cf->pager_index[k]; |
| 998 | |
| 999 | pi->line = line_index; |
| 1000 | pi->offset = i; |
| 1001 | pi->length = j; |
| 1002 | |
| 1003 | i += j; |
| 1004 | p += j; |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /** @brief Reindex entire pager buffer. |
| 1009 | * Resets the current pager index and then re-adds the lines in the pager |
| 1010 | * buffer to the index. |
| 1011 | * |
| 1012 | * Additionally this function attempts to retain the current page start |
| 1013 | * line offset by searching for the same top-of-screen line in the new index. |
| 1014 | * |
| 1015 | * @param cf The CLI session whose pager buffer should be reindexed. |
| 1016 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1017 | static void |
| 1018 | unix_cli_pager_reindex (unix_cli_file_t * cf) |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1019 | { |
| 1020 | word i, old_line, old_offset; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1021 | unix_cli_pager_index_t *pi; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1022 | |
| 1023 | /* If there is nothing in the pager buffer then make sure the index |
| 1024 | * is empty and move on. |
| 1025 | */ |
| 1026 | if (cf->pager_vector == 0) |
| 1027 | { |
| 1028 | vec_reset_length (cf->pager_index); |
| 1029 | return; |
| 1030 | } |
| 1031 | |
| 1032 | /* Retain a pointer to the current page start line so we can |
| 1033 | * find it later |
| 1034 | */ |
| 1035 | pi = &cf->pager_index[cf->pager_start]; |
| 1036 | old_line = pi->line; |
| 1037 | old_offset = pi->offset; |
| 1038 | |
| 1039 | /* Re-add the buffered lines to the index */ |
| 1040 | vec_reset_length (cf->pager_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1041 | vec_foreach_index (i, cf->pager_vector) |
| 1042 | { |
| 1043 | unix_cli_pager_add_line (cf, NULL, i); |
| 1044 | } |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1045 | |
| 1046 | /* Attempt to re-locate the previously stored page start line */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1047 | vec_foreach_index (i, cf->pager_index) |
| 1048 | { |
| 1049 | pi = &cf->pager_index[i]; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1050 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1051 | if (pi->line == old_line && |
| 1052 | (pi->offset <= old_offset || pi->offset + pi->length > old_offset)) |
| 1053 | { |
| 1054 | /* Found it! */ |
| 1055 | cf->pager_start = i; |
| 1056 | break; |
| 1057 | } |
| 1058 | } |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1059 | |
| 1060 | /* In case the start line was not found (rare), ensure the pager start |
| 1061 | * index is within bounds |
| 1062 | */ |
| 1063 | if (cf->pager_start >= vec_len (cf->pager_index)) |
| 1064 | { |
Chris Luke | 270b6de | 2016-05-19 14:23:25 -0400 | [diff] [blame] | 1065 | if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1066 | cf->pager_start = 0; |
Chris Luke | 270b6de | 2016-05-19 14:23:25 -0400 | [diff] [blame] | 1067 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1068 | cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1); |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | /** VLIB CLI output function. |
| 1073 | * |
| 1074 | * If the terminal has a pager configured then this function takes care |
| 1075 | * of collating output into the pager buffer; ensuring only the first page |
| 1076 | * is displayed and any lines in excess of the first page are buffered. |
| 1077 | * |
| 1078 | * If the maximum number of index lines in the buffer is exceeded then the |
| 1079 | * pager is cancelled and the contents of the current buffer are sent to the |
| 1080 | * terminal. |
| 1081 | * |
| 1082 | * If there is no pager configured then the output is sent directly to the |
| 1083 | * terminal. |
| 1084 | * |
| 1085 | * @param cli_file_index Index of the CLI session where this output is |
| 1086 | * directed. |
| 1087 | * @param buffer String of printabe bytes to be output. |
| 1088 | * @param buffer_bytes The number of bytes in @c buffer to be output. |
| 1089 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1090 | static void |
| 1091 | unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1092 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1093 | unix_main_t *um = &unix_main; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1094 | clib_file_main_t *fm = &file_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1095 | unix_cli_main_t *cm = &unix_cli_main; |
| 1096 | unix_cli_file_t *cf; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1097 | clib_file_t *uf; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1098 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1099 | cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1100 | uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1101 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1102 | if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0) |
| 1103 | { |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1104 | unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1105 | } |
| 1106 | else |
| 1107 | { |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1108 | word row = vec_len (cf->pager_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1109 | u8 *line; |
| 1110 | unix_cli_pager_index_t *pi; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1111 | |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1112 | /* Index and add the output lines to the pager buffer. */ |
| 1113 | unix_cli_pager_add_line (cf, buffer, buffer_bytes); |
| 1114 | |
| 1115 | /* Now iterate what was added to display the lines. |
| 1116 | * If we reach the bottom of the page, display a prompt. |
| 1117 | */ |
| 1118 | while (row < vec_len (cf->pager_index)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1119 | { |
| 1120 | if (row < cf->height - 1) |
| 1121 | { |
| 1122 | /* output this line */ |
| 1123 | pi = &cf->pager_index[row]; |
| 1124 | line = cf->pager_vector[pi->line] + pi->offset; |
| 1125 | unix_vlib_cli_output_cooked (cf, uf, line, pi->length); |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1126 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1127 | /* if the last line didn't end in newline, and we're at the |
| 1128 | * bottom of the page, add a newline */ |
| 1129 | if (line[pi->length - 1] != '\n' && row == cf->height - 2) |
| 1130 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
| 1131 | } |
| 1132 | else |
| 1133 | { |
| 1134 | /* Display the pager prompt every 10 lines */ |
| 1135 | if (!(row % 10)) |
| 1136 | unix_cli_pager_prompt (cf, uf); |
| 1137 | } |
| 1138 | row++; |
| 1139 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1140 | |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1141 | /* Check if we went over the pager buffer limit */ |
| 1142 | if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1143 | { |
| 1144 | /* Stop using the pager for the remainder of this CLI command */ |
| 1145 | cf->no_pager = 2; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1146 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1147 | /* If we likely printed the prompt, erase it */ |
| 1148 | if (vec_len (cf->pager_index) > cf->height - 1) |
| 1149 | unix_cli_pager_prompt_erase (cf, uf); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1150 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1151 | /* Dump out the contents of the buffer */ |
| 1152 | for (row = cf->pager_start + (cf->height - 1); |
| 1153 | row < vec_len (cf->pager_index); row++) |
| 1154 | { |
| 1155 | pi = &cf->pager_index[row]; |
| 1156 | line = cf->pager_vector[pi->line] + pi->offset; |
| 1157 | unix_vlib_cli_output_cooked (cf, uf, line, pi->length); |
| 1158 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1159 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1160 | unix_cli_pager_reset (cf); |
| 1161 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1162 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1165 | /** Identify whether a terminal type is ANSI capable. |
| 1166 | * |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 1167 | * Compares the string given in @c term with a list of terminal types known |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1168 | * to support ANSI escape sequences. |
| 1169 | * |
| 1170 | * This list contains, for example, @c xterm, @c screen and @c ansi. |
| 1171 | * |
| 1172 | * @param term A string with a terminal type in it. |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 1173 | * @param len The length of the string in @c term. |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 1174 | * |
| 1175 | * @return @c 1 if the terminal type is recognized as supporting ANSI |
| 1176 | * terminal sequences; @c 0 otherwise. |
| 1177 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1178 | static u8 |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 1179 | unix_cli_terminal_type_ansi (u8 * term, uword len) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1180 | { |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1181 | /* This may later be better done as a hash of some sort. */ |
| 1182 | #define _(a) do { \ |
| 1183 | if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \ |
| 1184 | } while(0) |
| 1185 | |
| 1186 | _("xterm"); |
| 1187 | _("xterm-color"); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1188 | _("xterm-256color"); /* iTerm on Mac */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1189 | _("screen"); |
Damjan Marion | 6e8ab16 | 2017-06-05 21:56:12 +0200 | [diff] [blame] | 1190 | _("screen-256color"); /* Screen and tmux */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1191 | _("ansi"); /* Microsoft Telnet */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1192 | #undef _ |
| 1193 | |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 1197 | /** Identify whether a terminal type is non-interactive. |
| 1198 | * |
| 1199 | * Compares the string given in @c term with a list of terminal types known |
| 1200 | * to be non-interactive, as send by tools such as @c vppctl . |
| 1201 | * |
| 1202 | * This list contains, for example, @c vppctl. |
| 1203 | * |
| 1204 | * @param term A string with a terminal type in it. |
| 1205 | * @param len The length of the string in @c term. |
| 1206 | * |
| 1207 | * @return @c 1 if the terminal type is recognized as being non-interactive; |
| 1208 | * @c 0 otherwise. |
| 1209 | */ |
| 1210 | static u8 |
| 1211 | unix_cli_terminal_type_noninteractive (u8 * term, uword len) |
| 1212 | { |
| 1213 | /* This may later be better done as a hash of some sort. */ |
| 1214 | #define _(a) do { \ |
| 1215 | if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \ |
| 1216 | } while(0) |
| 1217 | |
| 1218 | _("vppctl"); |
| 1219 | #undef _ |
| 1220 | |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | /** Set a session to be non-interactive. */ |
| 1225 | static void |
| 1226 | unix_cli_set_session_noninteractive (unix_cli_file_t * cf) |
| 1227 | { |
| 1228 | /* Non-interactive sessions don't get these */ |
| 1229 | cf->is_interactive = 0; |
| 1230 | cf->no_pager = 1; |
| 1231 | cf->history_limit = 0; |
| 1232 | cf->has_history = 0; |
| 1233 | cf->line_mode = 1; |
| 1234 | } |
| 1235 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 1236 | /** @brief Emit initial welcome banner and prompt on a connection. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1237 | static void |
| 1238 | unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf) |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1239 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1240 | unix_main_t *um = &unix_main; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1241 | clib_file_main_t *fm = &file_main; |
| 1242 | clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index); |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 1243 | unix_cli_banner_t *banner; |
| 1244 | int i, len; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1245 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 1246 | /* Mark the session as started if we get here */ |
| 1247 | cf->started = 1; |
| 1248 | |
| 1249 | if (!(cf->is_interactive)) /* No banner for non-interactive sessions */ |
| 1250 | return; |
| 1251 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1252 | /* |
| 1253 | * Put the first bytes directly into the buffer so that further output is |
| 1254 | * queued until everything is ready. (oterwise initial prompt can appear |
| 1255 | * mid way through VPP initialization) |
| 1256 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1257 | unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1); |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 1258 | |
Damjan Marion | 06d8226 | 2020-10-21 12:43:40 +0200 | [diff] [blame] | 1259 | if (!um->cli_no_banner && (um->flags & UNIX_FLAG_NOBANNER) == 0) |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 1260 | { |
Damjan Marion | 06d8226 | 2020-10-21 12:43:40 +0200 | [diff] [blame] | 1261 | if (cf->ansi_capable && (um->flags & UNIX_FLAG_NOCOLOR) == 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1262 | { |
| 1263 | banner = unix_cli_banner_color; |
| 1264 | len = ARRAY_LEN (unix_cli_banner_color); |
| 1265 | } |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 1266 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1267 | { |
| 1268 | banner = unix_cli_banner; |
| 1269 | len = ARRAY_LEN (unix_cli_banner); |
| 1270 | } |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 1271 | |
| 1272 | for (i = 0; i < len; i++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1273 | { |
| 1274 | unix_vlib_cli_output_cooked (cf, uf, |
| 1275 | banner[i].line, banner[i].length); |
| 1276 | } |
| 1277 | } |
Chris Luke | 572d812 | 2016-04-25 13:49:07 -0400 | [diff] [blame] | 1278 | |
| 1279 | /* Prompt. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1280 | unix_cli_cli_prompt (cf, uf); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1281 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1282 | } |
| 1283 | |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 1284 | /** |
| 1285 | * @brief A failsafe manager that ensures CLI sessions issue an initial |
| 1286 | * prompt if TELNET negotiation fails. |
| 1287 | */ |
| 1288 | static uword |
| 1289 | unix_cli_new_session_process (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 1290 | vlib_frame_t * f) |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1291 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1292 | unix_cli_main_t *cm = &unix_cli_main; |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 1293 | uword event_type, *event_data = 0; |
| 1294 | f64 wait = 10.0; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1295 | |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 1296 | while (1) |
| 1297 | { |
| 1298 | if (vec_len (cm->new_sessions) > 0) |
| 1299 | wait = vlib_process_wait_for_event_or_clock (vm, wait); |
| 1300 | else |
| 1301 | vlib_process_wait_for_event (vm); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1302 | |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 1303 | event_type = vlib_process_get_events (vm, &event_data); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1304 | |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 1305 | switch (event_type) |
| 1306 | { |
| 1307 | case ~0: /* no events => timeout */ |
| 1308 | break; |
| 1309 | |
| 1310 | case UNIX_CLI_NEW_SESSION_EVENT_ADD: |
| 1311 | { |
| 1312 | /* Add an identifier to the new session list */ |
| 1313 | unix_cli_new_session_t ns; |
| 1314 | |
Vladislav Grishenko | 49ebbf7 | 2021-12-29 14:30:32 +0500 | [diff] [blame] | 1315 | /* Check the connection didn't close already */ |
| 1316 | if (pool_is_free_index (cm->cli_file_pool, event_data[0])) |
| 1317 | break; |
| 1318 | |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 1319 | ns.cf_index = event_data[0]; |
| 1320 | ns.deadline = vlib_time_now (vm) + 1.0; |
| 1321 | |
| 1322 | vec_add1 (cm->new_sessions, ns); |
| 1323 | |
| 1324 | if (wait > 0.1) |
| 1325 | wait = 0.1; /* force a re-evaluation soon, but not too soon */ |
| 1326 | } |
| 1327 | break; |
| 1328 | |
| 1329 | default: |
| 1330 | clib_warning ("BUG: unknown event type 0x%wx", event_type); |
| 1331 | break; |
| 1332 | } |
| 1333 | |
| 1334 | vec_reset_length (event_data); |
| 1335 | |
| 1336 | if (vlib_process_suspend_time_is_zero (wait)) |
| 1337 | { |
| 1338 | /* Scan the list looking for expired deadlines. |
| 1339 | * Emit needed prompts and remove from the list. |
| 1340 | * While scanning, look for the nearest new deadline |
| 1341 | * for the next iteration. |
| 1342 | * Since the list is ordered with newest sessions first |
| 1343 | * we can make assumptions about expired sessions being |
| 1344 | * contiguous at the beginning and the next deadline is the |
| 1345 | * next entry on the list, if any. |
| 1346 | */ |
| 1347 | f64 now = vlib_time_now (vm); |
| 1348 | unix_cli_new_session_t *nsp; |
| 1349 | word index = 0; |
| 1350 | |
| 1351 | wait = INFINITY; |
| 1352 | |
| 1353 | vec_foreach (nsp, cm->new_sessions) |
| 1354 | { |
| 1355 | if (vlib_process_suspend_time_is_zero (nsp->deadline - now)) |
| 1356 | { |
| 1357 | /* Deadline reached */ |
| 1358 | unix_cli_file_t *cf; |
| 1359 | |
| 1360 | /* Mark the highwater */ |
| 1361 | index++; |
| 1362 | |
| 1363 | /* Check the connection didn't close already */ |
| 1364 | if (pool_is_free_index (cm->cli_file_pool, nsp->cf_index)) |
| 1365 | continue; |
| 1366 | |
| 1367 | cf = pool_elt_at_index (cm->cli_file_pool, nsp->cf_index); |
| 1368 | |
| 1369 | if (!cf->started) |
| 1370 | unix_cli_file_welcome (cm, cf); |
| 1371 | } |
| 1372 | else |
| 1373 | { |
| 1374 | wait = nsp->deadline - now; |
| 1375 | break; |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | if (index) |
| 1380 | { |
| 1381 | /* We have sessions to remove */ |
| 1382 | vec_delete (cm->new_sessions, index, 0); |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | return 0; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1388 | } |
| 1389 | |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 1390 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 1391 | /** @brief A mostly no-op Telnet state machine. |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1392 | * Process Telnet command bytes in a way that ensures we're mostly |
| 1393 | * transparent to the Telnet protocol. That is, it's mostly a no-op. |
| 1394 | * |
| 1395 | * @return -1 if we need more bytes, otherwise a positive integer number of |
| 1396 | * bytes to consume from the input_vector, not including the initial |
| 1397 | * IAC byte. |
| 1398 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1399 | static i32 |
| 1400 | unix_cli_process_telnet (unix_main_t * um, |
| 1401 | unix_cli_file_t * cf, |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1402 | clib_file_t * uf, u8 * input_vector, uword len) |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1403 | { |
| 1404 | /* Input_vector starts at IAC byte. |
| 1405 | * See if we have a complete message; if not, return -1 so we wait for more. |
| 1406 | * if we have a complete message, consume those bytes from the vector. |
| 1407 | */ |
| 1408 | i32 consume = 0; |
| 1409 | |
| 1410 | if (len == 1) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1411 | return -1; /* want more bytes */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1412 | |
| 1413 | switch (input_vector[1]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1414 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1415 | case IAC: |
| 1416 | /* two IAC's in a row means to pass through 0xff. |
| 1417 | * since that makes no sense here, just consume it. |
| 1418 | */ |
| 1419 | consume = 1; |
| 1420 | break; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1421 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1422 | case WILL: |
| 1423 | case WONT: |
| 1424 | case DO: |
| 1425 | case DONT: |
| 1426 | /* Expect 3 bytes */ |
Chris Luke | a9cf6af | 2018-09-05 21:00:52 -0400 | [diff] [blame] | 1427 | if (len < 3) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1428 | return -1; /* want more bytes */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1429 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1430 | consume = 2; |
| 1431 | break; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1432 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1433 | case SB: |
| 1434 | { |
| 1435 | /* Sub option - search ahead for IAC SE to end it */ |
| 1436 | i32 i; |
| 1437 | for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++) |
| 1438 | { |
| 1439 | if (input_vector[i - 1] == IAC && input_vector[i] == SE) |
| 1440 | { |
| 1441 | /* We have a complete message; see if we care about it */ |
| 1442 | switch (input_vector[2]) |
| 1443 | { |
| 1444 | case TELOPT_TTYPE: |
| 1445 | if (input_vector[3] != 0) |
| 1446 | break; |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 1447 | { |
| 1448 | /* See if the the terminal type is recognized */ |
| 1449 | u8 *term = input_vector + 4; |
| 1450 | uword len = i - 5; |
| 1451 | |
| 1452 | /* See if the terminal type is ANSI capable */ |
| 1453 | cf->ansi_capable = |
| 1454 | unix_cli_terminal_type_ansi (term, len); |
| 1455 | |
| 1456 | /* See if the terminal type indicates non-interactive */ |
| 1457 | if (unix_cli_terminal_type_noninteractive (term, len)) |
| 1458 | unix_cli_set_session_noninteractive (cf); |
| 1459 | } |
| 1460 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1461 | /* If session not started, we can release the pause */ |
| 1462 | if (!cf->started) |
| 1463 | /* Send the welcome banner and initial prompt */ |
| 1464 | unix_cli_file_welcome (&unix_cli_main, cf); |
| 1465 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1466 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1467 | case TELOPT_NAWS: |
| 1468 | /* Window size */ |
| 1469 | if (i != 8) /* check message is correct size */ |
| 1470 | break; |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 1471 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1472 | cf->width = |
| 1473 | clib_net_to_host_u16 (*((u16 *) (input_vector + 3))); |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 1474 | if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH) |
| 1475 | cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH; |
Chris Luke | 2d8bf30 | 2017-11-12 22:26:37 -0500 | [diff] [blame] | 1476 | if (cf->width == 0) |
| 1477 | cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH; |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 1478 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1479 | cf->height = |
| 1480 | clib_net_to_host_u16 (*((u16 *) (input_vector + 5))); |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 1481 | if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT) |
| 1482 | cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT; |
Chris Luke | 2d8bf30 | 2017-11-12 22:26:37 -0500 | [diff] [blame] | 1483 | if (cf->height == 0) |
| 1484 | cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT; |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 1485 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1486 | /* reindex pager buffer */ |
| 1487 | unix_cli_pager_reindex (cf); |
| 1488 | /* redraw page */ |
| 1489 | unix_cli_pager_redraw (cf, uf); |
| 1490 | break; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1491 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1492 | default: |
| 1493 | break; |
| 1494 | } |
| 1495 | /* Consume it all */ |
| 1496 | consume = i; |
| 1497 | break; |
| 1498 | } |
| 1499 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1500 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1501 | if (i == UNIX_CLI_MAX_DEPTH_TELNET) |
| 1502 | consume = 1; /* hit max search depth, advance one byte */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1503 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1504 | if (consume == 0) |
| 1505 | return -1; /* want more bytes */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1506 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1507 | break; |
| 1508 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1509 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1510 | case GA: |
| 1511 | case EL: |
| 1512 | case EC: |
| 1513 | case AO: |
| 1514 | case IP: |
| 1515 | case BREAK: |
| 1516 | case DM: |
| 1517 | case NOP: |
| 1518 | case SE: |
| 1519 | case EOR: |
| 1520 | case ABORT: |
| 1521 | case SUSP: |
| 1522 | case xEOF: |
| 1523 | /* Simple one-byte messages */ |
| 1524 | consume = 1; |
| 1525 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1526 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1527 | case AYT: |
| 1528 | /* Are You There - trigger a visible response */ |
| 1529 | consume = 1; |
| 1530 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10); |
| 1531 | break; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1532 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1533 | default: |
| 1534 | /* Unknown command! Eat the IAC byte */ |
| 1535 | break; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1536 | } |
| 1537 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1538 | return consume; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1539 | } |
| 1540 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 1541 | /** @brief Process actionable input. |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1542 | * Based on the \c action process the input; this typically involves |
| 1543 | * searching the command history or editing the current command line. |
| 1544 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1545 | static int |
| 1546 | unix_cli_line_process_one (unix_cli_main_t * cm, |
| 1547 | unix_main_t * um, |
| 1548 | unix_cli_file_t * cf, |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 1549 | clib_file_t * uf, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1550 | u8 input, unix_cli_parse_action_t action) |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1551 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1552 | u8 *prev; |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 1553 | u8 *save = 0; |
| 1554 | u8 **possible_commands; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1555 | int j, delta; |
| 1556 | |
| 1557 | switch (action) |
| 1558 | { |
| 1559 | case UNIX_CLI_PARSE_ACTION_NOACTION: |
| 1560 | break; |
| 1561 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1562 | case UNIX_CLI_PARSE_ACTION_REVSEARCH: |
| 1563 | case UNIX_CLI_PARSE_ACTION_FWDSEARCH: |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1564 | if (!cf->has_history || !cf->history_limit) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1565 | break; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1566 | if (cf->search_mode == 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1567 | { |
| 1568 | /* Erase the current command (if any) */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1569 | for (; cf->cursor > 0; cf->cursor--) |
| 1570 | { |
| 1571 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1572 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
| 1573 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1574 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1575 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1576 | vec_reset_length (cf->search_key); |
| 1577 | vec_reset_length (cf->current_command); |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1578 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1579 | if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH) |
| 1580 | cf->search_mode = -1; |
| 1581 | else |
| 1582 | cf->search_mode = 1; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1583 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1584 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1585 | { |
| 1586 | if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH) |
| 1587 | cf->search_mode = -1; |
| 1588 | else |
| 1589 | cf->search_mode = 1; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1590 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1591 | cf->excursion += cf->search_mode; |
| 1592 | goto search_again; |
| 1593 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1594 | break; |
| 1595 | |
| 1596 | case UNIX_CLI_PARSE_ACTION_ERASELINELEFT: |
| 1597 | /* Erase the command from the cursor to the start */ |
| 1598 | |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1599 | j = cf->cursor; |
| 1600 | /* Shimmy backwards to the new end of line position */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1601 | delta = vec_len (cf->current_command) - cf->cursor; |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1602 | for (; cf->cursor > delta; cf->cursor--) |
| 1603 | unix_vlib_cli_output_cursor_left (cf, uf); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1604 | /* Zap from here to the end of what is currently displayed */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1605 | for (; cf->cursor < vec_len (cf->current_command); cf->cursor++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1606 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1607 | /* Get back to the start of the line */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1608 | for (; cf->cursor > 0; cf->cursor--) |
| 1609 | unix_vlib_cli_output_cursor_left (cf, uf); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1610 | |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1611 | /* Delete the desired text from the command */ |
| 1612 | memmove (cf->current_command, cf->current_command + j, delta); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1613 | vec_set_len (cf->current_command, delta); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1614 | |
| 1615 | /* Print the new contents */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1616 | unix_vlib_cli_output_cooked (cf, uf, cf->current_command, delta); |
| 1617 | cf->cursor = delta; /* for backspace tracking */ |
| 1618 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1619 | /* Shimmy back to the start */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1620 | for (; cf->cursor > 0; cf->cursor--) |
| 1621 | unix_vlib_cli_output_cursor_left (cf, uf); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1622 | |
| 1623 | cf->search_mode = 0; |
| 1624 | break; |
| 1625 | |
| 1626 | case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT: |
| 1627 | /* Erase the command from the cursor to the end */ |
| 1628 | |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1629 | j = cf->cursor; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1630 | /* Zap from cursor to end of what is currently displayed */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1631 | for (; cf->cursor < (vec_len (cf->current_command)); cf->cursor++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1632 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1633 | /* Get back to where we were */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1634 | for (; cf->cursor > j; cf->cursor--) |
| 1635 | unix_vlib_cli_output_cursor_left (cf, uf); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1636 | |
| 1637 | /* Truncate the line at the cursor */ |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1638 | vec_set_len (cf->current_command, cf->cursor); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1639 | |
| 1640 | cf->search_mode = 0; |
| 1641 | break; |
| 1642 | |
Hiroki Shirokura | 67e4df1 | 2019-08-16 11:30:34 +0000 | [diff] [blame] | 1643 | case UNIX_CLI_PARSE_ACTION_ERASEWORDLEFT: |
| 1644 | /* calculate num of caracter to be erased */ |
| 1645 | delta = 0; |
| 1646 | while (cf->cursor > delta |
| 1647 | && cf->current_command[cf->cursor - delta - 1] == ' ') |
| 1648 | delta++; |
| 1649 | while (cf->cursor > delta |
| 1650 | && cf->current_command[cf->cursor - delta - 1] != ' ') |
| 1651 | delta++; |
| 1652 | |
| 1653 | if (vec_len (cf->current_command)) |
| 1654 | { |
| 1655 | if (cf->cursor > 0) |
| 1656 | { |
| 1657 | /* move cursor left delta times */ |
| 1658 | for (j = delta; j > 0; j--, cf->cursor--) |
| 1659 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1660 | save = cf->current_command + cf->cursor; |
| 1661 | |
| 1662 | /* redraw remainder of line */ |
| 1663 | memmove (cf->current_command + cf->cursor, |
| 1664 | cf->current_command + cf->cursor + delta, |
| 1665 | _vec_len (cf->current_command) - cf->cursor - delta); |
| 1666 | unix_vlib_cli_output_cooked (cf, uf, |
| 1667 | cf->current_command + cf->cursor, |
| 1668 | _vec_len (cf->current_command) - |
| 1669 | cf->cursor); |
| 1670 | cf->cursor += _vec_len (cf->current_command) - cf->cursor; |
| 1671 | |
| 1672 | /* print delta amount of blank spaces, |
| 1673 | * then finally fix the cursor position */ |
| 1674 | for (j = delta; j > 0; j--, cf->cursor--) |
| 1675 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1676 | for (j = delta; j > 0; j--, cf->cursor++) |
| 1677 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
| 1678 | for (; (cf->current_command + cf->cursor) > save; cf->cursor--) |
| 1679 | unix_vlib_cli_output_cursor_left (cf, uf); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1680 | vec_dec_len (cf->current_command, delta); |
Hiroki Shirokura | 67e4df1 | 2019-08-16 11:30:34 +0000 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | cf->search_mode = 0; |
| 1684 | cf->excursion = 0; |
| 1685 | vec_reset_length (cf->search_key); |
| 1686 | break; |
| 1687 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1688 | case UNIX_CLI_PARSE_ACTION_LEFT: |
| 1689 | if (cf->cursor > 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1690 | { |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1691 | unix_vlib_cli_output_cursor_left (cf, uf); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1692 | cf->cursor--; |
| 1693 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1694 | |
| 1695 | cf->search_mode = 0; |
| 1696 | break; |
| 1697 | |
| 1698 | case UNIX_CLI_PARSE_ACTION_RIGHT: |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1699 | if (cf->cursor < vec_len (cf->current_command)) |
| 1700 | { |
| 1701 | /* have to emit the character under the cursor */ |
| 1702 | unix_vlib_cli_output_cooked (cf, uf, |
| 1703 | cf->current_command + cf->cursor, 1); |
| 1704 | cf->cursor++; |
| 1705 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1706 | |
| 1707 | cf->search_mode = 0; |
| 1708 | break; |
| 1709 | |
| 1710 | case UNIX_CLI_PARSE_ACTION_UP: |
| 1711 | case UNIX_CLI_PARSE_ACTION_DOWN: |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1712 | if (!cf->has_history || !cf->history_limit) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1713 | break; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1714 | cf->search_mode = 0; |
| 1715 | /* Erase the command */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1716 | for (; cf->cursor < vec_len (cf->current_command); cf->cursor++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1717 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1718 | for (; cf->cursor > 0; cf->cursor--) |
| 1719 | { |
| 1720 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1721 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
| 1722 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1723 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1724 | vec_reset_length (cf->current_command); |
| 1725 | if (vec_len (cf->command_history)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1726 | { |
| 1727 | if (action == UNIX_CLI_PARSE_ACTION_UP) |
| 1728 | delta = -1; |
| 1729 | else |
| 1730 | delta = 1; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1731 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1732 | cf->excursion += delta; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1733 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1734 | if (cf->excursion == vec_len (cf->command_history)) |
| 1735 | { |
| 1736 | /* down-arrowed to last entry - want a blank line */ |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1737 | vec_set_len (cf->current_command, 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1738 | } |
| 1739 | else if (cf->excursion < 0) |
| 1740 | { |
| 1741 | /* up-arrowed over the start to the end, want a blank line */ |
| 1742 | cf->excursion = vec_len (cf->command_history); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1743 | vec_set_len (cf->current_command, 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1744 | } |
| 1745 | else |
| 1746 | { |
| 1747 | if (cf->excursion > (i32) vec_len (cf->command_history) - 1) |
| 1748 | /* down-arrowed past end - wrap to start */ |
| 1749 | cf->excursion = 0; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1750 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1751 | /* Print the command at the current position */ |
| 1752 | prev = cf->command_history[cf->excursion]; |
| 1753 | vec_validate (cf->current_command, vec_len (prev) - 1); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1754 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1755 | clib_memcpy (cf->current_command, prev, vec_len (prev)); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1756 | vec_set_len (cf->current_command, vec_len (prev)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1757 | unix_vlib_cli_output_cooked (cf, uf, cf->current_command, |
| 1758 | vec_len (cf->current_command)); |
| 1759 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1760 | } |
Yoann Desmouceaux | 561ae0a | 2017-09-20 10:08:28 +0200 | [diff] [blame] | 1761 | cf->cursor = vec_len (cf->current_command); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1762 | break; |
| 1763 | |
| 1764 | case UNIX_CLI_PARSE_ACTION_HOME: |
| 1765 | if (vec_len (cf->current_command) && cf->cursor > 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1766 | { |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1767 | for (; cf->cursor > 0; cf->cursor--) |
| 1768 | unix_vlib_cli_output_cursor_left (cf, uf); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1769 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1770 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1771 | cf->search_mode = 0; |
| 1772 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1773 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1774 | case UNIX_CLI_PARSE_ACTION_END: |
| 1775 | if (vec_len (cf->current_command) && |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1776 | cf->cursor < vec_len (cf->current_command)) |
| 1777 | { |
| 1778 | unix_vlib_cli_output_cooked (cf, uf, |
| 1779 | cf->current_command + cf->cursor, |
| 1780 | vec_len (cf->current_command) - |
| 1781 | cf->cursor); |
| 1782 | cf->cursor = vec_len (cf->current_command); |
| 1783 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1784 | |
| 1785 | cf->search_mode = 0; |
| 1786 | break; |
| 1787 | |
| 1788 | case UNIX_CLI_PARSE_ACTION_WORDLEFT: |
| 1789 | if (vec_len (cf->current_command) && cf->cursor > 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1790 | { |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1791 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1792 | cf->cursor--; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1793 | |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1794 | while (cf->cursor && isspace (cf->current_command[cf->cursor])) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1795 | { |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1796 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1797 | cf->cursor--; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1798 | } |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1799 | while (cf->cursor && !isspace (cf->current_command[cf->cursor])) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1800 | { |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1801 | if (isspace (cf->current_command[cf->cursor - 1])) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1802 | break; |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1803 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1804 | cf->cursor--; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1805 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1806 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1807 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1808 | |
| 1809 | cf->search_mode = 0; |
| 1810 | break; |
| 1811 | |
| 1812 | case UNIX_CLI_PARSE_ACTION_WORDRIGHT: |
| 1813 | if (vec_len (cf->current_command) && |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1814 | cf->cursor < vec_len (cf->current_command)) |
| 1815 | { |
| 1816 | int e = vec_len (cf->current_command); |
| 1817 | j = cf->cursor; |
| 1818 | while (j < e && !isspace (cf->current_command[j])) |
| 1819 | j++; |
| 1820 | while (j < e && isspace (cf->current_command[j])) |
| 1821 | j++; |
| 1822 | unix_vlib_cli_output_cooked (cf, uf, |
| 1823 | cf->current_command + cf->cursor, |
| 1824 | j - cf->cursor); |
| 1825 | cf->cursor = j; |
| 1826 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1827 | |
| 1828 | cf->search_mode = 0; |
| 1829 | break; |
| 1830 | |
| 1831 | |
| 1832 | case UNIX_CLI_PARSE_ACTION_ERASE: |
| 1833 | if (vec_len (cf->current_command)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1834 | { |
| 1835 | if (cf->cursor == vec_len (cf->current_command)) |
| 1836 | { |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1837 | unix_vlib_cli_output_cursor_left (cf, uf); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1838 | cf->cursor--; |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1839 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
| 1840 | cf->cursor++; |
| 1841 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1842 | cf->cursor--; |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1843 | vec_dec_len (cf->current_command, 1); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1844 | } |
| 1845 | else if (cf->cursor > 0) |
| 1846 | { |
| 1847 | /* shift everything at & to the right of the cursor left by 1 */ |
| 1848 | j = vec_len (cf->current_command) - cf->cursor; |
| 1849 | memmove (cf->current_command + cf->cursor - 1, |
| 1850 | cf->current_command + cf->cursor, j); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1851 | vec_dec_len (cf->current_command, 1); |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1852 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1853 | /* redraw the rest of the line */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1854 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1855 | cf->cursor--; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1856 | unix_vlib_cli_output_cooked (cf, uf, |
| 1857 | cf->current_command + cf->cursor, |
| 1858 | j); |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1859 | cf->cursor += j; |
| 1860 | /* erase last char */ |
| 1861 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
| 1862 | cf->cursor++; |
| 1863 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1864 | /* and shift the terminal cursor back where it should be */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1865 | j += 2; /* account for old string length and offset position */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1866 | while (--j) |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1867 | { |
| 1868 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1869 | cf->cursor--; |
| 1870 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1871 | } |
| 1872 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1873 | cf->search_mode = 0; |
| 1874 | cf->excursion = 0; |
| 1875 | vec_reset_length (cf->search_key); |
| 1876 | break; |
| 1877 | |
| 1878 | case UNIX_CLI_PARSE_ACTION_ERASERIGHT: |
| 1879 | if (vec_len (cf->current_command)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1880 | { |
| 1881 | if (cf->cursor < vec_len (cf->current_command)) |
| 1882 | { |
| 1883 | /* shift everything to the right of the cursor left by 1 */ |
| 1884 | j = vec_len (cf->current_command) - cf->cursor - 1; |
| 1885 | memmove (cf->current_command + cf->cursor, |
| 1886 | cf->current_command + cf->cursor + 1, j); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1887 | vec_dec_len (cf->current_command, 1); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1888 | /* redraw the rest of the line */ |
| 1889 | unix_vlib_cli_output_cooked (cf, uf, |
| 1890 | cf->current_command + cf->cursor, |
| 1891 | j); |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1892 | cf->cursor += j; |
| 1893 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
| 1894 | cf->cursor++; |
| 1895 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1896 | cf->cursor--; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1897 | /* and shift the terminal cursor back where it should be */ |
| 1898 | if (j) |
| 1899 | { |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1900 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1901 | cf->cursor--; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1902 | while (--j) |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1903 | { |
| 1904 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1905 | cf->cursor--; |
| 1906 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1907 | } |
| 1908 | } |
| 1909 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1910 | else if (input == 'D' - '@') |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1911 | { |
| 1912 | /* ^D with no command entered = quit */ |
| 1913 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5); |
| 1914 | vlib_process_signal_event (um->vlib_main, |
| 1915 | vlib_current_process (um->vlib_main), |
| 1916 | UNIX_CLI_PROCESS_EVENT_QUIT, |
| 1917 | cf - cm->cli_file_pool); |
| 1918 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1919 | cf->search_mode = 0; |
| 1920 | cf->excursion = 0; |
| 1921 | vec_reset_length (cf->search_key); |
| 1922 | break; |
| 1923 | |
| 1924 | case UNIX_CLI_PARSE_ACTION_CLEAR: |
| 1925 | /* If we're in ANSI mode, clear the screen. |
| 1926 | * Then redraw the prompt and any existing command input, then put |
| 1927 | * the cursor back where it was in that line. |
| 1928 | */ |
| 1929 | if (cf->ansi_capable) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1930 | unix_vlib_cli_output_cooked (cf, uf, |
| 1931 | (u8 *) ANSI_CLEAR, |
| 1932 | sizeof (ANSI_CLEAR) - 1); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1933 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1934 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1935 | |
| 1936 | unix_vlib_cli_output_raw (cf, uf, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1937 | cm->cli_prompt, vec_len (cm->cli_prompt)); |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1938 | unix_vlib_cli_output_cooked (cf, uf, |
| 1939 | cf->current_command, |
| 1940 | vec_len (cf->current_command)); |
| 1941 | j = cf->cursor; |
| 1942 | cf->cursor = vec_len (cf->current_command); |
| 1943 | for (; cf->cursor > j; cf->cursor--) |
| 1944 | unix_vlib_cli_output_cursor_left (cf, uf); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 1945 | |
| 1946 | break; |
| 1947 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 1948 | case UNIX_CLI_PARSE_ACTION_TAB: |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 1949 | if (cf->cursor < vec_len (cf->current_command)) |
| 1950 | { |
| 1951 | /* if we are in the middle of a line, complete only if |
| 1952 | * the cursor points to whitespace */ |
| 1953 | if (isspace (cf->current_command[cf->cursor])) |
| 1954 | { |
| 1955 | /* save and clear any input that is after the cursor */ |
| 1956 | vec_resize (save, vec_len (cf->current_command) - cf->cursor); |
| 1957 | clib_memcpy (save, cf->current_command + cf->cursor, |
| 1958 | vec_len (cf->current_command) - cf->cursor); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1959 | vec_set_len (cf->current_command, cf->cursor); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 1960 | } |
| 1961 | else |
| 1962 | { |
| 1963 | unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1); |
| 1964 | break; |
| 1965 | } |
| 1966 | } |
| 1967 | possible_commands = |
| 1968 | vlib_cli_get_possible_completions (cf->current_command); |
| 1969 | if (vec_len (possible_commands) == 1) |
| 1970 | { |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 1971 | u8 *completed = possible_commands[0]; |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1972 | j = cf->cursor; |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 1973 | |
| 1974 | /* find the last word of current_command */ |
| 1975 | while (j >= 1 && !isspace (cf->current_command[j - 1])) |
| 1976 | { |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1977 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 1978 | cf->cursor--; |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 1979 | j--; |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 1980 | } |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 1981 | vec_set_len (cf->current_command, j); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 1982 | |
| 1983 | /* replace it with the newly expanded command */ |
| 1984 | vec_append (cf->current_command, completed); |
| 1985 | |
| 1986 | /* echo to the terminal */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1987 | unix_vlib_cli_output_cooked (cf, uf, completed, |
| 1988 | vec_len (completed)); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 1989 | |
| 1990 | /* add one trailing space if needed */ |
| 1991 | if (vec_len (save) == 0) |
| 1992 | { |
| 1993 | vec_add1 (cf->current_command, ' '); |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 1994 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | cf->cursor = vec_len (cf->current_command); |
| 1998 | |
| 1999 | } |
| 2000 | else if (vec_len (possible_commands) >= 2) |
| 2001 | { |
| 2002 | u8 **possible_command; |
| 2003 | uword max_command_len = 0, min_command_len = ~0; |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2004 | u32 i; |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 2005 | |
| 2006 | vec_foreach (possible_command, possible_commands) |
| 2007 | { |
| 2008 | if (vec_len (*possible_command) > max_command_len) |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2009 | max_command_len = vec_len (*possible_command); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 2010 | if (vec_len (*possible_command) < min_command_len) |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2011 | min_command_len = vec_len (*possible_command); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
| 2015 | |
| 2016 | i = 0; |
| 2017 | vec_foreach (possible_command, possible_commands) |
| 2018 | { |
| 2019 | if (i + max_command_len >= cf->width) |
| 2020 | { |
| 2021 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
| 2022 | i = 0; |
| 2023 | } |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2024 | unix_vlib_cli_output_cooked (cf, uf, *possible_command, |
| 2025 | vec_len (*possible_command)); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 2026 | for (j = vec_len (*possible_command); j < max_command_len + 2; |
| 2027 | j++) |
| 2028 | { |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2029 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 2030 | } |
| 2031 | i += max_command_len + 2; |
| 2032 | } |
| 2033 | |
| 2034 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
| 2035 | |
| 2036 | /* rewrite prompt */ |
| 2037 | unix_cli_cli_prompt (cf, uf); |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2038 | unix_vlib_cli_output_cooked (cf, uf, cf->current_command, |
| 2039 | vec_len (cf->current_command)); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 2040 | |
| 2041 | /* count length of last word */ |
| 2042 | j = cf->cursor; |
| 2043 | i = 0; |
| 2044 | while (j >= 1 && !isspace (cf->current_command[j - 1])) |
| 2045 | { |
| 2046 | j--; |
| 2047 | i++; |
| 2048 | } |
| 2049 | |
| 2050 | /* determine smallest common command */ |
| 2051 | for (; i < min_command_len; i++) |
| 2052 | { |
| 2053 | u8 common = '\0'; |
| 2054 | int stop = 0; |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2055 | |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 2056 | vec_foreach (possible_command, possible_commands) |
| 2057 | { |
| 2058 | if (common == '\0') |
| 2059 | { |
| 2060 | common = (*possible_command)[i]; |
| 2061 | } |
| 2062 | else if (common != (*possible_command)[i]) |
| 2063 | { |
| 2064 | stop = 1; |
| 2065 | break; |
| 2066 | } |
| 2067 | } |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2068 | |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 2069 | if (!stop) |
| 2070 | { |
| 2071 | vec_add1 (cf->current_command, common); |
| 2072 | cf->cursor++; |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2073 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) & common, 1); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 2074 | } |
| 2075 | else |
| 2076 | { |
| 2077 | break; |
| 2078 | } |
| 2079 | } |
| 2080 | } |
| 2081 | else |
| 2082 | { |
| 2083 | unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1); |
| 2084 | } |
| 2085 | |
| 2086 | if (vec_len (save) > 0) |
| 2087 | { |
| 2088 | /* restore remaining input if tab was hit in the middle of a line */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2089 | unix_vlib_cli_output_cooked (cf, uf, save, vec_len (save)); |
| 2090 | cf->cursor += vec_len (save); |
| 2091 | for (j = 0; j < vec_len (save); j++, cf->cursor--) |
| 2092 | unix_vlib_cli_output_cursor_left (cf, uf); |
Yoann Desmouceaux | 3060e07 | 2017-05-18 11:00:48 +0200 | [diff] [blame] | 2093 | vec_append (cf->current_command, save); |
| 2094 | vec_free (save); |
| 2095 | } |
| 2096 | vec_free (possible_commands); |
| 2097 | |
| 2098 | break; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2099 | case UNIX_CLI_PARSE_ACTION_YANK: |
| 2100 | /* TODO */ |
| 2101 | break; |
| 2102 | |
| 2103 | |
| 2104 | case UNIX_CLI_PARSE_ACTION_PAGER_QUIT: |
| 2105 | pager_quit: |
| 2106 | unix_cli_pager_prompt_erase (cf, uf); |
| 2107 | unix_cli_pager_reset (cf); |
| 2108 | unix_cli_cli_prompt (cf, uf); |
| 2109 | break; |
| 2110 | |
| 2111 | case UNIX_CLI_PARSE_ACTION_PAGER_NEXT: |
| 2112 | case UNIX_CLI_PARSE_ACTION_PAGER_PGDN: |
| 2113 | /* show next page of the buffer */ |
Chris Luke | 70a745d | 2018-06-10 10:47:50 -0400 | [diff] [blame] | 2114 | if (cf->height + cf->pager_start <= vec_len (cf->pager_index)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2115 | { |
| 2116 | u8 *line = NULL; |
| 2117 | unix_cli_pager_index_t *pi = NULL; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 2118 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2119 | int m = cf->pager_start + (cf->height - 1); |
| 2120 | unix_cli_pager_prompt_erase (cf, uf); |
| 2121 | for (j = m; |
| 2122 | j < vec_len (cf->pager_index) && cf->pager_start < m; |
| 2123 | j++, cf->pager_start++) |
| 2124 | { |
| 2125 | pi = &cf->pager_index[j]; |
| 2126 | line = cf->pager_vector[pi->line] + pi->offset; |
| 2127 | unix_vlib_cli_output_cooked (cf, uf, line, pi->length); |
| 2128 | } |
| 2129 | /* if the last line didn't end in newline, add a newline */ |
| 2130 | if (pi && line[pi->length - 1] != '\n') |
| 2131 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
| 2132 | unix_cli_pager_prompt (cf, uf); |
| 2133 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2134 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2135 | { |
| 2136 | if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT) |
| 2137 | /* no more in buffer, exit, but only if it was <space> */ |
| 2138 | goto pager_quit; |
| 2139 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2140 | break; |
| 2141 | |
| 2142 | case UNIX_CLI_PARSE_ACTION_PAGER_DN: |
| 2143 | case UNIX_CLI_PARSE_ACTION_PAGER_CRLF: |
| 2144 | /* display the next line of the buffer */ |
Chris Luke | 70a745d | 2018-06-10 10:47:50 -0400 | [diff] [blame] | 2145 | if (cf->height + cf->pager_start <= vec_len (cf->pager_index)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2146 | { |
| 2147 | u8 *line; |
| 2148 | unix_cli_pager_index_t *pi; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 2149 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2150 | unix_cli_pager_prompt_erase (cf, uf); |
| 2151 | pi = &cf->pager_index[cf->pager_start + (cf->height - 1)]; |
| 2152 | line = cf->pager_vector[pi->line] + pi->offset; |
| 2153 | unix_vlib_cli_output_cooked (cf, uf, line, pi->length); |
| 2154 | cf->pager_start++; |
| 2155 | /* if the last line didn't end in newline, add a newline */ |
| 2156 | if (line[pi->length - 1] != '\n') |
| 2157 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
| 2158 | unix_cli_pager_prompt (cf, uf); |
| 2159 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2160 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2161 | { |
| 2162 | if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF) |
| 2163 | /* no more in buffer, exit, but only if it was <enter> */ |
| 2164 | goto pager_quit; |
| 2165 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2166 | |
| 2167 | break; |
| 2168 | |
| 2169 | case UNIX_CLI_PARSE_ACTION_PAGER_UP: |
| 2170 | /* scroll the page back one line */ |
| 2171 | if (cf->pager_start > 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2172 | { |
| 2173 | u8 *line = NULL; |
| 2174 | unix_cli_pager_index_t *pi = NULL; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 2175 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2176 | cf->pager_start--; |
| 2177 | if (cf->ansi_capable) |
| 2178 | { |
| 2179 | pi = &cf->pager_index[cf->pager_start]; |
| 2180 | line = cf->pager_vector[pi->line] + pi->offset; |
| 2181 | unix_cli_pager_prompt_erase (cf, uf); |
| 2182 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN, |
| 2183 | sizeof (ANSI_SCROLLDN) - 1); |
| 2184 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR, |
| 2185 | sizeof (ANSI_SAVECURSOR) - 1); |
| 2186 | unix_cli_ansi_cursor (cf, uf, 1, 1); |
| 2187 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE, |
| 2188 | sizeof (ANSI_CLEARLINE) - 1); |
| 2189 | unix_vlib_cli_output_cooked (cf, uf, line, pi->length); |
| 2190 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR, |
| 2191 | sizeof (ANSI_RESTCURSOR) - 1); |
| 2192 | unix_cli_pager_prompt_erase (cf, uf); |
| 2193 | unix_cli_pager_prompt (cf, uf); |
| 2194 | } |
| 2195 | else |
| 2196 | { |
| 2197 | int m = cf->pager_start + (cf->height - 1); |
| 2198 | unix_cli_pager_prompt_erase (cf, uf); |
| 2199 | for (j = cf->pager_start; |
| 2200 | j < vec_len (cf->pager_index) && j < m; j++) |
| 2201 | { |
| 2202 | pi = &cf->pager_index[j]; |
| 2203 | line = cf->pager_vector[pi->line] + pi->offset; |
| 2204 | unix_vlib_cli_output_cooked (cf, uf, line, pi->length); |
| 2205 | } |
| 2206 | /* if the last line didn't end in newline, add a newline */ |
| 2207 | if (pi && line[pi->length - 1] != '\n') |
| 2208 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
| 2209 | unix_cli_pager_prompt (cf, uf); |
| 2210 | } |
| 2211 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2212 | break; |
| 2213 | |
| 2214 | case UNIX_CLI_PARSE_ACTION_PAGER_TOP: |
| 2215 | /* back to the first page of the buffer */ |
| 2216 | if (cf->pager_start > 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2217 | { |
| 2218 | u8 *line = NULL; |
| 2219 | unix_cli_pager_index_t *pi = NULL; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 2220 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2221 | cf->pager_start = 0; |
| 2222 | int m = cf->pager_start + (cf->height - 1); |
| 2223 | unix_cli_pager_prompt_erase (cf, uf); |
| 2224 | for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m; |
| 2225 | j++) |
| 2226 | { |
| 2227 | pi = &cf->pager_index[j]; |
| 2228 | line = cf->pager_vector[pi->line] + pi->offset; |
| 2229 | unix_vlib_cli_output_cooked (cf, uf, line, pi->length); |
| 2230 | } |
| 2231 | /* if the last line didn't end in newline, add a newline */ |
| 2232 | if (pi && line[pi->length - 1] != '\n') |
| 2233 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
| 2234 | unix_cli_pager_prompt (cf, uf); |
| 2235 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2236 | break; |
| 2237 | |
| 2238 | case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM: |
| 2239 | /* skip to the last page of the buffer */ |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 2240 | if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2241 | { |
| 2242 | u8 *line = NULL; |
| 2243 | unix_cli_pager_index_t *pi = NULL; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 2244 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2245 | cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1); |
| 2246 | unix_cli_pager_prompt_erase (cf, uf); |
| 2247 | unix_cli_pager_message (cf, uf, "skipping", "\n"); |
| 2248 | for (j = cf->pager_start; j < vec_len (cf->pager_index); j++) |
| 2249 | { |
| 2250 | pi = &cf->pager_index[j]; |
| 2251 | line = cf->pager_vector[pi->line] + pi->offset; |
| 2252 | unix_vlib_cli_output_cooked (cf, uf, line, pi->length); |
| 2253 | } |
| 2254 | /* if the last line didn't end in newline, add a newline */ |
| 2255 | if (pi && line[pi->length - 1] != '\n') |
| 2256 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
| 2257 | unix_cli_pager_prompt (cf, uf); |
| 2258 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2259 | break; |
| 2260 | |
| 2261 | case UNIX_CLI_PARSE_ACTION_PAGER_PGUP: |
| 2262 | /* wander back one page in the buffer */ |
| 2263 | if (cf->pager_start > 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2264 | { |
| 2265 | u8 *line = NULL; |
| 2266 | unix_cli_pager_index_t *pi = NULL; |
| 2267 | int m; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 2268 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2269 | if (cf->pager_start >= cf->height) |
| 2270 | cf->pager_start -= cf->height - 1; |
| 2271 | else |
| 2272 | cf->pager_start = 0; |
| 2273 | m = cf->pager_start + cf->height - 1; |
| 2274 | unix_cli_pager_prompt_erase (cf, uf); |
| 2275 | for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m; |
| 2276 | j++) |
| 2277 | { |
| 2278 | pi = &cf->pager_index[j]; |
| 2279 | line = cf->pager_vector[pi->line] + pi->offset; |
| 2280 | unix_vlib_cli_output_cooked (cf, uf, line, pi->length); |
| 2281 | } |
| 2282 | /* if the last line didn't end in newline, add a newline */ |
| 2283 | if (pi && line[pi->length - 1] != '\n') |
| 2284 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
| 2285 | unix_cli_pager_prompt (cf, uf); |
| 2286 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2287 | break; |
| 2288 | |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 2289 | case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW: |
| 2290 | /* Redraw the current pager screen */ |
| 2291 | unix_cli_pager_redraw (cf, uf); |
| 2292 | break; |
| 2293 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2294 | case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH: |
| 2295 | /* search forwards in the buffer */ |
| 2296 | break; |
| 2297 | |
| 2298 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2299 | case UNIX_CLI_PARSE_ACTION_CRLF: |
| 2300 | crlf: |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2301 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); |
| 2302 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2303 | if (cf->has_history && cf->history_limit) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2304 | { |
| 2305 | if (cf->command_history |
| 2306 | && vec_len (cf->command_history) >= cf->history_limit) |
| 2307 | { |
| 2308 | vec_free (cf->command_history[0]); |
| 2309 | vec_delete (cf->command_history, 1, 0); |
| 2310 | } |
| 2311 | /* Don't add blank lines to the cmd history */ |
| 2312 | if (vec_len (cf->current_command)) |
| 2313 | { |
| 2314 | /* Don't duplicate the previous command */ |
| 2315 | j = vec_len (cf->command_history); |
| 2316 | if (j == 0 || |
| 2317 | (vec_len (cf->current_command) != |
| 2318 | vec_len (cf->command_history[j - 1]) |
| 2319 | || memcmp (cf->current_command, cf->command_history[j - 1], |
| 2320 | vec_len (cf->current_command)) != 0)) |
| 2321 | { |
| 2322 | /* copy the command to the history */ |
| 2323 | u8 *c = 0; |
| 2324 | vec_append (c, cf->current_command); |
| 2325 | vec_add1 (cf->command_history, c); |
| 2326 | cf->command_number++; |
| 2327 | } |
| 2328 | } |
| 2329 | cf->excursion = vec_len (cf->command_history); |
| 2330 | } |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2331 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2332 | cf->search_mode = 0; |
| 2333 | vec_reset_length (cf->search_key); |
| 2334 | cf->cursor = 0; |
| 2335 | |
| 2336 | return 0; |
| 2337 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2338 | case UNIX_CLI_PARSE_ACTION_PARTIALMATCH: |
| 2339 | case UNIX_CLI_PARSE_ACTION_NOMATCH: |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 2340 | if (vec_len (cf->pager_index)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2341 | { |
| 2342 | /* no-op for now */ |
| 2343 | } |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2344 | else if (cf->has_history && cf->search_mode != 0 && isprint (input)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2345 | { |
| 2346 | int k, limit, offset; |
| 2347 | u8 *item; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2348 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2349 | vec_add1 (cf->search_key, input); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2350 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2351 | search_again: |
| 2352 | for (j = 0; j < vec_len (cf->command_history); j++) |
| 2353 | { |
| 2354 | if (cf->excursion > (i32) vec_len (cf->command_history) - 1) |
| 2355 | cf->excursion = 0; |
| 2356 | else if (cf->excursion < 0) |
| 2357 | cf->excursion = vec_len (cf->command_history) - 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2358 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2359 | item = cf->command_history[cf->excursion]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2360 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2361 | limit = (vec_len (cf->search_key) > vec_len (item)) ? |
| 2362 | vec_len (item) : vec_len (cf->search_key); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2363 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2364 | for (offset = 0; offset <= vec_len (item) - limit; offset++) |
| 2365 | { |
| 2366 | for (k = 0; k < limit; k++) |
| 2367 | { |
| 2368 | if (item[k + offset] != cf->search_key[k]) |
| 2369 | goto next_offset; |
| 2370 | } |
| 2371 | goto found_at_offset; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2372 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2373 | next_offset: |
| 2374 | ; |
| 2375 | } |
| 2376 | goto next; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2377 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2378 | found_at_offset: |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2379 | for (; cf->cursor > 0; cf->cursor--) |
| 2380 | { |
| 2381 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 2382 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); |
| 2383 | unix_vlib_cli_output_cursor_left (cf, uf); |
| 2384 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2385 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2386 | vec_validate (cf->current_command, vec_len (item) - 1); |
| 2387 | clib_memcpy (cf->current_command, item, vec_len (item)); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 2388 | vec_set_len (cf->current_command, vec_len (item)); |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2389 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2390 | unix_vlib_cli_output_cooked (cf, uf, cf->current_command, |
| 2391 | vec_len (cf->current_command)); |
| 2392 | cf->cursor = vec_len (cf->current_command); |
| 2393 | goto found; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2394 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2395 | next: |
| 2396 | cf->excursion += cf->search_mode; |
| 2397 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2398 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2399 | unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12); |
| 2400 | vec_reset_length (cf->search_key); |
| 2401 | vec_reset_length (cf->current_command); |
| 2402 | cf->search_mode = 0; |
| 2403 | cf->cursor = 0; |
| 2404 | goto crlf; |
| 2405 | } |
| 2406 | else if (isprint (input)) /* skip any errant control codes */ |
| 2407 | { |
| 2408 | if (cf->cursor == vec_len (cf->current_command)) |
| 2409 | { |
| 2410 | /* Append to end */ |
| 2411 | vec_add1 (cf->current_command, input); |
| 2412 | cf->cursor++; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2413 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2414 | /* Echo the character back to the client */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2415 | unix_vlib_cli_output_cooked (cf, uf, &input, 1); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2416 | } |
| 2417 | else |
| 2418 | { |
| 2419 | /* Insert at cursor: resize +1 byte, move everything over */ |
| 2420 | j = vec_len (cf->current_command) - cf->cursor; |
| 2421 | vec_add1 (cf->current_command, (u8) 'A'); |
| 2422 | memmove (cf->current_command + cf->cursor + 1, |
| 2423 | cf->current_command + cf->cursor, j); |
| 2424 | cf->current_command[cf->cursor] = input; |
| 2425 | /* Redraw the line */ |
| 2426 | j++; |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2427 | unix_vlib_cli_output_cooked (cf, uf, |
| 2428 | cf->current_command + cf->cursor, |
| 2429 | j); |
| 2430 | cf->cursor += j; |
| 2431 | j--; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2432 | /* Put terminal cursor back */ |
Chris Luke | fc379d2 | 2018-06-05 23:50:19 -0400 | [diff] [blame] | 2433 | for (; j > 0; j--, cf->cursor--) |
| 2434 | unix_vlib_cli_output_cursor_left (cf, uf); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2435 | } |
| 2436 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2437 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2438 | { |
| 2439 | /* no-op - not printable or otherwise not actionable */ |
| 2440 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2441 | |
| 2442 | found: |
| 2443 | |
| 2444 | break; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2445 | |
| 2446 | case UNIX_CLI_PARSE_ACTION_TELNETIAC: |
| 2447 | break; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2448 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2449 | return 1; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2450 | } |
| 2451 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 2452 | /** @brief Process input bytes on a stream to provide line editing and |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2453 | * command history in the CLI. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2454 | static int |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2455 | unix_cli_line_edit (unix_cli_main_t * cm, unix_main_t * um, |
| 2456 | clib_file_main_t * fm, unix_cli_file_t * cf) |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2457 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2458 | clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2459 | int i; |
| 2460 | |
| 2461 | for (i = 0; i < vec_len (cf->input_vector); i++) |
| 2462 | { |
| 2463 | unix_cli_parse_action_t action; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2464 | i32 matched = 0; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2465 | unix_cli_parse_actions_t *a; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2466 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2467 | /* If we're in the pager mode, search the pager actions */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2468 | a = |
| 2469 | vec_len (cf->pager_index) ? unix_cli_parse_pager : |
| 2470 | unix_cli_parse_strings; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2471 | |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2472 | /* See if the input buffer is some sort of control code */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2473 | action = unix_cli_match_action (a, &cf->input_vector[i], |
| 2474 | vec_len (cf->input_vector) - i, |
| 2475 | &matched); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2476 | |
| 2477 | switch (action) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2478 | { |
| 2479 | case UNIX_CLI_PARSE_ACTION_PARTIALMATCH: |
| 2480 | if (i) |
| 2481 | { |
| 2482 | /* There was a partial match which means we need more bytes |
| 2483 | * than the input buffer currently has. |
| 2484 | * Since the bytes before here have been processed, shift |
| 2485 | * the remaining contents to the start of the input buffer. |
| 2486 | */ |
| 2487 | vec_delete (cf->input_vector, i, 0); |
| 2488 | } |
| 2489 | return 1; /* wait for more */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2490 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2491 | case UNIX_CLI_PARSE_ACTION_TELNETIAC: |
| 2492 | /* process telnet options */ |
| 2493 | matched = unix_cli_process_telnet (um, cf, uf, |
| 2494 | cf->input_vector + i, |
| 2495 | vec_len (cf->input_vector) - i); |
| 2496 | if (matched < 0) |
| 2497 | { |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2498 | /* There was a partial match which means we need more bytes |
| 2499 | * than the input buffer currently has. |
| 2500 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2501 | if (i) |
| 2502 | { |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2503 | /* |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2504 | * Since the bytes before here have been processed, shift |
| 2505 | * the remaining contents to the start of the input buffer. |
| 2506 | */ |
| 2507 | vec_delete (cf->input_vector, i, 0); |
| 2508 | } |
| 2509 | return 1; /* wait for more */ |
| 2510 | } |
| 2511 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2512 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2513 | default: |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2514 | /* If telnet option processing switched us to line mode, get us |
| 2515 | * out of here! |
| 2516 | */ |
| 2517 | if (cf->line_mode) |
| 2518 | { |
| 2519 | vec_delete (cf->input_vector, i, 0); |
Benoît Ganne | 433a498 | 2020-10-01 18:20:23 +0200 | [diff] [blame] | 2520 | vec_free (cf->current_command); |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2521 | cf->current_command = cf->input_vector; |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2525 | /* process the action */ |
| 2526 | if (!unix_cli_line_process_one (cm, um, cf, uf, |
| 2527 | cf->input_vector[i], action)) |
| 2528 | { |
| 2529 | /* CRLF found. Consume the bytes from the input_vector */ |
| 2530 | vec_delete (cf->input_vector, i + matched, 0); |
| 2531 | /* And tell our caller to execute cf->input_command */ |
| 2532 | return 0; |
| 2533 | } |
| 2534 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2535 | |
| 2536 | i += matched; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2537 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 2538 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2539 | vec_reset_length (cf->input_vector); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2540 | return 1; |
| 2541 | } |
| 2542 | |
Chris Luke | 8e5b041 | 2016-07-26 13:06:10 -0400 | [diff] [blame] | 2543 | /** @brief Process input to a CLI session. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2544 | static void |
| 2545 | unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2546 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2547 | unix_main_t *um = &unix_main; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2548 | clib_file_main_t *fm = &file_main; |
| 2549 | clib_file_t *uf; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2550 | unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2551 | unformat_input_t input; |
| 2552 | int vlib_parse_eval (u8 *); |
| 2553 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2554 | cm->current_input_file_index = cli_file_index; |
| 2555 | |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2556 | more: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2557 | /* Try vlibplex first. Someday... */ |
| 2558 | if (0 && vlib_parse_eval (cf->input_vector) == 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2559 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2560 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2561 | |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2562 | if (cf->line_mode) |
| 2563 | { |
| 2564 | /* just treat whatever we got as a complete line of input */ |
| 2565 | cf->current_command = cf->input_vector; |
| 2566 | } |
| 2567 | else |
| 2568 | { |
| 2569 | /* Line edit, echo, etc. */ |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2570 | if (unix_cli_line_edit (cm, um, fm, cf)) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2571 | /* want more input */ |
| 2572 | return; |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2573 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2574 | |
| 2575 | if (um->log_fd) |
| 2576 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2577 | static u8 *lv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2578 | vec_reset_length (lv); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2579 | lv = format (lv, "%U[%d]: %v", |
| 2580 | format_timeval, 0 /* current bat-time */ , |
| 2581 | 0 /* current bat-format */ , |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2582 | cli_file_index, cf->current_command); |
Fan Zhang | ce266ad | 2020-03-12 09:26:38 +0000 | [diff] [blame] | 2583 | if ((vec_len (cf->current_command) > 0) && |
| 2584 | (cf->current_command[vec_len (cf->current_command) - 1] != '\n')) |
Paul Vinciguerra | 8a023fd | 2020-03-01 00:47:17 -0500 | [diff] [blame] | 2585 | lv = format (lv, "\n"); |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2586 | int rv __attribute__ ((unused)) = write (um->log_fd, lv, vec_len (lv)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2587 | } |
| 2588 | |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 2589 | /* Run the command through the macro processor */ |
| 2590 | if (vec_len (cf->current_command)) |
| 2591 | { |
| 2592 | u8 *expanded; |
| 2593 | vec_validate (cf->current_command, vec_len (cf->current_command)); |
| 2594 | cf->current_command[vec_len (cf->current_command) - 1] = 0; |
| 2595 | /* The macro expander expects proper C-strings, not vectors */ |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 2596 | expanded = (u8 *) clib_macro_eval (&cf->macro_main, |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 2597 | (i8 *) cf->current_command, |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 2598 | 1 /* complain */ , |
| 2599 | 0 /* level */ , |
| 2600 | 8 /* max_level */ ); |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 2601 | /* Macro processor NULL terminates the return */ |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 2602 | vec_dec_len (expanded, 1); |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 2603 | vec_reset_length (cf->current_command); |
| 2604 | vec_append (cf->current_command, expanded); |
| 2605 | vec_free (expanded); |
| 2606 | } |
| 2607 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2608 | /* Build an unformat structure around our command */ |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2609 | unformat_init_vector (&input, cf->current_command); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2610 | |
| 2611 | /* Remove leading white space from input. */ |
| 2612 | (void) unformat (&input, ""); |
| 2613 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2614 | cf->pager_start = 0; /* start a new pager session */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2615 | |
| 2616 | if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2617 | vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output, |
| 2618 | cli_file_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2619 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2620 | /* Zero buffer since otherwise unformat_free will call vec_free on it. */ |
| 2621 | input.buffer = 0; |
| 2622 | |
| 2623 | unformat_free (&input); |
| 2624 | |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2625 | /* Re-fetch pointer since pool may have moved. */ |
| 2626 | cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2627 | uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index); |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2628 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2629 | done: |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2630 | /* reset vector; we'll re-use it later */ |
| 2631 | if (cf->line_mode) |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2632 | { |
| 2633 | vec_reset_length (cf->input_vector); |
| 2634 | cf->current_command = 0; |
| 2635 | } |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2636 | else |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2637 | { |
| 2638 | vec_reset_length (cf->current_command); |
| 2639 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2640 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2641 | if (cf->no_pager == 2) |
| 2642 | { |
| 2643 | /* Pager was programmatically disabled */ |
| 2644 | unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n"); |
| 2645 | cf->no_pager = um->cli_no_pager; |
| 2646 | } |
| 2647 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2648 | if (vec_len (cf->pager_index) == 0 |
| 2649 | || vec_len (cf->pager_index) < cf->height) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2650 | { |
| 2651 | /* There was no need for the pager */ |
| 2652 | unix_cli_pager_reset (cf); |
| 2653 | |
| 2654 | /* Prompt. */ |
| 2655 | unix_cli_cli_prompt (cf, uf); |
| 2656 | } |
| 2657 | else |
| 2658 | { |
| 2659 | /* Display the pager prompt */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2660 | unix_cli_pager_prompt (cf, uf); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 2661 | } |
Chris Luke | 93dcd1d | 2016-05-10 10:45:10 -0400 | [diff] [blame] | 2662 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2663 | /* Any residual data in the input vector? */ |
| 2664 | if (vec_len (cf->input_vector)) |
| 2665 | goto more; |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2666 | |
| 2667 | /* For non-interactive sessions send a NUL byte. |
| 2668 | * Specifically this is because vppctl needs to see some traffic in |
| 2669 | * order to move on to closing the session. Commands with no output |
| 2670 | * would thus cause vppctl to hang indefinitely in non-interactive mode |
| 2671 | * since there is also no prompt sent after the command completes. |
| 2672 | */ |
| 2673 | if (!cf->is_interactive) |
| 2674 | unix_vlib_cli_output_raw (cf, uf, (u8 *) "\0", 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2675 | } |
| 2676 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 2677 | /** Destroy a CLI session. |
| 2678 | * @note If we destroy the @c stdin session this additionally signals |
| 2679 | * the shutdown of VPP. |
| 2680 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2681 | static void |
| 2682 | unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2683 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2684 | unix_main_t *um = &unix_main; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2685 | clib_file_main_t *fm = &file_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2686 | unix_cli_file_t *cf; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2687 | clib_file_t *uf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2688 | int i; |
| 2689 | |
Steve Shin | 0d5a195 | 2018-06-29 09:40:20 -0700 | [diff] [blame] | 2690 | /* Validate cli_file_index */ |
| 2691 | if (pool_is_free_index (cm->cli_file_pool, cli_file_index)) |
| 2692 | return; |
| 2693 | |
Steven Luong | 17a6721 | 2021-08-20 19:14:16 -0700 | [diff] [blame] | 2694 | vec_foreach_index (i, cm->new_sessions) |
| 2695 | { |
| 2696 | unix_cli_new_session_t *ns = vec_elt_at_index (cm->new_sessions, i); |
| 2697 | |
| 2698 | if (ns->cf_index == cli_file_index) |
| 2699 | { |
Vladislav Grishenko | 49ebbf7 | 2021-12-29 14:30:32 +0500 | [diff] [blame] | 2700 | ns->cf_index = ~0; |
Steven Luong | 17a6721 | 2021-08-20 19:14:16 -0700 | [diff] [blame] | 2701 | break; |
| 2702 | } |
| 2703 | } |
| 2704 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2705 | cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2706 | uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2707 | |
| 2708 | /* Quit/EOF on stdin means quit program. */ |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2709 | if (uf->file_descriptor == STDIN_FILENO) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2710 | clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI); |
| 2711 | |
| 2712 | vec_free (cf->current_command); |
| 2713 | vec_free (cf->search_key); |
| 2714 | |
| 2715 | for (i = 0; i < vec_len (cf->command_history); i++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2716 | vec_free (cf->command_history[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2717 | |
| 2718 | vec_free (cf->command_history); |
Dave Barach | 5f09efe | 2020-11-09 17:11:57 -0500 | [diff] [blame] | 2719 | vec_free (cf->input_vector); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2720 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2721 | clib_file_del (fm, uf); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2722 | |
| 2723 | unix_cli_file_free (cf); |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 2724 | clib_macro_free (&cf->macro_main); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2725 | pool_put (cm->cli_file_pool, cf); |
| 2726 | } |
| 2727 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 2728 | /** Handle system events. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2729 | static uword |
| 2730 | unix_cli_process (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2731 | vlib_node_runtime_t * rt, vlib_frame_t * f) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2732 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2733 | unix_cli_main_t *cm = &unix_cli_main; |
| 2734 | uword i, *data = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2735 | |
| 2736 | while (1) |
| 2737 | { |
| 2738 | unix_cli_process_event_type_t event_type; |
| 2739 | vlib_process_wait_for_event (vm); |
| 2740 | event_type = vlib_process_get_events (vm, &data); |
| 2741 | |
| 2742 | switch (event_type) |
| 2743 | { |
| 2744 | case UNIX_CLI_PROCESS_EVENT_READ_READY: |
| 2745 | for (i = 0; i < vec_len (data); i++) |
| 2746 | unix_cli_process_input (cm, data[i]); |
| 2747 | break; |
| 2748 | |
| 2749 | case UNIX_CLI_PROCESS_EVENT_QUIT: |
| 2750 | /* Kill this process. */ |
| 2751 | for (i = 0; i < vec_len (data); i++) |
| 2752 | unix_cli_kill (cm, data[i]); |
| 2753 | goto done; |
| 2754 | } |
| 2755 | |
| 2756 | if (data) |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 2757 | vec_set_len (data, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2760 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2761 | vec_free (data); |
| 2762 | |
| 2763 | vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED); |
| 2764 | |
| 2765 | /* Add node index so we can re-use this process later. */ |
| 2766 | vec_add1 (cm->unused_cli_process_node_indices, rt->node_index); |
| 2767 | |
| 2768 | return 0; |
| 2769 | } |
| 2770 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 2771 | /** Called when a CLI session file descriptor can be written to without |
| 2772 | * blocking. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2773 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2774 | unix_cli_write_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2775 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2776 | unix_cli_main_t *cm = &unix_cli_main; |
| 2777 | unix_cli_file_t *cf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2778 | int n; |
| 2779 | |
| 2780 | cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data); |
| 2781 | |
| 2782 | /* Flush output vector. */ |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2783 | if (cf->is_socket) |
| 2784 | /* If it's a socket we use MSG_NOSIGNAL to prevent SIGPIPE */ |
| 2785 | n = send (uf->file_descriptor, |
| 2786 | cf->output_vector, vec_len (cf->output_vector), MSG_NOSIGNAL); |
| 2787 | else |
| 2788 | n = write (uf->file_descriptor, |
| 2789 | cf->output_vector, vec_len (cf->output_vector)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2790 | |
| 2791 | if (n < 0 && errno != EAGAIN) |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2792 | { |
| 2793 | if (errno == EPIPE) |
| 2794 | { |
| 2795 | /* connection closed on us */ |
| 2796 | unix_main_t *um = &unix_main; |
| 2797 | cf->has_epipe = 1; |
| 2798 | vlib_process_signal_event (um->vlib_main, cf->process_node_index, |
| 2799 | UNIX_CLI_PROCESS_EVENT_QUIT, |
| 2800 | uf->private_data); |
| 2801 | } |
| 2802 | else |
| 2803 | { |
| 2804 | return clib_error_return_unix (0, "write"); |
| 2805 | } |
| 2806 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2807 | |
| 2808 | else if (n > 0) |
| 2809 | unix_cli_del_pending_output (uf, cf, n); |
| 2810 | |
| 2811 | return /* no error */ 0; |
| 2812 | } |
| 2813 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 2814 | /** Called when a CLI session file descriptor has data to be read. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2815 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2816 | unix_cli_read_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2817 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2818 | unix_main_t *um = &unix_main; |
| 2819 | unix_cli_main_t *cm = &unix_cli_main; |
| 2820 | unix_cli_file_t *cf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2821 | uword l; |
| 2822 | int n, n_read, n_try; |
| 2823 | |
| 2824 | cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data); |
| 2825 | |
| 2826 | n = n_try = 4096; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2827 | while (n == n_try) |
| 2828 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2829 | l = vec_len (cf->input_vector); |
| 2830 | vec_resize (cf->input_vector, l + n_try); |
| 2831 | |
| 2832 | n = read (uf->file_descriptor, cf->input_vector + l, n_try); |
| 2833 | |
| 2834 | /* Error? */ |
| 2835 | if (n < 0 && errno != EAGAIN) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2836 | return clib_error_return_unix (0, "read"); |
| 2837 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2838 | n_read = n < 0 ? 0 : n; |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 2839 | vec_set_len (cf->input_vector, l + n_read); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2840 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2841 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2842 | if (!(n < 0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2843 | vlib_process_signal_event (um->vlib_main, |
| 2844 | cf->process_node_index, |
| 2845 | (n_read == 0 |
| 2846 | ? UNIX_CLI_PROCESS_EVENT_QUIT |
| 2847 | : UNIX_CLI_PROCESS_EVENT_READ_READY), |
| 2848 | /* event data */ uf->private_data); |
| 2849 | |
| 2850 | return /* no error */ 0; |
| 2851 | } |
| 2852 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2853 | /** Called when a CLI session file descriptor has an error condition. */ |
| 2854 | static clib_error_t * |
| 2855 | unix_cli_error_detected (clib_file_t * uf) |
| 2856 | { |
| 2857 | unix_main_t *um = &unix_main; |
| 2858 | unix_cli_main_t *cm = &unix_cli_main; |
| 2859 | unix_cli_file_t *cf; |
| 2860 | |
| 2861 | cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data); |
| 2862 | cf->has_epipe = 1; /* prevent writes while the close is pending */ |
| 2863 | vlib_process_signal_event (um->vlib_main, |
| 2864 | cf->process_node_index, |
| 2865 | UNIX_CLI_PROCESS_EVENT_QUIT, |
| 2866 | /* event data */ uf->private_data); |
| 2867 | |
| 2868 | return /* no error */ 0; |
| 2869 | } |
| 2870 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 2871 | /** Store a new CLI session. |
| 2872 | * @param name The name of the session. |
| 2873 | * @param fd The file descriptor for the session I/O. |
| 2874 | * @return The session ID. |
| 2875 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2876 | static u32 |
| 2877 | unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2878 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2879 | unix_main_t *um = &unix_main; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2880 | clib_file_main_t *fm = &file_main; |
Dave Barach | 52c33d6 | 2021-06-11 12:57:23 -0400 | [diff] [blame] | 2881 | vlib_node_main_t *nm = &vlib_get_main ()->node_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2882 | unix_cli_file_t *cf; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2883 | clib_file_t template = { 0 }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2884 | vlib_main_t *vm = um->vlib_main; |
Dave Barach | 0610039 | 2018-07-12 13:00:47 -0400 | [diff] [blame] | 2885 | vlib_node_t *n = 0; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 2886 | u8 *file_desc = 0; |
| 2887 | |
| 2888 | file_desc = format (0, "%s", name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2889 | |
| 2890 | name = (char *) format (0, "unix-cli-%s", name); |
| 2891 | |
| 2892 | if (vec_len (cm->unused_cli_process_node_indices) > 0) |
| 2893 | { |
| 2894 | uword l = vec_len (cm->unused_cli_process_node_indices); |
Dave Barach | 0610039 | 2018-07-12 13:00:47 -0400 | [diff] [blame] | 2895 | int i; |
| 2896 | vlib_main_t *this_vlib_main; |
| 2897 | u8 *old_name = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2898 | |
Dave Barach | 0610039 | 2018-07-12 13:00:47 -0400 | [diff] [blame] | 2899 | /* |
| 2900 | * Nodes are bulk-copied, so node name pointers are shared. |
| 2901 | * Find the cli node in all graph replicas, and give all of them |
| 2902 | * the same new name. |
| 2903 | * Then, throw away the old shared name-vector. |
| 2904 | */ |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 2905 | for (i = 0; i < vlib_get_n_threads (); i++) |
Dave Barach | 0610039 | 2018-07-12 13:00:47 -0400 | [diff] [blame] | 2906 | { |
Damjan Marion | 6ffb7c6 | 2021-03-26 13:06:13 +0100 | [diff] [blame] | 2907 | this_vlib_main = vlib_get_main_by_index (i); |
Dave Barach | 0610039 | 2018-07-12 13:00:47 -0400 | [diff] [blame] | 2908 | if (this_vlib_main == 0) |
| 2909 | continue; |
| 2910 | n = vlib_get_node (this_vlib_main, |
| 2911 | cm->unused_cli_process_node_indices[l - 1]); |
| 2912 | old_name = n->name; |
| 2913 | n->name = (u8 *) name; |
| 2914 | } |
Dave Barach | 52c33d6 | 2021-06-11 12:57:23 -0400 | [diff] [blame] | 2915 | ASSERT (old_name); |
| 2916 | hash_unset (nm->node_by_name, old_name); |
| 2917 | hash_set (nm->node_by_name, name, n->index); |
Dave Barach | 0610039 | 2018-07-12 13:00:47 -0400 | [diff] [blame] | 2918 | vec_free (old_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2919 | |
| 2920 | vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 2921 | vec_set_len (cm->unused_cli_process_node_indices, l - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2922 | } |
| 2923 | else |
| 2924 | { |
| 2925 | static vlib_node_registration_t r = { |
| 2926 | .function = unix_cli_process, |
| 2927 | .type = VLIB_NODE_TYPE_PROCESS, |
Chenmin Sun | 2fd44a0 | 2019-10-08 03:35:20 +0800 | [diff] [blame] | 2928 | .process_log2_n_stack_bytes = 18, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2929 | }; |
| 2930 | |
Dave Barach | 0610039 | 2018-07-12 13:00:47 -0400 | [diff] [blame] | 2931 | vlib_worker_thread_barrier_sync (vm); |
| 2932 | |
Damjan Marion | f87acfa | 2022-03-23 17:36:56 +0100 | [diff] [blame] | 2933 | vlib_register_node (vm, &r, "%v", name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2934 | vec_free (name); |
| 2935 | |
| 2936 | n = vlib_get_node (vm, r.index); |
Dave Barach | 0610039 | 2018-07-12 13:00:47 -0400 | [diff] [blame] | 2937 | vlib_worker_thread_node_runtime_update (); |
| 2938 | vlib_worker_thread_barrier_release (vm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | pool_get (cm->cli_file_pool, cf); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 2942 | clib_memset (cf, 0, sizeof (*cf)); |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 2943 | clib_macro_init (&cf->macro_main); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2944 | |
| 2945 | template.read_function = unix_cli_read_ready; |
| 2946 | template.write_function = unix_cli_write_ready; |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2947 | template.error_function = unix_cli_error_detected; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2948 | template.file_descriptor = fd; |
| 2949 | template.private_data = cf - cm->cli_file_pool; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 2950 | template.description = file_desc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2951 | |
| 2952 | cf->process_node_index = n->index; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2953 | cf->clib_file_index = clib_file_add (fm, &template); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2954 | cf->output_vector = 0; |
| 2955 | cf->input_vector = 0; |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 2956 | vec_validate (cf->current_command, 0); |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 2957 | vec_set_len (cf->current_command, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2958 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2959 | vlib_start_process (vm, n->runtime_index); |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 2960 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2961 | vlib_process_t *p = vlib_get_process_from_node (vm, n); |
Andrew Yourtchenko | 716d959 | 2016-05-10 10:51:34 +0000 | [diff] [blame] | 2962 | p->output_function = unix_vlib_cli_output; |
| 2963 | p->output_function_arg = cf - cm->cli_file_pool; |
| 2964 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2965 | return cf - cm->cli_file_pool; |
| 2966 | } |
| 2967 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 2968 | /** Telnet listening socket has a new connection. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2969 | static clib_error_t * |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2970 | unix_cli_listen_read_ready (clib_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2971 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2972 | unix_main_t *um = &unix_main; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 2973 | clib_file_main_t *fm = &file_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2974 | unix_cli_main_t *cm = &unix_cli_main; |
| 2975 | clib_socket_t *s = &um->cli_listen_socket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2976 | clib_socket_t client; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 2977 | char *client_name; |
| 2978 | clib_error_t *error; |
| 2979 | unix_cli_file_t *cf; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2980 | u32 cf_index; |
Chris Luke | a9cf6af | 2018-09-05 21:00:52 -0400 | [diff] [blame] | 2981 | int one; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2982 | |
| 2983 | error = clib_socket_accept (s, &client); |
| 2984 | if (error) |
| 2985 | return error; |
| 2986 | |
Chris Luke | a9cf6af | 2018-09-05 21:00:52 -0400 | [diff] [blame] | 2987 | /* Disable Nagle, ignore any errors doing so eg on PF_LOCAL socket */ |
| 2988 | one = 1; |
Chris Luke | c5cb638 | 2018-09-06 15:37:28 -0400 | [diff] [blame] | 2989 | (void) setsockopt (client.fd, IPPROTO_TCP, TCP_NODELAY, |
| 2990 | (void *) &one, sizeof (one)); |
Chris Luke | a9cf6af | 2018-09-05 21:00:52 -0400 | [diff] [blame] | 2991 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2992 | client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0); |
| 2993 | |
| 2994 | cf_index = unix_cli_file_add (cm, client_name, client.fd); |
| 2995 | cf = pool_elt_at_index (cm->cli_file_pool, cf_index); |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 2996 | cf->is_socket = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 2997 | |
| 2998 | /* No longer need CLIB version of socket. */ |
| 2999 | clib_socket_free (&client); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3000 | vec_free (client_name); |
| 3001 | |
| 3002 | /* if we're supposed to run telnet session in character mode (default) */ |
| 3003 | if (um->cli_line_mode == 0) |
| 3004 | { |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3005 | /* |
| 3006 | * Set telnet client character mode, echo on, suppress "go-ahead". |
| 3007 | * Technically these should be negotiated, but this works. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3008 | */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3009 | u8 charmode_option[] = { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3010 | IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */ |
| 3011 | IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */ |
| 3012 | IAC, WILL, TELOPT_SGA, /* server willl supress GA */ |
| 3013 | IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */ |
| 3014 | IAC, WILL, TELOPT_ECHO, /* server will do echo */ |
| 3015 | IAC, DONT, TELOPT_ECHO, /* client should not echo */ |
| 3016 | IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */ |
| 3017 | IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */ |
| 3018 | IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */ |
| 3019 | IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3020 | }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3021 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3022 | /* Enable history on this CLI */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3023 | cf->history_limit = um->cli_history_limit; |
| 3024 | cf->has_history = cf->history_limit != 0; |
| 3025 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3026 | /* This is an interactive session until we decide otherwise */ |
| 3027 | cf->is_interactive = 1; |
| 3028 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3029 | /* Make sure this session is in line mode */ |
| 3030 | cf->line_mode = 0; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3031 | |
| 3032 | /* We need CRLF */ |
| 3033 | cf->crlf_mode = 1; |
| 3034 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3035 | /* Setup the pager */ |
| 3036 | cf->no_pager = um->cli_no_pager; |
| 3037 | |
Chris Luke | 2d8bf30 | 2017-11-12 22:26:37 -0500 | [diff] [blame] | 3038 | /* Default terminal dimensions, should the terminal |
| 3039 | * fail to provide any. |
| 3040 | */ |
| 3041 | cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH; |
| 3042 | cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT; |
| 3043 | |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3044 | /* Send the telnet options */ |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3045 | uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3046 | unix_vlib_cli_output_raw (cf, uf, charmode_option, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3047 | ARRAY_LEN (charmode_option)); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3048 | |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 3049 | if (cm->new_session_process_node_index == ~0) |
| 3050 | { |
| 3051 | /* Create thw new session deadline process */ |
| 3052 | cm->new_session_process_node_index = |
| 3053 | vlib_process_create (um->vlib_main, "unix-cli-new-session", |
| 3054 | unix_cli_new_session_process, |
| 3055 | 16 /* log2_n_stack_bytes */ ); |
| 3056 | } |
| 3057 | |
| 3058 | /* In case the client doesn't negotiate terminal type, register |
| 3059 | * our session with a process that will emit the prompt if |
| 3060 | * a deadline passes */ |
| 3061 | vlib_process_signal_event (um->vlib_main, |
| 3062 | cm->new_session_process_node_index, |
| 3063 | UNIX_CLI_NEW_SESSION_EVENT_ADD, cf_index); |
| 3064 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | return error; |
| 3068 | } |
| 3069 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3070 | /** The system terminal has informed us that the window size |
| 3071 | * has changed. |
| 3072 | */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3073 | static void |
| 3074 | unix_cli_resize_interrupt (int signum) |
| 3075 | { |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 3076 | clib_file_main_t *fm = &file_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3077 | unix_cli_main_t *cm = &unix_cli_main; |
| 3078 | unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, |
| 3079 | cm->stdin_cli_file_index); |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 3080 | clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3081 | struct winsize ws; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3082 | (void) signum; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3083 | |
| 3084 | /* Terminal resized, fetch the new size */ |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3085 | if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) < 0) |
Dave Barach | b2a6e25 | 2016-07-27 10:00:58 -0400 | [diff] [blame] | 3086 | { |
| 3087 | /* "Should never happen..." */ |
| 3088 | clib_unix_warning ("TIOCGWINSZ"); |
| 3089 | /* We can't trust ws.XXX... */ |
| 3090 | return; |
| 3091 | } |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 3092 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3093 | cf->width = ws.ws_col; |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 3094 | if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH) |
| 3095 | cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH; |
Chris Luke | 2d8bf30 | 2017-11-12 22:26:37 -0500 | [diff] [blame] | 3096 | if (cf->width == 0) |
| 3097 | cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH; |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 3098 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3099 | cf->height = ws.ws_row; |
Chris Luke | b2bcad6 | 2017-09-18 08:51:22 -0400 | [diff] [blame] | 3100 | if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT) |
| 3101 | cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT; |
Chris Luke | 2d8bf30 | 2017-11-12 22:26:37 -0500 | [diff] [blame] | 3102 | if (cf->height == 0) |
| 3103 | cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT; |
Chris Luke | 862623d | 2016-05-14 10:13:34 -0400 | [diff] [blame] | 3104 | |
| 3105 | /* Reindex the pager buffer */ |
| 3106 | unix_cli_pager_reindex (cf); |
| 3107 | |
| 3108 | /* Redraw the page */ |
| 3109 | unix_cli_pager_redraw (cf, uf); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3110 | } |
| 3111 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3112 | /** Handle configuration directives in the @em unix section. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3113 | static clib_error_t * |
| 3114 | unix_cli_config (vlib_main_t * vm, unformat_input_t * input) |
| 3115 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3116 | unix_main_t *um = &unix_main; |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 3117 | clib_file_main_t *fm = &file_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3118 | unix_cli_main_t *cm = &unix_cli_main; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3119 | int flags; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3120 | clib_error_t *error = 0; |
| 3121 | unix_cli_file_t *cf; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3122 | u32 cf_index; |
| 3123 | struct termios tio; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3124 | struct sigaction sa; |
| 3125 | struct winsize ws; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3126 | u8 *term; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3127 | |
| 3128 | /* We depend on unix flags being set. */ |
| 3129 | if ((error = vlib_call_config_function (vm, unix_config))) |
| 3130 | return error; |
| 3131 | |
| 3132 | if (um->flags & UNIX_FLAG_INTERACTIVE) |
| 3133 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3134 | /* Set stdin to be non-blocking. */ |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3135 | if ((flags = fcntl (STDIN_FILENO, F_GETFL, 0)) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3136 | flags = 0; |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3137 | (void) fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3138 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3139 | cf_index = unix_cli_file_add (cm, "stdin", STDIN_FILENO); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3140 | cf = pool_elt_at_index (cm->cli_file_pool, cf_index); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3141 | cm->stdin_cli_file_index = cf_index; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3142 | |
| 3143 | /* If stdin is a tty and we are using chacracter mode, enable |
| 3144 | * history on the CLI and set the tty line discipline accordingly. */ |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3145 | if (isatty (STDIN_FILENO) && um->cli_line_mode == 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3146 | { |
| 3147 | /* Capture terminal resize events */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 3148 | clib_memset (&sa, 0, sizeof (sa)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3149 | sa.sa_handler = unix_cli_resize_interrupt; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3150 | if (sigaction (SIGWINCH, &sa, 0) < 0) |
| 3151 | clib_panic ("sigaction"); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3152 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3153 | /* Retrieve the current terminal size */ |
Dave Barach | a6ef36b | 2020-02-11 10:29:13 -0500 | [diff] [blame] | 3154 | if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0) |
| 3155 | { |
| 3156 | cf->width = ws.ws_col; |
| 3157 | cf->height = ws.ws_row; |
| 3158 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3159 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3160 | if (cf->width == 0 || cf->height == 0) |
Dave Barach | b66201f | 2017-09-22 13:34:39 -0400 | [diff] [blame] | 3161 | { |
| 3162 | /* |
| 3163 | * We have a tty, but no size. Use defaults. |
| 3164 | * vpp "unix interactive" inside emacs + gdb ends up here. |
| 3165 | */ |
Chris Luke | 2d8bf30 | 2017-11-12 22:26:37 -0500 | [diff] [blame] | 3166 | cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH; |
| 3167 | cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT; |
Dave Barach | b66201f | 2017-09-22 13:34:39 -0400 | [diff] [blame] | 3168 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3169 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3170 | /* Setup the history */ |
| 3171 | cf->history_limit = um->cli_history_limit; |
| 3172 | cf->has_history = cf->history_limit != 0; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3173 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3174 | /* Setup the pager */ |
| 3175 | cf->no_pager = um->cli_no_pager; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3176 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3177 | /* This is an interactive session until we decide otherwise */ |
| 3178 | cf->is_interactive = 1; |
| 3179 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3180 | /* We're going to be in char by char mode */ |
| 3181 | cf->line_mode = 0; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3182 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3183 | /* Save the original tty state so we can restore it later */ |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3184 | tcgetattr (STDIN_FILENO, &um->tio_stdin); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3185 | um->tio_isset = 1; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3186 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3187 | /* Tweak the tty settings */ |
| 3188 | tio = um->tio_stdin; |
| 3189 | /* echo off, canonical mode off, ext'd input processing off */ |
| 3190 | tio.c_lflag &= ~(ECHO | ICANON | IEXTEN); |
Chris Luke | 01dc6b9 | 2018-06-10 13:42:45 -0400 | [diff] [blame] | 3191 | /* disable XON/XOFF, so ^S invokes the history search */ |
| 3192 | tio.c_iflag &= ~(IXON | IXOFF); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3193 | tio.c_cc[VMIN] = 1; /* 1 byte at a time */ |
| 3194 | tio.c_cc[VTIME] = 0; /* no timer */ |
Chris Luke | 01dc6b9 | 2018-06-10 13:42:45 -0400 | [diff] [blame] | 3195 | tio.c_cc[VSTOP] = _POSIX_VDISABLE; /* not ^S */ |
| 3196 | tio.c_cc[VSTART] = _POSIX_VDISABLE; /* not ^Q */ |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3197 | tcsetattr (STDIN_FILENO, TCSAFLUSH, &tio); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3198 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3199 | /* See if we can do ANSI/VT100 output */ |
| 3200 | term = (u8 *) getenv ("TERM"); |
| 3201 | if (term != NULL) |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3202 | { |
| 3203 | int len = strlen ((char *) term); |
| 3204 | cf->ansi_capable = unix_cli_terminal_type_ansi (term, len); |
| 3205 | if (unix_cli_terminal_type_noninteractive (term, len)) |
| 3206 | unix_cli_set_session_noninteractive (cf); |
| 3207 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3208 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3209 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3210 | { |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3211 | /* No tty, so make sure the session doesn't have tty-like features */ |
| 3212 | unix_cli_set_session_noninteractive (cf); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3213 | } |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3214 | |
| 3215 | /* Send banner and initial prompt */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3216 | unix_cli_file_welcome (cm, cf); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3217 | } |
| 3218 | |
Ed Warnicke | a25bd1c | 2016-04-01 22:43:37 -0500 | [diff] [blame] | 3219 | /* If we have socket config, LISTEN, otherwise, don't */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3220 | clib_socket_t *s = &um->cli_listen_socket; |
| 3221 | if (s->config && s->config[0] != 0) |
| 3222 | { |
| 3223 | /* CLI listen. */ |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 3224 | clib_file_t template = { 0 }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3225 | |
Damjan Marion | 57d963f | 2017-07-20 19:17:06 +0200 | [diff] [blame] | 3226 | /* mkdir of file socketu, only under /run */ |
| 3227 | if (strncmp (s->config, "/run", 4) == 0) |
Chris Luke | 475674e | 2017-07-05 18:02:53 -0400 | [diff] [blame] | 3228 | { |
Damjan Marion | 57d963f | 2017-07-20 19:17:06 +0200 | [diff] [blame] | 3229 | u8 *tmp = format (0, "%s", s->config); |
| 3230 | int i = vec_len (tmp); |
| 3231 | while (i && tmp[--i] != '/') |
| 3232 | ; |
| 3233 | |
YohanPipereau | 71dd9d5 | 2019-06-06 16:34:14 +0200 | [diff] [blame] | 3234 | tmp[i] = '\0'; |
Damjan Marion | 57d963f | 2017-07-20 19:17:06 +0200 | [diff] [blame] | 3235 | |
| 3236 | if (i) |
| 3237 | vlib_unix_recursive_mkdir ((char *) tmp); |
| 3238 | vec_free (tmp); |
Chris Luke | 475674e | 2017-07-05 18:02:53 -0400 | [diff] [blame] | 3239 | } |
| 3240 | |
Damjan Marion | f49921f | 2017-09-11 16:52:11 +0200 | [diff] [blame] | 3241 | s->flags = CLIB_SOCKET_F_IS_SERVER | /* listen, don't connect */ |
| 3242 | CLIB_SOCKET_F_ALLOW_GROUP_WRITE; /* PF_LOCAL socket only */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3243 | error = clib_socket_init (s); |
Ed Warnicke | a25bd1c | 2016-04-01 22:43:37 -0500 | [diff] [blame] | 3244 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3245 | if (error) |
| 3246 | return error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3247 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3248 | template.read_function = unix_cli_listen_read_ready; |
| 3249 | template.file_descriptor = s->fd; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 3250 | template.description = format (0, "cli listener %s", s->config); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3251 | |
Damjan Marion | 56dd543 | 2017-09-08 19:52:02 +0200 | [diff] [blame] | 3252 | clib_file_add (fm, &template); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3253 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3254 | |
| 3255 | /* Set CLI prompt. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3256 | if (!cm->cli_prompt) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3257 | cm->cli_prompt = format (0, "VLIB: "); |
| 3258 | |
| 3259 | return 0; |
| 3260 | } |
| 3261 | |
Chris Luke | 90f52bf | 2016-09-12 08:55:13 -0400 | [diff] [blame] | 3262 | /*? |
| 3263 | * This module has no configurable parameters. |
| 3264 | ?*/ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3265 | VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli"); |
| 3266 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3267 | /** Called when VPP is shutting down, this restores the system |
| 3268 | * terminal state if previously saved. |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3269 | */ |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3270 | static clib_error_t * |
| 3271 | unix_cli_exit (vlib_main_t * vm) |
| 3272 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3273 | unix_main_t *um = &unix_main; |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3274 | |
| 3275 | /* If stdin is a tty and we saved the tty state, reset the tty state */ |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3276 | if (isatty (STDIN_FILENO) && um->tio_isset) |
| 3277 | tcsetattr (STDIN_FILENO, TCSAFLUSH, &um->tio_stdin); |
Chris Luke | 0aca5eb | 2016-04-25 13:48:54 -0400 | [diff] [blame] | 3278 | |
| 3279 | return 0; |
| 3280 | } |
| 3281 | |
| 3282 | VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit); |
| 3283 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3284 | /** Set the CLI prompt. |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3285 | * @param prompt The C string to set the prompt to. |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3286 | * @note This setting is global; it impacts all current |
| 3287 | * and future CLI sessions. |
| 3288 | */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3289 | void |
| 3290 | vlib_unix_cli_set_prompt (char *prompt) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3291 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3292 | char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s "; |
| 3293 | unix_cli_main_t *cm = &unix_cli_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3294 | if (cm->cli_prompt) |
| 3295 | vec_free (cm->cli_prompt); |
| 3296 | cm->cli_prompt = format (0, fmt, prompt); |
| 3297 | } |
| 3298 | |
Benoît Ganne | a58be82 | 2020-05-11 16:27:29 +0200 | [diff] [blame] | 3299 | static unix_cli_file_t * |
Benoît Ganne | 10a22a6 | 2020-05-15 16:12:23 +0200 | [diff] [blame] | 3300 | unix_cli_file_if_exists (unix_cli_main_t * cm) |
| 3301 | { |
| 3302 | if (!cm->cli_file_pool) |
| 3303 | return 0; |
| 3304 | return pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index); |
| 3305 | } |
| 3306 | |
| 3307 | static unix_cli_file_t * |
Benoît Ganne | a58be82 | 2020-05-11 16:27:29 +0200 | [diff] [blame] | 3308 | unix_cli_file_if_interactive (unix_cli_main_t * cm) |
| 3309 | { |
| 3310 | unix_cli_file_t *cf; |
Benoît Ganne | 10a22a6 | 2020-05-15 16:12:23 +0200 | [diff] [blame] | 3311 | if ((cf = unix_cli_file_if_exists (cm)) && !cf->is_interactive) |
Benoît Ganne | a58be82 | 2020-05-11 16:27:29 +0200 | [diff] [blame] | 3312 | return 0; |
| 3313 | return cf; |
| 3314 | } |
| 3315 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3316 | /** CLI command to quit the terminal session. |
| 3317 | * @note If this is a stdin session then this will |
| 3318 | * shutdown VPP also. |
| 3319 | */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3320 | static clib_error_t * |
| 3321 | unix_cli_quit (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3322 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3323 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3324 | unix_cli_main_t *cm = &unix_cli_main; |
Benoît Ganne | a58be82 | 2020-05-11 16:27:29 +0200 | [diff] [blame] | 3325 | unix_cli_file_t *cf; |
| 3326 | |
Benoît Ganne | 10a22a6 | 2020-05-15 16:12:23 +0200 | [diff] [blame] | 3327 | if (!(cf = unix_cli_file_if_exists (cm))) |
| 3328 | return clib_error_return (0, "invalid session"); |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3329 | |
| 3330 | /* Cosmetic: suppress the final prompt from appearing before we die */ |
| 3331 | cf->is_interactive = 0; |
| 3332 | cf->started = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3333 | |
| 3334 | vlib_process_signal_event (vm, |
| 3335 | vlib_current_process (vm), |
| 3336 | UNIX_CLI_PROCESS_EVENT_QUIT, |
| 3337 | cm->current_input_file_index); |
| 3338 | return 0; |
| 3339 | } |
| 3340 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3341 | /*? |
| 3342 | * Terminates the current CLI session. |
| 3343 | * |
| 3344 | * If VPP is running in @em interactive mode and this is the console session |
| 3345 | * (that is, the session on @c stdin) then this will also terminate VPP. |
| 3346 | ?*/ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3347 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3348 | VLIB_CLI_COMMAND (unix_cli_quit_command, static) = { |
| 3349 | .path = "quit", |
| 3350 | .short_help = "Exit CLI", |
| 3351 | .function = unix_cli_quit, |
| 3352 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3353 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3354 | |
Florin Coras | 33d1629 | 2018-03-16 09:13:37 -0700 | [diff] [blame] | 3355 | /* *INDENT-OFF* */ |
| 3356 | VLIB_CLI_COMMAND (unix_cli_q_command, static) = { |
| 3357 | .path = "q", |
| 3358 | .short_help = "Exit CLI", |
| 3359 | .function = unix_cli_quit, |
| 3360 | }; |
| 3361 | /* *INDENT-ON* */ |
| 3362 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3363 | /** CLI command to execute a VPP command script. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3364 | static clib_error_t * |
| 3365 | unix_cli_exec (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3366 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3367 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3368 | char *file_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3369 | int fd; |
| 3370 | unformat_input_t sub_input; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3371 | clib_error_t *error; |
Dave Barach | 85866e7 | 2020-11-09 10:30:06 -0500 | [diff] [blame] | 3372 | unix_cli_main_t *cm = &unix_cli_main; |
| 3373 | unix_cli_file_t *cf; |
| 3374 | u8 *file_data = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3375 | file_name = 0; |
| 3376 | fd = -1; |
| 3377 | error = 0; |
Dave Barach | 85866e7 | 2020-11-09 10:30:06 -0500 | [diff] [blame] | 3378 | struct stat s; |
| 3379 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3380 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3381 | if (!unformat (input, "%s", &file_name)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3382 | { |
| 3383 | error = clib_error_return (0, "expecting file name, got `%U'", |
| 3384 | format_unformat_error, input); |
| 3385 | goto done; |
| 3386 | } |
| 3387 | |
| 3388 | fd = open (file_name, O_RDONLY); |
| 3389 | if (fd < 0) |
| 3390 | { |
| 3391 | error = clib_error_return_unix (0, "failed to open `%s'", file_name); |
| 3392 | goto done; |
| 3393 | } |
| 3394 | |
| 3395 | /* Make sure its a regular file. */ |
Dave Barach | 85866e7 | 2020-11-09 10:30:06 -0500 | [diff] [blame] | 3396 | if (fstat (fd, &s) < 0) |
| 3397 | { |
| 3398 | error = clib_error_return_unix (0, "failed to stat `%s'", file_name); |
| 3399 | goto done; |
| 3400 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3401 | |
Dave Barach | 85866e7 | 2020-11-09 10:30:06 -0500 | [diff] [blame] | 3402 | if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode))) |
| 3403 | { |
| 3404 | error = clib_error_return (0, "not a regular file `%s'", file_name); |
| 3405 | goto done; |
| 3406 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3407 | |
Dave Barach | 85866e7 | 2020-11-09 10:30:06 -0500 | [diff] [blame] | 3408 | /* Read the file */ |
| 3409 | vec_validate (file_data, s.st_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3410 | |
Dave Barach | 85866e7 | 2020-11-09 10:30:06 -0500 | [diff] [blame] | 3411 | if (read (fd, file_data, s.st_size) != s.st_size) |
| 3412 | { |
| 3413 | error = clib_error_return_unix (0, "Failed to read %d bytes from '%s'", |
| 3414 | s.st_size, file_name); |
| 3415 | vec_free (file_data); |
| 3416 | goto done; |
| 3417 | } |
| 3418 | |
| 3419 | /* The macro expander expects a c string... */ |
| 3420 | vec_add1 (file_data, 0); |
| 3421 | |
| 3422 | unformat_init_vector (&sub_input, file_data); |
| 3423 | |
| 3424 | /* Run the file contents through the macro processor */ |
| 3425 | if (vec_len (sub_input.buffer) > 1) |
| 3426 | { |
| 3427 | u8 *expanded; |
| 3428 | clib_macro_main_t *mm = 0; |
| 3429 | |
| 3430 | /* Initial config process? Use the global macro table. */ |
| 3431 | if (pool_is_free_index |
| 3432 | (cm->cli_file_pool, cm->current_input_file_index)) |
| 3433 | mm = &cm->macro_main; |
| 3434 | else |
| 3435 | { |
| 3436 | /* Otherwise, use the per-cli-process macro table */ |
| 3437 | cf = pool_elt_at_index (cm->cli_file_pool, |
| 3438 | cm->current_input_file_index); |
| 3439 | mm = &cf->macro_main; |
| 3440 | } |
| 3441 | |
| 3442 | expanded = (u8 *) clib_macro_eval (mm, |
| 3443 | (i8 *) sub_input.buffer, |
| 3444 | 1 /* complain */ , |
| 3445 | 0 /* level */ , |
| 3446 | 8 /* max_level */ ); |
| 3447 | /* Macro processor NULL terminates the return */ |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 3448 | vec_dec_len (expanded, 1); |
Dave Barach | 85866e7 | 2020-11-09 10:30:06 -0500 | [diff] [blame] | 3449 | vec_reset_length (sub_input.buffer); |
| 3450 | vec_append (sub_input.buffer, expanded); |
| 3451 | vec_free (expanded); |
| 3452 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3453 | |
Ray Kinsella | 457271d | 2021-05-25 15:55:52 +0100 | [diff] [blame] | 3454 | vlib_cli_input (vm, &sub_input, 0, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3455 | unformat_free (&sub_input); |
| 3456 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3457 | done: |
Dave Barach | a6ef36b | 2020-02-11 10:29:13 -0500 | [diff] [blame] | 3458 | if (fd >= 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3459 | close (fd); |
| 3460 | vec_free (file_name); |
| 3461 | |
| 3462 | return error; |
| 3463 | } |
| 3464 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3465 | /*? |
Billy McFall | 28cf3b7 | 2018-01-15 17:54:52 -0500 | [diff] [blame] | 3466 | * Executes a sequence of CLI commands which are read from a file. If |
Nathan Skrzypczak | ecd6b1a | 2021-09-29 15:35:31 +0200 | [diff] [blame] | 3467 | * a command is unrecognized or otherwise invalid then the usual CLI |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3468 | * feedback will be generated, however execution of subsequent commands |
| 3469 | * from the file will continue. |
Billy McFall | 28cf3b7 | 2018-01-15 17:54:52 -0500 | [diff] [blame] | 3470 | * |
| 3471 | * The VPP code is indifferent to the file location. However, if SELinux |
| 3472 | * is enabled, then the file needs to have an SELinux label the VPP |
| 3473 | * process is allowed to access. For example, if a file is created in |
| 3474 | * '<em>/usr/share/vpp/</em>', it will be allowed. However, files manually |
| 3475 | * created in '/tmp/' or '/home/<user>/' will not be accessible by the VPP |
| 3476 | * process when SELinux is enabled. |
| 3477 | * |
| 3478 | * @cliexpar |
| 3479 | * Sample file: |
| 3480 | * @clistart |
| 3481 | * <b><em>$ cat /usr/share/vpp/scripts/gigup.txt</em></b> |
| 3482 | * set interface state GigabitEthernet0/8/0 up |
| 3483 | * set interface state GigabitEthernet0/9/0 up |
| 3484 | * @cliend |
| 3485 | * Example of how to execute a set of CLI commands from a file: |
| 3486 | * @cliexcmd{exec /usr/share/vpp/scripts/gigup.txt} |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3487 | ?*/ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3488 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3489 | VLIB_CLI_COMMAND (cli_exec, static) = { |
| 3490 | .path = "exec", |
Billy McFall | 28cf3b7 | 2018-01-15 17:54:52 -0500 | [diff] [blame] | 3491 | .short_help = "exec <filename>", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3492 | .function = unix_cli_exec, |
Dave Barach | b2ef4dd | 2016-03-23 08:56:01 -0400 | [diff] [blame] | 3493 | .is_mp_safe = 1, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3494 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3495 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3496 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3497 | /** CLI command to show various unix error statistics. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3498 | static clib_error_t * |
| 3499 | unix_show_errors (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3500 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3501 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3502 | unix_main_t *um = &unix_main; |
| 3503 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3504 | int i, n_errors_to_show; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3505 | unix_error_history_t *unix_errors = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3506 | |
| 3507 | n_errors_to_show = 1 << 30; |
| 3508 | |
| 3509 | if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 3510 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3511 | if (!unformat (input, "%d", &n_errors_to_show)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3512 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3513 | error = |
| 3514 | clib_error_return (0, |
| 3515 | "expecting integer number of errors to show, got `%U'", |
| 3516 | format_unformat_error, input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3517 | goto done; |
| 3518 | } |
| 3519 | } |
| 3520 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3521 | n_errors_to_show = |
| 3522 | clib_min (ARRAY_LEN (um->error_history), n_errors_to_show); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3523 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3524 | i = |
| 3525 | um->error_history_index > |
| 3526 | 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3527 | |
| 3528 | while (n_errors_to_show > 0) |
| 3529 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3530 | unix_error_history_t *eh = um->error_history + i; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3531 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3532 | if (!eh->error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3533 | break; |
| 3534 | |
| 3535 | vec_add1 (unix_errors, eh[0]); |
| 3536 | n_errors_to_show -= 1; |
| 3537 | if (i == 0) |
| 3538 | i = ARRAY_LEN (um->error_history) - 1; |
| 3539 | else |
| 3540 | i--; |
| 3541 | } |
| 3542 | |
| 3543 | if (vec_len (unix_errors) == 0) |
| 3544 | vlib_cli_output (vm, "no Unix errors so far"); |
| 3545 | else |
| 3546 | { |
| 3547 | vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors); |
| 3548 | for (i = vec_len (unix_errors) - 1; i >= 0; i--) |
| 3549 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3550 | unix_error_history_t *eh = vec_elt_at_index (unix_errors, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3551 | vlib_cli_output (vm, "%U: %U", |
| 3552 | format_time_interval, "h:m:s:u", eh->time, |
| 3553 | format_clib_error, eh->error); |
| 3554 | } |
| 3555 | vlib_cli_output (vm, "%U: time now", |
| 3556 | format_time_interval, "h:m:s:u", vlib_time_now (vm)); |
| 3557 | } |
| 3558 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3559 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3560 | vec_free (unix_errors); |
| 3561 | return error; |
| 3562 | } |
| 3563 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3564 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3565 | VLIB_CLI_COMMAND (cli_unix_show_errors, static) = { |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 3566 | .path = "show unix errors", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3567 | .short_help = "Show Unix system call error history", |
| 3568 | .function = unix_show_errors, |
| 3569 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3570 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3571 | |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 3572 | /** CLI command to show various unix error statistics. */ |
| 3573 | static clib_error_t * |
| 3574 | unix_show_files (vlib_main_t * vm, |
| 3575 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 3576 | { |
| 3577 | clib_error_t *error = 0; |
| 3578 | clib_file_main_t *fm = &file_main; |
| 3579 | clib_file_t *f; |
| 3580 | char path[PATH_MAX]; |
| 3581 | u8 *s = 0; |
| 3582 | |
| 3583 | vlib_cli_output (vm, "%3s %6s %12s %12s %12s %-32s %s", "FD", "Thread", |
| 3584 | "Read", "Write", "Error", "File Name", "Description"); |
| 3585 | |
| 3586 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 3587 | pool_foreach (f, fm->file_pool) |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 3588 | { |
| 3589 | int rv; |
| 3590 | s = format (s, "/proc/self/fd/%d%c", f->file_descriptor, 0); |
| 3591 | rv = readlink((char *) s, path, PATH_MAX - 1); |
| 3592 | |
| 3593 | path[rv < 0 ? 0 : rv] = 0; |
| 3594 | |
| 3595 | vlib_cli_output (vm, "%3d %6d %12d %12d %12d %-32s %v", |
| 3596 | f->file_descriptor, f->polling_thread_index, |
| 3597 | f->read_events, f->write_events, f->error_events, |
| 3598 | path, f->description); |
| 3599 | vec_reset_length (s); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 3600 | } |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 3601 | /* *INDENT-ON* */ |
| 3602 | vec_free (s); |
| 3603 | |
| 3604 | return error; |
| 3605 | } |
| 3606 | |
| 3607 | /* *INDENT-OFF* */ |
| 3608 | VLIB_CLI_COMMAND (cli_unix_show_files, static) = { |
| 3609 | .path = "show unix files", |
| 3610 | .short_help = "Show Unix files in use", |
| 3611 | .function = unix_show_files, |
| 3612 | }; |
| 3613 | /* *INDENT-ON* */ |
| 3614 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3615 | /** CLI command to show session command history. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3616 | static clib_error_t * |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 3617 | unix_cli_show_history (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3618 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 3619 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3620 | unix_cli_main_t *cm = &unix_cli_main; |
| 3621 | unix_cli_file_t *cf; |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 3622 | int i, j; |
| 3623 | |
Benoît Ganne | a58be82 | 2020-05-11 16:27:29 +0200 | [diff] [blame] | 3624 | if (!(cf = unix_cli_file_if_interactive (cm))) |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3625 | return clib_error_return (0, "invalid for non-interactive sessions"); |
| 3626 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3627 | if (cf->has_history && cf->history_limit) |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 3628 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3629 | i = 1 + cf->command_number - vec_len (cf->command_history); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3630 | for (j = 0; j < vec_len (cf->command_history); j++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3631 | vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3632 | } |
| 3633 | else |
| 3634 | { |
| 3635 | vlib_cli_output (vm, "History not enabled.\n"); |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 3636 | } |
| 3637 | |
| 3638 | return 0; |
| 3639 | } |
| 3640 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3641 | /*? |
| 3642 | * Displays the command history for the current session, if any. |
| 3643 | ?*/ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3644 | /* *INDENT-OFF* */ |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 3645 | VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = { |
| 3646 | .path = "history", |
| 3647 | .short_help = "Show current session command history", |
| 3648 | .function = unix_cli_show_history, |
| 3649 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3650 | /* *INDENT-ON* */ |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 3651 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3652 | /** CLI command to show terminal status. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3653 | static clib_error_t * |
| 3654 | unix_cli_show_terminal (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3655 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3656 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3657 | unix_main_t *um = &unix_main; |
| 3658 | unix_cli_main_t *cm = &unix_cli_main; |
| 3659 | unix_cli_file_t *cf; |
| 3660 | vlib_node_t *n; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3661 | |
Benoît Ganne | 10a22a6 | 2020-05-15 16:12:23 +0200 | [diff] [blame] | 3662 | if (!(cf = unix_cli_file_if_exists (cm))) |
| 3663 | return clib_error_return (0, "invalid session"); |
Benoît Ganne | a58be82 | 2020-05-11 16:27:29 +0200 | [diff] [blame] | 3664 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3665 | n = vlib_get_node (vm, cf->process_node_index); |
| 3666 | |
| 3667 | vlib_cli_output (vm, "Terminal name: %v\n", n->name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3668 | vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ? |
| 3669 | "line-by-line" : "char-by-char"); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3670 | vlib_cli_output (vm, "Terminal width: %d\n", cf->width); |
| 3671 | vlib_cli_output (vm, "Terminal height: %d\n", cf->height); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3672 | vlib_cli_output (vm, "ANSI capable: %s\n", |
| 3673 | cf->ansi_capable ? "yes" : "no"); |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3674 | vlib_cli_output (vm, "Interactive: %s\n", |
| 3675 | cf->is_interactive ? "yes" : "no"); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3676 | vlib_cli_output (vm, "History enabled: %s%s\n", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3677 | cf->has_history ? "yes" : "no", !cf->has_history |
| 3678 | || cf->history_limit ? "" : |
| 3679 | " (disabled by history limit)"); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3680 | if (cf->has_history) |
| 3681 | vlib_cli_output (vm, "History limit: %d\n", cf->history_limit); |
| 3682 | vlib_cli_output (vm, "Pager enabled: %s%s%s\n", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3683 | cf->no_pager ? "no" : "yes", |
| 3684 | cf->no_pager |
| 3685 | || cf->height ? "" : " (disabled by terminal height)", |
| 3686 | cf->no_pager |
| 3687 | || um->cli_pager_buffer_limit ? "" : |
| 3688 | " (disabled by buffer limit)"); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3689 | if (!cf->no_pager) |
| 3690 | vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3691 | vlib_cli_output (vm, "CRLF mode: %s\n", |
| 3692 | cf->crlf_mode ? "CR+LF" : "LF"); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3693 | |
| 3694 | return 0; |
| 3695 | } |
| 3696 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3697 | /*? |
| 3698 | * Displays various information about the state of the current terminal |
| 3699 | * session. |
| 3700 | * |
| 3701 | * @cliexpar |
| 3702 | * @cliexstart{show terminal} |
| 3703 | * Terminal name: unix-cli-stdin |
| 3704 | * Terminal mode: char-by-char |
| 3705 | * Terminal width: 123 |
| 3706 | * Terminal height: 48 |
| 3707 | * ANSI capable: yes |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3708 | * Interactive: yes |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3709 | * History enabled: yes |
| 3710 | * History limit: 50 |
| 3711 | * Pager enabled: yes |
| 3712 | * Pager limit: 100000 |
| 3713 | * CRLF mode: LF |
| 3714 | * @cliexend |
| 3715 | ?*/ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3716 | /* *INDENT-OFF* */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3717 | VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = { |
| 3718 | .path = "show terminal", |
| 3719 | .short_help = "Show current session terminal settings", |
| 3720 | .function = unix_cli_show_terminal, |
| 3721 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3722 | /* *INDENT-ON* */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3723 | |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3724 | /** CLI command to display a list of CLI sessions. */ |
| 3725 | static clib_error_t * |
| 3726 | unix_cli_show_cli_sessions (vlib_main_t * vm, |
| 3727 | unformat_input_t * input, |
| 3728 | vlib_cli_command_t * cmd) |
| 3729 | { |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3730 | unix_cli_main_t *cm = &unix_cli_main; |
| 3731 | clib_file_main_t *fm = &file_main; |
| 3732 | unix_cli_file_t *cf; |
| 3733 | clib_file_t *uf; |
| 3734 | vlib_node_t *n; |
| 3735 | |
| 3736 | vlib_cli_output (vm, "%-5s %-5s %-20s %s", "PNI", "FD", "Name", "Flags"); |
| 3737 | |
| 3738 | #define fl(x, y) ( (x) ? toupper((y)) : tolower((y)) ) |
| 3739 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 3740 | pool_foreach (cf, cm->cli_file_pool) { |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3741 | uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index); |
| 3742 | n = vlib_get_node (vm, cf->process_node_index); |
| 3743 | vlib_cli_output (vm, |
| 3744 | "%-5d %-5d %-20v %c%c%c%c%c\n", |
| 3745 | cf->process_node_index, |
| 3746 | uf->file_descriptor, |
| 3747 | n->name, |
| 3748 | fl (cf->is_interactive, 'i'), |
| 3749 | fl (cf->is_socket, 's'), |
| 3750 | fl (cf->line_mode, 'l'), |
| 3751 | fl (cf->has_epipe, 'p'), |
| 3752 | fl (cf->ansi_capable, 'a')); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 3753 | } |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3754 | /* *INDENT-ON* */ |
| 3755 | #undef fl |
| 3756 | |
| 3757 | return 0; |
| 3758 | } |
| 3759 | |
| 3760 | /*? |
| 3761 | * Displays a summary of all the current CLI sessions. |
| 3762 | * |
| 3763 | * Typically used to diagnose connection issues with the CLI |
| 3764 | * socket. |
| 3765 | * |
| 3766 | * @cliexpar |
| 3767 | * @cliexstart{show cli-sessions} |
| 3768 | * PNI FD Name Flags |
| 3769 | * 343 0 unix-cli-stdin IslpA |
| 3770 | * 344 7 unix-cli-local:20 ISlpA |
| 3771 | * 346 8 unix-cli-local:21 iSLpa |
| 3772 | * @cliexend |
| 3773 | |
| 3774 | * In this example we have the debug console of the running process |
| 3775 | * on stdin/out, we have an interactive socket session and we also |
| 3776 | * have a non-interactive socket session. |
| 3777 | * |
| 3778 | * Fields: |
| 3779 | * |
| 3780 | * - @em PNI: Process node index. |
| 3781 | * - @em FD: Unix file descriptor. |
| 3782 | * - @em Name: Name of the session. |
| 3783 | * - @em Flags: Various flags that describe the state of the session. |
| 3784 | * |
| 3785 | * @em Flags have the following meanings; lower-case typically negates |
| 3786 | * upper-case: |
| 3787 | * |
| 3788 | * - @em I Interactive session. |
| 3789 | * - @em S Connected by socket. |
| 3790 | * - @em s Not a socket, likely stdin. |
| 3791 | * - @em L Line-by-line mode. |
| 3792 | * - @em l Char-by-char mode. |
| 3793 | * - @em P EPIPE detected on connection; it will close soon. |
| 3794 | * - @em A ANSI-capable terminal. |
| 3795 | ?*/ |
| 3796 | /* *INDENT-OFF* */ |
| 3797 | VLIB_CLI_COMMAND (cli_unix_cli_show_cli_sessions, static) = { |
| 3798 | .path = "show cli-sessions", |
| 3799 | .short_help = "Show current CLI sessions", |
| 3800 | .function = unix_cli_show_cli_sessions, |
| 3801 | }; |
| 3802 | /* *INDENT-ON* */ |
| 3803 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3804 | /** CLI command to set terminal pager settings. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3805 | static clib_error_t * |
| 3806 | unix_cli_set_terminal_pager (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3807 | unformat_input_t * input, |
| 3808 | vlib_cli_command_t * cmd) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3809 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3810 | unix_main_t *um = &unix_main; |
| 3811 | unix_cli_main_t *cm = &unix_cli_main; |
| 3812 | unix_cli_file_t *cf; |
| 3813 | unformat_input_t _line_input, *line_input = &_line_input; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3814 | clib_error_t *error = 0; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3815 | |
Benoît Ganne | a58be82 | 2020-05-11 16:27:29 +0200 | [diff] [blame] | 3816 | if (!(cf = unix_cli_file_if_interactive (cm))) |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3817 | return clib_error_return (0, "invalid for non-interactive sessions"); |
| 3818 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3819 | if (!unformat_user (input, unformat_line_input, line_input)) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3820 | return 0; |
| 3821 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3822 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 3823 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3824 | if (unformat (line_input, "on")) |
| 3825 | cf->no_pager = 0; |
| 3826 | else if (unformat (line_input, "off")) |
| 3827 | cf->no_pager = 1; |
| 3828 | else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit)) |
| 3829 | vlib_cli_output (vm, |
| 3830 | "Pager limit set to %u lines; note, this is global.\n", |
| 3831 | um->cli_pager_buffer_limit); |
| 3832 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3833 | { |
| 3834 | error = clib_error_return (0, "unknown parameter: `%U`", |
| 3835 | format_unformat_error, line_input); |
| 3836 | goto done; |
| 3837 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3838 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3839 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3840 | done: |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3841 | unformat_free (line_input); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3842 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3843 | return error; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3844 | } |
| 3845 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3846 | /*? |
| 3847 | * Enables or disables the terminal pager for this session. Generally |
| 3848 | * this defaults to enabled. |
| 3849 | * |
| 3850 | * Additionally allows the pager buffer size to be set; though note that |
| 3851 | * this value is set globally and not per session. |
| 3852 | ?*/ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3853 | /* *INDENT-OFF* */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3854 | VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = { |
| 3855 | .path = "set terminal pager", |
| 3856 | .short_help = "set terminal pager [on|off] [limit <lines>]", |
| 3857 | .function = unix_cli_set_terminal_pager, |
| 3858 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3859 | /* *INDENT-ON* */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3860 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3861 | /** CLI command to set terminal history settings. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3862 | static clib_error_t * |
| 3863 | unix_cli_set_terminal_history (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3864 | unformat_input_t * input, |
| 3865 | vlib_cli_command_t * cmd) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3866 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3867 | unix_cli_main_t *cm = &unix_cli_main; |
| 3868 | unix_cli_file_t *cf; |
| 3869 | unformat_input_t _line_input, *line_input = &_line_input; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3870 | u32 limit; |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3871 | clib_error_t *error = 0; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3872 | |
Benoît Ganne | a58be82 | 2020-05-11 16:27:29 +0200 | [diff] [blame] | 3873 | if (!(cf = unix_cli_file_if_interactive (cm))) |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3874 | return clib_error_return (0, "invalid for non-interactive sessions"); |
| 3875 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3876 | if (!unformat_user (input, unformat_line_input, line_input)) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3877 | return 0; |
| 3878 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3879 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 3880 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3881 | if (unformat (line_input, "on")) |
| 3882 | cf->has_history = 1; |
| 3883 | else if (unformat (line_input, "off")) |
| 3884 | cf->has_history = 0; |
| 3885 | else if (unformat (line_input, "limit %u", &cf->history_limit)) |
| 3886 | ; |
| 3887 | else |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3888 | { |
| 3889 | error = clib_error_return (0, "unknown parameter: `%U`", |
| 3890 | format_unformat_error, line_input); |
| 3891 | goto done; |
| 3892 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3893 | |
Chris Luke | 5022c6c | 2019-05-17 10:17:16 -0400 | [diff] [blame] | 3894 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3895 | |
Chris Luke | 5022c6c | 2019-05-17 10:17:16 -0400 | [diff] [blame] | 3896 | /* If we reduced history size, or turned it off, purge the history */ |
| 3897 | limit = cf->has_history ? cf->history_limit : 0; |
| 3898 | if (limit < vec_len (cf->command_history)) |
| 3899 | { |
| 3900 | u32 i; |
| 3901 | |
| 3902 | /* How many items to remove from the start of history */ |
| 3903 | limit = vec_len (cf->command_history) - limit; |
| 3904 | |
| 3905 | for (i = 0; i < limit; i++) |
| 3906 | vec_free (cf->command_history[i]); |
| 3907 | |
| 3908 | vec_delete (cf->command_history, limit, 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3909 | } |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3910 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3911 | done: |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3912 | unformat_free (line_input); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3913 | |
Billy McFall | a9a20e7 | 2017-02-15 11:39:12 -0500 | [diff] [blame] | 3914 | return error; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3915 | } |
| 3916 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3917 | /*? |
| 3918 | * Enables or disables the command history function of the current |
| 3919 | * terminal. Generally this defaults to enabled. |
| 3920 | * |
| 3921 | * This command also allows the maximum size of the history buffer for |
| 3922 | * this session to be altered. |
| 3923 | ?*/ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3924 | /* *INDENT-OFF* */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3925 | VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = { |
| 3926 | .path = "set terminal history", |
| 3927 | .short_help = "set terminal history [on|off] [limit <lines>]", |
| 3928 | .function = unix_cli_set_terminal_history, |
| 3929 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3930 | /* *INDENT-ON* */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3931 | |
Chris Luke | b585097 | 2016-05-03 16:34:59 -0400 | [diff] [blame] | 3932 | /** CLI command to set terminal ANSI settings. */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3933 | static clib_error_t * |
| 3934 | unix_cli_set_terminal_ansi (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3935 | unformat_input_t * input, |
| 3936 | vlib_cli_command_t * cmd) |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3937 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3938 | unix_cli_main_t *cm = &unix_cli_main; |
| 3939 | unix_cli_file_t *cf; |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3940 | |
Benoît Ganne | a58be82 | 2020-05-11 16:27:29 +0200 | [diff] [blame] | 3941 | if (!(cf = unix_cli_file_if_interactive (cm))) |
Chris Luke | 03add7f | 2017-09-20 23:31:24 -0400 | [diff] [blame] | 3942 | return clib_error_return (0, "invalid for non-interactive sessions"); |
| 3943 | |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3944 | if (unformat (input, "on")) |
| 3945 | cf->ansi_capable = 1; |
| 3946 | else if (unformat (input, "off")) |
| 3947 | cf->ansi_capable = 0; |
| 3948 | else |
| 3949 | return clib_error_return (0, "unknown parameter: `%U`", |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3950 | format_unformat_error, input); |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3951 | |
| 3952 | return 0; |
| 3953 | } |
| 3954 | |
Chris Luke | 54ccf22 | 2016-07-25 16:38:11 -0400 | [diff] [blame] | 3955 | /*? |
| 3956 | * Enables or disables the use of ANSI control sequences by this terminal. |
| 3957 | * The default will vary based on terminal detection at the start of the |
| 3958 | * session. |
| 3959 | * |
| 3960 | * ANSI control sequences are used in a small number of places to provide, |
| 3961 | * for example, color text output and to control the cursor in the pager. |
| 3962 | ?*/ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3963 | /* *INDENT-OFF* */ |
Chris Luke | 7afda3a | 2016-04-25 13:49:22 -0400 | [diff] [blame] | 3964 | VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = { |
| 3965 | .path = "set terminal ansi", |
| 3966 | .short_help = "set terminal ansi [on|off]", |
| 3967 | .function = unix_cli_set_terminal_ansi, |
| 3968 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 3969 | /* *INDENT-ON* */ |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 3970 | |
Paul Vinciguerra | bfd7d29 | 2019-10-26 22:25:49 -0400 | [diff] [blame] | 3971 | |
| 3972 | #define MAX_CLI_WAIT 86400 |
| 3973 | /** CLI command to wait <sec> seconds. Useful for exec script. */ |
| 3974 | static clib_error_t * |
| 3975 | unix_wait_cmd (vlib_main_t * vm, |
| 3976 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 3977 | { |
| 3978 | unformat_input_t _line_input, *line_input = &_line_input; |
| 3979 | f64 sec = 1.0; |
| 3980 | |
| 3981 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 3982 | return 0; |
| 3983 | |
| 3984 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
| 3985 | { |
| 3986 | if (unformat (line_input, "%f", &sec)) |
| 3987 | ; |
| 3988 | else |
| 3989 | return clib_error_return (0, "unknown parameter: `%U`", |
| 3990 | format_unformat_error, input); |
| 3991 | } |
| 3992 | |
| 3993 | if (sec <= 0 || sec > MAX_CLI_WAIT || floor (sec * 1000) / 1000 != sec) |
| 3994 | return clib_error_return (0, |
| 3995 | "<sec> must be a positive value and less than 86400 (one day) with no more than msec precision."); |
| 3996 | |
| 3997 | vlib_process_wait_for_event_or_clock (vm, sec); |
| 3998 | vlib_cli_output (vm, "waited %.3f sec.", sec); |
| 3999 | |
| 4000 | unformat_free (line_input); |
| 4001 | return 0; |
| 4002 | } |
| 4003 | /* *INDENT-OFF* */ |
| 4004 | VLIB_CLI_COMMAND (cli_unix_wait_cmd, static) = { |
| 4005 | .path = "wait", |
| 4006 | .short_help = "wait <sec>", |
| 4007 | .function = unix_wait_cmd, |
| 4008 | }; |
| 4009 | /* *INDENT-ON* */ |
| 4010 | |
Chris Luke | 6b90fe3 | 2016-04-25 13:49:15 -0400 | [diff] [blame] | 4011 | static clib_error_t * |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 4012 | echo_cmd (vlib_main_t * vm, |
| 4013 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 4014 | { |
| 4015 | unformat_input_t _line_input, *line_input = &_line_input; |
| 4016 | |
| 4017 | /* Get a line of input. */ |
| 4018 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 4019 | { |
| 4020 | vlib_cli_output (vm, ""); |
| 4021 | return 0; |
| 4022 | } |
| 4023 | |
| 4024 | vlib_cli_output (vm, "%v", line_input->buffer); |
| 4025 | |
| 4026 | unformat_free (line_input); |
| 4027 | return 0; |
| 4028 | } |
| 4029 | |
| 4030 | /* *INDENT-OFF* */ |
| 4031 | VLIB_CLI_COMMAND (cli_unix_echo_cmd, static) = { |
| 4032 | .path = "echo", |
| 4033 | .short_help = "echo <rest-of-line>", |
| 4034 | .function = echo_cmd, |
| 4035 | }; |
| 4036 | /* *INDENT-ON* */ |
| 4037 | |
| 4038 | static clib_error_t * |
| 4039 | define_cmd_fn (vlib_main_t * vm, |
| 4040 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 4041 | { |
| 4042 | u8 *macro_name; |
| 4043 | unformat_input_t _line_input, *line_input = &_line_input; |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 4044 | clib_macro_main_t *mm = get_macro_main (); |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 4045 | clib_error_t *error; |
| 4046 | |
| 4047 | if (!unformat (input, "%s", ¯o_name)) |
| 4048 | return clib_error_return (0, "missing variable name..."); |
| 4049 | |
| 4050 | /* Remove white space */ |
| 4051 | (void) unformat (input, ""); |
| 4052 | |
| 4053 | /* Get a line of input. */ |
| 4054 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 4055 | { |
| 4056 | error = clib_error_return (0, "missing value for '%s'...", macro_name); |
| 4057 | vec_free (macro_name); |
| 4058 | return error; |
| 4059 | } |
| 4060 | /* the macro expander expects c-strings, not vectors... */ |
| 4061 | vec_add1 (line_input->buffer, 0); |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 4062 | clib_macro_set_value (mm, (char *) macro_name, (char *) line_input->buffer); |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 4063 | vec_free (macro_name); |
| 4064 | unformat_free (line_input); |
| 4065 | return 0; |
| 4066 | } |
| 4067 | |
| 4068 | /* *INDENT-OFF* */ |
| 4069 | VLIB_CLI_COMMAND (define_cmd, static) = { |
| 4070 | .path = "define", |
| 4071 | .short_help = "define <variable-name> <value>", |
| 4072 | .function = define_cmd_fn, |
| 4073 | }; |
| 4074 | |
| 4075 | /* *INDENT-ON* */ |
| 4076 | |
| 4077 | static clib_error_t * |
| 4078 | undefine_cmd_fn (vlib_main_t * vm, |
| 4079 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 4080 | { |
| 4081 | u8 *macro_name; |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 4082 | clib_macro_main_t *mm = get_macro_main (); |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 4083 | |
| 4084 | if (!unformat (input, "%s", ¯o_name)) |
| 4085 | return clib_error_return (0, "missing variable name..."); |
| 4086 | |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 4087 | if (clib_macro_unset (mm, (char *) macro_name)) |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 4088 | vlib_cli_output (vm, "%s wasn't set...", macro_name); |
| 4089 | |
| 4090 | vec_free (macro_name); |
| 4091 | return 0; |
| 4092 | } |
| 4093 | |
| 4094 | /* *INDENT-OFF* */ |
| 4095 | VLIB_CLI_COMMAND (undefine_cmd, static) = { |
| 4096 | .path = "undefine", |
| 4097 | .short_help = "undefine <variable-name>", |
| 4098 | .function = undefine_cmd_fn, |
| 4099 | }; |
| 4100 | /* *INDENT-ON* */ |
| 4101 | |
| 4102 | static clib_error_t * |
| 4103 | show_macro_cmd_fn (vlib_main_t * vm, |
| 4104 | unformat_input_t * input, vlib_cli_command_t * cmd) |
| 4105 | { |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 4106 | clib_macro_main_t *mm = get_macro_main (); |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 4107 | int evaluate = 1; |
| 4108 | |
| 4109 | if (unformat (input, "noevaluate %=", &evaluate, 0)) |
| 4110 | ; |
| 4111 | else if (unformat (input, "noeval %=", &evaluate, 0)) |
| 4112 | ; |
| 4113 | |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 4114 | vlib_cli_output (vm, "%U", format_clib_macro_main, mm, evaluate); |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 4115 | return 0; |
| 4116 | } |
| 4117 | |
| 4118 | /* *INDENT-OFF* */ |
| 4119 | VLIB_CLI_COMMAND (show_macro, static) = { |
| 4120 | .path = "show macro", |
| 4121 | .short_help = "show macro [noevaluate]", |
| 4122 | .function = show_macro_cmd_fn, |
| 4123 | }; |
| 4124 | /* *INDENT-ON* */ |
| 4125 | |
| 4126 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 4127 | unix_cli_init (vlib_main_t * vm) |
| 4128 | { |
Chris Luke | 6a3a4f7 | 2019-07-09 23:33:30 -0400 | [diff] [blame] | 4129 | unix_cli_main_t *cm = &unix_cli_main; |
| 4130 | |
| 4131 | /* Breadcrumb to indicate the new session process |
| 4132 | * has not been started */ |
| 4133 | cm->new_session_process_node_index = ~0; |
Dave Barach | 961e3c8 | 2020-06-18 17:04:18 -0400 | [diff] [blame] | 4134 | clib_macro_init (&cm->macro_main); |
Dave Barach | b30b954 | 2020-06-22 10:02:25 -0400 | [diff] [blame] | 4135 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 4136 | return 0; |
| 4137 | } |
| 4138 | |
| 4139 | VLIB_INIT_FUNCTION (unix_cli_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 4140 | |
| 4141 | /* |
| 4142 | * fd.io coding-style-patch-verification: ON |
| 4143 | * |
| 4144 | * Local Variables: |
| 4145 | * eval: (c-set-style "gnu") |
| 4146 | * End: |
| 4147 | */ |