blob: cf524213f5bbdca520bed39d2c80e505eaf3515a [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>
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
Chris Lukeb5850972016-05-03 16:34:59 -040061/** ANSI escape code. */
Chris Luke0aca5eb2016-04-25 13:48:54 -040062#define ESC "\x1b"
63
Chris Lukeb5850972016-05-03 16:34:59 -040064/** ANSI Control Sequence Introducer. */
Chris Luke0aca5eb2016-04-25 13:48:54 -040065#define CSI ESC "["
66
Chris Lukeb5850972016-05-03 16:34:59 -040067/** ANSI clear screen. */
Chris Luke7afda3a2016-04-25 13:49:22 -040068#define ANSI_CLEAR CSI "2J" CSI "1;1H"
Chris Lukeb5850972016-05-03 16:34:59 -040069/** ANSI reset color settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -040070#define ANSI_RESET CSI "0m"
Chris Lukeb5850972016-05-03 16:34:59 -040071/** ANSI Start bold text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040072#define ANSI_BOLD CSI "1m"
Chris Lukeb5850972016-05-03 16:34:59 -040073/** ANSI Stop bold text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040074#define ANSI_DIM CSI "2m"
Chris Lukeb5850972016-05-03 16:34:59 -040075/** ANSI Start dark red text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040076#define ANSI_DRED ANSI_DIM CSI "31m"
Chris Lukeb5850972016-05-03 16:34:59 -040077/** ANSI Start bright red text. */
Chris Luke7afda3a2016-04-25 13:49:22 -040078#define ANSI_BRED ANSI_BOLD CSI "31m"
Chris Lukeb5850972016-05-03 16:34:59 -040079/** ANSI clear line cursor is on. */
Chris Luke7afda3a2016-04-25 13:49:22 -040080#define ANSI_CLEARLINE CSI "2K"
Chris Lukeb5850972016-05-03 16:34:59 -040081/** ANSI scroll screen down one line. */
Chris Luke7afda3a2016-04-25 13:49:22 -040082#define ANSI_SCROLLDN CSI "1T"
Chris Lukeb5850972016-05-03 16:34:59 -040083/** ANSI save cursor position. */
Chris Luke7afda3a2016-04-25 13:49:22 -040084#define ANSI_SAVECURSOR CSI "s"
Chris Lukeb5850972016-05-03 16:34:59 -040085/** ANSI restore cursor position if previously saved. */
Chris Luke7afda3a2016-04-25 13:49:22 -040086#define ANSI_RESTCURSOR CSI "u"
Chris Luke0aca5eb2016-04-25 13:48:54 -040087
88/** Maximum depth into a byte stream from which to compile a Telnet
89 * protocol message. This is a saftey measure. */
Chris Luke1aed76f2016-04-29 08:14:38 -040090#define UNIX_CLI_MAX_DEPTH_TELNET 24
Chris Luke0aca5eb2016-04-25 13:48:54 -040091
Chris Luke0aca5eb2016-04-25 13:48:54 -040092/** Unix standard in */
93#define UNIX_CLI_STDIN_FD 0
94
95
Chris Lukeb5850972016-05-03 16:34:59 -040096/** A CLI banner line. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040097typedef struct
98{
99 u8 *line; /**< The line to print. */
100 u32 length; /**< The length of the line without terminating NUL. */
Chris Luke572d8122016-04-25 13:49:07 -0400101} unix_cli_banner_t;
102
103#define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
104/** Plain welcome banner. */
105static unix_cli_banner_t unix_cli_banner[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400106 _(" _______ _ _ _____ ___ \n"),
107 _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
108 _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
109 _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
110 _("\n")
Chris Luke572d8122016-04-25 13:49:07 -0400111};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400112
Chris Luke572d8122016-04-25 13:49:07 -0400113/** ANSI color welcome banner. */
114static unix_cli_banner_t unix_cli_banner_color[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400115 _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
116 _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
117 _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
118 _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"),
119 _("\n")
Chris Luke572d8122016-04-25 13:49:07 -0400120};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400121
Chris Luke572d8122016-04-25 13:49:07 -0400122#undef _
123
Chris Luke862623d2016-05-14 10:13:34 -0400124/** Pager line index */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400125typedef struct
126{
Chris Luke862623d2016-05-14 10:13:34 -0400127 /** Index into pager_vector */
128 u32 line;
129
130 /** Offset of the string in the line */
131 u32 offset;
132
133 /** Length of the string in the line */
134 u32 length;
135} unix_cli_pager_index_t;
136
Chris Luke572d8122016-04-25 13:49:07 -0400137
Chris Luke0aca5eb2016-04-25 13:48:54 -0400138/** Unix CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400139typedef struct
140{
Chris Lukeb5850972016-05-03 16:34:59 -0400141 /** The file index held by unix.c */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700142 u32 unix_file_index;
143
Chris Lukeb5850972016-05-03 16:34:59 -0400144 /** Vector of output pending write to file descriptor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400145 u8 *output_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
Chris Lukeb5850972016-05-03 16:34:59 -0400147 /** Vector of input saved by Unix input node to be processed by
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 CLI process. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149 u8 *input_vector;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Chris Luke54ccf222016-07-25 16:38:11 -0400151 /** This session has command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 u8 has_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400153 /** Array of vectors of commands in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400154 u8 **command_history;
Chris Luke54ccf222016-07-25 16:38:11 -0400155 /** The command currently pointed at by the history cursor. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400156 u8 *current_command;
Chris Luke54ccf222016-07-25 16:38:11 -0400157 /** How far from the end of the history array the user has browsed. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158 i32 excursion;
Chris Luke6b90fe32016-04-25 13:49:15 -0400159
Chris Lukeb5850972016-05-03 16:34:59 -0400160 /** Maximum number of history entries this session will store. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161 u32 history_limit;
Chris Luke6b90fe32016-04-25 13:49:15 -0400162
Chris Lukeb5850972016-05-03 16:34:59 -0400163 /** Current command line counter */
Chris Luke6b90fe32016-04-25 13:49:15 -0400164 u32 command_number;
165
Chris Luke54ccf222016-07-25 16:38:11 -0400166 /** The string being searched for in the history. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400167 u8 *search_key;
Chris Luke54ccf222016-07-25 16:38:11 -0400168 /** If non-zero then the CLI is searching in the history array.
169 * - @c -1 means search backwards.
170 * - @c 1 means search forwards.
171 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 int search_mode;
173
Chris Lukeb5850972016-05-03 16:34:59 -0400174 /** Position of the insert cursor on the current input line */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400175 u32 cursor;
176
Chris Lukeb5850972016-05-03 16:34:59 -0400177 /** Line mode or char mode */
Chris Luke7afda3a2016-04-25 13:49:22 -0400178 u8 line_mode;
179
Chris Lukeb5850972016-05-03 16:34:59 -0400180 /** Set if the CRLF mode wants CR + LF */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400181 u8 crlf_mode;
182
Chris Lukeb5850972016-05-03 16:34:59 -0400183 /** Can we do ANSI output? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400184 u8 ansi_capable;
185
Chris Lukeb5850972016-05-03 16:34:59 -0400186 /** Has the session started? */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400187 u8 started;
188
Chris Lukeb5850972016-05-03 16:34:59 -0400189 /** Disable the pager? */
Chris Luke7afda3a2016-04-25 13:49:22 -0400190 u8 no_pager;
191
Chris Lukeb5850972016-05-03 16:34:59 -0400192 /** Pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400193 u8 **pager_vector;
Chris Luke7afda3a2016-04-25 13:49:22 -0400194
Chris Luke862623d2016-05-14 10:13:34 -0400195 /** Index of line fragments in the pager buffer */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400196 unix_cli_pager_index_t *pager_index;
Chris Luke7afda3a2016-04-25 13:49:22 -0400197
Chris Lukeb5850972016-05-03 16:34:59 -0400198 /** Line number of top of page */
Chris Luke7afda3a2016-04-25 13:49:22 -0400199 u32 pager_start;
200
Chris Lukeb5850972016-05-03 16:34:59 -0400201 /** Terminal width */
202 u32 width;
Chris Luke7afda3a2016-04-25 13:49:22 -0400203
Chris Lukeb5850972016-05-03 16:34:59 -0400204 /** Terminal height */
205 u32 height;
206
207 /** Process node identifier */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 u32 process_node_index;
209} unix_cli_file_t;
210
Chris Lukeb5850972016-05-03 16:34:59 -0400211/** Resets the pager buffer and other data.
212 * @param f The CLI session whose pager needs to be reset.
213 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214always_inline void
Dave Barach9b8ffd92016-07-08 08:13:45 -0400215unix_cli_pager_reset (unix_cli_file_t * f)
Chris Luke7afda3a2016-04-25 13:49:22 -0400216{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400217 u8 **p;
Chris Luke7afda3a2016-04-25 13:49:22 -0400218
Chris Luke862623d2016-05-14 10:13:34 -0400219 f->pager_start = 0;
220
221 vec_free (f->pager_index);
222 f->pager_index = 0;
223
Chris Luke7afda3a2016-04-25 13:49:22 -0400224 vec_foreach (p, f->pager_vector)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400225 {
226 vec_free (*p);
227 }
Chris Luke862623d2016-05-14 10:13:34 -0400228 vec_free (f->pager_vector);
Chris Luke7afda3a2016-04-25 13:49:22 -0400229 f->pager_vector = 0;
230}
231
Chris Lukeb5850972016-05-03 16:34:59 -0400232/** Release storage used by a CLI session.
233 * @param f The CLI session whose storage needs to be released.
234 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400235always_inline void
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236unix_cli_file_free (unix_cli_file_t * f)
237{
238 vec_free (f->output_vector);
239 vec_free (f->input_vector);
Chris Luke862623d2016-05-14 10:13:34 -0400240 unix_cli_pager_reset (f);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241}
242
Chris Lukeb5850972016-05-03 16:34:59 -0400243/** CLI actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400244typedef enum
245{
Chris Luke54ccf222016-07-25 16:38:11 -0400246 UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */
247 UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */
248 UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */
249 UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */
250 UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */
251 UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */
252 UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */
253 UNIX_CLI_PARSE_ACTION_LEFT, /**< Left arrow */
254 UNIX_CLI_PARSE_ACTION_RIGHT, /**< Right arrow */
255 UNIX_CLI_PARSE_ACTION_HOME, /**< Home key (jump to start of line) */
256 UNIX_CLI_PARSE_ACTION_END, /**< End key (jump to end of line) */
257 UNIX_CLI_PARSE_ACTION_WORDLEFT, /**< Jump cursor to start of left word */
258 UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */
259 UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */
260 UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */
261 UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */
262 UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */
263 UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */
264 UNIX_CLI_PARSE_ACTION_YANK, /**< Undo last erase action */
265 UNIX_CLI_PARSE_ACTION_TELNETIAC, /**< Telnet control code */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400266
Chris Luke54ccf222016-07-25 16:38:11 -0400267 UNIX_CLI_PARSE_ACTION_PAGER_CRLF, /**< Enter pressed (CR, CRLF, LF, etc) */
268 UNIX_CLI_PARSE_ACTION_PAGER_QUIT, /**< Exit the pager session */
269 UNIX_CLI_PARSE_ACTION_PAGER_NEXT, /**< Scroll to next page */
270 UNIX_CLI_PARSE_ACTION_PAGER_DN, /**< Scroll to next line */
271 UNIX_CLI_PARSE_ACTION_PAGER_UP, /**< Scroll to previous line */
272 UNIX_CLI_PARSE_ACTION_PAGER_TOP, /**< Scroll to first line */
273 UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM, /**< Scroll to last line */
274 UNIX_CLI_PARSE_ACTION_PAGER_PGDN, /**< Scroll to next page */
275 UNIX_CLI_PARSE_ACTION_PAGER_PGUP, /**< Scroll to previous page */
276 UNIX_CLI_PARSE_ACTION_PAGER_REDRAW, /**< Clear and redraw the page on the terminal */
277 UNIX_CLI_PARSE_ACTION_PAGER_SEARCH, /**< Search the pager buffer */
Chris Luke7afda3a2016-04-25 13:49:22 -0400278
Chris Luke54ccf222016-07-25 16:38:11 -0400279 UNIX_CLI_PARSE_ACTION_PARTIALMATCH, /**< Action parser found a partial match */
280 UNIX_CLI_PARSE_ACTION_NOMATCH /**< Action parser did not find any match */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400281} unix_cli_parse_action_t;
282
Chris Luke8e5b0412016-07-26 13:06:10 -0400283/** @brief Mapping of input buffer strings to action values.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400284 * @note This won't work as a hash since we need to be able to do
285 * partial matches on the string.
286 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400287typedef struct
288{
289 u8 *input; /**< Input string to match. */
290 u32 len; /**< Length of input without final NUL. */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400291 unix_cli_parse_action_t action; /**< Action to take when matched. */
292} unix_cli_parse_actions_t;
293
Chris Lukeb5850972016-05-03 16:34:59 -0400294/** @brief Given a capital ASCII letter character return a @c NUL terminated
Chris Luke0aca5eb2016-04-25 13:48:54 -0400295 * string with the control code for that letter.
Chris Lukeb5850972016-05-03 16:34:59 -0400296 *
297 * @param c An ASCII character.
298 * @return A @c NUL terminated string of type @c u8[].
299 *
300 * @par Example
301 * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[].
Chris Luke0aca5eb2016-04-25 13:48:54 -0400302 */
303#define CTL(c) (u8[]){ (c) - '@', 0 }
304
305#define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
Chris Lukeb5850972016-05-03 16:34:59 -0400306/**
307 * Patterns to match on a CLI input stream.
308 * @showinitializer
309 */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400310static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400311 /* Line handling */
312 _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */
313 _("\n", UNIX_CLI_PARSE_ACTION_CRLF),
314 _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */
315 _("\r", UNIX_CLI_PARSE_ACTION_CRLF),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400316
Dave Barach9b8ffd92016-07-08 08:13:45 -0400317 /* Unix shell control codes */
318 _(CTL ('B'), UNIX_CLI_PARSE_ACTION_LEFT),
319 _(CTL ('F'), UNIX_CLI_PARSE_ACTION_RIGHT),
320 _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP),
321 _(CTL ('N'), UNIX_CLI_PARSE_ACTION_DOWN),
322 _(CTL ('A'), UNIX_CLI_PARSE_ACTION_HOME),
323 _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END),
324 _(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT),
325 _(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT),
326 _(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT),
327 _(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK),
328 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR),
329 _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */
330 _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */
331 _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */
332 _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */
333 _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400334
Dave Barach9b8ffd92016-07-08 08:13:45 -0400335 /* VT100 Normal mode - Broadest support */
336 _(CSI "A", UNIX_CLI_PARSE_ACTION_UP),
337 _(CSI "B", UNIX_CLI_PARSE_ACTION_DOWN),
338 _(CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT),
339 _(CSI "D", UNIX_CLI_PARSE_ACTION_LEFT),
340 _(CSI "H", UNIX_CLI_PARSE_ACTION_HOME),
341 _(CSI "F", UNIX_CLI_PARSE_ACTION_END),
342 _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */
343 _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */
344 _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400345
346 /* VT100 Application mode - Some Gnome Terminal functions use these */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400347 _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP),
348 _(ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN),
349 _(ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT),
350 _(ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT),
351 _(ESC "OH", UNIX_CLI_PARSE_ACTION_HOME),
352 _(ESC "OF", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400353
Dave Barach9b8ffd92016-07-08 08:13:45 -0400354 /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
355 _(CSI "1~", UNIX_CLI_PARSE_ACTION_HOME),
356 _(CSI "4~", UNIX_CLI_PARSE_ACTION_END),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400357
Dave Barach9b8ffd92016-07-08 08:13:45 -0400358 /* Emacs-ish history search */
359 _(CTL ('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH),
360 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH),
Chris Luke0aca5eb2016-04-25 13:48:54 -0400361
Dave Barach9b8ffd92016-07-08 08:13:45 -0400362 /* Other protocol things */
363 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
364 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
365 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400366};
Chris Lukeb5850972016-05-03 16:34:59 -0400367
368/**
369 * Patterns to match when a CLI session is in the pager.
370 * @showinitializer
371 */
Chris Luke7afda3a2016-04-25 13:49:22 -0400372static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400373 /* Line handling */
374 _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */
375 _("\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
376 _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */
377 _("\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
Chris Luke7afda3a2016-04-25 13:49:22 -0400378
Dave Barach9b8ffd92016-07-08 08:13:45 -0400379 /* Pager commands */
380 _(" ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT),
381 _("q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT),
382 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
383 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
384 _("/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH),
Chris Luke7afda3a2016-04-25 13:49:22 -0400385
Dave Barach9b8ffd92016-07-08 08:13:45 -0400386 /* VT100 */
387 _(CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP),
388 _(CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN),
389 _(CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
390 _(CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400391
Dave Barach9b8ffd92016-07-08 08:13:45 -0400392 /* VT100 Application mode */
393 _(ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP),
394 _(ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN),
395 _(ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
396 _(ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
Chris Luke7afda3a2016-04-25 13:49:22 -0400397
Dave Barach9b8ffd92016-07-08 08:13:45 -0400398 /* ANSI X3.41-1974 */
399 _(CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
400 _(CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
401 _(CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP),
402 _(CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN),
Chris Luke7afda3a2016-04-25 13:49:22 -0400403
Dave Barach9b8ffd92016-07-08 08:13:45 -0400404 /* Other protocol things */
405 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
406 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
407 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
Chris Luke7afda3a2016-04-25 13:49:22 -0400408};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400409
Chris Luke0aca5eb2016-04-25 13:48:54 -0400410#undef _
411
Chris Lukeb5850972016-05-03 16:34:59 -0400412/** CLI session events. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400413typedef enum
414{
Chris Lukeb5850972016-05-03 16:34:59 -0400415 UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400416 UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */
Chris Luke0aca5eb2016-04-25 13:48:54 -0400417} unix_cli_process_event_type_t;
418
Chris Lukeb5850972016-05-03 16:34:59 -0400419/** CLI global state. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400420typedef struct
421{
Chris Lukeb5850972016-05-03 16:34:59 -0400422 /** Prompt string for CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400423 u8 *cli_prompt;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Chris Lukeb5850972016-05-03 16:34:59 -0400425 /** Vec pool of CLI sessions. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400426 unix_cli_file_t *cli_file_pool;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427
Chris Lukeb5850972016-05-03 16:34:59 -0400428 /** Vec pool of unused session indices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400429 u32 *unused_cli_process_node_indices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430
Chris Lukeb5850972016-05-03 16:34:59 -0400431 /** The session index of the stdin cli */
Chris Luke7afda3a2016-04-25 13:49:22 -0400432 u32 stdin_cli_file_index;
433
Chris Lukeb5850972016-05-03 16:34:59 -0400434 /** File pool index of current input. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435 u32 current_input_file_index;
436} unix_cli_main_t;
437
Chris Lukeb5850972016-05-03 16:34:59 -0400438/** CLI global state */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439static unix_cli_main_t unix_cli_main;
440
Chris Luke0aca5eb2016-04-25 13:48:54 -0400441/**
Chris Luke8e5b0412016-07-26 13:06:10 -0400442 * @brief Search for a byte sequence in the action list.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400443 *
Chris Lukeb5850972016-05-03 16:34:59 -0400444 * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with
445 * the bytes in @a input of maximum length @a ilen bytes.
446 * When a match is made @a *matched indicates how many bytes were matched.
447 * Returns a value from the enum @ref unix_cli_parse_action_t to indicate
448 * whether no match was found, a partial match was found or a complete
449 * match was found and what action, if any, should be taken.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400450 *
Chris Lukeb5850972016-05-03 16:34:59 -0400451 * @param[in] a Actions list to search within.
452 * @param[in] input String fragment to search for.
453 * @param[in] ilen Length of the string in 'input'.
454 * @param[out] matched Pointer to an integer that will contain the number
455 * of bytes matched when a complete match is found.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400456 *
Chris Lukeb5850972016-05-03 16:34:59 -0400457 * @return Action from @ref unix_cli_parse_action_t that the string fragment
Chris Luke0aca5eb2016-04-25 13:48:54 -0400458 * matches.
Chris Lukeb5850972016-05-03 16:34:59 -0400459 * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the
460 * whole input string matches the start of at least one action.
461 * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
Chris Luke0aca5eb2016-04-25 13:48:54 -0400462 * match at all.
463 */
464static unix_cli_parse_action_t
Dave Barach9b8ffd92016-07-08 08:13:45 -0400465unix_cli_match_action (unix_cli_parse_actions_t * a,
466 u8 * input, u32 ilen, i32 * matched)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400467{
Chris Luke0aca5eb2016-04-25 13:48:54 -0400468 u8 partial = 0;
469
470 while (a->input)
471 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400472 if (ilen >= a->len)
473 {
474 /* see if the start of the input buffer exactly matches the current
475 * action string. */
476 if (memcmp (input, a->input, a->len) == 0)
477 {
478 *matched = a->len;
479 return a->action;
480 }
481 }
482 else
483 {
484 /* if the first ilen characters match, flag this as a partial -
485 * meaning keep collecting bytes in case of a future match */
486 if (memcmp (input, a->input, ilen) == 0)
487 partial = 1;
488 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400489
Dave Barach9b8ffd92016-07-08 08:13:45 -0400490 /* check next action */
491 a++;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400492 }
493
494 return partial ?
Dave Barach9b8ffd92016-07-08 08:13:45 -0400495 UNIX_CLI_PARSE_ACTION_PARTIALMATCH : UNIX_CLI_PARSE_ACTION_NOMATCH;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400496}
497
498
Chris Luke54ccf222016-07-25 16:38:11 -0400499/** Add bytes to the output vector and then flagg the I/O system that bytes
500 * are available to be sent.
501 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502static void
503unix_cli_add_pending_output (unix_file_t * uf,
504 unix_cli_file_t * cf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400505 u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400507 unix_main_t *um = &unix_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
509 vec_add (cf->output_vector, buffer, buffer_bytes);
510 if (vec_len (cf->output_vector) > 0)
511 {
512 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
513 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400514 if (!skip_update)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 um->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
516 }
517}
518
Chris Luke54ccf222016-07-25 16:38:11 -0400519/** Delete all bytes from the output vector and flag the I/O system
520 * that no more bytes are available to be sent.
521 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522static void
523unix_cli_del_pending_output (unix_file_t * uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400524 unix_cli_file_t * cf, uword n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400526 unix_main_t *um = &unix_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
528 vec_delete (cf->output_vector, n_bytes, 0);
529 if (vec_len (cf->output_vector) <= 0)
530 {
531 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
532 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400533 if (!skip_update)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534 um->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
535 }
536}
537
Chris Luke8e5b0412016-07-26 13:06:10 -0400538/** @brief A bit like strchr with a buffer length limit.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400539 * Search a buffer for the first instance of a character up to the limit of
540 * the buffer length. If found then return the position of that character.
541 *
542 * The key departure from strchr is that if the character is not found then
543 * return the buffer length.
544 *
545 * @param chr The byte value to search for.
546 * @param str The buffer in which to search for the value.
547 * @param len The depth into the buffer to search.
548 *
549 * @return The index of the first occurence of \c chr. If \c chr is not
550 * found then \c len instead.
551 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400552always_inline word
553unix_vlib_findchr (u8 chr, u8 * str, word len)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400554{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400555 word i = 0;
556 for (i = 0; i < len; i++, str++)
557 {
558 if (*str == chr)
559 return i;
560 }
561 return len;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400562}
563
Chris Luke8e5b0412016-07-26 13:06:10 -0400564/** @brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400565 * Attempts to write given buffer to the file descriptor of the given
566 * Unix CLI session. If that session already has data in the output buffer
567 * or if the write attempt tells us to try again later then the given buffer
568 * is appended to the pending output buffer instead.
569 *
570 * This is typically called only from \c unix_vlib_cli_output_cooked since
571 * that is where CRLF handling occurs or from places where we explicitly do
572 * not want cooked handling.
573 *
574 * @param cf Unix CLI session of the desired stream to write to.
575 * @param uf The Unix file structure of the desired stream to write to.
576 * @param buffer Pointer to the buffer that needs to be written.
577 * @param buffer_bytes The number of bytes from \c buffer to write.
578 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400579static void
580unix_vlib_cli_output_raw (unix_cli_file_t * cf,
581 unix_file_t * uf, u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400582{
583 int n = 0;
584
585 if (vec_len (cf->output_vector) == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400586 n = write (uf->file_descriptor, buffer, buffer_bytes);
Chris Luke0aca5eb2016-04-25 13:48:54 -0400587
588 if (n < 0 && errno != EAGAIN)
589 {
590 clib_unix_warning ("write");
591 }
592 else if ((word) n < (word) buffer_bytes)
593 {
594 /* We got EAGAIN or we already have stuff in the buffer;
595 * queue up whatever didn't get sent for later. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400596 if (n < 0)
597 n = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400598 unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
599 }
600}
601
Chris Luke8e5b0412016-07-26 13:06:10 -0400602/** @brief Process a buffer for CRLF handling before outputting it to the CLI.
Chris Luke0aca5eb2016-04-25 13:48:54 -0400603 *
604 * @param cf Unix CLI session of the desired stream to write to.
605 * @param uf The Unix file structure of the desired stream to write to.
606 * @param buffer Pointer to the buffer that needs to be written.
607 * @param buffer_bytes The number of bytes from \c buffer to write.
608 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400609static void
610unix_vlib_cli_output_cooked (unix_cli_file_t * cf,
611 unix_file_t * uf,
612 u8 * buffer, uword buffer_bytes)
Chris Luke0aca5eb2016-04-25 13:48:54 -0400613{
614 word end = 0, start = 0;
615
616 while (end < buffer_bytes)
617 {
618 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400619 {
620 /* iterate the line on \n's so we can insert a \r before it */
621 end = unix_vlib_findchr ('\n',
622 buffer + start,
623 buffer_bytes - start) + start;
624 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400625 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400626 {
627 /* otherwise just send the whole buffer */
628 end = buffer_bytes;
629 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400630
Dave Barach9b8ffd92016-07-08 08:13:45 -0400631 unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start);
Chris Luke0aca5eb2016-04-25 13:48:54 -0400632
633 if (cf->crlf_mode)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400634 {
635 if (end < buffer_bytes)
636 {
637 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2);
638 end++; /* skip the \n that we already sent */
639 }
640 start = end;
641 }
Chris Luke0aca5eb2016-04-25 13:48:54 -0400642 }
643}
644
Chris Luke8e5b0412016-07-26 13:06:10 -0400645/** @brief Output the CLI prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400646static void
647unix_cli_cli_prompt (unix_cli_file_t * cf, unix_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400648{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400649 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke7afda3a2016-04-25 13:49:22 -0400650
651 unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt, vec_len (cm->cli_prompt));
652}
653
Chris Luke8e5b0412016-07-26 13:06:10 -0400654/** @brief Output a pager prompt and show number of buffered lines */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400655static void
656unix_cli_pager_prompt (unix_cli_file_t * cf, unix_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400657{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400658 u8 *prompt;
Chris Luke270b6de2016-05-19 14:23:25 -0400659 u32 h;
660
661 h = cf->pager_start + (cf->height - 1);
662 if (h > vec_len (cf->pager_index))
663 h = vec_len (cf->pager_index);
Chris Luke7afda3a2016-04-25 13:49:22 -0400664
Dave Barach9b8ffd92016-07-08 08:13:45 -0400665 prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s",
666 cf->ansi_capable ? ANSI_BOLD : "",
667 cf->pager_start + 1,
668 h,
669 vec_len (cf->pager_index),
670 cf->ansi_capable ? ANSI_RESET : "");
Chris Luke7afda3a2016-04-25 13:49:22 -0400671
Dave Barach9b8ffd92016-07-08 08:13:45 -0400672 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400673
Dave Barach9b8ffd92016-07-08 08:13:45 -0400674 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400675}
676
Chris Luke8e5b0412016-07-26 13:06:10 -0400677/** @brief Output a pager "skipping" message */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400678static void
679unix_cli_pager_message (unix_cli_file_t * cf, unix_file_t * uf,
680 char *message, char *postfix)
Chris Luke7afda3a2016-04-25 13:49:22 -0400681{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400682 u8 *prompt;
Chris Luke7afda3a2016-04-25 13:49:22 -0400683
Dave Barach9b8ffd92016-07-08 08:13:45 -0400684 prompt = format (0, "\r%s-- %s --%s%s",
685 cf->ansi_capable ? ANSI_BOLD : "",
686 message, cf->ansi_capable ? ANSI_RESET : "", postfix);
Chris Luke7afda3a2016-04-25 13:49:22 -0400687
Dave Barach9b8ffd92016-07-08 08:13:45 -0400688 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
Chris Luke7afda3a2016-04-25 13:49:22 -0400689
Dave Barach9b8ffd92016-07-08 08:13:45 -0400690 vec_free (prompt);
Chris Luke7afda3a2016-04-25 13:49:22 -0400691}
692
Chris Luke8e5b0412016-07-26 13:06:10 -0400693/** @brief Erase the printed pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400694static void
695unix_cli_pager_prompt_erase (unix_cli_file_t * cf, unix_file_t * uf)
Chris Luke7afda3a2016-04-25 13:49:22 -0400696{
697 if (cf->ansi_capable)
698 {
Dave Barach9b8ffd92016-07-08 08:13:45 -0400699 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
700 unix_vlib_cli_output_cooked (cf, uf,
701 (u8 *) ANSI_CLEARLINE,
702 sizeof (ANSI_CLEARLINE) - 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400703 }
704 else
705 {
706 int i;
707
Dave Barach9b8ffd92016-07-08 08:13:45 -0400708 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
709 for (i = 0; i < cf->width - 1; i++)
710 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
711 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
Chris Luke7afda3a2016-04-25 13:49:22 -0400712 }
713}
714
Chris Luke8e5b0412016-07-26 13:06:10 -0400715/** @brief Uses an ANSI escape sequence to move the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400716static void
717unix_cli_ansi_cursor (unix_cli_file_t * cf, unix_file_t * uf, u16 x, u16 y)
Chris Luke7afda3a2016-04-25 13:49:22 -0400718{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400719 u8 *str;
Chris Luke7afda3a2016-04-25 13:49:22 -0400720
Dave Barach9b8ffd92016-07-08 08:13:45 -0400721 str = format (0, "%s%d;%dH", CSI, y, x);
Chris Luke7afda3a2016-04-25 13:49:22 -0400722
Dave Barach9b8ffd92016-07-08 08:13:45 -0400723 unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str));
Chris Luke7afda3a2016-04-25 13:49:22 -0400724
Dave Barach9b8ffd92016-07-08 08:13:45 -0400725 vec_free (str);
Chris Luke7afda3a2016-04-25 13:49:22 -0400726}
727
Chris Luke862623d2016-05-14 10:13:34 -0400728/** Redraw the currently displayed page of text.
729 * @param cf CLI session to redraw the pager buffer of.
730 * @param uf Unix file of the CLI session.
731 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400732static void
733unix_cli_pager_redraw (unix_cli_file_t * cf, unix_file_t * uf)
Chris Luke862623d2016-05-14 10:13:34 -0400734{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400735 unix_cli_pager_index_t *pi = NULL;
736 u8 *line = NULL;
Chris Luke862623d2016-05-14 10:13:34 -0400737 word i;
738
739 /* No active pager? Do nothing. */
740 if (!vec_len (cf->pager_index))
741 return;
742
743 if (cf->ansi_capable)
744 {
745 /* If we have ANSI, send the clear screen sequence */
746 unix_vlib_cli_output_cooked (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400747 (u8 *) ANSI_CLEAR,
748 sizeof (ANSI_CLEAR) - 1);
Chris Luke862623d2016-05-14 10:13:34 -0400749 }
750 else
751 {
752 /* Otherwise make sure we're on a blank line */
753 unix_cli_pager_prompt_erase (cf, uf);
754 }
755
756 /* (Re-)send the current page of content */
757 for (i = 0; i < cf->height - 1 &&
Dave Barach9b8ffd92016-07-08 08:13:45 -0400758 i + cf->pager_start < vec_len (cf->pager_index); i++)
Chris Luke862623d2016-05-14 10:13:34 -0400759 {
760 pi = &cf->pager_index[cf->pager_start + i];
761 line = cf->pager_vector[pi->line] + pi->offset;
762
763 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
764 }
765 /* if the last line didn't end in newline, add a newline */
766 if (pi && line[pi->length - 1] != '\n')
Dave Barach9b8ffd92016-07-08 08:13:45 -0400767 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke862623d2016-05-14 10:13:34 -0400768
769 unix_cli_pager_prompt (cf, uf);
770}
771
772/** @brief Process and add a line to the pager index.
773 * In normal operation this function will take the given character string
774 * found in @c line and with length @c len_or_index and iterates the over the
775 * contents, adding each line of text discovered within it to the
776 * pager index. Lines are identified by newlines ("<code>\\n</code>") and by
777 * strings longer than the width of the terminal.
778 *
779 * If instead @c line is @c NULL then @c len_or_index is taken to mean the
780 * index of an existing line in the pager buffer; this simply means that the
781 * input line does not need to be cloned since we alreayd have it. This is
782 * typical if we are reindexing the pager buffer.
783 *
784 * @param cf The CLI session whose pager we are adding to.
785 * @param line The string of text to be indexed into the pager buffer.
786 * If @c line is @c NULL then the mode of operation
787 * changes slightly; see the description above.
788 * @param len_or_index If @c line is a pointer to a string then this parameter
789 * indicates the length of that string; Otherwise this
790 * value provides the index in the pager buffer of an
791 * existing string to be indexed.
792 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400793static void
794unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index)
Chris Luke862623d2016-05-14 10:13:34 -0400795{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400796 u8 *p;
Chris Luke862623d2016-05-14 10:13:34 -0400797 word i, j, k;
798 word line_index, len;
799 u32 width = cf->width;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400800 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -0400801
802 if (line == NULL)
803 {
804 /* Use a line already in the pager buffer */
805 line_index = len_or_index;
806 p = cf->pager_vector[line_index];
807 len = vec_len (p);
808 }
809 else
810 {
811 len = len_or_index;
812 /* Add a copy of the raw string to the pager buffer */
813 p = vec_new (u8, len);
814 clib_memcpy (p, line, len);
815
816 /* store in pager buffer */
817 line_index = vec_len (cf->pager_vector);
818 vec_add1 (cf->pager_vector, p);
819 }
820
821 i = 0;
822 while (i < len)
823 {
824 /* Find the next line, or run to terminal width, or run to EOL */
825 int l = len - i;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400826 j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width);
Chris Luke862623d2016-05-14 10:13:34 -0400827
Dave Barach9b8ffd92016-07-08 08:13:45 -0400828 if (j < l && p[j] == '\n') /* incl \n */
829 j++;
Chris Luke862623d2016-05-14 10:13:34 -0400830
831 /* Add the line to the index */
832 k = vec_len (cf->pager_index);
833 vec_validate (cf->pager_index, k);
834 pi = &cf->pager_index[k];
835
836 pi->line = line_index;
837 pi->offset = i;
838 pi->length = j;
839
840 i += j;
841 p += j;
842 }
843}
844
845/** @brief Reindex entire pager buffer.
846 * Resets the current pager index and then re-adds the lines in the pager
847 * buffer to the index.
848 *
849 * Additionally this function attempts to retain the current page start
850 * line offset by searching for the same top-of-screen line in the new index.
851 *
852 * @param cf The CLI session whose pager buffer should be reindexed.
853 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400854static void
855unix_cli_pager_reindex (unix_cli_file_t * cf)
Chris Luke862623d2016-05-14 10:13:34 -0400856{
857 word i, old_line, old_offset;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400858 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -0400859
860 /* If there is nothing in the pager buffer then make sure the index
861 * is empty and move on.
862 */
863 if (cf->pager_vector == 0)
864 {
865 vec_reset_length (cf->pager_index);
866 return;
867 }
868
869 /* Retain a pointer to the current page start line so we can
870 * find it later
871 */
872 pi = &cf->pager_index[cf->pager_start];
873 old_line = pi->line;
874 old_offset = pi->offset;
875
876 /* Re-add the buffered lines to the index */
877 vec_reset_length (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400878 vec_foreach_index (i, cf->pager_vector)
879 {
880 unix_cli_pager_add_line (cf, NULL, i);
881 }
Chris Luke862623d2016-05-14 10:13:34 -0400882
883 /* Attempt to re-locate the previously stored page start line */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400884 vec_foreach_index (i, cf->pager_index)
885 {
886 pi = &cf->pager_index[i];
Chris Luke862623d2016-05-14 10:13:34 -0400887
Dave Barach9b8ffd92016-07-08 08:13:45 -0400888 if (pi->line == old_line &&
889 (pi->offset <= old_offset || pi->offset + pi->length > old_offset))
890 {
891 /* Found it! */
892 cf->pager_start = i;
893 break;
894 }
895 }
Chris Luke862623d2016-05-14 10:13:34 -0400896
897 /* In case the start line was not found (rare), ensure the pager start
898 * index is within bounds
899 */
900 if (cf->pager_start >= vec_len (cf->pager_index))
901 {
Chris Luke270b6de2016-05-19 14:23:25 -0400902 if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400903 cf->pager_start = 0;
Chris Luke270b6de2016-05-19 14:23:25 -0400904 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400905 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
Chris Luke862623d2016-05-14 10:13:34 -0400906 }
907}
908
909/** VLIB CLI output function.
910 *
911 * If the terminal has a pager configured then this function takes care
912 * of collating output into the pager buffer; ensuring only the first page
913 * is displayed and any lines in excess of the first page are buffered.
914 *
915 * If the maximum number of index lines in the buffer is exceeded then the
916 * pager is cancelled and the contents of the current buffer are sent to the
917 * terminal.
918 *
919 * If there is no pager configured then the output is sent directly to the
920 * terminal.
921 *
922 * @param cli_file_index Index of the CLI session where this output is
923 * directed.
924 * @param buffer String of printabe bytes to be output.
925 * @param buffer_bytes The number of bytes in @c buffer to be output.
926 */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400927static void
928unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400930 unix_main_t *um = &unix_main;
931 unix_cli_main_t *cm = &unix_cli_main;
932 unix_cli_file_t *cf;
933 unix_file_t *uf;
Chris Luke0aca5eb2016-04-25 13:48:54 -0400934
Ed Warnickecb9cada2015-12-08 15:45:58 -0700935 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
936 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937
Chris Luke7afda3a2016-04-25 13:49:22 -0400938 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
939 {
Chris Luke862623d2016-05-14 10:13:34 -0400940 unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes);
Chris Luke7afda3a2016-04-25 13:49:22 -0400941 }
942 else
943 {
Chris Luke862623d2016-05-14 10:13:34 -0400944 word row = vec_len (cf->pager_index);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400945 u8 *line;
946 unix_cli_pager_index_t *pi;
Chris Luke7afda3a2016-04-25 13:49:22 -0400947
Chris Luke862623d2016-05-14 10:13:34 -0400948 /* Index and add the output lines to the pager buffer. */
949 unix_cli_pager_add_line (cf, buffer, buffer_bytes);
950
951 /* Now iterate what was added to display the lines.
952 * If we reach the bottom of the page, display a prompt.
953 */
954 while (row < vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400955 {
956 if (row < cf->height - 1)
957 {
958 /* output this line */
959 pi = &cf->pager_index[row];
960 line = cf->pager_vector[pi->line] + pi->offset;
961 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
Chris Luke862623d2016-05-14 10:13:34 -0400962
Dave Barach9b8ffd92016-07-08 08:13:45 -0400963 /* if the last line didn't end in newline, and we're at the
964 * bottom of the page, add a newline */
965 if (line[pi->length - 1] != '\n' && row == cf->height - 2)
966 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
967 }
968 else
969 {
970 /* Display the pager prompt every 10 lines */
971 if (!(row % 10))
972 unix_cli_pager_prompt (cf, uf);
973 }
974 row++;
975 }
Chris Luke7afda3a2016-04-25 13:49:22 -0400976
Chris Luke862623d2016-05-14 10:13:34 -0400977 /* Check if we went over the pager buffer limit */
978 if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400979 {
980 /* Stop using the pager for the remainder of this CLI command */
981 cf->no_pager = 2;
Chris Luke7afda3a2016-04-25 13:49:22 -0400982
Dave Barach9b8ffd92016-07-08 08:13:45 -0400983 /* If we likely printed the prompt, erase it */
984 if (vec_len (cf->pager_index) > cf->height - 1)
985 unix_cli_pager_prompt_erase (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -0400986
Dave Barach9b8ffd92016-07-08 08:13:45 -0400987 /* Dump out the contents of the buffer */
988 for (row = cf->pager_start + (cf->height - 1);
989 row < vec_len (cf->pager_index); row++)
990 {
991 pi = &cf->pager_index[row];
992 line = cf->pager_vector[pi->line] + pi->offset;
993 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
994 }
Chris Luke7afda3a2016-04-25 13:49:22 -0400995
Dave Barach9b8ffd92016-07-08 08:13:45 -0400996 unix_cli_pager_reset (cf);
997 }
Chris Luke7afda3a2016-04-25 13:49:22 -0400998 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700999}
1000
Chris Luke862623d2016-05-14 10:13:34 -04001001/** Identify whether a terminal type is ANSI capable.
1002 *
Chris Luke54ccf222016-07-25 16:38:11 -04001003 * Compares the string given in @c term with a list of terminal types known
Chris Luke862623d2016-05-14 10:13:34 -04001004 * to support ANSI escape sequences.
1005 *
1006 * This list contains, for example, @c xterm, @c screen and @c ansi.
1007 *
1008 * @param term A string with a terminal type in it.
Chris Luke54ccf222016-07-25 16:38:11 -04001009 * @param len The length of the string in @c term.
Chris Luke862623d2016-05-14 10:13:34 -04001010 *
1011 * @return @c 1 if the terminal type is recognized as supporting ANSI
1012 * terminal sequences; @c 0 otherwise.
1013 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001014static u8
1015unix_cli_terminal_type (u8 * term, uword len)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016{
Chris Luke0aca5eb2016-04-25 13:48:54 -04001017 /* This may later be better done as a hash of some sort. */
1018#define _(a) do { \
1019 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1020 } while(0)
1021
1022 _("xterm");
1023 _("xterm-color");
Dave Barach9b8ffd92016-07-08 08:13:45 -04001024 _("xterm-256color"); /* iTerm on Mac */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001025 _("screen");
Dave Barach9b8ffd92016-07-08 08:13:45 -04001026 _("ansi"); /* Microsoft Telnet */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001027#undef _
1028
1029 return 0;
1030}
1031
Chris Luke8e5b0412016-07-26 13:06:10 -04001032/** @brief Emit initial welcome banner and prompt on a connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001033static void
1034unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001035{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001036 unix_main_t *um = &unix_main;
1037 unix_file_t *uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
Chris Luke572d8122016-04-25 13:49:07 -04001038 unix_cli_banner_t *banner;
1039 int i, len;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040
Chris Luke0aca5eb2016-04-25 13:48:54 -04001041 /*
1042 * Put the first bytes directly into the buffer so that further output is
1043 * queued until everything is ready. (oterwise initial prompt can appear
1044 * mid way through VPP initialization)
1045 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001046 unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
Chris Luke572d8122016-04-25 13:49:07 -04001047
1048 if (!um->cli_no_banner)
1049 {
1050 if (cf->ansi_capable)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001051 {
1052 banner = unix_cli_banner_color;
1053 len = ARRAY_LEN (unix_cli_banner_color);
1054 }
Chris Luke572d8122016-04-25 13:49:07 -04001055 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001056 {
1057 banner = unix_cli_banner;
1058 len = ARRAY_LEN (unix_cli_banner);
1059 }
Chris Luke572d8122016-04-25 13:49:07 -04001060
1061 for (i = 0; i < len; i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001062 {
1063 unix_vlib_cli_output_cooked (cf, uf,
1064 banner[i].line, banner[i].length);
1065 }
1066 }
Chris Luke572d8122016-04-25 13:49:07 -04001067
1068 /* Prompt. */
Chris Luke7afda3a2016-04-25 13:49:22 -04001069 unix_cli_cli_prompt (cf, uf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001070
1071 cf->started = 1;
1072}
1073
Chris Luke8e5b0412016-07-26 13:06:10 -04001074/** @brief A failsafe triggered on a timer to ensure we send the prompt
Chris Luke0aca5eb2016-04-25 13:48:54 -04001075 * to telnet sessions that fail to negotiate the terminal type. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001076static void
1077unix_cli_file_welcome_timer (any arg, f64 delay)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001078{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001079 unix_cli_main_t *cm = &unix_cli_main;
1080 unix_cli_file_t *cf;
1081 (void) delay;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001082
1083 /* Check the connection didn't close already */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001084 if (pool_is_free_index (cm->cli_file_pool, (uword) arg))
Chris Luke0aca5eb2016-04-25 13:48:54 -04001085 return;
1086
Dave Barach9b8ffd92016-07-08 08:13:45 -04001087 cf = pool_elt_at_index (cm->cli_file_pool, (uword) arg);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001088
1089 if (!cf->started)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001090 unix_cli_file_welcome (cm, cf);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001091}
1092
Chris Luke8e5b0412016-07-26 13:06:10 -04001093/** @brief A mostly no-op Telnet state machine.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001094 * Process Telnet command bytes in a way that ensures we're mostly
1095 * transparent to the Telnet protocol. That is, it's mostly a no-op.
1096 *
1097 * @return -1 if we need more bytes, otherwise a positive integer number of
1098 * bytes to consume from the input_vector, not including the initial
1099 * IAC byte.
1100 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001101static i32
1102unix_cli_process_telnet (unix_main_t * um,
1103 unix_cli_file_t * cf,
1104 unix_file_t * uf, u8 * input_vector, uword len)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001105{
1106 /* Input_vector starts at IAC byte.
1107 * See if we have a complete message; if not, return -1 so we wait for more.
1108 * if we have a complete message, consume those bytes from the vector.
1109 */
1110 i32 consume = 0;
1111
1112 if (len == 1)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001113 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001114
1115 switch (input_vector[1])
Ed Warnickecb9cada2015-12-08 15:45:58 -07001116 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04001117 case IAC:
1118 /* two IAC's in a row means to pass through 0xff.
1119 * since that makes no sense here, just consume it.
1120 */
1121 consume = 1;
1122 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001123
Dave Barach9b8ffd92016-07-08 08:13:45 -04001124 case WILL:
1125 case WONT:
1126 case DO:
1127 case DONT:
1128 /* Expect 3 bytes */
1129 if (vec_len (input_vector) < 3)
1130 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001131
Dave Barach9b8ffd92016-07-08 08:13:45 -04001132 consume = 2;
1133 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001134
Dave Barach9b8ffd92016-07-08 08:13:45 -04001135 case SB:
1136 {
1137 /* Sub option - search ahead for IAC SE to end it */
1138 i32 i;
1139 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
1140 {
1141 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
1142 {
1143 /* We have a complete message; see if we care about it */
1144 switch (input_vector[2])
1145 {
1146 case TELOPT_TTYPE:
1147 if (input_vector[3] != 0)
1148 break;
1149 /* See if the terminal type is ANSI capable */
1150 cf->ansi_capable =
1151 unix_cli_terminal_type (input_vector + 4, i - 5);
1152 /* If session not started, we can release the pause */
1153 if (!cf->started)
1154 /* Send the welcome banner and initial prompt */
1155 unix_cli_file_welcome (&unix_cli_main, cf);
1156 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001157
Dave Barach9b8ffd92016-07-08 08:13:45 -04001158 case TELOPT_NAWS:
1159 /* Window size */
1160 if (i != 8) /* check message is correct size */
1161 break;
1162 cf->width =
1163 clib_net_to_host_u16 (*((u16 *) (input_vector + 3)));
1164 cf->height =
1165 clib_net_to_host_u16 (*((u16 *) (input_vector + 5)));
1166 /* reindex pager buffer */
1167 unix_cli_pager_reindex (cf);
1168 /* redraw page */
1169 unix_cli_pager_redraw (cf, uf);
1170 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04001171
Dave Barach9b8ffd92016-07-08 08:13:45 -04001172 default:
1173 break;
1174 }
1175 /* Consume it all */
1176 consume = i;
1177 break;
1178 }
1179 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001180
Dave Barach9b8ffd92016-07-08 08:13:45 -04001181 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
1182 consume = 1; /* hit max search depth, advance one byte */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001183
Dave Barach9b8ffd92016-07-08 08:13:45 -04001184 if (consume == 0)
1185 return -1; /* want more bytes */
Chris Luke0aca5eb2016-04-25 13:48:54 -04001186
Dave Barach9b8ffd92016-07-08 08:13:45 -04001187 break;
1188 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001189
Dave Barach9b8ffd92016-07-08 08:13:45 -04001190 case GA:
1191 case EL:
1192 case EC:
1193 case AO:
1194 case IP:
1195 case BREAK:
1196 case DM:
1197 case NOP:
1198 case SE:
1199 case EOR:
1200 case ABORT:
1201 case SUSP:
1202 case xEOF:
1203 /* Simple one-byte messages */
1204 consume = 1;
1205 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001206
Dave Barach9b8ffd92016-07-08 08:13:45 -04001207 case AYT:
1208 /* Are You There - trigger a visible response */
1209 consume = 1;
1210 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
1211 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001212
Dave Barach9b8ffd92016-07-08 08:13:45 -04001213 default:
1214 /* Unknown command! Eat the IAC byte */
1215 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001216 }
1217
Dave Barach9b8ffd92016-07-08 08:13:45 -04001218 return consume;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001219}
1220
Chris Luke8e5b0412016-07-26 13:06:10 -04001221/** @brief Process actionable input.
Chris Luke0aca5eb2016-04-25 13:48:54 -04001222 * Based on the \c action process the input; this typically involves
1223 * searching the command history or editing the current command line.
1224 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001225static int
1226unix_cli_line_process_one (unix_cli_main_t * cm,
1227 unix_main_t * um,
1228 unix_cli_file_t * cf,
1229 unix_file_t * uf,
1230 u8 input, unix_cli_parse_action_t action)
Chris Luke0aca5eb2016-04-25 13:48:54 -04001231{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001232 u8 *prev;
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001233 u8 *save = 0;
1234 u8 **possible_commands;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001235 int j, delta;
1236
1237 switch (action)
1238 {
1239 case UNIX_CLI_PARSE_ACTION_NOACTION:
1240 break;
1241
Chris Luke0aca5eb2016-04-25 13:48:54 -04001242 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
1243 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
Chris Luke7afda3a2016-04-25 13:49:22 -04001244 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001245 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001246 if (cf->search_mode == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001247 {
1248 /* Erase the current command (if any) */
1249 for (j = 0; j < (vec_len (cf->current_command)); j++)
1250 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001251
Dave Barach9b8ffd92016-07-08 08:13:45 -04001252 vec_reset_length (cf->search_key);
1253 vec_reset_length (cf->current_command);
1254 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1255 cf->search_mode = -1;
1256 else
1257 cf->search_mode = 1;
1258 cf->cursor = 0;
1259 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001260 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001261 {
1262 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1263 cf->search_mode = -1;
1264 else
1265 cf->search_mode = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001266
Dave Barach9b8ffd92016-07-08 08:13:45 -04001267 cf->excursion += cf->search_mode;
1268 goto search_again;
1269 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001270 break;
1271
1272 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
1273 /* Erase the command from the cursor to the start */
1274
1275 /* Shimmy forwards to the new end of line position */
1276 delta = vec_len (cf->current_command) - cf->cursor;
1277 for (j = cf->cursor; j > delta; j--)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001278 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001279 /* Zap from here to the end of what is currently displayed */
1280 for (; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001281 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001282 /* Get back to the start of the line */
1283 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001284 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001285
Dave Barach9b8ffd92016-07-08 08:13:45 -04001286 j = vec_len (cf->current_command) - cf->cursor;
1287 memmove (cf->current_command, cf->current_command + cf->cursor, j);
1288 _vec_len (cf->current_command) = j;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001289
1290 /* Print the new contents */
1291 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j);
1292 /* Shimmy back to the start */
1293 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001294 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001295 cf->cursor = 0;
1296
1297 cf->search_mode = 0;
1298 break;
1299
1300 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
1301 /* Erase the command from the cursor to the end */
1302
1303 /* Zap from cursor to end of what is currently displayed */
1304 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001305 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001306 /* Get back to where we were */
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 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001309
1310 /* Truncate the line at the cursor */
Dave Barach9b8ffd92016-07-08 08:13:45 -04001311 _vec_len (cf->current_command) = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001312
1313 cf->search_mode = 0;
1314 break;
1315
1316 case UNIX_CLI_PARSE_ACTION_LEFT:
1317 if (cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001318 {
1319 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1320 cf->cursor--;
1321 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001322
1323 cf->search_mode = 0;
1324 break;
1325
1326 case UNIX_CLI_PARSE_ACTION_RIGHT:
Dave Barach9b8ffd92016-07-08 08:13:45 -04001327 if (cf->cursor < vec_len (cf->current_command))
1328 {
1329 /* have to emit the character under the cursor */
1330 unix_vlib_cli_output_cooked (cf, uf,
1331 cf->current_command + cf->cursor, 1);
1332 cf->cursor++;
1333 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001334
1335 cf->search_mode = 0;
1336 break;
1337
1338 case UNIX_CLI_PARSE_ACTION_UP:
1339 case UNIX_CLI_PARSE_ACTION_DOWN:
Chris Luke7afda3a2016-04-25 13:49:22 -04001340 if (!cf->has_history || !cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001341 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001342 cf->search_mode = 0;
1343 /* Erase the command */
1344 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001345 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001346 for (j = 0; j < (vec_len (cf->current_command)); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001347 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001348 vec_reset_length (cf->current_command);
1349 if (vec_len (cf->command_history))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001350 {
1351 if (action == UNIX_CLI_PARSE_ACTION_UP)
1352 delta = -1;
1353 else
1354 delta = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001355
Dave Barach9b8ffd92016-07-08 08:13:45 -04001356 cf->excursion += delta;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001357
Dave Barach9b8ffd92016-07-08 08:13:45 -04001358 if (cf->excursion == vec_len (cf->command_history))
1359 {
1360 /* down-arrowed to last entry - want a blank line */
1361 _vec_len (cf->current_command) = 0;
1362 }
1363 else if (cf->excursion < 0)
1364 {
1365 /* up-arrowed over the start to the end, want a blank line */
1366 cf->excursion = vec_len (cf->command_history);
1367 _vec_len (cf->current_command) = 0;
1368 }
1369 else
1370 {
1371 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1372 /* down-arrowed past end - wrap to start */
1373 cf->excursion = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001374
Dave Barach9b8ffd92016-07-08 08:13:45 -04001375 /* Print the command at the current position */
1376 prev = cf->command_history[cf->excursion];
1377 vec_validate (cf->current_command, vec_len (prev) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001378
Dave Barach9b8ffd92016-07-08 08:13:45 -04001379 clib_memcpy (cf->current_command, prev, vec_len (prev));
1380 _vec_len (cf->current_command) = vec_len (prev);
1381 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1382 vec_len (cf->current_command));
1383 }
1384 cf->cursor = vec_len (cf->current_command);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001385
Dave Barach9b8ffd92016-07-08 08:13:45 -04001386 break;
1387 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001388 break;
1389
1390 case UNIX_CLI_PARSE_ACTION_HOME:
1391 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001392 {
1393 while (cf->cursor)
1394 {
1395 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1396 cf->cursor--;
1397 }
1398 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001399
Chris Luke0aca5eb2016-04-25 13:48:54 -04001400 cf->search_mode = 0;
1401 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001402
Chris Luke0aca5eb2016-04-25 13:48:54 -04001403 case UNIX_CLI_PARSE_ACTION_END:
1404 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001405 cf->cursor < vec_len (cf->current_command))
1406 {
1407 unix_vlib_cli_output_cooked (cf, uf,
1408 cf->current_command + cf->cursor,
1409 vec_len (cf->current_command) -
1410 cf->cursor);
1411 cf->cursor = vec_len (cf->current_command);
1412 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001413
1414 cf->search_mode = 0;
1415 break;
1416
1417 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1418 if (vec_len (cf->current_command) && cf->cursor > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001419 {
1420 j = cf->cursor;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001421
Dave Barach9b8ffd92016-07-08 08:13:45 -04001422 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1423 j--;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001424
Dave Barach9b8ffd92016-07-08 08:13:45 -04001425 while (j && isspace (cf->current_command[j]))
1426 {
1427 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1428 j--;
1429 }
1430 while (j && !isspace (cf->current_command[j]))
1431 {
1432 if (isspace (cf->current_command[j - 1]))
1433 break;
1434 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1435 j--;
1436 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001437
Dave Barach9b8ffd92016-07-08 08:13:45 -04001438 cf->cursor = j;
1439 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001440
1441 cf->search_mode = 0;
1442 break;
1443
1444 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1445 if (vec_len (cf->current_command) &&
Dave Barach9b8ffd92016-07-08 08:13:45 -04001446 cf->cursor < vec_len (cf->current_command))
1447 {
1448 int e = vec_len (cf->current_command);
1449 j = cf->cursor;
1450 while (j < e && !isspace (cf->current_command[j]))
1451 j++;
1452 while (j < e && isspace (cf->current_command[j]))
1453 j++;
1454 unix_vlib_cli_output_cooked (cf, uf,
1455 cf->current_command + cf->cursor,
1456 j - cf->cursor);
1457 cf->cursor = j;
1458 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001459
1460 cf->search_mode = 0;
1461 break;
1462
1463
1464 case UNIX_CLI_PARSE_ACTION_ERASE:
1465 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001466 {
1467 if (cf->cursor == vec_len (cf->current_command))
1468 {
1469 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1470 _vec_len (cf->current_command)--;
1471 cf->cursor--;
1472 }
1473 else if (cf->cursor > 0)
1474 {
1475 /* shift everything at & to the right of the cursor left by 1 */
1476 j = vec_len (cf->current_command) - cf->cursor;
1477 memmove (cf->current_command + cf->cursor - 1,
1478 cf->current_command + cf->cursor, j);
1479 _vec_len (cf->current_command)--;
1480 cf->cursor--;
1481 /* redraw the rest of the line */
1482 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1483 unix_vlib_cli_output_cooked (cf, uf,
1484 cf->current_command + cf->cursor,
1485 j);
1486 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
1487 /* and shift the terminal cursor back where it should be */
1488 while (--j)
1489 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1490 }
1491 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001492 cf->search_mode = 0;
1493 cf->excursion = 0;
1494 vec_reset_length (cf->search_key);
1495 break;
1496
1497 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1498 if (vec_len (cf->current_command))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001499 {
1500 if (cf->cursor < vec_len (cf->current_command))
1501 {
1502 /* shift everything to the right of the cursor left by 1 */
1503 j = vec_len (cf->current_command) - cf->cursor - 1;
1504 memmove (cf->current_command + cf->cursor,
1505 cf->current_command + cf->cursor + 1, j);
1506 _vec_len (cf->current_command)--;
1507 /* redraw the rest of the line */
1508 unix_vlib_cli_output_cooked (cf, uf,
1509 cf->current_command + cf->cursor,
1510 j);
1511 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
1512 /* and shift the terminal cursor back where it should be */
1513 if (j)
1514 {
1515 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1516 while (--j)
1517 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1518 }
1519 }
1520 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001521 else if (input == 'D' - '@')
Dave Barach9b8ffd92016-07-08 08:13:45 -04001522 {
1523 /* ^D with no command entered = quit */
1524 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1525 vlib_process_signal_event (um->vlib_main,
1526 vlib_current_process (um->vlib_main),
1527 UNIX_CLI_PROCESS_EVENT_QUIT,
1528 cf - cm->cli_file_pool);
1529 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04001530 cf->search_mode = 0;
1531 cf->excursion = 0;
1532 vec_reset_length (cf->search_key);
1533 break;
1534
1535 case UNIX_CLI_PARSE_ACTION_CLEAR:
1536 /* If we're in ANSI mode, clear the screen.
1537 * Then redraw the prompt and any existing command input, then put
1538 * the cursor back where it was in that line.
1539 */
1540 if (cf->ansi_capable)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001541 unix_vlib_cli_output_cooked (cf, uf,
1542 (u8 *) ANSI_CLEAR,
1543 sizeof (ANSI_CLEAR) - 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001544 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001545 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001546
1547 unix_vlib_cli_output_raw (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001548 cm->cli_prompt, vec_len (cm->cli_prompt));
Chris Luke0aca5eb2016-04-25 13:48:54 -04001549 unix_vlib_cli_output_raw (cf, uf,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001550 cf->current_command,
1551 vec_len (cf->current_command));
1552 for (j = cf->cursor; j < vec_len (cf->current_command); j++)
1553 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001554
1555 break;
1556
Chris Luke7afda3a2016-04-25 13:49:22 -04001557 case UNIX_CLI_PARSE_ACTION_TAB:
Yoann Desmouceaux3060e072017-05-18 11:00:48 +02001558 if (cf->cursor < vec_len (cf->current_command))
1559 {
1560 /* if we are in the middle of a line, complete only if
1561 * the cursor points to whitespace */
1562 if (isspace (cf->current_command[cf->cursor]))
1563 {
1564 /* save and clear any input that is after the cursor */
1565 vec_resize (save, vec_len (cf->current_command) - cf->cursor);
1566 clib_memcpy (save, cf->current_command + cf->cursor,
1567 vec_len (cf->current_command) - cf->cursor);
1568 _vec_len (cf->current_command) = cf->cursor;
1569 }
1570 else
1571 {
1572 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1573 break;
1574 }
1575 }
1576 possible_commands =
1577 vlib_cli_get_possible_completions (cf->current_command);
1578 if (vec_len (possible_commands) == 1)
1579 {
1580 u32 j = cf->cursor;
1581 u8 *completed = possible_commands[0];
1582
1583 /* find the last word of current_command */
1584 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1585 {
1586 j--;
1587 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1588 }
1589 _vec_len (cf->current_command) = j;
1590
1591 /* replace it with the newly expanded command */
1592 vec_append (cf->current_command, completed);
1593
1594 /* echo to the terminal */
1595 unix_vlib_cli_output_raw (cf, uf, completed, vec_len (completed));
1596
1597 /* add one trailing space if needed */
1598 if (vec_len (save) == 0)
1599 {
1600 vec_add1 (cf->current_command, ' ');
1601 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1602 }
1603
1604 cf->cursor = vec_len (cf->current_command);
1605
1606 }
1607 else if (vec_len (possible_commands) >= 2)
1608 {
1609 u8 **possible_command;
1610 uword max_command_len = 0, min_command_len = ~0;
1611 u32 i, j;
1612
1613 vec_foreach (possible_command, possible_commands)
1614 {
1615 if (vec_len (*possible_command) > max_command_len)
1616 {
1617 max_command_len = vec_len (*possible_command);
1618 }
1619 if (vec_len (*possible_command) < min_command_len)
1620 {
1621 min_command_len = vec_len (*possible_command);
1622 }
1623 }
1624
1625 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1626
1627 i = 0;
1628 vec_foreach (possible_command, possible_commands)
1629 {
1630 if (i + max_command_len >= cf->width)
1631 {
1632 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1633 i = 0;
1634 }
1635 unix_vlib_cli_output_raw (cf, uf, *possible_command,
1636 vec_len (*possible_command));
1637 for (j = vec_len (*possible_command); j < max_command_len + 2;
1638 j++)
1639 {
1640 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1641 }
1642 i += max_command_len + 2;
1643 }
1644
1645 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1646
1647 /* rewrite prompt */
1648 unix_cli_cli_prompt (cf, uf);
1649 unix_vlib_cli_output_raw (cf, uf, cf->current_command,
1650 vec_len (cf->current_command));
1651
1652 /* count length of last word */
1653 j = cf->cursor;
1654 i = 0;
1655 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1656 {
1657 j--;
1658 i++;
1659 }
1660
1661 /* determine smallest common command */
1662 for (; i < min_command_len; i++)
1663 {
1664 u8 common = '\0';
1665 int stop = 0;
1666 vec_foreach (possible_command, possible_commands)
1667 {
1668 if (common == '\0')
1669 {
1670 common = (*possible_command)[i];
1671 }
1672 else if (common != (*possible_command)[i])
1673 {
1674 stop = 1;
1675 break;
1676 }
1677 }
1678 if (!stop)
1679 {
1680 vec_add1 (cf->current_command, common);
1681 cf->cursor++;
1682 unix_vlib_cli_output_raw (cf, uf, (u8 *) & common, 1);
1683 }
1684 else
1685 {
1686 break;
1687 }
1688 }
1689 }
1690 else
1691 {
1692 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1693 }
1694
1695 if (vec_len (save) > 0)
1696 {
1697 /* restore remaining input if tab was hit in the middle of a line */
1698 unix_vlib_cli_output_raw (cf, uf, save, vec_len (save));
1699 for (j = 0; j < vec_len (save); j++)
1700 {
1701 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1702 }
1703 vec_append (cf->current_command, save);
1704 vec_free (save);
1705 }
1706 vec_free (possible_commands);
1707
1708 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04001709 case UNIX_CLI_PARSE_ACTION_YANK:
1710 /* TODO */
1711 break;
1712
1713
1714 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
1715 pager_quit:
1716 unix_cli_pager_prompt_erase (cf, uf);
1717 unix_cli_pager_reset (cf);
1718 unix_cli_cli_prompt (cf, uf);
1719 break;
1720
1721 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
1722 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
1723 /* show next page of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001724 if (cf->height + cf->pager_start < vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001725 {
1726 u8 *line = NULL;
1727 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001728
Dave Barach9b8ffd92016-07-08 08:13:45 -04001729 int m = cf->pager_start + (cf->height - 1);
1730 unix_cli_pager_prompt_erase (cf, uf);
1731 for (j = m;
1732 j < vec_len (cf->pager_index) && cf->pager_start < m;
1733 j++, cf->pager_start++)
1734 {
1735 pi = &cf->pager_index[j];
1736 line = cf->pager_vector[pi->line] + pi->offset;
1737 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1738 }
1739 /* if the last line didn't end in newline, add a newline */
1740 if (pi && line[pi->length - 1] != '\n')
1741 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1742 unix_cli_pager_prompt (cf, uf);
1743 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001744 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001745 {
1746 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
1747 /* no more in buffer, exit, but only if it was <space> */
1748 goto pager_quit;
1749 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001750 break;
1751
1752 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
1753 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
1754 /* display the next line of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001755 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001756 {
1757 u8 *line;
1758 unix_cli_pager_index_t *pi;
Chris Luke862623d2016-05-14 10:13:34 -04001759
Dave Barach9b8ffd92016-07-08 08:13:45 -04001760 unix_cli_pager_prompt_erase (cf, uf);
1761 pi = &cf->pager_index[cf->pager_start + (cf->height - 1)];
1762 line = cf->pager_vector[pi->line] + pi->offset;
1763 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1764 cf->pager_start++;
1765 /* if the last line didn't end in newline, add a newline */
1766 if (line[pi->length - 1] != '\n')
1767 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1768 unix_cli_pager_prompt (cf, uf);
1769 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001770 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04001771 {
1772 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
1773 /* no more in buffer, exit, but only if it was <enter> */
1774 goto pager_quit;
1775 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001776
1777 break;
1778
1779 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
1780 /* scroll the page back one line */
1781 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001782 {
1783 u8 *line = NULL;
1784 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001785
Dave Barach9b8ffd92016-07-08 08:13:45 -04001786 cf->pager_start--;
1787 if (cf->ansi_capable)
1788 {
1789 pi = &cf->pager_index[cf->pager_start];
1790 line = cf->pager_vector[pi->line] + pi->offset;
1791 unix_cli_pager_prompt_erase (cf, uf);
1792 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN,
1793 sizeof (ANSI_SCROLLDN) - 1);
1794 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR,
1795 sizeof (ANSI_SAVECURSOR) - 1);
1796 unix_cli_ansi_cursor (cf, uf, 1, 1);
1797 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE,
1798 sizeof (ANSI_CLEARLINE) - 1);
1799 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1800 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR,
1801 sizeof (ANSI_RESTCURSOR) - 1);
1802 unix_cli_pager_prompt_erase (cf, uf);
1803 unix_cli_pager_prompt (cf, uf);
1804 }
1805 else
1806 {
1807 int m = cf->pager_start + (cf->height - 1);
1808 unix_cli_pager_prompt_erase (cf, uf);
1809 for (j = cf->pager_start;
1810 j < vec_len (cf->pager_index) && j < m; j++)
1811 {
1812 pi = &cf->pager_index[j];
1813 line = cf->pager_vector[pi->line] + pi->offset;
1814 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1815 }
1816 /* if the last line didn't end in newline, add a newline */
1817 if (pi && line[pi->length - 1] != '\n')
1818 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1819 unix_cli_pager_prompt (cf, uf);
1820 }
1821 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001822 break;
1823
1824 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
1825 /* back to the first page of the buffer */
1826 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001827 {
1828 u8 *line = NULL;
1829 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001830
Dave Barach9b8ffd92016-07-08 08:13:45 -04001831 cf->pager_start = 0;
1832 int m = cf->pager_start + (cf->height - 1);
1833 unix_cli_pager_prompt_erase (cf, uf);
1834 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1835 j++)
1836 {
1837 pi = &cf->pager_index[j];
1838 line = cf->pager_vector[pi->line] + pi->offset;
1839 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1840 }
1841 /* if the last line didn't end in newline, add a newline */
1842 if (pi && line[pi->length - 1] != '\n')
1843 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1844 unix_cli_pager_prompt (cf, uf);
1845 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001846 break;
1847
1848 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
1849 /* skip to the last page of the buffer */
Chris Luke862623d2016-05-14 10:13:34 -04001850 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001851 {
1852 u8 *line = NULL;
1853 unix_cli_pager_index_t *pi = NULL;
Chris Luke862623d2016-05-14 10:13:34 -04001854
Dave Barach9b8ffd92016-07-08 08:13:45 -04001855 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
1856 unix_cli_pager_prompt_erase (cf, uf);
1857 unix_cli_pager_message (cf, uf, "skipping", "\n");
1858 for (j = cf->pager_start; j < vec_len (cf->pager_index); j++)
1859 {
1860 pi = &cf->pager_index[j];
1861 line = cf->pager_vector[pi->line] + pi->offset;
1862 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1863 }
1864 /* if the last line didn't end in newline, add a newline */
1865 if (pi && line[pi->length - 1] != '\n')
1866 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1867 unix_cli_pager_prompt (cf, uf);
1868 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001869 break;
1870
1871 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
1872 /* wander back one page in the buffer */
1873 if (cf->pager_start > 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001874 {
1875 u8 *line = NULL;
1876 unix_cli_pager_index_t *pi = NULL;
1877 int m;
Chris Luke862623d2016-05-14 10:13:34 -04001878
Dave Barach9b8ffd92016-07-08 08:13:45 -04001879 if (cf->pager_start >= cf->height)
1880 cf->pager_start -= cf->height - 1;
1881 else
1882 cf->pager_start = 0;
1883 m = cf->pager_start + cf->height - 1;
1884 unix_cli_pager_prompt_erase (cf, uf);
1885 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1886 j++)
1887 {
1888 pi = &cf->pager_index[j];
1889 line = cf->pager_vector[pi->line] + pi->offset;
1890 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1891 }
1892 /* if the last line didn't end in newline, add a newline */
1893 if (pi && line[pi->length - 1] != '\n')
1894 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1895 unix_cli_pager_prompt (cf, uf);
1896 }
Chris Luke7afda3a2016-04-25 13:49:22 -04001897 break;
1898
Chris Luke862623d2016-05-14 10:13:34 -04001899 case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW:
1900 /* Redraw the current pager screen */
1901 unix_cli_pager_redraw (cf, uf);
1902 break;
1903
Chris Luke7afda3a2016-04-25 13:49:22 -04001904 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
1905 /* search forwards in the buffer */
1906 break;
1907
1908
Chris Luke0aca5eb2016-04-25 13:48:54 -04001909 case UNIX_CLI_PARSE_ACTION_CRLF:
1910 crlf:
Chris Luke0aca5eb2016-04-25 13:48:54 -04001911 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1912
Chris Luke7afda3a2016-04-25 13:49:22 -04001913 if (cf->has_history && cf->history_limit)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001914 {
1915 if (cf->command_history
1916 && vec_len (cf->command_history) >= cf->history_limit)
1917 {
1918 vec_free (cf->command_history[0]);
1919 vec_delete (cf->command_history, 1, 0);
1920 }
1921 /* Don't add blank lines to the cmd history */
1922 if (vec_len (cf->current_command))
1923 {
1924 /* Don't duplicate the previous command */
1925 j = vec_len (cf->command_history);
1926 if (j == 0 ||
1927 (vec_len (cf->current_command) !=
1928 vec_len (cf->command_history[j - 1])
1929 || memcmp (cf->current_command, cf->command_history[j - 1],
1930 vec_len (cf->current_command)) != 0))
1931 {
1932 /* copy the command to the history */
1933 u8 *c = 0;
1934 vec_append (c, cf->current_command);
1935 vec_add1 (cf->command_history, c);
1936 cf->command_number++;
1937 }
1938 }
1939 cf->excursion = vec_len (cf->command_history);
1940 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04001941
Chris Luke0aca5eb2016-04-25 13:48:54 -04001942 cf->search_mode = 0;
1943 vec_reset_length (cf->search_key);
1944 cf->cursor = 0;
1945
1946 return 0;
1947
Chris Luke7afda3a2016-04-25 13:49:22 -04001948 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
1949 case UNIX_CLI_PARSE_ACTION_NOMATCH:
Chris Luke862623d2016-05-14 10:13:34 -04001950 if (vec_len (cf->pager_index))
Dave Barach9b8ffd92016-07-08 08:13:45 -04001951 {
1952 /* no-op for now */
1953 }
1954 else if (cf->has_history && cf->search_mode && isprint (input))
1955 {
1956 int k, limit, offset;
1957 u8 *item;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001958
Dave Barach9b8ffd92016-07-08 08:13:45 -04001959 vec_add1 (cf->search_key, input);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001960
Dave Barach9b8ffd92016-07-08 08:13:45 -04001961 search_again:
1962 for (j = 0; j < vec_len (cf->command_history); j++)
1963 {
1964 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1965 cf->excursion = 0;
1966 else if (cf->excursion < 0)
1967 cf->excursion = vec_len (cf->command_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001968
Dave Barach9b8ffd92016-07-08 08:13:45 -04001969 item = cf->command_history[cf->excursion];
Ed Warnickecb9cada2015-12-08 15:45:58 -07001970
Dave Barach9b8ffd92016-07-08 08:13:45 -04001971 limit = (vec_len (cf->search_key) > vec_len (item)) ?
1972 vec_len (item) : vec_len (cf->search_key);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001973
Dave Barach9b8ffd92016-07-08 08:13:45 -04001974 for (offset = 0; offset <= vec_len (item) - limit; offset++)
1975 {
1976 for (k = 0; k < limit; k++)
1977 {
1978 if (item[k + offset] != cf->search_key[k])
1979 goto next_offset;
1980 }
1981 goto found_at_offset;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001982
Dave Barach9b8ffd92016-07-08 08:13:45 -04001983 next_offset:
1984 ;
1985 }
1986 goto next;
Chris Luke0aca5eb2016-04-25 13:48:54 -04001987
Dave Barach9b8ffd92016-07-08 08:13:45 -04001988 found_at_offset:
1989 for (j = 0; j < vec_len (cf->current_command); j++)
1990 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
Chris Luke0aca5eb2016-04-25 13:48:54 -04001991
Dave Barach9b8ffd92016-07-08 08:13:45 -04001992 vec_validate (cf->current_command, vec_len (item) - 1);
1993 clib_memcpy (cf->current_command, item, vec_len (item));
1994 _vec_len (cf->current_command) = vec_len (item);
Chris Luke93dcd1d2016-05-10 10:45:10 -04001995
Dave Barach9b8ffd92016-07-08 08:13:45 -04001996 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1997 vec_len (cf->current_command));
1998 cf->cursor = vec_len (cf->current_command);
1999 goto found;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002000
Dave Barach9b8ffd92016-07-08 08:13:45 -04002001 next:
2002 cf->excursion += cf->search_mode;
2003 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002004
Dave Barach9b8ffd92016-07-08 08:13:45 -04002005 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12);
2006 vec_reset_length (cf->search_key);
2007 vec_reset_length (cf->current_command);
2008 cf->search_mode = 0;
2009 cf->cursor = 0;
2010 goto crlf;
2011 }
2012 else if (isprint (input)) /* skip any errant control codes */
2013 {
2014 if (cf->cursor == vec_len (cf->current_command))
2015 {
2016 /* Append to end */
2017 vec_add1 (cf->current_command, input);
2018 cf->cursor++;
Chris Luke7afda3a2016-04-25 13:49:22 -04002019
Dave Barach9b8ffd92016-07-08 08:13:45 -04002020 /* Echo the character back to the client */
2021 unix_vlib_cli_output_raw (cf, uf, &input, 1);
2022 }
2023 else
2024 {
2025 /* Insert at cursor: resize +1 byte, move everything over */
2026 j = vec_len (cf->current_command) - cf->cursor;
2027 vec_add1 (cf->current_command, (u8) 'A');
2028 memmove (cf->current_command + cf->cursor + 1,
2029 cf->current_command + cf->cursor, j);
2030 cf->current_command[cf->cursor] = input;
2031 /* Redraw the line */
2032 j++;
2033 unix_vlib_cli_output_raw (cf, uf,
2034 cf->current_command + cf->cursor, j);
2035 /* Put terminal cursor back */
2036 while (--j)
2037 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
2038 cf->cursor++;
2039 }
2040 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002041 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002042 {
2043 /* no-op - not printable or otherwise not actionable */
2044 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002045
2046 found:
2047
2048 break;
Chris Luke7afda3a2016-04-25 13:49:22 -04002049
2050 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2051 break;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002052 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04002053 return 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002054}
2055
Chris Luke8e5b0412016-07-26 13:06:10 -04002056/** @brief Process input bytes on a stream to provide line editing and
Chris Luke0aca5eb2016-04-25 13:48:54 -04002057 * command history in the CLI. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002058static int
2059unix_cli_line_edit (unix_cli_main_t * cm,
2060 unix_main_t * um, unix_cli_file_t * cf)
Chris Luke0aca5eb2016-04-25 13:48:54 -04002061{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002062 unix_file_t *uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002063 int i;
2064
2065 for (i = 0; i < vec_len (cf->input_vector); i++)
2066 {
2067 unix_cli_parse_action_t action;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002068 i32 matched = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04002069 unix_cli_parse_actions_t *a;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002070
Chris Luke7afda3a2016-04-25 13:49:22 -04002071 /* If we're in the pager mode, search the pager actions */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002072 a =
2073 vec_len (cf->pager_index) ? unix_cli_parse_pager :
2074 unix_cli_parse_strings;
Chris Luke7afda3a2016-04-25 13:49:22 -04002075
Chris Luke93dcd1d2016-05-10 10:45:10 -04002076 /* See if the input buffer is some sort of control code */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002077 action = unix_cli_match_action (a, &cf->input_vector[i],
2078 vec_len (cf->input_vector) - i,
2079 &matched);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002080
2081 switch (action)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002082 {
2083 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2084 if (i)
2085 {
2086 /* There was a partial match which means we need more bytes
2087 * than the input buffer currently has.
2088 * Since the bytes before here have been processed, shift
2089 * the remaining contents to the start of the input buffer.
2090 */
2091 vec_delete (cf->input_vector, i, 0);
2092 }
2093 return 1; /* wait for more */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002094
Dave Barach9b8ffd92016-07-08 08:13:45 -04002095 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2096 /* process telnet options */
2097 matched = unix_cli_process_telnet (um, cf, uf,
2098 cf->input_vector + i,
2099 vec_len (cf->input_vector) - i);
2100 if (matched < 0)
2101 {
2102 if (i)
2103 {
2104 /* There was a partial match which means we need more bytes
2105 * than the input buffer currently has.
2106 * Since the bytes before here have been processed, shift
2107 * the remaining contents to the start of the input buffer.
2108 */
2109 vec_delete (cf->input_vector, i, 0);
2110 }
2111 return 1; /* wait for more */
2112 }
2113 break;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002114
Dave Barach9b8ffd92016-07-08 08:13:45 -04002115 default:
2116 /* process the action */
2117 if (!unix_cli_line_process_one (cm, um, cf, uf,
2118 cf->input_vector[i], action))
2119 {
2120 /* CRLF found. Consume the bytes from the input_vector */
2121 vec_delete (cf->input_vector, i + matched, 0);
2122 /* And tell our caller to execute cf->input_command */
2123 return 0;
2124 }
2125 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002126
2127 i += matched;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002128 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002129
Dave Barach9b8ffd92016-07-08 08:13:45 -04002130 vec_reset_length (cf->input_vector);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002131 return 1;
2132}
2133
Chris Luke8e5b0412016-07-26 13:06:10 -04002134/** @brief Process input to a CLI session. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002135static void
2136unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002137{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002138 unix_main_t *um = &unix_main;
2139 unix_file_t *uf;
2140 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002141 unformat_input_t input;
2142 int vlib_parse_eval (u8 *);
2143
Chris Luke93dcd1d2016-05-10 10:45:10 -04002144more:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002145 /* Try vlibplex first. Someday... */
2146 if (0 && vlib_parse_eval (cf->input_vector) == 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002147 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002148
Chris Luke93dcd1d2016-05-10 10:45:10 -04002149 if (cf->line_mode)
2150 {
2151 /* just treat whatever we got as a complete line of input */
2152 cf->current_command = cf->input_vector;
2153 }
2154 else
2155 {
2156 /* Line edit, echo, etc. */
2157 if (unix_cli_line_edit (cm, um, cf))
Dave Barach9b8ffd92016-07-08 08:13:45 -04002158 /* want more input */
2159 return;
Chris Luke93dcd1d2016-05-10 10:45:10 -04002160 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002161
2162 if (um->log_fd)
2163 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002164 static u8 *lv;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002165 vec_reset_length (lv);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002166 lv = format (lv, "%U[%d]: %v",
2167 format_timeval, 0 /* current bat-time */ ,
2168 0 /* current bat-format */ ,
2169 cli_file_index, cf->input_vector);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002170 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002171 int rv __attribute__ ((unused)) =
2172 write (um->log_fd, lv, vec_len (lv));
Ed Warnickecb9cada2015-12-08 15:45:58 -07002173 }
2174 }
2175
Chris Luke93dcd1d2016-05-10 10:45:10 -04002176 /* Copy our input command to a new string */
2177 unformat_init_vector (&input, cf->current_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002178
2179 /* Remove leading white space from input. */
2180 (void) unformat (&input, "");
2181
2182 cm->current_input_file_index = cli_file_index;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002183 cf->pager_start = 0; /* start a new pager session */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002184
2185 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002186 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output,
2187 cli_file_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002188
Ed Warnickecb9cada2015-12-08 15:45:58 -07002189 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
2190 input.buffer = 0;
2191
2192 unformat_free (&input);
2193
Chris Luke93dcd1d2016-05-10 10:45:10 -04002194 /* Re-fetch pointer since pool may have moved. */
2195 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2196 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
2197
Ed Warnickecb9cada2015-12-08 15:45:58 -07002198done:
Chris Luke93dcd1d2016-05-10 10:45:10 -04002199 /* reset vector; we'll re-use it later */
2200 if (cf->line_mode)
2201 vec_reset_length (cf->input_vector);
2202 else
2203 vec_reset_length (cf->current_command);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002204
Chris Luke7afda3a2016-04-25 13:49:22 -04002205 if (cf->no_pager == 2)
2206 {
2207 /* Pager was programmatically disabled */
2208 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
2209 cf->no_pager = um->cli_no_pager;
2210 }
2211
Dave Barach9b8ffd92016-07-08 08:13:45 -04002212 if (vec_len (cf->pager_index) == 0
2213 || vec_len (cf->pager_index) < cf->height)
Chris Luke7afda3a2016-04-25 13:49:22 -04002214 {
2215 /* There was no need for the pager */
2216 unix_cli_pager_reset (cf);
2217
2218 /* Prompt. */
2219 unix_cli_cli_prompt (cf, uf);
2220 }
2221 else
2222 {
2223 /* Display the pager prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002224 unix_cli_pager_prompt (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04002225 }
Chris Luke93dcd1d2016-05-10 10:45:10 -04002226
Dave Barach9b8ffd92016-07-08 08:13:45 -04002227 /* Any residual data in the input vector? */
2228 if (vec_len (cf->input_vector))
2229 goto more;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002230}
2231
Chris Luke54ccf222016-07-25 16:38:11 -04002232/** Destroy a CLI session.
2233 * @note If we destroy the @c stdin session this additionally signals
2234 * the shutdown of VPP.
2235 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002236static void
2237unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002238{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002239 unix_main_t *um = &unix_main;
2240 unix_cli_file_t *cf;
2241 unix_file_t *uf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002242 int i;
2243
2244 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2245 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
2246
2247 /* Quit/EOF on stdin means quit program. */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002248 if (uf->file_descriptor == UNIX_CLI_STDIN_FD)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002249 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
2250
2251 vec_free (cf->current_command);
2252 vec_free (cf->search_key);
2253
2254 for (i = 0; i < vec_len (cf->command_history); i++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002255 vec_free (cf->command_history[i]);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002256
2257 vec_free (cf->command_history);
2258
2259 unix_file_del (um, uf);
2260
2261 unix_cli_file_free (cf);
2262 pool_put (cm->cli_file_pool, cf);
2263}
2264
Chris Luke54ccf222016-07-25 16:38:11 -04002265/** Handle system events. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002266static uword
2267unix_cli_process (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002268 vlib_node_runtime_t * rt, vlib_frame_t * f)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002269{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002270 unix_cli_main_t *cm = &unix_cli_main;
2271 uword i, *data = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002272
2273 while (1)
2274 {
2275 unix_cli_process_event_type_t event_type;
2276 vlib_process_wait_for_event (vm);
2277 event_type = vlib_process_get_events (vm, &data);
2278
2279 switch (event_type)
2280 {
2281 case UNIX_CLI_PROCESS_EVENT_READ_READY:
2282 for (i = 0; i < vec_len (data); i++)
2283 unix_cli_process_input (cm, data[i]);
2284 break;
2285
2286 case UNIX_CLI_PROCESS_EVENT_QUIT:
2287 /* Kill this process. */
2288 for (i = 0; i < vec_len (data); i++)
2289 unix_cli_kill (cm, data[i]);
2290 goto done;
2291 }
2292
2293 if (data)
2294 _vec_len (data) = 0;
2295 }
2296
Dave Barach9b8ffd92016-07-08 08:13:45 -04002297done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002298 vec_free (data);
2299
2300 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
2301
2302 /* Add node index so we can re-use this process later. */
2303 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
2304
2305 return 0;
2306}
2307
Chris Luke54ccf222016-07-25 16:38:11 -04002308/** Called when a CLI session file descriptor can be written to without
2309 * blocking. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002310static clib_error_t *
2311unix_cli_write_ready (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002312{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002313 unix_cli_main_t *cm = &unix_cli_main;
2314 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002315 int n;
2316
2317 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2318
2319 /* Flush output vector. */
2320 n = write (uf->file_descriptor,
2321 cf->output_vector, vec_len (cf->output_vector));
2322
2323 if (n < 0 && errno != EAGAIN)
2324 return clib_error_return_unix (0, "write");
2325
2326 else if (n > 0)
2327 unix_cli_del_pending_output (uf, cf, n);
2328
2329 return /* no error */ 0;
2330}
2331
Chris Luke54ccf222016-07-25 16:38:11 -04002332/** Called when a CLI session file descriptor has data to be read. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002333static clib_error_t *
2334unix_cli_read_ready (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002335{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002336 unix_main_t *um = &unix_main;
2337 unix_cli_main_t *cm = &unix_cli_main;
2338 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002339 uword l;
2340 int n, n_read, n_try;
2341
2342 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2343
2344 n = n_try = 4096;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002345 while (n == n_try)
2346 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002347 l = vec_len (cf->input_vector);
2348 vec_resize (cf->input_vector, l + n_try);
2349
2350 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
2351
2352 /* Error? */
2353 if (n < 0 && errno != EAGAIN)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002354 return clib_error_return_unix (0, "read");
2355
Ed Warnickecb9cada2015-12-08 15:45:58 -07002356 n_read = n < 0 ? 0 : n;
2357 _vec_len (cf->input_vector) = l + n_read;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002358 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002359
Dave Barach9b8ffd92016-07-08 08:13:45 -04002360 if (!(n < 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002361 vlib_process_signal_event (um->vlib_main,
2362 cf->process_node_index,
2363 (n_read == 0
2364 ? UNIX_CLI_PROCESS_EVENT_QUIT
2365 : UNIX_CLI_PROCESS_EVENT_READ_READY),
2366 /* event data */ uf->private_data);
2367
2368 return /* no error */ 0;
2369}
2370
Chris Lukeb5850972016-05-03 16:34:59 -04002371/** Store a new CLI session.
2372 * @param name The name of the session.
2373 * @param fd The file descriptor for the session I/O.
2374 * @return The session ID.
2375 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002376static u32
2377unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002378{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002379 unix_main_t *um = &unix_main;
2380 unix_cli_file_t *cf;
2381 unix_file_t template = { 0 };
2382 vlib_main_t *vm = um->vlib_main;
2383 vlib_node_t *n;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002384
2385 name = (char *) format (0, "unix-cli-%s", name);
2386
2387 if (vec_len (cm->unused_cli_process_node_indices) > 0)
2388 {
2389 uword l = vec_len (cm->unused_cli_process_node_indices);
2390
2391 /* Find node and give it new name. */
2392 n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
2393 vec_free (n->name);
2394 n->name = (u8 *) name;
2395
2396 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
2397
2398 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
2399 }
2400 else
2401 {
2402 static vlib_node_registration_t r = {
2403 .function = unix_cli_process,
2404 .type = VLIB_NODE_TYPE_PROCESS,
Dave Barach96e12032016-06-09 15:32:48 -04002405 .process_log2_n_stack_bytes = 16,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002406 };
2407
2408 r.name = name;
2409 vlib_register_node (vm, &r);
2410 vec_free (name);
2411
2412 n = vlib_get_node (vm, r.index);
2413 }
2414
2415 pool_get (cm->cli_file_pool, cf);
2416 memset (cf, 0, sizeof (*cf));
2417
2418 template.read_function = unix_cli_read_ready;
2419 template.write_function = unix_cli_write_ready;
2420 template.file_descriptor = fd;
2421 template.private_data = cf - cm->cli_file_pool;
2422
2423 cf->process_node_index = n->index;
2424 cf->unix_file_index = unix_file_add (um, &template);
2425 cf->output_vector = 0;
2426 cf->input_vector = 0;
2427
Ed Warnickecb9cada2015-12-08 15:45:58 -07002428 vlib_start_process (vm, n->runtime_index);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002429
Dave Barach9b8ffd92016-07-08 08:13:45 -04002430 vlib_process_t *p = vlib_get_process_from_node (vm, n);
Andrew Yourtchenko716d9592016-05-10 10:51:34 +00002431 p->output_function = unix_vlib_cli_output;
2432 p->output_function_arg = cf - cm->cli_file_pool;
2433
Ed Warnickecb9cada2015-12-08 15:45:58 -07002434 return cf - cm->cli_file_pool;
2435}
2436
Chris Lukeb5850972016-05-03 16:34:59 -04002437/** Telnet listening socket has a new connection. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002438static clib_error_t *
2439unix_cli_listen_read_ready (unix_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002440{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002441 unix_main_t *um = &unix_main;
2442 unix_cli_main_t *cm = &unix_cli_main;
2443 clib_socket_t *s = &um->cli_listen_socket;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002444 clib_socket_t client;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002445 char *client_name;
2446 clib_error_t *error;
2447 unix_cli_file_t *cf;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002448 u32 cf_index;
2449
2450 error = clib_socket_accept (s, &client);
2451 if (error)
2452 return error;
2453
2454 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
2455
2456 cf_index = unix_cli_file_add (cm, client_name, client.fd);
2457 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2458
2459 /* No longer need CLIB version of socket. */
2460 clib_socket_free (&client);
2461
2462 vec_free (client_name);
2463
2464 /* if we're supposed to run telnet session in character mode (default) */
2465 if (um->cli_line_mode == 0)
2466 {
Chris Luke0aca5eb2016-04-25 13:48:54 -04002467 /*
2468 * Set telnet client character mode, echo on, suppress "go-ahead".
2469 * Technically these should be negotiated, but this works.
Ed Warnickecb9cada2015-12-08 15:45:58 -07002470 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002471 u8 charmode_option[] = {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002472 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
2473 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
2474 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
2475 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
2476 IAC, WILL, TELOPT_ECHO, /* server will do echo */
2477 IAC, DONT, TELOPT_ECHO, /* client should not echo */
2478 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
2479 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
2480 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
2481 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002482 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002483
Chris Luke0aca5eb2016-04-25 13:48:54 -04002484 /* Enable history on this CLI */
Chris Luke7afda3a2016-04-25 13:49:22 -04002485 cf->history_limit = um->cli_history_limit;
2486 cf->has_history = cf->history_limit != 0;
2487
2488 /* Make sure this session is in line mode */
2489 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002490
2491 /* We need CRLF */
2492 cf->crlf_mode = 1;
2493
Chris Luke7afda3a2016-04-25 13:49:22 -04002494 /* Setup the pager */
2495 cf->no_pager = um->cli_no_pager;
2496
Ed Warnickecb9cada2015-12-08 15:45:58 -07002497 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002498
2499 /* Send the telnet options */
2500 unix_vlib_cli_output_raw (cf, uf, charmode_option,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002501 ARRAY_LEN (charmode_option));
Chris Luke0aca5eb2016-04-25 13:48:54 -04002502
2503 /* In case the client doesn't negotiate terminal type, use
2504 * a timer to kick off the initial prompt. */
2505 timer_call (unix_cli_file_welcome_timer, cf_index, 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002506 }
2507
2508 return error;
2509}
2510
Chris Lukeb5850972016-05-03 16:34:59 -04002511/** The system terminal has informed us that the window size
2512 * has changed.
2513 */
Chris Luke7afda3a2016-04-25 13:49:22 -04002514static void
2515unix_cli_resize_interrupt (int signum)
2516{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002517 unix_main_t *um = &unix_main;
2518 unix_cli_main_t *cm = &unix_cli_main;
2519 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2520 cm->stdin_cli_file_index);
2521 unix_file_t *uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04002522 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002523 (void) signum;
Chris Luke7afda3a2016-04-25 13:49:22 -04002524
2525 /* Terminal resized, fetch the new size */
Dave Barachb2a6e252016-07-27 10:00:58 -04002526 if (ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws) < 0)
2527 {
2528 /* "Should never happen..." */
2529 clib_unix_warning ("TIOCGWINSZ");
2530 /* We can't trust ws.XXX... */
2531 return;
2532 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002533 cf->width = ws.ws_col;
2534 cf->height = ws.ws_row;
Chris Luke862623d2016-05-14 10:13:34 -04002535
2536 /* Reindex the pager buffer */
2537 unix_cli_pager_reindex (cf);
2538
2539 /* Redraw the page */
2540 unix_cli_pager_redraw (cf, uf);
Chris Luke7afda3a2016-04-25 13:49:22 -04002541}
2542
Chris Lukeb5850972016-05-03 16:34:59 -04002543/** Handle configuration directives in the @em unix section. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002544static clib_error_t *
2545unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
2546{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002547 unix_main_t *um = &unix_main;
2548 unix_cli_main_t *cm = &unix_cli_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002549 int flags;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002550 clib_error_t *error = 0;
2551 unix_cli_file_t *cf;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002552 u32 cf_index;
2553 struct termios tio;
Chris Luke7afda3a2016-04-25 13:49:22 -04002554 struct sigaction sa;
2555 struct winsize ws;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002556 u8 *term;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002557
2558 /* We depend on unix flags being set. */
2559 if ((error = vlib_call_config_function (vm, unix_config)))
2560 return error;
2561
2562 if (um->flags & UNIX_FLAG_INTERACTIVE)
2563 {
Ed Warnickecb9cada2015-12-08 15:45:58 -07002564 /* Set stdin to be non-blocking. */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002565 if ((flags = fcntl (UNIX_CLI_STDIN_FD, F_GETFL, 0)) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002566 flags = 0;
Dave Barachb2a6e252016-07-27 10:00:58 -04002567 (void) fcntl (UNIX_CLI_STDIN_FD, F_SETFL, flags | O_NONBLOCK);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002568
Chris Luke0aca5eb2016-04-25 13:48:54 -04002569 cf_index = unix_cli_file_add (cm, "stdin", UNIX_CLI_STDIN_FD);
2570 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
Chris Luke7afda3a2016-04-25 13:49:22 -04002571 cm->stdin_cli_file_index = cf_index;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002572
2573 /* If stdin is a tty and we are using chacracter mode, enable
2574 * history on the CLI and set the tty line discipline accordingly. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002575 if (isatty (UNIX_CLI_STDIN_FD) && um->cli_line_mode == 0)
2576 {
2577 /* Capture terminal resize events */
Ed Warnicke853e7202016-08-12 11:42:26 -07002578 memset (&sa, 0, sizeof (sa));
Dave Barach9b8ffd92016-07-08 08:13:45 -04002579 sa.sa_handler = unix_cli_resize_interrupt;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002580 if (sigaction (SIGWINCH, &sa, 0) < 0)
2581 clib_panic ("sigaction");
Chris Luke7afda3a2016-04-25 13:49:22 -04002582
Dave Barach9b8ffd92016-07-08 08:13:45 -04002583 /* Retrieve the current terminal size */
2584 ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws);
2585 cf->width = ws.ws_col;
2586 cf->height = ws.ws_row;
Chris Luke7afda3a2016-04-25 13:49:22 -04002587
Dave Barach9b8ffd92016-07-08 08:13:45 -04002588 if (cf->width == 0 || cf->height == 0)
2589 /* We have a tty, but no size. Stick to line mode. */
2590 goto notty;
Chris Luke7afda3a2016-04-25 13:49:22 -04002591
Dave Barach9b8ffd92016-07-08 08:13:45 -04002592 /* Setup the history */
2593 cf->history_limit = um->cli_history_limit;
2594 cf->has_history = cf->history_limit != 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04002595
Dave Barach9b8ffd92016-07-08 08:13:45 -04002596 /* Setup the pager */
2597 cf->no_pager = um->cli_no_pager;
Chris Luke7afda3a2016-04-25 13:49:22 -04002598
Dave Barach9b8ffd92016-07-08 08:13:45 -04002599 /* We're going to be in char by char mode */
2600 cf->line_mode = 0;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002601
Dave Barach9b8ffd92016-07-08 08:13:45 -04002602 /* Save the original tty state so we can restore it later */
2603 tcgetattr (UNIX_CLI_STDIN_FD, &um->tio_stdin);
2604 um->tio_isset = 1;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002605
Dave Barach9b8ffd92016-07-08 08:13:45 -04002606 /* Tweak the tty settings */
2607 tio = um->tio_stdin;
2608 /* echo off, canonical mode off, ext'd input processing off */
2609 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
2610 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
2611 tio.c_cc[VTIME] = 0; /* no timer */
2612 tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &tio);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002613
Dave Barach9b8ffd92016-07-08 08:13:45 -04002614 /* See if we can do ANSI/VT100 output */
2615 term = (u8 *) getenv ("TERM");
2616 if (term != NULL)
2617 cf->ansi_capable = unix_cli_terminal_type (term,
2618 strlen ((char *)
2619 term));
2620 }
Chris Luke7afda3a2016-04-25 13:49:22 -04002621 else
Dave Barach9b8ffd92016-07-08 08:13:45 -04002622 {
2623 notty:
2624 /* No tty, so make sure these things are off */
2625 cf->no_pager = 1;
2626 cf->history_limit = 0;
2627 cf->has_history = 0;
2628 cf->line_mode = 1;
2629 }
Chris Luke0aca5eb2016-04-25 13:48:54 -04002630
2631 /* Send banner and initial prompt */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002632 unix_cli_file_welcome (cm, cf);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002633 }
2634
Ed Warnickea25bd1c2016-04-01 22:43:37 -05002635 /* If we have socket config, LISTEN, otherwise, don't */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002636 clib_socket_t *s = &um->cli_listen_socket;
2637 if (s->config && s->config[0] != 0)
2638 {
2639 /* CLI listen. */
2640 unix_file_t template = { 0 };
Ed Warnickecb9cada2015-12-08 15:45:58 -07002641
Dave Barach9b8ffd92016-07-08 08:13:45 -04002642 s->flags = SOCKET_IS_SERVER; /* listen, don't connect */
2643 error = clib_socket_init (s);
Ed Warnickea25bd1c2016-04-01 22:43:37 -05002644
Dave Barach9b8ffd92016-07-08 08:13:45 -04002645 if (error)
2646 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002647
Dave Barach9b8ffd92016-07-08 08:13:45 -04002648 template.read_function = unix_cli_listen_read_ready;
2649 template.file_descriptor = s->fd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002650
Dave Barach9b8ffd92016-07-08 08:13:45 -04002651 unix_file_add (um, &template);
2652 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07002653
2654 /* Set CLI prompt. */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002655 if (!cm->cli_prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002656 cm->cli_prompt = format (0, "VLIB: ");
2657
2658 return 0;
2659}
2660
Chris Luke90f52bf2016-09-12 08:55:13 -04002661/*?
2662 * This module has no configurable parameters.
2663?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07002664VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
2665
Chris Luke54ccf222016-07-25 16:38:11 -04002666/** Called when VPP is shutting down, this restores the system
2667 * terminal state if previously saved.
Chris Lukeb5850972016-05-03 16:34:59 -04002668 */
Chris Luke0aca5eb2016-04-25 13:48:54 -04002669static clib_error_t *
2670unix_cli_exit (vlib_main_t * vm)
2671{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002672 unix_main_t *um = &unix_main;
Chris Luke0aca5eb2016-04-25 13:48:54 -04002673
2674 /* If stdin is a tty and we saved the tty state, reset the tty state */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002675 if (isatty (UNIX_CLI_STDIN_FD) && um->tio_isset)
2676 tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &um->tio_stdin);
Chris Luke0aca5eb2016-04-25 13:48:54 -04002677
2678 return 0;
2679}
2680
2681VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
2682
Chris Lukeb5850972016-05-03 16:34:59 -04002683/** Set the CLI prompt.
Chris Luke54ccf222016-07-25 16:38:11 -04002684 * @param prompt The C string to set the prompt to.
Chris Lukeb5850972016-05-03 16:34:59 -04002685 * @note This setting is global; it impacts all current
2686 * and future CLI sessions.
2687 */
Dave Barach9b8ffd92016-07-08 08:13:45 -04002688void
2689vlib_unix_cli_set_prompt (char *prompt)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002690{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002691 char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s ";
2692 unix_cli_main_t *cm = &unix_cli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002693 if (cm->cli_prompt)
2694 vec_free (cm->cli_prompt);
2695 cm->cli_prompt = format (0, fmt, prompt);
2696}
2697
Chris Lukeb5850972016-05-03 16:34:59 -04002698/** CLI command to quit the terminal session.
2699 * @note If this is a stdin session then this will
2700 * shutdown VPP also.
2701 */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002702static clib_error_t *
2703unix_cli_quit (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002704 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002705{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002706 unix_cli_main_t *cm = &unix_cli_main;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002707
2708 vlib_process_signal_event (vm,
2709 vlib_current_process (vm),
2710 UNIX_CLI_PROCESS_EVENT_QUIT,
2711 cm->current_input_file_index);
2712 return 0;
2713}
2714
Chris Luke54ccf222016-07-25 16:38:11 -04002715/*?
2716 * Terminates the current CLI session.
2717 *
2718 * If VPP is running in @em interactive mode and this is the console session
2719 * (that is, the session on @c stdin) then this will also terminate VPP.
2720?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002721/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002722VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
2723 .path = "quit",
2724 .short_help = "Exit CLI",
2725 .function = unix_cli_quit,
2726};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002727/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002728
Chris Lukeb5850972016-05-03 16:34:59 -04002729/** CLI command to execute a VPP command script. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002730static clib_error_t *
2731unix_cli_exec (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002732 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002733{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002734 char *file_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002735 int fd;
2736 unformat_input_t sub_input;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002737 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002738
2739 file_name = 0;
2740 fd = -1;
2741 error = 0;
2742
Dave Barach9b8ffd92016-07-08 08:13:45 -04002743 if (!unformat (input, "%s", &file_name))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002744 {
2745 error = clib_error_return (0, "expecting file name, got `%U'",
2746 format_unformat_error, input);
2747 goto done;
2748 }
2749
2750 fd = open (file_name, O_RDONLY);
2751 if (fd < 0)
2752 {
2753 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
2754 goto done;
2755 }
2756
2757 /* Make sure its a regular file. */
2758 {
2759 struct stat s;
2760
2761 if (fstat (fd, &s) < 0)
2762 {
2763 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
2764 goto done;
2765 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04002766
2767 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002768 {
2769 error = clib_error_return (0, "not a regular file `%s'", file_name);
2770 goto done;
2771 }
2772 }
2773
2774 unformat_init_unix_file (&sub_input, fd);
2775
2776 vlib_cli_input (vm, &sub_input, 0, 0);
2777 unformat_free (&sub_input);
2778
Dave Barach9b8ffd92016-07-08 08:13:45 -04002779done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002780 if (fd > 0)
2781 close (fd);
2782 vec_free (file_name);
2783
2784 return error;
2785}
2786
Chris Luke54ccf222016-07-25 16:38:11 -04002787/*?
2788 * Executes a sequence of CLI commands which are read from a file.
2789 *
2790 * If a command is unrecognised or otherwise invalid then the usual CLI
2791 * feedback will be generated, however execution of subsequent commands
2792 * from the file will continue.
2793?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002794/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002795VLIB_CLI_COMMAND (cli_exec, static) = {
2796 .path = "exec",
2797 .short_help = "Execute commands from file",
2798 .function = unix_cli_exec,
Dave Barachb2ef4dd2016-03-23 08:56:01 -04002799 .is_mp_safe = 1,
Ed Warnickecb9cada2015-12-08 15:45:58 -07002800};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002801/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002802
Chris Lukeb5850972016-05-03 16:34:59 -04002803/** CLI command to show various unix error statistics. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002804static clib_error_t *
2805unix_show_errors (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002806 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002807{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002808 unix_main_t *um = &unix_main;
2809 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002810 int i, n_errors_to_show;
Dave Barach9b8ffd92016-07-08 08:13:45 -04002811 unix_error_history_t *unix_errors = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002812
2813 n_errors_to_show = 1 << 30;
2814
2815 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2816 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002817 if (!unformat (input, "%d", &n_errors_to_show))
Ed Warnickecb9cada2015-12-08 15:45:58 -07002818 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002819 error =
2820 clib_error_return (0,
2821 "expecting integer number of errors to show, got `%U'",
2822 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002823 goto done;
2824 }
2825 }
2826
Dave Barach9b8ffd92016-07-08 08:13:45 -04002827 n_errors_to_show =
2828 clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002829
Dave Barach9b8ffd92016-07-08 08:13:45 -04002830 i =
2831 um->error_history_index >
2832 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002833
2834 while (n_errors_to_show > 0)
2835 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002836 unix_error_history_t *eh = um->error_history + i;
Ed Warnickecb9cada2015-12-08 15:45:58 -07002837
Dave Barach9b8ffd92016-07-08 08:13:45 -04002838 if (!eh->error)
Ed Warnickecb9cada2015-12-08 15:45:58 -07002839 break;
2840
2841 vec_add1 (unix_errors, eh[0]);
2842 n_errors_to_show -= 1;
2843 if (i == 0)
2844 i = ARRAY_LEN (um->error_history) - 1;
2845 else
2846 i--;
2847 }
2848
2849 if (vec_len (unix_errors) == 0)
2850 vlib_cli_output (vm, "no Unix errors so far");
2851 else
2852 {
2853 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
2854 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
2855 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002856 unix_error_history_t *eh = vec_elt_at_index (unix_errors, i);
Ed Warnickecb9cada2015-12-08 15:45:58 -07002857 vlib_cli_output (vm, "%U: %U",
2858 format_time_interval, "h:m:s:u", eh->time,
2859 format_clib_error, eh->error);
2860 }
2861 vlib_cli_output (vm, "%U: time now",
2862 format_time_interval, "h:m:s:u", vlib_time_now (vm));
2863 }
2864
Dave Barach9b8ffd92016-07-08 08:13:45 -04002865done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07002866 vec_free (unix_errors);
2867 return error;
2868}
2869
Dave Barach9b8ffd92016-07-08 08:13:45 -04002870/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002871VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
2872 .path = "show unix-errors",
2873 .short_help = "Show Unix system call error history",
2874 .function = unix_show_errors,
2875};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002876/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002877
Chris Lukeb5850972016-05-03 16:34:59 -04002878/** CLI command to show session command history. */
Ed Warnickecb9cada2015-12-08 15:45:58 -07002879static clib_error_t *
Chris Luke6b90fe32016-04-25 13:49:15 -04002880unix_cli_show_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002881 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke6b90fe32016-04-25 13:49:15 -04002882{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002883 unix_cli_main_t *cm = &unix_cli_main;
2884 unix_cli_file_t *cf;
Chris Luke6b90fe32016-04-25 13:49:15 -04002885 int i, j;
2886
2887 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2888
Chris Luke7afda3a2016-04-25 13:49:22 -04002889 if (cf->has_history && cf->history_limit)
Chris Luke6b90fe32016-04-25 13:49:15 -04002890 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04002891 i = 1 + cf->command_number - vec_len (cf->command_history);
Chris Luke7afda3a2016-04-25 13:49:22 -04002892 for (j = 0; j < vec_len (cf->command_history); j++)
Dave Barach9b8ffd92016-07-08 08:13:45 -04002893 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
Chris Luke7afda3a2016-04-25 13:49:22 -04002894 }
2895 else
2896 {
2897 vlib_cli_output (vm, "History not enabled.\n");
Chris Luke6b90fe32016-04-25 13:49:15 -04002898 }
2899
2900 return 0;
2901}
2902
Chris Luke54ccf222016-07-25 16:38:11 -04002903/*?
2904 * Displays the command history for the current session, if any.
2905?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002906/* *INDENT-OFF* */
Chris Luke6b90fe32016-04-25 13:49:15 -04002907VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
2908 .path = "history",
2909 .short_help = "Show current session command history",
2910 .function = unix_cli_show_history,
2911};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002912/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04002913
Chris Lukeb5850972016-05-03 16:34:59 -04002914/** CLI command to show terminal status. */
Chris Luke7afda3a2016-04-25 13:49:22 -04002915static clib_error_t *
2916unix_cli_show_terminal (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002917 unformat_input_t * input, vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04002918{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002919 unix_main_t *um = &unix_main;
2920 unix_cli_main_t *cm = &unix_cli_main;
2921 unix_cli_file_t *cf;
2922 vlib_node_t *n;
Chris Luke7afda3a2016-04-25 13:49:22 -04002923
2924 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2925 n = vlib_get_node (vm, cf->process_node_index);
2926
2927 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002928 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
2929 "line-by-line" : "char-by-char");
Chris Luke7afda3a2016-04-25 13:49:22 -04002930 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
2931 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002932 vlib_cli_output (vm, "ANSI capable: %s\n",
2933 cf->ansi_capable ? "yes" : "no");
Chris Luke7afda3a2016-04-25 13:49:22 -04002934 vlib_cli_output (vm, "History enabled: %s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04002935 cf->has_history ? "yes" : "no", !cf->has_history
2936 || cf->history_limit ? "" :
2937 " (disabled by history limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04002938 if (cf->has_history)
2939 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
2940 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
Dave Barach9b8ffd92016-07-08 08:13:45 -04002941 cf->no_pager ? "no" : "yes",
2942 cf->no_pager
2943 || cf->height ? "" : " (disabled by terminal height)",
2944 cf->no_pager
2945 || um->cli_pager_buffer_limit ? "" :
2946 " (disabled by buffer limit)");
Chris Luke7afda3a2016-04-25 13:49:22 -04002947 if (!cf->no_pager)
2948 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
Dave Barach9b8ffd92016-07-08 08:13:45 -04002949 vlib_cli_output (vm, "CRLF mode: %s\n",
2950 cf->crlf_mode ? "CR+LF" : "LF");
Chris Luke7afda3a2016-04-25 13:49:22 -04002951
2952 return 0;
2953}
2954
Chris Luke54ccf222016-07-25 16:38:11 -04002955/*?
2956 * Displays various information about the state of the current terminal
2957 * session.
2958 *
2959 * @cliexpar
2960 * @cliexstart{show terminal}
2961 * Terminal name: unix-cli-stdin
2962 * Terminal mode: char-by-char
2963 * Terminal width: 123
2964 * Terminal height: 48
2965 * ANSI capable: yes
2966 * History enabled: yes
2967 * History limit: 50
2968 * Pager enabled: yes
2969 * Pager limit: 100000
2970 * CRLF mode: LF
2971 * @cliexend
2972?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04002973/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04002974VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
2975 .path = "show terminal",
2976 .short_help = "Show current session terminal settings",
2977 .function = unix_cli_show_terminal,
2978};
Dave Barach9b8ffd92016-07-08 08:13:45 -04002979/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04002980
Chris Lukeb5850972016-05-03 16:34:59 -04002981/** CLI command to set terminal pager settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04002982static clib_error_t *
2983unix_cli_set_terminal_pager (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04002984 unformat_input_t * input,
2985 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04002986{
Dave Barach9b8ffd92016-07-08 08:13:45 -04002987 unix_main_t *um = &unix_main;
2988 unix_cli_main_t *cm = &unix_cli_main;
2989 unix_cli_file_t *cf;
2990 unformat_input_t _line_input, *line_input = &_line_input;
Billy McFalla9a20e72017-02-15 11:39:12 -05002991 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04002992
Dave Barach9b8ffd92016-07-08 08:13:45 -04002993 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04002994 return 0;
2995
2996 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2997
2998 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2999 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003000 if (unformat (line_input, "on"))
3001 cf->no_pager = 0;
3002 else if (unformat (line_input, "off"))
3003 cf->no_pager = 1;
3004 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
3005 vlib_cli_output (vm,
3006 "Pager limit set to %u lines; note, this is global.\n",
3007 um->cli_pager_buffer_limit);
3008 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003009 {
3010 error = clib_error_return (0, "unknown parameter: `%U`",
3011 format_unformat_error, line_input);
3012 goto done;
3013 }
Dave Barach9b8ffd92016-07-08 08:13:45 -04003014 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003015
Billy McFalla9a20e72017-02-15 11:39:12 -05003016done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003017 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003018
Billy McFalla9a20e72017-02-15 11:39:12 -05003019 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003020}
3021
Chris Luke54ccf222016-07-25 16:38:11 -04003022/*?
3023 * Enables or disables the terminal pager for this session. Generally
3024 * this defaults to enabled.
3025 *
3026 * Additionally allows the pager buffer size to be set; though note that
3027 * this value is set globally and not per session.
3028?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003029/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003030VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
3031 .path = "set terminal pager",
3032 .short_help = "set terminal pager [on|off] [limit <lines>]",
3033 .function = unix_cli_set_terminal_pager,
3034};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003035/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003036
Chris Lukeb5850972016-05-03 16:34:59 -04003037/** CLI command to set terminal history settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003038static clib_error_t *
3039unix_cli_set_terminal_history (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003040 unformat_input_t * input,
3041 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003042{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003043 unix_cli_main_t *cm = &unix_cli_main;
3044 unix_cli_file_t *cf;
3045 unformat_input_t _line_input, *line_input = &_line_input;
Chris Luke7afda3a2016-04-25 13:49:22 -04003046 u32 limit;
Billy McFalla9a20e72017-02-15 11:39:12 -05003047 clib_error_t *error = 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003048
Dave Barach9b8ffd92016-07-08 08:13:45 -04003049 if (!unformat_user (input, unformat_line_input, line_input))
Chris Luke7afda3a2016-04-25 13:49:22 -04003050 return 0;
3051
3052 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3053
3054 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3055 {
Dave Barach9b8ffd92016-07-08 08:13:45 -04003056 if (unformat (line_input, "on"))
3057 cf->has_history = 1;
3058 else if (unformat (line_input, "off"))
3059 cf->has_history = 0;
3060 else if (unformat (line_input, "limit %u", &cf->history_limit))
3061 ;
3062 else
Billy McFalla9a20e72017-02-15 11:39:12 -05003063 {
3064 error = clib_error_return (0, "unknown parameter: `%U`",
3065 format_unformat_error, line_input);
3066 goto done;
3067 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003068
Dave Barach9b8ffd92016-07-08 08:13:45 -04003069 /* If we reduced history size, or turned it off, purge the history */
3070 limit = cf->has_history ? cf->history_limit : 0;
Chris Luke7afda3a2016-04-25 13:49:22 -04003071
Dave Barach9b8ffd92016-07-08 08:13:45 -04003072 while (cf->command_history && vec_len (cf->command_history) >= limit)
3073 {
3074 vec_free (cf->command_history[0]);
3075 vec_delete (cf->command_history, 1, 0);
3076 }
3077 }
Chris Luke7afda3a2016-04-25 13:49:22 -04003078
Billy McFalla9a20e72017-02-15 11:39:12 -05003079done:
Dave Barach9b8ffd92016-07-08 08:13:45 -04003080 unformat_free (line_input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003081
Billy McFalla9a20e72017-02-15 11:39:12 -05003082 return error;
Chris Luke7afda3a2016-04-25 13:49:22 -04003083}
3084
Chris Luke54ccf222016-07-25 16:38:11 -04003085/*?
3086 * Enables or disables the command history function of the current
3087 * terminal. Generally this defaults to enabled.
3088 *
3089 * This command also allows the maximum size of the history buffer for
3090 * this session to be altered.
3091?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003092/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003093VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
3094 .path = "set terminal history",
3095 .short_help = "set terminal history [on|off] [limit <lines>]",
3096 .function = unix_cli_set_terminal_history,
3097};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003098/* *INDENT-ON* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003099
Chris Lukeb5850972016-05-03 16:34:59 -04003100/** CLI command to set terminal ANSI settings. */
Chris Luke7afda3a2016-04-25 13:49:22 -04003101static clib_error_t *
3102unix_cli_set_terminal_ansi (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -04003103 unformat_input_t * input,
3104 vlib_cli_command_t * cmd)
Chris Luke7afda3a2016-04-25 13:49:22 -04003105{
Dave Barach9b8ffd92016-07-08 08:13:45 -04003106 unix_cli_main_t *cm = &unix_cli_main;
3107 unix_cli_file_t *cf;
Chris Luke7afda3a2016-04-25 13:49:22 -04003108
3109 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3110
3111 if (unformat (input, "on"))
3112 cf->ansi_capable = 1;
3113 else if (unformat (input, "off"))
3114 cf->ansi_capable = 0;
3115 else
3116 return clib_error_return (0, "unknown parameter: `%U`",
Dave Barach9b8ffd92016-07-08 08:13:45 -04003117 format_unformat_error, input);
Chris Luke7afda3a2016-04-25 13:49:22 -04003118
3119 return 0;
3120}
3121
Chris Luke54ccf222016-07-25 16:38:11 -04003122/*?
3123 * Enables or disables the use of ANSI control sequences by this terminal.
3124 * The default will vary based on terminal detection at the start of the
3125 * session.
3126 *
3127 * ANSI control sequences are used in a small number of places to provide,
3128 * for example, color text output and to control the cursor in the pager.
3129?*/
Dave Barach9b8ffd92016-07-08 08:13:45 -04003130/* *INDENT-OFF* */
Chris Luke7afda3a2016-04-25 13:49:22 -04003131VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
3132 .path = "set terminal ansi",
3133 .short_help = "set terminal ansi [on|off]",
3134 .function = unix_cli_set_terminal_ansi,
3135};
Dave Barach9b8ffd92016-07-08 08:13:45 -04003136/* *INDENT-ON* */
Chris Luke6b90fe32016-04-25 13:49:15 -04003137
3138static clib_error_t *
Ed Warnickecb9cada2015-12-08 15:45:58 -07003139unix_cli_init (vlib_main_t * vm)
3140{
3141 return 0;
3142}
3143
3144VLIB_INIT_FUNCTION (unix_cli_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04003145
3146/*
3147 * fd.io coding-style-patch-verification: ON
3148 *
3149 * Local Variables:
3150 * eval: (c-set-style "gnu")
3151 * End:
3152 */