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