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