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