blob: fa61c6964bea786fb7e36fa97550889c2e307c5a [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070064
Chris Lukeb5850972016-05-03 16:34:59 -040065/** ANSI escape code. */
Chris Luke0aca5eb2016-04-25 13:48:54 -040066#define ESC "\x1b"
67
Chris Lukeb5850972016-05-03 16:34:59 -040068/** ANSI Control Sequence Introducer. */
Chris Luke0aca5eb2016-04-25 13:48:54 -040069#define CSI ESC "["
70
Chris Lukeb5850972016-05-03 16:34:59 -040071/** ANSI clear screen. */
Chris Luke7afda3a2016-04-25 13:49:22 -040072#define ANSI_CLEAR CSI "2J" CSI "1;1H"
Chris Lukeb5850972016-05-03 16:34:59 -040073/** ANSI reset color settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -040074#define ANSI_RESET CSI "0m"
Chris Lukeb5850972016-05-03 16:34:59 -040075/** ANSI Start bold text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040076#define ANSI_BOLD CSI "1m"
Chris Lukeb5850972016-05-03 16:34:59 -040077/** ANSI Stop bold text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040078#define ANSI_DIM CSI "2m"
Chris Lukeb5850972016-05-03 16:34:59 -040079/** ANSI Start dark red text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040080#define ANSI_DRED ANSI_DIM CSI "31m"
Chris Lukeb5850972016-05-03 16:34:59 -040081/** ANSI Start bright red text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040082#define ANSI_BRED ANSI_BOLD CSI "31m"
Chris Lukeb5850972016-05-03 16:34:59 -040083/** ANSI clear line cursor is on. */
Chris Luke7afda3a2016-04-25 13:49:22 -040084#define ANSI_CLEARLINE CSI "2K"
Chris Lukeb5850972016-05-03 16:34:59 -040085/** ANSI scroll screen down one line. */
Chris Luke7afda3a2016-04-25 13:49:22 -040086#define ANSI_SCROLLDN CSI "1T"
Chris Lukeb5850972016-05-03 16:34:59 -040087/** ANSI save cursor position. */
Chris Luke7afda3a2016-04-25 13:49:22 -040088#define ANSI_SAVECURSOR CSI "s"
Chris Lukeb5850972016-05-03 16:34:59 -040089/** ANSI restore cursor position if previously saved. */
Chris Luke7afda3a2016-04-25 13:49:22 -040090#define ANSI_RESTCURSOR CSI "u"
Chris Luke0aca5eb2016-04-25 13:48:54 -040091
92/** Maximum depth into a byte stream from which to compile a Telnet
Chris Luke2d8bf302017-11-12 22:26:37 -050093 * protocol message. This is a safety measure. */
Chris Luke1aed76f2016-04-29 08:14:38 -040094#define UNIX_CLI_MAX_DEPTH_TELNET 24
Chris Luke0aca5eb2016-04-25 13:48:54 -040095
Chris Lukeb2bcad62017-09-18 08:51:22 -040096/** Maximum terminal width we will accept */
97#define UNIX_CLI_MAX_TERMINAL_WIDTH 512
Chris Lukeb2bcad62017-09-18 08:51:22 -040098/** Maximum terminal height we will accept */
99#define UNIX_CLI_MAX_TERMINAL_HEIGHT 512
Chris Luke2d8bf302017-11-12 22:26:37 -0500100/** Default terminal height */
101#define UNIX_CLI_DEFAULT_TERMINAL_HEIGHT 24
102/** Default terminal width */
103#define UNIX_CLI_DEFAULT_TERMINAL_WIDTH 80
Chris Luke0aca5eb2016-04-25 13:48:54 -0400104
Chris Lukeb5850972016-05-03 16:34:59 -0400105/** A CLI banner line. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400106typedef struct
107{
108 u8 *line; /**< The line to print. */
109 u32 length; /**< The length of the line without terminating NUL. */
Chris Luke572d8122016-04-25 13:49:07 -0400110} unix_cli_banner_t;
111
112#define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
113/** Plain welcome banner. */
114static unix_cli_banner_t unix_cli_banner[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400115 _(" _______ _ _ _____ ___ \n"),
116 _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
117 _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
118 _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
119 _("\n")
Chris Luke572d8122016-04-25 13:49:07 -0400120};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400121
Chris Luke572d8122016-04-25 13:49:07 -0400122/** ANSI color welcome banner. */
123static unix_cli_banner_t unix_cli_banner_color[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400124 _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
125 _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
126 _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
127 _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"),
128 _("\n")
Chris Luke572d8122016-04-25 13:49:07 -0400129};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400130
Chris Luke572d8122016-04-25 13:49:07 -0400131#undef _
132
Chris Luke862623d2016-05-14 10:13:34 -0400133/** Pager line index */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400134typedef struct
135{
Chris Luke862623d2016-05-14 10:13:34 -0400136 /** Index into pager_vector */
137 u32 line;
138
139 /** Offset of the string in the line */
140 u32 offset;
141
142 /** Length of the string in the line */
143 u32 length;
144} unix_cli_pager_index_t;
145
Chris Luke572d8122016-04-25 13:49:07 -0400146
Chris Luke0aca5eb2016-04-25 13:48:54 -0400147/** Unix CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400148typedef struct
149{
Chris Lukeb5850972016-05-03 16:34:59 -0400150 /** The file index held by unix.c */
Damjan Marion56dd5432017-09-08 19:52:02 +0200151 u32 clib_file_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Chris Lukeb5850972016-05-03 16:34:59 -0400153 /** Vector of output pending write to file descriptor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400154 u8 *output_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
Chris Lukeb5850972016-05-03 16:34:59 -0400156 /** Vector of input saved by Unix input node to be processed by
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 CLI process. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400158 u8 *input_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Chris Luke54ccf222016-07-25 16:38:11 -0400160 /** This session has command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 u8 has_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400162 /** Array of vectors of commands in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400163 u8 **command_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400164 /** The command currently pointed at by the history cursor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400165 u8 *current_command;
Chris Luke54ccf222016-07-25 16:38:11 -0400166 /** How far from the end of the history array the user has browsed. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 i32 excursion;
Chris Luke6b90fe32016-04-25 13:49:15 -0400168
Chris Lukeb5850972016-05-03 16:34:59 -0400169 /** Maximum number of history entries this session will store. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170 u32 history_limit;
Chris Luke6b90fe32016-04-25 13:49:15 -0400171
Chris Lukeb5850972016-05-03 16:34:59 -0400172 /** Current command line counter */
Chris Luke6b90fe32016-04-25 13:49:15 -0400173 u32 command_number;
174
Chris Luke54ccf222016-07-25 16:38:11 -0400175 /** The string being searched for in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176 u8 *search_key;
Chris Luke54ccf222016-07-25 16:38:11 -0400177 /** If non-zero then the CLI is searching in the history array.
178 * - @c -1 means search backwards.
179 * - @c 1 means search forwards.
180 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 int search_mode;
182
Chris Lukeb5850972016-05-03 16:34:59 -0400183 /** Position of the insert cursor on the current input line */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400184 u32 cursor;
185
Chris Lukeb5850972016-05-03 16:34:59 -0400186 /** Line mode or char mode */
Chris Luke7afda3a2016-04-25 13:49:22 -0400187 u8 line_mode;
188
Chris Lukeb5850972016-05-03 16:34:59 -0400189 /** Set if the CRLF mode wants CR + LF */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400190 u8 crlf_mode;
191
Chris Lukeb5850972016-05-03 16:34:59 -0400192 /** Can we do ANSI output? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400193 u8 ansi_capable;
194
Chris Lukeb5850972016-05-03 16:34:59 -0400195 /** Has the session started? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400196 u8 started;
197
Chris Lukeb5850972016-05-03 16:34:59 -0400198 /** Disable the pager? */
Chris Luke7afda3a2016-04-25 13:49:22 -0400199 u8 no_pager;
200
Chris Luke03add7f2017-09-20 23:31:24 -0400201 /** Whether the session is interactive or not.
202 * Controls things like initial banner, the CLI prompt etc. */
203 u8 is_interactive;
204
205 /** Whether the session is attached to a socket. */
206 u8 is_socket;
207
208 /** If EPIPE has been detected, prevent further write-related
209 * activity on the descriptor.
210 */
211 u8 has_epipe;
212
Chris Lukeb5850972016-05-03 16:34:59 -0400213 /** Pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400214 u8 **pager_vector;
Chris Luke7afda3a2016-04-25 13:49:22 -0400215
Chris Luke862623d2016-05-14 10:13:34 -0400216 /** Index of line fragments in the pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400217 unix_cli_pager_index_t *pager_index;
Chris Luke7afda3a2016-04-25 13:49:22 -0400218
Chris Lukeb5850972016-05-03 16:34:59 -0400219 /** Line number of top of page */
Chris Luke7afda3a2016-04-25 13:49:22 -0400220 u32 pager_start;
221
Chris Lukeb5850972016-05-03 16:34:59 -0400222 /** Terminal width */
223 u32 width;
Chris Luke7afda3a2016-04-25 13:49:22 -0400224
Chris Lukeb5850972016-05-03 16:34:59 -0400225 /** Terminal height */
226 u32 height;
227
228 /** Process node identifier */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229 u32 process_node_index;
Chris Lukefc379d22018-06-05 23:50:19 -0400230
231 /** The current direction of cursor travel.
232 * This is important since when advancing left-to-right, at the
233 * right hand edge of the console the terminal typically defers
234 * wrapping the cursor to the next line until a character is
235 * actually displayed.
236 * This messes up our heuristic for whether to use ANSI to return
237 * the cursor to the end of the line and instead we have to
238 * nudge the cursor to the next line.
239 * A Value of @c 0 means we're advancing left-to-right; @c 1 means
240 * the opposite.
241 */
242 u8 cursor_direction;
243
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244} unix_cli_file_t;
245
Chris Lukeb5850972016-05-03 16:34:59 -0400246/** Resets the pager buffer and other data.
247 * @param f The CLI session whose pager needs to be reset.
248 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400250unix_cli_pager_reset (unix_cli_file_t * f)
Chris Luke7afda3a2016-04-25 13:49:22 -0400251{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400252 u8 **p;
Chris Luke7afda3a2016-04-25 13:49:22 -0400253
Chris Luke862623d2016-05-14 10:13:34 -0400254 f->pager_start = 0;
255
256 vec_free (f->pager_index);
257 f->pager_index = 0;
258
Chris Luke7afda3a2016-04-25 13:49:22 -0400259 vec_foreach (p, f->pager_vector)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400260 {
261 vec_free (*p);
262 }
Chris Luke862623d2016-05-14 10:13:34 -0400263 vec_free (f->pager_vector);
Chris Luke7afda3a2016-04-25 13:49:22 -0400264 f->pager_vector = 0;
265}
266
Chris Lukeb5850972016-05-03 16:34:59 -0400267/** Release storage used by a CLI session.
268 * @param f The CLI session whose storage needs to be released.
269 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400270always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271unix_cli_file_free (unix_cli_file_t * f)
272{
273 vec_free (f->output_vector);
274 vec_free (f->input_vector);
Chris Luke862623d2016-05-14 10:13:34 -0400275 unix_cli_pager_reset (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276}
277
Chris Lukeb5850972016-05-03 16:34:59 -0400278/** CLI actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400279typedef enum
280{
Chris Luke54ccf222016-07-25 16:38:11 -0400281 UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */
282 UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */
283 UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */
284 UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */
285 UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */
286 UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */
287 UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */
288 UNIX_CLI_PARSE_ACTION_LEFT, /**< Left arrow */
289 UNIX_CLI_PARSE_ACTION_RIGHT, /**< Right arrow */
290 UNIX_CLI_PARSE_ACTION_HOME, /**< Home key (jump to start of line) */
291 UNIX_CLI_PARSE_ACTION_END, /**< End key (jump to end of line) */
292 UNIX_CLI_PARSE_ACTION_WORDLEFT, /**< Jump cursor to start of left word */
293 UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */
294 UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */
295 UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */
296 UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */
297 UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */
298 UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */
299 UNIX_CLI_PARSE_ACTION_YANK, /**< Undo last erase action */
300 UNIX_CLI_PARSE_ACTION_TELNETIAC, /**< Telnet control code */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400301
Chris Luke54ccf222016-07-25 16:38:11 -0400302 UNIX_CLI_PARSE_ACTION_PAGER_CRLF, /**< Enter pressed (CR, CRLF, LF, etc) */
303 UNIX_CLI_PARSE_ACTION_PAGER_QUIT, /**< Exit the pager session */
304 UNIX_CLI_PARSE_ACTION_PAGER_NEXT, /**< Scroll to next page */
305 UNIX_CLI_PARSE_ACTION_PAGER_DN, /**< Scroll to next line */
306 UNIX_CLI_PARSE_ACTION_PAGER_UP, /**< Scroll to previous line */
307 UNIX_CLI_PARSE_ACTION_PAGER_TOP, /**< Scroll to first line */
308 UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM, /**< Scroll to last line */
309 UNIX_CLI_PARSE_ACTION_PAGER_PGDN, /**< Scroll to next page */
310 UNIX_CLI_PARSE_ACTION_PAGER_PGUP, /**< Scroll to previous page */
311 UNIX_CLI_PARSE_ACTION_PAGER_REDRAW, /**< Clear and redraw the page on the terminal */
312 UNIX_CLI_PARSE_ACTION_PAGER_SEARCH, /**< Search the pager buffer */
Chris Luke7afda3a2016-04-25 13:49:22 -0400313
Chris Luke54ccf222016-07-25 16:38:11 -0400314 UNIX_CLI_PARSE_ACTION_PARTIALMATCH, /**< Action parser found a partial match */
315 UNIX_CLI_PARSE_ACTION_NOMATCH /**< Action parser did not find any match */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400316} unix_cli_parse_action_t;
317
Chris Luke8e5b0412016-07-26 13:06:10 -0400318/** @brief Mapping of input buffer strings to action values.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400319 * @note This won't work as a hash since we need to be able to do
320 * partial matches on the string.
321 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400322typedef struct
323{
324 u8 *input; /**< Input string to match. */
325 u32 len; /**< Length of input without final NUL. */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400326 unix_cli_parse_action_t action; /**< Action to take when matched. */
327} unix_cli_parse_actions_t;
328
Chris Lukeb5850972016-05-03 16:34:59 -0400329/** @brief Given a capital ASCII letter character return a @c NUL terminated
Chris Luke0aca5eb2016-04-25 13:48:54 -0400330 * string with the control code for that letter.
Chris Lukeb5850972016-05-03 16:34:59 -0400331 *
332 * @param c An ASCII character.
333 * @return A @c NUL terminated string of type @c u8[].
334 *
335 * @par Example
336 * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[].
Chris Luke0aca5eb2016-04-25 13:48:54 -0400337 */
338#define CTL(c) (u8[]){ (c) - '@', 0 }
339
340#define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
Chris Lukeb5850972016-05-03 16:34:59 -0400341/**
342 * Patterns to match on a CLI input stream.
343 * @showinitializer
344 */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400345static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400346 /* Line handling */
347 _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */
348 _("\n", UNIX_CLI_PARSE_ACTION_CRLF),
349 _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */
350 _("\r", UNIX_CLI_PARSE_ACTION_CRLF),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400351
Dave Barach9b8ffd92016-07-08 08:13:45 -0400352 /* Unix shell control codes */
353 _(CTL ('B'), UNIX_CLI_PARSE_ACTION_LEFT),
354 _(CTL ('F'), UNIX_CLI_PARSE_ACTION_RIGHT),
355 _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP),
356 _(CTL ('N'), UNIX_CLI_PARSE_ACTION_DOWN),
357 _(CTL ('A'), UNIX_CLI_PARSE_ACTION_HOME),
358 _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END),
359 _(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT),
360 _(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT),
361 _(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT),
362 _(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK),
363 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR),
364 _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */
365 _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */
366 _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */
367 _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */
368 _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400369
Dave Barach9b8ffd92016-07-08 08:13:45 -0400370 /* VT100 Normal mode - Broadest support */
371 _(CSI "A", UNIX_CLI_PARSE_ACTION_UP),
372 _(CSI "B", UNIX_CLI_PARSE_ACTION_DOWN),
373 _(CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT),
374 _(CSI "D", UNIX_CLI_PARSE_ACTION_LEFT),
375 _(CSI "H", UNIX_CLI_PARSE_ACTION_HOME),
376 _(CSI "F", UNIX_CLI_PARSE_ACTION_END),
377 _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */
378 _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */
379 _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400380
381 /* VT100 Application mode - Some Gnome Terminal functions use these */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400382 _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP),
383 _(ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN),
384 _(ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT),
385 _(ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT),
386 _(ESC "OH", UNIX_CLI_PARSE_ACTION_HOME),
387 _(ESC "OF", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400388
Dave Barach9b8ffd92016-07-08 08:13:45 -0400389 /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
390 _(CSI "1~", UNIX_CLI_PARSE_ACTION_HOME),
391 _(CSI "4~", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400392
Dave Barach9b8ffd92016-07-08 08:13:45 -0400393 /* Emacs-ish history search */
394 _(CTL ('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH),
395 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400396
Dave Barach9b8ffd92016-07-08 08:13:45 -0400397 /* Other protocol things */
398 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
399 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
400 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400401};
Chris Lukeb5850972016-05-03 16:34:59 -0400402
403/**
404 * Patterns to match when a CLI session is in the pager.
405 * @showinitializer
406 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400407static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400408 /* Line handling */
409 _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */
410 _("\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
411 _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */
412 _("\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
Chris Luke7afda3a2016-04-25 13:49:22 -0400413
Dave Barach9b8ffd92016-07-08 08:13:45 -0400414 /* Pager commands */
415 _(" ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT),
416 _("q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT),
417 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
418 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
419 _("/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH),
Chris Luke7afda3a2016-04-25 13:49:22 -0400420
Dave Barach9b8ffd92016-07-08 08:13:45 -0400421 /* VT100 */
422 _(CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP),
423 _(CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN),
424 _(CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
425 _(CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400426
Dave Barach9b8ffd92016-07-08 08:13:45 -0400427 /* VT100 Application mode */
428 _(ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP),
429 _(ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN),
430 _(ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
431 _(ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400432
Dave Barach9b8ffd92016-07-08 08:13:45 -0400433 /* ANSI X3.41-1974 */
434 _(CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
435 _(CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
436 _(CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP),
437 _(CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN),
Chris Luke7afda3a2016-04-25 13:49:22 -0400438
Dave Barach9b8ffd92016-07-08 08:13:45 -0400439 /* Other protocol things */
440 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
441 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
442 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke7afda3a2016-04-25 13:49:22 -0400443};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444
Chris Luke0aca5eb2016-04-25 13:48:54 -0400445#undef _
446
Chris Lukeb5850972016-05-03 16:34:59 -0400447/** CLI session events. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400448typedef enum
449{
Chris Lukeb5850972016-05-03 16:34:59 -0400450 UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400451 UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400452} unix_cli_process_event_type_t;
453
Chris Luke6a3a4f72019-07-09 23:33:30 -0400454/** CLI session telnet negotiation timer events. */
455typedef enum
456{
457 UNIX_CLI_NEW_SESSION_EVENT_ADD, /**< Add a CLI session to the new session list */
458} unix_cli_timeout_event_type_t;
459
460/** Each new session is stored on a list with a deadline after which
461 * a prompt is issued, in case the session TELNET negotiation fails to
462 * complete. */
463typedef struct
464{
465 uword cf_index; /**< Session index of the new session. */
466 f64 deadline; /**< Deadline after which the new session must have a prompt. */
467} unix_cli_new_session_t;
468
Chris Lukeb5850972016-05-03 16:34:59 -0400469/** CLI global state. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400470typedef struct
471{
Chris Lukeb5850972016-05-03 16:34:59 -0400472 /** Prompt string for CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400473 u8 *cli_prompt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474
Chris Lukeb5850972016-05-03 16:34:59 -0400475 /** Vec pool of CLI sessions. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400476 unix_cli_file_t *cli_file_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477
Chris Lukeb5850972016-05-03 16:34:59 -0400478 /** Vec pool of unused session indices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400479 u32 *unused_cli_process_node_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700480
Chris Lukeb5850972016-05-03 16:34:59 -0400481 /** The session index of the stdin cli */
Chris Luke7afda3a2016-04-25 13:49:22 -0400482 u32 stdin_cli_file_index;
483
Chris Lukeb5850972016-05-03 16:34:59 -0400484 /** File pool index of current input. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 u32 current_input_file_index;
Chris Luke6a3a4f72019-07-09 23:33:30 -0400486
487 /** New session process node identifier */
488 u32 new_session_process_node_index;
489
490 /** List of new sessions */
491 unix_cli_new_session_t *new_sessions;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492} unix_cli_main_t;
493
Chris Lukeb5850972016-05-03 16:34:59 -0400494/** CLI global state */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495static unix_cli_main_t unix_cli_main;
496
Chris Luke0aca5eb2016-04-25 13:48:54 -0400497/**
Chris Luke8e5b0412016-07-26 13:06:10 -0400498 * @brief Search for a byte sequence in the action list.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400499 *
Chris Lukeb5850972016-05-03 16:34:59 -0400500 * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with
501 * the bytes in @a input of maximum length @a ilen bytes.
502 * When a match is made @a *matched indicates how many bytes were matched.
503 * Returns a value from the enum @ref unix_cli_parse_action_t to indicate
504 * whether no match was found, a partial match was found or a complete
505 * match was found and what action, if any, should be taken.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400506 *
Chris Lukeb5850972016-05-03 16:34:59 -0400507 * @param[in] a Actions list to search within.
508 * @param[in] input String fragment to search for.
509 * @param[in] ilen Length of the string in 'input'.
510 * @param[out] matched Pointer to an integer that will contain the number
511 * of bytes matched when a complete match is found.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400512 *
Chris Lukeb5850972016-05-03 16:34:59 -0400513 * @return Action from @ref unix_cli_parse_action_t that the string fragment
Chris Luke0aca5eb2016-04-25 13:48:54 -0400514 * matches.
Chris Lukeb5850972016-05-03 16:34:59 -0400515 * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the
516 * whole input string matches the start of at least one action.
517 * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
Chris Luke0aca5eb2016-04-25 13:48:54 -0400518 * match at all.
519 */
520static unix_cli_parse_action_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400521unix_cli_match_action (unix_cli_parse_actions_t * a,
522 u8 * input, u32 ilen, i32 * matched)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400523{
Chris Luke0aca5eb2016-04-25 13:48:54 -0400524 u8 partial = 0;
525
526 while (a->input)
527 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400528 if (ilen >= a->len)
529 {
530 /* see if the start of the input buffer exactly matches the current
531 * action string. */
532 if (memcmp (input, a->input, a->len) == 0)
533 {
534 *matched = a->len;
535 return a->action;
536 }
537 }
538 else
539 {
540 /* if the first ilen characters match, flag this as a partial -
541 * meaning keep collecting bytes in case of a future match */
542 if (memcmp (input, a->input, ilen) == 0)
543 partial = 1;
544 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400545
Dave Barach9b8ffd92016-07-08 08:13:45 -0400546 /* check next action */
547 a++;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400548 }
549
550 return partial ?
Dave Barach9b8ffd92016-07-08 08:13:45 -0400551 UNIX_CLI_PARSE_ACTION_PARTIALMATCH : UNIX_CLI_PARSE_ACTION_NOMATCH;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400552}
553
554
Chris Luke54ccf222016-07-25 16:38:11 -0400555/** Add bytes to the output vector and then flagg the I/O system that bytes
556 * are available to be sent.
557 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200559unix_cli_add_pending_output (clib_file_t * uf,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560 unix_cli_file_t * cf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400561 u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562{
Damjan Marion56dd5432017-09-08 19:52:02 +0200563 clib_file_main_t *fm = &file_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564
565 vec_add (cf->output_vector, buffer, buffer_bytes);
566 if (vec_len (cf->output_vector) > 0)
567 {
568 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
569 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400570 if (!skip_update)
Damjan Marion56dd5432017-09-08 19:52:02 +0200571 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 }
573}
574
Chris Luke54ccf222016-07-25 16:38:11 -0400575/** Delete all bytes from the output vector and flag the I/O system
576 * that no more bytes are available to be sent.
577 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200579unix_cli_del_pending_output (clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400580 unix_cli_file_t * cf, uword n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581{
Damjan Marion56dd5432017-09-08 19:52:02 +0200582 clib_file_main_t *fm = &file_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583
584 vec_delete (cf->output_vector, n_bytes, 0);
585 if (vec_len (cf->output_vector) <= 0)
586 {
587 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
588 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400589 if (!skip_update)
Damjan Marion56dd5432017-09-08 19:52:02 +0200590 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591 }
592}
593
Chris Luke8e5b0412016-07-26 13:06:10 -0400594/** @brief A bit like strchr with a buffer length limit.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400595 * Search a buffer for the first instance of a character up to the limit of
596 * the buffer length. If found then return the position of that character.
597 *
598 * The key departure from strchr is that if the character is not found then
599 * return the buffer length.
600 *
601 * @param chr The byte value to search for.
602 * @param str The buffer in which to search for the value.
603 * @param len The depth into the buffer to search.
604 *
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700605 * @return The index of the first occurrence of \c chr. If \c chr is not
Chris Luke0aca5eb2016-04-25 13:48:54 -0400606 * found then \c len instead.
607 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400608always_inline word
609unix_vlib_findchr (u8 chr, u8 * str, word len)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400610{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400611 word i = 0;
612 for (i = 0; i < len; i++, str++)
613 {
614 if (*str == chr)
615 return i;
616 }
617 return len;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400618}
619
Chris Luke8e5b0412016-07-26 13:06:10 -0400620/** @brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400621 * Attempts to write given buffer to the file descriptor of the given
622 * Unix CLI session. If that session already has data in the output buffer
623 * or if the write attempt tells us to try again later then the given buffer
624 * is appended to the pending output buffer instead.
625 *
626 * This is typically called only from \c unix_vlib_cli_output_cooked since
627 * that is where CRLF handling occurs or from places where we explicitly do
628 * not want cooked handling.
629 *
630 * @param cf Unix CLI session of the desired stream to write to.
631 * @param uf The Unix file structure of the desired stream to write to.
632 * @param buffer Pointer to the buffer that needs to be written.
633 * @param buffer_bytes The number of bytes from \c buffer to write.
634 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400635static void
636unix_vlib_cli_output_raw (unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +0200637 clib_file_t * uf, u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400638{
639 int n = 0;
640
Chris Luke03add7f2017-09-20 23:31:24 -0400641 if (cf->has_epipe) /* don't try writing anything */
642 return;
643
Chris Luke0aca5eb2016-04-25 13:48:54 -0400644 if (vec_len (cf->output_vector) == 0)
Chris Luke03add7f2017-09-20 23:31:24 -0400645 {
646 if (cf->is_socket)
647 /* If it's a socket we use MSG_NOSIGNAL to prevent SIGPIPE */
648 n = send (uf->file_descriptor, buffer, buffer_bytes, MSG_NOSIGNAL);
649 else
650 n = write (uf->file_descriptor, buffer, buffer_bytes);
651 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400652
653 if (n < 0 && errno != EAGAIN)
654 {
Chris Luke03add7f2017-09-20 23:31:24 -0400655 if (errno == EPIPE)
656 {
657 /* connection closed on us */
658 unix_main_t *um = &unix_main;
659 cf->has_epipe = 1;
660 vlib_process_signal_event (um->vlib_main, cf->process_node_index,
661 UNIX_CLI_PROCESS_EVENT_QUIT,
662 uf->private_data);
663 }
664 else
665 {
666 clib_unix_warning ("write");
667 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400668 }
669 else if ((word) n < (word) buffer_bytes)
670 {
671 /* We got EAGAIN or we already have stuff in the buffer;
672 * queue up whatever didn't get sent for later. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400673 if (n < 0)
674 n = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400675 unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
676 }
677}
678
Chris Luke8e5b0412016-07-26 13:06:10 -0400679/** @brief Process a buffer for CRLF handling before outputting it to the CLI.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400680 *
681 * @param cf Unix CLI session of the desired stream to write to.
682 * @param uf The Unix file structure of the desired stream to write to.
683 * @param buffer Pointer to the buffer that needs to be written.
684 * @param buffer_bytes The number of bytes from \c buffer to write.
685 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400686static void
687unix_vlib_cli_output_cooked (unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +0200688 clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400689 u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400690{
691 word end = 0, start = 0;
692
693 while (end < buffer_bytes)
694 {
695 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400696 {
697 /* iterate the line on \n's so we can insert a \r before it */
698 end = unix_vlib_findchr ('\n',
699 buffer + start,
700 buffer_bytes - start) + start;
701 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400702 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400703 {
704 /* otherwise just send the whole buffer */
705 end = buffer_bytes;
706 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400707
Dave Barach9b8ffd92016-07-08 08:13:45 -0400708 unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start);
Chris Luke0aca5eb2016-04-25 13:48:54 -0400709
710 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400711 {
712 if (end < buffer_bytes)
713 {
714 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2);
715 end++; /* skip the \n that we already sent */
716 }
717 start = end;
718 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400719 }
Chris Lukefc379d22018-06-05 23:50:19 -0400720
721 /* Use the last character to determine the last direction of the cursor. */
722 if (buffer_bytes > 0)
723 cf->cursor_direction = (buffer[buffer_bytes - 1] == (u8) '\b');
724}
725
726/** @brief Moves the terminal cursor one character to the left, with
727 * special handling when it reaches the left edge of the terminal window.
728 *
729 * Ordinarily we can simply send a '\b' to move the cursor left, however
730 * most terminals will not reverse-wrap to the end of the previous line
731 * if the cursor is in the left-most column. To counter this we must
732 * check the cursor position + prompt length modulo terminal width and
733 * if available use some other means, such as ANSI terminal escape
734 * sequences, to move the cursor.
735 *
736 * @param cf Unix CLI session of the desired stream to write to.
737 * @param uf The Unix file structure of the desired stream to write to.
738 */
739static void
740unix_vlib_cli_output_cursor_left (unix_cli_file_t * cf, clib_file_t * uf)
741{
742 unix_cli_main_t *cm = &unix_cli_main;
743 static u8 *ansi = 0; /* assumes no reentry */
744 u32 position;
745
746 if (!cf->is_interactive || !cf->ansi_capable || !cf->width)
747 {
748 /* No special handling for dumb terminals */
749 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
750 return;
751 }
752
753 position = ((u32) vec_len (cm->cli_prompt) + cf->cursor) % cf->width;
754
755 if (position != 0)
756 {
757 /* No special handling required if we're not at the left edge */
758 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
759 return;
760 }
761
762 if (!cf->cursor_direction)
763 {
764 /* Special handling for when we are at the left edge but
765 * the cursor was going left-to-right, but in this situation
766 * xterm-like terminals actually hide the cursor off the right
767 * edge. A \b here seems to jump one char too many, so let's
768 * force the cursor onto the next line instead.
769 */
770 if (cf->cursor < vec_len (cf->current_command))
771 unix_vlib_cli_output_cooked (cf, uf, &cf->current_command[cf->cursor],
772 1);
773 else
774 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
775 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
776 }
777
778 /* Relocate the cursor at the right hand edge one line above */
779 ansi = format (ansi, CSI "A" CSI "%dC", cf->width - 1);
780 unix_vlib_cli_output_cooked (cf, uf, ansi, vec_len (ansi));
781 vec_reset_length (ansi); /* keep the vec around for next time */
782 cf->cursor_direction = 1; /* going backwards now */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400783}
784
Chris Luke8e5b0412016-07-26 13:06:10 -0400785/** @brief Output the CLI prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400786static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200787unix_cli_cli_prompt (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400788{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400789 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke7afda3a2016-04-25 13:49:22 -0400790
Chris Luke03add7f2017-09-20 23:31:24 -0400791 if (cf->is_interactive) /* Only interactive sessions get a prompt */
792 unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt,
793 vec_len (cm->cli_prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400794}
795
Chris Luke8e5b0412016-07-26 13:06:10 -0400796/** @brief Output a pager prompt and show number of buffered lines */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400797static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200798unix_cli_pager_prompt (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400799{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400800 u8 *prompt;
Chris Luke270b6de2016-05-19 14:23:25 -0400801 u32 h;
802
803 h = cf->pager_start + (cf->height - 1);
804 if (h > vec_len (cf->pager_index))
805 h = vec_len (cf->pager_index);
Chris Luke7afda3a2016-04-25 13:49:22 -0400806
Dave Barach9b8ffd92016-07-08 08:13:45 -0400807 prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s",
808 cf->ansi_capable ? ANSI_BOLD : "",
809 cf->pager_start + 1,
810 h,
811 vec_len (cf->pager_index),
812 cf->ansi_capable ? ANSI_RESET : "");
Chris Luke7afda3a2016-04-25 13:49:22 -0400813
Dave Barach9b8ffd92016-07-08 08:13:45 -0400814 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400815
Dave Barach9b8ffd92016-07-08 08:13:45 -0400816 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400817}
818
Chris Luke8e5b0412016-07-26 13:06:10 -0400819/** @brief Output a pager "skipping" message */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400820static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200821unix_cli_pager_message (unix_cli_file_t * cf, clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400822 char *message, char *postfix)
Chris Luke7afda3a2016-04-25 13:49:22 -0400823{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400824 u8 *prompt;
Chris Luke7afda3a2016-04-25 13:49:22 -0400825
Dave Barach9b8ffd92016-07-08 08:13:45 -0400826 prompt = format (0, "\r%s-- %s --%s%s",
827 cf->ansi_capable ? ANSI_BOLD : "",
828 message, cf->ansi_capable ? ANSI_RESET : "", postfix);
Chris Luke7afda3a2016-04-25 13:49:22 -0400829
Dave Barach9b8ffd92016-07-08 08:13:45 -0400830 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400831
Dave Barach9b8ffd92016-07-08 08:13:45 -0400832 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400833}
834
Chris Luke8e5b0412016-07-26 13:06:10 -0400835/** @brief Erase the printed pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400836static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200837unix_cli_pager_prompt_erase (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400838{
839 if (cf->ansi_capable)
840 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400841 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
842 unix_vlib_cli_output_cooked (cf, uf,
843 (u8 *) ANSI_CLEARLINE,
844 sizeof (ANSI_CLEARLINE) - 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400845 }
846 else
847 {
848 int i;
849
Dave Barach9b8ffd92016-07-08 08:13:45 -0400850 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
851 for (i = 0; i < cf->width - 1; i++)
852 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
853 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400854 }
855}
856
Chris Luke8e5b0412016-07-26 13:06:10 -0400857/** @brief Uses an ANSI escape sequence to move the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400858static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200859unix_cli_ansi_cursor (unix_cli_file_t * cf, clib_file_t * uf, u16 x, u16 y)
Chris Luke7afda3a2016-04-25 13:49:22 -0400860{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400861 u8 *str;
Chris Luke7afda3a2016-04-25 13:49:22 -0400862
Dave Barach9b8ffd92016-07-08 08:13:45 -0400863 str = format (0, "%s%d;%dH", CSI, y, x);
Chris Luke7afda3a2016-04-25 13:49:22 -0400864
Dave Barach9b8ffd92016-07-08 08:13:45 -0400865 unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str));
Chris Luke7afda3a2016-04-25 13:49:22 -0400866
Dave Barach9b8ffd92016-07-08 08:13:45 -0400867 vec_free (str);
Chris Luke7afda3a2016-04-25 13:49:22 -0400868}
869
Chris Luke862623d2016-05-14 10:13:34 -0400870/** Redraw the currently displayed page of text.
871 * @param cf CLI session to redraw the pager buffer of.
872 * @param uf Unix file of the CLI session.
873 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400874static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200875unix_cli_pager_redraw (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke862623d2016-05-14 10:13:34 -0400876{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400877 unix_cli_pager_index_t *pi = NULL;
878 u8 *line = NULL;
Chris Luke862623d2016-05-14 10:13:34 -0400879 word i;
880
881 /* No active pager? Do nothing. */
882 if (!vec_len (cf->pager_index))
883 return;
884
885 if (cf->ansi_capable)
886 {
887 /* If we have ANSI, send the clear screen sequence */
888 unix_vlib_cli_output_cooked (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400889 (u8 *) ANSI_CLEAR,
890 sizeof (ANSI_CLEAR) - 1);
Chris Luke862623d2016-05-14 10:13:34 -0400891 }
892 else
893 {
894 /* Otherwise make sure we're on a blank line */
895 unix_cli_pager_prompt_erase (cf, uf);
896 }
897
898 /* (Re-)send the current page of content */
899 for (i = 0; i < cf->height - 1 &&
Dave Barach9b8ffd92016-07-08 08:13:45 -0400900 i + cf->pager_start < vec_len (cf->pager_index); i++)
Chris Luke862623d2016-05-14 10:13:34 -0400901 {
902 pi = &cf->pager_index[cf->pager_start + i];
903 line = cf->pager_vector[pi->line] + pi->offset;
904
905 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
906 }
907 /* if the last line didn't end in newline, add a newline */
908 if (pi && line[pi->length - 1] != '\n')
Dave Barach9b8ffd92016-07-08 08:13:45 -0400909 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke862623d2016-05-14 10:13:34 -0400910
911 unix_cli_pager_prompt (cf, uf);
912}
913
914/** @brief Process and add a line to the pager index.
915 * In normal operation this function will take the given character string
916 * found in @c line and with length @c len_or_index and iterates the over the
917 * contents, adding each line of text discovered within it to the
918 * pager index. Lines are identified by newlines ("<code>\\n</code>") and by
919 * strings longer than the width of the terminal.
920 *
921 * If instead @c line is @c NULL then @c len_or_index is taken to mean the
922 * index of an existing line in the pager buffer; this simply means that the
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700923 * input line does not need to be cloned since we already have it. This is
Chris Luke862623d2016-05-14 10:13:34 -0400924 * typical if we are reindexing the pager buffer.
925 *
926 * @param cf The CLI session whose pager we are adding to.
927 * @param line The string of text to be indexed into the pager buffer.
928 * If @c line is @c NULL then the mode of operation
929 * changes slightly; see the description above.
930 * @param len_or_index If @c line is a pointer to a string then this parameter
931 * indicates the length of that string; Otherwise this
932 * value provides the index in the pager buffer of an
933 * existing string to be indexed.
934 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400935static void
936unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index)
Chris Luke862623d2016-05-14 10:13:34 -0400937{
Neale Ranns9c2c2432017-12-05 13:34:36 -0800938 u8 *p = NULL;
Chris Luke862623d2016-05-14 10:13:34 -0400939 word i, j, k;
940 word line_index, len;
941 u32 width = cf->width;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400942 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -0400943
944 if (line == NULL)
945 {
946 /* Use a line already in the pager buffer */
947 line_index = len_or_index;
Neale Ranns9c2c2432017-12-05 13:34:36 -0800948 if (cf->pager_vector != NULL)
949 p = cf->pager_vector[line_index];
Chris Luke862623d2016-05-14 10:13:34 -0400950 len = vec_len (p);
951 }
952 else
953 {
954 len = len_or_index;
955 /* Add a copy of the raw string to the pager buffer */
956 p = vec_new (u8, len);
957 clib_memcpy (p, line, len);
958
959 /* store in pager buffer */
960 line_index = vec_len (cf->pager_vector);
961 vec_add1 (cf->pager_vector, p);
962 }
963
964 i = 0;
965 while (i < len)
966 {
967 /* Find the next line, or run to terminal width, or run to EOL */
968 int l = len - i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400969 j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width);
Chris Luke862623d2016-05-14 10:13:34 -0400970
Dave Barach9b8ffd92016-07-08 08:13:45 -0400971 if (j < l && p[j] == '\n') /* incl \n */
972 j++;
Chris Luke862623d2016-05-14 10:13:34 -0400973
974 /* Add the line to the index */
975 k = vec_len (cf->pager_index);
976 vec_validate (cf->pager_index, k);
977 pi = &cf->pager_index[k];
978
979 pi->line = line_index;
980 pi->offset = i;
981 pi->length = j;
982
983 i += j;
984 p += j;
985 }
986}
987
988/** @brief Reindex entire pager buffer.
989 * Resets the current pager index and then re-adds the lines in the pager
990 * buffer to the index.
991 *
992 * Additionally this function attempts to retain the current page start
993 * line offset by searching for the same top-of-screen line in the new index.
994 *
995 * @param cf The CLI session whose pager buffer should be reindexed.
996 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400997static void
998unix_cli_pager_reindex (unix_cli_file_t * cf)
Chris Luke862623d2016-05-14 10:13:34 -0400999{
1000 word i, old_line, old_offset;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001001 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -04001002
1003 /* If there is nothing in the pager buffer then make sure the index
1004 * is empty and move on.
1005 */
1006 if (cf->pager_vector == 0)
1007 {
1008 vec_reset_length (cf->pager_index);
1009 return;
1010 }
1011
1012 /* Retain a pointer to the current page start line so we can
1013 * find it later
1014 */
1015 pi = &cf->pager_index[cf->pager_start];
1016 old_line = pi->line;
1017 old_offset = pi->offset;
1018
1019 /* Re-add the buffered lines to the index */
1020 vec_reset_length (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001021 vec_foreach_index (i, cf->pager_vector)
1022 {
1023 unix_cli_pager_add_line (cf, NULL, i);
1024 }
Chris Luke862623d2016-05-14 10:13:34 -04001025
1026 /* Attempt to re-locate the previously stored page start line */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001027 vec_foreach_index (i, cf->pager_index)
1028 {
1029 pi = &cf->pager_index[i];
Chris Luke862623d2016-05-14 10:13:34 -04001030
Dave Barach9b8ffd92016-07-08 08:13:45 -04001031 if (pi->line == old_line &&
1032 (pi->offset <= old_offset || pi->offset + pi->length > old_offset))
1033 {
1034 /* Found it! */
1035 cf->pager_start = i;
1036 break;
1037 }
1038 }
Chris Luke862623d2016-05-14 10:13:34 -04001039
1040 /* In case the start line was not found (rare), ensure the pager start
1041 * index is within bounds
1042 */
1043 if (cf->pager_start >= vec_len (cf->pager_index))
1044 {
Chris Luke270b6de2016-05-19 14:23:25 -04001045 if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001046 cf->pager_start = 0;
Chris Luke270b6de2016-05-19 14:23:25 -04001047 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001048 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
Chris Luke862623d2016-05-14 10:13:34 -04001049 }
1050}
1051
1052/** VLIB CLI output function.
1053 *
1054 * If the terminal has a pager configured then this function takes care
1055 * of collating output into the pager buffer; ensuring only the first page
1056 * is displayed and any lines in excess of the first page are buffered.
1057 *
1058 * If the maximum number of index lines in the buffer is exceeded then the
1059 * pager is cancelled and the contents of the current buffer are sent to the
1060 * terminal.
1061 *
1062 * If there is no pager configured then the output is sent directly to the
1063 * terminal.
1064 *
1065 * @param cli_file_index Index of the CLI session where this output is
1066 * directed.
1067 * @param buffer String of printabe bytes to be output.
1068 * @param buffer_bytes The number of bytes in @c buffer to be output.
1069 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001070static void
1071unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001073 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02001074 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001075 unix_cli_main_t *cm = &unix_cli_main;
1076 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +02001077 clib_file_t *uf;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001078
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02001080 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081
Chris Luke7afda3a2016-04-25 13:49:22 -04001082 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
1083 {
Chris Luke862623d2016-05-14 10:13:34 -04001084 unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes);
Chris Luke7afda3a2016-04-25 13:49:22 -04001085 }
1086 else
1087 {
Chris Luke862623d2016-05-14 10:13:34 -04001088 word row = vec_len (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001089 u8 *line;
1090 unix_cli_pager_index_t *pi;
Chris Luke7afda3a2016-04-25 13:49:22 -04001091
Chris Luke862623d2016-05-14 10:13:34 -04001092 /* Index and add the output lines to the pager buffer. */
1093 unix_cli_pager_add_line (cf, buffer, buffer_bytes);
1094
1095 /* Now iterate what was added to display the lines.
1096 * If we reach the bottom of the page, display a prompt.
1097 */
1098 while (row < vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001099 {
1100 if (row < cf->height - 1)
1101 {
1102 /* output this line */
1103 pi = &cf->pager_index[row];
1104 line = cf->pager_vector[pi->line] + pi->offset;
1105 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
Chris Luke862623d2016-05-14 10:13:34 -04001106
Dave Barach9b8ffd92016-07-08 08:13:45 -04001107 /* if the last line didn't end in newline, and we're at the
1108 * bottom of the page, add a newline */
1109 if (line[pi->length - 1] != '\n' && row == cf->height - 2)
1110 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1111 }
1112 else
1113 {
1114 /* Display the pager prompt every 10 lines */
1115 if (!(row % 10))
1116 unix_cli_pager_prompt (cf, uf);
1117 }
1118 row++;
1119 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001120
Chris Luke862623d2016-05-14 10:13:34 -04001121 /* Check if we went over the pager buffer limit */
1122 if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001123 {
1124 /* Stop using the pager for the remainder of this CLI command */
1125 cf->no_pager = 2;
Chris Luke7afda3a2016-04-25 13:49:22 -04001126
Dave Barach9b8ffd92016-07-08 08:13:45 -04001127 /* If we likely printed the prompt, erase it */
1128 if (vec_len (cf->pager_index) > cf->height - 1)
1129 unix_cli_pager_prompt_erase (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04001130
Dave Barach9b8ffd92016-07-08 08:13:45 -04001131 /* Dump out the contents of the buffer */
1132 for (row = cf->pager_start + (cf->height - 1);
1133 row < vec_len (cf->pager_index); row++)
1134 {
1135 pi = &cf->pager_index[row];
1136 line = cf->pager_vector[pi->line] + pi->offset;
1137 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1138 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001139
Dave Barach9b8ffd92016-07-08 08:13:45 -04001140 unix_cli_pager_reset (cf);
1141 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001142 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001143}
1144
Chris Luke862623d2016-05-14 10:13:34 -04001145/** Identify whether a terminal type is ANSI capable.
1146 *
Chris Luke54ccf222016-07-25 16:38:11 -04001147 * Compares the string given in @c term with a list of terminal types known
Chris Luke862623d2016-05-14 10:13:34 -04001148 * to support ANSI escape sequences.
1149 *
1150 * This list contains, for example, @c xterm, @c screen and @c ansi.
1151 *
1152 * @param term A string with a terminal type in it.
Chris Luke54ccf222016-07-25 16:38:11 -04001153 * @param len The length of the string in @c term.
Chris Luke862623d2016-05-14 10:13:34 -04001154 *
1155 * @return @c 1 if the terminal type is recognized as supporting ANSI
1156 * terminal sequences; @c 0 otherwise.
1157 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001158static u8
Chris Luke03add7f2017-09-20 23:31:24 -04001159unix_cli_terminal_type_ansi (u8 * term, uword len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160{
Chris Luke0aca5eb2016-04-25 13:48:54 -04001161 /* This may later be better done as a hash of some sort. */
1162#define _(a) do { \
1163 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1164 } while(0)
1165
1166 _("xterm");
1167 _("xterm-color");
Dave Barach9b8ffd92016-07-08 08:13:45 -04001168 _("xterm-256color"); /* iTerm on Mac */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001169 _("screen");
Damjan Marion6e8ab162017-06-05 21:56:12 +02001170 _("screen-256color"); /* Screen and tmux */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001171 _("ansi"); /* Microsoft Telnet */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001172#undef _
1173
1174 return 0;
1175}
1176
Chris Luke03add7f2017-09-20 23:31:24 -04001177/** Identify whether a terminal type is non-interactive.
1178 *
1179 * Compares the string given in @c term with a list of terminal types known
1180 * to be non-interactive, as send by tools such as @c vppctl .
1181 *
1182 * This list contains, for example, @c vppctl.
1183 *
1184 * @param term A string with a terminal type in it.
1185 * @param len The length of the string in @c term.
1186 *
1187 * @return @c 1 if the terminal type is recognized as being non-interactive;
1188 * @c 0 otherwise.
1189 */
1190static u8
1191unix_cli_terminal_type_noninteractive (u8 * term, uword len)
1192{
1193 /* This may later be better done as a hash of some sort. */
1194#define _(a) do { \
1195 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1196 } while(0)
1197
1198 _("vppctl");
1199#undef _
1200
1201 return 0;
1202}
1203
1204/** Set a session to be non-interactive. */
1205static void
1206unix_cli_set_session_noninteractive (unix_cli_file_t * cf)
1207{
1208 /* Non-interactive sessions don't get these */
1209 cf->is_interactive = 0;
1210 cf->no_pager = 1;
1211 cf->history_limit = 0;
1212 cf->has_history = 0;
1213 cf->line_mode = 1;
1214}
1215
Chris Luke8e5b0412016-07-26 13:06:10 -04001216/** @brief Emit initial welcome banner and prompt on a connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001217static void
1218unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001219{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001220 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02001221 clib_file_main_t *fm = &file_main;
1222 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke572d8122016-04-25 13:49:07 -04001223 unix_cli_banner_t *banner;
1224 int i, len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001225
Chris Luke03add7f2017-09-20 23:31:24 -04001226 /* Mark the session as started if we get here */
1227 cf->started = 1;
1228
1229 if (!(cf->is_interactive)) /* No banner for non-interactive sessions */
1230 return;
1231
Chris Luke0aca5eb2016-04-25 13:48:54 -04001232 /*
1233 * Put the first bytes directly into the buffer so that further output is
1234 * queued until everything is ready. (oterwise initial prompt can appear
1235 * mid way through VPP initialization)
1236 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001237 unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
Chris Luke572d8122016-04-25 13:49:07 -04001238
1239 if (!um->cli_no_banner)
1240 {
1241 if (cf->ansi_capable)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001242 {
1243 banner = unix_cli_banner_color;
1244 len = ARRAY_LEN (unix_cli_banner_color);
1245 }
Chris Luke572d8122016-04-25 13:49:07 -04001246 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001247 {
1248 banner = unix_cli_banner;
1249 len = ARRAY_LEN (unix_cli_banner);
1250 }
Chris Luke572d8122016-04-25 13:49:07 -04001251
1252 for (i = 0; i < len; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001253 {
1254 unix_vlib_cli_output_cooked (cf, uf,
1255 banner[i].line, banner[i].length);
1256 }
1257 }
Chris Luke572d8122016-04-25 13:49:07 -04001258
1259 /* Prompt. */
Chris Luke7afda3a2016-04-25 13:49:22 -04001260 unix_cli_cli_prompt (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001261
Chris Luke0aca5eb2016-04-25 13:48:54 -04001262}
1263
Chris Luke6a3a4f72019-07-09 23:33:30 -04001264/**
1265 * @brief A failsafe manager that ensures CLI sessions issue an initial
1266 * prompt if TELNET negotiation fails.
1267 */
1268static uword
1269unix_cli_new_session_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
1270 vlib_frame_t * f)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001271{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001272 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke6a3a4f72019-07-09 23:33:30 -04001273 uword event_type, *event_data = 0;
1274 f64 wait = 10.0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001275
Chris Luke6a3a4f72019-07-09 23:33:30 -04001276 while (1)
1277 {
1278 if (vec_len (cm->new_sessions) > 0)
1279 wait = vlib_process_wait_for_event_or_clock (vm, wait);
1280 else
1281 vlib_process_wait_for_event (vm);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001282
Chris Luke6a3a4f72019-07-09 23:33:30 -04001283 event_type = vlib_process_get_events (vm, &event_data);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001284
Chris Luke6a3a4f72019-07-09 23:33:30 -04001285 switch (event_type)
1286 {
1287 case ~0: /* no events => timeout */
1288 break;
1289
1290 case UNIX_CLI_NEW_SESSION_EVENT_ADD:
1291 {
1292 /* Add an identifier to the new session list */
1293 unix_cli_new_session_t ns;
1294
1295 ns.cf_index = event_data[0];
1296 ns.deadline = vlib_time_now (vm) + 1.0;
1297
1298 vec_add1 (cm->new_sessions, ns);
1299
1300 if (wait > 0.1)
1301 wait = 0.1; /* force a re-evaluation soon, but not too soon */
1302 }
1303 break;
1304
1305 default:
1306 clib_warning ("BUG: unknown event type 0x%wx", event_type);
1307 break;
1308 }
1309
1310 vec_reset_length (event_data);
1311
1312 if (vlib_process_suspend_time_is_zero (wait))
1313 {
1314 /* Scan the list looking for expired deadlines.
1315 * Emit needed prompts and remove from the list.
1316 * While scanning, look for the nearest new deadline
1317 * for the next iteration.
1318 * Since the list is ordered with newest sessions first
1319 * we can make assumptions about expired sessions being
1320 * contiguous at the beginning and the next deadline is the
1321 * next entry on the list, if any.
1322 */
1323 f64 now = vlib_time_now (vm);
1324 unix_cli_new_session_t *nsp;
1325 word index = 0;
1326
1327 wait = INFINITY;
1328
1329 vec_foreach (nsp, cm->new_sessions)
1330 {
1331 if (vlib_process_suspend_time_is_zero (nsp->deadline - now))
1332 {
1333 /* Deadline reached */
1334 unix_cli_file_t *cf;
1335
1336 /* Mark the highwater */
1337 index++;
1338
1339 /* Check the connection didn't close already */
1340 if (pool_is_free_index (cm->cli_file_pool, nsp->cf_index))
1341 continue;
1342
1343 cf = pool_elt_at_index (cm->cli_file_pool, nsp->cf_index);
1344
1345 if (!cf->started)
1346 unix_cli_file_welcome (cm, cf);
1347 }
1348 else
1349 {
1350 wait = nsp->deadline - now;
1351 break;
1352 }
1353 }
1354
1355 if (index)
1356 {
1357 /* We have sessions to remove */
1358 vec_delete (cm->new_sessions, index, 0);
1359 }
1360 }
1361 }
1362
1363 return 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001364}
1365
Chris Luke6a3a4f72019-07-09 23:33:30 -04001366
Chris Luke8e5b0412016-07-26 13:06:10 -04001367/** @brief A mostly no-op Telnet state machine.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001368 * Process Telnet command bytes in a way that ensures we're mostly
1369 * transparent to the Telnet protocol. That is, it's mostly a no-op.
1370 *
1371 * @return -1 if we need more bytes, otherwise a positive integer number of
1372 * bytes to consume from the input_vector, not including the initial
1373 * IAC byte.
1374 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001375static i32
1376unix_cli_process_telnet (unix_main_t * um,
1377 unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +02001378 clib_file_t * uf, u8 * input_vector, uword len)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001379{
1380 /* Input_vector starts at IAC byte.
1381 * See if we have a complete message; if not, return -1 so we wait for more.
1382 * if we have a complete message, consume those bytes from the vector.
1383 */
1384 i32 consume = 0;
1385
1386 if (len == 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001387 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001388
1389 switch (input_vector[1])
Ed Warnickecb9cada2015-12-08 15:45:58 -07001390 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001391 case IAC:
1392 /* two IAC's in a row means to pass through 0xff.
1393 * since that makes no sense here, just consume it.
1394 */
1395 consume = 1;
1396 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001397
Dave Barach9b8ffd92016-07-08 08:13:45 -04001398 case WILL:
1399 case WONT:
1400 case DO:
1401 case DONT:
1402 /* Expect 3 bytes */
Chris Lukea9cf6af2018-09-05 21:00:52 -04001403 if (len < 3)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001404 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001405
Dave Barach9b8ffd92016-07-08 08:13:45 -04001406 consume = 2;
1407 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001408
Dave Barach9b8ffd92016-07-08 08:13:45 -04001409 case SB:
1410 {
1411 /* Sub option - search ahead for IAC SE to end it */
1412 i32 i;
1413 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
1414 {
1415 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
1416 {
1417 /* We have a complete message; see if we care about it */
1418 switch (input_vector[2])
1419 {
1420 case TELOPT_TTYPE:
1421 if (input_vector[3] != 0)
1422 break;
Chris Luke03add7f2017-09-20 23:31:24 -04001423 {
1424 /* See if the the terminal type is recognized */
1425 u8 *term = input_vector + 4;
1426 uword len = i - 5;
1427
1428 /* See if the terminal type is ANSI capable */
1429 cf->ansi_capable =
1430 unix_cli_terminal_type_ansi (term, len);
1431
1432 /* See if the terminal type indicates non-interactive */
1433 if (unix_cli_terminal_type_noninteractive (term, len))
1434 unix_cli_set_session_noninteractive (cf);
1435 }
1436
Dave Barach9b8ffd92016-07-08 08:13:45 -04001437 /* If session not started, we can release the pause */
1438 if (!cf->started)
1439 /* Send the welcome banner and initial prompt */
1440 unix_cli_file_welcome (&unix_cli_main, cf);
1441 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001442
Dave Barach9b8ffd92016-07-08 08:13:45 -04001443 case TELOPT_NAWS:
1444 /* Window size */
1445 if (i != 8) /* check message is correct size */
1446 break;
Chris Lukeb2bcad62017-09-18 08:51:22 -04001447
Dave Barach9b8ffd92016-07-08 08:13:45 -04001448 cf->width =
1449 clib_net_to_host_u16 (*((u16 *) (input_vector + 3)));
Chris Lukeb2bcad62017-09-18 08:51:22 -04001450 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
1451 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
Chris Luke2d8bf302017-11-12 22:26:37 -05001452 if (cf->width == 0)
1453 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
Chris Lukeb2bcad62017-09-18 08:51:22 -04001454
Dave Barach9b8ffd92016-07-08 08:13:45 -04001455 cf->height =
1456 clib_net_to_host_u16 (*((u16 *) (input_vector + 5)));
Chris Lukeb2bcad62017-09-18 08:51:22 -04001457 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
1458 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
Chris Luke2d8bf302017-11-12 22:26:37 -05001459 if (cf->height == 0)
1460 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
Chris Lukeb2bcad62017-09-18 08:51:22 -04001461
Dave Barach9b8ffd92016-07-08 08:13:45 -04001462 /* reindex pager buffer */
1463 unix_cli_pager_reindex (cf);
1464 /* redraw page */
1465 unix_cli_pager_redraw (cf, uf);
1466 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04001467
Dave Barach9b8ffd92016-07-08 08:13:45 -04001468 default:
1469 break;
1470 }
1471 /* Consume it all */
1472 consume = i;
1473 break;
1474 }
1475 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001476
Dave Barach9b8ffd92016-07-08 08:13:45 -04001477 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
1478 consume = 1; /* hit max search depth, advance one byte */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001479
Dave Barach9b8ffd92016-07-08 08:13:45 -04001480 if (consume == 0)
1481 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001482
Dave Barach9b8ffd92016-07-08 08:13:45 -04001483 break;
1484 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001485
Dave Barach9b8ffd92016-07-08 08:13:45 -04001486 case GA:
1487 case EL:
1488 case EC:
1489 case AO:
1490 case IP:
1491 case BREAK:
1492 case DM:
1493 case NOP:
1494 case SE:
1495 case EOR:
1496 case ABORT:
1497 case SUSP:
1498 case xEOF:
1499 /* Simple one-byte messages */
1500 consume = 1;
1501 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001502
Dave Barach9b8ffd92016-07-08 08:13:45 -04001503 case AYT:
1504 /* Are You There - trigger a visible response */
1505 consume = 1;
1506 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
1507 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001508
Dave Barach9b8ffd92016-07-08 08:13:45 -04001509 default:
1510 /* Unknown command! Eat the IAC byte */
1511 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001512 }
1513
Dave Barach9b8ffd92016-07-08 08:13:45 -04001514 return consume;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001515}
1516
Chris Luke8e5b0412016-07-26 13:06:10 -04001517/** @brief Process actionable input.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001518 * Based on the \c action process the input; this typically involves
1519 * searching the command history or editing the current command line.
1520 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001521static int
1522unix_cli_line_process_one (unix_cli_main_t * cm,
1523 unix_main_t * um,
1524 unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +02001525 clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001526 u8 input, unix_cli_parse_action_t action)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001527{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001528 u8 *prev;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001529 u8 *save = 0;
1530 u8 **possible_commands;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001531 int j, delta;
1532
1533 switch (action)
1534 {
1535 case UNIX_CLI_PARSE_ACTION_NOACTION:
1536 break;
1537
Chris Luke0aca5eb2016-04-25 13:48:54 -04001538 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
1539 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
Chris Luke7afda3a2016-04-25 13:49:22 -04001540 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001541 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001542 if (cf->search_mode == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001543 {
1544 /* Erase the current command (if any) */
Chris Lukefc379d22018-06-05 23:50:19 -04001545 for (; cf->cursor > 0; cf->cursor--)
1546 {
1547 unix_vlib_cli_output_cursor_left (cf, uf);
1548 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1549 unix_vlib_cli_output_cursor_left (cf, uf);
1550 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001551
Dave Barach9b8ffd92016-07-08 08:13:45 -04001552 vec_reset_length (cf->search_key);
1553 vec_reset_length (cf->current_command);
Chris Lukefc379d22018-06-05 23:50:19 -04001554
Dave Barach9b8ffd92016-07-08 08:13:45 -04001555 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1556 cf->search_mode = -1;
1557 else
1558 cf->search_mode = 1;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001559 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001560 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001561 {
1562 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1563 cf->search_mode = -1;
1564 else
1565 cf->search_mode = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001566
Dave Barach9b8ffd92016-07-08 08:13:45 -04001567 cf->excursion += cf->search_mode;
1568 goto search_again;
1569 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001570 break;
1571
1572 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
1573 /* Erase the command from the cursor to the start */
1574
Chris Lukefc379d22018-06-05 23:50:19 -04001575 j = cf->cursor;
1576 /* Shimmy backwards to the new end of line position */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001577 delta = vec_len (cf->current_command) - cf->cursor;
Chris Lukefc379d22018-06-05 23:50:19 -04001578 for (; cf->cursor > delta; cf->cursor--)
1579 unix_vlib_cli_output_cursor_left (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001580 /* Zap from here to the end of what is currently displayed */
Chris Lukefc379d22018-06-05 23:50:19 -04001581 for (; cf->cursor < vec_len (cf->current_command); cf->cursor++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001582 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001583 /* Get back to the start of the line */
Chris Lukefc379d22018-06-05 23:50:19 -04001584 for (; cf->cursor > 0; cf->cursor--)
1585 unix_vlib_cli_output_cursor_left (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001586
Chris Lukefc379d22018-06-05 23:50:19 -04001587 /* Delete the desired text from the command */
1588 memmove (cf->current_command, cf->current_command + j, delta);
1589 _vec_len (cf->current_command) = delta;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001590
1591 /* Print the new contents */
Chris Lukefc379d22018-06-05 23:50:19 -04001592 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, delta);
1593 cf->cursor = delta; /* for backspace tracking */
1594
Chris Luke0aca5eb2016-04-25 13:48:54 -04001595 /* Shimmy back to the start */
Chris Lukefc379d22018-06-05 23:50:19 -04001596 for (; cf->cursor > 0; cf->cursor--)
1597 unix_vlib_cli_output_cursor_left (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001598
1599 cf->search_mode = 0;
1600 break;
1601
1602 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
1603 /* Erase the command from the cursor to the end */
1604
Chris Lukefc379d22018-06-05 23:50:19 -04001605 j = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001606 /* Zap from cursor to end of what is currently displayed */
Chris Lukefc379d22018-06-05 23:50:19 -04001607 for (; cf->cursor < (vec_len (cf->current_command)); cf->cursor++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001608 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001609 /* Get back to where we were */
Chris Lukefc379d22018-06-05 23:50:19 -04001610 for (; cf->cursor > j; cf->cursor--)
1611 unix_vlib_cli_output_cursor_left (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001612
1613 /* Truncate the line at the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001614 _vec_len (cf->current_command) = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001615
1616 cf->search_mode = 0;
1617 break;
1618
1619 case UNIX_CLI_PARSE_ACTION_LEFT:
1620 if (cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001621 {
Chris Lukefc379d22018-06-05 23:50:19 -04001622 unix_vlib_cli_output_cursor_left (cf, uf);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001623 cf->cursor--;
1624 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001625
1626 cf->search_mode = 0;
1627 break;
1628
1629 case UNIX_CLI_PARSE_ACTION_RIGHT:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001630 if (cf->cursor < vec_len (cf->current_command))
1631 {
1632 /* have to emit the character under the cursor */
1633 unix_vlib_cli_output_cooked (cf, uf,
1634 cf->current_command + cf->cursor, 1);
1635 cf->cursor++;
1636 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001637
1638 cf->search_mode = 0;
1639 break;
1640
1641 case UNIX_CLI_PARSE_ACTION_UP:
1642 case UNIX_CLI_PARSE_ACTION_DOWN:
Chris Luke7afda3a2016-04-25 13:49:22 -04001643 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001644 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001645 cf->search_mode = 0;
1646 /* Erase the command */
Chris Lukefc379d22018-06-05 23:50:19 -04001647 for (; cf->cursor < vec_len (cf->current_command); cf->cursor++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001648 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Lukefc379d22018-06-05 23:50:19 -04001649 for (; cf->cursor > 0; cf->cursor--)
1650 {
1651 unix_vlib_cli_output_cursor_left (cf, uf);
1652 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1653 unix_vlib_cli_output_cursor_left (cf, uf);
1654 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001655 vec_reset_length (cf->current_command);
1656 if (vec_len (cf->command_history))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001657 {
1658 if (action == UNIX_CLI_PARSE_ACTION_UP)
1659 delta = -1;
1660 else
1661 delta = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001662
Dave Barach9b8ffd92016-07-08 08:13:45 -04001663 cf->excursion += delta;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001664
Dave Barach9b8ffd92016-07-08 08:13:45 -04001665 if (cf->excursion == vec_len (cf->command_history))
1666 {
1667 /* down-arrowed to last entry - want a blank line */
1668 _vec_len (cf->current_command) = 0;
1669 }
1670 else if (cf->excursion < 0)
1671 {
1672 /* up-arrowed over the start to the end, want a blank line */
1673 cf->excursion = vec_len (cf->command_history);
1674 _vec_len (cf->current_command) = 0;
1675 }
1676 else
1677 {
1678 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1679 /* down-arrowed past end - wrap to start */
1680 cf->excursion = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001681
Dave Barach9b8ffd92016-07-08 08:13:45 -04001682 /* Print the command at the current position */
1683 prev = cf->command_history[cf->excursion];
1684 vec_validate (cf->current_command, vec_len (prev) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001685
Dave Barach9b8ffd92016-07-08 08:13:45 -04001686 clib_memcpy (cf->current_command, prev, vec_len (prev));
1687 _vec_len (cf->current_command) = vec_len (prev);
1688 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1689 vec_len (cf->current_command));
1690 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001691 }
Yoann Desmouceaux561ae0a2017-09-20 10:08:28 +02001692 cf->cursor = vec_len (cf->current_command);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001693 break;
1694
1695 case UNIX_CLI_PARSE_ACTION_HOME:
1696 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001697 {
Chris Lukefc379d22018-06-05 23:50:19 -04001698 for (; cf->cursor > 0; cf->cursor--)
1699 unix_vlib_cli_output_cursor_left (cf, uf);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001700 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001701
Chris Luke0aca5eb2016-04-25 13:48:54 -04001702 cf->search_mode = 0;
1703 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001704
Chris Luke0aca5eb2016-04-25 13:48:54 -04001705 case UNIX_CLI_PARSE_ACTION_END:
1706 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001707 cf->cursor < vec_len (cf->current_command))
1708 {
1709 unix_vlib_cli_output_cooked (cf, uf,
1710 cf->current_command + cf->cursor,
1711 vec_len (cf->current_command) -
1712 cf->cursor);
1713 cf->cursor = vec_len (cf->current_command);
1714 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001715
1716 cf->search_mode = 0;
1717 break;
1718
1719 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1720 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001721 {
Chris Lukefc379d22018-06-05 23:50:19 -04001722 unix_vlib_cli_output_cursor_left (cf, uf);
1723 cf->cursor--;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001724
Chris Lukefc379d22018-06-05 23:50:19 -04001725 while (cf->cursor && isspace (cf->current_command[cf->cursor]))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001726 {
Chris Lukefc379d22018-06-05 23:50:19 -04001727 unix_vlib_cli_output_cursor_left (cf, uf);
1728 cf->cursor--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001729 }
Chris Lukefc379d22018-06-05 23:50:19 -04001730 while (cf->cursor && !isspace (cf->current_command[cf->cursor]))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001731 {
Chris Lukefc379d22018-06-05 23:50:19 -04001732 if (isspace (cf->current_command[cf->cursor - 1]))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001733 break;
Chris Lukefc379d22018-06-05 23:50:19 -04001734 unix_vlib_cli_output_cursor_left (cf, uf);
1735 cf->cursor--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001736 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001737
Dave Barach9b8ffd92016-07-08 08:13:45 -04001738 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001739
1740 cf->search_mode = 0;
1741 break;
1742
1743 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1744 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001745 cf->cursor < vec_len (cf->current_command))
1746 {
1747 int e = vec_len (cf->current_command);
1748 j = cf->cursor;
1749 while (j < e && !isspace (cf->current_command[j]))
1750 j++;
1751 while (j < e && isspace (cf->current_command[j]))
1752 j++;
1753 unix_vlib_cli_output_cooked (cf, uf,
1754 cf->current_command + cf->cursor,
1755 j - cf->cursor);
1756 cf->cursor = j;
1757 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001758
1759 cf->search_mode = 0;
1760 break;
1761
1762
1763 case UNIX_CLI_PARSE_ACTION_ERASE:
1764 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001765 {
1766 if (cf->cursor == vec_len (cf->current_command))
1767 {
Chris Lukefc379d22018-06-05 23:50:19 -04001768 unix_vlib_cli_output_cursor_left (cf, uf);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001769 cf->cursor--;
Chris Lukefc379d22018-06-05 23:50:19 -04001770 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1771 cf->cursor++;
1772 unix_vlib_cli_output_cursor_left (cf, uf);
1773 cf->cursor--;
1774 _vec_len (cf->current_command)--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001775 }
1776 else if (cf->cursor > 0)
1777 {
1778 /* shift everything at & to the right of the cursor left by 1 */
1779 j = vec_len (cf->current_command) - cf->cursor;
1780 memmove (cf->current_command + cf->cursor - 1,
1781 cf->current_command + cf->cursor, j);
1782 _vec_len (cf->current_command)--;
Chris Lukefc379d22018-06-05 23:50:19 -04001783
Dave Barach9b8ffd92016-07-08 08:13:45 -04001784 /* redraw the rest of the line */
Chris Lukefc379d22018-06-05 23:50:19 -04001785 unix_vlib_cli_output_cursor_left (cf, uf);
1786 cf->cursor--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001787 unix_vlib_cli_output_cooked (cf, uf,
1788 cf->current_command + cf->cursor,
1789 j);
Chris Lukefc379d22018-06-05 23:50:19 -04001790 cf->cursor += j;
1791 /* erase last char */
1792 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1793 cf->cursor++;
1794
Dave Barach9b8ffd92016-07-08 08:13:45 -04001795 /* and shift the terminal cursor back where it should be */
Chris Lukefc379d22018-06-05 23:50:19 -04001796 j += 2; /* account for old string length and offset position */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001797 while (--j)
Chris Lukefc379d22018-06-05 23:50:19 -04001798 {
1799 unix_vlib_cli_output_cursor_left (cf, uf);
1800 cf->cursor--;
1801 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001802 }
1803 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001804 cf->search_mode = 0;
1805 cf->excursion = 0;
1806 vec_reset_length (cf->search_key);
1807 break;
1808
1809 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1810 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001811 {
1812 if (cf->cursor < vec_len (cf->current_command))
1813 {
1814 /* shift everything to the right of the cursor left by 1 */
1815 j = vec_len (cf->current_command) - cf->cursor - 1;
1816 memmove (cf->current_command + cf->cursor,
1817 cf->current_command + cf->cursor + 1, j);
1818 _vec_len (cf->current_command)--;
1819 /* redraw the rest of the line */
1820 unix_vlib_cli_output_cooked (cf, uf,
1821 cf->current_command + cf->cursor,
1822 j);
Chris Lukefc379d22018-06-05 23:50:19 -04001823 cf->cursor += j;
1824 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1825 cf->cursor++;
1826 unix_vlib_cli_output_cursor_left (cf, uf);
1827 cf->cursor--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001828 /* and shift the terminal cursor back where it should be */
1829 if (j)
1830 {
Chris Lukefc379d22018-06-05 23:50:19 -04001831 unix_vlib_cli_output_cursor_left (cf, uf);
1832 cf->cursor--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001833 while (--j)
Chris Lukefc379d22018-06-05 23:50:19 -04001834 {
1835 unix_vlib_cli_output_cursor_left (cf, uf);
1836 cf->cursor--;
1837 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001838 }
1839 }
1840 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001841 else if (input == 'D' - '@')
Dave Barach9b8ffd92016-07-08 08:13:45 -04001842 {
1843 /* ^D with no command entered = quit */
1844 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1845 vlib_process_signal_event (um->vlib_main,
1846 vlib_current_process (um->vlib_main),
1847 UNIX_CLI_PROCESS_EVENT_QUIT,
1848 cf - cm->cli_file_pool);
1849 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001850 cf->search_mode = 0;
1851 cf->excursion = 0;
1852 vec_reset_length (cf->search_key);
1853 break;
1854
1855 case UNIX_CLI_PARSE_ACTION_CLEAR:
1856 /* If we're in ANSI mode, clear the screen.
1857 * Then redraw the prompt and any existing command input, then put
1858 * the cursor back where it was in that line.
1859 */
1860 if (cf->ansi_capable)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001861 unix_vlib_cli_output_cooked (cf, uf,
1862 (u8 *) ANSI_CLEAR,
1863 sizeof (ANSI_CLEAR) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001864 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001865 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001866
1867 unix_vlib_cli_output_raw (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001868 cm->cli_prompt, vec_len (cm->cli_prompt));
Chris Lukefc379d22018-06-05 23:50:19 -04001869 unix_vlib_cli_output_cooked (cf, uf,
1870 cf->current_command,
1871 vec_len (cf->current_command));
1872 j = cf->cursor;
1873 cf->cursor = vec_len (cf->current_command);
1874 for (; cf->cursor > j; cf->cursor--)
1875 unix_vlib_cli_output_cursor_left (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001876
1877 break;
1878
Chris Luke7afda3a2016-04-25 13:49:22 -04001879 case UNIX_CLI_PARSE_ACTION_TAB:
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001880 if (cf->cursor < vec_len (cf->current_command))
1881 {
1882 /* if we are in the middle of a line, complete only if
1883 * the cursor points to whitespace */
1884 if (isspace (cf->current_command[cf->cursor]))
1885 {
1886 /* save and clear any input that is after the cursor */
1887 vec_resize (save, vec_len (cf->current_command) - cf->cursor);
1888 clib_memcpy (save, cf->current_command + cf->cursor,
1889 vec_len (cf->current_command) - cf->cursor);
1890 _vec_len (cf->current_command) = cf->cursor;
1891 }
1892 else
1893 {
1894 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1895 break;
1896 }
1897 }
1898 possible_commands =
1899 vlib_cli_get_possible_completions (cf->current_command);
1900 if (vec_len (possible_commands) == 1)
1901 {
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001902 u8 *completed = possible_commands[0];
Chris Lukefc379d22018-06-05 23:50:19 -04001903 j = cf->cursor;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001904
1905 /* find the last word of current_command */
1906 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1907 {
Chris Lukefc379d22018-06-05 23:50:19 -04001908 unix_vlib_cli_output_cursor_left (cf, uf);
1909 cf->cursor--;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001910 j--;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001911 }
1912 _vec_len (cf->current_command) = j;
1913
1914 /* replace it with the newly expanded command */
1915 vec_append (cf->current_command, completed);
1916
1917 /* echo to the terminal */
Chris Lukefc379d22018-06-05 23:50:19 -04001918 unix_vlib_cli_output_cooked (cf, uf, completed,
1919 vec_len (completed));
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001920
1921 /* add one trailing space if needed */
1922 if (vec_len (save) == 0)
1923 {
1924 vec_add1 (cf->current_command, ' ');
Chris Lukefc379d22018-06-05 23:50:19 -04001925 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001926 }
1927
1928 cf->cursor = vec_len (cf->current_command);
1929
1930 }
1931 else if (vec_len (possible_commands) >= 2)
1932 {
1933 u8 **possible_command;
1934 uword max_command_len = 0, min_command_len = ~0;
Chris Lukefc379d22018-06-05 23:50:19 -04001935 u32 i;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001936
1937 vec_foreach (possible_command, possible_commands)
1938 {
1939 if (vec_len (*possible_command) > max_command_len)
Chris Lukefc379d22018-06-05 23:50:19 -04001940 max_command_len = vec_len (*possible_command);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001941 if (vec_len (*possible_command) < min_command_len)
Chris Lukefc379d22018-06-05 23:50:19 -04001942 min_command_len = vec_len (*possible_command);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001943 }
1944
1945 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1946
1947 i = 0;
1948 vec_foreach (possible_command, possible_commands)
1949 {
1950 if (i + max_command_len >= cf->width)
1951 {
1952 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1953 i = 0;
1954 }
Chris Lukefc379d22018-06-05 23:50:19 -04001955 unix_vlib_cli_output_cooked (cf, uf, *possible_command,
1956 vec_len (*possible_command));
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001957 for (j = vec_len (*possible_command); j < max_command_len + 2;
1958 j++)
1959 {
Chris Lukefc379d22018-06-05 23:50:19 -04001960 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001961 }
1962 i += max_command_len + 2;
1963 }
1964
1965 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1966
1967 /* rewrite prompt */
1968 unix_cli_cli_prompt (cf, uf);
Chris Lukefc379d22018-06-05 23:50:19 -04001969 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1970 vec_len (cf->current_command));
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001971
1972 /* count length of last word */
1973 j = cf->cursor;
1974 i = 0;
1975 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1976 {
1977 j--;
1978 i++;
1979 }
1980
1981 /* determine smallest common command */
1982 for (; i < min_command_len; i++)
1983 {
1984 u8 common = '\0';
1985 int stop = 0;
Chris Lukefc379d22018-06-05 23:50:19 -04001986
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001987 vec_foreach (possible_command, possible_commands)
1988 {
1989 if (common == '\0')
1990 {
1991 common = (*possible_command)[i];
1992 }
1993 else if (common != (*possible_command)[i])
1994 {
1995 stop = 1;
1996 break;
1997 }
1998 }
Chris Lukefc379d22018-06-05 23:50:19 -04001999
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002000 if (!stop)
2001 {
2002 vec_add1 (cf->current_command, common);
2003 cf->cursor++;
Chris Lukefc379d22018-06-05 23:50:19 -04002004 unix_vlib_cli_output_cooked (cf, uf, (u8 *) & common, 1);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002005 }
2006 else
2007 {
2008 break;
2009 }
2010 }
2011 }
2012 else
2013 {
2014 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
2015 }
2016
2017 if (vec_len (save) > 0)
2018 {
2019 /* restore remaining input if tab was hit in the middle of a line */
Chris Lukefc379d22018-06-05 23:50:19 -04002020 unix_vlib_cli_output_cooked (cf, uf, save, vec_len (save));
2021 cf->cursor += vec_len (save);
2022 for (j = 0; j < vec_len (save); j++, cf->cursor--)
2023 unix_vlib_cli_output_cursor_left (cf, uf);
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02002024 vec_append (cf->current_command, save);
2025 vec_free (save);
2026 }
2027 vec_free (possible_commands);
2028
2029 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04002030 case UNIX_CLI_PARSE_ACTION_YANK:
2031 /* TODO */
2032 break;
2033
2034
2035 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
2036 pager_quit:
2037 unix_cli_pager_prompt_erase (cf, uf);
2038 unix_cli_pager_reset (cf);
2039 unix_cli_cli_prompt (cf, uf);
2040 break;
2041
2042 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
2043 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
2044 /* show next page of the buffer */
Chris Luke70a745d2018-06-10 10:47:50 -04002045 if (cf->height + cf->pager_start <= vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002046 {
2047 u8 *line = NULL;
2048 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04002049
Dave Barach9b8ffd92016-07-08 08:13:45 -04002050 int m = cf->pager_start + (cf->height - 1);
2051 unix_cli_pager_prompt_erase (cf, uf);
2052 for (j = m;
2053 j < vec_len (cf->pager_index) && cf->pager_start < m;
2054 j++, cf->pager_start++)
2055 {
2056 pi = &cf->pager_index[j];
2057 line = cf->pager_vector[pi->line] + pi->offset;
2058 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2059 }
2060 /* if the last line didn't end in newline, add a newline */
2061 if (pi && line[pi->length - 1] != '\n')
2062 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2063 unix_cli_pager_prompt (cf, uf);
2064 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002065 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002066 {
2067 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
2068 /* no more in buffer, exit, but only if it was <space> */
2069 goto pager_quit;
2070 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002071 break;
2072
2073 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
2074 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
2075 /* display the next line of the buffer */
Chris Luke70a745d2018-06-10 10:47:50 -04002076 if (cf->height + cf->pager_start <= vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002077 {
2078 u8 *line;
2079 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -04002080
Dave Barach9b8ffd92016-07-08 08:13:45 -04002081 unix_cli_pager_prompt_erase (cf, uf);
2082 pi = &cf->pager_index[cf->pager_start + (cf->height - 1)];
2083 line = cf->pager_vector[pi->line] + pi->offset;
2084 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2085 cf->pager_start++;
2086 /* if the last line didn't end in newline, add a newline */
2087 if (line[pi->length - 1] != '\n')
2088 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2089 unix_cli_pager_prompt (cf, uf);
2090 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002091 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002092 {
2093 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
2094 /* no more in buffer, exit, but only if it was <enter> */
2095 goto pager_quit;
2096 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002097
2098 break;
2099
2100 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
2101 /* scroll the page back one line */
2102 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002103 {
2104 u8 *line = NULL;
2105 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04002106
Dave Barach9b8ffd92016-07-08 08:13:45 -04002107 cf->pager_start--;
2108 if (cf->ansi_capable)
2109 {
2110 pi = &cf->pager_index[cf->pager_start];
2111 line = cf->pager_vector[pi->line] + pi->offset;
2112 unix_cli_pager_prompt_erase (cf, uf);
2113 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN,
2114 sizeof (ANSI_SCROLLDN) - 1);
2115 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR,
2116 sizeof (ANSI_SAVECURSOR) - 1);
2117 unix_cli_ansi_cursor (cf, uf, 1, 1);
2118 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE,
2119 sizeof (ANSI_CLEARLINE) - 1);
2120 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2121 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR,
2122 sizeof (ANSI_RESTCURSOR) - 1);
2123 unix_cli_pager_prompt_erase (cf, uf);
2124 unix_cli_pager_prompt (cf, uf);
2125 }
2126 else
2127 {
2128 int m = cf->pager_start + (cf->height - 1);
2129 unix_cli_pager_prompt_erase (cf, uf);
2130 for (j = cf->pager_start;
2131 j < vec_len (cf->pager_index) && j < m; j++)
2132 {
2133 pi = &cf->pager_index[j];
2134 line = cf->pager_vector[pi->line] + pi->offset;
2135 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2136 }
2137 /* if the last line didn't end in newline, add a newline */
2138 if (pi && line[pi->length - 1] != '\n')
2139 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2140 unix_cli_pager_prompt (cf, uf);
2141 }
2142 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002143 break;
2144
2145 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
2146 /* back to the first page of the buffer */
2147 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002148 {
2149 u8 *line = NULL;
2150 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04002151
Dave Barach9b8ffd92016-07-08 08:13:45 -04002152 cf->pager_start = 0;
2153 int m = cf->pager_start + (cf->height - 1);
2154 unix_cli_pager_prompt_erase (cf, uf);
2155 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
2156 j++)
2157 {
2158 pi = &cf->pager_index[j];
2159 line = cf->pager_vector[pi->line] + pi->offset;
2160 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2161 }
2162 /* if the last line didn't end in newline, add a newline */
2163 if (pi && line[pi->length - 1] != '\n')
2164 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2165 unix_cli_pager_prompt (cf, uf);
2166 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002167 break;
2168
2169 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
2170 /* skip to the last page of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04002171 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002172 {
2173 u8 *line = NULL;
2174 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04002175
Dave Barach9b8ffd92016-07-08 08:13:45 -04002176 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
2177 unix_cli_pager_prompt_erase (cf, uf);
2178 unix_cli_pager_message (cf, uf, "skipping", "\n");
2179 for (j = cf->pager_start; j < vec_len (cf->pager_index); j++)
2180 {
2181 pi = &cf->pager_index[j];
2182 line = cf->pager_vector[pi->line] + pi->offset;
2183 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2184 }
2185 /* if the last line didn't end in newline, add a newline */
2186 if (pi && line[pi->length - 1] != '\n')
2187 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2188 unix_cli_pager_prompt (cf, uf);
2189 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002190 break;
2191
2192 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
2193 /* wander back one page in the buffer */
2194 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002195 {
2196 u8 *line = NULL;
2197 unix_cli_pager_index_t *pi = NULL;
2198 int m;
Chris Luke862623d2016-05-14 10:13:34 -04002199
Dave Barach9b8ffd92016-07-08 08:13:45 -04002200 if (cf->pager_start >= cf->height)
2201 cf->pager_start -= cf->height - 1;
2202 else
2203 cf->pager_start = 0;
2204 m = cf->pager_start + cf->height - 1;
2205 unix_cli_pager_prompt_erase (cf, uf);
2206 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
2207 j++)
2208 {
2209 pi = &cf->pager_index[j];
2210 line = cf->pager_vector[pi->line] + pi->offset;
2211 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2212 }
2213 /* if the last line didn't end in newline, add a newline */
2214 if (pi && line[pi->length - 1] != '\n')
2215 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2216 unix_cli_pager_prompt (cf, uf);
2217 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002218 break;
2219
Chris Luke862623d2016-05-14 10:13:34 -04002220 case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW:
2221 /* Redraw the current pager screen */
2222 unix_cli_pager_redraw (cf, uf);
2223 break;
2224
Chris Luke7afda3a2016-04-25 13:49:22 -04002225 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
2226 /* search forwards in the buffer */
2227 break;
2228
2229
Chris Luke0aca5eb2016-04-25 13:48:54 -04002230 case UNIX_CLI_PARSE_ACTION_CRLF:
2231 crlf:
Chris Luke0aca5eb2016-04-25 13:48:54 -04002232 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2233
Chris Luke7afda3a2016-04-25 13:49:22 -04002234 if (cf->has_history && cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002235 {
2236 if (cf->command_history
2237 && vec_len (cf->command_history) >= cf->history_limit)
2238 {
2239 vec_free (cf->command_history[0]);
2240 vec_delete (cf->command_history, 1, 0);
2241 }
2242 /* Don't add blank lines to the cmd history */
2243 if (vec_len (cf->current_command))
2244 {
2245 /* Don't duplicate the previous command */
2246 j = vec_len (cf->command_history);
2247 if (j == 0 ||
2248 (vec_len (cf->current_command) !=
2249 vec_len (cf->command_history[j - 1])
2250 || memcmp (cf->current_command, cf->command_history[j - 1],
2251 vec_len (cf->current_command)) != 0))
2252 {
2253 /* copy the command to the history */
2254 u8 *c = 0;
2255 vec_append (c, cf->current_command);
2256 vec_add1 (cf->command_history, c);
2257 cf->command_number++;
2258 }
2259 }
2260 cf->excursion = vec_len (cf->command_history);
2261 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002262
Chris Luke0aca5eb2016-04-25 13:48:54 -04002263 cf->search_mode = 0;
2264 vec_reset_length (cf->search_key);
2265 cf->cursor = 0;
2266
2267 return 0;
2268
Chris Luke7afda3a2016-04-25 13:49:22 -04002269 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2270 case UNIX_CLI_PARSE_ACTION_NOMATCH:
Chris Luke862623d2016-05-14 10:13:34 -04002271 if (vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002272 {
2273 /* no-op for now */
2274 }
Chris Lukefc379d22018-06-05 23:50:19 -04002275 else if (cf->has_history && cf->search_mode != 0 && isprint (input))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002276 {
2277 int k, limit, offset;
2278 u8 *item;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002279
Dave Barach9b8ffd92016-07-08 08:13:45 -04002280 vec_add1 (cf->search_key, input);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002281
Dave Barach9b8ffd92016-07-08 08:13:45 -04002282 search_again:
2283 for (j = 0; j < vec_len (cf->command_history); j++)
2284 {
2285 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
2286 cf->excursion = 0;
2287 else if (cf->excursion < 0)
2288 cf->excursion = vec_len (cf->command_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002289
Dave Barach9b8ffd92016-07-08 08:13:45 -04002290 item = cf->command_history[cf->excursion];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002291
Dave Barach9b8ffd92016-07-08 08:13:45 -04002292 limit = (vec_len (cf->search_key) > vec_len (item)) ?
2293 vec_len (item) : vec_len (cf->search_key);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002294
Dave Barach9b8ffd92016-07-08 08:13:45 -04002295 for (offset = 0; offset <= vec_len (item) - limit; offset++)
2296 {
2297 for (k = 0; k < limit; k++)
2298 {
2299 if (item[k + offset] != cf->search_key[k])
2300 goto next_offset;
2301 }
2302 goto found_at_offset;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002303
Dave Barach9b8ffd92016-07-08 08:13:45 -04002304 next_offset:
2305 ;
2306 }
2307 goto next;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002308
Dave Barach9b8ffd92016-07-08 08:13:45 -04002309 found_at_offset:
Chris Lukefc379d22018-06-05 23:50:19 -04002310 for (; cf->cursor > 0; cf->cursor--)
2311 {
2312 unix_vlib_cli_output_cursor_left (cf, uf);
2313 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
2314 unix_vlib_cli_output_cursor_left (cf, uf);
2315 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002316
Dave Barach9b8ffd92016-07-08 08:13:45 -04002317 vec_validate (cf->current_command, vec_len (item) - 1);
2318 clib_memcpy (cf->current_command, item, vec_len (item));
2319 _vec_len (cf->current_command) = vec_len (item);
Chris Luke93dcd1d2016-05-10 10:45:10 -04002320
Dave Barach9b8ffd92016-07-08 08:13:45 -04002321 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
2322 vec_len (cf->current_command));
2323 cf->cursor = vec_len (cf->current_command);
2324 goto found;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002325
Dave Barach9b8ffd92016-07-08 08:13:45 -04002326 next:
2327 cf->excursion += cf->search_mode;
2328 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002329
Dave Barach9b8ffd92016-07-08 08:13:45 -04002330 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12);
2331 vec_reset_length (cf->search_key);
2332 vec_reset_length (cf->current_command);
2333 cf->search_mode = 0;
2334 cf->cursor = 0;
2335 goto crlf;
2336 }
2337 else if (isprint (input)) /* skip any errant control codes */
2338 {
2339 if (cf->cursor == vec_len (cf->current_command))
2340 {
2341 /* Append to end */
2342 vec_add1 (cf->current_command, input);
2343 cf->cursor++;
Chris Luke7afda3a2016-04-25 13:49:22 -04002344
Dave Barach9b8ffd92016-07-08 08:13:45 -04002345 /* Echo the character back to the client */
Chris Lukefc379d22018-06-05 23:50:19 -04002346 unix_vlib_cli_output_cooked (cf, uf, &input, 1);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002347 }
2348 else
2349 {
2350 /* Insert at cursor: resize +1 byte, move everything over */
2351 j = vec_len (cf->current_command) - cf->cursor;
2352 vec_add1 (cf->current_command, (u8) 'A');
2353 memmove (cf->current_command + cf->cursor + 1,
2354 cf->current_command + cf->cursor, j);
2355 cf->current_command[cf->cursor] = input;
2356 /* Redraw the line */
2357 j++;
Chris Lukefc379d22018-06-05 23:50:19 -04002358 unix_vlib_cli_output_cooked (cf, uf,
2359 cf->current_command + cf->cursor,
2360 j);
2361 cf->cursor += j;
2362 j--;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002363 /* Put terminal cursor back */
Chris Lukefc379d22018-06-05 23:50:19 -04002364 for (; j > 0; j--, cf->cursor--)
2365 unix_vlib_cli_output_cursor_left (cf, uf);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002366 }
2367 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002368 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002369 {
2370 /* no-op - not printable or otherwise not actionable */
2371 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002372
2373 found:
2374
2375 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04002376
2377 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2378 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002379 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04002380 return 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002381}
2382
Chris Luke8e5b0412016-07-26 13:06:10 -04002383/** @brief Process input bytes on a stream to provide line editing and
Chris Luke0aca5eb2016-04-25 13:48:54 -04002384 * command history in the CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002385static int
Damjan Marion56dd5432017-09-08 19:52:02 +02002386unix_cli_line_edit (unix_cli_main_t * cm, unix_main_t * um,
2387 clib_file_main_t * fm, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04002388{
Damjan Marion56dd5432017-09-08 19:52:02 +02002389 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002390 int i;
2391
2392 for (i = 0; i < vec_len (cf->input_vector); i++)
2393 {
2394 unix_cli_parse_action_t action;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002395 i32 matched = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04002396 unix_cli_parse_actions_t *a;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002397
Chris Luke7afda3a2016-04-25 13:49:22 -04002398 /* If we're in the pager mode, search the pager actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002399 a =
2400 vec_len (cf->pager_index) ? unix_cli_parse_pager :
2401 unix_cli_parse_strings;
Chris Luke7afda3a2016-04-25 13:49:22 -04002402
Chris Luke93dcd1d2016-05-10 10:45:10 -04002403 /* See if the input buffer is some sort of control code */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002404 action = unix_cli_match_action (a, &cf->input_vector[i],
2405 vec_len (cf->input_vector) - i,
2406 &matched);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002407
2408 switch (action)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002409 {
2410 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2411 if (i)
2412 {
2413 /* There was a partial match which means we need more bytes
2414 * than the input buffer currently has.
2415 * Since the bytes before here have been processed, shift
2416 * the remaining contents to the start of the input buffer.
2417 */
2418 vec_delete (cf->input_vector, i, 0);
2419 }
2420 return 1; /* wait for more */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002421
Dave Barach9b8ffd92016-07-08 08:13:45 -04002422 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2423 /* process telnet options */
2424 matched = unix_cli_process_telnet (um, cf, uf,
2425 cf->input_vector + i,
2426 vec_len (cf->input_vector) - i);
2427 if (matched < 0)
2428 {
Chris Luke03add7f2017-09-20 23:31:24 -04002429 /* There was a partial match which means we need more bytes
2430 * than the input buffer currently has.
2431 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002432 if (i)
2433 {
Chris Luke03add7f2017-09-20 23:31:24 -04002434 /*
Dave Barach9b8ffd92016-07-08 08:13:45 -04002435 * Since the bytes before here have been processed, shift
2436 * the remaining contents to the start of the input buffer.
2437 */
2438 vec_delete (cf->input_vector, i, 0);
2439 }
2440 return 1; /* wait for more */
2441 }
2442 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002443
Dave Barach9b8ffd92016-07-08 08:13:45 -04002444 default:
Chris Luke03add7f2017-09-20 23:31:24 -04002445 /* If telnet option processing switched us to line mode, get us
2446 * out of here!
2447 */
2448 if (cf->line_mode)
2449 {
2450 vec_delete (cf->input_vector, i, 0);
2451 cf->current_command = cf->input_vector;
2452 return 0;
2453 }
2454
Dave Barach9b8ffd92016-07-08 08:13:45 -04002455 /* process the action */
2456 if (!unix_cli_line_process_one (cm, um, cf, uf,
2457 cf->input_vector[i], action))
2458 {
2459 /* CRLF found. Consume the bytes from the input_vector */
2460 vec_delete (cf->input_vector, i + matched, 0);
2461 /* And tell our caller to execute cf->input_command */
2462 return 0;
2463 }
2464 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002465
2466 i += matched;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002467 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002468
Dave Barach9b8ffd92016-07-08 08:13:45 -04002469 vec_reset_length (cf->input_vector);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002470 return 1;
2471}
2472
Chris Luke8e5b0412016-07-26 13:06:10 -04002473/** @brief Process input to a CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002474static void
2475unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002476{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002477 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002478 clib_file_main_t *fm = &file_main;
2479 clib_file_t *uf;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002480 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002481 unformat_input_t input;
2482 int vlib_parse_eval (u8 *);
2483
Chris Luke03add7f2017-09-20 23:31:24 -04002484 cm->current_input_file_index = cli_file_index;
2485
Chris Luke93dcd1d2016-05-10 10:45:10 -04002486more:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002487 /* Try vlibplex first. Someday... */
2488 if (0 && vlib_parse_eval (cf->input_vector) == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002489 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002490
Chris Luke03add7f2017-09-20 23:31:24 -04002491
Chris Luke93dcd1d2016-05-10 10:45:10 -04002492 if (cf->line_mode)
2493 {
2494 /* just treat whatever we got as a complete line of input */
2495 cf->current_command = cf->input_vector;
2496 }
2497 else
2498 {
2499 /* Line edit, echo, etc. */
Damjan Marion56dd5432017-09-08 19:52:02 +02002500 if (unix_cli_line_edit (cm, um, fm, cf))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002501 /* want more input */
2502 return;
Chris Luke93dcd1d2016-05-10 10:45:10 -04002503 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002504
2505 if (um->log_fd)
2506 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002507 static u8 *lv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002508 vec_reset_length (lv);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002509 lv = format (lv, "%U[%d]: %v",
2510 format_timeval, 0 /* current bat-time */ ,
2511 0 /* current bat-format */ ,
Chris Luke03add7f2017-09-20 23:31:24 -04002512 cli_file_index, cf->current_command);
2513 int rv __attribute__ ((unused)) = write (um->log_fd, lv, vec_len (lv));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002514 }
2515
Chris Luke03add7f2017-09-20 23:31:24 -04002516 /* Build an unformat structure around our command */
Chris Luke93dcd1d2016-05-10 10:45:10 -04002517 unformat_init_vector (&input, cf->current_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002518
2519 /* Remove leading white space from input. */
2520 (void) unformat (&input, "");
2521
Dave Barach9b8ffd92016-07-08 08:13:45 -04002522 cf->pager_start = 0; /* start a new pager session */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002523
2524 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002525 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output,
2526 cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002527
Ed Warnickecb9cada2015-12-08 15:45:58 -07002528 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
2529 input.buffer = 0;
2530
2531 unformat_free (&input);
2532
Chris Luke93dcd1d2016-05-10 10:45:10 -04002533 /* Re-fetch pointer since pool may have moved. */
2534 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002535 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke93dcd1d2016-05-10 10:45:10 -04002536
Ed Warnickecb9cada2015-12-08 15:45:58 -07002537done:
Chris Luke93dcd1d2016-05-10 10:45:10 -04002538 /* reset vector; we'll re-use it later */
2539 if (cf->line_mode)
Chris Luke03add7f2017-09-20 23:31:24 -04002540 {
2541 vec_reset_length (cf->input_vector);
2542 cf->current_command = 0;
2543 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002544 else
Chris Luke03add7f2017-09-20 23:31:24 -04002545 {
2546 vec_reset_length (cf->current_command);
2547 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002548
Chris Luke7afda3a2016-04-25 13:49:22 -04002549 if (cf->no_pager == 2)
2550 {
2551 /* Pager was programmatically disabled */
2552 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
2553 cf->no_pager = um->cli_no_pager;
2554 }
2555
Dave Barach9b8ffd92016-07-08 08:13:45 -04002556 if (vec_len (cf->pager_index) == 0
2557 || vec_len (cf->pager_index) < cf->height)
Chris Luke7afda3a2016-04-25 13:49:22 -04002558 {
2559 /* There was no need for the pager */
2560 unix_cli_pager_reset (cf);
2561
2562 /* Prompt. */
2563 unix_cli_cli_prompt (cf, uf);
2564 }
2565 else
2566 {
2567 /* Display the pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002568 unix_cli_pager_prompt (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04002569 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002570
Dave Barach9b8ffd92016-07-08 08:13:45 -04002571 /* Any residual data in the input vector? */
2572 if (vec_len (cf->input_vector))
2573 goto more;
Chris Luke03add7f2017-09-20 23:31:24 -04002574
2575 /* For non-interactive sessions send a NUL byte.
2576 * Specifically this is because vppctl needs to see some traffic in
2577 * order to move on to closing the session. Commands with no output
2578 * would thus cause vppctl to hang indefinitely in non-interactive mode
2579 * since there is also no prompt sent after the command completes.
2580 */
2581 if (!cf->is_interactive)
2582 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\0", 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002583}
2584
Chris Luke54ccf222016-07-25 16:38:11 -04002585/** Destroy a CLI session.
2586 * @note If we destroy the @c stdin session this additionally signals
2587 * the shutdown of VPP.
2588 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002589static void
2590unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002591{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002592 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002593 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002594 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +02002595 clib_file_t *uf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002596 int i;
2597
Steve Shin0d5a1952018-06-29 09:40:20 -07002598 /* Validate cli_file_index */
2599 if (pool_is_free_index (cm->cli_file_pool, cli_file_index))
2600 return;
2601
Ed Warnickecb9cada2015-12-08 15:45:58 -07002602 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002603 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002604
2605 /* Quit/EOF on stdin means quit program. */
Chris Luke03add7f2017-09-20 23:31:24 -04002606 if (uf->file_descriptor == STDIN_FILENO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002607 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
2608
2609 vec_free (cf->current_command);
2610 vec_free (cf->search_key);
2611
2612 for (i = 0; i < vec_len (cf->command_history); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002613 vec_free (cf->command_history[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002614
2615 vec_free (cf->command_history);
2616
Damjan Marion56dd5432017-09-08 19:52:02 +02002617 clib_file_del (fm, uf);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002618
2619 unix_cli_file_free (cf);
2620 pool_put (cm->cli_file_pool, cf);
2621}
2622
Chris Luke54ccf222016-07-25 16:38:11 -04002623/** Handle system events. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002624static uword
2625unix_cli_process (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002626 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002627{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002628 unix_cli_main_t *cm = &unix_cli_main;
2629 uword i, *data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002630
2631 while (1)
2632 {
2633 unix_cli_process_event_type_t event_type;
2634 vlib_process_wait_for_event (vm);
2635 event_type = vlib_process_get_events (vm, &data);
2636
2637 switch (event_type)
2638 {
2639 case UNIX_CLI_PROCESS_EVENT_READ_READY:
2640 for (i = 0; i < vec_len (data); i++)
2641 unix_cli_process_input (cm, data[i]);
2642 break;
2643
2644 case UNIX_CLI_PROCESS_EVENT_QUIT:
2645 /* Kill this process. */
2646 for (i = 0; i < vec_len (data); i++)
2647 unix_cli_kill (cm, data[i]);
2648 goto done;
2649 }
2650
2651 if (data)
2652 _vec_len (data) = 0;
2653 }
2654
Dave Barach9b8ffd92016-07-08 08:13:45 -04002655done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002656 vec_free (data);
2657
2658 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
2659
2660 /* Add node index so we can re-use this process later. */
2661 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
2662
2663 return 0;
2664}
2665
Chris Luke54ccf222016-07-25 16:38:11 -04002666/** Called when a CLI session file descriptor can be written to without
2667 * blocking. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002668static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002669unix_cli_write_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002670{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002671 unix_cli_main_t *cm = &unix_cli_main;
2672 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002673 int n;
2674
2675 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2676
2677 /* Flush output vector. */
Chris Luke03add7f2017-09-20 23:31:24 -04002678 if (cf->is_socket)
2679 /* If it's a socket we use MSG_NOSIGNAL to prevent SIGPIPE */
2680 n = send (uf->file_descriptor,
2681 cf->output_vector, vec_len (cf->output_vector), MSG_NOSIGNAL);
2682 else
2683 n = write (uf->file_descriptor,
2684 cf->output_vector, vec_len (cf->output_vector));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002685
2686 if (n < 0 && errno != EAGAIN)
Chris Luke03add7f2017-09-20 23:31:24 -04002687 {
2688 if (errno == EPIPE)
2689 {
2690 /* connection closed on us */
2691 unix_main_t *um = &unix_main;
2692 cf->has_epipe = 1;
2693 vlib_process_signal_event (um->vlib_main, cf->process_node_index,
2694 UNIX_CLI_PROCESS_EVENT_QUIT,
2695 uf->private_data);
2696 }
2697 else
2698 {
2699 return clib_error_return_unix (0, "write");
2700 }
2701 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002702
2703 else if (n > 0)
2704 unix_cli_del_pending_output (uf, cf, n);
2705
2706 return /* no error */ 0;
2707}
2708
Chris Luke54ccf222016-07-25 16:38:11 -04002709/** Called when a CLI session file descriptor has data to be read. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002710static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002711unix_cli_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002712{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002713 unix_main_t *um = &unix_main;
2714 unix_cli_main_t *cm = &unix_cli_main;
2715 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002716 uword l;
2717 int n, n_read, n_try;
2718
2719 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2720
2721 n = n_try = 4096;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002722 while (n == n_try)
2723 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002724 l = vec_len (cf->input_vector);
2725 vec_resize (cf->input_vector, l + n_try);
2726
2727 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
2728
2729 /* Error? */
2730 if (n < 0 && errno != EAGAIN)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002731 return clib_error_return_unix (0, "read");
2732
Ed Warnickecb9cada2015-12-08 15:45:58 -07002733 n_read = n < 0 ? 0 : n;
2734 _vec_len (cf->input_vector) = l + n_read;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002735 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002736
Dave Barach9b8ffd92016-07-08 08:13:45 -04002737 if (!(n < 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002738 vlib_process_signal_event (um->vlib_main,
2739 cf->process_node_index,
2740 (n_read == 0
2741 ? UNIX_CLI_PROCESS_EVENT_QUIT
2742 : UNIX_CLI_PROCESS_EVENT_READ_READY),
2743 /* event data */ uf->private_data);
2744
2745 return /* no error */ 0;
2746}
2747
Chris Luke03add7f2017-09-20 23:31:24 -04002748/** Called when a CLI session file descriptor has an error condition. */
2749static clib_error_t *
2750unix_cli_error_detected (clib_file_t * uf)
2751{
2752 unix_main_t *um = &unix_main;
2753 unix_cli_main_t *cm = &unix_cli_main;
2754 unix_cli_file_t *cf;
2755
2756 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2757 cf->has_epipe = 1; /* prevent writes while the close is pending */
2758 vlib_process_signal_event (um->vlib_main,
2759 cf->process_node_index,
2760 UNIX_CLI_PROCESS_EVENT_QUIT,
2761 /* event data */ uf->private_data);
2762
2763 return /* no error */ 0;
2764}
2765
Chris Lukeb5850972016-05-03 16:34:59 -04002766/** Store a new CLI session.
2767 * @param name The name of the session.
2768 * @param fd The file descriptor for the session I/O.
2769 * @return The session ID.
2770 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002771static u32
2772unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002773{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002774 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002775 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002776 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +02002777 clib_file_t template = { 0 };
Dave Barach9b8ffd92016-07-08 08:13:45 -04002778 vlib_main_t *vm = um->vlib_main;
Dave Barach06100392018-07-12 13:00:47 -04002779 vlib_node_t *n = 0;
Damjan Marionceab7882018-01-19 20:56:12 +01002780 u8 *file_desc = 0;
2781
2782 file_desc = format (0, "%s", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002783
2784 name = (char *) format (0, "unix-cli-%s", name);
2785
2786 if (vec_len (cm->unused_cli_process_node_indices) > 0)
2787 {
2788 uword l = vec_len (cm->unused_cli_process_node_indices);
Dave Barach06100392018-07-12 13:00:47 -04002789 int i;
2790 vlib_main_t *this_vlib_main;
2791 u8 *old_name = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002792
Dave Barach06100392018-07-12 13:00:47 -04002793 /*
2794 * Nodes are bulk-copied, so node name pointers are shared.
2795 * Find the cli node in all graph replicas, and give all of them
2796 * the same new name.
2797 * Then, throw away the old shared name-vector.
2798 */
2799 for (i = 0; i < vec_len (vlib_mains); i++)
2800 {
2801 this_vlib_main = vlib_mains[i];
2802 if (this_vlib_main == 0)
2803 continue;
2804 n = vlib_get_node (this_vlib_main,
2805 cm->unused_cli_process_node_indices[l - 1]);
2806 old_name = n->name;
2807 n->name = (u8 *) name;
2808 }
2809 vec_free (old_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002810
2811 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
2812
2813 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
2814 }
2815 else
2816 {
2817 static vlib_node_registration_t r = {
2818 .function = unix_cli_process,
2819 .type = VLIB_NODE_TYPE_PROCESS,
Dave Barach96e12032016-06-09 15:32:48 -04002820 .process_log2_n_stack_bytes = 16,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002821 };
2822
2823 r.name = name;
Dave Barach06100392018-07-12 13:00:47 -04002824
2825 vlib_worker_thread_barrier_sync (vm);
2826
Ed Warnickecb9cada2015-12-08 15:45:58 -07002827 vlib_register_node (vm, &r);
2828 vec_free (name);
2829
2830 n = vlib_get_node (vm, r.index);
Dave Barach06100392018-07-12 13:00:47 -04002831 vlib_worker_thread_node_runtime_update ();
2832 vlib_worker_thread_barrier_release (vm);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002833 }
2834
2835 pool_get (cm->cli_file_pool, cf);
Dave Barachb7b92992018-10-17 10:38:51 -04002836 clib_memset (cf, 0, sizeof (*cf));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002837
2838 template.read_function = unix_cli_read_ready;
2839 template.write_function = unix_cli_write_ready;
Chris Luke03add7f2017-09-20 23:31:24 -04002840 template.error_function = unix_cli_error_detected;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002841 template.file_descriptor = fd;
2842 template.private_data = cf - cm->cli_file_pool;
Damjan Marionceab7882018-01-19 20:56:12 +01002843 template.description = file_desc;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002844
2845 cf->process_node_index = n->index;
Damjan Marion56dd5432017-09-08 19:52:02 +02002846 cf->clib_file_index = clib_file_add (fm, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002847 cf->output_vector = 0;
2848 cf->input_vector = 0;
2849
Ed Warnickecb9cada2015-12-08 15:45:58 -07002850 vlib_start_process (vm, n->runtime_index);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002851
Dave Barach9b8ffd92016-07-08 08:13:45 -04002852 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002853 p->output_function = unix_vlib_cli_output;
2854 p->output_function_arg = cf - cm->cli_file_pool;
2855
Ed Warnickecb9cada2015-12-08 15:45:58 -07002856 return cf - cm->cli_file_pool;
2857}
2858
Chris Lukeb5850972016-05-03 16:34:59 -04002859/** Telnet listening socket has a new connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002860static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002861unix_cli_listen_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002862{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002863 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002864 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002865 unix_cli_main_t *cm = &unix_cli_main;
2866 clib_socket_t *s = &um->cli_listen_socket;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002867 clib_socket_t client;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002868 char *client_name;
2869 clib_error_t *error;
2870 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002871 u32 cf_index;
Chris Lukea9cf6af2018-09-05 21:00:52 -04002872 int one;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002873
2874 error = clib_socket_accept (s, &client);
2875 if (error)
2876 return error;
2877
Chris Lukea9cf6af2018-09-05 21:00:52 -04002878 /* Disable Nagle, ignore any errors doing so eg on PF_LOCAL socket */
2879 one = 1;
Chris Lukec5cb6382018-09-06 15:37:28 -04002880 (void) setsockopt (client.fd, IPPROTO_TCP, TCP_NODELAY,
2881 (void *) &one, sizeof (one));
Chris Lukea9cf6af2018-09-05 21:00:52 -04002882
Ed Warnickecb9cada2015-12-08 15:45:58 -07002883 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
2884
2885 cf_index = unix_cli_file_add (cm, client_name, client.fd);
2886 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
Chris Luke03add7f2017-09-20 23:31:24 -04002887 cf->is_socket = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002888
2889 /* No longer need CLIB version of socket. */
2890 clib_socket_free (&client);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002891 vec_free (client_name);
2892
2893 /* if we're supposed to run telnet session in character mode (default) */
2894 if (um->cli_line_mode == 0)
2895 {
Chris Luke0aca5eb2016-04-25 13:48:54 -04002896 /*
2897 * Set telnet client character mode, echo on, suppress "go-ahead".
2898 * Technically these should be negotiated, but this works.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002899 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002900 u8 charmode_option[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002901 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
2902 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
2903 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
2904 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
2905 IAC, WILL, TELOPT_ECHO, /* server will do echo */
2906 IAC, DONT, TELOPT_ECHO, /* client should not echo */
2907 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
2908 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
2909 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
2910 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002911 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002912
Chris Luke0aca5eb2016-04-25 13:48:54 -04002913 /* Enable history on this CLI */
Chris Luke7afda3a2016-04-25 13:49:22 -04002914 cf->history_limit = um->cli_history_limit;
2915 cf->has_history = cf->history_limit != 0;
2916
Chris Luke03add7f2017-09-20 23:31:24 -04002917 /* This is an interactive session until we decide otherwise */
2918 cf->is_interactive = 1;
2919
Chris Luke7afda3a2016-04-25 13:49:22 -04002920 /* Make sure this session is in line mode */
2921 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002922
2923 /* We need CRLF */
2924 cf->crlf_mode = 1;
2925
Chris Luke7afda3a2016-04-25 13:49:22 -04002926 /* Setup the pager */
2927 cf->no_pager = um->cli_no_pager;
2928
Chris Luke2d8bf302017-11-12 22:26:37 -05002929 /* Default terminal dimensions, should the terminal
2930 * fail to provide any.
2931 */
2932 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
2933 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
2934
Chris Luke0aca5eb2016-04-25 13:48:54 -04002935 /* Send the telnet options */
Chris Luke03add7f2017-09-20 23:31:24 -04002936 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002937 unix_vlib_cli_output_raw (cf, uf, charmode_option,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002938 ARRAY_LEN (charmode_option));
Chris Luke0aca5eb2016-04-25 13:48:54 -04002939
Chris Luke6a3a4f72019-07-09 23:33:30 -04002940 if (cm->new_session_process_node_index == ~0)
2941 {
2942 /* Create thw new session deadline process */
2943 cm->new_session_process_node_index =
2944 vlib_process_create (um->vlib_main, "unix-cli-new-session",
2945 unix_cli_new_session_process,
2946 16 /* log2_n_stack_bytes */ );
2947 }
2948
2949 /* In case the client doesn't negotiate terminal type, register
2950 * our session with a process that will emit the prompt if
2951 * a deadline passes */
2952 vlib_process_signal_event (um->vlib_main,
2953 cm->new_session_process_node_index,
2954 UNIX_CLI_NEW_SESSION_EVENT_ADD, cf_index);
2955
Ed Warnickecb9cada2015-12-08 15:45:58 -07002956 }
2957
2958 return error;
2959}
2960
Chris Lukeb5850972016-05-03 16:34:59 -04002961/** The system terminal has informed us that the window size
2962 * has changed.
2963 */
Chris Luke7afda3a2016-04-25 13:49:22 -04002964static void
2965unix_cli_resize_interrupt (int signum)
2966{
Damjan Marion56dd5432017-09-08 19:52:02 +02002967 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002968 unix_cli_main_t *cm = &unix_cli_main;
2969 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2970 cm->stdin_cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002971 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04002972 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002973 (void) signum;
Chris Luke7afda3a2016-04-25 13:49:22 -04002974
2975 /* Terminal resized, fetch the new size */
Chris Luke03add7f2017-09-20 23:31:24 -04002976 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
Dave Barachb2a6e252016-07-27 10:00:58 -04002977 {
2978 /* "Should never happen..." */
2979 clib_unix_warning ("TIOCGWINSZ");
2980 /* We can't trust ws.XXX... */
2981 return;
2982 }
Chris Lukeb2bcad62017-09-18 08:51:22 -04002983
Chris Luke7afda3a2016-04-25 13:49:22 -04002984 cf->width = ws.ws_col;
Chris Lukeb2bcad62017-09-18 08:51:22 -04002985 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
2986 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
Chris Luke2d8bf302017-11-12 22:26:37 -05002987 if (cf->width == 0)
2988 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
Chris Lukeb2bcad62017-09-18 08:51:22 -04002989
Chris Luke7afda3a2016-04-25 13:49:22 -04002990 cf->height = ws.ws_row;
Chris Lukeb2bcad62017-09-18 08:51:22 -04002991 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
2992 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
Chris Luke2d8bf302017-11-12 22:26:37 -05002993 if (cf->height == 0)
2994 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
Chris Luke862623d2016-05-14 10:13:34 -04002995
2996 /* Reindex the pager buffer */
2997 unix_cli_pager_reindex (cf);
2998
2999 /* Redraw the page */
3000 unix_cli_pager_redraw (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04003001}
3002
Chris Lukeb5850972016-05-03 16:34:59 -04003003/** Handle configuration directives in the @em unix section. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003004static clib_error_t *
3005unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
3006{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003007 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02003008 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003009 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003010 int flags;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003011 clib_error_t *error = 0;
3012 unix_cli_file_t *cf;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003013 u32 cf_index;
3014 struct termios tio;
Chris Luke7afda3a2016-04-25 13:49:22 -04003015 struct sigaction sa;
3016 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003017 u8 *term;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003018
3019 /* We depend on unix flags being set. */
3020 if ((error = vlib_call_config_function (vm, unix_config)))
3021 return error;
3022
3023 if (um->flags & UNIX_FLAG_INTERACTIVE)
3024 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07003025 /* Set stdin to be non-blocking. */
Chris Luke03add7f2017-09-20 23:31:24 -04003026 if ((flags = fcntl (STDIN_FILENO, F_GETFL, 0)) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04003027 flags = 0;
Chris Luke03add7f2017-09-20 23:31:24 -04003028 (void) fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003029
Chris Luke03add7f2017-09-20 23:31:24 -04003030 cf_index = unix_cli_file_add (cm, "stdin", STDIN_FILENO);
Chris Luke0aca5eb2016-04-25 13:48:54 -04003031 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04003032 cm->stdin_cli_file_index = cf_index;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003033
3034 /* If stdin is a tty and we are using chacracter mode, enable
3035 * history on the CLI and set the tty line discipline accordingly. */
Chris Luke03add7f2017-09-20 23:31:24 -04003036 if (isatty (STDIN_FILENO) && um->cli_line_mode == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04003037 {
3038 /* Capture terminal resize events */
Dave Barachb7b92992018-10-17 10:38:51 -04003039 clib_memset (&sa, 0, sizeof (sa));
Dave Barach9b8ffd92016-07-08 08:13:45 -04003040 sa.sa_handler = unix_cli_resize_interrupt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003041 if (sigaction (SIGWINCH, &sa, 0) < 0)
3042 clib_panic ("sigaction");
Chris Luke7afda3a2016-04-25 13:49:22 -04003043
Dave Barach9b8ffd92016-07-08 08:13:45 -04003044 /* Retrieve the current terminal size */
Chris Luke03add7f2017-09-20 23:31:24 -04003045 ioctl (STDIN_FILENO, TIOCGWINSZ, &ws);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003046 cf->width = ws.ws_col;
3047 cf->height = ws.ws_row;
Chris Luke7afda3a2016-04-25 13:49:22 -04003048
Dave Barach9b8ffd92016-07-08 08:13:45 -04003049 if (cf->width == 0 || cf->height == 0)
Dave Barachb66201f2017-09-22 13:34:39 -04003050 {
3051 /*
3052 * We have a tty, but no size. Use defaults.
3053 * vpp "unix interactive" inside emacs + gdb ends up here.
3054 */
Chris Luke2d8bf302017-11-12 22:26:37 -05003055 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
3056 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
Dave Barachb66201f2017-09-22 13:34:39 -04003057 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003058
Dave Barach9b8ffd92016-07-08 08:13:45 -04003059 /* Setup the history */
3060 cf->history_limit = um->cli_history_limit;
3061 cf->has_history = cf->history_limit != 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003062
Dave Barach9b8ffd92016-07-08 08:13:45 -04003063 /* Setup the pager */
3064 cf->no_pager = um->cli_no_pager;
Chris Luke7afda3a2016-04-25 13:49:22 -04003065
Chris Luke03add7f2017-09-20 23:31:24 -04003066 /* This is an interactive session until we decide otherwise */
3067 cf->is_interactive = 1;
3068
Dave Barach9b8ffd92016-07-08 08:13:45 -04003069 /* We're going to be in char by char mode */
3070 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003071
Dave Barach9b8ffd92016-07-08 08:13:45 -04003072 /* Save the original tty state so we can restore it later */
Chris Luke03add7f2017-09-20 23:31:24 -04003073 tcgetattr (STDIN_FILENO, &um->tio_stdin);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003074 um->tio_isset = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003075
Dave Barach9b8ffd92016-07-08 08:13:45 -04003076 /* Tweak the tty settings */
3077 tio = um->tio_stdin;
3078 /* echo off, canonical mode off, ext'd input processing off */
3079 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
Chris Luke01dc6b92018-06-10 13:42:45 -04003080 /* disable XON/XOFF, so ^S invokes the history search */
3081 tio.c_iflag &= ~(IXON | IXOFF);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003082 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
3083 tio.c_cc[VTIME] = 0; /* no timer */
Chris Luke01dc6b92018-06-10 13:42:45 -04003084 tio.c_cc[VSTOP] = _POSIX_VDISABLE; /* not ^S */
3085 tio.c_cc[VSTART] = _POSIX_VDISABLE; /* not ^Q */
Chris Luke03add7f2017-09-20 23:31:24 -04003086 tcsetattr (STDIN_FILENO, TCSAFLUSH, &tio);
Chris Luke0aca5eb2016-04-25 13:48:54 -04003087
Dave Barach9b8ffd92016-07-08 08:13:45 -04003088 /* See if we can do ANSI/VT100 output */
3089 term = (u8 *) getenv ("TERM");
3090 if (term != NULL)
Chris Luke03add7f2017-09-20 23:31:24 -04003091 {
3092 int len = strlen ((char *) term);
3093 cf->ansi_capable = unix_cli_terminal_type_ansi (term, len);
3094 if (unix_cli_terminal_type_noninteractive (term, len))
3095 unix_cli_set_session_noninteractive (cf);
3096 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04003097 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003098 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04003099 {
Chris Luke03add7f2017-09-20 23:31:24 -04003100 /* No tty, so make sure the session doesn't have tty-like features */
3101 unix_cli_set_session_noninteractive (cf);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003102 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04003103
3104 /* Send banner and initial prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04003105 unix_cli_file_welcome (cm, cf);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003106 }
3107
Ed Warnickea25bd1c2016-04-01 22:43:37 -05003108 /* If we have socket config, LISTEN, otherwise, don't */
Dave Barach9b8ffd92016-07-08 08:13:45 -04003109 clib_socket_t *s = &um->cli_listen_socket;
3110 if (s->config && s->config[0] != 0)
3111 {
3112 /* CLI listen. */
Damjan Marion56dd5432017-09-08 19:52:02 +02003113 clib_file_t template = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07003114
Damjan Marion57d963f2017-07-20 19:17:06 +02003115 /* mkdir of file socketu, only under /run */
3116 if (strncmp (s->config, "/run", 4) == 0)
Chris Luke475674e2017-07-05 18:02:53 -04003117 {
Damjan Marion57d963f2017-07-20 19:17:06 +02003118 u8 *tmp = format (0, "%s", s->config);
3119 int i = vec_len (tmp);
3120 while (i && tmp[--i] != '/')
3121 ;
3122
3123 tmp[i] = 0;
3124
3125 if (i)
3126 vlib_unix_recursive_mkdir ((char *) tmp);
3127 vec_free (tmp);
Chris Luke475674e2017-07-05 18:02:53 -04003128 }
3129
Damjan Marionf49921f2017-09-11 16:52:11 +02003130 s->flags = CLIB_SOCKET_F_IS_SERVER | /* listen, don't connect */
3131 CLIB_SOCKET_F_ALLOW_GROUP_WRITE; /* PF_LOCAL socket only */
Dave Barach9b8ffd92016-07-08 08:13:45 -04003132 error = clib_socket_init (s);
Ed Warnickea25bd1c2016-04-01 22:43:37 -05003133
Dave Barach9b8ffd92016-07-08 08:13:45 -04003134 if (error)
3135 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003136
Dave Barach9b8ffd92016-07-08 08:13:45 -04003137 template.read_function = unix_cli_listen_read_ready;
3138 template.file_descriptor = s->fd;
Damjan Marionceab7882018-01-19 20:56:12 +01003139 template.description = format (0, "cli listener %s", s->config);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003140
Damjan Marion56dd5432017-09-08 19:52:02 +02003141 clib_file_add (fm, &template);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003142 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07003143
3144 /* Set CLI prompt. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04003145 if (!cm->cli_prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003146 cm->cli_prompt = format (0, "VLIB: ");
3147
3148 return 0;
3149}
3150
Chris Luke90f52bf2016-09-12 08:55:13 -04003151/*?
3152 * This module has no configurable parameters.
3153?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07003154VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
3155
Chris Luke54ccf222016-07-25 16:38:11 -04003156/** Called when VPP is shutting down, this restores the system
3157 * terminal state if previously saved.
Chris Lukeb5850972016-05-03 16:34:59 -04003158 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04003159static clib_error_t *
3160unix_cli_exit (vlib_main_t * vm)
3161{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003162 unix_main_t *um = &unix_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04003163
3164 /* If stdin is a tty and we saved the tty state, reset the tty state */
Chris Luke03add7f2017-09-20 23:31:24 -04003165 if (isatty (STDIN_FILENO) && um->tio_isset)
3166 tcsetattr (STDIN_FILENO, TCSAFLUSH, &um->tio_stdin);
Chris Luke0aca5eb2016-04-25 13:48:54 -04003167
3168 return 0;
3169}
3170
3171VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
3172
Chris Lukeb5850972016-05-03 16:34:59 -04003173/** Set the CLI prompt.
Chris Luke54ccf222016-07-25 16:38:11 -04003174 * @param prompt The C string to set the prompt to.
Chris Lukeb5850972016-05-03 16:34:59 -04003175 * @note This setting is global; it impacts all current
3176 * and future CLI sessions.
3177 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04003178void
3179vlib_unix_cli_set_prompt (char *prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003180{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003181 char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s ";
3182 unix_cli_main_t *cm = &unix_cli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003183 if (cm->cli_prompt)
3184 vec_free (cm->cli_prompt);
3185 cm->cli_prompt = format (0, fmt, prompt);
3186}
3187
Chris Lukeb5850972016-05-03 16:34:59 -04003188/** CLI command to quit the terminal session.
3189 * @note If this is a stdin session then this will
3190 * shutdown VPP also.
3191 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003192static clib_error_t *
3193unix_cli_quit (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003194 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003195{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003196 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke03add7f2017-09-20 23:31:24 -04003197 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
3198 cm->current_input_file_index);
3199
3200 /* Cosmetic: suppress the final prompt from appearing before we die */
3201 cf->is_interactive = 0;
3202 cf->started = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003203
3204 vlib_process_signal_event (vm,
3205 vlib_current_process (vm),
3206 UNIX_CLI_PROCESS_EVENT_QUIT,
3207 cm->current_input_file_index);
3208 return 0;
3209}
3210
Chris Luke54ccf222016-07-25 16:38:11 -04003211/*?
3212 * Terminates the current CLI session.
3213 *
3214 * If VPP is running in @em interactive mode and this is the console session
3215 * (that is, the session on @c stdin) then this will also terminate VPP.
3216?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003217/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003218VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
3219 .path = "quit",
3220 .short_help = "Exit CLI",
3221 .function = unix_cli_quit,
3222};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003223/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003224
Florin Coras33d16292018-03-16 09:13:37 -07003225/* *INDENT-OFF* */
3226VLIB_CLI_COMMAND (unix_cli_q_command, static) = {
3227 .path = "q",
3228 .short_help = "Exit CLI",
3229 .function = unix_cli_quit,
3230};
3231/* *INDENT-ON* */
3232
Chris Lukeb5850972016-05-03 16:34:59 -04003233/** CLI command to execute a VPP command script. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003234static clib_error_t *
3235unix_cli_exec (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003236 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003237{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003238 char *file_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003239 int fd;
3240 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003241 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003242
3243 file_name = 0;
3244 fd = -1;
3245 error = 0;
3246
Dave Barach9b8ffd92016-07-08 08:13:45 -04003247 if (!unformat (input, "%s", &file_name))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003248 {
3249 error = clib_error_return (0, "expecting file name, got `%U'",
3250 format_unformat_error, input);
3251 goto done;
3252 }
3253
3254 fd = open (file_name, O_RDONLY);
3255 if (fd < 0)
3256 {
3257 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
3258 goto done;
3259 }
3260
3261 /* Make sure its a regular file. */
3262 {
3263 struct stat s;
3264
3265 if (fstat (fd, &s) < 0)
3266 {
3267 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
3268 goto done;
3269 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04003270
3271 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003272 {
3273 error = clib_error_return (0, "not a regular file `%s'", file_name);
3274 goto done;
3275 }
3276 }
3277
Dave Barach59b25652017-09-10 15:04:27 -04003278 unformat_init_clib_file (&sub_input, fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003279
3280 vlib_cli_input (vm, &sub_input, 0, 0);
3281 unformat_free (&sub_input);
3282
Dave Barach9b8ffd92016-07-08 08:13:45 -04003283done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003284 if (fd > 0)
3285 close (fd);
3286 vec_free (file_name);
3287
3288 return error;
3289}
3290
Chris Luke54ccf222016-07-25 16:38:11 -04003291/*?
Billy McFall28cf3b72018-01-15 17:54:52 -05003292 * Executes a sequence of CLI commands which are read from a file. If
3293 * a command is unrecognised or otherwise invalid then the usual CLI
Chris Luke54ccf222016-07-25 16:38:11 -04003294 * feedback will be generated, however execution of subsequent commands
3295 * from the file will continue.
Billy McFall28cf3b72018-01-15 17:54:52 -05003296 *
3297 * The VPP code is indifferent to the file location. However, if SELinux
3298 * is enabled, then the file needs to have an SELinux label the VPP
3299 * process is allowed to access. For example, if a file is created in
3300 * '<em>/usr/share/vpp/</em>', it will be allowed. However, files manually
3301 * created in '/tmp/' or '/home/<user>/' will not be accessible by the VPP
3302 * process when SELinux is enabled.
3303 *
3304 * @cliexpar
3305 * Sample file:
3306 * @clistart
3307 * <b><em>$ cat /usr/share/vpp/scripts/gigup.txt</em></b>
3308 * set interface state GigabitEthernet0/8/0 up
3309 * set interface state GigabitEthernet0/9/0 up
3310 * @cliend
3311 * Example of how to execute a set of CLI commands from a file:
3312 * @cliexcmd{exec /usr/share/vpp/scripts/gigup.txt}
Chris Luke54ccf222016-07-25 16:38:11 -04003313?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003314/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003315VLIB_CLI_COMMAND (cli_exec, static) = {
3316 .path = "exec",
Billy McFall28cf3b72018-01-15 17:54:52 -05003317 .short_help = "exec <filename>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003318 .function = unix_cli_exec,
Dave Barachb2ef4dd2016-03-23 08:56:01 -04003319 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003320};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003321/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003322
Chris Lukeb5850972016-05-03 16:34:59 -04003323/** CLI command to show various unix error statistics. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003324static clib_error_t *
3325unix_show_errors (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003326 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003327{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003328 unix_main_t *um = &unix_main;
3329 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003330 int i, n_errors_to_show;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003331 unix_error_history_t *unix_errors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003332
3333 n_errors_to_show = 1 << 30;
3334
3335 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3336 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003337 if (!unformat (input, "%d", &n_errors_to_show))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003338 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003339 error =
3340 clib_error_return (0,
3341 "expecting integer number of errors to show, got `%U'",
3342 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003343 goto done;
3344 }
3345 }
3346
Dave Barach9b8ffd92016-07-08 08:13:45 -04003347 n_errors_to_show =
3348 clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003349
Dave Barach9b8ffd92016-07-08 08:13:45 -04003350 i =
3351 um->error_history_index >
3352 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003353
3354 while (n_errors_to_show > 0)
3355 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003356 unix_error_history_t *eh = um->error_history + i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003357
Dave Barach9b8ffd92016-07-08 08:13:45 -04003358 if (!eh->error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003359 break;
3360
3361 vec_add1 (unix_errors, eh[0]);
3362 n_errors_to_show -= 1;
3363 if (i == 0)
3364 i = ARRAY_LEN (um->error_history) - 1;
3365 else
3366 i--;
3367 }
3368
3369 if (vec_len (unix_errors) == 0)
3370 vlib_cli_output (vm, "no Unix errors so far");
3371 else
3372 {
3373 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
3374 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
3375 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003376 unix_error_history_t *eh = vec_elt_at_index (unix_errors, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003377 vlib_cli_output (vm, "%U: %U",
3378 format_time_interval, "h:m:s:u", eh->time,
3379 format_clib_error, eh->error);
3380 }
3381 vlib_cli_output (vm, "%U: time now",
3382 format_time_interval, "h:m:s:u", vlib_time_now (vm));
3383 }
3384
Dave Barach9b8ffd92016-07-08 08:13:45 -04003385done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003386 vec_free (unix_errors);
3387 return error;
3388}
3389
Dave Barach9b8ffd92016-07-08 08:13:45 -04003390/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003391VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
Damjan Marionceab7882018-01-19 20:56:12 +01003392 .path = "show unix errors",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003393 .short_help = "Show Unix system call error history",
3394 .function = unix_show_errors,
3395};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003396/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003397
Damjan Marionceab7882018-01-19 20:56:12 +01003398/** CLI command to show various unix error statistics. */
3399static clib_error_t *
3400unix_show_files (vlib_main_t * vm,
3401 unformat_input_t * input, vlib_cli_command_t * cmd)
3402{
3403 clib_error_t *error = 0;
3404 clib_file_main_t *fm = &file_main;
3405 clib_file_t *f;
3406 char path[PATH_MAX];
3407 u8 *s = 0;
3408
3409 vlib_cli_output (vm, "%3s %6s %12s %12s %12s %-32s %s", "FD", "Thread",
3410 "Read", "Write", "Error", "File Name", "Description");
3411
3412 /* *INDENT-OFF* */
3413 pool_foreach (f, fm->file_pool,(
3414 {
3415 int rv;
3416 s = format (s, "/proc/self/fd/%d%c", f->file_descriptor, 0);
3417 rv = readlink((char *) s, path, PATH_MAX - 1);
3418
3419 path[rv < 0 ? 0 : rv] = 0;
3420
3421 vlib_cli_output (vm, "%3d %6d %12d %12d %12d %-32s %v",
3422 f->file_descriptor, f->polling_thread_index,
3423 f->read_events, f->write_events, f->error_events,
3424 path, f->description);
3425 vec_reset_length (s);
3426 }));
3427 /* *INDENT-ON* */
3428 vec_free (s);
3429
3430 return error;
3431}
3432
3433/* *INDENT-OFF* */
3434VLIB_CLI_COMMAND (cli_unix_show_files, static) = {
3435 .path = "show unix files",
3436 .short_help = "Show Unix files in use",
3437 .function = unix_show_files,
3438};
3439/* *INDENT-ON* */
3440
Chris Lukeb5850972016-05-03 16:34:59 -04003441/** CLI command to show session command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003442static clib_error_t *
Chris Luke6b90fe32016-04-25 13:49:15 -04003443unix_cli_show_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003444 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke6b90fe32016-04-25 13:49:15 -04003445{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003446 unix_cli_main_t *cm = &unix_cli_main;
3447 unix_cli_file_t *cf;
Chris Luke6b90fe32016-04-25 13:49:15 -04003448 int i, j;
3449
3450 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3451
Chris Luke03add7f2017-09-20 23:31:24 -04003452 if (!cf->is_interactive)
3453 return clib_error_return (0, "invalid for non-interactive sessions");
3454
Chris Luke7afda3a2016-04-25 13:49:22 -04003455 if (cf->has_history && cf->history_limit)
Chris Luke6b90fe32016-04-25 13:49:15 -04003456 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003457 i = 1 + cf->command_number - vec_len (cf->command_history);
Chris Luke7afda3a2016-04-25 13:49:22 -04003458 for (j = 0; j < vec_len (cf->command_history); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04003459 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
Chris Luke7afda3a2016-04-25 13:49:22 -04003460 }
3461 else
3462 {
3463 vlib_cli_output (vm, "History not enabled.\n");
Chris Luke6b90fe32016-04-25 13:49:15 -04003464 }
3465
3466 return 0;
3467}
3468
Chris Luke54ccf222016-07-25 16:38:11 -04003469/*?
3470 * Displays the command history for the current session, if any.
3471?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003472/* *INDENT-OFF* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003473VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
3474 .path = "history",
3475 .short_help = "Show current session command history",
3476 .function = unix_cli_show_history,
3477};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003478/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003479
Chris Lukeb5850972016-05-03 16:34:59 -04003480/** CLI command to show terminal status. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003481static clib_error_t *
3482unix_cli_show_terminal (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003483 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003484{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003485 unix_main_t *um = &unix_main;
3486 unix_cli_main_t *cm = &unix_cli_main;
3487 unix_cli_file_t *cf;
3488 vlib_node_t *n;
Chris Luke7afda3a2016-04-25 13:49:22 -04003489
3490 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3491 n = vlib_get_node (vm, cf->process_node_index);
3492
3493 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003494 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
3495 "line-by-line" : "char-by-char");
Chris Luke7afda3a2016-04-25 13:49:22 -04003496 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
3497 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003498 vlib_cli_output (vm, "ANSI capable: %s\n",
3499 cf->ansi_capable ? "yes" : "no");
Chris Luke03add7f2017-09-20 23:31:24 -04003500 vlib_cli_output (vm, "Interactive: %s\n",
3501 cf->is_interactive ? "yes" : "no");
Chris Luke7afda3a2016-04-25 13:49:22 -04003502 vlib_cli_output (vm, "History enabled: %s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003503 cf->has_history ? "yes" : "no", !cf->has_history
3504 || cf->history_limit ? "" :
3505 " (disabled by history limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04003506 if (cf->has_history)
3507 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
3508 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003509 cf->no_pager ? "no" : "yes",
3510 cf->no_pager
3511 || cf->height ? "" : " (disabled by terminal height)",
3512 cf->no_pager
3513 || um->cli_pager_buffer_limit ? "" :
3514 " (disabled by buffer limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04003515 if (!cf->no_pager)
3516 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003517 vlib_cli_output (vm, "CRLF mode: %s\n",
3518 cf->crlf_mode ? "CR+LF" : "LF");
Chris Luke7afda3a2016-04-25 13:49:22 -04003519
3520 return 0;
3521}
3522
Chris Luke54ccf222016-07-25 16:38:11 -04003523/*?
3524 * Displays various information about the state of the current terminal
3525 * session.
3526 *
3527 * @cliexpar
3528 * @cliexstart{show terminal}
3529 * Terminal name: unix-cli-stdin
3530 * Terminal mode: char-by-char
3531 * Terminal width: 123
3532 * Terminal height: 48
3533 * ANSI capable: yes
Chris Luke03add7f2017-09-20 23:31:24 -04003534 * Interactive: yes
Chris Luke54ccf222016-07-25 16:38:11 -04003535 * History enabled: yes
3536 * History limit: 50
3537 * Pager enabled: yes
3538 * Pager limit: 100000
3539 * CRLF mode: LF
3540 * @cliexend
3541?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003542/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003543VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
3544 .path = "show terminal",
3545 .short_help = "Show current session terminal settings",
3546 .function = unix_cli_show_terminal,
3547};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003548/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003549
Chris Luke03add7f2017-09-20 23:31:24 -04003550/** CLI command to display a list of CLI sessions. */
3551static clib_error_t *
3552unix_cli_show_cli_sessions (vlib_main_t * vm,
3553 unformat_input_t * input,
3554 vlib_cli_command_t * cmd)
3555{
Chris Luke03add7f2017-09-20 23:31:24 -04003556 unix_cli_main_t *cm = &unix_cli_main;
3557 clib_file_main_t *fm = &file_main;
3558 unix_cli_file_t *cf;
3559 clib_file_t *uf;
3560 vlib_node_t *n;
3561
3562 vlib_cli_output (vm, "%-5s %-5s %-20s %s", "PNI", "FD", "Name", "Flags");
3563
3564#define fl(x, y) ( (x) ? toupper((y)) : tolower((y)) )
3565 /* *INDENT-OFF* */
3566 pool_foreach (cf, cm->cli_file_pool, ({
3567 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
3568 n = vlib_get_node (vm, cf->process_node_index);
3569 vlib_cli_output (vm,
3570 "%-5d %-5d %-20v %c%c%c%c%c\n",
3571 cf->process_node_index,
3572 uf->file_descriptor,
3573 n->name,
3574 fl (cf->is_interactive, 'i'),
3575 fl (cf->is_socket, 's'),
3576 fl (cf->line_mode, 'l'),
3577 fl (cf->has_epipe, 'p'),
3578 fl (cf->ansi_capable, 'a'));
3579 }));
3580 /* *INDENT-ON* */
3581#undef fl
3582
3583 return 0;
3584}
3585
3586/*?
3587 * Displays a summary of all the current CLI sessions.
3588 *
3589 * Typically used to diagnose connection issues with the CLI
3590 * socket.
3591 *
3592 * @cliexpar
3593 * @cliexstart{show cli-sessions}
3594 * PNI FD Name Flags
3595 * 343 0 unix-cli-stdin IslpA
3596 * 344 7 unix-cli-local:20 ISlpA
3597 * 346 8 unix-cli-local:21 iSLpa
3598 * @cliexend
3599
3600 * In this example we have the debug console of the running process
3601 * on stdin/out, we have an interactive socket session and we also
3602 * have a non-interactive socket session.
3603 *
3604 * Fields:
3605 *
3606 * - @em PNI: Process node index.
3607 * - @em FD: Unix file descriptor.
3608 * - @em Name: Name of the session.
3609 * - @em Flags: Various flags that describe the state of the session.
3610 *
3611 * @em Flags have the following meanings; lower-case typically negates
3612 * upper-case:
3613 *
3614 * - @em I Interactive session.
3615 * - @em S Connected by socket.
3616 * - @em s Not a socket, likely stdin.
3617 * - @em L Line-by-line mode.
3618 * - @em l Char-by-char mode.
3619 * - @em P EPIPE detected on connection; it will close soon.
3620 * - @em A ANSI-capable terminal.
3621?*/
3622/* *INDENT-OFF* */
3623VLIB_CLI_COMMAND (cli_unix_cli_show_cli_sessions, static) = {
3624 .path = "show cli-sessions",
3625 .short_help = "Show current CLI sessions",
3626 .function = unix_cli_show_cli_sessions,
3627};
3628/* *INDENT-ON* */
3629
Chris Lukeb5850972016-05-03 16:34:59 -04003630/** CLI command to set terminal pager settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003631static clib_error_t *
3632unix_cli_set_terminal_pager (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003633 unformat_input_t * input,
3634 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003635{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003636 unix_main_t *um = &unix_main;
3637 unix_cli_main_t *cm = &unix_cli_main;
3638 unix_cli_file_t *cf;
3639 unformat_input_t _line_input, *line_input = &_line_input;
Billy McFalla9a20e72017-02-15 11:39:12 -05003640 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003641
Chris Luke03add7f2017-09-20 23:31:24 -04003642 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3643
3644 if (!cf->is_interactive)
3645 return clib_error_return (0, "invalid for non-interactive sessions");
3646
Dave Barach9b8ffd92016-07-08 08:13:45 -04003647 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04003648 return 0;
3649
Chris Luke7afda3a2016-04-25 13:49:22 -04003650 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3651 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003652 if (unformat (line_input, "on"))
3653 cf->no_pager = 0;
3654 else if (unformat (line_input, "off"))
3655 cf->no_pager = 1;
3656 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
3657 vlib_cli_output (vm,
3658 "Pager limit set to %u lines; note, this is global.\n",
3659 um->cli_pager_buffer_limit);
3660 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003661 {
3662 error = clib_error_return (0, "unknown parameter: `%U`",
3663 format_unformat_error, line_input);
3664 goto done;
3665 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04003666 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003667
Billy McFalla9a20e72017-02-15 11:39:12 -05003668done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003669 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003670
Billy McFalla9a20e72017-02-15 11:39:12 -05003671 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003672}
3673
Chris Luke54ccf222016-07-25 16:38:11 -04003674/*?
3675 * Enables or disables the terminal pager for this session. Generally
3676 * this defaults to enabled.
3677 *
3678 * Additionally allows the pager buffer size to be set; though note that
3679 * this value is set globally and not per session.
3680?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003681/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003682VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
3683 .path = "set terminal pager",
3684 .short_help = "set terminal pager [on|off] [limit <lines>]",
3685 .function = unix_cli_set_terminal_pager,
3686};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003687/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003688
Chris Lukeb5850972016-05-03 16:34:59 -04003689/** CLI command to set terminal history settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003690static clib_error_t *
3691unix_cli_set_terminal_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003692 unformat_input_t * input,
3693 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003694{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003695 unix_cli_main_t *cm = &unix_cli_main;
3696 unix_cli_file_t *cf;
3697 unformat_input_t _line_input, *line_input = &_line_input;
Chris Luke7afda3a2016-04-25 13:49:22 -04003698 u32 limit;
Billy McFalla9a20e72017-02-15 11:39:12 -05003699 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003700
Chris Luke03add7f2017-09-20 23:31:24 -04003701 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3702
3703 if (!cf->is_interactive)
3704 return clib_error_return (0, "invalid for non-interactive sessions");
3705
Dave Barach9b8ffd92016-07-08 08:13:45 -04003706 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04003707 return 0;
3708
Chris Luke7afda3a2016-04-25 13:49:22 -04003709 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3710 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003711 if (unformat (line_input, "on"))
3712 cf->has_history = 1;
3713 else if (unformat (line_input, "off"))
3714 cf->has_history = 0;
3715 else if (unformat (line_input, "limit %u", &cf->history_limit))
3716 ;
3717 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003718 {
3719 error = clib_error_return (0, "unknown parameter: `%U`",
3720 format_unformat_error, line_input);
3721 goto done;
3722 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003723
Chris Luke5022c6c2019-05-17 10:17:16 -04003724 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003725
Chris Luke5022c6c2019-05-17 10:17:16 -04003726 /* If we reduced history size, or turned it off, purge the history */
3727 limit = cf->has_history ? cf->history_limit : 0;
3728 if (limit < vec_len (cf->command_history))
3729 {
3730 u32 i;
3731
3732 /* How many items to remove from the start of history */
3733 limit = vec_len (cf->command_history) - limit;
3734
3735 for (i = 0; i < limit; i++)
3736 vec_free (cf->command_history[i]);
3737
3738 vec_delete (cf->command_history, limit, 0);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003739 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003740
Billy McFalla9a20e72017-02-15 11:39:12 -05003741done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003742 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003743
Billy McFalla9a20e72017-02-15 11:39:12 -05003744 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003745}
3746
Chris Luke54ccf222016-07-25 16:38:11 -04003747/*?
3748 * Enables or disables the command history function of the current
3749 * terminal. Generally this defaults to enabled.
3750 *
3751 * This command also allows the maximum size of the history buffer for
3752 * this session to be altered.
3753?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003754/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003755VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
3756 .path = "set terminal history",
3757 .short_help = "set terminal history [on|off] [limit <lines>]",
3758 .function = unix_cli_set_terminal_history,
3759};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003760/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003761
Chris Lukeb5850972016-05-03 16:34:59 -04003762/** CLI command to set terminal ANSI settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003763static clib_error_t *
3764unix_cli_set_terminal_ansi (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003765 unformat_input_t * input,
3766 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003767{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003768 unix_cli_main_t *cm = &unix_cli_main;
3769 unix_cli_file_t *cf;
Chris Luke7afda3a2016-04-25 13:49:22 -04003770
3771 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3772
Chris Luke03add7f2017-09-20 23:31:24 -04003773 if (!cf->is_interactive)
3774 return clib_error_return (0, "invalid for non-interactive sessions");
3775
Chris Luke7afda3a2016-04-25 13:49:22 -04003776 if (unformat (input, "on"))
3777 cf->ansi_capable = 1;
3778 else if (unformat (input, "off"))
3779 cf->ansi_capable = 0;
3780 else
3781 return clib_error_return (0, "unknown parameter: `%U`",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003782 format_unformat_error, input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003783
3784 return 0;
3785}
3786
Chris Luke54ccf222016-07-25 16:38:11 -04003787/*?
3788 * Enables or disables the use of ANSI control sequences by this terminal.
3789 * The default will vary based on terminal detection at the start of the
3790 * session.
3791 *
3792 * ANSI control sequences are used in a small number of places to provide,
3793 * for example, color text output and to control the cursor in the pager.
3794?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003795/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003796VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
3797 .path = "set terminal ansi",
3798 .short_help = "set terminal ansi [on|off]",
3799 .function = unix_cli_set_terminal_ansi,
3800};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003801/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003802
3803static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07003804unix_cli_init (vlib_main_t * vm)
3805{
Chris Luke6a3a4f72019-07-09 23:33:30 -04003806 unix_cli_main_t *cm = &unix_cli_main;
3807
3808 /* Breadcrumb to indicate the new session process
3809 * has not been started */
3810 cm->new_session_process_node_index = ~0;
3811
Ed Warnickecb9cada2015-12-08 15:45:58 -07003812 return 0;
3813}
3814
3815VLIB_INIT_FUNCTION (unix_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003816
3817/*
3818 * fd.io coding-style-patch-verification: ON
3819 *
3820 * Local Variables:
3821 * eval: (c-set-style "gnu")
3822 * End:
3823 */