2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * cli.c: Unix stdin/socket CLI.
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vlib/vlib.h>
41 #include <vlib/unix/unix.h>
42 #include <vppinfra/timer.h>
50 #include <arpa/telnet.h>
51 #include <sys/ioctl.h>
53 /* ANSI Escape code. */
56 /* ANSI Control Sequence Introducer. */
60 #define ANSI_CLEAR CSI "2J" CSI "1;1H"
61 #define ANSI_RESET CSI "0m"
62 #define ANSI_BOLD CSI "1m"
63 #define ANSI_DIM CSI "2m"
64 #define ANSI_DRED ANSI_DIM CSI "31m"
65 #define ANSI_BRED ANSI_BOLD CSI "31m"
66 #define ANSI_CLEAR CSI "2J" CSI "1;1H"
67 #define ANSI_CLEARLINE CSI "2K"
68 #define ANSI_SCROLLDN CSI "1T"
69 #define ANSI_SAVECURSOR CSI "s"
70 #define ANSI_RESTCURSOR CSI "u"
72 /** Maximum depth into a byte stream from which to compile a Telnet
73 * protocol message. This is a saftey measure. */
74 #define UNIX_CLI_MAX_DEPTH_TELNET 24
76 /** Unix standard in */
77 #define UNIX_CLI_STDIN_FD 0
85 #define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
86 /** Plain welcome banner. */
87 static unix_cli_banner_t unix_cli_banner[] = {
88 _(" _______ _ _ _____ ___ \n"),
89 _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
90 _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
91 _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
94 /** ANSI color welcome banner. */
95 static unix_cli_banner_t unix_cli_banner_color[] = {
96 _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
97 _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
98 _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
99 _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"),
105 /** Unix CLI session. */
109 /* Vector of output pending write to file descriptor. */
112 /* Vector of input saved by Unix input node to be processed by
117 u8 ** command_history;
118 u8 * current_command;
121 /* Maximum number of history entries this session will store. */
124 /* Current command line counter */
130 /* Position of the insert cursor on the current input line */
133 /* Line mode or char mode */
136 /* Set if the CRLF mode wants CR + LF */
139 /* Can we do ANSI output? */
142 /* Has the session started? */
145 /* Disable the pager? */
151 /* Lines currently displayed */
154 /* Line number of top of page */
160 u32 process_node_index;
164 unix_cli_pager_reset (unix_cli_file_t *f)
168 f->pager_lines = f->pager_start = 0;
169 vec_foreach (p, f->pager_vector)
173 vec_free(f->pager_vector);
178 unix_cli_file_free (unix_cli_file_t * f)
180 vec_free (f->output_vector);
181 vec_free (f->input_vector);
182 unix_cli_pager_reset(f);
187 UNIX_CLI_PARSE_ACTION_NOACTION = 0,
188 UNIX_CLI_PARSE_ACTION_CRLF,
189 UNIX_CLI_PARSE_ACTION_TAB,
190 UNIX_CLI_PARSE_ACTION_ERASE,
191 UNIX_CLI_PARSE_ACTION_ERASERIGHT,
192 UNIX_CLI_PARSE_ACTION_UP,
193 UNIX_CLI_PARSE_ACTION_DOWN,
194 UNIX_CLI_PARSE_ACTION_LEFT,
195 UNIX_CLI_PARSE_ACTION_RIGHT,
196 UNIX_CLI_PARSE_ACTION_HOME,
197 UNIX_CLI_PARSE_ACTION_END,
198 UNIX_CLI_PARSE_ACTION_WORDLEFT,
199 UNIX_CLI_PARSE_ACTION_WORDRIGHT,
200 UNIX_CLI_PARSE_ACTION_ERASELINELEFT,
201 UNIX_CLI_PARSE_ACTION_ERASELINERIGHT,
202 UNIX_CLI_PARSE_ACTION_CLEAR,
203 UNIX_CLI_PARSE_ACTION_REVSEARCH,
204 UNIX_CLI_PARSE_ACTION_FWDSEARCH,
205 UNIX_CLI_PARSE_ACTION_YANK,
206 UNIX_CLI_PARSE_ACTION_TELNETIAC,
208 UNIX_CLI_PARSE_ACTION_PAGER_CRLF,
209 UNIX_CLI_PARSE_ACTION_PAGER_QUIT,
210 UNIX_CLI_PARSE_ACTION_PAGER_NEXT,
211 UNIX_CLI_PARSE_ACTION_PAGER_DN,
212 UNIX_CLI_PARSE_ACTION_PAGER_UP,
213 UNIX_CLI_PARSE_ACTION_PAGER_TOP,
214 UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM,
215 UNIX_CLI_PARSE_ACTION_PAGER_PGDN,
216 UNIX_CLI_PARSE_ACTION_PAGER_PGUP,
217 UNIX_CLI_PARSE_ACTION_PAGER_SEARCH,
219 UNIX_CLI_PARSE_ACTION_PARTIALMATCH,
220 UNIX_CLI_PARSE_ACTION_NOMATCH
221 } unix_cli_parse_action_t;
223 /** \brief Mapping of input buffer strings to action values.
224 * @note This won't work as a hash since we need to be able to do
225 * partial matches on the string.
228 u8 *input; /**< Input string to match. */
229 u32 len; /**< Length of input without final NUL. */
230 unix_cli_parse_action_t action; /**< Action to take when matched. */
231 } unix_cli_parse_actions_t;
233 /** \brief Given a capital ASCII letter character return a NUL terminated
234 * string with the control code for that letter.
235 * \example CTL('A') returns { 0x01, 0x00 } as a u8[].
237 #define CTL(c) (u8[]){ (c) - '@', 0 }
239 #define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
240 static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
242 _( "\r\n", UNIX_CLI_PARSE_ACTION_CRLF ), /* Must be before '\r' */
243 _( "\n", UNIX_CLI_PARSE_ACTION_CRLF ),
244 _( "\r\0", UNIX_CLI_PARSE_ACTION_CRLF ), /* Telnet does this */
245 _( "\r", UNIX_CLI_PARSE_ACTION_CRLF ),
247 /* Unix shell control codes */
248 _( CTL('B'), UNIX_CLI_PARSE_ACTION_LEFT ),
249 _( CTL('F'), UNIX_CLI_PARSE_ACTION_RIGHT ),
250 _( CTL('P'), UNIX_CLI_PARSE_ACTION_UP ),
251 _( CTL('N'), UNIX_CLI_PARSE_ACTION_DOWN ),
252 _( CTL('A'), UNIX_CLI_PARSE_ACTION_HOME ),
253 _( CTL('E'), UNIX_CLI_PARSE_ACTION_END ),
254 _( CTL('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT ),
255 _( CTL('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT ),
256 _( CTL('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT ),
257 _( CTL('Y'), UNIX_CLI_PARSE_ACTION_YANK ),
258 _( CTL('L'), UNIX_CLI_PARSE_ACTION_CLEAR ),
259 _( ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT ), /* Alt-B */
260 _( ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT ), /* Alt-F */
261 _( "\b", UNIX_CLI_PARSE_ACTION_ERASE ), /* ^H */
262 _( "\x7f", UNIX_CLI_PARSE_ACTION_ERASE ), /* Backspace */
263 _( "\t", UNIX_CLI_PARSE_ACTION_TAB ), /* ^I */
265 /* VT100 Normal mode - Broadest support */
266 _( CSI "A", UNIX_CLI_PARSE_ACTION_UP ),
267 _( CSI "B", UNIX_CLI_PARSE_ACTION_DOWN ),
268 _( CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT ),
269 _( CSI "D", UNIX_CLI_PARSE_ACTION_LEFT ),
270 _( CSI "H", UNIX_CLI_PARSE_ACTION_HOME ),
271 _( CSI "F", UNIX_CLI_PARSE_ACTION_END ),
272 _( CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT ), /* Delete */
273 _( CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT ), /* C-Left */
274 _( CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT ),/* C-Right */
276 /* VT100 Application mode - Some Gnome Terminal functions use these */
277 _( ESC "OA", UNIX_CLI_PARSE_ACTION_UP ),
278 _( ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN ),
279 _( ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT ),
280 _( ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT ),
281 _( ESC "OH", UNIX_CLI_PARSE_ACTION_HOME ),
282 _( ESC "OF", UNIX_CLI_PARSE_ACTION_END ),
284 /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
285 _( CSI "1~", UNIX_CLI_PARSE_ACTION_HOME ),
286 _( CSI "4~", UNIX_CLI_PARSE_ACTION_END ),
288 /* Emacs-ish history search */
289 _( CTL('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH ),
290 _( CTL('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH ),
292 /* Other protocol things */
293 _( "\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC ), /* IAC */
294 _( "\0", UNIX_CLI_PARSE_ACTION_NOACTION ), /* NUL */
295 _( NULL, UNIX_CLI_PARSE_ACTION_NOMATCH )
297 static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
299 _( "\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ), /* Must be before '\r' */
300 _( "\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ),
301 _( "\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ), /* Telnet does this */
302 _( "\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ),
305 _( " ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT ),
306 _( "q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT ),
307 _( "/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH ),
310 _( CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP ),
311 _( CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN ),
312 _( CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP ),
313 _( CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM ),
315 /* VT100 Application mode */
316 _( ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP ),
317 _( ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN ),
318 _( ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP ),
319 _( ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM ),
321 /* ANSI X3.41-1974 */
322 _( CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP ),
323 _( CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM ),
324 _( CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP ),
325 _( CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN ),
327 /* Other protocol things */
328 _( "\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC ), /* IAC */
329 _( "\0", UNIX_CLI_PARSE_ACTION_NOACTION ), /* NUL */
330 _( NULL, UNIX_CLI_PARSE_ACTION_NOMATCH )
335 UNIX_CLI_PROCESS_EVENT_READ_READY,
336 UNIX_CLI_PROCESS_EVENT_QUIT,
337 } unix_cli_process_event_type_t;
340 /* Prompt string for CLI. */
343 unix_cli_file_t * cli_file_pool;
345 u32 * unused_cli_process_node_indices;
347 /* The session index of the stdin cli */
348 u32 stdin_cli_file_index;
350 /* File pool index of current input. */
351 u32 current_input_file_index;
354 static unix_cli_main_t unix_cli_main;
357 * \brief Search for a byte sequence in the action list.
359 * Searches unix_cli_parse_actions[] for a match with the bytes in \c input
360 * of maximum length \c ilen . When a match is made \c *matched indicates how
361 * many bytes were matched. Returns a value from the enum
362 * \c unix_cli_parse_action_t to indicate whether no match was found, a
363 * partial match was found or a complete match was found and what action,
364 * if any, should be taken.
366 * @param a Actions list to search within.
367 * @param input String fragment to search for.
368 * @param ilen Length of the string in 'input'.
369 * @param matched Pointer to an integer that will contain the number of
370 * bytes matched when a complete match is found.
372 * @return Action from \v unix_cli_parse_action_t that the string fragment
374 * \c UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the whole
375 * input string matches the start of at least one action.
376 * \c UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
379 static unix_cli_parse_action_t
380 unix_cli_match_action(unix_cli_parse_actions_t *a,
381 u8 *input, u32 ilen, i32 *matched)
389 /* see if the start of the input buffer exactly matches the current
391 if (memcmp(input, a->input, a->len) == 0)
399 /* if the first ilen characters match, flag this as a partial -
400 * meaning keep collecting bytes in case of a future match */
401 if (memcmp(input, a->input, ilen) == 0)
405 /* check next action */
410 UNIX_CLI_PARSE_ACTION_PARTIALMATCH :
411 UNIX_CLI_PARSE_ACTION_NOMATCH;
416 unix_cli_add_pending_output (unix_file_t * uf,
417 unix_cli_file_t * cf,
421 unix_main_t * um = &unix_main;
423 vec_add (cf->output_vector, buffer, buffer_bytes);
424 if (vec_len (cf->output_vector) > 0)
426 int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
427 uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
429 um->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
434 unix_cli_del_pending_output (unix_file_t * uf,
435 unix_cli_file_t * cf,
438 unix_main_t * um = &unix_main;
440 vec_delete (cf->output_vector, n_bytes, 0);
441 if (vec_len (cf->output_vector) <= 0)
443 int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
444 uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
446 um->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
450 /** \brief A bit like strchr with a buffer length limit.
451 * Search a buffer for the first instance of a character up to the limit of
452 * the buffer length. If found then return the position of that character.
454 * The key departure from strchr is that if the character is not found then
455 * return the buffer length.
457 * @param chr The byte value to search for.
458 * @param str The buffer in which to search for the value.
459 * @param len The depth into the buffer to search.
461 * @return The index of the first occurence of \c chr. If \c chr is not
462 * found then \c len instead.
464 always_inline word unix_vlib_findchr(u8 chr, u8 *str, word len)
467 for (i = 0; i < len; i++, str++)
475 /** \brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
476 * Attempts to write given buffer to the file descriptor of the given
477 * Unix CLI session. If that session already has data in the output buffer
478 * or if the write attempt tells us to try again later then the given buffer
479 * is appended to the pending output buffer instead.
481 * This is typically called only from \c unix_vlib_cli_output_cooked since
482 * that is where CRLF handling occurs or from places where we explicitly do
483 * not want cooked handling.
485 * @param cf Unix CLI session of the desired stream to write to.
486 * @param uf The Unix file structure of the desired stream to write to.
487 * @param buffer Pointer to the buffer that needs to be written.
488 * @param buffer_bytes The number of bytes from \c buffer to write.
490 static void unix_vlib_cli_output_raw(unix_cli_file_t * cf,
497 if (vec_len (cf->output_vector) == 0)
498 n = write (uf->file_descriptor, buffer, buffer_bytes);
500 if (n < 0 && errno != EAGAIN)
502 clib_unix_warning ("write");
504 else if ((word) n < (word) buffer_bytes)
506 /* We got EAGAIN or we already have stuff in the buffer;
507 * queue up whatever didn't get sent for later. */
509 unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
513 /** \brief Process a buffer for CRLF handling before outputting it to the CLI.
515 * @param cf Unix CLI session of the desired stream to write to.
516 * @param uf The Unix file structure of the desired stream to write to.
517 * @param buffer Pointer to the buffer that needs to be written.
518 * @param buffer_bytes The number of bytes from \c buffer to write.
520 static void unix_vlib_cli_output_cooked(unix_cli_file_t * cf,
525 word end = 0, start = 0;
527 while (end < buffer_bytes)
531 /* iterate the line on \n's so we can insert a \r before it */
532 end = unix_vlib_findchr('\n',
534 buffer_bytes - start) + start;
538 /* otherwise just send the whole buffer */
542 unix_vlib_cli_output_raw(cf, uf, buffer + start, end - start);
546 if (end < buffer_bytes)
548 unix_vlib_cli_output_raw(cf, uf, (u8 *)"\r\n", 2);
549 end ++; /* skip the \n that we already sent */
556 /** \brief Output the CLI prompt */
557 static void unix_cli_cli_prompt(unix_cli_file_t * cf, unix_file_t * uf)
559 unix_cli_main_t * cm = &unix_cli_main;
561 unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt, vec_len (cm->cli_prompt));
564 /** \brief Output a pager prompt and show number of buffered lines */
565 static void unix_cli_pager_prompt(unix_cli_file_t * cf, unix_file_t * uf)
569 prompt = format(0, "\r%s-- more -- (%d-%d/%d)%s",
570 cf->ansi_capable ? ANSI_BOLD : "",
572 cf->pager_start + cf->height,
574 cf->ansi_capable ? ANSI_RESET: "");
576 unix_vlib_cli_output_cooked(cf, uf, prompt, vec_len(prompt));
581 /** \brief Output a pager "skipping" message */
582 static void unix_cli_pager_message(unix_cli_file_t * cf, unix_file_t * uf,
583 char *message, char *postfix)
587 prompt = format(0, "\r%s-- %s --%s%s",
588 cf->ansi_capable ? ANSI_BOLD : "",
590 cf->ansi_capable ? ANSI_RESET: "",
593 unix_vlib_cli_output_cooked(cf, uf, prompt, vec_len(prompt));
598 /** \brief Erase the printed pager prompt */
599 static void unix_cli_pager_prompt_erase(unix_cli_file_t * cf, unix_file_t * uf)
601 if (cf->ansi_capable)
603 unix_vlib_cli_output_cooked(cf, uf, (u8 *)"\r", 1);
604 unix_vlib_cli_output_cooked(cf, uf,
605 (u8 *)ANSI_CLEARLINE, sizeof(ANSI_CLEARLINE) - 1);
611 unix_vlib_cli_output_cooked(cf, uf, (u8 *)"\r", 1);
612 for (i = 0; i < cf->width - 1; i ++)
613 unix_vlib_cli_output_cooked(cf, uf, (u8 *)" ", 1);
614 unix_vlib_cli_output_cooked(cf, uf, (u8 *)"\r", 1);
618 /** \brief Uses an ANSI escape sequence to move the cursor */
619 static void unix_cli_ansi_cursor(unix_cli_file_t * cf, unix_file_t * uf,
624 str = format(0, "%s%d;%dH", CSI, y, x);
626 unix_vlib_cli_output_cooked(cf, uf, str, vec_len(str));
631 /** \brief VLIB CLI output function. */
632 static void unix_vlib_cli_output (uword cli_file_index,
636 unix_main_t * um = &unix_main;
637 unix_cli_main_t * cm = &unix_cli_main;
638 unix_cli_file_t * cf;
641 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
642 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
644 if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
646 unix_vlib_cli_output_cooked(cf, uf, buffer, buffer_bytes);
650 /* At each \n add the line to the pager buffer.
651 * If we've not yet displayed a whole page in this command then
652 * also output the line.
653 * If we have displayed a whole page then display a pager prompt
654 * and count the lines we've buffered.
658 uword i, len = buffer_bytes;
662 i = unix_vlib_findchr('\n', p, len);
664 i ++; /* include the '\n' */
666 /* make a vec out of this substring */
667 line = vec_new(u8, i);
670 /* store in pager buffer */
671 vec_add1(cf->pager_vector, line);
674 if (cf->pager_lines < cf->height)
676 /* output this line */
677 unix_vlib_cli_output_cooked(cf, uf, p, i);
678 /* TODO: stop if line longer than cf->width and would
679 * overflow cf->height */
683 /* Display the pager prompt every 10 lines */
684 if (!(cf->pager_lines % 10))
685 unix_cli_pager_prompt(cf, uf);
692 /* Check if we went over the pager buffer limit */
693 if (cf->pager_lines > um->cli_pager_buffer_limit)
695 /* Stop using the pager for the remainder of this CLI command */
698 /* If we likely printed the prompt, erase it */
699 if (cf->pager_lines > cf->height - 1)
700 unix_cli_pager_prompt_erase (cf, uf);
702 /* Dump out the contents of the buffer */
703 for (i = cf->pager_start + (cf->height - 1);
704 i < cf->pager_lines; i ++)
705 unix_vlib_cli_output_cooked (cf, uf,
707 vec_len(cf->pager_vector[i]));
709 unix_cli_pager_reset (cf);
714 /** \brief Identify whether a terminal type is ANSI capable. */
715 static u8 unix_cli_terminal_type(u8 * term, uword len)
717 /* This may later be better done as a hash of some sort. */
719 if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
724 _("xterm-256color"); /* iTerm on Mac */
726 _("ansi"); /* Microsoft Telnet */
732 /** \brief Emit initial welcome banner and prompt on a connection. */
733 static void unix_cli_file_welcome(unix_cli_main_t * cm, unix_cli_file_t * cf)
735 unix_main_t * um = &unix_main;
736 unix_file_t * uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
737 unix_cli_banner_t *banner;
741 * Put the first bytes directly into the buffer so that further output is
742 * queued until everything is ready. (oterwise initial prompt can appear
743 * mid way through VPP initialization)
745 unix_cli_add_pending_output (uf, cf, (u8 *)"\r", 1);
747 if (!um->cli_no_banner)
749 if (cf->ansi_capable)
751 banner = unix_cli_banner_color;
752 len = ARRAY_LEN(unix_cli_banner_color);
756 banner = unix_cli_banner;
757 len = ARRAY_LEN(unix_cli_banner);
760 for (i = 0; i < len; i++)
762 unix_vlib_cli_output_cooked(cf, uf,
769 unix_cli_cli_prompt (cf, uf);
774 /** \brief A failsafe triggered on a timer to ensure we send the prompt
775 * to telnet sessions that fail to negotiate the terminal type. */
776 static void unix_cli_file_welcome_timer(any arg, f64 delay)
778 unix_cli_main_t * cm = &unix_cli_main;
779 unix_cli_file_t * cf;
782 /* Check the connection didn't close already */
783 if (pool_is_free_index (cm->cli_file_pool, (uword)arg))
786 cf = pool_elt_at_index (cm->cli_file_pool, (uword)arg);
789 unix_cli_file_welcome(cm, cf);
792 /** \brief A mostly no-op Telnet state machine.
793 * Process Telnet command bytes in a way that ensures we're mostly
794 * transparent to the Telnet protocol. That is, it's mostly a no-op.
796 * @return -1 if we need more bytes, otherwise a positive integer number of
797 * bytes to consume from the input_vector, not including the initial
800 static i32 unix_cli_process_telnet(unix_main_t * um,
801 unix_cli_file_t * cf,
806 /* Input_vector starts at IAC byte.
807 * See if we have a complete message; if not, return -1 so we wait for more.
808 * if we have a complete message, consume those bytes from the vector.
813 return -1; /* want more bytes */
815 switch (input_vector[1])
818 /* two IAC's in a row means to pass through 0xff.
819 * since that makes no sense here, just consume it.
829 if (vec_len(input_vector) < 3)
830 return -1; /* want more bytes */
837 /* Sub option - search ahead for IAC SE to end it */
839 for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
841 if (input_vector[i - 1] == IAC && input_vector[i] == SE)
843 /* We have a complete message; see if we care about it */
844 switch (input_vector[2])
847 if (input_vector[3] != 0)
849 /* See if the terminal type is ANSI capable */
851 unix_cli_terminal_type(input_vector + 4, i - 5);
852 /* If session not started, we can release the pause */
854 /* Send the welcome banner and initial prompt */
855 unix_cli_file_welcome(&unix_cli_main, cf);
860 if (i != 8) /* check message is correct size */
862 cf->width = clib_net_to_host_u16(*((u16 *)(input_vector + 3)));
863 cf->height = clib_net_to_host_u16(*((u16 *)(input_vector + 5)));
875 if (i == UNIX_CLI_MAX_DEPTH_TELNET)
876 consume = 1; /* hit max search depth, advance one byte */
879 return -1; /* want more bytes */
897 /* Simple one-byte messages */
902 /* Are You There - trigger a visible response */
904 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
908 /* Unknown command! Eat the IAC byte */
915 /** \brief Process actionable input.
916 * Based on the \c action process the input; this typically involves
917 * searching the command history or editing the current command line.
919 static int unix_cli_line_process_one(unix_cli_main_t * cm,
921 unix_cli_file_t * cf,
924 unix_cli_parse_action_t action)
931 case UNIX_CLI_PARSE_ACTION_NOACTION:
934 case UNIX_CLI_PARSE_ACTION_REVSEARCH:
935 case UNIX_CLI_PARSE_ACTION_FWDSEARCH:
936 if (!cf->has_history || !cf->history_limit)
938 if (cf->search_mode == 0)
940 /* Erase the current command (if any) */
941 for (j = 0; j < (vec_len (cf->current_command)); j++)
942 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
944 vec_reset_length (cf->search_key);
945 vec_reset_length (cf->current_command);
946 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
947 cf->search_mode = -1;
954 if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
955 cf->search_mode = -1;
959 cf->excursion += cf->search_mode;
964 case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
965 /* Erase the command from the cursor to the start */
967 /* Shimmy forwards to the new end of line position */
968 delta = vec_len (cf->current_command) - cf->cursor;
969 for (j = cf->cursor; j > delta; j--)
970 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
971 /* Zap from here to the end of what is currently displayed */
972 for (; j < (vec_len (cf->current_command)); j++)
973 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
974 /* Get back to the start of the line */
975 for (j = 0; j < (vec_len (cf->current_command)); j++)
976 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
978 j = vec_len(cf->current_command) - cf->cursor;
979 memmove(cf->current_command,
980 cf->current_command + cf->cursor,
982 _vec_len(cf->current_command) = j;
984 /* Print the new contents */
985 unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j);
986 /* Shimmy back to the start */
987 for (j = 0; j < (vec_len (cf->current_command)); j++)
988 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
994 case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
995 /* Erase the command from the cursor to the end */
997 /* Zap from cursor to end of what is currently displayed */
998 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
999 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1000 /* Get back to where we were */
1001 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1002 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1004 /* Truncate the line at the cursor */
1005 _vec_len(cf->current_command) = cf->cursor;
1007 cf->search_mode = 0;
1010 case UNIX_CLI_PARSE_ACTION_LEFT:
1013 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1017 cf->search_mode = 0;
1020 case UNIX_CLI_PARSE_ACTION_RIGHT:
1021 if (cf->cursor < vec_len(cf->current_command))
1023 /* have to emit the character under the cursor */
1024 unix_vlib_cli_output_cooked (cf, uf, cf->current_command + cf->cursor, 1);
1028 cf->search_mode = 0;
1031 case UNIX_CLI_PARSE_ACTION_UP:
1032 case UNIX_CLI_PARSE_ACTION_DOWN:
1033 if (!cf->has_history || !cf->history_limit)
1035 cf->search_mode = 0;
1036 /* Erase the command */
1037 for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1038 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1039 for (j = 0; j < (vec_len (cf->current_command)); j++)
1040 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1041 vec_reset_length (cf->current_command);
1042 if (vec_len (cf->command_history))
1044 if (action == UNIX_CLI_PARSE_ACTION_UP)
1049 cf->excursion += delta;
1051 if (cf->excursion == vec_len (cf->command_history))
1053 /* down-arrowed to last entry - want a blank line */
1054 _vec_len (cf->current_command) = 0;
1056 else if (cf->excursion < 0)
1058 /* up-arrowed over the start to the end, want a blank line */
1059 cf->excursion = vec_len (cf->command_history);
1060 _vec_len (cf->current_command) = 0;
1064 if (cf->excursion > (i32) vec_len (cf->command_history) -1)
1065 /* down-arrowed past end - wrap to start */
1068 /* Print the command at the current position */
1069 prev = cf->command_history [cf->excursion];
1070 vec_validate (cf->current_command, vec_len(prev)-1);
1072 clib_memcpy (cf->current_command, prev, vec_len(prev));
1073 _vec_len (cf->current_command) = vec_len(prev);
1074 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1075 vec_len (cf->current_command));
1077 cf->cursor = vec_len(cf->current_command);
1083 case UNIX_CLI_PARSE_ACTION_HOME:
1084 if (vec_len (cf->current_command) && cf->cursor > 0)
1088 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1093 cf->search_mode = 0;
1096 case UNIX_CLI_PARSE_ACTION_END:
1097 if (vec_len (cf->current_command) &&
1098 cf->cursor < vec_len(cf->current_command))
1100 unix_vlib_cli_output_cooked (cf, uf,
1101 cf->current_command + cf->cursor,
1102 vec_len(cf->current_command) - cf->cursor);
1103 cf->cursor = vec_len(cf->current_command);
1106 cf->search_mode = 0;
1109 case UNIX_CLI_PARSE_ACTION_WORDLEFT:
1110 if (vec_len (cf->current_command) && cf->cursor > 0)
1114 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1117 while (j && isspace(cf->current_command[j]))
1119 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1122 while (j && !isspace(cf->current_command[j]))
1124 if (isspace(cf->current_command[j - 1]))
1126 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1133 cf->search_mode = 0;
1136 case UNIX_CLI_PARSE_ACTION_WORDRIGHT:
1137 if (vec_len (cf->current_command) &&
1138 cf->cursor < vec_len(cf->current_command))
1140 int e = vec_len(cf->current_command);
1142 while (j < e && !isspace(cf->current_command[j]))
1144 while (j < e && isspace(cf->current_command[j]))
1146 unix_vlib_cli_output_cooked (cf, uf,
1147 cf->current_command + cf->cursor,
1152 cf->search_mode = 0;
1156 case UNIX_CLI_PARSE_ACTION_ERASE:
1157 if (vec_len (cf->current_command))
1159 if (cf->cursor == vec_len(cf->current_command))
1161 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1162 _vec_len (cf->current_command)--;
1165 else if (cf->cursor > 0)
1167 /* shift everything at & to the right of the cursor left by 1 */
1168 j = vec_len (cf->current_command) - cf->cursor;
1169 memmove (cf->current_command + cf->cursor - 1,
1170 cf->current_command + cf->cursor,
1172 _vec_len (cf->current_command)--;
1174 /* redraw the rest of the line */
1175 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1176 unix_vlib_cli_output_cooked (cf, uf,
1177 cf->current_command + cf->cursor, j);
1178 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
1179 /* and shift the terminal cursor back where it should be */
1181 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1184 cf->search_mode = 0;
1186 vec_reset_length (cf->search_key);
1189 case UNIX_CLI_PARSE_ACTION_ERASERIGHT:
1190 if (vec_len (cf->current_command))
1192 if (cf->cursor < vec_len(cf->current_command))
1194 /* shift everything to the right of the cursor left by 1 */
1195 j = vec_len (cf->current_command) - cf->cursor - 1;
1196 memmove (cf->current_command + cf->cursor,
1197 cf->current_command + cf->cursor + 1,
1199 _vec_len (cf->current_command)--;
1200 /* redraw the rest of the line */
1201 unix_vlib_cli_output_cooked (cf, uf,
1202 cf->current_command + cf->cursor, j);
1203 unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
1204 /* and shift the terminal cursor back where it should be */
1207 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1209 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1213 else if (input == 'D' - '@')
1215 /* ^D with no command entered = quit */
1216 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1217 vlib_process_signal_event (um->vlib_main,
1218 vlib_current_process (um->vlib_main),
1219 UNIX_CLI_PROCESS_EVENT_QUIT,
1220 cf - cm->cli_file_pool);
1222 cf->search_mode = 0;
1224 vec_reset_length (cf->search_key);
1227 case UNIX_CLI_PARSE_ACTION_CLEAR:
1228 /* If we're in ANSI mode, clear the screen.
1229 * Then redraw the prompt and any existing command input, then put
1230 * the cursor back where it was in that line.
1232 if (cf->ansi_capable)
1233 unix_vlib_cli_output_cooked (cf, uf,
1235 sizeof(ANSI_CLEAR)-1);
1237 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1239 unix_vlib_cli_output_raw (cf, uf,
1241 vec_len (cm->cli_prompt));
1242 unix_vlib_cli_output_raw (cf, uf,
1243 cf->current_command,
1244 vec_len (cf->current_command));
1245 for (j = cf->cursor; j < vec_len(cf->current_command); j++)
1246 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1250 case UNIX_CLI_PARSE_ACTION_TAB:
1251 case UNIX_CLI_PARSE_ACTION_YANK:
1256 case UNIX_CLI_PARSE_ACTION_PAGER_QUIT:
1258 unix_cli_pager_prompt_erase (cf, uf);
1259 unix_cli_pager_reset (cf);
1260 unix_cli_cli_prompt (cf, uf);
1263 case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
1264 case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
1265 /* show next page of the buffer */
1266 if (cf->height + cf->pager_start < cf->pager_lines)
1269 int m = cf->pager_start + (cf->height - 1);
1270 unix_cli_pager_prompt_erase (cf, uf);
1272 j < cf->pager_lines && cf->pager_start < m;
1273 j ++, cf->pager_start ++)
1275 line = cf->pager_vector[j];
1276 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1278 unix_cli_pager_prompt (cf, uf);
1282 if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
1283 /* no more in buffer, exit, but only if it was <space> */
1288 case UNIX_CLI_PARSE_ACTION_PAGER_DN:
1289 case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
1290 /* display the next line of the buffer */
1291 if (cf->pager_start < cf->pager_lines - (cf->height - 1))
1294 unix_cli_pager_prompt_erase (cf, uf);
1295 line = cf->pager_vector[cf->pager_start + (cf->height - 1)];
1296 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1298 unix_cli_pager_prompt (cf, uf);
1302 if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
1303 /* no more in buffer, exit, but only if it was <enter> */
1309 case UNIX_CLI_PARSE_ACTION_PAGER_UP:
1310 /* scroll the page back one line */
1311 if (cf->pager_start > 0)
1315 if (cf->ansi_capable)
1317 unix_cli_pager_prompt_erase (cf, uf);
1318 unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_SCROLLDN, sizeof(ANSI_SCROLLDN) - 1);
1319 unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_SAVECURSOR, sizeof(ANSI_SAVECURSOR) - 1);
1320 unix_cli_ansi_cursor(cf, uf, 1, 1);
1321 unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_CLEARLINE, sizeof(ANSI_CLEARLINE) - 1);
1322 unix_vlib_cli_output_cooked (cf, uf, cf->pager_vector[cf->pager_start], vec_len(cf->pager_vector[cf->pager_start]));
1323 unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_RESTCURSOR, sizeof(ANSI_RESTCURSOR) - 1);
1324 unix_cli_pager_prompt_erase (cf, uf);
1325 unix_cli_pager_prompt (cf, uf);
1329 int m = cf->pager_start + (cf->height - 1);
1330 unix_cli_pager_prompt_erase (cf, uf);
1331 for (j = cf->pager_start; j < cf->pager_lines && j < m; j ++)
1333 line = cf->pager_vector[j];
1334 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1336 unix_cli_pager_prompt (cf, uf);
1341 case UNIX_CLI_PARSE_ACTION_PAGER_TOP:
1342 /* back to the first page of the buffer */
1343 if (cf->pager_start > 0)
1346 cf->pager_start = 0;
1347 int m = cf->pager_start + (cf->height - 1);
1348 unix_cli_pager_prompt_erase (cf, uf);
1349 for (j = cf->pager_start; j < cf->pager_lines && j < m; j ++)
1351 line = cf->pager_vector[j];
1352 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1354 unix_cli_pager_prompt (cf, uf);
1358 case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM:
1359 /* skip to the last page of the buffer */
1360 if (cf->pager_start < cf->pager_lines - (cf->height - 1))
1363 cf->pager_start = cf->pager_lines - (cf->height - 1);
1364 unix_cli_pager_prompt_erase (cf, uf);
1365 unix_cli_pager_message (cf, uf, "skipping", "\n");
1366 for (j = cf->pager_start; j < cf->pager_lines; j ++)
1368 line = cf->pager_vector[j];
1369 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1371 unix_cli_pager_prompt (cf, uf);
1375 case UNIX_CLI_PARSE_ACTION_PAGER_PGUP:
1376 /* wander back one page in the buffer */
1377 if (cf->pager_start > 0)
1381 if (cf->pager_start >= cf->height)
1382 cf->pager_start -= cf->height - 1;
1384 cf->pager_start = 1;
1385 m = cf->pager_start + cf->height - 1;
1386 unix_cli_pager_prompt_erase (cf, uf);
1387 for (j = cf->pager_start; j < cf->pager_lines && j < m; j ++)
1389 line = cf->pager_vector[j];
1390 unix_vlib_cli_output_cooked (cf, uf, line, vec_len(line));
1392 unix_cli_pager_prompt (cf, uf);
1396 case UNIX_CLI_PARSE_ACTION_PAGER_SEARCH:
1397 /* search forwards in the buffer */
1401 case UNIX_CLI_PARSE_ACTION_CRLF:
1403 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1405 if (cf->has_history && cf->history_limit)
1407 if (cf->command_history && vec_len(cf->command_history) >= cf->history_limit)
1409 vec_free (cf->command_history[0]);
1410 vec_delete (cf->command_history, 1, 0);
1412 /* Don't add blank lines to the cmd history */
1413 if (vec_len (cf->current_command))
1415 /* Don't duplicate the previous command */
1416 j = vec_len(cf->command_history);
1418 (vec_len (cf->current_command) != vec_len (cf->command_history[j - 1]) ||
1419 memcmp(cf->current_command, cf->command_history[j - 1],
1420 vec_len (cf->current_command)) != 0))
1422 /* copy the command to the history */
1424 vec_append(c, cf->current_command);
1425 vec_add1 (cf->command_history, c);
1426 cf->command_number ++;
1429 cf->excursion = vec_len (cf->command_history);
1432 cf->search_mode = 0;
1433 vec_reset_length (cf->search_key);
1438 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
1439 case UNIX_CLI_PARSE_ACTION_NOMATCH:
1440 if (cf->pager_lines)
1444 else if (cf->has_history && cf->search_mode && isprint(input))
1446 int k, limit, offset;
1449 vec_add1 (cf->search_key, input);
1452 for (j = 0; j < vec_len(cf->command_history); j++)
1454 if (cf->excursion > (i32) vec_len (cf->command_history) -1)
1456 else if (cf->excursion < 0)
1457 cf->excursion = vec_len (cf->command_history) -1;
1459 item = cf->command_history[cf->excursion];
1461 limit = (vec_len(cf->search_key) > vec_len (item)) ?
1462 vec_len(item) : vec_len (cf->search_key);
1464 for (offset = 0; offset <= vec_len(item) - limit; offset++)
1466 for (k = 0; k < limit; k++)
1468 if (item[k+offset] != cf->search_key[k])
1471 goto found_at_offset;
1479 for (j = 0; j < vec_len (cf->current_command); j++)
1480 unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1482 vec_validate (cf->current_command, vec_len(item)-1);
1483 clib_memcpy (cf->current_command, item, vec_len(item));
1484 _vec_len (cf->current_command) = vec_len(item);
1486 unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
1487 vec_len (cf->current_command));
1488 cf->cursor = vec_len (cf->current_command);
1492 cf->excursion += cf->search_mode;
1495 unix_vlib_cli_output_cooked (cf, uf, (u8 *)"\nNo match...", 12);
1496 vec_reset_length (cf->search_key);
1497 vec_reset_length (cf->current_command);
1498 cf->search_mode = 0;
1502 else if (isprint(input)) /* skip any errant control codes */
1504 if (cf->cursor == vec_len(cf->current_command))
1507 vec_add1 (cf->current_command, input);
1510 /* Echo the character back to the client */
1511 unix_vlib_cli_output_raw (cf, uf, &input, 1);
1515 /* Insert at cursor: resize +1 byte, move everything over */
1516 j = vec_len (cf->current_command) - cf->cursor;
1517 vec_add1 (cf->current_command, (u8)'A');
1518 memmove (cf->current_command + cf->cursor + 1,
1519 cf->current_command + cf->cursor,
1521 cf->current_command[cf->cursor] = input;
1522 /* Redraw the line */
1524 unix_vlib_cli_output_raw (cf, uf,
1525 cf->current_command + cf->cursor, j);
1526 /* Put terminal cursor back */
1528 unix_vlib_cli_output_raw (cf, uf, (u8 *)"\b", 1);
1534 /* no-op - not printable or otherwise not actionable */
1541 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
1547 /** \brief Process input bytes on a stream to provide line editing and
1548 * command history in the CLI. */
1549 static int unix_cli_line_edit (unix_cli_main_t * cm,
1551 unix_cli_file_t * cf)
1553 unix_file_t * uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
1556 for (i = 0; i < vec_len (cf->input_vector); i++)
1558 unix_cli_parse_action_t action;
1560 unix_cli_parse_actions_t *a;
1562 /* If we're in the pager mode, search the pager actions */
1563 a = cf->pager_lines ? unix_cli_parse_pager : unix_cli_parse_strings;
1565 /* See if the input buffer is some sort of control code */
1566 action = unix_cli_match_action(a, &cf->input_vector[i],
1567 vec_len (cf->input_vector) - i, &matched);
1571 case UNIX_CLI_PARSE_ACTION_PARTIALMATCH:
1574 /* There was a partial match which means we need more bytes
1575 * than the input buffer currently has.
1576 * Since the bytes before here have been processed, shift
1577 * the remaining contents to the start of the input buffer.
1579 vec_delete (cf->input_vector, i, 0);
1581 return 1; /* wait for more */
1583 case UNIX_CLI_PARSE_ACTION_TELNETIAC:
1584 /* process telnet options */
1585 matched = unix_cli_process_telnet(um, cf, uf,
1586 cf->input_vector + i, vec_len(cf->input_vector) - i);
1591 /* There was a partial match which means we need more bytes
1592 * than the input buffer currently has.
1593 * Since the bytes before here have been processed, shift
1594 * the remaining contents to the start of the input buffer.
1596 vec_delete (cf->input_vector, i, 0);
1598 return 1; /* wait for more */
1603 /* process the action */
1604 if (!unix_cli_line_process_one(cm, um, cf, uf,
1605 cf->input_vector[i], action))
1607 /* CRLF found. Consume the bytes from the input_vector */
1608 vec_delete (cf->input_vector, i + matched, 0);
1609 /* And tell our caller to execute cf->input_command */
1617 vec_reset_length(cf->input_vector);
1621 /** \brief Process input to a CLI session. */
1622 static void unix_cli_process_input (unix_cli_main_t * cm,
1623 uword cli_file_index)
1625 unix_main_t * um = &unix_main;
1627 unix_cli_file_t * cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
1628 unformat_input_t input;
1629 int vlib_parse_eval (u8 *);
1632 /* Try vlibplex first. Someday... */
1633 if (0 && vlib_parse_eval (cf->input_vector) == 0)
1638 /* just treat whatever we got as a complete line of input */
1639 cf->current_command = cf->input_vector;
1643 /* Line edit, echo, etc. */
1644 if (unix_cli_line_edit (cm, um, cf))
1645 /* want more input */
1652 vec_reset_length (lv);
1653 lv = format (lv, "%U[%d]: %v",
1655 0 /* current bat-time */,
1656 0 /* current bat-format */,
1660 int rv __attribute__((unused)) =
1661 write (um->log_fd, lv, vec_len(lv));
1665 /* Copy our input command to a new string */
1666 unformat_init_vector (&input, cf->current_command);
1668 /* Remove leading white space from input. */
1669 (void) unformat (&input, "");
1671 cm->current_input_file_index = cli_file_index;
1672 cf->pager_lines = 0; /* start a new pager session */
1673 cf->pager_start = 0;
1675 if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
1676 vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output, cli_file_index);
1678 /* Zero buffer since otherwise unformat_free will call vec_free on it. */
1681 unformat_free (&input);
1683 /* Re-fetch pointer since pool may have moved. */
1684 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
1685 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
1688 /* reset vector; we'll re-use it later */
1690 vec_reset_length (cf->input_vector);
1692 vec_reset_length (cf->current_command);
1694 if (cf->no_pager == 2)
1696 /* Pager was programmatically disabled */
1697 unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
1698 cf->no_pager = um->cli_no_pager;
1701 if (cf->pager_lines == 0 || cf->pager_lines < cf->height)
1703 /* There was no need for the pager */
1704 unix_cli_pager_reset (cf);
1707 unix_cli_cli_prompt (cf, uf);
1711 /* Display the pager prompt */
1712 unix_cli_pager_prompt(cf, uf);
1715 /* Any residual data in the input vector? */
1716 if (vec_len (cf->input_vector))
1720 static void unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
1722 unix_main_t * um = &unix_main;
1723 unix_cli_file_t * cf;
1727 cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
1728 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
1730 /* Quit/EOF on stdin means quit program. */
1731 if (uf->file_descriptor == UNIX_CLI_STDIN_FD)
1732 clib_longjmp (&um->vlib_main->main_loop_exit, VLIB_MAIN_LOOP_EXIT_CLI);
1734 vec_free (cf->current_command);
1735 vec_free (cf->search_key);
1737 for (i = 0; i < vec_len (cf->command_history); i++)
1738 vec_free (cf->command_history[i]);
1740 vec_free (cf->command_history);
1742 unix_file_del (um, uf);
1744 unix_cli_file_free (cf);
1745 pool_put (cm->cli_file_pool, cf);
1749 unix_cli_process (vlib_main_t * vm,
1750 vlib_node_runtime_t * rt,
1753 unix_cli_main_t * cm = &unix_cli_main;
1754 uword i, * data = 0;
1758 unix_cli_process_event_type_t event_type;
1759 vlib_process_wait_for_event (vm);
1760 event_type = vlib_process_get_events (vm, &data);
1764 case UNIX_CLI_PROCESS_EVENT_READ_READY:
1765 for (i = 0; i < vec_len (data); i++)
1766 unix_cli_process_input (cm, data[i]);
1769 case UNIX_CLI_PROCESS_EVENT_QUIT:
1770 /* Kill this process. */
1771 for (i = 0; i < vec_len (data); i++)
1772 unix_cli_kill (cm, data[i]);
1777 _vec_len (data) = 0;
1783 vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
1785 /* Add node index so we can re-use this process later. */
1786 vec_add1 (cm->unused_cli_process_node_indices, rt->node_index);
1791 static clib_error_t * unix_cli_write_ready (unix_file_t * uf)
1793 unix_cli_main_t * cm = &unix_cli_main;
1794 unix_cli_file_t * cf;
1797 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
1799 /* Flush output vector. */
1800 n = write (uf->file_descriptor,
1801 cf->output_vector, vec_len (cf->output_vector));
1803 if (n < 0 && errno != EAGAIN)
1804 return clib_error_return_unix (0, "write");
1807 unix_cli_del_pending_output (uf, cf, n);
1809 return /* no error */ 0;
1812 static clib_error_t * unix_cli_read_ready (unix_file_t * uf)
1814 unix_main_t * um = &unix_main;
1815 unix_cli_main_t * cm = &unix_cli_main;
1816 unix_cli_file_t * cf;
1818 int n, n_read, n_try;
1820 cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data);
1823 while (n == n_try) {
1824 l = vec_len (cf->input_vector);
1825 vec_resize (cf->input_vector, l + n_try);
1827 n = read (uf->file_descriptor, cf->input_vector + l, n_try);
1830 if (n < 0 && errno != EAGAIN)
1831 return clib_error_return_unix (0, "read");
1833 n_read = n < 0 ? 0 : n;
1834 _vec_len (cf->input_vector) = l + n_read;
1838 vlib_process_signal_event (um->vlib_main,
1839 cf->process_node_index,
1841 ? UNIX_CLI_PROCESS_EVENT_QUIT
1842 : UNIX_CLI_PROCESS_EVENT_READ_READY),
1843 /* event data */ uf->private_data);
1845 return /* no error */ 0;
1848 static u32 unix_cli_file_add (unix_cli_main_t * cm, char * name, int fd)
1850 unix_main_t * um = &unix_main;
1851 unix_cli_file_t * cf;
1852 unix_file_t template = {0};
1853 vlib_main_t * vm = um->vlib_main;
1856 name = (char *) format (0, "unix-cli-%s", name);
1858 if (vec_len (cm->unused_cli_process_node_indices) > 0)
1860 uword l = vec_len (cm->unused_cli_process_node_indices);
1862 /* Find node and give it new name. */
1863 n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
1865 n->name = (u8 *) name;
1867 vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
1869 _vec_len (cm->unused_cli_process_node_indices) = l - 1;
1873 static vlib_node_registration_t r = {
1874 .function = unix_cli_process,
1875 .type = VLIB_NODE_TYPE_PROCESS,
1876 .process_log2_n_stack_bytes = 14,
1880 vlib_register_node (vm, &r);
1883 n = vlib_get_node (vm, r.index);
1886 pool_get (cm->cli_file_pool, cf);
1887 memset (cf, 0, sizeof (*cf));
1889 template.read_function = unix_cli_read_ready;
1890 template.write_function = unix_cli_write_ready;
1891 template.file_descriptor = fd;
1892 template.private_data = cf - cm->cli_file_pool;
1894 cf->process_node_index = n->index;
1895 cf->unix_file_index = unix_file_add (um, &template);
1896 cf->output_vector = 0;
1897 cf->input_vector = 0;
1899 vlib_start_process (vm, n->runtime_index);
1901 vlib_process_t * p = vlib_get_process_from_node(vm, n);
1902 p->output_function = unix_vlib_cli_output;
1903 p->output_function_arg = cf - cm->cli_file_pool;
1905 return cf - cm->cli_file_pool;
1908 static clib_error_t * unix_cli_listen_read_ready (unix_file_t * uf)
1910 unix_main_t * um = &unix_main;
1911 unix_cli_main_t * cm = &unix_cli_main;
1912 clib_socket_t * s = &um->cli_listen_socket;
1913 clib_socket_t client;
1915 clib_error_t * error;
1916 unix_cli_file_t * cf;
1919 error = clib_socket_accept (s, &client);
1923 client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
1925 cf_index = unix_cli_file_add (cm, client_name, client.fd);
1926 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
1928 /* No longer need CLIB version of socket. */
1929 clib_socket_free (&client);
1931 vec_free (client_name);
1933 /* if we're supposed to run telnet session in character mode (default) */
1934 if (um->cli_line_mode == 0)
1937 * Set telnet client character mode, echo on, suppress "go-ahead".
1938 * Technically these should be negotiated, but this works.
1940 u8 charmode_option[] = {
1941 IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
1942 IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
1943 IAC, WILL, TELOPT_SGA, /* server willl supress GA */
1944 IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
1945 IAC, WILL, TELOPT_ECHO, /* server will do echo */
1946 IAC, DONT, TELOPT_ECHO, /* client should not echo */
1947 IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
1948 IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
1949 IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
1950 IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
1953 /* Enable history on this CLI */
1954 cf->history_limit = um->cli_history_limit;
1955 cf->has_history = cf->history_limit != 0;
1957 /* Make sure this session is in line mode */
1963 /* Setup the pager */
1964 cf->no_pager = um->cli_no_pager;
1966 uf = pool_elt_at_index (um->file_pool, cf->unix_file_index);
1968 /* Send the telnet options */
1969 unix_vlib_cli_output_raw (cf, uf, charmode_option,
1970 ARRAY_LEN(charmode_option));
1972 /* In case the client doesn't negotiate terminal type, use
1973 * a timer to kick off the initial prompt. */
1974 timer_call (unix_cli_file_welcome_timer, cf_index, 1);
1981 unix_cli_resize_interrupt (int signum)
1983 unix_cli_main_t * cm = &unix_cli_main;
1984 unix_cli_file_t * cf = pool_elt_at_index (cm->cli_file_pool,
1985 cm->stdin_cli_file_index);
1989 /* Terminal resized, fetch the new size */
1990 ioctl(UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws);
1991 cf->width = ws.ws_col;
1992 cf->height = ws.ws_row;
1995 static clib_error_t *
1996 unix_cli_config (vlib_main_t * vm, unformat_input_t * input)
1998 unix_main_t * um = &unix_main;
1999 unix_cli_main_t * cm = &unix_cli_main;
2001 clib_error_t * error = 0;
2002 unix_cli_file_t * cf;
2005 struct sigaction sa;
2009 /* We depend on unix flags being set. */
2010 if ((error = vlib_call_config_function (vm, unix_config)))
2013 if (um->flags & UNIX_FLAG_INTERACTIVE)
2015 /* Set stdin to be non-blocking. */
2016 if ((flags = fcntl (UNIX_CLI_STDIN_FD, F_GETFL, 0)) < 0)
2018 fcntl (UNIX_CLI_STDIN_FD, F_SETFL, flags | O_NONBLOCK);
2020 cf_index = unix_cli_file_add (cm, "stdin", UNIX_CLI_STDIN_FD);
2021 cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2022 cm->stdin_cli_file_index = cf_index;
2024 /* If stdin is a tty and we are using chacracter mode, enable
2025 * history on the CLI and set the tty line discipline accordingly. */
2026 if (isatty(UNIX_CLI_STDIN_FD) && um->cli_line_mode == 0)
2028 /* Capture terminal resize events */
2029 sa.sa_handler = unix_cli_resize_interrupt;
2031 if (sigaction (SIGWINCH, &sa, 0) < 0)
2032 clib_panic ("sigaction");
2034 /* Retrieve the current terminal size */
2035 ioctl(UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws);
2036 cf->width = ws.ws_col;
2037 cf->height = ws.ws_row;
2039 if (cf->width == 0 || cf->height == 0)
2040 /* We have a tty, but no size. Stick to line mode. */
2043 /* Setup the history */
2044 cf->history_limit = um->cli_history_limit;
2045 cf->has_history = cf->history_limit != 0;
2047 /* Setup the pager */
2048 cf->no_pager = um->cli_no_pager;
2050 /* We're going to be in char by char mode */
2053 /* Save the original tty state so we can restore it later */
2054 tcgetattr(UNIX_CLI_STDIN_FD, &um->tio_stdin);
2057 /* Tweak the tty settings */
2058 tio = um->tio_stdin;
2059 /* echo off, canonical mode off, ext'd input processing off */
2060 tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
2061 tio.c_cc[VMIN] = 1; /* 1 byte at a time */
2062 tio.c_cc[VTIME] = 0; /* no timer */
2063 tcsetattr(UNIX_CLI_STDIN_FD, TCSAFLUSH, &tio);
2065 /* See if we can do ANSI/VT100 output */
2066 term = (u8 *)getenv("TERM");
2068 cf->ansi_capable = unix_cli_terminal_type(term,
2069 strlen((char *)term));
2074 /* No tty, so make sure these things are off */
2076 cf->history_limit = 0;
2077 cf->has_history = 0;
2081 /* Send banner and initial prompt */
2082 unix_cli_file_welcome(cm, cf);
2085 /* If we have socket config, LISTEN, otherwise, don't */
2086 clib_socket_t * s = &um->cli_listen_socket;
2087 if(s->config && s->config[0] != 0) {
2089 unix_file_t template = {0};
2091 s->flags = SOCKET_IS_SERVER; /* listen, don't connect */
2092 error = clib_socket_init (s);
2097 template.read_function = unix_cli_listen_read_ready;
2098 template.file_descriptor = s->fd;
2100 unix_file_add (um, &template);
2103 /* Set CLI prompt. */
2104 if (! cm->cli_prompt)
2105 cm->cli_prompt = format (0, "VLIB: ");
2110 VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli");
2112 static clib_error_t *
2113 unix_cli_exit (vlib_main_t * vm)
2115 unix_main_t * um = &unix_main;
2117 /* If stdin is a tty and we saved the tty state, reset the tty state */
2118 if (isatty(UNIX_CLI_STDIN_FD) && um->tio_isset)
2119 tcsetattr(UNIX_CLI_STDIN_FD, TCSAFLUSH, &um->tio_stdin);
2124 VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit);
2126 void vlib_unix_cli_set_prompt (char * prompt)
2128 char * fmt = (prompt[strlen(prompt)-1] == ' ') ? "%s" : "%s ";
2129 unix_cli_main_t * cm = &unix_cli_main;
2131 vec_free (cm->cli_prompt);
2132 cm->cli_prompt = format (0, fmt, prompt);
2135 static clib_error_t *
2136 unix_cli_quit (vlib_main_t * vm,
2137 unformat_input_t * input,
2138 vlib_cli_command_t * cmd)
2140 unix_cli_main_t * cm = &unix_cli_main;
2142 vlib_process_signal_event (vm,
2143 vlib_current_process (vm),
2144 UNIX_CLI_PROCESS_EVENT_QUIT,
2145 cm->current_input_file_index);
2149 VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
2151 .short_help = "Exit CLI",
2152 .function = unix_cli_quit,
2155 static clib_error_t *
2156 unix_cli_exec (vlib_main_t * vm,
2157 unformat_input_t * input,
2158 vlib_cli_command_t * cmd)
2162 unformat_input_t sub_input;
2163 clib_error_t * error;
2169 if (! unformat (input, "%s", &file_name))
2171 error = clib_error_return (0, "expecting file name, got `%U'",
2172 format_unformat_error, input);
2176 fd = open (file_name, O_RDONLY);
2179 error = clib_error_return_unix (0, "failed to open `%s'", file_name);
2183 /* Make sure its a regular file. */
2187 if (fstat (fd, &s) < 0)
2189 error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
2193 if (! (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
2195 error = clib_error_return (0, "not a regular file `%s'", file_name);
2200 unformat_init_unix_file (&sub_input, fd);
2202 vlib_cli_input (vm, &sub_input, 0, 0);
2203 unformat_free (&sub_input);
2208 vec_free (file_name);
2213 VLIB_CLI_COMMAND (cli_exec, static) = {
2215 .short_help = "Execute commands from file",
2216 .function = unix_cli_exec,
2220 static clib_error_t *
2221 unix_show_errors (vlib_main_t * vm,
2222 unformat_input_t * input,
2223 vlib_cli_command_t * cmd)
2225 unix_main_t * um = &unix_main;
2226 clib_error_t * error = 0;
2227 int i, n_errors_to_show;
2228 unix_error_history_t * unix_errors = 0;
2230 n_errors_to_show = 1 << 30;
2232 if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2234 if (! unformat (input, "%d", &n_errors_to_show))
2236 error = clib_error_return (0, "expecting integer number of errors to show, got `%U'",
2237 format_unformat_error, input);
2242 n_errors_to_show = clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
2244 i = um->error_history_index > 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
2246 while (n_errors_to_show > 0)
2248 unix_error_history_t * eh = um->error_history + i;
2253 vec_add1 (unix_errors, eh[0]);
2254 n_errors_to_show -= 1;
2256 i = ARRAY_LEN (um->error_history) - 1;
2261 if (vec_len (unix_errors) == 0)
2262 vlib_cli_output (vm, "no Unix errors so far");
2265 vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
2266 for (i = vec_len (unix_errors) - 1; i >= 0; i--)
2268 unix_error_history_t * eh = vec_elt_at_index (unix_errors, i);
2269 vlib_cli_output (vm, "%U: %U",
2270 format_time_interval, "h:m:s:u", eh->time,
2271 format_clib_error, eh->error);
2273 vlib_cli_output (vm, "%U: time now",
2274 format_time_interval, "h:m:s:u", vlib_time_now (vm));
2278 vec_free (unix_errors);
2282 VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
2283 .path = "show unix-errors",
2284 .short_help = "Show Unix system call error history",
2285 .function = unix_show_errors,
2288 static clib_error_t *
2289 unix_cli_show_history (vlib_main_t * vm,
2290 unformat_input_t * input,
2291 vlib_cli_command_t * cmd)
2293 unix_cli_main_t * cm = &unix_cli_main;
2294 unix_cli_file_t * cf;
2297 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2299 if (cf->has_history && cf->history_limit)
2301 i = 1 + cf->command_number - vec_len(cf->command_history);
2302 for (j = 0; j < vec_len (cf->command_history); j++)
2303 vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
2307 vlib_cli_output (vm, "History not enabled.\n");
2313 VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
2315 .short_help = "Show current session command history",
2316 .function = unix_cli_show_history,
2319 static clib_error_t *
2320 unix_cli_show_terminal (vlib_main_t * vm,
2321 unformat_input_t * input,
2322 vlib_cli_command_t * cmd)
2324 unix_main_t * um = &unix_main;
2325 unix_cli_main_t * cm = &unix_cli_main;
2326 unix_cli_file_t * cf;
2329 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2330 n = vlib_get_node (vm, cf->process_node_index);
2332 vlib_cli_output (vm, "Terminal name: %v\n", n->name);
2333 vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
2336 vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
2337 vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
2338 vlib_cli_output (vm, "ANSI capable: %s\n", cf->ansi_capable ? "yes" : "no");
2339 vlib_cli_output (vm, "History enabled: %s%s\n",
2340 cf->has_history ? "yes" : "no",
2341 !cf->has_history || cf->history_limit ? "" : " (disabled by history limit)");
2342 if (cf->has_history)
2343 vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
2344 vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
2345 cf->no_pager ? "no" : "yes",
2346 cf->no_pager || cf->height ? "" : " (disabled by terminal height)",
2347 cf->no_pager || um->cli_pager_buffer_limit ? "" : " (disabled by buffer limit)");
2349 vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
2350 vlib_cli_output (vm, "CRLF mode: %s\n", cf->crlf_mode ? "CR+LF" : "LF");
2355 VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
2356 .path = "show terminal",
2357 .short_help = "Show current session terminal settings",
2358 .function = unix_cli_show_terminal,
2361 static clib_error_t *
2362 unix_cli_set_terminal_pager (vlib_main_t * vm,
2363 unformat_input_t * input,
2364 vlib_cli_command_t * cmd)
2366 unix_main_t * um = &unix_main;
2367 unix_cli_main_t * cm = &unix_cli_main;
2368 unix_cli_file_t * cf;
2369 unformat_input_t _line_input, * line_input = &_line_input;
2371 if (! unformat_user (input, unformat_line_input, line_input))
2374 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2376 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2378 if (unformat (line_input, "on"))
2380 else if (unformat (line_input, "off"))
2382 else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
2383 vlib_cli_output (vm, "Pager limit set to %u lines; note, this is global.\n",
2384 um->cli_pager_buffer_limit);
2386 return clib_error_return (0, "unknown parameter: `%U`",
2387 format_unformat_error, line_input);
2390 unformat_free(line_input);
2395 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
2396 .path = "set terminal pager",
2397 .short_help = "set terminal pager [on|off] [limit <lines>]",
2398 .function = unix_cli_set_terminal_pager,
2401 static clib_error_t *
2402 unix_cli_set_terminal_history (vlib_main_t * vm,
2403 unformat_input_t * input,
2404 vlib_cli_command_t * cmd)
2406 unix_cli_main_t * cm = &unix_cli_main;
2407 unix_cli_file_t * cf;
2408 unformat_input_t _line_input, * line_input = &_line_input;
2411 if (! unformat_user (input, unformat_line_input, line_input))
2414 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2416 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2418 if (unformat (line_input, "on"))
2419 cf->has_history = 1;
2420 else if (unformat (line_input, "off"))
2421 cf->has_history = 0;
2422 else if (unformat (line_input, "limit %u", &cf->history_limit))
2425 return clib_error_return (0, "unknown parameter: `%U`",
2426 format_unformat_error, line_input);
2428 /* If we reduced history size, or turned it off, purge the history */
2429 limit = cf->has_history ? cf->history_limit : 0;
2431 while (cf->command_history && vec_len(cf->command_history) >= limit)
2433 vec_free (cf->command_history[0]);
2434 vec_delete (cf->command_history, 1, 0);
2438 unformat_free(line_input);
2443 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
2444 .path = "set terminal history",
2445 .short_help = "set terminal history [on|off] [limit <lines>]",
2446 .function = unix_cli_set_terminal_history,
2449 static clib_error_t *
2450 unix_cli_set_terminal_ansi (vlib_main_t * vm,
2451 unformat_input_t * input,
2452 vlib_cli_command_t * cmd)
2454 unix_cli_main_t * cm = &unix_cli_main;
2455 unix_cli_file_t * cf;
2457 cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
2459 if (unformat (input, "on"))
2460 cf->ansi_capable = 1;
2461 else if (unformat (input, "off"))
2462 cf->ansi_capable = 0;
2464 return clib_error_return (0, "unknown parameter: `%U`",
2465 format_unformat_error, input);
2470 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
2471 .path = "set terminal ansi",
2472 .short_help = "set terminal ansi [on|off]",
2473 .function = unix_cli_set_terminal_ansi,
2476 static clib_error_t *
2477 unix_cli_init (vlib_main_t * vm)
2482 VLIB_INIT_FUNCTION (unix_cli_init);