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 /** Unix standard in */
95 #define UNIX_CLI_STDIN_FD 0
98 /** A CLI banner line. */
101 u8 *line; /**< The line to print. */
102 u32 length; /**< The length of the line without terminating NUL. */
105 #define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
106 /** Plain welcome banner. */
107 static unix_cli_banner_t unix_cli_banner[] = {
108 _(" _______ _ _ _____ ___ \n"),
109 _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
110 _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
111 _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
115 /** ANSI color welcome banner. */
116 static unix_cli_banner_t unix_cli_banner_color[] = {
117 _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
118 _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
119 _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
120 _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"),
126 /** Pager line index */
129 /** Index into pager_vector */
132 /** Offset of the string in the line */
135 /** Length of the string in the line */
137 } unix_cli_pager_index_t;
140 /** Unix CLI session. */
143 /** The file index held by unix.c */
146 /** Vector of output pending write to file descriptor. */
149 /** Vector of input saved by Unix input node to be processed by
153 /** This session has command history. */
155 /** Array of vectors of commands in the history. */
156 u8 **command_history;
157 /** The command currently pointed at by the history cursor. */
159 /** How far from the end of the history array the user has browsed. */
162 /** Maximum number of history entries this session will store. */
165 /** Current command line counter */
168 /** The string being searched for in the history. */
170 /** If non-zero then the CLI is searching in the history array.
171 * - @c -1 means search backwards.
172 * - @c 1 means search forwards.
176 /** Position of the insert cursor on the current input line */
179 /** Line mode or char mode */
182 /** Set if the CRLF mode wants CR + LF */
185 /** Can we do ANSI output? */
188 /** Has the session started? */
191 /** Disable the pager? */
197 /** Index of line fragments in the pager buffer */
198 unix_cli_pager_index_t *pager_index;
200 /** Line number of top of page */
203 /** Terminal width */
206 /** Terminal height */
209 /** Process node identifier */
210 u32 process_node_index;
213 /** Resets the pager buffer and other data.
214 * @param f The CLI session whose pager needs to be reset.
217 unix_cli_pager_reset (unix_cli_file_t * f)
223 vec_free (f->pager_index);
226 vec_foreach (p, f->pager_vector)
230 vec_free (f->pager_vector);
234 /** Release storage used by a CLI session.
235 * @param f The CLI session whose storage needs to be released.
238 unix_cli_file_free (unix_cli_file_t * f)
240 vec_free (f->output_vector);
241 vec_free (f->input_vector);
242 unix_cli_pager_reset (f);
248 UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */
249 UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */
250 UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */
251 UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */
252 UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */
253 UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */
254 UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */
255 UNIX_CLI_PARSE_ACTION_LEFT, /**< Left arrow */
256 UNIX_CLI_PARSE_ACTION_RIGHT, /**< Right arrow */
257 UNIX_CLI_PARSE_ACTION_HOME, /**< Home key (jump to start of line) */
258 UNIX_CLI_PARSE_ACTION_END, /**< End key (jump to end of line) */
259 UNIX_CLI_PARSE_ACTION_WORDLEFT, /**< Jump cursor to start of left word */
260 UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */
261 UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */
262 UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */
263 UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */
264 UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */
265 UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */
266 UNIX_CLI_PARSE_ACTION_YANK, /**< Undo last erase action */
267 UNIX_CLI_PARSE_ACTION_TELNETIAC, /**< Telnet control code */
269 UNIX_CLI_PARSE_ACTION_PAGER_CRLF, /**< Enter pressed (CR, CRLF, LF, etc) */
270 UNIX_CLI_PARSE_ACTION_PAGER_QUIT, /**< Exit the pager session */
271 UNIX_CLI_PARSE_ACTION_PAGER_NEXT, /**< Scroll to next page */
272 UNIX_CLI_PARSE_ACTION_PAGER_DN, /**< Scroll to next line */
273 UNIX_CLI_PARSE_ACTION_PAGER_UP, /**< Scroll to previous line */
274 UNIX_CLI_PARSE_ACTION_PAGER_TOP, /**< Scroll to first line */
275 UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM, /**< Scroll to last line */
276 UNIX_CLI_PARSE_ACTION_PAGER_PGDN, /**< Scroll to next page */
277 UNIX_CLI_PARSE_ACTION_PAGER_PGUP, /**< Scroll to previous page */
278 UNIX_CLI_PARSE_ACTION_PAGER_REDRAW, /**< Clear and redraw the page on the terminal */
279 UNIX_CLI_PARSE_ACTION_PAGER_SEARCH, /**< Search the pager buffer */
281 UNIX_CLI_PARSE_ACTION_PARTIALMATCH, /**< Action parser found a partial match */
282 UNIX_CLI_PARSE_ACTION_NOMATCH /**< Action parser did not find any match */
283 } unix_cli_parse_action_t;
285 /** @brief Mapping of input buffer strings to action values.
286 * @note This won't work as a hash since we need to be able to do
287 * partial matches on the string.
291 u8 *input; /**< Input string to match. */
292 u32 len; /**< Length of input without final NUL. */
293 unix_cli_parse_action_t action; /**< Action to take when matched. */
294 } unix_cli_parse_actions_t;
296 /** @brief Given a capital ASCII letter character return a @c NUL terminated
297 * string with the control code for that letter.
299 * @param c An ASCII character.
300 * @return A @c NUL terminated string of type @c u8[].
303 * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[].
305 #define CTL(c) (u8[]){ (c) - '@', 0 }
307 #define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
309 * Patterns to match on a CLI input stream.
312 static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
314 _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */
315 _("\n", UNIX_CLI_PARSE_ACTION_CRLF),
316 _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */
317 _("\r", UNIX_CLI_PARSE_ACTION_CRLF),
319 /* Unix shell control codes */
320 _(CTL ('B'), UNIX_CLI_PARSE_ACTION_LEFT),
321 _(CTL ('F'), UNIX_CLI_PARSE_ACTION_RIGHT),
322 _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP),
323 _(CTL ('N'), UNIX_CLI_PARSE_ACTION_DOWN),
324 _(CTL ('A'), UNIX_CLI_PARSE_ACTION_HOME),
325 _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END),
326 _(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT),
327 _(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT),
328 _(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT),
329 _(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK),
330 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR),
331 _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */
332 _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */
333 _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */
334 _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */
335 _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */
337 /* VT100 Normal mode - Broadest support */
338 _(CSI "A", UNIX_CLI_PARSE_ACTION_UP),
339 _(CSI "B", UNIX_CLI_PARSE_ACTION_DOWN),
340 _(CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT),
341 _(CSI "D", UNIX_CLI_PARSE_ACTION_LEFT),
342 _(CSI "H", UNIX_CLI_PARSE_ACTION_HOME),
343 _(CSI "F", UNIX_CLI_PARSE_ACTION_END),
344 _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */
345 _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */
346 _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */
348 /* VT100 Application mode - Some Gnome Terminal functions use these */
349 _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP),
350 _(ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN),
351 _(ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT),
352 _(ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT),
353 _(ESC "OH", UNIX_CLI_PARSE_ACTION_HOME),
354 _(ESC "OF", UNIX_CLI_PARSE_ACTION_END),
356 /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
357 _(CSI "1~", UNIX_CLI_PARSE_ACTION_HOME),
358 _(CSI "4~", UNIX_CLI_PARSE_ACTION_END),
360 /* Emacs-ish history search */
361 _(CTL ('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH),
362 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH),
364 /* Other protocol things */
365 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
366 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
367 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
371 * Patterns to match when a CLI session is in the pager.
374 static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
376 _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */
377 _("\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
378 _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */
379 _("\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
382 _(" ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT),
383 _("q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT),
384 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
385 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
386 _("/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH),
389 _(CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP),
390 _(CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN),
391 _(CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
392 _(CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
394 /* VT100 Application mode */
395 _(ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP),
396 _(ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN),
397 _(ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
398 _(ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
400 /* ANSI X3.41-1974 */
401 _(CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
402 _(CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
403 _(CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP),
404 _(CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN),
406 /* Other protocol things */
407 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
408 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
409 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
414 /** CLI session events. */
417 UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */
418 UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */
419 } unix_cli_process_event_type_t;
421 /** CLI global state. */
424 /** Prompt string for CLI. */
427 /** Vec pool of CLI sessions. */
428 unix_cli_file_t *cli_file_pool;
430 /** Vec pool of unused session indices. */
431 u32 *unused_cli_process_node_indices;
433 /** The session index of the stdin cli */
434 u32 stdin_cli_file_index;
436 /** File pool index of current input. */
437 u32 current_input_file_index;
440 /** CLI global state */
441 static unix_cli_main_t unix_cli_main;
444 * @brief Search for a byte sequence in the action list.
446 * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with
447 * the bytes in @a input of maximum length @a ilen bytes.
448 * When a match is made @a *matched indicates how many bytes were matched.
449 * Returns a value from the enum @ref unix_cli_parse_action_t to indicate
450 * whether no match was found, a partial match was found or a complete
451 * match was found and what action, if any, should be taken.
453 * @param[in] a Actions list to search within.
454 * @param[in] input String fragment to search for.
455 * @param[in] ilen Length of the string in 'input'.
456 * @param[out] matched Pointer to an integer that will contain the number
457 * of bytes matched when a complete match is found.
459 * @return Action from @ref unix_cli_parse_action_t that the string fragment
461 * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the
462 * whole input string matches the start of at least one action.
463 * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
466 static unix_cli_parse_action_t
467 unix_cli_match_action (unix_cli_parse_actions_t * a,
468 u8 * input, u32 ilen, i32 * matched)
476 /* see if the start of the input buffer exactly matches the current
478 if (memcmp (input, a->input, a->len) == 0)
486 /* if the first ilen characters match, flag this as a partial -
487 * meaning keep collecting bytes in case of a future match */
488 if (memcmp (input, a->input, ilen) == 0)
492 /* check next action */
497 UNIX_CLI_PARSE_ACTION_PARTIALMATCH : UNIX_CLI_PARSE_ACTION_NOMATCH;
501 /** Add bytes to the output vector and then flagg the I/O system that bytes
502 * are available to be sent.
505 unix_cli_add_pending_output (clib_file_t * uf,
506 unix_cli_file_t * cf,
507 u8 * buffer, uword buffer_bytes)
509 clib_file_main_t *fm = &file_main;
511 vec_add (cf->output_vector, buffer, buffer_bytes);
512 if (vec_len (cf->output_vector) > 0)
514 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
515 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
517 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
521 /** Delete all bytes from the output vector and flag the I/O system
522 * that no more bytes are available to be sent.
525 unix_cli_del_pending_output (clib_file_t * uf,
526 unix_cli_file_t * cf, uword n_bytes)
528 clib_file_main_t *fm = &file_main;
530 vec_delete (cf->output_vector, n_bytes, 0);
531 if (vec_len (cf->output_vector) <= 0)
533 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
534 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
536 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
540 /** @brief A bit like strchr with a buffer length limit.
541 * Search a buffer for the first instance of a character up to the limit of
542 * the buffer length. If found then return the position of that character.
544 * The key departure from strchr is that if the character is not found then
545 * return the buffer length.
547 * @param chr The byte value to search for.
548 * @param str The buffer in which to search for the value.
549 * @param len The depth into the buffer to search.
551 * @return The index of the first occurence of \c chr. If \c chr is not
552 * found then \c len instead.
555 unix_vlib_findchr (u8 chr, u8 * str, word len)
558 for (i = 0; i < len; i++, str++)
566 /** @brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
567 * Attempts to write given buffer to the file descriptor of the given
568 * Unix CLI session. If that session already has data in the output buffer
569 * or if the write attempt tells us to try again later then the given buffer
570 * is appended to the pending output buffer instead.
572 * This is typically called only from \c unix_vlib_cli_output_cooked since
573 * that is where CRLF handling occurs or from places where we explicitly do
574 * not want cooked handling.
576 * @param cf Unix CLI session of the desired stream to write to.
577 * @param uf The Unix file structure of the desired stream to write to.
578 * @param buffer Pointer to the buffer that needs to be written.
579 * @param buffer_bytes The number of bytes from \c buffer to write.
582 unix_vlib_cli_output_raw (unix_cli_file_t * cf,
583 clib_file_t * uf, u8 * buffer, uword buffer_bytes)
587 if (vec_len (cf->output_vector) == 0)
588 n = write (uf->file_descriptor, buffer, buffer_bytes);
590 if (n < 0 && errno != EAGAIN)
592 clib_unix_warning ("write");
594 else if ((word) n < (word) buffer_bytes)
596 /* We got EAGAIN or we already have stuff in the buffer;
597 * queue up whatever didn't get sent for later. */
600 unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
604 /** @brief Process a buffer for CRLF handling before outputting it to the CLI.
606 * @param cf Unix CLI session of the desired stream to write to.
607 * @param uf The Unix file structure of the desired stream to write to.
608 * @param buffer Pointer to the buffer that needs to be written.
609 * @param buffer_bytes The number of bytes from \c buffer to write.
612 unix_vlib_cli_output_cooked (unix_cli_file_t * cf,
614 u8 * buffer, uword buffer_bytes)
616 word end = 0, start = 0;
618 while (end < buffer_bytes)
622 /* iterate the line on \n's so we can insert a \r before it */
623 end = unix_vlib_findchr ('\n',
625 buffer_bytes - start) + start;
629 /* otherwise just send the whole buffer */
633 unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start);
637 if (end < buffer_bytes)
639 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2);
640 end++; /* skip the \n that we already sent */
647 /** @brief Output the CLI prompt */
649 unix_cli_cli_prompt (unix_cli_file_t * cf, clib_file_t * uf)
651 unix_cli_main_t *cm = &unix_cli_main;
653 unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt, vec_len (cm->cli_prompt));
656 /** @brief Output a pager prompt and show number of buffered lines */
658 unix_cli_pager_prompt (unix_cli_file_t * cf, clib_file_t * uf)
663 h = cf->pager_start + (cf->height - 1);
664 if (h > vec_len (cf->pager_index))
665 h = vec_len (cf->pager_index);
667 prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s",
668 cf->ansi_capable ? ANSI_BOLD : "",
671 vec_len (cf->pager_index),
672 cf->ansi_capable ? ANSI_RESET : "");
674 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
679 /** @brief Output a pager "skipping" message */
681 unix_cli_pager_message (unix_cli_file_t * cf, clib_file_t * uf,
682 char *message, char *postfix)
686 prompt = format (0, "\r%s-- %s --%s%s",
687 cf->ansi_capable ? ANSI_BOLD : "",
688 message, cf->ansi_capable ? ANSI_RESET : "", postfix);
690 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
695 /** @brief Erase the printed pager prompt */
697 unix_cli_pager_prompt_erase (unix_cli_file_t * cf, clib_file_t * uf)
699 if (cf->ansi_capable)
701 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
702 unix_vlib_cli_output_cooked (cf, uf,
703 (u8 *) ANSI_CLEARLINE,
704 sizeof (ANSI_CLEARLINE) - 1);
710 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
711 for (i = 0; i < cf->width - 1; i++)
712 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
713 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
717 /** @brief Uses an ANSI escape sequence to move the cursor */
719 unix_cli_ansi_cursor (unix_cli_file_t * cf, clib_file_t * uf, u16 x, u16 y)
723 str = format (0, "%s%d;%dH", CSI, y, x);
725 unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str));
730 /** Redraw the currently displayed page of text.
731 * @param cf CLI session to redraw the pager buffer of.
732 * @param uf Unix file of the CLI session.
735 unix_cli_pager_redraw (unix_cli_file_t * cf, clib_file_t * uf)
737 unix_cli_pager_index_t *pi = NULL;
741 /* No active pager? Do nothing. */
742 if (!vec_len (cf->pager_index))
745 if (cf->ansi_capable)
747 /* If we have ANSI, send the clear screen sequence */
748 unix_vlib_cli_output_cooked (cf, uf,
750 sizeof (ANSI_CLEAR) - 1);
754 /* Otherwise make sure we're on a blank line */
755 unix_cli_pager_prompt_erase (cf, uf);
758 /* (Re-)send the current page of content */
759 for (i = 0; i < cf->height - 1 &&
760 i + cf->pager_start < vec_len (cf->pager_index); i++)
762 pi = &cf->pager_index[cf->pager_start + i];
763 line = cf->pager_vector[pi->line] + pi->offset;
765 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
767 /* if the last line didn't end in newline, add a newline */
768 if (pi && line[pi->length - 1] != '\n')
769 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
771 unix_cli_pager_prompt (cf, uf);
774 /** @brief Process and add a line to the pager index.
775 * In normal operation this function will take the given character string
776 * found in @c line and with length @c len_or_index and iterates the over the
777 * contents, adding each line of text discovered within it to the
778 * pager index. Lines are identified by newlines ("<code>\\n</code>") and by
779 * strings longer than the width of the terminal.
781 * If instead @c line is @c NULL then @c len_or_index is taken to mean the
782 * index of an existing line in the pager buffer; this simply means that the
783 * input line does not need to be cloned since we alreayd have it. This is
784 * typical if we are reindexing the pager buffer.
786 * @param cf The CLI session whose pager we are adding to.
787 * @param line The string of text to be indexed into the pager buffer.
788 * If @c line is @c NULL then the mode of operation
789 * changes slightly; see the description above.
790 * @param len_or_index If @c line is a pointer to a string then this parameter
791 * indicates the length of that string; Otherwise this
792 * value provides the index in the pager buffer of an
793 * existing string to be indexed.
796 unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index)
800 word line_index, len;
801 u32 width = cf->width;
802 unix_cli_pager_index_t *pi;
806 /* Use a line already in the pager buffer */
807 line_index = len_or_index;
808 p = cf->pager_vector[line_index];
814 /* Add a copy of the raw string to the pager buffer */
815 p = vec_new (u8, len);
816 clib_memcpy (p, line, len);
818 /* store in pager buffer */
819 line_index = vec_len (cf->pager_vector);
820 vec_add1 (cf->pager_vector, p);
826 /* Find the next line, or run to terminal width, or run to EOL */
828 j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width);
830 if (j < l && p[j] == '\n') /* incl \n */
833 /* Add the line to the index */
834 k = vec_len (cf->pager_index);
835 vec_validate (cf->pager_index, k);
836 pi = &cf->pager_index[k];
838 pi->line = line_index;
847 /** @brief Reindex entire pager buffer.
848 * Resets the current pager index and then re-adds the lines in the pager
849 * buffer to the index.
851 * Additionally this function attempts to retain the current page start
852 * line offset by searching for the same top-of-screen line in the new index.
854 * @param cf The CLI session whose pager buffer should be reindexed.
857 unix_cli_pager_reindex (unix_cli_file_t * cf)
859 word i, old_line, old_offset;
860 unix_cli_pager_index_t *pi;
862 /* If there is nothing in the pager buffer then make sure the index
863 * is empty and move on.
865 if (cf->pager_vector == 0)
867 vec_reset_length (cf->pager_index);
871 /* Retain a pointer to the current page start line so we can
874 pi = &cf->pager_index[cf->pager_start];
876 old_offset = pi->offset;
878 /* Re-add the buffered lines to the index */
879 vec_reset_length (cf->pager_index);
880 vec_foreach_index (i, cf->pager_vector)
882 unix_cli_pager_add_line (cf, NULL, i);
885 /* Attempt to re-locate the previously stored page start line */
886 vec_foreach_index (i, cf->pager_index)
888 pi = &cf->pager_index[i];
890 if (pi->line == old_line &&
891 (pi->offset <= old_offset || pi->offset + pi->length > old_offset))
899 /* In case the start line was not found (rare), ensure the pager start
900 * index is within bounds
902 if (cf->pager_start >= vec_len (cf->pager_index))
904 if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1))
907 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
911 /** VLIB CLI output function.
913 * If the terminal has a pager configured then this function takes care
914 * of collating output into the pager buffer; ensuring only the first page
915 * is displayed and any lines in excess of the first page are buffered.
917 * If the maximum number of index lines in the buffer is exceeded then the
918 * pager is cancelled and the contents of the current buffer are sent to the
921 * If there is no pager configured then the output is sent directly to the
924 * @param cli_file_index Index of the CLI session where this output is
926 * @param buffer String of printabe bytes to be output.
927 * @param buffer_bytes The number of bytes in @c buffer to be output.
930 unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes)
932 unix_main_t *um = &unix_main;
933 clib_file_main_t *fm = &file_main;
934 unix_cli_main_t *cm = &unix_cli_main;
938 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
939 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
941 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
943 unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes);
947 word row = vec_len (cf->pager_index);
949 unix_cli_pager_index_t *pi;
951 /* Index and add the output lines to the pager buffer. */
952 unix_cli_pager_add_line (cf, buffer, buffer_bytes);
954 /* Now iterate what was added to display the lines.
955 * If we reach the bottom of the page, display a prompt.
957 while (row < vec_len (cf->pager_index))
959 if (row < cf->height - 1)
961 /* output this line */
962 pi = &cf->pager_index[row];
963 line = cf->pager_vector[pi->line] + pi->offset;
964 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
966 /* if the last line didn't end in newline, and we're at the
967 * bottom of the page, add a newline */
968 if (line[pi->length - 1] != '\n' && row == cf->height - 2)
969 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
973 /* Display the pager prompt every 10 lines */
975 unix_cli_pager_prompt (cf, uf);
980 /* Check if we went over the pager buffer limit */
981 if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit)
983 /* Stop using the pager for the remainder of this CLI command */
986 /* If we likely printed the prompt, erase it */
987 if (vec_len (cf->pager_index) > cf->height - 1)
988 unix_cli_pager_prompt_erase (cf, uf);
990 /* Dump out the contents of the buffer */
991 for (row = cf->pager_start + (cf->height - 1);
992 row < vec_len (cf->pager_index); row++)
994 pi = &cf->pager_index[row];
995 line = cf->pager_vector[pi->line] + pi->offset;
996 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
999 unix_cli_pager_reset (cf);
1004 /** Identify whether a terminal type is ANSI capable.
1006 * Compares the string given in @c term with a list of terminal types known
1007 * to support ANSI escape sequences.
1009 * This list contains, for example, @c xterm, @c screen and @c ansi.
1011 * @param term A string with a terminal type in it.
1012 * @param len The length of the string in @c term.
1014 * @return @c 1 if the terminal type is recognized as supporting ANSI
1015 * terminal sequences; @c 0 otherwise.
1018 unix_cli_terminal_type (u8 * term, uword len)
1020 /* This may later be better done as a hash of some sort. */
1022 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1027 _("xterm-256color"); /* iTerm on Mac */
1029 _("screen-256color"); /* Screen and tmux */
1030 _("ansi"); /* Microsoft Telnet */
1036 /** @brief Emit initial welcome banner and prompt on a connection. */
1038 unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf)
1040 unix_main_t *um = &unix_main;
1041 clib_file_main_t *fm = &file_main;
1042 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
1043 unix_cli_banner_t *banner;
1047 * Put the first bytes directly into the buffer so that further output is
1048 * queued until everything is ready. (oterwise initial prompt can appear
1049 * mid way through VPP initialization)
1051 unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
1053 if (!um->cli_no_banner)
1055 if (cf->ansi_capable)
1057 banner = unix_cli_banner_color;
1058 len = ARRAY_LEN (unix_cli_banner_color);
1062 banner = unix_cli_banner;
1063 len = ARRAY_LEN (unix_cli_banner);
1066 for (i = 0; i < len; i++)
1068 unix_vlib_cli_output_cooked (cf, uf,
1069 banner[i].line, banner[i].length);
1074 unix_cli_cli_prompt (cf, uf);
1079 /** @brief A failsafe triggered on a timer to ensure we send the prompt
1080 * to telnet sessions that fail to negotiate the terminal type. */
1082 unix_cli_file_welcome_timer (any arg, f64 delay)
1084 unix_cli_main_t *cm = &unix_cli_main;
1085 unix_cli_file_t *cf;
1088 /* Check the connection didn't close already */
1089 if (pool_is_free_index (cm->cli_file_pool, (uword) arg))
1092 cf = pool_elt_at_index (cm->cli_file_pool, (uword) arg);
1095 unix_cli_file_welcome (cm, cf);
1098 /** @brief A mostly no-op Telnet state machine.
1099 * Process Telnet command bytes in a way that ensures we're mostly
1100 * transparent to the Telnet protocol. That is, it's mostly a no-op.
1102 * @return -1 if we need more bytes, otherwise a positive integer number of
1103 * bytes to consume from the input_vector, not including the initial
1107 unix_cli_process_telnet (unix_main_t * um,
1108 unix_cli_file_t * cf,
1109 clib_file_t * uf, u8 * input_vector, uword len)
1111 /* Input_vector starts at IAC byte.
1112 * See if we have a complete message; if not, return -1 so we wait for more.
1113 * if we have a complete message, consume those bytes from the vector.
1118 return -1; /* want more bytes */
1120 switch (input_vector[1])
1123 /* two IAC's in a row means to pass through 0xff.
1124 * since that makes no sense here, just consume it.
1133 /* Expect 3 bytes */
1134 if (vec_len (input_vector) < 3)
1135 return -1; /* want more bytes */
1142 /* Sub option - search ahead for IAC SE to end it */
1144 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
1146 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
1148 /* We have a complete message; see if we care about it */
1149 switch (input_vector[2])
1152 if (input_vector[3] != 0)
1154 /* See if the terminal type is ANSI capable */
1156 unix_cli_terminal_type (input_vector + 4, i - 5);
1157 /* If session not started, we can release the pause */
1159 /* Send the welcome banner and initial prompt */
1160 unix_cli_file_welcome (&unix_cli_main, cf);
1165 if (i != 8) /* check message is correct size */
1168 clib_net_to_host_u16 (*((u16 *) (input_vector + 3)));
1170 clib_net_to_host_u16 (*((u16 *) (input_vector + 5)));
1171 /* reindex pager buffer */
1172 unix_cli_pager_reindex (cf);
1174 unix_cli_pager_redraw (cf, uf);
1180 /* Consume it all */
1186 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
1187 consume = 1; /* hit max search depth, advance one byte */
1190 return -1; /* want more bytes */
1208 /* Simple one-byte messages */
1213 /* Are You There - trigger a visible response */
1215 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
1219 /* Unknown command! Eat the IAC byte */
1226 /** @brief Process actionable input.
1227 * Based on the \c action process the input; this typically involves
1228 * searching the command history or editing the current command line.
1231 unix_cli_line_process_one (unix_cli_main_t * cm,
1233 unix_cli_file_t * cf,
1235 u8 input, unix_cli_parse_action_t action)
1239 u8 **possible_commands;
1244 case UNIX_CLI_PARSE_ACTION_NOACTION:
1247 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
1248 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
1249 if (!cf->has_history || !cf->history_limit)
1251 if (cf->search_mode == 0)
1253 /* Erase the current command (if any) */
1254 for (j = 0; j < (vec_len (cf->current_command)); j++)
1255 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1257 vec_reset_length (cf->search_key);
1258 vec_reset_length (cf->current_command);
1259 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1260 cf->search_mode = -1;
1262 cf->search_mode = 1;
1267 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1268 cf->search_mode = -1;
1270 cf->search_mode = 1;
1272 cf->excursion += cf->search_mode;
1277 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
1278 /* Erase the command from the cursor to the start */
1280 /* Shimmy forwards to the new end of line position */
1281 delta = vec_len (cf->current_command) - cf->cursor;
1282 for (j = cf->cursor; j > delta; j--)
1283 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1284 /* Zap from here to the end of what is currently displayed */
1285 for (; j < (vec_len (cf->current_command)); j++)
1286 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1287 /* Get back to the start of the line */
1288 for (j = 0; j < (vec_len (cf->current_command)); j++)
1289 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1291 j = vec_len (cf->current_command) - cf->cursor;
1292 memmove (cf->current_command, cf->current_command + cf->cursor, j);
1293 _vec_len (cf->current_command) = j;
1295 /* Print the new contents */
1296 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j);
1297 /* Shimmy back to the start */
1298 for (j = 0; j < (vec_len (cf->current_command)); j++)
1299 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1302 cf->search_mode = 0;
1305 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
1306 /* Erase the command from the cursor to the end */
1308 /* Zap from cursor to end of what is currently displayed */
1309 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1310 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1311 /* Get back to where we were */
1312 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1313 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1315 /* Truncate the line at the cursor */
1316 _vec_len (cf->current_command) = cf->cursor;
1318 cf->search_mode = 0;
1321 case UNIX_CLI_PARSE_ACTION_LEFT:
1324 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1328 cf->search_mode = 0;
1331 case UNIX_CLI_PARSE_ACTION_RIGHT:
1332 if (cf->cursor < vec_len (cf->current_command))
1334 /* have to emit the character under the cursor */
1335 unix_vlib_cli_output_cooked (cf, uf,
1336 cf->current_command + cf->cursor, 1);
1340 cf->search_mode = 0;
1343 case UNIX_CLI_PARSE_ACTION_UP:
1344 case UNIX_CLI_PARSE_ACTION_DOWN:
1345 if (!cf->has_history || !cf->history_limit)
1347 cf->search_mode = 0;
1348 /* Erase the command */
1349 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1350 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1351 for (j = 0; j < (vec_len (cf->current_command)); j++)
1352 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1353 vec_reset_length (cf->current_command);
1354 if (vec_len (cf->command_history))
1356 if (action == UNIX_CLI_PARSE_ACTION_UP)
1361 cf->excursion += delta;
1363 if (cf->excursion == vec_len (cf->command_history))
1365 /* down-arrowed to last entry - want a blank line */
1366 _vec_len (cf->current_command) = 0;
1368 else if (cf->excursion < 0)
1370 /* up-arrowed over the start to the end, want a blank line */
1371 cf->excursion = vec_len (cf->command_history);
1372 _vec_len (cf->current_command) = 0;
1376 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1377 /* down-arrowed past end - wrap to start */
1380 /* Print the command at the current position */
1381 prev = cf->command_history[cf->excursion];
1382 vec_validate (cf->current_command, vec_len (prev) - 1);
1384 clib_memcpy (cf->current_command, prev, vec_len (prev));
1385 _vec_len (cf->current_command) = vec_len (prev);
1386 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1387 vec_len (cf->current_command));
1389 cf->cursor = vec_len (cf->current_command);
1395 case UNIX_CLI_PARSE_ACTION_HOME:
1396 if (vec_len (cf->current_command) && cf->cursor > 0)
1400 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1405 cf->search_mode = 0;
1408 case UNIX_CLI_PARSE_ACTION_END:
1409 if (vec_len (cf->current_command) &&
1410 cf->cursor < vec_len (cf->current_command))
1412 unix_vlib_cli_output_cooked (cf, uf,
1413 cf->current_command + cf->cursor,
1414 vec_len (cf->current_command) -
1416 cf->cursor = vec_len (cf->current_command);
1419 cf->search_mode = 0;
1422 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1423 if (vec_len (cf->current_command) && cf->cursor > 0)
1427 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1430 while (j && isspace (cf->current_command[j]))
1432 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1435 while (j && !isspace (cf->current_command[j]))
1437 if (isspace (cf->current_command[j - 1]))
1439 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1446 cf->search_mode = 0;
1449 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1450 if (vec_len (cf->current_command) &&
1451 cf->cursor < vec_len (cf->current_command))
1453 int e = vec_len (cf->current_command);
1455 while (j < e && !isspace (cf->current_command[j]))
1457 while (j < e && isspace (cf->current_command[j]))
1459 unix_vlib_cli_output_cooked (cf, uf,
1460 cf->current_command + cf->cursor,
1465 cf->search_mode = 0;
1469 case UNIX_CLI_PARSE_ACTION_ERASE:
1470 if (vec_len (cf->current_command))
1472 if (cf->cursor == vec_len (cf->current_command))
1474 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1475 _vec_len (cf->current_command)--;
1478 else if (cf->cursor > 0)
1480 /* shift everything at & to the right of the cursor left by 1 */
1481 j = vec_len (cf->current_command) - cf->cursor;
1482 memmove (cf->current_command + cf->cursor - 1,
1483 cf->current_command + cf->cursor, j);
1484 _vec_len (cf->current_command)--;
1486 /* redraw the rest of the line */
1487 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1488 unix_vlib_cli_output_cooked (cf, uf,
1489 cf->current_command + cf->cursor,
1491 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
1492 /* and shift the terminal cursor back where it should be */
1494 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1497 cf->search_mode = 0;
1499 vec_reset_length (cf->search_key);
1502 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1503 if (vec_len (cf->current_command))
1505 if (cf->cursor < vec_len (cf->current_command))
1507 /* shift everything to the right of the cursor left by 1 */
1508 j = vec_len (cf->current_command) - cf->cursor - 1;
1509 memmove (cf->current_command + cf->cursor,
1510 cf->current_command + cf->cursor + 1, j);
1511 _vec_len (cf->current_command)--;
1512 /* redraw the rest of the line */
1513 unix_vlib_cli_output_cooked (cf, uf,
1514 cf->current_command + cf->cursor,
1516 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
1517 /* and shift the terminal cursor back where it should be */
1520 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1522 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1526 else if (input == 'D' - '@')
1528 /* ^D with no command entered = quit */
1529 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1530 vlib_process_signal_event (um->vlib_main,
1531 vlib_current_process (um->vlib_main),
1532 UNIX_CLI_PROCESS_EVENT_QUIT,
1533 cf - cm->cli_file_pool);
1535 cf->search_mode = 0;
1537 vec_reset_length (cf->search_key);
1540 case UNIX_CLI_PARSE_ACTION_CLEAR:
1541 /* If we're in ANSI mode, clear the screen.
1542 * Then redraw the prompt and any existing command input, then put
1543 * the cursor back where it was in that line.
1545 if (cf->ansi_capable)
1546 unix_vlib_cli_output_cooked (cf, uf,
1548 sizeof (ANSI_CLEAR) - 1);
1550 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1552 unix_vlib_cli_output_raw (cf, uf,
1553 cm->cli_prompt, vec_len (cm->cli_prompt));
1554 unix_vlib_cli_output_raw (cf, uf,
1555 cf->current_command,
1556 vec_len (cf->current_command));
1557 for (j = cf->cursor; j < vec_len (cf->current_command); j++)
1558 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1562 case UNIX_CLI_PARSE_ACTION_TAB:
1563 if (cf->cursor < vec_len (cf->current_command))
1565 /* if we are in the middle of a line, complete only if
1566 * the cursor points to whitespace */
1567 if (isspace (cf->current_command[cf->cursor]))
1569 /* save and clear any input that is after the cursor */
1570 vec_resize (save, vec_len (cf->current_command) - cf->cursor);
1571 clib_memcpy (save, cf->current_command + cf->cursor,
1572 vec_len (cf->current_command) - cf->cursor);
1573 _vec_len (cf->current_command) = cf->cursor;
1577 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1582 vlib_cli_get_possible_completions (cf->current_command);
1583 if (vec_len (possible_commands) == 1)
1586 u8 *completed = possible_commands[0];
1588 /* find the last word of current_command */
1589 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1592 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1594 _vec_len (cf->current_command) = j;
1596 /* replace it with the newly expanded command */
1597 vec_append (cf->current_command, completed);
1599 /* echo to the terminal */
1600 unix_vlib_cli_output_raw (cf, uf, completed, vec_len (completed));
1602 /* add one trailing space if needed */
1603 if (vec_len (save) == 0)
1605 vec_add1 (cf->current_command, ' ');
1606 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1609 cf->cursor = vec_len (cf->current_command);
1612 else if (vec_len (possible_commands) >= 2)
1614 u8 **possible_command;
1615 uword max_command_len = 0, min_command_len = ~0;
1618 vec_foreach (possible_command, possible_commands)
1620 if (vec_len (*possible_command) > max_command_len)
1622 max_command_len = vec_len (*possible_command);
1624 if (vec_len (*possible_command) < min_command_len)
1626 min_command_len = vec_len (*possible_command);
1630 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1633 vec_foreach (possible_command, possible_commands)
1635 if (i + max_command_len >= cf->width)
1637 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1640 unix_vlib_cli_output_raw (cf, uf, *possible_command,
1641 vec_len (*possible_command));
1642 for (j = vec_len (*possible_command); j < max_command_len + 2;
1645 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1647 i += max_command_len + 2;
1650 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1652 /* rewrite prompt */
1653 unix_cli_cli_prompt (cf, uf);
1654 unix_vlib_cli_output_raw (cf, uf, cf->current_command,
1655 vec_len (cf->current_command));
1657 /* count length of last word */
1660 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1666 /* determine smallest common command */
1667 for (; i < min_command_len; i++)
1671 vec_foreach (possible_command, possible_commands)
1675 common = (*possible_command)[i];
1677 else if (common != (*possible_command)[i])
1685 vec_add1 (cf->current_command, common);
1687 unix_vlib_cli_output_raw (cf, uf, (u8 *) & common, 1);
1697 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1700 if (vec_len (save) > 0)
1702 /* restore remaining input if tab was hit in the middle of a line */
1703 unix_vlib_cli_output_raw (cf, uf, save, vec_len (save));
1704 for (j = 0; j < vec_len (save); j++)
1706 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1708 vec_append (cf->current_command, save);
1711 vec_free (possible_commands);
1714 case UNIX_CLI_PARSE_ACTION_YANK:
1719 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
1721 unix_cli_pager_prompt_erase (cf, uf);
1722 unix_cli_pager_reset (cf);
1723 unix_cli_cli_prompt (cf, uf);
1726 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
1727 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
1728 /* show next page of the buffer */
1729 if (cf->height + cf->pager_start < vec_len (cf->pager_index))
1732 unix_cli_pager_index_t *pi = NULL;
1734 int m = cf->pager_start + (cf->height - 1);
1735 unix_cli_pager_prompt_erase (cf, uf);
1737 j < vec_len (cf->pager_index) && cf->pager_start < m;
1738 j++, cf->pager_start++)
1740 pi = &cf->pager_index[j];
1741 line = cf->pager_vector[pi->line] + pi->offset;
1742 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1744 /* if the last line didn't end in newline, add a newline */
1745 if (pi && line[pi->length - 1] != '\n')
1746 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1747 unix_cli_pager_prompt (cf, uf);
1751 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
1752 /* no more in buffer, exit, but only if it was <space> */
1757 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
1758 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
1759 /* display the next line of the buffer */
1760 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
1763 unix_cli_pager_index_t *pi;
1765 unix_cli_pager_prompt_erase (cf, uf);
1766 pi = &cf->pager_index[cf->pager_start + (cf->height - 1)];
1767 line = cf->pager_vector[pi->line] + pi->offset;
1768 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1770 /* if the last line didn't end in newline, add a newline */
1771 if (line[pi->length - 1] != '\n')
1772 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1773 unix_cli_pager_prompt (cf, uf);
1777 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
1778 /* no more in buffer, exit, but only if it was <enter> */
1784 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
1785 /* scroll the page back one line */
1786 if (cf->pager_start > 0)
1789 unix_cli_pager_index_t *pi = NULL;
1792 if (cf->ansi_capable)
1794 pi = &cf->pager_index[cf->pager_start];
1795 line = cf->pager_vector[pi->line] + pi->offset;
1796 unix_cli_pager_prompt_erase (cf, uf);
1797 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN,
1798 sizeof (ANSI_SCROLLDN) - 1);
1799 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR,
1800 sizeof (ANSI_SAVECURSOR) - 1);
1801 unix_cli_ansi_cursor (cf, uf, 1, 1);
1802 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE,
1803 sizeof (ANSI_CLEARLINE) - 1);
1804 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1805 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR,
1806 sizeof (ANSI_RESTCURSOR) - 1);
1807 unix_cli_pager_prompt_erase (cf, uf);
1808 unix_cli_pager_prompt (cf, uf);
1812 int m = cf->pager_start + (cf->height - 1);
1813 unix_cli_pager_prompt_erase (cf, uf);
1814 for (j = cf->pager_start;
1815 j < vec_len (cf->pager_index) && j < m; j++)
1817 pi = &cf->pager_index[j];
1818 line = cf->pager_vector[pi->line] + pi->offset;
1819 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1821 /* if the last line didn't end in newline, add a newline */
1822 if (pi && line[pi->length - 1] != '\n')
1823 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1824 unix_cli_pager_prompt (cf, uf);
1829 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
1830 /* back to the first page of the buffer */
1831 if (cf->pager_start > 0)
1834 unix_cli_pager_index_t *pi = NULL;
1836 cf->pager_start = 0;
1837 int m = cf->pager_start + (cf->height - 1);
1838 unix_cli_pager_prompt_erase (cf, uf);
1839 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1842 pi = &cf->pager_index[j];
1843 line = cf->pager_vector[pi->line] + pi->offset;
1844 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1846 /* if the last line didn't end in newline, add a newline */
1847 if (pi && line[pi->length - 1] != '\n')
1848 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1849 unix_cli_pager_prompt (cf, uf);
1853 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
1854 /* skip to the last page of the buffer */
1855 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
1858 unix_cli_pager_index_t *pi = NULL;
1860 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
1861 unix_cli_pager_prompt_erase (cf, uf);
1862 unix_cli_pager_message (cf, uf, "skipping", "\n");
1863 for (j = cf->pager_start; j < vec_len (cf->pager_index); j++)
1865 pi = &cf->pager_index[j];
1866 line = cf->pager_vector[pi->line] + pi->offset;
1867 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1869 /* if the last line didn't end in newline, add a newline */
1870 if (pi && line[pi->length - 1] != '\n')
1871 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1872 unix_cli_pager_prompt (cf, uf);
1876 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
1877 /* wander back one page in the buffer */
1878 if (cf->pager_start > 0)
1881 unix_cli_pager_index_t *pi = NULL;
1884 if (cf->pager_start >= cf->height)
1885 cf->pager_start -= cf->height - 1;
1887 cf->pager_start = 0;
1888 m = cf->pager_start + cf->height - 1;
1889 unix_cli_pager_prompt_erase (cf, uf);
1890 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1893 pi = &cf->pager_index[j];
1894 line = cf->pager_vector[pi->line] + pi->offset;
1895 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1897 /* if the last line didn't end in newline, add a newline */
1898 if (pi && line[pi->length - 1] != '\n')
1899 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1900 unix_cli_pager_prompt (cf, uf);
1904 case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW:
1905 /* Redraw the current pager screen */
1906 unix_cli_pager_redraw (cf, uf);
1909 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
1910 /* search forwards in the buffer */
1914 case UNIX_CLI_PARSE_ACTION_CRLF:
1916 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1918 if (cf->has_history && cf->history_limit)
1920 if (cf->command_history
1921 && vec_len (cf->command_history) >= cf->history_limit)
1923 vec_free (cf->command_history[0]);
1924 vec_delete (cf->command_history, 1, 0);
1926 /* Don't add blank lines to the cmd history */
1927 if (vec_len (cf->current_command))
1929 /* Don't duplicate the previous command */
1930 j = vec_len (cf->command_history);
1932 (vec_len (cf->current_command) !=
1933 vec_len (cf->command_history[j - 1])
1934 || memcmp (cf->current_command, cf->command_history[j - 1],
1935 vec_len (cf->current_command)) != 0))
1937 /* copy the command to the history */
1939 vec_append (c, cf->current_command);
1940 vec_add1 (cf->command_history, c);
1941 cf->command_number++;
1944 cf->excursion = vec_len (cf->command_history);
1947 cf->search_mode = 0;
1948 vec_reset_length (cf->search_key);
1953 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
1954 case UNIX_CLI_PARSE_ACTION_NOMATCH:
1955 if (vec_len (cf->pager_index))
1959 else if (cf->has_history && cf->search_mode && isprint (input))
1961 int k, limit, offset;
1964 vec_add1 (cf->search_key, input);
1967 for (j = 0; j < vec_len (cf->command_history); j++)
1969 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1971 else if (cf->excursion < 0)
1972 cf->excursion = vec_len (cf->command_history) - 1;
1974 item = cf->command_history[cf->excursion];
1976 limit = (vec_len (cf->search_key) > vec_len (item)) ?
1977 vec_len (item) : vec_len (cf->search_key);
1979 for (offset = 0; offset <= vec_len (item) - limit; offset++)
1981 for (k = 0; k < limit; k++)
1983 if (item[k + offset] != cf->search_key[k])
1986 goto found_at_offset;
1994 for (j = 0; j < vec_len (cf->current_command); j++)
1995 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1997 vec_validate (cf->current_command, vec_len (item) - 1);
1998 clib_memcpy (cf->current_command, item, vec_len (item));
1999 _vec_len (cf->current_command) = vec_len (item);
2001 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
2002 vec_len (cf->current_command));
2003 cf->cursor = vec_len (cf->current_command);
2007 cf->excursion += cf->search_mode;
2010 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12);
2011 vec_reset_length (cf->search_key);
2012 vec_reset_length (cf->current_command);
2013 cf->search_mode = 0;
2017 else if (isprint (input)) /* skip any errant control codes */
2019 if (cf->cursor == vec_len (cf->current_command))
2022 vec_add1 (cf->current_command, input);
2025 /* Echo the character back to the client */
2026 unix_vlib_cli_output_raw (cf, uf, &input, 1);
2030 /* Insert at cursor: resize +1 byte, move everything over */
2031 j = vec_len (cf->current_command) - cf->cursor;
2032 vec_add1 (cf->current_command, (u8) 'A');
2033 memmove (cf->current_command + cf->cursor + 1,
2034 cf->current_command + cf->cursor, j);
2035 cf->current_command[cf->cursor] = input;
2036 /* Redraw the line */
2038 unix_vlib_cli_output_raw (cf, uf,
2039 cf->current_command + cf->cursor, j);
2040 /* Put terminal cursor back */
2042 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
2048 /* no-op - not printable or otherwise not actionable */
2055 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2061 /** @brief Process input bytes on a stream to provide line editing and
2062 * command history in the CLI. */
2064 unix_cli_line_edit (unix_cli_main_t * cm, unix_main_t * um,
2065 clib_file_main_t * fm, unix_cli_file_t * cf)
2067 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2070 for (i = 0; i < vec_len (cf->input_vector); i++)
2072 unix_cli_parse_action_t action;
2074 unix_cli_parse_actions_t *a;
2076 /* If we're in the pager mode, search the pager actions */
2078 vec_len (cf->pager_index) ? unix_cli_parse_pager :
2079 unix_cli_parse_strings;
2081 /* See if the input buffer is some sort of control code */
2082 action = unix_cli_match_action (a, &cf->input_vector[i],
2083 vec_len (cf->input_vector) - i,
2088 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2091 /* There was a partial match which means we need more bytes
2092 * than the input buffer currently has.
2093 * Since the bytes before here have been processed, shift
2094 * the remaining contents to the start of the input buffer.
2096 vec_delete (cf->input_vector, i, 0);
2098 return 1; /* wait for more */
2100 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2101 /* process telnet options */
2102 matched = unix_cli_process_telnet (um, cf, uf,
2103 cf->input_vector + i,
2104 vec_len (cf->input_vector) - i);
2109 /* There was a partial match which means we need more bytes
2110 * than the input buffer currently has.
2111 * Since the bytes before here have been processed, shift
2112 * the remaining contents to the start of the input buffer.
2114 vec_delete (cf->input_vector, i, 0);
2116 return 1; /* wait for more */
2121 /* process the action */
2122 if (!unix_cli_line_process_one (cm, um, cf, uf,
2123 cf->input_vector[i], action))
2125 /* CRLF found. Consume the bytes from the input_vector */
2126 vec_delete (cf->input_vector, i + matched, 0);
2127 /* And tell our caller to execute cf->input_command */
2135 vec_reset_length (cf->input_vector);
2139 /** @brief Process input to a CLI session. */
2141 unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index)
2143 unix_main_t *um = &unix_main;
2144 clib_file_main_t *fm = &file_main;
2146 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2147 unformat_input_t input;
2148 int vlib_parse_eval (u8 *);
2151 /* Try vlibplex first. Someday... */
2152 if (0 && vlib_parse_eval (cf->input_vector) == 0)
2157 /* just treat whatever we got as a complete line of input */
2158 cf->current_command = cf->input_vector;
2162 /* Line edit, echo, etc. */
2163 if (unix_cli_line_edit (cm, um, fm, cf))
2164 /* want more input */
2171 vec_reset_length (lv);
2172 lv = format (lv, "%U[%d]: %v",
2173 format_timeval, 0 /* current bat-time */ ,
2174 0 /* current bat-format */ ,
2175 cli_file_index, cf->input_vector);
2177 int rv __attribute__ ((unused)) =
2178 write (um->log_fd, lv, vec_len (lv));
2182 /* Copy our input command to a new string */
2183 unformat_init_vector (&input, cf->current_command);
2185 /* Remove leading white space from input. */
2186 (void) unformat (&input, "");
2188 cm->current_input_file_index = cli_file_index;
2189 cf->pager_start = 0; /* start a new pager session */
2191 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
2192 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output,
2195 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
2198 unformat_free (&input);
2200 /* Re-fetch pointer since pool may have moved. */
2201 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2202 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2205 /* reset vector; we'll re-use it later */
2207 vec_reset_length (cf->input_vector);
2209 vec_reset_length (cf->current_command);
2211 if (cf->no_pager == 2)
2213 /* Pager was programmatically disabled */
2214 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
2215 cf->no_pager = um->cli_no_pager;
2218 if (vec_len (cf->pager_index) == 0
2219 || vec_len (cf->pager_index) < cf->height)
2221 /* There was no need for the pager */
2222 unix_cli_pager_reset (cf);
2225 unix_cli_cli_prompt (cf, uf);
2229 /* Display the pager prompt */
2230 unix_cli_pager_prompt (cf, uf);
2233 /* Any residual data in the input vector? */
2234 if (vec_len (cf->input_vector))
2238 /** Destroy a CLI session.
2239 * @note If we destroy the @c stdin session this additionally signals
2240 * the shutdown of VPP.
2243 unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
2245 unix_main_t *um = &unix_main;
2246 clib_file_main_t *fm = &file_main;
2247 unix_cli_file_t *cf;
2251 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2252 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2254 /* Quit/EOF on stdin means quit program. */
2255 if (uf->file_descriptor == UNIX_CLI_STDIN_FD)
2256 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
2258 vec_free (cf->current_command);
2259 vec_free (cf->search_key);
2261 for (i = 0; i < vec_len (cf->command_history); i++)
2262 vec_free (cf->command_history[i]);
2264 vec_free (cf->command_history);
2266 clib_file_del (fm, uf);
2268 unix_cli_file_free (cf);
2269 pool_put (cm->cli_file_pool, cf);
2272 /** Handle system events. */
2274 unix_cli_process (vlib_main_t * vm,
2275 vlib_node_runtime_t * rt, vlib_frame_t * f)
2277 unix_cli_main_t *cm = &unix_cli_main;
2282 unix_cli_process_event_type_t event_type;
2283 vlib_process_wait_for_event (vm);
2284 event_type = vlib_process_get_events (vm, &data);
2288 case UNIX_CLI_PROCESS_EVENT_READ_READY:
2289 for (i = 0; i < vec_len (data); i++)
2290 unix_cli_process_input (cm, data[i]);
2293 case UNIX_CLI_PROCESS_EVENT_QUIT:
2294 /* Kill this process. */
2295 for (i = 0; i < vec_len (data); i++)
2296 unix_cli_kill (cm, data[i]);
2301 _vec_len (data) = 0;
2307 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
2309 /* Add node index so we can re-use this process later. */
2310 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
2315 /** Called when a CLI session file descriptor can be written to without
2317 static clib_error_t *
2318 unix_cli_write_ready (clib_file_t * uf)
2320 unix_cli_main_t *cm = &unix_cli_main;
2321 unix_cli_file_t *cf;
2324 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2326 /* Flush output vector. */
2327 n = write (uf->file_descriptor,
2328 cf->output_vector, vec_len (cf->output_vector));
2330 if (n < 0 && errno != EAGAIN)
2331 return clib_error_return_unix (0, "write");
2334 unix_cli_del_pending_output (uf, cf, n);
2336 return /* no error */ 0;
2339 /** Called when a CLI session file descriptor has data to be read. */
2340 static clib_error_t *
2341 unix_cli_read_ready (clib_file_t * uf)
2343 unix_main_t *um = &unix_main;
2344 unix_cli_main_t *cm = &unix_cli_main;
2345 unix_cli_file_t *cf;
2347 int n, n_read, n_try;
2349 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2354 l = vec_len (cf->input_vector);
2355 vec_resize (cf->input_vector, l + n_try);
2357 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
2360 if (n < 0 && errno != EAGAIN)
2361 return clib_error_return_unix (0, "read");
2363 n_read = n < 0 ? 0 : n;
2364 _vec_len (cf->input_vector) = l + n_read;
2368 vlib_process_signal_event (um->vlib_main,
2369 cf->process_node_index,
2371 ? UNIX_CLI_PROCESS_EVENT_QUIT
2372 : UNIX_CLI_PROCESS_EVENT_READ_READY),
2373 /* event data */ uf->private_data);
2375 return /* no error */ 0;
2378 /** Store a new CLI session.
2379 * @param name The name of the session.
2380 * @param fd The file descriptor for the session I/O.
2381 * @return The session ID.
2384 unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
2386 unix_main_t *um = &unix_main;
2387 clib_file_main_t *fm = &file_main;
2388 unix_cli_file_t *cf;
2389 clib_file_t template = { 0 };
2390 vlib_main_t *vm = um->vlib_main;
2393 name = (char *) format (0, "unix-cli-%s", name);
2395 if (vec_len (cm->unused_cli_process_node_indices) > 0)
2397 uword l = vec_len (cm->unused_cli_process_node_indices);
2399 /* Find node and give it new name. */
2400 n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
2402 n->name = (u8 *) name;
2404 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
2406 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
2410 static vlib_node_registration_t r = {
2411 .function = unix_cli_process,
2412 .type = VLIB_NODE_TYPE_PROCESS,
2413 .process_log2_n_stack_bytes = 16,
2417 vlib_register_node (vm, &r);
2420 n = vlib_get_node (vm, r.index);
2423 pool_get (cm->cli_file_pool, cf);
2424 memset (cf, 0, sizeof (*cf));
2426 template.read_function = unix_cli_read_ready;
2427 template.write_function = unix_cli_write_ready;
2428 template.file_descriptor = fd;
2429 template.private_data = cf - cm->cli_file_pool;
2431 cf->process_node_index = n->index;
2432 cf->clib_file_index = clib_file_add (fm, &template);
2433 cf->output_vector = 0;
2434 cf->input_vector = 0;
2436 vlib_start_process (vm, n->runtime_index);
2438 vlib_process_t *p = vlib_get_process_from_node (vm, n);
2439 p->output_function = unix_vlib_cli_output;
2440 p->output_function_arg = cf - cm->cli_file_pool;
2442 return cf - cm->cli_file_pool;
2445 /** Telnet listening socket has a new connection. */
2446 static clib_error_t *
2447 unix_cli_listen_read_ready (clib_file_t * uf)
2449 unix_main_t *um = &unix_main;
2450 clib_file_main_t *fm = &file_main;
2451 unix_cli_main_t *cm = &unix_cli_main;
2452 clib_socket_t *s = &um->cli_listen_socket;
2453 clib_socket_t client;
2455 clib_error_t *error;
2456 unix_cli_file_t *cf;
2459 error = clib_socket_accept (s, &client);
2463 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
2465 cf_index = unix_cli_file_add (cm, client_name, client.fd);
2466 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2468 /* No longer need CLIB version of socket. */
2469 clib_socket_free (&client);
2471 vec_free (client_name);
2473 /* if we're supposed to run telnet session in character mode (default) */
2474 if (um->cli_line_mode == 0)
2477 * Set telnet client character mode, echo on, suppress "go-ahead".
2478 * Technically these should be negotiated, but this works.
2480 u8 charmode_option[] = {
2481 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
2482 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
2483 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
2484 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
2485 IAC, WILL, TELOPT_ECHO, /* server will do echo */
2486 IAC, DONT, TELOPT_ECHO, /* client should not echo */
2487 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
2488 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
2489 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
2490 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
2493 /* Enable history on this CLI */
2494 cf->history_limit = um->cli_history_limit;
2495 cf->has_history = cf->history_limit != 0;
2497 /* Make sure this session is in line mode */
2503 /* Setup the pager */
2504 cf->no_pager = um->cli_no_pager;
2506 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2508 /* Send the telnet options */
2509 unix_vlib_cli_output_raw (cf, uf, charmode_option,
2510 ARRAY_LEN (charmode_option));
2512 /* In case the client doesn't negotiate terminal type, use
2513 * a timer to kick off the initial prompt. */
2514 timer_call (unix_cli_file_welcome_timer, cf_index, 1);
2520 /** The system terminal has informed us that the window size
2524 unix_cli_resize_interrupt (int signum)
2526 clib_file_main_t *fm = &file_main;
2527 unix_cli_main_t *cm = &unix_cli_main;
2528 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2529 cm->stdin_cli_file_index);
2530 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2534 /* Terminal resized, fetch the new size */
2535 if (ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws) < 0)
2537 /* "Should never happen..." */
2538 clib_unix_warning ("TIOCGWINSZ");
2539 /* We can't trust ws.XXX... */
2542 cf->width = ws.ws_col;
2543 cf->height = ws.ws_row;
2545 /* Reindex the pager buffer */
2546 unix_cli_pager_reindex (cf);
2548 /* Redraw the page */
2549 unix_cli_pager_redraw (cf, uf);
2552 /** Handle configuration directives in the @em unix section. */
2553 static clib_error_t *
2554 unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
2556 unix_main_t *um = &unix_main;
2557 clib_file_main_t *fm = &file_main;
2558 unix_cli_main_t *cm = &unix_cli_main;
2560 clib_error_t *error = 0;
2561 unix_cli_file_t *cf;
2564 struct sigaction sa;
2568 /* We depend on unix flags being set. */
2569 if ((error = vlib_call_config_function (vm, unix_config)))
2572 if (um->flags & UNIX_FLAG_INTERACTIVE)
2574 /* Set stdin to be non-blocking. */
2575 if ((flags = fcntl (UNIX_CLI_STDIN_FD, F_GETFL, 0)) < 0)
2577 (void) fcntl (UNIX_CLI_STDIN_FD, F_SETFL, flags | O_NONBLOCK);
2579 cf_index = unix_cli_file_add (cm, "stdin", UNIX_CLI_STDIN_FD);
2580 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2581 cm->stdin_cli_file_index = cf_index;
2583 /* If stdin is a tty and we are using chacracter mode, enable
2584 * history on the CLI and set the tty line discipline accordingly. */
2585 if (isatty (UNIX_CLI_STDIN_FD) && um->cli_line_mode == 0)
2587 /* Capture terminal resize events */
2588 memset (&sa, 0, sizeof (sa));
2589 sa.sa_handler = unix_cli_resize_interrupt;
2590 if (sigaction (SIGWINCH, &sa, 0) < 0)
2591 clib_panic ("sigaction");
2593 /* Retrieve the current terminal size */
2594 ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws);
2595 cf->width = ws.ws_col;
2596 cf->height = ws.ws_row;
2598 if (cf->width == 0 || cf->height == 0)
2599 /* We have a tty, but no size. Stick to line mode. */
2602 /* Setup the history */
2603 cf->history_limit = um->cli_history_limit;
2604 cf->has_history = cf->history_limit != 0;
2606 /* Setup the pager */
2607 cf->no_pager = um->cli_no_pager;
2609 /* We're going to be in char by char mode */
2612 /* Save the original tty state so we can restore it later */
2613 tcgetattr (UNIX_CLI_STDIN_FD, &um->tio_stdin);
2616 /* Tweak the tty settings */
2617 tio = um->tio_stdin;
2618 /* echo off, canonical mode off, ext'd input processing off */
2619 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
2620 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
2621 tio.c_cc[VTIME] = 0; /* no timer */
2622 tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &tio);
2624 /* See if we can do ANSI/VT100 output */
2625 term = (u8 *) getenv ("TERM");
2627 cf->ansi_capable = unix_cli_terminal_type (term,
2634 /* No tty, so make sure these things are off */
2636 cf->history_limit = 0;
2637 cf->has_history = 0;
2641 /* Send banner and initial prompt */
2642 unix_cli_file_welcome (cm, cf);
2645 /* If we have socket config, LISTEN, otherwise, don't */
2646 clib_socket_t *s = &um->cli_listen_socket;
2647 if (s->config && s->config[0] != 0)
2650 clib_file_t template = { 0 };
2652 /* mkdir of file socketu, only under /run */
2653 if (strncmp (s->config, "/run", 4) == 0)
2655 u8 *tmp = format (0, "%s", s->config);
2656 int i = vec_len (tmp);
2657 while (i && tmp[--i] != '/')
2663 vlib_unix_recursive_mkdir ((char *) tmp);
2667 s->flags = CLIB_SOCKET_F_IS_SERVER | /* listen, don't connect */
2668 CLIB_SOCKET_F_ALLOW_GROUP_WRITE; /* PF_LOCAL socket only */
2669 error = clib_socket_init (s);
2674 template.read_function = unix_cli_listen_read_ready;
2675 template.file_descriptor = s->fd;
2677 clib_file_add (fm, &template);
2680 /* Set CLI prompt. */
2681 if (!cm->cli_prompt)
2682 cm->cli_prompt = format (0, "VLIB: ");
2688 * This module has no configurable parameters.
2690 VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
2692 /** Called when VPP is shutting down, this restores the system
2693 * terminal state if previously saved.
2695 static clib_error_t *
2696 unix_cli_exit (vlib_main_t * vm)
2698 unix_main_t *um = &unix_main;
2700 /* If stdin is a tty and we saved the tty state, reset the tty state */
2701 if (isatty (UNIX_CLI_STDIN_FD) && um->tio_isset)
2702 tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &um->tio_stdin);
2707 VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
2709 /** Set the CLI prompt.
2710 * @param prompt The C string to set the prompt to.
2711 * @note This setting is global; it impacts all current
2712 * and future CLI sessions.
2715 vlib_unix_cli_set_prompt (char *prompt)
2717 char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s ";
2718 unix_cli_main_t *cm = &unix_cli_main;
2720 vec_free (cm->cli_prompt);
2721 cm->cli_prompt = format (0, fmt, prompt);
2724 /** CLI command to quit the terminal session.
2725 * @note If this is a stdin session then this will
2726 * shutdown VPP also.
2728 static clib_error_t *
2729 unix_cli_quit (vlib_main_t * vm,
2730 unformat_input_t * input, vlib_cli_command_t * cmd)
2732 unix_cli_main_t *cm = &unix_cli_main;
2734 vlib_process_signal_event (vm,
2735 vlib_current_process (vm),
2736 UNIX_CLI_PROCESS_EVENT_QUIT,
2737 cm->current_input_file_index);
2742 * Terminates the current CLI session.
2744 * If VPP is running in @em interactive mode and this is the console session
2745 * (that is, the session on @c stdin) then this will also terminate VPP.
2748 VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
2750 .short_help = "Exit CLI",
2751 .function = unix_cli_quit,
2755 /** CLI command to execute a VPP command script. */
2756 static clib_error_t *
2757 unix_cli_exec (vlib_main_t * vm,
2758 unformat_input_t * input, vlib_cli_command_t * cmd)
2762 unformat_input_t sub_input;
2763 clib_error_t *error;
2769 if (!unformat (input, "%s", &file_name))
2771 error = clib_error_return (0, "expecting file name, got `%U'",
2772 format_unformat_error, input);
2776 fd = open (file_name, O_RDONLY);
2779 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
2783 /* Make sure its a regular file. */
2787 if (fstat (fd, &s) < 0)
2789 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
2793 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
2795 error = clib_error_return (0, "not a regular file `%s'", file_name);
2800 unformat_init_unix_file (&sub_input, fd);
2802 vlib_cli_input (vm, &sub_input, 0, 0);
2803 unformat_free (&sub_input);
2808 vec_free (file_name);
2814 * Executes a sequence of CLI commands which are read from a file.
2816 * If a command is unrecognised or otherwise invalid then the usual CLI
2817 * feedback will be generated, however execution of subsequent commands
2818 * from the file will continue.
2821 VLIB_CLI_COMMAND (cli_exec, static) = {
2823 .short_help = "Execute commands from file",
2824 .function = unix_cli_exec,
2829 /** CLI command to show various unix error statistics. */
2830 static clib_error_t *
2831 unix_show_errors (vlib_main_t * vm,
2832 unformat_input_t * input, vlib_cli_command_t * cmd)
2834 unix_main_t *um = &unix_main;
2835 clib_error_t *error = 0;
2836 int i, n_errors_to_show;
2837 unix_error_history_t *unix_errors = 0;
2839 n_errors_to_show = 1 << 30;
2841 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2843 if (!unformat (input, "%d", &n_errors_to_show))
2846 clib_error_return (0,
2847 "expecting integer number of errors to show, got `%U'",
2848 format_unformat_error, input);
2854 clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
2857 um->error_history_index >
2858 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
2860 while (n_errors_to_show > 0)
2862 unix_error_history_t *eh = um->error_history + i;
2867 vec_add1 (unix_errors, eh[0]);
2868 n_errors_to_show -= 1;
2870 i = ARRAY_LEN (um->error_history) - 1;
2875 if (vec_len (unix_errors) == 0)
2876 vlib_cli_output (vm, "no Unix errors so far");
2879 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
2880 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
2882 unix_error_history_t *eh = vec_elt_at_index (unix_errors, i);
2883 vlib_cli_output (vm, "%U: %U",
2884 format_time_interval, "h:m:s:u", eh->time,
2885 format_clib_error, eh->error);
2887 vlib_cli_output (vm, "%U: time now",
2888 format_time_interval, "h:m:s:u", vlib_time_now (vm));
2892 vec_free (unix_errors);
2897 VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
2898 .path = "show unix-errors",
2899 .short_help = "Show Unix system call error history",
2900 .function = unix_show_errors,
2904 /** CLI command to show session command history. */
2905 static clib_error_t *
2906 unix_cli_show_history (vlib_main_t * vm,
2907 unformat_input_t * input, vlib_cli_command_t * cmd)
2909 unix_cli_main_t *cm = &unix_cli_main;
2910 unix_cli_file_t *cf;
2913 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2915 if (cf->has_history && cf->history_limit)
2917 i = 1 + cf->command_number - vec_len (cf->command_history);
2918 for (j = 0; j < vec_len (cf->command_history); j++)
2919 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
2923 vlib_cli_output (vm, "History not enabled.\n");
2930 * Displays the command history for the current session, if any.
2933 VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
2935 .short_help = "Show current session command history",
2936 .function = unix_cli_show_history,
2940 /** CLI command to show terminal status. */
2941 static clib_error_t *
2942 unix_cli_show_terminal (vlib_main_t * vm,
2943 unformat_input_t * input, vlib_cli_command_t * cmd)
2945 unix_main_t *um = &unix_main;
2946 unix_cli_main_t *cm = &unix_cli_main;
2947 unix_cli_file_t *cf;
2950 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2951 n = vlib_get_node (vm, cf->process_node_index);
2953 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
2954 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
2955 "line-by-line" : "char-by-char");
2956 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
2957 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
2958 vlib_cli_output (vm, "ANSI capable: %s\n",
2959 cf->ansi_capable ? "yes" : "no");
2960 vlib_cli_output (vm, "History enabled: %s%s\n",
2961 cf->has_history ? "yes" : "no", !cf->has_history
2962 || cf->history_limit ? "" :
2963 " (disabled by history limit)");
2964 if (cf->has_history)
2965 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
2966 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
2967 cf->no_pager ? "no" : "yes",
2969 || cf->height ? "" : " (disabled by terminal height)",
2971 || um->cli_pager_buffer_limit ? "" :
2972 " (disabled by buffer limit)");
2974 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
2975 vlib_cli_output (vm, "CRLF mode: %s\n",
2976 cf->crlf_mode ? "CR+LF" : "LF");
2982 * Displays various information about the state of the current terminal
2986 * @cliexstart{show terminal}
2987 * Terminal name: unix-cli-stdin
2988 * Terminal mode: char-by-char
2989 * Terminal width: 123
2990 * Terminal height: 48
2992 * History enabled: yes
2994 * Pager enabled: yes
2995 * Pager limit: 100000
3000 VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
3001 .path = "show terminal",
3002 .short_help = "Show current session terminal settings",
3003 .function = unix_cli_show_terminal,
3007 /** CLI command to set terminal pager settings. */
3008 static clib_error_t *
3009 unix_cli_set_terminal_pager (vlib_main_t * vm,
3010 unformat_input_t * input,
3011 vlib_cli_command_t * cmd)
3013 unix_main_t *um = &unix_main;
3014 unix_cli_main_t *cm = &unix_cli_main;
3015 unix_cli_file_t *cf;
3016 unformat_input_t _line_input, *line_input = &_line_input;
3017 clib_error_t *error = 0;
3019 if (!unformat_user (input, unformat_line_input, line_input))
3022 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3024 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3026 if (unformat (line_input, "on"))
3028 else if (unformat (line_input, "off"))
3030 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
3031 vlib_cli_output (vm,
3032 "Pager limit set to %u lines; note, this is global.\n",
3033 um->cli_pager_buffer_limit);
3036 error = clib_error_return (0, "unknown parameter: `%U`",
3037 format_unformat_error, line_input);
3043 unformat_free (line_input);
3049 * Enables or disables the terminal pager for this session. Generally
3050 * this defaults to enabled.
3052 * Additionally allows the pager buffer size to be set; though note that
3053 * this value is set globally and not per session.
3056 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
3057 .path = "set terminal pager",
3058 .short_help = "set terminal pager [on|off] [limit <lines>]",
3059 .function = unix_cli_set_terminal_pager,
3063 /** CLI command to set terminal history settings. */
3064 static clib_error_t *
3065 unix_cli_set_terminal_history (vlib_main_t * vm,
3066 unformat_input_t * input,
3067 vlib_cli_command_t * cmd)
3069 unix_cli_main_t *cm = &unix_cli_main;
3070 unix_cli_file_t *cf;
3071 unformat_input_t _line_input, *line_input = &_line_input;
3073 clib_error_t *error = 0;
3075 if (!unformat_user (input, unformat_line_input, line_input))
3078 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3080 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3082 if (unformat (line_input, "on"))
3083 cf->has_history = 1;
3084 else if (unformat (line_input, "off"))
3085 cf->has_history = 0;
3086 else if (unformat (line_input, "limit %u", &cf->history_limit))
3090 error = clib_error_return (0, "unknown parameter: `%U`",
3091 format_unformat_error, line_input);
3095 /* If we reduced history size, or turned it off, purge the history */
3096 limit = cf->has_history ? cf->history_limit : 0;
3098 while (cf->command_history && vec_len (cf->command_history) >= limit)
3100 vec_free (cf->command_history[0]);
3101 vec_delete (cf->command_history, 1, 0);
3106 unformat_free (line_input);
3112 * Enables or disables the command history function of the current
3113 * terminal. Generally this defaults to enabled.
3115 * This command also allows the maximum size of the history buffer for
3116 * this session to be altered.
3119 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
3120 .path = "set terminal history",
3121 .short_help = "set terminal history [on|off] [limit <lines>]",
3122 .function = unix_cli_set_terminal_history,
3126 /** CLI command to set terminal ANSI settings. */
3127 static clib_error_t *
3128 unix_cli_set_terminal_ansi (vlib_main_t * vm,
3129 unformat_input_t * input,
3130 vlib_cli_command_t * cmd)
3132 unix_cli_main_t *cm = &unix_cli_main;
3133 unix_cli_file_t *cf;
3135 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3137 if (unformat (input, "on"))
3138 cf->ansi_capable = 1;
3139 else if (unformat (input, "off"))
3140 cf->ansi_capable = 0;
3142 return clib_error_return (0, "unknown parameter: `%U`",
3143 format_unformat_error, input);
3149 * Enables or disables the use of ANSI control sequences by this terminal.
3150 * The default will vary based on terminal detection at the start of the
3153 * ANSI control sequences are used in a small number of places to provide,
3154 * for example, color text output and to control the cursor in the pager.
3157 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
3158 .path = "set terminal ansi",
3159 .short_help = "set terminal ansi [on|off]",
3160 .function = unix_cli_set_terminal_ansi,
3164 static clib_error_t *
3165 unix_cli_init (vlib_main_t * vm)
3170 VLIB_INIT_FUNCTION (unix_cli_init);
3173 * fd.io coding-style-patch-verification: ON
3176 * eval: (c-set-style "gnu")