blob: 0e035b1342b0f219ba73a8d9b5960a4119511fd2 [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070062
Chris Lukeb5850972016-05-03 16:34:59 -040063/** ANSI escape code. */
Chris Luke0aca5eb2016-04-25 13:48:54 -040064#define ESC "\x1b"
65
Chris Lukeb5850972016-05-03 16:34:59 -040066/** ANSI Control Sequence Introducer. */
Chris Luke0aca5eb2016-04-25 13:48:54 -040067#define CSI ESC "["
68
Chris Lukeb5850972016-05-03 16:34:59 -040069/** ANSI clear screen. */
Chris Luke7afda3a2016-04-25 13:49:22 -040070#define ANSI_CLEAR CSI "2J" CSI "1;1H"
Chris Lukeb5850972016-05-03 16:34:59 -040071/** ANSI reset color settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -040072#define ANSI_RESET CSI "0m"
Chris Lukeb5850972016-05-03 16:34:59 -040073/** ANSI Start bold text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040074#define ANSI_BOLD CSI "1m"
Chris Lukeb5850972016-05-03 16:34:59 -040075/** ANSI Stop bold text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040076#define ANSI_DIM CSI "2m"
Chris Lukeb5850972016-05-03 16:34:59 -040077/** ANSI Start dark red text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040078#define ANSI_DRED ANSI_DIM CSI "31m"
Chris Lukeb5850972016-05-03 16:34:59 -040079/** ANSI Start bright red text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040080#define ANSI_BRED ANSI_BOLD CSI "31m"
Chris Lukeb5850972016-05-03 16:34:59 -040081/** ANSI clear line cursor is on. */
Chris Luke7afda3a2016-04-25 13:49:22 -040082#define ANSI_CLEARLINE CSI "2K"
Chris Lukeb5850972016-05-03 16:34:59 -040083/** ANSI scroll screen down one line. */
Chris Luke7afda3a2016-04-25 13:49:22 -040084#define ANSI_SCROLLDN CSI "1T"
Chris Lukeb5850972016-05-03 16:34:59 -040085/** ANSI save cursor position. */
Chris Luke7afda3a2016-04-25 13:49:22 -040086#define ANSI_SAVECURSOR CSI "s"
Chris Lukeb5850972016-05-03 16:34:59 -040087/** ANSI restore cursor position if previously saved. */
Chris Luke7afda3a2016-04-25 13:49:22 -040088#define ANSI_RESTCURSOR CSI "u"
Chris Luke0aca5eb2016-04-25 13:48:54 -040089
90/** Maximum depth into a byte stream from which to compile a Telnet
91 * protocol message. This is a saftey measure. */
Chris Luke1aed76f2016-04-29 08:14:38 -040092#define UNIX_CLI_MAX_DEPTH_TELNET 24
Chris Luke0aca5eb2016-04-25 13:48:54 -040093
Chris Lukeb2bcad62017-09-18 08:51:22 -040094/** Minimum terminal width we will accept */
95#define UNIX_CLI_MIN_TERMINAL_WIDTH 1
96/** Maximum terminal width we will accept */
97#define UNIX_CLI_MAX_TERMINAL_WIDTH 512
98/** Minimum terminal height we will accept */
99#define UNIX_CLI_MIN_TERMINAL_HEIGHT 1
100/** Maximum terminal height we will accept */
101#define UNIX_CLI_MAX_TERMINAL_HEIGHT 512
102
Chris Luke0aca5eb2016-04-25 13:48:54 -0400103/** Unix standard in */
104#define UNIX_CLI_STDIN_FD 0
105
106
Chris Lukeb5850972016-05-03 16:34:59 -0400107/** A CLI banner line. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400108typedef struct
109{
110 u8 *line; /**< The line to print. */
111 u32 length; /**< The length of the line without terminating NUL. */
Chris Luke572d8122016-04-25 13:49:07 -0400112} unix_cli_banner_t;
113
114#define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
115/** Plain welcome banner. */
116static unix_cli_banner_t unix_cli_banner[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400117 _(" _______ _ _ _____ ___ \n"),
118 _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
119 _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
120 _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
121 _("\n")
Chris Luke572d8122016-04-25 13:49:07 -0400122};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400123
Chris Luke572d8122016-04-25 13:49:07 -0400124/** ANSI color welcome banner. */
125static unix_cli_banner_t unix_cli_banner_color[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400126 _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
127 _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
128 _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
129 _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"),
130 _("\n")
Chris Luke572d8122016-04-25 13:49:07 -0400131};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400132
Chris Luke572d8122016-04-25 13:49:07 -0400133#undef _
134
Chris Luke862623d2016-05-14 10:13:34 -0400135/** Pager line index */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400136typedef struct
137{
Chris Luke862623d2016-05-14 10:13:34 -0400138 /** Index into pager_vector */
139 u32 line;
140
141 /** Offset of the string in the line */
142 u32 offset;
143
144 /** Length of the string in the line */
145 u32 length;
146} unix_cli_pager_index_t;
147
Chris Luke572d8122016-04-25 13:49:07 -0400148
Chris Luke0aca5eb2016-04-25 13:48:54 -0400149/** Unix CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400150typedef struct
151{
Chris Lukeb5850972016-05-03 16:34:59 -0400152 /** The file index held by unix.c */
Damjan Marion56dd5432017-09-08 19:52:02 +0200153 u32 clib_file_index;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
Chris Lukeb5850972016-05-03 16:34:59 -0400155 /** Vector of output pending write to file descriptor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156 u8 *output_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157
Chris Lukeb5850972016-05-03 16:34:59 -0400158 /** Vector of input saved by Unix input node to be processed by
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 CLI process. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400160 u8 *input_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Chris Luke54ccf222016-07-25 16:38:11 -0400162 /** This session has command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 u8 has_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400164 /** Array of vectors of commands in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400165 u8 **command_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400166 /** The command currently pointed at by the history cursor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400167 u8 *current_command;
Chris Luke54ccf222016-07-25 16:38:11 -0400168 /** How far from the end of the history array the user has browsed. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 i32 excursion;
Chris Luke6b90fe32016-04-25 13:49:15 -0400170
Chris Lukeb5850972016-05-03 16:34:59 -0400171 /** Maximum number of history entries this session will store. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 u32 history_limit;
Chris Luke6b90fe32016-04-25 13:49:15 -0400173
Chris Lukeb5850972016-05-03 16:34:59 -0400174 /** Current command line counter */
Chris Luke6b90fe32016-04-25 13:49:15 -0400175 u32 command_number;
176
Chris Luke54ccf222016-07-25 16:38:11 -0400177 /** The string being searched for in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400178 u8 *search_key;
Chris Luke54ccf222016-07-25 16:38:11 -0400179 /** If non-zero then the CLI is searching in the history array.
180 * - @c -1 means search backwards.
181 * - @c 1 means search forwards.
182 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183 int search_mode;
184
Chris Lukeb5850972016-05-03 16:34:59 -0400185 /** Position of the insert cursor on the current input line */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400186 u32 cursor;
187
Chris Lukeb5850972016-05-03 16:34:59 -0400188 /** Line mode or char mode */
Chris Luke7afda3a2016-04-25 13:49:22 -0400189 u8 line_mode;
190
Chris Lukeb5850972016-05-03 16:34:59 -0400191 /** Set if the CRLF mode wants CR + LF */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400192 u8 crlf_mode;
193
Chris Lukeb5850972016-05-03 16:34:59 -0400194 /** Can we do ANSI output? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400195 u8 ansi_capable;
196
Chris Lukeb5850972016-05-03 16:34:59 -0400197 /** Has the session started? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400198 u8 started;
199
Chris Lukeb5850972016-05-03 16:34:59 -0400200 /** Disable the pager? */
Chris Luke7afda3a2016-04-25 13:49:22 -0400201 u8 no_pager;
202
Chris Lukeb5850972016-05-03 16:34:59 -0400203 /** Pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400204 u8 **pager_vector;
Chris Luke7afda3a2016-04-25 13:49:22 -0400205
Chris Luke862623d2016-05-14 10:13:34 -0400206 /** Index of line fragments in the pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400207 unix_cli_pager_index_t *pager_index;
Chris Luke7afda3a2016-04-25 13:49:22 -0400208
Chris Lukeb5850972016-05-03 16:34:59 -0400209 /** Line number of top of page */
Chris Luke7afda3a2016-04-25 13:49:22 -0400210 u32 pager_start;
211
Chris Lukeb5850972016-05-03 16:34:59 -0400212 /** Terminal width */
213 u32 width;
Chris Luke7afda3a2016-04-25 13:49:22 -0400214
Chris Lukeb5850972016-05-03 16:34:59 -0400215 /** Terminal height */
216 u32 height;
217
218 /** Process node identifier */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219 u32 process_node_index;
220} unix_cli_file_t;
221
Chris Lukeb5850972016-05-03 16:34:59 -0400222/** Resets the pager buffer and other data.
223 * @param f The CLI session whose pager needs to be reset.
224 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400226unix_cli_pager_reset (unix_cli_file_t * f)
Chris Luke7afda3a2016-04-25 13:49:22 -0400227{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400228 u8 **p;
Chris Luke7afda3a2016-04-25 13:49:22 -0400229
Chris Luke862623d2016-05-14 10:13:34 -0400230 f->pager_start = 0;
231
232 vec_free (f->pager_index);
233 f->pager_index = 0;
234
Chris Luke7afda3a2016-04-25 13:49:22 -0400235 vec_foreach (p, f->pager_vector)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400236 {
237 vec_free (*p);
238 }
Chris Luke862623d2016-05-14 10:13:34 -0400239 vec_free (f->pager_vector);
Chris Luke7afda3a2016-04-25 13:49:22 -0400240 f->pager_vector = 0;
241}
242
Chris Lukeb5850972016-05-03 16:34:59 -0400243/** Release storage used by a CLI session.
244 * @param f The CLI session whose storage needs to be released.
245 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400246always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247unix_cli_file_free (unix_cli_file_t * f)
248{
249 vec_free (f->output_vector);
250 vec_free (f->input_vector);
Chris Luke862623d2016-05-14 10:13:34 -0400251 unix_cli_pager_reset (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252}
253
Chris Lukeb5850972016-05-03 16:34:59 -0400254/** CLI actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400255typedef enum
256{
Chris Luke54ccf222016-07-25 16:38:11 -0400257 UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */
258 UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */
259 UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */
260 UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */
261 UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */
262 UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */
263 UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */
264 UNIX_CLI_PARSE_ACTION_LEFT, /**< Left arrow */
265 UNIX_CLI_PARSE_ACTION_RIGHT, /**< Right arrow */
266 UNIX_CLI_PARSE_ACTION_HOME, /**< Home key (jump to start of line) */
267 UNIX_CLI_PARSE_ACTION_END, /**< End key (jump to end of line) */
268 UNIX_CLI_PARSE_ACTION_WORDLEFT, /**< Jump cursor to start of left word */
269 UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */
270 UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */
271 UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */
272 UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */
273 UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */
274 UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */
275 UNIX_CLI_PARSE_ACTION_YANK, /**< Undo last erase action */
276 UNIX_CLI_PARSE_ACTION_TELNETIAC, /**< Telnet control code */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400277
Chris Luke54ccf222016-07-25 16:38:11 -0400278 UNIX_CLI_PARSE_ACTION_PAGER_CRLF, /**< Enter pressed (CR, CRLF, LF, etc) */
279 UNIX_CLI_PARSE_ACTION_PAGER_QUIT, /**< Exit the pager session */
280 UNIX_CLI_PARSE_ACTION_PAGER_NEXT, /**< Scroll to next page */
281 UNIX_CLI_PARSE_ACTION_PAGER_DN, /**< Scroll to next line */
282 UNIX_CLI_PARSE_ACTION_PAGER_UP, /**< Scroll to previous line */
283 UNIX_CLI_PARSE_ACTION_PAGER_TOP, /**< Scroll to first line */
284 UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM, /**< Scroll to last line */
285 UNIX_CLI_PARSE_ACTION_PAGER_PGDN, /**< Scroll to next page */
286 UNIX_CLI_PARSE_ACTION_PAGER_PGUP, /**< Scroll to previous page */
287 UNIX_CLI_PARSE_ACTION_PAGER_REDRAW, /**< Clear and redraw the page on the terminal */
288 UNIX_CLI_PARSE_ACTION_PAGER_SEARCH, /**< Search the pager buffer */
Chris Luke7afda3a2016-04-25 13:49:22 -0400289
Chris Luke54ccf222016-07-25 16:38:11 -0400290 UNIX_CLI_PARSE_ACTION_PARTIALMATCH, /**< Action parser found a partial match */
291 UNIX_CLI_PARSE_ACTION_NOMATCH /**< Action parser did not find any match */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400292} unix_cli_parse_action_t;
293
Chris Luke8e5b0412016-07-26 13:06:10 -0400294/** @brief Mapping of input buffer strings to action values.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400295 * @note This won't work as a hash since we need to be able to do
296 * partial matches on the string.
297 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400298typedef struct
299{
300 u8 *input; /**< Input string to match. */
301 u32 len; /**< Length of input without final NUL. */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400302 unix_cli_parse_action_t action; /**< Action to take when matched. */
303} unix_cli_parse_actions_t;
304
Chris Lukeb5850972016-05-03 16:34:59 -0400305/** @brief Given a capital ASCII letter character return a @c NUL terminated
Chris Luke0aca5eb2016-04-25 13:48:54 -0400306 * string with the control code for that letter.
Chris Lukeb5850972016-05-03 16:34:59 -0400307 *
308 * @param c An ASCII character.
309 * @return A @c NUL terminated string of type @c u8[].
310 *
311 * @par Example
312 * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[].
Chris Luke0aca5eb2016-04-25 13:48:54 -0400313 */
314#define CTL(c) (u8[]){ (c) - '@', 0 }
315
316#define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
Chris Lukeb5850972016-05-03 16:34:59 -0400317/**
318 * Patterns to match on a CLI input stream.
319 * @showinitializer
320 */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400321static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400322 /* Line handling */
323 _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */
324 _("\n", UNIX_CLI_PARSE_ACTION_CRLF),
325 _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */
326 _("\r", UNIX_CLI_PARSE_ACTION_CRLF),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400327
Dave Barach9b8ffd92016-07-08 08:13:45 -0400328 /* Unix shell control codes */
329 _(CTL ('B'), UNIX_CLI_PARSE_ACTION_LEFT),
330 _(CTL ('F'), UNIX_CLI_PARSE_ACTION_RIGHT),
331 _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP),
332 _(CTL ('N'), UNIX_CLI_PARSE_ACTION_DOWN),
333 _(CTL ('A'), UNIX_CLI_PARSE_ACTION_HOME),
334 _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END),
335 _(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT),
336 _(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT),
337 _(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT),
338 _(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK),
339 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR),
340 _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */
341 _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */
342 _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */
343 _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */
344 _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400345
Dave Barach9b8ffd92016-07-08 08:13:45 -0400346 /* VT100 Normal mode - Broadest support */
347 _(CSI "A", UNIX_CLI_PARSE_ACTION_UP),
348 _(CSI "B", UNIX_CLI_PARSE_ACTION_DOWN),
349 _(CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT),
350 _(CSI "D", UNIX_CLI_PARSE_ACTION_LEFT),
351 _(CSI "H", UNIX_CLI_PARSE_ACTION_HOME),
352 _(CSI "F", UNIX_CLI_PARSE_ACTION_END),
353 _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */
354 _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */
355 _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400356
357 /* VT100 Application mode - Some Gnome Terminal functions use these */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400358 _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP),
359 _(ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN),
360 _(ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT),
361 _(ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT),
362 _(ESC "OH", UNIX_CLI_PARSE_ACTION_HOME),
363 _(ESC "OF", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400364
Dave Barach9b8ffd92016-07-08 08:13:45 -0400365 /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
366 _(CSI "1~", UNIX_CLI_PARSE_ACTION_HOME),
367 _(CSI "4~", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400368
Dave Barach9b8ffd92016-07-08 08:13:45 -0400369 /* Emacs-ish history search */
370 _(CTL ('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH),
371 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400372
Dave Barach9b8ffd92016-07-08 08:13:45 -0400373 /* Other protocol things */
374 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
375 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
376 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400377};
Chris Lukeb5850972016-05-03 16:34:59 -0400378
379/**
380 * Patterns to match when a CLI session is in the pager.
381 * @showinitializer
382 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400383static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400384 /* Line handling */
385 _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */
386 _("\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
387 _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */
388 _("\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
Chris Luke7afda3a2016-04-25 13:49:22 -0400389
Dave Barach9b8ffd92016-07-08 08:13:45 -0400390 /* Pager commands */
391 _(" ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT),
392 _("q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT),
393 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
394 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
395 _("/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH),
Chris Luke7afda3a2016-04-25 13:49:22 -0400396
Dave Barach9b8ffd92016-07-08 08:13:45 -0400397 /* VT100 */
398 _(CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP),
399 _(CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN),
400 _(CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
401 _(CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400402
Dave Barach9b8ffd92016-07-08 08:13:45 -0400403 /* VT100 Application mode */
404 _(ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP),
405 _(ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN),
406 _(ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
407 _(ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400408
Dave Barach9b8ffd92016-07-08 08:13:45 -0400409 /* ANSI X3.41-1974 */
410 _(CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
411 _(CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
412 _(CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP),
413 _(CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN),
Chris Luke7afda3a2016-04-25 13:49:22 -0400414
Dave Barach9b8ffd92016-07-08 08:13:45 -0400415 /* Other protocol things */
416 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
417 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
418 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke7afda3a2016-04-25 13:49:22 -0400419};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400420
Chris Luke0aca5eb2016-04-25 13:48:54 -0400421#undef _
422
Chris Lukeb5850972016-05-03 16:34:59 -0400423/** CLI session events. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400424typedef enum
425{
Chris Lukeb5850972016-05-03 16:34:59 -0400426 UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400427 UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400428} unix_cli_process_event_type_t;
429
Chris Lukeb5850972016-05-03 16:34:59 -0400430/** CLI global state. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400431typedef struct
432{
Chris Lukeb5850972016-05-03 16:34:59 -0400433 /** Prompt string for CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400434 u8 *cli_prompt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435
Chris Lukeb5850972016-05-03 16:34:59 -0400436 /** Vec pool of CLI sessions. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400437 unix_cli_file_t *cli_file_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
Chris Lukeb5850972016-05-03 16:34:59 -0400439 /** Vec pool of unused session indices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400440 u32 *unused_cli_process_node_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Chris Lukeb5850972016-05-03 16:34:59 -0400442 /** The session index of the stdin cli */
Chris Luke7afda3a2016-04-25 13:49:22 -0400443 u32 stdin_cli_file_index;
444
Chris Lukeb5850972016-05-03 16:34:59 -0400445 /** File pool index of current input. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 u32 current_input_file_index;
447} unix_cli_main_t;
448
Chris Lukeb5850972016-05-03 16:34:59 -0400449/** CLI global state */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450static unix_cli_main_t unix_cli_main;
451
Chris Luke0aca5eb2016-04-25 13:48:54 -0400452/**
Chris Luke8e5b0412016-07-26 13:06:10 -0400453 * @brief Search for a byte sequence in the action list.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400454 *
Chris Lukeb5850972016-05-03 16:34:59 -0400455 * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with
456 * the bytes in @a input of maximum length @a ilen bytes.
457 * When a match is made @a *matched indicates how many bytes were matched.
458 * Returns a value from the enum @ref unix_cli_parse_action_t to indicate
459 * whether no match was found, a partial match was found or a complete
460 * match was found and what action, if any, should be taken.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400461 *
Chris Lukeb5850972016-05-03 16:34:59 -0400462 * @param[in] a Actions list to search within.
463 * @param[in] input String fragment to search for.
464 * @param[in] ilen Length of the string in 'input'.
465 * @param[out] matched Pointer to an integer that will contain the number
466 * of bytes matched when a complete match is found.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400467 *
Chris Lukeb5850972016-05-03 16:34:59 -0400468 * @return Action from @ref unix_cli_parse_action_t that the string fragment
Chris Luke0aca5eb2016-04-25 13:48:54 -0400469 * matches.
Chris Lukeb5850972016-05-03 16:34:59 -0400470 * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the
471 * whole input string matches the start of at least one action.
472 * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
Chris Luke0aca5eb2016-04-25 13:48:54 -0400473 * match at all.
474 */
475static unix_cli_parse_action_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400476unix_cli_match_action (unix_cli_parse_actions_t * a,
477 u8 * input, u32 ilen, i32 * matched)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400478{
Chris Luke0aca5eb2016-04-25 13:48:54 -0400479 u8 partial = 0;
480
481 while (a->input)
482 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400483 if (ilen >= a->len)
484 {
485 /* see if the start of the input buffer exactly matches the current
486 * action string. */
487 if (memcmp (input, a->input, a->len) == 0)
488 {
489 *matched = a->len;
490 return a->action;
491 }
492 }
493 else
494 {
495 /* if the first ilen characters match, flag this as a partial -
496 * meaning keep collecting bytes in case of a future match */
497 if (memcmp (input, a->input, ilen) == 0)
498 partial = 1;
499 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400500
Dave Barach9b8ffd92016-07-08 08:13:45 -0400501 /* check next action */
502 a++;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400503 }
504
505 return partial ?
Dave Barach9b8ffd92016-07-08 08:13:45 -0400506 UNIX_CLI_PARSE_ACTION_PARTIALMATCH : UNIX_CLI_PARSE_ACTION_NOMATCH;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400507}
508
509
Chris Luke54ccf222016-07-25 16:38:11 -0400510/** Add bytes to the output vector and then flagg the I/O system that bytes
511 * are available to be sent.
512 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200514unix_cli_add_pending_output (clib_file_t * uf,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 unix_cli_file_t * cf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400516 u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517{
Damjan Marion56dd5432017-09-08 19:52:02 +0200518 clib_file_main_t *fm = &file_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519
520 vec_add (cf->output_vector, buffer, buffer_bytes);
521 if (vec_len (cf->output_vector) > 0)
522 {
523 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
524 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400525 if (!skip_update)
Damjan Marion56dd5432017-09-08 19:52:02 +0200526 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 }
528}
529
Chris Luke54ccf222016-07-25 16:38:11 -0400530/** Delete all bytes from the output vector and flag the I/O system
531 * that no more bytes are available to be sent.
532 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200534unix_cli_del_pending_output (clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400535 unix_cli_file_t * cf, uword n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536{
Damjan Marion56dd5432017-09-08 19:52:02 +0200537 clib_file_main_t *fm = &file_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538
539 vec_delete (cf->output_vector, n_bytes, 0);
540 if (vec_len (cf->output_vector) <= 0)
541 {
542 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
543 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400544 if (!skip_update)
Damjan Marion56dd5432017-09-08 19:52:02 +0200545 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546 }
547}
548
Chris Luke8e5b0412016-07-26 13:06:10 -0400549/** @brief A bit like strchr with a buffer length limit.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400550 * Search a buffer for the first instance of a character up to the limit of
551 * the buffer length. If found then return the position of that character.
552 *
553 * The key departure from strchr is that if the character is not found then
554 * return the buffer length.
555 *
556 * @param chr The byte value to search for.
557 * @param str The buffer in which to search for the value.
558 * @param len The depth into the buffer to search.
559 *
560 * @return The index of the first occurence of \c chr. If \c chr is not
561 * found then \c len instead.
562 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400563always_inline word
564unix_vlib_findchr (u8 chr, u8 * str, word len)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400565{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400566 word i = 0;
567 for (i = 0; i < len; i++, str++)
568 {
569 if (*str == chr)
570 return i;
571 }
572 return len;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400573}
574
Chris Luke8e5b0412016-07-26 13:06:10 -0400575/** @brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400576 * Attempts to write given buffer to the file descriptor of the given
577 * Unix CLI session. If that session already has data in the output buffer
578 * or if the write attempt tells us to try again later then the given buffer
579 * is appended to the pending output buffer instead.
580 *
581 * This is typically called only from \c unix_vlib_cli_output_cooked since
582 * that is where CRLF handling occurs or from places where we explicitly do
583 * not want cooked handling.
584 *
585 * @param cf Unix CLI session of the desired stream to write to.
586 * @param uf The Unix file structure of the desired stream to write to.
587 * @param buffer Pointer to the buffer that needs to be written.
588 * @param buffer_bytes The number of bytes from \c buffer to write.
589 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400590static void
591unix_vlib_cli_output_raw (unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +0200592 clib_file_t * uf, u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400593{
594 int n = 0;
595
596 if (vec_len (cf->output_vector) == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400597 n = write (uf->file_descriptor, buffer, buffer_bytes);
Chris Luke0aca5eb2016-04-25 13:48:54 -0400598
599 if (n < 0 && errno != EAGAIN)
600 {
601 clib_unix_warning ("write");
602 }
603 else if ((word) n < (word) buffer_bytes)
604 {
605 /* We got EAGAIN or we already have stuff in the buffer;
606 * queue up whatever didn't get sent for later. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400607 if (n < 0)
608 n = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400609 unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
610 }
611}
612
Chris Luke8e5b0412016-07-26 13:06:10 -0400613/** @brief Process a buffer for CRLF handling before outputting it to the CLI.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400614 *
615 * @param cf Unix CLI session of the desired stream to write to.
616 * @param uf The Unix file structure of the desired stream to write to.
617 * @param buffer Pointer to the buffer that needs to be written.
618 * @param buffer_bytes The number of bytes from \c buffer to write.
619 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400620static void
621unix_vlib_cli_output_cooked (unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +0200622 clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400623 u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400624{
625 word end = 0, start = 0;
626
627 while (end < buffer_bytes)
628 {
629 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400630 {
631 /* iterate the line on \n's so we can insert a \r before it */
632 end = unix_vlib_findchr ('\n',
633 buffer + start,
634 buffer_bytes - start) + start;
635 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400636 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400637 {
638 /* otherwise just send the whole buffer */
639 end = buffer_bytes;
640 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400641
Dave Barach9b8ffd92016-07-08 08:13:45 -0400642 unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start);
Chris Luke0aca5eb2016-04-25 13:48:54 -0400643
644 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400645 {
646 if (end < buffer_bytes)
647 {
648 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2);
649 end++; /* skip the \n that we already sent */
650 }
651 start = end;
652 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400653 }
654}
655
Chris Luke8e5b0412016-07-26 13:06:10 -0400656/** @brief Output the CLI prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200658unix_cli_cli_prompt (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400659{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400660 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke7afda3a2016-04-25 13:49:22 -0400661
662 unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt, vec_len (cm->cli_prompt));
663}
664
Chris Luke8e5b0412016-07-26 13:06:10 -0400665/** @brief Output a pager prompt and show number of buffered lines */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400666static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200667unix_cli_pager_prompt (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400668{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400669 u8 *prompt;
Chris Luke270b6de2016-05-19 14:23:25 -0400670 u32 h;
671
672 h = cf->pager_start + (cf->height - 1);
673 if (h > vec_len (cf->pager_index))
674 h = vec_len (cf->pager_index);
Chris Luke7afda3a2016-04-25 13:49:22 -0400675
Dave Barach9b8ffd92016-07-08 08:13:45 -0400676 prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s",
677 cf->ansi_capable ? ANSI_BOLD : "",
678 cf->pager_start + 1,
679 h,
680 vec_len (cf->pager_index),
681 cf->ansi_capable ? ANSI_RESET : "");
Chris Luke7afda3a2016-04-25 13:49:22 -0400682
Dave Barach9b8ffd92016-07-08 08:13:45 -0400683 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400684
Dave Barach9b8ffd92016-07-08 08:13:45 -0400685 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400686}
687
Chris Luke8e5b0412016-07-26 13:06:10 -0400688/** @brief Output a pager "skipping" message */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400689static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200690unix_cli_pager_message (unix_cli_file_t * cf, clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400691 char *message, char *postfix)
Chris Luke7afda3a2016-04-25 13:49:22 -0400692{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400693 u8 *prompt;
Chris Luke7afda3a2016-04-25 13:49:22 -0400694
Dave Barach9b8ffd92016-07-08 08:13:45 -0400695 prompt = format (0, "\r%s-- %s --%s%s",
696 cf->ansi_capable ? ANSI_BOLD : "",
697 message, cf->ansi_capable ? ANSI_RESET : "", postfix);
Chris Luke7afda3a2016-04-25 13:49:22 -0400698
Dave Barach9b8ffd92016-07-08 08:13:45 -0400699 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400700
Dave Barach9b8ffd92016-07-08 08:13:45 -0400701 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400702}
703
Chris Luke8e5b0412016-07-26 13:06:10 -0400704/** @brief Erase the printed pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400705static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200706unix_cli_pager_prompt_erase (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400707{
708 if (cf->ansi_capable)
709 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400710 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
711 unix_vlib_cli_output_cooked (cf, uf,
712 (u8 *) ANSI_CLEARLINE,
713 sizeof (ANSI_CLEARLINE) - 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400714 }
715 else
716 {
717 int i;
718
Dave Barach9b8ffd92016-07-08 08:13:45 -0400719 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
720 for (i = 0; i < cf->width - 1; i++)
721 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
722 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400723 }
724}
725
Chris Luke8e5b0412016-07-26 13:06:10 -0400726/** @brief Uses an ANSI escape sequence to move the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400727static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200728unix_cli_ansi_cursor (unix_cli_file_t * cf, clib_file_t * uf, u16 x, u16 y)
Chris Luke7afda3a2016-04-25 13:49:22 -0400729{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400730 u8 *str;
Chris Luke7afda3a2016-04-25 13:49:22 -0400731
Dave Barach9b8ffd92016-07-08 08:13:45 -0400732 str = format (0, "%s%d;%dH", CSI, y, x);
Chris Luke7afda3a2016-04-25 13:49:22 -0400733
Dave Barach9b8ffd92016-07-08 08:13:45 -0400734 unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str));
Chris Luke7afda3a2016-04-25 13:49:22 -0400735
Dave Barach9b8ffd92016-07-08 08:13:45 -0400736 vec_free (str);
Chris Luke7afda3a2016-04-25 13:49:22 -0400737}
738
Chris Luke862623d2016-05-14 10:13:34 -0400739/** Redraw the currently displayed page of text.
740 * @param cf CLI session to redraw the pager buffer of.
741 * @param uf Unix file of the CLI session.
742 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400743static void
Damjan Marion56dd5432017-09-08 19:52:02 +0200744unix_cli_pager_redraw (unix_cli_file_t * cf, clib_file_t * uf)
Chris Luke862623d2016-05-14 10:13:34 -0400745{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400746 unix_cli_pager_index_t *pi = NULL;
747 u8 *line = NULL;
Chris Luke862623d2016-05-14 10:13:34 -0400748 word i;
749
750 /* No active pager? Do nothing. */
751 if (!vec_len (cf->pager_index))
752 return;
753
754 if (cf->ansi_capable)
755 {
756 /* If we have ANSI, send the clear screen sequence */
757 unix_vlib_cli_output_cooked (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400758 (u8 *) ANSI_CLEAR,
759 sizeof (ANSI_CLEAR) - 1);
Chris Luke862623d2016-05-14 10:13:34 -0400760 }
761 else
762 {
763 /* Otherwise make sure we're on a blank line */
764 unix_cli_pager_prompt_erase (cf, uf);
765 }
766
767 /* (Re-)send the current page of content */
768 for (i = 0; i < cf->height - 1 &&
Dave Barach9b8ffd92016-07-08 08:13:45 -0400769 i + cf->pager_start < vec_len (cf->pager_index); i++)
Chris Luke862623d2016-05-14 10:13:34 -0400770 {
771 pi = &cf->pager_index[cf->pager_start + i];
772 line = cf->pager_vector[pi->line] + pi->offset;
773
774 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
775 }
776 /* if the last line didn't end in newline, add a newline */
777 if (pi && line[pi->length - 1] != '\n')
Dave Barach9b8ffd92016-07-08 08:13:45 -0400778 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke862623d2016-05-14 10:13:34 -0400779
780 unix_cli_pager_prompt (cf, uf);
781}
782
783/** @brief Process and add a line to the pager index.
784 * In normal operation this function will take the given character string
785 * found in @c line and with length @c len_or_index and iterates the over the
786 * contents, adding each line of text discovered within it to the
787 * pager index. Lines are identified by newlines ("<code>\\n</code>") and by
788 * strings longer than the width of the terminal.
789 *
790 * If instead @c line is @c NULL then @c len_or_index is taken to mean the
791 * index of an existing line in the pager buffer; this simply means that the
792 * input line does not need to be cloned since we alreayd have it. This is
793 * typical if we are reindexing the pager buffer.
794 *
795 * @param cf The CLI session whose pager we are adding to.
796 * @param line The string of text to be indexed into the pager buffer.
797 * If @c line is @c NULL then the mode of operation
798 * changes slightly; see the description above.
799 * @param len_or_index If @c line is a pointer to a string then this parameter
800 * indicates the length of that string; Otherwise this
801 * value provides the index in the pager buffer of an
802 * existing string to be indexed.
803 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400804static void
805unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index)
Chris Luke862623d2016-05-14 10:13:34 -0400806{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400807 u8 *p;
Chris Luke862623d2016-05-14 10:13:34 -0400808 word i, j, k;
809 word line_index, len;
810 u32 width = cf->width;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400811 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -0400812
813 if (line == NULL)
814 {
815 /* Use a line already in the pager buffer */
816 line_index = len_or_index;
817 p = cf->pager_vector[line_index];
818 len = vec_len (p);
819 }
820 else
821 {
822 len = len_or_index;
823 /* Add a copy of the raw string to the pager buffer */
824 p = vec_new (u8, len);
825 clib_memcpy (p, line, len);
826
827 /* store in pager buffer */
828 line_index = vec_len (cf->pager_vector);
829 vec_add1 (cf->pager_vector, p);
830 }
831
832 i = 0;
833 while (i < len)
834 {
835 /* Find the next line, or run to terminal width, or run to EOL */
836 int l = len - i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400837 j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width);
Chris Luke862623d2016-05-14 10:13:34 -0400838
Dave Barach9b8ffd92016-07-08 08:13:45 -0400839 if (j < l && p[j] == '\n') /* incl \n */
840 j++;
Chris Luke862623d2016-05-14 10:13:34 -0400841
842 /* Add the line to the index */
843 k = vec_len (cf->pager_index);
844 vec_validate (cf->pager_index, k);
845 pi = &cf->pager_index[k];
846
847 pi->line = line_index;
848 pi->offset = i;
849 pi->length = j;
850
851 i += j;
852 p += j;
853 }
854}
855
856/** @brief Reindex entire pager buffer.
857 * Resets the current pager index and then re-adds the lines in the pager
858 * buffer to the index.
859 *
860 * Additionally this function attempts to retain the current page start
861 * line offset by searching for the same top-of-screen line in the new index.
862 *
863 * @param cf The CLI session whose pager buffer should be reindexed.
864 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400865static void
866unix_cli_pager_reindex (unix_cli_file_t * cf)
Chris Luke862623d2016-05-14 10:13:34 -0400867{
868 word i, old_line, old_offset;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400869 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -0400870
871 /* If there is nothing in the pager buffer then make sure the index
872 * is empty and move on.
873 */
874 if (cf->pager_vector == 0)
875 {
876 vec_reset_length (cf->pager_index);
877 return;
878 }
879
880 /* Retain a pointer to the current page start line so we can
881 * find it later
882 */
883 pi = &cf->pager_index[cf->pager_start];
884 old_line = pi->line;
885 old_offset = pi->offset;
886
887 /* Re-add the buffered lines to the index */
888 vec_reset_length (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400889 vec_foreach_index (i, cf->pager_vector)
890 {
891 unix_cli_pager_add_line (cf, NULL, i);
892 }
Chris Luke862623d2016-05-14 10:13:34 -0400893
894 /* Attempt to re-locate the previously stored page start line */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400895 vec_foreach_index (i, cf->pager_index)
896 {
897 pi = &cf->pager_index[i];
Chris Luke862623d2016-05-14 10:13:34 -0400898
Dave Barach9b8ffd92016-07-08 08:13:45 -0400899 if (pi->line == old_line &&
900 (pi->offset <= old_offset || pi->offset + pi->length > old_offset))
901 {
902 /* Found it! */
903 cf->pager_start = i;
904 break;
905 }
906 }
Chris Luke862623d2016-05-14 10:13:34 -0400907
908 /* In case the start line was not found (rare), ensure the pager start
909 * index is within bounds
910 */
911 if (cf->pager_start >= vec_len (cf->pager_index))
912 {
Chris Luke270b6de2016-05-19 14:23:25 -0400913 if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400914 cf->pager_start = 0;
Chris Luke270b6de2016-05-19 14:23:25 -0400915 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400916 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
Chris Luke862623d2016-05-14 10:13:34 -0400917 }
918}
919
920/** VLIB CLI output function.
921 *
922 * If the terminal has a pager configured then this function takes care
923 * of collating output into the pager buffer; ensuring only the first page
924 * is displayed and any lines in excess of the first page are buffered.
925 *
926 * If the maximum number of index lines in the buffer is exceeded then the
927 * pager is cancelled and the contents of the current buffer are sent to the
928 * terminal.
929 *
930 * If there is no pager configured then the output is sent directly to the
931 * terminal.
932 *
933 * @param cli_file_index Index of the CLI session where this output is
934 * directed.
935 * @param buffer String of printabe bytes to be output.
936 * @param buffer_bytes The number of bytes in @c buffer to be output.
937 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400938static void
939unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400941 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +0200942 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400943 unix_cli_main_t *cm = &unix_cli_main;
944 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +0200945 clib_file_t *uf;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400946
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +0200948 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700949
Chris Luke7afda3a2016-04-25 13:49:22 -0400950 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
951 {
Chris Luke862623d2016-05-14 10:13:34 -0400952 unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes);
Chris Luke7afda3a2016-04-25 13:49:22 -0400953 }
954 else
955 {
Chris Luke862623d2016-05-14 10:13:34 -0400956 word row = vec_len (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400957 u8 *line;
958 unix_cli_pager_index_t *pi;
Chris Luke7afda3a2016-04-25 13:49:22 -0400959
Chris Luke862623d2016-05-14 10:13:34 -0400960 /* Index and add the output lines to the pager buffer. */
961 unix_cli_pager_add_line (cf, buffer, buffer_bytes);
962
963 /* Now iterate what was added to display the lines.
964 * If we reach the bottom of the page, display a prompt.
965 */
966 while (row < vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400967 {
968 if (row < cf->height - 1)
969 {
970 /* output this line */
971 pi = &cf->pager_index[row];
972 line = cf->pager_vector[pi->line] + pi->offset;
973 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
Chris Luke862623d2016-05-14 10:13:34 -0400974
Dave Barach9b8ffd92016-07-08 08:13:45 -0400975 /* if the last line didn't end in newline, and we're at the
976 * bottom of the page, add a newline */
977 if (line[pi->length - 1] != '\n' && row == cf->height - 2)
978 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
979 }
980 else
981 {
982 /* Display the pager prompt every 10 lines */
983 if (!(row % 10))
984 unix_cli_pager_prompt (cf, uf);
985 }
986 row++;
987 }
Chris Luke7afda3a2016-04-25 13:49:22 -0400988
Chris Luke862623d2016-05-14 10:13:34 -0400989 /* Check if we went over the pager buffer limit */
990 if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400991 {
992 /* Stop using the pager for the remainder of this CLI command */
993 cf->no_pager = 2;
Chris Luke7afda3a2016-04-25 13:49:22 -0400994
Dave Barach9b8ffd92016-07-08 08:13:45 -0400995 /* If we likely printed the prompt, erase it */
996 if (vec_len (cf->pager_index) > cf->height - 1)
997 unix_cli_pager_prompt_erase (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -0400998
Dave Barach9b8ffd92016-07-08 08:13:45 -0400999 /* Dump out the contents of the buffer */
1000 for (row = cf->pager_start + (cf->height - 1);
1001 row < vec_len (cf->pager_index); row++)
1002 {
1003 pi = &cf->pager_index[row];
1004 line = cf->pager_vector[pi->line] + pi->offset;
1005 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1006 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001007
Dave Barach9b8ffd92016-07-08 08:13:45 -04001008 unix_cli_pager_reset (cf);
1009 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001010 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011}
1012
Chris Luke862623d2016-05-14 10:13:34 -04001013/** Identify whether a terminal type is ANSI capable.
1014 *
Chris Luke54ccf222016-07-25 16:38:11 -04001015 * Compares the string given in @c term with a list of terminal types known
Chris Luke862623d2016-05-14 10:13:34 -04001016 * to support ANSI escape sequences.
1017 *
1018 * This list contains, for example, @c xterm, @c screen and @c ansi.
1019 *
1020 * @param term A string with a terminal type in it.
Chris Luke54ccf222016-07-25 16:38:11 -04001021 * @param len The length of the string in @c term.
Chris Luke862623d2016-05-14 10:13:34 -04001022 *
1023 * @return @c 1 if the terminal type is recognized as supporting ANSI
1024 * terminal sequences; @c 0 otherwise.
1025 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001026static u8
1027unix_cli_terminal_type (u8 * term, uword len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028{
Chris Luke0aca5eb2016-04-25 13:48:54 -04001029 /* This may later be better done as a hash of some sort. */
1030#define _(a) do { \
1031 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1032 } while(0)
1033
1034 _("xterm");
1035 _("xterm-color");
Dave Barach9b8ffd92016-07-08 08:13:45 -04001036 _("xterm-256color"); /* iTerm on Mac */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001037 _("screen");
Damjan Marion6e8ab162017-06-05 21:56:12 +02001038 _("screen-256color"); /* Screen and tmux */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001039 _("ansi"); /* Microsoft Telnet */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001040#undef _
1041
1042 return 0;
1043}
1044
Chris Luke8e5b0412016-07-26 13:06:10 -04001045/** @brief Emit initial welcome banner and prompt on a connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001046static void
1047unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001048{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001049 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02001050 clib_file_main_t *fm = &file_main;
1051 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke572d8122016-04-25 13:49:07 -04001052 unix_cli_banner_t *banner;
1053 int i, len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001054
Chris Luke0aca5eb2016-04-25 13:48:54 -04001055 /*
1056 * Put the first bytes directly into the buffer so that further output is
1057 * queued until everything is ready. (oterwise initial prompt can appear
1058 * mid way through VPP initialization)
1059 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001060 unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
Chris Luke572d8122016-04-25 13:49:07 -04001061
1062 if (!um->cli_no_banner)
1063 {
1064 if (cf->ansi_capable)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001065 {
1066 banner = unix_cli_banner_color;
1067 len = ARRAY_LEN (unix_cli_banner_color);
1068 }
Chris Luke572d8122016-04-25 13:49:07 -04001069 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001070 {
1071 banner = unix_cli_banner;
1072 len = ARRAY_LEN (unix_cli_banner);
1073 }
Chris Luke572d8122016-04-25 13:49:07 -04001074
1075 for (i = 0; i < len; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001076 {
1077 unix_vlib_cli_output_cooked (cf, uf,
1078 banner[i].line, banner[i].length);
1079 }
1080 }
Chris Luke572d8122016-04-25 13:49:07 -04001081
1082 /* Prompt. */
Chris Luke7afda3a2016-04-25 13:49:22 -04001083 unix_cli_cli_prompt (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001084
1085 cf->started = 1;
1086}
1087
Chris Luke8e5b0412016-07-26 13:06:10 -04001088/** @brief A failsafe triggered on a timer to ensure we send the prompt
Chris Luke0aca5eb2016-04-25 13:48:54 -04001089 * to telnet sessions that fail to negotiate the terminal type. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001090static void
1091unix_cli_file_welcome_timer (any arg, f64 delay)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001092{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001093 unix_cli_main_t *cm = &unix_cli_main;
1094 unix_cli_file_t *cf;
1095 (void) delay;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001096
1097 /* Check the connection didn't close already */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001098 if (pool_is_free_index (cm->cli_file_pool, (uword) arg))
Chris Luke0aca5eb2016-04-25 13:48:54 -04001099 return;
1100
Dave Barach9b8ffd92016-07-08 08:13:45 -04001101 cf = pool_elt_at_index (cm->cli_file_pool, (uword) arg);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001102
1103 if (!cf->started)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001104 unix_cli_file_welcome (cm, cf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001105}
1106
Chris Luke8e5b0412016-07-26 13:06:10 -04001107/** @brief A mostly no-op Telnet state machine.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001108 * Process Telnet command bytes in a way that ensures we're mostly
1109 * transparent to the Telnet protocol. That is, it's mostly a no-op.
1110 *
1111 * @return -1 if we need more bytes, otherwise a positive integer number of
1112 * bytes to consume from the input_vector, not including the initial
1113 * IAC byte.
1114 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001115static i32
1116unix_cli_process_telnet (unix_main_t * um,
1117 unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +02001118 clib_file_t * uf, u8 * input_vector, uword len)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001119{
1120 /* Input_vector starts at IAC byte.
1121 * See if we have a complete message; if not, return -1 so we wait for more.
1122 * if we have a complete message, consume those bytes from the vector.
1123 */
1124 i32 consume = 0;
1125
1126 if (len == 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001127 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001128
1129 switch (input_vector[1])
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001131 case IAC:
1132 /* two IAC's in a row means to pass through 0xff.
1133 * since that makes no sense here, just consume it.
1134 */
1135 consume = 1;
1136 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001137
Dave Barach9b8ffd92016-07-08 08:13:45 -04001138 case WILL:
1139 case WONT:
1140 case DO:
1141 case DONT:
1142 /* Expect 3 bytes */
1143 if (vec_len (input_vector) < 3)
1144 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001145
Dave Barach9b8ffd92016-07-08 08:13:45 -04001146 consume = 2;
1147 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001148
Dave Barach9b8ffd92016-07-08 08:13:45 -04001149 case SB:
1150 {
1151 /* Sub option - search ahead for IAC SE to end it */
1152 i32 i;
1153 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
1154 {
1155 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
1156 {
1157 /* We have a complete message; see if we care about it */
1158 switch (input_vector[2])
1159 {
1160 case TELOPT_TTYPE:
1161 if (input_vector[3] != 0)
1162 break;
1163 /* See if the terminal type is ANSI capable */
1164 cf->ansi_capable =
1165 unix_cli_terminal_type (input_vector + 4, i - 5);
1166 /* If session not started, we can release the pause */
1167 if (!cf->started)
1168 /* Send the welcome banner and initial prompt */
1169 unix_cli_file_welcome (&unix_cli_main, cf);
1170 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001171
Dave Barach9b8ffd92016-07-08 08:13:45 -04001172 case TELOPT_NAWS:
1173 /* Window size */
1174 if (i != 8) /* check message is correct size */
1175 break;
Chris Lukeb2bcad62017-09-18 08:51:22 -04001176
Dave Barach9b8ffd92016-07-08 08:13:45 -04001177 cf->width =
1178 clib_net_to_host_u16 (*((u16 *) (input_vector + 3)));
Chris Lukeb2bcad62017-09-18 08:51:22 -04001179 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
1180 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
1181 if (cf->width < UNIX_CLI_MIN_TERMINAL_WIDTH)
1182 cf->width = UNIX_CLI_MIN_TERMINAL_WIDTH;
1183
Dave Barach9b8ffd92016-07-08 08:13:45 -04001184 cf->height =
1185 clib_net_to_host_u16 (*((u16 *) (input_vector + 5)));
Chris Lukeb2bcad62017-09-18 08:51:22 -04001186 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
1187 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
1188 if (cf->height < UNIX_CLI_MIN_TERMINAL_HEIGHT)
1189 cf->height = UNIX_CLI_MIN_TERMINAL_HEIGHT;
1190
Dave Barach9b8ffd92016-07-08 08:13:45 -04001191 /* reindex pager buffer */
1192 unix_cli_pager_reindex (cf);
1193 /* redraw page */
1194 unix_cli_pager_redraw (cf, uf);
1195 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04001196
Dave Barach9b8ffd92016-07-08 08:13:45 -04001197 default:
1198 break;
1199 }
1200 /* Consume it all */
1201 consume = i;
1202 break;
1203 }
1204 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001205
Dave Barach9b8ffd92016-07-08 08:13:45 -04001206 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
1207 consume = 1; /* hit max search depth, advance one byte */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001208
Dave Barach9b8ffd92016-07-08 08:13:45 -04001209 if (consume == 0)
1210 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001211
Dave Barach9b8ffd92016-07-08 08:13:45 -04001212 break;
1213 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214
Dave Barach9b8ffd92016-07-08 08:13:45 -04001215 case GA:
1216 case EL:
1217 case EC:
1218 case AO:
1219 case IP:
1220 case BREAK:
1221 case DM:
1222 case NOP:
1223 case SE:
1224 case EOR:
1225 case ABORT:
1226 case SUSP:
1227 case xEOF:
1228 /* Simple one-byte messages */
1229 consume = 1;
1230 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001231
Dave Barach9b8ffd92016-07-08 08:13:45 -04001232 case AYT:
1233 /* Are You There - trigger a visible response */
1234 consume = 1;
1235 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
1236 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001237
Dave Barach9b8ffd92016-07-08 08:13:45 -04001238 default:
1239 /* Unknown command! Eat the IAC byte */
1240 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001241 }
1242
Dave Barach9b8ffd92016-07-08 08:13:45 -04001243 return consume;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001244}
1245
Chris Luke8e5b0412016-07-26 13:06:10 -04001246/** @brief Process actionable input.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001247 * Based on the \c action process the input; this typically involves
1248 * searching the command history or editing the current command line.
1249 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001250static int
1251unix_cli_line_process_one (unix_cli_main_t * cm,
1252 unix_main_t * um,
1253 unix_cli_file_t * cf,
Damjan Marion56dd5432017-09-08 19:52:02 +02001254 clib_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001255 u8 input, unix_cli_parse_action_t action)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001256{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001257 u8 *prev;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001258 u8 *save = 0;
1259 u8 **possible_commands;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001260 int j, delta;
1261
1262 switch (action)
1263 {
1264 case UNIX_CLI_PARSE_ACTION_NOACTION:
1265 break;
1266
Chris Luke0aca5eb2016-04-25 13:48:54 -04001267 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
1268 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
Chris Luke7afda3a2016-04-25 13:49:22 -04001269 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001270 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001271 if (cf->search_mode == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001272 {
1273 /* Erase the current command (if any) */
1274 for (j = 0; j < (vec_len (cf->current_command)); j++)
1275 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001276
Dave Barach9b8ffd92016-07-08 08:13:45 -04001277 vec_reset_length (cf->search_key);
1278 vec_reset_length (cf->current_command);
1279 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1280 cf->search_mode = -1;
1281 else
1282 cf->search_mode = 1;
1283 cf->cursor = 0;
1284 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001285 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001286 {
1287 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1288 cf->search_mode = -1;
1289 else
1290 cf->search_mode = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001291
Dave Barach9b8ffd92016-07-08 08:13:45 -04001292 cf->excursion += cf->search_mode;
1293 goto search_again;
1294 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001295 break;
1296
1297 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
1298 /* Erase the command from the cursor to the start */
1299
1300 /* Shimmy forwards to the new end of line position */
1301 delta = vec_len (cf->current_command) - cf->cursor;
1302 for (j = cf->cursor; j > delta; j--)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001303 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001304 /* Zap from here to the end of what is currently displayed */
1305 for (; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001306 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001307 /* Get back to the start of the line */
1308 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001309 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001310
Dave Barach9b8ffd92016-07-08 08:13:45 -04001311 j = vec_len (cf->current_command) - cf->cursor;
1312 memmove (cf->current_command, cf->current_command + cf->cursor, j);
1313 _vec_len (cf->current_command) = j;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001314
1315 /* Print the new contents */
1316 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j);
1317 /* Shimmy back to the start */
1318 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001319 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001320 cf->cursor = 0;
1321
1322 cf->search_mode = 0;
1323 break;
1324
1325 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
1326 /* Erase the command from the cursor to the end */
1327
1328 /* Zap from cursor to end of what is currently displayed */
1329 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001330 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001331 /* Get back to where we were */
1332 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001333 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001334
1335 /* Truncate the line at the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001336 _vec_len (cf->current_command) = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001337
1338 cf->search_mode = 0;
1339 break;
1340
1341 case UNIX_CLI_PARSE_ACTION_LEFT:
1342 if (cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001343 {
1344 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1345 cf->cursor--;
1346 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001347
1348 cf->search_mode = 0;
1349 break;
1350
1351 case UNIX_CLI_PARSE_ACTION_RIGHT:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001352 if (cf->cursor < vec_len (cf->current_command))
1353 {
1354 /* have to emit the character under the cursor */
1355 unix_vlib_cli_output_cooked (cf, uf,
1356 cf->current_command + cf->cursor, 1);
1357 cf->cursor++;
1358 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001359
1360 cf->search_mode = 0;
1361 break;
1362
1363 case UNIX_CLI_PARSE_ACTION_UP:
1364 case UNIX_CLI_PARSE_ACTION_DOWN:
Chris Luke7afda3a2016-04-25 13:49:22 -04001365 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001366 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001367 cf->search_mode = 0;
1368 /* Erase the command */
1369 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001370 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001371 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001372 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001373 vec_reset_length (cf->current_command);
1374 if (vec_len (cf->command_history))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001375 {
1376 if (action == UNIX_CLI_PARSE_ACTION_UP)
1377 delta = -1;
1378 else
1379 delta = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001380
Dave Barach9b8ffd92016-07-08 08:13:45 -04001381 cf->excursion += delta;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001382
Dave Barach9b8ffd92016-07-08 08:13:45 -04001383 if (cf->excursion == vec_len (cf->command_history))
1384 {
1385 /* down-arrowed to last entry - want a blank line */
1386 _vec_len (cf->current_command) = 0;
1387 }
1388 else if (cf->excursion < 0)
1389 {
1390 /* up-arrowed over the start to the end, want a blank line */
1391 cf->excursion = vec_len (cf->command_history);
1392 _vec_len (cf->current_command) = 0;
1393 }
1394 else
1395 {
1396 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1397 /* down-arrowed past end - wrap to start */
1398 cf->excursion = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001399
Dave Barach9b8ffd92016-07-08 08:13:45 -04001400 /* Print the command at the current position */
1401 prev = cf->command_history[cf->excursion];
1402 vec_validate (cf->current_command, vec_len (prev) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001403
Dave Barach9b8ffd92016-07-08 08:13:45 -04001404 clib_memcpy (cf->current_command, prev, vec_len (prev));
1405 _vec_len (cf->current_command) = vec_len (prev);
1406 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1407 vec_len (cf->current_command));
1408 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04001409 }
Yoann Desmouceaux561ae0a2017-09-20 10:08:28 +02001410 cf->cursor = vec_len (cf->current_command);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001411 break;
1412
1413 case UNIX_CLI_PARSE_ACTION_HOME:
1414 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001415 {
1416 while (cf->cursor)
1417 {
1418 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1419 cf->cursor--;
1420 }
1421 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001422
Chris Luke0aca5eb2016-04-25 13:48:54 -04001423 cf->search_mode = 0;
1424 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001425
Chris Luke0aca5eb2016-04-25 13:48:54 -04001426 case UNIX_CLI_PARSE_ACTION_END:
1427 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001428 cf->cursor < vec_len (cf->current_command))
1429 {
1430 unix_vlib_cli_output_cooked (cf, uf,
1431 cf->current_command + cf->cursor,
1432 vec_len (cf->current_command) -
1433 cf->cursor);
1434 cf->cursor = vec_len (cf->current_command);
1435 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001436
1437 cf->search_mode = 0;
1438 break;
1439
1440 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1441 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001442 {
1443 j = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001444
Dave Barach9b8ffd92016-07-08 08:13:45 -04001445 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1446 j--;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001447
Dave Barach9b8ffd92016-07-08 08:13:45 -04001448 while (j && isspace (cf->current_command[j]))
1449 {
1450 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1451 j--;
1452 }
1453 while (j && !isspace (cf->current_command[j]))
1454 {
1455 if (isspace (cf->current_command[j - 1]))
1456 break;
1457 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1458 j--;
1459 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001460
Dave Barach9b8ffd92016-07-08 08:13:45 -04001461 cf->cursor = j;
1462 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001463
1464 cf->search_mode = 0;
1465 break;
1466
1467 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1468 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001469 cf->cursor < vec_len (cf->current_command))
1470 {
1471 int e = vec_len (cf->current_command);
1472 j = cf->cursor;
1473 while (j < e && !isspace (cf->current_command[j]))
1474 j++;
1475 while (j < e && isspace (cf->current_command[j]))
1476 j++;
1477 unix_vlib_cli_output_cooked (cf, uf,
1478 cf->current_command + cf->cursor,
1479 j - cf->cursor);
1480 cf->cursor = j;
1481 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001482
1483 cf->search_mode = 0;
1484 break;
1485
1486
1487 case UNIX_CLI_PARSE_ACTION_ERASE:
1488 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001489 {
1490 if (cf->cursor == vec_len (cf->current_command))
1491 {
1492 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1493 _vec_len (cf->current_command)--;
1494 cf->cursor--;
1495 }
1496 else if (cf->cursor > 0)
1497 {
1498 /* shift everything at & to the right of the cursor left by 1 */
1499 j = vec_len (cf->current_command) - cf->cursor;
1500 memmove (cf->current_command + cf->cursor - 1,
1501 cf->current_command + cf->cursor, j);
1502 _vec_len (cf->current_command)--;
1503 cf->cursor--;
1504 /* redraw the rest of the line */
1505 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1506 unix_vlib_cli_output_cooked (cf, uf,
1507 cf->current_command + cf->cursor,
1508 j);
1509 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
1510 /* and shift the terminal cursor back where it should be */
1511 while (--j)
1512 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1513 }
1514 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001515 cf->search_mode = 0;
1516 cf->excursion = 0;
1517 vec_reset_length (cf->search_key);
1518 break;
1519
1520 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1521 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001522 {
1523 if (cf->cursor < vec_len (cf->current_command))
1524 {
1525 /* shift everything to the right of the cursor left by 1 */
1526 j = vec_len (cf->current_command) - cf->cursor - 1;
1527 memmove (cf->current_command + cf->cursor,
1528 cf->current_command + cf->cursor + 1, j);
1529 _vec_len (cf->current_command)--;
1530 /* redraw the rest of the line */
1531 unix_vlib_cli_output_cooked (cf, uf,
1532 cf->current_command + cf->cursor,
1533 j);
1534 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
1535 /* and shift the terminal cursor back where it should be */
1536 if (j)
1537 {
1538 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1539 while (--j)
1540 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1541 }
1542 }
1543 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001544 else if (input == 'D' - '@')
Dave Barach9b8ffd92016-07-08 08:13:45 -04001545 {
1546 /* ^D with no command entered = quit */
1547 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1548 vlib_process_signal_event (um->vlib_main,
1549 vlib_current_process (um->vlib_main),
1550 UNIX_CLI_PROCESS_EVENT_QUIT,
1551 cf - cm->cli_file_pool);
1552 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001553 cf->search_mode = 0;
1554 cf->excursion = 0;
1555 vec_reset_length (cf->search_key);
1556 break;
1557
1558 case UNIX_CLI_PARSE_ACTION_CLEAR:
1559 /* If we're in ANSI mode, clear the screen.
1560 * Then redraw the prompt and any existing command input, then put
1561 * the cursor back where it was in that line.
1562 */
1563 if (cf->ansi_capable)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001564 unix_vlib_cli_output_cooked (cf, uf,
1565 (u8 *) ANSI_CLEAR,
1566 sizeof (ANSI_CLEAR) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001567 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001568 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001569
1570 unix_vlib_cli_output_raw (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001571 cm->cli_prompt, vec_len (cm->cli_prompt));
Chris Luke0aca5eb2016-04-25 13:48:54 -04001572 unix_vlib_cli_output_raw (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001573 cf->current_command,
1574 vec_len (cf->current_command));
1575 for (j = cf->cursor; j < vec_len (cf->current_command); j++)
1576 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001577
1578 break;
1579
Chris Luke7afda3a2016-04-25 13:49:22 -04001580 case UNIX_CLI_PARSE_ACTION_TAB:
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001581 if (cf->cursor < vec_len (cf->current_command))
1582 {
1583 /* if we are in the middle of a line, complete only if
1584 * the cursor points to whitespace */
1585 if (isspace (cf->current_command[cf->cursor]))
1586 {
1587 /* save and clear any input that is after the cursor */
1588 vec_resize (save, vec_len (cf->current_command) - cf->cursor);
1589 clib_memcpy (save, cf->current_command + cf->cursor,
1590 vec_len (cf->current_command) - cf->cursor);
1591 _vec_len (cf->current_command) = cf->cursor;
1592 }
1593 else
1594 {
1595 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1596 break;
1597 }
1598 }
1599 possible_commands =
1600 vlib_cli_get_possible_completions (cf->current_command);
1601 if (vec_len (possible_commands) == 1)
1602 {
1603 u32 j = cf->cursor;
1604 u8 *completed = possible_commands[0];
1605
1606 /* find the last word of current_command */
1607 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1608 {
1609 j--;
1610 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1611 }
1612 _vec_len (cf->current_command) = j;
1613
1614 /* replace it with the newly expanded command */
1615 vec_append (cf->current_command, completed);
1616
1617 /* echo to the terminal */
1618 unix_vlib_cli_output_raw (cf, uf, completed, vec_len (completed));
1619
1620 /* add one trailing space if needed */
1621 if (vec_len (save) == 0)
1622 {
1623 vec_add1 (cf->current_command, ' ');
1624 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1625 }
1626
1627 cf->cursor = vec_len (cf->current_command);
1628
1629 }
1630 else if (vec_len (possible_commands) >= 2)
1631 {
1632 u8 **possible_command;
1633 uword max_command_len = 0, min_command_len = ~0;
1634 u32 i, j;
1635
1636 vec_foreach (possible_command, possible_commands)
1637 {
1638 if (vec_len (*possible_command) > max_command_len)
1639 {
1640 max_command_len = vec_len (*possible_command);
1641 }
1642 if (vec_len (*possible_command) < min_command_len)
1643 {
1644 min_command_len = vec_len (*possible_command);
1645 }
1646 }
1647
1648 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1649
1650 i = 0;
1651 vec_foreach (possible_command, possible_commands)
1652 {
1653 if (i + max_command_len >= cf->width)
1654 {
1655 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1656 i = 0;
1657 }
1658 unix_vlib_cli_output_raw (cf, uf, *possible_command,
1659 vec_len (*possible_command));
1660 for (j = vec_len (*possible_command); j < max_command_len + 2;
1661 j++)
1662 {
1663 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1664 }
1665 i += max_command_len + 2;
1666 }
1667
1668 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1669
1670 /* rewrite prompt */
1671 unix_cli_cli_prompt (cf, uf);
1672 unix_vlib_cli_output_raw (cf, uf, cf->current_command,
1673 vec_len (cf->current_command));
1674
1675 /* count length of last word */
1676 j = cf->cursor;
1677 i = 0;
1678 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1679 {
1680 j--;
1681 i++;
1682 }
1683
1684 /* determine smallest common command */
1685 for (; i < min_command_len; i++)
1686 {
1687 u8 common = '\0';
1688 int stop = 0;
1689 vec_foreach (possible_command, possible_commands)
1690 {
1691 if (common == '\0')
1692 {
1693 common = (*possible_command)[i];
1694 }
1695 else if (common != (*possible_command)[i])
1696 {
1697 stop = 1;
1698 break;
1699 }
1700 }
1701 if (!stop)
1702 {
1703 vec_add1 (cf->current_command, common);
1704 cf->cursor++;
1705 unix_vlib_cli_output_raw (cf, uf, (u8 *) & common, 1);
1706 }
1707 else
1708 {
1709 break;
1710 }
1711 }
1712 }
1713 else
1714 {
1715 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1716 }
1717
1718 if (vec_len (save) > 0)
1719 {
1720 /* restore remaining input if tab was hit in the middle of a line */
1721 unix_vlib_cli_output_raw (cf, uf, save, vec_len (save));
1722 for (j = 0; j < vec_len (save); j++)
1723 {
1724 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1725 }
1726 vec_append (cf->current_command, save);
1727 vec_free (save);
1728 }
1729 vec_free (possible_commands);
1730
1731 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04001732 case UNIX_CLI_PARSE_ACTION_YANK:
1733 /* TODO */
1734 break;
1735
1736
1737 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
1738 pager_quit:
1739 unix_cli_pager_prompt_erase (cf, uf);
1740 unix_cli_pager_reset (cf);
1741 unix_cli_cli_prompt (cf, uf);
1742 break;
1743
1744 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
1745 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
1746 /* show next page of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001747 if (cf->height + cf->pager_start < vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001748 {
1749 u8 *line = NULL;
1750 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001751
Dave Barach9b8ffd92016-07-08 08:13:45 -04001752 int m = cf->pager_start + (cf->height - 1);
1753 unix_cli_pager_prompt_erase (cf, uf);
1754 for (j = m;
1755 j < vec_len (cf->pager_index) && cf->pager_start < m;
1756 j++, cf->pager_start++)
1757 {
1758 pi = &cf->pager_index[j];
1759 line = cf->pager_vector[pi->line] + pi->offset;
1760 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1761 }
1762 /* if the last line didn't end in newline, add a newline */
1763 if (pi && line[pi->length - 1] != '\n')
1764 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1765 unix_cli_pager_prompt (cf, uf);
1766 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001767 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001768 {
1769 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
1770 /* no more in buffer, exit, but only if it was <space> */
1771 goto pager_quit;
1772 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001773 break;
1774
1775 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
1776 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
1777 /* display the next line of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001778 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001779 {
1780 u8 *line;
1781 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -04001782
Dave Barach9b8ffd92016-07-08 08:13:45 -04001783 unix_cli_pager_prompt_erase (cf, uf);
1784 pi = &cf->pager_index[cf->pager_start + (cf->height - 1)];
1785 line = cf->pager_vector[pi->line] + pi->offset;
1786 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1787 cf->pager_start++;
1788 /* if the last line didn't end in newline, add a newline */
1789 if (line[pi->length - 1] != '\n')
1790 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1791 unix_cli_pager_prompt (cf, uf);
1792 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001793 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001794 {
1795 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
1796 /* no more in buffer, exit, but only if it was <enter> */
1797 goto pager_quit;
1798 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001799
1800 break;
1801
1802 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
1803 /* scroll the page back one line */
1804 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001805 {
1806 u8 *line = NULL;
1807 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001808
Dave Barach9b8ffd92016-07-08 08:13:45 -04001809 cf->pager_start--;
1810 if (cf->ansi_capable)
1811 {
1812 pi = &cf->pager_index[cf->pager_start];
1813 line = cf->pager_vector[pi->line] + pi->offset;
1814 unix_cli_pager_prompt_erase (cf, uf);
1815 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN,
1816 sizeof (ANSI_SCROLLDN) - 1);
1817 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR,
1818 sizeof (ANSI_SAVECURSOR) - 1);
1819 unix_cli_ansi_cursor (cf, uf, 1, 1);
1820 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE,
1821 sizeof (ANSI_CLEARLINE) - 1);
1822 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1823 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR,
1824 sizeof (ANSI_RESTCURSOR) - 1);
1825 unix_cli_pager_prompt_erase (cf, uf);
1826 unix_cli_pager_prompt (cf, uf);
1827 }
1828 else
1829 {
1830 int m = cf->pager_start + (cf->height - 1);
1831 unix_cli_pager_prompt_erase (cf, uf);
1832 for (j = cf->pager_start;
1833 j < vec_len (cf->pager_index) && j < m; j++)
1834 {
1835 pi = &cf->pager_index[j];
1836 line = cf->pager_vector[pi->line] + pi->offset;
1837 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1838 }
1839 /* if the last line didn't end in newline, add a newline */
1840 if (pi && line[pi->length - 1] != '\n')
1841 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1842 unix_cli_pager_prompt (cf, uf);
1843 }
1844 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001845 break;
1846
1847 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
1848 /* back to the first page of the buffer */
1849 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001850 {
1851 u8 *line = NULL;
1852 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001853
Dave Barach9b8ffd92016-07-08 08:13:45 -04001854 cf->pager_start = 0;
1855 int m = cf->pager_start + (cf->height - 1);
1856 unix_cli_pager_prompt_erase (cf, uf);
1857 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1858 j++)
1859 {
1860 pi = &cf->pager_index[j];
1861 line = cf->pager_vector[pi->line] + pi->offset;
1862 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1863 }
1864 /* if the last line didn't end in newline, add a newline */
1865 if (pi && line[pi->length - 1] != '\n')
1866 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1867 unix_cli_pager_prompt (cf, uf);
1868 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001869 break;
1870
1871 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
1872 /* skip to the last page of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001873 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001874 {
1875 u8 *line = NULL;
1876 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001877
Dave Barach9b8ffd92016-07-08 08:13:45 -04001878 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
1879 unix_cli_pager_prompt_erase (cf, uf);
1880 unix_cli_pager_message (cf, uf, "skipping", "\n");
1881 for (j = cf->pager_start; j < vec_len (cf->pager_index); j++)
1882 {
1883 pi = &cf->pager_index[j];
1884 line = cf->pager_vector[pi->line] + pi->offset;
1885 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1886 }
1887 /* if the last line didn't end in newline, add a newline */
1888 if (pi && line[pi->length - 1] != '\n')
1889 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1890 unix_cli_pager_prompt (cf, uf);
1891 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001892 break;
1893
1894 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
1895 /* wander back one page in the buffer */
1896 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001897 {
1898 u8 *line = NULL;
1899 unix_cli_pager_index_t *pi = NULL;
1900 int m;
Chris Luke862623d2016-05-14 10:13:34 -04001901
Dave Barach9b8ffd92016-07-08 08:13:45 -04001902 if (cf->pager_start >= cf->height)
1903 cf->pager_start -= cf->height - 1;
1904 else
1905 cf->pager_start = 0;
1906 m = cf->pager_start + cf->height - 1;
1907 unix_cli_pager_prompt_erase (cf, uf);
1908 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1909 j++)
1910 {
1911 pi = &cf->pager_index[j];
1912 line = cf->pager_vector[pi->line] + pi->offset;
1913 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1914 }
1915 /* if the last line didn't end in newline, add a newline */
1916 if (pi && line[pi->length - 1] != '\n')
1917 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1918 unix_cli_pager_prompt (cf, uf);
1919 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001920 break;
1921
Chris Luke862623d2016-05-14 10:13:34 -04001922 case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW:
1923 /* Redraw the current pager screen */
1924 unix_cli_pager_redraw (cf, uf);
1925 break;
1926
Chris Luke7afda3a2016-04-25 13:49:22 -04001927 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
1928 /* search forwards in the buffer */
1929 break;
1930
1931
Chris Luke0aca5eb2016-04-25 13:48:54 -04001932 case UNIX_CLI_PARSE_ACTION_CRLF:
1933 crlf:
Chris Luke0aca5eb2016-04-25 13:48:54 -04001934 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1935
Chris Luke7afda3a2016-04-25 13:49:22 -04001936 if (cf->has_history && cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001937 {
1938 if (cf->command_history
1939 && vec_len (cf->command_history) >= cf->history_limit)
1940 {
1941 vec_free (cf->command_history[0]);
1942 vec_delete (cf->command_history, 1, 0);
1943 }
1944 /* Don't add blank lines to the cmd history */
1945 if (vec_len (cf->current_command))
1946 {
1947 /* Don't duplicate the previous command */
1948 j = vec_len (cf->command_history);
1949 if (j == 0 ||
1950 (vec_len (cf->current_command) !=
1951 vec_len (cf->command_history[j - 1])
1952 || memcmp (cf->current_command, cf->command_history[j - 1],
1953 vec_len (cf->current_command)) != 0))
1954 {
1955 /* copy the command to the history */
1956 u8 *c = 0;
1957 vec_append (c, cf->current_command);
1958 vec_add1 (cf->command_history, c);
1959 cf->command_number++;
1960 }
1961 }
1962 cf->excursion = vec_len (cf->command_history);
1963 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04001964
Chris Luke0aca5eb2016-04-25 13:48:54 -04001965 cf->search_mode = 0;
1966 vec_reset_length (cf->search_key);
1967 cf->cursor = 0;
1968
1969 return 0;
1970
Chris Luke7afda3a2016-04-25 13:49:22 -04001971 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
1972 case UNIX_CLI_PARSE_ACTION_NOMATCH:
Chris Luke862623d2016-05-14 10:13:34 -04001973 if (vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001974 {
1975 /* no-op for now */
1976 }
1977 else if (cf->has_history && cf->search_mode && isprint (input))
1978 {
1979 int k, limit, offset;
1980 u8 *item;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001981
Dave Barach9b8ffd92016-07-08 08:13:45 -04001982 vec_add1 (cf->search_key, input);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001983
Dave Barach9b8ffd92016-07-08 08:13:45 -04001984 search_again:
1985 for (j = 0; j < vec_len (cf->command_history); j++)
1986 {
1987 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1988 cf->excursion = 0;
1989 else if (cf->excursion < 0)
1990 cf->excursion = vec_len (cf->command_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001991
Dave Barach9b8ffd92016-07-08 08:13:45 -04001992 item = cf->command_history[cf->excursion];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001993
Dave Barach9b8ffd92016-07-08 08:13:45 -04001994 limit = (vec_len (cf->search_key) > vec_len (item)) ?
1995 vec_len (item) : vec_len (cf->search_key);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001996
Dave Barach9b8ffd92016-07-08 08:13:45 -04001997 for (offset = 0; offset <= vec_len (item) - limit; offset++)
1998 {
1999 for (k = 0; k < limit; k++)
2000 {
2001 if (item[k + offset] != cf->search_key[k])
2002 goto next_offset;
2003 }
2004 goto found_at_offset;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002005
Dave Barach9b8ffd92016-07-08 08:13:45 -04002006 next_offset:
2007 ;
2008 }
2009 goto next;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002010
Dave Barach9b8ffd92016-07-08 08:13:45 -04002011 found_at_offset:
2012 for (j = 0; j < vec_len (cf->current_command); j++)
2013 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002014
Dave Barach9b8ffd92016-07-08 08:13:45 -04002015 vec_validate (cf->current_command, vec_len (item) - 1);
2016 clib_memcpy (cf->current_command, item, vec_len (item));
2017 _vec_len (cf->current_command) = vec_len (item);
Chris Luke93dcd1d2016-05-10 10:45:10 -04002018
Dave Barach9b8ffd92016-07-08 08:13:45 -04002019 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
2020 vec_len (cf->current_command));
2021 cf->cursor = vec_len (cf->current_command);
2022 goto found;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002023
Dave Barach9b8ffd92016-07-08 08:13:45 -04002024 next:
2025 cf->excursion += cf->search_mode;
2026 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002027
Dave Barach9b8ffd92016-07-08 08:13:45 -04002028 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12);
2029 vec_reset_length (cf->search_key);
2030 vec_reset_length (cf->current_command);
2031 cf->search_mode = 0;
2032 cf->cursor = 0;
2033 goto crlf;
2034 }
2035 else if (isprint (input)) /* skip any errant control codes */
2036 {
2037 if (cf->cursor == vec_len (cf->current_command))
2038 {
2039 /* Append to end */
2040 vec_add1 (cf->current_command, input);
2041 cf->cursor++;
Chris Luke7afda3a2016-04-25 13:49:22 -04002042
Dave Barach9b8ffd92016-07-08 08:13:45 -04002043 /* Echo the character back to the client */
2044 unix_vlib_cli_output_raw (cf, uf, &input, 1);
2045 }
2046 else
2047 {
2048 /* Insert at cursor: resize +1 byte, move everything over */
2049 j = vec_len (cf->current_command) - cf->cursor;
2050 vec_add1 (cf->current_command, (u8) 'A');
2051 memmove (cf->current_command + cf->cursor + 1,
2052 cf->current_command + cf->cursor, j);
2053 cf->current_command[cf->cursor] = input;
2054 /* Redraw the line */
2055 j++;
2056 unix_vlib_cli_output_raw (cf, uf,
2057 cf->current_command + cf->cursor, j);
2058 /* Put terminal cursor back */
2059 while (--j)
2060 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
2061 cf->cursor++;
2062 }
2063 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002064 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002065 {
2066 /* no-op - not printable or otherwise not actionable */
2067 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002068
2069 found:
2070
2071 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04002072
2073 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2074 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002075 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04002076 return 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002077}
2078
Chris Luke8e5b0412016-07-26 13:06:10 -04002079/** @brief Process input bytes on a stream to provide line editing and
Chris Luke0aca5eb2016-04-25 13:48:54 -04002080 * command history in the CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002081static int
Damjan Marion56dd5432017-09-08 19:52:02 +02002082unix_cli_line_edit (unix_cli_main_t * cm, unix_main_t * um,
2083 clib_file_main_t * fm, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04002084{
Damjan Marion56dd5432017-09-08 19:52:02 +02002085 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002086 int i;
2087
2088 for (i = 0; i < vec_len (cf->input_vector); i++)
2089 {
2090 unix_cli_parse_action_t action;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002091 i32 matched = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04002092 unix_cli_parse_actions_t *a;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002093
Chris Luke7afda3a2016-04-25 13:49:22 -04002094 /* If we're in the pager mode, search the pager actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002095 a =
2096 vec_len (cf->pager_index) ? unix_cli_parse_pager :
2097 unix_cli_parse_strings;
Chris Luke7afda3a2016-04-25 13:49:22 -04002098
Chris Luke93dcd1d2016-05-10 10:45:10 -04002099 /* See if the input buffer is some sort of control code */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002100 action = unix_cli_match_action (a, &cf->input_vector[i],
2101 vec_len (cf->input_vector) - i,
2102 &matched);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002103
2104 switch (action)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002105 {
2106 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2107 if (i)
2108 {
2109 /* There was a partial match which means we need more bytes
2110 * than the input buffer currently has.
2111 * Since the bytes before here have been processed, shift
2112 * the remaining contents to the start of the input buffer.
2113 */
2114 vec_delete (cf->input_vector, i, 0);
2115 }
2116 return 1; /* wait for more */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002117
Dave Barach9b8ffd92016-07-08 08:13:45 -04002118 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2119 /* process telnet options */
2120 matched = unix_cli_process_telnet (um, cf, uf,
2121 cf->input_vector + i,
2122 vec_len (cf->input_vector) - i);
2123 if (matched < 0)
2124 {
2125 if (i)
2126 {
2127 /* There was a partial match which means we need more bytes
2128 * than the input buffer currently has.
2129 * Since the bytes before here have been processed, shift
2130 * the remaining contents to the start of the input buffer.
2131 */
2132 vec_delete (cf->input_vector, i, 0);
2133 }
2134 return 1; /* wait for more */
2135 }
2136 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002137
Dave Barach9b8ffd92016-07-08 08:13:45 -04002138 default:
2139 /* process the action */
2140 if (!unix_cli_line_process_one (cm, um, cf, uf,
2141 cf->input_vector[i], action))
2142 {
2143 /* CRLF found. Consume the bytes from the input_vector */
2144 vec_delete (cf->input_vector, i + matched, 0);
2145 /* And tell our caller to execute cf->input_command */
2146 return 0;
2147 }
2148 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002149
2150 i += matched;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002151 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002152
Dave Barach9b8ffd92016-07-08 08:13:45 -04002153 vec_reset_length (cf->input_vector);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002154 return 1;
2155}
2156
Chris Luke8e5b0412016-07-26 13:06:10 -04002157/** @brief Process input to a CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002158static void
2159unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002160{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002161 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002162 clib_file_main_t *fm = &file_main;
2163 clib_file_t *uf;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002164 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002165 unformat_input_t input;
2166 int vlib_parse_eval (u8 *);
2167
Chris Luke93dcd1d2016-05-10 10:45:10 -04002168more:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002169 /* Try vlibplex first. Someday... */
2170 if (0 && vlib_parse_eval (cf->input_vector) == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002171 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002172
Chris Luke93dcd1d2016-05-10 10:45:10 -04002173 if (cf->line_mode)
2174 {
2175 /* just treat whatever we got as a complete line of input */
2176 cf->current_command = cf->input_vector;
2177 }
2178 else
2179 {
2180 /* Line edit, echo, etc. */
Damjan Marion56dd5432017-09-08 19:52:02 +02002181 if (unix_cli_line_edit (cm, um, fm, cf))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002182 /* want more input */
2183 return;
Chris Luke93dcd1d2016-05-10 10:45:10 -04002184 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002185
2186 if (um->log_fd)
2187 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002188 static u8 *lv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002189 vec_reset_length (lv);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002190 lv = format (lv, "%U[%d]: %v",
2191 format_timeval, 0 /* current bat-time */ ,
2192 0 /* current bat-format */ ,
2193 cli_file_index, cf->input_vector);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002194 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002195 int rv __attribute__ ((unused)) =
2196 write (um->log_fd, lv, vec_len (lv));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002197 }
2198 }
2199
Chris Luke93dcd1d2016-05-10 10:45:10 -04002200 /* Copy our input command to a new string */
2201 unformat_init_vector (&input, cf->current_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002202
2203 /* Remove leading white space from input. */
2204 (void) unformat (&input, "");
2205
2206 cm->current_input_file_index = cli_file_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002207 cf->pager_start = 0; /* start a new pager session */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002208
2209 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002210 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output,
2211 cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002212
Ed Warnickecb9cada2015-12-08 15:45:58 -07002213 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
2214 input.buffer = 0;
2215
2216 unformat_free (&input);
2217
Chris Luke93dcd1d2016-05-10 10:45:10 -04002218 /* Re-fetch pointer since pool may have moved. */
2219 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002220 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke93dcd1d2016-05-10 10:45:10 -04002221
Ed Warnickecb9cada2015-12-08 15:45:58 -07002222done:
Chris Luke93dcd1d2016-05-10 10:45:10 -04002223 /* reset vector; we'll re-use it later */
2224 if (cf->line_mode)
2225 vec_reset_length (cf->input_vector);
2226 else
2227 vec_reset_length (cf->current_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002228
Chris Luke7afda3a2016-04-25 13:49:22 -04002229 if (cf->no_pager == 2)
2230 {
2231 /* Pager was programmatically disabled */
2232 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
2233 cf->no_pager = um->cli_no_pager;
2234 }
2235
Dave Barach9b8ffd92016-07-08 08:13:45 -04002236 if (vec_len (cf->pager_index) == 0
2237 || vec_len (cf->pager_index) < cf->height)
Chris Luke7afda3a2016-04-25 13:49:22 -04002238 {
2239 /* There was no need for the pager */
2240 unix_cli_pager_reset (cf);
2241
2242 /* Prompt. */
2243 unix_cli_cli_prompt (cf, uf);
2244 }
2245 else
2246 {
2247 /* Display the pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002248 unix_cli_pager_prompt (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04002249 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002250
Dave Barach9b8ffd92016-07-08 08:13:45 -04002251 /* Any residual data in the input vector? */
2252 if (vec_len (cf->input_vector))
2253 goto more;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002254}
2255
Chris Luke54ccf222016-07-25 16:38:11 -04002256/** Destroy a CLI session.
2257 * @note If we destroy the @c stdin session this additionally signals
2258 * the shutdown of VPP.
2259 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002260static void
2261unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002262{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002263 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002264 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002265 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +02002266 clib_file_t *uf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002267 int i;
2268
2269 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002270 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002271
2272 /* Quit/EOF on stdin means quit program. */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002273 if (uf->file_descriptor == UNIX_CLI_STDIN_FD)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002274 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
2275
2276 vec_free (cf->current_command);
2277 vec_free (cf->search_key);
2278
2279 for (i = 0; i < vec_len (cf->command_history); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002280 vec_free (cf->command_history[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002281
2282 vec_free (cf->command_history);
2283
Damjan Marion56dd5432017-09-08 19:52:02 +02002284 clib_file_del (fm, uf);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002285
2286 unix_cli_file_free (cf);
2287 pool_put (cm->cli_file_pool, cf);
2288}
2289
Chris Luke54ccf222016-07-25 16:38:11 -04002290/** Handle system events. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002291static uword
2292unix_cli_process (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002293 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002294{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002295 unix_cli_main_t *cm = &unix_cli_main;
2296 uword i, *data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002297
2298 while (1)
2299 {
2300 unix_cli_process_event_type_t event_type;
2301 vlib_process_wait_for_event (vm);
2302 event_type = vlib_process_get_events (vm, &data);
2303
2304 switch (event_type)
2305 {
2306 case UNIX_CLI_PROCESS_EVENT_READ_READY:
2307 for (i = 0; i < vec_len (data); i++)
2308 unix_cli_process_input (cm, data[i]);
2309 break;
2310
2311 case UNIX_CLI_PROCESS_EVENT_QUIT:
2312 /* Kill this process. */
2313 for (i = 0; i < vec_len (data); i++)
2314 unix_cli_kill (cm, data[i]);
2315 goto done;
2316 }
2317
2318 if (data)
2319 _vec_len (data) = 0;
2320 }
2321
Dave Barach9b8ffd92016-07-08 08:13:45 -04002322done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002323 vec_free (data);
2324
2325 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
2326
2327 /* Add node index so we can re-use this process later. */
2328 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
2329
2330 return 0;
2331}
2332
Chris Luke54ccf222016-07-25 16:38:11 -04002333/** Called when a CLI session file descriptor can be written to without
2334 * blocking. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002335static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002336unix_cli_write_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002337{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002338 unix_cli_main_t *cm = &unix_cli_main;
2339 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002340 int n;
2341
2342 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2343
2344 /* Flush output vector. */
2345 n = write (uf->file_descriptor,
2346 cf->output_vector, vec_len (cf->output_vector));
2347
2348 if (n < 0 && errno != EAGAIN)
2349 return clib_error_return_unix (0, "write");
2350
2351 else if (n > 0)
2352 unix_cli_del_pending_output (uf, cf, n);
2353
2354 return /* no error */ 0;
2355}
2356
Chris Luke54ccf222016-07-25 16:38:11 -04002357/** Called when a CLI session file descriptor has data to be read. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002358static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002359unix_cli_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002360{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002361 unix_main_t *um = &unix_main;
2362 unix_cli_main_t *cm = &unix_cli_main;
2363 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002364 uword l;
2365 int n, n_read, n_try;
2366
2367 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2368
2369 n = n_try = 4096;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002370 while (n == n_try)
2371 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002372 l = vec_len (cf->input_vector);
2373 vec_resize (cf->input_vector, l + n_try);
2374
2375 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
2376
2377 /* Error? */
2378 if (n < 0 && errno != EAGAIN)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002379 return clib_error_return_unix (0, "read");
2380
Ed Warnickecb9cada2015-12-08 15:45:58 -07002381 n_read = n < 0 ? 0 : n;
2382 _vec_len (cf->input_vector) = l + n_read;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002383 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002384
Dave Barach9b8ffd92016-07-08 08:13:45 -04002385 if (!(n < 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002386 vlib_process_signal_event (um->vlib_main,
2387 cf->process_node_index,
2388 (n_read == 0
2389 ? UNIX_CLI_PROCESS_EVENT_QUIT
2390 : UNIX_CLI_PROCESS_EVENT_READ_READY),
2391 /* event data */ uf->private_data);
2392
2393 return /* no error */ 0;
2394}
2395
Chris Lukeb5850972016-05-03 16:34:59 -04002396/** Store a new CLI session.
2397 * @param name The name of the session.
2398 * @param fd The file descriptor for the session I/O.
2399 * @return The session ID.
2400 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002401static u32
2402unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002403{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002404 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002405 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002406 unix_cli_file_t *cf;
Damjan Marion56dd5432017-09-08 19:52:02 +02002407 clib_file_t template = { 0 };
Dave Barach9b8ffd92016-07-08 08:13:45 -04002408 vlib_main_t *vm = um->vlib_main;
2409 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002410
2411 name = (char *) format (0, "unix-cli-%s", name);
2412
2413 if (vec_len (cm->unused_cli_process_node_indices) > 0)
2414 {
2415 uword l = vec_len (cm->unused_cli_process_node_indices);
2416
2417 /* Find node and give it new name. */
2418 n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
2419 vec_free (n->name);
2420 n->name = (u8 *) name;
2421
2422 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
2423
2424 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
2425 }
2426 else
2427 {
2428 static vlib_node_registration_t r = {
2429 .function = unix_cli_process,
2430 .type = VLIB_NODE_TYPE_PROCESS,
Dave Barach96e12032016-06-09 15:32:48 -04002431 .process_log2_n_stack_bytes = 16,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002432 };
2433
2434 r.name = name;
2435 vlib_register_node (vm, &r);
2436 vec_free (name);
2437
2438 n = vlib_get_node (vm, r.index);
2439 }
2440
2441 pool_get (cm->cli_file_pool, cf);
2442 memset (cf, 0, sizeof (*cf));
2443
2444 template.read_function = unix_cli_read_ready;
2445 template.write_function = unix_cli_write_ready;
2446 template.file_descriptor = fd;
2447 template.private_data = cf - cm->cli_file_pool;
2448
2449 cf->process_node_index = n->index;
Damjan Marion56dd5432017-09-08 19:52:02 +02002450 cf->clib_file_index = clib_file_add (fm, &template);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002451 cf->output_vector = 0;
2452 cf->input_vector = 0;
2453
Ed Warnickecb9cada2015-12-08 15:45:58 -07002454 vlib_start_process (vm, n->runtime_index);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002455
Dave Barach9b8ffd92016-07-08 08:13:45 -04002456 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002457 p->output_function = unix_vlib_cli_output;
2458 p->output_function_arg = cf - cm->cli_file_pool;
2459
Ed Warnickecb9cada2015-12-08 15:45:58 -07002460 return cf - cm->cli_file_pool;
2461}
2462
Chris Lukeb5850972016-05-03 16:34:59 -04002463/** Telnet listening socket has a new connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002464static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +02002465unix_cli_listen_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002466{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002467 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002468 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002469 unix_cli_main_t *cm = &unix_cli_main;
2470 clib_socket_t *s = &um->cli_listen_socket;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002471 clib_socket_t client;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002472 char *client_name;
2473 clib_error_t *error;
2474 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002475 u32 cf_index;
2476
2477 error = clib_socket_accept (s, &client);
2478 if (error)
2479 return error;
2480
2481 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
2482
2483 cf_index = unix_cli_file_add (cm, client_name, client.fd);
2484 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2485
2486 /* No longer need CLIB version of socket. */
2487 clib_socket_free (&client);
2488
2489 vec_free (client_name);
2490
2491 /* if we're supposed to run telnet session in character mode (default) */
2492 if (um->cli_line_mode == 0)
2493 {
Chris Luke0aca5eb2016-04-25 13:48:54 -04002494 /*
2495 * Set telnet client character mode, echo on, suppress "go-ahead".
2496 * Technically these should be negotiated, but this works.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002497 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002498 u8 charmode_option[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002499 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
2500 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
2501 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
2502 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
2503 IAC, WILL, TELOPT_ECHO, /* server will do echo */
2504 IAC, DONT, TELOPT_ECHO, /* client should not echo */
2505 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
2506 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
2507 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
2508 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002509 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002510
Chris Luke0aca5eb2016-04-25 13:48:54 -04002511 /* Enable history on this CLI */
Chris Luke7afda3a2016-04-25 13:49:22 -04002512 cf->history_limit = um->cli_history_limit;
2513 cf->has_history = cf->history_limit != 0;
2514
2515 /* Make sure this session is in line mode */
2516 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002517
2518 /* We need CRLF */
2519 cf->crlf_mode = 1;
2520
Chris Luke7afda3a2016-04-25 13:49:22 -04002521 /* Setup the pager */
2522 cf->no_pager = um->cli_no_pager;
2523
Damjan Marion56dd5432017-09-08 19:52:02 +02002524 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002525
2526 /* Send the telnet options */
2527 unix_vlib_cli_output_raw (cf, uf, charmode_option,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002528 ARRAY_LEN (charmode_option));
Chris Luke0aca5eb2016-04-25 13:48:54 -04002529
2530 /* In case the client doesn't negotiate terminal type, use
2531 * a timer to kick off the initial prompt. */
2532 timer_call (unix_cli_file_welcome_timer, cf_index, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002533 }
2534
2535 return error;
2536}
2537
Chris Lukeb5850972016-05-03 16:34:59 -04002538/** The system terminal has informed us that the window size
2539 * has changed.
2540 */
Chris Luke7afda3a2016-04-25 13:49:22 -04002541static void
2542unix_cli_resize_interrupt (int signum)
2543{
Damjan Marion56dd5432017-09-08 19:52:02 +02002544 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002545 unix_cli_main_t *cm = &unix_cli_main;
2546 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2547 cm->stdin_cli_file_index);
Damjan Marion56dd5432017-09-08 19:52:02 +02002548 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04002549 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002550 (void) signum;
Chris Luke7afda3a2016-04-25 13:49:22 -04002551
2552 /* Terminal resized, fetch the new size */
Dave Barachb2a6e252016-07-27 10:00:58 -04002553 if (ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws) < 0)
2554 {
2555 /* "Should never happen..." */
2556 clib_unix_warning ("TIOCGWINSZ");
2557 /* We can't trust ws.XXX... */
2558 return;
2559 }
Chris Lukeb2bcad62017-09-18 08:51:22 -04002560
Chris Luke7afda3a2016-04-25 13:49:22 -04002561 cf->width = ws.ws_col;
Chris Lukeb2bcad62017-09-18 08:51:22 -04002562 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
2563 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
2564 if (cf->width < UNIX_CLI_MIN_TERMINAL_WIDTH)
2565 cf->width = UNIX_CLI_MIN_TERMINAL_WIDTH;
2566
Chris Luke7afda3a2016-04-25 13:49:22 -04002567 cf->height = ws.ws_row;
Chris Lukeb2bcad62017-09-18 08:51:22 -04002568 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
2569 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
2570 if (cf->height < UNIX_CLI_MIN_TERMINAL_HEIGHT)
2571 cf->height = UNIX_CLI_MIN_TERMINAL_HEIGHT;
Chris Luke862623d2016-05-14 10:13:34 -04002572
2573 /* Reindex the pager buffer */
2574 unix_cli_pager_reindex (cf);
2575
2576 /* Redraw the page */
2577 unix_cli_pager_redraw (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04002578}
2579
Chris Lukeb5850972016-05-03 16:34:59 -04002580/** Handle configuration directives in the @em unix section. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002581static clib_error_t *
2582unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
2583{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002584 unix_main_t *um = &unix_main;
Damjan Marion56dd5432017-09-08 19:52:02 +02002585 clib_file_main_t *fm = &file_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002586 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002587 int flags;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002588 clib_error_t *error = 0;
2589 unix_cli_file_t *cf;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002590 u32 cf_index;
2591 struct termios tio;
Chris Luke7afda3a2016-04-25 13:49:22 -04002592 struct sigaction sa;
2593 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002594 u8 *term;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002595
2596 /* We depend on unix flags being set. */
2597 if ((error = vlib_call_config_function (vm, unix_config)))
2598 return error;
2599
2600 if (um->flags & UNIX_FLAG_INTERACTIVE)
2601 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002602 /* Set stdin to be non-blocking. */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002603 if ((flags = fcntl (UNIX_CLI_STDIN_FD, F_GETFL, 0)) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002604 flags = 0;
Dave Barachb2a6e252016-07-27 10:00:58 -04002605 (void) fcntl (UNIX_CLI_STDIN_FD, F_SETFL, flags | O_NONBLOCK);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002606
Chris Luke0aca5eb2016-04-25 13:48:54 -04002607 cf_index = unix_cli_file_add (cm, "stdin", UNIX_CLI_STDIN_FD);
2608 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04002609 cm->stdin_cli_file_index = cf_index;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002610
2611 /* If stdin is a tty and we are using chacracter mode, enable
2612 * history on the CLI and set the tty line discipline accordingly. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002613 if (isatty (UNIX_CLI_STDIN_FD) && um->cli_line_mode == 0)
2614 {
2615 /* Capture terminal resize events */
Ed Warnicke853e7202016-08-12 11:42:26 -07002616 memset (&sa, 0, sizeof (sa));
Dave Barach9b8ffd92016-07-08 08:13:45 -04002617 sa.sa_handler = unix_cli_resize_interrupt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002618 if (sigaction (SIGWINCH, &sa, 0) < 0)
2619 clib_panic ("sigaction");
Chris Luke7afda3a2016-04-25 13:49:22 -04002620
Dave Barach9b8ffd92016-07-08 08:13:45 -04002621 /* Retrieve the current terminal size */
2622 ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws);
2623 cf->width = ws.ws_col;
2624 cf->height = ws.ws_row;
Chris Luke7afda3a2016-04-25 13:49:22 -04002625
Dave Barach9b8ffd92016-07-08 08:13:45 -04002626 if (cf->width == 0 || cf->height == 0)
2627 /* We have a tty, but no size. Stick to line mode. */
2628 goto notty;
Chris Luke7afda3a2016-04-25 13:49:22 -04002629
Dave Barach9b8ffd92016-07-08 08:13:45 -04002630 /* Setup the history */
2631 cf->history_limit = um->cli_history_limit;
2632 cf->has_history = cf->history_limit != 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04002633
Dave Barach9b8ffd92016-07-08 08:13:45 -04002634 /* Setup the pager */
2635 cf->no_pager = um->cli_no_pager;
Chris Luke7afda3a2016-04-25 13:49:22 -04002636
Dave Barach9b8ffd92016-07-08 08:13:45 -04002637 /* We're going to be in char by char mode */
2638 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002639
Dave Barach9b8ffd92016-07-08 08:13:45 -04002640 /* Save the original tty state so we can restore it later */
2641 tcgetattr (UNIX_CLI_STDIN_FD, &um->tio_stdin);
2642 um->tio_isset = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002643
Dave Barach9b8ffd92016-07-08 08:13:45 -04002644 /* Tweak the tty settings */
2645 tio = um->tio_stdin;
2646 /* echo off, canonical mode off, ext'd input processing off */
2647 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
2648 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
2649 tio.c_cc[VTIME] = 0; /* no timer */
2650 tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &tio);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002651
Dave Barach9b8ffd92016-07-08 08:13:45 -04002652 /* See if we can do ANSI/VT100 output */
2653 term = (u8 *) getenv ("TERM");
2654 if (term != NULL)
2655 cf->ansi_capable = unix_cli_terminal_type (term,
2656 strlen ((char *)
2657 term));
2658 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002659 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002660 {
2661 notty:
2662 /* No tty, so make sure these things are off */
2663 cf->no_pager = 1;
2664 cf->history_limit = 0;
2665 cf->has_history = 0;
2666 cf->line_mode = 1;
2667 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002668
2669 /* Send banner and initial prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002670 unix_cli_file_welcome (cm, cf);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002671 }
2672
Ed Warnickea25bd1c2016-04-01 22:43:37 -05002673 /* If we have socket config, LISTEN, otherwise, don't */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002674 clib_socket_t *s = &um->cli_listen_socket;
2675 if (s->config && s->config[0] != 0)
2676 {
2677 /* CLI listen. */
Damjan Marion56dd5432017-09-08 19:52:02 +02002678 clib_file_t template = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002679
Damjan Marion57d963f2017-07-20 19:17:06 +02002680 /* mkdir of file socketu, only under /run */
2681 if (strncmp (s->config, "/run", 4) == 0)
Chris Luke475674e2017-07-05 18:02:53 -04002682 {
Damjan Marion57d963f2017-07-20 19:17:06 +02002683 u8 *tmp = format (0, "%s", s->config);
2684 int i = vec_len (tmp);
2685 while (i && tmp[--i] != '/')
2686 ;
2687
2688 tmp[i] = 0;
2689
2690 if (i)
2691 vlib_unix_recursive_mkdir ((char *) tmp);
2692 vec_free (tmp);
Chris Luke475674e2017-07-05 18:02:53 -04002693 }
2694
Damjan Marionf49921f2017-09-11 16:52:11 +02002695 s->flags = CLIB_SOCKET_F_IS_SERVER | /* listen, don't connect */
2696 CLIB_SOCKET_F_ALLOW_GROUP_WRITE; /* PF_LOCAL socket only */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002697 error = clib_socket_init (s);
Ed Warnickea25bd1c2016-04-01 22:43:37 -05002698
Dave Barach9b8ffd92016-07-08 08:13:45 -04002699 if (error)
2700 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002701
Dave Barach9b8ffd92016-07-08 08:13:45 -04002702 template.read_function = unix_cli_listen_read_ready;
2703 template.file_descriptor = s->fd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002704
Damjan Marion56dd5432017-09-08 19:52:02 +02002705 clib_file_add (fm, &template);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002706 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002707
2708 /* Set CLI prompt. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002709 if (!cm->cli_prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002710 cm->cli_prompt = format (0, "VLIB: ");
2711
2712 return 0;
2713}
2714
Chris Luke90f52bf2016-09-12 08:55:13 -04002715/*?
2716 * This module has no configurable parameters.
2717?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002718VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
2719
Chris Luke54ccf222016-07-25 16:38:11 -04002720/** Called when VPP is shutting down, this restores the system
2721 * terminal state if previously saved.
Chris Lukeb5850972016-05-03 16:34:59 -04002722 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002723static clib_error_t *
2724unix_cli_exit (vlib_main_t * vm)
2725{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002726 unix_main_t *um = &unix_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002727
2728 /* If stdin is a tty and we saved the tty state, reset the tty state */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002729 if (isatty (UNIX_CLI_STDIN_FD) && um->tio_isset)
2730 tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &um->tio_stdin);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002731
2732 return 0;
2733}
2734
2735VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
2736
Chris Lukeb5850972016-05-03 16:34:59 -04002737/** Set the CLI prompt.
Chris Luke54ccf222016-07-25 16:38:11 -04002738 * @param prompt The C string to set the prompt to.
Chris Lukeb5850972016-05-03 16:34:59 -04002739 * @note This setting is global; it impacts all current
2740 * and future CLI sessions.
2741 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002742void
2743vlib_unix_cli_set_prompt (char *prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002744{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002745 char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s ";
2746 unix_cli_main_t *cm = &unix_cli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002747 if (cm->cli_prompt)
2748 vec_free (cm->cli_prompt);
2749 cm->cli_prompt = format (0, fmt, prompt);
2750}
2751
Chris Lukeb5850972016-05-03 16:34:59 -04002752/** CLI command to quit the terminal session.
2753 * @note If this is a stdin session then this will
2754 * shutdown VPP also.
2755 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002756static clib_error_t *
2757unix_cli_quit (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002758 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002759{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002760 unix_cli_main_t *cm = &unix_cli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002761
2762 vlib_process_signal_event (vm,
2763 vlib_current_process (vm),
2764 UNIX_CLI_PROCESS_EVENT_QUIT,
2765 cm->current_input_file_index);
2766 return 0;
2767}
2768
Chris Luke54ccf222016-07-25 16:38:11 -04002769/*?
2770 * Terminates the current CLI session.
2771 *
2772 * If VPP is running in @em interactive mode and this is the console session
2773 * (that is, the session on @c stdin) then this will also terminate VPP.
2774?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002775/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002776VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
2777 .path = "quit",
2778 .short_help = "Exit CLI",
2779 .function = unix_cli_quit,
2780};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002781/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002782
Chris Lukeb5850972016-05-03 16:34:59 -04002783/** CLI command to execute a VPP command script. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002784static clib_error_t *
2785unix_cli_exec (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002786 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002787{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002788 char *file_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002789 int fd;
2790 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002791 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002792
2793 file_name = 0;
2794 fd = -1;
2795 error = 0;
2796
Dave Barach9b8ffd92016-07-08 08:13:45 -04002797 if (!unformat (input, "%s", &file_name))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002798 {
2799 error = clib_error_return (0, "expecting file name, got `%U'",
2800 format_unformat_error, input);
2801 goto done;
2802 }
2803
2804 fd = open (file_name, O_RDONLY);
2805 if (fd < 0)
2806 {
2807 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
2808 goto done;
2809 }
2810
2811 /* Make sure its a regular file. */
2812 {
2813 struct stat s;
2814
2815 if (fstat (fd, &s) < 0)
2816 {
2817 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
2818 goto done;
2819 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04002820
2821 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002822 {
2823 error = clib_error_return (0, "not a regular file `%s'", file_name);
2824 goto done;
2825 }
2826 }
2827
2828 unformat_init_unix_file (&sub_input, fd);
2829
2830 vlib_cli_input (vm, &sub_input, 0, 0);
2831 unformat_free (&sub_input);
2832
Dave Barach9b8ffd92016-07-08 08:13:45 -04002833done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002834 if (fd > 0)
2835 close (fd);
2836 vec_free (file_name);
2837
2838 return error;
2839}
2840
Chris Luke54ccf222016-07-25 16:38:11 -04002841/*?
2842 * Executes a sequence of CLI commands which are read from a file.
2843 *
2844 * If a command is unrecognised or otherwise invalid then the usual CLI
2845 * feedback will be generated, however execution of subsequent commands
2846 * from the file will continue.
2847?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002848/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002849VLIB_CLI_COMMAND (cli_exec, static) = {
2850 .path = "exec",
2851 .short_help = "Execute commands from file",
2852 .function = unix_cli_exec,
Dave Barachb2ef4dd2016-03-23 08:56:01 -04002853 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002854};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002855/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002856
Chris Lukeb5850972016-05-03 16:34:59 -04002857/** CLI command to show various unix error statistics. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002858static clib_error_t *
2859unix_show_errors (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002860 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002861{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002862 unix_main_t *um = &unix_main;
2863 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002864 int i, n_errors_to_show;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002865 unix_error_history_t *unix_errors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002866
2867 n_errors_to_show = 1 << 30;
2868
2869 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2870 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002871 if (!unformat (input, "%d", &n_errors_to_show))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002872 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002873 error =
2874 clib_error_return (0,
2875 "expecting integer number of errors to show, got `%U'",
2876 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002877 goto done;
2878 }
2879 }
2880
Dave Barach9b8ffd92016-07-08 08:13:45 -04002881 n_errors_to_show =
2882 clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002883
Dave Barach9b8ffd92016-07-08 08:13:45 -04002884 i =
2885 um->error_history_index >
2886 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002887
2888 while (n_errors_to_show > 0)
2889 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002890 unix_error_history_t *eh = um->error_history + i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002891
Dave Barach9b8ffd92016-07-08 08:13:45 -04002892 if (!eh->error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002893 break;
2894
2895 vec_add1 (unix_errors, eh[0]);
2896 n_errors_to_show -= 1;
2897 if (i == 0)
2898 i = ARRAY_LEN (um->error_history) - 1;
2899 else
2900 i--;
2901 }
2902
2903 if (vec_len (unix_errors) == 0)
2904 vlib_cli_output (vm, "no Unix errors so far");
2905 else
2906 {
2907 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
2908 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
2909 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002910 unix_error_history_t *eh = vec_elt_at_index (unix_errors, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002911 vlib_cli_output (vm, "%U: %U",
2912 format_time_interval, "h:m:s:u", eh->time,
2913 format_clib_error, eh->error);
2914 }
2915 vlib_cli_output (vm, "%U: time now",
2916 format_time_interval, "h:m:s:u", vlib_time_now (vm));
2917 }
2918
Dave Barach9b8ffd92016-07-08 08:13:45 -04002919done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002920 vec_free (unix_errors);
2921 return error;
2922}
2923
Dave Barach9b8ffd92016-07-08 08:13:45 -04002924/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002925VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
2926 .path = "show unix-errors",
2927 .short_help = "Show Unix system call error history",
2928 .function = unix_show_errors,
2929};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002930/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002931
Chris Lukeb5850972016-05-03 16:34:59 -04002932/** CLI command to show session command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002933static clib_error_t *
Chris Luke6b90fe32016-04-25 13:49:15 -04002934unix_cli_show_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002935 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke6b90fe32016-04-25 13:49:15 -04002936{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002937 unix_cli_main_t *cm = &unix_cli_main;
2938 unix_cli_file_t *cf;
Chris Luke6b90fe32016-04-25 13:49:15 -04002939 int i, j;
2940
2941 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2942
Chris Luke7afda3a2016-04-25 13:49:22 -04002943 if (cf->has_history && cf->history_limit)
Chris Luke6b90fe32016-04-25 13:49:15 -04002944 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002945 i = 1 + cf->command_number - vec_len (cf->command_history);
Chris Luke7afda3a2016-04-25 13:49:22 -04002946 for (j = 0; j < vec_len (cf->command_history); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002947 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
Chris Luke7afda3a2016-04-25 13:49:22 -04002948 }
2949 else
2950 {
2951 vlib_cli_output (vm, "History not enabled.\n");
Chris Luke6b90fe32016-04-25 13:49:15 -04002952 }
2953
2954 return 0;
2955}
2956
Chris Luke54ccf222016-07-25 16:38:11 -04002957/*?
2958 * Displays the command history for the current session, if any.
2959?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002960/* *INDENT-OFF* */
Chris Luke6b90fe32016-04-25 13:49:15 -04002961VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
2962 .path = "history",
2963 .short_help = "Show current session command history",
2964 .function = unix_cli_show_history,
2965};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002966/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04002967
Chris Lukeb5850972016-05-03 16:34:59 -04002968/** CLI command to show terminal status. */
Chris Luke7afda3a2016-04-25 13:49:22 -04002969static clib_error_t *
2970unix_cli_show_terminal (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002971 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04002972{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002973 unix_main_t *um = &unix_main;
2974 unix_cli_main_t *cm = &unix_cli_main;
2975 unix_cli_file_t *cf;
2976 vlib_node_t *n;
Chris Luke7afda3a2016-04-25 13:49:22 -04002977
2978 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2979 n = vlib_get_node (vm, cf->process_node_index);
2980
2981 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002982 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
2983 "line-by-line" : "char-by-char");
Chris Luke7afda3a2016-04-25 13:49:22 -04002984 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
2985 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002986 vlib_cli_output (vm, "ANSI capable: %s\n",
2987 cf->ansi_capable ? "yes" : "no");
Chris Luke7afda3a2016-04-25 13:49:22 -04002988 vlib_cli_output (vm, "History enabled: %s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04002989 cf->has_history ? "yes" : "no", !cf->has_history
2990 || cf->history_limit ? "" :
2991 " (disabled by history limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04002992 if (cf->has_history)
2993 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
2994 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04002995 cf->no_pager ? "no" : "yes",
2996 cf->no_pager
2997 || cf->height ? "" : " (disabled by terminal height)",
2998 cf->no_pager
2999 || um->cli_pager_buffer_limit ? "" :
3000 " (disabled by buffer limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04003001 if (!cf->no_pager)
3002 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003003 vlib_cli_output (vm, "CRLF mode: %s\n",
3004 cf->crlf_mode ? "CR+LF" : "LF");
Chris Luke7afda3a2016-04-25 13:49:22 -04003005
3006 return 0;
3007}
3008
Chris Luke54ccf222016-07-25 16:38:11 -04003009/*?
3010 * Displays various information about the state of the current terminal
3011 * session.
3012 *
3013 * @cliexpar
3014 * @cliexstart{show terminal}
3015 * Terminal name: unix-cli-stdin
3016 * Terminal mode: char-by-char
3017 * Terminal width: 123
3018 * Terminal height: 48
3019 * ANSI capable: yes
3020 * History enabled: yes
3021 * History limit: 50
3022 * Pager enabled: yes
3023 * Pager limit: 100000
3024 * CRLF mode: LF
3025 * @cliexend
3026?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003027/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003028VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
3029 .path = "show terminal",
3030 .short_help = "Show current session terminal settings",
3031 .function = unix_cli_show_terminal,
3032};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003033/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003034
Chris Lukeb5850972016-05-03 16:34:59 -04003035/** CLI command to set terminal pager settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003036static clib_error_t *
3037unix_cli_set_terminal_pager (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003038 unformat_input_t * input,
3039 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003040{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003041 unix_main_t *um = &unix_main;
3042 unix_cli_main_t *cm = &unix_cli_main;
3043 unix_cli_file_t *cf;
3044 unformat_input_t _line_input, *line_input = &_line_input;
Billy McFalla9a20e72017-02-15 11:39:12 -05003045 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003046
Dave Barach9b8ffd92016-07-08 08:13:45 -04003047 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04003048 return 0;
3049
3050 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3051
3052 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3053 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003054 if (unformat (line_input, "on"))
3055 cf->no_pager = 0;
3056 else if (unformat (line_input, "off"))
3057 cf->no_pager = 1;
3058 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
3059 vlib_cli_output (vm,
3060 "Pager limit set to %u lines; note, this is global.\n",
3061 um->cli_pager_buffer_limit);
3062 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003063 {
3064 error = clib_error_return (0, "unknown parameter: `%U`",
3065 format_unformat_error, line_input);
3066 goto done;
3067 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04003068 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003069
Billy McFalla9a20e72017-02-15 11:39:12 -05003070done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003071 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003072
Billy McFalla9a20e72017-02-15 11:39:12 -05003073 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003074}
3075
Chris Luke54ccf222016-07-25 16:38:11 -04003076/*?
3077 * Enables or disables the terminal pager for this session. Generally
3078 * this defaults to enabled.
3079 *
3080 * Additionally allows the pager buffer size to be set; though note that
3081 * this value is set globally and not per session.
3082?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003083/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003084VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
3085 .path = "set terminal pager",
3086 .short_help = "set terminal pager [on|off] [limit <lines>]",
3087 .function = unix_cli_set_terminal_pager,
3088};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003089/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003090
Chris Lukeb5850972016-05-03 16:34:59 -04003091/** CLI command to set terminal history settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003092static clib_error_t *
3093unix_cli_set_terminal_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003094 unformat_input_t * input,
3095 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003096{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003097 unix_cli_main_t *cm = &unix_cli_main;
3098 unix_cli_file_t *cf;
3099 unformat_input_t _line_input, *line_input = &_line_input;
Chris Luke7afda3a2016-04-25 13:49:22 -04003100 u32 limit;
Billy McFalla9a20e72017-02-15 11:39:12 -05003101 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003102
Dave Barach9b8ffd92016-07-08 08:13:45 -04003103 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04003104 return 0;
3105
3106 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3107
3108 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3109 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003110 if (unformat (line_input, "on"))
3111 cf->has_history = 1;
3112 else if (unformat (line_input, "off"))
3113 cf->has_history = 0;
3114 else if (unformat (line_input, "limit %u", &cf->history_limit))
3115 ;
3116 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003117 {
3118 error = clib_error_return (0, "unknown parameter: `%U`",
3119 format_unformat_error, line_input);
3120 goto done;
3121 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003122
Dave Barach9b8ffd92016-07-08 08:13:45 -04003123 /* If we reduced history size, or turned it off, purge the history */
3124 limit = cf->has_history ? cf->history_limit : 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003125
Dave Barach9b8ffd92016-07-08 08:13:45 -04003126 while (cf->command_history && vec_len (cf->command_history) >= limit)
3127 {
3128 vec_free (cf->command_history[0]);
3129 vec_delete (cf->command_history, 1, 0);
3130 }
3131 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003132
Billy McFalla9a20e72017-02-15 11:39:12 -05003133done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003134 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003135
Billy McFalla9a20e72017-02-15 11:39:12 -05003136 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003137}
3138
Chris Luke54ccf222016-07-25 16:38:11 -04003139/*?
3140 * Enables or disables the command history function of the current
3141 * terminal. Generally this defaults to enabled.
3142 *
3143 * This command also allows the maximum size of the history buffer for
3144 * this session to be altered.
3145?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003146/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003147VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
3148 .path = "set terminal history",
3149 .short_help = "set terminal history [on|off] [limit <lines>]",
3150 .function = unix_cli_set_terminal_history,
3151};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003152/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003153
Chris Lukeb5850972016-05-03 16:34:59 -04003154/** CLI command to set terminal ANSI settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003155static clib_error_t *
3156unix_cli_set_terminal_ansi (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003157 unformat_input_t * input,
3158 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003159{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003160 unix_cli_main_t *cm = &unix_cli_main;
3161 unix_cli_file_t *cf;
Chris Luke7afda3a2016-04-25 13:49:22 -04003162
3163 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3164
3165 if (unformat (input, "on"))
3166 cf->ansi_capable = 1;
3167 else if (unformat (input, "off"))
3168 cf->ansi_capable = 0;
3169 else
3170 return clib_error_return (0, "unknown parameter: `%U`",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003171 format_unformat_error, input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003172
3173 return 0;
3174}
3175
Chris Luke54ccf222016-07-25 16:38:11 -04003176/*?
3177 * Enables or disables the use of ANSI control sequences by this terminal.
3178 * The default will vary based on terminal detection at the start of the
3179 * session.
3180 *
3181 * ANSI control sequences are used in a small number of places to provide,
3182 * for example, color text output and to control the cursor in the pager.
3183?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003184/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003185VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
3186 .path = "set terminal ansi",
3187 .short_help = "set terminal ansi [on|off]",
3188 .function = unix_cli_set_terminal_ansi,
3189};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003190/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003191
3192static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07003193unix_cli_init (vlib_main_t * vm)
3194{
3195 return 0;
3196}
3197
3198VLIB_INIT_FUNCTION (unix_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003199
3200/*
3201 * fd.io coding-style-patch-verification: ON
3202 *
3203 * Local Variables:
3204 * eval: (c-set-style "gnu")
3205 * End:
3206 */