blob: 1befa25def4b478e48248b6c05c5c6da3cfa3826 [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 Luke0aca5eb2016-04-25 13:48:54 -040094/** Unix standard in */
95#define UNIX_CLI_STDIN_FD 0
96
97
Chris Lukeb5850972016-05-03 16:34:59 -040098/** A CLI banner line. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040099typedef struct
100{
101 u8 *line; /**< The line to print. */
102 u32 length; /**< The length of the line without terminating NUL. */
Chris Luke572d8122016-04-25 13:49:07 -0400103} unix_cli_banner_t;
104
105#define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
106/** Plain welcome banner. */
107static unix_cli_banner_t unix_cli_banner[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400108 _(" _______ _ _ _____ ___ \n"),
109 _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
110 _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
111 _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
112 _("\n")
Chris Luke572d8122016-04-25 13:49:07 -0400113};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400114
Chris Luke572d8122016-04-25 13:49:07 -0400115/** ANSI color welcome banner. */
116static unix_cli_banner_t unix_cli_banner_color[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400117 _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
118 _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
119 _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
120 _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \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#undef _
125
Chris Luke862623d2016-05-14 10:13:34 -0400126/** Pager line index */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400127typedef struct
128{
Chris Luke862623d2016-05-14 10:13:34 -0400129 /** Index into pager_vector */
130 u32 line;
131
132 /** Offset of the string in the line */
133 u32 offset;
134
135 /** Length of the string in the line */
136 u32 length;
137} unix_cli_pager_index_t;
138
Chris Luke572d8122016-04-25 13:49:07 -0400139
Chris Luke0aca5eb2016-04-25 13:48:54 -0400140/** Unix CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400141typedef struct
142{
Chris Lukeb5850972016-05-03 16:34:59 -0400143 /** The file index held by unix.c */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 u32 unix_file_index;
145
Chris Lukeb5850972016-05-03 16:34:59 -0400146 /** Vector of output pending write to file descriptor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400147 u8 *output_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148
Chris Lukeb5850972016-05-03 16:34:59 -0400149 /** Vector of input saved by Unix input node to be processed by
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150 CLI process. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400151 u8 *input_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Chris Luke54ccf222016-07-25 16:38:11 -0400153 /** This session has command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 u8 has_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400155 /** Array of vectors of commands in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156 u8 **command_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400157 /** The command currently pointed at by the history cursor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400158 u8 *current_command;
Chris Luke54ccf222016-07-25 16:38:11 -0400159 /** How far from the end of the history array the user has browsed. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700160 i32 excursion;
Chris Luke6b90fe32016-04-25 13:49:15 -0400161
Chris Lukeb5850972016-05-03 16:34:59 -0400162 /** Maximum number of history entries this session will store. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 u32 history_limit;
Chris Luke6b90fe32016-04-25 13:49:15 -0400164
Chris Lukeb5850972016-05-03 16:34:59 -0400165 /** Current command line counter */
Chris Luke6b90fe32016-04-25 13:49:15 -0400166 u32 command_number;
167
Chris Luke54ccf222016-07-25 16:38:11 -0400168 /** The string being searched for in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400169 u8 *search_key;
Chris Luke54ccf222016-07-25 16:38:11 -0400170 /** If non-zero then the CLI is searching in the history array.
171 * - @c -1 means search backwards.
172 * - @c 1 means search forwards.
173 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174 int search_mode;
175
Chris Lukeb5850972016-05-03 16:34:59 -0400176 /** Position of the insert cursor on the current input line */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400177 u32 cursor;
178
Chris Lukeb5850972016-05-03 16:34:59 -0400179 /** Line mode or char mode */
Chris Luke7afda3a2016-04-25 13:49:22 -0400180 u8 line_mode;
181
Chris Lukeb5850972016-05-03 16:34:59 -0400182 /** Set if the CRLF mode wants CR + LF */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400183 u8 crlf_mode;
184
Chris Lukeb5850972016-05-03 16:34:59 -0400185 /** Can we do ANSI output? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400186 u8 ansi_capable;
187
Chris Lukeb5850972016-05-03 16:34:59 -0400188 /** Has the session started? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400189 u8 started;
190
Chris Lukeb5850972016-05-03 16:34:59 -0400191 /** Disable the pager? */
Chris Luke7afda3a2016-04-25 13:49:22 -0400192 u8 no_pager;
193
Chris Lukeb5850972016-05-03 16:34:59 -0400194 /** Pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400195 u8 **pager_vector;
Chris Luke7afda3a2016-04-25 13:49:22 -0400196
Chris Luke862623d2016-05-14 10:13:34 -0400197 /** Index of line fragments in the pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400198 unix_cli_pager_index_t *pager_index;
Chris Luke7afda3a2016-04-25 13:49:22 -0400199
Chris Lukeb5850972016-05-03 16:34:59 -0400200 /** Line number of top of page */
Chris Luke7afda3a2016-04-25 13:49:22 -0400201 u32 pager_start;
202
Chris Lukeb5850972016-05-03 16:34:59 -0400203 /** Terminal width */
204 u32 width;
Chris Luke7afda3a2016-04-25 13:49:22 -0400205
Chris Lukeb5850972016-05-03 16:34:59 -0400206 /** Terminal height */
207 u32 height;
208
209 /** Process node identifier */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210 u32 process_node_index;
211} unix_cli_file_t;
212
Chris Lukeb5850972016-05-03 16:34:59 -0400213/** Resets the pager buffer and other data.
214 * @param f The CLI session whose pager needs to be reset.
215 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400217unix_cli_pager_reset (unix_cli_file_t * f)
Chris Luke7afda3a2016-04-25 13:49:22 -0400218{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400219 u8 **p;
Chris Luke7afda3a2016-04-25 13:49:22 -0400220
Chris Luke862623d2016-05-14 10:13:34 -0400221 f->pager_start = 0;
222
223 vec_free (f->pager_index);
224 f->pager_index = 0;
225
Chris Luke7afda3a2016-04-25 13:49:22 -0400226 vec_foreach (p, f->pager_vector)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400227 {
228 vec_free (*p);
229 }
Chris Luke862623d2016-05-14 10:13:34 -0400230 vec_free (f->pager_vector);
Chris Luke7afda3a2016-04-25 13:49:22 -0400231 f->pager_vector = 0;
232}
233
Chris Lukeb5850972016-05-03 16:34:59 -0400234/** Release storage used by a CLI session.
235 * @param f The CLI session whose storage needs to be released.
236 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400237always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238unix_cli_file_free (unix_cli_file_t * f)
239{
240 vec_free (f->output_vector);
241 vec_free (f->input_vector);
Chris Luke862623d2016-05-14 10:13:34 -0400242 unix_cli_pager_reset (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243}
244
Chris Lukeb5850972016-05-03 16:34:59 -0400245/** CLI actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400246typedef enum
247{
Chris Luke54ccf222016-07-25 16:38:11 -0400248 UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */
249 UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */
250 UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */
251 UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */
252 UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */
253 UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */
254 UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */
255 UNIX_CLI_PARSE_ACTION_LEFT, /**< Left arrow */
256 UNIX_CLI_PARSE_ACTION_RIGHT, /**< Right arrow */
257 UNIX_CLI_PARSE_ACTION_HOME, /**< Home key (jump to start of line) */
258 UNIX_CLI_PARSE_ACTION_END, /**< End key (jump to end of line) */
259 UNIX_CLI_PARSE_ACTION_WORDLEFT, /**< Jump cursor to start of left word */
260 UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */
261 UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */
262 UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */
263 UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */
264 UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */
265 UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */
266 UNIX_CLI_PARSE_ACTION_YANK, /**< Undo last erase action */
267 UNIX_CLI_PARSE_ACTION_TELNETIAC, /**< Telnet control code */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400268
Chris Luke54ccf222016-07-25 16:38:11 -0400269 UNIX_CLI_PARSE_ACTION_PAGER_CRLF, /**< Enter pressed (CR, CRLF, LF, etc) */
270 UNIX_CLI_PARSE_ACTION_PAGER_QUIT, /**< Exit the pager session */
271 UNIX_CLI_PARSE_ACTION_PAGER_NEXT, /**< Scroll to next page */
272 UNIX_CLI_PARSE_ACTION_PAGER_DN, /**< Scroll to next line */
273 UNIX_CLI_PARSE_ACTION_PAGER_UP, /**< Scroll to previous line */
274 UNIX_CLI_PARSE_ACTION_PAGER_TOP, /**< Scroll to first line */
275 UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM, /**< Scroll to last line */
276 UNIX_CLI_PARSE_ACTION_PAGER_PGDN, /**< Scroll to next page */
277 UNIX_CLI_PARSE_ACTION_PAGER_PGUP, /**< Scroll to previous page */
278 UNIX_CLI_PARSE_ACTION_PAGER_REDRAW, /**< Clear and redraw the page on the terminal */
279 UNIX_CLI_PARSE_ACTION_PAGER_SEARCH, /**< Search the pager buffer */
Chris Luke7afda3a2016-04-25 13:49:22 -0400280
Chris Luke54ccf222016-07-25 16:38:11 -0400281 UNIX_CLI_PARSE_ACTION_PARTIALMATCH, /**< Action parser found a partial match */
282 UNIX_CLI_PARSE_ACTION_NOMATCH /**< Action parser did not find any match */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400283} unix_cli_parse_action_t;
284
Chris Luke8e5b0412016-07-26 13:06:10 -0400285/** @brief Mapping of input buffer strings to action values.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400286 * @note This won't work as a hash since we need to be able to do
287 * partial matches on the string.
288 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400289typedef struct
290{
291 u8 *input; /**< Input string to match. */
292 u32 len; /**< Length of input without final NUL. */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400293 unix_cli_parse_action_t action; /**< Action to take when matched. */
294} unix_cli_parse_actions_t;
295
Chris Lukeb5850972016-05-03 16:34:59 -0400296/** @brief Given a capital ASCII letter character return a @c NUL terminated
Chris Luke0aca5eb2016-04-25 13:48:54 -0400297 * string with the control code for that letter.
Chris Lukeb5850972016-05-03 16:34:59 -0400298 *
299 * @param c An ASCII character.
300 * @return A @c NUL terminated string of type @c u8[].
301 *
302 * @par Example
303 * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[].
Chris Luke0aca5eb2016-04-25 13:48:54 -0400304 */
305#define CTL(c) (u8[]){ (c) - '@', 0 }
306
307#define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
Chris Lukeb5850972016-05-03 16:34:59 -0400308/**
309 * Patterns to match on a CLI input stream.
310 * @showinitializer
311 */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400312static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400313 /* Line handling */
314 _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */
315 _("\n", UNIX_CLI_PARSE_ACTION_CRLF),
316 _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */
317 _("\r", UNIX_CLI_PARSE_ACTION_CRLF),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400318
Dave Barach9b8ffd92016-07-08 08:13:45 -0400319 /* Unix shell control codes */
320 _(CTL ('B'), UNIX_CLI_PARSE_ACTION_LEFT),
321 _(CTL ('F'), UNIX_CLI_PARSE_ACTION_RIGHT),
322 _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP),
323 _(CTL ('N'), UNIX_CLI_PARSE_ACTION_DOWN),
324 _(CTL ('A'), UNIX_CLI_PARSE_ACTION_HOME),
325 _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END),
326 _(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT),
327 _(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT),
328 _(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT),
329 _(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK),
330 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR),
331 _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */
332 _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */
333 _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */
334 _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */
335 _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400336
Dave Barach9b8ffd92016-07-08 08:13:45 -0400337 /* VT100 Normal mode - Broadest support */
338 _(CSI "A", UNIX_CLI_PARSE_ACTION_UP),
339 _(CSI "B", UNIX_CLI_PARSE_ACTION_DOWN),
340 _(CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT),
341 _(CSI "D", UNIX_CLI_PARSE_ACTION_LEFT),
342 _(CSI "H", UNIX_CLI_PARSE_ACTION_HOME),
343 _(CSI "F", UNIX_CLI_PARSE_ACTION_END),
344 _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */
345 _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */
346 _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400347
348 /* VT100 Application mode - Some Gnome Terminal functions use these */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400349 _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP),
350 _(ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN),
351 _(ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT),
352 _(ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT),
353 _(ESC "OH", UNIX_CLI_PARSE_ACTION_HOME),
354 _(ESC "OF", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400355
Dave Barach9b8ffd92016-07-08 08:13:45 -0400356 /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
357 _(CSI "1~", UNIX_CLI_PARSE_ACTION_HOME),
358 _(CSI "4~", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400359
Dave Barach9b8ffd92016-07-08 08:13:45 -0400360 /* Emacs-ish history search */
361 _(CTL ('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH),
362 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400363
Dave Barach9b8ffd92016-07-08 08:13:45 -0400364 /* Other protocol things */
365 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
366 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
367 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400368};
Chris Lukeb5850972016-05-03 16:34:59 -0400369
370/**
371 * Patterns to match when a CLI session is in the pager.
372 * @showinitializer
373 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400374static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400375 /* Line handling */
376 _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */
377 _("\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
378 _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */
379 _("\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
Chris Luke7afda3a2016-04-25 13:49:22 -0400380
Dave Barach9b8ffd92016-07-08 08:13:45 -0400381 /* Pager commands */
382 _(" ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT),
383 _("q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT),
384 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
385 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
386 _("/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH),
Chris Luke7afda3a2016-04-25 13:49:22 -0400387
Dave Barach9b8ffd92016-07-08 08:13:45 -0400388 /* VT100 */
389 _(CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP),
390 _(CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN),
391 _(CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
392 _(CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400393
Dave Barach9b8ffd92016-07-08 08:13:45 -0400394 /* VT100 Application mode */
395 _(ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP),
396 _(ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN),
397 _(ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
398 _(ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400399
Dave Barach9b8ffd92016-07-08 08:13:45 -0400400 /* ANSI X3.41-1974 */
401 _(CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
402 _(CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
403 _(CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP),
404 _(CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN),
Chris Luke7afda3a2016-04-25 13:49:22 -0400405
Dave Barach9b8ffd92016-07-08 08:13:45 -0400406 /* Other protocol things */
407 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
408 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
409 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke7afda3a2016-04-25 13:49:22 -0400410};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400411
Chris Luke0aca5eb2016-04-25 13:48:54 -0400412#undef _
413
Chris Lukeb5850972016-05-03 16:34:59 -0400414/** CLI session events. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400415typedef enum
416{
Chris Lukeb5850972016-05-03 16:34:59 -0400417 UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400418 UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400419} unix_cli_process_event_type_t;
420
Chris Lukeb5850972016-05-03 16:34:59 -0400421/** CLI global state. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400422typedef struct
423{
Chris Lukeb5850972016-05-03 16:34:59 -0400424 /** Prompt string for CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400425 u8 *cli_prompt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
Chris Lukeb5850972016-05-03 16:34:59 -0400427 /** Vec pool of CLI sessions. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400428 unix_cli_file_t *cli_file_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429
Chris Lukeb5850972016-05-03 16:34:59 -0400430 /** Vec pool of unused session indices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400431 u32 *unused_cli_process_node_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700432
Chris Lukeb5850972016-05-03 16:34:59 -0400433 /** The session index of the stdin cli */
Chris Luke7afda3a2016-04-25 13:49:22 -0400434 u32 stdin_cli_file_index;
435
Chris Lukeb5850972016-05-03 16:34:59 -0400436 /** File pool index of current input. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437 u32 current_input_file_index;
438} unix_cli_main_t;
439
Chris Lukeb5850972016-05-03 16:34:59 -0400440/** CLI global state */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441static unix_cli_main_t unix_cli_main;
442
Chris Luke0aca5eb2016-04-25 13:48:54 -0400443/**
Chris Luke8e5b0412016-07-26 13:06:10 -0400444 * @brief Search for a byte sequence in the action list.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400445 *
Chris Lukeb5850972016-05-03 16:34:59 -0400446 * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with
447 * the bytes in @a input of maximum length @a ilen bytes.
448 * When a match is made @a *matched indicates how many bytes were matched.
449 * Returns a value from the enum @ref unix_cli_parse_action_t to indicate
450 * whether no match was found, a partial match was found or a complete
451 * match was found and what action, if any, should be taken.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400452 *
Chris Lukeb5850972016-05-03 16:34:59 -0400453 * @param[in] a Actions list to search within.
454 * @param[in] input String fragment to search for.
455 * @param[in] ilen Length of the string in 'input'.
456 * @param[out] matched Pointer to an integer that will contain the number
457 * of bytes matched when a complete match is found.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400458 *
Chris Lukeb5850972016-05-03 16:34:59 -0400459 * @return Action from @ref unix_cli_parse_action_t that the string fragment
Chris Luke0aca5eb2016-04-25 13:48:54 -0400460 * matches.
Chris Lukeb5850972016-05-03 16:34:59 -0400461 * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the
462 * whole input string matches the start of at least one action.
463 * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
Chris Luke0aca5eb2016-04-25 13:48:54 -0400464 * match at all.
465 */
466static unix_cli_parse_action_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400467unix_cli_match_action (unix_cli_parse_actions_t * a,
468 u8 * input, u32 ilen, i32 * matched)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400469{
Chris Luke0aca5eb2016-04-25 13:48:54 -0400470 u8 partial = 0;
471
472 while (a->input)
473 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400474 if (ilen >= a->len)
475 {
476 /* see if the start of the input buffer exactly matches the current
477 * action string. */
478 if (memcmp (input, a->input, a->len) == 0)
479 {
480 *matched = a->len;
481 return a->action;
482 }
483 }
484 else
485 {
486 /* if the first ilen characters match, flag this as a partial -
487 * meaning keep collecting bytes in case of a future match */
488 if (memcmp (input, a->input, ilen) == 0)
489 partial = 1;
490 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400491
Dave Barach9b8ffd92016-07-08 08:13:45 -0400492 /* check next action */
493 a++;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400494 }
495
496 return partial ?
Dave Barach9b8ffd92016-07-08 08:13:45 -0400497 UNIX_CLI_PARSE_ACTION_PARTIALMATCH : UNIX_CLI_PARSE_ACTION_NOMATCH;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400498}
499
500
Chris Luke54ccf222016-07-25 16:38:11 -0400501/** Add bytes to the output vector and then flagg the I/O system that bytes
502 * are available to be sent.
503 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504static void
505unix_cli_add_pending_output (unix_file_t * uf,
506 unix_cli_file_t * cf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400507 u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400509 unix_main_t *um = &unix_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510
511 vec_add (cf->output_vector, buffer, buffer_bytes);
512 if (vec_len (cf->output_vector) > 0)
513 {
514 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
515 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400516 if (!skip_update)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 um->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
518 }
519}
520
Chris Luke54ccf222016-07-25 16:38:11 -0400521/** Delete all bytes from the output vector and flag the I/O system
522 * that no more bytes are available to be sent.
523 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524static void
525unix_cli_del_pending_output (unix_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400526 unix_cli_file_t * cf, uword n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400528 unix_main_t *um = &unix_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
530 vec_delete (cf->output_vector, n_bytes, 0);
531 if (vec_len (cf->output_vector) <= 0)
532 {
533 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
534 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400535 if (!skip_update)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 um->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
537 }
538}
539
Chris Luke8e5b0412016-07-26 13:06:10 -0400540/** @brief A bit like strchr with a buffer length limit.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400541 * Search a buffer for the first instance of a character up to the limit of
542 * the buffer length. If found then return the position of that character.
543 *
544 * The key departure from strchr is that if the character is not found then
545 * return the buffer length.
546 *
547 * @param chr The byte value to search for.
548 * @param str The buffer in which to search for the value.
549 * @param len The depth into the buffer to search.
550 *
551 * @return The index of the first occurence of \c chr. If \c chr is not
552 * found then \c len instead.
553 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400554always_inline word
555unix_vlib_findchr (u8 chr, u8 * str, word len)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400556{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400557 word i = 0;
558 for (i = 0; i < len; i++, str++)
559 {
560 if (*str == chr)
561 return i;
562 }
563 return len;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400564}
565
Chris Luke8e5b0412016-07-26 13:06:10 -0400566/** @brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400567 * Attempts to write given buffer to the file descriptor of the given
568 * Unix CLI session. If that session already has data in the output buffer
569 * or if the write attempt tells us to try again later then the given buffer
570 * is appended to the pending output buffer instead.
571 *
572 * This is typically called only from \c unix_vlib_cli_output_cooked since
573 * that is where CRLF handling occurs or from places where we explicitly do
574 * not want cooked handling.
575 *
576 * @param cf Unix CLI session of the desired stream to write to.
577 * @param uf The Unix file structure of the desired stream to write to.
578 * @param buffer Pointer to the buffer that needs to be written.
579 * @param buffer_bytes The number of bytes from \c buffer to write.
580 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400581static void
582unix_vlib_cli_output_raw (unix_cli_file_t * cf,
583 unix_file_t * uf, u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400584{
585 int n = 0;
586
587 if (vec_len (cf->output_vector) == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400588 n = write (uf->file_descriptor, buffer, buffer_bytes);
Chris Luke0aca5eb2016-04-25 13:48:54 -0400589
590 if (n < 0 && errno != EAGAIN)
591 {
592 clib_unix_warning ("write");
593 }
594 else if ((word) n < (word) buffer_bytes)
595 {
596 /* We got EAGAIN or we already have stuff in the buffer;
597 * queue up whatever didn't get sent for later. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400598 if (n < 0)
599 n = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400600 unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
601 }
602}
603
Chris Luke8e5b0412016-07-26 13:06:10 -0400604/** @brief Process a buffer for CRLF handling before outputting it to the CLI.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400605 *
606 * @param cf Unix CLI session of the desired stream to write to.
607 * @param uf The Unix file structure of the desired stream to write to.
608 * @param buffer Pointer to the buffer that needs to be written.
609 * @param buffer_bytes The number of bytes from \c buffer to write.
610 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400611static void
612unix_vlib_cli_output_cooked (unix_cli_file_t * cf,
613 unix_file_t * uf,
614 u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400615{
616 word end = 0, start = 0;
617
618 while (end < buffer_bytes)
619 {
620 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400621 {
622 /* iterate the line on \n's so we can insert a \r before it */
623 end = unix_vlib_findchr ('\n',
624 buffer + start,
625 buffer_bytes - start) + start;
626 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400627 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400628 {
629 /* otherwise just send the whole buffer */
630 end = buffer_bytes;
631 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400632
Dave Barach9b8ffd92016-07-08 08:13:45 -0400633 unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start);
Chris Luke0aca5eb2016-04-25 13:48:54 -0400634
635 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400636 {
637 if (end < buffer_bytes)
638 {
639 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2);
640 end++; /* skip the \n that we already sent */
641 }
642 start = end;
643 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400644 }
645}
646
Chris Luke8e5b0412016-07-26 13:06:10 -0400647/** @brief Output the CLI prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400648static void
649unix_cli_cli_prompt (unix_cli_file_t * cf, unix_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400650{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400651 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke7afda3a2016-04-25 13:49:22 -0400652
653 unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt, vec_len (cm->cli_prompt));
654}
655
Chris Luke8e5b0412016-07-26 13:06:10 -0400656/** @brief Output a pager prompt and show number of buffered lines */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400657static void
658unix_cli_pager_prompt (unix_cli_file_t * cf, unix_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400659{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400660 u8 *prompt;
Chris Luke270b6de2016-05-19 14:23:25 -0400661 u32 h;
662
663 h = cf->pager_start + (cf->height - 1);
664 if (h > vec_len (cf->pager_index))
665 h = vec_len (cf->pager_index);
Chris Luke7afda3a2016-04-25 13:49:22 -0400666
Dave Barach9b8ffd92016-07-08 08:13:45 -0400667 prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s",
668 cf->ansi_capable ? ANSI_BOLD : "",
669 cf->pager_start + 1,
670 h,
671 vec_len (cf->pager_index),
672 cf->ansi_capable ? ANSI_RESET : "");
Chris Luke7afda3a2016-04-25 13:49:22 -0400673
Dave Barach9b8ffd92016-07-08 08:13:45 -0400674 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400675
Dave Barach9b8ffd92016-07-08 08:13:45 -0400676 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400677}
678
Chris Luke8e5b0412016-07-26 13:06:10 -0400679/** @brief Output a pager "skipping" message */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400680static void
681unix_cli_pager_message (unix_cli_file_t * cf, unix_file_t * uf,
682 char *message, char *postfix)
Chris Luke7afda3a2016-04-25 13:49:22 -0400683{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400684 u8 *prompt;
Chris Luke7afda3a2016-04-25 13:49:22 -0400685
Dave Barach9b8ffd92016-07-08 08:13:45 -0400686 prompt = format (0, "\r%s-- %s --%s%s",
687 cf->ansi_capable ? ANSI_BOLD : "",
688 message, cf->ansi_capable ? ANSI_RESET : "", postfix);
Chris Luke7afda3a2016-04-25 13:49:22 -0400689
Dave Barach9b8ffd92016-07-08 08:13:45 -0400690 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400691
Dave Barach9b8ffd92016-07-08 08:13:45 -0400692 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400693}
694
Chris Luke8e5b0412016-07-26 13:06:10 -0400695/** @brief Erase the printed pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400696static void
697unix_cli_pager_prompt_erase (unix_cli_file_t * cf, unix_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400698{
699 if (cf->ansi_capable)
700 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400701 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
702 unix_vlib_cli_output_cooked (cf, uf,
703 (u8 *) ANSI_CLEARLINE,
704 sizeof (ANSI_CLEARLINE) - 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400705 }
706 else
707 {
708 int i;
709
Dave Barach9b8ffd92016-07-08 08:13:45 -0400710 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
711 for (i = 0; i < cf->width - 1; i++)
712 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
713 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400714 }
715}
716
Chris Luke8e5b0412016-07-26 13:06:10 -0400717/** @brief Uses an ANSI escape sequence to move the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400718static void
719unix_cli_ansi_cursor (unix_cli_file_t * cf, unix_file_t * uf, u16 x, u16 y)
Chris Luke7afda3a2016-04-25 13:49:22 -0400720{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400721 u8 *str;
Chris Luke7afda3a2016-04-25 13:49:22 -0400722
Dave Barach9b8ffd92016-07-08 08:13:45 -0400723 str = format (0, "%s%d;%dH", CSI, y, x);
Chris Luke7afda3a2016-04-25 13:49:22 -0400724
Dave Barach9b8ffd92016-07-08 08:13:45 -0400725 unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str));
Chris Luke7afda3a2016-04-25 13:49:22 -0400726
Dave Barach9b8ffd92016-07-08 08:13:45 -0400727 vec_free (str);
Chris Luke7afda3a2016-04-25 13:49:22 -0400728}
729
Chris Luke862623d2016-05-14 10:13:34 -0400730/** Redraw the currently displayed page of text.
731 * @param cf CLI session to redraw the pager buffer of.
732 * @param uf Unix file of the CLI session.
733 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400734static void
735unix_cli_pager_redraw (unix_cli_file_t * cf, unix_file_t * uf)
Chris Luke862623d2016-05-14 10:13:34 -0400736{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400737 unix_cli_pager_index_t *pi = NULL;
738 u8 *line = NULL;
Chris Luke862623d2016-05-14 10:13:34 -0400739 word i;
740
741 /* No active pager? Do nothing. */
742 if (!vec_len (cf->pager_index))
743 return;
744
745 if (cf->ansi_capable)
746 {
747 /* If we have ANSI, send the clear screen sequence */
748 unix_vlib_cli_output_cooked (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400749 (u8 *) ANSI_CLEAR,
750 sizeof (ANSI_CLEAR) - 1);
Chris Luke862623d2016-05-14 10:13:34 -0400751 }
752 else
753 {
754 /* Otherwise make sure we're on a blank line */
755 unix_cli_pager_prompt_erase (cf, uf);
756 }
757
758 /* (Re-)send the current page of content */
759 for (i = 0; i < cf->height - 1 &&
Dave Barach9b8ffd92016-07-08 08:13:45 -0400760 i + cf->pager_start < vec_len (cf->pager_index); i++)
Chris Luke862623d2016-05-14 10:13:34 -0400761 {
762 pi = &cf->pager_index[cf->pager_start + i];
763 line = cf->pager_vector[pi->line] + pi->offset;
764
765 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
766 }
767 /* if the last line didn't end in newline, add a newline */
768 if (pi && line[pi->length - 1] != '\n')
Dave Barach9b8ffd92016-07-08 08:13:45 -0400769 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke862623d2016-05-14 10:13:34 -0400770
771 unix_cli_pager_prompt (cf, uf);
772}
773
774/** @brief Process and add a line to the pager index.
775 * In normal operation this function will take the given character string
776 * found in @c line and with length @c len_or_index and iterates the over the
777 * contents, adding each line of text discovered within it to the
778 * pager index. Lines are identified by newlines ("<code>\\n</code>") and by
779 * strings longer than the width of the terminal.
780 *
781 * If instead @c line is @c NULL then @c len_or_index is taken to mean the
782 * index of an existing line in the pager buffer; this simply means that the
783 * input line does not need to be cloned since we alreayd have it. This is
784 * typical if we are reindexing the pager buffer.
785 *
786 * @param cf The CLI session whose pager we are adding to.
787 * @param line The string of text to be indexed into the pager buffer.
788 * If @c line is @c NULL then the mode of operation
789 * changes slightly; see the description above.
790 * @param len_or_index If @c line is a pointer to a string then this parameter
791 * indicates the length of that string; Otherwise this
792 * value provides the index in the pager buffer of an
793 * existing string to be indexed.
794 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400795static void
796unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index)
Chris Luke862623d2016-05-14 10:13:34 -0400797{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400798 u8 *p;
Chris Luke862623d2016-05-14 10:13:34 -0400799 word i, j, k;
800 word line_index, len;
801 u32 width = cf->width;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400802 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -0400803
804 if (line == NULL)
805 {
806 /* Use a line already in the pager buffer */
807 line_index = len_or_index;
808 p = cf->pager_vector[line_index];
809 len = vec_len (p);
810 }
811 else
812 {
813 len = len_or_index;
814 /* Add a copy of the raw string to the pager buffer */
815 p = vec_new (u8, len);
816 clib_memcpy (p, line, len);
817
818 /* store in pager buffer */
819 line_index = vec_len (cf->pager_vector);
820 vec_add1 (cf->pager_vector, p);
821 }
822
823 i = 0;
824 while (i < len)
825 {
826 /* Find the next line, or run to terminal width, or run to EOL */
827 int l = len - i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400828 j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width);
Chris Luke862623d2016-05-14 10:13:34 -0400829
Dave Barach9b8ffd92016-07-08 08:13:45 -0400830 if (j < l && p[j] == '\n') /* incl \n */
831 j++;
Chris Luke862623d2016-05-14 10:13:34 -0400832
833 /* Add the line to the index */
834 k = vec_len (cf->pager_index);
835 vec_validate (cf->pager_index, k);
836 pi = &cf->pager_index[k];
837
838 pi->line = line_index;
839 pi->offset = i;
840 pi->length = j;
841
842 i += j;
843 p += j;
844 }
845}
846
847/** @brief Reindex entire pager buffer.
848 * Resets the current pager index and then re-adds the lines in the pager
849 * buffer to the index.
850 *
851 * Additionally this function attempts to retain the current page start
852 * line offset by searching for the same top-of-screen line in the new index.
853 *
854 * @param cf The CLI session whose pager buffer should be reindexed.
855 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400856static void
857unix_cli_pager_reindex (unix_cli_file_t * cf)
Chris Luke862623d2016-05-14 10:13:34 -0400858{
859 word i, old_line, old_offset;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400860 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -0400861
862 /* If there is nothing in the pager buffer then make sure the index
863 * is empty and move on.
864 */
865 if (cf->pager_vector == 0)
866 {
867 vec_reset_length (cf->pager_index);
868 return;
869 }
870
871 /* Retain a pointer to the current page start line so we can
872 * find it later
873 */
874 pi = &cf->pager_index[cf->pager_start];
875 old_line = pi->line;
876 old_offset = pi->offset;
877
878 /* Re-add the buffered lines to the index */
879 vec_reset_length (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400880 vec_foreach_index (i, cf->pager_vector)
881 {
882 unix_cli_pager_add_line (cf, NULL, i);
883 }
Chris Luke862623d2016-05-14 10:13:34 -0400884
885 /* Attempt to re-locate the previously stored page start line */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400886 vec_foreach_index (i, cf->pager_index)
887 {
888 pi = &cf->pager_index[i];
Chris Luke862623d2016-05-14 10:13:34 -0400889
Dave Barach9b8ffd92016-07-08 08:13:45 -0400890 if (pi->line == old_line &&
891 (pi->offset <= old_offset || pi->offset + pi->length > old_offset))
892 {
893 /* Found it! */
894 cf->pager_start = i;
895 break;
896 }
897 }
Chris Luke862623d2016-05-14 10:13:34 -0400898
899 /* In case the start line was not found (rare), ensure the pager start
900 * index is within bounds
901 */
902 if (cf->pager_start >= vec_len (cf->pager_index))
903 {
Chris Luke270b6de2016-05-19 14:23:25 -0400904 if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400905 cf->pager_start = 0;
Chris Luke270b6de2016-05-19 14:23:25 -0400906 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400907 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
Chris Luke862623d2016-05-14 10:13:34 -0400908 }
909}
910
911/** VLIB CLI output function.
912 *
913 * If the terminal has a pager configured then this function takes care
914 * of collating output into the pager buffer; ensuring only the first page
915 * is displayed and any lines in excess of the first page are buffered.
916 *
917 * If the maximum number of index lines in the buffer is exceeded then the
918 * pager is cancelled and the contents of the current buffer are sent to the
919 * terminal.
920 *
921 * If there is no pager configured then the output is sent directly to the
922 * terminal.
923 *
924 * @param cli_file_index Index of the CLI session where this output is
925 * directed.
926 * @param buffer String of printabe bytes to be output.
927 * @param buffer_bytes The number of bytes in @c buffer to be output.
928 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400929static void
930unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400932 unix_main_t *um = &unix_main;
933 unix_cli_main_t *cm = &unix_cli_main;
934 unix_cli_file_t *cf;
935 unix_file_t *uf;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400936
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
938 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939
Chris Luke7afda3a2016-04-25 13:49:22 -0400940 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
941 {
Chris Luke862623d2016-05-14 10:13:34 -0400942 unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes);
Chris Luke7afda3a2016-04-25 13:49:22 -0400943 }
944 else
945 {
Chris Luke862623d2016-05-14 10:13:34 -0400946 word row = vec_len (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400947 u8 *line;
948 unix_cli_pager_index_t *pi;
Chris Luke7afda3a2016-04-25 13:49:22 -0400949
Chris Luke862623d2016-05-14 10:13:34 -0400950 /* Index and add the output lines to the pager buffer. */
951 unix_cli_pager_add_line (cf, buffer, buffer_bytes);
952
953 /* Now iterate what was added to display the lines.
954 * If we reach the bottom of the page, display a prompt.
955 */
956 while (row < vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400957 {
958 if (row < cf->height - 1)
959 {
960 /* output this line */
961 pi = &cf->pager_index[row];
962 line = cf->pager_vector[pi->line] + pi->offset;
963 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
Chris Luke862623d2016-05-14 10:13:34 -0400964
Dave Barach9b8ffd92016-07-08 08:13:45 -0400965 /* if the last line didn't end in newline, and we're at the
966 * bottom of the page, add a newline */
967 if (line[pi->length - 1] != '\n' && row == cf->height - 2)
968 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
969 }
970 else
971 {
972 /* Display the pager prompt every 10 lines */
973 if (!(row % 10))
974 unix_cli_pager_prompt (cf, uf);
975 }
976 row++;
977 }
Chris Luke7afda3a2016-04-25 13:49:22 -0400978
Chris Luke862623d2016-05-14 10:13:34 -0400979 /* Check if we went over the pager buffer limit */
980 if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400981 {
982 /* Stop using the pager for the remainder of this CLI command */
983 cf->no_pager = 2;
Chris Luke7afda3a2016-04-25 13:49:22 -0400984
Dave Barach9b8ffd92016-07-08 08:13:45 -0400985 /* If we likely printed the prompt, erase it */
986 if (vec_len (cf->pager_index) > cf->height - 1)
987 unix_cli_pager_prompt_erase (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -0400988
Dave Barach9b8ffd92016-07-08 08:13:45 -0400989 /* Dump out the contents of the buffer */
990 for (row = cf->pager_start + (cf->height - 1);
991 row < vec_len (cf->pager_index); row++)
992 {
993 pi = &cf->pager_index[row];
994 line = cf->pager_vector[pi->line] + pi->offset;
995 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
996 }
Chris Luke7afda3a2016-04-25 13:49:22 -0400997
Dave Barach9b8ffd92016-07-08 08:13:45 -0400998 unix_cli_pager_reset (cf);
999 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001000 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001001}
1002
Chris Luke862623d2016-05-14 10:13:34 -04001003/** Identify whether a terminal type is ANSI capable.
1004 *
Chris Luke54ccf222016-07-25 16:38:11 -04001005 * Compares the string given in @c term with a list of terminal types known
Chris Luke862623d2016-05-14 10:13:34 -04001006 * to support ANSI escape sequences.
1007 *
1008 * This list contains, for example, @c xterm, @c screen and @c ansi.
1009 *
1010 * @param term A string with a terminal type in it.
Chris Luke54ccf222016-07-25 16:38:11 -04001011 * @param len The length of the string in @c term.
Chris Luke862623d2016-05-14 10:13:34 -04001012 *
1013 * @return @c 1 if the terminal type is recognized as supporting ANSI
1014 * terminal sequences; @c 0 otherwise.
1015 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001016static u8
1017unix_cli_terminal_type (u8 * term, uword len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001018{
Chris Luke0aca5eb2016-04-25 13:48:54 -04001019 /* This may later be better done as a hash of some sort. */
1020#define _(a) do { \
1021 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1022 } while(0)
1023
1024 _("xterm");
1025 _("xterm-color");
Dave Barach9b8ffd92016-07-08 08:13:45 -04001026 _("xterm-256color"); /* iTerm on Mac */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001027 _("screen");
Damjan Marion6e8ab162017-06-05 21:56:12 +02001028 _("screen-256color"); /* Screen and tmux */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001029 _("ansi"); /* Microsoft Telnet */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001030#undef _
1031
1032 return 0;
1033}
1034
Chris Luke8e5b0412016-07-26 13:06:10 -04001035/** @brief Emit initial welcome banner and prompt on a connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001036static void
1037unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001038{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001039 unix_main_t *um = &unix_main;
1040 unix_file_t *uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
Chris Luke572d8122016-04-25 13:49:07 -04001041 unix_cli_banner_t *banner;
1042 int i, len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001043
Chris Luke0aca5eb2016-04-25 13:48:54 -04001044 /*
1045 * Put the first bytes directly into the buffer so that further output is
1046 * queued until everything is ready. (oterwise initial prompt can appear
1047 * mid way through VPP initialization)
1048 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001049 unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
Chris Luke572d8122016-04-25 13:49:07 -04001050
1051 if (!um->cli_no_banner)
1052 {
1053 if (cf->ansi_capable)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001054 {
1055 banner = unix_cli_banner_color;
1056 len = ARRAY_LEN (unix_cli_banner_color);
1057 }
Chris Luke572d8122016-04-25 13:49:07 -04001058 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001059 {
1060 banner = unix_cli_banner;
1061 len = ARRAY_LEN (unix_cli_banner);
1062 }
Chris Luke572d8122016-04-25 13:49:07 -04001063
1064 for (i = 0; i < len; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001065 {
1066 unix_vlib_cli_output_cooked (cf, uf,
1067 banner[i].line, banner[i].length);
1068 }
1069 }
Chris Luke572d8122016-04-25 13:49:07 -04001070
1071 /* Prompt. */
Chris Luke7afda3a2016-04-25 13:49:22 -04001072 unix_cli_cli_prompt (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001073
1074 cf->started = 1;
1075}
1076
Chris Luke8e5b0412016-07-26 13:06:10 -04001077/** @brief A failsafe triggered on a timer to ensure we send the prompt
Chris Luke0aca5eb2016-04-25 13:48:54 -04001078 * to telnet sessions that fail to negotiate the terminal type. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001079static void
1080unix_cli_file_welcome_timer (any arg, f64 delay)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001081{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001082 unix_cli_main_t *cm = &unix_cli_main;
1083 unix_cli_file_t *cf;
1084 (void) delay;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001085
1086 /* Check the connection didn't close already */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001087 if (pool_is_free_index (cm->cli_file_pool, (uword) arg))
Chris Luke0aca5eb2016-04-25 13:48:54 -04001088 return;
1089
Dave Barach9b8ffd92016-07-08 08:13:45 -04001090 cf = pool_elt_at_index (cm->cli_file_pool, (uword) arg);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001091
1092 if (!cf->started)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001093 unix_cli_file_welcome (cm, cf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001094}
1095
Chris Luke8e5b0412016-07-26 13:06:10 -04001096/** @brief A mostly no-op Telnet state machine.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001097 * Process Telnet command bytes in a way that ensures we're mostly
1098 * transparent to the Telnet protocol. That is, it's mostly a no-op.
1099 *
1100 * @return -1 if we need more bytes, otherwise a positive integer number of
1101 * bytes to consume from the input_vector, not including the initial
1102 * IAC byte.
1103 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001104static i32
1105unix_cli_process_telnet (unix_main_t * um,
1106 unix_cli_file_t * cf,
1107 unix_file_t * uf, u8 * input_vector, uword len)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001108{
1109 /* Input_vector starts at IAC byte.
1110 * See if we have a complete message; if not, return -1 so we wait for more.
1111 * if we have a complete message, consume those bytes from the vector.
1112 */
1113 i32 consume = 0;
1114
1115 if (len == 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001116 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001117
1118 switch (input_vector[1])
Ed Warnickecb9cada2015-12-08 15:45:58 -07001119 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001120 case IAC:
1121 /* two IAC's in a row means to pass through 0xff.
1122 * since that makes no sense here, just consume it.
1123 */
1124 consume = 1;
1125 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001126
Dave Barach9b8ffd92016-07-08 08:13:45 -04001127 case WILL:
1128 case WONT:
1129 case DO:
1130 case DONT:
1131 /* Expect 3 bytes */
1132 if (vec_len (input_vector) < 3)
1133 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001134
Dave Barach9b8ffd92016-07-08 08:13:45 -04001135 consume = 2;
1136 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001137
Dave Barach9b8ffd92016-07-08 08:13:45 -04001138 case SB:
1139 {
1140 /* Sub option - search ahead for IAC SE to end it */
1141 i32 i;
1142 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
1143 {
1144 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
1145 {
1146 /* We have a complete message; see if we care about it */
1147 switch (input_vector[2])
1148 {
1149 case TELOPT_TTYPE:
1150 if (input_vector[3] != 0)
1151 break;
1152 /* See if the terminal type is ANSI capable */
1153 cf->ansi_capable =
1154 unix_cli_terminal_type (input_vector + 4, i - 5);
1155 /* If session not started, we can release the pause */
1156 if (!cf->started)
1157 /* Send the welcome banner and initial prompt */
1158 unix_cli_file_welcome (&unix_cli_main, cf);
1159 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160
Dave Barach9b8ffd92016-07-08 08:13:45 -04001161 case TELOPT_NAWS:
1162 /* Window size */
1163 if (i != 8) /* check message is correct size */
1164 break;
1165 cf->width =
1166 clib_net_to_host_u16 (*((u16 *) (input_vector + 3)));
1167 cf->height =
1168 clib_net_to_host_u16 (*((u16 *) (input_vector + 5)));
1169 /* reindex pager buffer */
1170 unix_cli_pager_reindex (cf);
1171 /* redraw page */
1172 unix_cli_pager_redraw (cf, uf);
1173 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04001174
Dave Barach9b8ffd92016-07-08 08:13:45 -04001175 default:
1176 break;
1177 }
1178 /* Consume it all */
1179 consume = i;
1180 break;
1181 }
1182 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183
Dave Barach9b8ffd92016-07-08 08:13:45 -04001184 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
1185 consume = 1; /* hit max search depth, advance one byte */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001186
Dave Barach9b8ffd92016-07-08 08:13:45 -04001187 if (consume == 0)
1188 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001189
Dave Barach9b8ffd92016-07-08 08:13:45 -04001190 break;
1191 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192
Dave Barach9b8ffd92016-07-08 08:13:45 -04001193 case GA:
1194 case EL:
1195 case EC:
1196 case AO:
1197 case IP:
1198 case BREAK:
1199 case DM:
1200 case NOP:
1201 case SE:
1202 case EOR:
1203 case ABORT:
1204 case SUSP:
1205 case xEOF:
1206 /* Simple one-byte messages */
1207 consume = 1;
1208 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209
Dave Barach9b8ffd92016-07-08 08:13:45 -04001210 case AYT:
1211 /* Are You There - trigger a visible response */
1212 consume = 1;
1213 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
1214 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001215
Dave Barach9b8ffd92016-07-08 08:13:45 -04001216 default:
1217 /* Unknown command! Eat the IAC byte */
1218 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001219 }
1220
Dave Barach9b8ffd92016-07-08 08:13:45 -04001221 return consume;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001222}
1223
Chris Luke8e5b0412016-07-26 13:06:10 -04001224/** @brief Process actionable input.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001225 * Based on the \c action process the input; this typically involves
1226 * searching the command history or editing the current command line.
1227 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001228static int
1229unix_cli_line_process_one (unix_cli_main_t * cm,
1230 unix_main_t * um,
1231 unix_cli_file_t * cf,
1232 unix_file_t * uf,
1233 u8 input, unix_cli_parse_action_t action)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001234{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001235 u8 *prev;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001236 u8 *save = 0;
1237 u8 **possible_commands;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001238 int j, delta;
1239
1240 switch (action)
1241 {
1242 case UNIX_CLI_PARSE_ACTION_NOACTION:
1243 break;
1244
Chris Luke0aca5eb2016-04-25 13:48:54 -04001245 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
1246 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
Chris Luke7afda3a2016-04-25 13:49:22 -04001247 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001248 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001249 if (cf->search_mode == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001250 {
1251 /* Erase the current command (if any) */
1252 for (j = 0; j < (vec_len (cf->current_command)); j++)
1253 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001254
Dave Barach9b8ffd92016-07-08 08:13:45 -04001255 vec_reset_length (cf->search_key);
1256 vec_reset_length (cf->current_command);
1257 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1258 cf->search_mode = -1;
1259 else
1260 cf->search_mode = 1;
1261 cf->cursor = 0;
1262 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001263 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001264 {
1265 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1266 cf->search_mode = -1;
1267 else
1268 cf->search_mode = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001269
Dave Barach9b8ffd92016-07-08 08:13:45 -04001270 cf->excursion += cf->search_mode;
1271 goto search_again;
1272 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001273 break;
1274
1275 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
1276 /* Erase the command from the cursor to the start */
1277
1278 /* Shimmy forwards to the new end of line position */
1279 delta = vec_len (cf->current_command) - cf->cursor;
1280 for (j = cf->cursor; j > delta; j--)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001281 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001282 /* Zap from here to the end of what is currently displayed */
1283 for (; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001284 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001285 /* Get back to the start of the line */
1286 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001287 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001288
Dave Barach9b8ffd92016-07-08 08:13:45 -04001289 j = vec_len (cf->current_command) - cf->cursor;
1290 memmove (cf->current_command, cf->current_command + cf->cursor, j);
1291 _vec_len (cf->current_command) = j;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001292
1293 /* Print the new contents */
1294 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j);
1295 /* Shimmy back to the start */
1296 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001297 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001298 cf->cursor = 0;
1299
1300 cf->search_mode = 0;
1301 break;
1302
1303 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
1304 /* Erase the command from the cursor to the end */
1305
1306 /* Zap from cursor to end of what is currently displayed */
1307 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001308 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001309 /* Get back to where we were */
1310 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001311 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001312
1313 /* Truncate the line at the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001314 _vec_len (cf->current_command) = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001315
1316 cf->search_mode = 0;
1317 break;
1318
1319 case UNIX_CLI_PARSE_ACTION_LEFT:
1320 if (cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001321 {
1322 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1323 cf->cursor--;
1324 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001325
1326 cf->search_mode = 0;
1327 break;
1328
1329 case UNIX_CLI_PARSE_ACTION_RIGHT:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001330 if (cf->cursor < vec_len (cf->current_command))
1331 {
1332 /* have to emit the character under the cursor */
1333 unix_vlib_cli_output_cooked (cf, uf,
1334 cf->current_command + cf->cursor, 1);
1335 cf->cursor++;
1336 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001337
1338 cf->search_mode = 0;
1339 break;
1340
1341 case UNIX_CLI_PARSE_ACTION_UP:
1342 case UNIX_CLI_PARSE_ACTION_DOWN:
Chris Luke7afda3a2016-04-25 13:49:22 -04001343 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001344 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001345 cf->search_mode = 0;
1346 /* Erase the command */
1347 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001348 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001349 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001350 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001351 vec_reset_length (cf->current_command);
1352 if (vec_len (cf->command_history))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001353 {
1354 if (action == UNIX_CLI_PARSE_ACTION_UP)
1355 delta = -1;
1356 else
1357 delta = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001358
Dave Barach9b8ffd92016-07-08 08:13:45 -04001359 cf->excursion += delta;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001360
Dave Barach9b8ffd92016-07-08 08:13:45 -04001361 if (cf->excursion == vec_len (cf->command_history))
1362 {
1363 /* down-arrowed to last entry - want a blank line */
1364 _vec_len (cf->current_command) = 0;
1365 }
1366 else if (cf->excursion < 0)
1367 {
1368 /* up-arrowed over the start to the end, want a blank line */
1369 cf->excursion = vec_len (cf->command_history);
1370 _vec_len (cf->current_command) = 0;
1371 }
1372 else
1373 {
1374 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1375 /* down-arrowed past end - wrap to start */
1376 cf->excursion = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001377
Dave Barach9b8ffd92016-07-08 08:13:45 -04001378 /* Print the command at the current position */
1379 prev = cf->command_history[cf->excursion];
1380 vec_validate (cf->current_command, vec_len (prev) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001381
Dave Barach9b8ffd92016-07-08 08:13:45 -04001382 clib_memcpy (cf->current_command, prev, vec_len (prev));
1383 _vec_len (cf->current_command) = vec_len (prev);
1384 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1385 vec_len (cf->current_command));
1386 }
1387 cf->cursor = vec_len (cf->current_command);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001388
Dave Barach9b8ffd92016-07-08 08:13:45 -04001389 break;
1390 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001391 break;
1392
1393 case UNIX_CLI_PARSE_ACTION_HOME:
1394 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001395 {
1396 while (cf->cursor)
1397 {
1398 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1399 cf->cursor--;
1400 }
1401 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001402
Chris Luke0aca5eb2016-04-25 13:48:54 -04001403 cf->search_mode = 0;
1404 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001405
Chris Luke0aca5eb2016-04-25 13:48:54 -04001406 case UNIX_CLI_PARSE_ACTION_END:
1407 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001408 cf->cursor < vec_len (cf->current_command))
1409 {
1410 unix_vlib_cli_output_cooked (cf, uf,
1411 cf->current_command + cf->cursor,
1412 vec_len (cf->current_command) -
1413 cf->cursor);
1414 cf->cursor = vec_len (cf->current_command);
1415 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001416
1417 cf->search_mode = 0;
1418 break;
1419
1420 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1421 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001422 {
1423 j = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001424
Dave Barach9b8ffd92016-07-08 08:13:45 -04001425 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1426 j--;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001427
Dave Barach9b8ffd92016-07-08 08:13:45 -04001428 while (j && isspace (cf->current_command[j]))
1429 {
1430 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1431 j--;
1432 }
1433 while (j && !isspace (cf->current_command[j]))
1434 {
1435 if (isspace (cf->current_command[j - 1]))
1436 break;
1437 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1438 j--;
1439 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001440
Dave Barach9b8ffd92016-07-08 08:13:45 -04001441 cf->cursor = j;
1442 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001443
1444 cf->search_mode = 0;
1445 break;
1446
1447 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1448 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001449 cf->cursor < vec_len (cf->current_command))
1450 {
1451 int e = vec_len (cf->current_command);
1452 j = cf->cursor;
1453 while (j < e && !isspace (cf->current_command[j]))
1454 j++;
1455 while (j < e && isspace (cf->current_command[j]))
1456 j++;
1457 unix_vlib_cli_output_cooked (cf, uf,
1458 cf->current_command + cf->cursor,
1459 j - cf->cursor);
1460 cf->cursor = j;
1461 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001462
1463 cf->search_mode = 0;
1464 break;
1465
1466
1467 case UNIX_CLI_PARSE_ACTION_ERASE:
1468 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001469 {
1470 if (cf->cursor == vec_len (cf->current_command))
1471 {
1472 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1473 _vec_len (cf->current_command)--;
1474 cf->cursor--;
1475 }
1476 else if (cf->cursor > 0)
1477 {
1478 /* shift everything at & to the right of the cursor left by 1 */
1479 j = vec_len (cf->current_command) - cf->cursor;
1480 memmove (cf->current_command + cf->cursor - 1,
1481 cf->current_command + cf->cursor, j);
1482 _vec_len (cf->current_command)--;
1483 cf->cursor--;
1484 /* redraw the rest of the line */
1485 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1486 unix_vlib_cli_output_cooked (cf, uf,
1487 cf->current_command + cf->cursor,
1488 j);
1489 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
1490 /* and shift the terminal cursor back where it should be */
1491 while (--j)
1492 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1493 }
1494 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001495 cf->search_mode = 0;
1496 cf->excursion = 0;
1497 vec_reset_length (cf->search_key);
1498 break;
1499
1500 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1501 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001502 {
1503 if (cf->cursor < vec_len (cf->current_command))
1504 {
1505 /* shift everything to the right of the cursor left by 1 */
1506 j = vec_len (cf->current_command) - cf->cursor - 1;
1507 memmove (cf->current_command + cf->cursor,
1508 cf->current_command + cf->cursor + 1, j);
1509 _vec_len (cf->current_command)--;
1510 /* redraw the rest of the line */
1511 unix_vlib_cli_output_cooked (cf, uf,
1512 cf->current_command + cf->cursor,
1513 j);
1514 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
1515 /* and shift the terminal cursor back where it should be */
1516 if (j)
1517 {
1518 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1519 while (--j)
1520 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1521 }
1522 }
1523 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001524 else if (input == 'D' - '@')
Dave Barach9b8ffd92016-07-08 08:13:45 -04001525 {
1526 /* ^D with no command entered = quit */
1527 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1528 vlib_process_signal_event (um->vlib_main,
1529 vlib_current_process (um->vlib_main),
1530 UNIX_CLI_PROCESS_EVENT_QUIT,
1531 cf - cm->cli_file_pool);
1532 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001533 cf->search_mode = 0;
1534 cf->excursion = 0;
1535 vec_reset_length (cf->search_key);
1536 break;
1537
1538 case UNIX_CLI_PARSE_ACTION_CLEAR:
1539 /* If we're in ANSI mode, clear the screen.
1540 * Then redraw the prompt and any existing command input, then put
1541 * the cursor back where it was in that line.
1542 */
1543 if (cf->ansi_capable)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001544 unix_vlib_cli_output_cooked (cf, uf,
1545 (u8 *) ANSI_CLEAR,
1546 sizeof (ANSI_CLEAR) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001547 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001548 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001549
1550 unix_vlib_cli_output_raw (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001551 cm->cli_prompt, vec_len (cm->cli_prompt));
Chris Luke0aca5eb2016-04-25 13:48:54 -04001552 unix_vlib_cli_output_raw (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001553 cf->current_command,
1554 vec_len (cf->current_command));
1555 for (j = cf->cursor; j < vec_len (cf->current_command); j++)
1556 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001557
1558 break;
1559
Chris Luke7afda3a2016-04-25 13:49:22 -04001560 case UNIX_CLI_PARSE_ACTION_TAB:
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001561 if (cf->cursor < vec_len (cf->current_command))
1562 {
1563 /* if we are in the middle of a line, complete only if
1564 * the cursor points to whitespace */
1565 if (isspace (cf->current_command[cf->cursor]))
1566 {
1567 /* save and clear any input that is after the cursor */
1568 vec_resize (save, vec_len (cf->current_command) - cf->cursor);
1569 clib_memcpy (save, cf->current_command + cf->cursor,
1570 vec_len (cf->current_command) - cf->cursor);
1571 _vec_len (cf->current_command) = cf->cursor;
1572 }
1573 else
1574 {
1575 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1576 break;
1577 }
1578 }
1579 possible_commands =
1580 vlib_cli_get_possible_completions (cf->current_command);
1581 if (vec_len (possible_commands) == 1)
1582 {
1583 u32 j = cf->cursor;
1584 u8 *completed = possible_commands[0];
1585
1586 /* find the last word of current_command */
1587 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1588 {
1589 j--;
1590 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1591 }
1592 _vec_len (cf->current_command) = j;
1593
1594 /* replace it with the newly expanded command */
1595 vec_append (cf->current_command, completed);
1596
1597 /* echo to the terminal */
1598 unix_vlib_cli_output_raw (cf, uf, completed, vec_len (completed));
1599
1600 /* add one trailing space if needed */
1601 if (vec_len (save) == 0)
1602 {
1603 vec_add1 (cf->current_command, ' ');
1604 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1605 }
1606
1607 cf->cursor = vec_len (cf->current_command);
1608
1609 }
1610 else if (vec_len (possible_commands) >= 2)
1611 {
1612 u8 **possible_command;
1613 uword max_command_len = 0, min_command_len = ~0;
1614 u32 i, j;
1615
1616 vec_foreach (possible_command, possible_commands)
1617 {
1618 if (vec_len (*possible_command) > max_command_len)
1619 {
1620 max_command_len = vec_len (*possible_command);
1621 }
1622 if (vec_len (*possible_command) < min_command_len)
1623 {
1624 min_command_len = vec_len (*possible_command);
1625 }
1626 }
1627
1628 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1629
1630 i = 0;
1631 vec_foreach (possible_command, possible_commands)
1632 {
1633 if (i + max_command_len >= cf->width)
1634 {
1635 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1636 i = 0;
1637 }
1638 unix_vlib_cli_output_raw (cf, uf, *possible_command,
1639 vec_len (*possible_command));
1640 for (j = vec_len (*possible_command); j < max_command_len + 2;
1641 j++)
1642 {
1643 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1644 }
1645 i += max_command_len + 2;
1646 }
1647
1648 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1649
1650 /* rewrite prompt */
1651 unix_cli_cli_prompt (cf, uf);
1652 unix_vlib_cli_output_raw (cf, uf, cf->current_command,
1653 vec_len (cf->current_command));
1654
1655 /* count length of last word */
1656 j = cf->cursor;
1657 i = 0;
1658 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1659 {
1660 j--;
1661 i++;
1662 }
1663
1664 /* determine smallest common command */
1665 for (; i < min_command_len; i++)
1666 {
1667 u8 common = '\0';
1668 int stop = 0;
1669 vec_foreach (possible_command, possible_commands)
1670 {
1671 if (common == '\0')
1672 {
1673 common = (*possible_command)[i];
1674 }
1675 else if (common != (*possible_command)[i])
1676 {
1677 stop = 1;
1678 break;
1679 }
1680 }
1681 if (!stop)
1682 {
1683 vec_add1 (cf->current_command, common);
1684 cf->cursor++;
1685 unix_vlib_cli_output_raw (cf, uf, (u8 *) & common, 1);
1686 }
1687 else
1688 {
1689 break;
1690 }
1691 }
1692 }
1693 else
1694 {
1695 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1696 }
1697
1698 if (vec_len (save) > 0)
1699 {
1700 /* restore remaining input if tab was hit in the middle of a line */
1701 unix_vlib_cli_output_raw (cf, uf, save, vec_len (save));
1702 for (j = 0; j < vec_len (save); j++)
1703 {
1704 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1705 }
1706 vec_append (cf->current_command, save);
1707 vec_free (save);
1708 }
1709 vec_free (possible_commands);
1710
1711 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04001712 case UNIX_CLI_PARSE_ACTION_YANK:
1713 /* TODO */
1714 break;
1715
1716
1717 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
1718 pager_quit:
1719 unix_cli_pager_prompt_erase (cf, uf);
1720 unix_cli_pager_reset (cf);
1721 unix_cli_cli_prompt (cf, uf);
1722 break;
1723
1724 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
1725 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
1726 /* show next page of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001727 if (cf->height + cf->pager_start < vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001728 {
1729 u8 *line = NULL;
1730 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001731
Dave Barach9b8ffd92016-07-08 08:13:45 -04001732 int m = cf->pager_start + (cf->height - 1);
1733 unix_cli_pager_prompt_erase (cf, uf);
1734 for (j = m;
1735 j < vec_len (cf->pager_index) && cf->pager_start < m;
1736 j++, cf->pager_start++)
1737 {
1738 pi = &cf->pager_index[j];
1739 line = cf->pager_vector[pi->line] + pi->offset;
1740 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1741 }
1742 /* if the last line didn't end in newline, add a newline */
1743 if (pi && line[pi->length - 1] != '\n')
1744 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1745 unix_cli_pager_prompt (cf, uf);
1746 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001747 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001748 {
1749 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
1750 /* no more in buffer, exit, but only if it was <space> */
1751 goto pager_quit;
1752 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001753 break;
1754
1755 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
1756 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
1757 /* display the next line of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001758 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001759 {
1760 u8 *line;
1761 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -04001762
Dave Barach9b8ffd92016-07-08 08:13:45 -04001763 unix_cli_pager_prompt_erase (cf, uf);
1764 pi = &cf->pager_index[cf->pager_start + (cf->height - 1)];
1765 line = cf->pager_vector[pi->line] + pi->offset;
1766 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1767 cf->pager_start++;
1768 /* if the last line didn't end in newline, add a newline */
1769 if (line[pi->length - 1] != '\n')
1770 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1771 unix_cli_pager_prompt (cf, uf);
1772 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001773 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001774 {
1775 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
1776 /* no more in buffer, exit, but only if it was <enter> */
1777 goto pager_quit;
1778 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001779
1780 break;
1781
1782 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
1783 /* scroll the page back one line */
1784 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001785 {
1786 u8 *line = NULL;
1787 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001788
Dave Barach9b8ffd92016-07-08 08:13:45 -04001789 cf->pager_start--;
1790 if (cf->ansi_capable)
1791 {
1792 pi = &cf->pager_index[cf->pager_start];
1793 line = cf->pager_vector[pi->line] + pi->offset;
1794 unix_cli_pager_prompt_erase (cf, uf);
1795 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN,
1796 sizeof (ANSI_SCROLLDN) - 1);
1797 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR,
1798 sizeof (ANSI_SAVECURSOR) - 1);
1799 unix_cli_ansi_cursor (cf, uf, 1, 1);
1800 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE,
1801 sizeof (ANSI_CLEARLINE) - 1);
1802 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1803 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR,
1804 sizeof (ANSI_RESTCURSOR) - 1);
1805 unix_cli_pager_prompt_erase (cf, uf);
1806 unix_cli_pager_prompt (cf, uf);
1807 }
1808 else
1809 {
1810 int m = cf->pager_start + (cf->height - 1);
1811 unix_cli_pager_prompt_erase (cf, uf);
1812 for (j = cf->pager_start;
1813 j < vec_len (cf->pager_index) && j < m; j++)
1814 {
1815 pi = &cf->pager_index[j];
1816 line = cf->pager_vector[pi->line] + pi->offset;
1817 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1818 }
1819 /* if the last line didn't end in newline, add a newline */
1820 if (pi && line[pi->length - 1] != '\n')
1821 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1822 unix_cli_pager_prompt (cf, uf);
1823 }
1824 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001825 break;
1826
1827 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
1828 /* back to the first page of the buffer */
1829 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001830 {
1831 u8 *line = NULL;
1832 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001833
Dave Barach9b8ffd92016-07-08 08:13:45 -04001834 cf->pager_start = 0;
1835 int m = cf->pager_start + (cf->height - 1);
1836 unix_cli_pager_prompt_erase (cf, uf);
1837 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1838 j++)
1839 {
1840 pi = &cf->pager_index[j];
1841 line = cf->pager_vector[pi->line] + pi->offset;
1842 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1843 }
1844 /* if the last line didn't end in newline, add a newline */
1845 if (pi && line[pi->length - 1] != '\n')
1846 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1847 unix_cli_pager_prompt (cf, uf);
1848 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001849 break;
1850
1851 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
1852 /* skip to the last page of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001853 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001854 {
1855 u8 *line = NULL;
1856 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001857
Dave Barach9b8ffd92016-07-08 08:13:45 -04001858 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
1859 unix_cli_pager_prompt_erase (cf, uf);
1860 unix_cli_pager_message (cf, uf, "skipping", "\n");
1861 for (j = cf->pager_start; j < vec_len (cf->pager_index); j++)
1862 {
1863 pi = &cf->pager_index[j];
1864 line = cf->pager_vector[pi->line] + pi->offset;
1865 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1866 }
1867 /* if the last line didn't end in newline, add a newline */
1868 if (pi && line[pi->length - 1] != '\n')
1869 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1870 unix_cli_pager_prompt (cf, uf);
1871 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001872 break;
1873
1874 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
1875 /* wander back one page in the buffer */
1876 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001877 {
1878 u8 *line = NULL;
1879 unix_cli_pager_index_t *pi = NULL;
1880 int m;
Chris Luke862623d2016-05-14 10:13:34 -04001881
Dave Barach9b8ffd92016-07-08 08:13:45 -04001882 if (cf->pager_start >= cf->height)
1883 cf->pager_start -= cf->height - 1;
1884 else
1885 cf->pager_start = 0;
1886 m = cf->pager_start + cf->height - 1;
1887 unix_cli_pager_prompt_erase (cf, uf);
1888 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1889 j++)
1890 {
1891 pi = &cf->pager_index[j];
1892 line = cf->pager_vector[pi->line] + pi->offset;
1893 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1894 }
1895 /* if the last line didn't end in newline, add a newline */
1896 if (pi && line[pi->length - 1] != '\n')
1897 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1898 unix_cli_pager_prompt (cf, uf);
1899 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001900 break;
1901
Chris Luke862623d2016-05-14 10:13:34 -04001902 case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW:
1903 /* Redraw the current pager screen */
1904 unix_cli_pager_redraw (cf, uf);
1905 break;
1906
Chris Luke7afda3a2016-04-25 13:49:22 -04001907 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
1908 /* search forwards in the buffer */
1909 break;
1910
1911
Chris Luke0aca5eb2016-04-25 13:48:54 -04001912 case UNIX_CLI_PARSE_ACTION_CRLF:
1913 crlf:
Chris Luke0aca5eb2016-04-25 13:48:54 -04001914 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1915
Chris Luke7afda3a2016-04-25 13:49:22 -04001916 if (cf->has_history && cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001917 {
1918 if (cf->command_history
1919 && vec_len (cf->command_history) >= cf->history_limit)
1920 {
1921 vec_free (cf->command_history[0]);
1922 vec_delete (cf->command_history, 1, 0);
1923 }
1924 /* Don't add blank lines to the cmd history */
1925 if (vec_len (cf->current_command))
1926 {
1927 /* Don't duplicate the previous command */
1928 j = vec_len (cf->command_history);
1929 if (j == 0 ||
1930 (vec_len (cf->current_command) !=
1931 vec_len (cf->command_history[j - 1])
1932 || memcmp (cf->current_command, cf->command_history[j - 1],
1933 vec_len (cf->current_command)) != 0))
1934 {
1935 /* copy the command to the history */
1936 u8 *c = 0;
1937 vec_append (c, cf->current_command);
1938 vec_add1 (cf->command_history, c);
1939 cf->command_number++;
1940 }
1941 }
1942 cf->excursion = vec_len (cf->command_history);
1943 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04001944
Chris Luke0aca5eb2016-04-25 13:48:54 -04001945 cf->search_mode = 0;
1946 vec_reset_length (cf->search_key);
1947 cf->cursor = 0;
1948
1949 return 0;
1950
Chris Luke7afda3a2016-04-25 13:49:22 -04001951 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
1952 case UNIX_CLI_PARSE_ACTION_NOMATCH:
Chris Luke862623d2016-05-14 10:13:34 -04001953 if (vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001954 {
1955 /* no-op for now */
1956 }
1957 else if (cf->has_history && cf->search_mode && isprint (input))
1958 {
1959 int k, limit, offset;
1960 u8 *item;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001961
Dave Barach9b8ffd92016-07-08 08:13:45 -04001962 vec_add1 (cf->search_key, input);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001963
Dave Barach9b8ffd92016-07-08 08:13:45 -04001964 search_again:
1965 for (j = 0; j < vec_len (cf->command_history); j++)
1966 {
1967 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1968 cf->excursion = 0;
1969 else if (cf->excursion < 0)
1970 cf->excursion = vec_len (cf->command_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001971
Dave Barach9b8ffd92016-07-08 08:13:45 -04001972 item = cf->command_history[cf->excursion];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001973
Dave Barach9b8ffd92016-07-08 08:13:45 -04001974 limit = (vec_len (cf->search_key) > vec_len (item)) ?
1975 vec_len (item) : vec_len (cf->search_key);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001976
Dave Barach9b8ffd92016-07-08 08:13:45 -04001977 for (offset = 0; offset <= vec_len (item) - limit; offset++)
1978 {
1979 for (k = 0; k < limit; k++)
1980 {
1981 if (item[k + offset] != cf->search_key[k])
1982 goto next_offset;
1983 }
1984 goto found_at_offset;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001985
Dave Barach9b8ffd92016-07-08 08:13:45 -04001986 next_offset:
1987 ;
1988 }
1989 goto next;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001990
Dave Barach9b8ffd92016-07-08 08:13:45 -04001991 found_at_offset:
1992 for (j = 0; j < vec_len (cf->current_command); j++)
1993 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001994
Dave Barach9b8ffd92016-07-08 08:13:45 -04001995 vec_validate (cf->current_command, vec_len (item) - 1);
1996 clib_memcpy (cf->current_command, item, vec_len (item));
1997 _vec_len (cf->current_command) = vec_len (item);
Chris Luke93dcd1d2016-05-10 10:45:10 -04001998
Dave Barach9b8ffd92016-07-08 08:13:45 -04001999 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
2000 vec_len (cf->current_command));
2001 cf->cursor = vec_len (cf->current_command);
2002 goto found;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002003
Dave Barach9b8ffd92016-07-08 08:13:45 -04002004 next:
2005 cf->excursion += cf->search_mode;
2006 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002007
Dave Barach9b8ffd92016-07-08 08:13:45 -04002008 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12);
2009 vec_reset_length (cf->search_key);
2010 vec_reset_length (cf->current_command);
2011 cf->search_mode = 0;
2012 cf->cursor = 0;
2013 goto crlf;
2014 }
2015 else if (isprint (input)) /* skip any errant control codes */
2016 {
2017 if (cf->cursor == vec_len (cf->current_command))
2018 {
2019 /* Append to end */
2020 vec_add1 (cf->current_command, input);
2021 cf->cursor++;
Chris Luke7afda3a2016-04-25 13:49:22 -04002022
Dave Barach9b8ffd92016-07-08 08:13:45 -04002023 /* Echo the character back to the client */
2024 unix_vlib_cli_output_raw (cf, uf, &input, 1);
2025 }
2026 else
2027 {
2028 /* Insert at cursor: resize +1 byte, move everything over */
2029 j = vec_len (cf->current_command) - cf->cursor;
2030 vec_add1 (cf->current_command, (u8) 'A');
2031 memmove (cf->current_command + cf->cursor + 1,
2032 cf->current_command + cf->cursor, j);
2033 cf->current_command[cf->cursor] = input;
2034 /* Redraw the line */
2035 j++;
2036 unix_vlib_cli_output_raw (cf, uf,
2037 cf->current_command + cf->cursor, j);
2038 /* Put terminal cursor back */
2039 while (--j)
2040 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
2041 cf->cursor++;
2042 }
2043 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002044 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002045 {
2046 /* no-op - not printable or otherwise not actionable */
2047 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002048
2049 found:
2050
2051 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04002052
2053 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2054 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002055 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04002056 return 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002057}
2058
Chris Luke8e5b0412016-07-26 13:06:10 -04002059/** @brief Process input bytes on a stream to provide line editing and
Chris Luke0aca5eb2016-04-25 13:48:54 -04002060 * command history in the CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002061static int
2062unix_cli_line_edit (unix_cli_main_t * cm,
2063 unix_main_t * um, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04002064{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002065 unix_file_t *uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002066 int i;
2067
2068 for (i = 0; i < vec_len (cf->input_vector); i++)
2069 {
2070 unix_cli_parse_action_t action;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002071 i32 matched = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04002072 unix_cli_parse_actions_t *a;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002073
Chris Luke7afda3a2016-04-25 13:49:22 -04002074 /* If we're in the pager mode, search the pager actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002075 a =
2076 vec_len (cf->pager_index) ? unix_cli_parse_pager :
2077 unix_cli_parse_strings;
Chris Luke7afda3a2016-04-25 13:49:22 -04002078
Chris Luke93dcd1d2016-05-10 10:45:10 -04002079 /* See if the input buffer is some sort of control code */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002080 action = unix_cli_match_action (a, &cf->input_vector[i],
2081 vec_len (cf->input_vector) - i,
2082 &matched);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002083
2084 switch (action)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002085 {
2086 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2087 if (i)
2088 {
2089 /* There was a partial match which means we need more bytes
2090 * than the input buffer currently has.
2091 * Since the bytes before here have been processed, shift
2092 * the remaining contents to the start of the input buffer.
2093 */
2094 vec_delete (cf->input_vector, i, 0);
2095 }
2096 return 1; /* wait for more */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002097
Dave Barach9b8ffd92016-07-08 08:13:45 -04002098 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2099 /* process telnet options */
2100 matched = unix_cli_process_telnet (um, cf, uf,
2101 cf->input_vector + i,
2102 vec_len (cf->input_vector) - i);
2103 if (matched < 0)
2104 {
2105 if (i)
2106 {
2107 /* There was a partial match which means we need more bytes
2108 * than the input buffer currently has.
2109 * Since the bytes before here have been processed, shift
2110 * the remaining contents to the start of the input buffer.
2111 */
2112 vec_delete (cf->input_vector, i, 0);
2113 }
2114 return 1; /* wait for more */
2115 }
2116 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002117
Dave Barach9b8ffd92016-07-08 08:13:45 -04002118 default:
2119 /* process the action */
2120 if (!unix_cli_line_process_one (cm, um, cf, uf,
2121 cf->input_vector[i], action))
2122 {
2123 /* CRLF found. Consume the bytes from the input_vector */
2124 vec_delete (cf->input_vector, i + matched, 0);
2125 /* And tell our caller to execute cf->input_command */
2126 return 0;
2127 }
2128 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002129
2130 i += matched;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002131 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002132
Dave Barach9b8ffd92016-07-08 08:13:45 -04002133 vec_reset_length (cf->input_vector);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002134 return 1;
2135}
2136
Chris Luke8e5b0412016-07-26 13:06:10 -04002137/** @brief Process input to a CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002138static void
2139unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002140{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002141 unix_main_t *um = &unix_main;
2142 unix_file_t *uf;
2143 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002144 unformat_input_t input;
2145 int vlib_parse_eval (u8 *);
2146
Chris Luke93dcd1d2016-05-10 10:45:10 -04002147more:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002148 /* Try vlibplex first. Someday... */
2149 if (0 && vlib_parse_eval (cf->input_vector) == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002150 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002151
Chris Luke93dcd1d2016-05-10 10:45:10 -04002152 if (cf->line_mode)
2153 {
2154 /* just treat whatever we got as a complete line of input */
2155 cf->current_command = cf->input_vector;
2156 }
2157 else
2158 {
2159 /* Line edit, echo, etc. */
2160 if (unix_cli_line_edit (cm, um, cf))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002161 /* want more input */
2162 return;
Chris Luke93dcd1d2016-05-10 10:45:10 -04002163 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002164
2165 if (um->log_fd)
2166 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002167 static u8 *lv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002168 vec_reset_length (lv);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002169 lv = format (lv, "%U[%d]: %v",
2170 format_timeval, 0 /* current bat-time */ ,
2171 0 /* current bat-format */ ,
2172 cli_file_index, cf->input_vector);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002173 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002174 int rv __attribute__ ((unused)) =
2175 write (um->log_fd, lv, vec_len (lv));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002176 }
2177 }
2178
Chris Luke93dcd1d2016-05-10 10:45:10 -04002179 /* Copy our input command to a new string */
2180 unformat_init_vector (&input, cf->current_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002181
2182 /* Remove leading white space from input. */
2183 (void) unformat (&input, "");
2184
2185 cm->current_input_file_index = cli_file_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002186 cf->pager_start = 0; /* start a new pager session */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002187
2188 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002189 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output,
2190 cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002191
Ed Warnickecb9cada2015-12-08 15:45:58 -07002192 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
2193 input.buffer = 0;
2194
2195 unformat_free (&input);
2196
Chris Luke93dcd1d2016-05-10 10:45:10 -04002197 /* Re-fetch pointer since pool may have moved. */
2198 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2199 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
2200
Ed Warnickecb9cada2015-12-08 15:45:58 -07002201done:
Chris Luke93dcd1d2016-05-10 10:45:10 -04002202 /* reset vector; we'll re-use it later */
2203 if (cf->line_mode)
2204 vec_reset_length (cf->input_vector);
2205 else
2206 vec_reset_length (cf->current_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002207
Chris Luke7afda3a2016-04-25 13:49:22 -04002208 if (cf->no_pager == 2)
2209 {
2210 /* Pager was programmatically disabled */
2211 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
2212 cf->no_pager = um->cli_no_pager;
2213 }
2214
Dave Barach9b8ffd92016-07-08 08:13:45 -04002215 if (vec_len (cf->pager_index) == 0
2216 || vec_len (cf->pager_index) < cf->height)
Chris Luke7afda3a2016-04-25 13:49:22 -04002217 {
2218 /* There was no need for the pager */
2219 unix_cli_pager_reset (cf);
2220
2221 /* Prompt. */
2222 unix_cli_cli_prompt (cf, uf);
2223 }
2224 else
2225 {
2226 /* Display the pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002227 unix_cli_pager_prompt (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04002228 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002229
Dave Barach9b8ffd92016-07-08 08:13:45 -04002230 /* Any residual data in the input vector? */
2231 if (vec_len (cf->input_vector))
2232 goto more;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002233}
2234
Chris Luke54ccf222016-07-25 16:38:11 -04002235/** Destroy a CLI session.
2236 * @note If we destroy the @c stdin session this additionally signals
2237 * the shutdown of VPP.
2238 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002239static void
2240unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002241{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002242 unix_main_t *um = &unix_main;
2243 unix_cli_file_t *cf;
2244 unix_file_t *uf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002245 int i;
2246
2247 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2248 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
2249
2250 /* Quit/EOF on stdin means quit program. */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002251 if (uf->file_descriptor == UNIX_CLI_STDIN_FD)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002252 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
2253
2254 vec_free (cf->current_command);
2255 vec_free (cf->search_key);
2256
2257 for (i = 0; i < vec_len (cf->command_history); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002258 vec_free (cf->command_history[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002259
2260 vec_free (cf->command_history);
2261
2262 unix_file_del (um, uf);
2263
2264 unix_cli_file_free (cf);
2265 pool_put (cm->cli_file_pool, cf);
2266}
2267
Chris Luke54ccf222016-07-25 16:38:11 -04002268/** Handle system events. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002269static uword
2270unix_cli_process (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002271 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002272{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002273 unix_cli_main_t *cm = &unix_cli_main;
2274 uword i, *data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002275
2276 while (1)
2277 {
2278 unix_cli_process_event_type_t event_type;
2279 vlib_process_wait_for_event (vm);
2280 event_type = vlib_process_get_events (vm, &data);
2281
2282 switch (event_type)
2283 {
2284 case UNIX_CLI_PROCESS_EVENT_READ_READY:
2285 for (i = 0; i < vec_len (data); i++)
2286 unix_cli_process_input (cm, data[i]);
2287 break;
2288
2289 case UNIX_CLI_PROCESS_EVENT_QUIT:
2290 /* Kill this process. */
2291 for (i = 0; i < vec_len (data); i++)
2292 unix_cli_kill (cm, data[i]);
2293 goto done;
2294 }
2295
2296 if (data)
2297 _vec_len (data) = 0;
2298 }
2299
Dave Barach9b8ffd92016-07-08 08:13:45 -04002300done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002301 vec_free (data);
2302
2303 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
2304
2305 /* Add node index so we can re-use this process later. */
2306 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
2307
2308 return 0;
2309}
2310
Chris Luke54ccf222016-07-25 16:38:11 -04002311/** Called when a CLI session file descriptor can be written to without
2312 * blocking. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002313static clib_error_t *
2314unix_cli_write_ready (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002315{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002316 unix_cli_main_t *cm = &unix_cli_main;
2317 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002318 int n;
2319
2320 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2321
2322 /* Flush output vector. */
2323 n = write (uf->file_descriptor,
2324 cf->output_vector, vec_len (cf->output_vector));
2325
2326 if (n < 0 && errno != EAGAIN)
2327 return clib_error_return_unix (0, "write");
2328
2329 else if (n > 0)
2330 unix_cli_del_pending_output (uf, cf, n);
2331
2332 return /* no error */ 0;
2333}
2334
Chris Luke54ccf222016-07-25 16:38:11 -04002335/** Called when a CLI session file descriptor has data to be read. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002336static clib_error_t *
2337unix_cli_read_ready (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002338{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002339 unix_main_t *um = &unix_main;
2340 unix_cli_main_t *cm = &unix_cli_main;
2341 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002342 uword l;
2343 int n, n_read, n_try;
2344
2345 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2346
2347 n = n_try = 4096;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002348 while (n == n_try)
2349 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002350 l = vec_len (cf->input_vector);
2351 vec_resize (cf->input_vector, l + n_try);
2352
2353 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
2354
2355 /* Error? */
2356 if (n < 0 && errno != EAGAIN)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002357 return clib_error_return_unix (0, "read");
2358
Ed Warnickecb9cada2015-12-08 15:45:58 -07002359 n_read = n < 0 ? 0 : n;
2360 _vec_len (cf->input_vector) = l + n_read;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002361 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002362
Dave Barach9b8ffd92016-07-08 08:13:45 -04002363 if (!(n < 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002364 vlib_process_signal_event (um->vlib_main,
2365 cf->process_node_index,
2366 (n_read == 0
2367 ? UNIX_CLI_PROCESS_EVENT_QUIT
2368 : UNIX_CLI_PROCESS_EVENT_READ_READY),
2369 /* event data */ uf->private_data);
2370
2371 return /* no error */ 0;
2372}
2373
Chris Lukeb5850972016-05-03 16:34:59 -04002374/** Store a new CLI session.
2375 * @param name The name of the session.
2376 * @param fd The file descriptor for the session I/O.
2377 * @return The session ID.
2378 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002379static u32
2380unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002381{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002382 unix_main_t *um = &unix_main;
2383 unix_cli_file_t *cf;
2384 unix_file_t template = { 0 };
2385 vlib_main_t *vm = um->vlib_main;
2386 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002387
2388 name = (char *) format (0, "unix-cli-%s", name);
2389
2390 if (vec_len (cm->unused_cli_process_node_indices) > 0)
2391 {
2392 uword l = vec_len (cm->unused_cli_process_node_indices);
2393
2394 /* Find node and give it new name. */
2395 n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
2396 vec_free (n->name);
2397 n->name = (u8 *) name;
2398
2399 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
2400
2401 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
2402 }
2403 else
2404 {
2405 static vlib_node_registration_t r = {
2406 .function = unix_cli_process,
2407 .type = VLIB_NODE_TYPE_PROCESS,
Dave Barach96e12032016-06-09 15:32:48 -04002408 .process_log2_n_stack_bytes = 16,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002409 };
2410
2411 r.name = name;
2412 vlib_register_node (vm, &r);
2413 vec_free (name);
2414
2415 n = vlib_get_node (vm, r.index);
2416 }
2417
2418 pool_get (cm->cli_file_pool, cf);
2419 memset (cf, 0, sizeof (*cf));
2420
2421 template.read_function = unix_cli_read_ready;
2422 template.write_function = unix_cli_write_ready;
2423 template.file_descriptor = fd;
2424 template.private_data = cf - cm->cli_file_pool;
2425
2426 cf->process_node_index = n->index;
2427 cf->unix_file_index = unix_file_add (um, &template);
2428 cf->output_vector = 0;
2429 cf->input_vector = 0;
2430
Ed Warnickecb9cada2015-12-08 15:45:58 -07002431 vlib_start_process (vm, n->runtime_index);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002432
Dave Barach9b8ffd92016-07-08 08:13:45 -04002433 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002434 p->output_function = unix_vlib_cli_output;
2435 p->output_function_arg = cf - cm->cli_file_pool;
2436
Ed Warnickecb9cada2015-12-08 15:45:58 -07002437 return cf - cm->cli_file_pool;
2438}
2439
Chris Lukeb5850972016-05-03 16:34:59 -04002440/** Telnet listening socket has a new connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002441static clib_error_t *
2442unix_cli_listen_read_ready (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002443{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002444 unix_main_t *um = &unix_main;
2445 unix_cli_main_t *cm = &unix_cli_main;
2446 clib_socket_t *s = &um->cli_listen_socket;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002447 clib_socket_t client;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002448 char *client_name;
2449 clib_error_t *error;
2450 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002451 u32 cf_index;
2452
2453 error = clib_socket_accept (s, &client);
2454 if (error)
2455 return error;
2456
2457 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
2458
2459 cf_index = unix_cli_file_add (cm, client_name, client.fd);
2460 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2461
2462 /* No longer need CLIB version of socket. */
2463 clib_socket_free (&client);
2464
2465 vec_free (client_name);
2466
2467 /* if we're supposed to run telnet session in character mode (default) */
2468 if (um->cli_line_mode == 0)
2469 {
Chris Luke0aca5eb2016-04-25 13:48:54 -04002470 /*
2471 * Set telnet client character mode, echo on, suppress "go-ahead".
2472 * Technically these should be negotiated, but this works.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002473 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002474 u8 charmode_option[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002475 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
2476 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
2477 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
2478 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
2479 IAC, WILL, TELOPT_ECHO, /* server will do echo */
2480 IAC, DONT, TELOPT_ECHO, /* client should not echo */
2481 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
2482 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
2483 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
2484 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002485 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002486
Chris Luke0aca5eb2016-04-25 13:48:54 -04002487 /* Enable history on this CLI */
Chris Luke7afda3a2016-04-25 13:49:22 -04002488 cf->history_limit = um->cli_history_limit;
2489 cf->has_history = cf->history_limit != 0;
2490
2491 /* Make sure this session is in line mode */
2492 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002493
2494 /* We need CRLF */
2495 cf->crlf_mode = 1;
2496
Chris Luke7afda3a2016-04-25 13:49:22 -04002497 /* Setup the pager */
2498 cf->no_pager = um->cli_no_pager;
2499
Ed Warnickecb9cada2015-12-08 15:45:58 -07002500 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002501
2502 /* Send the telnet options */
2503 unix_vlib_cli_output_raw (cf, uf, charmode_option,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002504 ARRAY_LEN (charmode_option));
Chris Luke0aca5eb2016-04-25 13:48:54 -04002505
2506 /* In case the client doesn't negotiate terminal type, use
2507 * a timer to kick off the initial prompt. */
2508 timer_call (unix_cli_file_welcome_timer, cf_index, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002509 }
2510
2511 return error;
2512}
2513
Chris Lukeb5850972016-05-03 16:34:59 -04002514/** The system terminal has informed us that the window size
2515 * has changed.
2516 */
Chris Luke7afda3a2016-04-25 13:49:22 -04002517static void
2518unix_cli_resize_interrupt (int signum)
2519{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002520 unix_main_t *um = &unix_main;
2521 unix_cli_main_t *cm = &unix_cli_main;
2522 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2523 cm->stdin_cli_file_index);
2524 unix_file_t *uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04002525 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002526 (void) signum;
Chris Luke7afda3a2016-04-25 13:49:22 -04002527
2528 /* Terminal resized, fetch the new size */
Dave Barachb2a6e252016-07-27 10:00:58 -04002529 if (ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws) < 0)
2530 {
2531 /* "Should never happen..." */
2532 clib_unix_warning ("TIOCGWINSZ");
2533 /* We can't trust ws.XXX... */
2534 return;
2535 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002536 cf->width = ws.ws_col;
2537 cf->height = ws.ws_row;
Chris Luke862623d2016-05-14 10:13:34 -04002538
2539 /* Reindex the pager buffer */
2540 unix_cli_pager_reindex (cf);
2541
2542 /* Redraw the page */
2543 unix_cli_pager_redraw (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04002544}
2545
Chris Lukeb5850972016-05-03 16:34:59 -04002546/** Handle configuration directives in the @em unix section. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002547static clib_error_t *
2548unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
2549{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002550 unix_main_t *um = &unix_main;
2551 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002552 int flags;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002553 clib_error_t *error = 0;
2554 unix_cli_file_t *cf;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002555 u32 cf_index;
2556 struct termios tio;
Chris Luke7afda3a2016-04-25 13:49:22 -04002557 struct sigaction sa;
2558 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002559 u8 *term;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002560
2561 /* We depend on unix flags being set. */
2562 if ((error = vlib_call_config_function (vm, unix_config)))
2563 return error;
2564
2565 if (um->flags & UNIX_FLAG_INTERACTIVE)
2566 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002567 /* Set stdin to be non-blocking. */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002568 if ((flags = fcntl (UNIX_CLI_STDIN_FD, F_GETFL, 0)) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002569 flags = 0;
Dave Barachb2a6e252016-07-27 10:00:58 -04002570 (void) fcntl (UNIX_CLI_STDIN_FD, F_SETFL, flags | O_NONBLOCK);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002571
Chris Luke0aca5eb2016-04-25 13:48:54 -04002572 cf_index = unix_cli_file_add (cm, "stdin", UNIX_CLI_STDIN_FD);
2573 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04002574 cm->stdin_cli_file_index = cf_index;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002575
2576 /* If stdin is a tty and we are using chacracter mode, enable
2577 * history on the CLI and set the tty line discipline accordingly. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002578 if (isatty (UNIX_CLI_STDIN_FD) && um->cli_line_mode == 0)
2579 {
2580 /* Capture terminal resize events */
Ed Warnicke853e7202016-08-12 11:42:26 -07002581 memset (&sa, 0, sizeof (sa));
Dave Barach9b8ffd92016-07-08 08:13:45 -04002582 sa.sa_handler = unix_cli_resize_interrupt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002583 if (sigaction (SIGWINCH, &sa, 0) < 0)
2584 clib_panic ("sigaction");
Chris Luke7afda3a2016-04-25 13:49:22 -04002585
Dave Barach9b8ffd92016-07-08 08:13:45 -04002586 /* Retrieve the current terminal size */
2587 ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws);
2588 cf->width = ws.ws_col;
2589 cf->height = ws.ws_row;
Chris Luke7afda3a2016-04-25 13:49:22 -04002590
Dave Barach9b8ffd92016-07-08 08:13:45 -04002591 if (cf->width == 0 || cf->height == 0)
2592 /* We have a tty, but no size. Stick to line mode. */
2593 goto notty;
Chris Luke7afda3a2016-04-25 13:49:22 -04002594
Dave Barach9b8ffd92016-07-08 08:13:45 -04002595 /* Setup the history */
2596 cf->history_limit = um->cli_history_limit;
2597 cf->has_history = cf->history_limit != 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04002598
Dave Barach9b8ffd92016-07-08 08:13:45 -04002599 /* Setup the pager */
2600 cf->no_pager = um->cli_no_pager;
Chris Luke7afda3a2016-04-25 13:49:22 -04002601
Dave Barach9b8ffd92016-07-08 08:13:45 -04002602 /* We're going to be in char by char mode */
2603 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002604
Dave Barach9b8ffd92016-07-08 08:13:45 -04002605 /* Save the original tty state so we can restore it later */
2606 tcgetattr (UNIX_CLI_STDIN_FD, &um->tio_stdin);
2607 um->tio_isset = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002608
Dave Barach9b8ffd92016-07-08 08:13:45 -04002609 /* Tweak the tty settings */
2610 tio = um->tio_stdin;
2611 /* echo off, canonical mode off, ext'd input processing off */
2612 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
2613 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
2614 tio.c_cc[VTIME] = 0; /* no timer */
2615 tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &tio);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002616
Dave Barach9b8ffd92016-07-08 08:13:45 -04002617 /* See if we can do ANSI/VT100 output */
2618 term = (u8 *) getenv ("TERM");
2619 if (term != NULL)
2620 cf->ansi_capable = unix_cli_terminal_type (term,
2621 strlen ((char *)
2622 term));
2623 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002624 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002625 {
2626 notty:
2627 /* No tty, so make sure these things are off */
2628 cf->no_pager = 1;
2629 cf->history_limit = 0;
2630 cf->has_history = 0;
2631 cf->line_mode = 1;
2632 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002633
2634 /* Send banner and initial prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002635 unix_cli_file_welcome (cm, cf);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002636 }
2637
Ed Warnickea25bd1c2016-04-01 22:43:37 -05002638 /* If we have socket config, LISTEN, otherwise, don't */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002639 clib_socket_t *s = &um->cli_listen_socket;
2640 if (s->config && s->config[0] != 0)
2641 {
2642 /* CLI listen. */
2643 unix_file_t template = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002644
Chris Luke475674e2017-07-05 18:02:53 -04002645 /* If our listen address looks like a path and it starts with
2646 * VPP_RUN_DIR, go make sure VPP_RUN_DIR exists before trying to open
2647 * a socket in it.
2648 */
2649 if (strncmp (s->config, VPP_RUN_DIR "/", strlen (VPP_RUN_DIR) + 1) == 0)
2650 {
2651 error = unix_make_vpp_run_dir ();
2652 if (error)
2653 return error;
2654 }
2655
Damjan Marionf6616382017-06-21 12:01:37 +02002656 s->flags = SOCKET_IS_SERVER | /* listen, don't connect */
2657 SOCKET_ALLOW_GROUP_WRITE; /* PF_LOCAL socket only */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002658 error = clib_socket_init (s);
Ed Warnickea25bd1c2016-04-01 22:43:37 -05002659
Dave Barach9b8ffd92016-07-08 08:13:45 -04002660 if (error)
2661 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002662
Dave Barach9b8ffd92016-07-08 08:13:45 -04002663 template.read_function = unix_cli_listen_read_ready;
2664 template.file_descriptor = s->fd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002665
Dave Barach9b8ffd92016-07-08 08:13:45 -04002666 unix_file_add (um, &template);
2667 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002668
2669 /* Set CLI prompt. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002670 if (!cm->cli_prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002671 cm->cli_prompt = format (0, "VLIB: ");
2672
2673 return 0;
2674}
2675
Chris Luke90f52bf2016-09-12 08:55:13 -04002676/*?
2677 * This module has no configurable parameters.
2678?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002679VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
2680
Chris Luke54ccf222016-07-25 16:38:11 -04002681/** Called when VPP is shutting down, this restores the system
2682 * terminal state if previously saved.
Chris Lukeb5850972016-05-03 16:34:59 -04002683 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002684static clib_error_t *
2685unix_cli_exit (vlib_main_t * vm)
2686{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002687 unix_main_t *um = &unix_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002688
2689 /* If stdin is a tty and we saved the tty state, reset the tty state */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002690 if (isatty (UNIX_CLI_STDIN_FD) && um->tio_isset)
2691 tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &um->tio_stdin);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002692
2693 return 0;
2694}
2695
2696VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
2697
Chris Lukeb5850972016-05-03 16:34:59 -04002698/** Set the CLI prompt.
Chris Luke54ccf222016-07-25 16:38:11 -04002699 * @param prompt The C string to set the prompt to.
Chris Lukeb5850972016-05-03 16:34:59 -04002700 * @note This setting is global; it impacts all current
2701 * and future CLI sessions.
2702 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002703void
2704vlib_unix_cli_set_prompt (char *prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002705{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002706 char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s ";
2707 unix_cli_main_t *cm = &unix_cli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002708 if (cm->cli_prompt)
2709 vec_free (cm->cli_prompt);
2710 cm->cli_prompt = format (0, fmt, prompt);
2711}
2712
Chris Lukeb5850972016-05-03 16:34:59 -04002713/** CLI command to quit the terminal session.
2714 * @note If this is a stdin session then this will
2715 * shutdown VPP also.
2716 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002717static clib_error_t *
2718unix_cli_quit (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002719 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002720{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002721 unix_cli_main_t *cm = &unix_cli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002722
2723 vlib_process_signal_event (vm,
2724 vlib_current_process (vm),
2725 UNIX_CLI_PROCESS_EVENT_QUIT,
2726 cm->current_input_file_index);
2727 return 0;
2728}
2729
Chris Luke54ccf222016-07-25 16:38:11 -04002730/*?
2731 * Terminates the current CLI session.
2732 *
2733 * If VPP is running in @em interactive mode and this is the console session
2734 * (that is, the session on @c stdin) then this will also terminate VPP.
2735?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002736/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002737VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
2738 .path = "quit",
2739 .short_help = "Exit CLI",
2740 .function = unix_cli_quit,
2741};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002742/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002743
Chris Lukeb5850972016-05-03 16:34:59 -04002744/** CLI command to execute a VPP command script. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002745static clib_error_t *
2746unix_cli_exec (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002747 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002748{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002749 char *file_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002750 int fd;
2751 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002752 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002753
2754 file_name = 0;
2755 fd = -1;
2756 error = 0;
2757
Dave Barach9b8ffd92016-07-08 08:13:45 -04002758 if (!unformat (input, "%s", &file_name))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002759 {
2760 error = clib_error_return (0, "expecting file name, got `%U'",
2761 format_unformat_error, input);
2762 goto done;
2763 }
2764
2765 fd = open (file_name, O_RDONLY);
2766 if (fd < 0)
2767 {
2768 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
2769 goto done;
2770 }
2771
2772 /* Make sure its a regular file. */
2773 {
2774 struct stat s;
2775
2776 if (fstat (fd, &s) < 0)
2777 {
2778 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
2779 goto done;
2780 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04002781
2782 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002783 {
2784 error = clib_error_return (0, "not a regular file `%s'", file_name);
2785 goto done;
2786 }
2787 }
2788
2789 unformat_init_unix_file (&sub_input, fd);
2790
2791 vlib_cli_input (vm, &sub_input, 0, 0);
2792 unformat_free (&sub_input);
2793
Dave Barach9b8ffd92016-07-08 08:13:45 -04002794done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002795 if (fd > 0)
2796 close (fd);
2797 vec_free (file_name);
2798
2799 return error;
2800}
2801
Chris Luke54ccf222016-07-25 16:38:11 -04002802/*?
2803 * Executes a sequence of CLI commands which are read from a file.
2804 *
2805 * If a command is unrecognised or otherwise invalid then the usual CLI
2806 * feedback will be generated, however execution of subsequent commands
2807 * from the file will continue.
2808?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002809/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002810VLIB_CLI_COMMAND (cli_exec, static) = {
2811 .path = "exec",
2812 .short_help = "Execute commands from file",
2813 .function = unix_cli_exec,
Dave Barachb2ef4dd2016-03-23 08:56:01 -04002814 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002815};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002816/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002817
Chris Lukeb5850972016-05-03 16:34:59 -04002818/** CLI command to show various unix error statistics. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002819static clib_error_t *
2820unix_show_errors (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002821 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002822{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002823 unix_main_t *um = &unix_main;
2824 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002825 int i, n_errors_to_show;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002826 unix_error_history_t *unix_errors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002827
2828 n_errors_to_show = 1 << 30;
2829
2830 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2831 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002832 if (!unformat (input, "%d", &n_errors_to_show))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002833 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002834 error =
2835 clib_error_return (0,
2836 "expecting integer number of errors to show, got `%U'",
2837 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002838 goto done;
2839 }
2840 }
2841
Dave Barach9b8ffd92016-07-08 08:13:45 -04002842 n_errors_to_show =
2843 clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002844
Dave Barach9b8ffd92016-07-08 08:13:45 -04002845 i =
2846 um->error_history_index >
2847 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002848
2849 while (n_errors_to_show > 0)
2850 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002851 unix_error_history_t *eh = um->error_history + i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002852
Dave Barach9b8ffd92016-07-08 08:13:45 -04002853 if (!eh->error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002854 break;
2855
2856 vec_add1 (unix_errors, eh[0]);
2857 n_errors_to_show -= 1;
2858 if (i == 0)
2859 i = ARRAY_LEN (um->error_history) - 1;
2860 else
2861 i--;
2862 }
2863
2864 if (vec_len (unix_errors) == 0)
2865 vlib_cli_output (vm, "no Unix errors so far");
2866 else
2867 {
2868 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
2869 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
2870 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002871 unix_error_history_t *eh = vec_elt_at_index (unix_errors, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002872 vlib_cli_output (vm, "%U: %U",
2873 format_time_interval, "h:m:s:u", eh->time,
2874 format_clib_error, eh->error);
2875 }
2876 vlib_cli_output (vm, "%U: time now",
2877 format_time_interval, "h:m:s:u", vlib_time_now (vm));
2878 }
2879
Dave Barach9b8ffd92016-07-08 08:13:45 -04002880done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002881 vec_free (unix_errors);
2882 return error;
2883}
2884
Dave Barach9b8ffd92016-07-08 08:13:45 -04002885/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002886VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
2887 .path = "show unix-errors",
2888 .short_help = "Show Unix system call error history",
2889 .function = unix_show_errors,
2890};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002891/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002892
Chris Lukeb5850972016-05-03 16:34:59 -04002893/** CLI command to show session command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002894static clib_error_t *
Chris Luke6b90fe32016-04-25 13:49:15 -04002895unix_cli_show_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002896 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke6b90fe32016-04-25 13:49:15 -04002897{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002898 unix_cli_main_t *cm = &unix_cli_main;
2899 unix_cli_file_t *cf;
Chris Luke6b90fe32016-04-25 13:49:15 -04002900 int i, j;
2901
2902 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2903
Chris Luke7afda3a2016-04-25 13:49:22 -04002904 if (cf->has_history && cf->history_limit)
Chris Luke6b90fe32016-04-25 13:49:15 -04002905 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002906 i = 1 + cf->command_number - vec_len (cf->command_history);
Chris Luke7afda3a2016-04-25 13:49:22 -04002907 for (j = 0; j < vec_len (cf->command_history); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002908 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
Chris Luke7afda3a2016-04-25 13:49:22 -04002909 }
2910 else
2911 {
2912 vlib_cli_output (vm, "History not enabled.\n");
Chris Luke6b90fe32016-04-25 13:49:15 -04002913 }
2914
2915 return 0;
2916}
2917
Chris Luke54ccf222016-07-25 16:38:11 -04002918/*?
2919 * Displays the command history for the current session, if any.
2920?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002921/* *INDENT-OFF* */
Chris Luke6b90fe32016-04-25 13:49:15 -04002922VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
2923 .path = "history",
2924 .short_help = "Show current session command history",
2925 .function = unix_cli_show_history,
2926};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002927/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04002928
Chris Lukeb5850972016-05-03 16:34:59 -04002929/** CLI command to show terminal status. */
Chris Luke7afda3a2016-04-25 13:49:22 -04002930static clib_error_t *
2931unix_cli_show_terminal (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002932 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04002933{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002934 unix_main_t *um = &unix_main;
2935 unix_cli_main_t *cm = &unix_cli_main;
2936 unix_cli_file_t *cf;
2937 vlib_node_t *n;
Chris Luke7afda3a2016-04-25 13:49:22 -04002938
2939 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2940 n = vlib_get_node (vm, cf->process_node_index);
2941
2942 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002943 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
2944 "line-by-line" : "char-by-char");
Chris Luke7afda3a2016-04-25 13:49:22 -04002945 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
2946 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002947 vlib_cli_output (vm, "ANSI capable: %s\n",
2948 cf->ansi_capable ? "yes" : "no");
Chris Luke7afda3a2016-04-25 13:49:22 -04002949 vlib_cli_output (vm, "History enabled: %s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04002950 cf->has_history ? "yes" : "no", !cf->has_history
2951 || cf->history_limit ? "" :
2952 " (disabled by history limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04002953 if (cf->has_history)
2954 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
2955 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04002956 cf->no_pager ? "no" : "yes",
2957 cf->no_pager
2958 || cf->height ? "" : " (disabled by terminal height)",
2959 cf->no_pager
2960 || um->cli_pager_buffer_limit ? "" :
2961 " (disabled by buffer limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04002962 if (!cf->no_pager)
2963 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002964 vlib_cli_output (vm, "CRLF mode: %s\n",
2965 cf->crlf_mode ? "CR+LF" : "LF");
Chris Luke7afda3a2016-04-25 13:49:22 -04002966
2967 return 0;
2968}
2969
Chris Luke54ccf222016-07-25 16:38:11 -04002970/*?
2971 * Displays various information about the state of the current terminal
2972 * session.
2973 *
2974 * @cliexpar
2975 * @cliexstart{show terminal}
2976 * Terminal name: unix-cli-stdin
2977 * Terminal mode: char-by-char
2978 * Terminal width: 123
2979 * Terminal height: 48
2980 * ANSI capable: yes
2981 * History enabled: yes
2982 * History limit: 50
2983 * Pager enabled: yes
2984 * Pager limit: 100000
2985 * CRLF mode: LF
2986 * @cliexend
2987?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002988/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04002989VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
2990 .path = "show terminal",
2991 .short_help = "Show current session terminal settings",
2992 .function = unix_cli_show_terminal,
2993};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002994/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04002995
Chris Lukeb5850972016-05-03 16:34:59 -04002996/** CLI command to set terminal pager settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04002997static clib_error_t *
2998unix_cli_set_terminal_pager (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002999 unformat_input_t * input,
3000 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003001{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003002 unix_main_t *um = &unix_main;
3003 unix_cli_main_t *cm = &unix_cli_main;
3004 unix_cli_file_t *cf;
3005 unformat_input_t _line_input, *line_input = &_line_input;
Billy McFalla9a20e72017-02-15 11:39:12 -05003006 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003007
Dave Barach9b8ffd92016-07-08 08:13:45 -04003008 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04003009 return 0;
3010
3011 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3012
3013 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3014 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003015 if (unformat (line_input, "on"))
3016 cf->no_pager = 0;
3017 else if (unformat (line_input, "off"))
3018 cf->no_pager = 1;
3019 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
3020 vlib_cli_output (vm,
3021 "Pager limit set to %u lines; note, this is global.\n",
3022 um->cli_pager_buffer_limit);
3023 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003024 {
3025 error = clib_error_return (0, "unknown parameter: `%U`",
3026 format_unformat_error, line_input);
3027 goto done;
3028 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04003029 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003030
Billy McFalla9a20e72017-02-15 11:39:12 -05003031done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003032 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003033
Billy McFalla9a20e72017-02-15 11:39:12 -05003034 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003035}
3036
Chris Luke54ccf222016-07-25 16:38:11 -04003037/*?
3038 * Enables or disables the terminal pager for this session. Generally
3039 * this defaults to enabled.
3040 *
3041 * Additionally allows the pager buffer size to be set; though note that
3042 * this value is set globally and not per session.
3043?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003044/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003045VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
3046 .path = "set terminal pager",
3047 .short_help = "set terminal pager [on|off] [limit <lines>]",
3048 .function = unix_cli_set_terminal_pager,
3049};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003050/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003051
Chris Lukeb5850972016-05-03 16:34:59 -04003052/** CLI command to set terminal history settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003053static clib_error_t *
3054unix_cli_set_terminal_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003055 unformat_input_t * input,
3056 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003057{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003058 unix_cli_main_t *cm = &unix_cli_main;
3059 unix_cli_file_t *cf;
3060 unformat_input_t _line_input, *line_input = &_line_input;
Chris Luke7afda3a2016-04-25 13:49:22 -04003061 u32 limit;
Billy McFalla9a20e72017-02-15 11:39:12 -05003062 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003063
Dave Barach9b8ffd92016-07-08 08:13:45 -04003064 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04003065 return 0;
3066
3067 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3068
3069 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3070 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003071 if (unformat (line_input, "on"))
3072 cf->has_history = 1;
3073 else if (unformat (line_input, "off"))
3074 cf->has_history = 0;
3075 else if (unformat (line_input, "limit %u", &cf->history_limit))
3076 ;
3077 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003078 {
3079 error = clib_error_return (0, "unknown parameter: `%U`",
3080 format_unformat_error, line_input);
3081 goto done;
3082 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003083
Dave Barach9b8ffd92016-07-08 08:13:45 -04003084 /* If we reduced history size, or turned it off, purge the history */
3085 limit = cf->has_history ? cf->history_limit : 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003086
Dave Barach9b8ffd92016-07-08 08:13:45 -04003087 while (cf->command_history && vec_len (cf->command_history) >= limit)
3088 {
3089 vec_free (cf->command_history[0]);
3090 vec_delete (cf->command_history, 1, 0);
3091 }
3092 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003093
Billy McFalla9a20e72017-02-15 11:39:12 -05003094done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003095 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003096
Billy McFalla9a20e72017-02-15 11:39:12 -05003097 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003098}
3099
Chris Luke54ccf222016-07-25 16:38:11 -04003100/*?
3101 * Enables or disables the command history function of the current
3102 * terminal. Generally this defaults to enabled.
3103 *
3104 * This command also allows the maximum size of the history buffer for
3105 * this session to be altered.
3106?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003107/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003108VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
3109 .path = "set terminal history",
3110 .short_help = "set terminal history [on|off] [limit <lines>]",
3111 .function = unix_cli_set_terminal_history,
3112};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003113/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003114
Chris Lukeb5850972016-05-03 16:34:59 -04003115/** CLI command to set terminal ANSI settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003116static clib_error_t *
3117unix_cli_set_terminal_ansi (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003118 unformat_input_t * input,
3119 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003120{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003121 unix_cli_main_t *cm = &unix_cli_main;
3122 unix_cli_file_t *cf;
Chris Luke7afda3a2016-04-25 13:49:22 -04003123
3124 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3125
3126 if (unformat (input, "on"))
3127 cf->ansi_capable = 1;
3128 else if (unformat (input, "off"))
3129 cf->ansi_capable = 0;
3130 else
3131 return clib_error_return (0, "unknown parameter: `%U`",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003132 format_unformat_error, input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003133
3134 return 0;
3135}
3136
Chris Luke54ccf222016-07-25 16:38:11 -04003137/*?
3138 * Enables or disables the use of ANSI control sequences by this terminal.
3139 * The default will vary based on terminal detection at the start of the
3140 * session.
3141 *
3142 * ANSI control sequences are used in a small number of places to provide,
3143 * for example, color text output and to control the cursor in the pager.
3144?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003145/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003146VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
3147 .path = "set terminal ansi",
3148 .short_help = "set terminal ansi [on|off]",
3149 .function = unix_cli_set_terminal_ansi,
3150};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003151/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003152
3153static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07003154unix_cli_init (vlib_main_t * vm)
3155{
3156 return 0;
3157}
3158
3159VLIB_INIT_FUNCTION (unix_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003160
3161/*
3162 * fd.io coding-style-patch-verification: ON
3163 *
3164 * Local Variables:
3165 * eval: (c-set-style "gnu")
3166 * End:
3167 */