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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * cli.c: Unix stdin/socket CLI.
18 * Copyright (c) 2008 Eliot Dresselhaus
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:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
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.
40 * @file vlib/vlib/unix/cli.c
41 * @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.
46 #include <vlib/vlib.h>
47 #include <vlib/unix/unix.h>
48 #include <vppinfra/timer.h>
56 #include <arpa/telnet.h>
57 #include <sys/ioctl.h>
59 /** ANSI escape code. */
62 /** ANSI Control Sequence Introducer. */
65 /** ANSI clear screen. */
66 #define ANSI_CLEAR CSI "2J" CSI "1;1H"
67 /** ANSI reset color settings. */
68 #define ANSI_RESET CSI "0m"
69 /** ANSI Start bold text. */
70 #define ANSI_BOLD CSI "1m"
71 /** ANSI Stop bold text. */
72 #define ANSI_DIM CSI "2m"
73 /** ANSI Start dark red text. */
74 #define ANSI_DRED ANSI_DIM CSI "31m"
75 /** ANSI Start bright red text. */
76 #define ANSI_BRED ANSI_BOLD CSI "31m"
77 /** ANSI clear line cursor is on. */
78 #define ANSI_CLEARLINE CSI "2K"
79 /** ANSI scroll screen down one line. */
80 #define ANSI_SCROLLDN CSI "1T"
81 /** ANSI save cursor position. */
82 #define ANSI_SAVECURSOR CSI "s"
83 /** ANSI restore cursor position if previously saved. */
84 #define ANSI_RESTCURSOR CSI "u"
86 /** Maximum depth into a byte stream from which to compile a Telnet
87 * protocol message. This is a saftey measure. */
88 #define UNIX_CLI_MAX_DEPTH_TELNET 24
90 /** Unix standard in */
91 #define UNIX_CLI_STDIN_FD 0
94 /** A CLI banner line. */
96 u8 * line; /**< The line to print. */
97 u32 length; /**< The length of the line without terminating NUL. */
100 #define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
101 /** Plain welcome banner. */
102 static unix_cli_banner_t unix_cli_banner[] = {
103 _(" _______ _ _ _____ ___ \n"),
104 _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
105 _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
106 _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
109 /** ANSI color welcome banner. */
110 static unix_cli_banner_t unix_cli_banner_color[] = {
111 _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
112 _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
113 _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
114 _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"),
120 /** Unix CLI session. */
122 /** The file index held by unix.c */
125 /** Vector of output pending write to file descriptor. */
128 /** Vector of input saved by Unix input node to be processed by
133 u8 ** command_history;
134 u8 * current_command;
137 /** Maximum number of history entries this session will store. */
140 /** Current command line counter */
146 /** Position of the insert cursor on the current input line */
149 /** Line mode or char mode */
152 /** Set if the CRLF mode wants CR + LF */
155 /** Can we do ANSI output? */
158 /** Has the session started? */
161 /** Disable the pager? */
167 /** Lines currently displayed */
170 /** Line number of top of page */
173 /** Terminal width */
176 /** Terminal height */
179 /** Process node identifier */
180 u32 process_node_index;
183 /** Resets the pager buffer and other data.
184 * @param f The CLI session whose pager needs to be reset.
187 unix_cli_pager_reset (unix_cli_file_t *f)
191 f->pager_lines = f->pager_start = 0;
192 vec_foreach (p, f->pager_vector)
196 vec_free(f->pager_vector);
200 /** Release storage used by a CLI session.
201 * @param f The CLI session whose storage needs to be released.
204 unix_cli_file_free (unix_cli_file_t * f)
206 vec_free (f->output_vector);
207 vec_free (f->input_vector);
208 unix_cli_pager_reset(f);
213 UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */
214 UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */
215 UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */
216 UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */
217 UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */
218 UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */
219 UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */
220 UNIX_CLI_PARSE_ACTION_LEFT,
221 UNIX_CLI_PARSE_ACTION_RIGHT,
222 UNIX_CLI_PARSE_ACTION_HOME,
223 UNIX_CLI_PARSE_ACTION_END,
224 UNIX_CLI_PARSE_ACTION_WORDLEFT,
225 UNIX_CLI_PARSE_ACTION_WORDRIGHT,
226 UNIX_CLI_PARSE_ACTION_ERASELINELEFT,
227 UNIX_CLI_PARSE_ACTION_ERASELINERIGHT,
228 UNIX_CLI_PARSE_ACTION_CLEAR,
229 UNIX_CLI_PARSE_ACTION_REVSEARCH,
230 UNIX_CLI_PARSE_ACTION_FWDSEARCH,
231 UNIX_CLI_PARSE_ACTION_YANK,
232 UNIX_CLI_PARSE_ACTION_TELNETIAC,
234 UNIX_CLI_PARSE_ACTION_PAGER_CRLF,
235 UNIX_CLI_PARSE_ACTION_PAGER_QUIT,
236 UNIX_CLI_PARSE_ACTION_PAGER_NEXT,
237 UNIX_CLI_PARSE_ACTION_PAGER_DN,
238 UNIX_CLI_PARSE_ACTION_PAGER_UP,
239 UNIX_CLI_PARSE_ACTION_PAGER_TOP,
240 UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM,
241 UNIX_CLI_PARSE_ACTION_PAGER_PGDN,
242 UNIX_CLI_PARSE_ACTION_PAGER_PGUP,
243 UNIX_CLI_PARSE_ACTION_PAGER_SEARCH,
245 UNIX_CLI_PARSE_ACTION_PARTIALMATCH,
246 UNIX_CLI_PARSE_ACTION_NOMATCH
247 } unix_cli_parse_action_t;
249 /** \brief Mapping of input buffer strings to action values.
250 * @note This won't work as a hash since we need to be able to do
251 * partial matches on the string.
254 u8 *input; /**< Input string to match. */
255 u32 len; /**< Length of input without final NUL. */
256 unix_cli_parse_action_t action; /**< Action to take when matched. */
257 } unix_cli_parse_actions_t;
259 /** @brief Given a capital ASCII letter character return a @c NUL terminated
260 * string with the control code for that letter.
262 * @param c An ASCII character.
263 * @return A @c NUL terminated string of type @c u8[].
266 * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[].
268 #define CTL(c) (u8[]){ (c) - '@', 0 }
270 #define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
272 * Patterns to match on a CLI input stream.
275 static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
277 _( "\r\n", UNIX_CLI_PARSE_ACTION_CRLF ), /* Must be before '\r' */
278 _( "\n", UNIX_CLI_PARSE_ACTION_CRLF ),
279 _( "\r\0", UNIX_CLI_PARSE_ACTION_CRLF ), /* Telnet does this */
280 _( "\r", UNIX_CLI_PARSE_ACTION_CRLF ),
282 /* Unix shell control codes */
283 _( CTL('B'), UNIX_CLI_PARSE_ACTION_LEFT ),
284 _( CTL('F'), UNIX_CLI_PARSE_ACTION_RIGHT ),
285 _( CTL('P'), UNIX_CLI_PARSE_ACTION_UP ),
286 _( CTL('N'), UNIX_CLI_PARSE_ACTION_DOWN ),
287 _( CTL('A'), UNIX_CLI_PARSE_ACTION_HOME ),
288 _( CTL('E'), UNIX_CLI_PARSE_ACTION_END ),
289 _( CTL('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT ),
290 _( CTL('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT ),
291 _( CTL('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT ),
292 _( CTL('Y'), UNIX_CLI_PARSE_ACTION_YANK ),
293 _( CTL('L'), UNIX_CLI_PARSE_ACTION_CLEAR ),
294 _( ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT ), /* Alt-B */
295 _( ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT ), /* Alt-F */
296 _( "\b", UNIX_CLI_PARSE_ACTION_ERASE ), /* ^H */
297 _( "\x7f", UNIX_CLI_PARSE_ACTION_ERASE ), /* Backspace */
298 _( "\t", UNIX_CLI_PARSE_ACTION_TAB ), /* ^I */
300 /* VT100 Normal mode - Broadest support */
301 _( CSI "A", UNIX_CLI_PARSE_ACTION_UP ),
302 _( CSI "B", UNIX_CLI_PARSE_ACTION_DOWN ),
303 _( CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT ),
304 _( CSI "D", UNIX_CLI_PARSE_ACTION_LEFT ),
305 _( CSI "H", UNIX_CLI_PARSE_ACTION_HOME ),
306 _( CSI "F", UNIX_CLI_PARSE_ACTION_END ),
307 _( CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT ), /* Delete */
308 _( CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT ), /* C-Left */
309 _( CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT ),/* C-Right */
311 /* VT100 Application mode - Some Gnome Terminal functions use these */
312 _( ESC "OA", UNIX_CLI_PARSE_ACTION_UP ),
313 _( ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN ),
314 _( ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT ),
315 _( ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT ),
316 _( ESC "OH", UNIX_CLI_PARSE_ACTION_HOME ),
317 _( ESC "OF", UNIX_CLI_PARSE_ACTION_END ),
319 /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
320 _( CSI "1~", UNIX_CLI_PARSE_ACTION_HOME ),
321 _( CSI "4~", UNIX_CLI_PARSE_ACTION_END ),
323 /* Emacs-ish history search */
324 _( CTL('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH ),
325 _( CTL('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH ),
327 /* Other protocol things */
328 _( "\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC ), /* IAC */
329 _( "\0", UNIX_CLI_PARSE_ACTION_NOACTION ), /* NUL */
330 _( NULL, UNIX_CLI_PARSE_ACTION_NOMATCH )
334 * Patterns to match when a CLI session is in the pager.
337 static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
339 _( "\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ), /* Must be before '\r' */
340 _( "\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ),
341 _( "\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ), /* Telnet does this */
342 _( "\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ),
345 _( " ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT ),
346 _( "q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT ),
347 _( "/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH ),
350 _( CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP ),
351 _( CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN ),
352 _( CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP ),
353 _( CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM ),
355 /* VT100 Application mode */
356 _( ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP ),
357 _( ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN ),
358 _( ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP ),
359 _( ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM ),
361 /* ANSI X3.41-1974 */
362 _( CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP ),
363 _( CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM ),
364 _( CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP ),
365 _( CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN ),
367 /* Other protocol things */
368 _( "\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC ), /* IAC */
369 _( "\0", UNIX_CLI_PARSE_ACTION_NOACTION ), /* NUL */
370 _( NULL, UNIX_CLI_PARSE_ACTION_NOMATCH )
374 /** CLI session events. */
376 UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */
377 UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */
378 } unix_cli_process_event_type_t;
380 /** CLI global state. */
382 /** Prompt string for CLI. */
385 /** Vec pool of CLI sessions. */
386 unix_cli_file_t * cli_file_pool;
388 /** Vec pool of unused session indices. */
389 u32 * unused_cli_process_node_indices;
391 /** The session index of the stdin cli */
392 u32 stdin_cli_file_index;
394 /** File pool index of current input. */
395 u32 current_input_file_index;
398 /** CLI global state */
399 static unix_cli_main_t unix_cli_main;
402 * \brief Search for a byte sequence in the action list.
404 * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with
405 * the bytes in @a input of maximum length @a ilen bytes.
406 * When a match is made @a *matched indicates how many bytes were matched.
407 * Returns a value from the enum @ref unix_cli_parse_action_t to indicate
408 * whether no match was found, a partial match was found or a complete
409 * match was found and what action, if any, should be taken.
411 * @param[in] a Actions list to search within.
412 * @param[in] input String fragment to search for.
413 * @param[in] ilen Length of the string in 'input'.
414 * @param[out] matched Pointer to an integer that will contain the number
415 * of bytes matched when a complete match is found.
417 * @return Action from @ref unix_cli_parse_action_t that the string fragment
419 * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the
420 * whole input string matches the start of at least one action.
421 * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
424 static unix_cli_parse_action_t
425 unix_cli_match_action(unix_cli_parse_actions_t *a,
426 u8 *input, u32 ilen, i32 *matched)
434 /* see if the start of the input buffer exactly matches the current
436 if (memcmp(input, a->input, a->len) == 0)
444 /* if the first ilen characters match, flag this as a partial -
445 * meaning keep collecting bytes in case of a future match */
446 if (memcmp(input, a->input, ilen) == 0)
450 /* check next action */
455 UNIX_CLI_PARSE_ACTION_PARTIALMATCH :
456 UNIX_CLI_PARSE_ACTION_NOMATCH;
461 unix_cli_add_pending_output (unix_file_t * uf,
462 unix_cli_file_t * cf,
466 unix_main_t * um = &unix_main;
468 vec_add (cf->output_vector, buffer, buffer_bytes);
469 if (vec_len (cf->output_vector) > 0)
471 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
472 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
474 um->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
479 unix_cli_del_pending_output (unix_file_t * uf,
480 unix_cli_file_t * cf,
483 unix_main_t * um = &unix_main;
485 vec_delete (cf->output_vector, n_bytes, 0);
486 if (vec_len (cf->output_vector) <= 0)
488 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
489 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
491 um->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
495 /** \brief A bit like strchr with a buffer length limit.
496 * Search a buffer for the first instance of a character up to the limit of
497 * the buffer length. If found then return the position of that character.
499 * The key departure from strchr is that if the character is not found then
500 * return the buffer length.
502 * @param chr The byte value to search for.
503 * @param str The buffer in which to search for the value.
504 * @param len The depth into the buffer to search.
506 * @return The index of the first occurence of \c chr. If \c chr is not
507 * found then \c len instead.
509 always_inline word unix_vlib_findchr(u8 chr, u8 *str, word len)
512 for (i = 0; i < len; i++, str++)
520 /** \brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
521 * Attempts to write given buffer to the file descriptor of the given
522 * Unix CLI session. If that session already has data in the output buffer
523 * or if the write attempt tells us to try again later then the given buffer
524 * is appended to the pending output buffer instead.
526 * This is typically called only from \c unix_vlib_cli_output_cooked since
527 * that is where CRLF handling occurs or from places where we explicitly do
528 * not want cooked handling.
530 * @param cf Unix CLI session of the desired stream to write to.
531 * @param uf The Unix file structure of the desired stream to write to.
532 * @param buffer Pointer to the buffer that needs to be written.
533 * @param buffer_bytes The number of bytes from \c buffer to write.
535 static void unix_vlib_cli_output_raw(unix_cli_file_t * cf,
542 if (vec_len (cf->output_vector) == 0)
543 n = write (uf->file_descriptor, buffer, buffer_bytes);
545 if (n < 0 && errno != EAGAIN)
547 clib_unix_warning ("write");
549 else if ((word) n < (word) buffer_bytes)
551 /* We got EAGAIN or we already have stuff in the buffer;
552 * queue up whatever didn't get sent for later. */
554 unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
558 /** \brief Process a buffer for CRLF handling before outputting it to the CLI.
560 * @param cf Unix CLI session of the desired stream to write to.
561 * @param uf The Unix file structure of the desired stream to write to.
562 * @param buffer Pointer to the buffer that needs to be written.
563 * @param buffer_bytes The number of bytes from \c buffer to write.
565 static void unix_vlib_cli_output_cooked(unix_cli_file_t * cf,
570 word end = 0, start = 0;
572 while (end < buffer_bytes)
576 /* iterate the line on \n's so we can insert a \r before it */
577 end = unix_vlib_findchr('\n',
579 buffer_bytes - start) + start;
583 /* otherwise just send the whole buffer */
587 unix_vlib_cli_output_raw(cf, uf, buffer + start, end - start);
591 if (end < buffer_bytes)
593 unix_vlib_cli_output_raw(cf, uf, (u8 *)"\r\n", 2);
594 end ++; /* skip the \n that we already sent */
601 /** \brief Output the CLI prompt */
602 static void unix_cli_cli_prompt(unix_cli_file_t * cf, unix_file_t * uf)
604 unix_cli_main_t * cm = &unix_cli_main;
606 unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt, vec_len (cm->cli_prompt));
609 /** \brief Output a pager prompt and show number of buffered lines */
610 static void unix_cli_pager_prompt(unix_cli_file_t * cf, unix_file_t * uf)
614 prompt = format(0, "\r%s-- more -- (%d-%d/%d)%s",
615 cf->ansi_capable ? ANSI_BOLD : "",
617 cf->pager_start + cf->height,
619 cf->ansi_capable ? ANSI_RESET: "");
621 unix_vlib_cli_output_cooked(cf, uf, prompt, vec_len(prompt));
626 /** \brief Output a pager "skipping" message */
627 static void unix_cli_pager_message(unix_cli_file_t * cf, unix_file_t * uf,
628 char *message, char *postfix)
632 prompt = format(0, "\r%s-- %s --%s%s",
633 cf->ansi_capable ? ANSI_BOLD : "",
635 cf->ansi_capable ? ANSI_RESET: "",
638 unix_vlib_cli_output_cooked(cf, uf, prompt, vec_len(prompt));
643 /** \brief Erase the printed pager prompt */
644 static void unix_cli_pager_prompt_erase(unix_cli_file_t * cf, unix_file_t * uf)
646 if (cf->ansi_capable)
648 unix_vlib_cli_output_cooked(cf, uf, (u8 *)"\r", 1);
649 unix_vlib_cli_output_cooked(cf, uf,
650 (u8 *)ANSI_CLEARLINE, sizeof(ANSI_CLEARLINE) - 1);
656 unix_vlib_cli_output_cooked(cf, uf, (u8 *)"\r", 1);
657 for (i = 0; i < cf->width - 1; i ++)
658 unix_vlib_cli_output_cooked(cf, uf, (u8 *)" ", 1);
659 unix_vlib_cli_output_cooked(cf, uf, (u8 *)"\r", 1);
663 /** \brief Uses an ANSI escape sequence to move the cursor */
664 static void unix_cli_ansi_cursor(unix_cli_file_t * cf, unix_file_t * uf,
669 str = format(0, "%s%d;%dH", CSI, y, x);
671 unix_vlib_cli_output_cooked(cf, uf, str, vec_len(str));
676 /** \brief VLIB CLI output function. */
677 static void unix_vlib_cli_output (uword cli_file_index,
681 unix_main_t * um = &unix_main;
682 unix_cli_main_t * cm = &unix_cli_main;
683 unix_cli_file_t * cf;
686 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
687 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
689 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
691 unix_vlib_cli_output_cooked(cf, uf, buffer, buffer_bytes);
695 /* At each \n add the line to the pager buffer.
696 * If we've not yet displayed a whole page in this command then
697 * also output the line.
698 * If we have displayed a whole page then display a pager prompt
699 * and count the lines we've buffered.
703 uword i, len = buffer_bytes;
707 i = unix_vlib_findchr('\n', p, len);
709 i ++; /* include the '\n' */
711 /* make a vec out of this substring */
712 line = vec_new(u8, i);
715 /* store in pager buffer */
716 vec_add1(cf->pager_vector, line);
719 if (cf->pager_lines < cf->height)
721 /* output this line */
722 unix_vlib_cli_output_cooked(cf, uf, p, i);
723 /* TODO: stop if line longer than cf->width and would
724 * overflow cf->height */
728 /* Display the pager prompt every 10 lines */
729 if (!(cf->pager_lines % 10))
730 unix_cli_pager_prompt(cf, uf);
737 /* Check if we went over the pager buffer limit */
738 if (cf->pager_lines > um->cli_pager_buffer_limit)
740 /* Stop using the pager for the remainder of this CLI command */
743 /* If we likely printed the prompt, erase it */
744 if (cf->pager_lines > cf->height - 1)
745 unix_cli_pager_prompt_erase (cf, uf);
747 /* Dump out the contents of the buffer */
748 for (i = cf->pager_start + (cf->height - 1);
749 i < cf->pager_lines; i ++)
750 unix_vlib_cli_output_cooked (cf, uf,
752 vec_len(cf->pager_vector[i]));
754 unix_cli_pager_reset (cf);
759 /** \brief Identify whether a terminal type is ANSI capable. */
760 static u8 unix_cli_terminal_type(u8 * term, uword len)
762 /* This may later be better done as a hash of some sort. */
764 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
769 _("xterm-256color"); /* iTerm on Mac */
771 _("ansi"); /* Microsoft Telnet */
777 /** \brief Emit initial welcome banner and prompt on a connection. */
778 static void unix_cli_file_welcome(unix_cli_main_t * cm, unix_cli_file_t * cf)
780 unix_main_t * um = &unix_main;
781 unix_file_t * uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
782 unix_cli_banner_t *banner;
786 * Put the first bytes directly into the buffer so that further output is
787 * queued until everything is ready. (oterwise initial prompt can appear
788 * mid way through VPP initialization)
790 unix_cli_add_pending_output (uf, cf, (u8 *)"\r", 1);
792 if (!um->cli_no_banner)
794 if (cf->ansi_capable)
796 banner = unix_cli_banner_color;
797 len = ARRAY_LEN(unix_cli_banner_color);
801 banner = unix_cli_banner;
802 len = ARRAY_LEN(unix_cli_banner);
805 for (i = 0; i < len; i++)
807 unix_vlib_cli_output_cooked(cf, uf,
814 unix_cli_cli_prompt (cf, uf);
819 /** \brief A failsafe triggered on a timer to ensure we send the prompt
820 * to telnet sessions that fail to negotiate the terminal type. */
821 static void unix_cli_file_welcome_timer(any arg, f64 delay)
823 unix_cli_main_t * cm = &unix_cli_main;
824 unix_cli_file_t * cf;
827 /* Check the connection didn't close already */
828 if (pool_is_free_index (cm->cli_file_pool, (uword)arg))
831 cf = pool_elt_at_index (cm->cli_file_pool, (uword)arg);
834 unix_cli_file_welcome(cm, cf);
837 /** \brief A mostly no-op Telnet state machine.
838 * Process Telnet command bytes in a way that ensures we're mostly
839 * transparent to the Telnet protocol. That is, it's mostly a no-op.
841 * @return -1 if we need more bytes, otherwise a positive integer number of
842 * bytes to consume from the input_vector, not including the initial
845 static i32 unix_cli_process_telnet(unix_main_t * um,
846 unix_cli_file_t * cf,
851 /* Input_vector starts at IAC byte.
852 * See if we have a complete message; if not, return -1 so we wait for more.
853 * if we have a complete message, consume those bytes from the vector.
858 return -1; /* want more bytes */
860 switch (input_vector[1])
863 /* two IAC's in a row means to pass through 0xff.
864 * since that makes no sense here, just consume it.
874 if (vec_len(input_vector) < 3)
875 return -1; /* want more bytes */
882 /* Sub option - search ahead for IAC SE to end it */
884 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
886 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
888 /* We have a complete message; see if we care about it */
889 switch (input_vector[2])
892 if (input_vector[3] != 0)
894 /* See if the terminal type is ANSI capable */
896 unix_cli_terminal_type(input_vector + 4, i - 5);
897 /* If session not started, we can release the pause */
899 /* Send the welcome banner and initial prompt */
900 unix_cli_file_welcome(&unix_cli_main, cf);
905 if (i != 8) /* check message is correct size */
907 cf->width = clib_net_to_host_u16(*((u16 *)(input_vector + 3)));
908 cf->height = clib_net_to_host_u16(*((u16 *)(input_vector + 5)));
920 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
921 consume = 1; /* hit max search depth, advance one byte */
924 return -1; /* want more bytes */
942 /* Simple one-byte messages */
947 /* Are You There - trigger a visible response */
949 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
953 /* Unknown command! Eat the IAC byte */
960 /** \brief Process actionable input.
961 * Based on the \c action process the input; this typically involves
962 * searching the command history or editing the current command line.
964 static int unix_cli_line_process_one(unix_cli_main_t * cm,
966 unix_cli_file_t * cf,
969 unix_cli_parse_action_t action)
976 case UNIX_CLI_PARSE_ACTION_NOACTION:
979 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
980 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
981 if (!cf->has_history || !cf->history_limit)
983 if (cf->search_mode == 0)
985 /* Erase the current command (if any) */
986 for (j = 0; j < (vec_len (cf->current_command)); j++)
987 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
989 vec_reset_length (cf->search_key);
990 vec_reset_length (cf->current_command);
991 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
992 cf->search_mode = -1;
999 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1000 cf->search_mode = -1;
1002 cf->search_mode = 1;
1004 cf->excursion += cf->search_mode;
1009 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
1010 /* Erase the command from the cursor to the start */
1012 /* Shimmy forwards to the new end of line position */
1013 delta = vec_len (cf->current_command) - cf->cursor;
1014 for (j = cf->cursor; j > delta; j--)
1015 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1016 /* Zap from here to the end of what is currently displayed */
1017 for (; j < (vec_len (cf->current_command)); j++)
1018 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1019 /* Get back to the start of the line */
1020 for (j = 0; j < (vec_len (cf->current_command)); j++)
1021 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1023 j = vec_len(cf->current_command) - cf->cursor;
1024 memmove(cf->current_command,
1025 cf->current_command + cf->cursor,
1027 _vec_len(cf->current_command) = j;
1029 /* Print the new contents */
1030 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j);
1031 /* Shimmy back to the start */
1032 for (j = 0; j < (vec_len (cf->current_command)); j++)
1033 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1036 cf->search_mode = 0;
1039 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
1040 /* Erase the command from the cursor to the end */
1042 /* Zap from cursor to end of what is currently displayed */
1043 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1044 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1045 /* Get back to where we were */
1046 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1047 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1049 /* Truncate the line at the cursor */
1050 _vec_len(cf->current_command) = cf->cursor;
1052 cf->search_mode = 0;
1055 case UNIX_CLI_PARSE_ACTION_LEFT:
1058 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1062 cf->search_mode = 0;
1065 case UNIX_CLI_PARSE_ACTION_RIGHT:
1066 if (cf->cursor < vec_len(cf->current_command))
1068 /* have to emit the character under the cursor */
1069 unix_vlib_cli_output_cooked (cf, uf, cf->current_command + cf->cursor, 1);
1073 cf->search_mode = 0;
1076 case UNIX_CLI_PARSE_ACTION_UP:
1077 case UNIX_CLI_PARSE_ACTION_DOWN:
1078 if (!cf->has_history || !cf->history_limit)
1080 cf->search_mode = 0;
1081 /* Erase the command */
1082 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1083 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1084 for (j = 0; j < (vec_len (cf->current_command)); j++)
1085 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1086 vec_reset_length (cf->current_command);
1087 if (vec_len (cf->command_history))
1089 if (action == UNIX_CLI_PARSE_ACTION_UP)
1094 cf->excursion += delta;
1096 if (cf->excursion == vec_len (cf->command_history))
1098 /* down-arrowed to last entry - want a blank line */
1099 _vec_len (cf->current_command) = 0;
1101 else if (cf->excursion < 0)
1103 /* up-arrowed over the start to the end, want a blank line */
1104 cf->excursion = vec_len (cf->command_history);
1105 _vec_len (cf->current_command) = 0;
1109 if (cf->excursion > (i32) vec_len (cf->command_history) -1)
1110 /* down-arrowed past end - wrap to start */
1113 /* Print the command at the current position */
1114 prev = cf->command_history [cf->excursion];
1115 vec_validate (cf->current_command, vec_len(prev)-1);
1117 clib_memcpy (cf->current_command, prev, vec_len(prev));
1118 _vec_len (cf->current_command) = vec_len(prev);
1119 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1120 vec_len (cf->current_command));
1122 cf->cursor = vec_len(cf->current_command);
1128 case UNIX_CLI_PARSE_ACTION_HOME:
1129 if (vec_len (cf->current_command) && cf->cursor > 0)
1133 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1138 cf->search_mode = 0;
1141 case UNIX_CLI_PARSE_ACTION_END:
1142 if (vec_len (cf->current_command) &&
1143 cf->cursor < vec_len(cf->current_command))
1145 unix_vlib_cli_output_cooked (cf, uf,
1146 cf->current_command + cf->cursor,
1147 vec_len(cf->current_command) - cf->cursor);
1148 cf->cursor = vec_len(cf->current_command);
1151 cf->search_mode = 0;
1154 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1155 if (vec_len (cf->current_command) && cf->cursor > 0)
1159 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1162 while (j && isspace(cf->current_command[j]))
1164 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1167 while (j && !isspace(cf->current_command[j]))
1169 if (isspace(cf->current_command[j - 1]))
1171 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1178 cf->search_mode = 0;
1181 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1182 if (vec_len (cf->current_command) &&
1183 cf->cursor < vec_len(cf->current_command))
1185 int e = vec_len(cf->current_command);
1187 while (j < e && !isspace(cf->current_command[j]))
1189 while (j < e && isspace(cf->current_command[j]))
1191 unix_vlib_cli_output_cooked (cf, uf,
1192 cf->current_command + cf->cursor,
1197 cf->search_mode = 0;
1201 case UNIX_CLI_PARSE_ACTION_ERASE:
1202 if (vec_len (cf->current_command))
1204 if (cf->cursor == vec_len(cf->current_command))
1206 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1207 _vec_len (cf->current_command)--;
1210 else if (cf->cursor > 0)
1212 /* shift everything at & to the right of the cursor left by 1 */
1213 j = vec_len (cf->current_command) - cf->cursor;
1214 memmove (cf->current_command + cf->cursor - 1,
1215 cf->current_command + cf->cursor,
1217 _vec_len (cf->current_command)--;
1219 /* redraw the rest of the line */
1220 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1221 unix_vlib_cli_output_cooked (cf, uf,
1222 cf->current_command + cf->cursor, j);
1223 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
1224 /* and shift the terminal cursor back where it should be */
1226 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1229 cf->search_mode = 0;
1231 vec_reset_length (cf->search_key);
1234 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1235 if (vec_len (cf->current_command))
1237 if (cf->cursor < vec_len(cf->current_command))
1239 /* shift everything to the right of the cursor left by 1 */
1240 j = vec_len (cf->current_command) - cf->cursor - 1;
1241 memmove (cf->current_command + cf->cursor,
1242 cf->current_command + cf->cursor + 1,
1244 _vec_len (cf->current_command)--;
1245 /* redraw the rest of the line */
1246 unix_vlib_cli_output_cooked (cf, uf,
1247 cf->current_command + cf->cursor, j);
1248 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
1249 /* and shift the terminal cursor back where it should be */
1252 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1254 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1258 else if (input == 'D' - '@')
1260 /* ^D with no command entered = quit */
1261 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1262 vlib_process_signal_event (um->vlib_main,
1263 vlib_current_process (um->vlib_main),
1264 UNIX_CLI_PROCESS_EVENT_QUIT,
1265 cf - cm->cli_file_pool);
1267 cf->search_mode = 0;
1269 vec_reset_length (cf->search_key);
1272 case UNIX_CLI_PARSE_ACTION_CLEAR:
1273 /* If we're in ANSI mode, clear the screen.
1274 * Then redraw the prompt and any existing command input, then put
1275 * the cursor back where it was in that line.
1277 if (cf->ansi_capable)
1278 unix_vlib_cli_output_cooked (cf, uf,
1280 sizeof(ANSI_CLEAR)-1);
1282 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1284 unix_vlib_cli_output_raw (cf, uf,
1286 vec_len (cm->cli_prompt));
1287 unix_vlib_cli_output_raw (cf, uf,
1288 cf->current_command,
1289 vec_len (cf->current_command));
1290 for (j = cf->cursor; j < vec_len(cf->current_command); j++)
1291 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1295 case UNIX_CLI_PARSE_ACTION_TAB:
1296 case UNIX_CLI_PARSE_ACTION_YANK:
1301 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
1303 unix_cli_pager_prompt_erase (cf, uf);
1304 unix_cli_pager_reset (cf);
1305 unix_cli_cli_prompt (cf, uf);
1308 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
1309 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
1310 /* show next page of the buffer */
1311 if (cf->height + cf->pager_start < cf->pager_lines)
1314 int m = cf->pager_start + (cf->height - 1);
1315 unix_cli_pager_prompt_erase (cf, uf);
1317 j < cf->pager_lines && cf->pager_start < m;
1318 j ++, cf->pager_start ++)
1320 line = cf->pager_vector[j];
1321 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1323 unix_cli_pager_prompt (cf, uf);
1327 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
1328 /* no more in buffer, exit, but only if it was <space> */
1333 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
1334 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
1335 /* display the next line of the buffer */
1336 if (cf->pager_start < cf->pager_lines - (cf->height - 1))
1339 unix_cli_pager_prompt_erase (cf, uf);
1340 line = cf->pager_vector[cf->pager_start + (cf->height - 1)];
1341 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1343 unix_cli_pager_prompt (cf, uf);
1347 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
1348 /* no more in buffer, exit, but only if it was <enter> */
1354 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
1355 /* scroll the page back one line */
1356 if (cf->pager_start > 0)
1360 if (cf->ansi_capable)
1362 unix_cli_pager_prompt_erase (cf, uf);
1363 unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_SCROLLDN, sizeof(ANSI_SCROLLDN) - 1);
1364 unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_SAVECURSOR, sizeof(ANSI_SAVECURSOR) - 1);
1365 unix_cli_ansi_cursor(cf, uf, 1, 1);
1366 unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_CLEARLINE, sizeof(ANSI_CLEARLINE) - 1);
1367 unix_vlib_cli_output_cooked (cf, uf, cf->pager_vector[cf->pager_start], vec_len(cf->pager_vector[cf->pager_start]));
1368 unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_RESTCURSOR, sizeof(ANSI_RESTCURSOR) - 1);
1369 unix_cli_pager_prompt_erase (cf, uf);
1370 unix_cli_pager_prompt (cf, uf);
1374 int m = cf->pager_start + (cf->height - 1);
1375 unix_cli_pager_prompt_erase (cf, uf);
1376 for (j = cf->pager_start; j < cf->pager_lines && j < m; j ++)
1378 line = cf->pager_vector[j];
1379 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1381 unix_cli_pager_prompt (cf, uf);
1386 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
1387 /* back to the first page of the buffer */
1388 if (cf->pager_start > 0)
1391 cf->pager_start = 0;
1392 int m = cf->pager_start + (cf->height - 1);
1393 unix_cli_pager_prompt_erase (cf, uf);
1394 for (j = cf->pager_start; j < cf->pager_lines && j < m; j ++)
1396 line = cf->pager_vector[j];
1397 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1399 unix_cli_pager_prompt (cf, uf);
1403 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
1404 /* skip to the last page of the buffer */
1405 if (cf->pager_start < cf->pager_lines - (cf->height - 1))
1408 cf->pager_start = cf->pager_lines - (cf->height - 1);
1409 unix_cli_pager_prompt_erase (cf, uf);
1410 unix_cli_pager_message (cf, uf, "skipping", "\n");
1411 for (j = cf->pager_start; j < cf->pager_lines; j ++)
1413 line = cf->pager_vector[j];
1414 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1416 unix_cli_pager_prompt (cf, uf);
1420 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
1421 /* wander back one page in the buffer */
1422 if (cf->pager_start > 0)
1426 if (cf->pager_start >= cf->height)
1427 cf->pager_start -= cf->height - 1;
1429 cf->pager_start = 1;
1430 m = cf->pager_start + cf->height - 1;
1431 unix_cli_pager_prompt_erase (cf, uf);
1432 for (j = cf->pager_start; j < cf->pager_lines && j < m; j ++)
1434 line = cf->pager_vector[j];
1435 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1437 unix_cli_pager_prompt (cf, uf);
1441 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
1442 /* search forwards in the buffer */
1446 case UNIX_CLI_PARSE_ACTION_CRLF:
1448 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1450 if (cf->has_history && cf->history_limit)
1452 if (cf->command_history && vec_len(cf->command_history) >= cf->history_limit)
1454 vec_free (cf->command_history[0]);
1455 vec_delete (cf->command_history, 1, 0);
1457 /* Don't add blank lines to the cmd history */
1458 if (vec_len (cf->current_command))
1460 /* Don't duplicate the previous command */
1461 j = vec_len(cf->command_history);
1463 (vec_len (cf->current_command) != vec_len (cf->command_history[j - 1]) ||
1464 memcmp(cf->current_command, cf->command_history[j - 1],
1465 vec_len (cf->current_command)) != 0))
1467 /* copy the command to the history */
1469 vec_append(c, cf->current_command);
1470 vec_add1 (cf->command_history, c);
1471 cf->command_number ++;
1474 cf->excursion = vec_len (cf->command_history);
1477 cf->search_mode = 0;
1478 vec_reset_length (cf->search_key);
1483 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
1484 case UNIX_CLI_PARSE_ACTION_NOMATCH:
1485 if (cf->pager_lines)
1489 else if (cf->has_history && cf->search_mode && isprint(input))
1491 int k, limit, offset;
1494 vec_add1 (cf->search_key, input);
1497 for (j = 0; j < vec_len(cf->command_history); j++)
1499 if (cf->excursion > (i32) vec_len (cf->command_history) -1)
1501 else if (cf->excursion < 0)
1502 cf->excursion = vec_len (cf->command_history) -1;
1504 item = cf->command_history[cf->excursion];
1506 limit = (vec_len(cf->search_key) > vec_len (item)) ?
1507 vec_len(item) : vec_len (cf->search_key);
1509 for (offset = 0; offset <= vec_len(item) - limit; offset++)
1511 for (k = 0; k < limit; k++)
1513 if (item[k+offset] != cf->search_key[k])
1516 goto found_at_offset;
1524 for (j = 0; j < vec_len (cf->current_command); j++)
1525 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1527 vec_validate (cf->current_command, vec_len(item)-1);
1528 clib_memcpy (cf->current_command, item, vec_len(item));
1529 _vec_len (cf->current_command) = vec_len(item);
1531 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1532 vec_len (cf->current_command));
1533 cf->cursor = vec_len (cf->current_command);
1537 cf->excursion += cf->search_mode;
1540 unix_vlib_cli_output_cooked (cf, uf, (u8 *)"\nNo match...", 12);
1541 vec_reset_length (cf->search_key);
1542 vec_reset_length (cf->current_command);
1543 cf->search_mode = 0;
1547 else if (isprint(input)) /* skip any errant control codes */
1549 if (cf->cursor == vec_len(cf->current_command))
1552 vec_add1 (cf->current_command, input);
1555 /* Echo the character back to the client */
1556 unix_vlib_cli_output_raw (cf, uf, &input, 1);
1560 /* Insert at cursor: resize +1 byte, move everything over */
1561 j = vec_len (cf->current_command) - cf->cursor;
1562 vec_add1 (cf->current_command, (u8)'A');
1563 memmove (cf->current_command + cf->cursor + 1,
1564 cf->current_command + cf->cursor,
1566 cf->current_command[cf->cursor] = input;
1567 /* Redraw the line */
1569 unix_vlib_cli_output_raw (cf, uf,
1570 cf->current_command + cf->cursor, j);
1571 /* Put terminal cursor back */
1573 unix_vlib_cli_output_raw (cf, uf, (u8 *)"\b", 1);
1579 /* no-op - not printable or otherwise not actionable */
1586 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
1592 /** \brief Process input bytes on a stream to provide line editing and
1593 * command history in the CLI. */
1594 static int unix_cli_line_edit (unix_cli_main_t * cm,
1596 unix_cli_file_t * cf)
1598 unix_file_t * uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
1601 for (i = 0; i < vec_len (cf->input_vector); i++)
1603 unix_cli_parse_action_t action;
1605 unix_cli_parse_actions_t *a;
1607 /* If we're in the pager mode, search the pager actions */
1608 a = cf->pager_lines ? unix_cli_parse_pager : unix_cli_parse_strings;
1610 /* See if the input buffer is some sort of control code */
1611 action = unix_cli_match_action(a, &cf->input_vector[i],
1612 vec_len (cf->input_vector) - i, &matched);
1616 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
1619 /* There was a partial match which means we need more bytes
1620 * than the input buffer currently has.
1621 * Since the bytes before here have been processed, shift
1622 * the remaining contents to the start of the input buffer.
1624 vec_delete (cf->input_vector, i, 0);
1626 return 1; /* wait for more */
1628 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
1629 /* process telnet options */
1630 matched = unix_cli_process_telnet(um, cf, uf,
1631 cf->input_vector + i, vec_len(cf->input_vector) - i);
1636 /* There was a partial match which means we need more bytes
1637 * than the input buffer currently has.
1638 * Since the bytes before here have been processed, shift
1639 * the remaining contents to the start of the input buffer.
1641 vec_delete (cf->input_vector, i, 0);
1643 return 1; /* wait for more */
1648 /* process the action */
1649 if (!unix_cli_line_process_one(cm, um, cf, uf,
1650 cf->input_vector[i], action))
1652 /* CRLF found. Consume the bytes from the input_vector */
1653 vec_delete (cf->input_vector, i + matched, 0);
1654 /* And tell our caller to execute cf->input_command */
1662 vec_reset_length(cf->input_vector);
1666 /** \brief Process input to a CLI session. */
1667 static void unix_cli_process_input (unix_cli_main_t * cm,
1668 uword cli_file_index)
1670 unix_main_t * um = &unix_main;
1672 unix_cli_file_t * cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
1673 unformat_input_t input;
1674 int vlib_parse_eval (u8 *);
1677 /* Try vlibplex first. Someday... */
1678 if (0 && vlib_parse_eval (cf->input_vector) == 0)
1683 /* just treat whatever we got as a complete line of input */
1684 cf->current_command = cf->input_vector;
1688 /* Line edit, echo, etc. */
1689 if (unix_cli_line_edit (cm, um, cf))
1690 /* want more input */
1697 vec_reset_length (lv);
1698 lv = format (lv, "%U[%d]: %v",
1700 0 /* current bat-time */,
1701 0 /* current bat-format */,
1705 int rv __attribute__((unused)) =
1706 write (um->log_fd, lv, vec_len(lv));
1710 /* Copy our input command to a new string */
1711 unformat_init_vector (&input, cf->current_command);
1713 /* Remove leading white space from input. */
1714 (void) unformat (&input, "");
1716 cm->current_input_file_index = cli_file_index;
1717 cf->pager_lines = 0; /* start a new pager session */
1718 cf->pager_start = 0;
1720 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
1721 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output, cli_file_index);
1723 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
1726 unformat_free (&input);
1728 /* Re-fetch pointer since pool may have moved. */
1729 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
1730 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
1733 /* reset vector; we'll re-use it later */
1735 vec_reset_length (cf->input_vector);
1737 vec_reset_length (cf->current_command);
1739 if (cf->no_pager == 2)
1741 /* Pager was programmatically disabled */
1742 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
1743 cf->no_pager = um->cli_no_pager;
1746 if (cf->pager_lines == 0 || cf->pager_lines < cf->height)
1748 /* There was no need for the pager */
1749 unix_cli_pager_reset (cf);
1752 unix_cli_cli_prompt (cf, uf);
1756 /* Display the pager prompt */
1757 unix_cli_pager_prompt(cf, uf);
1760 /* Any residual data in the input vector? */
1761 if (vec_len (cf->input_vector))
1765 static void unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
1767 unix_main_t * um = &unix_main;
1768 unix_cli_file_t * cf;
1772 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
1773 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
1775 /* Quit/EOF on stdin means quit program. */
1776 if (uf->file_descriptor == UNIX_CLI_STDIN_FD)
1777 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
1779 vec_free (cf->current_command);
1780 vec_free (cf->search_key);
1782 for (i = 0; i < vec_len (cf->command_history); i++)
1783 vec_free (cf->command_history[i]);
1785 vec_free (cf->command_history);
1787 unix_file_del (um, uf);
1789 unix_cli_file_free (cf);
1790 pool_put (cm->cli_file_pool, cf);
1794 unix_cli_process (vlib_main_t * vm,
1795 vlib_node_runtime_t * rt,
1798 unix_cli_main_t * cm = &unix_cli_main;
1799 uword i, * data = 0;
1803 unix_cli_process_event_type_t event_type;
1804 vlib_process_wait_for_event (vm);
1805 event_type = vlib_process_get_events (vm, &data);
1809 case UNIX_CLI_PROCESS_EVENT_READ_READY:
1810 for (i = 0; i < vec_len (data); i++)
1811 unix_cli_process_input (cm, data[i]);
1814 case UNIX_CLI_PROCESS_EVENT_QUIT:
1815 /* Kill this process. */
1816 for (i = 0; i < vec_len (data); i++)
1817 unix_cli_kill (cm, data[i]);
1822 _vec_len (data) = 0;
1828 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
1830 /* Add node index so we can re-use this process later. */
1831 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
1836 static clib_error_t * unix_cli_write_ready (unix_file_t * uf)
1838 unix_cli_main_t * cm = &unix_cli_main;
1839 unix_cli_file_t * cf;
1842 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
1844 /* Flush output vector. */
1845 n = write (uf->file_descriptor,
1846 cf->output_vector, vec_len (cf->output_vector));
1848 if (n < 0 && errno != EAGAIN)
1849 return clib_error_return_unix (0, "write");
1852 unix_cli_del_pending_output (uf, cf, n);
1854 return /* no error */ 0;
1857 static clib_error_t * unix_cli_read_ready (unix_file_t * uf)
1859 unix_main_t * um = &unix_main;
1860 unix_cli_main_t * cm = &unix_cli_main;
1861 unix_cli_file_t * cf;
1863 int n, n_read, n_try;
1865 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
1868 while (n == n_try) {
1869 l = vec_len (cf->input_vector);
1870 vec_resize (cf->input_vector, l + n_try);
1872 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
1875 if (n < 0 && errno != EAGAIN)
1876 return clib_error_return_unix (0, "read");
1878 n_read = n < 0 ? 0 : n;
1879 _vec_len (cf->input_vector) = l + n_read;
1883 vlib_process_signal_event (um->vlib_main,
1884 cf->process_node_index,
1886 ? UNIX_CLI_PROCESS_EVENT_QUIT
1887 : UNIX_CLI_PROCESS_EVENT_READ_READY),
1888 /* event data */ uf->private_data);
1890 return /* no error */ 0;
1893 /** Store a new CLI session.
1894 * @param name The name of the session.
1895 * @param fd The file descriptor for the session I/O.
1896 * @return The session ID.
1898 static u32 unix_cli_file_add (unix_cli_main_t * cm, char * name, int fd)
1900 unix_main_t * um = &unix_main;
1901 unix_cli_file_t * cf;
1902 unix_file_t template = {0};
1903 vlib_main_t * vm = um->vlib_main;
1906 name = (char *) format (0, "unix-cli-%s", name);
1908 if (vec_len (cm->unused_cli_process_node_indices) > 0)
1910 uword l = vec_len (cm->unused_cli_process_node_indices);
1912 /* Find node and give it new name. */
1913 n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
1915 n->name = (u8 *) name;
1917 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
1919 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
1923 static vlib_node_registration_t r = {
1924 .function = unix_cli_process,
1925 .type = VLIB_NODE_TYPE_PROCESS,
1926 .process_log2_n_stack_bytes = 14,
1930 vlib_register_node (vm, &r);
1933 n = vlib_get_node (vm, r.index);
1936 pool_get (cm->cli_file_pool, cf);
1937 memset (cf, 0, sizeof (*cf));
1939 template.read_function = unix_cli_read_ready;
1940 template.write_function = unix_cli_write_ready;
1941 template.file_descriptor = fd;
1942 template.private_data = cf - cm->cli_file_pool;
1944 cf->process_node_index = n->index;
1945 cf->unix_file_index = unix_file_add (um, &template);
1946 cf->output_vector = 0;
1947 cf->input_vector = 0;
1949 vlib_start_process (vm, n->runtime_index);
1951 vlib_process_t * p = vlib_get_process_from_node(vm, n);
1952 p->output_function = unix_vlib_cli_output;
1953 p->output_function_arg = cf - cm->cli_file_pool;
1955 return cf - cm->cli_file_pool;
1958 /** Telnet listening socket has a new connection. */
1959 static clib_error_t * unix_cli_listen_read_ready (unix_file_t * uf)
1961 unix_main_t * um = &unix_main;
1962 unix_cli_main_t * cm = &unix_cli_main;
1963 clib_socket_t * s = &um->cli_listen_socket;
1964 clib_socket_t client;
1966 clib_error_t * error;
1967 unix_cli_file_t * cf;
1970 error = clib_socket_accept (s, &client);
1974 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
1976 cf_index = unix_cli_file_add (cm, client_name, client.fd);
1977 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
1979 /* No longer need CLIB version of socket. */
1980 clib_socket_free (&client);
1982 vec_free (client_name);
1984 /* if we're supposed to run telnet session in character mode (default) */
1985 if (um->cli_line_mode == 0)
1988 * Set telnet client character mode, echo on, suppress "go-ahead".
1989 * Technically these should be negotiated, but this works.
1991 u8 charmode_option[] = {
1992 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
1993 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
1994 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
1995 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
1996 IAC, WILL, TELOPT_ECHO, /* server will do echo */
1997 IAC, DONT, TELOPT_ECHO, /* client should not echo */
1998 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
1999 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
2000 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
2001 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
2004 /* Enable history on this CLI */
2005 cf->history_limit = um->cli_history_limit;
2006 cf->has_history = cf->history_limit != 0;
2008 /* Make sure this session is in line mode */
2014 /* Setup the pager */
2015 cf->no_pager = um->cli_no_pager;
2017 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
2019 /* Send the telnet options */
2020 unix_vlib_cli_output_raw (cf, uf, charmode_option,
2021 ARRAY_LEN(charmode_option));
2023 /* In case the client doesn't negotiate terminal type, use
2024 * a timer to kick off the initial prompt. */
2025 timer_call (unix_cli_file_welcome_timer, cf_index, 1);
2031 /** The system terminal has informed us that the window size
2035 unix_cli_resize_interrupt (int signum)
2037 unix_cli_main_t * cm = &unix_cli_main;
2038 unix_cli_file_t * cf = pool_elt_at_index (cm->cli_file_pool,
2039 cm->stdin_cli_file_index);
2043 /* Terminal resized, fetch the new size */
2044 ioctl(UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws);
2045 cf->width = ws.ws_col;
2046 cf->height = ws.ws_row;
2049 /** Handle configuration directives in the @em unix section. */
2050 static clib_error_t *
2051 unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
2053 unix_main_t * um = &unix_main;
2054 unix_cli_main_t * cm = &unix_cli_main;
2056 clib_error_t * error = 0;
2057 unix_cli_file_t * cf;
2060 struct sigaction sa;
2064 /* We depend on unix flags being set. */
2065 if ((error = vlib_call_config_function (vm, unix_config)))
2068 if (um->flags & UNIX_FLAG_INTERACTIVE)
2070 /* Set stdin to be non-blocking. */
2071 if ((flags = fcntl (UNIX_CLI_STDIN_FD, F_GETFL, 0)) < 0)
2073 fcntl (UNIX_CLI_STDIN_FD, F_SETFL, flags | O_NONBLOCK);
2075 cf_index = unix_cli_file_add (cm, "stdin", UNIX_CLI_STDIN_FD);
2076 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2077 cm->stdin_cli_file_index = cf_index;
2079 /* If stdin is a tty and we are using chacracter mode, enable
2080 * history on the CLI and set the tty line discipline accordingly. */
2081 if (isatty(UNIX_CLI_STDIN_FD) && um->cli_line_mode == 0)
2083 /* Capture terminal resize events */
2084 sa.sa_handler = unix_cli_resize_interrupt;
2086 if (sigaction (SIGWINCH, &sa, 0) < 0)
2087 clib_panic ("sigaction");
2089 /* Retrieve the current terminal size */
2090 ioctl(UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws);
2091 cf->width = ws.ws_col;
2092 cf->height = ws.ws_row;
2094 if (cf->width == 0 || cf->height == 0)
2095 /* We have a tty, but no size. Stick to line mode. */
2098 /* Setup the history */
2099 cf->history_limit = um->cli_history_limit;
2100 cf->has_history = cf->history_limit != 0;
2102 /* Setup the pager */
2103 cf->no_pager = um->cli_no_pager;
2105 /* We're going to be in char by char mode */
2108 /* Save the original tty state so we can restore it later */
2109 tcgetattr(UNIX_CLI_STDIN_FD, &um->tio_stdin);
2112 /* Tweak the tty settings */
2113 tio = um->tio_stdin;
2114 /* echo off, canonical mode off, ext'd input processing off */
2115 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
2116 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
2117 tio.c_cc[VTIME] = 0; /* no timer */
2118 tcsetattr(UNIX_CLI_STDIN_FD, TCSAFLUSH, &tio);
2120 /* See if we can do ANSI/VT100 output */
2121 term = (u8 *)getenv("TERM");
2123 cf->ansi_capable = unix_cli_terminal_type(term,
2124 strlen((char *)term));
2129 /* No tty, so make sure these things are off */
2131 cf->history_limit = 0;
2132 cf->has_history = 0;
2136 /* Send banner and initial prompt */
2137 unix_cli_file_welcome(cm, cf);
2140 /* If we have socket config, LISTEN, otherwise, don't */
2141 clib_socket_t * s = &um->cli_listen_socket;
2142 if(s->config && s->config[0] != 0) {
2144 unix_file_t template = {0};
2146 s->flags = SOCKET_IS_SERVER; /* listen, don't connect */
2147 error = clib_socket_init (s);
2152 template.read_function = unix_cli_listen_read_ready;
2153 template.file_descriptor = s->fd;
2155 unix_file_add (um, &template);
2158 /* Set CLI prompt. */
2159 if (! cm->cli_prompt)
2160 cm->cli_prompt = format (0, "VLIB: ");
2165 VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
2167 /** Called when VPP is shutting down, this resets the system
2168 * terminal state, if previously saved.
2170 static clib_error_t *
2171 unix_cli_exit (vlib_main_t * vm)
2173 unix_main_t * um = &unix_main;
2175 /* If stdin is a tty and we saved the tty state, reset the tty state */
2176 if (isatty(UNIX_CLI_STDIN_FD) && um->tio_isset)
2177 tcsetattr(UNIX_CLI_STDIN_FD, TCSAFLUSH, &um->tio_stdin);
2182 VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
2184 /** Set the CLI prompt.
2185 * @param The C string to set the prompt to.
2186 * @note This setting is global; it impacts all current
2187 * and future CLI sessions.
2189 void vlib_unix_cli_set_prompt (char * prompt)
2191 char * fmt = (prompt[strlen(prompt)-1] == ' ') ? "%s" : "%s ";
2192 unix_cli_main_t * cm = &unix_cli_main;
2194 vec_free (cm->cli_prompt);
2195 cm->cli_prompt = format (0, fmt, prompt);
2198 /** CLI command to quit the terminal session.
2199 * @note If this is a stdin session then this will
2200 * shutdown VPP also.
2202 static clib_error_t *
2203 unix_cli_quit (vlib_main_t * vm,
2204 unformat_input_t * input,
2205 vlib_cli_command_t * cmd)
2207 unix_cli_main_t * cm = &unix_cli_main;
2209 vlib_process_signal_event (vm,
2210 vlib_current_process (vm),
2211 UNIX_CLI_PROCESS_EVENT_QUIT,
2212 cm->current_input_file_index);
2216 VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
2218 .short_help = "Exit CLI",
2219 .function = unix_cli_quit,
2222 /** CLI command to execute a VPP command script. */
2223 static clib_error_t *
2224 unix_cli_exec (vlib_main_t * vm,
2225 unformat_input_t * input,
2226 vlib_cli_command_t * cmd)
2230 unformat_input_t sub_input;
2231 clib_error_t * error;
2237 if (! unformat (input, "%s", &file_name))
2239 error = clib_error_return (0, "expecting file name, got `%U'",
2240 format_unformat_error, input);
2244 fd = open (file_name, O_RDONLY);
2247 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
2251 /* Make sure its a regular file. */
2255 if (fstat (fd, &s) < 0)
2257 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
2261 if (! (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
2263 error = clib_error_return (0, "not a regular file `%s'", file_name);
2268 unformat_init_unix_file (&sub_input, fd);
2270 vlib_cli_input (vm, &sub_input, 0, 0);
2271 unformat_free (&sub_input);
2276 vec_free (file_name);
2281 VLIB_CLI_COMMAND (cli_exec, static) = {
2283 .short_help = "Execute commands from file",
2284 .function = unix_cli_exec,
2288 /** CLI command to show various unix error statistics. */
2289 static clib_error_t *
2290 unix_show_errors (vlib_main_t * vm,
2291 unformat_input_t * input,
2292 vlib_cli_command_t * cmd)
2294 unix_main_t * um = &unix_main;
2295 clib_error_t * error = 0;
2296 int i, n_errors_to_show;
2297 unix_error_history_t * unix_errors = 0;
2299 n_errors_to_show = 1 << 30;
2301 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2303 if (! unformat (input, "%d", &n_errors_to_show))
2305 error = clib_error_return (0, "expecting integer number of errors to show, got `%U'",
2306 format_unformat_error, input);
2311 n_errors_to_show = clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
2313 i = um->error_history_index > 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
2315 while (n_errors_to_show > 0)
2317 unix_error_history_t * eh = um->error_history + i;
2322 vec_add1 (unix_errors, eh[0]);
2323 n_errors_to_show -= 1;
2325 i = ARRAY_LEN (um->error_history) - 1;
2330 if (vec_len (unix_errors) == 0)
2331 vlib_cli_output (vm, "no Unix errors so far");
2334 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
2335 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
2337 unix_error_history_t * eh = vec_elt_at_index (unix_errors, i);
2338 vlib_cli_output (vm, "%U: %U",
2339 format_time_interval, "h:m:s:u", eh->time,
2340 format_clib_error, eh->error);
2342 vlib_cli_output (vm, "%U: time now",
2343 format_time_interval, "h:m:s:u", vlib_time_now (vm));
2347 vec_free (unix_errors);
2351 VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
2352 .path = "show unix-errors",
2353 .short_help = "Show Unix system call error history",
2354 .function = unix_show_errors,
2357 /** CLI command to show session command history. */
2358 static clib_error_t *
2359 unix_cli_show_history (vlib_main_t * vm,
2360 unformat_input_t * input,
2361 vlib_cli_command_t * cmd)
2363 unix_cli_main_t * cm = &unix_cli_main;
2364 unix_cli_file_t * cf;
2367 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2369 if (cf->has_history && cf->history_limit)
2371 i = 1 + cf->command_number - vec_len(cf->command_history);
2372 for (j = 0; j < vec_len (cf->command_history); j++)
2373 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
2377 vlib_cli_output (vm, "History not enabled.\n");
2383 VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
2385 .short_help = "Show current session command history",
2386 .function = unix_cli_show_history,
2389 /** CLI command to show terminal status. */
2390 static clib_error_t *
2391 unix_cli_show_terminal (vlib_main_t * vm,
2392 unformat_input_t * input,
2393 vlib_cli_command_t * cmd)
2395 unix_main_t * um = &unix_main;
2396 unix_cli_main_t * cm = &unix_cli_main;
2397 unix_cli_file_t * cf;
2400 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2401 n = vlib_get_node (vm, cf->process_node_index);
2403 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
2404 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
2407 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
2408 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
2409 vlib_cli_output (vm, "ANSI capable: %s\n", cf->ansi_capable ? "yes" : "no");
2410 vlib_cli_output (vm, "History enabled: %s%s\n",
2411 cf->has_history ? "yes" : "no",
2412 !cf->has_history || cf->history_limit ? "" : " (disabled by history limit)");
2413 if (cf->has_history)
2414 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
2415 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
2416 cf->no_pager ? "no" : "yes",
2417 cf->no_pager || cf->height ? "" : " (disabled by terminal height)",
2418 cf->no_pager || um->cli_pager_buffer_limit ? "" : " (disabled by buffer limit)");
2420 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
2421 vlib_cli_output (vm, "CRLF mode: %s\n", cf->crlf_mode ? "CR+LF" : "LF");
2426 VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
2427 .path = "show terminal",
2428 .short_help = "Show current session terminal settings",
2429 .function = unix_cli_show_terminal,
2432 /** CLI command to set terminal pager settings. */
2433 static clib_error_t *
2434 unix_cli_set_terminal_pager (vlib_main_t * vm,
2435 unformat_input_t * input,
2436 vlib_cli_command_t * cmd)
2438 unix_main_t * um = &unix_main;
2439 unix_cli_main_t * cm = &unix_cli_main;
2440 unix_cli_file_t * cf;
2441 unformat_input_t _line_input, * line_input = &_line_input;
2443 if (! unformat_user (input, unformat_line_input, line_input))
2446 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2448 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2450 if (unformat (line_input, "on"))
2452 else if (unformat (line_input, "off"))
2454 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
2455 vlib_cli_output (vm, "Pager limit set to %u lines; note, this is global.\n",
2456 um->cli_pager_buffer_limit);
2458 return clib_error_return (0, "unknown parameter: `%U`",
2459 format_unformat_error, line_input);
2462 unformat_free(line_input);
2467 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
2468 .path = "set terminal pager",
2469 .short_help = "set terminal pager [on|off] [limit <lines>]",
2470 .function = unix_cli_set_terminal_pager,
2473 /** CLI command to set terminal history settings. */
2474 static clib_error_t *
2475 unix_cli_set_terminal_history (vlib_main_t * vm,
2476 unformat_input_t * input,
2477 vlib_cli_command_t * cmd)
2479 unix_cli_main_t * cm = &unix_cli_main;
2480 unix_cli_file_t * cf;
2481 unformat_input_t _line_input, * line_input = &_line_input;
2484 if (! unformat_user (input, unformat_line_input, line_input))
2487 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2489 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2491 if (unformat (line_input, "on"))
2492 cf->has_history = 1;
2493 else if (unformat (line_input, "off"))
2494 cf->has_history = 0;
2495 else if (unformat (line_input, "limit %u", &cf->history_limit))
2498 return clib_error_return (0, "unknown parameter: `%U`",
2499 format_unformat_error, line_input);
2501 /* If we reduced history size, or turned it off, purge the history */
2502 limit = cf->has_history ? cf->history_limit : 0;
2504 while (cf->command_history && vec_len(cf->command_history) >= limit)
2506 vec_free (cf->command_history[0]);
2507 vec_delete (cf->command_history, 1, 0);
2511 unformat_free(line_input);
2516 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
2517 .path = "set terminal history",
2518 .short_help = "set terminal history [on|off] [limit <lines>]",
2519 .function = unix_cli_set_terminal_history,
2522 /** CLI command to set terminal ANSI settings. */
2523 static clib_error_t *
2524 unix_cli_set_terminal_ansi (vlib_main_t * vm,
2525 unformat_input_t * input,
2526 vlib_cli_command_t * cmd)
2528 unix_cli_main_t * cm = &unix_cli_main;
2529 unix_cli_file_t * cf;
2531 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2533 if (unformat (input, "on"))
2534 cf->ansi_capable = 1;
2535 else if (unformat (input, "off"))
2536 cf->ansi_capable = 0;
2538 return clib_error_return (0, "unknown parameter: `%U`",
2539 format_unformat_error, input);
2544 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
2545 .path = "set terminal ansi",
2546 .short_help = "set terminal ansi [on|off]",
2547 .function = unix_cli_set_terminal_ansi,
2550 static clib_error_t *
2551 unix_cli_init (vlib_main_t * vm)
2556 VLIB_INIT_FUNCTION (unix_cli_init);