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