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.
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.
45 /*? %%clicmd:group_label Command line session %% ?*/
46 /*? %%syscfg:group_label Command line session %% ?*/
48 #include <vlib/vlib.h>
49 #include <vlib/unix/unix.h>
50 #include <vppinfra/timer.h>
58 #include <arpa/telnet.h>
59 #include <sys/ioctl.h>
60 #include <sys/types.h>
63 /** ANSI escape code. */
66 /** ANSI Control Sequence Introducer. */
69 /** ANSI clear screen. */
70 #define ANSI_CLEAR CSI "2J" CSI "1;1H"
71 /** ANSI reset color settings. */
72 #define ANSI_RESET CSI "0m"
73 /** ANSI Start bold text. */
74 #define ANSI_BOLD CSI "1m"
75 /** ANSI Stop bold text. */
76 #define ANSI_DIM CSI "2m"
77 /** ANSI Start dark red text. */
78 #define ANSI_DRED ANSI_DIM CSI "31m"
79 /** ANSI Start bright red text. */
80 #define ANSI_BRED ANSI_BOLD CSI "31m"
81 /** ANSI clear line cursor is on. */
82 #define ANSI_CLEARLINE CSI "2K"
83 /** ANSI scroll screen down one line. */
84 #define ANSI_SCROLLDN CSI "1T"
85 /** ANSI save cursor position. */
86 #define ANSI_SAVECURSOR CSI "s"
87 /** ANSI restore cursor position if previously saved. */
88 #define ANSI_RESTCURSOR CSI "u"
90 /** Maximum depth into a byte stream from which to compile a Telnet
91 * protocol message. This is a saftey measure. */
92 #define UNIX_CLI_MAX_DEPTH_TELNET 24
94 /** Minimum terminal width we will accept */
95 #define UNIX_CLI_MIN_TERMINAL_WIDTH 1
96 /** Maximum terminal width we will accept */
97 #define UNIX_CLI_MAX_TERMINAL_WIDTH 512
98 /** Minimum terminal height we will accept */
99 #define UNIX_CLI_MIN_TERMINAL_HEIGHT 1
100 /** Maximum terminal height we will accept */
101 #define UNIX_CLI_MAX_TERMINAL_HEIGHT 512
103 /** Unix standard in */
104 #define UNIX_CLI_STDIN_FD 0
107 /** A CLI banner line. */
110 u8 *line; /**< The line to print. */
111 u32 length; /**< The length of the line without terminating NUL. */
114 #define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
115 /** Plain welcome banner. */
116 static unix_cli_banner_t unix_cli_banner[] = {
117 _(" _______ _ _ _____ ___ \n"),
118 _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
119 _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
120 _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
124 /** ANSI color welcome banner. */
125 static unix_cli_banner_t unix_cli_banner_color[] = {
126 _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
127 _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
128 _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
129 _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"),
135 /** Pager line index */
138 /** Index into pager_vector */
141 /** Offset of the string in the line */
144 /** Length of the string in the line */
146 } unix_cli_pager_index_t;
149 /** Unix CLI session. */
152 /** The file index held by unix.c */
155 /** Vector of output pending write to file descriptor. */
158 /** Vector of input saved by Unix input node to be processed by
162 /** This session has command history. */
164 /** Array of vectors of commands in the history. */
165 u8 **command_history;
166 /** The command currently pointed at by the history cursor. */
168 /** How far from the end of the history array the user has browsed. */
171 /** Maximum number of history entries this session will store. */
174 /** Current command line counter */
177 /** The string being searched for in the history. */
179 /** If non-zero then the CLI is searching in the history array.
180 * - @c -1 means search backwards.
181 * - @c 1 means search forwards.
185 /** Position of the insert cursor on the current input line */
188 /** Line mode or char mode */
191 /** Set if the CRLF mode wants CR + LF */
194 /** Can we do ANSI output? */
197 /** Has the session started? */
200 /** Disable the pager? */
206 /** Index of line fragments in the pager buffer */
207 unix_cli_pager_index_t *pager_index;
209 /** Line number of top of page */
212 /** Terminal width */
215 /** Terminal height */
218 /** Process node identifier */
219 u32 process_node_index;
222 /** Resets the pager buffer and other data.
223 * @param f The CLI session whose pager needs to be reset.
226 unix_cli_pager_reset (unix_cli_file_t * f)
232 vec_free (f->pager_index);
235 vec_foreach (p, f->pager_vector)
239 vec_free (f->pager_vector);
243 /** Release storage used by a CLI session.
244 * @param f The CLI session whose storage needs to be released.
247 unix_cli_file_free (unix_cli_file_t * f)
249 vec_free (f->output_vector);
250 vec_free (f->input_vector);
251 unix_cli_pager_reset (f);
257 UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */
258 UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */
259 UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */
260 UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */
261 UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */
262 UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */
263 UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */
264 UNIX_CLI_PARSE_ACTION_LEFT, /**< Left arrow */
265 UNIX_CLI_PARSE_ACTION_RIGHT, /**< Right arrow */
266 UNIX_CLI_PARSE_ACTION_HOME, /**< Home key (jump to start of line) */
267 UNIX_CLI_PARSE_ACTION_END, /**< End key (jump to end of line) */
268 UNIX_CLI_PARSE_ACTION_WORDLEFT, /**< Jump cursor to start of left word */
269 UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */
270 UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */
271 UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */
272 UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */
273 UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */
274 UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */
275 UNIX_CLI_PARSE_ACTION_YANK, /**< Undo last erase action */
276 UNIX_CLI_PARSE_ACTION_TELNETIAC, /**< Telnet control code */
278 UNIX_CLI_PARSE_ACTION_PAGER_CRLF, /**< Enter pressed (CR, CRLF, LF, etc) */
279 UNIX_CLI_PARSE_ACTION_PAGER_QUIT, /**< Exit the pager session */
280 UNIX_CLI_PARSE_ACTION_PAGER_NEXT, /**< Scroll to next page */
281 UNIX_CLI_PARSE_ACTION_PAGER_DN, /**< Scroll to next line */
282 UNIX_CLI_PARSE_ACTION_PAGER_UP, /**< Scroll to previous line */
283 UNIX_CLI_PARSE_ACTION_PAGER_TOP, /**< Scroll to first line */
284 UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM, /**< Scroll to last line */
285 UNIX_CLI_PARSE_ACTION_PAGER_PGDN, /**< Scroll to next page */
286 UNIX_CLI_PARSE_ACTION_PAGER_PGUP, /**< Scroll to previous page */
287 UNIX_CLI_PARSE_ACTION_PAGER_REDRAW, /**< Clear and redraw the page on the terminal */
288 UNIX_CLI_PARSE_ACTION_PAGER_SEARCH, /**< Search the pager buffer */
290 UNIX_CLI_PARSE_ACTION_PARTIALMATCH, /**< Action parser found a partial match */
291 UNIX_CLI_PARSE_ACTION_NOMATCH /**< Action parser did not find any match */
292 } unix_cli_parse_action_t;
294 /** @brief Mapping of input buffer strings to action values.
295 * @note This won't work as a hash since we need to be able to do
296 * partial matches on the string.
300 u8 *input; /**< Input string to match. */
301 u32 len; /**< Length of input without final NUL. */
302 unix_cli_parse_action_t action; /**< Action to take when matched. */
303 } unix_cli_parse_actions_t;
305 /** @brief Given a capital ASCII letter character return a @c NUL terminated
306 * string with the control code for that letter.
308 * @param c An ASCII character.
309 * @return A @c NUL terminated string of type @c u8[].
312 * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[].
314 #define CTL(c) (u8[]){ (c) - '@', 0 }
316 #define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
318 * Patterns to match on a CLI input stream.
321 static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
323 _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */
324 _("\n", UNIX_CLI_PARSE_ACTION_CRLF),
325 _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */
326 _("\r", UNIX_CLI_PARSE_ACTION_CRLF),
328 /* Unix shell control codes */
329 _(CTL ('B'), UNIX_CLI_PARSE_ACTION_LEFT),
330 _(CTL ('F'), UNIX_CLI_PARSE_ACTION_RIGHT),
331 _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP),
332 _(CTL ('N'), UNIX_CLI_PARSE_ACTION_DOWN),
333 _(CTL ('A'), UNIX_CLI_PARSE_ACTION_HOME),
334 _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END),
335 _(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT),
336 _(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT),
337 _(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT),
338 _(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK),
339 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR),
340 _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */
341 _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */
342 _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */
343 _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */
344 _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */
346 /* VT100 Normal mode - Broadest support */
347 _(CSI "A", UNIX_CLI_PARSE_ACTION_UP),
348 _(CSI "B", UNIX_CLI_PARSE_ACTION_DOWN),
349 _(CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT),
350 _(CSI "D", UNIX_CLI_PARSE_ACTION_LEFT),
351 _(CSI "H", UNIX_CLI_PARSE_ACTION_HOME),
352 _(CSI "F", UNIX_CLI_PARSE_ACTION_END),
353 _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */
354 _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */
355 _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */
357 /* VT100 Application mode - Some Gnome Terminal functions use these */
358 _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP),
359 _(ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN),
360 _(ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT),
361 _(ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT),
362 _(ESC "OH", UNIX_CLI_PARSE_ACTION_HOME),
363 _(ESC "OF", UNIX_CLI_PARSE_ACTION_END),
365 /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
366 _(CSI "1~", UNIX_CLI_PARSE_ACTION_HOME),
367 _(CSI "4~", UNIX_CLI_PARSE_ACTION_END),
369 /* Emacs-ish history search */
370 _(CTL ('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH),
371 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH),
373 /* Other protocol things */
374 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
375 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
376 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
380 * Patterns to match when a CLI session is in the pager.
383 static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
385 _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */
386 _("\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
387 _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */
388 _("\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
391 _(" ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT),
392 _("q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT),
393 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
394 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
395 _("/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH),
398 _(CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP),
399 _(CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN),
400 _(CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
401 _(CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
403 /* VT100 Application mode */
404 _(ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP),
405 _(ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN),
406 _(ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
407 _(ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
409 /* ANSI X3.41-1974 */
410 _(CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
411 _(CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
412 _(CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP),
413 _(CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN),
415 /* Other protocol things */
416 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
417 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
418 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
423 /** CLI session events. */
426 UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */
427 UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */
428 } unix_cli_process_event_type_t;
430 /** CLI global state. */
433 /** Prompt string for CLI. */
436 /** Vec pool of CLI sessions. */
437 unix_cli_file_t *cli_file_pool;
439 /** Vec pool of unused session indices. */
440 u32 *unused_cli_process_node_indices;
442 /** The session index of the stdin cli */
443 u32 stdin_cli_file_index;
445 /** File pool index of current input. */
446 u32 current_input_file_index;
449 /** CLI global state */
450 static unix_cli_main_t unix_cli_main;
453 * @brief Search for a byte sequence in the action list.
455 * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with
456 * the bytes in @a input of maximum length @a ilen bytes.
457 * When a match is made @a *matched indicates how many bytes were matched.
458 * Returns a value from the enum @ref unix_cli_parse_action_t to indicate
459 * whether no match was found, a partial match was found or a complete
460 * match was found and what action, if any, should be taken.
462 * @param[in] a Actions list to search within.
463 * @param[in] input String fragment to search for.
464 * @param[in] ilen Length of the string in 'input'.
465 * @param[out] matched Pointer to an integer that will contain the number
466 * of bytes matched when a complete match is found.
468 * @return Action from @ref unix_cli_parse_action_t that the string fragment
470 * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the
471 * whole input string matches the start of at least one action.
472 * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
475 static unix_cli_parse_action_t
476 unix_cli_match_action (unix_cli_parse_actions_t * a,
477 u8 * input, u32 ilen, i32 * matched)
485 /* see if the start of the input buffer exactly matches the current
487 if (memcmp (input, a->input, a->len) == 0)
495 /* if the first ilen characters match, flag this as a partial -
496 * meaning keep collecting bytes in case of a future match */
497 if (memcmp (input, a->input, ilen) == 0)
501 /* check next action */
506 UNIX_CLI_PARSE_ACTION_PARTIALMATCH : UNIX_CLI_PARSE_ACTION_NOMATCH;
510 /** Add bytes to the output vector and then flagg the I/O system that bytes
511 * are available to be sent.
514 unix_cli_add_pending_output (clib_file_t * uf,
515 unix_cli_file_t * cf,
516 u8 * buffer, uword buffer_bytes)
518 clib_file_main_t *fm = &file_main;
520 vec_add (cf->output_vector, buffer, buffer_bytes);
521 if (vec_len (cf->output_vector) > 0)
523 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
524 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
526 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
530 /** Delete all bytes from the output vector and flag the I/O system
531 * that no more bytes are available to be sent.
534 unix_cli_del_pending_output (clib_file_t * uf,
535 unix_cli_file_t * cf, uword n_bytes)
537 clib_file_main_t *fm = &file_main;
539 vec_delete (cf->output_vector, n_bytes, 0);
540 if (vec_len (cf->output_vector) <= 0)
542 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
543 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
545 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
549 /** @brief A bit like strchr with a buffer length limit.
550 * Search a buffer for the first instance of a character up to the limit of
551 * the buffer length. If found then return the position of that character.
553 * The key departure from strchr is that if the character is not found then
554 * return the buffer length.
556 * @param chr The byte value to search for.
557 * @param str The buffer in which to search for the value.
558 * @param len The depth into the buffer to search.
560 * @return The index of the first occurence of \c chr. If \c chr is not
561 * found then \c len instead.
564 unix_vlib_findchr (u8 chr, u8 * str, word len)
567 for (i = 0; i < len; i++, str++)
575 /** @brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
576 * Attempts to write given buffer to the file descriptor of the given
577 * Unix CLI session. If that session already has data in the output buffer
578 * or if the write attempt tells us to try again later then the given buffer
579 * is appended to the pending output buffer instead.
581 * This is typically called only from \c unix_vlib_cli_output_cooked since
582 * that is where CRLF handling occurs or from places where we explicitly do
583 * not want cooked handling.
585 * @param cf Unix CLI session of the desired stream to write to.
586 * @param uf The Unix file structure of the desired stream to write to.
587 * @param buffer Pointer to the buffer that needs to be written.
588 * @param buffer_bytes The number of bytes from \c buffer to write.
591 unix_vlib_cli_output_raw (unix_cli_file_t * cf,
592 clib_file_t * uf, u8 * buffer, uword buffer_bytes)
596 if (vec_len (cf->output_vector) == 0)
597 n = write (uf->file_descriptor, buffer, buffer_bytes);
599 if (n < 0 && errno != EAGAIN)
601 clib_unix_warning ("write");
603 else if ((word) n < (word) buffer_bytes)
605 /* We got EAGAIN or we already have stuff in the buffer;
606 * queue up whatever didn't get sent for later. */
609 unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
613 /** @brief Process a buffer for CRLF handling before outputting it to the CLI.
615 * @param cf Unix CLI session of the desired stream to write to.
616 * @param uf The Unix file structure of the desired stream to write to.
617 * @param buffer Pointer to the buffer that needs to be written.
618 * @param buffer_bytes The number of bytes from \c buffer to write.
621 unix_vlib_cli_output_cooked (unix_cli_file_t * cf,
623 u8 * buffer, uword buffer_bytes)
625 word end = 0, start = 0;
627 while (end < buffer_bytes)
631 /* iterate the line on \n's so we can insert a \r before it */
632 end = unix_vlib_findchr ('\n',
634 buffer_bytes - start) + start;
638 /* otherwise just send the whole buffer */
642 unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start);
646 if (end < buffer_bytes)
648 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2);
649 end++; /* skip the \n that we already sent */
656 /** @brief Output the CLI prompt */
658 unix_cli_cli_prompt (unix_cli_file_t * cf, clib_file_t * uf)
660 unix_cli_main_t *cm = &unix_cli_main;
662 unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt, vec_len (cm->cli_prompt));
665 /** @brief Output a pager prompt and show number of buffered lines */
667 unix_cli_pager_prompt (unix_cli_file_t * cf, clib_file_t * uf)
672 h = cf->pager_start + (cf->height - 1);
673 if (h > vec_len (cf->pager_index))
674 h = vec_len (cf->pager_index);
676 prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s",
677 cf->ansi_capable ? ANSI_BOLD : "",
680 vec_len (cf->pager_index),
681 cf->ansi_capable ? ANSI_RESET : "");
683 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
688 /** @brief Output a pager "skipping" message */
690 unix_cli_pager_message (unix_cli_file_t * cf, clib_file_t * uf,
691 char *message, char *postfix)
695 prompt = format (0, "\r%s-- %s --%s%s",
696 cf->ansi_capable ? ANSI_BOLD : "",
697 message, cf->ansi_capable ? ANSI_RESET : "", postfix);
699 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
704 /** @brief Erase the printed pager prompt */
706 unix_cli_pager_prompt_erase (unix_cli_file_t * cf, clib_file_t * uf)
708 if (cf->ansi_capable)
710 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
711 unix_vlib_cli_output_cooked (cf, uf,
712 (u8 *) ANSI_CLEARLINE,
713 sizeof (ANSI_CLEARLINE) - 1);
719 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
720 for (i = 0; i < cf->width - 1; i++)
721 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
722 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
726 /** @brief Uses an ANSI escape sequence to move the cursor */
728 unix_cli_ansi_cursor (unix_cli_file_t * cf, clib_file_t * uf, u16 x, u16 y)
732 str = format (0, "%s%d;%dH", CSI, y, x);
734 unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str));
739 /** Redraw the currently displayed page of text.
740 * @param cf CLI session to redraw the pager buffer of.
741 * @param uf Unix file of the CLI session.
744 unix_cli_pager_redraw (unix_cli_file_t * cf, clib_file_t * uf)
746 unix_cli_pager_index_t *pi = NULL;
750 /* No active pager? Do nothing. */
751 if (!vec_len (cf->pager_index))
754 if (cf->ansi_capable)
756 /* If we have ANSI, send the clear screen sequence */
757 unix_vlib_cli_output_cooked (cf, uf,
759 sizeof (ANSI_CLEAR) - 1);
763 /* Otherwise make sure we're on a blank line */
764 unix_cli_pager_prompt_erase (cf, uf);
767 /* (Re-)send the current page of content */
768 for (i = 0; i < cf->height - 1 &&
769 i + cf->pager_start < vec_len (cf->pager_index); i++)
771 pi = &cf->pager_index[cf->pager_start + i];
772 line = cf->pager_vector[pi->line] + pi->offset;
774 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
776 /* if the last line didn't end in newline, add a newline */
777 if (pi && line[pi->length - 1] != '\n')
778 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
780 unix_cli_pager_prompt (cf, uf);
783 /** @brief Process and add a line to the pager index.
784 * In normal operation this function will take the given character string
785 * found in @c line and with length @c len_or_index and iterates the over the
786 * contents, adding each line of text discovered within it to the
787 * pager index. Lines are identified by newlines ("<code>\\n</code>") and by
788 * strings longer than the width of the terminal.
790 * If instead @c line is @c NULL then @c len_or_index is taken to mean the
791 * index of an existing line in the pager buffer; this simply means that the
792 * input line does not need to be cloned since we alreayd have it. This is
793 * typical if we are reindexing the pager buffer.
795 * @param cf The CLI session whose pager we are adding to.
796 * @param line The string of text to be indexed into the pager buffer.
797 * If @c line is @c NULL then the mode of operation
798 * changes slightly; see the description above.
799 * @param len_or_index If @c line is a pointer to a string then this parameter
800 * indicates the length of that string; Otherwise this
801 * value provides the index in the pager buffer of an
802 * existing string to be indexed.
805 unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index)
809 word line_index, len;
810 u32 width = cf->width;
811 unix_cli_pager_index_t *pi;
815 /* Use a line already in the pager buffer */
816 line_index = len_or_index;
817 p = cf->pager_vector[line_index];
823 /* Add a copy of the raw string to the pager buffer */
824 p = vec_new (u8, len);
825 clib_memcpy (p, line, len);
827 /* store in pager buffer */
828 line_index = vec_len (cf->pager_vector);
829 vec_add1 (cf->pager_vector, p);
835 /* Find the next line, or run to terminal width, or run to EOL */
837 j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width);
839 if (j < l && p[j] == '\n') /* incl \n */
842 /* Add the line to the index */
843 k = vec_len (cf->pager_index);
844 vec_validate (cf->pager_index, k);
845 pi = &cf->pager_index[k];
847 pi->line = line_index;
856 /** @brief Reindex entire pager buffer.
857 * Resets the current pager index and then re-adds the lines in the pager
858 * buffer to the index.
860 * Additionally this function attempts to retain the current page start
861 * line offset by searching for the same top-of-screen line in the new index.
863 * @param cf The CLI session whose pager buffer should be reindexed.
866 unix_cli_pager_reindex (unix_cli_file_t * cf)
868 word i, old_line, old_offset;
869 unix_cli_pager_index_t *pi;
871 /* If there is nothing in the pager buffer then make sure the index
872 * is empty and move on.
874 if (cf->pager_vector == 0)
876 vec_reset_length (cf->pager_index);
880 /* Retain a pointer to the current page start line so we can
883 pi = &cf->pager_index[cf->pager_start];
885 old_offset = pi->offset;
887 /* Re-add the buffered lines to the index */
888 vec_reset_length (cf->pager_index);
889 vec_foreach_index (i, cf->pager_vector)
891 unix_cli_pager_add_line (cf, NULL, i);
894 /* Attempt to re-locate the previously stored page start line */
895 vec_foreach_index (i, cf->pager_index)
897 pi = &cf->pager_index[i];
899 if (pi->line == old_line &&
900 (pi->offset <= old_offset || pi->offset + pi->length > old_offset))
908 /* In case the start line was not found (rare), ensure the pager start
909 * index is within bounds
911 if (cf->pager_start >= vec_len (cf->pager_index))
913 if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1))
916 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
920 /** VLIB CLI output function.
922 * If the terminal has a pager configured then this function takes care
923 * of collating output into the pager buffer; ensuring only the first page
924 * is displayed and any lines in excess of the first page are buffered.
926 * If the maximum number of index lines in the buffer is exceeded then the
927 * pager is cancelled and the contents of the current buffer are sent to the
930 * If there is no pager configured then the output is sent directly to the
933 * @param cli_file_index Index of the CLI session where this output is
935 * @param buffer String of printabe bytes to be output.
936 * @param buffer_bytes The number of bytes in @c buffer to be output.
939 unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes)
941 unix_main_t *um = &unix_main;
942 clib_file_main_t *fm = &file_main;
943 unix_cli_main_t *cm = &unix_cli_main;
947 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
948 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
950 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
952 unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes);
956 word row = vec_len (cf->pager_index);
958 unix_cli_pager_index_t *pi;
960 /* Index and add the output lines to the pager buffer. */
961 unix_cli_pager_add_line (cf, buffer, buffer_bytes);
963 /* Now iterate what was added to display the lines.
964 * If we reach the bottom of the page, display a prompt.
966 while (row < vec_len (cf->pager_index))
968 if (row < cf->height - 1)
970 /* output this line */
971 pi = &cf->pager_index[row];
972 line = cf->pager_vector[pi->line] + pi->offset;
973 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
975 /* if the last line didn't end in newline, and we're at the
976 * bottom of the page, add a newline */
977 if (line[pi->length - 1] != '\n' && row == cf->height - 2)
978 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
982 /* Display the pager prompt every 10 lines */
984 unix_cli_pager_prompt (cf, uf);
989 /* Check if we went over the pager buffer limit */
990 if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit)
992 /* Stop using the pager for the remainder of this CLI command */
995 /* If we likely printed the prompt, erase it */
996 if (vec_len (cf->pager_index) > cf->height - 1)
997 unix_cli_pager_prompt_erase (cf, uf);
999 /* Dump out the contents of the buffer */
1000 for (row = cf->pager_start + (cf->height - 1);
1001 row < vec_len (cf->pager_index); row++)
1003 pi = &cf->pager_index[row];
1004 line = cf->pager_vector[pi->line] + pi->offset;
1005 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1008 unix_cli_pager_reset (cf);
1013 /** Identify whether a terminal type is ANSI capable.
1015 * Compares the string given in @c term with a list of terminal types known
1016 * to support ANSI escape sequences.
1018 * This list contains, for example, @c xterm, @c screen and @c ansi.
1020 * @param term A string with a terminal type in it.
1021 * @param len The length of the string in @c term.
1023 * @return @c 1 if the terminal type is recognized as supporting ANSI
1024 * terminal sequences; @c 0 otherwise.
1027 unix_cli_terminal_type (u8 * term, uword len)
1029 /* This may later be better done as a hash of some sort. */
1031 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1036 _("xterm-256color"); /* iTerm on Mac */
1038 _("screen-256color"); /* Screen and tmux */
1039 _("ansi"); /* Microsoft Telnet */
1045 /** @brief Emit initial welcome banner and prompt on a connection. */
1047 unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf)
1049 unix_main_t *um = &unix_main;
1050 clib_file_main_t *fm = &file_main;
1051 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
1052 unix_cli_banner_t *banner;
1056 * Put the first bytes directly into the buffer so that further output is
1057 * queued until everything is ready. (oterwise initial prompt can appear
1058 * mid way through VPP initialization)
1060 unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
1062 if (!um->cli_no_banner)
1064 if (cf->ansi_capable)
1066 banner = unix_cli_banner_color;
1067 len = ARRAY_LEN (unix_cli_banner_color);
1071 banner = unix_cli_banner;
1072 len = ARRAY_LEN (unix_cli_banner);
1075 for (i = 0; i < len; i++)
1077 unix_vlib_cli_output_cooked (cf, uf,
1078 banner[i].line, banner[i].length);
1083 unix_cli_cli_prompt (cf, uf);
1088 /** @brief A failsafe triggered on a timer to ensure we send the prompt
1089 * to telnet sessions that fail to negotiate the terminal type. */
1091 unix_cli_file_welcome_timer (any arg, f64 delay)
1093 unix_cli_main_t *cm = &unix_cli_main;
1094 unix_cli_file_t *cf;
1097 /* Check the connection didn't close already */
1098 if (pool_is_free_index (cm->cli_file_pool, (uword) arg))
1101 cf = pool_elt_at_index (cm->cli_file_pool, (uword) arg);
1104 unix_cli_file_welcome (cm, cf);
1107 /** @brief A mostly no-op Telnet state machine.
1108 * Process Telnet command bytes in a way that ensures we're mostly
1109 * transparent to the Telnet protocol. That is, it's mostly a no-op.
1111 * @return -1 if we need more bytes, otherwise a positive integer number of
1112 * bytes to consume from the input_vector, not including the initial
1116 unix_cli_process_telnet (unix_main_t * um,
1117 unix_cli_file_t * cf,
1118 clib_file_t * uf, u8 * input_vector, uword len)
1120 /* Input_vector starts at IAC byte.
1121 * See if we have a complete message; if not, return -1 so we wait for more.
1122 * if we have a complete message, consume those bytes from the vector.
1127 return -1; /* want more bytes */
1129 switch (input_vector[1])
1132 /* two IAC's in a row means to pass through 0xff.
1133 * since that makes no sense here, just consume it.
1142 /* Expect 3 bytes */
1143 if (vec_len (input_vector) < 3)
1144 return -1; /* want more bytes */
1151 /* Sub option - search ahead for IAC SE to end it */
1153 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
1155 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
1157 /* We have a complete message; see if we care about it */
1158 switch (input_vector[2])
1161 if (input_vector[3] != 0)
1163 /* See if the terminal type is ANSI capable */
1165 unix_cli_terminal_type (input_vector + 4, i - 5);
1166 /* If session not started, we can release the pause */
1168 /* Send the welcome banner and initial prompt */
1169 unix_cli_file_welcome (&unix_cli_main, cf);
1174 if (i != 8) /* check message is correct size */
1178 clib_net_to_host_u16 (*((u16 *) (input_vector + 3)));
1179 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
1180 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
1181 if (cf->width < UNIX_CLI_MIN_TERMINAL_WIDTH)
1182 cf->width = UNIX_CLI_MIN_TERMINAL_WIDTH;
1185 clib_net_to_host_u16 (*((u16 *) (input_vector + 5)));
1186 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
1187 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
1188 if (cf->height < UNIX_CLI_MIN_TERMINAL_HEIGHT)
1189 cf->height = UNIX_CLI_MIN_TERMINAL_HEIGHT;
1191 /* reindex pager buffer */
1192 unix_cli_pager_reindex (cf);
1194 unix_cli_pager_redraw (cf, uf);
1200 /* Consume it all */
1206 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
1207 consume = 1; /* hit max search depth, advance one byte */
1210 return -1; /* want more bytes */
1228 /* Simple one-byte messages */
1233 /* Are You There - trigger a visible response */
1235 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
1239 /* Unknown command! Eat the IAC byte */
1246 /** @brief Process actionable input.
1247 * Based on the \c action process the input; this typically involves
1248 * searching the command history or editing the current command line.
1251 unix_cli_line_process_one (unix_cli_main_t * cm,
1253 unix_cli_file_t * cf,
1255 u8 input, unix_cli_parse_action_t action)
1259 u8 **possible_commands;
1264 case UNIX_CLI_PARSE_ACTION_NOACTION:
1267 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
1268 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
1269 if (!cf->has_history || !cf->history_limit)
1271 if (cf->search_mode == 0)
1273 /* Erase the current command (if any) */
1274 for (j = 0; j < (vec_len (cf->current_command)); j++)
1275 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1277 vec_reset_length (cf->search_key);
1278 vec_reset_length (cf->current_command);
1279 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1280 cf->search_mode = -1;
1282 cf->search_mode = 1;
1287 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1288 cf->search_mode = -1;
1290 cf->search_mode = 1;
1292 cf->excursion += cf->search_mode;
1297 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
1298 /* Erase the command from the cursor to the start */
1300 /* Shimmy forwards to the new end of line position */
1301 delta = vec_len (cf->current_command) - cf->cursor;
1302 for (j = cf->cursor; j > delta; j--)
1303 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1304 /* Zap from here to the end of what is currently displayed */
1305 for (; j < (vec_len (cf->current_command)); j++)
1306 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1307 /* Get back to the start of the line */
1308 for (j = 0; j < (vec_len (cf->current_command)); j++)
1309 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1311 j = vec_len (cf->current_command) - cf->cursor;
1312 memmove (cf->current_command, cf->current_command + cf->cursor, j);
1313 _vec_len (cf->current_command) = j;
1315 /* Print the new contents */
1316 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j);
1317 /* Shimmy back to the start */
1318 for (j = 0; j < (vec_len (cf->current_command)); j++)
1319 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1322 cf->search_mode = 0;
1325 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
1326 /* Erase the command from the cursor to the end */
1328 /* Zap from cursor to end of what is currently displayed */
1329 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1330 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1331 /* Get back to where we were */
1332 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1333 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1335 /* Truncate the line at the cursor */
1336 _vec_len (cf->current_command) = cf->cursor;
1338 cf->search_mode = 0;
1341 case UNIX_CLI_PARSE_ACTION_LEFT:
1344 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1348 cf->search_mode = 0;
1351 case UNIX_CLI_PARSE_ACTION_RIGHT:
1352 if (cf->cursor < vec_len (cf->current_command))
1354 /* have to emit the character under the cursor */
1355 unix_vlib_cli_output_cooked (cf, uf,
1356 cf->current_command + cf->cursor, 1);
1360 cf->search_mode = 0;
1363 case UNIX_CLI_PARSE_ACTION_UP:
1364 case UNIX_CLI_PARSE_ACTION_DOWN:
1365 if (!cf->has_history || !cf->history_limit)
1367 cf->search_mode = 0;
1368 /* Erase the command */
1369 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1370 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1371 for (j = 0; j < (vec_len (cf->current_command)); j++)
1372 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1373 vec_reset_length (cf->current_command);
1374 if (vec_len (cf->command_history))
1376 if (action == UNIX_CLI_PARSE_ACTION_UP)
1381 cf->excursion += delta;
1383 if (cf->excursion == vec_len (cf->command_history))
1385 /* down-arrowed to last entry - want a blank line */
1386 _vec_len (cf->current_command) = 0;
1388 else if (cf->excursion < 0)
1390 /* up-arrowed over the start to the end, want a blank line */
1391 cf->excursion = vec_len (cf->command_history);
1392 _vec_len (cf->current_command) = 0;
1396 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1397 /* down-arrowed past end - wrap to start */
1400 /* Print the command at the current position */
1401 prev = cf->command_history[cf->excursion];
1402 vec_validate (cf->current_command, vec_len (prev) - 1);
1404 clib_memcpy (cf->current_command, prev, vec_len (prev));
1405 _vec_len (cf->current_command) = vec_len (prev);
1406 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1407 vec_len (cf->current_command));
1409 cf->cursor = vec_len (cf->current_command);
1415 case UNIX_CLI_PARSE_ACTION_HOME:
1416 if (vec_len (cf->current_command) && cf->cursor > 0)
1420 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1425 cf->search_mode = 0;
1428 case UNIX_CLI_PARSE_ACTION_END:
1429 if (vec_len (cf->current_command) &&
1430 cf->cursor < vec_len (cf->current_command))
1432 unix_vlib_cli_output_cooked (cf, uf,
1433 cf->current_command + cf->cursor,
1434 vec_len (cf->current_command) -
1436 cf->cursor = vec_len (cf->current_command);
1439 cf->search_mode = 0;
1442 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1443 if (vec_len (cf->current_command) && cf->cursor > 0)
1447 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1450 while (j && isspace (cf->current_command[j]))
1452 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1455 while (j && !isspace (cf->current_command[j]))
1457 if (isspace (cf->current_command[j - 1]))
1459 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1466 cf->search_mode = 0;
1469 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1470 if (vec_len (cf->current_command) &&
1471 cf->cursor < vec_len (cf->current_command))
1473 int e = vec_len (cf->current_command);
1475 while (j < e && !isspace (cf->current_command[j]))
1477 while (j < e && isspace (cf->current_command[j]))
1479 unix_vlib_cli_output_cooked (cf, uf,
1480 cf->current_command + cf->cursor,
1485 cf->search_mode = 0;
1489 case UNIX_CLI_PARSE_ACTION_ERASE:
1490 if (vec_len (cf->current_command))
1492 if (cf->cursor == vec_len (cf->current_command))
1494 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1495 _vec_len (cf->current_command)--;
1498 else if (cf->cursor > 0)
1500 /* shift everything at & to the right of the cursor left by 1 */
1501 j = vec_len (cf->current_command) - cf->cursor;
1502 memmove (cf->current_command + cf->cursor - 1,
1503 cf->current_command + cf->cursor, j);
1504 _vec_len (cf->current_command)--;
1506 /* redraw the rest of the line */
1507 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1508 unix_vlib_cli_output_cooked (cf, uf,
1509 cf->current_command + cf->cursor,
1511 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
1512 /* and shift the terminal cursor back where it should be */
1514 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1517 cf->search_mode = 0;
1519 vec_reset_length (cf->search_key);
1522 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1523 if (vec_len (cf->current_command))
1525 if (cf->cursor < vec_len (cf->current_command))
1527 /* shift everything to the right of the cursor left by 1 */
1528 j = vec_len (cf->current_command) - cf->cursor - 1;
1529 memmove (cf->current_command + cf->cursor,
1530 cf->current_command + cf->cursor + 1, j);
1531 _vec_len (cf->current_command)--;
1532 /* redraw the rest of the line */
1533 unix_vlib_cli_output_cooked (cf, uf,
1534 cf->current_command + cf->cursor,
1536 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
1537 /* and shift the terminal cursor back where it should be */
1540 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1542 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1546 else if (input == 'D' - '@')
1548 /* ^D with no command entered = quit */
1549 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1550 vlib_process_signal_event (um->vlib_main,
1551 vlib_current_process (um->vlib_main),
1552 UNIX_CLI_PROCESS_EVENT_QUIT,
1553 cf - cm->cli_file_pool);
1555 cf->search_mode = 0;
1557 vec_reset_length (cf->search_key);
1560 case UNIX_CLI_PARSE_ACTION_CLEAR:
1561 /* If we're in ANSI mode, clear the screen.
1562 * Then redraw the prompt and any existing command input, then put
1563 * the cursor back where it was in that line.
1565 if (cf->ansi_capable)
1566 unix_vlib_cli_output_cooked (cf, uf,
1568 sizeof (ANSI_CLEAR) - 1);
1570 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1572 unix_vlib_cli_output_raw (cf, uf,
1573 cm->cli_prompt, vec_len (cm->cli_prompt));
1574 unix_vlib_cli_output_raw (cf, uf,
1575 cf->current_command,
1576 vec_len (cf->current_command));
1577 for (j = cf->cursor; j < vec_len (cf->current_command); j++)
1578 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1582 case UNIX_CLI_PARSE_ACTION_TAB:
1583 if (cf->cursor < vec_len (cf->current_command))
1585 /* if we are in the middle of a line, complete only if
1586 * the cursor points to whitespace */
1587 if (isspace (cf->current_command[cf->cursor]))
1589 /* save and clear any input that is after the cursor */
1590 vec_resize (save, vec_len (cf->current_command) - cf->cursor);
1591 clib_memcpy (save, cf->current_command + cf->cursor,
1592 vec_len (cf->current_command) - cf->cursor);
1593 _vec_len (cf->current_command) = cf->cursor;
1597 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1602 vlib_cli_get_possible_completions (cf->current_command);
1603 if (vec_len (possible_commands) == 1)
1606 u8 *completed = possible_commands[0];
1608 /* find the last word of current_command */
1609 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1612 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1614 _vec_len (cf->current_command) = j;
1616 /* replace it with the newly expanded command */
1617 vec_append (cf->current_command, completed);
1619 /* echo to the terminal */
1620 unix_vlib_cli_output_raw (cf, uf, completed, vec_len (completed));
1622 /* add one trailing space if needed */
1623 if (vec_len (save) == 0)
1625 vec_add1 (cf->current_command, ' ');
1626 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1629 cf->cursor = vec_len (cf->current_command);
1632 else if (vec_len (possible_commands) >= 2)
1634 u8 **possible_command;
1635 uword max_command_len = 0, min_command_len = ~0;
1638 vec_foreach (possible_command, possible_commands)
1640 if (vec_len (*possible_command) > max_command_len)
1642 max_command_len = vec_len (*possible_command);
1644 if (vec_len (*possible_command) < min_command_len)
1646 min_command_len = vec_len (*possible_command);
1650 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1653 vec_foreach (possible_command, possible_commands)
1655 if (i + max_command_len >= cf->width)
1657 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1660 unix_vlib_cli_output_raw (cf, uf, *possible_command,
1661 vec_len (*possible_command));
1662 for (j = vec_len (*possible_command); j < max_command_len + 2;
1665 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1667 i += max_command_len + 2;
1670 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1672 /* rewrite prompt */
1673 unix_cli_cli_prompt (cf, uf);
1674 unix_vlib_cli_output_raw (cf, uf, cf->current_command,
1675 vec_len (cf->current_command));
1677 /* count length of last word */
1680 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1686 /* determine smallest common command */
1687 for (; i < min_command_len; i++)
1691 vec_foreach (possible_command, possible_commands)
1695 common = (*possible_command)[i];
1697 else if (common != (*possible_command)[i])
1705 vec_add1 (cf->current_command, common);
1707 unix_vlib_cli_output_raw (cf, uf, (u8 *) & common, 1);
1717 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1720 if (vec_len (save) > 0)
1722 /* restore remaining input if tab was hit in the middle of a line */
1723 unix_vlib_cli_output_raw (cf, uf, save, vec_len (save));
1724 for (j = 0; j < vec_len (save); j++)
1726 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1728 vec_append (cf->current_command, save);
1731 vec_free (possible_commands);
1734 case UNIX_CLI_PARSE_ACTION_YANK:
1739 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
1741 unix_cli_pager_prompt_erase (cf, uf);
1742 unix_cli_pager_reset (cf);
1743 unix_cli_cli_prompt (cf, uf);
1746 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
1747 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
1748 /* show next page of the buffer */
1749 if (cf->height + cf->pager_start < vec_len (cf->pager_index))
1752 unix_cli_pager_index_t *pi = NULL;
1754 int m = cf->pager_start + (cf->height - 1);
1755 unix_cli_pager_prompt_erase (cf, uf);
1757 j < vec_len (cf->pager_index) && cf->pager_start < m;
1758 j++, cf->pager_start++)
1760 pi = &cf->pager_index[j];
1761 line = cf->pager_vector[pi->line] + pi->offset;
1762 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1764 /* if the last line didn't end in newline, add a newline */
1765 if (pi && line[pi->length - 1] != '\n')
1766 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1767 unix_cli_pager_prompt (cf, uf);
1771 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
1772 /* no more in buffer, exit, but only if it was <space> */
1777 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
1778 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
1779 /* display the next line of the buffer */
1780 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
1783 unix_cli_pager_index_t *pi;
1785 unix_cli_pager_prompt_erase (cf, uf);
1786 pi = &cf->pager_index[cf->pager_start + (cf->height - 1)];
1787 line = cf->pager_vector[pi->line] + pi->offset;
1788 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1790 /* if the last line didn't end in newline, add a newline */
1791 if (line[pi->length - 1] != '\n')
1792 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1793 unix_cli_pager_prompt (cf, uf);
1797 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
1798 /* no more in buffer, exit, but only if it was <enter> */
1804 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
1805 /* scroll the page back one line */
1806 if (cf->pager_start > 0)
1809 unix_cli_pager_index_t *pi = NULL;
1812 if (cf->ansi_capable)
1814 pi = &cf->pager_index[cf->pager_start];
1815 line = cf->pager_vector[pi->line] + pi->offset;
1816 unix_cli_pager_prompt_erase (cf, uf);
1817 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN,
1818 sizeof (ANSI_SCROLLDN) - 1);
1819 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR,
1820 sizeof (ANSI_SAVECURSOR) - 1);
1821 unix_cli_ansi_cursor (cf, uf, 1, 1);
1822 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE,
1823 sizeof (ANSI_CLEARLINE) - 1);
1824 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1825 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR,
1826 sizeof (ANSI_RESTCURSOR) - 1);
1827 unix_cli_pager_prompt_erase (cf, uf);
1828 unix_cli_pager_prompt (cf, uf);
1832 int m = cf->pager_start + (cf->height - 1);
1833 unix_cli_pager_prompt_erase (cf, uf);
1834 for (j = cf->pager_start;
1835 j < vec_len (cf->pager_index) && j < m; j++)
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);
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);
1849 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
1850 /* back to the first page of the buffer */
1851 if (cf->pager_start > 0)
1854 unix_cli_pager_index_t *pi = NULL;
1856 cf->pager_start = 0;
1857 int m = cf->pager_start + (cf->height - 1);
1858 unix_cli_pager_prompt_erase (cf, uf);
1859 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1862 pi = &cf->pager_index[j];
1863 line = cf->pager_vector[pi->line] + pi->offset;
1864 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1866 /* if the last line didn't end in newline, add a newline */
1867 if (pi && line[pi->length - 1] != '\n')
1868 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1869 unix_cli_pager_prompt (cf, uf);
1873 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
1874 /* skip to the last page of the buffer */
1875 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
1878 unix_cli_pager_index_t *pi = NULL;
1880 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
1881 unix_cli_pager_prompt_erase (cf, uf);
1882 unix_cli_pager_message (cf, uf, "skipping", "\n");
1883 for (j = cf->pager_start; j < vec_len (cf->pager_index); j++)
1885 pi = &cf->pager_index[j];
1886 line = cf->pager_vector[pi->line] + pi->offset;
1887 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1889 /* if the last line didn't end in newline, add a newline */
1890 if (pi && line[pi->length - 1] != '\n')
1891 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1892 unix_cli_pager_prompt (cf, uf);
1896 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
1897 /* wander back one page in the buffer */
1898 if (cf->pager_start > 0)
1901 unix_cli_pager_index_t *pi = NULL;
1904 if (cf->pager_start >= cf->height)
1905 cf->pager_start -= cf->height - 1;
1907 cf->pager_start = 0;
1908 m = cf->pager_start + cf->height - 1;
1909 unix_cli_pager_prompt_erase (cf, uf);
1910 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1913 pi = &cf->pager_index[j];
1914 line = cf->pager_vector[pi->line] + pi->offset;
1915 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1917 /* if the last line didn't end in newline, add a newline */
1918 if (pi && line[pi->length - 1] != '\n')
1919 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1920 unix_cli_pager_prompt (cf, uf);
1924 case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW:
1925 /* Redraw the current pager screen */
1926 unix_cli_pager_redraw (cf, uf);
1929 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
1930 /* search forwards in the buffer */
1934 case UNIX_CLI_PARSE_ACTION_CRLF:
1936 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1938 if (cf->has_history && cf->history_limit)
1940 if (cf->command_history
1941 && vec_len (cf->command_history) >= cf->history_limit)
1943 vec_free (cf->command_history[0]);
1944 vec_delete (cf->command_history, 1, 0);
1946 /* Don't add blank lines to the cmd history */
1947 if (vec_len (cf->current_command))
1949 /* Don't duplicate the previous command */
1950 j = vec_len (cf->command_history);
1952 (vec_len (cf->current_command) !=
1953 vec_len (cf->command_history[j - 1])
1954 || memcmp (cf->current_command, cf->command_history[j - 1],
1955 vec_len (cf->current_command)) != 0))
1957 /* copy the command to the history */
1959 vec_append (c, cf->current_command);
1960 vec_add1 (cf->command_history, c);
1961 cf->command_number++;
1964 cf->excursion = vec_len (cf->command_history);
1967 cf->search_mode = 0;
1968 vec_reset_length (cf->search_key);
1973 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
1974 case UNIX_CLI_PARSE_ACTION_NOMATCH:
1975 if (vec_len (cf->pager_index))
1979 else if (cf->has_history && cf->search_mode && isprint (input))
1981 int k, limit, offset;
1984 vec_add1 (cf->search_key, input);
1987 for (j = 0; j < vec_len (cf->command_history); j++)
1989 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1991 else if (cf->excursion < 0)
1992 cf->excursion = vec_len (cf->command_history) - 1;
1994 item = cf->command_history[cf->excursion];
1996 limit = (vec_len (cf->search_key) > vec_len (item)) ?
1997 vec_len (item) : vec_len (cf->search_key);
1999 for (offset = 0; offset <= vec_len (item) - limit; offset++)
2001 for (k = 0; k < limit; k++)
2003 if (item[k + offset] != cf->search_key[k])
2006 goto found_at_offset;
2014 for (j = 0; j < vec_len (cf->current_command); j++)
2015 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
2017 vec_validate (cf->current_command, vec_len (item) - 1);
2018 clib_memcpy (cf->current_command, item, vec_len (item));
2019 _vec_len (cf->current_command) = vec_len (item);
2021 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
2022 vec_len (cf->current_command));
2023 cf->cursor = vec_len (cf->current_command);
2027 cf->excursion += cf->search_mode;
2030 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12);
2031 vec_reset_length (cf->search_key);
2032 vec_reset_length (cf->current_command);
2033 cf->search_mode = 0;
2037 else if (isprint (input)) /* skip any errant control codes */
2039 if (cf->cursor == vec_len (cf->current_command))
2042 vec_add1 (cf->current_command, input);
2045 /* Echo the character back to the client */
2046 unix_vlib_cli_output_raw (cf, uf, &input, 1);
2050 /* Insert at cursor: resize +1 byte, move everything over */
2051 j = vec_len (cf->current_command) - cf->cursor;
2052 vec_add1 (cf->current_command, (u8) 'A');
2053 memmove (cf->current_command + cf->cursor + 1,
2054 cf->current_command + cf->cursor, j);
2055 cf->current_command[cf->cursor] = input;
2056 /* Redraw the line */
2058 unix_vlib_cli_output_raw (cf, uf,
2059 cf->current_command + cf->cursor, j);
2060 /* Put terminal cursor back */
2062 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
2068 /* no-op - not printable or otherwise not actionable */
2075 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2081 /** @brief Process input bytes on a stream to provide line editing and
2082 * command history in the CLI. */
2084 unix_cli_line_edit (unix_cli_main_t * cm, unix_main_t * um,
2085 clib_file_main_t * fm, unix_cli_file_t * cf)
2087 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2090 for (i = 0; i < vec_len (cf->input_vector); i++)
2092 unix_cli_parse_action_t action;
2094 unix_cli_parse_actions_t *a;
2096 /* If we're in the pager mode, search the pager actions */
2098 vec_len (cf->pager_index) ? unix_cli_parse_pager :
2099 unix_cli_parse_strings;
2101 /* See if the input buffer is some sort of control code */
2102 action = unix_cli_match_action (a, &cf->input_vector[i],
2103 vec_len (cf->input_vector) - i,
2108 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2111 /* There was a partial match which means we need more bytes
2112 * than the input buffer currently has.
2113 * Since the bytes before here have been processed, shift
2114 * the remaining contents to the start of the input buffer.
2116 vec_delete (cf->input_vector, i, 0);
2118 return 1; /* wait for more */
2120 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2121 /* process telnet options */
2122 matched = unix_cli_process_telnet (um, cf, uf,
2123 cf->input_vector + i,
2124 vec_len (cf->input_vector) - i);
2129 /* There was a partial match which means we need more bytes
2130 * than the input buffer currently has.
2131 * Since the bytes before here have been processed, shift
2132 * the remaining contents to the start of the input buffer.
2134 vec_delete (cf->input_vector, i, 0);
2136 return 1; /* wait for more */
2141 /* process the action */
2142 if (!unix_cli_line_process_one (cm, um, cf, uf,
2143 cf->input_vector[i], action))
2145 /* CRLF found. Consume the bytes from the input_vector */
2146 vec_delete (cf->input_vector, i + matched, 0);
2147 /* And tell our caller to execute cf->input_command */
2155 vec_reset_length (cf->input_vector);
2159 /** @brief Process input to a CLI session. */
2161 unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index)
2163 unix_main_t *um = &unix_main;
2164 clib_file_main_t *fm = &file_main;
2166 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2167 unformat_input_t input;
2168 int vlib_parse_eval (u8 *);
2171 /* Try vlibplex first. Someday... */
2172 if (0 && vlib_parse_eval (cf->input_vector) == 0)
2177 /* just treat whatever we got as a complete line of input */
2178 cf->current_command = cf->input_vector;
2182 /* Line edit, echo, etc. */
2183 if (unix_cli_line_edit (cm, um, fm, cf))
2184 /* want more input */
2191 vec_reset_length (lv);
2192 lv = format (lv, "%U[%d]: %v",
2193 format_timeval, 0 /* current bat-time */ ,
2194 0 /* current bat-format */ ,
2195 cli_file_index, cf->input_vector);
2197 int rv __attribute__ ((unused)) =
2198 write (um->log_fd, lv, vec_len (lv));
2202 /* Copy our input command to a new string */
2203 unformat_init_vector (&input, cf->current_command);
2205 /* Remove leading white space from input. */
2206 (void) unformat (&input, "");
2208 cm->current_input_file_index = cli_file_index;
2209 cf->pager_start = 0; /* start a new pager session */
2211 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
2212 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output,
2215 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
2218 unformat_free (&input);
2220 /* Re-fetch pointer since pool may have moved. */
2221 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2222 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2225 /* reset vector; we'll re-use it later */
2227 vec_reset_length (cf->input_vector);
2229 vec_reset_length (cf->current_command);
2231 if (cf->no_pager == 2)
2233 /* Pager was programmatically disabled */
2234 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
2235 cf->no_pager = um->cli_no_pager;
2238 if (vec_len (cf->pager_index) == 0
2239 || vec_len (cf->pager_index) < cf->height)
2241 /* There was no need for the pager */
2242 unix_cli_pager_reset (cf);
2245 unix_cli_cli_prompt (cf, uf);
2249 /* Display the pager prompt */
2250 unix_cli_pager_prompt (cf, uf);
2253 /* Any residual data in the input vector? */
2254 if (vec_len (cf->input_vector))
2258 /** Destroy a CLI session.
2259 * @note If we destroy the @c stdin session this additionally signals
2260 * the shutdown of VPP.
2263 unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
2265 unix_main_t *um = &unix_main;
2266 clib_file_main_t *fm = &file_main;
2267 unix_cli_file_t *cf;
2271 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2272 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2274 /* Quit/EOF on stdin means quit program. */
2275 if (uf->file_descriptor == UNIX_CLI_STDIN_FD)
2276 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
2278 vec_free (cf->current_command);
2279 vec_free (cf->search_key);
2281 for (i = 0; i < vec_len (cf->command_history); i++)
2282 vec_free (cf->command_history[i]);
2284 vec_free (cf->command_history);
2286 clib_file_del (fm, uf);
2288 unix_cli_file_free (cf);
2289 pool_put (cm->cli_file_pool, cf);
2292 /** Handle system events. */
2294 unix_cli_process (vlib_main_t * vm,
2295 vlib_node_runtime_t * rt, vlib_frame_t * f)
2297 unix_cli_main_t *cm = &unix_cli_main;
2302 unix_cli_process_event_type_t event_type;
2303 vlib_process_wait_for_event (vm);
2304 event_type = vlib_process_get_events (vm, &data);
2308 case UNIX_CLI_PROCESS_EVENT_READ_READY:
2309 for (i = 0; i < vec_len (data); i++)
2310 unix_cli_process_input (cm, data[i]);
2313 case UNIX_CLI_PROCESS_EVENT_QUIT:
2314 /* Kill this process. */
2315 for (i = 0; i < vec_len (data); i++)
2316 unix_cli_kill (cm, data[i]);
2321 _vec_len (data) = 0;
2327 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
2329 /* Add node index so we can re-use this process later. */
2330 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
2335 /** Called when a CLI session file descriptor can be written to without
2337 static clib_error_t *
2338 unix_cli_write_ready (clib_file_t * uf)
2340 unix_cli_main_t *cm = &unix_cli_main;
2341 unix_cli_file_t *cf;
2344 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2346 /* Flush output vector. */
2347 n = write (uf->file_descriptor,
2348 cf->output_vector, vec_len (cf->output_vector));
2350 if (n < 0 && errno != EAGAIN)
2351 return clib_error_return_unix (0, "write");
2354 unix_cli_del_pending_output (uf, cf, n);
2356 return /* no error */ 0;
2359 /** Called when a CLI session file descriptor has data to be read. */
2360 static clib_error_t *
2361 unix_cli_read_ready (clib_file_t * uf)
2363 unix_main_t *um = &unix_main;
2364 unix_cli_main_t *cm = &unix_cli_main;
2365 unix_cli_file_t *cf;
2367 int n, n_read, n_try;
2369 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2374 l = vec_len (cf->input_vector);
2375 vec_resize (cf->input_vector, l + n_try);
2377 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
2380 if (n < 0 && errno != EAGAIN)
2381 return clib_error_return_unix (0, "read");
2383 n_read = n < 0 ? 0 : n;
2384 _vec_len (cf->input_vector) = l + n_read;
2388 vlib_process_signal_event (um->vlib_main,
2389 cf->process_node_index,
2391 ? UNIX_CLI_PROCESS_EVENT_QUIT
2392 : UNIX_CLI_PROCESS_EVENT_READ_READY),
2393 /* event data */ uf->private_data);
2395 return /* no error */ 0;
2398 /** Store a new CLI session.
2399 * @param name The name of the session.
2400 * @param fd The file descriptor for the session I/O.
2401 * @return The session ID.
2404 unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
2406 unix_main_t *um = &unix_main;
2407 clib_file_main_t *fm = &file_main;
2408 unix_cli_file_t *cf;
2409 clib_file_t template = { 0 };
2410 vlib_main_t *vm = um->vlib_main;
2413 name = (char *) format (0, "unix-cli-%s", name);
2415 if (vec_len (cm->unused_cli_process_node_indices) > 0)
2417 uword l = vec_len (cm->unused_cli_process_node_indices);
2419 /* Find node and give it new name. */
2420 n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
2422 n->name = (u8 *) name;
2424 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
2426 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
2430 static vlib_node_registration_t r = {
2431 .function = unix_cli_process,
2432 .type = VLIB_NODE_TYPE_PROCESS,
2433 .process_log2_n_stack_bytes = 16,
2437 vlib_register_node (vm, &r);
2440 n = vlib_get_node (vm, r.index);
2443 pool_get (cm->cli_file_pool, cf);
2444 memset (cf, 0, sizeof (*cf));
2446 template.read_function = unix_cli_read_ready;
2447 template.write_function = unix_cli_write_ready;
2448 template.file_descriptor = fd;
2449 template.private_data = cf - cm->cli_file_pool;
2451 cf->process_node_index = n->index;
2452 cf->clib_file_index = clib_file_add (fm, &template);
2453 cf->output_vector = 0;
2454 cf->input_vector = 0;
2456 vlib_start_process (vm, n->runtime_index);
2458 vlib_process_t *p = vlib_get_process_from_node (vm, n);
2459 p->output_function = unix_vlib_cli_output;
2460 p->output_function_arg = cf - cm->cli_file_pool;
2462 return cf - cm->cli_file_pool;
2465 /** Telnet listening socket has a new connection. */
2466 static clib_error_t *
2467 unix_cli_listen_read_ready (clib_file_t * uf)
2469 unix_main_t *um = &unix_main;
2470 clib_file_main_t *fm = &file_main;
2471 unix_cli_main_t *cm = &unix_cli_main;
2472 clib_socket_t *s = &um->cli_listen_socket;
2473 clib_socket_t client;
2475 clib_error_t *error;
2476 unix_cli_file_t *cf;
2479 error = clib_socket_accept (s, &client);
2483 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
2485 cf_index = unix_cli_file_add (cm, client_name, client.fd);
2486 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2488 /* No longer need CLIB version of socket. */
2489 clib_socket_free (&client);
2491 vec_free (client_name);
2493 /* if we're supposed to run telnet session in character mode (default) */
2494 if (um->cli_line_mode == 0)
2497 * Set telnet client character mode, echo on, suppress "go-ahead".
2498 * Technically these should be negotiated, but this works.
2500 u8 charmode_option[] = {
2501 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
2502 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
2503 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
2504 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
2505 IAC, WILL, TELOPT_ECHO, /* server will do echo */
2506 IAC, DONT, TELOPT_ECHO, /* client should not echo */
2507 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
2508 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
2509 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
2510 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
2513 /* Enable history on this CLI */
2514 cf->history_limit = um->cli_history_limit;
2515 cf->has_history = cf->history_limit != 0;
2517 /* Make sure this session is in line mode */
2523 /* Setup the pager */
2524 cf->no_pager = um->cli_no_pager;
2526 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2528 /* Send the telnet options */
2529 unix_vlib_cli_output_raw (cf, uf, charmode_option,
2530 ARRAY_LEN (charmode_option));
2532 /* In case the client doesn't negotiate terminal type, use
2533 * a timer to kick off the initial prompt. */
2534 timer_call (unix_cli_file_welcome_timer, cf_index, 1);
2540 /** The system terminal has informed us that the window size
2544 unix_cli_resize_interrupt (int signum)
2546 clib_file_main_t *fm = &file_main;
2547 unix_cli_main_t *cm = &unix_cli_main;
2548 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2549 cm->stdin_cli_file_index);
2550 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2554 /* Terminal resized, fetch the new size */
2555 if (ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws) < 0)
2557 /* "Should never happen..." */
2558 clib_unix_warning ("TIOCGWINSZ");
2559 /* We can't trust ws.XXX... */
2563 cf->width = ws.ws_col;
2564 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
2565 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
2566 if (cf->width < UNIX_CLI_MIN_TERMINAL_WIDTH)
2567 cf->width = UNIX_CLI_MIN_TERMINAL_WIDTH;
2569 cf->height = ws.ws_row;
2570 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
2571 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
2572 if (cf->height < UNIX_CLI_MIN_TERMINAL_HEIGHT)
2573 cf->height = UNIX_CLI_MIN_TERMINAL_HEIGHT;
2575 /* Reindex the pager buffer */
2576 unix_cli_pager_reindex (cf);
2578 /* Redraw the page */
2579 unix_cli_pager_redraw (cf, uf);
2582 /** Handle configuration directives in the @em unix section. */
2583 static clib_error_t *
2584 unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
2586 unix_main_t *um = &unix_main;
2587 clib_file_main_t *fm = &file_main;
2588 unix_cli_main_t *cm = &unix_cli_main;
2590 clib_error_t *error = 0;
2591 unix_cli_file_t *cf;
2594 struct sigaction sa;
2598 /* We depend on unix flags being set. */
2599 if ((error = vlib_call_config_function (vm, unix_config)))
2602 if (um->flags & UNIX_FLAG_INTERACTIVE)
2604 /* Set stdin to be non-blocking. */
2605 if ((flags = fcntl (UNIX_CLI_STDIN_FD, F_GETFL, 0)) < 0)
2607 (void) fcntl (UNIX_CLI_STDIN_FD, F_SETFL, flags | O_NONBLOCK);
2609 cf_index = unix_cli_file_add (cm, "stdin", UNIX_CLI_STDIN_FD);
2610 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2611 cm->stdin_cli_file_index = cf_index;
2613 /* If stdin is a tty and we are using chacracter mode, enable
2614 * history on the CLI and set the tty line discipline accordingly. */
2615 if (isatty (UNIX_CLI_STDIN_FD) && um->cli_line_mode == 0)
2617 /* Capture terminal resize events */
2618 memset (&sa, 0, sizeof (sa));
2619 sa.sa_handler = unix_cli_resize_interrupt;
2620 if (sigaction (SIGWINCH, &sa, 0) < 0)
2621 clib_panic ("sigaction");
2623 /* Retrieve the current terminal size */
2624 ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws);
2625 cf->width = ws.ws_col;
2626 cf->height = ws.ws_row;
2628 if (cf->width == 0 || cf->height == 0)
2629 /* We have a tty, but no size. Stick to line mode. */
2632 /* Setup the history */
2633 cf->history_limit = um->cli_history_limit;
2634 cf->has_history = cf->history_limit != 0;
2636 /* Setup the pager */
2637 cf->no_pager = um->cli_no_pager;
2639 /* We're going to be in char by char mode */
2642 /* Save the original tty state so we can restore it later */
2643 tcgetattr (UNIX_CLI_STDIN_FD, &um->tio_stdin);
2646 /* Tweak the tty settings */
2647 tio = um->tio_stdin;
2648 /* echo off, canonical mode off, ext'd input processing off */
2649 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
2650 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
2651 tio.c_cc[VTIME] = 0; /* no timer */
2652 tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &tio);
2654 /* See if we can do ANSI/VT100 output */
2655 term = (u8 *) getenv ("TERM");
2657 cf->ansi_capable = unix_cli_terminal_type (term,
2664 /* No tty, so make sure these things are off */
2666 cf->history_limit = 0;
2667 cf->has_history = 0;
2671 /* Send banner and initial prompt */
2672 unix_cli_file_welcome (cm, cf);
2675 /* If we have socket config, LISTEN, otherwise, don't */
2676 clib_socket_t *s = &um->cli_listen_socket;
2677 if (s->config && s->config[0] != 0)
2680 clib_file_t template = { 0 };
2682 /* mkdir of file socketu, only under /run */
2683 if (strncmp (s->config, "/run", 4) == 0)
2685 u8 *tmp = format (0, "%s", s->config);
2686 int i = vec_len (tmp);
2687 while (i && tmp[--i] != '/')
2693 vlib_unix_recursive_mkdir ((char *) tmp);
2697 s->flags = CLIB_SOCKET_F_IS_SERVER | /* listen, don't connect */
2698 CLIB_SOCKET_F_ALLOW_GROUP_WRITE; /* PF_LOCAL socket only */
2699 error = clib_socket_init (s);
2704 template.read_function = unix_cli_listen_read_ready;
2705 template.file_descriptor = s->fd;
2707 clib_file_add (fm, &template);
2710 /* Set CLI prompt. */
2711 if (!cm->cli_prompt)
2712 cm->cli_prompt = format (0, "VLIB: ");
2718 * This module has no configurable parameters.
2720 VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
2722 /** Called when VPP is shutting down, this restores the system
2723 * terminal state if previously saved.
2725 static clib_error_t *
2726 unix_cli_exit (vlib_main_t * vm)
2728 unix_main_t *um = &unix_main;
2730 /* If stdin is a tty and we saved the tty state, reset the tty state */
2731 if (isatty (UNIX_CLI_STDIN_FD) && um->tio_isset)
2732 tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &um->tio_stdin);
2737 VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
2739 /** Set the CLI prompt.
2740 * @param prompt The C string to set the prompt to.
2741 * @note This setting is global; it impacts all current
2742 * and future CLI sessions.
2745 vlib_unix_cli_set_prompt (char *prompt)
2747 char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s ";
2748 unix_cli_main_t *cm = &unix_cli_main;
2750 vec_free (cm->cli_prompt);
2751 cm->cli_prompt = format (0, fmt, prompt);
2754 /** CLI command to quit the terminal session.
2755 * @note If this is a stdin session then this will
2756 * shutdown VPP also.
2758 static clib_error_t *
2759 unix_cli_quit (vlib_main_t * vm,
2760 unformat_input_t * input, vlib_cli_command_t * cmd)
2762 unix_cli_main_t *cm = &unix_cli_main;
2764 vlib_process_signal_event (vm,
2765 vlib_current_process (vm),
2766 UNIX_CLI_PROCESS_EVENT_QUIT,
2767 cm->current_input_file_index);
2772 * Terminates the current CLI session.
2774 * If VPP is running in @em interactive mode and this is the console session
2775 * (that is, the session on @c stdin) then this will also terminate VPP.
2778 VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
2780 .short_help = "Exit CLI",
2781 .function = unix_cli_quit,
2785 /** CLI command to execute a VPP command script. */
2786 static clib_error_t *
2787 unix_cli_exec (vlib_main_t * vm,
2788 unformat_input_t * input, vlib_cli_command_t * cmd)
2792 unformat_input_t sub_input;
2793 clib_error_t *error;
2799 if (!unformat (input, "%s", &file_name))
2801 error = clib_error_return (0, "expecting file name, got `%U'",
2802 format_unformat_error, input);
2806 fd = open (file_name, O_RDONLY);
2809 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
2813 /* Make sure its a regular file. */
2817 if (fstat (fd, &s) < 0)
2819 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
2823 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
2825 error = clib_error_return (0, "not a regular file `%s'", file_name);
2830 unformat_init_unix_file (&sub_input, fd);
2832 vlib_cli_input (vm, &sub_input, 0, 0);
2833 unformat_free (&sub_input);
2838 vec_free (file_name);
2844 * Executes a sequence of CLI commands which are read from a file.
2846 * If a command is unrecognised or otherwise invalid then the usual CLI
2847 * feedback will be generated, however execution of subsequent commands
2848 * from the file will continue.
2851 VLIB_CLI_COMMAND (cli_exec, static) = {
2853 .short_help = "Execute commands from file",
2854 .function = unix_cli_exec,
2859 /** CLI command to show various unix error statistics. */
2860 static clib_error_t *
2861 unix_show_errors (vlib_main_t * vm,
2862 unformat_input_t * input, vlib_cli_command_t * cmd)
2864 unix_main_t *um = &unix_main;
2865 clib_error_t *error = 0;
2866 int i, n_errors_to_show;
2867 unix_error_history_t *unix_errors = 0;
2869 n_errors_to_show = 1 << 30;
2871 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2873 if (!unformat (input, "%d", &n_errors_to_show))
2876 clib_error_return (0,
2877 "expecting integer number of errors to show, got `%U'",
2878 format_unformat_error, input);
2884 clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
2887 um->error_history_index >
2888 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
2890 while (n_errors_to_show > 0)
2892 unix_error_history_t *eh = um->error_history + i;
2897 vec_add1 (unix_errors, eh[0]);
2898 n_errors_to_show -= 1;
2900 i = ARRAY_LEN (um->error_history) - 1;
2905 if (vec_len (unix_errors) == 0)
2906 vlib_cli_output (vm, "no Unix errors so far");
2909 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
2910 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
2912 unix_error_history_t *eh = vec_elt_at_index (unix_errors, i);
2913 vlib_cli_output (vm, "%U: %U",
2914 format_time_interval, "h:m:s:u", eh->time,
2915 format_clib_error, eh->error);
2917 vlib_cli_output (vm, "%U: time now",
2918 format_time_interval, "h:m:s:u", vlib_time_now (vm));
2922 vec_free (unix_errors);
2927 VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
2928 .path = "show unix-errors",
2929 .short_help = "Show Unix system call error history",
2930 .function = unix_show_errors,
2934 /** CLI command to show session command history. */
2935 static clib_error_t *
2936 unix_cli_show_history (vlib_main_t * vm,
2937 unformat_input_t * input, vlib_cli_command_t * cmd)
2939 unix_cli_main_t *cm = &unix_cli_main;
2940 unix_cli_file_t *cf;
2943 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2945 if (cf->has_history && cf->history_limit)
2947 i = 1 + cf->command_number - vec_len (cf->command_history);
2948 for (j = 0; j < vec_len (cf->command_history); j++)
2949 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
2953 vlib_cli_output (vm, "History not enabled.\n");
2960 * Displays the command history for the current session, if any.
2963 VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
2965 .short_help = "Show current session command history",
2966 .function = unix_cli_show_history,
2970 /** CLI command to show terminal status. */
2971 static clib_error_t *
2972 unix_cli_show_terminal (vlib_main_t * vm,
2973 unformat_input_t * input, vlib_cli_command_t * cmd)
2975 unix_main_t *um = &unix_main;
2976 unix_cli_main_t *cm = &unix_cli_main;
2977 unix_cli_file_t *cf;
2980 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2981 n = vlib_get_node (vm, cf->process_node_index);
2983 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
2984 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
2985 "line-by-line" : "char-by-char");
2986 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
2987 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
2988 vlib_cli_output (vm, "ANSI capable: %s\n",
2989 cf->ansi_capable ? "yes" : "no");
2990 vlib_cli_output (vm, "History enabled: %s%s\n",
2991 cf->has_history ? "yes" : "no", !cf->has_history
2992 || cf->history_limit ? "" :
2993 " (disabled by history limit)");
2994 if (cf->has_history)
2995 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
2996 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
2997 cf->no_pager ? "no" : "yes",
2999 || cf->height ? "" : " (disabled by terminal height)",
3001 || um->cli_pager_buffer_limit ? "" :
3002 " (disabled by buffer limit)");
3004 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
3005 vlib_cli_output (vm, "CRLF mode: %s\n",
3006 cf->crlf_mode ? "CR+LF" : "LF");
3012 * Displays various information about the state of the current terminal
3016 * @cliexstart{show terminal}
3017 * Terminal name: unix-cli-stdin
3018 * Terminal mode: char-by-char
3019 * Terminal width: 123
3020 * Terminal height: 48
3022 * History enabled: yes
3024 * Pager enabled: yes
3025 * Pager limit: 100000
3030 VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
3031 .path = "show terminal",
3032 .short_help = "Show current session terminal settings",
3033 .function = unix_cli_show_terminal,
3037 /** CLI command to set terminal pager settings. */
3038 static clib_error_t *
3039 unix_cli_set_terminal_pager (vlib_main_t * vm,
3040 unformat_input_t * input,
3041 vlib_cli_command_t * cmd)
3043 unix_main_t *um = &unix_main;
3044 unix_cli_main_t *cm = &unix_cli_main;
3045 unix_cli_file_t *cf;
3046 unformat_input_t _line_input, *line_input = &_line_input;
3047 clib_error_t *error = 0;
3049 if (!unformat_user (input, unformat_line_input, line_input))
3052 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3054 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3056 if (unformat (line_input, "on"))
3058 else if (unformat (line_input, "off"))
3060 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
3061 vlib_cli_output (vm,
3062 "Pager limit set to %u lines; note, this is global.\n",
3063 um->cli_pager_buffer_limit);
3066 error = clib_error_return (0, "unknown parameter: `%U`",
3067 format_unformat_error, line_input);
3073 unformat_free (line_input);
3079 * Enables or disables the terminal pager for this session. Generally
3080 * this defaults to enabled.
3082 * Additionally allows the pager buffer size to be set; though note that
3083 * this value is set globally and not per session.
3086 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
3087 .path = "set terminal pager",
3088 .short_help = "set terminal pager [on|off] [limit <lines>]",
3089 .function = unix_cli_set_terminal_pager,
3093 /** CLI command to set terminal history settings. */
3094 static clib_error_t *
3095 unix_cli_set_terminal_history (vlib_main_t * vm,
3096 unformat_input_t * input,
3097 vlib_cli_command_t * cmd)
3099 unix_cli_main_t *cm = &unix_cli_main;
3100 unix_cli_file_t *cf;
3101 unformat_input_t _line_input, *line_input = &_line_input;
3103 clib_error_t *error = 0;
3105 if (!unformat_user (input, unformat_line_input, line_input))
3108 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3110 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3112 if (unformat (line_input, "on"))
3113 cf->has_history = 1;
3114 else if (unformat (line_input, "off"))
3115 cf->has_history = 0;
3116 else if (unformat (line_input, "limit %u", &cf->history_limit))
3120 error = clib_error_return (0, "unknown parameter: `%U`",
3121 format_unformat_error, line_input);
3125 /* If we reduced history size, or turned it off, purge the history */
3126 limit = cf->has_history ? cf->history_limit : 0;
3128 while (cf->command_history && vec_len (cf->command_history) >= limit)
3130 vec_free (cf->command_history[0]);
3131 vec_delete (cf->command_history, 1, 0);
3136 unformat_free (line_input);
3142 * Enables or disables the command history function of the current
3143 * terminal. Generally this defaults to enabled.
3145 * This command also allows the maximum size of the history buffer for
3146 * this session to be altered.
3149 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
3150 .path = "set terminal history",
3151 .short_help = "set terminal history [on|off] [limit <lines>]",
3152 .function = unix_cli_set_terminal_history,
3156 /** CLI command to set terminal ANSI settings. */
3157 static clib_error_t *
3158 unix_cli_set_terminal_ansi (vlib_main_t * vm,
3159 unformat_input_t * input,
3160 vlib_cli_command_t * cmd)
3162 unix_cli_main_t *cm = &unix_cli_main;
3163 unix_cli_file_t *cf;
3165 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3167 if (unformat (input, "on"))
3168 cf->ansi_capable = 1;
3169 else if (unformat (input, "off"))
3170 cf->ansi_capable = 0;
3172 return clib_error_return (0, "unknown parameter: `%U`",
3173 format_unformat_error, input);
3179 * Enables or disables the use of ANSI control sequences by this terminal.
3180 * The default will vary based on terminal detection at the start of the
3183 * ANSI control sequences are used in a small number of places to provide,
3184 * for example, color text output and to control the cursor in the pager.
3187 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
3188 .path = "set terminal ansi",
3189 .short_help = "set terminal ansi [on|off]",
3190 .function = unix_cli_set_terminal_ansi,
3194 static clib_error_t *
3195 unix_cli_init (vlib_main_t * vm)
3200 VLIB_INIT_FUNCTION (unix_cli_init);
3203 * fd.io coding-style-patch-verification: ON
3206 * eval: (c-set-style "gnu")