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