blob: 5838bbbc7a21d32d105b8ec04256c7bfae736bf1 [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>
Chris Luke0aca5eb2016-04-25 13:48:54 -040050#include <vppinfra/timer.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
Chris Luke0aca5eb2016-04-25 13:48:54 -040052#include <ctype.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070053#include <fcntl.h>
54#include <sys/stat.h>
55#include <termios.h>
Chris Luke7afda3a2016-04-25 13:49:22 -040056#include <signal.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070057#include <unistd.h>
58#include <arpa/telnet.h>
Chris Luke7afda3a2016-04-25 13:49:22 -040059#include <sys/ioctl.h>
Damjan Marionf6616382017-06-21 12:01:37 +020060#include <sys/types.h>
61#include <unistd.h>
Damjan Marionceab7882018-01-19 20:56:12 +010062#include <limits.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Chris Lukeb5850972016-05-03 16:34:59 -040064/** ANSI escape code. */
Chris Luke0aca5eb2016-04-25 13:48:54 -040065#define ESC "\x1b"
66
Chris Lukeb5850972016-05-03 16:34:59 -040067/** ANSI Control Sequence Introducer. */
Chris Luke0aca5eb2016-04-25 13:48:54 -040068#define CSI ESC "["
69
Chris Lukeb5850972016-05-03 16:34:59 -040070/** ANSI clear screen. */
Chris Luke7afda3a2016-04-25 13:49:22 -040071#define ANSI_CLEAR CSI "2J" CSI "1;1H"
Chris Lukeb5850972016-05-03 16:34:59 -040072/** ANSI reset color settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -040073#define ANSI_RESET CSI "0m"
Chris Lukeb5850972016-05-03 16:34:59 -040074/** ANSI Start bold text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040075#define ANSI_BOLD CSI "1m"
Chris Lukeb5850972016-05-03 16:34:59 -040076/** ANSI Stop bold text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040077#define ANSI_DIM CSI "2m"
Chris Lukeb5850972016-05-03 16:34:59 -040078/** ANSI Start dark red text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040079#define ANSI_DRED ANSI_DIM CSI "31m"
Chris Lukeb5850972016-05-03 16:34:59 -040080/** ANSI Start bright red text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040081#define ANSI_BRED ANSI_BOLD CSI "31m"
Chris Lukeb5850972016-05-03 16:34:59 -040082/** ANSI clear line cursor is on. */
Chris Luke7afda3a2016-04-25 13:49:22 -040083#define ANSI_CLEARLINE CSI "2K"
Chris Lukeb5850972016-05-03 16:34:59 -040084/** ANSI scroll screen down one line. */
Chris Luke7afda3a2016-04-25 13:49:22 -040085#define ANSI_SCROLLDN CSI "1T"
Chris Lukeb5850972016-05-03 16:34:59 -040086/** ANSI save cursor position. */
Chris Luke7afda3a2016-04-25 13:49:22 -040087#define ANSI_SAVECURSOR CSI "s"
Chris Lukeb5850972016-05-03 16:34:59 -040088/** ANSI restore cursor position if previously saved. */
Chris Luke7afda3a2016-04-25 13:49:22 -040089#define ANSI_RESTCURSOR CSI "u"
Chris Luke0aca5eb2016-04-25 13:48:54 -040090
91/** Maximum depth into a byte stream from which to compile a Telnet
Chris Luke2d8bf302017-11-12 22:26:37 -050092 * protocol message. This is a safety measure. */
Chris Luke1aed76f2016-04-29 08:14:38 -040093#define UNIX_CLI_MAX_DEPTH_TELNET 24
Chris Luke0aca5eb2016-04-25 13:48:54 -040094
Chris Lukeb2bcad62017-09-18 08:51:22 -040095/** Maximum terminal width we will accept */
96#define UNIX_CLI_MAX_TERMINAL_WIDTH 512
Chris Lukeb2bcad62017-09-18 08:51:22 -040097/** Maximum terminal height we will accept */
98#define UNIX_CLI_MAX_TERMINAL_HEIGHT 512
Chris Luke2d8bf302017-11-12 22:26:37 -050099/** Default terminal height */
100#define UNIX_CLI_DEFAULT_TERMINAL_HEIGHT 24
101/** Default terminal width */
102#define UNIX_CLI_DEFAULT_TERMINAL_WIDTH 80
Chris Luke0aca5eb2016-04-25 13:48:54 -0400103
Chris Lukeb5850972016-05-03 16:34:59 -0400104/** A CLI banner line. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400105typedef struct
106{
107 u8 *line; /**< The line to print. */
108 u32 length; /**< The length of the line without terminating NUL. */
Chris Luke572d8122016-04-25 13:49:07 -0400109} unix_cli_banner_t;
110
111#define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
112/** Plain welcome banner. */
113static unix_cli_banner_t unix_cli_banner[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400114 _(" _______ _ _ _____ ___ \n"),
115 _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
116 _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
117 _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
118 _("\n")
Chris Luke572d8122016-04-25 13:49:07 -0400119};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400120
Chris Luke572d8122016-04-25 13:49:07 -0400121/** ANSI color welcome banner. */
122static unix_cli_banner_t unix_cli_banner_color[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400123 _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
124 _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
125 _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
126 _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"),
127 _("\n")
Chris Luke572d8122016-04-25 13:49:07 -0400128};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400129
Chris Luke572d8122016-04-25 13:49:07 -0400130#undef _
131
Chris Luke862623d2016-05-14 10:13:34 -0400132/** Pager line index */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400133typedef struct
134{
Chris Luke862623d2016-05-14 10:13:34 -0400135 /** Index into pager_vector */
136 u32 line;
137
138 /** Offset of the string in the line */
139 u32 offset;
140
141 /** Length of the string in the line */
142 u32 length;
143} unix_cli_pager_index_t;
144
Chris Luke572d8122016-04-25 13:49:07 -0400145
Chris Luke0aca5eb2016-04-25 13:48:54 -0400146/** Unix CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400147typedef struct
148{
Chris Lukeb5850972016-05-03 16:34:59 -0400149 /** The file index held by unix.c */
Damjan Marion56dd5432017-09-08 19:52:02 +0200150 u32 clib_file_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
Chris Lukeb5850972016-05-03 16:34:59 -0400152 /** Vector of output pending write to file descriptor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400153 u8 *output_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
Chris Lukeb5850972016-05-03 16:34:59 -0400155 /** Vector of input saved by Unix input node to be processed by
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156 CLI process. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400157 u8 *input_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158
Chris Luke54ccf222016-07-25 16:38:11 -0400159 /** This session has command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 u8 has_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400161 /** Array of vectors of commands in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400162 u8 **command_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400163 /** The command currently pointed at by the history cursor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400164 u8 *current_command;
Chris Luke54ccf222016-07-25 16:38:11 -0400165 /** How far from the end of the history array the user has browsed. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166 i32 excursion;
Chris Luke6b90fe32016-04-25 13:49:15 -0400167
Chris Lukeb5850972016-05-03 16:34:59 -0400168 /** Maximum number of history entries this session will store. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 u32 history_limit;
Chris Luke6b90fe32016-04-25 13:49:15 -0400170
Chris Lukeb5850972016-05-03 16:34:59 -0400171 /** Current command line counter */
Chris Luke6b90fe32016-04-25 13:49:15 -0400172 u32 command_number;
173
Chris Luke54ccf222016-07-25 16:38:11 -0400174 /** The string being searched for in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400175 u8 *search_key;
Chris Luke54ccf222016-07-25 16:38:11 -0400176 /** If non-zero then the CLI is searching in the history array.
177 * - @c -1 means search backwards.
178 * - @c 1 means search forwards.
179 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700180 int search_mode;
181
Chris Lukeb5850972016-05-03 16:34:59 -0400182 /** Position of the insert cursor on the current input line */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400183 u32 cursor;
184
Chris Lukeb5850972016-05-03 16:34:59 -0400185 /** Line mode or char mode */
Chris Luke7afda3a2016-04-25 13:49:22 -0400186 u8 line_mode;
187
Chris Lukeb5850972016-05-03 16:34:59 -0400188 /** Set if the CRLF mode wants CR + LF */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400189 u8 crlf_mode;
190
Chris Lukeb5850972016-05-03 16:34:59 -0400191 /** Can we do ANSI output? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400192 u8 ansi_capable;
193
Chris Lukeb5850972016-05-03 16:34:59 -0400194 /** Has the session started? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400195 u8 started;
196
Chris Lukeb5850972016-05-03 16:34:59 -0400197 /** Disable the pager? */
Chris Luke7afda3a2016-04-25 13:49:22 -0400198 u8 no_pager;
199
Chris Luke03add7f2017-09-20 23:31:24 -0400200 /** Whether the session is interactive or not.
201 * Controls things like initial banner, the CLI prompt etc. */
202 u8 is_interactive;
203
204 /** Whether the session is attached to a socket. */
205 u8 is_socket;
206
207 /** If EPIPE has been detected, prevent further write-related
208 * activity on the descriptor.
209 */
210 u8 has_epipe;
211
Chris Lukeb5850972016-05-03 16:34:59 -0400212 /** Pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400213 u8 **pager_vector;
Chris Luke7afda3a2016-04-25 13:49:22 -0400214
Chris Luke862623d2016-05-14 10:13:34 -0400215 /** Index of line fragments in the pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400216 unix_cli_pager_index_t *pager_index;
Chris Luke7afda3a2016-04-25 13:49:22 -0400217
Chris Lukeb5850972016-05-03 16:34:59 -0400218 /** Line number of top of page */
Chris Luke7afda3a2016-04-25 13:49:22 -0400219 u32 pager_start;
220
Chris Lukeb5850972016-05-03 16:34:59 -0400221 /** Terminal width */
222 u32 width;
Chris Luke7afda3a2016-04-25 13:49:22 -0400223
Chris Lukeb5850972016-05-03 16:34:59 -0400224 /** Terminal height */
225 u32 height;
226
227 /** Process node identifier */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228 u32 process_node_index;
229} unix_cli_file_t;
230
Chris Lukeb5850972016-05-03 16:34:59 -0400231/** Resets the pager buffer and other data.
232 * @param f The CLI session whose pager needs to be reset.
233 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400235unix_cli_pager_reset (unix_cli_file_t * f)
Chris Luke7afda3a2016-04-25 13:49:22 -0400236{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400237 u8 **p;
Chris Luke7afda3a2016-04-25 13:49:22 -0400238
Chris Luke862623d2016-05-14 10:13:34 -0400239 f->pager_start = 0;
240
241 vec_free (f->pager_index);
242 f->pager_index = 0;
243
Chris Luke7afda3a2016-04-25 13:49:22 -0400244 vec_foreach (p, f->pager_vector)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400245 {
246 vec_free (*p);
247 }
Chris Luke862623d2016-05-14 10:13:34 -0400248 vec_free (f->pager_vector);
Chris Luke7afda3a2016-04-25 13:49:22 -0400249 f->pager_vector = 0;
250}
251
Chris Lukeb5850972016-05-03 16:34:59 -0400252/** Release storage used by a CLI session.
253 * @param f The CLI session whose storage needs to be released.
254 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400255always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256unix_cli_file_free (unix_cli_file_t * f)
257{
258 vec_free (f->output_vector);
259 vec_free (f->input_vector);
Chris Luke862623d2016-05-14 10:13:34 -0400260 unix_cli_pager_reset (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261}
262
Chris Lukeb5850972016-05-03 16:34:59 -0400263/** CLI actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400264typedef enum
265{
Chris Luke54ccf222016-07-25 16:38:11 -0400266 UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */
267 UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */
268 UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */
269 UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */
270 UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */
271 UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */
272 UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */
273 UNIX_CLI_PARSE_ACTION_LEFT, /**< Left arrow */
274 UNIX_CLI_PARSE_ACTION_RIGHT, /**< Right arrow */
275 UNIX_CLI_PARSE_ACTION_HOME, /**< Home key (jump to start of line) */
276 UNIX_CLI_PARSE_ACTION_END, /**< End key (jump to end of line) */
277 UNIX_CLI_PARSE_ACTION_WORDLEFT, /**< Jump cursor to start of left word */
278 UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */
279 UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */
280 UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */
281 UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */
282 UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */
283 UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */
284 UNIX_CLI_PARSE_ACTION_YANK, /**< Undo last erase action */
285 UNIX_CLI_PARSE_ACTION_TELNETIAC, /**< Telnet control code */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400286
Chris Luke54ccf222016-07-25 16:38:11 -0400287 UNIX_CLI_PARSE_ACTION_PAGER_CRLF, /**< Enter pressed (CR, CRLF, LF, etc) */
288 UNIX_CLI_PARSE_ACTION_PAGER_QUIT, /**< Exit the pager session */
289 UNIX_CLI_PARSE_ACTION_PAGER_NEXT, /**< Scroll to next page */
290 UNIX_CLI_PARSE_ACTION_PAGER_DN, /**< Scroll to next line */
291 UNIX_CLI_PARSE_ACTION_PAGER_UP, /**< Scroll to previous line */
292 UNIX_CLI_PARSE_ACTION_PAGER_TOP, /**< Scroll to first line */
293 UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM, /**< Scroll to last line */
294 UNIX_CLI_PARSE_ACTION_PAGER_PGDN, /**< Scroll to next page */
295 UNIX_CLI_PARSE_ACTION_PAGER_PGUP, /**< Scroll to previous page */
296 UNIX_CLI_PARSE_ACTION_PAGER_REDRAW, /**< Clear and redraw the page on the terminal */
297 UNIX_CLI_PARSE_ACTION_PAGER_SEARCH, /**< Search the pager buffer */
Chris Luke7afda3a2016-04-25 13:49:22 -0400298
Chris Luke54ccf222016-07-25 16:38:11 -0400299 UNIX_CLI_PARSE_ACTION_PARTIALMATCH, /**< Action parser found a partial match */
300 UNIX_CLI_PARSE_ACTION_NOMATCH /**< Action parser did not find any match */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400301} unix_cli_parse_action_t;
302
Chris Luke8e5b0412016-07-26 13:06:10 -0400303/** @brief Mapping of input buffer strings to action values.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400304 * @note This won't work as a hash since we need to be able to do
305 * partial matches on the string.
306 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400307typedef struct
308{
309 u8 *input; /**< Input string to match. */
310 u32 len; /**< Length of input without final NUL. */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400311 unix_cli_parse_action_t action; /**< Action to take when matched. */
312} unix_cli_parse_actions_t;
313
Chris Lukeb5850972016-05-03 16:34:59 -0400314/** @brief Given a capital ASCII letter character return a @c NUL terminated
Chris Luke0aca5eb2016-04-25 13:48:54 -0400315 * string with the control code for that letter.
Chris Lukeb5850972016-05-03 16:34:59 -0400316 *
317 * @param c An ASCII character.
318 * @return A @c NUL terminated string of type @c u8[].
319 *
320 * @par Example
321 * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[].
Chris Luke0aca5eb2016-04-25 13:48:54 -0400322 */
323#define CTL(c) (u8[]){ (c) - '@', 0 }
324
325#define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
Chris Lukeb5850972016-05-03 16:34:59 -0400326/**
327 * Patterns to match on a CLI input stream.
328 * @showinitializer
329 */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400330static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400331 /* Line handling */
332 _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */
333 _("\n", UNIX_CLI_PARSE_ACTION_CRLF),
334 _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */
335 _("\r", UNIX_CLI_PARSE_ACTION_CRLF),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400336
Dave Barach9b8ffd92016-07-08 08:13:45 -0400337 /* Unix shell control codes */
338 _(CTL ('B'), UNIX_CLI_PARSE_ACTION_LEFT),
339 _(CTL ('F'), UNIX_CLI_PARSE_ACTION_RIGHT),
340 _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP),
341 _(CTL ('N'), UNIX_CLI_PARSE_ACTION_DOWN),
342 _(CTL ('A'), UNIX_CLI_PARSE_ACTION_HOME),
343 _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END),
344 _(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT),
345 _(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT),
346 _(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT),
347 _(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK),
348 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR),
349 _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */
350 _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */
351 _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */
352 _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */
353 _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400354
Dave Barach9b8ffd92016-07-08 08:13:45 -0400355 /* VT100 Normal mode - Broadest support */
356 _(CSI "A", UNIX_CLI_PARSE_ACTION_UP),
357 _(CSI "B", UNIX_CLI_PARSE_ACTION_DOWN),
358 _(CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT),
359 _(CSI "D", UNIX_CLI_PARSE_ACTION_LEFT),
360 _(CSI "H", UNIX_CLI_PARSE_ACTION_HOME),
361 _(CSI "F", UNIX_CLI_PARSE_ACTION_END),
362 _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */
363 _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */
364 _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400365
366 /* VT100 Application mode - Some Gnome Terminal functions use these */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400367 _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP),
368 _(ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN),
369 _(ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT),
370 _(ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT),
371 _(ESC "OH", UNIX_CLI_PARSE_ACTION_HOME),
372 _(ESC "OF", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400373
Dave Barach9b8ffd92016-07-08 08:13:45 -0400374 /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
375 _(CSI "1~", UNIX_CLI_PARSE_ACTION_HOME),
376 _(CSI "4~", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400377
Dave Barach9b8ffd92016-07-08 08:13:45 -0400378 /* Emacs-ish history search */
379 _(CTL ('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH),
380 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400381
Dave Barach9b8ffd92016-07-08 08:13:45 -0400382 /* Other protocol things */
383 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
384 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
385 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400386};
Chris Lukeb5850972016-05-03 16:34:59 -0400387
388/**
389 * Patterns to match when a CLI session is in the pager.
390 * @showinitializer
391 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400392static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400393 /* Line handling */
394 _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */
395 _("\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
396 _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */
397 _("\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
Chris Luke7afda3a2016-04-25 13:49:22 -0400398
Dave Barach9b8ffd92016-07-08 08:13:45 -0400399 /* Pager commands */
400 _(" ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT),
401 _("q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT),
402 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
403 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
404 _("/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH),
Chris Luke7afda3a2016-04-25 13:49:22 -0400405
Dave Barach9b8ffd92016-07-08 08:13:45 -0400406 /* VT100 */
407 _(CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP),
408 _(CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN),
409 _(CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
410 _(CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400411
Dave Barach9b8ffd92016-07-08 08:13:45 -0400412 /* VT100 Application mode */
413 _(ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP),
414 _(ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN),
415 _(ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
416 _(ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400417
Dave Barach9b8ffd92016-07-08 08:13:45 -0400418 /* ANSI X3.41-1974 */
419 _(CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
420 _(CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
421 _(CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP),
422 _(CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN),
Chris Luke7afda3a2016-04-25 13:49:22 -0400423
Dave Barach9b8ffd92016-07-08 08:13:45 -0400424 /* Other protocol things */
425 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
426 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
427 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke7afda3a2016-04-25 13:49:22 -0400428};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400429
Chris Luke0aca5eb2016-04-25 13:48:54 -0400430#undef _
431
Chris Lukeb5850972016-05-03 16:34:59 -0400432/** CLI session events. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400433typedef enum
434{
Chris Lukeb5850972016-05-03 16:34:59 -0400435 UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400436 UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400437} unix_cli_process_event_type_t;
438
Chris Lukeb5850972016-05-03 16:34:59 -0400439/** CLI global state. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400440typedef struct
441{
Chris Lukeb5850972016-05-03 16:34:59 -0400442 /** Prompt string for CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400443 u8 *cli_prompt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444
Chris Lukeb5850972016-05-03 16:34:59 -0400445 /** Vec pool of CLI sessions. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400446 unix_cli_file_t *cli_file_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
Chris Lukeb5850972016-05-03 16:34:59 -0400448 /** Vec pool of unused session indices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400449 u32 *unused_cli_process_node_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450
Chris Lukeb5850972016-05-03 16:34:59 -0400451 /** The session index of the stdin cli */
Chris Luke7afda3a2016-04-25 13:49:22 -0400452 u32 stdin_cli_file_index;
453
Chris Lukeb5850972016-05-03 16:34:59 -0400454 /** File pool index of current input. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455 u32 current_input_file_index;
456} unix_cli_main_t;
457
Chris Lukeb5850972016-05-03 16:34:59 -0400458/** CLI global state */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459static unix_cli_main_t unix_cli_main;
460
Chris Luke0aca5eb2016-04-25 13:48:54 -0400461/**
Chris Luke8e5b0412016-07-26 13:06:10 -0400462 * @brief Search for a byte sequence in the action list.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400463 *
Chris Lukeb5850972016-05-03 16:34:59 -0400464 * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with
465 * the bytes in @a input of maximum length @a ilen bytes.
466 * When a match is made @a *matched indicates how many bytes were matched.
467 * Returns a value from the enum @ref unix_cli_parse_action_t to indicate
468 * whether no match was found, a partial match was found or a complete
469 * match was found and what action, if any, should be taken.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400470 *
Chris Lukeb5850972016-05-03 16:34:59 -0400471 * @param[in] a Actions list to search within.
472 * @param[in] input String fragment to search for.
473 * @param[in] ilen Length of the string in 'input'.
474 * @param[out] matched Pointer to an integer that will contain the number
475 * of bytes matched when a complete match is found.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400476 *
Chris Lukeb5850972016-05-03 16:34:59 -0400477 * @return Action from @ref unix_cli_parse_action_t that the string fragment
Chris Luke0aca5eb2016-04-25 13:48:54 -0400478 * matches.
Chris Lukeb5850972016-05-03 16:34:59 -0400479 * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the
480 * whole input string matches the start of at least one action.
481 * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
Chris Luke0aca5eb2016-04-25 13:48:54 -0400482 * match at all.
483 */
484static unix_cli_parse_action_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400485unix_cli_match_action (unix_cli_parse_actions_t * a,
486 u8 * input, u32 ilen, i32 * matched)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400487{
Chris Luke0aca5eb2016-04-25 13:48:54 -0400488 u8 partial = 0;
489
490 while (a->input)
491 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400492 if (ilen >= a->len)
493 {
494 /* see if the start of the input buffer exactly matches the current
495 * action string. */
496 if (memcmp (input, a->input, a->len) == 0)
497 {
498 *matched = a->len;
499 return a->action;
500 }
501 }
502 else
503 {
504 /* if the first ilen characters match, flag this as a partial -
505 * meaning keep collecting bytes in case of a future match */
506 if (memcmp (input, a->input, ilen) == 0)
507 partial = 1;
508 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400509
Dave Barach9b8ffd92016-07-08 08:13:45 -0400510 /* check next action */
511 a++;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400512 }
513
514 return partial ?
Dave Barach9b8ffd92016-07-08 08:13:45 -0400515 UNIX_CLI_PARSE_ACTION_PARTIALMATCH : UNIX_CLI_PARSE_ACTION_NOMATCH;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400516}
517
518
Chris Luke54ccf222016-07-25 16:38:11 -0400519/** Add bytes to the output vector and then flagg the I/O system that bytes
520 * are available to be sent.
521 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200523unix_cli_add_pending_output (clib_file_t * uf,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524 unix_cli_file_t * cf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400525 u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526{
Damjan Marion56dd5432017-09-08 19:52:02 +0200527 clib_file_main_t *fm = &file_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528
529 vec_add (cf->output_vector, buffer, buffer_bytes);
530 if (vec_len (cf->output_vector) > 0)
531 {
532 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
533 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400534 if (!skip_update)
Damjan Marion56dd5432017-09-08 19:52:02 +0200535 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 }
537}
538
Chris Luke54ccf222016-07-25 16:38:11 -0400539/** Delete all bytes from the output vector and flag the I/O system
540 * that no more bytes are available to be sent.
541 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200543unix_cli_del_pending_output (clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400544 unix_cli_file_t * cf, uword n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545{
Damjan Marion56dd5432017-09-08 19:52:02 +0200546 clib_file_main_t *fm = &file_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547
548 vec_delete (cf->output_vector, n_bytes, 0);
549 if (vec_len (cf->output_vector) <= 0)
550 {
551 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
552 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400553 if (!skip_update)
Damjan Marion56dd5432017-09-08 19:52:02 +0200554 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555 }
556}
557
Chris Luke8e5b0412016-07-26 13:06:10 -0400558/** @brief A bit like strchr with a buffer length limit.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400559 * Search a buffer for the first instance of a character up to the limit of
560 * the buffer length. If found then return the position of that character.
561 *
562 * The key departure from strchr is that if the character is not found then
563 * return the buffer length.
564 *
565 * @param chr The byte value to search for.
566 * @param str The buffer in which to search for the value.
567 * @param len The depth into the buffer to search.
568 *
569 * @return The index of the first occurence of \c chr. If \c chr is not
570 * found then \c len instead.
571 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400572always_inline word
573unix_vlib_findchr (u8 chr, u8 * str, word len)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400574{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400575 word i = 0;
576 for (i = 0; i < len; i++, str++)
577 {
578 if (*str == chr)
579 return i;
580 }
581 return len;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400582}
583
Chris Luke8e5b0412016-07-26 13:06:10 -0400584/** @brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400585 * Attempts to write given buffer to the file descriptor of the given
586 * Unix CLI session. If that session already has data in the output buffer
587 * or if the write attempt tells us to try again later then the given buffer
588 * is appended to the pending output buffer instead.
589 *
590 * This is typically called only from \c unix_vlib_cli_output_cooked since
591 * that is where CRLF handling occurs or from places where we explicitly do
592 * not want cooked handling.
593 *
594 * @param cf Unix CLI session of the desired stream to write to.
595 * @param uf The Unix file structure of the desired stream to write to.
596 * @param buffer Pointer to the buffer that needs to be written.
597 * @param buffer_bytes The number of bytes from \c buffer to write.
598 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400599static void
600unix_vlib_cli_output_raw (unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +0200601 clib_file_t * uf, u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400602{
603 int n = 0;
604
Chris Luke03add7f2017-09-20 23:31:24 -0400605 if (cf->has_epipe) /* don't try writing anything */
606 return;
607
Chris Luke0aca5eb2016-04-25 13:48:54 -0400608 if (vec_len (cf->output_vector) == 0)
Chris Luke03add7f2017-09-20 23:31:24 -0400609 {
610 if (cf->is_socket)
611 /* If it's a socket we use MSG_NOSIGNAL to prevent SIGPIPE */
612 n = send (uf->file_descriptor, buffer, buffer_bytes, MSG_NOSIGNAL);
613 else
614 n = write (uf->file_descriptor, buffer, buffer_bytes);
615 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400616
617 if (n < 0 && errno != EAGAIN)
618 {
Chris Luke03add7f2017-09-20 23:31:24 -0400619 if (errno == EPIPE)
620 {
621 /* connection closed on us */
622 unix_main_t *um = &unix_main;
623 cf->has_epipe = 1;
624 vlib_process_signal_event (um->vlib_main, cf->process_node_index,
625 UNIX_CLI_PROCESS_EVENT_QUIT,
626 uf->private_data);
627 }
628 else
629 {
630 clib_unix_warning ("write");
631 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400632 }
633 else if ((word) n < (word) buffer_bytes)
634 {
635 /* We got EAGAIN or we already have stuff in the buffer;
636 * queue up whatever didn't get sent for later. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400637 if (n < 0)
638 n = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400639 unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
640 }
641}
642
Chris Luke8e5b0412016-07-26 13:06:10 -0400643/** @brief Process a buffer for CRLF handling before outputting it to the CLI.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400644 *
645 * @param cf Unix CLI session of the desired stream to write to.
646 * @param uf The Unix file structure of the desired stream to write to.
647 * @param buffer Pointer to the buffer that needs to be written.
648 * @param buffer_bytes The number of bytes from \c buffer to write.
649 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400650static void
651unix_vlib_cli_output_cooked (unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +0200652 clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400653 u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400654{
655 word end = 0, start = 0;
656
657 while (end < buffer_bytes)
658 {
659 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400660 {
661 /* iterate the line on \n's so we can insert a \r before it */
662 end = unix_vlib_findchr ('\n',
663 buffer + start,
664 buffer_bytes - start) + start;
665 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400666 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400667 {
668 /* otherwise just send the whole buffer */
669 end = buffer_bytes;
670 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400671
Dave Barach9b8ffd92016-07-08 08:13:45 -0400672 unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start);
Chris Luke0aca5eb2016-04-25 13:48:54 -0400673
674 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400675 {
676 if (end < buffer_bytes)
677 {
678 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2);
679 end++; /* skip the \n that we already sent */
680 }
681 start = end;
682 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400683 }
684}
685
Chris Luke8e5b0412016-07-26 13:06:10 -0400686/** @brief Output the CLI prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400687static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200688unix_cli_cli_prompt (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400689{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400690 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke7afda3a2016-04-25 13:49:22 -0400691
Chris Luke03add7f2017-09-20 23:31:24 -0400692 if (cf->is_interactive) /* Only interactive sessions get a prompt */
693 unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt,
694 vec_len (cm->cli_prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400695}
696
Chris Luke8e5b0412016-07-26 13:06:10 -0400697/** @brief Output a pager prompt and show number of buffered lines */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400698static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200699unix_cli_pager_prompt (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400700{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400701 u8 *prompt;
Chris Luke270b6de2016-05-19 14:23:25 -0400702 u32 h;
703
704 h = cf->pager_start + (cf->height - 1);
705 if (h > vec_len (cf->pager_index))
706 h = vec_len (cf->pager_index);
Chris Luke7afda3a2016-04-25 13:49:22 -0400707
Dave Barach9b8ffd92016-07-08 08:13:45 -0400708 prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s",
709 cf->ansi_capable ? ANSI_BOLD : "",
710 cf->pager_start + 1,
711 h,
712 vec_len (cf->pager_index),
713 cf->ansi_capable ? ANSI_RESET : "");
Chris Luke7afda3a2016-04-25 13:49:22 -0400714
Dave Barach9b8ffd92016-07-08 08:13:45 -0400715 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400716
Dave Barach9b8ffd92016-07-08 08:13:45 -0400717 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400718}
719
Chris Luke8e5b0412016-07-26 13:06:10 -0400720/** @brief Output a pager "skipping" message */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400721static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200722unix_cli_pager_message (unix_cli_file_t * cf, clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400723 char *message, char *postfix)
Chris Luke7afda3a2016-04-25 13:49:22 -0400724{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400725 u8 *prompt;
Chris Luke7afda3a2016-04-25 13:49:22 -0400726
Dave Barach9b8ffd92016-07-08 08:13:45 -0400727 prompt = format (0, "\r%s-- %s --%s%s",
728 cf->ansi_capable ? ANSI_BOLD : "",
729 message, cf->ansi_capable ? ANSI_RESET : "", postfix);
Chris Luke7afda3a2016-04-25 13:49:22 -0400730
Dave Barach9b8ffd92016-07-08 08:13:45 -0400731 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400732
Dave Barach9b8ffd92016-07-08 08:13:45 -0400733 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400734}
735
Chris Luke8e5b0412016-07-26 13:06:10 -0400736/** @brief Erase the printed pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400737static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200738unix_cli_pager_prompt_erase (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400739{
740 if (cf->ansi_capable)
741 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400742 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
743 unix_vlib_cli_output_cooked (cf, uf,
744 (u8 *) ANSI_CLEARLINE,
745 sizeof (ANSI_CLEARLINE) - 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400746 }
747 else
748 {
749 int i;
750
Dave Barach9b8ffd92016-07-08 08:13:45 -0400751 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
752 for (i = 0; i < cf->width - 1; i++)
753 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
754 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400755 }
756}
757
Chris Luke8e5b0412016-07-26 13:06:10 -0400758/** @brief Uses an ANSI escape sequence to move the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400759static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200760unix_cli_ansi_cursor (unix_cli_file_t * cf, clib_file_t * uf, u16 x, u16 y)
Chris Luke7afda3a2016-04-25 13:49:22 -0400761{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400762 u8 *str;
Chris Luke7afda3a2016-04-25 13:49:22 -0400763
Dave Barach9b8ffd92016-07-08 08:13:45 -0400764 str = format (0, "%s%d;%dH", CSI, y, x);
Chris Luke7afda3a2016-04-25 13:49:22 -0400765
Dave Barach9b8ffd92016-07-08 08:13:45 -0400766 unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str));
Chris Luke7afda3a2016-04-25 13:49:22 -0400767
Dave Barach9b8ffd92016-07-08 08:13:45 -0400768 vec_free (str);
Chris Luke7afda3a2016-04-25 13:49:22 -0400769}
770
Chris Luke862623d2016-05-14 10:13:34 -0400771/** Redraw the currently displayed page of text.
772 * @param cf CLI session to redraw the pager buffer of.
773 * @param uf Unix file of the CLI session.
774 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400775static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200776unix_cli_pager_redraw (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke862623d2016-05-14 10:13:34 -0400777{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400778 unix_cli_pager_index_t *pi = NULL;
779 u8 *line = NULL;
Chris Luke862623d2016-05-14 10:13:34 -0400780 word i;
781
782 /* No active pager? Do nothing. */
783 if (!vec_len (cf->pager_index))
784 return;
785
786 if (cf->ansi_capable)
787 {
788 /* If we have ANSI, send the clear screen sequence */
789 unix_vlib_cli_output_cooked (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400790 (u8 *) ANSI_CLEAR,
791 sizeof (ANSI_CLEAR) - 1);
Chris Luke862623d2016-05-14 10:13:34 -0400792 }
793 else
794 {
795 /* Otherwise make sure we're on a blank line */
796 unix_cli_pager_prompt_erase (cf, uf);
797 }
798
799 /* (Re-)send the current page of content */
800 for (i = 0; i < cf->height - 1 &&
Dave Barach9b8ffd92016-07-08 08:13:45 -0400801 i + cf->pager_start < vec_len (cf->pager_index); i++)
Chris Luke862623d2016-05-14 10:13:34 -0400802 {
803 pi = &cf->pager_index[cf->pager_start + i];
804 line = cf->pager_vector[pi->line] + pi->offset;
805
806 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
807 }
808 /* if the last line didn't end in newline, add a newline */
809 if (pi && line[pi->length - 1] != '\n')
Dave Barach9b8ffd92016-07-08 08:13:45 -0400810 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke862623d2016-05-14 10:13:34 -0400811
812 unix_cli_pager_prompt (cf, uf);
813}
814
815/** @brief Process and add a line to the pager index.
816 * In normal operation this function will take the given character string
817 * found in @c line and with length @c len_or_index and iterates the over the
818 * contents, adding each line of text discovered within it to the
819 * pager index. Lines are identified by newlines ("<code>\\n</code>") and by
820 * strings longer than the width of the terminal.
821 *
822 * If instead @c line is @c NULL then @c len_or_index is taken to mean the
823 * index of an existing line in the pager buffer; this simply means that the
824 * input line does not need to be cloned since we alreayd have it. This is
825 * typical if we are reindexing the pager buffer.
826 *
827 * @param cf The CLI session whose pager we are adding to.
828 * @param line The string of text to be indexed into the pager buffer.
829 * If @c line is @c NULL then the mode of operation
830 * changes slightly; see the description above.
831 * @param len_or_index If @c line is a pointer to a string then this parameter
832 * indicates the length of that string; Otherwise this
833 * value provides the index in the pager buffer of an
834 * existing string to be indexed.
835 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400836static void
837unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index)
Chris Luke862623d2016-05-14 10:13:34 -0400838{
Neale Ranns9c2c2432017-12-05 13:34:36 -0800839 u8 *p = NULL;
Chris Luke862623d2016-05-14 10:13:34 -0400840 word i, j, k;
841 word line_index, len;
842 u32 width = cf->width;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400843 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -0400844
845 if (line == NULL)
846 {
847 /* Use a line already in the pager buffer */
848 line_index = len_or_index;
Neale Ranns9c2c2432017-12-05 13:34:36 -0800849 if (cf->pager_vector != NULL)
850 p = cf->pager_vector[line_index];
Chris Luke862623d2016-05-14 10:13:34 -0400851 len = vec_len (p);
852 }
853 else
854 {
855 len = len_or_index;
856 /* Add a copy of the raw string to the pager buffer */
857 p = vec_new (u8, len);
858 clib_memcpy (p, line, len);
859
860 /* store in pager buffer */
861 line_index = vec_len (cf->pager_vector);
862 vec_add1 (cf->pager_vector, p);
863 }
864
865 i = 0;
866 while (i < len)
867 {
868 /* Find the next line, or run to terminal width, or run to EOL */
869 int l = len - i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400870 j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width);
Chris Luke862623d2016-05-14 10:13:34 -0400871
Dave Barach9b8ffd92016-07-08 08:13:45 -0400872 if (j < l && p[j] == '\n') /* incl \n */
873 j++;
Chris Luke862623d2016-05-14 10:13:34 -0400874
875 /* Add the line to the index */
876 k = vec_len (cf->pager_index);
877 vec_validate (cf->pager_index, k);
878 pi = &cf->pager_index[k];
879
880 pi->line = line_index;
881 pi->offset = i;
882 pi->length = j;
883
884 i += j;
885 p += j;
886 }
887}
888
889/** @brief Reindex entire pager buffer.
890 * Resets the current pager index and then re-adds the lines in the pager
891 * buffer to the index.
892 *
893 * Additionally this function attempts to retain the current page start
894 * line offset by searching for the same top-of-screen line in the new index.
895 *
896 * @param cf The CLI session whose pager buffer should be reindexed.
897 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400898static void
899unix_cli_pager_reindex (unix_cli_file_t * cf)
Chris Luke862623d2016-05-14 10:13:34 -0400900{
901 word i, old_line, old_offset;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400902 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -0400903
904 /* If there is nothing in the pager buffer then make sure the index
905 * is empty and move on.
906 */
907 if (cf->pager_vector == 0)
908 {
909 vec_reset_length (cf->pager_index);
910 return;
911 }
912
913 /* Retain a pointer to the current page start line so we can
914 * find it later
915 */
916 pi = &cf->pager_index[cf->pager_start];
917 old_line = pi->line;
918 old_offset = pi->offset;
919
920 /* Re-add the buffered lines to the index */
921 vec_reset_length (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400922 vec_foreach_index (i, cf->pager_vector)
923 {
924 unix_cli_pager_add_line (cf, NULL, i);
925 }
Chris Luke862623d2016-05-14 10:13:34 -0400926
927 /* Attempt to re-locate the previously stored page start line */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400928 vec_foreach_index (i, cf->pager_index)
929 {
930 pi = &cf->pager_index[i];
Chris Luke862623d2016-05-14 10:13:34 -0400931
Dave Barach9b8ffd92016-07-08 08:13:45 -0400932 if (pi->line == old_line &&
933 (pi->offset <= old_offset || pi->offset + pi->length > old_offset))
934 {
935 /* Found it! */
936 cf->pager_start = i;
937 break;
938 }
939 }
Chris Luke862623d2016-05-14 10:13:34 -0400940
941 /* In case the start line was not found (rare), ensure the pager start
942 * index is within bounds
943 */
944 if (cf->pager_start >= vec_len (cf->pager_index))
945 {
Chris Luke270b6de2016-05-19 14:23:25 -0400946 if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400947 cf->pager_start = 0;
Chris Luke270b6de2016-05-19 14:23:25 -0400948 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400949 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
Chris Luke862623d2016-05-14 10:13:34 -0400950 }
951}
952
953/** VLIB CLI output function.
954 *
955 * If the terminal has a pager configured then this function takes care
956 * of collating output into the pager buffer; ensuring only the first page
957 * is displayed and any lines in excess of the first page are buffered.
958 *
959 * If the maximum number of index lines in the buffer is exceeded then the
960 * pager is cancelled and the contents of the current buffer are sent to the
961 * terminal.
962 *
963 * If there is no pager configured then the output is sent directly to the
964 * terminal.
965 *
966 * @param cli_file_index Index of the CLI session where this output is
967 * directed.
968 * @param buffer String of printabe bytes to be output.
969 * @param buffer_bytes The number of bytes in @c buffer to be output.
970 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400971static void
972unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400974 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +0200975 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400976 unix_cli_main_t *cm = &unix_cli_main;
977 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +0200978 clib_file_t *uf;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400979
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +0200981 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982
Chris Luke7afda3a2016-04-25 13:49:22 -0400983 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
984 {
Chris Luke862623d2016-05-14 10:13:34 -0400985 unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes);
Chris Luke7afda3a2016-04-25 13:49:22 -0400986 }
987 else
988 {
Chris Luke862623d2016-05-14 10:13:34 -0400989 word row = vec_len (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400990 u8 *line;
991 unix_cli_pager_index_t *pi;
Chris Luke7afda3a2016-04-25 13:49:22 -0400992
Chris Luke862623d2016-05-14 10:13:34 -0400993 /* Index and add the output lines to the pager buffer. */
994 unix_cli_pager_add_line (cf, buffer, buffer_bytes);
995
996 /* Now iterate what was added to display the lines.
997 * If we reach the bottom of the page, display a prompt.
998 */
999 while (row < vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001000 {
1001 if (row < cf->height - 1)
1002 {
1003 /* output this line */
1004 pi = &cf->pager_index[row];
1005 line = cf->pager_vector[pi->line] + pi->offset;
1006 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
Chris Luke862623d2016-05-14 10:13:34 -04001007
Dave Barach9b8ffd92016-07-08 08:13:45 -04001008 /* if the last line didn't end in newline, and we're at the
1009 * bottom of the page, add a newline */
1010 if (line[pi->length - 1] != '\n' && row == cf->height - 2)
1011 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1012 }
1013 else
1014 {
1015 /* Display the pager prompt every 10 lines */
1016 if (!(row % 10))
1017 unix_cli_pager_prompt (cf, uf);
1018 }
1019 row++;
1020 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001021
Chris Luke862623d2016-05-14 10:13:34 -04001022 /* Check if we went over the pager buffer limit */
1023 if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001024 {
1025 /* Stop using the pager for the remainder of this CLI command */
1026 cf->no_pager = 2;
Chris Luke7afda3a2016-04-25 13:49:22 -04001027
Dave Barach9b8ffd92016-07-08 08:13:45 -04001028 /* If we likely printed the prompt, erase it */
1029 if (vec_len (cf->pager_index) > cf->height - 1)
1030 unix_cli_pager_prompt_erase (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04001031
Dave Barach9b8ffd92016-07-08 08:13:45 -04001032 /* Dump out the contents of the buffer */
1033 for (row = cf->pager_start + (cf->height - 1);
1034 row < vec_len (cf->pager_index); row++)
1035 {
1036 pi = &cf->pager_index[row];
1037 line = cf->pager_vector[pi->line] + pi->offset;
1038 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1039 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001040
Dave Barach9b8ffd92016-07-08 08:13:45 -04001041 unix_cli_pager_reset (cf);
1042 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001043 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001044}
1045
Chris Luke862623d2016-05-14 10:13:34 -04001046/** Identify whether a terminal type is ANSI capable.
1047 *
Chris Luke54ccf222016-07-25 16:38:11 -04001048 * Compares the string given in @c term with a list of terminal types known
Chris Luke862623d2016-05-14 10:13:34 -04001049 * to support ANSI escape sequences.
1050 *
1051 * This list contains, for example, @c xterm, @c screen and @c ansi.
1052 *
1053 * @param term A string with a terminal type in it.
Chris Luke54ccf222016-07-25 16:38:11 -04001054 * @param len The length of the string in @c term.
Chris Luke862623d2016-05-14 10:13:34 -04001055 *
1056 * @return @c 1 if the terminal type is recognized as supporting ANSI
1057 * terminal sequences; @c 0 otherwise.
1058 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001059static u8
Chris Luke03add7f2017-09-20 23:31:24 -04001060unix_cli_terminal_type_ansi (u8 * term, uword len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001061{
Chris Luke0aca5eb2016-04-25 13:48:54 -04001062 /* This may later be better done as a hash of some sort. */
1063#define _(a) do { \
1064 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1065 } while(0)
1066
1067 _("xterm");
1068 _("xterm-color");
Dave Barach9b8ffd92016-07-08 08:13:45 -04001069 _("xterm-256color"); /* iTerm on Mac */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001070 _("screen");
Damjan Marion6e8ab162017-06-05 21:56:12 +02001071 _("screen-256color"); /* Screen and tmux */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001072 _("ansi"); /* Microsoft Telnet */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001073#undef _
1074
1075 return 0;
1076}
1077
Chris Luke03add7f2017-09-20 23:31:24 -04001078/** Identify whether a terminal type is non-interactive.
1079 *
1080 * Compares the string given in @c term with a list of terminal types known
1081 * to be non-interactive, as send by tools such as @c vppctl .
1082 *
1083 * This list contains, for example, @c vppctl.
1084 *
1085 * @param term A string with a terminal type in it.
1086 * @param len The length of the string in @c term.
1087 *
1088 * @return @c 1 if the terminal type is recognized as being non-interactive;
1089 * @c 0 otherwise.
1090 */
1091static u8
1092unix_cli_terminal_type_noninteractive (u8 * term, uword len)
1093{
1094 /* This may later be better done as a hash of some sort. */
1095#define _(a) do { \
1096 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1097 } while(0)
1098
1099 _("vppctl");
1100#undef _
1101
1102 return 0;
1103}
1104
1105/** Set a session to be non-interactive. */
1106static void
1107unix_cli_set_session_noninteractive (unix_cli_file_t * cf)
1108{
1109 /* Non-interactive sessions don't get these */
1110 cf->is_interactive = 0;
1111 cf->no_pager = 1;
1112 cf->history_limit = 0;
1113 cf->has_history = 0;
1114 cf->line_mode = 1;
1115}
1116
Chris Luke8e5b0412016-07-26 13:06:10 -04001117/** @brief Emit initial welcome banner and prompt on a connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001118static void
1119unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001120{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001121 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02001122 clib_file_main_t *fm = &file_main;
1123 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke572d8122016-04-25 13:49:07 -04001124 unix_cli_banner_t *banner;
1125 int i, len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001126
Chris Luke03add7f2017-09-20 23:31:24 -04001127 /* Mark the session as started if we get here */
1128 cf->started = 1;
1129
1130 if (!(cf->is_interactive)) /* No banner for non-interactive sessions */
1131 return;
1132
Chris Luke0aca5eb2016-04-25 13:48:54 -04001133 /*
1134 * Put the first bytes directly into the buffer so that further output is
1135 * queued until everything is ready. (oterwise initial prompt can appear
1136 * mid way through VPP initialization)
1137 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001138 unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
Chris Luke572d8122016-04-25 13:49:07 -04001139
1140 if (!um->cli_no_banner)
1141 {
1142 if (cf->ansi_capable)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001143 {
1144 banner = unix_cli_banner_color;
1145 len = ARRAY_LEN (unix_cli_banner_color);
1146 }
Chris Luke572d8122016-04-25 13:49:07 -04001147 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001148 {
1149 banner = unix_cli_banner;
1150 len = ARRAY_LEN (unix_cli_banner);
1151 }
Chris Luke572d8122016-04-25 13:49:07 -04001152
1153 for (i = 0; i < len; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001154 {
1155 unix_vlib_cli_output_cooked (cf, uf,
1156 banner[i].line, banner[i].length);
1157 }
1158 }
Chris Luke572d8122016-04-25 13:49:07 -04001159
1160 /* Prompt. */
Chris Luke7afda3a2016-04-25 13:49:22 -04001161 unix_cli_cli_prompt (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001162
Chris Luke0aca5eb2016-04-25 13:48:54 -04001163}
1164
Chris Luke8e5b0412016-07-26 13:06:10 -04001165/** @brief A failsafe triggered on a timer to ensure we send the prompt
Chris Luke0aca5eb2016-04-25 13:48:54 -04001166 * to telnet sessions that fail to negotiate the terminal type. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001167static void
1168unix_cli_file_welcome_timer (any arg, f64 delay)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001169{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001170 unix_cli_main_t *cm = &unix_cli_main;
1171 unix_cli_file_t *cf;
1172 (void) delay;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001173
1174 /* Check the connection didn't close already */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001175 if (pool_is_free_index (cm->cli_file_pool, (uword) arg))
Chris Luke0aca5eb2016-04-25 13:48:54 -04001176 return;
1177
Dave Barach9b8ffd92016-07-08 08:13:45 -04001178 cf = pool_elt_at_index (cm->cli_file_pool, (uword) arg);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001179
1180 if (!cf->started)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001181 unix_cli_file_welcome (cm, cf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001182}
1183
Chris Luke8e5b0412016-07-26 13:06:10 -04001184/** @brief A mostly no-op Telnet state machine.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001185 * Process Telnet command bytes in a way that ensures we're mostly
1186 * transparent to the Telnet protocol. That is, it's mostly a no-op.
1187 *
1188 * @return -1 if we need more bytes, otherwise a positive integer number of
1189 * bytes to consume from the input_vector, not including the initial
1190 * IAC byte.
1191 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001192static i32
1193unix_cli_process_telnet (unix_main_t * um,
1194 unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +02001195 clib_file_t * uf, u8 * input_vector, uword len)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001196{
1197 /* Input_vector starts at IAC byte.
1198 * See if we have a complete message; if not, return -1 so we wait for more.
1199 * if we have a complete message, consume those bytes from the vector.
1200 */
1201 i32 consume = 0;
1202
1203 if (len == 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001204 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001205
1206 switch (input_vector[1])
Ed Warnickecb9cada2015-12-08 15:45:58 -07001207 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001208 case IAC:
1209 /* two IAC's in a row means to pass through 0xff.
1210 * since that makes no sense here, just consume it.
1211 */
1212 consume = 1;
1213 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001214
Dave Barach9b8ffd92016-07-08 08:13:45 -04001215 case WILL:
1216 case WONT:
1217 case DO:
1218 case DONT:
1219 /* Expect 3 bytes */
1220 if (vec_len (input_vector) < 3)
1221 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001222
Dave Barach9b8ffd92016-07-08 08:13:45 -04001223 consume = 2;
1224 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001225
Dave Barach9b8ffd92016-07-08 08:13:45 -04001226 case SB:
1227 {
1228 /* Sub option - search ahead for IAC SE to end it */
1229 i32 i;
1230 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
1231 {
1232 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
1233 {
1234 /* We have a complete message; see if we care about it */
1235 switch (input_vector[2])
1236 {
1237 case TELOPT_TTYPE:
1238 if (input_vector[3] != 0)
1239 break;
Chris Luke03add7f2017-09-20 23:31:24 -04001240 {
1241 /* See if the the terminal type is recognized */
1242 u8 *term = input_vector + 4;
1243 uword len = i - 5;
1244
1245 /* See if the terminal type is ANSI capable */
1246 cf->ansi_capable =
1247 unix_cli_terminal_type_ansi (term, len);
1248
1249 /* See if the terminal type indicates non-interactive */
1250 if (unix_cli_terminal_type_noninteractive (term, len))
1251 unix_cli_set_session_noninteractive (cf);
1252 }
1253
Dave Barach9b8ffd92016-07-08 08:13:45 -04001254 /* If session not started, we can release the pause */
1255 if (!cf->started)
1256 /* Send the welcome banner and initial prompt */
1257 unix_cli_file_welcome (&unix_cli_main, cf);
1258 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259
Dave Barach9b8ffd92016-07-08 08:13:45 -04001260 case TELOPT_NAWS:
1261 /* Window size */
1262 if (i != 8) /* check message is correct size */
1263 break;
Chris Lukeb2bcad62017-09-18 08:51:22 -04001264
Dave Barach9b8ffd92016-07-08 08:13:45 -04001265 cf->width =
1266 clib_net_to_host_u16 (*((u16 *) (input_vector + 3)));
Chris Lukeb2bcad62017-09-18 08:51:22 -04001267 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
1268 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
Chris Luke2d8bf302017-11-12 22:26:37 -05001269 if (cf->width == 0)
1270 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
Chris Lukeb2bcad62017-09-18 08:51:22 -04001271
Dave Barach9b8ffd92016-07-08 08:13:45 -04001272 cf->height =
1273 clib_net_to_host_u16 (*((u16 *) (input_vector + 5)));
Chris Lukeb2bcad62017-09-18 08:51:22 -04001274 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
1275 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
Chris Luke2d8bf302017-11-12 22:26:37 -05001276 if (cf->height == 0)
1277 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
Chris Lukeb2bcad62017-09-18 08:51:22 -04001278
Dave Barach9b8ffd92016-07-08 08:13:45 -04001279 /* reindex pager buffer */
1280 unix_cli_pager_reindex (cf);
1281 /* redraw page */
1282 unix_cli_pager_redraw (cf, uf);
1283 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04001284
Dave Barach9b8ffd92016-07-08 08:13:45 -04001285 default:
1286 break;
1287 }
1288 /* Consume it all */
1289 consume = i;
1290 break;
1291 }
1292 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001293
Dave Barach9b8ffd92016-07-08 08:13:45 -04001294 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
1295 consume = 1; /* hit max search depth, advance one byte */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001296
Dave Barach9b8ffd92016-07-08 08:13:45 -04001297 if (consume == 0)
1298 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001299
Dave Barach9b8ffd92016-07-08 08:13:45 -04001300 break;
1301 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001302
Dave Barach9b8ffd92016-07-08 08:13:45 -04001303 case GA:
1304 case EL:
1305 case EC:
1306 case AO:
1307 case IP:
1308 case BREAK:
1309 case DM:
1310 case NOP:
1311 case SE:
1312 case EOR:
1313 case ABORT:
1314 case SUSP:
1315 case xEOF:
1316 /* Simple one-byte messages */
1317 consume = 1;
1318 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001319
Dave Barach9b8ffd92016-07-08 08:13:45 -04001320 case AYT:
1321 /* Are You There - trigger a visible response */
1322 consume = 1;
1323 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
1324 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001325
Dave Barach9b8ffd92016-07-08 08:13:45 -04001326 default:
1327 /* Unknown command! Eat the IAC byte */
1328 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001329 }
1330
Dave Barach9b8ffd92016-07-08 08:13:45 -04001331 return consume;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001332}
1333
Chris Luke8e5b0412016-07-26 13:06:10 -04001334/** @brief Process actionable input.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001335 * Based on the \c action process the input; this typically involves
1336 * searching the command history or editing the current command line.
1337 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001338static int
1339unix_cli_line_process_one (unix_cli_main_t * cm,
1340 unix_main_t * um,
1341 unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +02001342 clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001343 u8 input, unix_cli_parse_action_t action)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001344{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001345 u8 *prev;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001346 u8 *save = 0;
1347 u8 **possible_commands;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001348 int j, delta;
1349
1350 switch (action)
1351 {
1352 case UNIX_CLI_PARSE_ACTION_NOACTION:
1353 break;
1354
Chris Luke0aca5eb2016-04-25 13:48:54 -04001355 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
1356 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
Chris Luke7afda3a2016-04-25 13:49:22 -04001357 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001358 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001359 if (cf->search_mode == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001360 {
1361 /* Erase the current command (if any) */
1362 for (j = 0; j < (vec_len (cf->current_command)); j++)
1363 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001364
Dave Barach9b8ffd92016-07-08 08:13:45 -04001365 vec_reset_length (cf->search_key);
1366 vec_reset_length (cf->current_command);
1367 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1368 cf->search_mode = -1;
1369 else
1370 cf->search_mode = 1;
1371 cf->cursor = 0;
1372 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001373 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001374 {
1375 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1376 cf->search_mode = -1;
1377 else
1378 cf->search_mode = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001379
Dave Barach9b8ffd92016-07-08 08:13:45 -04001380 cf->excursion += cf->search_mode;
1381 goto search_again;
1382 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001383 break;
1384
1385 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
1386 /* Erase the command from the cursor to the start */
1387
1388 /* Shimmy forwards to the new end of line position */
1389 delta = vec_len (cf->current_command) - cf->cursor;
1390 for (j = cf->cursor; j > delta; j--)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001391 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001392 /* Zap from here to the end of what is currently displayed */
1393 for (; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001394 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001395 /* Get back to the start of the line */
1396 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001397 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001398
Dave Barach9b8ffd92016-07-08 08:13:45 -04001399 j = vec_len (cf->current_command) - cf->cursor;
1400 memmove (cf->current_command, cf->current_command + cf->cursor, j);
1401 _vec_len (cf->current_command) = j;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001402
1403 /* Print the new contents */
1404 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j);
1405 /* Shimmy back to the start */
1406 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001407 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001408 cf->cursor = 0;
1409
1410 cf->search_mode = 0;
1411 break;
1412
1413 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
1414 /* Erase the command from the cursor to the end */
1415
1416 /* Zap from cursor to end of what is currently displayed */
1417 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001418 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001419 /* Get back to where we were */
1420 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001421 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001422
1423 /* Truncate the line at the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001424 _vec_len (cf->current_command) = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001425
1426 cf->search_mode = 0;
1427 break;
1428
1429 case UNIX_CLI_PARSE_ACTION_LEFT:
1430 if (cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001431 {
1432 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1433 cf->cursor--;
1434 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001435
1436 cf->search_mode = 0;
1437 break;
1438
1439 case UNIX_CLI_PARSE_ACTION_RIGHT:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001440 if (cf->cursor < vec_len (cf->current_command))
1441 {
1442 /* have to emit the character under the cursor */
1443 unix_vlib_cli_output_cooked (cf, uf,
1444 cf->current_command + cf->cursor, 1);
1445 cf->cursor++;
1446 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001447
1448 cf->search_mode = 0;
1449 break;
1450
1451 case UNIX_CLI_PARSE_ACTION_UP:
1452 case UNIX_CLI_PARSE_ACTION_DOWN:
Chris Luke7afda3a2016-04-25 13:49:22 -04001453 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001454 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001455 cf->search_mode = 0;
1456 /* Erase the command */
1457 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001458 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001459 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001460 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001461 vec_reset_length (cf->current_command);
1462 if (vec_len (cf->command_history))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001463 {
1464 if (action == UNIX_CLI_PARSE_ACTION_UP)
1465 delta = -1;
1466 else
1467 delta = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001468
Dave Barach9b8ffd92016-07-08 08:13:45 -04001469 cf->excursion += delta;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001470
Dave Barach9b8ffd92016-07-08 08:13:45 -04001471 if (cf->excursion == vec_len (cf->command_history))
1472 {
1473 /* down-arrowed to last entry - want a blank line */
1474 _vec_len (cf->current_command) = 0;
1475 }
1476 else if (cf->excursion < 0)
1477 {
1478 /* up-arrowed over the start to the end, want a blank line */
1479 cf->excursion = vec_len (cf->command_history);
1480 _vec_len (cf->current_command) = 0;
1481 }
1482 else
1483 {
1484 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1485 /* down-arrowed past end - wrap to start */
1486 cf->excursion = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001487
Dave Barach9b8ffd92016-07-08 08:13:45 -04001488 /* Print the command at the current position */
1489 prev = cf->command_history[cf->excursion];
1490 vec_validate (cf->current_command, vec_len (prev) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001491
Dave Barach9b8ffd92016-07-08 08:13:45 -04001492 clib_memcpy (cf->current_command, prev, vec_len (prev));
1493 _vec_len (cf->current_command) = vec_len (prev);
1494 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1495 vec_len (cf->current_command));
1496 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001497 }
Yoann Desmouceaux561ae0a2017-09-20 10:08:28 +02001498 cf->cursor = vec_len (cf->current_command);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001499 break;
1500
1501 case UNIX_CLI_PARSE_ACTION_HOME:
1502 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001503 {
1504 while (cf->cursor)
1505 {
1506 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1507 cf->cursor--;
1508 }
1509 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001510
Chris Luke0aca5eb2016-04-25 13:48:54 -04001511 cf->search_mode = 0;
1512 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001513
Chris Luke0aca5eb2016-04-25 13:48:54 -04001514 case UNIX_CLI_PARSE_ACTION_END:
1515 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001516 cf->cursor < vec_len (cf->current_command))
1517 {
1518 unix_vlib_cli_output_cooked (cf, uf,
1519 cf->current_command + cf->cursor,
1520 vec_len (cf->current_command) -
1521 cf->cursor);
1522 cf->cursor = vec_len (cf->current_command);
1523 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001524
1525 cf->search_mode = 0;
1526 break;
1527
1528 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1529 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001530 {
1531 j = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001532
Dave Barach9b8ffd92016-07-08 08:13:45 -04001533 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1534 j--;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001535
Dave Barach9b8ffd92016-07-08 08:13:45 -04001536 while (j && isspace (cf->current_command[j]))
1537 {
1538 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1539 j--;
1540 }
1541 while (j && !isspace (cf->current_command[j]))
1542 {
1543 if (isspace (cf->current_command[j - 1]))
1544 break;
1545 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1546 j--;
1547 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001548
Dave Barach9b8ffd92016-07-08 08:13:45 -04001549 cf->cursor = j;
1550 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001551
1552 cf->search_mode = 0;
1553 break;
1554
1555 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1556 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001557 cf->cursor < vec_len (cf->current_command))
1558 {
1559 int e = vec_len (cf->current_command);
1560 j = cf->cursor;
1561 while (j < e && !isspace (cf->current_command[j]))
1562 j++;
1563 while (j < e && isspace (cf->current_command[j]))
1564 j++;
1565 unix_vlib_cli_output_cooked (cf, uf,
1566 cf->current_command + cf->cursor,
1567 j - cf->cursor);
1568 cf->cursor = j;
1569 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001570
1571 cf->search_mode = 0;
1572 break;
1573
1574
1575 case UNIX_CLI_PARSE_ACTION_ERASE:
1576 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001577 {
1578 if (cf->cursor == vec_len (cf->current_command))
1579 {
1580 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1581 _vec_len (cf->current_command)--;
1582 cf->cursor--;
1583 }
1584 else if (cf->cursor > 0)
1585 {
1586 /* shift everything at & to the right of the cursor left by 1 */
1587 j = vec_len (cf->current_command) - cf->cursor;
1588 memmove (cf->current_command + cf->cursor - 1,
1589 cf->current_command + cf->cursor, j);
1590 _vec_len (cf->current_command)--;
1591 cf->cursor--;
1592 /* redraw the rest of the line */
1593 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1594 unix_vlib_cli_output_cooked (cf, uf,
1595 cf->current_command + cf->cursor,
1596 j);
1597 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
1598 /* and shift the terminal cursor back where it should be */
1599 while (--j)
1600 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1601 }
1602 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001603 cf->search_mode = 0;
1604 cf->excursion = 0;
1605 vec_reset_length (cf->search_key);
1606 break;
1607
1608 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1609 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001610 {
1611 if (cf->cursor < vec_len (cf->current_command))
1612 {
1613 /* shift everything to the right of the cursor left by 1 */
1614 j = vec_len (cf->current_command) - cf->cursor - 1;
1615 memmove (cf->current_command + cf->cursor,
1616 cf->current_command + cf->cursor + 1, j);
1617 _vec_len (cf->current_command)--;
1618 /* redraw the rest of the line */
1619 unix_vlib_cli_output_cooked (cf, uf,
1620 cf->current_command + cf->cursor,
1621 j);
1622 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
1623 /* and shift the terminal cursor back where it should be */
1624 if (j)
1625 {
1626 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1627 while (--j)
1628 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1629 }
1630 }
1631 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001632 else if (input == 'D' - '@')
Dave Barach9b8ffd92016-07-08 08:13:45 -04001633 {
1634 /* ^D with no command entered = quit */
1635 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1636 vlib_process_signal_event (um->vlib_main,
1637 vlib_current_process (um->vlib_main),
1638 UNIX_CLI_PROCESS_EVENT_QUIT,
1639 cf - cm->cli_file_pool);
1640 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001641 cf->search_mode = 0;
1642 cf->excursion = 0;
1643 vec_reset_length (cf->search_key);
1644 break;
1645
1646 case UNIX_CLI_PARSE_ACTION_CLEAR:
1647 /* If we're in ANSI mode, clear the screen.
1648 * Then redraw the prompt and any existing command input, then put
1649 * the cursor back where it was in that line.
1650 */
1651 if (cf->ansi_capable)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001652 unix_vlib_cli_output_cooked (cf, uf,
1653 (u8 *) ANSI_CLEAR,
1654 sizeof (ANSI_CLEAR) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001655 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001656 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001657
1658 unix_vlib_cli_output_raw (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001659 cm->cli_prompt, vec_len (cm->cli_prompt));
Chris Luke0aca5eb2016-04-25 13:48:54 -04001660 unix_vlib_cli_output_raw (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001661 cf->current_command,
1662 vec_len (cf->current_command));
1663 for (j = cf->cursor; j < vec_len (cf->current_command); j++)
1664 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001665
1666 break;
1667
Chris Luke7afda3a2016-04-25 13:49:22 -04001668 case UNIX_CLI_PARSE_ACTION_TAB:
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001669 if (cf->cursor < vec_len (cf->current_command))
1670 {
1671 /* if we are in the middle of a line, complete only if
1672 * the cursor points to whitespace */
1673 if (isspace (cf->current_command[cf->cursor]))
1674 {
1675 /* save and clear any input that is after the cursor */
1676 vec_resize (save, vec_len (cf->current_command) - cf->cursor);
1677 clib_memcpy (save, cf->current_command + cf->cursor,
1678 vec_len (cf->current_command) - cf->cursor);
1679 _vec_len (cf->current_command) = cf->cursor;
1680 }
1681 else
1682 {
1683 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1684 break;
1685 }
1686 }
1687 possible_commands =
1688 vlib_cli_get_possible_completions (cf->current_command);
1689 if (vec_len (possible_commands) == 1)
1690 {
1691 u32 j = cf->cursor;
1692 u8 *completed = possible_commands[0];
1693
1694 /* find the last word of current_command */
1695 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1696 {
1697 j--;
1698 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1699 }
1700 _vec_len (cf->current_command) = j;
1701
1702 /* replace it with the newly expanded command */
1703 vec_append (cf->current_command, completed);
1704
1705 /* echo to the terminal */
1706 unix_vlib_cli_output_raw (cf, uf, completed, vec_len (completed));
1707
1708 /* add one trailing space if needed */
1709 if (vec_len (save) == 0)
1710 {
1711 vec_add1 (cf->current_command, ' ');
1712 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1713 }
1714
1715 cf->cursor = vec_len (cf->current_command);
1716
1717 }
1718 else if (vec_len (possible_commands) >= 2)
1719 {
1720 u8 **possible_command;
1721 uword max_command_len = 0, min_command_len = ~0;
1722 u32 i, j;
1723
1724 vec_foreach (possible_command, possible_commands)
1725 {
1726 if (vec_len (*possible_command) > max_command_len)
1727 {
1728 max_command_len = vec_len (*possible_command);
1729 }
1730 if (vec_len (*possible_command) < min_command_len)
1731 {
1732 min_command_len = vec_len (*possible_command);
1733 }
1734 }
1735
1736 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1737
1738 i = 0;
1739 vec_foreach (possible_command, possible_commands)
1740 {
1741 if (i + max_command_len >= cf->width)
1742 {
1743 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1744 i = 0;
1745 }
1746 unix_vlib_cli_output_raw (cf, uf, *possible_command,
1747 vec_len (*possible_command));
1748 for (j = vec_len (*possible_command); j < max_command_len + 2;
1749 j++)
1750 {
1751 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1752 }
1753 i += max_command_len + 2;
1754 }
1755
1756 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1757
1758 /* rewrite prompt */
1759 unix_cli_cli_prompt (cf, uf);
1760 unix_vlib_cli_output_raw (cf, uf, cf->current_command,
1761 vec_len (cf->current_command));
1762
1763 /* count length of last word */
1764 j = cf->cursor;
1765 i = 0;
1766 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1767 {
1768 j--;
1769 i++;
1770 }
1771
1772 /* determine smallest common command */
1773 for (; i < min_command_len; i++)
1774 {
1775 u8 common = '\0';
1776 int stop = 0;
1777 vec_foreach (possible_command, possible_commands)
1778 {
1779 if (common == '\0')
1780 {
1781 common = (*possible_command)[i];
1782 }
1783 else if (common != (*possible_command)[i])
1784 {
1785 stop = 1;
1786 break;
1787 }
1788 }
1789 if (!stop)
1790 {
1791 vec_add1 (cf->current_command, common);
1792 cf->cursor++;
1793 unix_vlib_cli_output_raw (cf, uf, (u8 *) & common, 1);
1794 }
1795 else
1796 {
1797 break;
1798 }
1799 }
1800 }
1801 else
1802 {
1803 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1804 }
1805
1806 if (vec_len (save) > 0)
1807 {
1808 /* restore remaining input if tab was hit in the middle of a line */
1809 unix_vlib_cli_output_raw (cf, uf, save, vec_len (save));
1810 for (j = 0; j < vec_len (save); j++)
1811 {
1812 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1813 }
1814 vec_append (cf->current_command, save);
1815 vec_free (save);
1816 }
1817 vec_free (possible_commands);
1818
1819 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04001820 case UNIX_CLI_PARSE_ACTION_YANK:
1821 /* TODO */
1822 break;
1823
1824
1825 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
1826 pager_quit:
1827 unix_cli_pager_prompt_erase (cf, uf);
1828 unix_cli_pager_reset (cf);
1829 unix_cli_cli_prompt (cf, uf);
1830 break;
1831
1832 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
1833 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
1834 /* show next page of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001835 if (cf->height + cf->pager_start < vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001836 {
1837 u8 *line = NULL;
1838 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001839
Dave Barach9b8ffd92016-07-08 08:13:45 -04001840 int m = cf->pager_start + (cf->height - 1);
1841 unix_cli_pager_prompt_erase (cf, uf);
1842 for (j = m;
1843 j < vec_len (cf->pager_index) && cf->pager_start < m;
1844 j++, cf->pager_start++)
1845 {
1846 pi = &cf->pager_index[j];
1847 line = cf->pager_vector[pi->line] + pi->offset;
1848 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1849 }
1850 /* if the last line didn't end in newline, add a newline */
1851 if (pi && line[pi->length - 1] != '\n')
1852 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1853 unix_cli_pager_prompt (cf, uf);
1854 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001855 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001856 {
1857 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
1858 /* no more in buffer, exit, but only if it was <space> */
1859 goto pager_quit;
1860 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001861 break;
1862
1863 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
1864 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
1865 /* display the next line of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001866 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001867 {
1868 u8 *line;
1869 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -04001870
Dave Barach9b8ffd92016-07-08 08:13:45 -04001871 unix_cli_pager_prompt_erase (cf, uf);
1872 pi = &cf->pager_index[cf->pager_start + (cf->height - 1)];
1873 line = cf->pager_vector[pi->line] + pi->offset;
1874 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1875 cf->pager_start++;
1876 /* if the last line didn't end in newline, add a newline */
1877 if (line[pi->length - 1] != '\n')
1878 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1879 unix_cli_pager_prompt (cf, uf);
1880 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001881 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001882 {
1883 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
1884 /* no more in buffer, exit, but only if it was <enter> */
1885 goto pager_quit;
1886 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001887
1888 break;
1889
1890 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
1891 /* scroll the page back one line */
1892 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001893 {
1894 u8 *line = NULL;
1895 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001896
Dave Barach9b8ffd92016-07-08 08:13:45 -04001897 cf->pager_start--;
1898 if (cf->ansi_capable)
1899 {
1900 pi = &cf->pager_index[cf->pager_start];
1901 line = cf->pager_vector[pi->line] + pi->offset;
1902 unix_cli_pager_prompt_erase (cf, uf);
1903 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN,
1904 sizeof (ANSI_SCROLLDN) - 1);
1905 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR,
1906 sizeof (ANSI_SAVECURSOR) - 1);
1907 unix_cli_ansi_cursor (cf, uf, 1, 1);
1908 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE,
1909 sizeof (ANSI_CLEARLINE) - 1);
1910 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1911 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR,
1912 sizeof (ANSI_RESTCURSOR) - 1);
1913 unix_cli_pager_prompt_erase (cf, uf);
1914 unix_cli_pager_prompt (cf, uf);
1915 }
1916 else
1917 {
1918 int m = cf->pager_start + (cf->height - 1);
1919 unix_cli_pager_prompt_erase (cf, uf);
1920 for (j = cf->pager_start;
1921 j < vec_len (cf->pager_index) && j < m; j++)
1922 {
1923 pi = &cf->pager_index[j];
1924 line = cf->pager_vector[pi->line] + pi->offset;
1925 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1926 }
1927 /* if the last line didn't end in newline, add a newline */
1928 if (pi && line[pi->length - 1] != '\n')
1929 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1930 unix_cli_pager_prompt (cf, uf);
1931 }
1932 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001933 break;
1934
1935 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
1936 /* back to the first page of the buffer */
1937 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001938 {
1939 u8 *line = NULL;
1940 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001941
Dave Barach9b8ffd92016-07-08 08:13:45 -04001942 cf->pager_start = 0;
1943 int m = cf->pager_start + (cf->height - 1);
1944 unix_cli_pager_prompt_erase (cf, uf);
1945 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1946 j++)
1947 {
1948 pi = &cf->pager_index[j];
1949 line = cf->pager_vector[pi->line] + pi->offset;
1950 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1951 }
1952 /* if the last line didn't end in newline, add a newline */
1953 if (pi && line[pi->length - 1] != '\n')
1954 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1955 unix_cli_pager_prompt (cf, uf);
1956 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001957 break;
1958
1959 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
1960 /* skip to the last page of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001961 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001962 {
1963 u8 *line = NULL;
1964 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001965
Dave Barach9b8ffd92016-07-08 08:13:45 -04001966 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
1967 unix_cli_pager_prompt_erase (cf, uf);
1968 unix_cli_pager_message (cf, uf, "skipping", "\n");
1969 for (j = cf->pager_start; j < vec_len (cf->pager_index); j++)
1970 {
1971 pi = &cf->pager_index[j];
1972 line = cf->pager_vector[pi->line] + pi->offset;
1973 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1974 }
1975 /* if the last line didn't end in newline, add a newline */
1976 if (pi && line[pi->length - 1] != '\n')
1977 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1978 unix_cli_pager_prompt (cf, uf);
1979 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001980 break;
1981
1982 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
1983 /* wander back one page in the buffer */
1984 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001985 {
1986 u8 *line = NULL;
1987 unix_cli_pager_index_t *pi = NULL;
1988 int m;
Chris Luke862623d2016-05-14 10:13:34 -04001989
Dave Barach9b8ffd92016-07-08 08:13:45 -04001990 if (cf->pager_start >= cf->height)
1991 cf->pager_start -= cf->height - 1;
1992 else
1993 cf->pager_start = 0;
1994 m = cf->pager_start + cf->height - 1;
1995 unix_cli_pager_prompt_erase (cf, uf);
1996 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1997 j++)
1998 {
1999 pi = &cf->pager_index[j];
2000 line = cf->pager_vector[pi->line] + pi->offset;
2001 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2002 }
2003 /* if the last line didn't end in newline, add a newline */
2004 if (pi && line[pi->length - 1] != '\n')
2005 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2006 unix_cli_pager_prompt (cf, uf);
2007 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002008 break;
2009
Chris Luke862623d2016-05-14 10:13:34 -04002010 case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW:
2011 /* Redraw the current pager screen */
2012 unix_cli_pager_redraw (cf, uf);
2013 break;
2014
Chris Luke7afda3a2016-04-25 13:49:22 -04002015 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
2016 /* search forwards in the buffer */
2017 break;
2018
2019
Chris Luke0aca5eb2016-04-25 13:48:54 -04002020 case UNIX_CLI_PARSE_ACTION_CRLF:
2021 crlf:
Chris Luke0aca5eb2016-04-25 13:48:54 -04002022 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2023
Chris Luke7afda3a2016-04-25 13:49:22 -04002024 if (cf->has_history && cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002025 {
2026 if (cf->command_history
2027 && vec_len (cf->command_history) >= cf->history_limit)
2028 {
2029 vec_free (cf->command_history[0]);
2030 vec_delete (cf->command_history, 1, 0);
2031 }
2032 /* Don't add blank lines to the cmd history */
2033 if (vec_len (cf->current_command))
2034 {
2035 /* Don't duplicate the previous command */
2036 j = vec_len (cf->command_history);
2037 if (j == 0 ||
2038 (vec_len (cf->current_command) !=
2039 vec_len (cf->command_history[j - 1])
2040 || memcmp (cf->current_command, cf->command_history[j - 1],
2041 vec_len (cf->current_command)) != 0))
2042 {
2043 /* copy the command to the history */
2044 u8 *c = 0;
2045 vec_append (c, cf->current_command);
2046 vec_add1 (cf->command_history, c);
2047 cf->command_number++;
2048 }
2049 }
2050 cf->excursion = vec_len (cf->command_history);
2051 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002052
Chris Luke0aca5eb2016-04-25 13:48:54 -04002053 cf->search_mode = 0;
2054 vec_reset_length (cf->search_key);
2055 cf->cursor = 0;
2056
2057 return 0;
2058
Chris Luke7afda3a2016-04-25 13:49:22 -04002059 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2060 case UNIX_CLI_PARSE_ACTION_NOMATCH:
Chris Luke862623d2016-05-14 10:13:34 -04002061 if (vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002062 {
2063 /* no-op for now */
2064 }
2065 else if (cf->has_history && cf->search_mode && isprint (input))
2066 {
2067 int k, limit, offset;
2068 u8 *item;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002069
Dave Barach9b8ffd92016-07-08 08:13:45 -04002070 vec_add1 (cf->search_key, input);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002071
Dave Barach9b8ffd92016-07-08 08:13:45 -04002072 search_again:
2073 for (j = 0; j < vec_len (cf->command_history); j++)
2074 {
2075 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
2076 cf->excursion = 0;
2077 else if (cf->excursion < 0)
2078 cf->excursion = vec_len (cf->command_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002079
Dave Barach9b8ffd92016-07-08 08:13:45 -04002080 item = cf->command_history[cf->excursion];
Ed Warnickecb9cada2015-12-08 15:45:58 -07002081
Dave Barach9b8ffd92016-07-08 08:13:45 -04002082 limit = (vec_len (cf->search_key) > vec_len (item)) ?
2083 vec_len (item) : vec_len (cf->search_key);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002084
Dave Barach9b8ffd92016-07-08 08:13:45 -04002085 for (offset = 0; offset <= vec_len (item) - limit; offset++)
2086 {
2087 for (k = 0; k < limit; k++)
2088 {
2089 if (item[k + offset] != cf->search_key[k])
2090 goto next_offset;
2091 }
2092 goto found_at_offset;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002093
Dave Barach9b8ffd92016-07-08 08:13:45 -04002094 next_offset:
2095 ;
2096 }
2097 goto next;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002098
Dave Barach9b8ffd92016-07-08 08:13:45 -04002099 found_at_offset:
2100 for (j = 0; j < vec_len (cf->current_command); j++)
2101 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002102
Dave Barach9b8ffd92016-07-08 08:13:45 -04002103 vec_validate (cf->current_command, vec_len (item) - 1);
2104 clib_memcpy (cf->current_command, item, vec_len (item));
2105 _vec_len (cf->current_command) = vec_len (item);
Chris Luke93dcd1d2016-05-10 10:45:10 -04002106
Dave Barach9b8ffd92016-07-08 08:13:45 -04002107 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
2108 vec_len (cf->current_command));
2109 cf->cursor = vec_len (cf->current_command);
2110 goto found;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002111
Dave Barach9b8ffd92016-07-08 08:13:45 -04002112 next:
2113 cf->excursion += cf->search_mode;
2114 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002115
Dave Barach9b8ffd92016-07-08 08:13:45 -04002116 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12);
2117 vec_reset_length (cf->search_key);
2118 vec_reset_length (cf->current_command);
2119 cf->search_mode = 0;
2120 cf->cursor = 0;
2121 goto crlf;
2122 }
2123 else if (isprint (input)) /* skip any errant control codes */
2124 {
2125 if (cf->cursor == vec_len (cf->current_command))
2126 {
2127 /* Append to end */
2128 vec_add1 (cf->current_command, input);
2129 cf->cursor++;
Chris Luke7afda3a2016-04-25 13:49:22 -04002130
Dave Barach9b8ffd92016-07-08 08:13:45 -04002131 /* Echo the character back to the client */
2132 unix_vlib_cli_output_raw (cf, uf, &input, 1);
2133 }
2134 else
2135 {
2136 /* Insert at cursor: resize +1 byte, move everything over */
2137 j = vec_len (cf->current_command) - cf->cursor;
2138 vec_add1 (cf->current_command, (u8) 'A');
2139 memmove (cf->current_command + cf->cursor + 1,
2140 cf->current_command + cf->cursor, j);
2141 cf->current_command[cf->cursor] = input;
2142 /* Redraw the line */
2143 j++;
2144 unix_vlib_cli_output_raw (cf, uf,
2145 cf->current_command + cf->cursor, j);
2146 /* Put terminal cursor back */
2147 while (--j)
2148 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
2149 cf->cursor++;
2150 }
2151 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002152 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002153 {
2154 /* no-op - not printable or otherwise not actionable */
2155 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002156
2157 found:
2158
2159 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04002160
2161 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2162 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002163 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04002164 return 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002165}
2166
Chris Luke8e5b0412016-07-26 13:06:10 -04002167/** @brief Process input bytes on a stream to provide line editing and
Chris Luke0aca5eb2016-04-25 13:48:54 -04002168 * command history in the CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002169static int
Damjan Marion56dd5432017-09-08 19:52:02 +02002170unix_cli_line_edit (unix_cli_main_t * cm, unix_main_t * um,
2171 clib_file_main_t * fm, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04002172{
Damjan Marion56dd5432017-09-08 19:52:02 +02002173 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002174 int i;
2175
2176 for (i = 0; i < vec_len (cf->input_vector); i++)
2177 {
2178 unix_cli_parse_action_t action;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002179 i32 matched = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04002180 unix_cli_parse_actions_t *a;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002181
Chris Luke7afda3a2016-04-25 13:49:22 -04002182 /* If we're in the pager mode, search the pager actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002183 a =
2184 vec_len (cf->pager_index) ? unix_cli_parse_pager :
2185 unix_cli_parse_strings;
Chris Luke7afda3a2016-04-25 13:49:22 -04002186
Chris Luke93dcd1d2016-05-10 10:45:10 -04002187 /* See if the input buffer is some sort of control code */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002188 action = unix_cli_match_action (a, &cf->input_vector[i],
2189 vec_len (cf->input_vector) - i,
2190 &matched);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002191
2192 switch (action)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002193 {
2194 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2195 if (i)
2196 {
2197 /* There was a partial match which means we need more bytes
2198 * than the input buffer currently has.
2199 * Since the bytes before here have been processed, shift
2200 * the remaining contents to the start of the input buffer.
2201 */
2202 vec_delete (cf->input_vector, i, 0);
2203 }
2204 return 1; /* wait for more */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002205
Dave Barach9b8ffd92016-07-08 08:13:45 -04002206 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2207 /* process telnet options */
2208 matched = unix_cli_process_telnet (um, cf, uf,
2209 cf->input_vector + i,
2210 vec_len (cf->input_vector) - i);
2211 if (matched < 0)
2212 {
Chris Luke03add7f2017-09-20 23:31:24 -04002213 /* There was a partial match which means we need more bytes
2214 * than the input buffer currently has.
2215 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002216 if (i)
2217 {
Chris Luke03add7f2017-09-20 23:31:24 -04002218 /*
Dave Barach9b8ffd92016-07-08 08:13:45 -04002219 * Since the bytes before here have been processed, shift
2220 * the remaining contents to the start of the input buffer.
2221 */
2222 vec_delete (cf->input_vector, i, 0);
2223 }
2224 return 1; /* wait for more */
2225 }
2226 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002227
Dave Barach9b8ffd92016-07-08 08:13:45 -04002228 default:
Chris Luke03add7f2017-09-20 23:31:24 -04002229 /* If telnet option processing switched us to line mode, get us
2230 * out of here!
2231 */
2232 if (cf->line_mode)
2233 {
2234 vec_delete (cf->input_vector, i, 0);
2235 cf->current_command = cf->input_vector;
2236 return 0;
2237 }
2238
Dave Barach9b8ffd92016-07-08 08:13:45 -04002239 /* process the action */
2240 if (!unix_cli_line_process_one (cm, um, cf, uf,
2241 cf->input_vector[i], action))
2242 {
2243 /* CRLF found. Consume the bytes from the input_vector */
2244 vec_delete (cf->input_vector, i + matched, 0);
2245 /* And tell our caller to execute cf->input_command */
2246 return 0;
2247 }
2248 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002249
2250 i += matched;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002251 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002252
Dave Barach9b8ffd92016-07-08 08:13:45 -04002253 vec_reset_length (cf->input_vector);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002254 return 1;
2255}
2256
Chris Luke8e5b0412016-07-26 13:06:10 -04002257/** @brief Process input to a CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002258static void
2259unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002260{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002261 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002262 clib_file_main_t *fm = &file_main;
2263 clib_file_t *uf;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002264 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002265 unformat_input_t input;
2266 int vlib_parse_eval (u8 *);
2267
Chris Luke03add7f2017-09-20 23:31:24 -04002268 cm->current_input_file_index = cli_file_index;
2269
Chris Luke93dcd1d2016-05-10 10:45:10 -04002270more:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002271 /* Try vlibplex first. Someday... */
2272 if (0 && vlib_parse_eval (cf->input_vector) == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002273 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002274
Chris Luke03add7f2017-09-20 23:31:24 -04002275
Chris Luke93dcd1d2016-05-10 10:45:10 -04002276 if (cf->line_mode)
2277 {
2278 /* just treat whatever we got as a complete line of input */
2279 cf->current_command = cf->input_vector;
2280 }
2281 else
2282 {
2283 /* Line edit, echo, etc. */
Damjan Marion56dd5432017-09-08 19:52:02 +02002284 if (unix_cli_line_edit (cm, um, fm, cf))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002285 /* want more input */
2286 return;
Chris Luke93dcd1d2016-05-10 10:45:10 -04002287 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002288
2289 if (um->log_fd)
2290 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002291 static u8 *lv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002292 vec_reset_length (lv);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002293 lv = format (lv, "%U[%d]: %v",
2294 format_timeval, 0 /* current bat-time */ ,
2295 0 /* current bat-format */ ,
Chris Luke03add7f2017-09-20 23:31:24 -04002296 cli_file_index, cf->current_command);
2297 int rv __attribute__ ((unused)) = write (um->log_fd, lv, vec_len (lv));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002298 }
2299
Chris Luke03add7f2017-09-20 23:31:24 -04002300 /* Build an unformat structure around our command */
Chris Luke93dcd1d2016-05-10 10:45:10 -04002301 unformat_init_vector (&input, cf->current_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002302
2303 /* Remove leading white space from input. */
2304 (void) unformat (&input, "");
2305
Dave Barach9b8ffd92016-07-08 08:13:45 -04002306 cf->pager_start = 0; /* start a new pager session */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002307
2308 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002309 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output,
2310 cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002311
Ed Warnickecb9cada2015-12-08 15:45:58 -07002312 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
2313 input.buffer = 0;
2314
2315 unformat_free (&input);
2316
Chris Luke93dcd1d2016-05-10 10:45:10 -04002317 /* Re-fetch pointer since pool may have moved. */
2318 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002319 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke93dcd1d2016-05-10 10:45:10 -04002320
Ed Warnickecb9cada2015-12-08 15:45:58 -07002321done:
Chris Luke93dcd1d2016-05-10 10:45:10 -04002322 /* reset vector; we'll re-use it later */
2323 if (cf->line_mode)
Chris Luke03add7f2017-09-20 23:31:24 -04002324 {
2325 vec_reset_length (cf->input_vector);
2326 cf->current_command = 0;
2327 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002328 else
Chris Luke03add7f2017-09-20 23:31:24 -04002329 {
2330 vec_reset_length (cf->current_command);
2331 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002332
Chris Luke7afda3a2016-04-25 13:49:22 -04002333 if (cf->no_pager == 2)
2334 {
2335 /* Pager was programmatically disabled */
2336 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
2337 cf->no_pager = um->cli_no_pager;
2338 }
2339
Dave Barach9b8ffd92016-07-08 08:13:45 -04002340 if (vec_len (cf->pager_index) == 0
2341 || vec_len (cf->pager_index) < cf->height)
Chris Luke7afda3a2016-04-25 13:49:22 -04002342 {
2343 /* There was no need for the pager */
2344 unix_cli_pager_reset (cf);
2345
2346 /* Prompt. */
2347 unix_cli_cli_prompt (cf, uf);
2348 }
2349 else
2350 {
2351 /* Display the pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002352 unix_cli_pager_prompt (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04002353 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002354
Dave Barach9b8ffd92016-07-08 08:13:45 -04002355 /* Any residual data in the input vector? */
2356 if (vec_len (cf->input_vector))
2357 goto more;
Chris Luke03add7f2017-09-20 23:31:24 -04002358
2359 /* For non-interactive sessions send a NUL byte.
2360 * Specifically this is because vppctl needs to see some traffic in
2361 * order to move on to closing the session. Commands with no output
2362 * would thus cause vppctl to hang indefinitely in non-interactive mode
2363 * since there is also no prompt sent after the command completes.
2364 */
2365 if (!cf->is_interactive)
2366 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\0", 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002367}
2368
Chris Luke54ccf222016-07-25 16:38:11 -04002369/** Destroy a CLI session.
2370 * @note If we destroy the @c stdin session this additionally signals
2371 * the shutdown of VPP.
2372 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002373static void
2374unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002375{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002376 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002377 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002378 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +02002379 clib_file_t *uf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002380 int i;
2381
2382 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002383 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002384
2385 /* Quit/EOF on stdin means quit program. */
Chris Luke03add7f2017-09-20 23:31:24 -04002386 if (uf->file_descriptor == STDIN_FILENO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002387 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
2388
2389 vec_free (cf->current_command);
2390 vec_free (cf->search_key);
2391
2392 for (i = 0; i < vec_len (cf->command_history); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002393 vec_free (cf->command_history[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002394
2395 vec_free (cf->command_history);
2396
Damjan Marion56dd5432017-09-08 19:52:02 +02002397 clib_file_del (fm, uf);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002398
2399 unix_cli_file_free (cf);
2400 pool_put (cm->cli_file_pool, cf);
2401}
2402
Chris Luke54ccf222016-07-25 16:38:11 -04002403/** Handle system events. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002404static uword
2405unix_cli_process (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002406 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002407{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002408 unix_cli_main_t *cm = &unix_cli_main;
2409 uword i, *data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002410
2411 while (1)
2412 {
2413 unix_cli_process_event_type_t event_type;
2414 vlib_process_wait_for_event (vm);
2415 event_type = vlib_process_get_events (vm, &data);
2416
2417 switch (event_type)
2418 {
2419 case UNIX_CLI_PROCESS_EVENT_READ_READY:
2420 for (i = 0; i < vec_len (data); i++)
2421 unix_cli_process_input (cm, data[i]);
2422 break;
2423
2424 case UNIX_CLI_PROCESS_EVENT_QUIT:
2425 /* Kill this process. */
2426 for (i = 0; i < vec_len (data); i++)
2427 unix_cli_kill (cm, data[i]);
2428 goto done;
2429 }
2430
2431 if (data)
2432 _vec_len (data) = 0;
2433 }
2434
Dave Barach9b8ffd92016-07-08 08:13:45 -04002435done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002436 vec_free (data);
2437
2438 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
2439
2440 /* Add node index so we can re-use this process later. */
2441 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
2442
2443 return 0;
2444}
2445
Chris Luke54ccf222016-07-25 16:38:11 -04002446/** Called when a CLI session file descriptor can be written to without
2447 * blocking. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002448static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002449unix_cli_write_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002450{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002451 unix_cli_main_t *cm = &unix_cli_main;
2452 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002453 int n;
2454
2455 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2456
2457 /* Flush output vector. */
Chris Luke03add7f2017-09-20 23:31:24 -04002458 if (cf->is_socket)
2459 /* If it's a socket we use MSG_NOSIGNAL to prevent SIGPIPE */
2460 n = send (uf->file_descriptor,
2461 cf->output_vector, vec_len (cf->output_vector), MSG_NOSIGNAL);
2462 else
2463 n = write (uf->file_descriptor,
2464 cf->output_vector, vec_len (cf->output_vector));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002465
2466 if (n < 0 && errno != EAGAIN)
Chris Luke03add7f2017-09-20 23:31:24 -04002467 {
2468 if (errno == EPIPE)
2469 {
2470 /* connection closed on us */
2471 unix_main_t *um = &unix_main;
2472 cf->has_epipe = 1;
2473 vlib_process_signal_event (um->vlib_main, cf->process_node_index,
2474 UNIX_CLI_PROCESS_EVENT_QUIT,
2475 uf->private_data);
2476 }
2477 else
2478 {
2479 return clib_error_return_unix (0, "write");
2480 }
2481 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002482
2483 else if (n > 0)
2484 unix_cli_del_pending_output (uf, cf, n);
2485
2486 return /* no error */ 0;
2487}
2488
Chris Luke54ccf222016-07-25 16:38:11 -04002489/** Called when a CLI session file descriptor has data to be read. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002490static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002491unix_cli_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002492{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002493 unix_main_t *um = &unix_main;
2494 unix_cli_main_t *cm = &unix_cli_main;
2495 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002496 uword l;
2497 int n, n_read, n_try;
2498
2499 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2500
2501 n = n_try = 4096;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002502 while (n == n_try)
2503 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002504 l = vec_len (cf->input_vector);
2505 vec_resize (cf->input_vector, l + n_try);
2506
2507 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
2508
2509 /* Error? */
2510 if (n < 0 && errno != EAGAIN)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002511 return clib_error_return_unix (0, "read");
2512
Ed Warnickecb9cada2015-12-08 15:45:58 -07002513 n_read = n < 0 ? 0 : n;
2514 _vec_len (cf->input_vector) = l + n_read;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002515 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002516
Dave Barach9b8ffd92016-07-08 08:13:45 -04002517 if (!(n < 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002518 vlib_process_signal_event (um->vlib_main,
2519 cf->process_node_index,
2520 (n_read == 0
2521 ? UNIX_CLI_PROCESS_EVENT_QUIT
2522 : UNIX_CLI_PROCESS_EVENT_READ_READY),
2523 /* event data */ uf->private_data);
2524
2525 return /* no error */ 0;
2526}
2527
Chris Luke03add7f2017-09-20 23:31:24 -04002528/** Called when a CLI session file descriptor has an error condition. */
2529static clib_error_t *
2530unix_cli_error_detected (clib_file_t * uf)
2531{
2532 unix_main_t *um = &unix_main;
2533 unix_cli_main_t *cm = &unix_cli_main;
2534 unix_cli_file_t *cf;
2535
2536 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2537 cf->has_epipe = 1; /* prevent writes while the close is pending */
2538 vlib_process_signal_event (um->vlib_main,
2539 cf->process_node_index,
2540 UNIX_CLI_PROCESS_EVENT_QUIT,
2541 /* event data */ uf->private_data);
2542
2543 return /* no error */ 0;
2544}
2545
Chris Lukeb5850972016-05-03 16:34:59 -04002546/** Store a new CLI session.
2547 * @param name The name of the session.
2548 * @param fd The file descriptor for the session I/O.
2549 * @return The session ID.
2550 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002551static u32
2552unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002553{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002554 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002555 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002556 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +02002557 clib_file_t template = { 0 };
Dave Barach9b8ffd92016-07-08 08:13:45 -04002558 vlib_main_t *vm = um->vlib_main;
2559 vlib_node_t *n;
Damjan Marionceab7882018-01-19 20:56:12 +01002560 u8 *file_desc = 0;
2561
2562 file_desc = format (0, "%s", name);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002563
2564 name = (char *) format (0, "unix-cli-%s", name);
2565
2566 if (vec_len (cm->unused_cli_process_node_indices) > 0)
2567 {
2568 uword l = vec_len (cm->unused_cli_process_node_indices);
2569
2570 /* Find node and give it new name. */
2571 n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
2572 vec_free (n->name);
2573 n->name = (u8 *) name;
2574
2575 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
2576
2577 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
2578 }
2579 else
2580 {
2581 static vlib_node_registration_t r = {
2582 .function = unix_cli_process,
2583 .type = VLIB_NODE_TYPE_PROCESS,
Dave Barach96e12032016-06-09 15:32:48 -04002584 .process_log2_n_stack_bytes = 16,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002585 };
2586
2587 r.name = name;
2588 vlib_register_node (vm, &r);
2589 vec_free (name);
2590
2591 n = vlib_get_node (vm, r.index);
2592 }
2593
2594 pool_get (cm->cli_file_pool, cf);
2595 memset (cf, 0, sizeof (*cf));
2596
2597 template.read_function = unix_cli_read_ready;
2598 template.write_function = unix_cli_write_ready;
Chris Luke03add7f2017-09-20 23:31:24 -04002599 template.error_function = unix_cli_error_detected;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002600 template.file_descriptor = fd;
2601 template.private_data = cf - cm->cli_file_pool;
Damjan Marionceab7882018-01-19 20:56:12 +01002602 template.description = file_desc;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002603
2604 cf->process_node_index = n->index;
Damjan Marion56dd5432017-09-08 19:52:02 +02002605 cf->clib_file_index = clib_file_add (fm, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002606 cf->output_vector = 0;
2607 cf->input_vector = 0;
2608
Ed Warnickecb9cada2015-12-08 15:45:58 -07002609 vlib_start_process (vm, n->runtime_index);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002610
Dave Barach9b8ffd92016-07-08 08:13:45 -04002611 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002612 p->output_function = unix_vlib_cli_output;
2613 p->output_function_arg = cf - cm->cli_file_pool;
2614
Ed Warnickecb9cada2015-12-08 15:45:58 -07002615 return cf - cm->cli_file_pool;
2616}
2617
Chris Lukeb5850972016-05-03 16:34:59 -04002618/** Telnet listening socket has a new connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002619static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002620unix_cli_listen_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002621{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002622 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002623 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002624 unix_cli_main_t *cm = &unix_cli_main;
2625 clib_socket_t *s = &um->cli_listen_socket;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002626 clib_socket_t client;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002627 char *client_name;
2628 clib_error_t *error;
2629 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002630 u32 cf_index;
2631
2632 error = clib_socket_accept (s, &client);
2633 if (error)
2634 return error;
2635
2636 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
2637
2638 cf_index = unix_cli_file_add (cm, client_name, client.fd);
2639 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
Chris Luke03add7f2017-09-20 23:31:24 -04002640 cf->is_socket = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002641
2642 /* No longer need CLIB version of socket. */
2643 clib_socket_free (&client);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002644 vec_free (client_name);
2645
2646 /* if we're supposed to run telnet session in character mode (default) */
2647 if (um->cli_line_mode == 0)
2648 {
Chris Luke0aca5eb2016-04-25 13:48:54 -04002649 /*
2650 * Set telnet client character mode, echo on, suppress "go-ahead".
2651 * Technically these should be negotiated, but this works.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002652 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002653 u8 charmode_option[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002654 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
2655 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
2656 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
2657 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
2658 IAC, WILL, TELOPT_ECHO, /* server will do echo */
2659 IAC, DONT, TELOPT_ECHO, /* client should not echo */
2660 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
2661 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
2662 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
2663 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002664 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002665
Chris Luke0aca5eb2016-04-25 13:48:54 -04002666 /* Enable history on this CLI */
Chris Luke7afda3a2016-04-25 13:49:22 -04002667 cf->history_limit = um->cli_history_limit;
2668 cf->has_history = cf->history_limit != 0;
2669
Chris Luke03add7f2017-09-20 23:31:24 -04002670 /* This is an interactive session until we decide otherwise */
2671 cf->is_interactive = 1;
2672
Chris Luke7afda3a2016-04-25 13:49:22 -04002673 /* Make sure this session is in line mode */
2674 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002675
2676 /* We need CRLF */
2677 cf->crlf_mode = 1;
2678
Chris Luke7afda3a2016-04-25 13:49:22 -04002679 /* Setup the pager */
2680 cf->no_pager = um->cli_no_pager;
2681
Chris Luke2d8bf302017-11-12 22:26:37 -05002682 /* Default terminal dimensions, should the terminal
2683 * fail to provide any.
2684 */
2685 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
2686 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
2687
Chris Luke0aca5eb2016-04-25 13:48:54 -04002688 /* Send the telnet options */
Chris Luke03add7f2017-09-20 23:31:24 -04002689 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002690 unix_vlib_cli_output_raw (cf, uf, charmode_option,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002691 ARRAY_LEN (charmode_option));
Chris Luke0aca5eb2016-04-25 13:48:54 -04002692
2693 /* In case the client doesn't negotiate terminal type, use
2694 * a timer to kick off the initial prompt. */
2695 timer_call (unix_cli_file_welcome_timer, cf_index, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002696 }
2697
2698 return error;
2699}
2700
Chris Lukeb5850972016-05-03 16:34:59 -04002701/** The system terminal has informed us that the window size
2702 * has changed.
2703 */
Chris Luke7afda3a2016-04-25 13:49:22 -04002704static void
2705unix_cli_resize_interrupt (int signum)
2706{
Damjan Marion56dd5432017-09-08 19:52:02 +02002707 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002708 unix_cli_main_t *cm = &unix_cli_main;
2709 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2710 cm->stdin_cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002711 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04002712 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002713 (void) signum;
Chris Luke7afda3a2016-04-25 13:49:22 -04002714
2715 /* Terminal resized, fetch the new size */
Chris Luke03add7f2017-09-20 23:31:24 -04002716 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
Dave Barachb2a6e252016-07-27 10:00:58 -04002717 {
2718 /* "Should never happen..." */
2719 clib_unix_warning ("TIOCGWINSZ");
2720 /* We can't trust ws.XXX... */
2721 return;
2722 }
Chris Lukeb2bcad62017-09-18 08:51:22 -04002723
Chris Luke7afda3a2016-04-25 13:49:22 -04002724 cf->width = ws.ws_col;
Chris Lukeb2bcad62017-09-18 08:51:22 -04002725 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
2726 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
Chris Luke2d8bf302017-11-12 22:26:37 -05002727 if (cf->width == 0)
2728 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
Chris Lukeb2bcad62017-09-18 08:51:22 -04002729
Chris Luke7afda3a2016-04-25 13:49:22 -04002730 cf->height = ws.ws_row;
Chris Lukeb2bcad62017-09-18 08:51:22 -04002731 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
2732 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
Chris Luke2d8bf302017-11-12 22:26:37 -05002733 if (cf->height == 0)
2734 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
Chris Luke862623d2016-05-14 10:13:34 -04002735
2736 /* Reindex the pager buffer */
2737 unix_cli_pager_reindex (cf);
2738
2739 /* Redraw the page */
2740 unix_cli_pager_redraw (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04002741}
2742
Chris Lukeb5850972016-05-03 16:34:59 -04002743/** Handle configuration directives in the @em unix section. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002744static clib_error_t *
2745unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
2746{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002747 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002748 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002749 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002750 int flags;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002751 clib_error_t *error = 0;
2752 unix_cli_file_t *cf;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002753 u32 cf_index;
2754 struct termios tio;
Chris Luke7afda3a2016-04-25 13:49:22 -04002755 struct sigaction sa;
2756 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002757 u8 *term;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002758
2759 /* We depend on unix flags being set. */
2760 if ((error = vlib_call_config_function (vm, unix_config)))
2761 return error;
2762
2763 if (um->flags & UNIX_FLAG_INTERACTIVE)
2764 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002765 /* Set stdin to be non-blocking. */
Chris Luke03add7f2017-09-20 23:31:24 -04002766 if ((flags = fcntl (STDIN_FILENO, F_GETFL, 0)) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002767 flags = 0;
Chris Luke03add7f2017-09-20 23:31:24 -04002768 (void) fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002769
Chris Luke03add7f2017-09-20 23:31:24 -04002770 cf_index = unix_cli_file_add (cm, "stdin", STDIN_FILENO);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002771 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04002772 cm->stdin_cli_file_index = cf_index;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002773
2774 /* If stdin is a tty and we are using chacracter mode, enable
2775 * history on the CLI and set the tty line discipline accordingly. */
Chris Luke03add7f2017-09-20 23:31:24 -04002776 if (isatty (STDIN_FILENO) && um->cli_line_mode == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002777 {
2778 /* Capture terminal resize events */
Ed Warnicke853e7202016-08-12 11:42:26 -07002779 memset (&sa, 0, sizeof (sa));
Dave Barach9b8ffd92016-07-08 08:13:45 -04002780 sa.sa_handler = unix_cli_resize_interrupt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002781 if (sigaction (SIGWINCH, &sa, 0) < 0)
2782 clib_panic ("sigaction");
Chris Luke7afda3a2016-04-25 13:49:22 -04002783
Dave Barach9b8ffd92016-07-08 08:13:45 -04002784 /* Retrieve the current terminal size */
Chris Luke03add7f2017-09-20 23:31:24 -04002785 ioctl (STDIN_FILENO, TIOCGWINSZ, &ws);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002786 cf->width = ws.ws_col;
2787 cf->height = ws.ws_row;
Chris Luke7afda3a2016-04-25 13:49:22 -04002788
Dave Barach9b8ffd92016-07-08 08:13:45 -04002789 if (cf->width == 0 || cf->height == 0)
Dave Barachb66201f2017-09-22 13:34:39 -04002790 {
2791 /*
2792 * We have a tty, but no size. Use defaults.
2793 * vpp "unix interactive" inside emacs + gdb ends up here.
2794 */
Chris Luke2d8bf302017-11-12 22:26:37 -05002795 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
2796 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
Dave Barachb66201f2017-09-22 13:34:39 -04002797 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002798
Dave Barach9b8ffd92016-07-08 08:13:45 -04002799 /* Setup the history */
2800 cf->history_limit = um->cli_history_limit;
2801 cf->has_history = cf->history_limit != 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04002802
Dave Barach9b8ffd92016-07-08 08:13:45 -04002803 /* Setup the pager */
2804 cf->no_pager = um->cli_no_pager;
Chris Luke7afda3a2016-04-25 13:49:22 -04002805
Chris Luke03add7f2017-09-20 23:31:24 -04002806 /* This is an interactive session until we decide otherwise */
2807 cf->is_interactive = 1;
2808
Dave Barach9b8ffd92016-07-08 08:13:45 -04002809 /* We're going to be in char by char mode */
2810 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002811
Dave Barach9b8ffd92016-07-08 08:13:45 -04002812 /* Save the original tty state so we can restore it later */
Chris Luke03add7f2017-09-20 23:31:24 -04002813 tcgetattr (STDIN_FILENO, &um->tio_stdin);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002814 um->tio_isset = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002815
Dave Barach9b8ffd92016-07-08 08:13:45 -04002816 /* Tweak the tty settings */
2817 tio = um->tio_stdin;
2818 /* echo off, canonical mode off, ext'd input processing off */
2819 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
2820 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
2821 tio.c_cc[VTIME] = 0; /* no timer */
Chris Luke03add7f2017-09-20 23:31:24 -04002822 tcsetattr (STDIN_FILENO, TCSAFLUSH, &tio);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002823
Dave Barach9b8ffd92016-07-08 08:13:45 -04002824 /* See if we can do ANSI/VT100 output */
2825 term = (u8 *) getenv ("TERM");
2826 if (term != NULL)
Chris Luke03add7f2017-09-20 23:31:24 -04002827 {
2828 int len = strlen ((char *) term);
2829 cf->ansi_capable = unix_cli_terminal_type_ansi (term, len);
2830 if (unix_cli_terminal_type_noninteractive (term, len))
2831 unix_cli_set_session_noninteractive (cf);
2832 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04002833 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002834 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002835 {
Chris Luke03add7f2017-09-20 23:31:24 -04002836 /* No tty, so make sure the session doesn't have tty-like features */
2837 unix_cli_set_session_noninteractive (cf);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002838 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002839
2840 /* Send banner and initial prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002841 unix_cli_file_welcome (cm, cf);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002842 }
2843
Ed Warnickea25bd1c2016-04-01 22:43:37 -05002844 /* If we have socket config, LISTEN, otherwise, don't */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002845 clib_socket_t *s = &um->cli_listen_socket;
2846 if (s->config && s->config[0] != 0)
2847 {
2848 /* CLI listen. */
Damjan Marion56dd5432017-09-08 19:52:02 +02002849 clib_file_t template = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002850
Damjan Marion57d963f2017-07-20 19:17:06 +02002851 /* mkdir of file socketu, only under /run */
2852 if (strncmp (s->config, "/run", 4) == 0)
Chris Luke475674e2017-07-05 18:02:53 -04002853 {
Damjan Marion57d963f2017-07-20 19:17:06 +02002854 u8 *tmp = format (0, "%s", s->config);
2855 int i = vec_len (tmp);
2856 while (i && tmp[--i] != '/')
2857 ;
2858
2859 tmp[i] = 0;
2860
2861 if (i)
2862 vlib_unix_recursive_mkdir ((char *) tmp);
2863 vec_free (tmp);
Chris Luke475674e2017-07-05 18:02:53 -04002864 }
2865
Damjan Marionf49921f2017-09-11 16:52:11 +02002866 s->flags = CLIB_SOCKET_F_IS_SERVER | /* listen, don't connect */
2867 CLIB_SOCKET_F_ALLOW_GROUP_WRITE; /* PF_LOCAL socket only */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002868 error = clib_socket_init (s);
Ed Warnickea25bd1c2016-04-01 22:43:37 -05002869
Dave Barach9b8ffd92016-07-08 08:13:45 -04002870 if (error)
2871 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002872
Dave Barach9b8ffd92016-07-08 08:13:45 -04002873 template.read_function = unix_cli_listen_read_ready;
2874 template.file_descriptor = s->fd;
Damjan Marionceab7882018-01-19 20:56:12 +01002875 template.description = format (0, "cli listener %s", s->config);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002876
Damjan Marion56dd5432017-09-08 19:52:02 +02002877 clib_file_add (fm, &template);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002878 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002879
2880 /* Set CLI prompt. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002881 if (!cm->cli_prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002882 cm->cli_prompt = format (0, "VLIB: ");
2883
2884 return 0;
2885}
2886
Chris Luke90f52bf2016-09-12 08:55:13 -04002887/*?
2888 * This module has no configurable parameters.
2889?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002890VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
2891
Chris Luke54ccf222016-07-25 16:38:11 -04002892/** Called when VPP is shutting down, this restores the system
2893 * terminal state if previously saved.
Chris Lukeb5850972016-05-03 16:34:59 -04002894 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002895static clib_error_t *
2896unix_cli_exit (vlib_main_t * vm)
2897{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002898 unix_main_t *um = &unix_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002899
2900 /* If stdin is a tty and we saved the tty state, reset the tty state */
Chris Luke03add7f2017-09-20 23:31:24 -04002901 if (isatty (STDIN_FILENO) && um->tio_isset)
2902 tcsetattr (STDIN_FILENO, TCSAFLUSH, &um->tio_stdin);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002903
2904 return 0;
2905}
2906
2907VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
2908
Chris Lukeb5850972016-05-03 16:34:59 -04002909/** Set the CLI prompt.
Chris Luke54ccf222016-07-25 16:38:11 -04002910 * @param prompt The C string to set the prompt to.
Chris Lukeb5850972016-05-03 16:34:59 -04002911 * @note This setting is global; it impacts all current
2912 * and future CLI sessions.
2913 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002914void
2915vlib_unix_cli_set_prompt (char *prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002916{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002917 char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s ";
2918 unix_cli_main_t *cm = &unix_cli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002919 if (cm->cli_prompt)
2920 vec_free (cm->cli_prompt);
2921 cm->cli_prompt = format (0, fmt, prompt);
2922}
2923
Chris Lukeb5850972016-05-03 16:34:59 -04002924/** CLI command to quit the terminal session.
2925 * @note If this is a stdin session then this will
2926 * shutdown VPP also.
2927 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002928static clib_error_t *
2929unix_cli_quit (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002930 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002931{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002932 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke03add7f2017-09-20 23:31:24 -04002933 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2934 cm->current_input_file_index);
2935
2936 /* Cosmetic: suppress the final prompt from appearing before we die */
2937 cf->is_interactive = 0;
2938 cf->started = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002939
2940 vlib_process_signal_event (vm,
2941 vlib_current_process (vm),
2942 UNIX_CLI_PROCESS_EVENT_QUIT,
2943 cm->current_input_file_index);
2944 return 0;
2945}
2946
Chris Luke54ccf222016-07-25 16:38:11 -04002947/*?
2948 * Terminates the current CLI session.
2949 *
2950 * If VPP is running in @em interactive mode and this is the console session
2951 * (that is, the session on @c stdin) then this will also terminate VPP.
2952?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002953/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002954VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
2955 .path = "quit",
2956 .short_help = "Exit CLI",
2957 .function = unix_cli_quit,
2958};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002959/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002960
Chris Lukeb5850972016-05-03 16:34:59 -04002961/** CLI command to execute a VPP command script. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002962static clib_error_t *
2963unix_cli_exec (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002964 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002965{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002966 char *file_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002967 int fd;
2968 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002969 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002970
2971 file_name = 0;
2972 fd = -1;
2973 error = 0;
2974
Dave Barach9b8ffd92016-07-08 08:13:45 -04002975 if (!unformat (input, "%s", &file_name))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002976 {
2977 error = clib_error_return (0, "expecting file name, got `%U'",
2978 format_unformat_error, input);
2979 goto done;
2980 }
2981
2982 fd = open (file_name, O_RDONLY);
2983 if (fd < 0)
2984 {
2985 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
2986 goto done;
2987 }
2988
2989 /* Make sure its a regular file. */
2990 {
2991 struct stat s;
2992
2993 if (fstat (fd, &s) < 0)
2994 {
2995 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
2996 goto done;
2997 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04002998
2999 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003000 {
3001 error = clib_error_return (0, "not a regular file `%s'", file_name);
3002 goto done;
3003 }
3004 }
3005
Dave Barach59b25652017-09-10 15:04:27 -04003006 unformat_init_clib_file (&sub_input, fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003007
3008 vlib_cli_input (vm, &sub_input, 0, 0);
3009 unformat_free (&sub_input);
3010
Dave Barach9b8ffd92016-07-08 08:13:45 -04003011done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003012 if (fd > 0)
3013 close (fd);
3014 vec_free (file_name);
3015
3016 return error;
3017}
3018
Chris Luke54ccf222016-07-25 16:38:11 -04003019/*?
Billy McFall28cf3b72018-01-15 17:54:52 -05003020 * Executes a sequence of CLI commands which are read from a file. If
3021 * a command is unrecognised or otherwise invalid then the usual CLI
Chris Luke54ccf222016-07-25 16:38:11 -04003022 * feedback will be generated, however execution of subsequent commands
3023 * from the file will continue.
Billy McFall28cf3b72018-01-15 17:54:52 -05003024 *
3025 * The VPP code is indifferent to the file location. However, if SELinux
3026 * is enabled, then the file needs to have an SELinux label the VPP
3027 * process is allowed to access. For example, if a file is created in
3028 * '<em>/usr/share/vpp/</em>', it will be allowed. However, files manually
3029 * created in '/tmp/' or '/home/<user>/' will not be accessible by the VPP
3030 * process when SELinux is enabled.
3031 *
3032 * @cliexpar
3033 * Sample file:
3034 * @clistart
3035 * <b><em>$ cat /usr/share/vpp/scripts/gigup.txt</em></b>
3036 * set interface state GigabitEthernet0/8/0 up
3037 * set interface state GigabitEthernet0/9/0 up
3038 * @cliend
3039 * Example of how to execute a set of CLI commands from a file:
3040 * @cliexcmd{exec /usr/share/vpp/scripts/gigup.txt}
Chris Luke54ccf222016-07-25 16:38:11 -04003041?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003042/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003043VLIB_CLI_COMMAND (cli_exec, static) = {
3044 .path = "exec",
Billy McFall28cf3b72018-01-15 17:54:52 -05003045 .short_help = "exec <filename>",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003046 .function = unix_cli_exec,
Dave Barachb2ef4dd2016-03-23 08:56:01 -04003047 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -07003048};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003049/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003050
Chris Lukeb5850972016-05-03 16:34:59 -04003051/** CLI command to show various unix error statistics. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003052static clib_error_t *
3053unix_show_errors (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003054 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003055{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003056 unix_main_t *um = &unix_main;
3057 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003058 int i, n_errors_to_show;
Dave Barach9b8ffd92016-07-08 08:13:45 -04003059 unix_error_history_t *unix_errors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003060
3061 n_errors_to_show = 1 << 30;
3062
3063 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3064 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003065 if (!unformat (input, "%d", &n_errors_to_show))
Ed Warnickecb9cada2015-12-08 15:45:58 -07003066 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003067 error =
3068 clib_error_return (0,
3069 "expecting integer number of errors to show, got `%U'",
3070 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003071 goto done;
3072 }
3073 }
3074
Dave Barach9b8ffd92016-07-08 08:13:45 -04003075 n_errors_to_show =
3076 clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003077
Dave Barach9b8ffd92016-07-08 08:13:45 -04003078 i =
3079 um->error_history_index >
3080 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003081
3082 while (n_errors_to_show > 0)
3083 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003084 unix_error_history_t *eh = um->error_history + i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07003085
Dave Barach9b8ffd92016-07-08 08:13:45 -04003086 if (!eh->error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07003087 break;
3088
3089 vec_add1 (unix_errors, eh[0]);
3090 n_errors_to_show -= 1;
3091 if (i == 0)
3092 i = ARRAY_LEN (um->error_history) - 1;
3093 else
3094 i--;
3095 }
3096
3097 if (vec_len (unix_errors) == 0)
3098 vlib_cli_output (vm, "no Unix errors so far");
3099 else
3100 {
3101 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
3102 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
3103 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003104 unix_error_history_t *eh = vec_elt_at_index (unix_errors, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -07003105 vlib_cli_output (vm, "%U: %U",
3106 format_time_interval, "h:m:s:u", eh->time,
3107 format_clib_error, eh->error);
3108 }
3109 vlib_cli_output (vm, "%U: time now",
3110 format_time_interval, "h:m:s:u", vlib_time_now (vm));
3111 }
3112
Dave Barach9b8ffd92016-07-08 08:13:45 -04003113done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07003114 vec_free (unix_errors);
3115 return error;
3116}
3117
Dave Barach9b8ffd92016-07-08 08:13:45 -04003118/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003119VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
Damjan Marionceab7882018-01-19 20:56:12 +01003120 .path = "show unix errors",
Ed Warnickecb9cada2015-12-08 15:45:58 -07003121 .short_help = "Show Unix system call error history",
3122 .function = unix_show_errors,
3123};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003124/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003125
Damjan Marionceab7882018-01-19 20:56:12 +01003126/** CLI command to show various unix error statistics. */
3127static clib_error_t *
3128unix_show_files (vlib_main_t * vm,
3129 unformat_input_t * input, vlib_cli_command_t * cmd)
3130{
3131 clib_error_t *error = 0;
3132 clib_file_main_t *fm = &file_main;
3133 clib_file_t *f;
3134 char path[PATH_MAX];
3135 u8 *s = 0;
3136
3137 vlib_cli_output (vm, "%3s %6s %12s %12s %12s %-32s %s", "FD", "Thread",
3138 "Read", "Write", "Error", "File Name", "Description");
3139
3140 /* *INDENT-OFF* */
3141 pool_foreach (f, fm->file_pool,(
3142 {
3143 int rv;
3144 s = format (s, "/proc/self/fd/%d%c", f->file_descriptor, 0);
3145 rv = readlink((char *) s, path, PATH_MAX - 1);
3146
3147 path[rv < 0 ? 0 : rv] = 0;
3148
3149 vlib_cli_output (vm, "%3d %6d %12d %12d %12d %-32s %v",
3150 f->file_descriptor, f->polling_thread_index,
3151 f->read_events, f->write_events, f->error_events,
3152 path, f->description);
3153 vec_reset_length (s);
3154 }));
3155 /* *INDENT-ON* */
3156 vec_free (s);
3157
3158 return error;
3159}
3160
3161/* *INDENT-OFF* */
3162VLIB_CLI_COMMAND (cli_unix_show_files, static) = {
3163 .path = "show unix files",
3164 .short_help = "Show Unix files in use",
3165 .function = unix_show_files,
3166};
3167/* *INDENT-ON* */
3168
Chris Lukeb5850972016-05-03 16:34:59 -04003169/** CLI command to show session command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07003170static clib_error_t *
Chris Luke6b90fe32016-04-25 13:49:15 -04003171unix_cli_show_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003172 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke6b90fe32016-04-25 13:49:15 -04003173{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003174 unix_cli_main_t *cm = &unix_cli_main;
3175 unix_cli_file_t *cf;
Chris Luke6b90fe32016-04-25 13:49:15 -04003176 int i, j;
3177
3178 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3179
Chris Luke03add7f2017-09-20 23:31:24 -04003180 if (!cf->is_interactive)
3181 return clib_error_return (0, "invalid for non-interactive sessions");
3182
Chris Luke7afda3a2016-04-25 13:49:22 -04003183 if (cf->has_history && cf->history_limit)
Chris Luke6b90fe32016-04-25 13:49:15 -04003184 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003185 i = 1 + cf->command_number - vec_len (cf->command_history);
Chris Luke7afda3a2016-04-25 13:49:22 -04003186 for (j = 0; j < vec_len (cf->command_history); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04003187 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
Chris Luke7afda3a2016-04-25 13:49:22 -04003188 }
3189 else
3190 {
3191 vlib_cli_output (vm, "History not enabled.\n");
Chris Luke6b90fe32016-04-25 13:49:15 -04003192 }
3193
3194 return 0;
3195}
3196
Chris Luke54ccf222016-07-25 16:38:11 -04003197/*?
3198 * Displays the command history for the current session, if any.
3199?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003200/* *INDENT-OFF* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003201VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
3202 .path = "history",
3203 .short_help = "Show current session command history",
3204 .function = unix_cli_show_history,
3205};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003206/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003207
Chris Lukeb5850972016-05-03 16:34:59 -04003208/** CLI command to show terminal status. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003209static clib_error_t *
3210unix_cli_show_terminal (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003211 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003212{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003213 unix_main_t *um = &unix_main;
3214 unix_cli_main_t *cm = &unix_cli_main;
3215 unix_cli_file_t *cf;
3216 vlib_node_t *n;
Chris Luke7afda3a2016-04-25 13:49:22 -04003217
3218 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3219 n = vlib_get_node (vm, cf->process_node_index);
3220
3221 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003222 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
3223 "line-by-line" : "char-by-char");
Chris Luke7afda3a2016-04-25 13:49:22 -04003224 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
3225 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003226 vlib_cli_output (vm, "ANSI capable: %s\n",
3227 cf->ansi_capable ? "yes" : "no");
Chris Luke03add7f2017-09-20 23:31:24 -04003228 vlib_cli_output (vm, "Interactive: %s\n",
3229 cf->is_interactive ? "yes" : "no");
Chris Luke7afda3a2016-04-25 13:49:22 -04003230 vlib_cli_output (vm, "History enabled: %s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003231 cf->has_history ? "yes" : "no", !cf->has_history
3232 || cf->history_limit ? "" :
3233 " (disabled by history limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04003234 if (cf->has_history)
3235 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
3236 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003237 cf->no_pager ? "no" : "yes",
3238 cf->no_pager
3239 || cf->height ? "" : " (disabled by terminal height)",
3240 cf->no_pager
3241 || um->cli_pager_buffer_limit ? "" :
3242 " (disabled by buffer limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04003243 if (!cf->no_pager)
3244 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003245 vlib_cli_output (vm, "CRLF mode: %s\n",
3246 cf->crlf_mode ? "CR+LF" : "LF");
Chris Luke7afda3a2016-04-25 13:49:22 -04003247
3248 return 0;
3249}
3250
Chris Luke54ccf222016-07-25 16:38:11 -04003251/*?
3252 * Displays various information about the state of the current terminal
3253 * session.
3254 *
3255 * @cliexpar
3256 * @cliexstart{show terminal}
3257 * Terminal name: unix-cli-stdin
3258 * Terminal mode: char-by-char
3259 * Terminal width: 123
3260 * Terminal height: 48
3261 * ANSI capable: yes
Chris Luke03add7f2017-09-20 23:31:24 -04003262 * Interactive: yes
Chris Luke54ccf222016-07-25 16:38:11 -04003263 * History enabled: yes
3264 * History limit: 50
3265 * Pager enabled: yes
3266 * Pager limit: 100000
3267 * CRLF mode: LF
3268 * @cliexend
3269?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003270/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003271VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
3272 .path = "show terminal",
3273 .short_help = "Show current session terminal settings",
3274 .function = unix_cli_show_terminal,
3275};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003276/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003277
Chris Luke03add7f2017-09-20 23:31:24 -04003278/** CLI command to display a list of CLI sessions. */
3279static clib_error_t *
3280unix_cli_show_cli_sessions (vlib_main_t * vm,
3281 unformat_input_t * input,
3282 vlib_cli_command_t * cmd)
3283{
3284 //unix_main_t *um = &unix_main;
3285 unix_cli_main_t *cm = &unix_cli_main;
3286 clib_file_main_t *fm = &file_main;
3287 unix_cli_file_t *cf;
3288 clib_file_t *uf;
3289 vlib_node_t *n;
3290
3291 vlib_cli_output (vm, "%-5s %-5s %-20s %s", "PNI", "FD", "Name", "Flags");
3292
3293#define fl(x, y) ( (x) ? toupper((y)) : tolower((y)) )
3294 /* *INDENT-OFF* */
3295 pool_foreach (cf, cm->cli_file_pool, ({
3296 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
3297 n = vlib_get_node (vm, cf->process_node_index);
3298 vlib_cli_output (vm,
3299 "%-5d %-5d %-20v %c%c%c%c%c\n",
3300 cf->process_node_index,
3301 uf->file_descriptor,
3302 n->name,
3303 fl (cf->is_interactive, 'i'),
3304 fl (cf->is_socket, 's'),
3305 fl (cf->line_mode, 'l'),
3306 fl (cf->has_epipe, 'p'),
3307 fl (cf->ansi_capable, 'a'));
3308 }));
3309 /* *INDENT-ON* */
3310#undef fl
3311
3312 return 0;
3313}
3314
3315/*?
3316 * Displays a summary of all the current CLI sessions.
3317 *
3318 * Typically used to diagnose connection issues with the CLI
3319 * socket.
3320 *
3321 * @cliexpar
3322 * @cliexstart{show cli-sessions}
3323 * PNI FD Name Flags
3324 * 343 0 unix-cli-stdin IslpA
3325 * 344 7 unix-cli-local:20 ISlpA
3326 * 346 8 unix-cli-local:21 iSLpa
3327 * @cliexend
3328
3329 * In this example we have the debug console of the running process
3330 * on stdin/out, we have an interactive socket session and we also
3331 * have a non-interactive socket session.
3332 *
3333 * Fields:
3334 *
3335 * - @em PNI: Process node index.
3336 * - @em FD: Unix file descriptor.
3337 * - @em Name: Name of the session.
3338 * - @em Flags: Various flags that describe the state of the session.
3339 *
3340 * @em Flags have the following meanings; lower-case typically negates
3341 * upper-case:
3342 *
3343 * - @em I Interactive session.
3344 * - @em S Connected by socket.
3345 * - @em s Not a socket, likely stdin.
3346 * - @em L Line-by-line mode.
3347 * - @em l Char-by-char mode.
3348 * - @em P EPIPE detected on connection; it will close soon.
3349 * - @em A ANSI-capable terminal.
3350?*/
3351/* *INDENT-OFF* */
3352VLIB_CLI_COMMAND (cli_unix_cli_show_cli_sessions, static) = {
3353 .path = "show cli-sessions",
3354 .short_help = "Show current CLI sessions",
3355 .function = unix_cli_show_cli_sessions,
3356};
3357/* *INDENT-ON* */
3358
Chris Lukeb5850972016-05-03 16:34:59 -04003359/** CLI command to set terminal pager settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003360static clib_error_t *
3361unix_cli_set_terminal_pager (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003362 unformat_input_t * input,
3363 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003364{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003365 unix_main_t *um = &unix_main;
3366 unix_cli_main_t *cm = &unix_cli_main;
3367 unix_cli_file_t *cf;
3368 unformat_input_t _line_input, *line_input = &_line_input;
Billy McFalla9a20e72017-02-15 11:39:12 -05003369 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003370
Chris Luke03add7f2017-09-20 23:31:24 -04003371 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3372
3373 if (!cf->is_interactive)
3374 return clib_error_return (0, "invalid for non-interactive sessions");
3375
Dave Barach9b8ffd92016-07-08 08:13:45 -04003376 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04003377 return 0;
3378
Chris Luke7afda3a2016-04-25 13:49:22 -04003379 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3380 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003381 if (unformat (line_input, "on"))
3382 cf->no_pager = 0;
3383 else if (unformat (line_input, "off"))
3384 cf->no_pager = 1;
3385 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
3386 vlib_cli_output (vm,
3387 "Pager limit set to %u lines; note, this is global.\n",
3388 um->cli_pager_buffer_limit);
3389 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003390 {
3391 error = clib_error_return (0, "unknown parameter: `%U`",
3392 format_unformat_error, line_input);
3393 goto done;
3394 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04003395 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003396
Billy McFalla9a20e72017-02-15 11:39:12 -05003397done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003398 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003399
Billy McFalla9a20e72017-02-15 11:39:12 -05003400 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003401}
3402
Chris Luke54ccf222016-07-25 16:38:11 -04003403/*?
3404 * Enables or disables the terminal pager for this session. Generally
3405 * this defaults to enabled.
3406 *
3407 * Additionally allows the pager buffer size to be set; though note that
3408 * this value is set globally and not per session.
3409?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003410/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003411VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
3412 .path = "set terminal pager",
3413 .short_help = "set terminal pager [on|off] [limit <lines>]",
3414 .function = unix_cli_set_terminal_pager,
3415};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003416/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003417
Chris Lukeb5850972016-05-03 16:34:59 -04003418/** CLI command to set terminal history settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003419static clib_error_t *
3420unix_cli_set_terminal_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003421 unformat_input_t * input,
3422 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003423{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003424 unix_cli_main_t *cm = &unix_cli_main;
3425 unix_cli_file_t *cf;
3426 unformat_input_t _line_input, *line_input = &_line_input;
Chris Luke7afda3a2016-04-25 13:49:22 -04003427 u32 limit;
Billy McFalla9a20e72017-02-15 11:39:12 -05003428 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003429
Chris Luke03add7f2017-09-20 23:31:24 -04003430 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3431
3432 if (!cf->is_interactive)
3433 return clib_error_return (0, "invalid for non-interactive sessions");
3434
Dave Barach9b8ffd92016-07-08 08:13:45 -04003435 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04003436 return 0;
3437
Chris Luke7afda3a2016-04-25 13:49:22 -04003438 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3439 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003440 if (unformat (line_input, "on"))
3441 cf->has_history = 1;
3442 else if (unformat (line_input, "off"))
3443 cf->has_history = 0;
3444 else if (unformat (line_input, "limit %u", &cf->history_limit))
3445 ;
3446 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003447 {
3448 error = clib_error_return (0, "unknown parameter: `%U`",
3449 format_unformat_error, line_input);
3450 goto done;
3451 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003452
Dave Barach9b8ffd92016-07-08 08:13:45 -04003453 /* If we reduced history size, or turned it off, purge the history */
3454 limit = cf->has_history ? cf->history_limit : 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003455
Dave Barach9b8ffd92016-07-08 08:13:45 -04003456 while (cf->command_history && vec_len (cf->command_history) >= limit)
3457 {
3458 vec_free (cf->command_history[0]);
3459 vec_delete (cf->command_history, 1, 0);
3460 }
3461 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003462
Billy McFalla9a20e72017-02-15 11:39:12 -05003463done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003464 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003465
Billy McFalla9a20e72017-02-15 11:39:12 -05003466 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003467}
3468
Chris Luke54ccf222016-07-25 16:38:11 -04003469/*?
3470 * Enables or disables the command history function of the current
3471 * terminal. Generally this defaults to enabled.
3472 *
3473 * This command also allows the maximum size of the history buffer for
3474 * this session to be altered.
3475?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003476/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003477VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
3478 .path = "set terminal history",
3479 .short_help = "set terminal history [on|off] [limit <lines>]",
3480 .function = unix_cli_set_terminal_history,
3481};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003482/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003483
Chris Lukeb5850972016-05-03 16:34:59 -04003484/** CLI command to set terminal ANSI settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003485static clib_error_t *
3486unix_cli_set_terminal_ansi (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003487 unformat_input_t * input,
3488 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003489{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003490 unix_cli_main_t *cm = &unix_cli_main;
3491 unix_cli_file_t *cf;
Chris Luke7afda3a2016-04-25 13:49:22 -04003492
3493 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3494
Chris Luke03add7f2017-09-20 23:31:24 -04003495 if (!cf->is_interactive)
3496 return clib_error_return (0, "invalid for non-interactive sessions");
3497
Chris Luke7afda3a2016-04-25 13:49:22 -04003498 if (unformat (input, "on"))
3499 cf->ansi_capable = 1;
3500 else if (unformat (input, "off"))
3501 cf->ansi_capable = 0;
3502 else
3503 return clib_error_return (0, "unknown parameter: `%U`",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003504 format_unformat_error, input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003505
3506 return 0;
3507}
3508
Chris Luke54ccf222016-07-25 16:38:11 -04003509/*?
3510 * Enables or disables the use of ANSI control sequences by this terminal.
3511 * The default will vary based on terminal detection at the start of the
3512 * session.
3513 *
3514 * ANSI control sequences are used in a small number of places to provide,
3515 * for example, color text output and to control the cursor in the pager.
3516?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003517/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003518VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
3519 .path = "set terminal ansi",
3520 .short_help = "set terminal ansi [on|off]",
3521 .function = unix_cli_set_terminal_ansi,
3522};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003523/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003524
3525static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07003526unix_cli_init (vlib_main_t * vm)
3527{
3528 return 0;
3529}
3530
3531VLIB_INIT_FUNCTION (unix_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003532
3533/*
3534 * fd.io coding-style-patch-verification: ON
3535 *
3536 * Local Variables:
3537 * eval: (c-set-style "gnu")
3538 * End:
3539 */