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