blob: c7f0bccf2864647115f5533f44d1088b48a767da [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Lukeb5850972016-05-03 16:34:59 -040039/**
Chris Luke8e5b0412016-07-26 13:06:10 -040040 * @file
Chris Lukeb5850972016-05-03 16:34:59 -040041 * @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 Luke90f52bf2016-09-12 08:55:13 -040045/*? %%clicmd:group_label Command line session %% ?*/
46/*? %%syscfg:group_label Command line session %% ?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -070047
48#include <vlib/vlib.h>
49#include <vlib/unix/unix.h>
50
Chris Luke0aca5eb2016-04-25 13:48:54 -040051#include <ctype.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070052#include <fcntl.h>
53#include <sys/stat.h>
54#include <termios.h>
Chris Luke7afda3a2016-04-25 13:49:22 -040055#include <signal.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070056#include <unistd.h>
57#include <arpa/telnet.h>
Chris Luke7afda3a2016-04-25 13:49:22 -040058#include <sys/ioctl.h>
Damjan Marionf6616382017-06-21 12:01:37 +020059#include <sys/types.h>
60#include <unistd.h>
Damjan Marionceab7882018-01-19 20:56:12 +010061#include <limits.h>
Chris Lukea9cf6af2018-09-05 21:00:52 -040062#include <netinet/tcp.h>
Chris Luke6a3a4f72019-07-09 23:33:30 -040063#include <math.h>
Dave Barach961e3c82020-06-18 17:04:18 -040064#include <vppinfra/macros.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070065
Chris Lukeb5850972016-05-03 16:34:59 -040066/** ANSI escape code. */
Chris Luke0aca5eb2016-04-25 13:48:54 -040067#define ESC "\x1b"
68
Chris Lukeb5850972016-05-03 16:34:59 -040069/** ANSI Control Sequence Introducer. */
Chris Luke0aca5eb2016-04-25 13:48:54 -040070#define CSI ESC "["
71
Chris Lukeb5850972016-05-03 16:34:59 -040072/** ANSI clear screen. */
Chris Luke7afda3a2016-04-25 13:49:22 -040073#define ANSI_CLEAR CSI "2J" CSI "1;1H"
Chris Lukeb5850972016-05-03 16:34:59 -040074/** ANSI reset color settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -040075#define ANSI_RESET CSI "0m"
Chris Lukeb5850972016-05-03 16:34:59 -040076/** ANSI Start bold text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040077#define ANSI_BOLD CSI "1m"
Chris Lukeb5850972016-05-03 16:34:59 -040078/** ANSI Stop bold text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040079#define ANSI_DIM CSI "2m"
Chris Lukeb5850972016-05-03 16:34:59 -040080/** ANSI Start dark red text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040081#define ANSI_DRED ANSI_DIM CSI "31m"
Chris Lukeb5850972016-05-03 16:34:59 -040082/** ANSI Start bright red text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040083#define ANSI_BRED ANSI_BOLD CSI "31m"
Chris Lukeb5850972016-05-03 16:34:59 -040084/** ANSI clear line cursor is on. */
Chris Luke7afda3a2016-04-25 13:49:22 -040085#define ANSI_CLEARLINE CSI "2K"
Chris Lukeb5850972016-05-03 16:34:59 -040086/** ANSI scroll screen down one line. */
Chris Luke7afda3a2016-04-25 13:49:22 -040087#define ANSI_SCROLLDN CSI "1T"
Chris Lukeb5850972016-05-03 16:34:59 -040088/** ANSI save cursor position. */
Chris Luke7afda3a2016-04-25 13:49:22 -040089#define ANSI_SAVECURSOR CSI "s"
Chris Lukeb5850972016-05-03 16:34:59 -040090/** ANSI restore cursor position if previously saved. */
Chris Luke7afda3a2016-04-25 13:49:22 -040091#define ANSI_RESTCURSOR CSI "u"
Chris Luke0aca5eb2016-04-25 13:48:54 -040092
93/** Maximum depth into a byte stream from which to compile a Telnet
Chris Luke2d8bf302017-11-12 22:26:37 -050094 * protocol message. This is a safety measure. */
Vladimir Isaevb59095f2020-08-11 17:15:58 +030095#define UNIX_CLI_MAX_DEPTH_TELNET 32
Chris Luke0aca5eb2016-04-25 13:48:54 -040096
Chris Lukeb2bcad62017-09-18 08:51:22 -040097/** Maximum terminal width we will accept */
98#define UNIX_CLI_MAX_TERMINAL_WIDTH 512
Chris Lukeb2bcad62017-09-18 08:51:22 -040099/** Maximum terminal height we will accept */
100#define UNIX_CLI_MAX_TERMINAL_HEIGHT 512
Chris Luke2d8bf302017-11-12 22:26:37 -0500101/** Default terminal height */
102#define UNIX_CLI_DEFAULT_TERMINAL_HEIGHT 24
103/** Default terminal width */
104#define UNIX_CLI_DEFAULT_TERMINAL_WIDTH 80
Chris Luke0aca5eb2016-04-25 13:48:54 -0400105
Chris Lukeb5850972016-05-03 16:34:59 -0400106/** A CLI banner line. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400107typedef struct
108{
109 u8 *line; /**< The line to print. */
110 u32 length; /**< The length of the line without terminating NUL. */
Chris Luke572d8122016-04-25 13:49:07 -0400111} unix_cli_banner_t;
112
113#define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
114/** Plain welcome banner. */
115static unix_cli_banner_t unix_cli_banner[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400116 _(" _______ _ _ _____ ___ \n"),
117 _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
118 _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
119 _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
120 _("\n")
Chris Luke572d8122016-04-25 13:49:07 -0400121};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400122
Chris Luke572d8122016-04-25 13:49:07 -0400123/** ANSI color welcome banner. */
124static unix_cli_banner_t unix_cli_banner_color[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400125 _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
126 _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
127 _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
128 _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"),
129 _("\n")
Chris Luke572d8122016-04-25 13:49:07 -0400130};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400131
Chris Luke572d8122016-04-25 13:49:07 -0400132#undef _
133
Chris Luke862623d2016-05-14 10:13:34 -0400134/** Pager line index */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400135typedef struct
136{
Chris Luke862623d2016-05-14 10:13:34 -0400137 /** Index into pager_vector */
138 u32 line;
139
140 /** Offset of the string in the line */
141 u32 offset;
142
143 /** Length of the string in the line */
144 u32 length;
145} unix_cli_pager_index_t;
146
Chris Luke572d8122016-04-25 13:49:07 -0400147
Chris Luke0aca5eb2016-04-25 13:48:54 -0400148/** Unix CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149typedef struct
150{
Chris Lukeb5850972016-05-03 16:34:59 -0400151 /** The file index held by unix.c */
Damjan Marion56dd5432017-09-08 19:52:02 +0200152 u32 clib_file_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
Chris Lukeb5850972016-05-03 16:34:59 -0400154 /** Vector of output pending write to file descriptor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400155 u8 *output_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156
Chris Lukeb5850972016-05-03 16:34:59 -0400157 /** Vector of input saved by Unix input node to be processed by
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 CLI process. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400159 u8 *input_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160
Chris Luke54ccf222016-07-25 16:38:11 -0400161 /** This session has command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162 u8 has_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400163 /** Array of vectors of commands in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400164 u8 **command_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400165 /** The command currently pointed at by the history cursor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400166 u8 *current_command;
Chris Luke54ccf222016-07-25 16:38:11 -0400167 /** How far from the end of the history array the user has browsed. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168 i32 excursion;
Chris Luke6b90fe32016-04-25 13:49:15 -0400169
Chris Lukeb5850972016-05-03 16:34:59 -0400170 /** Maximum number of history entries this session will store. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 u32 history_limit;
Chris Luke6b90fe32016-04-25 13:49:15 -0400172
Chris Lukeb5850972016-05-03 16:34:59 -0400173 /** Current command line counter */
Chris Luke6b90fe32016-04-25 13:49:15 -0400174 u32 command_number;
175
Chris Luke54ccf222016-07-25 16:38:11 -0400176 /** The string being searched for in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400177 u8 *search_key;
Chris Luke54ccf222016-07-25 16:38:11 -0400178 /** If non-zero then the CLI is searching in the history array.
179 * - @c -1 means search backwards.
180 * - @c 1 means search forwards.
181 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182 int search_mode;
183
Chris Lukeb5850972016-05-03 16:34:59 -0400184 /** Position of the insert cursor on the current input line */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400185 u32 cursor;
186
Chris Lukeb5850972016-05-03 16:34:59 -0400187 /** Line mode or char mode */
Chris Luke7afda3a2016-04-25 13:49:22 -0400188 u8 line_mode;
189
Chris Lukeb5850972016-05-03 16:34:59 -0400190 /** Set if the CRLF mode wants CR + LF */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400191 u8 crlf_mode;
192
Chris Lukeb5850972016-05-03 16:34:59 -0400193 /** Can we do ANSI output? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400194 u8 ansi_capable;
195
Chris Lukeb5850972016-05-03 16:34:59 -0400196 /** Has the session started? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400197 u8 started;
198
Chris Lukeb5850972016-05-03 16:34:59 -0400199 /** Disable the pager? */
Chris Luke7afda3a2016-04-25 13:49:22 -0400200 u8 no_pager;
201
Chris Luke03add7f2017-09-20 23:31:24 -0400202 /** Whether the session is interactive or not.
203 * Controls things like initial banner, the CLI prompt etc. */
204 u8 is_interactive;
205
206 /** Whether the session is attached to a socket. */
207 u8 is_socket;
208
209 /** If EPIPE has been detected, prevent further write-related
210 * activity on the descriptor.
211 */
212 u8 has_epipe;
213
Chris Lukeb5850972016-05-03 16:34:59 -0400214 /** Pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400215 u8 **pager_vector;
Chris Luke7afda3a2016-04-25 13:49:22 -0400216
Chris Luke862623d2016-05-14 10:13:34 -0400217 /** Index of line fragments in the pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400218 unix_cli_pager_index_t *pager_index;
Chris Luke7afda3a2016-04-25 13:49:22 -0400219
Chris Lukeb5850972016-05-03 16:34:59 -0400220 /** Line number of top of page */
Chris Luke7afda3a2016-04-25 13:49:22 -0400221 u32 pager_start;
222
Chris Lukeb5850972016-05-03 16:34:59 -0400223 /** Terminal width */
224 u32 width;
Chris Luke7afda3a2016-04-25 13:49:22 -0400225
Chris Lukeb5850972016-05-03 16:34:59 -0400226 /** Terminal height */
227 u32 height;
228
229 /** Process node identifier */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230 u32 process_node_index;
Chris Lukefc379d22018-06-05 23:50:19 -0400231
232 /** The current direction of cursor travel.
233 * This is important since when advancing left-to-right, at the
234 * right hand edge of the console the terminal typically defers
235 * wrapping the cursor to the next line until a character is
236 * actually displayed.
237 * This messes up our heuristic for whether to use ANSI to return
238 * the cursor to the end of the line and instead we have to
239 * nudge the cursor to the next line.
240 * A Value of @c 0 means we're advancing left-to-right; @c 1 means
241 * the opposite.
242 */
243 u8 cursor_direction;
244
Dave Barachb30b9542020-06-22 10:02:25 -0400245 /** Macro tables for this session */
246 clib_macro_main_t macro_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247} unix_cli_file_t;
248
Chris Lukeb5850972016-05-03 16:34:59 -0400249/** Resets the pager buffer and other data.
250 * @param f The CLI session whose pager needs to be reset.
251 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400253unix_cli_pager_reset (unix_cli_file_t * f)
Chris Luke7afda3a2016-04-25 13:49:22 -0400254{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400255 u8 **p;
Chris Luke7afda3a2016-04-25 13:49:22 -0400256
Chris Luke862623d2016-05-14 10:13:34 -0400257 f->pager_start = 0;
258
259 vec_free (f->pager_index);
260 f->pager_index = 0;
261
Chris Luke7afda3a2016-04-25 13:49:22 -0400262 vec_foreach (p, f->pager_vector)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400263 {
264 vec_free (*p);
265 }
Chris Luke862623d2016-05-14 10:13:34 -0400266 vec_free (f->pager_vector);
Chris Luke7afda3a2016-04-25 13:49:22 -0400267 f->pager_vector = 0;
268}
269
Chris Lukeb5850972016-05-03 16:34:59 -0400270/** Release storage used by a CLI session.
271 * @param f The CLI session whose storage needs to be released.
272 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400273always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274unix_cli_file_free (unix_cli_file_t * f)
275{
276 vec_free (f->output_vector);
277 vec_free (f->input_vector);
Chris Luke862623d2016-05-14 10:13:34 -0400278 unix_cli_pager_reset (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279}
280
Chris Lukeb5850972016-05-03 16:34:59 -0400281/** CLI actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400282typedef enum
283{
Chris Luke54ccf222016-07-25 16:38:11 -0400284 UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */
285 UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */
286 UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */
287 UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */
288 UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */
289 UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */
290 UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */
291 UNIX_CLI_PARSE_ACTION_LEFT, /**< Left arrow */
292 UNIX_CLI_PARSE_ACTION_RIGHT, /**< Right arrow */
293 UNIX_CLI_PARSE_ACTION_HOME, /**< Home key (jump to start of line) */
294 UNIX_CLI_PARSE_ACTION_END, /**< End key (jump to end of line) */
295 UNIX_CLI_PARSE_ACTION_WORDLEFT, /**< Jump cursor to start of left word */
296 UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */
297 UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */
298 UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */
Hiroki Shirokura67e4df12019-08-16 11:30:34 +0000299 UNIX_CLI_PARSE_ACTION_ERASEWORDLEFT, /**< Erase word left */
Chris Luke54ccf222016-07-25 16:38:11 -0400300 UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */
301 UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */
302 UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */
303 UNIX_CLI_PARSE_ACTION_YANK, /**< Undo last erase action */
304 UNIX_CLI_PARSE_ACTION_TELNETIAC, /**< Telnet control code */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400305
Chris Luke54ccf222016-07-25 16:38:11 -0400306 UNIX_CLI_PARSE_ACTION_PAGER_CRLF, /**< Enter pressed (CR, CRLF, LF, etc) */
307 UNIX_CLI_PARSE_ACTION_PAGER_QUIT, /**< Exit the pager session */
308 UNIX_CLI_PARSE_ACTION_PAGER_NEXT, /**< Scroll to next page */
309 UNIX_CLI_PARSE_ACTION_PAGER_DN, /**< Scroll to next line */
310 UNIX_CLI_PARSE_ACTION_PAGER_UP, /**< Scroll to previous line */
311 UNIX_CLI_PARSE_ACTION_PAGER_TOP, /**< Scroll to first line */
312 UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM, /**< Scroll to last line */
313 UNIX_CLI_PARSE_ACTION_PAGER_PGDN, /**< Scroll to next page */
314 UNIX_CLI_PARSE_ACTION_PAGER_PGUP, /**< Scroll to previous page */
315 UNIX_CLI_PARSE_ACTION_PAGER_REDRAW, /**< Clear and redraw the page on the terminal */
316 UNIX_CLI_PARSE_ACTION_PAGER_SEARCH, /**< Search the pager buffer */
Chris Luke7afda3a2016-04-25 13:49:22 -0400317
Chris Luke54ccf222016-07-25 16:38:11 -0400318 UNIX_CLI_PARSE_ACTION_PARTIALMATCH, /**< Action parser found a partial match */
319 UNIX_CLI_PARSE_ACTION_NOMATCH /**< Action parser did not find any match */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400320} unix_cli_parse_action_t;
321
Chris Luke8e5b0412016-07-26 13:06:10 -0400322/** @brief Mapping of input buffer strings to action values.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400323 * @note This won't work as a hash since we need to be able to do
324 * partial matches on the string.
325 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400326typedef struct
327{
328 u8 *input; /**< Input string to match. */
329 u32 len; /**< Length of input without final NUL. */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400330 unix_cli_parse_action_t action; /**< Action to take when matched. */
331} unix_cli_parse_actions_t;
332
Chris Lukeb5850972016-05-03 16:34:59 -0400333/** @brief Given a capital ASCII letter character return a @c NUL terminated
Chris Luke0aca5eb2016-04-25 13:48:54 -0400334 * string with the control code for that letter.
Chris Lukeb5850972016-05-03 16:34:59 -0400335 *
336 * @param c An ASCII character.
337 * @return A @c NUL terminated string of type @c u8[].
338 *
339 * @par Example
340 * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[].
Chris Luke0aca5eb2016-04-25 13:48:54 -0400341 */
342#define CTL(c) (u8[]){ (c) - '@', 0 }
343
344#define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
Chris Lukeb5850972016-05-03 16:34:59 -0400345/**
346 * Patterns to match on a CLI input stream.
347 * @showinitializer
348 */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400349static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400350 /* Line handling */
351 _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */
352 _("\n", UNIX_CLI_PARSE_ACTION_CRLF),
353 _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */
354 _("\r", UNIX_CLI_PARSE_ACTION_CRLF),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400355
Dave Barach9b8ffd92016-07-08 08:13:45 -0400356 /* Unix shell control codes */
357 _(CTL ('B'), UNIX_CLI_PARSE_ACTION_LEFT),
358 _(CTL ('F'), UNIX_CLI_PARSE_ACTION_RIGHT),
359 _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP),
360 _(CTL ('N'), UNIX_CLI_PARSE_ACTION_DOWN),
361 _(CTL ('A'), UNIX_CLI_PARSE_ACTION_HOME),
362 _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END),
363 _(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT),
364 _(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT),
365 _(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT),
Hiroki Shirokura67e4df12019-08-16 11:30:34 +0000366 _(CTL ('W'), UNIX_CLI_PARSE_ACTION_ERASEWORDLEFT),
Dave Barach9b8ffd92016-07-08 08:13:45 -0400367 _(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK),
368 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR),
369 _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */
370 _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */
371 _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */
372 _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */
373 _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400374
Dave Barach9b8ffd92016-07-08 08:13:45 -0400375 /* VT100 Normal mode - Broadest support */
376 _(CSI "A", UNIX_CLI_PARSE_ACTION_UP),
377 _(CSI "B", UNIX_CLI_PARSE_ACTION_DOWN),
378 _(CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT),
379 _(CSI "D", UNIX_CLI_PARSE_ACTION_LEFT),
380 _(CSI "H", UNIX_CLI_PARSE_ACTION_HOME),
381 _(CSI "F", UNIX_CLI_PARSE_ACTION_END),
382 _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */
383 _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */
384 _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400385
386 /* VT100 Application mode - Some Gnome Terminal functions use these */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400387 _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP),
388 _(ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN),
389 _(ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT),
390 _(ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT),
391 _(ESC "OH", UNIX_CLI_PARSE_ACTION_HOME),
392 _(ESC "OF", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400393
Dave Barach9b8ffd92016-07-08 08:13:45 -0400394 /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
395 _(CSI "1~", UNIX_CLI_PARSE_ACTION_HOME),
396 _(CSI "4~", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400397
Dave Barach9b8ffd92016-07-08 08:13:45 -0400398 /* Emacs-ish history search */
399 _(CTL ('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH),
400 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400401
Dave Barach9b8ffd92016-07-08 08:13:45 -0400402 /* Other protocol things */
403 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
404 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
405 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400406};
Chris Lukeb5850972016-05-03 16:34:59 -0400407
408/**
409 * Patterns to match when a CLI session is in the pager.
410 * @showinitializer
411 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400412static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400413 /* Line handling */
414 _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */
415 _("\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
416 _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */
417 _("\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
Chris Luke7afda3a2016-04-25 13:49:22 -0400418
Dave Barach9b8ffd92016-07-08 08:13:45 -0400419 /* Pager commands */
420 _(" ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT),
421 _("q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT),
422 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
423 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
424 _("/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH),
Chris Luke7afda3a2016-04-25 13:49:22 -0400425
Dave Barach9b8ffd92016-07-08 08:13:45 -0400426 /* VT100 */
427 _(CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP),
428 _(CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN),
429 _(CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
430 _(CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400431
Dave Barach9b8ffd92016-07-08 08:13:45 -0400432 /* VT100 Application mode */
433 _(ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP),
434 _(ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN),
435 _(ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
436 _(ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400437
Dave Barach9b8ffd92016-07-08 08:13:45 -0400438 /* ANSI X3.41-1974 */
439 _(CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
440 _(CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
441 _(CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP),
442 _(CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN),
Chris Luke7afda3a2016-04-25 13:49:22 -0400443
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444 /* Other protocol things */
445 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
446 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
447 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke7afda3a2016-04-25 13:49:22 -0400448};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400449
Chris Luke0aca5eb2016-04-25 13:48:54 -0400450#undef _
451
Chris Luke6a3a4f72019-07-09 23:33:30 -0400452/** CLI session telnet negotiation timer events. */
453typedef enum
454{
455 UNIX_CLI_NEW_SESSION_EVENT_ADD, /**< Add a CLI session to the new session list */
456} unix_cli_timeout_event_type_t;
457
458/** Each new session is stored on a list with a deadline after which
459 * a prompt is issued, in case the session TELNET negotiation fails to
460 * complete. */
461typedef struct
462{
463 uword cf_index; /**< Session index of the new session. */
464 f64 deadline; /**< Deadline after which the new session must have a prompt. */
465} unix_cli_new_session_t;
466
Chris Lukeb5850972016-05-03 16:34:59 -0400467/** CLI global state. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400468typedef struct
469{
Chris Lukeb5850972016-05-03 16:34:59 -0400470 /** Prompt string for CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400471 u8 *cli_prompt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472
Chris Lukeb5850972016-05-03 16:34:59 -0400473 /** Vec pool of CLI sessions. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400474 unix_cli_file_t *cli_file_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700475
Chris Lukeb5850972016-05-03 16:34:59 -0400476 /** Vec pool of unused session indices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400477 u32 *unused_cli_process_node_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478
Chris Lukeb5850972016-05-03 16:34:59 -0400479 /** The session index of the stdin cli */
Chris Luke7afda3a2016-04-25 13:49:22 -0400480 u32 stdin_cli_file_index;
481
Chris Lukeb5850972016-05-03 16:34:59 -0400482 /** File pool index of current input. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483 u32 current_input_file_index;
Chris Luke6a3a4f72019-07-09 23:33:30 -0400484
485 /** New session process node identifier */
486 u32 new_session_process_node_index;
487
488 /** List of new sessions */
489 unix_cli_new_session_t *new_sessions;
Dave Barach961e3c82020-06-18 17:04:18 -0400490
Dave Barachb30b9542020-06-22 10:02:25 -0400491 /** system default macro table */
Dave Barach961e3c82020-06-18 17:04:18 -0400492 clib_macro_main_t macro_main;
493
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494} unix_cli_main_t;
495
Chris Lukeb5850972016-05-03 16:34:59 -0400496/** CLI global state */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497static unix_cli_main_t unix_cli_main;
498
Dave Barachb30b9542020-06-22 10:02:25 -0400499/** Return the macro main / tables we should use for this session
500 */
501static clib_macro_main_t *
502get_macro_main (void)
503{
504 unix_cli_main_t *cm = &unix_cli_main;
505 vlib_main_t *vm = vlib_get_main ();
506 vlib_process_t *cp = vlib_get_current_process (vm);
507 unix_cli_file_t *cf;
508
509 if (pool_is_free_index (cm->cli_file_pool, cp->output_function_arg))
510 return (&cm->macro_main);
511
512 cf = pool_elt_at_index (cm->cli_file_pool, cp->output_function_arg);
513
514 return (&cf->macro_main);
515}
516
Chris Luke0aca5eb2016-04-25 13:48:54 -0400517/**
Chris Luke8e5b0412016-07-26 13:06:10 -0400518 * @brief Search for a byte sequence in the action list.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400519 *
Chris Lukeb5850972016-05-03 16:34:59 -0400520 * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with
521 * the bytes in @a input of maximum length @a ilen bytes.
522 * When a match is made @a *matched indicates how many bytes were matched.
523 * Returns a value from the enum @ref unix_cli_parse_action_t to indicate
524 * whether no match was found, a partial match was found or a complete
525 * match was found and what action, if any, should be taken.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400526 *
Chris Lukeb5850972016-05-03 16:34:59 -0400527 * @param[in] a Actions list to search within.
528 * @param[in] input String fragment to search for.
529 * @param[in] ilen Length of the string in 'input'.
530 * @param[out] matched Pointer to an integer that will contain the number
531 * of bytes matched when a complete match is found.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400532 *
Chris Lukeb5850972016-05-03 16:34:59 -0400533 * @return Action from @ref unix_cli_parse_action_t that the string fragment
Chris Luke0aca5eb2016-04-25 13:48:54 -0400534 * matches.
Chris Lukeb5850972016-05-03 16:34:59 -0400535 * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the
536 * whole input string matches the start of at least one action.
537 * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
Chris Luke0aca5eb2016-04-25 13:48:54 -0400538 * match at all.
539 */
540static unix_cli_parse_action_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400541unix_cli_match_action (unix_cli_parse_actions_t * a,
542 u8 * input, u32 ilen, i32 * matched)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400543{
Chris Luke0aca5eb2016-04-25 13:48:54 -0400544 u8 partial = 0;
545
546 while (a->input)
547 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400548 if (ilen >= a->len)
549 {
550 /* see if the start of the input buffer exactly matches the current
551 * action string. */
552 if (memcmp (input, a->input, a->len) == 0)
553 {
554 *matched = a->len;
555 return a->action;
556 }
557 }
558 else
559 {
560 /* if the first ilen characters match, flag this as a partial -
561 * meaning keep collecting bytes in case of a future match */
562 if (memcmp (input, a->input, ilen) == 0)
563 partial = 1;
564 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400565
Dave Barach9b8ffd92016-07-08 08:13:45 -0400566 /* check next action */
567 a++;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400568 }
569
570 return partial ?
Dave Barach9b8ffd92016-07-08 08:13:45 -0400571 UNIX_CLI_PARSE_ACTION_PARTIALMATCH : UNIX_CLI_PARSE_ACTION_NOMATCH;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400572}
573
574
Chris Luke54ccf222016-07-25 16:38:11 -0400575/** Add bytes to the output vector and then flagg the I/O system that bytes
576 * are available to be sent.
577 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200579unix_cli_add_pending_output (clib_file_t * uf,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580 unix_cli_file_t * cf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400581 u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582{
Damjan Marion56dd5432017-09-08 19:52:02 +0200583 clib_file_main_t *fm = &file_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584
585 vec_add (cf->output_vector, buffer, buffer_bytes);
586 if (vec_len (cf->output_vector) > 0)
587 {
588 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
589 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400590 if (!skip_update)
Damjan Marion56dd5432017-09-08 19:52:02 +0200591 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592 }
593}
594
Chris Luke54ccf222016-07-25 16:38:11 -0400595/** Delete all bytes from the output vector and flag the I/O system
596 * that no more bytes are available to be sent.
597 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200599unix_cli_del_pending_output (clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400600 unix_cli_file_t * cf, uword n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601{
Damjan Marion56dd5432017-09-08 19:52:02 +0200602 clib_file_main_t *fm = &file_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
604 vec_delete (cf->output_vector, n_bytes, 0);
605 if (vec_len (cf->output_vector) <= 0)
606 {
607 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
608 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400609 if (!skip_update)
Damjan Marion56dd5432017-09-08 19:52:02 +0200610 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700611 }
612}
613
Chris Luke8e5b0412016-07-26 13:06:10 -0400614/** @brief A bit like strchr with a buffer length limit.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400615 * Search a buffer for the first instance of a character up to the limit of
616 * the buffer length. If found then return the position of that character.
617 *
618 * The key departure from strchr is that if the character is not found then
619 * return the buffer length.
620 *
621 * @param chr The byte value to search for.
622 * @param str The buffer in which to search for the value.
623 * @param len The depth into the buffer to search.
624 *
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700625 * @return The index of the first occurrence of \c chr. If \c chr is not
Chris Luke0aca5eb2016-04-25 13:48:54 -0400626 * found then \c len instead.
627 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400628always_inline word
629unix_vlib_findchr (u8 chr, u8 * str, word len)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400630{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400631 word i = 0;
632 for (i = 0; i < len; i++, str++)
633 {
634 if (*str == chr)
635 return i;
636 }
637 return len;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400638}
639
Chris Luke8e5b0412016-07-26 13:06:10 -0400640/** @brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400641 * Attempts to write given buffer to the file descriptor of the given
642 * Unix CLI session. If that session already has data in the output buffer
643 * or if the write attempt tells us to try again later then the given buffer
644 * is appended to the pending output buffer instead.
645 *
646 * This is typically called only from \c unix_vlib_cli_output_cooked since
647 * that is where CRLF handling occurs or from places where we explicitly do
648 * not want cooked handling.
649 *
650 * @param cf Unix CLI session of the desired stream to write to.
651 * @param uf The Unix file structure of the desired stream to write to.
652 * @param buffer Pointer to the buffer that needs to be written.
653 * @param buffer_bytes The number of bytes from \c buffer to write.
654 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400655static void
656unix_vlib_cli_output_raw (unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +0200657 clib_file_t * uf, u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400658{
659 int n = 0;
660
Chris Luke03add7f2017-09-20 23:31:24 -0400661 if (cf->has_epipe) /* don't try writing anything */
662 return;
663
Chris Luke0aca5eb2016-04-25 13:48:54 -0400664 if (vec_len (cf->output_vector) == 0)
Chris Luke03add7f2017-09-20 23:31:24 -0400665 {
666 if (cf->is_socket)
667 /* If it's a socket we use MSG_NOSIGNAL to prevent SIGPIPE */
668 n = send (uf->file_descriptor, buffer, buffer_bytes, MSG_NOSIGNAL);
669 else
670 n = write (uf->file_descriptor, buffer, buffer_bytes);
671 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400672
673 if (n < 0 && errno != EAGAIN)
674 {
Chris Luke03add7f2017-09-20 23:31:24 -0400675 if (errno == EPIPE)
676 {
677 /* connection closed on us */
678 unix_main_t *um = &unix_main;
679 cf->has_epipe = 1;
680 vlib_process_signal_event (um->vlib_main, cf->process_node_index,
681 UNIX_CLI_PROCESS_EVENT_QUIT,
682 uf->private_data);
683 }
684 else
685 {
686 clib_unix_warning ("write");
687 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400688 }
689 else if ((word) n < (word) buffer_bytes)
690 {
691 /* We got EAGAIN or we already have stuff in the buffer;
692 * queue up whatever didn't get sent for later. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400693 if (n < 0)
694 n = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400695 unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
696 }
697}
698
Chris Luke8e5b0412016-07-26 13:06:10 -0400699/** @brief Process a buffer for CRLF handling before outputting it to the CLI.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400700 *
701 * @param cf Unix CLI session of the desired stream to write to.
702 * @param uf The Unix file structure of the desired stream to write to.
703 * @param buffer Pointer to the buffer that needs to be written.
704 * @param buffer_bytes The number of bytes from \c buffer to write.
705 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400706static void
707unix_vlib_cli_output_cooked (unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +0200708 clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400709 u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400710{
711 word end = 0, start = 0;
712
713 while (end < buffer_bytes)
714 {
715 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400716 {
717 /* iterate the line on \n's so we can insert a \r before it */
718 end = unix_vlib_findchr ('\n',
719 buffer + start,
720 buffer_bytes - start) + start;
721 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400722 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400723 {
724 /* otherwise just send the whole buffer */
725 end = buffer_bytes;
726 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400727
Dave Barach9b8ffd92016-07-08 08:13:45 -0400728 unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start);
Chris Luke0aca5eb2016-04-25 13:48:54 -0400729
730 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400731 {
732 if (end < buffer_bytes)
733 {
734 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2);
735 end++; /* skip the \n that we already sent */
736 }
737 start = end;
738 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400739 }
Chris Lukefc379d22018-06-05 23:50:19 -0400740
741 /* Use the last character to determine the last direction of the cursor. */
742 if (buffer_bytes > 0)
743 cf->cursor_direction = (buffer[buffer_bytes - 1] == (u8) '\b');
744}
745
746/** @brief Moves the terminal cursor one character to the left, with
747 * special handling when it reaches the left edge of the terminal window.
748 *
749 * Ordinarily we can simply send a '\b' to move the cursor left, however
750 * most terminals will not reverse-wrap to the end of the previous line
751 * if the cursor is in the left-most column. To counter this we must
752 * check the cursor position + prompt length modulo terminal width and
753 * if available use some other means, such as ANSI terminal escape
754 * sequences, to move the cursor.
755 *
756 * @param cf Unix CLI session of the desired stream to write to.
757 * @param uf The Unix file structure of the desired stream to write to.
758 */
759static void
760unix_vlib_cli_output_cursor_left (unix_cli_file_t * cf, clib_file_t * uf)
761{
762 unix_cli_main_t *cm = &unix_cli_main;
763 static u8 *ansi = 0; /* assumes no reentry */
764 u32 position;
765
766 if (!cf->is_interactive || !cf->ansi_capable || !cf->width)
767 {
768 /* No special handling for dumb terminals */
769 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
770 return;
771 }
772
773 position = ((u32) vec_len (cm->cli_prompt) + cf->cursor) % cf->width;
774
775 if (position != 0)
776 {
777 /* No special handling required if we're not at the left edge */
778 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
779 return;
780 }
781
782 if (!cf->cursor_direction)
783 {
784 /* Special handling for when we are at the left edge but
785 * the cursor was going left-to-right, but in this situation
786 * xterm-like terminals actually hide the cursor off the right
787 * edge. A \b here seems to jump one char too many, so let's
788 * force the cursor onto the next line instead.
789 */
790 if (cf->cursor < vec_len (cf->current_command))
791 unix_vlib_cli_output_cooked (cf, uf, &cf->current_command[cf->cursor],
792 1);
793 else
794 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
795 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
796 }
797
798 /* Relocate the cursor at the right hand edge one line above */
799 ansi = format (ansi, CSI "A" CSI "%dC", cf->width - 1);
800 unix_vlib_cli_output_cooked (cf, uf, ansi, vec_len (ansi));
801 vec_reset_length (ansi); /* keep the vec around for next time */
802 cf->cursor_direction = 1; /* going backwards now */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400803}
804
Chris Luke8e5b0412016-07-26 13:06:10 -0400805/** @brief Output the CLI prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400806static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200807unix_cli_cli_prompt (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400808{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400809 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke7afda3a2016-04-25 13:49:22 -0400810
Chris Luke03add7f2017-09-20 23:31:24 -0400811 if (cf->is_interactive) /* Only interactive sessions get a prompt */
812 unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt,
813 vec_len (cm->cli_prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400814}
815
Chris Luke8e5b0412016-07-26 13:06:10 -0400816/** @brief Output a pager prompt and show number of buffered lines */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400817static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200818unix_cli_pager_prompt (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400819{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400820 u8 *prompt;
Chris Luke270b6de2016-05-19 14:23:25 -0400821 u32 h;
822
823 h = cf->pager_start + (cf->height - 1);
824 if (h > vec_len (cf->pager_index))
825 h = vec_len (cf->pager_index);
Chris Luke7afda3a2016-04-25 13:49:22 -0400826
Dave Barach9b8ffd92016-07-08 08:13:45 -0400827 prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s",
828 cf->ansi_capable ? ANSI_BOLD : "",
829 cf->pager_start + 1,
830 h,
831 vec_len (cf->pager_index),
832 cf->ansi_capable ? ANSI_RESET : "");
Chris Luke7afda3a2016-04-25 13:49:22 -0400833
Dave Barach9b8ffd92016-07-08 08:13:45 -0400834 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400835
Dave Barach9b8ffd92016-07-08 08:13:45 -0400836 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400837}
838
Chris Luke8e5b0412016-07-26 13:06:10 -0400839/** @brief Output a pager "skipping" message */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400840static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200841unix_cli_pager_message (unix_cli_file_t * cf, clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400842 char *message, char *postfix)
Chris Luke7afda3a2016-04-25 13:49:22 -0400843{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400844 u8 *prompt;
Chris Luke7afda3a2016-04-25 13:49:22 -0400845
Dave Barach9b8ffd92016-07-08 08:13:45 -0400846 prompt = format (0, "\r%s-- %s --%s%s",
847 cf->ansi_capable ? ANSI_BOLD : "",
848 message, cf->ansi_capable ? ANSI_RESET : "", postfix);
Chris Luke7afda3a2016-04-25 13:49:22 -0400849
Dave Barach9b8ffd92016-07-08 08:13:45 -0400850 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400851
Dave Barach9b8ffd92016-07-08 08:13:45 -0400852 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400853}
854
Chris Luke8e5b0412016-07-26 13:06:10 -0400855/** @brief Erase the printed pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400856static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200857unix_cli_pager_prompt_erase (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400858{
859 if (cf->ansi_capable)
860 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400861 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
862 unix_vlib_cli_output_cooked (cf, uf,
863 (u8 *) ANSI_CLEARLINE,
864 sizeof (ANSI_CLEARLINE) - 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400865 }
866 else
867 {
868 int i;
869
Dave Barach9b8ffd92016-07-08 08:13:45 -0400870 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
871 for (i = 0; i < cf->width - 1; i++)
872 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
873 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400874 }
875}
876
Chris Luke8e5b0412016-07-26 13:06:10 -0400877/** @brief Uses an ANSI escape sequence to move the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400878static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200879unix_cli_ansi_cursor (unix_cli_file_t * cf, clib_file_t * uf, u16 x, u16 y)
Chris Luke7afda3a2016-04-25 13:49:22 -0400880{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400881 u8 *str;
Chris Luke7afda3a2016-04-25 13:49:22 -0400882
Dave Barach9b8ffd92016-07-08 08:13:45 -0400883 str = format (0, "%s%d;%dH", CSI, y, x);
Chris Luke7afda3a2016-04-25 13:49:22 -0400884
Dave Barach9b8ffd92016-07-08 08:13:45 -0400885 unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str));
Chris Luke7afda3a2016-04-25 13:49:22 -0400886
Dave Barach9b8ffd92016-07-08 08:13:45 -0400887 vec_free (str);
Chris Luke7afda3a2016-04-25 13:49:22 -0400888}
889
Chris Luke862623d2016-05-14 10:13:34 -0400890/** Redraw the currently displayed page of text.
891 * @param cf CLI session to redraw the pager buffer of.
892 * @param uf Unix file of the CLI session.
893 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400894static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200895unix_cli_pager_redraw (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke862623d2016-05-14 10:13:34 -0400896{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400897 unix_cli_pager_index_t *pi = NULL;
898 u8 *line = NULL;
Chris Luke862623d2016-05-14 10:13:34 -0400899 word i;
900
901 /* No active pager? Do nothing. */
902 if (!vec_len (cf->pager_index))
903 return;
904
905 if (cf->ansi_capable)
906 {
907 /* If we have ANSI, send the clear screen sequence */
908 unix_vlib_cli_output_cooked (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400909 (u8 *) ANSI_CLEAR,
910 sizeof (ANSI_CLEAR) - 1);
Chris Luke862623d2016-05-14 10:13:34 -0400911 }
912 else
913 {
914 /* Otherwise make sure we're on a blank line */
915 unix_cli_pager_prompt_erase (cf, uf);
916 }
917
918 /* (Re-)send the current page of content */
919 for (i = 0; i < cf->height - 1 &&
Dave Barach9b8ffd92016-07-08 08:13:45 -0400920 i + cf->pager_start < vec_len (cf->pager_index); i++)
Chris Luke862623d2016-05-14 10:13:34 -0400921 {
922 pi = &cf->pager_index[cf->pager_start + i];
923 line = cf->pager_vector[pi->line] + pi->offset;
924
925 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
926 }
927 /* if the last line didn't end in newline, add a newline */
928 if (pi && line[pi->length - 1] != '\n')
Dave Barach9b8ffd92016-07-08 08:13:45 -0400929 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke862623d2016-05-14 10:13:34 -0400930
931 unix_cli_pager_prompt (cf, uf);
932}
933
934/** @brief Process and add a line to the pager index.
935 * In normal operation this function will take the given character string
936 * found in @c line and with length @c len_or_index and iterates the over the
937 * contents, adding each line of text discovered within it to the
938 * pager index. Lines are identified by newlines ("<code>\\n</code>") and by
939 * strings longer than the width of the terminal.
940 *
941 * If instead @c line is @c NULL then @c len_or_index is taken to mean the
942 * index of an existing line in the pager buffer; this simply means that the
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700943 * input line does not need to be cloned since we already have it. This is
Chris Luke862623d2016-05-14 10:13:34 -0400944 * typical if we are reindexing the pager buffer.
945 *
946 * @param cf The CLI session whose pager we are adding to.
947 * @param line The string of text to be indexed into the pager buffer.
948 * If @c line is @c NULL then the mode of operation
949 * changes slightly; see the description above.
950 * @param len_or_index If @c line is a pointer to a string then this parameter
951 * indicates the length of that string; Otherwise this
952 * value provides the index in the pager buffer of an
953 * existing string to be indexed.
954 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400955static void
956unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index)
Chris Luke862623d2016-05-14 10:13:34 -0400957{
Neale Ranns9c2c2432017-12-05 13:34:36 -0800958 u8 *p = NULL;
Chris Luke862623d2016-05-14 10:13:34 -0400959 word i, j, k;
960 word line_index, len;
961 u32 width = cf->width;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400962 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -0400963
964 if (line == NULL)
965 {
966 /* Use a line already in the pager buffer */
967 line_index = len_or_index;
Neale Ranns9c2c2432017-12-05 13:34:36 -0800968 if (cf->pager_vector != NULL)
969 p = cf->pager_vector[line_index];
Chris Luke862623d2016-05-14 10:13:34 -0400970 len = vec_len (p);
971 }
972 else
973 {
974 len = len_or_index;
975 /* Add a copy of the raw string to the pager buffer */
976 p = vec_new (u8, len);
977 clib_memcpy (p, line, len);
978
979 /* store in pager buffer */
980 line_index = vec_len (cf->pager_vector);
981 vec_add1 (cf->pager_vector, p);
982 }
983
984 i = 0;
985 while (i < len)
986 {
987 /* Find the next line, or run to terminal width, or run to EOL */
988 int l = len - i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400989 j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width);
Chris Luke862623d2016-05-14 10:13:34 -0400990
Dave Barach9b8ffd92016-07-08 08:13:45 -0400991 if (j < l && p[j] == '\n') /* incl \n */
992 j++;
Chris Luke862623d2016-05-14 10:13:34 -0400993
994 /* Add the line to the index */
995 k = vec_len (cf->pager_index);
996 vec_validate (cf->pager_index, k);
997 pi = &cf->pager_index[k];
998
999 pi->line = line_index;
1000 pi->offset = i;
1001 pi->length = j;
1002
1003 i += j;
1004 p += j;
1005 }
1006}
1007
1008/** @brief Reindex entire pager buffer.
1009 * Resets the current pager index and then re-adds the lines in the pager
1010 * buffer to the index.
1011 *
1012 * Additionally this function attempts to retain the current page start
1013 * line offset by searching for the same top-of-screen line in the new index.
1014 *
1015 * @param cf The CLI session whose pager buffer should be reindexed.
1016 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001017static void
1018unix_cli_pager_reindex (unix_cli_file_t * cf)
Chris Luke862623d2016-05-14 10:13:34 -04001019{
1020 word i, old_line, old_offset;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001021 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -04001022
1023 /* If there is nothing in the pager buffer then make sure the index
1024 * is empty and move on.
1025 */
1026 if (cf->pager_vector == 0)
1027 {
1028 vec_reset_length (cf->pager_index);
1029 return;
1030 }
1031
1032 /* Retain a pointer to the current page start line so we can
1033 * find it later
1034 */
1035 pi = &cf->pager_index[cf->pager_start];
1036 old_line = pi->line;
1037 old_offset = pi->offset;
1038
1039 /* Re-add the buffered lines to the index */
1040 vec_reset_length (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001041 vec_foreach_index (i, cf->pager_vector)
1042 {
1043 unix_cli_pager_add_line (cf, NULL, i);
1044 }
Chris Luke862623d2016-05-14 10:13:34 -04001045
1046 /* Attempt to re-locate the previously stored page start line */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001047 vec_foreach_index (i, cf->pager_index)
1048 {
1049 pi = &cf->pager_index[i];
Chris Luke862623d2016-05-14 10:13:34 -04001050
Dave Barach9b8ffd92016-07-08 08:13:45 -04001051 if (pi->line == old_line &&
1052 (pi->offset <= old_offset || pi->offset + pi->length > old_offset))
1053 {
1054 /* Found it! */
1055 cf->pager_start = i;
1056 break;
1057 }
1058 }
Chris Luke862623d2016-05-14 10:13:34 -04001059
1060 /* In case the start line was not found (rare), ensure the pager start
1061 * index is within bounds
1062 */
1063 if (cf->pager_start >= vec_len (cf->pager_index))
1064 {
Chris Luke270b6de2016-05-19 14:23:25 -04001065 if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001066 cf->pager_start = 0;
Chris Luke270b6de2016-05-19 14:23:25 -04001067 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001068 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
Chris Luke862623d2016-05-14 10:13:34 -04001069 }
1070}
1071
1072/** VLIB CLI output function.
1073 *
1074 * If the terminal has a pager configured then this function takes care
1075 * of collating output into the pager buffer; ensuring only the first page
1076 * is displayed and any lines in excess of the first page are buffered.
1077 *
1078 * If the maximum number of index lines in the buffer is exceeded then the
1079 * pager is cancelled and the contents of the current buffer are sent to the
1080 * terminal.
1081 *
1082 * If there is no pager configured then the output is sent directly to the
1083 * terminal.
1084 *
1085 * @param cli_file_index Index of the CLI session where this output is
1086 * directed.
1087 * @param buffer String of printabe bytes to be output.
1088 * @param buffer_bytes The number of bytes in @c buffer to be output.
1089 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001090static void
1091unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001093 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02001094 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001095 unix_cli_main_t *cm = &unix_cli_main;
1096 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +02001097 clib_file_t *uf;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001098
Ed Warnickecb9cada2015-12-08 15:45:58 -07001099 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02001100 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101
Chris Luke7afda3a2016-04-25 13:49:22 -04001102 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
1103 {
Chris Luke862623d2016-05-14 10:13:34 -04001104 unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes);
Chris Luke7afda3a2016-04-25 13:49:22 -04001105 }
1106 else
1107 {
Chris Luke862623d2016-05-14 10:13:34 -04001108 word row = vec_len (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001109 u8 *line;
1110 unix_cli_pager_index_t *pi;
Chris Luke7afda3a2016-04-25 13:49:22 -04001111
Chris Luke862623d2016-05-14 10:13:34 -04001112 /* Index and add the output lines to the pager buffer. */
1113 unix_cli_pager_add_line (cf, buffer, buffer_bytes);
1114
1115 /* Now iterate what was added to display the lines.
1116 * If we reach the bottom of the page, display a prompt.
1117 */
1118 while (row < vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001119 {
1120 if (row < cf->height - 1)
1121 {
1122 /* output this line */
1123 pi = &cf->pager_index[row];
1124 line = cf->pager_vector[pi->line] + pi->offset;
1125 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
Chris Luke862623d2016-05-14 10:13:34 -04001126
Dave Barach9b8ffd92016-07-08 08:13:45 -04001127 /* if the last line didn't end in newline, and we're at the
1128 * bottom of the page, add a newline */
1129 if (line[pi->length - 1] != '\n' && row == cf->height - 2)
1130 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1131 }
1132 else
1133 {
1134 /* Display the pager prompt every 10 lines */
1135 if (!(row % 10))
1136 unix_cli_pager_prompt (cf, uf);
1137 }
1138 row++;
1139 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001140
Chris Luke862623d2016-05-14 10:13:34 -04001141 /* Check if we went over the pager buffer limit */
1142 if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001143 {
1144 /* Stop using the pager for the remainder of this CLI command */
1145 cf->no_pager = 2;
Chris Luke7afda3a2016-04-25 13:49:22 -04001146
Dave Barach9b8ffd92016-07-08 08:13:45 -04001147 /* If we likely printed the prompt, erase it */
1148 if (vec_len (cf->pager_index) > cf->height - 1)
1149 unix_cli_pager_prompt_erase (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04001150
Dave Barach9b8ffd92016-07-08 08:13:45 -04001151 /* Dump out the contents of the buffer */
1152 for (row = cf->pager_start + (cf->height - 1);
1153 row < vec_len (cf->pager_index); row++)
1154 {
1155 pi = &cf->pager_index[row];
1156 line = cf->pager_vector[pi->line] + pi->offset;
1157 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1158 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001159
Dave Barach9b8ffd92016-07-08 08:13:45 -04001160 unix_cli_pager_reset (cf);
1161 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001162 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001163}
1164
Chris Luke862623d2016-05-14 10:13:34 -04001165/** Identify whether a terminal type is ANSI capable.
1166 *
Chris Luke54ccf222016-07-25 16:38:11 -04001167 * Compares the string given in @c term with a list of terminal types known
Chris Luke862623d2016-05-14 10:13:34 -04001168 * to support ANSI escape sequences.
1169 *
1170 * This list contains, for example, @c xterm, @c screen and @c ansi.
1171 *
1172 * @param term A string with a terminal type in it.
Chris Luke54ccf222016-07-25 16:38:11 -04001173 * @param len The length of the string in @c term.
Chris Luke862623d2016-05-14 10:13:34 -04001174 *
1175 * @return @c 1 if the terminal type is recognized as supporting ANSI
1176 * terminal sequences; @c 0 otherwise.
1177 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001178static u8
Chris Luke03add7f2017-09-20 23:31:24 -04001179unix_cli_terminal_type_ansi (u8 * term, uword len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001180{
Chris Luke0aca5eb2016-04-25 13:48:54 -04001181 /* This may later be better done as a hash of some sort. */
1182#define _(a) do { \
1183 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1184 } while(0)
1185
1186 _("xterm");
1187 _("xterm-color");
Dave Barach9b8ffd92016-07-08 08:13:45 -04001188 _("xterm-256color"); /* iTerm on Mac */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001189 _("screen");
Damjan Marion6e8ab162017-06-05 21:56:12 +02001190 _("screen-256color"); /* Screen and tmux */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001191 _("ansi"); /* Microsoft Telnet */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001192#undef _
1193
1194 return 0;
1195}
1196
Chris Luke03add7f2017-09-20 23:31:24 -04001197/** Identify whether a terminal type is non-interactive.
1198 *
1199 * Compares the string given in @c term with a list of terminal types known
1200 * to be non-interactive, as send by tools such as @c vppctl .
1201 *
1202 * This list contains, for example, @c vppctl.
1203 *
1204 * @param term A string with a terminal type in it.
1205 * @param len The length of the string in @c term.
1206 *
1207 * @return @c 1 if the terminal type is recognized as being non-interactive;
1208 * @c 0 otherwise.
1209 */
1210static u8
1211unix_cli_terminal_type_noninteractive (u8 * term, uword len)
1212{
1213 /* This may later be better done as a hash of some sort. */
1214#define _(a) do { \
1215 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1216 } while(0)
1217
1218 _("vppctl");
1219#undef _
1220
1221 return 0;
1222}
1223
1224/** Set a session to be non-interactive. */
1225static void
1226unix_cli_set_session_noninteractive (unix_cli_file_t * cf)
1227{
1228 /* Non-interactive sessions don't get these */
1229 cf->is_interactive = 0;
1230 cf->no_pager = 1;
1231 cf->history_limit = 0;
1232 cf->has_history = 0;
1233 cf->line_mode = 1;
1234}
1235
Chris Luke8e5b0412016-07-26 13:06:10 -04001236/** @brief Emit initial welcome banner and prompt on a connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001237static void
1238unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001239{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001240 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02001241 clib_file_main_t *fm = &file_main;
1242 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke572d8122016-04-25 13:49:07 -04001243 unix_cli_banner_t *banner;
1244 int i, len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001245
Chris Luke03add7f2017-09-20 23:31:24 -04001246 /* Mark the session as started if we get here */
1247 cf->started = 1;
1248
1249 if (!(cf->is_interactive)) /* No banner for non-interactive sessions */
1250 return;
1251
Chris Luke0aca5eb2016-04-25 13:48:54 -04001252 /*
1253 * Put the first bytes directly into the buffer so that further output is
1254 * queued until everything is ready. (oterwise initial prompt can appear
1255 * mid way through VPP initialization)
1256 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001257 unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
Chris Luke572d8122016-04-25 13:49:07 -04001258
Damjan Marion06d82262020-10-21 12:43:40 +02001259 if (!um->cli_no_banner && (um->flags & UNIX_FLAG_NOBANNER) == 0)
Chris Luke572d8122016-04-25 13:49:07 -04001260 {
Damjan Marion06d82262020-10-21 12:43:40 +02001261 if (cf->ansi_capable && (um->flags & UNIX_FLAG_NOCOLOR) == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001262 {
1263 banner = unix_cli_banner_color;
1264 len = ARRAY_LEN (unix_cli_banner_color);
1265 }
Chris Luke572d8122016-04-25 13:49:07 -04001266 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001267 {
1268 banner = unix_cli_banner;
1269 len = ARRAY_LEN (unix_cli_banner);
1270 }
Chris Luke572d8122016-04-25 13:49:07 -04001271
1272 for (i = 0; i < len; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001273 {
1274 unix_vlib_cli_output_cooked (cf, uf,
1275 banner[i].line, banner[i].length);
1276 }
1277 }
Chris Luke572d8122016-04-25 13:49:07 -04001278
1279 /* Prompt. */
Chris Luke7afda3a2016-04-25 13:49:22 -04001280 unix_cli_cli_prompt (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001281
Chris Luke0aca5eb2016-04-25 13:48:54 -04001282}
1283
Chris Luke6a3a4f72019-07-09 23:33:30 -04001284/**
1285 * @brief A failsafe manager that ensures CLI sessions issue an initial
1286 * prompt if TELNET negotiation fails.
1287 */
1288static uword
1289unix_cli_new_session_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1290 vlib_frame_t * f)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001291{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001292 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke6a3a4f72019-07-09 23:33:30 -04001293 uword event_type, *event_data = 0;
1294 f64 wait = 10.0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001295
Chris Luke6a3a4f72019-07-09 23:33:30 -04001296 while (1)
1297 {
1298 if (vec_len (cm->new_sessions) > 0)
1299 wait = vlib_process_wait_for_event_or_clock (vm, wait);
1300 else
1301 vlib_process_wait_for_event (vm);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001302
Chris Luke6a3a4f72019-07-09 23:33:30 -04001303 event_type = vlib_process_get_events (vm, &event_data);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001304
Chris Luke6a3a4f72019-07-09 23:33:30 -04001305 switch (event_type)
1306 {
1307 case ~0: /* no events => timeout */
1308 break;
1309
1310 case UNIX_CLI_NEW_SESSION_EVENT_ADD:
1311 {
1312 /* Add an identifier to the new session list */
1313 unix_cli_new_session_t ns;
1314
1315 ns.cf_index = event_data[0];
1316 ns.deadline = vlib_time_now (vm) + 1.0;
1317
1318 vec_add1 (cm->new_sessions, ns);
1319
1320 if (wait > 0.1)
1321 wait = 0.1; /* force a re-evaluation soon, but not too soon */
1322 }
1323 break;
1324
1325 default:
1326 clib_warning ("BUG: unknown event type 0x%wx", event_type);
1327 break;
1328 }
1329
1330 vec_reset_length (event_data);
1331
1332 if (vlib_process_suspend_time_is_zero (wait))
1333 {
1334 /* Scan the list looking for expired deadlines.
1335 * Emit needed prompts and remove from the list.
1336 * While scanning, look for the nearest new deadline
1337 * for the next iteration.
1338 * Since the list is ordered with newest sessions first
1339 * we can make assumptions about expired sessions being
1340 * contiguous at the beginning and the next deadline is the
1341 * next entry on the list, if any.
1342 */
1343 f64 now = vlib_time_now (vm);
1344 unix_cli_new_session_t *nsp;
1345 word index = 0;
1346
1347 wait = INFINITY;
1348
1349 vec_foreach (nsp, cm->new_sessions)
1350 {
1351 if (vlib_process_suspend_time_is_zero (nsp->deadline - now))
1352 {
1353 /* Deadline reached */
1354 unix_cli_file_t *cf;
1355
1356 /* Mark the highwater */
1357 index++;
1358
1359 /* Check the connection didn't close already */
1360 if (pool_is_free_index (cm->cli_file_pool, nsp->cf_index))
1361 continue;
1362
1363 cf = pool_elt_at_index (cm->cli_file_pool, nsp->cf_index);
1364
1365 if (!cf->started)
1366 unix_cli_file_welcome (cm, cf);
1367 }
1368 else
1369 {
1370 wait = nsp->deadline - now;
1371 break;
1372 }
1373 }
1374
1375 if (index)
1376 {
1377 /* We have sessions to remove */
1378 vec_delete (cm->new_sessions, index, 0);
1379 }
1380 }
1381 }
1382
1383 return 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001384}
1385
Chris Luke6a3a4f72019-07-09 23:33:30 -04001386
Chris Luke8e5b0412016-07-26 13:06:10 -04001387/** @brief A mostly no-op Telnet state machine.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001388 * Process Telnet command bytes in a way that ensures we're mostly
1389 * transparent to the Telnet protocol. That is, it's mostly a no-op.
1390 *
1391 * @return -1 if we need more bytes, otherwise a positive integer number of
1392 * bytes to consume from the input_vector, not including the initial
1393 * IAC byte.
1394 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001395static i32
1396unix_cli_process_telnet (unix_main_t * um,
1397 unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +02001398 clib_file_t * uf, u8 * input_vector, uword len)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001399{
1400 /* Input_vector starts at IAC byte.
1401 * See if we have a complete message; if not, return -1 so we wait for more.
1402 * if we have a complete message, consume those bytes from the vector.
1403 */
1404 i32 consume = 0;
1405
1406 if (len == 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001407 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001408
1409 switch (input_vector[1])
Ed Warnickecb9cada2015-12-08 15:45:58 -07001410 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001411 case IAC:
1412 /* two IAC's in a row means to pass through 0xff.
1413 * since that makes no sense here, just consume it.
1414 */
1415 consume = 1;
1416 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001417
Dave Barach9b8ffd92016-07-08 08:13:45 -04001418 case WILL:
1419 case WONT:
1420 case DO:
1421 case DONT:
1422 /* Expect 3 bytes */
Chris Lukea9cf6af2018-09-05 21:00:52 -04001423 if (len < 3)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001424 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001425
Dave Barach9b8ffd92016-07-08 08:13:45 -04001426 consume = 2;
1427 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001428
Dave Barach9b8ffd92016-07-08 08:13:45 -04001429 case SB:
1430 {
1431 /* Sub option - search ahead for IAC SE to end it */
1432 i32 i;
1433 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
1434 {
1435 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
1436 {
1437 /* We have a complete message; see if we care about it */
1438 switch (input_vector[2])
1439 {
1440 case TELOPT_TTYPE:
1441 if (input_vector[3] != 0)
1442 break;
Chris Luke03add7f2017-09-20 23:31:24 -04001443 {
1444 /* See if the the terminal type is recognized */
1445 u8 *term = input_vector + 4;
1446 uword len = i - 5;
1447
1448 /* See if the terminal type is ANSI capable */
1449 cf->ansi_capable =
1450 unix_cli_terminal_type_ansi (term, len);
1451
1452 /* See if the terminal type indicates non-interactive */
1453 if (unix_cli_terminal_type_noninteractive (term, len))
1454 unix_cli_set_session_noninteractive (cf);
1455 }
1456
Dave Barach9b8ffd92016-07-08 08:13:45 -04001457 /* If session not started, we can release the pause */
1458 if (!cf->started)
1459 /* Send the welcome banner and initial prompt */
1460 unix_cli_file_welcome (&unix_cli_main, cf);
1461 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001462
Dave Barach9b8ffd92016-07-08 08:13:45 -04001463 case TELOPT_NAWS:
1464 /* Window size */
1465 if (i != 8) /* check message is correct size */
1466 break;
Chris Lukeb2bcad62017-09-18 08:51:22 -04001467
Dave Barach9b8ffd92016-07-08 08:13:45 -04001468 cf->width =
1469 clib_net_to_host_u16 (*((u16 *) (input_vector + 3)));
Chris Lukeb2bcad62017-09-18 08:51:22 -04001470 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
1471 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
Chris Luke2d8bf302017-11-12 22:26:37 -05001472 if (cf->width == 0)
1473 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
Chris Lukeb2bcad62017-09-18 08:51:22 -04001474
Dave Barach9b8ffd92016-07-08 08:13:45 -04001475 cf->height =
1476 clib_net_to_host_u16 (*((u16 *) (input_vector + 5)));
Chris Lukeb2bcad62017-09-18 08:51:22 -04001477 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
1478 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
Chris Luke2d8bf302017-11-12 22:26:37 -05001479 if (cf->height == 0)
1480 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
Chris Lukeb2bcad62017-09-18 08:51:22 -04001481
Dave Barach9b8ffd92016-07-08 08:13:45 -04001482 /* reindex pager buffer */
1483 unix_cli_pager_reindex (cf);
1484 /* redraw page */
1485 unix_cli_pager_redraw (cf, uf);
1486 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04001487
Dave Barach9b8ffd92016-07-08 08:13:45 -04001488 default:
1489 break;
1490 }
1491 /* Consume it all */
1492 consume = i;
1493 break;
1494 }
1495 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001496
Dave Barach9b8ffd92016-07-08 08:13:45 -04001497 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
1498 consume = 1; /* hit max search depth, advance one byte */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001499
Dave Barach9b8ffd92016-07-08 08:13:45 -04001500 if (consume == 0)
1501 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001502
Dave Barach9b8ffd92016-07-08 08:13:45 -04001503 break;
1504 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001505
Dave Barach9b8ffd92016-07-08 08:13:45 -04001506 case GA:
1507 case EL:
1508 case EC:
1509 case AO:
1510 case IP:
1511 case BREAK:
1512 case DM:
1513 case NOP:
1514 case SE:
1515 case EOR:
1516 case ABORT:
1517 case SUSP:
1518 case xEOF:
1519 /* Simple one-byte messages */
1520 consume = 1;
1521 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001522
Dave Barach9b8ffd92016-07-08 08:13:45 -04001523 case AYT:
1524 /* Are You There - trigger a visible response */
1525 consume = 1;
1526 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
1527 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001528
Dave Barach9b8ffd92016-07-08 08:13:45 -04001529 default:
1530 /* Unknown command! Eat the IAC byte */
1531 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001532 }
1533
Dave Barach9b8ffd92016-07-08 08:13:45 -04001534 return consume;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001535}
1536
Chris Luke8e5b0412016-07-26 13:06:10 -04001537/** @brief Process actionable input.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001538 * Based on the \c action process the input; this typically involves
1539 * searching the command history or editing the current command line.
1540 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001541static int
1542unix_cli_line_process_one (unix_cli_main_t * cm,
1543 unix_main_t * um,
1544 unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +02001545 clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001546 u8 input, unix_cli_parse_action_t action)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001547{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001548 u8 *prev;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001549 u8 *save = 0;
1550 u8 **possible_commands;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001551 int j, delta;
1552
1553 switch (action)
1554 {
1555 case UNIX_CLI_PARSE_ACTION_NOACTION:
1556 break;
1557
Chris Luke0aca5eb2016-04-25 13:48:54 -04001558 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
1559 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
Chris Luke7afda3a2016-04-25 13:49:22 -04001560 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001561 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001562 if (cf->search_mode == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001563 {
1564 /* Erase the current command (if any) */
Chris Lukefc379d22018-06-05 23:50:19 -04001565 for (; cf->cursor > 0; cf->cursor--)
1566 {
1567 unix_vlib_cli_output_cursor_left (cf, uf);
1568 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1569 unix_vlib_cli_output_cursor_left (cf, uf);
1570 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001571
Dave Barach9b8ffd92016-07-08 08:13:45 -04001572 vec_reset_length (cf->search_key);
1573 vec_reset_length (cf->current_command);
Chris Lukefc379d22018-06-05 23:50:19 -04001574
Dave Barach9b8ffd92016-07-08 08:13:45 -04001575 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1576 cf->search_mode = -1;
1577 else
1578 cf->search_mode = 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001579 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001580 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001581 {
1582 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1583 cf->search_mode = -1;
1584 else
1585 cf->search_mode = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001586
Dave Barach9b8ffd92016-07-08 08:13:45 -04001587 cf->excursion += cf->search_mode;
1588 goto search_again;
1589 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001590 break;
1591
1592 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
1593 /* Erase the command from the cursor to the start */
1594
Chris Lukefc379d22018-06-05 23:50:19 -04001595 j = cf->cursor;
1596 /* Shimmy backwards to the new end of line position */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001597 delta = vec_len (cf->current_command) - cf->cursor;
Chris Lukefc379d22018-06-05 23:50:19 -04001598 for (; cf->cursor > delta; cf->cursor--)
1599 unix_vlib_cli_output_cursor_left (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001600 /* Zap from here to the end of what is currently displayed */
Chris Lukefc379d22018-06-05 23:50:19 -04001601 for (; cf->cursor < vec_len (cf->current_command); cf->cursor++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001602 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001603 /* Get back to the start of the line */
Chris Lukefc379d22018-06-05 23:50:19 -04001604 for (; cf->cursor > 0; cf->cursor--)
1605 unix_vlib_cli_output_cursor_left (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001606
Chris Lukefc379d22018-06-05 23:50:19 -04001607 /* Delete the desired text from the command */
1608 memmove (cf->current_command, cf->current_command + j, delta);
1609 _vec_len (cf->current_command) = delta;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001610
1611 /* Print the new contents */
Chris Lukefc379d22018-06-05 23:50:19 -04001612 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, delta);
1613 cf->cursor = delta; /* for backspace tracking */
1614
Chris Luke0aca5eb2016-04-25 13:48:54 -04001615 /* Shimmy back to the start */
Chris Lukefc379d22018-06-05 23:50:19 -04001616 for (; cf->cursor > 0; cf->cursor--)
1617 unix_vlib_cli_output_cursor_left (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001618
1619 cf->search_mode = 0;
1620 break;
1621
1622 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
1623 /* Erase the command from the cursor to the end */
1624
Chris Lukefc379d22018-06-05 23:50:19 -04001625 j = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001626 /* Zap from cursor to end of what is currently displayed */
Chris Lukefc379d22018-06-05 23:50:19 -04001627 for (; cf->cursor < (vec_len (cf->current_command)); cf->cursor++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001628 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001629 /* Get back to where we were */
Chris Lukefc379d22018-06-05 23:50:19 -04001630 for (; cf->cursor > j; cf->cursor--)
1631 unix_vlib_cli_output_cursor_left (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001632
1633 /* Truncate the line at the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001634 _vec_len (cf->current_command) = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001635
1636 cf->search_mode = 0;
1637 break;
1638
Hiroki Shirokura67e4df12019-08-16 11:30:34 +00001639 case UNIX_CLI_PARSE_ACTION_ERASEWORDLEFT:
1640 /* calculate num of caracter to be erased */
1641 delta = 0;
1642 while (cf->cursor > delta
1643 && cf->current_command[cf->cursor - delta - 1] == ' ')
1644 delta++;
1645 while (cf->cursor > delta
1646 && cf->current_command[cf->cursor - delta - 1] != ' ')
1647 delta++;
1648
1649 if (vec_len (cf->current_command))
1650 {
1651 if (cf->cursor > 0)
1652 {
1653 /* move cursor left delta times */
1654 for (j = delta; j > 0; j--, cf->cursor--)
1655 unix_vlib_cli_output_cursor_left (cf, uf);
1656 save = cf->current_command + cf->cursor;
1657
1658 /* redraw remainder of line */
1659 memmove (cf->current_command + cf->cursor,
1660 cf->current_command + cf->cursor + delta,
1661 _vec_len (cf->current_command) - cf->cursor - delta);
1662 unix_vlib_cli_output_cooked (cf, uf,
1663 cf->current_command + cf->cursor,
1664 _vec_len (cf->current_command) -
1665 cf->cursor);
1666 cf->cursor += _vec_len (cf->current_command) - cf->cursor;
1667
1668 /* print delta amount of blank spaces,
1669 * then finally fix the cursor position */
1670 for (j = delta; j > 0; j--, cf->cursor--)
1671 unix_vlib_cli_output_cursor_left (cf, uf);
1672 for (j = delta; j > 0; j--, cf->cursor++)
1673 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1674 for (; (cf->current_command + cf->cursor) > save; cf->cursor--)
1675 unix_vlib_cli_output_cursor_left (cf, uf);
1676 _vec_len (cf->current_command) -= delta;
1677 }
1678 }
1679 cf->search_mode = 0;
1680 cf->excursion = 0;
1681 vec_reset_length (cf->search_key);
1682 break;
1683
Chris Luke0aca5eb2016-04-25 13:48:54 -04001684 case UNIX_CLI_PARSE_ACTION_LEFT:
1685 if (cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001686 {
Chris Lukefc379d22018-06-05 23:50:19 -04001687 unix_vlib_cli_output_cursor_left (cf, uf);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001688 cf->cursor--;
1689 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001690
1691 cf->search_mode = 0;
1692 break;
1693
1694 case UNIX_CLI_PARSE_ACTION_RIGHT:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001695 if (cf->cursor < vec_len (cf->current_command))
1696 {
1697 /* have to emit the character under the cursor */
1698 unix_vlib_cli_output_cooked (cf, uf,
1699 cf->current_command + cf->cursor, 1);
1700 cf->cursor++;
1701 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001702
1703 cf->search_mode = 0;
1704 break;
1705
1706 case UNIX_CLI_PARSE_ACTION_UP:
1707 case UNIX_CLI_PARSE_ACTION_DOWN:
Chris Luke7afda3a2016-04-25 13:49:22 -04001708 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001709 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001710 cf->search_mode = 0;
1711 /* Erase the command */
Chris Lukefc379d22018-06-05 23:50:19 -04001712 for (; cf->cursor < vec_len (cf->current_command); cf->cursor++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001713 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Lukefc379d22018-06-05 23:50:19 -04001714 for (; cf->cursor > 0; cf->cursor--)
1715 {
1716 unix_vlib_cli_output_cursor_left (cf, uf);
1717 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1718 unix_vlib_cli_output_cursor_left (cf, uf);
1719 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001720 vec_reset_length (cf->current_command);
1721 if (vec_len (cf->command_history))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001722 {
1723 if (action == UNIX_CLI_PARSE_ACTION_UP)
1724 delta = -1;
1725 else
1726 delta = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001727
Dave Barach9b8ffd92016-07-08 08:13:45 -04001728 cf->excursion += delta;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001729
Dave Barach9b8ffd92016-07-08 08:13:45 -04001730 if (cf->excursion == vec_len (cf->command_history))
1731 {
1732 /* down-arrowed to last entry - want a blank line */
1733 _vec_len (cf->current_command) = 0;
1734 }
1735 else if (cf->excursion < 0)
1736 {
1737 /* up-arrowed over the start to the end, want a blank line */
1738 cf->excursion = vec_len (cf->command_history);
1739 _vec_len (cf->current_command) = 0;
1740 }
1741 else
1742 {
1743 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1744 /* down-arrowed past end - wrap to start */
1745 cf->excursion = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001746
Dave Barach9b8ffd92016-07-08 08:13:45 -04001747 /* Print the command at the current position */
1748 prev = cf->command_history[cf->excursion];
1749 vec_validate (cf->current_command, vec_len (prev) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001750
Dave Barach9b8ffd92016-07-08 08:13:45 -04001751 clib_memcpy (cf->current_command, prev, vec_len (prev));
1752 _vec_len (cf->current_command) = vec_len (prev);
1753 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1754 vec_len (cf->current_command));
1755 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001756 }
Yoann Desmouceaux561ae0a2017-09-20 10:08:28 +02001757 cf->cursor = vec_len (cf->current_command);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001758 break;
1759
1760 case UNIX_CLI_PARSE_ACTION_HOME:
1761 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001762 {
Chris Lukefc379d22018-06-05 23:50:19 -04001763 for (; cf->cursor > 0; cf->cursor--)
1764 unix_vlib_cli_output_cursor_left (cf, uf);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001765 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001766
Chris Luke0aca5eb2016-04-25 13:48:54 -04001767 cf->search_mode = 0;
1768 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001769
Chris Luke0aca5eb2016-04-25 13:48:54 -04001770 case UNIX_CLI_PARSE_ACTION_END:
1771 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001772 cf->cursor < vec_len (cf->current_command))
1773 {
1774 unix_vlib_cli_output_cooked (cf, uf,
1775 cf->current_command + cf->cursor,
1776 vec_len (cf->current_command) -
1777 cf->cursor);
1778 cf->cursor = vec_len (cf->current_command);
1779 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001780
1781 cf->search_mode = 0;
1782 break;
1783
1784 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1785 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001786 {
Chris Lukefc379d22018-06-05 23:50:19 -04001787 unix_vlib_cli_output_cursor_left (cf, uf);
1788 cf->cursor--;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001789
Chris Lukefc379d22018-06-05 23:50:19 -04001790 while (cf->cursor && isspace (cf->current_command[cf->cursor]))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001791 {
Chris Lukefc379d22018-06-05 23:50:19 -04001792 unix_vlib_cli_output_cursor_left (cf, uf);
1793 cf->cursor--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001794 }
Chris Lukefc379d22018-06-05 23:50:19 -04001795 while (cf->cursor && !isspace (cf->current_command[cf->cursor]))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001796 {
Chris Lukefc379d22018-06-05 23:50:19 -04001797 if (isspace (cf->current_command[cf->cursor - 1]))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001798 break;
Chris Lukefc379d22018-06-05 23:50:19 -04001799 unix_vlib_cli_output_cursor_left (cf, uf);
1800 cf->cursor--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001801 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001802
Dave Barach9b8ffd92016-07-08 08:13:45 -04001803 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001804
1805 cf->search_mode = 0;
1806 break;
1807
1808 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1809 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001810 cf->cursor < vec_len (cf->current_command))
1811 {
1812 int e = vec_len (cf->current_command);
1813 j = cf->cursor;
1814 while (j < e && !isspace (cf->current_command[j]))
1815 j++;
1816 while (j < e && isspace (cf->current_command[j]))
1817 j++;
1818 unix_vlib_cli_output_cooked (cf, uf,
1819 cf->current_command + cf->cursor,
1820 j - cf->cursor);
1821 cf->cursor = j;
1822 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001823
1824 cf->search_mode = 0;
1825 break;
1826
1827
1828 case UNIX_CLI_PARSE_ACTION_ERASE:
1829 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001830 {
1831 if (cf->cursor == vec_len (cf->current_command))
1832 {
Chris Lukefc379d22018-06-05 23:50:19 -04001833 unix_vlib_cli_output_cursor_left (cf, uf);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001834 cf->cursor--;
Chris Lukefc379d22018-06-05 23:50:19 -04001835 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1836 cf->cursor++;
1837 unix_vlib_cli_output_cursor_left (cf, uf);
1838 cf->cursor--;
1839 _vec_len (cf->current_command)--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001840 }
1841 else if (cf->cursor > 0)
1842 {
1843 /* shift everything at & to the right of the cursor left by 1 */
1844 j = vec_len (cf->current_command) - cf->cursor;
1845 memmove (cf->current_command + cf->cursor - 1,
1846 cf->current_command + cf->cursor, j);
1847 _vec_len (cf->current_command)--;
Chris Lukefc379d22018-06-05 23:50:19 -04001848
Dave Barach9b8ffd92016-07-08 08:13:45 -04001849 /* redraw the rest of the line */
Chris Lukefc379d22018-06-05 23:50:19 -04001850 unix_vlib_cli_output_cursor_left (cf, uf);
1851 cf->cursor--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001852 unix_vlib_cli_output_cooked (cf, uf,
1853 cf->current_command + cf->cursor,
1854 j);
Chris Lukefc379d22018-06-05 23:50:19 -04001855 cf->cursor += j;
1856 /* erase last char */
1857 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1858 cf->cursor++;
1859
Dave Barach9b8ffd92016-07-08 08:13:45 -04001860 /* and shift the terminal cursor back where it should be */
Chris Lukefc379d22018-06-05 23:50:19 -04001861 j += 2; /* account for old string length and offset position */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001862 while (--j)
Chris Lukefc379d22018-06-05 23:50:19 -04001863 {
1864 unix_vlib_cli_output_cursor_left (cf, uf);
1865 cf->cursor--;
1866 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001867 }
1868 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001869 cf->search_mode = 0;
1870 cf->excursion = 0;
1871 vec_reset_length (cf->search_key);
1872 break;
1873
1874 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1875 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001876 {
1877 if (cf->cursor < vec_len (cf->current_command))
1878 {
1879 /* shift everything to the right of the cursor left by 1 */
1880 j = vec_len (cf->current_command) - cf->cursor - 1;
1881 memmove (cf->current_command + cf->cursor,
1882 cf->current_command + cf->cursor + 1, j);
1883 _vec_len (cf->current_command)--;
1884 /* redraw the rest of the line */
1885 unix_vlib_cli_output_cooked (cf, uf,
1886 cf->current_command + cf->cursor,
1887 j);
Chris Lukefc379d22018-06-05 23:50:19 -04001888 cf->cursor += j;
1889 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1890 cf->cursor++;
1891 unix_vlib_cli_output_cursor_left (cf, uf);
1892 cf->cursor--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001893 /* and shift the terminal cursor back where it should be */
1894 if (j)
1895 {
Chris Lukefc379d22018-06-05 23:50:19 -04001896 unix_vlib_cli_output_cursor_left (cf, uf);
1897 cf->cursor--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001898 while (--j)
Chris Lukefc379d22018-06-05 23:50:19 -04001899 {
1900 unix_vlib_cli_output_cursor_left (cf, uf);
1901 cf->cursor--;
1902 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001903 }
1904 }
1905 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001906 else if (input == 'D' - '@')
Dave Barach9b8ffd92016-07-08 08:13:45 -04001907 {
1908 /* ^D with no command entered = quit */
1909 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1910 vlib_process_signal_event (um->vlib_main,
1911 vlib_current_process (um->vlib_main),
1912 UNIX_CLI_PROCESS_EVENT_QUIT,
1913 cf - cm->cli_file_pool);
1914 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001915 cf->search_mode = 0;
1916 cf->excursion = 0;
1917 vec_reset_length (cf->search_key);
1918 break;
1919
1920 case UNIX_CLI_PARSE_ACTION_CLEAR:
1921 /* If we're in ANSI mode, clear the screen.
1922 * Then redraw the prompt and any existing command input, then put
1923 * the cursor back where it was in that line.
1924 */
1925 if (cf->ansi_capable)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001926 unix_vlib_cli_output_cooked (cf, uf,
1927 (u8 *) ANSI_CLEAR,
1928 sizeof (ANSI_CLEAR) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001929 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001930 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001931
1932 unix_vlib_cli_output_raw (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001933 cm->cli_prompt, vec_len (cm->cli_prompt));
Chris Lukefc379d22018-06-05 23:50:19 -04001934 unix_vlib_cli_output_cooked (cf, uf,
1935 cf->current_command,
1936 vec_len (cf->current_command));
1937 j = cf->cursor;
1938 cf->cursor = vec_len (cf->current_command);
1939 for (; cf->cursor > j; cf->cursor--)
1940 unix_vlib_cli_output_cursor_left (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001941
1942 break;
1943
Chris Luke7afda3a2016-04-25 13:49:22 -04001944 case UNIX_CLI_PARSE_ACTION_TAB:
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001945 if (cf->cursor < vec_len (cf->current_command))
1946 {
1947 /* if we are in the middle of a line, complete only if
1948 * the cursor points to whitespace */
1949 if (isspace (cf->current_command[cf->cursor]))
1950 {
1951 /* save and clear any input that is after the cursor */
1952 vec_resize (save, vec_len (cf->current_command) - cf->cursor);
1953 clib_memcpy (save, cf->current_command + cf->cursor,
1954 vec_len (cf->current_command) - cf->cursor);
1955 _vec_len (cf->current_command) = cf->cursor;
1956 }
1957 else
1958 {
1959 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1960 break;
1961 }
1962 }
1963 possible_commands =
1964 vlib_cli_get_possible_completions (cf->current_command);
1965 if (vec_len (possible_commands) == 1)
1966 {
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001967 u8 *completed = possible_commands[0];
Chris Lukefc379d22018-06-05 23:50:19 -04001968 j = cf->cursor;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001969
1970 /* find the last word of current_command */
1971 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1972 {
Chris Lukefc379d22018-06-05 23:50:19 -04001973 unix_vlib_cli_output_cursor_left (cf, uf);
1974 cf->cursor--;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001975 j--;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001976 }
1977 _vec_len (cf->current_command) = j;
1978
1979 /* replace it with the newly expanded command */
1980 vec_append (cf->current_command, completed);
1981
1982 /* echo to the terminal */
Chris Lukefc379d22018-06-05 23:50:19 -04001983 unix_vlib_cli_output_cooked (cf, uf, completed,
1984 vec_len (completed));
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001985
1986 /* add one trailing space if needed */
1987 if (vec_len (save) == 0)
1988 {
1989 vec_add1 (cf->current_command, ' ');
Chris Lukefc379d22018-06-05 23:50:19 -04001990 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001991 }
1992
1993 cf->cursor = vec_len (cf->current_command);
1994
1995 }
1996 else if (vec_len (possible_commands) >= 2)
1997 {
1998 u8 **possible_command;
1999 uword max_command_len = 0, min_command_len = ~0;
Chris Lukefc379d22018-06-05 23:50:19 -04002000 u32 i;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002001
2002 vec_foreach (possible_command, possible_commands)
2003 {
2004 if (vec_len (*possible_command) > max_command_len)
Chris Lukefc379d22018-06-05 23:50:19 -04002005 max_command_len = vec_len (*possible_command);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002006 if (vec_len (*possible_command) < min_command_len)
Chris Lukefc379d22018-06-05 23:50:19 -04002007 min_command_len = vec_len (*possible_command);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002008 }
2009
2010 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2011
2012 i = 0;
2013 vec_foreach (possible_command, possible_commands)
2014 {
2015 if (i + max_command_len >= cf->width)
2016 {
2017 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2018 i = 0;
2019 }
Chris Lukefc379d22018-06-05 23:50:19 -04002020 unix_vlib_cli_output_cooked (cf, uf, *possible_command,
2021 vec_len (*possible_command));
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002022 for (j = vec_len (*possible_command); j < max_command_len + 2;
2023 j++)
2024 {
Chris Lukefc379d22018-06-05 23:50:19 -04002025 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002026 }
2027 i += max_command_len + 2;
2028 }
2029
2030 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2031
2032 /* rewrite prompt */
2033 unix_cli_cli_prompt (cf, uf);
Chris Lukefc379d22018-06-05 23:50:19 -04002034 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
2035 vec_len (cf->current_command));
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002036
2037 /* count length of last word */
2038 j = cf->cursor;
2039 i = 0;
2040 while (j >= 1 && !isspace (cf->current_command[j - 1]))
2041 {
2042 j--;
2043 i++;
2044 }
2045
2046 /* determine smallest common command */
2047 for (; i < min_command_len; i++)
2048 {
2049 u8 common = '\0';
2050 int stop = 0;
Chris Lukefc379d22018-06-05 23:50:19 -04002051
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002052 vec_foreach (possible_command, possible_commands)
2053 {
2054 if (common == '\0')
2055 {
2056 common = (*possible_command)[i];
2057 }
2058 else if (common != (*possible_command)[i])
2059 {
2060 stop = 1;
2061 break;
2062 }
2063 }
Chris Lukefc379d22018-06-05 23:50:19 -04002064
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002065 if (!stop)
2066 {
2067 vec_add1 (cf->current_command, common);
2068 cf->cursor++;
Chris Lukefc379d22018-06-05 23:50:19 -04002069 unix_vlib_cli_output_cooked (cf, uf, (u8 *) & common, 1);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002070 }
2071 else
2072 {
2073 break;
2074 }
2075 }
2076 }
2077 else
2078 {
2079 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
2080 }
2081
2082 if (vec_len (save) > 0)
2083 {
2084 /* restore remaining input if tab was hit in the middle of a line */
Chris Lukefc379d22018-06-05 23:50:19 -04002085 unix_vlib_cli_output_cooked (cf, uf, save, vec_len (save));
2086 cf->cursor += vec_len (save);
2087 for (j = 0; j < vec_len (save); j++, cf->cursor--)
2088 unix_vlib_cli_output_cursor_left (cf, uf);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002089 vec_append (cf->current_command, save);
2090 vec_free (save);
2091 }
2092 vec_free (possible_commands);
2093
2094 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04002095 case UNIX_CLI_PARSE_ACTION_YANK:
2096 /* TODO */
2097 break;
2098
2099
2100 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
2101 pager_quit:
2102 unix_cli_pager_prompt_erase (cf, uf);
2103 unix_cli_pager_reset (cf);
2104 unix_cli_cli_prompt (cf, uf);
2105 break;
2106
2107 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
2108 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
2109 /* show next page of the buffer */
Chris Luke70a745d2018-06-10 10:47:50 -04002110 if (cf->height + cf->pager_start <= vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002111 {
2112 u8 *line = NULL;
2113 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04002114
Dave Barach9b8ffd92016-07-08 08:13:45 -04002115 int m = cf->pager_start + (cf->height - 1);
2116 unix_cli_pager_prompt_erase (cf, uf);
2117 for (j = m;
2118 j < vec_len (cf->pager_index) && cf->pager_start < m;
2119 j++, cf->pager_start++)
2120 {
2121 pi = &cf->pager_index[j];
2122 line = cf->pager_vector[pi->line] + pi->offset;
2123 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2124 }
2125 /* if the last line didn't end in newline, add a newline */
2126 if (pi && line[pi->length - 1] != '\n')
2127 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2128 unix_cli_pager_prompt (cf, uf);
2129 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002130 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002131 {
2132 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
2133 /* no more in buffer, exit, but only if it was <space> */
2134 goto pager_quit;
2135 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002136 break;
2137
2138 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
2139 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
2140 /* display the next line of the buffer */
Chris Luke70a745d2018-06-10 10:47:50 -04002141 if (cf->height + cf->pager_start <= vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002142 {
2143 u8 *line;
2144 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -04002145
Dave Barach9b8ffd92016-07-08 08:13:45 -04002146 unix_cli_pager_prompt_erase (cf, uf);
2147 pi = &cf->pager_index[cf->pager_start + (cf->height - 1)];
2148 line = cf->pager_vector[pi->line] + pi->offset;
2149 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2150 cf->pager_start++;
2151 /* if the last line didn't end in newline, add a newline */
2152 if (line[pi->length - 1] != '\n')
2153 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2154 unix_cli_pager_prompt (cf, uf);
2155 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002156 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002157 {
2158 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
2159 /* no more in buffer, exit, but only if it was <enter> */
2160 goto pager_quit;
2161 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002162
2163 break;
2164
2165 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
2166 /* scroll the page back one line */
2167 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002168 {
2169 u8 *line = NULL;
2170 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04002171
Dave Barach9b8ffd92016-07-08 08:13:45 -04002172 cf->pager_start--;
2173 if (cf->ansi_capable)
2174 {
2175 pi = &cf->pager_index[cf->pager_start];
2176 line = cf->pager_vector[pi->line] + pi->offset;
2177 unix_cli_pager_prompt_erase (cf, uf);
2178 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN,
2179 sizeof (ANSI_SCROLLDN) - 1);
2180 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR,
2181 sizeof (ANSI_SAVECURSOR) - 1);
2182 unix_cli_ansi_cursor (cf, uf, 1, 1);
2183 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE,
2184 sizeof (ANSI_CLEARLINE) - 1);
2185 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2186 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR,
2187 sizeof (ANSI_RESTCURSOR) - 1);
2188 unix_cli_pager_prompt_erase (cf, uf);
2189 unix_cli_pager_prompt (cf, uf);
2190 }
2191 else
2192 {
2193 int m = cf->pager_start + (cf->height - 1);
2194 unix_cli_pager_prompt_erase (cf, uf);
2195 for (j = cf->pager_start;
2196 j < vec_len (cf->pager_index) && j < m; j++)
2197 {
2198 pi = &cf->pager_index[j];
2199 line = cf->pager_vector[pi->line] + pi->offset;
2200 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2201 }
2202 /* if the last line didn't end in newline, add a newline */
2203 if (pi && line[pi->length - 1] != '\n')
2204 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2205 unix_cli_pager_prompt (cf, uf);
2206 }
2207 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002208 break;
2209
2210 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
2211 /* back to the first page of the buffer */
2212 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002213 {
2214 u8 *line = NULL;
2215 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04002216
Dave Barach9b8ffd92016-07-08 08:13:45 -04002217 cf->pager_start = 0;
2218 int m = cf->pager_start + (cf->height - 1);
2219 unix_cli_pager_prompt_erase (cf, uf);
2220 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
2221 j++)
2222 {
2223 pi = &cf->pager_index[j];
2224 line = cf->pager_vector[pi->line] + pi->offset;
2225 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2226 }
2227 /* if the last line didn't end in newline, add a newline */
2228 if (pi && line[pi->length - 1] != '\n')
2229 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2230 unix_cli_pager_prompt (cf, uf);
2231 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002232 break;
2233
2234 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
2235 /* skip to the last page of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04002236 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002237 {
2238 u8 *line = NULL;
2239 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04002240
Dave Barach9b8ffd92016-07-08 08:13:45 -04002241 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
2242 unix_cli_pager_prompt_erase (cf, uf);
2243 unix_cli_pager_message (cf, uf, "skipping", "\n");
2244 for (j = cf->pager_start; j < vec_len (cf->pager_index); j++)
2245 {
2246 pi = &cf->pager_index[j];
2247 line = cf->pager_vector[pi->line] + pi->offset;
2248 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2249 }
2250 /* if the last line didn't end in newline, add a newline */
2251 if (pi && line[pi->length - 1] != '\n')
2252 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2253 unix_cli_pager_prompt (cf, uf);
2254 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002255 break;
2256
2257 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
2258 /* wander back one page in the buffer */
2259 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002260 {
2261 u8 *line = NULL;
2262 unix_cli_pager_index_t *pi = NULL;
2263 int m;
Chris Luke862623d2016-05-14 10:13:34 -04002264
Dave Barach9b8ffd92016-07-08 08:13:45 -04002265 if (cf->pager_start >= cf->height)
2266 cf->pager_start -= cf->height - 1;
2267 else
2268 cf->pager_start = 0;
2269 m = cf->pager_start + cf->height - 1;
2270 unix_cli_pager_prompt_erase (cf, uf);
2271 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
2272 j++)
2273 {
2274 pi = &cf->pager_index[j];
2275 line = cf->pager_vector[pi->line] + pi->offset;
2276 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2277 }
2278 /* if the last line didn't end in newline, add a newline */
2279 if (pi && line[pi->length - 1] != '\n')
2280 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2281 unix_cli_pager_prompt (cf, uf);
2282 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002283 break;
2284
Chris Luke862623d2016-05-14 10:13:34 -04002285 case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW:
2286 /* Redraw the current pager screen */
2287 unix_cli_pager_redraw (cf, uf);
2288 break;
2289
Chris Luke7afda3a2016-04-25 13:49:22 -04002290 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
2291 /* search forwards in the buffer */
2292 break;
2293
2294
Chris Luke0aca5eb2016-04-25 13:48:54 -04002295 case UNIX_CLI_PARSE_ACTION_CRLF:
2296 crlf:
Chris Luke0aca5eb2016-04-25 13:48:54 -04002297 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2298
Chris Luke7afda3a2016-04-25 13:49:22 -04002299 if (cf->has_history && cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002300 {
2301 if (cf->command_history
2302 && vec_len (cf->command_history) >= cf->history_limit)
2303 {
2304 vec_free (cf->command_history[0]);
2305 vec_delete (cf->command_history, 1, 0);
2306 }
2307 /* Don't add blank lines to the cmd history */
2308 if (vec_len (cf->current_command))
2309 {
2310 /* Don't duplicate the previous command */
2311 j = vec_len (cf->command_history);
2312 if (j == 0 ||
2313 (vec_len (cf->current_command) !=
2314 vec_len (cf->command_history[j - 1])
2315 || memcmp (cf->current_command, cf->command_history[j - 1],
2316 vec_len (cf->current_command)) != 0))
2317 {
2318 /* copy the command to the history */
2319 u8 *c = 0;
2320 vec_append (c, cf->current_command);
2321 vec_add1 (cf->command_history, c);
2322 cf->command_number++;
2323 }
2324 }
2325 cf->excursion = vec_len (cf->command_history);
2326 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002327
Chris Luke0aca5eb2016-04-25 13:48:54 -04002328 cf->search_mode = 0;
2329 vec_reset_length (cf->search_key);
2330 cf->cursor = 0;
2331
2332 return 0;
2333
Chris Luke7afda3a2016-04-25 13:49:22 -04002334 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2335 case UNIX_CLI_PARSE_ACTION_NOMATCH:
Chris Luke862623d2016-05-14 10:13:34 -04002336 if (vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002337 {
2338 /* no-op for now */
2339 }
Chris Lukefc379d22018-06-05 23:50:19 -04002340 else if (cf->has_history && cf->search_mode != 0 && isprint (input))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002341 {
2342 int k, limit, offset;
2343 u8 *item;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002344
Dave Barach9b8ffd92016-07-08 08:13:45 -04002345 vec_add1 (cf->search_key, input);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002346
Dave Barach9b8ffd92016-07-08 08:13:45 -04002347 search_again:
2348 for (j = 0; j < vec_len (cf->command_history); j++)
2349 {
2350 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
2351 cf->excursion = 0;
2352 else if (cf->excursion < 0)
2353 cf->excursion = vec_len (cf->command_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002354
Dave Barach9b8ffd92016-07-08 08:13:45 -04002355 item = cf->command_history[cf->excursion];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002356
Dave Barach9b8ffd92016-07-08 08:13:45 -04002357 limit = (vec_len (cf->search_key) > vec_len (item)) ?
2358 vec_len (item) : vec_len (cf->search_key);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002359
Dave Barach9b8ffd92016-07-08 08:13:45 -04002360 for (offset = 0; offset <= vec_len (item) - limit; offset++)
2361 {
2362 for (k = 0; k < limit; k++)
2363 {
2364 if (item[k + offset] != cf->search_key[k])
2365 goto next_offset;
2366 }
2367 goto found_at_offset;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002368
Dave Barach9b8ffd92016-07-08 08:13:45 -04002369 next_offset:
2370 ;
2371 }
2372 goto next;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002373
Dave Barach9b8ffd92016-07-08 08:13:45 -04002374 found_at_offset:
Chris Lukefc379d22018-06-05 23:50:19 -04002375 for (; cf->cursor > 0; cf->cursor--)
2376 {
2377 unix_vlib_cli_output_cursor_left (cf, uf);
2378 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
2379 unix_vlib_cli_output_cursor_left (cf, uf);
2380 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002381
Dave Barach9b8ffd92016-07-08 08:13:45 -04002382 vec_validate (cf->current_command, vec_len (item) - 1);
2383 clib_memcpy (cf->current_command, item, vec_len (item));
2384 _vec_len (cf->current_command) = vec_len (item);
Chris Luke93dcd1d2016-05-10 10:45:10 -04002385
Dave Barach9b8ffd92016-07-08 08:13:45 -04002386 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
2387 vec_len (cf->current_command));
2388 cf->cursor = vec_len (cf->current_command);
2389 goto found;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002390
Dave Barach9b8ffd92016-07-08 08:13:45 -04002391 next:
2392 cf->excursion += cf->search_mode;
2393 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002394
Dave Barach9b8ffd92016-07-08 08:13:45 -04002395 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12);
2396 vec_reset_length (cf->search_key);
2397 vec_reset_length (cf->current_command);
2398 cf->search_mode = 0;
2399 cf->cursor = 0;
2400 goto crlf;
2401 }
2402 else if (isprint (input)) /* skip any errant control codes */
2403 {
2404 if (cf->cursor == vec_len (cf->current_command))
2405 {
2406 /* Append to end */
2407 vec_add1 (cf->current_command, input);
2408 cf->cursor++;
Chris Luke7afda3a2016-04-25 13:49:22 -04002409
Dave Barach9b8ffd92016-07-08 08:13:45 -04002410 /* Echo the character back to the client */
Chris Lukefc379d22018-06-05 23:50:19 -04002411 unix_vlib_cli_output_cooked (cf, uf, &input, 1);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002412 }
2413 else
2414 {
2415 /* Insert at cursor: resize +1 byte, move everything over */
2416 j = vec_len (cf->current_command) - cf->cursor;
2417 vec_add1 (cf->current_command, (u8) 'A');
2418 memmove (cf->current_command + cf->cursor + 1,
2419 cf->current_command + cf->cursor, j);
2420 cf->current_command[cf->cursor] = input;
2421 /* Redraw the line */
2422 j++;
Chris Lukefc379d22018-06-05 23:50:19 -04002423 unix_vlib_cli_output_cooked (cf, uf,
2424 cf->current_command + cf->cursor,
2425 j);
2426 cf->cursor += j;
2427 j--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002428 /* Put terminal cursor back */
Chris Lukefc379d22018-06-05 23:50:19 -04002429 for (; j > 0; j--, cf->cursor--)
2430 unix_vlib_cli_output_cursor_left (cf, uf);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002431 }
2432 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002433 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002434 {
2435 /* no-op - not printable or otherwise not actionable */
2436 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002437
2438 found:
2439
2440 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04002441
2442 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2443 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002444 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04002445 return 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002446}
2447
Chris Luke8e5b0412016-07-26 13:06:10 -04002448/** @brief Process input bytes on a stream to provide line editing and
Chris Luke0aca5eb2016-04-25 13:48:54 -04002449 * command history in the CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002450static int
Damjan Marion56dd5432017-09-08 19:52:02 +02002451unix_cli_line_edit (unix_cli_main_t * cm, unix_main_t * um,
2452 clib_file_main_t * fm, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04002453{
Damjan Marion56dd5432017-09-08 19:52:02 +02002454 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002455 int i;
2456
2457 for (i = 0; i < vec_len (cf->input_vector); i++)
2458 {
2459 unix_cli_parse_action_t action;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002460 i32 matched = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04002461 unix_cli_parse_actions_t *a;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002462
Chris Luke7afda3a2016-04-25 13:49:22 -04002463 /* If we're in the pager mode, search the pager actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002464 a =
2465 vec_len (cf->pager_index) ? unix_cli_parse_pager :
2466 unix_cli_parse_strings;
Chris Luke7afda3a2016-04-25 13:49:22 -04002467
Chris Luke93dcd1d2016-05-10 10:45:10 -04002468 /* See if the input buffer is some sort of control code */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002469 action = unix_cli_match_action (a, &cf->input_vector[i],
2470 vec_len (cf->input_vector) - i,
2471 &matched);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002472
2473 switch (action)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002474 {
2475 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2476 if (i)
2477 {
2478 /* There was a partial match which means we need more bytes
2479 * than the input buffer currently has.
2480 * Since the bytes before here have been processed, shift
2481 * the remaining contents to the start of the input buffer.
2482 */
2483 vec_delete (cf->input_vector, i, 0);
2484 }
2485 return 1; /* wait for more */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002486
Dave Barach9b8ffd92016-07-08 08:13:45 -04002487 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2488 /* process telnet options */
2489 matched = unix_cli_process_telnet (um, cf, uf,
2490 cf->input_vector + i,
2491 vec_len (cf->input_vector) - i);
2492 if (matched < 0)
2493 {
Chris Luke03add7f2017-09-20 23:31:24 -04002494 /* There was a partial match which means we need more bytes
2495 * than the input buffer currently has.
2496 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002497 if (i)
2498 {
Chris Luke03add7f2017-09-20 23:31:24 -04002499 /*
Dave Barach9b8ffd92016-07-08 08:13:45 -04002500 * Since the bytes before here have been processed, shift
2501 * the remaining contents to the start of the input buffer.
2502 */
2503 vec_delete (cf->input_vector, i, 0);
2504 }
2505 return 1; /* wait for more */
2506 }
2507 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002508
Dave Barach9b8ffd92016-07-08 08:13:45 -04002509 default:
Chris Luke03add7f2017-09-20 23:31:24 -04002510 /* If telnet option processing switched us to line mode, get us
2511 * out of here!
2512 */
2513 if (cf->line_mode)
2514 {
2515 vec_delete (cf->input_vector, i, 0);
Benoît Ganne433a4982020-10-01 18:20:23 +02002516 vec_free (cf->current_command);
Chris Luke03add7f2017-09-20 23:31:24 -04002517 cf->current_command = cf->input_vector;
2518 return 0;
2519 }
2520
Dave Barach9b8ffd92016-07-08 08:13:45 -04002521 /* process the action */
2522 if (!unix_cli_line_process_one (cm, um, cf, uf,
2523 cf->input_vector[i], action))
2524 {
2525 /* CRLF found. Consume the bytes from the input_vector */
2526 vec_delete (cf->input_vector, i + matched, 0);
2527 /* And tell our caller to execute cf->input_command */
2528 return 0;
2529 }
2530 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002531
2532 i += matched;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002533 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002534
Dave Barach9b8ffd92016-07-08 08:13:45 -04002535 vec_reset_length (cf->input_vector);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002536 return 1;
2537}
2538
Chris Luke8e5b0412016-07-26 13:06:10 -04002539/** @brief Process input to a CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002540static void
2541unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002542{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002543 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002544 clib_file_main_t *fm = &file_main;
2545 clib_file_t *uf;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002546 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002547 unformat_input_t input;
2548 int vlib_parse_eval (u8 *);
2549
Chris Luke03add7f2017-09-20 23:31:24 -04002550 cm->current_input_file_index = cli_file_index;
2551
Chris Luke93dcd1d2016-05-10 10:45:10 -04002552more:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002553 /* Try vlibplex first. Someday... */
2554 if (0 && vlib_parse_eval (cf->input_vector) == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002555 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002556
Chris Luke03add7f2017-09-20 23:31:24 -04002557
Chris Luke93dcd1d2016-05-10 10:45:10 -04002558 if (cf->line_mode)
2559 {
2560 /* just treat whatever we got as a complete line of input */
2561 cf->current_command = cf->input_vector;
2562 }
2563 else
2564 {
2565 /* Line edit, echo, etc. */
Damjan Marion56dd5432017-09-08 19:52:02 +02002566 if (unix_cli_line_edit (cm, um, fm, cf))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002567 /* want more input */
2568 return;
Chris Luke93dcd1d2016-05-10 10:45:10 -04002569 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002570
2571 if (um->log_fd)
2572 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002573 static u8 *lv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002574 vec_reset_length (lv);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002575 lv = format (lv, "%U[%d]: %v",
2576 format_timeval, 0 /* current bat-time */ ,
2577 0 /* current bat-format */ ,
Chris Luke03add7f2017-09-20 23:31:24 -04002578 cli_file_index, cf->current_command);
Fan Zhangce266ad2020-03-12 09:26:38 +00002579 if ((vec_len (cf->current_command) > 0) &&
2580 (cf->current_command[vec_len (cf->current_command) - 1] != '\n'))
Paul Vinciguerra8a023fd2020-03-01 00:47:17 -05002581 lv = format (lv, "\n");
Chris Luke03add7f2017-09-20 23:31:24 -04002582 int rv __attribute__ ((unused)) = write (um->log_fd, lv, vec_len (lv));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002583 }
2584
Dave Barach961e3c82020-06-18 17:04:18 -04002585 /* Run the command through the macro processor */
2586 if (vec_len (cf->current_command))
2587 {
2588 u8 *expanded;
2589 vec_validate (cf->current_command, vec_len (cf->current_command));
2590 cf->current_command[vec_len (cf->current_command) - 1] = 0;
2591 /* The macro expander expects proper C-strings, not vectors */
Dave Barachb30b9542020-06-22 10:02:25 -04002592 expanded = (u8 *) clib_macro_eval (&cf->macro_main,
Dave Barach961e3c82020-06-18 17:04:18 -04002593 (i8 *) cf->current_command,
Dave Barachb30b9542020-06-22 10:02:25 -04002594 1 /* complain */ ,
2595 0 /* level */ ,
2596 8 /* max_level */ );
Dave Barach961e3c82020-06-18 17:04:18 -04002597 /* Macro processor NULL terminates the return */
2598 _vec_len (expanded) -= 1;
2599 vec_reset_length (cf->current_command);
2600 vec_append (cf->current_command, expanded);
2601 vec_free (expanded);
2602 }
2603
Chris Luke03add7f2017-09-20 23:31:24 -04002604 /* Build an unformat structure around our command */
Chris Luke93dcd1d2016-05-10 10:45:10 -04002605 unformat_init_vector (&input, cf->current_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002606
2607 /* Remove leading white space from input. */
2608 (void) unformat (&input, "");
2609
Dave Barach9b8ffd92016-07-08 08:13:45 -04002610 cf->pager_start = 0; /* start a new pager session */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002611
2612 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002613 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output,
2614 cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002615
Ed Warnickecb9cada2015-12-08 15:45:58 -07002616 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
2617 input.buffer = 0;
2618
2619 unformat_free (&input);
2620
Chris Luke93dcd1d2016-05-10 10:45:10 -04002621 /* Re-fetch pointer since pool may have moved. */
2622 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002623 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke93dcd1d2016-05-10 10:45:10 -04002624
Ed Warnickecb9cada2015-12-08 15:45:58 -07002625done:
Chris Luke93dcd1d2016-05-10 10:45:10 -04002626 /* reset vector; we'll re-use it later */
2627 if (cf->line_mode)
Chris Luke03add7f2017-09-20 23:31:24 -04002628 {
2629 vec_reset_length (cf->input_vector);
2630 cf->current_command = 0;
2631 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002632 else
Chris Luke03add7f2017-09-20 23:31:24 -04002633 {
2634 vec_reset_length (cf->current_command);
2635 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002636
Chris Luke7afda3a2016-04-25 13:49:22 -04002637 if (cf->no_pager == 2)
2638 {
2639 /* Pager was programmatically disabled */
2640 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
2641 cf->no_pager = um->cli_no_pager;
2642 }
2643
Dave Barach9b8ffd92016-07-08 08:13:45 -04002644 if (vec_len (cf->pager_index) == 0
2645 || vec_len (cf->pager_index) < cf->height)
Chris Luke7afda3a2016-04-25 13:49:22 -04002646 {
2647 /* There was no need for the pager */
2648 unix_cli_pager_reset (cf);
2649
2650 /* Prompt. */
2651 unix_cli_cli_prompt (cf, uf);
2652 }
2653 else
2654 {
2655 /* Display the pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002656 unix_cli_pager_prompt (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04002657 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002658
Dave Barach9b8ffd92016-07-08 08:13:45 -04002659 /* Any residual data in the input vector? */
2660 if (vec_len (cf->input_vector))
2661 goto more;
Chris Luke03add7f2017-09-20 23:31:24 -04002662
2663 /* For non-interactive sessions send a NUL byte.
2664 * Specifically this is because vppctl needs to see some traffic in
2665 * order to move on to closing the session. Commands with no output
2666 * would thus cause vppctl to hang indefinitely in non-interactive mode
2667 * since there is also no prompt sent after the command completes.
2668 */
2669 if (!cf->is_interactive)
2670 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\0", 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002671}
2672
Chris Luke54ccf222016-07-25 16:38:11 -04002673/** Destroy a CLI session.
2674 * @note If we destroy the @c stdin session this additionally signals
2675 * the shutdown of VPP.
2676 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002677static void
2678unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002679{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002680 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002681 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002682 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +02002683 clib_file_t *uf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002684 int i;
2685
Steve Shin0d5a1952018-06-29 09:40:20 -07002686 /* Validate cli_file_index */
2687 if (pool_is_free_index (cm->cli_file_pool, cli_file_index))
2688 return;
2689
Steven Luong17a67212021-08-20 19:14:16 -07002690 vec_foreach_index (i, cm->new_sessions)
2691 {
2692 unix_cli_new_session_t *ns = vec_elt_at_index (cm->new_sessions, i);
2693
2694 if (ns->cf_index == cli_file_index)
2695 {
2696 vec_del1 (cm->new_sessions, i);
2697 break;
2698 }
2699 }
2700
Ed Warnickecb9cada2015-12-08 15:45:58 -07002701 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002702 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002703
2704 /* Quit/EOF on stdin means quit program. */
Chris Luke03add7f2017-09-20 23:31:24 -04002705 if (uf->file_descriptor == STDIN_FILENO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002706 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
2707
2708 vec_free (cf->current_command);
2709 vec_free (cf->search_key);
2710
2711 for (i = 0; i < vec_len (cf->command_history); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002712 vec_free (cf->command_history[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002713
2714 vec_free (cf->command_history);
Dave Barach5f09efe2020-11-09 17:11:57 -05002715 vec_free (cf->input_vector);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002716
Damjan Marion56dd5432017-09-08 19:52:02 +02002717 clib_file_del (fm, uf);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002718
2719 unix_cli_file_free (cf);
Dave Barachb30b9542020-06-22 10:02:25 -04002720 clib_macro_free (&cf->macro_main);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002721 pool_put (cm->cli_file_pool, cf);
2722}
2723
Chris Luke54ccf222016-07-25 16:38:11 -04002724/** Handle system events. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002725static uword
2726unix_cli_process (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002727 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002728{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002729 unix_cli_main_t *cm = &unix_cli_main;
2730 uword i, *data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002731
2732 while (1)
2733 {
2734 unix_cli_process_event_type_t event_type;
2735 vlib_process_wait_for_event (vm);
2736 event_type = vlib_process_get_events (vm, &data);
2737
2738 switch (event_type)
2739 {
2740 case UNIX_CLI_PROCESS_EVENT_READ_READY:
2741 for (i = 0; i < vec_len (data); i++)
2742 unix_cli_process_input (cm, data[i]);
2743 break;
2744
2745 case UNIX_CLI_PROCESS_EVENT_QUIT:
2746 /* Kill this process. */
2747 for (i = 0; i < vec_len (data); i++)
2748 unix_cli_kill (cm, data[i]);
2749 goto done;
2750 }
2751
2752 if (data)
2753 _vec_len (data) = 0;
2754 }
2755
Dave Barach9b8ffd92016-07-08 08:13:45 -04002756done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002757 vec_free (data);
2758
2759 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
2760
2761 /* Add node index so we can re-use this process later. */
2762 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
2763
2764 return 0;
2765}
2766
Chris Luke54ccf222016-07-25 16:38:11 -04002767/** Called when a CLI session file descriptor can be written to without
2768 * blocking. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002769static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002770unix_cli_write_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002771{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002772 unix_cli_main_t *cm = &unix_cli_main;
2773 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002774 int n;
2775
2776 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2777
2778 /* Flush output vector. */
Chris Luke03add7f2017-09-20 23:31:24 -04002779 if (cf->is_socket)
2780 /* If it's a socket we use MSG_NOSIGNAL to prevent SIGPIPE */
2781 n = send (uf->file_descriptor,
2782 cf->output_vector, vec_len (cf->output_vector), MSG_NOSIGNAL);
2783 else
2784 n = write (uf->file_descriptor,
2785 cf->output_vector, vec_len (cf->output_vector));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002786
2787 if (n < 0 && errno != EAGAIN)
Chris Luke03add7f2017-09-20 23:31:24 -04002788 {
2789 if (errno == EPIPE)
2790 {
2791 /* connection closed on us */
2792 unix_main_t *um = &unix_main;
2793 cf->has_epipe = 1;
2794 vlib_process_signal_event (um->vlib_main, cf->process_node_index,
2795 UNIX_CLI_PROCESS_EVENT_QUIT,
2796 uf->private_data);
2797 }
2798 else
2799 {
2800 return clib_error_return_unix (0, "write");
2801 }
2802 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002803
2804 else if (n > 0)
2805 unix_cli_del_pending_output (uf, cf, n);
2806
2807 return /* no error */ 0;
2808}
2809
Chris Luke54ccf222016-07-25 16:38:11 -04002810/** Called when a CLI session file descriptor has data to be read. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002811static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002812unix_cli_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002813{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002814 unix_main_t *um = &unix_main;
2815 unix_cli_main_t *cm = &unix_cli_main;
2816 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002817 uword l;
2818 int n, n_read, n_try;
2819
2820 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2821
2822 n = n_try = 4096;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002823 while (n == n_try)
2824 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002825 l = vec_len (cf->input_vector);
2826 vec_resize (cf->input_vector, l + n_try);
2827
2828 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
2829
2830 /* Error? */
2831 if (n < 0 && errno != EAGAIN)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002832 return clib_error_return_unix (0, "read");
2833
Ed Warnickecb9cada2015-12-08 15:45:58 -07002834 n_read = n < 0 ? 0 : n;
2835 _vec_len (cf->input_vector) = l + n_read;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002836 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002837
Dave Barach9b8ffd92016-07-08 08:13:45 -04002838 if (!(n < 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002839 vlib_process_signal_event (um->vlib_main,
2840 cf->process_node_index,
2841 (n_read == 0
2842 ? UNIX_CLI_PROCESS_EVENT_QUIT
2843 : UNIX_CLI_PROCESS_EVENT_READ_READY),
2844 /* event data */ uf->private_data);
2845
2846 return /* no error */ 0;
2847}
2848
Chris Luke03add7f2017-09-20 23:31:24 -04002849/** Called when a CLI session file descriptor has an error condition. */
2850static clib_error_t *
2851unix_cli_error_detected (clib_file_t * uf)
2852{
2853 unix_main_t *um = &unix_main;
2854 unix_cli_main_t *cm = &unix_cli_main;
2855 unix_cli_file_t *cf;
2856
2857 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2858 cf->has_epipe = 1; /* prevent writes while the close is pending */
2859 vlib_process_signal_event (um->vlib_main,
2860 cf->process_node_index,
2861 UNIX_CLI_PROCESS_EVENT_QUIT,
2862 /* event data */ uf->private_data);
2863
2864 return /* no error */ 0;
2865}
2866
Chris Lukeb5850972016-05-03 16:34:59 -04002867/** Store a new CLI session.
2868 * @param name The name of the session.
2869 * @param fd The file descriptor for the session I/O.
2870 * @return The session ID.
2871 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002872static u32
2873unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002874{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002875 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002876 clib_file_main_t *fm = &file_main;
Dave Barach52c33d62021-06-11 12:57:23 -04002877 vlib_node_main_t *nm = &vlib_get_main ()->node_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002878 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +02002879 clib_file_t template = { 0 };
Dave Barach9b8ffd92016-07-08 08:13:45 -04002880 vlib_main_t *vm = um->vlib_main;
Dave Barach06100392018-07-12 13:00:47 -04002881 vlib_node_t *n = 0;
Damjan Marionceab7882018-01-19 20:56:12 +01002882 u8 *file_desc = 0;
2883
2884 file_desc = format (0, "%s", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002885
2886 name = (char *) format (0, "unix-cli-%s", name);
2887
2888 if (vec_len (cm->unused_cli_process_node_indices) > 0)
2889 {
2890 uword l = vec_len (cm->unused_cli_process_node_indices);
Dave Barach06100392018-07-12 13:00:47 -04002891 int i;
2892 vlib_main_t *this_vlib_main;
2893 u8 *old_name = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002894
Dave Barach06100392018-07-12 13:00:47 -04002895 /*
2896 * Nodes are bulk-copied, so node name pointers are shared.
2897 * Find the cli node in all graph replicas, and give all of them
2898 * the same new name.
2899 * Then, throw away the old shared name-vector.
2900 */
Damjan Marion6ffb7c62021-03-26 13:06:13 +01002901 for (i = 0; i < vlib_get_n_threads (); i++)
Dave Barach06100392018-07-12 13:00:47 -04002902 {
Damjan Marion6ffb7c62021-03-26 13:06:13 +01002903 this_vlib_main = vlib_get_main_by_index (i);
Dave Barach06100392018-07-12 13:00:47 -04002904 if (this_vlib_main == 0)
2905 continue;
2906 n = vlib_get_node (this_vlib_main,
2907 cm->unused_cli_process_node_indices[l - 1]);
2908 old_name = n->name;
2909 n->name = (u8 *) name;
2910 }
Dave Barach52c33d62021-06-11 12:57:23 -04002911 ASSERT (old_name);
2912 hash_unset (nm->node_by_name, old_name);
2913 hash_set (nm->node_by_name, name, n->index);
Dave Barach06100392018-07-12 13:00:47 -04002914 vec_free (old_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002915
2916 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002917 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
2918 }
2919 else
2920 {
2921 static vlib_node_registration_t r = {
2922 .function = unix_cli_process,
2923 .type = VLIB_NODE_TYPE_PROCESS,
Chenmin Sun2fd44a02019-10-08 03:35:20 +08002924 .process_log2_n_stack_bytes = 18,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002925 };
2926
2927 r.name = name;
Dave Barach06100392018-07-12 13:00:47 -04002928
2929 vlib_worker_thread_barrier_sync (vm);
2930
Ed Warnickecb9cada2015-12-08 15:45:58 -07002931 vlib_register_node (vm, &r);
2932 vec_free (name);
2933
2934 n = vlib_get_node (vm, r.index);
Dave Barach06100392018-07-12 13:00:47 -04002935 vlib_worker_thread_node_runtime_update ();
2936 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002937 }
2938
2939 pool_get (cm->cli_file_pool, cf);
Dave Barachb7b92992018-10-17 10:38:51 -04002940 clib_memset (cf, 0, sizeof (*cf));
Dave Barachb30b9542020-06-22 10:02:25 -04002941 clib_macro_init (&cf->macro_main);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002942
2943 template.read_function = unix_cli_read_ready;
2944 template.write_function = unix_cli_write_ready;
Chris Luke03add7f2017-09-20 23:31:24 -04002945 template.error_function = unix_cli_error_detected;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002946 template.file_descriptor = fd;
2947 template.private_data = cf - cm->cli_file_pool;
Damjan Marionceab7882018-01-19 20:56:12 +01002948 template.description = file_desc;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002949
2950 cf->process_node_index = n->index;
Damjan Marion56dd5432017-09-08 19:52:02 +02002951 cf->clib_file_index = clib_file_add (fm, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002952 cf->output_vector = 0;
2953 cf->input_vector = 0;
Dave Barach961e3c82020-06-18 17:04:18 -04002954 vec_validate (cf->current_command, 0);
2955 _vec_len (cf->current_command) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002956
Ed Warnickecb9cada2015-12-08 15:45:58 -07002957 vlib_start_process (vm, n->runtime_index);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002958
Dave Barach9b8ffd92016-07-08 08:13:45 -04002959 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002960 p->output_function = unix_vlib_cli_output;
2961 p->output_function_arg = cf - cm->cli_file_pool;
2962
Ed Warnickecb9cada2015-12-08 15:45:58 -07002963 return cf - cm->cli_file_pool;
2964}
2965
Chris Lukeb5850972016-05-03 16:34:59 -04002966/** Telnet listening socket has a new connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002967static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002968unix_cli_listen_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002969{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002970 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002971 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002972 unix_cli_main_t *cm = &unix_cli_main;
2973 clib_socket_t *s = &um->cli_listen_socket;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002974 clib_socket_t client;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002975 char *client_name;
2976 clib_error_t *error;
2977 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002978 u32 cf_index;
Chris Lukea9cf6af2018-09-05 21:00:52 -04002979 int one;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002980
2981 error = clib_socket_accept (s, &client);
2982 if (error)
2983 return error;
2984
Chris Lukea9cf6af2018-09-05 21:00:52 -04002985 /* Disable Nagle, ignore any errors doing so eg on PF_LOCAL socket */
2986 one = 1;
Chris Lukec5cb6382018-09-06 15:37:28 -04002987 (void) setsockopt (client.fd, IPPROTO_TCP, TCP_NODELAY,
2988 (void *) &one, sizeof (one));
Chris Lukea9cf6af2018-09-05 21:00:52 -04002989
Ed Warnickecb9cada2015-12-08 15:45:58 -07002990 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
2991
2992 cf_index = unix_cli_file_add (cm, client_name, client.fd);
2993 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
Chris Luke03add7f2017-09-20 23:31:24 -04002994 cf->is_socket = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002995
2996 /* No longer need CLIB version of socket. */
2997 clib_socket_free (&client);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002998 vec_free (client_name);
2999
3000 /* if we're supposed to run telnet session in character mode (default) */
3001 if (um->cli_line_mode == 0)
3002 {
Chris Luke0aca5eb2016-04-25 13:48:54 -04003003 /*
3004 * Set telnet client character mode, echo on, suppress "go-ahead".
3005 * Technically these should be negotiated, but this works.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003006 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04003007 u8 charmode_option[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003008 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
3009 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
3010 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
3011 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
3012 IAC, WILL, TELOPT_ECHO, /* server will do echo */
3013 IAC, DONT, TELOPT_ECHO, /* client should not echo */
3014 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
3015 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
3016 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
3017 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
Chris Luke0aca5eb2016-04-25 13:48:54 -04003018 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07003019
Chris Luke0aca5eb2016-04-25 13:48:54 -04003020 /* Enable history on this CLI */
Chris Luke7afda3a2016-04-25 13:49:22 -04003021 cf->history_limit = um->cli_history_limit;
3022 cf->has_history = cf->history_limit != 0;
3023
Chris Luke03add7f2017-09-20 23:31:24 -04003024 /* This is an interactive session until we decide otherwise */
3025 cf->is_interactive = 1;
3026
Chris Luke7afda3a2016-04-25 13:49:22 -04003027 /* Make sure this session is in line mode */
3028 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003029
3030 /* We need CRLF */
3031 cf->crlf_mode = 1;
3032
Chris Luke7afda3a2016-04-25 13:49:22 -04003033 /* Setup the pager */
3034 cf->no_pager = um->cli_no_pager;
3035
Chris Luke2d8bf302017-11-12 22:26:37 -05003036 /* Default terminal dimensions, should the terminal
3037 * fail to provide any.
3038 */
3039 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
3040 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
3041
Chris Luke0aca5eb2016-04-25 13:48:54 -04003042 /* Send the telnet options */
Chris Luke03add7f2017-09-20 23:31:24 -04003043 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04003044 unix_vlib_cli_output_raw (cf, uf, charmode_option,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003045 ARRAY_LEN (charmode_option));
Chris Luke0aca5eb2016-04-25 13:48:54 -04003046
Chris Luke6a3a4f72019-07-09 23:33:30 -04003047 if (cm->new_session_process_node_index == ~0)
3048 {
3049 /* Create thw new session deadline process */
3050 cm->new_session_process_node_index =
3051 vlib_process_create (um->vlib_main, "unix-cli-new-session",
3052 unix_cli_new_session_process,
3053 16 /* log2_n_stack_bytes */ );
3054 }
3055
3056 /* In case the client doesn't negotiate terminal type, register
3057 * our session with a process that will emit the prompt if
3058 * a deadline passes */
3059 vlib_process_signal_event (um->vlib_main,
3060 cm->new_session_process_node_index,
3061 UNIX_CLI_NEW_SESSION_EVENT_ADD, cf_index);
3062
Ed Warnickecb9cada2015-12-08 15:45:58 -07003063 }
3064
3065 return error;
3066}
3067
Chris Lukeb5850972016-05-03 16:34:59 -04003068/** The system terminal has informed us that the window size
3069 * has changed.
3070 */
Chris Luke7afda3a2016-04-25 13:49:22 -04003071static void
3072unix_cli_resize_interrupt (int signum)
3073{
Damjan Marion56dd5432017-09-08 19:52:02 +02003074 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003075 unix_cli_main_t *cm = &unix_cli_main;
3076 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
3077 cm->stdin_cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02003078 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04003079 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003080 (void) signum;
Chris Luke7afda3a2016-04-25 13:49:22 -04003081
3082 /* Terminal resized, fetch the new size */
Chris Luke03add7f2017-09-20 23:31:24 -04003083 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
Dave Barachb2a6e252016-07-27 10:00:58 -04003084 {
3085 /* "Should never happen..." */
3086 clib_unix_warning ("TIOCGWINSZ");
3087 /* We can't trust ws.XXX... */
3088 return;
3089 }
Chris Lukeb2bcad62017-09-18 08:51:22 -04003090
Chris Luke7afda3a2016-04-25 13:49:22 -04003091 cf->width = ws.ws_col;
Chris Lukeb2bcad62017-09-18 08:51:22 -04003092 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
3093 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
Chris Luke2d8bf302017-11-12 22:26:37 -05003094 if (cf->width == 0)
3095 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
Chris Lukeb2bcad62017-09-18 08:51:22 -04003096
Chris Luke7afda3a2016-04-25 13:49:22 -04003097 cf->height = ws.ws_row;
Chris Lukeb2bcad62017-09-18 08:51:22 -04003098 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
3099 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
Chris Luke2d8bf302017-11-12 22:26:37 -05003100 if (cf->height == 0)
3101 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
Chris Luke862623d2016-05-14 10:13:34 -04003102
3103 /* Reindex the pager buffer */
3104 unix_cli_pager_reindex (cf);
3105
3106 /* Redraw the page */
3107 unix_cli_pager_redraw (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04003108}
3109
Chris Lukeb5850972016-05-03 16:34:59 -04003110/** Handle configuration directives in the @em unix section. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003111static clib_error_t *
3112unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
3113{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003114 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02003115 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003116 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003117 int flags;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003118 clib_error_t *error = 0;
3119 unix_cli_file_t *cf;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003120 u32 cf_index;
3121 struct termios tio;
Chris Luke7afda3a2016-04-25 13:49:22 -04003122 struct sigaction sa;
3123 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003124 u8 *term;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003125
3126 /* We depend on unix flags being set. */
3127 if ((error = vlib_call_config_function (vm, unix_config)))
3128 return error;
3129
3130 if (um->flags & UNIX_FLAG_INTERACTIVE)
3131 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07003132 /* Set stdin to be non-blocking. */
Chris Luke03add7f2017-09-20 23:31:24 -04003133 if ((flags = fcntl (STDIN_FILENO, F_GETFL, 0)) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04003134 flags = 0;
Chris Luke03add7f2017-09-20 23:31:24 -04003135 (void) fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003136
Chris Luke03add7f2017-09-20 23:31:24 -04003137 cf_index = unix_cli_file_add (cm, "stdin", STDIN_FILENO);
Chris Luke0aca5eb2016-04-25 13:48:54 -04003138 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04003139 cm->stdin_cli_file_index = cf_index;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003140
3141 /* If stdin is a tty and we are using chacracter mode, enable
3142 * history on the CLI and set the tty line discipline accordingly. */
Chris Luke03add7f2017-09-20 23:31:24 -04003143 if (isatty (STDIN_FILENO) && um->cli_line_mode == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04003144 {
3145 /* Capture terminal resize events */
Dave Barachb7b92992018-10-17 10:38:51 -04003146 clib_memset (&sa, 0, sizeof (sa));
Dave Barach9b8ffd92016-07-08 08:13:45 -04003147 sa.sa_handler = unix_cli_resize_interrupt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003148 if (sigaction (SIGWINCH, &sa, 0) < 0)
3149 clib_panic ("sigaction");
Chris Luke7afda3a2016-04-25 13:49:22 -04003150
Dave Barach9b8ffd92016-07-08 08:13:45 -04003151 /* Retrieve the current terminal size */
Dave Baracha6ef36b2020-02-11 10:29:13 -05003152 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0)
3153 {
3154 cf->width = ws.ws_col;
3155 cf->height = ws.ws_row;
3156 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003157
Dave Barach9b8ffd92016-07-08 08:13:45 -04003158 if (cf->width == 0 || cf->height == 0)
Dave Barachb66201f2017-09-22 13:34:39 -04003159 {
3160 /*
3161 * We have a tty, but no size. Use defaults.
3162 * vpp "unix interactive" inside emacs + gdb ends up here.
3163 */
Chris Luke2d8bf302017-11-12 22:26:37 -05003164 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
3165 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
Dave Barachb66201f2017-09-22 13:34:39 -04003166 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003167
Dave Barach9b8ffd92016-07-08 08:13:45 -04003168 /* Setup the history */
3169 cf->history_limit = um->cli_history_limit;
3170 cf->has_history = cf->history_limit != 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003171
Dave Barach9b8ffd92016-07-08 08:13:45 -04003172 /* Setup the pager */
3173 cf->no_pager = um->cli_no_pager;
Chris Luke7afda3a2016-04-25 13:49:22 -04003174
Chris Luke03add7f2017-09-20 23:31:24 -04003175 /* This is an interactive session until we decide otherwise */
3176 cf->is_interactive = 1;
3177
Dave Barach9b8ffd92016-07-08 08:13:45 -04003178 /* We're going to be in char by char mode */
3179 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003180
Dave Barach9b8ffd92016-07-08 08:13:45 -04003181 /* Save the original tty state so we can restore it later */
Chris Luke03add7f2017-09-20 23:31:24 -04003182 tcgetattr (STDIN_FILENO, &um->tio_stdin);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003183 um->tio_isset = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003184
Dave Barach9b8ffd92016-07-08 08:13:45 -04003185 /* Tweak the tty settings */
3186 tio = um->tio_stdin;
3187 /* echo off, canonical mode off, ext'd input processing off */
3188 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
Chris Luke01dc6b92018-06-10 13:42:45 -04003189 /* disable XON/XOFF, so ^S invokes the history search */
3190 tio.c_iflag &= ~(IXON | IXOFF);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003191 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
3192 tio.c_cc[VTIME] = 0; /* no timer */
Chris Luke01dc6b92018-06-10 13:42:45 -04003193 tio.c_cc[VSTOP] = _POSIX_VDISABLE; /* not ^S */
3194 tio.c_cc[VSTART] = _POSIX_VDISABLE; /* not ^Q */
Chris Luke03add7f2017-09-20 23:31:24 -04003195 tcsetattr (STDIN_FILENO, TCSAFLUSH, &tio);
Chris Luke0aca5eb2016-04-25 13:48:54 -04003196
Dave Barach9b8ffd92016-07-08 08:13:45 -04003197 /* See if we can do ANSI/VT100 output */
3198 term = (u8 *) getenv ("TERM");
3199 if (term != NULL)
Chris Luke03add7f2017-09-20 23:31:24 -04003200 {
3201 int len = strlen ((char *) term);
3202 cf->ansi_capable = unix_cli_terminal_type_ansi (term, len);
3203 if (unix_cli_terminal_type_noninteractive (term, len))
3204 unix_cli_set_session_noninteractive (cf);
3205 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04003206 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003207 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04003208 {
Chris Luke03add7f2017-09-20 23:31:24 -04003209 /* No tty, so make sure the session doesn't have tty-like features */
3210 unix_cli_set_session_noninteractive (cf);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003211 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04003212
3213 /* Send banner and initial prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04003214 unix_cli_file_welcome (cm, cf);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003215 }
3216
Ed Warnickea25bd1c2016-04-01 22:43:37 -05003217 /* If we have socket config, LISTEN, otherwise, don't */
Dave Barach9b8ffd92016-07-08 08:13:45 -04003218 clib_socket_t *s = &um->cli_listen_socket;
3219 if (s->config && s->config[0] != 0)
3220 {
3221 /* CLI listen. */
Damjan Marion56dd5432017-09-08 19:52:02 +02003222 clib_file_t template = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07003223
Damjan Marion57d963f2017-07-20 19:17:06 +02003224 /* mkdir of file socketu, only under /run */
3225 if (strncmp (s->config, "/run", 4) == 0)
Chris Luke475674e2017-07-05 18:02:53 -04003226 {
Damjan Marion57d963f2017-07-20 19:17:06 +02003227 u8 *tmp = format (0, "%s", s->config);
3228 int i = vec_len (tmp);
3229 while (i && tmp[--i] != '/')
3230 ;
3231
YohanPipereau71dd9d52019-06-06 16:34:14 +02003232 tmp[i] = '\0';
Damjan Marion57d963f2017-07-20 19:17:06 +02003233
3234 if (i)
3235 vlib_unix_recursive_mkdir ((char *) tmp);
3236 vec_free (tmp);
Chris Luke475674e2017-07-05 18:02:53 -04003237 }
3238
Damjan Marionf49921f2017-09-11 16:52:11 +02003239 s->flags = CLIB_SOCKET_F_IS_SERVER | /* listen, don't connect */
3240 CLIB_SOCKET_F_ALLOW_GROUP_WRITE; /* PF_LOCAL socket only */
Dave Barach9b8ffd92016-07-08 08:13:45 -04003241 error = clib_socket_init (s);
Ed Warnickea25bd1c2016-04-01 22:43:37 -05003242
Dave Barach9b8ffd92016-07-08 08:13:45 -04003243 if (error)
3244 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003245
Dave Barach9b8ffd92016-07-08 08:13:45 -04003246 template.read_function = unix_cli_listen_read_ready;
3247 template.file_descriptor = s->fd;
Damjan Marionceab7882018-01-19 20:56:12 +01003248 template.description = format (0, "cli listener %s", s->config);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003249
Damjan Marion56dd5432017-09-08 19:52:02 +02003250 clib_file_add (fm, &template);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003251 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003252
3253 /* Set CLI prompt. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04003254 if (!cm->cli_prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003255 cm->cli_prompt = format (0, "VLIB: ");
3256
3257 return 0;
3258}
3259
Chris Luke90f52bf2016-09-12 08:55:13 -04003260/*?
3261 * This module has no configurable parameters.
3262?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07003263VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
3264
Chris Luke54ccf222016-07-25 16:38:11 -04003265/** Called when VPP is shutting down, this restores the system
3266 * terminal state if previously saved.
Chris Lukeb5850972016-05-03 16:34:59 -04003267 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04003268static clib_error_t *
3269unix_cli_exit (vlib_main_t * vm)
3270{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003271 unix_main_t *um = &unix_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003272
3273 /* If stdin is a tty and we saved the tty state, reset the tty state */
Chris Luke03add7f2017-09-20 23:31:24 -04003274 if (isatty (STDIN_FILENO) && um->tio_isset)
3275 tcsetattr (STDIN_FILENO, TCSAFLUSH, &um->tio_stdin);
Chris Luke0aca5eb2016-04-25 13:48:54 -04003276
3277 return 0;
3278}
3279
3280VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
3281
Chris Lukeb5850972016-05-03 16:34:59 -04003282/** Set the CLI prompt.
Chris Luke54ccf222016-07-25 16:38:11 -04003283 * @param prompt The C string to set the prompt to.
Chris Lukeb5850972016-05-03 16:34:59 -04003284 * @note This setting is global; it impacts all current
3285 * and future CLI sessions.
3286 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04003287void
3288vlib_unix_cli_set_prompt (char *prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003289{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003290 char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s ";
3291 unix_cli_main_t *cm = &unix_cli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003292 if (cm->cli_prompt)
3293 vec_free (cm->cli_prompt);
3294 cm->cli_prompt = format (0, fmt, prompt);
3295}
3296
Benoît Gannea58be822020-05-11 16:27:29 +02003297static unix_cli_file_t *
Benoît Ganne10a22a62020-05-15 16:12:23 +02003298unix_cli_file_if_exists (unix_cli_main_t * cm)
3299{
3300 if (!cm->cli_file_pool)
3301 return 0;
3302 return pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3303}
3304
3305static unix_cli_file_t *
Benoît Gannea58be822020-05-11 16:27:29 +02003306unix_cli_file_if_interactive (unix_cli_main_t * cm)
3307{
3308 unix_cli_file_t *cf;
Benoît Ganne10a22a62020-05-15 16:12:23 +02003309 if ((cf = unix_cli_file_if_exists (cm)) && !cf->is_interactive)
Benoît Gannea58be822020-05-11 16:27:29 +02003310 return 0;
3311 return cf;
3312}
3313
Chris Lukeb5850972016-05-03 16:34:59 -04003314/** CLI command to quit the terminal session.
3315 * @note If this is a stdin session then this will
3316 * shutdown VPP also.
3317 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003318static clib_error_t *
3319unix_cli_quit (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003320 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003321{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003322 unix_cli_main_t *cm = &unix_cli_main;
Benoît Gannea58be822020-05-11 16:27:29 +02003323 unix_cli_file_t *cf;
3324
Benoît Ganne10a22a62020-05-15 16:12:23 +02003325 if (!(cf = unix_cli_file_if_exists (cm)))
3326 return clib_error_return (0, "invalid session");
Chris Luke03add7f2017-09-20 23:31:24 -04003327
3328 /* Cosmetic: suppress the final prompt from appearing before we die */
3329 cf->is_interactive = 0;
3330 cf->started = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003331
3332 vlib_process_signal_event (vm,
3333 vlib_current_process (vm),
3334 UNIX_CLI_PROCESS_EVENT_QUIT,
3335 cm->current_input_file_index);
3336 return 0;
3337}
3338
Chris Luke54ccf222016-07-25 16:38:11 -04003339/*?
3340 * Terminates the current CLI session.
3341 *
3342 * If VPP is running in @em interactive mode and this is the console session
3343 * (that is, the session on @c stdin) then this will also terminate VPP.
3344?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003345/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003346VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
3347 .path = "quit",
3348 .short_help = "Exit CLI",
3349 .function = unix_cli_quit,
3350};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003351/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003352
Florin Coras33d16292018-03-16 09:13:37 -07003353/* *INDENT-OFF* */
3354VLIB_CLI_COMMAND (unix_cli_q_command, static) = {
3355 .path = "q",
3356 .short_help = "Exit CLI",
3357 .function = unix_cli_quit,
3358};
3359/* *INDENT-ON* */
3360
Chris Lukeb5850972016-05-03 16:34:59 -04003361/** CLI command to execute a VPP command script. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003362static clib_error_t *
3363unix_cli_exec (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003364 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003365{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003366 char *file_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003367 int fd;
3368 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003369 clib_error_t *error;
Dave Barach85866e72020-11-09 10:30:06 -05003370 unix_cli_main_t *cm = &unix_cli_main;
3371 unix_cli_file_t *cf;
3372 u8 *file_data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003373 file_name = 0;
3374 fd = -1;
3375 error = 0;
Dave Barach85866e72020-11-09 10:30:06 -05003376 struct stat s;
3377
Ed Warnickecb9cada2015-12-08 15:45:58 -07003378
Dave Barach9b8ffd92016-07-08 08:13:45 -04003379 if (!unformat (input, "%s", &file_name))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003380 {
3381 error = clib_error_return (0, "expecting file name, got `%U'",
3382 format_unformat_error, input);
3383 goto done;
3384 }
3385
3386 fd = open (file_name, O_RDONLY);
3387 if (fd < 0)
3388 {
3389 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
3390 goto done;
3391 }
3392
3393 /* Make sure its a regular file. */
Dave Barach85866e72020-11-09 10:30:06 -05003394 if (fstat (fd, &s) < 0)
3395 {
3396 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
3397 goto done;
3398 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003399
Dave Barach85866e72020-11-09 10:30:06 -05003400 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
3401 {
3402 error = clib_error_return (0, "not a regular file `%s'", file_name);
3403 goto done;
3404 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04003405
Dave Barach85866e72020-11-09 10:30:06 -05003406 /* Read the file */
3407 vec_validate (file_data, s.st_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003408
Dave Barach85866e72020-11-09 10:30:06 -05003409 if (read (fd, file_data, s.st_size) != s.st_size)
3410 {
3411 error = clib_error_return_unix (0, "Failed to read %d bytes from '%s'",
3412 s.st_size, file_name);
3413 vec_free (file_data);
3414 goto done;
3415 }
3416
3417 /* The macro expander expects a c string... */
3418 vec_add1 (file_data, 0);
3419
3420 unformat_init_vector (&sub_input, file_data);
3421
3422 /* Run the file contents through the macro processor */
3423 if (vec_len (sub_input.buffer) > 1)
3424 {
3425 u8 *expanded;
3426 clib_macro_main_t *mm = 0;
3427
3428 /* Initial config process? Use the global macro table. */
3429 if (pool_is_free_index
3430 (cm->cli_file_pool, cm->current_input_file_index))
3431 mm = &cm->macro_main;
3432 else
3433 {
3434 /* Otherwise, use the per-cli-process macro table */
3435 cf = pool_elt_at_index (cm->cli_file_pool,
3436 cm->current_input_file_index);
3437 mm = &cf->macro_main;
3438 }
3439
3440 expanded = (u8 *) clib_macro_eval (mm,
3441 (i8 *) sub_input.buffer,
3442 1 /* complain */ ,
3443 0 /* level */ ,
3444 8 /* max_level */ );
3445 /* Macro processor NULL terminates the return */
3446 _vec_len (expanded) -= 1;
3447 vec_reset_length (sub_input.buffer);
3448 vec_append (sub_input.buffer, expanded);
3449 vec_free (expanded);
3450 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003451
Ray Kinsella457271d2021-05-25 15:55:52 +01003452 vlib_cli_input (vm, &sub_input, 0, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003453 unformat_free (&sub_input);
3454
Dave Barach9b8ffd92016-07-08 08:13:45 -04003455done:
Dave Baracha6ef36b2020-02-11 10:29:13 -05003456 if (fd >= 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003457 close (fd);
3458 vec_free (file_name);
3459
3460 return error;
3461}
3462
Chris Luke54ccf222016-07-25 16:38:11 -04003463/*?
Billy McFall28cf3b72018-01-15 17:54:52 -05003464 * Executes a sequence of CLI commands which are read from a file. If
3465 * a command is unrecognised or otherwise invalid then the usual CLI
Chris Luke54ccf222016-07-25 16:38:11 -04003466 * feedback will be generated, however execution of subsequent commands
3467 * from the file will continue.
Billy McFall28cf3b72018-01-15 17:54:52 -05003468 *
3469 * The VPP code is indifferent to the file location. However, if SELinux
3470 * is enabled, then the file needs to have an SELinux label the VPP
3471 * process is allowed to access. For example, if a file is created in
3472 * '<em>/usr/share/vpp/</em>', it will be allowed. However, files manually
3473 * created in '/tmp/' or '/home/<user>/' will not be accessible by the VPP
3474 * process when SELinux is enabled.
3475 *
3476 * @cliexpar
3477 * Sample file:
3478 * @clistart
3479 * <b><em>$ cat /usr/share/vpp/scripts/gigup.txt</em></b>
3480 * set interface state GigabitEthernet0/8/0 up
3481 * set interface state GigabitEthernet0/9/0 up
3482 * @cliend
3483 * Example of how to execute a set of CLI commands from a file:
3484 * @cliexcmd{exec /usr/share/vpp/scripts/gigup.txt}
Chris Luke54ccf222016-07-25 16:38:11 -04003485?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003486/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003487VLIB_CLI_COMMAND (cli_exec, static) = {
3488 .path = "exec",
Billy McFall28cf3b72018-01-15 17:54:52 -05003489 .short_help = "exec <filename>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003490 .function = unix_cli_exec,
Dave Barachb2ef4dd2016-03-23 08:56:01 -04003491 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003492};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003493/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003494
Chris Lukeb5850972016-05-03 16:34:59 -04003495/** CLI command to show various unix error statistics. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003496static clib_error_t *
3497unix_show_errors (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003498 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003499{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003500 unix_main_t *um = &unix_main;
3501 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003502 int i, n_errors_to_show;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003503 unix_error_history_t *unix_errors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003504
3505 n_errors_to_show = 1 << 30;
3506
3507 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3508 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003509 if (!unformat (input, "%d", &n_errors_to_show))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003510 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003511 error =
3512 clib_error_return (0,
3513 "expecting integer number of errors to show, got `%U'",
3514 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003515 goto done;
3516 }
3517 }
3518
Dave Barach9b8ffd92016-07-08 08:13:45 -04003519 n_errors_to_show =
3520 clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003521
Dave Barach9b8ffd92016-07-08 08:13:45 -04003522 i =
3523 um->error_history_index >
3524 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003525
3526 while (n_errors_to_show > 0)
3527 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003528 unix_error_history_t *eh = um->error_history + i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003529
Dave Barach9b8ffd92016-07-08 08:13:45 -04003530 if (!eh->error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003531 break;
3532
3533 vec_add1 (unix_errors, eh[0]);
3534 n_errors_to_show -= 1;
3535 if (i == 0)
3536 i = ARRAY_LEN (um->error_history) - 1;
3537 else
3538 i--;
3539 }
3540
3541 if (vec_len (unix_errors) == 0)
3542 vlib_cli_output (vm, "no Unix errors so far");
3543 else
3544 {
3545 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
3546 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
3547 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003548 unix_error_history_t *eh = vec_elt_at_index (unix_errors, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003549 vlib_cli_output (vm, "%U: %U",
3550 format_time_interval, "h:m:s:u", eh->time,
3551 format_clib_error, eh->error);
3552 }
3553 vlib_cli_output (vm, "%U: time now",
3554 format_time_interval, "h:m:s:u", vlib_time_now (vm));
3555 }
3556
Dave Barach9b8ffd92016-07-08 08:13:45 -04003557done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003558 vec_free (unix_errors);
3559 return error;
3560}
3561
Dave Barach9b8ffd92016-07-08 08:13:45 -04003562/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003563VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
Damjan Marionceab7882018-01-19 20:56:12 +01003564 .path = "show unix errors",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003565 .short_help = "Show Unix system call error history",
3566 .function = unix_show_errors,
3567};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003568/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003569
Damjan Marionceab7882018-01-19 20:56:12 +01003570/** CLI command to show various unix error statistics. */
3571static clib_error_t *
3572unix_show_files (vlib_main_t * vm,
3573 unformat_input_t * input, vlib_cli_command_t * cmd)
3574{
3575 clib_error_t *error = 0;
3576 clib_file_main_t *fm = &file_main;
3577 clib_file_t *f;
3578 char path[PATH_MAX];
3579 u8 *s = 0;
3580
3581 vlib_cli_output (vm, "%3s %6s %12s %12s %12s %-32s %s", "FD", "Thread",
3582 "Read", "Write", "Error", "File Name", "Description");
3583
3584 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01003585 pool_foreach (f, fm->file_pool)
Damjan Marionceab7882018-01-19 20:56:12 +01003586 {
3587 int rv;
3588 s = format (s, "/proc/self/fd/%d%c", f->file_descriptor, 0);
3589 rv = readlink((char *) s, path, PATH_MAX - 1);
3590
3591 path[rv < 0 ? 0 : rv] = 0;
3592
3593 vlib_cli_output (vm, "%3d %6d %12d %12d %12d %-32s %v",
3594 f->file_descriptor, f->polling_thread_index,
3595 f->read_events, f->write_events, f->error_events,
3596 path, f->description);
3597 vec_reset_length (s);
Damjan Marionb2c31b62020-12-13 21:47:40 +01003598 }
Damjan Marionceab7882018-01-19 20:56:12 +01003599 /* *INDENT-ON* */
3600 vec_free (s);
3601
3602 return error;
3603}
3604
3605/* *INDENT-OFF* */
3606VLIB_CLI_COMMAND (cli_unix_show_files, static) = {
3607 .path = "show unix files",
3608 .short_help = "Show Unix files in use",
3609 .function = unix_show_files,
3610};
3611/* *INDENT-ON* */
3612
Chris Lukeb5850972016-05-03 16:34:59 -04003613/** CLI command to show session command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003614static clib_error_t *
Chris Luke6b90fe32016-04-25 13:49:15 -04003615unix_cli_show_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003616 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke6b90fe32016-04-25 13:49:15 -04003617{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003618 unix_cli_main_t *cm = &unix_cli_main;
3619 unix_cli_file_t *cf;
Chris Luke6b90fe32016-04-25 13:49:15 -04003620 int i, j;
3621
Benoît Gannea58be822020-05-11 16:27:29 +02003622 if (!(cf = unix_cli_file_if_interactive (cm)))
Chris Luke03add7f2017-09-20 23:31:24 -04003623 return clib_error_return (0, "invalid for non-interactive sessions");
3624
Chris Luke7afda3a2016-04-25 13:49:22 -04003625 if (cf->has_history && cf->history_limit)
Chris Luke6b90fe32016-04-25 13:49:15 -04003626 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003627 i = 1 + cf->command_number - vec_len (cf->command_history);
Chris Luke7afda3a2016-04-25 13:49:22 -04003628 for (j = 0; j < vec_len (cf->command_history); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04003629 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
Chris Luke7afda3a2016-04-25 13:49:22 -04003630 }
3631 else
3632 {
3633 vlib_cli_output (vm, "History not enabled.\n");
Chris Luke6b90fe32016-04-25 13:49:15 -04003634 }
3635
3636 return 0;
3637}
3638
Chris Luke54ccf222016-07-25 16:38:11 -04003639/*?
3640 * Displays the command history for the current session, if any.
3641?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003642/* *INDENT-OFF* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003643VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
3644 .path = "history",
3645 .short_help = "Show current session command history",
3646 .function = unix_cli_show_history,
3647};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003648/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003649
Chris Lukeb5850972016-05-03 16:34:59 -04003650/** CLI command to show terminal status. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003651static clib_error_t *
3652unix_cli_show_terminal (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003653 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003654{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003655 unix_main_t *um = &unix_main;
3656 unix_cli_main_t *cm = &unix_cli_main;
3657 unix_cli_file_t *cf;
3658 vlib_node_t *n;
Chris Luke7afda3a2016-04-25 13:49:22 -04003659
Benoît Ganne10a22a62020-05-15 16:12:23 +02003660 if (!(cf = unix_cli_file_if_exists (cm)))
3661 return clib_error_return (0, "invalid session");
Benoît Gannea58be822020-05-11 16:27:29 +02003662
Chris Luke7afda3a2016-04-25 13:49:22 -04003663 n = vlib_get_node (vm, cf->process_node_index);
3664
3665 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003666 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
3667 "line-by-line" : "char-by-char");
Chris Luke7afda3a2016-04-25 13:49:22 -04003668 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
3669 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003670 vlib_cli_output (vm, "ANSI capable: %s\n",
3671 cf->ansi_capable ? "yes" : "no");
Chris Luke03add7f2017-09-20 23:31:24 -04003672 vlib_cli_output (vm, "Interactive: %s\n",
3673 cf->is_interactive ? "yes" : "no");
Chris Luke7afda3a2016-04-25 13:49:22 -04003674 vlib_cli_output (vm, "History enabled: %s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003675 cf->has_history ? "yes" : "no", !cf->has_history
3676 || cf->history_limit ? "" :
3677 " (disabled by history limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04003678 if (cf->has_history)
3679 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
3680 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003681 cf->no_pager ? "no" : "yes",
3682 cf->no_pager
3683 || cf->height ? "" : " (disabled by terminal height)",
3684 cf->no_pager
3685 || um->cli_pager_buffer_limit ? "" :
3686 " (disabled by buffer limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04003687 if (!cf->no_pager)
3688 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003689 vlib_cli_output (vm, "CRLF mode: %s\n",
3690 cf->crlf_mode ? "CR+LF" : "LF");
Chris Luke7afda3a2016-04-25 13:49:22 -04003691
3692 return 0;
3693}
3694
Chris Luke54ccf222016-07-25 16:38:11 -04003695/*?
3696 * Displays various information about the state of the current terminal
3697 * session.
3698 *
3699 * @cliexpar
3700 * @cliexstart{show terminal}
3701 * Terminal name: unix-cli-stdin
3702 * Terminal mode: char-by-char
3703 * Terminal width: 123
3704 * Terminal height: 48
3705 * ANSI capable: yes
Chris Luke03add7f2017-09-20 23:31:24 -04003706 * Interactive: yes
Chris Luke54ccf222016-07-25 16:38:11 -04003707 * History enabled: yes
3708 * History limit: 50
3709 * Pager enabled: yes
3710 * Pager limit: 100000
3711 * CRLF mode: LF
3712 * @cliexend
3713?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003714/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003715VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
3716 .path = "show terminal",
3717 .short_help = "Show current session terminal settings",
3718 .function = unix_cli_show_terminal,
3719};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003720/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003721
Chris Luke03add7f2017-09-20 23:31:24 -04003722/** CLI command to display a list of CLI sessions. */
3723static clib_error_t *
3724unix_cli_show_cli_sessions (vlib_main_t * vm,
3725 unformat_input_t * input,
3726 vlib_cli_command_t * cmd)
3727{
Chris Luke03add7f2017-09-20 23:31:24 -04003728 unix_cli_main_t *cm = &unix_cli_main;
3729 clib_file_main_t *fm = &file_main;
3730 unix_cli_file_t *cf;
3731 clib_file_t *uf;
3732 vlib_node_t *n;
3733
3734 vlib_cli_output (vm, "%-5s %-5s %-20s %s", "PNI", "FD", "Name", "Flags");
3735
3736#define fl(x, y) ( (x) ? toupper((y)) : tolower((y)) )
3737 /* *INDENT-OFF* */
Damjan Marionb2c31b62020-12-13 21:47:40 +01003738 pool_foreach (cf, cm->cli_file_pool) {
Chris Luke03add7f2017-09-20 23:31:24 -04003739 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
3740 n = vlib_get_node (vm, cf->process_node_index);
3741 vlib_cli_output (vm,
3742 "%-5d %-5d %-20v %c%c%c%c%c\n",
3743 cf->process_node_index,
3744 uf->file_descriptor,
3745 n->name,
3746 fl (cf->is_interactive, 'i'),
3747 fl (cf->is_socket, 's'),
3748 fl (cf->line_mode, 'l'),
3749 fl (cf->has_epipe, 'p'),
3750 fl (cf->ansi_capable, 'a'));
Damjan Marionb2c31b62020-12-13 21:47:40 +01003751 }
Chris Luke03add7f2017-09-20 23:31:24 -04003752 /* *INDENT-ON* */
3753#undef fl
3754
3755 return 0;
3756}
3757
3758/*?
3759 * Displays a summary of all the current CLI sessions.
3760 *
3761 * Typically used to diagnose connection issues with the CLI
3762 * socket.
3763 *
3764 * @cliexpar
3765 * @cliexstart{show cli-sessions}
3766 * PNI FD Name Flags
3767 * 343 0 unix-cli-stdin IslpA
3768 * 344 7 unix-cli-local:20 ISlpA
3769 * 346 8 unix-cli-local:21 iSLpa
3770 * @cliexend
3771
3772 * In this example we have the debug console of the running process
3773 * on stdin/out, we have an interactive socket session and we also
3774 * have a non-interactive socket session.
3775 *
3776 * Fields:
3777 *
3778 * - @em PNI: Process node index.
3779 * - @em FD: Unix file descriptor.
3780 * - @em Name: Name of the session.
3781 * - @em Flags: Various flags that describe the state of the session.
3782 *
3783 * @em Flags have the following meanings; lower-case typically negates
3784 * upper-case:
3785 *
3786 * - @em I Interactive session.
3787 * - @em S Connected by socket.
3788 * - @em s Not a socket, likely stdin.
3789 * - @em L Line-by-line mode.
3790 * - @em l Char-by-char mode.
3791 * - @em P EPIPE detected on connection; it will close soon.
3792 * - @em A ANSI-capable terminal.
3793?*/
3794/* *INDENT-OFF* */
3795VLIB_CLI_COMMAND (cli_unix_cli_show_cli_sessions, static) = {
3796 .path = "show cli-sessions",
3797 .short_help = "Show current CLI sessions",
3798 .function = unix_cli_show_cli_sessions,
3799};
3800/* *INDENT-ON* */
3801
Chris Lukeb5850972016-05-03 16:34:59 -04003802/** CLI command to set terminal pager settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003803static clib_error_t *
3804unix_cli_set_terminal_pager (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003805 unformat_input_t * input,
3806 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003807{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003808 unix_main_t *um = &unix_main;
3809 unix_cli_main_t *cm = &unix_cli_main;
3810 unix_cli_file_t *cf;
3811 unformat_input_t _line_input, *line_input = &_line_input;
Billy McFalla9a20e72017-02-15 11:39:12 -05003812 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003813
Benoît Gannea58be822020-05-11 16:27:29 +02003814 if (!(cf = unix_cli_file_if_interactive (cm)))
Chris Luke03add7f2017-09-20 23:31:24 -04003815 return clib_error_return (0, "invalid for non-interactive sessions");
3816
Dave Barach9b8ffd92016-07-08 08:13:45 -04003817 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04003818 return 0;
3819
Chris Luke7afda3a2016-04-25 13:49:22 -04003820 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3821 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003822 if (unformat (line_input, "on"))
3823 cf->no_pager = 0;
3824 else if (unformat (line_input, "off"))
3825 cf->no_pager = 1;
3826 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
3827 vlib_cli_output (vm,
3828 "Pager limit set to %u lines; note, this is global.\n",
3829 um->cli_pager_buffer_limit);
3830 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003831 {
3832 error = clib_error_return (0, "unknown parameter: `%U`",
3833 format_unformat_error, line_input);
3834 goto done;
3835 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04003836 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003837
Billy McFalla9a20e72017-02-15 11:39:12 -05003838done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003839 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003840
Billy McFalla9a20e72017-02-15 11:39:12 -05003841 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003842}
3843
Chris Luke54ccf222016-07-25 16:38:11 -04003844/*?
3845 * Enables or disables the terminal pager for this session. Generally
3846 * this defaults to enabled.
3847 *
3848 * Additionally allows the pager buffer size to be set; though note that
3849 * this value is set globally and not per session.
3850?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003851/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003852VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
3853 .path = "set terminal pager",
3854 .short_help = "set terminal pager [on|off] [limit <lines>]",
3855 .function = unix_cli_set_terminal_pager,
3856};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003857/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003858
Chris Lukeb5850972016-05-03 16:34:59 -04003859/** CLI command to set terminal history settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003860static clib_error_t *
3861unix_cli_set_terminal_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003862 unformat_input_t * input,
3863 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003864{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003865 unix_cli_main_t *cm = &unix_cli_main;
3866 unix_cli_file_t *cf;
3867 unformat_input_t _line_input, *line_input = &_line_input;
Chris Luke7afda3a2016-04-25 13:49:22 -04003868 u32 limit;
Billy McFalla9a20e72017-02-15 11:39:12 -05003869 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003870
Benoît Gannea58be822020-05-11 16:27:29 +02003871 if (!(cf = unix_cli_file_if_interactive (cm)))
Chris Luke03add7f2017-09-20 23:31:24 -04003872 return clib_error_return (0, "invalid for non-interactive sessions");
3873
Dave Barach9b8ffd92016-07-08 08:13:45 -04003874 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04003875 return 0;
3876
Chris Luke7afda3a2016-04-25 13:49:22 -04003877 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3878 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003879 if (unformat (line_input, "on"))
3880 cf->has_history = 1;
3881 else if (unformat (line_input, "off"))
3882 cf->has_history = 0;
3883 else if (unformat (line_input, "limit %u", &cf->history_limit))
3884 ;
3885 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003886 {
3887 error = clib_error_return (0, "unknown parameter: `%U`",
3888 format_unformat_error, line_input);
3889 goto done;
3890 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003891
Chris Luke5022c6c2019-05-17 10:17:16 -04003892 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003893
Chris Luke5022c6c2019-05-17 10:17:16 -04003894 /* If we reduced history size, or turned it off, purge the history */
3895 limit = cf->has_history ? cf->history_limit : 0;
3896 if (limit < vec_len (cf->command_history))
3897 {
3898 u32 i;
3899
3900 /* How many items to remove from the start of history */
3901 limit = vec_len (cf->command_history) - limit;
3902
3903 for (i = 0; i < limit; i++)
3904 vec_free (cf->command_history[i]);
3905
3906 vec_delete (cf->command_history, limit, 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003907 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003908
Billy McFalla9a20e72017-02-15 11:39:12 -05003909done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003910 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003911
Billy McFalla9a20e72017-02-15 11:39:12 -05003912 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003913}
3914
Chris Luke54ccf222016-07-25 16:38:11 -04003915/*?
3916 * Enables or disables the command history function of the current
3917 * terminal. Generally this defaults to enabled.
3918 *
3919 * This command also allows the maximum size of the history buffer for
3920 * this session to be altered.
3921?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003922/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003923VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
3924 .path = "set terminal history",
3925 .short_help = "set terminal history [on|off] [limit <lines>]",
3926 .function = unix_cli_set_terminal_history,
3927};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003928/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003929
Chris Lukeb5850972016-05-03 16:34:59 -04003930/** CLI command to set terminal ANSI settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003931static clib_error_t *
3932unix_cli_set_terminal_ansi (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003933 unformat_input_t * input,
3934 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003935{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003936 unix_cli_main_t *cm = &unix_cli_main;
3937 unix_cli_file_t *cf;
Chris Luke7afda3a2016-04-25 13:49:22 -04003938
Benoît Gannea58be822020-05-11 16:27:29 +02003939 if (!(cf = unix_cli_file_if_interactive (cm)))
Chris Luke03add7f2017-09-20 23:31:24 -04003940 return clib_error_return (0, "invalid for non-interactive sessions");
3941
Chris Luke7afda3a2016-04-25 13:49:22 -04003942 if (unformat (input, "on"))
3943 cf->ansi_capable = 1;
3944 else if (unformat (input, "off"))
3945 cf->ansi_capable = 0;
3946 else
3947 return clib_error_return (0, "unknown parameter: `%U`",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003948 format_unformat_error, input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003949
3950 return 0;
3951}
3952
Chris Luke54ccf222016-07-25 16:38:11 -04003953/*?
3954 * Enables or disables the use of ANSI control sequences by this terminal.
3955 * The default will vary based on terminal detection at the start of the
3956 * session.
3957 *
3958 * ANSI control sequences are used in a small number of places to provide,
3959 * for example, color text output and to control the cursor in the pager.
3960?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003961/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003962VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
3963 .path = "set terminal ansi",
3964 .short_help = "set terminal ansi [on|off]",
3965 .function = unix_cli_set_terminal_ansi,
3966};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003967/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003968
Paul Vinciguerrabfd7d292019-10-26 22:25:49 -04003969
3970#define MAX_CLI_WAIT 86400
3971/** CLI command to wait <sec> seconds. Useful for exec script. */
3972static clib_error_t *
3973unix_wait_cmd (vlib_main_t * vm,
3974 unformat_input_t * input, vlib_cli_command_t * cmd)
3975{
3976 unformat_input_t _line_input, *line_input = &_line_input;
3977 f64 sec = 1.0;
3978
3979 if (!unformat_user (input, unformat_line_input, line_input))
3980 return 0;
3981
3982 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3983 {
3984 if (unformat (line_input, "%f", &sec))
3985 ;
3986 else
3987 return clib_error_return (0, "unknown parameter: `%U`",
3988 format_unformat_error, input);
3989 }
3990
3991 if (sec <= 0 || sec > MAX_CLI_WAIT || floor (sec * 1000) / 1000 != sec)
3992 return clib_error_return (0,
3993 "<sec> must be a positive value and less than 86400 (one day) with no more than msec precision.");
3994
3995 vlib_process_wait_for_event_or_clock (vm, sec);
3996 vlib_cli_output (vm, "waited %.3f sec.", sec);
3997
3998 unformat_free (line_input);
3999 return 0;
4000}
4001/* *INDENT-OFF* */
4002VLIB_CLI_COMMAND (cli_unix_wait_cmd, static) = {
4003 .path = "wait",
4004 .short_help = "wait <sec>",
4005 .function = unix_wait_cmd,
4006};
4007/* *INDENT-ON* */
4008
Chris Luke6b90fe32016-04-25 13:49:15 -04004009static clib_error_t *
Dave Barach961e3c82020-06-18 17:04:18 -04004010echo_cmd (vlib_main_t * vm,
4011 unformat_input_t * input, vlib_cli_command_t * cmd)
4012{
4013 unformat_input_t _line_input, *line_input = &_line_input;
4014
4015 /* Get a line of input. */
4016 if (!unformat_user (input, unformat_line_input, line_input))
4017 {
4018 vlib_cli_output (vm, "");
4019 return 0;
4020 }
4021
4022 vlib_cli_output (vm, "%v", line_input->buffer);
4023
4024 unformat_free (line_input);
4025 return 0;
4026}
4027
4028/* *INDENT-OFF* */
4029VLIB_CLI_COMMAND (cli_unix_echo_cmd, static) = {
4030 .path = "echo",
4031 .short_help = "echo <rest-of-line>",
4032 .function = echo_cmd,
4033};
4034/* *INDENT-ON* */
4035
4036static clib_error_t *
4037define_cmd_fn (vlib_main_t * vm,
4038 unformat_input_t * input, vlib_cli_command_t * cmd)
4039{
4040 u8 *macro_name;
4041 unformat_input_t _line_input, *line_input = &_line_input;
Dave Barachb30b9542020-06-22 10:02:25 -04004042 clib_macro_main_t *mm = get_macro_main ();
Dave Barach961e3c82020-06-18 17:04:18 -04004043 clib_error_t *error;
4044
4045 if (!unformat (input, "%s", &macro_name))
4046 return clib_error_return (0, "missing variable name...");
4047
4048 /* Remove white space */
4049 (void) unformat (input, "");
4050
4051 /* Get a line of input. */
4052 if (!unformat_user (input, unformat_line_input, line_input))
4053 {
4054 error = clib_error_return (0, "missing value for '%s'...", macro_name);
4055 vec_free (macro_name);
4056 return error;
4057 }
4058 /* the macro expander expects c-strings, not vectors... */
4059 vec_add1 (line_input->buffer, 0);
Dave Barachb30b9542020-06-22 10:02:25 -04004060 clib_macro_set_value (mm, (char *) macro_name, (char *) line_input->buffer);
Dave Barach961e3c82020-06-18 17:04:18 -04004061 vec_free (macro_name);
4062 unformat_free (line_input);
4063 return 0;
4064}
4065
4066/* *INDENT-OFF* */
4067VLIB_CLI_COMMAND (define_cmd, static) = {
4068 .path = "define",
4069 .short_help = "define <variable-name> <value>",
4070 .function = define_cmd_fn,
4071};
4072
4073/* *INDENT-ON* */
4074
4075static clib_error_t *
4076undefine_cmd_fn (vlib_main_t * vm,
4077 unformat_input_t * input, vlib_cli_command_t * cmd)
4078{
4079 u8 *macro_name;
Dave Barachb30b9542020-06-22 10:02:25 -04004080 clib_macro_main_t *mm = get_macro_main ();
Dave Barach961e3c82020-06-18 17:04:18 -04004081
4082 if (!unformat (input, "%s", &macro_name))
4083 return clib_error_return (0, "missing variable name...");
4084
Dave Barachb30b9542020-06-22 10:02:25 -04004085 if (clib_macro_unset (mm, (char *) macro_name))
Dave Barach961e3c82020-06-18 17:04:18 -04004086 vlib_cli_output (vm, "%s wasn't set...", macro_name);
4087
4088 vec_free (macro_name);
4089 return 0;
4090}
4091
4092/* *INDENT-OFF* */
4093VLIB_CLI_COMMAND (undefine_cmd, static) = {
4094 .path = "undefine",
4095 .short_help = "undefine <variable-name>",
4096 .function = undefine_cmd_fn,
4097};
4098/* *INDENT-ON* */
4099
4100static clib_error_t *
4101show_macro_cmd_fn (vlib_main_t * vm,
4102 unformat_input_t * input, vlib_cli_command_t * cmd)
4103{
Dave Barachb30b9542020-06-22 10:02:25 -04004104 clib_macro_main_t *mm = get_macro_main ();
Dave Barach961e3c82020-06-18 17:04:18 -04004105 int evaluate = 1;
4106
4107 if (unformat (input, "noevaluate %=", &evaluate, 0))
4108 ;
4109 else if (unformat (input, "noeval %=", &evaluate, 0))
4110 ;
4111
Dave Barachb30b9542020-06-22 10:02:25 -04004112 vlib_cli_output (vm, "%U", format_clib_macro_main, mm, evaluate);
Dave Barach961e3c82020-06-18 17:04:18 -04004113 return 0;
4114}
4115
4116/* *INDENT-OFF* */
4117VLIB_CLI_COMMAND (show_macro, static) = {
4118 .path = "show macro",
4119 .short_help = "show macro [noevaluate]",
4120 .function = show_macro_cmd_fn,
4121};
4122/* *INDENT-ON* */
4123
4124static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07004125unix_cli_init (vlib_main_t * vm)
4126{
Chris Luke6a3a4f72019-07-09 23:33:30 -04004127 unix_cli_main_t *cm = &unix_cli_main;
4128
4129 /* Breadcrumb to indicate the new session process
4130 * has not been started */
4131 cm->new_session_process_node_index = ~0;
Dave Barach961e3c82020-06-18 17:04:18 -04004132 clib_macro_init (&cm->macro_main);
Dave Barachb30b9542020-06-22 10:02:25 -04004133
Ed Warnickecb9cada2015-12-08 15:45:58 -07004134 return 0;
4135}
4136
4137VLIB_INIT_FUNCTION (unix_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04004138
4139/*
4140 * fd.io coding-style-patch-verification: ON
4141 *
4142 * Local Variables:
4143 * eval: (c-set-style "gnu")
4144 * End:
4145 */