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>
64 /** ANSI escape code. */
67 /** ANSI Control Sequence Introducer. */
70 /** ANSI clear screen. */
71 #define ANSI_CLEAR CSI "2J" CSI "1;1H"
72 /** ANSI reset color settings. */
73 #define ANSI_RESET CSI "0m"
74 /** ANSI Start bold text. */
75 #define ANSI_BOLD CSI "1m"
76 /** ANSI Stop bold text. */
77 #define ANSI_DIM CSI "2m"
78 /** ANSI Start dark red text. */
79 #define ANSI_DRED ANSI_DIM CSI "31m"
80 /** ANSI Start bright red text. */
81 #define ANSI_BRED ANSI_BOLD CSI "31m"
82 /** ANSI clear line cursor is on. */
83 #define ANSI_CLEARLINE CSI "2K"
84 /** ANSI scroll screen down one line. */
85 #define ANSI_SCROLLDN CSI "1T"
86 /** ANSI save cursor position. */
87 #define ANSI_SAVECURSOR CSI "s"
88 /** ANSI restore cursor position if previously saved. */
89 #define ANSI_RESTCURSOR CSI "u"
91 /** Maximum depth into a byte stream from which to compile a Telnet
92 * protocol message. This is a safety measure. */
93 #define UNIX_CLI_MAX_DEPTH_TELNET 24
95 /** Maximum terminal width we will accept */
96 #define UNIX_CLI_MAX_TERMINAL_WIDTH 512
97 /** Maximum terminal height we will accept */
98 #define UNIX_CLI_MAX_TERMINAL_HEIGHT 512
99 /** Default terminal height */
100 #define UNIX_CLI_DEFAULT_TERMINAL_HEIGHT 24
101 /** Default terminal width */
102 #define UNIX_CLI_DEFAULT_TERMINAL_WIDTH 80
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 if (cf->pager_vector != NULL)
850 p = cf->pager_vector[line_index];
856 /* Add a copy of the raw string to the pager buffer */
857 p = vec_new (u8, len);
858 clib_memcpy (p, line, len);
860 /* store in pager buffer */
861 line_index = vec_len (cf->pager_vector);
862 vec_add1 (cf->pager_vector, p);
868 /* Find the next line, or run to terminal width, or run to EOL */
870 j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width);
872 if (j < l && p[j] == '\n') /* incl \n */
875 /* Add the line to the index */
876 k = vec_len (cf->pager_index);
877 vec_validate (cf->pager_index, k);
878 pi = &cf->pager_index[k];
880 pi->line = line_index;
889 /** @brief Reindex entire pager buffer.
890 * Resets the current pager index and then re-adds the lines in the pager
891 * buffer to the index.
893 * Additionally this function attempts to retain the current page start
894 * line offset by searching for the same top-of-screen line in the new index.
896 * @param cf The CLI session whose pager buffer should be reindexed.
899 unix_cli_pager_reindex (unix_cli_file_t * cf)
901 word i, old_line, old_offset;
902 unix_cli_pager_index_t *pi;
904 /* If there is nothing in the pager buffer then make sure the index
905 * is empty and move on.
907 if (cf->pager_vector == 0)
909 vec_reset_length (cf->pager_index);
913 /* Retain a pointer to the current page start line so we can
916 pi = &cf->pager_index[cf->pager_start];
918 old_offset = pi->offset;
920 /* Re-add the buffered lines to the index */
921 vec_reset_length (cf->pager_index);
922 vec_foreach_index (i, cf->pager_vector)
924 unix_cli_pager_add_line (cf, NULL, i);
927 /* Attempt to re-locate the previously stored page start line */
928 vec_foreach_index (i, cf->pager_index)
930 pi = &cf->pager_index[i];
932 if (pi->line == old_line &&
933 (pi->offset <= old_offset || pi->offset + pi->length > old_offset))
941 /* In case the start line was not found (rare), ensure the pager start
942 * index is within bounds
944 if (cf->pager_start >= vec_len (cf->pager_index))
946 if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1))
949 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
953 /** VLIB CLI output function.
955 * If the terminal has a pager configured then this function takes care
956 * of collating output into the pager buffer; ensuring only the first page
957 * is displayed and any lines in excess of the first page are buffered.
959 * If the maximum number of index lines in the buffer is exceeded then the
960 * pager is cancelled and the contents of the current buffer are sent to the
963 * If there is no pager configured then the output is sent directly to the
966 * @param cli_file_index Index of the CLI session where this output is
968 * @param buffer String of printabe bytes to be output.
969 * @param buffer_bytes The number of bytes in @c buffer to be output.
972 unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes)
974 unix_main_t *um = &unix_main;
975 clib_file_main_t *fm = &file_main;
976 unix_cli_main_t *cm = &unix_cli_main;
980 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
981 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
983 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
985 unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes);
989 word row = vec_len (cf->pager_index);
991 unix_cli_pager_index_t *pi;
993 /* Index and add the output lines to the pager buffer. */
994 unix_cli_pager_add_line (cf, buffer, buffer_bytes);
996 /* Now iterate what was added to display the lines.
997 * If we reach the bottom of the page, display a prompt.
999 while (row < vec_len (cf->pager_index))
1001 if (row < cf->height - 1)
1003 /* output this line */
1004 pi = &cf->pager_index[row];
1005 line = cf->pager_vector[pi->line] + pi->offset;
1006 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1008 /* if the last line didn't end in newline, and we're at the
1009 * bottom of the page, add a newline */
1010 if (line[pi->length - 1] != '\n' && row == cf->height - 2)
1011 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1015 /* Display the pager prompt every 10 lines */
1017 unix_cli_pager_prompt (cf, uf);
1022 /* Check if we went over the pager buffer limit */
1023 if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit)
1025 /* Stop using the pager for the remainder of this CLI command */
1028 /* If we likely printed the prompt, erase it */
1029 if (vec_len (cf->pager_index) > cf->height - 1)
1030 unix_cli_pager_prompt_erase (cf, uf);
1032 /* Dump out the contents of the buffer */
1033 for (row = cf->pager_start + (cf->height - 1);
1034 row < vec_len (cf->pager_index); row++)
1036 pi = &cf->pager_index[row];
1037 line = cf->pager_vector[pi->line] + pi->offset;
1038 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1041 unix_cli_pager_reset (cf);
1046 /** Identify whether a terminal type is ANSI capable.
1048 * Compares the string given in @c term with a list of terminal types known
1049 * to support ANSI escape sequences.
1051 * This list contains, for example, @c xterm, @c screen and @c ansi.
1053 * @param term A string with a terminal type in it.
1054 * @param len The length of the string in @c term.
1056 * @return @c 1 if the terminal type is recognized as supporting ANSI
1057 * terminal sequences; @c 0 otherwise.
1060 unix_cli_terminal_type_ansi (u8 * term, uword len)
1062 /* This may later be better done as a hash of some sort. */
1064 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1069 _("xterm-256color"); /* iTerm on Mac */
1071 _("screen-256color"); /* Screen and tmux */
1072 _("ansi"); /* Microsoft Telnet */
1078 /** Identify whether a terminal type is non-interactive.
1080 * Compares the string given in @c term with a list of terminal types known
1081 * to be non-interactive, as send by tools such as @c vppctl .
1083 * This list contains, for example, @c vppctl.
1085 * @param term A string with a terminal type in it.
1086 * @param len The length of the string in @c term.
1088 * @return @c 1 if the terminal type is recognized as being non-interactive;
1092 unix_cli_terminal_type_noninteractive (u8 * term, uword len)
1094 /* This may later be better done as a hash of some sort. */
1096 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1105 /** Set a session to be non-interactive. */
1107 unix_cli_set_session_noninteractive (unix_cli_file_t * cf)
1109 /* Non-interactive sessions don't get these */
1110 cf->is_interactive = 0;
1112 cf->history_limit = 0;
1113 cf->has_history = 0;
1117 /** @brief Emit initial welcome banner and prompt on a connection. */
1119 unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf)
1121 unix_main_t *um = &unix_main;
1122 clib_file_main_t *fm = &file_main;
1123 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
1124 unix_cli_banner_t *banner;
1127 /* Mark the session as started if we get here */
1130 if (!(cf->is_interactive)) /* No banner for non-interactive sessions */
1134 * Put the first bytes directly into the buffer so that further output is
1135 * queued until everything is ready. (oterwise initial prompt can appear
1136 * mid way through VPP initialization)
1138 unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
1140 if (!um->cli_no_banner)
1142 if (cf->ansi_capable)
1144 banner = unix_cli_banner_color;
1145 len = ARRAY_LEN (unix_cli_banner_color);
1149 banner = unix_cli_banner;
1150 len = ARRAY_LEN (unix_cli_banner);
1153 for (i = 0; i < len; i++)
1155 unix_vlib_cli_output_cooked (cf, uf,
1156 banner[i].line, banner[i].length);
1161 unix_cli_cli_prompt (cf, uf);
1165 /** @brief A failsafe triggered on a timer to ensure we send the prompt
1166 * to telnet sessions that fail to negotiate the terminal type. */
1168 unix_cli_file_welcome_timer (any arg, f64 delay)
1170 unix_cli_main_t *cm = &unix_cli_main;
1171 unix_cli_file_t *cf;
1174 /* Check the connection didn't close already */
1175 if (pool_is_free_index (cm->cli_file_pool, (uword) arg))
1178 cf = pool_elt_at_index (cm->cli_file_pool, (uword) arg);
1181 unix_cli_file_welcome (cm, cf);
1184 /** @brief A mostly no-op Telnet state machine.
1185 * Process Telnet command bytes in a way that ensures we're mostly
1186 * transparent to the Telnet protocol. That is, it's mostly a no-op.
1188 * @return -1 if we need more bytes, otherwise a positive integer number of
1189 * bytes to consume from the input_vector, not including the initial
1193 unix_cli_process_telnet (unix_main_t * um,
1194 unix_cli_file_t * cf,
1195 clib_file_t * uf, u8 * input_vector, uword len)
1197 /* Input_vector starts at IAC byte.
1198 * See if we have a complete message; if not, return -1 so we wait for more.
1199 * if we have a complete message, consume those bytes from the vector.
1204 return -1; /* want more bytes */
1206 switch (input_vector[1])
1209 /* two IAC's in a row means to pass through 0xff.
1210 * since that makes no sense here, just consume it.
1219 /* Expect 3 bytes */
1220 if (vec_len (input_vector) < 3)
1221 return -1; /* want more bytes */
1228 /* Sub option - search ahead for IAC SE to end it */
1230 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
1232 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
1234 /* We have a complete message; see if we care about it */
1235 switch (input_vector[2])
1238 if (input_vector[3] != 0)
1241 /* See if the the terminal type is recognized */
1242 u8 *term = input_vector + 4;
1245 /* See if the terminal type is ANSI capable */
1247 unix_cli_terminal_type_ansi (term, len);
1249 /* See if the terminal type indicates non-interactive */
1250 if (unix_cli_terminal_type_noninteractive (term, len))
1251 unix_cli_set_session_noninteractive (cf);
1254 /* If session not started, we can release the pause */
1256 /* Send the welcome banner and initial prompt */
1257 unix_cli_file_welcome (&unix_cli_main, cf);
1262 if (i != 8) /* check message is correct size */
1266 clib_net_to_host_u16 (*((u16 *) (input_vector + 3)));
1267 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
1268 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
1270 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
1273 clib_net_to_host_u16 (*((u16 *) (input_vector + 5)));
1274 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
1275 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
1276 if (cf->height == 0)
1277 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
1279 /* reindex pager buffer */
1280 unix_cli_pager_reindex (cf);
1282 unix_cli_pager_redraw (cf, uf);
1288 /* Consume it all */
1294 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
1295 consume = 1; /* hit max search depth, advance one byte */
1298 return -1; /* want more bytes */
1316 /* Simple one-byte messages */
1321 /* Are You There - trigger a visible response */
1323 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
1327 /* Unknown command! Eat the IAC byte */
1334 /** @brief Process actionable input.
1335 * Based on the \c action process the input; this typically involves
1336 * searching the command history or editing the current command line.
1339 unix_cli_line_process_one (unix_cli_main_t * cm,
1341 unix_cli_file_t * cf,
1343 u8 input, unix_cli_parse_action_t action)
1347 u8 **possible_commands;
1352 case UNIX_CLI_PARSE_ACTION_NOACTION:
1355 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
1356 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
1357 if (!cf->has_history || !cf->history_limit)
1359 if (cf->search_mode == 0)
1361 /* Erase the current command (if any) */
1362 for (j = 0; j < (vec_len (cf->current_command)); j++)
1363 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1365 vec_reset_length (cf->search_key);
1366 vec_reset_length (cf->current_command);
1367 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1368 cf->search_mode = -1;
1370 cf->search_mode = 1;
1375 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1376 cf->search_mode = -1;
1378 cf->search_mode = 1;
1380 cf->excursion += cf->search_mode;
1385 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
1386 /* Erase the command from the cursor to the start */
1388 /* Shimmy forwards to the new end of line position */
1389 delta = vec_len (cf->current_command) - cf->cursor;
1390 for (j = cf->cursor; j > delta; j--)
1391 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1392 /* Zap from here to the end of what is currently displayed */
1393 for (; j < (vec_len (cf->current_command)); j++)
1394 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1395 /* Get back to the start of the line */
1396 for (j = 0; j < (vec_len (cf->current_command)); j++)
1397 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1399 j = vec_len (cf->current_command) - cf->cursor;
1400 memmove (cf->current_command, cf->current_command + cf->cursor, j);
1401 _vec_len (cf->current_command) = j;
1403 /* Print the new contents */
1404 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j);
1405 /* Shimmy back to the start */
1406 for (j = 0; j < (vec_len (cf->current_command)); j++)
1407 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1410 cf->search_mode = 0;
1413 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
1414 /* Erase the command from the cursor to the end */
1416 /* Zap from cursor to end of what is currently displayed */
1417 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1418 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1419 /* Get back to where we were */
1420 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1421 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1423 /* Truncate the line at the cursor */
1424 _vec_len (cf->current_command) = cf->cursor;
1426 cf->search_mode = 0;
1429 case UNIX_CLI_PARSE_ACTION_LEFT:
1432 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1436 cf->search_mode = 0;
1439 case UNIX_CLI_PARSE_ACTION_RIGHT:
1440 if (cf->cursor < vec_len (cf->current_command))
1442 /* have to emit the character under the cursor */
1443 unix_vlib_cli_output_cooked (cf, uf,
1444 cf->current_command + cf->cursor, 1);
1448 cf->search_mode = 0;
1451 case UNIX_CLI_PARSE_ACTION_UP:
1452 case UNIX_CLI_PARSE_ACTION_DOWN:
1453 if (!cf->has_history || !cf->history_limit)
1455 cf->search_mode = 0;
1456 /* Erase the command */
1457 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1458 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1459 for (j = 0; j < (vec_len (cf->current_command)); j++)
1460 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1461 vec_reset_length (cf->current_command);
1462 if (vec_len (cf->command_history))
1464 if (action == UNIX_CLI_PARSE_ACTION_UP)
1469 cf->excursion += delta;
1471 if (cf->excursion == vec_len (cf->command_history))
1473 /* down-arrowed to last entry - want a blank line */
1474 _vec_len (cf->current_command) = 0;
1476 else if (cf->excursion < 0)
1478 /* up-arrowed over the start to the end, want a blank line */
1479 cf->excursion = vec_len (cf->command_history);
1480 _vec_len (cf->current_command) = 0;
1484 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1485 /* down-arrowed past end - wrap to start */
1488 /* Print the command at the current position */
1489 prev = cf->command_history[cf->excursion];
1490 vec_validate (cf->current_command, vec_len (prev) - 1);
1492 clib_memcpy (cf->current_command, prev, vec_len (prev));
1493 _vec_len (cf->current_command) = vec_len (prev);
1494 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1495 vec_len (cf->current_command));
1498 cf->cursor = vec_len (cf->current_command);
1501 case UNIX_CLI_PARSE_ACTION_HOME:
1502 if (vec_len (cf->current_command) && cf->cursor > 0)
1506 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1511 cf->search_mode = 0;
1514 case UNIX_CLI_PARSE_ACTION_END:
1515 if (vec_len (cf->current_command) &&
1516 cf->cursor < vec_len (cf->current_command))
1518 unix_vlib_cli_output_cooked (cf, uf,
1519 cf->current_command + cf->cursor,
1520 vec_len (cf->current_command) -
1522 cf->cursor = vec_len (cf->current_command);
1525 cf->search_mode = 0;
1528 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1529 if (vec_len (cf->current_command) && cf->cursor > 0)
1533 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1536 while (j && isspace (cf->current_command[j]))
1538 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1541 while (j && !isspace (cf->current_command[j]))
1543 if (isspace (cf->current_command[j - 1]))
1545 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1552 cf->search_mode = 0;
1555 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1556 if (vec_len (cf->current_command) &&
1557 cf->cursor < vec_len (cf->current_command))
1559 int e = vec_len (cf->current_command);
1561 while (j < e && !isspace (cf->current_command[j]))
1563 while (j < e && isspace (cf->current_command[j]))
1565 unix_vlib_cli_output_cooked (cf, uf,
1566 cf->current_command + cf->cursor,
1571 cf->search_mode = 0;
1575 case UNIX_CLI_PARSE_ACTION_ERASE:
1576 if (vec_len (cf->current_command))
1578 if (cf->cursor == vec_len (cf->current_command))
1580 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1581 _vec_len (cf->current_command)--;
1584 else if (cf->cursor > 0)
1586 /* shift everything at & to the right of the cursor left by 1 */
1587 j = vec_len (cf->current_command) - cf->cursor;
1588 memmove (cf->current_command + cf->cursor - 1,
1589 cf->current_command + cf->cursor, j);
1590 _vec_len (cf->current_command)--;
1592 /* redraw the rest of the line */
1593 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1594 unix_vlib_cli_output_cooked (cf, uf,
1595 cf->current_command + cf->cursor,
1597 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
1598 /* and shift the terminal cursor back where it should be */
1600 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1603 cf->search_mode = 0;
1605 vec_reset_length (cf->search_key);
1608 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1609 if (vec_len (cf->current_command))
1611 if (cf->cursor < vec_len (cf->current_command))
1613 /* shift everything to the right of the cursor left by 1 */
1614 j = vec_len (cf->current_command) - cf->cursor - 1;
1615 memmove (cf->current_command + cf->cursor,
1616 cf->current_command + cf->cursor + 1, j);
1617 _vec_len (cf->current_command)--;
1618 /* redraw the rest of the line */
1619 unix_vlib_cli_output_cooked (cf, uf,
1620 cf->current_command + cf->cursor,
1622 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
1623 /* and shift the terminal cursor back where it should be */
1626 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1628 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1632 else if (input == 'D' - '@')
1634 /* ^D with no command entered = quit */
1635 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1636 vlib_process_signal_event (um->vlib_main,
1637 vlib_current_process (um->vlib_main),
1638 UNIX_CLI_PROCESS_EVENT_QUIT,
1639 cf - cm->cli_file_pool);
1641 cf->search_mode = 0;
1643 vec_reset_length (cf->search_key);
1646 case UNIX_CLI_PARSE_ACTION_CLEAR:
1647 /* If we're in ANSI mode, clear the screen.
1648 * Then redraw the prompt and any existing command input, then put
1649 * the cursor back where it was in that line.
1651 if (cf->ansi_capable)
1652 unix_vlib_cli_output_cooked (cf, uf,
1654 sizeof (ANSI_CLEAR) - 1);
1656 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1658 unix_vlib_cli_output_raw (cf, uf,
1659 cm->cli_prompt, vec_len (cm->cli_prompt));
1660 unix_vlib_cli_output_raw (cf, uf,
1661 cf->current_command,
1662 vec_len (cf->current_command));
1663 for (j = cf->cursor; j < vec_len (cf->current_command); j++)
1664 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1668 case UNIX_CLI_PARSE_ACTION_TAB:
1669 if (cf->cursor < vec_len (cf->current_command))
1671 /* if we are in the middle of a line, complete only if
1672 * the cursor points to whitespace */
1673 if (isspace (cf->current_command[cf->cursor]))
1675 /* save and clear any input that is after the cursor */
1676 vec_resize (save, vec_len (cf->current_command) - cf->cursor);
1677 clib_memcpy (save, cf->current_command + cf->cursor,
1678 vec_len (cf->current_command) - cf->cursor);
1679 _vec_len (cf->current_command) = cf->cursor;
1683 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1688 vlib_cli_get_possible_completions (cf->current_command);
1689 if (vec_len (possible_commands) == 1)
1692 u8 *completed = possible_commands[0];
1694 /* find the last word of current_command */
1695 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1698 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1700 _vec_len (cf->current_command) = j;
1702 /* replace it with the newly expanded command */
1703 vec_append (cf->current_command, completed);
1705 /* echo to the terminal */
1706 unix_vlib_cli_output_raw (cf, uf, completed, vec_len (completed));
1708 /* add one trailing space if needed */
1709 if (vec_len (save) == 0)
1711 vec_add1 (cf->current_command, ' ');
1712 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1715 cf->cursor = vec_len (cf->current_command);
1718 else if (vec_len (possible_commands) >= 2)
1720 u8 **possible_command;
1721 uword max_command_len = 0, min_command_len = ~0;
1724 vec_foreach (possible_command, possible_commands)
1726 if (vec_len (*possible_command) > max_command_len)
1728 max_command_len = vec_len (*possible_command);
1730 if (vec_len (*possible_command) < min_command_len)
1732 min_command_len = vec_len (*possible_command);
1736 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1739 vec_foreach (possible_command, possible_commands)
1741 if (i + max_command_len >= cf->width)
1743 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1746 unix_vlib_cli_output_raw (cf, uf, *possible_command,
1747 vec_len (*possible_command));
1748 for (j = vec_len (*possible_command); j < max_command_len + 2;
1751 unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
1753 i += max_command_len + 2;
1756 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1758 /* rewrite prompt */
1759 unix_cli_cli_prompt (cf, uf);
1760 unix_vlib_cli_output_raw (cf, uf, cf->current_command,
1761 vec_len (cf->current_command));
1763 /* count length of last word */
1766 while (j >= 1 && !isspace (cf->current_command[j - 1]))
1772 /* determine smallest common command */
1773 for (; i < min_command_len; i++)
1777 vec_foreach (possible_command, possible_commands)
1781 common = (*possible_command)[i];
1783 else if (common != (*possible_command)[i])
1791 vec_add1 (cf->current_command, common);
1793 unix_vlib_cli_output_raw (cf, uf, (u8 *) & common, 1);
1803 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\a", 1);
1806 if (vec_len (save) > 0)
1808 /* restore remaining input if tab was hit in the middle of a line */
1809 unix_vlib_cli_output_raw (cf, uf, save, vec_len (save));
1810 for (j = 0; j < vec_len (save); j++)
1812 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1814 vec_append (cf->current_command, save);
1817 vec_free (possible_commands);
1820 case UNIX_CLI_PARSE_ACTION_YANK:
1825 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
1827 unix_cli_pager_prompt_erase (cf, uf);
1828 unix_cli_pager_reset (cf);
1829 unix_cli_cli_prompt (cf, uf);
1832 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
1833 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
1834 /* show next page of the buffer */
1835 if (cf->height + cf->pager_start <= vec_len (cf->pager_index))
1838 unix_cli_pager_index_t *pi = NULL;
1840 int m = cf->pager_start + (cf->height - 1);
1841 unix_cli_pager_prompt_erase (cf, uf);
1843 j < vec_len (cf->pager_index) && cf->pager_start < m;
1844 j++, cf->pager_start++)
1846 pi = &cf->pager_index[j];
1847 line = cf->pager_vector[pi->line] + pi->offset;
1848 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1850 /* if the last line didn't end in newline, add a newline */
1851 if (pi && line[pi->length - 1] != '\n')
1852 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1853 unix_cli_pager_prompt (cf, uf);
1857 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
1858 /* no more in buffer, exit, but only if it was <space> */
1863 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
1864 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
1865 /* display the next line of the buffer */
1866 if (cf->height + cf->pager_start <= vec_len (cf->pager_index))
1869 unix_cli_pager_index_t *pi;
1871 unix_cli_pager_prompt_erase (cf, uf);
1872 pi = &cf->pager_index[cf->pager_start + (cf->height - 1)];
1873 line = cf->pager_vector[pi->line] + pi->offset;
1874 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1876 /* if the last line didn't end in newline, add a newline */
1877 if (line[pi->length - 1] != '\n')
1878 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1879 unix_cli_pager_prompt (cf, uf);
1883 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
1884 /* no more in buffer, exit, but only if it was <enter> */
1890 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
1891 /* scroll the page back one line */
1892 if (cf->pager_start > 0)
1895 unix_cli_pager_index_t *pi = NULL;
1898 if (cf->ansi_capable)
1900 pi = &cf->pager_index[cf->pager_start];
1901 line = cf->pager_vector[pi->line] + pi->offset;
1902 unix_cli_pager_prompt_erase (cf, uf);
1903 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN,
1904 sizeof (ANSI_SCROLLDN) - 1);
1905 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR,
1906 sizeof (ANSI_SAVECURSOR) - 1);
1907 unix_cli_ansi_cursor (cf, uf, 1, 1);
1908 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE,
1909 sizeof (ANSI_CLEARLINE) - 1);
1910 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1911 unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR,
1912 sizeof (ANSI_RESTCURSOR) - 1);
1913 unix_cli_pager_prompt_erase (cf, uf);
1914 unix_cli_pager_prompt (cf, uf);
1918 int m = cf->pager_start + (cf->height - 1);
1919 unix_cli_pager_prompt_erase (cf, uf);
1920 for (j = cf->pager_start;
1921 j < vec_len (cf->pager_index) && j < m; j++)
1923 pi = &cf->pager_index[j];
1924 line = cf->pager_vector[pi->line] + pi->offset;
1925 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1927 /* if the last line didn't end in newline, add a newline */
1928 if (pi && line[pi->length - 1] != '\n')
1929 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1930 unix_cli_pager_prompt (cf, uf);
1935 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
1936 /* back to the first page of the buffer */
1937 if (cf->pager_start > 0)
1940 unix_cli_pager_index_t *pi = NULL;
1942 cf->pager_start = 0;
1943 int m = cf->pager_start + (cf->height - 1);
1944 unix_cli_pager_prompt_erase (cf, uf);
1945 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1948 pi = &cf->pager_index[j];
1949 line = cf->pager_vector[pi->line] + pi->offset;
1950 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1952 /* if the last line didn't end in newline, add a newline */
1953 if (pi && line[pi->length - 1] != '\n')
1954 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1955 unix_cli_pager_prompt (cf, uf);
1959 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
1960 /* skip to the last page of the buffer */
1961 if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
1964 unix_cli_pager_index_t *pi = NULL;
1966 cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
1967 unix_cli_pager_prompt_erase (cf, uf);
1968 unix_cli_pager_message (cf, uf, "skipping", "\n");
1969 for (j = cf->pager_start; j < vec_len (cf->pager_index); j++)
1971 pi = &cf->pager_index[j];
1972 line = cf->pager_vector[pi->line] + pi->offset;
1973 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1975 /* if the last line didn't end in newline, add a newline */
1976 if (pi && line[pi->length - 1] != '\n')
1977 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1978 unix_cli_pager_prompt (cf, uf);
1982 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
1983 /* wander back one page in the buffer */
1984 if (cf->pager_start > 0)
1987 unix_cli_pager_index_t *pi = NULL;
1990 if (cf->pager_start >= cf->height)
1991 cf->pager_start -= cf->height - 1;
1993 cf->pager_start = 0;
1994 m = cf->pager_start + cf->height - 1;
1995 unix_cli_pager_prompt_erase (cf, uf);
1996 for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1999 pi = &cf->pager_index[j];
2000 line = cf->pager_vector[pi->line] + pi->offset;
2001 unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
2003 /* if the last line didn't end in newline, add a newline */
2004 if (pi && line[pi->length - 1] != '\n')
2005 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2006 unix_cli_pager_prompt (cf, uf);
2010 case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW:
2011 /* Redraw the current pager screen */
2012 unix_cli_pager_redraw (cf, uf);
2015 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
2016 /* search forwards in the buffer */
2020 case UNIX_CLI_PARSE_ACTION_CRLF:
2022 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
2024 if (cf->has_history && cf->history_limit)
2026 if (cf->command_history
2027 && vec_len (cf->command_history) >= cf->history_limit)
2029 vec_free (cf->command_history[0]);
2030 vec_delete (cf->command_history, 1, 0);
2032 /* Don't add blank lines to the cmd history */
2033 if (vec_len (cf->current_command))
2035 /* Don't duplicate the previous command */
2036 j = vec_len (cf->command_history);
2038 (vec_len (cf->current_command) !=
2039 vec_len (cf->command_history[j - 1])
2040 || memcmp (cf->current_command, cf->command_history[j - 1],
2041 vec_len (cf->current_command)) != 0))
2043 /* copy the command to the history */
2045 vec_append (c, cf->current_command);
2046 vec_add1 (cf->command_history, c);
2047 cf->command_number++;
2050 cf->excursion = vec_len (cf->command_history);
2053 cf->search_mode = 0;
2054 vec_reset_length (cf->search_key);
2059 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2060 case UNIX_CLI_PARSE_ACTION_NOMATCH:
2061 if (vec_len (cf->pager_index))
2065 else if (cf->has_history && cf->search_mode && isprint (input))
2067 int k, limit, offset;
2070 vec_add1 (cf->search_key, input);
2073 for (j = 0; j < vec_len (cf->command_history); j++)
2075 if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
2077 else if (cf->excursion < 0)
2078 cf->excursion = vec_len (cf->command_history) - 1;
2080 item = cf->command_history[cf->excursion];
2082 limit = (vec_len (cf->search_key) > vec_len (item)) ?
2083 vec_len (item) : vec_len (cf->search_key);
2085 for (offset = 0; offset <= vec_len (item) - limit; offset++)
2087 for (k = 0; k < limit; k++)
2089 if (item[k + offset] != cf->search_key[k])
2092 goto found_at_offset;
2100 for (j = 0; j < vec_len (cf->current_command); j++)
2101 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
2103 vec_validate (cf->current_command, vec_len (item) - 1);
2104 clib_memcpy (cf->current_command, item, vec_len (item));
2105 _vec_len (cf->current_command) = vec_len (item);
2107 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
2108 vec_len (cf->current_command));
2109 cf->cursor = vec_len (cf->current_command);
2113 cf->excursion += cf->search_mode;
2116 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12);
2117 vec_reset_length (cf->search_key);
2118 vec_reset_length (cf->current_command);
2119 cf->search_mode = 0;
2123 else if (isprint (input)) /* skip any errant control codes */
2125 if (cf->cursor == vec_len (cf->current_command))
2128 vec_add1 (cf->current_command, input);
2131 /* Echo the character back to the client */
2132 unix_vlib_cli_output_raw (cf, uf, &input, 1);
2136 /* Insert at cursor: resize +1 byte, move everything over */
2137 j = vec_len (cf->current_command) - cf->cursor;
2138 vec_add1 (cf->current_command, (u8) 'A');
2139 memmove (cf->current_command + cf->cursor + 1,
2140 cf->current_command + cf->cursor, j);
2141 cf->current_command[cf->cursor] = input;
2142 /* Redraw the line */
2144 unix_vlib_cli_output_raw (cf, uf,
2145 cf->current_command + cf->cursor, j);
2146 /* Put terminal cursor back */
2148 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
2154 /* no-op - not printable or otherwise not actionable */
2161 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2167 /** @brief Process input bytes on a stream to provide line editing and
2168 * command history in the CLI. */
2170 unix_cli_line_edit (unix_cli_main_t * cm, unix_main_t * um,
2171 clib_file_main_t * fm, unix_cli_file_t * cf)
2173 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2176 for (i = 0; i < vec_len (cf->input_vector); i++)
2178 unix_cli_parse_action_t action;
2180 unix_cli_parse_actions_t *a;
2182 /* If we're in the pager mode, search the pager actions */
2184 vec_len (cf->pager_index) ? unix_cli_parse_pager :
2185 unix_cli_parse_strings;
2187 /* See if the input buffer is some sort of control code */
2188 action = unix_cli_match_action (a, &cf->input_vector[i],
2189 vec_len (cf->input_vector) - i,
2194 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
2197 /* There was a partial match which means we need more bytes
2198 * than the input buffer currently has.
2199 * Since the bytes before here have been processed, shift
2200 * the remaining contents to the start of the input buffer.
2202 vec_delete (cf->input_vector, i, 0);
2204 return 1; /* wait for more */
2206 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
2207 /* process telnet options */
2208 matched = unix_cli_process_telnet (um, cf, uf,
2209 cf->input_vector + i,
2210 vec_len (cf->input_vector) - i);
2213 /* There was a partial match which means we need more bytes
2214 * than the input buffer currently has.
2219 * Since the bytes before here have been processed, shift
2220 * the remaining contents to the start of the input buffer.
2222 vec_delete (cf->input_vector, i, 0);
2224 return 1; /* wait for more */
2229 /* If telnet option processing switched us to line mode, get us
2234 vec_delete (cf->input_vector, i, 0);
2235 cf->current_command = cf->input_vector;
2239 /* process the action */
2240 if (!unix_cli_line_process_one (cm, um, cf, uf,
2241 cf->input_vector[i], action))
2243 /* CRLF found. Consume the bytes from the input_vector */
2244 vec_delete (cf->input_vector, i + matched, 0);
2245 /* And tell our caller to execute cf->input_command */
2253 vec_reset_length (cf->input_vector);
2257 /** @brief Process input to a CLI session. */
2259 unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index)
2261 unix_main_t *um = &unix_main;
2262 clib_file_main_t *fm = &file_main;
2264 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2265 unformat_input_t input;
2266 int vlib_parse_eval (u8 *);
2268 cm->current_input_file_index = cli_file_index;
2271 /* Try vlibplex first. Someday... */
2272 if (0 && vlib_parse_eval (cf->input_vector) == 0)
2278 /* just treat whatever we got as a complete line of input */
2279 cf->current_command = cf->input_vector;
2283 /* Line edit, echo, etc. */
2284 if (unix_cli_line_edit (cm, um, fm, cf))
2285 /* want more input */
2292 vec_reset_length (lv);
2293 lv = format (lv, "%U[%d]: %v",
2294 format_timeval, 0 /* current bat-time */ ,
2295 0 /* current bat-format */ ,
2296 cli_file_index, cf->current_command);
2297 int rv __attribute__ ((unused)) = write (um->log_fd, lv, vec_len (lv));
2300 /* Build an unformat structure around our command */
2301 unformat_init_vector (&input, cf->current_command);
2303 /* Remove leading white space from input. */
2304 (void) unformat (&input, "");
2306 cf->pager_start = 0; /* start a new pager session */
2308 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
2309 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output,
2312 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
2315 unformat_free (&input);
2317 /* Re-fetch pointer since pool may have moved. */
2318 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2319 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2322 /* reset vector; we'll re-use it later */
2325 vec_reset_length (cf->input_vector);
2326 cf->current_command = 0;
2330 vec_reset_length (cf->current_command);
2333 if (cf->no_pager == 2)
2335 /* Pager was programmatically disabled */
2336 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
2337 cf->no_pager = um->cli_no_pager;
2340 if (vec_len (cf->pager_index) == 0
2341 || vec_len (cf->pager_index) < cf->height)
2343 /* There was no need for the pager */
2344 unix_cli_pager_reset (cf);
2347 unix_cli_cli_prompt (cf, uf);
2351 /* Display the pager prompt */
2352 unix_cli_pager_prompt (cf, uf);
2355 /* Any residual data in the input vector? */
2356 if (vec_len (cf->input_vector))
2359 /* For non-interactive sessions send a NUL byte.
2360 * Specifically this is because vppctl needs to see some traffic in
2361 * order to move on to closing the session. Commands with no output
2362 * would thus cause vppctl to hang indefinitely in non-interactive mode
2363 * since there is also no prompt sent after the command completes.
2365 if (!cf->is_interactive)
2366 unix_vlib_cli_output_raw (cf, uf, (u8 *) "\0", 1);
2369 /** Destroy a CLI session.
2370 * @note If we destroy the @c stdin session this additionally signals
2371 * the shutdown of VPP.
2374 unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
2376 unix_main_t *um = &unix_main;
2377 clib_file_main_t *fm = &file_main;
2378 unix_cli_file_t *cf;
2382 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2383 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2385 /* Quit/EOF on stdin means quit program. */
2386 if (uf->file_descriptor == STDIN_FILENO)
2387 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
2389 vec_free (cf->current_command);
2390 vec_free (cf->search_key);
2392 for (i = 0; i < vec_len (cf->command_history); i++)
2393 vec_free (cf->command_history[i]);
2395 vec_free (cf->command_history);
2397 clib_file_del (fm, uf);
2399 unix_cli_file_free (cf);
2400 pool_put (cm->cli_file_pool, cf);
2403 /** Handle system events. */
2405 unix_cli_process (vlib_main_t * vm,
2406 vlib_node_runtime_t * rt, vlib_frame_t * f)
2408 unix_cli_main_t *cm = &unix_cli_main;
2413 unix_cli_process_event_type_t event_type;
2414 vlib_process_wait_for_event (vm);
2415 event_type = vlib_process_get_events (vm, &data);
2419 case UNIX_CLI_PROCESS_EVENT_READ_READY:
2420 for (i = 0; i < vec_len (data); i++)
2421 unix_cli_process_input (cm, data[i]);
2424 case UNIX_CLI_PROCESS_EVENT_QUIT:
2425 /* Kill this process. */
2426 for (i = 0; i < vec_len (data); i++)
2427 unix_cli_kill (cm, data[i]);
2432 _vec_len (data) = 0;
2438 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
2440 /* Add node index so we can re-use this process later. */
2441 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
2446 /** Called when a CLI session file descriptor can be written to without
2448 static clib_error_t *
2449 unix_cli_write_ready (clib_file_t * uf)
2451 unix_cli_main_t *cm = &unix_cli_main;
2452 unix_cli_file_t *cf;
2455 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2457 /* Flush output vector. */
2459 /* If it's a socket we use MSG_NOSIGNAL to prevent SIGPIPE */
2460 n = send (uf->file_descriptor,
2461 cf->output_vector, vec_len (cf->output_vector), MSG_NOSIGNAL);
2463 n = write (uf->file_descriptor,
2464 cf->output_vector, vec_len (cf->output_vector));
2466 if (n < 0 && errno != EAGAIN)
2470 /* connection closed on us */
2471 unix_main_t *um = &unix_main;
2473 vlib_process_signal_event (um->vlib_main, cf->process_node_index,
2474 UNIX_CLI_PROCESS_EVENT_QUIT,
2479 return clib_error_return_unix (0, "write");
2484 unix_cli_del_pending_output (uf, cf, n);
2486 return /* no error */ 0;
2489 /** Called when a CLI session file descriptor has data to be read. */
2490 static clib_error_t *
2491 unix_cli_read_ready (clib_file_t * uf)
2493 unix_main_t *um = &unix_main;
2494 unix_cli_main_t *cm = &unix_cli_main;
2495 unix_cli_file_t *cf;
2497 int n, n_read, n_try;
2499 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2504 l = vec_len (cf->input_vector);
2505 vec_resize (cf->input_vector, l + n_try);
2507 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
2510 if (n < 0 && errno != EAGAIN)
2511 return clib_error_return_unix (0, "read");
2513 n_read = n < 0 ? 0 : n;
2514 _vec_len (cf->input_vector) = l + n_read;
2518 vlib_process_signal_event (um->vlib_main,
2519 cf->process_node_index,
2521 ? UNIX_CLI_PROCESS_EVENT_QUIT
2522 : UNIX_CLI_PROCESS_EVENT_READ_READY),
2523 /* event data */ uf->private_data);
2525 return /* no error */ 0;
2528 /** Called when a CLI session file descriptor has an error condition. */
2529 static clib_error_t *
2530 unix_cli_error_detected (clib_file_t * uf)
2532 unix_main_t *um = &unix_main;
2533 unix_cli_main_t *cm = &unix_cli_main;
2534 unix_cli_file_t *cf;
2536 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
2537 cf->has_epipe = 1; /* prevent writes while the close is pending */
2538 vlib_process_signal_event (um->vlib_main,
2539 cf->process_node_index,
2540 UNIX_CLI_PROCESS_EVENT_QUIT,
2541 /* event data */ uf->private_data);
2543 return /* no error */ 0;
2546 /** Store a new CLI session.
2547 * @param name The name of the session.
2548 * @param fd The file descriptor for the session I/O.
2549 * @return The session ID.
2552 unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
2554 unix_main_t *um = &unix_main;
2555 clib_file_main_t *fm = &file_main;
2556 unix_cli_file_t *cf;
2557 clib_file_t template = { 0 };
2558 vlib_main_t *vm = um->vlib_main;
2562 file_desc = format (0, "%s", name);
2564 name = (char *) format (0, "unix-cli-%s", name);
2566 if (vec_len (cm->unused_cli_process_node_indices) > 0)
2568 uword l = vec_len (cm->unused_cli_process_node_indices);
2570 /* Find node and give it new name. */
2571 n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
2573 n->name = (u8 *) name;
2575 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
2577 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
2581 static vlib_node_registration_t r = {
2582 .function = unix_cli_process,
2583 .type = VLIB_NODE_TYPE_PROCESS,
2584 .process_log2_n_stack_bytes = 16,
2588 vlib_register_node (vm, &r);
2591 n = vlib_get_node (vm, r.index);
2594 pool_get (cm->cli_file_pool, cf);
2595 memset (cf, 0, sizeof (*cf));
2597 template.read_function = unix_cli_read_ready;
2598 template.write_function = unix_cli_write_ready;
2599 template.error_function = unix_cli_error_detected;
2600 template.file_descriptor = fd;
2601 template.private_data = cf - cm->cli_file_pool;
2602 template.description = file_desc;
2604 cf->process_node_index = n->index;
2605 cf->clib_file_index = clib_file_add (fm, &template);
2606 cf->output_vector = 0;
2607 cf->input_vector = 0;
2609 vlib_start_process (vm, n->runtime_index);
2611 vlib_process_t *p = vlib_get_process_from_node (vm, n);
2612 p->output_function = unix_vlib_cli_output;
2613 p->output_function_arg = cf - cm->cli_file_pool;
2615 return cf - cm->cli_file_pool;
2618 /** Telnet listening socket has a new connection. */
2619 static clib_error_t *
2620 unix_cli_listen_read_ready (clib_file_t * uf)
2622 unix_main_t *um = &unix_main;
2623 clib_file_main_t *fm = &file_main;
2624 unix_cli_main_t *cm = &unix_cli_main;
2625 clib_socket_t *s = &um->cli_listen_socket;
2626 clib_socket_t client;
2628 clib_error_t *error;
2629 unix_cli_file_t *cf;
2632 error = clib_socket_accept (s, &client);
2636 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
2638 cf_index = unix_cli_file_add (cm, client_name, client.fd);
2639 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2642 /* No longer need CLIB version of socket. */
2643 clib_socket_free (&client);
2644 vec_free (client_name);
2646 /* if we're supposed to run telnet session in character mode (default) */
2647 if (um->cli_line_mode == 0)
2650 * Set telnet client character mode, echo on, suppress "go-ahead".
2651 * Technically these should be negotiated, but this works.
2653 u8 charmode_option[] = {
2654 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
2655 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
2656 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
2657 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
2658 IAC, WILL, TELOPT_ECHO, /* server will do echo */
2659 IAC, DONT, TELOPT_ECHO, /* client should not echo */
2660 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
2661 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
2662 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
2663 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
2666 /* Enable history on this CLI */
2667 cf->history_limit = um->cli_history_limit;
2668 cf->has_history = cf->history_limit != 0;
2670 /* This is an interactive session until we decide otherwise */
2671 cf->is_interactive = 1;
2673 /* Make sure this session is in line mode */
2679 /* Setup the pager */
2680 cf->no_pager = um->cli_no_pager;
2682 /* Default terminal dimensions, should the terminal
2683 * fail to provide any.
2685 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
2686 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
2688 /* Send the telnet options */
2689 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2690 unix_vlib_cli_output_raw (cf, uf, charmode_option,
2691 ARRAY_LEN (charmode_option));
2693 /* In case the client doesn't negotiate terminal type, use
2694 * a timer to kick off the initial prompt. */
2695 timer_call (unix_cli_file_welcome_timer, cf_index, 1);
2701 /** The system terminal has informed us that the window size
2705 unix_cli_resize_interrupt (int signum)
2707 clib_file_main_t *fm = &file_main;
2708 unix_cli_main_t *cm = &unix_cli_main;
2709 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2710 cm->stdin_cli_file_index);
2711 clib_file_t *uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
2715 /* Terminal resized, fetch the new size */
2716 if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) < 0)
2718 /* "Should never happen..." */
2719 clib_unix_warning ("TIOCGWINSZ");
2720 /* We can't trust ws.XXX... */
2724 cf->width = ws.ws_col;
2725 if (cf->width > UNIX_CLI_MAX_TERMINAL_WIDTH)
2726 cf->width = UNIX_CLI_MAX_TERMINAL_WIDTH;
2728 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
2730 cf->height = ws.ws_row;
2731 if (cf->height > UNIX_CLI_MAX_TERMINAL_HEIGHT)
2732 cf->height = UNIX_CLI_MAX_TERMINAL_HEIGHT;
2733 if (cf->height == 0)
2734 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
2736 /* Reindex the pager buffer */
2737 unix_cli_pager_reindex (cf);
2739 /* Redraw the page */
2740 unix_cli_pager_redraw (cf, uf);
2743 /** Handle configuration directives in the @em unix section. */
2744 static clib_error_t *
2745 unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
2747 unix_main_t *um = &unix_main;
2748 clib_file_main_t *fm = &file_main;
2749 unix_cli_main_t *cm = &unix_cli_main;
2751 clib_error_t *error = 0;
2752 unix_cli_file_t *cf;
2755 struct sigaction sa;
2759 /* We depend on unix flags being set. */
2760 if ((error = vlib_call_config_function (vm, unix_config)))
2763 if (um->flags & UNIX_FLAG_INTERACTIVE)
2765 /* Set stdin to be non-blocking. */
2766 if ((flags = fcntl (STDIN_FILENO, F_GETFL, 0)) < 0)
2768 (void) fcntl (STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
2770 cf_index = unix_cli_file_add (cm, "stdin", STDIN_FILENO);
2771 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2772 cm->stdin_cli_file_index = cf_index;
2774 /* If stdin is a tty and we are using chacracter mode, enable
2775 * history on the CLI and set the tty line discipline accordingly. */
2776 if (isatty (STDIN_FILENO) && um->cli_line_mode == 0)
2778 /* Capture terminal resize events */
2779 memset (&sa, 0, sizeof (sa));
2780 sa.sa_handler = unix_cli_resize_interrupt;
2781 if (sigaction (SIGWINCH, &sa, 0) < 0)
2782 clib_panic ("sigaction");
2784 /* Retrieve the current terminal size */
2785 ioctl (STDIN_FILENO, TIOCGWINSZ, &ws);
2786 cf->width = ws.ws_col;
2787 cf->height = ws.ws_row;
2789 if (cf->width == 0 || cf->height == 0)
2792 * We have a tty, but no size. Use defaults.
2793 * vpp "unix interactive" inside emacs + gdb ends up here.
2795 cf->width = UNIX_CLI_DEFAULT_TERMINAL_WIDTH;
2796 cf->height = UNIX_CLI_DEFAULT_TERMINAL_HEIGHT;
2799 /* Setup the history */
2800 cf->history_limit = um->cli_history_limit;
2801 cf->has_history = cf->history_limit != 0;
2803 /* Setup the pager */
2804 cf->no_pager = um->cli_no_pager;
2806 /* This is an interactive session until we decide otherwise */
2807 cf->is_interactive = 1;
2809 /* We're going to be in char by char mode */
2812 /* Save the original tty state so we can restore it later */
2813 tcgetattr (STDIN_FILENO, &um->tio_stdin);
2816 /* Tweak the tty settings */
2817 tio = um->tio_stdin;
2818 /* echo off, canonical mode off, ext'd input processing off */
2819 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
2820 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
2821 tio.c_cc[VTIME] = 0; /* no timer */
2822 tcsetattr (STDIN_FILENO, TCSAFLUSH, &tio);
2824 /* See if we can do ANSI/VT100 output */
2825 term = (u8 *) getenv ("TERM");
2828 int len = strlen ((char *) term);
2829 cf->ansi_capable = unix_cli_terminal_type_ansi (term, len);
2830 if (unix_cli_terminal_type_noninteractive (term, len))
2831 unix_cli_set_session_noninteractive (cf);
2836 /* No tty, so make sure the session doesn't have tty-like features */
2837 unix_cli_set_session_noninteractive (cf);
2840 /* Send banner and initial prompt */
2841 unix_cli_file_welcome (cm, cf);
2844 /* If we have socket config, LISTEN, otherwise, don't */
2845 clib_socket_t *s = &um->cli_listen_socket;
2846 if (s->config && s->config[0] != 0)
2849 clib_file_t template = { 0 };
2851 /* mkdir of file socketu, only under /run */
2852 if (strncmp (s->config, "/run", 4) == 0)
2854 u8 *tmp = format (0, "%s", s->config);
2855 int i = vec_len (tmp);
2856 while (i && tmp[--i] != '/')
2862 vlib_unix_recursive_mkdir ((char *) tmp);
2866 s->flags = CLIB_SOCKET_F_IS_SERVER | /* listen, don't connect */
2867 CLIB_SOCKET_F_ALLOW_GROUP_WRITE; /* PF_LOCAL socket only */
2868 error = clib_socket_init (s);
2873 template.read_function = unix_cli_listen_read_ready;
2874 template.file_descriptor = s->fd;
2875 template.description = format (0, "cli listener %s", s->config);
2877 clib_file_add (fm, &template);
2880 /* Set CLI prompt. */
2881 if (!cm->cli_prompt)
2882 cm->cli_prompt = format (0, "VLIB: ");
2888 * This module has no configurable parameters.
2890 VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
2892 /** Called when VPP is shutting down, this restores the system
2893 * terminal state if previously saved.
2895 static clib_error_t *
2896 unix_cli_exit (vlib_main_t * vm)
2898 unix_main_t *um = &unix_main;
2900 /* If stdin is a tty and we saved the tty state, reset the tty state */
2901 if (isatty (STDIN_FILENO) && um->tio_isset)
2902 tcsetattr (STDIN_FILENO, TCSAFLUSH, &um->tio_stdin);
2907 VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
2909 /** Set the CLI prompt.
2910 * @param prompt The C string to set the prompt to.
2911 * @note This setting is global; it impacts all current
2912 * and future CLI sessions.
2915 vlib_unix_cli_set_prompt (char *prompt)
2917 char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s ";
2918 unix_cli_main_t *cm = &unix_cli_main;
2920 vec_free (cm->cli_prompt);
2921 cm->cli_prompt = format (0, fmt, prompt);
2924 /** CLI command to quit the terminal session.
2925 * @note If this is a stdin session then this will
2926 * shutdown VPP also.
2928 static clib_error_t *
2929 unix_cli_quit (vlib_main_t * vm,
2930 unformat_input_t * input, vlib_cli_command_t * cmd)
2932 unix_cli_main_t *cm = &unix_cli_main;
2933 unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
2934 cm->current_input_file_index);
2936 /* Cosmetic: suppress the final prompt from appearing before we die */
2937 cf->is_interactive = 0;
2940 vlib_process_signal_event (vm,
2941 vlib_current_process (vm),
2942 UNIX_CLI_PROCESS_EVENT_QUIT,
2943 cm->current_input_file_index);
2948 * Terminates the current CLI session.
2950 * If VPP is running in @em interactive mode and this is the console session
2951 * (that is, the session on @c stdin) then this will also terminate VPP.
2954 VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
2956 .short_help = "Exit CLI",
2957 .function = unix_cli_quit,
2962 VLIB_CLI_COMMAND (unix_cli_q_command, static) = {
2964 .short_help = "Exit CLI",
2965 .function = unix_cli_quit,
2969 /** CLI command to execute a VPP command script. */
2970 static clib_error_t *
2971 unix_cli_exec (vlib_main_t * vm,
2972 unformat_input_t * input, vlib_cli_command_t * cmd)
2976 unformat_input_t sub_input;
2977 clib_error_t *error;
2983 if (!unformat (input, "%s", &file_name))
2985 error = clib_error_return (0, "expecting file name, got `%U'",
2986 format_unformat_error, input);
2990 fd = open (file_name, O_RDONLY);
2993 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
2997 /* Make sure its a regular file. */
3001 if (fstat (fd, &s) < 0)
3003 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
3007 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
3009 error = clib_error_return (0, "not a regular file `%s'", file_name);
3014 unformat_init_clib_file (&sub_input, fd);
3016 vlib_cli_input (vm, &sub_input, 0, 0);
3017 unformat_free (&sub_input);
3022 vec_free (file_name);
3028 * Executes a sequence of CLI commands which are read from a file. If
3029 * a command is unrecognised or otherwise invalid then the usual CLI
3030 * feedback will be generated, however execution of subsequent commands
3031 * from the file will continue.
3033 * The VPP code is indifferent to the file location. However, if SELinux
3034 * is enabled, then the file needs to have an SELinux label the VPP
3035 * process is allowed to access. For example, if a file is created in
3036 * '<em>/usr/share/vpp/</em>', it will be allowed. However, files manually
3037 * created in '/tmp/' or '/home/<user>/' will not be accessible by the VPP
3038 * process when SELinux is enabled.
3043 * <b><em>$ cat /usr/share/vpp/scripts/gigup.txt</em></b>
3044 * set interface state GigabitEthernet0/8/0 up
3045 * set interface state GigabitEthernet0/9/0 up
3047 * Example of how to execute a set of CLI commands from a file:
3048 * @cliexcmd{exec /usr/share/vpp/scripts/gigup.txt}
3051 VLIB_CLI_COMMAND (cli_exec, static) = {
3053 .short_help = "exec <filename>",
3054 .function = unix_cli_exec,
3059 /** CLI command to show various unix error statistics. */
3060 static clib_error_t *
3061 unix_show_errors (vlib_main_t * vm,
3062 unformat_input_t * input, vlib_cli_command_t * cmd)
3064 unix_main_t *um = &unix_main;
3065 clib_error_t *error = 0;
3066 int i, n_errors_to_show;
3067 unix_error_history_t *unix_errors = 0;
3069 n_errors_to_show = 1 << 30;
3071 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
3073 if (!unformat (input, "%d", &n_errors_to_show))
3076 clib_error_return (0,
3077 "expecting integer number of errors to show, got `%U'",
3078 format_unformat_error, input);
3084 clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
3087 um->error_history_index >
3088 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
3090 while (n_errors_to_show > 0)
3092 unix_error_history_t *eh = um->error_history + i;
3097 vec_add1 (unix_errors, eh[0]);
3098 n_errors_to_show -= 1;
3100 i = ARRAY_LEN (um->error_history) - 1;
3105 if (vec_len (unix_errors) == 0)
3106 vlib_cli_output (vm, "no Unix errors so far");
3109 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
3110 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
3112 unix_error_history_t *eh = vec_elt_at_index (unix_errors, i);
3113 vlib_cli_output (vm, "%U: %U",
3114 format_time_interval, "h:m:s:u", eh->time,
3115 format_clib_error, eh->error);
3117 vlib_cli_output (vm, "%U: time now",
3118 format_time_interval, "h:m:s:u", vlib_time_now (vm));
3122 vec_free (unix_errors);
3127 VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
3128 .path = "show unix errors",
3129 .short_help = "Show Unix system call error history",
3130 .function = unix_show_errors,
3134 /** CLI command to show various unix error statistics. */
3135 static clib_error_t *
3136 unix_show_files (vlib_main_t * vm,
3137 unformat_input_t * input, vlib_cli_command_t * cmd)
3139 clib_error_t *error = 0;
3140 clib_file_main_t *fm = &file_main;
3142 char path[PATH_MAX];
3145 vlib_cli_output (vm, "%3s %6s %12s %12s %12s %-32s %s", "FD", "Thread",
3146 "Read", "Write", "Error", "File Name", "Description");
3149 pool_foreach (f, fm->file_pool,(
3152 s = format (s, "/proc/self/fd/%d%c", f->file_descriptor, 0);
3153 rv = readlink((char *) s, path, PATH_MAX - 1);
3155 path[rv < 0 ? 0 : rv] = 0;
3157 vlib_cli_output (vm, "%3d %6d %12d %12d %12d %-32s %v",
3158 f->file_descriptor, f->polling_thread_index,
3159 f->read_events, f->write_events, f->error_events,
3160 path, f->description);
3161 vec_reset_length (s);
3170 VLIB_CLI_COMMAND (cli_unix_show_files, static) = {
3171 .path = "show unix files",
3172 .short_help = "Show Unix files in use",
3173 .function = unix_show_files,
3177 /** CLI command to show session command history. */
3178 static clib_error_t *
3179 unix_cli_show_history (vlib_main_t * vm,
3180 unformat_input_t * input, vlib_cli_command_t * cmd)
3182 unix_cli_main_t *cm = &unix_cli_main;
3183 unix_cli_file_t *cf;
3186 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3188 if (!cf->is_interactive)
3189 return clib_error_return (0, "invalid for non-interactive sessions");
3191 if (cf->has_history && cf->history_limit)
3193 i = 1 + cf->command_number - vec_len (cf->command_history);
3194 for (j = 0; j < vec_len (cf->command_history); j++)
3195 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
3199 vlib_cli_output (vm, "History not enabled.\n");
3206 * Displays the command history for the current session, if any.
3209 VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
3211 .short_help = "Show current session command history",
3212 .function = unix_cli_show_history,
3216 /** CLI command to show terminal status. */
3217 static clib_error_t *
3218 unix_cli_show_terminal (vlib_main_t * vm,
3219 unformat_input_t * input, vlib_cli_command_t * cmd)
3221 unix_main_t *um = &unix_main;
3222 unix_cli_main_t *cm = &unix_cli_main;
3223 unix_cli_file_t *cf;
3226 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3227 n = vlib_get_node (vm, cf->process_node_index);
3229 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
3230 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
3231 "line-by-line" : "char-by-char");
3232 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
3233 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
3234 vlib_cli_output (vm, "ANSI capable: %s\n",
3235 cf->ansi_capable ? "yes" : "no");
3236 vlib_cli_output (vm, "Interactive: %s\n",
3237 cf->is_interactive ? "yes" : "no");
3238 vlib_cli_output (vm, "History enabled: %s%s\n",
3239 cf->has_history ? "yes" : "no", !cf->has_history
3240 || cf->history_limit ? "" :
3241 " (disabled by history limit)");
3242 if (cf->has_history)
3243 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
3244 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
3245 cf->no_pager ? "no" : "yes",
3247 || cf->height ? "" : " (disabled by terminal height)",
3249 || um->cli_pager_buffer_limit ? "" :
3250 " (disabled by buffer limit)");
3252 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
3253 vlib_cli_output (vm, "CRLF mode: %s\n",
3254 cf->crlf_mode ? "CR+LF" : "LF");
3260 * Displays various information about the state of the current terminal
3264 * @cliexstart{show terminal}
3265 * Terminal name: unix-cli-stdin
3266 * Terminal mode: char-by-char
3267 * Terminal width: 123
3268 * Terminal height: 48
3271 * History enabled: yes
3273 * Pager enabled: yes
3274 * Pager limit: 100000
3279 VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
3280 .path = "show terminal",
3281 .short_help = "Show current session terminal settings",
3282 .function = unix_cli_show_terminal,
3286 /** CLI command to display a list of CLI sessions. */
3287 static clib_error_t *
3288 unix_cli_show_cli_sessions (vlib_main_t * vm,
3289 unformat_input_t * input,
3290 vlib_cli_command_t * cmd)
3292 //unix_main_t *um = &unix_main;
3293 unix_cli_main_t *cm = &unix_cli_main;
3294 clib_file_main_t *fm = &file_main;
3295 unix_cli_file_t *cf;
3299 vlib_cli_output (vm, "%-5s %-5s %-20s %s", "PNI", "FD", "Name", "Flags");
3301 #define fl(x, y) ( (x) ? toupper((y)) : tolower((y)) )
3303 pool_foreach (cf, cm->cli_file_pool, ({
3304 uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
3305 n = vlib_get_node (vm, cf->process_node_index);
3306 vlib_cli_output (vm,
3307 "%-5d %-5d %-20v %c%c%c%c%c\n",
3308 cf->process_node_index,
3309 uf->file_descriptor,
3311 fl (cf->is_interactive, 'i'),
3312 fl (cf->is_socket, 's'),
3313 fl (cf->line_mode, 'l'),
3314 fl (cf->has_epipe, 'p'),
3315 fl (cf->ansi_capable, 'a'));
3324 * Displays a summary of all the current CLI sessions.
3326 * Typically used to diagnose connection issues with the CLI
3330 * @cliexstart{show cli-sessions}
3332 * 343 0 unix-cli-stdin IslpA
3333 * 344 7 unix-cli-local:20 ISlpA
3334 * 346 8 unix-cli-local:21 iSLpa
3337 * In this example we have the debug console of the running process
3338 * on stdin/out, we have an interactive socket session and we also
3339 * have a non-interactive socket session.
3343 * - @em PNI: Process node index.
3344 * - @em FD: Unix file descriptor.
3345 * - @em Name: Name of the session.
3346 * - @em Flags: Various flags that describe the state of the session.
3348 * @em Flags have the following meanings; lower-case typically negates
3351 * - @em I Interactive session.
3352 * - @em S Connected by socket.
3353 * - @em s Not a socket, likely stdin.
3354 * - @em L Line-by-line mode.
3355 * - @em l Char-by-char mode.
3356 * - @em P EPIPE detected on connection; it will close soon.
3357 * - @em A ANSI-capable terminal.
3360 VLIB_CLI_COMMAND (cli_unix_cli_show_cli_sessions, static) = {
3361 .path = "show cli-sessions",
3362 .short_help = "Show current CLI sessions",
3363 .function = unix_cli_show_cli_sessions,
3367 /** CLI command to set terminal pager settings. */
3368 static clib_error_t *
3369 unix_cli_set_terminal_pager (vlib_main_t * vm,
3370 unformat_input_t * input,
3371 vlib_cli_command_t * cmd)
3373 unix_main_t *um = &unix_main;
3374 unix_cli_main_t *cm = &unix_cli_main;
3375 unix_cli_file_t *cf;
3376 unformat_input_t _line_input, *line_input = &_line_input;
3377 clib_error_t *error = 0;
3379 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3381 if (!cf->is_interactive)
3382 return clib_error_return (0, "invalid for non-interactive sessions");
3384 if (!unformat_user (input, unformat_line_input, line_input))
3387 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3389 if (unformat (line_input, "on"))
3391 else if (unformat (line_input, "off"))
3393 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
3394 vlib_cli_output (vm,
3395 "Pager limit set to %u lines; note, this is global.\n",
3396 um->cli_pager_buffer_limit);
3399 error = clib_error_return (0, "unknown parameter: `%U`",
3400 format_unformat_error, line_input);
3406 unformat_free (line_input);
3412 * Enables or disables the terminal pager for this session. Generally
3413 * this defaults to enabled.
3415 * Additionally allows the pager buffer size to be set; though note that
3416 * this value is set globally and not per session.
3419 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
3420 .path = "set terminal pager",
3421 .short_help = "set terminal pager [on|off] [limit <lines>]",
3422 .function = unix_cli_set_terminal_pager,
3426 /** CLI command to set terminal history settings. */
3427 static clib_error_t *
3428 unix_cli_set_terminal_history (vlib_main_t * vm,
3429 unformat_input_t * input,
3430 vlib_cli_command_t * cmd)
3432 unix_cli_main_t *cm = &unix_cli_main;
3433 unix_cli_file_t *cf;
3434 unformat_input_t _line_input, *line_input = &_line_input;
3436 clib_error_t *error = 0;
3438 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3440 if (!cf->is_interactive)
3441 return clib_error_return (0, "invalid for non-interactive sessions");
3443 if (!unformat_user (input, unformat_line_input, line_input))
3446 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
3448 if (unformat (line_input, "on"))
3449 cf->has_history = 1;
3450 else if (unformat (line_input, "off"))
3451 cf->has_history = 0;
3452 else if (unformat (line_input, "limit %u", &cf->history_limit))
3456 error = clib_error_return (0, "unknown parameter: `%U`",
3457 format_unformat_error, line_input);
3461 /* If we reduced history size, or turned it off, purge the history */
3462 limit = cf->has_history ? cf->history_limit : 0;
3464 while (cf->command_history && vec_len (cf->command_history) >= limit)
3466 vec_free (cf->command_history[0]);
3467 vec_delete (cf->command_history, 1, 0);
3472 unformat_free (line_input);
3478 * Enables or disables the command history function of the current
3479 * terminal. Generally this defaults to enabled.
3481 * This command also allows the maximum size of the history buffer for
3482 * this session to be altered.
3485 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
3486 .path = "set terminal history",
3487 .short_help = "set terminal history [on|off] [limit <lines>]",
3488 .function = unix_cli_set_terminal_history,
3492 /** CLI command to set terminal ANSI settings. */
3493 static clib_error_t *
3494 unix_cli_set_terminal_ansi (vlib_main_t * vm,
3495 unformat_input_t * input,
3496 vlib_cli_command_t * cmd)
3498 unix_cli_main_t *cm = &unix_cli_main;
3499 unix_cli_file_t *cf;
3501 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
3503 if (!cf->is_interactive)
3504 return clib_error_return (0, "invalid for non-interactive sessions");
3506 if (unformat (input, "on"))
3507 cf->ansi_capable = 1;
3508 else if (unformat (input, "off"))
3509 cf->ansi_capable = 0;
3511 return clib_error_return (0, "unknown parameter: `%U`",
3512 format_unformat_error, input);
3518 * Enables or disables the use of ANSI control sequences by this terminal.
3519 * The default will vary based on terminal detection at the start of the
3522 * ANSI control sequences are used in a small number of places to provide,
3523 * for example, color text output and to control the cursor in the pager.
3526 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
3527 .path = "set terminal ansi",
3528 .short_help = "set terminal ansi [on|off]",
3529 .function = unix_cli_set_terminal_ansi,
3533 static clib_error_t *
3534 unix_cli_init (vlib_main_t * vm)
3539 VLIB_INIT_FUNCTION (unix_cli_init);
3542 * fd.io coding-style-patch-verification: ON
3545 * eval: (c-set-style "gnu")