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
104 /** A CLI banner line. */
107 u8 *line; /**< The line to print. */
108 u32 length; /**< The length of the line without terminating NUL. */
111 #define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
112 /** Plain welcome banner. */
113 static unix_cli_banner_t unix_cli_banner[] = {
114 _(" _______ _ _ _____ ___ \n"),
115 _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
116 _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
117 _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
121 /** ANSI color welcome banner. */
122 static unix_cli_banner_t unix_cli_banner_color[] = {
123 _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
124 _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
125 _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
126 _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"),
132 /** Pager line index */
135 /** Index into pager_vector */
138 /** Offset of the string in the line */
141 /** Length of the string in the line */
143 } unix_cli_pager_index_t;
146 /** Unix CLI session. */
149 /** The file index held by unix.c */
152 /** Vector of output pending write to file descriptor. */
155 /** Vector of input saved by Unix input node to be processed by
159 /** This session has command history. */
161 /** Array of vectors of commands in the history. */
162 u8 **command_history;
163 /** The command currently pointed at by the history cursor. */
165 /** How far from the end of the history array the user has browsed. */
168 /** Maximum number of history entries this session will store. */
171 /** Current command line counter */
174 /** The string being searched for in the history. */
176 /** If non-zero then the CLI is searching in the history array.
177 * - @c -1 means search backwards.
178 * - @c 1 means search forwards.
182 /** Position of the insert cursor on the current input line */
185 /** Line mode or char mode */
188 /** Set if the CRLF mode wants CR + LF */
191 /** Can we do ANSI output? */
194 /** Has the session started? */
197 /** Disable the pager? */
200 /** Whether the session is interactive or not.
201 * Controls things like initial banner, the CLI prompt etc. */
204 /** Whether the session is attached to a socket. */
207 /** If EPIPE has been detected, prevent further write-related
208 * activity on the descriptor.
215 /** Index of line fragments in the pager buffer */
216 unix_cli_pager_index_t *pager_index;
218 /** Line number of top of page */
221 /** Terminal width */
224 /** Terminal height */
227 /** Process node identifier */
228 u32 process_node_index;
231 /** Resets the pager buffer and other data.
232 * @param f The CLI session whose pager needs to be reset.
235 unix_cli_pager_reset (unix_cli_file_t * f)
241 vec_free (f->pager_index);
244 vec_foreach (p, f->pager_vector)
248 vec_free (f->pager_vector);
252 /** Release storage used by a CLI session.
253 * @param f The CLI session whose storage needs to be released.
256 unix_cli_file_free (unix_cli_file_t * f)
258 vec_free (f->output_vector);
259 vec_free (f->input_vector);
260 unix_cli_pager_reset (f);
266 UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */
267 UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */
268 UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */
269 UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */
270 UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */
271 UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */
272 UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */
273 UNIX_CLI_PARSE_ACTION_LEFT, /**< Left arrow */
274 UNIX_CLI_PARSE_ACTION_RIGHT, /**< Right arrow */
275 UNIX_CLI_PARSE_ACTION_HOME, /**< Home key (jump to start of line) */
276 UNIX_CLI_PARSE_ACTION_END, /**< End key (jump to end of line) */
277 UNIX_CLI_PARSE_ACTION_WORDLEFT, /**< Jump cursor to start of left word */
278 UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */
279 UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */
280 UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */
281 UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */
282 UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */
283 UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */
284 UNIX_CLI_PARSE_ACTION_YANK, /**< Undo last erase action */
285 UNIX_CLI_PARSE_ACTION_TELNETIAC, /**< Telnet control code */
287 UNIX_CLI_PARSE_ACTION_PAGER_CRLF, /**< Enter pressed (CR, CRLF, LF, etc) */
288 UNIX_CLI_PARSE_ACTION_PAGER_QUIT, /**< Exit the pager session */
289 UNIX_CLI_PARSE_ACTION_PAGER_NEXT, /**< Scroll to next page */
290 UNIX_CLI_PARSE_ACTION_PAGER_DN, /**< Scroll to next line */
291 UNIX_CLI_PARSE_ACTION_PAGER_UP, /**< Scroll to previous line */
292 UNIX_CLI_PARSE_ACTION_PAGER_TOP, /**< Scroll to first line */
293 UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM, /**< Scroll to last line */
294 UNIX_CLI_PARSE_ACTION_PAGER_PGDN, /**< Scroll to next page */
295 UNIX_CLI_PARSE_ACTION_PAGER_PGUP, /**< Scroll to previous page */
296 UNIX_CLI_PARSE_ACTION_PAGER_REDRAW, /**< Clear and redraw the page on the terminal */
297 UNIX_CLI_PARSE_ACTION_PAGER_SEARCH, /**< Search the pager buffer */
299 UNIX_CLI_PARSE_ACTION_PARTIALMATCH, /**< Action parser found a partial match */
300 UNIX_CLI_PARSE_ACTION_NOMATCH /**< Action parser did not find any match */
301 } unix_cli_parse_action_t;
303 /** @brief Mapping of input buffer strings to action values.
304 * @note This won't work as a hash since we need to be able to do
305 * partial matches on the string.
309 u8 *input; /**< Input string to match. */
310 u32 len; /**< Length of input without final NUL. */
311 unix_cli_parse_action_t action; /**< Action to take when matched. */
312 } unix_cli_parse_actions_t;
314 /** @brief Given a capital ASCII letter character return a @c NUL terminated
315 * string with the control code for that letter.
317 * @param c An ASCII character.
318 * @return A @c NUL terminated string of type @c u8[].
321 * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[].
323 #define CTL(c) (u8[]){ (c) - '@', 0 }
325 #define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
327 * Patterns to match on a CLI input stream.
330 static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
332 _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */
333 _("\n", UNIX_CLI_PARSE_ACTION_CRLF),
334 _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */
335 _("\r", UNIX_CLI_PARSE_ACTION_CRLF),
337 /* Unix shell control codes */
338 _(CTL ('B'), UNIX_CLI_PARSE_ACTION_LEFT),
339 _(CTL ('F'), UNIX_CLI_PARSE_ACTION_RIGHT),
340 _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP),
341 _(CTL ('N'), UNIX_CLI_PARSE_ACTION_DOWN),
342 _(CTL ('A'), UNIX_CLI_PARSE_ACTION_HOME),
343 _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END),
344 _(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT),
345 _(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT),
346 _(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT),
347 _(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK),
348 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR),
349 _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */
350 _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */
351 _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */
352 _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */
353 _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */
355 /* VT100 Normal mode - Broadest support */
356 _(CSI "A", UNIX_CLI_PARSE_ACTION_UP),
357 _(CSI "B", UNIX_CLI_PARSE_ACTION_DOWN),
358 _(CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT),
359 _(CSI "D", UNIX_CLI_PARSE_ACTION_LEFT),
360 _(CSI "H", UNIX_CLI_PARSE_ACTION_HOME),
361 _(CSI "F", UNIX_CLI_PARSE_ACTION_END),
362 _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */
363 _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */
364 _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */
366 /* VT100 Application mode - Some Gnome Terminal functions use these */
367 _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP),
368 _(ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN),
369 _(ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT),
370 _(ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT),
371 _(ESC "OH", UNIX_CLI_PARSE_ACTION_HOME),
372 _(ESC "OF", UNIX_CLI_PARSE_ACTION_END),
374 /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
375 _(CSI "1~", UNIX_CLI_PARSE_ACTION_HOME),
376 _(CSI "4~", UNIX_CLI_PARSE_ACTION_END),
378 /* Emacs-ish history search */
379 _(CTL ('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH),
380 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH),
382 /* Other protocol things */
383 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
384 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
385 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
389 * Patterns to match when a CLI session is in the pager.
392 static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
394 _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */
395 _("\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
396 _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */
397 _("\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF),
400 _(" ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT),
401 _("q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT),
402 _(CTL ('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
403 _(CTL ('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW),
404 _("/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH),
407 _(CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP),
408 _(CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN),
409 _(CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
410 _(CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
412 /* VT100 Application mode */
413 _(ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP),
414 _(ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN),
415 _(ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
416 _(ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
418 /* ANSI X3.41-1974 */
419 _(CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP),
420 _(CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM),
421 _(CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP),
422 _(CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN),
424 /* Other protocol things */
425 _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
426 _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
427 _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH)
432 /** CLI session events. */
435 UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */
436 UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */
437 } unix_cli_process_event_type_t;
439 /** CLI global state. */
442 /** Prompt string for CLI. */
445 /** Vec pool of CLI sessions. */
446 unix_cli_file_t *cli_file_pool;
448 /** Vec pool of unused session indices. */
449 u32 *unused_cli_process_node_indices;
451 /** The session index of the stdin cli */
452 u32 stdin_cli_file_index;
454 /** File pool index of current input. */
455 u32 current_input_file_index;
458 /** CLI global state */
459 static unix_cli_main_t unix_cli_main;
462 * @brief Search for a byte sequence in the action list.
464 * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with
465 * the bytes in @a input of maximum length @a ilen bytes.
466 * When a match is made @a *matched indicates how many bytes were matched.
467 * Returns a value from the enum @ref unix_cli_parse_action_t to indicate
468 * whether no match was found, a partial match was found or a complete
469 * match was found and what action, if any, should be taken.
471 * @param[in] a Actions list to search within.
472 * @param[in] input String fragment to search for.
473 * @param[in] ilen Length of the string in 'input'.
474 * @param[out] matched Pointer to an integer that will contain the number
475 * of bytes matched when a complete match is found.
477 * @return Action from @ref unix_cli_parse_action_t that the string fragment
479 * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the
480 * whole input string matches the start of at least one action.
481 * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
484 static unix_cli_parse_action_t
485 unix_cli_match_action (unix_cli_parse_actions_t * a,
486 u8 * input, u32 ilen, i32 * matched)
494 /* see if the start of the input buffer exactly matches the current
496 if (memcmp (input, a->input, a->len) == 0)
504 /* if the first ilen characters match, flag this as a partial -
505 * meaning keep collecting bytes in case of a future match */
506 if (memcmp (input, a->input, ilen) == 0)
510 /* check next action */
515 UNIX_CLI_PARSE_ACTION_PARTIALMATCH : UNIX_CLI_PARSE_ACTION_NOMATCH;
519 /** Add bytes to the output vector and then flagg the I/O system that bytes
520 * are available to be sent.
523 unix_cli_add_pending_output (clib_file_t * uf,
524 unix_cli_file_t * cf,
525 u8 * buffer, uword buffer_bytes)
527 clib_file_main_t *fm = &file_main;
529 vec_add (cf->output_vector, buffer, buffer_bytes);
530 if (vec_len (cf->output_vector) > 0)
532 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
533 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
535 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
539 /** Delete all bytes from the output vector and flag the I/O system
540 * that no more bytes are available to be sent.
543 unix_cli_del_pending_output (clib_file_t * uf,
544 unix_cli_file_t * cf, uword n_bytes)
546 clib_file_main_t *fm = &file_main;
548 vec_delete (cf->output_vector, n_bytes, 0);
549 if (vec_len (cf->output_vector) <= 0)
551 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
552 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
554 fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
558 /** @brief A bit like strchr with a buffer length limit.
559 * Search a buffer for the first instance of a character up to the limit of
560 * the buffer length. If found then return the position of that character.
562 * The key departure from strchr is that if the character is not found then
563 * return the buffer length.
565 * @param chr The byte value to search for.
566 * @param str The buffer in which to search for the value.
567 * @param len The depth into the buffer to search.
569 * @return The index of the first occurence of \c chr. If \c chr is not
570 * found then \c len instead.
573 unix_vlib_findchr (u8 chr, u8 * str, word len)
576 for (i = 0; i < len; i++, str++)
584 /** @brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
585 * Attempts to write given buffer to the file descriptor of the given
586 * Unix CLI session. If that session already has data in the output buffer
587 * or if the write attempt tells us to try again later then the given buffer
588 * is appended to the pending output buffer instead.
590 * This is typically called only from \c unix_vlib_cli_output_cooked since
591 * that is where CRLF handling occurs or from places where we explicitly do
592 * not want cooked handling.
594 * @param cf Unix CLI session of the desired stream to write to.
595 * @param uf The Unix file structure of the desired stream to write to.
596 * @param buffer Pointer to the buffer that needs to be written.
597 * @param buffer_bytes The number of bytes from \c buffer to write.
600 unix_vlib_cli_output_raw (unix_cli_file_t * cf,
601 clib_file_t * uf, u8 * buffer, uword buffer_bytes)
605 if (cf->has_epipe) /* don't try writing anything */
608 if (vec_len (cf->output_vector) == 0)
611 /* If it's a socket we use MSG_NOSIGNAL to prevent SIGPIPE */
612 n = send (uf->file_descriptor, buffer, buffer_bytes, MSG_NOSIGNAL);
614 n = write (uf->file_descriptor, buffer, buffer_bytes);
617 if (n < 0 && errno != EAGAIN)
621 /* connection closed on us */
622 unix_main_t *um = &unix_main;
624 vlib_process_signal_event (um->vlib_main, cf->process_node_index,
625 UNIX_CLI_PROCESS_EVENT_QUIT,
630 clib_unix_warning ("write");
633 else if ((word) n < (word) buffer_bytes)
635 /* We got EAGAIN or we already have stuff in the buffer;
636 * queue up whatever didn't get sent for later. */
639 unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
643 /** @brief Process a buffer for CRLF handling before outputting it to the CLI.
645 * @param cf Unix CLI session of the desired stream to write to.
646 * @param uf The Unix file structure of the desired stream to write to.
647 * @param buffer Pointer to the buffer that needs to be written.
648 * @param buffer_bytes The number of bytes from \c buffer to write.
651 unix_vlib_cli_output_cooked (unix_cli_file_t * cf,
653 u8 * buffer, uword buffer_bytes)
655 word end = 0, start = 0;
657 while (end < buffer_bytes)
661 /* iterate the line on \n's so we can insert a \r before it */
662 end = unix_vlib_findchr ('\n',
664 buffer_bytes - start) + start;
668 /* otherwise just send the whole buffer */
672 unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start);
676 if (end < buffer_bytes)
678 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2);
679 end++; /* skip the \n that we already sent */
686 /** @brief Output the CLI prompt */
688 unix_cli_cli_prompt (unix_cli_file_t * cf, clib_file_t * uf)
690 unix_cli_main_t *cm = &unix_cli_main;
692 if (cf->is_interactive) /* Only interactive sessions get a prompt */
693 unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt,
694 vec_len (cm->cli_prompt));
697 /** @brief Output a pager prompt and show number of buffered lines */
699 unix_cli_pager_prompt (unix_cli_file_t * cf, clib_file_t * uf)
704 h = cf->pager_start + (cf->height - 1);
705 if (h > vec_len (cf->pager_index))
706 h = vec_len (cf->pager_index);
708 prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s",
709 cf->ansi_capable ? ANSI_BOLD : "",
712 vec_len (cf->pager_index),
713 cf->ansi_capable ? ANSI_RESET : "");
715 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
720 /** @brief Output a pager "skipping" message */
722 unix_cli_pager_message (unix_cli_file_t * cf, clib_file_t * uf,
723 char *message, char *postfix)
727 prompt = format (0, "\r%s-- %s --%s%s",
728 cf->ansi_capable ? ANSI_BOLD : "",
729 message, cf->ansi_capable ? ANSI_RESET : "", postfix);
731 unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
736 /** @brief Erase the printed pager prompt */
738 unix_cli_pager_prompt_erase (unix_cli_file_t * cf, clib_file_t * uf)
740 if (cf->ansi_capable)
742 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
743 unix_vlib_cli_output_cooked (cf, uf,
744 (u8 *) ANSI_CLEARLINE,
745 sizeof (ANSI_CLEARLINE) - 1);
751 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
752 for (i = 0; i < cf->width - 1; i++)
753 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
754 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
758 /** @brief Uses an ANSI escape sequence to move the cursor */
760 unix_cli_ansi_cursor (unix_cli_file_t * cf, clib_file_t * uf, u16 x, u16 y)
764 str = format (0, "%s%d;%dH", CSI, y, x);
766 unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str));
771 /** Redraw the currently displayed page of text.
772 * @param cf CLI session to redraw the pager buffer of.
773 * @param uf Unix file of the CLI session.
776 unix_cli_pager_redraw (unix_cli_file_t * cf, clib_file_t * uf)
778 unix_cli_pager_index_t *pi = NULL;
782 /* No active pager? Do nothing. */
783 if (!vec_len (cf->pager_index))
786 if (cf->ansi_capable)
788 /* If we have ANSI, send the clear screen sequence */
789 unix_vlib_cli_output_cooked (cf, uf,
791 sizeof (ANSI_CLEAR) - 1);
795 /* Otherwise make sure we're on a blank line */
796 unix_cli_pager_prompt_erase (cf, uf);
799 /* (Re-)send the current page of content */
800 for (i = 0; i < cf->height - 1 &&
801 i + cf->pager_start < vec_len (cf->pager_index); i++)
803 pi = &cf->pager_index[cf->pager_start + i];
804 line = cf->pager_vector[pi->line] + pi->offset;
806 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
808 /* if the last line didn't end in newline, add a newline */
809 if (pi && line[pi->length - 1] != '\n')
810 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
812 unix_cli_pager_prompt (cf, uf);
815 /** @brief Process and add a line to the pager index.
816 * In normal operation this function will take the given character string
817 * found in @c line and with length @c len_or_index and iterates the over the
818 * contents, adding each line of text discovered within it to the
819 * pager index. Lines are identified by newlines ("<code>\\n</code>") and by
820 * strings longer than the width of the terminal.
822 * If instead @c line is @c NULL then @c len_or_index is taken to mean the
823 * index of an existing line in the pager buffer; this simply means that the
824 * input line does not need to be cloned since we alreayd have it. This is
825 * typical if we are reindexing the pager buffer.
827 * @param cf The CLI session whose pager we are adding to.
828 * @param line The string of text to be indexed into the pager buffer.
829 * If @c line is @c NULL then the mode of operation
830 * changes slightly; see the description above.
831 * @param len_or_index If @c line is a pointer to a string then this parameter
832 * indicates the length of that string; Otherwise this
833 * value provides the index in the pager buffer of an
834 * existing string to be indexed.
837 unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index)
841 word line_index, len;
842 u32 width = cf->width;
843 unix_cli_pager_index_t *pi;
847 /* Use a line already in the pager buffer */
848 line_index = len_or_index;
849 p = cf->pager_vector[line_index];
855 /* Add a copy of the raw string to the pager buffer */
856 p = vec_new (u8, len);
857 clib_memcpy (p, line, len);
859 /* store in pager buffer */
860 line_index = vec_len (cf->pager_vector);
861 vec_add1 (cf->pager_vector, p);
867 /* Find the next line, or run to terminal width, or run to EOL */
869 j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width);
871 if (j < l && p[j] == '\n') /* incl \n */
874 /* Add the line to the index */
875 k = vec_len (cf->pager_index);
876 vec_validate (cf->pager_index, k);
877 pi = &cf->pager_index[k];
879 pi->line = line_index;
888 /** @brief Reindex entire pager buffer.
889 * Resets the current pager index and then re-adds the lines in the pager
890 * buffer to the index.
892 * Additionally this function attempts to retain the current page start
893 * line offset by searching for the same top-of-screen line in the new index.
895 * @param cf The CLI session whose pager buffer should be reindexed.
898 unix_cli_pager_reindex (unix_cli_file_t * cf)
900 word i, old_line, old_offset;
901 unix_cli_pager_index_t *pi;
903 /* If there is nothing in the pager buffer then make sure the index
904 * is empty and move on.
906 if (cf->pager_vector == 0)
908 vec_reset_length (cf->pager_index);
912 /* Retain a pointer to the current page start line so we can
915 pi = &cf->pager_index[cf->pager_start];
917 old_offset = pi->offset;
919 /* Re-add the buffered lines to the index */
920 vec_reset_length (cf->pager_index);
921 vec_foreach_index (i, cf->pager_vector)
923 unix_cli_pager_add_line (cf, NULL, i);
926 /* Attempt to re-locate the previously stored page start line */
927 vec_foreach_index (i, cf->pager_index)
929 pi = &cf->pager_index[i];
931 if (pi->line == old_line &&
932 (pi->offset <= old_offset || pi->offset + pi->length > old_offset))
940 /* In case the start line was not found (rare), ensure the pager start
941 * index is within bounds
943 if (cf->pager_start >= vec_len (cf->pager_index))
945 if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1))
948 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
952 /** VLIB CLI output function.
954 * If the terminal has a pager configured then this function takes care
955 * of collating output into the pager buffer; ensuring only the first page
956 * is displayed and any lines in excess of the first page are buffered.
958 * If the maximum number of index lines in the buffer is exceeded then the
959 * pager is cancelled and the contents of the current buffer are sent to the
962 * If there is no pager configured then the output is sent directly to the
965 * @param cli_file_index Index of the CLI session where this output is
967 * @param buffer String of printabe bytes to be output.
968 * @param buffer_bytes The number of bytes in @c buffer to be output.
971 unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes)
973 unix_main_t *um = &unix_main;
974 clib_file_main_t *fm = &file_main;
975 unix_cli_main_t *cm = &unix_cli_main;
979 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
980 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
982 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
984 unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes);
988 word row = vec_len (cf->pager_index);
990 unix_cli_pager_index_t *pi;
992 /* Index and add the output lines to the pager buffer. */
993 unix_cli_pager_add_line (cf, buffer, buffer_bytes);
995 /* Now iterate what was added to display the lines.
996 * If we reach the bottom of the page, display a prompt.
998 while (row < vec_len (cf->pager_index))
1000 if (row < cf->height - 1)
1002 /* output this line */
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);
1007 /* if the last line didn't end in newline, and we're at the
1008 * bottom of the page, add a newline */
1009 if (line[pi->length - 1] != '\n' && row == cf->height - 2)
1010 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1014 /* Display the pager prompt every 10 lines */
1016 unix_cli_pager_prompt (cf, uf);
1021 /* Check if we went over the pager buffer limit */
1022 if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit)
1024 /* Stop using the pager for the remainder of this CLI command */
1027 /* If we likely printed the prompt, erase it */
1028 if (vec_len (cf->pager_index) > cf->height - 1)
1029 unix_cli_pager_prompt_erase (cf, uf);
1031 /* Dump out the contents of the buffer */
1032 for (row = cf->pager_start + (cf->height - 1);
1033 row < vec_len (cf->pager_index); row++)
1035 pi = &cf->pager_index[row];
1036 line = cf->pager_vector[pi->line] + pi->offset;
1037 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1040 unix_cli_pager_reset (cf);
1045 /** Identify whether a terminal type is ANSI capable.
1047 * Compares the string given in @c term with a list of terminal types known
1048 * to support ANSI escape sequences.
1050 * This list contains, for example, @c xterm, @c screen and @c ansi.
1052 * @param term A string with a terminal type in it.
1053 * @param len The length of the string in @c term.
1055 * @return @c 1 if the terminal type is recognized as supporting ANSI
1056 * terminal sequences; @c 0 otherwise.
1059 unix_cli_terminal_type_ansi (u8 * term, uword len)
1061 /* This may later be better done as a hash of some sort. */
1063 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1068 _("xterm-256color"); /* iTerm on Mac */
1070 _("screen-256color"); /* Screen and tmux */
1071 _("ansi"); /* Microsoft Telnet */
1077 /** Identify whether a terminal type is non-interactive.
1079 * Compares the string given in @c term with a list of terminal types known
1080 * to be non-interactive, as send by tools such as @c vppctl .
1082 * This list contains, for example, @c vppctl.
1084 * @param term A string with a terminal type in it.
1085 * @param len The length of the string in @c term.
1087 * @return @c 1 if the terminal type is recognized as being non-interactive;
1091 unix_cli_terminal_type_noninteractive (u8 * term, uword len)
1093 /* This may later be better done as a hash of some sort. */
1095 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1104 /** Set a session to be non-interactive. */
1106 unix_cli_set_session_noninteractive (unix_cli_file_t * cf)
1108 /* Non-interactive sessions don't get these */
1109 cf->is_interactive = 0;
1111 cf->history_limit = 0;
1112 cf->has_history = 0;
1116 /** @brief Emit initial welcome banner and prompt on a connection. */
1118 unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf)
1120 unix_main_t *um = &unix_main;
1121 clib_file_main_t *fm = &file_main;
1122 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
1123 unix_cli_banner_t *banner;
1126 /* Mark the session as started if we get here */
1129 if (!(cf->is_interactive)) /* No banner for non-interactive sessions */
1133 * Put the first bytes directly into the buffer so that further output is
1134 * queued until everything is ready. (oterwise initial prompt can appear
1135 * mid way through VPP initialization)
1137 unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
1139 if (!um->cli_no_banner)
1141 if (cf->ansi_capable)
1143 banner = unix_cli_banner_color;
1144 len = ARRAY_LEN (unix_cli_banner_color);
1148 banner = unix_cli_banner;
1149 len = ARRAY_LEN (unix_cli_banner);
1152 for (i = 0; i < len; i++)
1154 unix_vlib_cli_output_cooked (cf, uf,
1155 banner[i].line, banner[i].length);
1160 unix_cli_cli_prompt (cf, uf);
1164 /** @brief A failsafe triggered on a timer to ensure we send the prompt
1165 * to telnet sessions that fail to negotiate the terminal type. */
1167 unix_cli_file_welcome_timer (any arg, f64 delay)
1169 unix_cli_main_t *cm = &unix_cli_main;
1170 unix_cli_file_t *cf;
1173 /* Check the connection didn't close already */
1174 if (pool_is_free_index (cm->cli_file_pool, (uword) arg))
1177 cf = pool_elt_at_index (cm->cli_file_pool, (uword) arg);
1180 unix_cli_file_welcome (cm, cf);
1183 /** @brief A mostly no-op Telnet state machine.
1184 * Process Telnet command bytes in a way that ensures we're mostly
1185 * transparent to the Telnet protocol. That is, it's mostly a no-op.
1187 * @return -1 if we need more bytes, otherwise a positive integer number of
1188 * bytes to consume from the input_vector, not including the initial
1192 unix_cli_process_telnet (unix_main_t * um,
1193 unix_cli_file_t * cf,
1194 clib_file_t * uf, u8 * input_vector, uword len)
1196 /* Input_vector starts at IAC byte.
1197 * See if we have a complete message; if not, return -1 so we wait for more.
1198 * if we have a complete message, consume those bytes from the vector.
1203 return -1; /* want more bytes */
1205 switch (input_vector[1])
1208 /* two IAC's in a row means to pass through 0xff.
1209 * since that makes no sense here, just consume it.
1218 /* Expect 3 bytes */
1219 if (vec_len (input_vector) < 3)
1220 return -1; /* want more bytes */
1227 /* Sub option - search ahead for IAC SE to end it */
1229 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
1231 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
1233 /* We have a complete message; see if we care about it */
1234 switch (input_vector[2])
1237 if (input_vector[3] != 0)
1240 /* See if the the terminal type is recognized */
1241 u8 *term = input_vector + 4;
1244 /* See if the terminal type is ANSI capable */
1246 unix_cli_terminal_type_ansi (term, len);
1248 /* See if the terminal type indicates non-interactive */
1249 if (unix_cli_terminal_type_noninteractive (term, len))
1250 unix_cli_set_session_noninteractive (cf);
1253 /* If session not started, we can release the pause */
1255 /* Send the welcome banner and initial prompt */
1256 unix_cli_file_welcome (&unix_cli_main, cf);
1261 if (i != 8) /* check message is correct size */
1265 clib_net_to_host_u16 (*((u16 *) (input_vector + 3)));
1266 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
1267 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
1268 if (cf->width < UNIX_CLI_MIN_TERMINAL_WIDTH)
1269 cf->width = UNIX_CLI_MIN_TERMINAL_WIDTH;
1272 clib_net_to_host_u16 (*((u16 *) (input_vector + 5)));
1273 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
1274 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
1275 if (cf->height < UNIX_CLI_MIN_TERMINAL_HEIGHT)
1276 cf->height = UNIX_CLI_MIN_TERMINAL_HEIGHT;
1278 /* reindex pager buffer */
1279 unix_cli_pager_reindex (cf);
1281 unix_cli_pager_redraw (cf, uf);
1287 /* Consume it all */
1293 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
1294 consume = 1; /* hit max search depth, advance one byte */
1297 return -1; /* want more bytes */
1315 /* Simple one-byte messages */
1320 /* Are You There - trigger a visible response */
1322 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
1326 /* Unknown command! Eat the IAC byte */
1333 /** @brief Process actionable input.
1334 * Based on the \c action process the input; this typically involves
1335 * searching the command history or editing the current command line.
1338 unix_cli_line_process_one (unix_cli_main_t * cm,
1340 unix_cli_file_t * cf,
1342 u8 input, unix_cli_parse_action_t action)
1346 u8 **possible_commands;
1351 case UNIX_CLI_PARSE_ACTION_NOACTION:
1354 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
1355 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
1356 if (!cf->has_history || !cf->history_limit)
1358 if (cf->search_mode == 0)
1360 /* Erase the current command (if any) */
1361 for (j = 0; j < (vec_len (cf->current_command)); j++)
1362 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1364 vec_reset_length (cf->search_key);
1365 vec_reset_length (cf->current_command);
1366 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1367 cf->search_mode = -1;
1369 cf->search_mode = 1;
1374 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1375 cf->search_mode = -1;
1377 cf->search_mode = 1;
1379 cf->excursion += cf->search_mode;
1384 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
1385 /* Erase the command from the cursor to the start */
1387 /* Shimmy forwards to the new end of line position */
1388 delta = vec_len (cf->current_command) - cf->cursor;
1389 for (j = cf->cursor; j > delta; j--)
1390 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1391 /* Zap from here to the end of what is currently displayed */
1392 for (; j < (vec_len (cf->current_command)); j++)
1393 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1394 /* Get back to the start of the line */
1395 for (j = 0; j < (vec_len (cf->current_command)); j++)
1396 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1398 j = vec_len (cf->current_command) - cf->cursor;
1399 memmove (cf->current_command, cf->current_command + cf->cursor, j);
1400 _vec_len (cf->current_command) = j;
1402 /* Print the new contents */
1403 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j);
1404 /* Shimmy back to the start */
1405 for (j = 0; j < (vec_len (cf->current_command)); j++)
1406 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1409 cf->search_mode = 0;
1412 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
1413 /* Erase the command from the cursor to the end */
1415 /* Zap from cursor to end of what is currently displayed */
1416 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1417 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1418 /* Get back to where we were */
1419 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1420 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1422 /* Truncate the line at the cursor */
1423 _vec_len (cf->current_command) = cf->cursor;
1425 cf->search_mode = 0;
1428 case UNIX_CLI_PARSE_ACTION_LEFT:
1431 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1435 cf->search_mode = 0;
1438 case UNIX_CLI_PARSE_ACTION_RIGHT:
1439 if (cf->cursor < vec_len (cf->current_command))
1441 /* have to emit the character under the cursor */
1442 unix_vlib_cli_output_cooked (cf, uf,
1443 cf->current_command + cf->cursor, 1);
1447 cf->search_mode = 0;
1450 case UNIX_CLI_PARSE_ACTION_UP:
1451 case UNIX_CLI_PARSE_ACTION_DOWN:
1452 if (!cf->has_history || !cf->history_limit)
1454 cf->search_mode = 0;
1455 /* Erase the command */
1456 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1457 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1458 for (j = 0; j < (vec_len (cf->current_command)); j++)
1459 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1460 vec_reset_length (cf->current_command);
1461 if (vec_len (cf->command_history))
1463 if (action == UNIX_CLI_PARSE_ACTION_UP)
1468 cf->excursion += delta;
1470 if (cf->excursion == vec_len (cf->command_history))
1472 /* down-arrowed to last entry - want a blank line */
1473 _vec_len (cf->current_command) = 0;
1475 else if (cf->excursion < 0)
1477 /* up-arrowed over the start to the end, want a blank line */
1478 cf->excursion = vec_len (cf->command_history);
1479 _vec_len (cf->current_command) = 0;
1483 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1484 /* down-arrowed past end - wrap to start */
1487 /* Print the command at the current position */
1488 prev = cf->command_history[cf->excursion];
1489 vec_validate (cf->current_command, vec_len (prev) - 1);
1491 clib_memcpy (cf->current_command, prev, vec_len (prev));
1492 _vec_len (cf->current_command) = vec_len (prev);
1493 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1494 vec_len (cf->current_command));
1497 cf->cursor = vec_len (cf->current_command);
1500 case UNIX_CLI_PARSE_ACTION_HOME:
1501 if (vec_len (cf->current_command) && cf->cursor > 0)
1505 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1510 cf->search_mode = 0;
1513 case UNIX_CLI_PARSE_ACTION_END:
1514 if (vec_len (cf->current_command) &&
1515 cf->cursor < vec_len (cf->current_command))
1517 unix_vlib_cli_output_cooked (cf, uf,
1518 cf->current_command + cf->cursor,
1519 vec_len (cf->current_command) -
1521 cf->cursor = vec_len (cf->current_command);
1524 cf->search_mode = 0;
1527 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1528 if (vec_len (cf->current_command) && cf->cursor > 0)
1532 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1535 while (j && isspace (cf->current_command[j]))
1537 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1540 while (j && !isspace (cf->current_command[j]))
1542 if (isspace (cf->current_command[j - 1]))
1544 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1551 cf->search_mode = 0;
1554 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1555 if (vec_len (cf->current_command) &&
1556 cf->cursor < vec_len (cf->current_command))
1558 int e = vec_len (cf->current_command);
1560 while (j < e && !isspace (cf->current_command[j]))
1562 while (j < e && isspace (cf->current_command[j]))
1564 unix_vlib_cli_output_cooked (cf, uf,
1565 cf->current_command + cf->cursor,
1570 cf->search_mode = 0;
1574 case UNIX_CLI_PARSE_ACTION_ERASE:
1575 if (vec_len (cf->current_command))
1577 if (cf->cursor == vec_len (cf->current_command))
1579 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1580 _vec_len (cf->current_command)--;
1583 else if (cf->cursor > 0)
1585 /* shift everything at & to the right of the cursor left by 1 */
1586 j = vec_len (cf->current_command) - cf->cursor;
1587 memmove (cf->current_command + cf->cursor - 1,
1588 cf->current_command + cf->cursor, j);
1589 _vec_len (cf->current_command)--;
1591 /* redraw the rest of the line */
1592 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1593 unix_vlib_cli_output_cooked (cf, uf,
1594 cf->current_command + cf->cursor,
1596 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
1597 /* and shift the terminal cursor back where it should be */
1599 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1602 cf->search_mode = 0;
1604 vec_reset_length (cf->search_key);
1607 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1608 if (vec_len (cf->current_command))
1610 if (cf->cursor < vec_len (cf->current_command))
1612 /* shift everything to the right of the cursor left by 1 */
1613 j = vec_len (cf->current_command) - cf->cursor - 1;
1614 memmove (cf->current_command + cf->cursor,
1615 cf->current_command + cf->cursor + 1, j);
1616 _vec_len (cf->current_command)--;
1617 /* redraw the rest of the line */
1618 unix_vlib_cli_output_cooked (cf, uf,
1619 cf->current_command + cf->cursor,
1621 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
1622 /* and shift the terminal cursor back where it should be */
1625 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1627 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1631 else if (input == 'D' - '@')
1633 /* ^D with no command entered = quit */
1634 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1635 vlib_process_signal_event (um->vlib_main,
1636 vlib_current_process (um->vlib_main),
1637 UNIX_CLI_PROCESS_EVENT_QUIT,
1638 cf - cm->cli_file_pool);
1640 cf->search_mode = 0;
1642 vec_reset_length (cf->search_key);
1645 case UNIX_CLI_PARSE_ACTION_CLEAR:
1646 /* If we're in ANSI mode, clear the screen.
1647 * Then redraw the prompt and any existing command input, then put
1648 * the cursor back where it was in that line.
1650 if (cf->ansi_capable)
1651 unix_vlib_cli_output_cooked (cf, uf,
1653 sizeof (ANSI_CLEAR) - 1);
1655 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1657 unix_vlib_cli_output_raw (cf, uf,
1658 cm->cli_prompt, vec_len (cm->cli_prompt));
1659 unix_vlib_cli_output_raw (cf, uf,
1660 cf->current_command,
1661 vec_len (cf->current_command));
1662 for (j = cf->cursor; j < vec_len (cf->current_command); j++)
1663 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1667 case UNIX_CLI_PARSE_ACTION_TAB:
1668 if (cf->cursor < vec_len (cf->current_command))
1670 /* if we are in the middle of a line, complete only if
1671 * the cursor points to whitespace */
1672 if (isspace (cf->current_command[cf->cursor]))
1674 /* save and clear any input that is after the cursor */
1675 vec_resize (save, vec_len (cf->current_command) - cf->cursor);
1676 clib_memcpy (save, cf->current_command + cf->cursor,
1677 vec_len (cf->current_command) - cf->cursor);
1678 _vec_len (cf->current_command) = cf->cursor;
1682 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1687 vlib_cli_get_possible_completions (cf->current_command);
1688 if (vec_len (possible_commands) == 1)
1691 u8 *completed = possible_commands[0];
1693 /* find the last word of current_command */
1694 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1697 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1699 _vec_len (cf->current_command) = j;
1701 /* replace it with the newly expanded command */
1702 vec_append (cf->current_command, completed);
1704 /* echo to the terminal */
1705 unix_vlib_cli_output_raw (cf, uf, completed, vec_len (completed));
1707 /* add one trailing space if needed */
1708 if (vec_len (save) == 0)
1710 vec_add1 (cf->current_command, ' ');
1711 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1714 cf->cursor = vec_len (cf->current_command);
1717 else if (vec_len (possible_commands) >= 2)
1719 u8 **possible_command;
1720 uword max_command_len = 0, min_command_len = ~0;
1723 vec_foreach (possible_command, possible_commands)
1725 if (vec_len (*possible_command) > max_command_len)
1727 max_command_len = vec_len (*possible_command);
1729 if (vec_len (*possible_command) < min_command_len)
1731 min_command_len = vec_len (*possible_command);
1735 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1738 vec_foreach (possible_command, possible_commands)
1740 if (i + max_command_len >= cf->width)
1742 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1745 unix_vlib_cli_output_raw (cf, uf, *possible_command,
1746 vec_len (*possible_command));
1747 for (j = vec_len (*possible_command); j < max_command_len + 2;
1750 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1752 i += max_command_len + 2;
1755 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1757 /* rewrite prompt */
1758 unix_cli_cli_prompt (cf, uf);
1759 unix_vlib_cli_output_raw (cf, uf, cf->current_command,
1760 vec_len (cf->current_command));
1762 /* count length of last word */
1765 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1771 /* determine smallest common command */
1772 for (; i < min_command_len; i++)
1776 vec_foreach (possible_command, possible_commands)
1780 common = (*possible_command)[i];
1782 else if (common != (*possible_command)[i])
1790 vec_add1 (cf->current_command, common);
1792 unix_vlib_cli_output_raw (cf, uf, (u8 *) & common, 1);
1802 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1805 if (vec_len (save) > 0)
1807 /* restore remaining input if tab was hit in the middle of a line */
1808 unix_vlib_cli_output_raw (cf, uf, save, vec_len (save));
1809 for (j = 0; j < vec_len (save); j++)
1811 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1813 vec_append (cf->current_command, save);
1816 vec_free (possible_commands);
1819 case UNIX_CLI_PARSE_ACTION_YANK:
1824 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
1826 unix_cli_pager_prompt_erase (cf, uf);
1827 unix_cli_pager_reset (cf);
1828 unix_cli_cli_prompt (cf, uf);
1831 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
1832 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
1833 /* show next page of the buffer */
1834 if (cf->height + cf->pager_start < vec_len (cf->pager_index))
1837 unix_cli_pager_index_t *pi = NULL;
1839 int m = cf->pager_start + (cf->height - 1);
1840 unix_cli_pager_prompt_erase (cf, uf);
1842 j < vec_len (cf->pager_index) && cf->pager_start < m;
1843 j++, cf->pager_start++)
1845 pi = &cf->pager_index[j];
1846 line = cf->pager_vector[pi->line] + pi->offset;
1847 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1849 /* if the last line didn't end in newline, add a newline */
1850 if (pi && line[pi->length - 1] != '\n')
1851 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1852 unix_cli_pager_prompt (cf, uf);
1856 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
1857 /* no more in buffer, exit, but only if it was <space> */
1862 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
1863 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
1864 /* display the next line of the buffer */
1865 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
1868 unix_cli_pager_index_t *pi;
1870 unix_cli_pager_prompt_erase (cf, uf);
1871 pi = &cf->pager_index[cf->pager_start + (cf->height - 1)];
1872 line = cf->pager_vector[pi->line] + pi->offset;
1873 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1875 /* if the last line didn't end in newline, add a newline */
1876 if (line[pi->length - 1] != '\n')
1877 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1878 unix_cli_pager_prompt (cf, uf);
1882 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
1883 /* no more in buffer, exit, but only if it was <enter> */
1889 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
1890 /* scroll the page back one line */
1891 if (cf->pager_start > 0)
1894 unix_cli_pager_index_t *pi = NULL;
1897 if (cf->ansi_capable)
1899 pi = &cf->pager_index[cf->pager_start];
1900 line = cf->pager_vector[pi->line] + pi->offset;
1901 unix_cli_pager_prompt_erase (cf, uf);
1902 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN,
1903 sizeof (ANSI_SCROLLDN) - 1);
1904 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR,
1905 sizeof (ANSI_SAVECURSOR) - 1);
1906 unix_cli_ansi_cursor (cf, uf, 1, 1);
1907 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE,
1908 sizeof (ANSI_CLEARLINE) - 1);
1909 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1910 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR,
1911 sizeof (ANSI_RESTCURSOR) - 1);
1912 unix_cli_pager_prompt_erase (cf, uf);
1913 unix_cli_pager_prompt (cf, uf);
1917 int m = cf->pager_start + (cf->height - 1);
1918 unix_cli_pager_prompt_erase (cf, uf);
1919 for (j = cf->pager_start;
1920 j < vec_len (cf->pager_index) && j < m; j++)
1922 pi = &cf->pager_index[j];
1923 line = cf->pager_vector[pi->line] + pi->offset;
1924 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1926 /* if the last line didn't end in newline, add a newline */
1927 if (pi && line[pi->length - 1] != '\n')
1928 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1929 unix_cli_pager_prompt (cf, uf);
1934 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
1935 /* back to the first page of the buffer */
1936 if (cf->pager_start > 0)
1939 unix_cli_pager_index_t *pi = NULL;
1941 cf->pager_start = 0;
1942 int m = cf->pager_start + (cf->height - 1);
1943 unix_cli_pager_prompt_erase (cf, uf);
1944 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1947 pi = &cf->pager_index[j];
1948 line = cf->pager_vector[pi->line] + pi->offset;
1949 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1951 /* if the last line didn't end in newline, add a newline */
1952 if (pi && line[pi->length - 1] != '\n')
1953 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1954 unix_cli_pager_prompt (cf, uf);
1958 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
1959 /* skip to the last page of the buffer */
1960 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
1963 unix_cli_pager_index_t *pi = NULL;
1965 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
1966 unix_cli_pager_prompt_erase (cf, uf);
1967 unix_cli_pager_message (cf, uf, "skipping", "\n");
1968 for (j = cf->pager_start; j < vec_len (cf->pager_index); j++)
1970 pi = &cf->pager_index[j];
1971 line = cf->pager_vector[pi->line] + pi->offset;
1972 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1974 /* if the last line didn't end in newline, add a newline */
1975 if (pi && line[pi->length - 1] != '\n')
1976 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1977 unix_cli_pager_prompt (cf, uf);
1981 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
1982 /* wander back one page in the buffer */
1983 if (cf->pager_start > 0)
1986 unix_cli_pager_index_t *pi = NULL;
1989 if (cf->pager_start >= cf->height)
1990 cf->pager_start -= cf->height - 1;
1992 cf->pager_start = 0;
1993 m = cf->pager_start + cf->height - 1;
1994 unix_cli_pager_prompt_erase (cf, uf);
1995 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1998 pi = &cf->pager_index[j];
1999 line = cf->pager_vector[pi->line] + pi->offset;
2000 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2002 /* if the last line didn't end in newline, add a newline */
2003 if (pi && line[pi->length - 1] != '\n')
2004 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2005 unix_cli_pager_prompt (cf, uf);
2009 case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW:
2010 /* Redraw the current pager screen */
2011 unix_cli_pager_redraw (cf, uf);
2014 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
2015 /* search forwards in the buffer */
2019 case UNIX_CLI_PARSE_ACTION_CRLF:
2021 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2023 if (cf->has_history && cf->history_limit)
2025 if (cf->command_history
2026 && vec_len (cf->command_history) >= cf->history_limit)
2028 vec_free (cf->command_history[0]);
2029 vec_delete (cf->command_history, 1, 0);
2031 /* Don't add blank lines to the cmd history */
2032 if (vec_len (cf->current_command))
2034 /* Don't duplicate the previous command */
2035 j = vec_len (cf->command_history);
2037 (vec_len (cf->current_command) !=
2038 vec_len (cf->command_history[j - 1])
2039 || memcmp (cf->current_command, cf->command_history[j - 1],
2040 vec_len (cf->current_command)) != 0))
2042 /* copy the command to the history */
2044 vec_append (c, cf->current_command);
2045 vec_add1 (cf->command_history, c);
2046 cf->command_number++;
2049 cf->excursion = vec_len (cf->command_history);
2052 cf->search_mode = 0;
2053 vec_reset_length (cf->search_key);
2058 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2059 case UNIX_CLI_PARSE_ACTION_NOMATCH:
2060 if (vec_len (cf->pager_index))
2064 else if (cf->has_history && cf->search_mode && isprint (input))
2066 int k, limit, offset;
2069 vec_add1 (cf->search_key, input);
2072 for (j = 0; j < vec_len (cf->command_history); j++)
2074 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
2076 else if (cf->excursion < 0)
2077 cf->excursion = vec_len (cf->command_history) - 1;
2079 item = cf->command_history[cf->excursion];
2081 limit = (vec_len (cf->search_key) > vec_len (item)) ?
2082 vec_len (item) : vec_len (cf->search_key);
2084 for (offset = 0; offset <= vec_len (item) - limit; offset++)
2086 for (k = 0; k < limit; k++)
2088 if (item[k + offset] != cf->search_key[k])
2091 goto found_at_offset;
2099 for (j = 0; j < vec_len (cf->current_command); j++)
2100 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
2102 vec_validate (cf->current_command, vec_len (item) - 1);
2103 clib_memcpy (cf->current_command, item, vec_len (item));
2104 _vec_len (cf->current_command) = vec_len (item);
2106 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
2107 vec_len (cf->current_command));
2108 cf->cursor = vec_len (cf->current_command);
2112 cf->excursion += cf->search_mode;
2115 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12);
2116 vec_reset_length (cf->search_key);
2117 vec_reset_length (cf->current_command);
2118 cf->search_mode = 0;
2122 else if (isprint (input)) /* skip any errant control codes */
2124 if (cf->cursor == vec_len (cf->current_command))
2127 vec_add1 (cf->current_command, input);
2130 /* Echo the character back to the client */
2131 unix_vlib_cli_output_raw (cf, uf, &input, 1);
2135 /* Insert at cursor: resize +1 byte, move everything over */
2136 j = vec_len (cf->current_command) - cf->cursor;
2137 vec_add1 (cf->current_command, (u8) 'A');
2138 memmove (cf->current_command + cf->cursor + 1,
2139 cf->current_command + cf->cursor, j);
2140 cf->current_command[cf->cursor] = input;
2141 /* Redraw the line */
2143 unix_vlib_cli_output_raw (cf, uf,
2144 cf->current_command + cf->cursor, j);
2145 /* Put terminal cursor back */
2147 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
2153 /* no-op - not printable or otherwise not actionable */
2160 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2166 /** @brief Process input bytes on a stream to provide line editing and
2167 * command history in the CLI. */
2169 unix_cli_line_edit (unix_cli_main_t * cm, unix_main_t * um,
2170 clib_file_main_t * fm, unix_cli_file_t * cf)
2172 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2175 for (i = 0; i < vec_len (cf->input_vector); i++)
2177 unix_cli_parse_action_t action;
2179 unix_cli_parse_actions_t *a;
2181 /* If we're in the pager mode, search the pager actions */
2183 vec_len (cf->pager_index) ? unix_cli_parse_pager :
2184 unix_cli_parse_strings;
2186 /* See if the input buffer is some sort of control code */
2187 action = unix_cli_match_action (a, &cf->input_vector[i],
2188 vec_len (cf->input_vector) - i,
2193 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2196 /* There was a partial match which means we need more bytes
2197 * than the input buffer currently has.
2198 * Since the bytes before here have been processed, shift
2199 * the remaining contents to the start of the input buffer.
2201 vec_delete (cf->input_vector, i, 0);
2203 return 1; /* wait for more */
2205 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2206 /* process telnet options */
2207 matched = unix_cli_process_telnet (um, cf, uf,
2208 cf->input_vector + i,
2209 vec_len (cf->input_vector) - i);
2212 /* There was a partial match which means we need more bytes
2213 * than the input buffer currently has.
2218 * Since the bytes before here have been processed, shift
2219 * the remaining contents to the start of the input buffer.
2221 vec_delete (cf->input_vector, i, 0);
2223 return 1; /* wait for more */
2228 /* If telnet option processing switched us to line mode, get us
2233 vec_delete (cf->input_vector, i, 0);
2234 cf->current_command = cf->input_vector;
2238 /* process the action */
2239 if (!unix_cli_line_process_one (cm, um, cf, uf,
2240 cf->input_vector[i], action))
2242 /* CRLF found. Consume the bytes from the input_vector */
2243 vec_delete (cf->input_vector, i + matched, 0);
2244 /* And tell our caller to execute cf->input_command */
2252 vec_reset_length (cf->input_vector);
2256 /** @brief Process input to a CLI session. */
2258 unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index)
2260 unix_main_t *um = &unix_main;
2261 clib_file_main_t *fm = &file_main;
2263 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2264 unformat_input_t input;
2265 int vlib_parse_eval (u8 *);
2267 cm->current_input_file_index = cli_file_index;
2270 /* Try vlibplex first. Someday... */
2271 if (0 && vlib_parse_eval (cf->input_vector) == 0)
2277 /* just treat whatever we got as a complete line of input */
2278 cf->current_command = cf->input_vector;
2282 /* Line edit, echo, etc. */
2283 if (unix_cli_line_edit (cm, um, fm, cf))
2284 /* want more input */
2291 vec_reset_length (lv);
2292 lv = format (lv, "%U[%d]: %v",
2293 format_timeval, 0 /* current bat-time */ ,
2294 0 /* current bat-format */ ,
2295 cli_file_index, cf->current_command);
2296 int rv __attribute__ ((unused)) = write (um->log_fd, lv, vec_len (lv));
2299 /* Build an unformat structure around our command */
2300 unformat_init_vector (&input, cf->current_command);
2302 /* Remove leading white space from input. */
2303 (void) unformat (&input, "");
2305 cf->pager_start = 0; /* start a new pager session */
2307 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
2308 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output,
2311 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
2314 unformat_free (&input);
2316 /* Re-fetch pointer since pool may have moved. */
2317 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2318 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2321 /* reset vector; we'll re-use it later */
2324 vec_reset_length (cf->input_vector);
2325 cf->current_command = 0;
2329 vec_reset_length (cf->current_command);
2332 if (cf->no_pager == 2)
2334 /* Pager was programmatically disabled */
2335 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
2336 cf->no_pager = um->cli_no_pager;
2339 if (vec_len (cf->pager_index) == 0
2340 || vec_len (cf->pager_index) < cf->height)
2342 /* There was no need for the pager */
2343 unix_cli_pager_reset (cf);
2346 unix_cli_cli_prompt (cf, uf);
2350 /* Display the pager prompt */
2351 unix_cli_pager_prompt (cf, uf);
2354 /* Any residual data in the input vector? */
2355 if (vec_len (cf->input_vector))
2358 /* For non-interactive sessions send a NUL byte.
2359 * Specifically this is because vppctl needs to see some traffic in
2360 * order to move on to closing the session. Commands with no output
2361 * would thus cause vppctl to hang indefinitely in non-interactive mode
2362 * since there is also no prompt sent after the command completes.
2364 if (!cf->is_interactive)
2365 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\0", 1);
2368 /** Destroy a CLI session.
2369 * @note If we destroy the @c stdin session this additionally signals
2370 * the shutdown of VPP.
2373 unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
2375 unix_main_t *um = &unix_main;
2376 clib_file_main_t *fm = &file_main;
2377 unix_cli_file_t *cf;
2381 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2382 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2384 /* Quit/EOF on stdin means quit program. */
2385 if (uf->file_descriptor == STDIN_FILENO)
2386 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
2388 vec_free (cf->current_command);
2389 vec_free (cf->search_key);
2391 for (i = 0; i < vec_len (cf->command_history); i++)
2392 vec_free (cf->command_history[i]);
2394 vec_free (cf->command_history);
2396 clib_file_del (fm, uf);
2398 unix_cli_file_free (cf);
2399 pool_put (cm->cli_file_pool, cf);
2402 /** Handle system events. */
2404 unix_cli_process (vlib_main_t * vm,
2405 vlib_node_runtime_t * rt, vlib_frame_t * f)
2407 unix_cli_main_t *cm = &unix_cli_main;
2412 unix_cli_process_event_type_t event_type;
2413 vlib_process_wait_for_event (vm);
2414 event_type = vlib_process_get_events (vm, &data);
2418 case UNIX_CLI_PROCESS_EVENT_READ_READY:
2419 for (i = 0; i < vec_len (data); i++)
2420 unix_cli_process_input (cm, data[i]);
2423 case UNIX_CLI_PROCESS_EVENT_QUIT:
2424 /* Kill this process. */
2425 for (i = 0; i < vec_len (data); i++)
2426 unix_cli_kill (cm, data[i]);
2431 _vec_len (data) = 0;
2437 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
2439 /* Add node index so we can re-use this process later. */
2440 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
2445 /** Called when a CLI session file descriptor can be written to without
2447 static clib_error_t *
2448 unix_cli_write_ready (clib_file_t * uf)
2450 unix_cli_main_t *cm = &unix_cli_main;
2451 unix_cli_file_t *cf;
2454 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2456 /* Flush output vector. */
2458 /* If it's a socket we use MSG_NOSIGNAL to prevent SIGPIPE */
2459 n = send (uf->file_descriptor,
2460 cf->output_vector, vec_len (cf->output_vector), MSG_NOSIGNAL);
2462 n = write (uf->file_descriptor,
2463 cf->output_vector, vec_len (cf->output_vector));
2465 if (n < 0 && errno != EAGAIN)
2469 /* connection closed on us */
2470 unix_main_t *um = &unix_main;
2472 vlib_process_signal_event (um->vlib_main, cf->process_node_index,
2473 UNIX_CLI_PROCESS_EVENT_QUIT,
2478 return clib_error_return_unix (0, "write");
2483 unix_cli_del_pending_output (uf, cf, n);
2485 return /* no error */ 0;
2488 /** Called when a CLI session file descriptor has data to be read. */
2489 static clib_error_t *
2490 unix_cli_read_ready (clib_file_t * uf)
2492 unix_main_t *um = &unix_main;
2493 unix_cli_main_t *cm = &unix_cli_main;
2494 unix_cli_file_t *cf;
2496 int n, n_read, n_try;
2498 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2503 l = vec_len (cf->input_vector);
2504 vec_resize (cf->input_vector, l + n_try);
2506 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
2509 if (n < 0 && errno != EAGAIN)
2510 return clib_error_return_unix (0, "read");
2512 n_read = n < 0 ? 0 : n;
2513 _vec_len (cf->input_vector) = l + n_read;
2517 vlib_process_signal_event (um->vlib_main,
2518 cf->process_node_index,
2520 ? UNIX_CLI_PROCESS_EVENT_QUIT
2521 : UNIX_CLI_PROCESS_EVENT_READ_READY),
2522 /* event data */ uf->private_data);
2524 return /* no error */ 0;
2527 /** Called when a CLI session file descriptor has an error condition. */
2528 static clib_error_t *
2529 unix_cli_error_detected (clib_file_t * uf)
2531 unix_main_t *um = &unix_main;
2532 unix_cli_main_t *cm = &unix_cli_main;
2533 unix_cli_file_t *cf;
2535 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2536 cf->has_epipe = 1; /* prevent writes while the close is pending */
2537 vlib_process_signal_event (um->vlib_main,
2538 cf->process_node_index,
2539 UNIX_CLI_PROCESS_EVENT_QUIT,
2540 /* event data */ uf->private_data);
2542 return /* no error */ 0;
2545 /** Store a new CLI session.
2546 * @param name The name of the session.
2547 * @param fd The file descriptor for the session I/O.
2548 * @return The session ID.
2551 unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
2553 unix_main_t *um = &unix_main;
2554 clib_file_main_t *fm = &file_main;
2555 unix_cli_file_t *cf;
2556 clib_file_t template = { 0 };
2557 vlib_main_t *vm = um->vlib_main;
2560 name = (char *) format (0, "unix-cli-%s", name);
2562 if (vec_len (cm->unused_cli_process_node_indices) > 0)
2564 uword l = vec_len (cm->unused_cli_process_node_indices);
2566 /* Find node and give it new name. */
2567 n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
2569 n->name = (u8 *) name;
2571 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
2573 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
2577 static vlib_node_registration_t r = {
2578 .function = unix_cli_process,
2579 .type = VLIB_NODE_TYPE_PROCESS,
2580 .process_log2_n_stack_bytes = 16,
2584 vlib_register_node (vm, &r);
2587 n = vlib_get_node (vm, r.index);
2590 pool_get (cm->cli_file_pool, cf);
2591 memset (cf, 0, sizeof (*cf));
2593 template.read_function = unix_cli_read_ready;
2594 template.write_function = unix_cli_write_ready;
2595 template.error_function = unix_cli_error_detected;
2596 template.file_descriptor = fd;
2597 template.private_data = cf - cm->cli_file_pool;
2599 cf->process_node_index = n->index;
2600 cf->clib_file_index = clib_file_add (fm, &template);
2601 cf->output_vector = 0;
2602 cf->input_vector = 0;
2604 vlib_start_process (vm, n->runtime_index);
2606 vlib_process_t *p = vlib_get_process_from_node (vm, n);
2607 p->output_function = unix_vlib_cli_output;
2608 p->output_function_arg = cf - cm->cli_file_pool;
2610 return cf - cm->cli_file_pool;
2613 /** Telnet listening socket has a new connection. */
2614 static clib_error_t *
2615 unix_cli_listen_read_ready (clib_file_t * uf)
2617 unix_main_t *um = &unix_main;
2618 clib_file_main_t *fm = &file_main;
2619 unix_cli_main_t *cm = &unix_cli_main;
2620 clib_socket_t *s = &um->cli_listen_socket;
2621 clib_socket_t client;
2623 clib_error_t *error;
2624 unix_cli_file_t *cf;
2627 error = clib_socket_accept (s, &client);
2631 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
2633 cf_index = unix_cli_file_add (cm, client_name, client.fd);
2634 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2637 /* No longer need CLIB version of socket. */
2638 clib_socket_free (&client);
2639 vec_free (client_name);
2641 /* if we're supposed to run telnet session in character mode (default) */
2642 if (um->cli_line_mode == 0)
2645 * Set telnet client character mode, echo on, suppress "go-ahead".
2646 * Technically these should be negotiated, but this works.
2648 u8 charmode_option[] = {
2649 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
2650 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
2651 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
2652 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
2653 IAC, WILL, TELOPT_ECHO, /* server will do echo */
2654 IAC, DONT, TELOPT_ECHO, /* client should not echo */
2655 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
2656 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
2657 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
2658 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
2661 /* Enable history on this CLI */
2662 cf->history_limit = um->cli_history_limit;
2663 cf->has_history = cf->history_limit != 0;
2665 /* This is an interactive session until we decide otherwise */
2666 cf->is_interactive = 1;
2668 /* Make sure this session is in line mode */
2674 /* Setup the pager */
2675 cf->no_pager = um->cli_no_pager;
2677 /* Send the telnet options */
2678 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2679 unix_vlib_cli_output_raw (cf, uf, charmode_option,
2680 ARRAY_LEN (charmode_option));
2682 /* In case the client doesn't negotiate terminal type, use
2683 * a timer to kick off the initial prompt. */
2684 timer_call (unix_cli_file_welcome_timer, cf_index, 1);
2690 /** The system terminal has informed us that the window size
2694 unix_cli_resize_interrupt (int signum)
2696 clib_file_main_t *fm = &file_main;
2697 unix_cli_main_t *cm = &unix_cli_main;
2698 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2699 cm->stdin_cli_file_index);
2700 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2704 /* Terminal resized, fetch the new size */
2705 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
2707 /* "Should never happen..." */
2708 clib_unix_warning ("TIOCGWINSZ");
2709 /* We can't trust ws.XXX... */
2713 cf->width = ws.ws_col;
2714 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
2715 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
2716 if (cf->width < UNIX_CLI_MIN_TERMINAL_WIDTH)
2717 cf->width = UNIX_CLI_MIN_TERMINAL_WIDTH;
2719 cf->height = ws.ws_row;
2720 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
2721 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
2722 if (cf->height < UNIX_CLI_MIN_TERMINAL_HEIGHT)
2723 cf->height = UNIX_CLI_MIN_TERMINAL_HEIGHT;
2725 /* Reindex the pager buffer */
2726 unix_cli_pager_reindex (cf);
2728 /* Redraw the page */
2729 unix_cli_pager_redraw (cf, uf);
2732 /** Handle configuration directives in the @em unix section. */
2733 static clib_error_t *
2734 unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
2736 unix_main_t *um = &unix_main;
2737 clib_file_main_t *fm = &file_main;
2738 unix_cli_main_t *cm = &unix_cli_main;
2740 clib_error_t *error = 0;
2741 unix_cli_file_t *cf;
2744 struct sigaction sa;
2748 /* We depend on unix flags being set. */
2749 if ((error = vlib_call_config_function (vm, unix_config)))
2752 if (um->flags & UNIX_FLAG_INTERACTIVE)
2754 /* Set stdin to be non-blocking. */
2755 if ((flags = fcntl (STDIN_FILENO, F_GETFL, 0)) < 0)
2757 (void) fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
2759 cf_index = unix_cli_file_add (cm, "stdin", STDIN_FILENO);
2760 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2761 cm->stdin_cli_file_index = cf_index;
2763 /* If stdin is a tty and we are using chacracter mode, enable
2764 * history on the CLI and set the tty line discipline accordingly. */
2765 if (isatty (STDIN_FILENO) && um->cli_line_mode == 0)
2767 /* Capture terminal resize events */
2768 memset (&sa, 0, sizeof (sa));
2769 sa.sa_handler = unix_cli_resize_interrupt;
2770 if (sigaction (SIGWINCH, &sa, 0) < 0)
2771 clib_panic ("sigaction");
2773 /* Retrieve the current terminal size */
2774 ioctl (STDIN_FILENO, TIOCGWINSZ, &ws);
2775 cf->width = ws.ws_col;
2776 cf->height = ws.ws_row;
2778 if (cf->width == 0 || cf->height == 0)
2779 /* We have a tty, but no size. Stick to line mode. */
2782 /* Setup the history */
2783 cf->history_limit = um->cli_history_limit;
2784 cf->has_history = cf->history_limit != 0;
2786 /* Setup the pager */
2787 cf->no_pager = um->cli_no_pager;
2789 /* This is an interactive session until we decide otherwise */
2790 cf->is_interactive = 1;
2792 /* We're going to be in char by char mode */
2795 /* Save the original tty state so we can restore it later */
2796 tcgetattr (STDIN_FILENO, &um->tio_stdin);
2799 /* Tweak the tty settings */
2800 tio = um->tio_stdin;
2801 /* echo off, canonical mode off, ext'd input processing off */
2802 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
2803 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
2804 tio.c_cc[VTIME] = 0; /* no timer */
2805 tcsetattr (STDIN_FILENO, TCSAFLUSH, &tio);
2807 /* See if we can do ANSI/VT100 output */
2808 term = (u8 *) getenv ("TERM");
2811 int len = strlen ((char *) term);
2812 cf->ansi_capable = unix_cli_terminal_type_ansi (term, len);
2813 if (unix_cli_terminal_type_noninteractive (term, len))
2814 unix_cli_set_session_noninteractive (cf);
2820 /* No tty, so make sure the session doesn't have tty-like features */
2821 unix_cli_set_session_noninteractive (cf);
2824 /* Send banner and initial prompt */
2825 unix_cli_file_welcome (cm, cf);
2828 /* If we have socket config, LISTEN, otherwise, don't */
2829 clib_socket_t *s = &um->cli_listen_socket;
2830 if (s->config && s->config[0] != 0)
2833 clib_file_t template = { 0 };
2835 /* mkdir of file socketu, only under /run */
2836 if (strncmp (s->config, "/run", 4) == 0)
2838 u8 *tmp = format (0, "%s", s->config);
2839 int i = vec_len (tmp);
2840 while (i && tmp[--i] != '/')
2846 vlib_unix_recursive_mkdir ((char *) tmp);
2850 s->flags = CLIB_SOCKET_F_IS_SERVER | /* listen, don't connect */
2851 CLIB_SOCKET_F_ALLOW_GROUP_WRITE; /* PF_LOCAL socket only */
2852 error = clib_socket_init (s);
2857 template.read_function = unix_cli_listen_read_ready;
2858 template.file_descriptor = s->fd;
2860 clib_file_add (fm, &template);
2863 /* Set CLI prompt. */
2864 if (!cm->cli_prompt)
2865 cm->cli_prompt = format (0, "VLIB: ");
2871 * This module has no configurable parameters.
2873 VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
2875 /** Called when VPP is shutting down, this restores the system
2876 * terminal state if previously saved.
2878 static clib_error_t *
2879 unix_cli_exit (vlib_main_t * vm)
2881 unix_main_t *um = &unix_main;
2883 /* If stdin is a tty and we saved the tty state, reset the tty state */
2884 if (isatty (STDIN_FILENO) && um->tio_isset)
2885 tcsetattr (STDIN_FILENO, TCSAFLUSH, &um->tio_stdin);
2890 VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
2892 /** Set the CLI prompt.
2893 * @param prompt The C string to set the prompt to.
2894 * @note This setting is global; it impacts all current
2895 * and future CLI sessions.
2898 vlib_unix_cli_set_prompt (char *prompt)
2900 char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s ";
2901 unix_cli_main_t *cm = &unix_cli_main;
2903 vec_free (cm->cli_prompt);
2904 cm->cli_prompt = format (0, fmt, prompt);
2907 /** CLI command to quit the terminal session.
2908 * @note If this is a stdin session then this will
2909 * shutdown VPP also.
2911 static clib_error_t *
2912 unix_cli_quit (vlib_main_t * vm,
2913 unformat_input_t * input, vlib_cli_command_t * cmd)
2915 unix_cli_main_t *cm = &unix_cli_main;
2916 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2917 cm->current_input_file_index);
2919 /* Cosmetic: suppress the final prompt from appearing before we die */
2920 cf->is_interactive = 0;
2923 vlib_process_signal_event (vm,
2924 vlib_current_process (vm),
2925 UNIX_CLI_PROCESS_EVENT_QUIT,
2926 cm->current_input_file_index);
2931 * Terminates the current CLI session.
2933 * If VPP is running in @em interactive mode and this is the console session
2934 * (that is, the session on @c stdin) then this will also terminate VPP.
2937 VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
2939 .short_help = "Exit CLI",
2940 .function = unix_cli_quit,
2944 /** CLI command to execute a VPP command script. */
2945 static clib_error_t *
2946 unix_cli_exec (vlib_main_t * vm,
2947 unformat_input_t * input, vlib_cli_command_t * cmd)
2951 unformat_input_t sub_input;
2952 clib_error_t *error;
2958 if (!unformat (input, "%s", &file_name))
2960 error = clib_error_return (0, "expecting file name, got `%U'",
2961 format_unformat_error, input);
2965 fd = open (file_name, O_RDONLY);
2968 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
2972 /* Make sure its a regular file. */
2976 if (fstat (fd, &s) < 0)
2978 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
2982 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
2984 error = clib_error_return (0, "not a regular file `%s'", file_name);
2989 unformat_init_unix_file (&sub_input, fd);
2991 vlib_cli_input (vm, &sub_input, 0, 0);
2992 unformat_free (&sub_input);
2997 vec_free (file_name);
3003 * Executes a sequence of CLI commands which are read from a file.
3005 * If a command is unrecognised or otherwise invalid then the usual CLI
3006 * feedback will be generated, however execution of subsequent commands
3007 * from the file will continue.
3010 VLIB_CLI_COMMAND (cli_exec, static) = {
3012 .short_help = "Execute commands from file",
3013 .function = unix_cli_exec,
3018 /** CLI command to show various unix error statistics. */
3019 static clib_error_t *
3020 unix_show_errors (vlib_main_t * vm,
3021 unformat_input_t * input, vlib_cli_command_t * cmd)
3023 unix_main_t *um = &unix_main;
3024 clib_error_t *error = 0;
3025 int i, n_errors_to_show;
3026 unix_error_history_t *unix_errors = 0;
3028 n_errors_to_show = 1 << 30;
3030 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3032 if (!unformat (input, "%d", &n_errors_to_show))
3035 clib_error_return (0,
3036 "expecting integer number of errors to show, got `%U'",
3037 format_unformat_error, input);
3043 clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
3046 um->error_history_index >
3047 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
3049 while (n_errors_to_show > 0)
3051 unix_error_history_t *eh = um->error_history + i;
3056 vec_add1 (unix_errors, eh[0]);
3057 n_errors_to_show -= 1;
3059 i = ARRAY_LEN (um->error_history) - 1;
3064 if (vec_len (unix_errors) == 0)
3065 vlib_cli_output (vm, "no Unix errors so far");
3068 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
3069 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
3071 unix_error_history_t *eh = vec_elt_at_index (unix_errors, i);
3072 vlib_cli_output (vm, "%U: %U",
3073 format_time_interval, "h:m:s:u", eh->time,
3074 format_clib_error, eh->error);
3076 vlib_cli_output (vm, "%U: time now",
3077 format_time_interval, "h:m:s:u", vlib_time_now (vm));
3081 vec_free (unix_errors);
3086 VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
3087 .path = "show unix-errors",
3088 .short_help = "Show Unix system call error history",
3089 .function = unix_show_errors,
3093 /** CLI command to show session command history. */
3094 static clib_error_t *
3095 unix_cli_show_history (vlib_main_t * vm,
3096 unformat_input_t * input, vlib_cli_command_t * cmd)
3098 unix_cli_main_t *cm = &unix_cli_main;
3099 unix_cli_file_t *cf;
3102 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3104 if (!cf->is_interactive)
3105 return clib_error_return (0, "invalid for non-interactive sessions");
3107 if (cf->has_history && cf->history_limit)
3109 i = 1 + cf->command_number - vec_len (cf->command_history);
3110 for (j = 0; j < vec_len (cf->command_history); j++)
3111 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
3115 vlib_cli_output (vm, "History not enabled.\n");
3122 * Displays the command history for the current session, if any.
3125 VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
3127 .short_help = "Show current session command history",
3128 .function = unix_cli_show_history,
3132 /** CLI command to show terminal status. */
3133 static clib_error_t *
3134 unix_cli_show_terminal (vlib_main_t * vm,
3135 unformat_input_t * input, vlib_cli_command_t * cmd)
3137 unix_main_t *um = &unix_main;
3138 unix_cli_main_t *cm = &unix_cli_main;
3139 unix_cli_file_t *cf;
3142 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3143 n = vlib_get_node (vm, cf->process_node_index);
3145 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
3146 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
3147 "line-by-line" : "char-by-char");
3148 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
3149 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
3150 vlib_cli_output (vm, "ANSI capable: %s\n",
3151 cf->ansi_capable ? "yes" : "no");
3152 vlib_cli_output (vm, "Interactive: %s\n",
3153 cf->is_interactive ? "yes" : "no");
3154 vlib_cli_output (vm, "History enabled: %s%s\n",
3155 cf->has_history ? "yes" : "no", !cf->has_history
3156 || cf->history_limit ? "" :
3157 " (disabled by history limit)");
3158 if (cf->has_history)
3159 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
3160 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
3161 cf->no_pager ? "no" : "yes",
3163 || cf->height ? "" : " (disabled by terminal height)",
3165 || um->cli_pager_buffer_limit ? "" :
3166 " (disabled by buffer limit)");
3168 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
3169 vlib_cli_output (vm, "CRLF mode: %s\n",
3170 cf->crlf_mode ? "CR+LF" : "LF");
3176 * Displays various information about the state of the current terminal
3180 * @cliexstart{show terminal}
3181 * Terminal name: unix-cli-stdin
3182 * Terminal mode: char-by-char
3183 * Terminal width: 123
3184 * Terminal height: 48
3187 * History enabled: yes
3189 * Pager enabled: yes
3190 * Pager limit: 100000
3195 VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
3196 .path = "show terminal",
3197 .short_help = "Show current session terminal settings",
3198 .function = unix_cli_show_terminal,
3202 /** CLI command to display a list of CLI sessions. */
3203 static clib_error_t *
3204 unix_cli_show_cli_sessions (vlib_main_t * vm,
3205 unformat_input_t * input,
3206 vlib_cli_command_t * cmd)
3208 //unix_main_t *um = &unix_main;
3209 unix_cli_main_t *cm = &unix_cli_main;
3210 clib_file_main_t *fm = &file_main;
3211 unix_cli_file_t *cf;
3215 vlib_cli_output (vm, "%-5s %-5s %-20s %s", "PNI", "FD", "Name", "Flags");
3217 #define fl(x, y) ( (x) ? toupper((y)) : tolower((y)) )
3219 pool_foreach (cf, cm->cli_file_pool, ({
3220 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
3221 n = vlib_get_node (vm, cf->process_node_index);
3222 vlib_cli_output (vm,
3223 "%-5d %-5d %-20v %c%c%c%c%c\n",
3224 cf->process_node_index,
3225 uf->file_descriptor,
3227 fl (cf->is_interactive, 'i'),
3228 fl (cf->is_socket, 's'),
3229 fl (cf->line_mode, 'l'),
3230 fl (cf->has_epipe, 'p'),
3231 fl (cf->ansi_capable, 'a'));
3240 * Displays a summary of all the current CLI sessions.
3242 * Typically used to diagnose connection issues with the CLI
3246 * @cliexstart{show cli-sessions}
3248 * 343 0 unix-cli-stdin IslpA
3249 * 344 7 unix-cli-local:20 ISlpA
3250 * 346 8 unix-cli-local:21 iSLpa
3253 * In this example we have the debug console of the running process
3254 * on stdin/out, we have an interactive socket session and we also
3255 * have a non-interactive socket session.
3259 * - @em PNI: Process node index.
3260 * - @em FD: Unix file descriptor.
3261 * - @em Name: Name of the session.
3262 * - @em Flags: Various flags that describe the state of the session.
3264 * @em Flags have the following meanings; lower-case typically negates
3267 * - @em I Interactive session.
3268 * - @em S Connected by socket.
3269 * - @em s Not a socket, likely stdin.
3270 * - @em L Line-by-line mode.
3271 * - @em l Char-by-char mode.
3272 * - @em P EPIPE detected on connection; it will close soon.
3273 * - @em A ANSI-capable terminal.
3276 VLIB_CLI_COMMAND (cli_unix_cli_show_cli_sessions, static) = {
3277 .path = "show cli-sessions",
3278 .short_help = "Show current CLI sessions",
3279 .function = unix_cli_show_cli_sessions,
3283 /** CLI command to set terminal pager settings. */
3284 static clib_error_t *
3285 unix_cli_set_terminal_pager (vlib_main_t * vm,
3286 unformat_input_t * input,
3287 vlib_cli_command_t * cmd)
3289 unix_main_t *um = &unix_main;
3290 unix_cli_main_t *cm = &unix_cli_main;
3291 unix_cli_file_t *cf;
3292 unformat_input_t _line_input, *line_input = &_line_input;
3293 clib_error_t *error = 0;
3295 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3297 if (!cf->is_interactive)
3298 return clib_error_return (0, "invalid for non-interactive sessions");
3300 if (!unformat_user (input, unformat_line_input, line_input))
3303 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3305 if (unformat (line_input, "on"))
3307 else if (unformat (line_input, "off"))
3309 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
3310 vlib_cli_output (vm,
3311 "Pager limit set to %u lines; note, this is global.\n",
3312 um->cli_pager_buffer_limit);
3315 error = clib_error_return (0, "unknown parameter: `%U`",
3316 format_unformat_error, line_input);
3322 unformat_free (line_input);
3328 * Enables or disables the terminal pager for this session. Generally
3329 * this defaults to enabled.
3331 * Additionally allows the pager buffer size to be set; though note that
3332 * this value is set globally and not per session.
3335 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
3336 .path = "set terminal pager",
3337 .short_help = "set terminal pager [on|off] [limit <lines>]",
3338 .function = unix_cli_set_terminal_pager,
3342 /** CLI command to set terminal history settings. */
3343 static clib_error_t *
3344 unix_cli_set_terminal_history (vlib_main_t * vm,
3345 unformat_input_t * input,
3346 vlib_cli_command_t * cmd)
3348 unix_cli_main_t *cm = &unix_cli_main;
3349 unix_cli_file_t *cf;
3350 unformat_input_t _line_input, *line_input = &_line_input;
3352 clib_error_t *error = 0;
3354 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3356 if (!cf->is_interactive)
3357 return clib_error_return (0, "invalid for non-interactive sessions");
3359 if (!unformat_user (input, unformat_line_input, line_input))
3362 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3364 if (unformat (line_input, "on"))
3365 cf->has_history = 1;
3366 else if (unformat (line_input, "off"))
3367 cf->has_history = 0;
3368 else if (unformat (line_input, "limit %u", &cf->history_limit))
3372 error = clib_error_return (0, "unknown parameter: `%U`",
3373 format_unformat_error, line_input);
3377 /* If we reduced history size, or turned it off, purge the history */
3378 limit = cf->has_history ? cf->history_limit : 0;
3380 while (cf->command_history && vec_len (cf->command_history) >= limit)
3382 vec_free (cf->command_history[0]);
3383 vec_delete (cf->command_history, 1, 0);
3388 unformat_free (line_input);
3394 * Enables or disables the command history function of the current
3395 * terminal. Generally this defaults to enabled.
3397 * This command also allows the maximum size of the history buffer for
3398 * this session to be altered.
3401 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
3402 .path = "set terminal history",
3403 .short_help = "set terminal history [on|off] [limit <lines>]",
3404 .function = unix_cli_set_terminal_history,
3408 /** CLI command to set terminal ANSI settings. */
3409 static clib_error_t *
3410 unix_cli_set_terminal_ansi (vlib_main_t * vm,
3411 unformat_input_t * input,
3412 vlib_cli_command_t * cmd)
3414 unix_cli_main_t *cm = &unix_cli_main;
3415 unix_cli_file_t *cf;
3417 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3419 if (!cf->is_interactive)
3420 return clib_error_return (0, "invalid for non-interactive sessions");
3422 if (unformat (input, "on"))
3423 cf->ansi_capable = 1;
3424 else if (unformat (input, "off"))
3425 cf->ansi_capable = 0;
3427 return clib_error_return (0, "unknown parameter: `%U`",
3428 format_unformat_error, input);
3434 * Enables or disables the use of ANSI control sequences by this terminal.
3435 * The default will vary based on terminal detection at the start of the
3438 * ANSI control sequences are used in a small number of places to provide,
3439 * for example, color text output and to control the cursor in the pager.
3442 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
3443 .path = "set terminal ansi",
3444 .short_help = "set terminal ansi [on|off]",
3445 .function = unix_cli_set_terminal_ansi,
3449 static clib_error_t *
3450 unix_cli_init (vlib_main_t * vm)
3455 VLIB_INIT_FUNCTION (unix_cli_init);
3458 * fd.io coding-style-patch-verification: ON
3461 * eval: (c-set-style "gnu")