FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
cli.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16  * cli.c: Unix stdin/socket CLI.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
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:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
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.
38  */
39 /**
40  * @file
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.
44  */
45 /*? %%clicmd:group_label Command line session %% ?*/
46 /*? %%syscfg:group_label Command line session %% ?*/
47 
48 #include <vlib/vlib.h>
49 #include <vlib/unix/unix.h>
50 #include <vppinfra/timer.h>
51 
52 #include <ctype.h>
53 #include <fcntl.h>
54 #include <sys/stat.h>
55 #include <termios.h>
56 #include <signal.h>
57 #include <unistd.h>
58 #include <arpa/telnet.h>
59 #include <sys/ioctl.h>
60 
61 /** ANSI escape code. */
62 #define ESC "\x1b"
63 
64 /** ANSI Control Sequence Introducer. */
65 #define CSI ESC "["
66 
67 /** ANSI clear screen. */
68 #define ANSI_CLEAR CSI "2J" CSI "1;1H"
69 /** ANSI reset color settings. */
70 #define ANSI_RESET CSI "0m"
71 /** ANSI Start bold text. */
72 #define ANSI_BOLD CSI "1m"
73 /** ANSI Stop bold text. */
74 #define ANSI_DIM CSI "2m"
75 /** ANSI Start dark red text. */
76 #define ANSI_DRED ANSI_DIM CSI "31m"
77 /** ANSI Start bright red text. */
78 #define ANSI_BRED ANSI_BOLD CSI "31m"
79 /** ANSI clear line cursor is on. */
80 #define ANSI_CLEARLINE CSI "2K"
81 /** ANSI scroll screen down one line. */
82 #define ANSI_SCROLLDN CSI "1T"
83 /** ANSI save cursor position. */
84 #define ANSI_SAVECURSOR CSI "s"
85 /** ANSI restore cursor position if previously saved. */
86 #define ANSI_RESTCURSOR CSI "u"
87 
88 /** Maximum depth into a byte stream from which to compile a Telnet
89  * protocol message. This is a saftey measure. */
90 #define UNIX_CLI_MAX_DEPTH_TELNET 24
91 
92 /** Unix standard in */
93 #define UNIX_CLI_STDIN_FD 0
94 
95 
96 /** A CLI banner line. */
97 typedef struct
98 {
99  u8 *line; /**< The line to print. */
100  u32 length; /**< The length of the line without terminating NUL. */
102 
103 #define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 }
104 /** Plain welcome banner. */
105 static unix_cli_banner_t unix_cli_banner[] = {
106  _(" _______ _ _ _____ ___ \n"),
107  _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"),
108  _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"),
109  _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"),
110  _("\n")
111 };
112 
113 /** ANSI color welcome banner. */
114 static unix_cli_banner_t unix_cli_banner_color[] = {
115  _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"),
116  _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"),
117  _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"),
118  _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"),
119  _("\n")
120 };
121 
122 #undef _
123 
124 /** Pager line index */
125 typedef struct
126 {
127  /** Index into pager_vector */
129 
130  /** Offset of the string in the line */
132 
133  /** Length of the string in the line */
136 
137 
138 /** Unix CLI session. */
139 typedef struct
140 {
141  /** The file index held by unix.c */
143 
144  /** Vector of output pending write to file descriptor. */
146 
147  /** Vector of input saved by Unix input node to be processed by
148  CLI process. */
150 
151  /** This session has command history. */
153  /** Array of vectors of commands in the history. */
155  /** The command currently pointed at by the history cursor. */
157  /** How far from the end of the history array the user has browsed. */
159 
160  /** Maximum number of history entries this session will store. */
162 
163  /** Current command line counter */
165 
166  /** The string being searched for in the history. */
168  /** If non-zero then the CLI is searching in the history array.
169  * - @c -1 means search backwards.
170  * - @c 1 means search forwards.
171  */
173 
174  /** Position of the insert cursor on the current input line */
176 
177  /** Line mode or char mode */
179 
180  /** Set if the CRLF mode wants CR + LF */
182 
183  /** Can we do ANSI output? */
185 
186  /** Has the session started? */
188 
189  /** Disable the pager? */
191 
192  /** Pager buffer */
194 
195  /** Index of line fragments in the pager buffer */
197 
198  /** Line number of top of page */
200 
201  /** Terminal width */
203 
204  /** Terminal height */
206 
207  /** Process node identifier */
210 
211 /** Resets the pager buffer and other data.
212  * @param f The CLI session whose pager needs to be reset.
213  */
214 always_inline void
216 {
217  u8 **p;
218 
219  f->pager_start = 0;
220 
221  vec_free (f->pager_index);
222  f->pager_index = 0;
223 
224  vec_foreach (p, f->pager_vector)
225  {
226  vec_free (*p);
227  }
228  vec_free (f->pager_vector);
229  f->pager_vector = 0;
230 }
231 
232 /** Release storage used by a CLI session.
233  * @param f The CLI session whose storage needs to be released.
234  */
235 always_inline void
237 {
238  vec_free (f->output_vector);
239  vec_free (f->input_vector);
241 }
242 
243 /** CLI actions */
244 typedef enum
245 {
246  UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */
247  UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */
248  UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */
249  UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */
250  UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */
251  UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */
252  UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */
253  UNIX_CLI_PARSE_ACTION_LEFT, /**< Left arrow */
254  UNIX_CLI_PARSE_ACTION_RIGHT, /**< Right arrow */
255  UNIX_CLI_PARSE_ACTION_HOME, /**< Home key (jump to start of line) */
256  UNIX_CLI_PARSE_ACTION_END, /**< End key (jump to end of line) */
257  UNIX_CLI_PARSE_ACTION_WORDLEFT, /**< Jump cursor to start of left word */
258  UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */
259  UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */
260  UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */
261  UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */
262  UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */
263  UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */
264  UNIX_CLI_PARSE_ACTION_YANK, /**< Undo last erase action */
265  UNIX_CLI_PARSE_ACTION_TELNETIAC, /**< Telnet control code */
266 
267  UNIX_CLI_PARSE_ACTION_PAGER_CRLF, /**< Enter pressed (CR, CRLF, LF, etc) */
268  UNIX_CLI_PARSE_ACTION_PAGER_QUIT, /**< Exit the pager session */
269  UNIX_CLI_PARSE_ACTION_PAGER_NEXT, /**< Scroll to next page */
270  UNIX_CLI_PARSE_ACTION_PAGER_DN, /**< Scroll to next line */
271  UNIX_CLI_PARSE_ACTION_PAGER_UP, /**< Scroll to previous line */
272  UNIX_CLI_PARSE_ACTION_PAGER_TOP, /**< Scroll to first line */
273  UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM, /**< Scroll to last line */
274  UNIX_CLI_PARSE_ACTION_PAGER_PGDN, /**< Scroll to next page */
275  UNIX_CLI_PARSE_ACTION_PAGER_PGUP, /**< Scroll to previous page */
276  UNIX_CLI_PARSE_ACTION_PAGER_REDRAW, /**< Clear and redraw the page on the terminal */
277  UNIX_CLI_PARSE_ACTION_PAGER_SEARCH, /**< Search the pager buffer */
278 
279  UNIX_CLI_PARSE_ACTION_PARTIALMATCH, /**< Action parser found a partial match */
280  UNIX_CLI_PARSE_ACTION_NOMATCH /**< Action parser did not find any match */
282 
283 /** @brief Mapping of input buffer strings to action values.
284  * @note This won't work as a hash since we need to be able to do
285  * partial matches on the string.
286  */
287 typedef struct
288 {
289  u8 *input; /**< Input string to match. */
290  u32 len; /**< Length of input without final NUL. */
291  unix_cli_parse_action_t action; /**< Action to take when matched. */
293 
294 /** @brief Given a capital ASCII letter character return a @c NUL terminated
295  * string with the control code for that letter.
296  *
297  * @param c An ASCII character.
298  * @return A @c NUL terminated string of type @c u8[].
299  *
300  * @par Example
301  * @c CTL('A') returns <code>{ 0x01, 0x00 }</code> as a @c u8[].
302  */
303 #define CTL(c) (u8[]){ (c) - '@', 0 }
304 
305 #define _(a,b) { .input = (u8 *)(a), .len = sizeof(a) - 1, .action = (b) }
306 /**
307  * Patterns to match on a CLI input stream.
308  * @showinitializer
309  */
310 static unix_cli_parse_actions_t unix_cli_parse_strings[] = {
311  /* Line handling */
312  _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */
314  _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */
316 
317  /* Unix shell control codes */
320  _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP),
323  _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END),
329  _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */
330  _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */
331  _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */
332  _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */
333  _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */
334 
335  /* VT100 Normal mode - Broadest support */
342  _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */
343  _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */
344  _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */
345 
346  /* VT100 Application mode - Some Gnome Terminal functions use these */
347  _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP),
353 
354  /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */
357 
358  /* Emacs-ish history search */
361 
362  /* Other protocol things */
363  _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
364  _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
366 };
367 
368 /**
369  * Patterns to match when a CLI session is in the pager.
370  * @showinitializer
371  */
372 static unix_cli_parse_actions_t unix_cli_parse_pager[] = {
373  /* Line handling */
374  _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */
376  _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */
378 
379  /* Pager commands */
385 
386  /* VT100 */
391 
392  /* VT100 Application mode */
397 
398  /* ANSI X3.41-1974 */
403 
404  /* Other protocol things */
405  _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */
406  _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */
408 };
409 
410 #undef _
411 
412 /** CLI session events. */
413 typedef enum
414 {
415  UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */
416  UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */
418 
419 /** CLI global state. */
420 typedef struct
421 {
422  /** Prompt string for CLI. */
424 
425  /** Vec pool of CLI sessions. */
427 
428  /** Vec pool of unused session indices. */
430 
431  /** The session index of the stdin cli */
433 
434  /** File pool index of current input. */
437 
438 /** CLI global state */
440 
441 /**
442  * @brief Search for a byte sequence in the action list.
443  *
444  * Searches the @ref unix_cli_parse_actions_t list in @a a for a match with
445  * the bytes in @a input of maximum length @a ilen bytes.
446  * When a match is made @a *matched indicates how many bytes were matched.
447  * Returns a value from the enum @ref unix_cli_parse_action_t to indicate
448  * whether no match was found, a partial match was found or a complete
449  * match was found and what action, if any, should be taken.
450  *
451  * @param[in] a Actions list to search within.
452  * @param[in] input String fragment to search for.
453  * @param[in] ilen Length of the string in 'input'.
454  * @param[out] matched Pointer to an integer that will contain the number
455  * of bytes matched when a complete match is found.
456  *
457  * @return Action from @ref unix_cli_parse_action_t that the string fragment
458  * matches.
459  * @ref UNIX_CLI_PARSE_ACTION_PARTIALMATCH is returned when the
460  * whole input string matches the start of at least one action.
461  * @ref UNIX_CLI_PARSE_ACTION_NOMATCH is returned when there is no
462  * match at all.
463  */
466  u8 * input, u32 ilen, i32 * matched)
467 {
468  u8 partial = 0;
469 
470  while (a->input)
471  {
472  if (ilen >= a->len)
473  {
474  /* see if the start of the input buffer exactly matches the current
475  * action string. */
476  if (memcmp (input, a->input, a->len) == 0)
477  {
478  *matched = a->len;
479  return a->action;
480  }
481  }
482  else
483  {
484  /* if the first ilen characters match, flag this as a partial -
485  * meaning keep collecting bytes in case of a future match */
486  if (memcmp (input, a->input, ilen) == 0)
487  partial = 1;
488  }
489 
490  /* check next action */
491  a++;
492  }
493 
494  return partial ?
496 }
497 
498 
499 /** Add bytes to the output vector and then flagg the I/O system that bytes
500  * are available to be sent.
501  */
502 static void
504  unix_cli_file_t * cf,
505  u8 * buffer, uword buffer_bytes)
506 {
507  unix_main_t *um = &unix_main;
508 
509  vec_add (cf->output_vector, buffer, buffer_bytes);
510  if (vec_len (cf->output_vector) > 0)
511  {
512  int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
514  if (!skip_update)
516  }
517 }
518 
519 /** Delete all bytes from the output vector and flag the I/O system
520  * that no more bytes are available to be sent.
521  */
522 static void
524  unix_cli_file_t * cf, uword n_bytes)
525 {
526  unix_main_t *um = &unix_main;
527 
528  vec_delete (cf->output_vector, n_bytes, 0);
529  if (vec_len (cf->output_vector) <= 0)
530  {
531  int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE);
533  if (!skip_update)
535  }
536 }
537 
538 /** @brief A bit like strchr with a buffer length limit.
539  * Search a buffer for the first instance of a character up to the limit of
540  * the buffer length. If found then return the position of that character.
541  *
542  * The key departure from strchr is that if the character is not found then
543  * return the buffer length.
544  *
545  * @param chr The byte value to search for.
546  * @param str The buffer in which to search for the value.
547  * @param len The depth into the buffer to search.
548  *
549  * @return The index of the first occurence of \c chr. If \c chr is not
550  * found then \c len instead.
551  */
553 unix_vlib_findchr (u8 chr, u8 * str, word len)
554 {
555  word i = 0;
556  for (i = 0; i < len; i++, str++)
557  {
558  if (*str == chr)
559  return i;
560  }
561  return len;
562 }
563 
564 /** @brief Send a buffer to the CLI stream if possible, enqueue it otherwise.
565  * Attempts to write given buffer to the file descriptor of the given
566  * Unix CLI session. If that session already has data in the output buffer
567  * or if the write attempt tells us to try again later then the given buffer
568  * is appended to the pending output buffer instead.
569  *
570  * This is typically called only from \c unix_vlib_cli_output_cooked since
571  * that is where CRLF handling occurs or from places where we explicitly do
572  * not want cooked handling.
573  *
574  * @param cf Unix CLI session of the desired stream to write to.
575  * @param uf The Unix file structure of the desired stream to write to.
576  * @param buffer Pointer to the buffer that needs to be written.
577  * @param buffer_bytes The number of bytes from \c buffer to write.
578  */
579 static void
581  unix_file_t * uf, u8 * buffer, uword buffer_bytes)
582 {
583  int n = 0;
584 
585  if (vec_len (cf->output_vector) == 0)
586  n = write (uf->file_descriptor, buffer, buffer_bytes);
587 
588  if (n < 0 && errno != EAGAIN)
589  {
590  clib_unix_warning ("write");
591  }
592  else if ((word) n < (word) buffer_bytes)
593  {
594  /* We got EAGAIN or we already have stuff in the buffer;
595  * queue up whatever didn't get sent for later. */
596  if (n < 0)
597  n = 0;
598  unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n);
599  }
600 }
601 
602 /** @brief Process a buffer for CRLF handling before outputting it to the CLI.
603  *
604  * @param cf Unix CLI session of the desired stream to write to.
605  * @param uf The Unix file structure of the desired stream to write to.
606  * @param buffer Pointer to the buffer that needs to be written.
607  * @param buffer_bytes The number of bytes from \c buffer to write.
608  */
609 static void
611  unix_file_t * uf,
612  u8 * buffer, uword buffer_bytes)
613 {
614  word end = 0, start = 0;
615 
616  while (end < buffer_bytes)
617  {
618  if (cf->crlf_mode)
619  {
620  /* iterate the line on \n's so we can insert a \r before it */
621  end = unix_vlib_findchr ('\n',
622  buffer + start,
623  buffer_bytes - start) + start;
624  }
625  else
626  {
627  /* otherwise just send the whole buffer */
628  end = buffer_bytes;
629  }
630 
631  unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start);
632 
633  if (cf->crlf_mode)
634  {
635  if (end < buffer_bytes)
636  {
637  unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2);
638  end++; /* skip the \n that we already sent */
639  }
640  start = end;
641  }
642  }
643 }
644 
645 /** @brief Output the CLI prompt */
646 static void
648 {
650 
652 }
653 
654 /** @brief Output a pager prompt and show number of buffered lines */
655 static void
657 {
658  u8 *prompt;
659  u32 h;
660 
661  h = cf->pager_start + (cf->height - 1);
662  if (h > vec_len (cf->pager_index))
663  h = vec_len (cf->pager_index);
664 
665  prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s",
666  cf->ansi_capable ? ANSI_BOLD : "",
667  cf->pager_start + 1,
668  h,
669  vec_len (cf->pager_index),
670  cf->ansi_capable ? ANSI_RESET : "");
671 
672  unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
673 
674  vec_free (prompt);
675 }
676 
677 /** @brief Output a pager "skipping" message */
678 static void
680  char *message, char *postfix)
681 {
682  u8 *prompt;
683 
684  prompt = format (0, "\r%s-- %s --%s%s",
685  cf->ansi_capable ? ANSI_BOLD : "",
686  message, cf->ansi_capable ? ANSI_RESET : "", postfix);
687 
688  unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt));
689 
690  vec_free (prompt);
691 }
692 
693 /** @brief Erase the printed pager prompt */
694 static void
696 {
697  if (cf->ansi_capable)
698  {
699  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
701  (u8 *) ANSI_CLEARLINE,
702  sizeof (ANSI_CLEARLINE) - 1);
703  }
704  else
705  {
706  int i;
707 
708  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
709  for (i = 0; i < cf->width - 1; i++)
710  unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
711  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
712  }
713 }
714 
715 /** @brief Uses an ANSI escape sequence to move the cursor */
716 static void
718 {
719  u8 *str;
720 
721  str = format (0, "%s%d;%dH", CSI, y, x);
722 
723  unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str));
724 
725  vec_free (str);
726 }
727 
728 /** Redraw the currently displayed page of text.
729  * @param cf CLI session to redraw the pager buffer of.
730  * @param uf Unix file of the CLI session.
731  */
732 static void
734 {
736  u8 *line = NULL;
737  word i;
738 
739  /* No active pager? Do nothing. */
740  if (!vec_len (cf->pager_index))
741  return;
742 
743  if (cf->ansi_capable)
744  {
745  /* If we have ANSI, send the clear screen sequence */
747  (u8 *) ANSI_CLEAR,
748  sizeof (ANSI_CLEAR) - 1);
749  }
750  else
751  {
752  /* Otherwise make sure we're on a blank line */
754  }
755 
756  /* (Re-)send the current page of content */
757  for (i = 0; i < cf->height - 1 &&
758  i + cf->pager_start < vec_len (cf->pager_index); i++)
759  {
760  pi = &cf->pager_index[cf->pager_start + i];
761  line = cf->pager_vector[pi->line] + pi->offset;
762 
763  unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
764  }
765  /* if the last line didn't end in newline, add a newline */
766  if (pi && line[pi->length - 1] != '\n')
767  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
768 
769  unix_cli_pager_prompt (cf, uf);
770 }
771 
772 /** @brief Process and add a line to the pager index.
773  * In normal operation this function will take the given character string
774  * found in @c line and with length @c len_or_index and iterates the over the
775  * contents, adding each line of text discovered within it to the
776  * pager index. Lines are identified by newlines ("<code>\\n</code>") and by
777  * strings longer than the width of the terminal.
778  *
779  * If instead @c line is @c NULL then @c len_or_index is taken to mean the
780  * index of an existing line in the pager buffer; this simply means that the
781  * input line does not need to be cloned since we alreayd have it. This is
782  * typical if we are reindexing the pager buffer.
783  *
784  * @param cf The CLI session whose pager we are adding to.
785  * @param line The string of text to be indexed into the pager buffer.
786  * If @c line is @c NULL then the mode of operation
787  * changes slightly; see the description above.
788  * @param len_or_index If @c line is a pointer to a string then this parameter
789  * indicates the length of that string; Otherwise this
790  * value provides the index in the pager buffer of an
791  * existing string to be indexed.
792  */
793 static void
794 unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index)
795 {
796  u8 *p;
797  word i, j, k;
798  word line_index, len;
799  u32 width = cf->width;
801 
802  if (line == NULL)
803  {
804  /* Use a line already in the pager buffer */
805  line_index = len_or_index;
806  p = cf->pager_vector[line_index];
807  len = vec_len (p);
808  }
809  else
810  {
811  len = len_or_index;
812  /* Add a copy of the raw string to the pager buffer */
813  p = vec_new (u8, len);
814  clib_memcpy (p, line, len);
815 
816  /* store in pager buffer */
817  line_index = vec_len (cf->pager_vector);
818  vec_add1 (cf->pager_vector, p);
819  }
820 
821  i = 0;
822  while (i < len)
823  {
824  /* Find the next line, or run to terminal width, or run to EOL */
825  int l = len - i;
826  j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width);
827 
828  if (j < l && p[j] == '\n') /* incl \n */
829  j++;
830 
831  /* Add the line to the index */
832  k = vec_len (cf->pager_index);
833  vec_validate (cf->pager_index, k);
834  pi = &cf->pager_index[k];
835 
836  pi->line = line_index;
837  pi->offset = i;
838  pi->length = j;
839 
840  i += j;
841  p += j;
842  }
843 }
844 
845 /** @brief Reindex entire pager buffer.
846  * Resets the current pager index and then re-adds the lines in the pager
847  * buffer to the index.
848  *
849  * Additionally this function attempts to retain the current page start
850  * line offset by searching for the same top-of-screen line in the new index.
851  *
852  * @param cf The CLI session whose pager buffer should be reindexed.
853  */
854 static void
856 {
857  word i, old_line, old_offset;
859 
860  /* If there is nothing in the pager buffer then make sure the index
861  * is empty and move on.
862  */
863  if (cf->pager_vector == 0)
864  {
866  return;
867  }
868 
869  /* Retain a pointer to the current page start line so we can
870  * find it later
871  */
872  pi = &cf->pager_index[cf->pager_start];
873  old_line = pi->line;
874  old_offset = pi->offset;
875 
876  /* Re-add the buffered lines to the index */
879  {
880  unix_cli_pager_add_line (cf, NULL, i);
881  }
882 
883  /* Attempt to re-locate the previously stored page start line */
885  {
886  pi = &cf->pager_index[i];
887 
888  if (pi->line == old_line &&
889  (pi->offset <= old_offset || pi->offset + pi->length > old_offset))
890  {
891  /* Found it! */
892  cf->pager_start = i;
893  break;
894  }
895  }
896 
897  /* In case the start line was not found (rare), ensure the pager start
898  * index is within bounds
899  */
900  if (cf->pager_start >= vec_len (cf->pager_index))
901  {
902  if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1))
903  cf->pager_start = 0;
904  else
905  cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
906  }
907 }
908 
909 /** VLIB CLI output function.
910  *
911  * If the terminal has a pager configured then this function takes care
912  * of collating output into the pager buffer; ensuring only the first page
913  * is displayed and any lines in excess of the first page are buffered.
914  *
915  * If the maximum number of index lines in the buffer is exceeded then the
916  * pager is cancelled and the contents of the current buffer are sent to the
917  * terminal.
918  *
919  * If there is no pager configured then the output is sent directly to the
920  * terminal.
921  *
922  * @param cli_file_index Index of the CLI session where this output is
923  * directed.
924  * @param buffer String of printabe bytes to be output.
925  * @param buffer_bytes The number of bytes in @c buffer to be output.
926  */
927 static void
928 unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes)
929 {
930  unix_main_t *um = &unix_main;
932  unix_cli_file_t *cf;
933  unix_file_t *uf;
934 
935  cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
937 
938  if (cf->no_pager || um->cli_pager_buffer_limit == 0 || cf->height == 0)
939  {
940  unix_vlib_cli_output_cooked (cf, uf, buffer, buffer_bytes);
941  }
942  else
943  {
944  word row = vec_len (cf->pager_index);
945  u8 *line;
947 
948  /* Index and add the output lines to the pager buffer. */
949  unix_cli_pager_add_line (cf, buffer, buffer_bytes);
950 
951  /* Now iterate what was added to display the lines.
952  * If we reach the bottom of the page, display a prompt.
953  */
954  while (row < vec_len (cf->pager_index))
955  {
956  if (row < cf->height - 1)
957  {
958  /* output this line */
959  pi = &cf->pager_index[row];
960  line = cf->pager_vector[pi->line] + pi->offset;
961  unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
962 
963  /* if the last line didn't end in newline, and we're at the
964  * bottom of the page, add a newline */
965  if (line[pi->length - 1] != '\n' && row == cf->height - 2)
966  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
967  }
968  else
969  {
970  /* Display the pager prompt every 10 lines */
971  if (!(row % 10))
972  unix_cli_pager_prompt (cf, uf);
973  }
974  row++;
975  }
976 
977  /* Check if we went over the pager buffer limit */
979  {
980  /* Stop using the pager for the remainder of this CLI command */
981  cf->no_pager = 2;
982 
983  /* If we likely printed the prompt, erase it */
984  if (vec_len (cf->pager_index) > cf->height - 1)
986 
987  /* Dump out the contents of the buffer */
988  for (row = cf->pager_start + (cf->height - 1);
989  row < vec_len (cf->pager_index); row++)
990  {
991  pi = &cf->pager_index[row];
992  line = cf->pager_vector[pi->line] + pi->offset;
993  unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
994  }
995 
997  }
998  }
999 }
1000 
1001 /** Identify whether a terminal type is ANSI capable.
1002  *
1003  * Compares the string given in @c term with a list of terminal types known
1004  * to support ANSI escape sequences.
1005  *
1006  * This list contains, for example, @c xterm, @c screen and @c ansi.
1007  *
1008  * @param term A string with a terminal type in it.
1009  * @param len The length of the string in @c term.
1010  *
1011  * @return @c 1 if the terminal type is recognized as supporting ANSI
1012  * terminal sequences; @c 0 otherwise.
1013  */
1014 static u8
1016 {
1017  /* This may later be better done as a hash of some sort. */
1018 #define _(a) do { \
1019  if (strncasecmp(a, (char *)term, (size_t)len) == 0) return 1; \
1020  } while(0)
1021 
1022  _("xterm");
1023  _("xterm-color");
1024  _("xterm-256color"); /* iTerm on Mac */
1025  _("screen");
1026  _("ansi"); /* Microsoft Telnet */
1027 #undef _
1028 
1029  return 0;
1030 }
1031 
1032 /** @brief Emit initial welcome banner and prompt on a connection. */
1033 static void
1035 {
1036  unix_main_t *um = &unix_main;
1038  unix_cli_banner_t *banner;
1039  int i, len;
1040 
1041  /*
1042  * Put the first bytes directly into the buffer so that further output is
1043  * queued until everything is ready. (oterwise initial prompt can appear
1044  * mid way through VPP initialization)
1045  */
1046  unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
1047 
1048  if (!um->cli_no_banner)
1049  {
1050  if (cf->ansi_capable)
1051  {
1052  banner = unix_cli_banner_color;
1053  len = ARRAY_LEN (unix_cli_banner_color);
1054  }
1055  else
1056  {
1057  banner = unix_cli_banner;
1058  len = ARRAY_LEN (unix_cli_banner);
1059  }
1060 
1061  for (i = 0; i < len; i++)
1062  {
1064  banner[i].line, banner[i].length);
1065  }
1066  }
1067 
1068  /* Prompt. */
1069  unix_cli_cli_prompt (cf, uf);
1070 
1071  cf->started = 1;
1072 }
1073 
1074 /** @brief A failsafe triggered on a timer to ensure we send the prompt
1075  * to telnet sessions that fail to negotiate the terminal type. */
1076 static void
1078 {
1080  unix_cli_file_t *cf;
1081  (void) delay;
1082 
1083  /* Check the connection didn't close already */
1084  if (pool_is_free_index (cm->cli_file_pool, (uword) arg))
1085  return;
1086 
1087  cf = pool_elt_at_index (cm->cli_file_pool, (uword) arg);
1088 
1089  if (!cf->started)
1090  unix_cli_file_welcome (cm, cf);
1091 }
1092 
1093 /** @brief A mostly no-op Telnet state machine.
1094  * Process Telnet command bytes in a way that ensures we're mostly
1095  * transparent to the Telnet protocol. That is, it's mostly a no-op.
1096  *
1097  * @return -1 if we need more bytes, otherwise a positive integer number of
1098  * bytes to consume from the input_vector, not including the initial
1099  * IAC byte.
1100  */
1101 static i32
1103  unix_cli_file_t * cf,
1104  unix_file_t * uf, u8 * input_vector, uword len)
1105 {
1106  /* Input_vector starts at IAC byte.
1107  * See if we have a complete message; if not, return -1 so we wait for more.
1108  * if we have a complete message, consume those bytes from the vector.
1109  */
1110  i32 consume = 0;
1111 
1112  if (len == 1)
1113  return -1; /* want more bytes */
1114 
1115  switch (input_vector[1])
1116  {
1117  case IAC:
1118  /* two IAC's in a row means to pass through 0xff.
1119  * since that makes no sense here, just consume it.
1120  */
1121  consume = 1;
1122  break;
1123 
1124  case WILL:
1125  case WONT:
1126  case DO:
1127  case DONT:
1128  /* Expect 3 bytes */
1129  if (vec_len (input_vector) < 3)
1130  return -1; /* want more bytes */
1131 
1132  consume = 2;
1133  break;
1134 
1135  case SB:
1136  {
1137  /* Sub option - search ahead for IAC SE to end it */
1138  i32 i;
1139  for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++)
1140  {
1141  if (input_vector[i - 1] == IAC && input_vector[i] == SE)
1142  {
1143  /* We have a complete message; see if we care about it */
1144  switch (input_vector[2])
1145  {
1146  case TELOPT_TTYPE:
1147  if (input_vector[3] != 0)
1148  break;
1149  /* See if the terminal type is ANSI capable */
1150  cf->ansi_capable =
1151  unix_cli_terminal_type (input_vector + 4, i - 5);
1152  /* If session not started, we can release the pause */
1153  if (!cf->started)
1154  /* Send the welcome banner and initial prompt */
1155  unix_cli_file_welcome (&unix_cli_main, cf);
1156  break;
1157 
1158  case TELOPT_NAWS:
1159  /* Window size */
1160  if (i != 8) /* check message is correct size */
1161  break;
1162  cf->width =
1163  clib_net_to_host_u16 (*((u16 *) (input_vector + 3)));
1164  cf->height =
1165  clib_net_to_host_u16 (*((u16 *) (input_vector + 5)));
1166  /* reindex pager buffer */
1168  /* redraw page */
1169  unix_cli_pager_redraw (cf, uf);
1170  break;
1171 
1172  default:
1173  break;
1174  }
1175  /* Consume it all */
1176  consume = i;
1177  break;
1178  }
1179  }
1180 
1181  if (i == UNIX_CLI_MAX_DEPTH_TELNET)
1182  consume = 1; /* hit max search depth, advance one byte */
1183 
1184  if (consume == 0)
1185  return -1; /* want more bytes */
1186 
1187  break;
1188  }
1189 
1190  case GA:
1191  case EL:
1192  case EC:
1193  case AO:
1194  case IP:
1195  case BREAK:
1196  case DM:
1197  case NOP:
1198  case SE:
1199  case EOR:
1200  case ABORT:
1201  case SUSP:
1202  case xEOF:
1203  /* Simple one-byte messages */
1204  consume = 1;
1205  break;
1206 
1207  case AYT:
1208  /* Are You There - trigger a visible response */
1209  consume = 1;
1210  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10);
1211  break;
1212 
1213  default:
1214  /* Unknown command! Eat the IAC byte */
1215  break;
1216  }
1217 
1218  return consume;
1219 }
1220 
1221 /** @brief Process actionable input.
1222  * Based on the \c action process the input; this typically involves
1223  * searching the command history or editing the current command line.
1224  */
1225 static int
1227  unix_main_t * um,
1228  unix_cli_file_t * cf,
1229  unix_file_t * uf,
1230  u8 input, unix_cli_parse_action_t action)
1231 {
1232  u8 *prev;
1233  int j, delta;
1234 
1235  switch (action)
1236  {
1238  break;
1239 
1242  if (!cf->has_history || !cf->history_limit)
1243  break;
1244  if (cf->search_mode == 0)
1245  {
1246  /* Erase the current command (if any) */
1247  for (j = 0; j < (vec_len (cf->current_command)); j++)
1248  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1249 
1252  if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1253  cf->search_mode = -1;
1254  else
1255  cf->search_mode = 1;
1256  cf->cursor = 0;
1257  }
1258  else
1259  {
1260  if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
1261  cf->search_mode = -1;
1262  else
1263  cf->search_mode = 1;
1264 
1265  cf->excursion += cf->search_mode;
1266  goto search_again;
1267  }
1268  break;
1269 
1271  /* Erase the command from the cursor to the start */
1272 
1273  /* Shimmy forwards to the new end of line position */
1274  delta = vec_len (cf->current_command) - cf->cursor;
1275  for (j = cf->cursor; j > delta; j--)
1276  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1277  /* Zap from here to the end of what is currently displayed */
1278  for (; j < (vec_len (cf->current_command)); j++)
1279  unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1280  /* Get back to the start of the line */
1281  for (j = 0; j < (vec_len (cf->current_command)); j++)
1282  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1283 
1284  j = vec_len (cf->current_command) - cf->cursor;
1285  memmove (cf->current_command, cf->current_command + cf->cursor, j);
1286  _vec_len (cf->current_command) = j;
1287 
1288  /* Print the new contents */
1290  /* Shimmy back to the start */
1291  for (j = 0; j < (vec_len (cf->current_command)); j++)
1292  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1293  cf->cursor = 0;
1294 
1295  cf->search_mode = 0;
1296  break;
1297 
1299  /* Erase the command from the cursor to the end */
1300 
1301  /* Zap from cursor to end of what is currently displayed */
1302  for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1303  unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1304  /* Get back to where we were */
1305  for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1306  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1307 
1308  /* Truncate the line at the cursor */
1309  _vec_len (cf->current_command) = cf->cursor;
1310 
1311  cf->search_mode = 0;
1312  break;
1313 
1315  if (cf->cursor > 0)
1316  {
1317  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1318  cf->cursor--;
1319  }
1320 
1321  cf->search_mode = 0;
1322  break;
1323 
1325  if (cf->cursor < vec_len (cf->current_command))
1326  {
1327  /* have to emit the character under the cursor */
1329  cf->current_command + cf->cursor, 1);
1330  cf->cursor++;
1331  }
1332 
1333  cf->search_mode = 0;
1334  break;
1335 
1338  if (!cf->has_history || !cf->history_limit)
1339  break;
1340  cf->search_mode = 0;
1341  /* Erase the command */
1342  for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
1343  unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
1344  for (j = 0; j < (vec_len (cf->current_command)); j++)
1345  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1347  if (vec_len (cf->command_history))
1348  {
1349  if (action == UNIX_CLI_PARSE_ACTION_UP)
1350  delta = -1;
1351  else
1352  delta = 1;
1353 
1354  cf->excursion += delta;
1355 
1356  if (cf->excursion == vec_len (cf->command_history))
1357  {
1358  /* down-arrowed to last entry - want a blank line */
1359  _vec_len (cf->current_command) = 0;
1360  }
1361  else if (cf->excursion < 0)
1362  {
1363  /* up-arrowed over the start to the end, want a blank line */
1364  cf->excursion = vec_len (cf->command_history);
1365  _vec_len (cf->current_command) = 0;
1366  }
1367  else
1368  {
1369  if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1370  /* down-arrowed past end - wrap to start */
1371  cf->excursion = 0;
1372 
1373  /* Print the command at the current position */
1374  prev = cf->command_history[cf->excursion];
1375  vec_validate (cf->current_command, vec_len (prev) - 1);
1376 
1377  clib_memcpy (cf->current_command, prev, vec_len (prev));
1378  _vec_len (cf->current_command) = vec_len (prev);
1380  vec_len (cf->current_command));
1381  }
1382  cf->cursor = vec_len (cf->current_command);
1383 
1384  break;
1385  }
1386  break;
1387 
1389  if (vec_len (cf->current_command) && cf->cursor > 0)
1390  {
1391  while (cf->cursor)
1392  {
1393  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1394  cf->cursor--;
1395  }
1396  }
1397 
1398  cf->search_mode = 0;
1399  break;
1400 
1402  if (vec_len (cf->current_command) &&
1403  cf->cursor < vec_len (cf->current_command))
1404  {
1406  cf->current_command + cf->cursor,
1407  vec_len (cf->current_command) -
1408  cf->cursor);
1409  cf->cursor = vec_len (cf->current_command);
1410  }
1411 
1412  cf->search_mode = 0;
1413  break;
1414 
1416  if (vec_len (cf->current_command) && cf->cursor > 0)
1417  {
1418  j = cf->cursor;
1419 
1420  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1421  j--;
1422 
1423  while (j && isspace (cf->current_command[j]))
1424  {
1425  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1426  j--;
1427  }
1428  while (j && !isspace (cf->current_command[j]))
1429  {
1430  if (isspace (cf->current_command[j - 1]))
1431  break;
1432  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1433  j--;
1434  }
1435 
1436  cf->cursor = j;
1437  }
1438 
1439  cf->search_mode = 0;
1440  break;
1441 
1443  if (vec_len (cf->current_command) &&
1444  cf->cursor < vec_len (cf->current_command))
1445  {
1446  int e = vec_len (cf->current_command);
1447  j = cf->cursor;
1448  while (j < e && !isspace (cf->current_command[j]))
1449  j++;
1450  while (j < e && isspace (cf->current_command[j]))
1451  j++;
1453  cf->current_command + cf->cursor,
1454  j - cf->cursor);
1455  cf->cursor = j;
1456  }
1457 
1458  cf->search_mode = 0;
1459  break;
1460 
1461 
1463  if (vec_len (cf->current_command))
1464  {
1465  if (cf->cursor == vec_len (cf->current_command))
1466  {
1467  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1468  _vec_len (cf->current_command)--;
1469  cf->cursor--;
1470  }
1471  else if (cf->cursor > 0)
1472  {
1473  /* shift everything at & to the right of the cursor left by 1 */
1474  j = vec_len (cf->current_command) - cf->cursor;
1475  memmove (cf->current_command + cf->cursor - 1,
1476  cf->current_command + cf->cursor, j);
1477  _vec_len (cf->current_command)--;
1478  cf->cursor--;
1479  /* redraw the rest of the line */
1480  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1482  cf->current_command + cf->cursor,
1483  j);
1484  unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
1485  /* and shift the terminal cursor back where it should be */
1486  while (--j)
1487  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1488  }
1489  }
1490  cf->search_mode = 0;
1491  cf->excursion = 0;
1493  break;
1494 
1496  if (vec_len (cf->current_command))
1497  {
1498  if (cf->cursor < vec_len (cf->current_command))
1499  {
1500  /* shift everything to the right of the cursor left by 1 */
1501  j = vec_len (cf->current_command) - cf->cursor - 1;
1502  memmove (cf->current_command + cf->cursor,
1503  cf->current_command + cf->cursor + 1, j);
1504  _vec_len (cf->current_command)--;
1505  /* redraw the rest of the line */
1507  cf->current_command + cf->cursor,
1508  j);
1509  unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
1510  /* and shift the terminal cursor back where it should be */
1511  if (j)
1512  {
1513  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1514  while (--j)
1515  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1516  }
1517  }
1518  }
1519  else if (input == 'D' - '@')
1520  {
1521  /* ^D with no command entered = quit */
1522  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5);
1526  cf - cm->cli_file_pool);
1527  }
1528  cf->search_mode = 0;
1529  cf->excursion = 0;
1531  break;
1532 
1534  /* If we're in ANSI mode, clear the screen.
1535  * Then redraw the prompt and any existing command input, then put
1536  * the cursor back where it was in that line.
1537  */
1538  if (cf->ansi_capable)
1540  (u8 *) ANSI_CLEAR,
1541  sizeof (ANSI_CLEAR) - 1);
1542  else
1543  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1544 
1545  unix_vlib_cli_output_raw (cf, uf,
1546  cm->cli_prompt, vec_len (cm->cli_prompt));
1547  unix_vlib_cli_output_raw (cf, uf,
1548  cf->current_command,
1549  vec_len (cf->current_command));
1550  for (j = cf->cursor; j < vec_len (cf->current_command); j++)
1551  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
1552 
1553  break;
1554 
1557  /* TODO */
1558  break;
1559 
1560 
1562  pager_quit:
1563  unix_cli_pager_prompt_erase (cf, uf);
1564  unix_cli_pager_reset (cf);
1565  unix_cli_cli_prompt (cf, uf);
1566  break;
1567 
1570  /* show next page of the buffer */
1571  if (cf->height + cf->pager_start < vec_len (cf->pager_index))
1572  {
1573  u8 *line = NULL;
1575 
1576  int m = cf->pager_start + (cf->height - 1);
1577  unix_cli_pager_prompt_erase (cf, uf);
1578  for (j = m;
1579  j < vec_len (cf->pager_index) && cf->pager_start < m;
1580  j++, cf->pager_start++)
1581  {
1582  pi = &cf->pager_index[j];
1583  line = cf->pager_vector[pi->line] + pi->offset;
1584  unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1585  }
1586  /* if the last line didn't end in newline, add a newline */
1587  if (pi && line[pi->length - 1] != '\n')
1588  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1589  unix_cli_pager_prompt (cf, uf);
1590  }
1591  else
1592  {
1593  if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT)
1594  /* no more in buffer, exit, but only if it was <space> */
1595  goto pager_quit;
1596  }
1597  break;
1598 
1601  /* display the next line of the buffer */
1602  if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
1603  {
1604  u8 *line;
1606 
1607  unix_cli_pager_prompt_erase (cf, uf);
1608  pi = &cf->pager_index[cf->pager_start + (cf->height - 1)];
1609  line = cf->pager_vector[pi->line] + pi->offset;
1610  unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1611  cf->pager_start++;
1612  /* if the last line didn't end in newline, add a newline */
1613  if (line[pi->length - 1] != '\n')
1614  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1615  unix_cli_pager_prompt (cf, uf);
1616  }
1617  else
1618  {
1619  if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF)
1620  /* no more in buffer, exit, but only if it was <enter> */
1621  goto pager_quit;
1622  }
1623 
1624  break;
1625 
1627  /* scroll the page back one line */
1628  if (cf->pager_start > 0)
1629  {
1630  u8 *line = NULL;
1632 
1633  cf->pager_start--;
1634  if (cf->ansi_capable)
1635  {
1636  pi = &cf->pager_index[cf->pager_start];
1637  line = cf->pager_vector[pi->line] + pi->offset;
1638  unix_cli_pager_prompt_erase (cf, uf);
1640  sizeof (ANSI_SCROLLDN) - 1);
1642  sizeof (ANSI_SAVECURSOR) - 1);
1643  unix_cli_ansi_cursor (cf, uf, 1, 1);
1645  sizeof (ANSI_CLEARLINE) - 1);
1646  unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1648  sizeof (ANSI_RESTCURSOR) - 1);
1649  unix_cli_pager_prompt_erase (cf, uf);
1650  unix_cli_pager_prompt (cf, uf);
1651  }
1652  else
1653  {
1654  int m = cf->pager_start + (cf->height - 1);
1655  unix_cli_pager_prompt_erase (cf, uf);
1656  for (j = cf->pager_start;
1657  j < vec_len (cf->pager_index) && j < m; j++)
1658  {
1659  pi = &cf->pager_index[j];
1660  line = cf->pager_vector[pi->line] + pi->offset;
1661  unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1662  }
1663  /* if the last line didn't end in newline, add a newline */
1664  if (pi && line[pi->length - 1] != '\n')
1665  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1666  unix_cli_pager_prompt (cf, uf);
1667  }
1668  }
1669  break;
1670 
1672  /* back to the first page of the buffer */
1673  if (cf->pager_start > 0)
1674  {
1675  u8 *line = NULL;
1677 
1678  cf->pager_start = 0;
1679  int m = cf->pager_start + (cf->height - 1);
1680  unix_cli_pager_prompt_erase (cf, uf);
1681  for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1682  j++)
1683  {
1684  pi = &cf->pager_index[j];
1685  line = cf->pager_vector[pi->line] + pi->offset;
1686  unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1687  }
1688  /* if the last line didn't end in newline, add a newline */
1689  if (pi && line[pi->length - 1] != '\n')
1690  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1691  unix_cli_pager_prompt (cf, uf);
1692  }
1693  break;
1694 
1696  /* skip to the last page of the buffer */
1697  if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
1698  {
1699  u8 *line = NULL;
1701 
1702  cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1);
1703  unix_cli_pager_prompt_erase (cf, uf);
1704  unix_cli_pager_message (cf, uf, "skipping", "\n");
1705  for (j = cf->pager_start; j < vec_len (cf->pager_index); j++)
1706  {
1707  pi = &cf->pager_index[j];
1708  line = cf->pager_vector[pi->line] + pi->offset;
1709  unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1710  }
1711  /* if the last line didn't end in newline, add a newline */
1712  if (pi && line[pi->length - 1] != '\n')
1713  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1714  unix_cli_pager_prompt (cf, uf);
1715  }
1716  break;
1717 
1719  /* wander back one page in the buffer */
1720  if (cf->pager_start > 0)
1721  {
1722  u8 *line = NULL;
1724  int m;
1725 
1726  if (cf->pager_start >= cf->height)
1727  cf->pager_start -= cf->height - 1;
1728  else
1729  cf->pager_start = 0;
1730  m = cf->pager_start + cf->height - 1;
1731  unix_cli_pager_prompt_erase (cf, uf);
1732  for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m;
1733  j++)
1734  {
1735  pi = &cf->pager_index[j];
1736  line = cf->pager_vector[pi->line] + pi->offset;
1737  unix_vlib_cli_output_cooked (cf, uf, line, pi->length);
1738  }
1739  /* if the last line didn't end in newline, add a newline */
1740  if (pi && line[pi->length - 1] != '\n')
1741  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1742  unix_cli_pager_prompt (cf, uf);
1743  }
1744  break;
1745 
1747  /* Redraw the current pager screen */
1748  unix_cli_pager_redraw (cf, uf);
1749  break;
1750 
1752  /* search forwards in the buffer */
1753  break;
1754 
1755 
1757  crlf:
1758  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
1759 
1760  if (cf->has_history && cf->history_limit)
1761  {
1762  if (cf->command_history
1763  && vec_len (cf->command_history) >= cf->history_limit)
1764  {
1765  vec_free (cf->command_history[0]);
1766  vec_delete (cf->command_history, 1, 0);
1767  }
1768  /* Don't add blank lines to the cmd history */
1769  if (vec_len (cf->current_command))
1770  {
1771  /* Don't duplicate the previous command */
1772  j = vec_len (cf->command_history);
1773  if (j == 0 ||
1774  (vec_len (cf->current_command) !=
1775  vec_len (cf->command_history[j - 1])
1776  || memcmp (cf->current_command, cf->command_history[j - 1],
1777  vec_len (cf->current_command)) != 0))
1778  {
1779  /* copy the command to the history */
1780  u8 *c = 0;
1781  vec_append (c, cf->current_command);
1782  vec_add1 (cf->command_history, c);
1783  cf->command_number++;
1784  }
1785  }
1786  cf->excursion = vec_len (cf->command_history);
1787  }
1788 
1789  cf->search_mode = 0;
1791  cf->cursor = 0;
1792 
1793  return 0;
1794 
1797  if (vec_len (cf->pager_index))
1798  {
1799  /* no-op for now */
1800  }
1801  else if (cf->has_history && cf->search_mode && isprint (input))
1802  {
1803  int k, limit, offset;
1804  u8 *item;
1805 
1806  vec_add1 (cf->search_key, input);
1807 
1808  search_again:
1809  for (j = 0; j < vec_len (cf->command_history); j++)
1810  {
1811  if (cf->excursion > (i32) vec_len (cf->command_history) - 1)
1812  cf->excursion = 0;
1813  else if (cf->excursion < 0)
1814  cf->excursion = vec_len (cf->command_history) - 1;
1815 
1816  item = cf->command_history[cf->excursion];
1817 
1818  limit = (vec_len (cf->search_key) > vec_len (item)) ?
1819  vec_len (item) : vec_len (cf->search_key);
1820 
1821  for (offset = 0; offset <= vec_len (item) - limit; offset++)
1822  {
1823  for (k = 0; k < limit; k++)
1824  {
1825  if (item[k + offset] != cf->search_key[k])
1826  goto next_offset;
1827  }
1828  goto found_at_offset;
1829 
1830  next_offset:
1831  ;
1832  }
1833  goto next;
1834 
1835  found_at_offset:
1836  for (j = 0; j < vec_len (cf->current_command); j++)
1837  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
1838 
1839  vec_validate (cf->current_command, vec_len (item) - 1);
1840  clib_memcpy (cf->current_command, item, vec_len (item));
1841  _vec_len (cf->current_command) = vec_len (item);
1842 
1844  vec_len (cf->current_command));
1845  cf->cursor = vec_len (cf->current_command);
1846  goto found;
1847 
1848  next:
1849  cf->excursion += cf->search_mode;
1850  }
1851 
1852  unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12);
1855  cf->search_mode = 0;
1856  cf->cursor = 0;
1857  goto crlf;
1858  }
1859  else if (isprint (input)) /* skip any errant control codes */
1860  {
1861  if (cf->cursor == vec_len (cf->current_command))
1862  {
1863  /* Append to end */
1864  vec_add1 (cf->current_command, input);
1865  cf->cursor++;
1866 
1867  /* Echo the character back to the client */
1868  unix_vlib_cli_output_raw (cf, uf, &input, 1);
1869  }
1870  else
1871  {
1872  /* Insert at cursor: resize +1 byte, move everything over */
1873  j = vec_len (cf->current_command) - cf->cursor;
1874  vec_add1 (cf->current_command, (u8) 'A');
1875  memmove (cf->current_command + cf->cursor + 1,
1876  cf->current_command + cf->cursor, j);
1877  cf->current_command[cf->cursor] = input;
1878  /* Redraw the line */
1879  j++;
1880  unix_vlib_cli_output_raw (cf, uf,
1881  cf->current_command + cf->cursor, j);
1882  /* Put terminal cursor back */
1883  while (--j)
1884  unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
1885  cf->cursor++;
1886  }
1887  }
1888  else
1889  {
1890  /* no-op - not printable or otherwise not actionable */
1891  }
1892 
1893  found:
1894 
1895  break;
1896 
1898  break;
1899  }
1900  return 1;
1901 }
1902 
1903 /** @brief Process input bytes on a stream to provide line editing and
1904  * command history in the CLI. */
1905 static int
1907  unix_main_t * um, unix_cli_file_t * cf)
1908 {
1910  int i;
1911 
1912  for (i = 0; i < vec_len (cf->input_vector); i++)
1913  {
1914  unix_cli_parse_action_t action;
1915  i32 matched = 0;
1917 
1918  /* If we're in the pager mode, search the pager actions */
1919  a =
1920  vec_len (cf->pager_index) ? unix_cli_parse_pager :
1922 
1923  /* See if the input buffer is some sort of control code */
1924  action = unix_cli_match_action (a, &cf->input_vector[i],
1925  vec_len (cf->input_vector) - i,
1926  &matched);
1927 
1928  switch (action)
1929  {
1931  if (i)
1932  {
1933  /* There was a partial match which means we need more bytes
1934  * than the input buffer currently has.
1935  * Since the bytes before here have been processed, shift
1936  * the remaining contents to the start of the input buffer.
1937  */
1938  vec_delete (cf->input_vector, i, 0);
1939  }
1940  return 1; /* wait for more */
1941 
1943  /* process telnet options */
1944  matched = unix_cli_process_telnet (um, cf, uf,
1945  cf->input_vector + i,
1946  vec_len (cf->input_vector) - i);
1947  if (matched < 0)
1948  {
1949  if (i)
1950  {
1951  /* There was a partial match which means we need more bytes
1952  * than the input buffer currently has.
1953  * Since the bytes before here have been processed, shift
1954  * the remaining contents to the start of the input buffer.
1955  */
1956  vec_delete (cf->input_vector, i, 0);
1957  }
1958  return 1; /* wait for more */
1959  }
1960  break;
1961 
1962  default:
1963  /* process the action */
1964  if (!unix_cli_line_process_one (cm, um, cf, uf,
1965  cf->input_vector[i], action))
1966  {
1967  /* CRLF found. Consume the bytes from the input_vector */
1968  vec_delete (cf->input_vector, i + matched, 0);
1969  /* And tell our caller to execute cf->input_command */
1970  return 0;
1971  }
1972  }
1973 
1974  i += matched;
1975  }
1976 
1978  return 1;
1979 }
1980 
1981 /** @brief Process input to a CLI session. */
1982 static void
1984 {
1985  unix_main_t *um = &unix_main;
1986  unix_file_t *uf;
1987  unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
1988  unformat_input_t input;
1989  int vlib_parse_eval (u8 *);
1990 
1991 more:
1992  /* Try vlibplex first. Someday... */
1993  if (0 && vlib_parse_eval (cf->input_vector) == 0)
1994  goto done;
1995 
1996  if (cf->line_mode)
1997  {
1998  /* just treat whatever we got as a complete line of input */
1999  cf->current_command = cf->input_vector;
2000  }
2001  else
2002  {
2003  /* Line edit, echo, etc. */
2004  if (unix_cli_line_edit (cm, um, cf))
2005  /* want more input */
2006  return;
2007  }
2008 
2009  if (um->log_fd)
2010  {
2011  static u8 *lv;
2012  vec_reset_length (lv);
2013  lv = format (lv, "%U[%d]: %v",
2014  format_timeval, 0 /* current bat-time */ ,
2015  0 /* current bat-format */ ,
2016  cli_file_index, cf->input_vector);
2017  {
2018  int rv __attribute__ ((unused)) =
2019  write (um->log_fd, lv, vec_len (lv));
2020  }
2021  }
2022 
2023  /* Copy our input command to a new string */
2024  unformat_init_vector (&input, cf->current_command);
2025 
2026  /* Remove leading white space from input. */
2027  (void) unformat (&input, "");
2028 
2029  cm->current_input_file_index = cli_file_index;
2030  cf->pager_start = 0; /* start a new pager session */
2031 
2034  cli_file_index);
2035 
2036  /* Zero buffer since otherwise unformat_free will call vec_free on it. */
2037  input.buffer = 0;
2038 
2039  unformat_free (&input);
2040 
2041  /* Re-fetch pointer since pool may have moved. */
2042  cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2044 
2045 done:
2046  /* reset vector; we'll re-use it later */
2047  if (cf->line_mode)
2049  else
2051 
2052  if (cf->no_pager == 2)
2053  {
2054  /* Pager was programmatically disabled */
2055  unix_cli_pager_message (cf, uf, "pager buffer overflowed", "\n");
2056  cf->no_pager = um->cli_no_pager;
2057  }
2058 
2059  if (vec_len (cf->pager_index) == 0
2060  || vec_len (cf->pager_index) < cf->height)
2061  {
2062  /* There was no need for the pager */
2063  unix_cli_pager_reset (cf);
2064 
2065  /* Prompt. */
2066  unix_cli_cli_prompt (cf, uf);
2067  }
2068  else
2069  {
2070  /* Display the pager prompt */
2071  unix_cli_pager_prompt (cf, uf);
2072  }
2073 
2074  /* Any residual data in the input vector? */
2075  if (vec_len (cf->input_vector))
2076  goto more;
2077 }
2078 
2079 /** Destroy a CLI session.
2080  * @note If we destroy the @c stdin session this additionally signals
2081  * the shutdown of VPP.
2082  */
2083 static void
2084 unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index)
2085 {
2086  unix_main_t *um = &unix_main;
2087  unix_cli_file_t *cf;
2088  unix_file_t *uf;
2089  int i;
2090 
2091  cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
2093 
2094  /* Quit/EOF on stdin means quit program. */
2097 
2098  vec_free (cf->current_command);
2099  vec_free (cf->search_key);
2100 
2101  for (i = 0; i < vec_len (cf->command_history); i++)
2102  vec_free (cf->command_history[i]);
2103 
2104  vec_free (cf->command_history);
2105 
2106  unix_file_del (um, uf);
2107 
2108  unix_cli_file_free (cf);
2109  pool_put (cm->cli_file_pool, cf);
2110 }
2111 
2112 /** Handle system events. */
2113 static uword
2116 {
2118  uword i, *data = 0;
2119 
2120  while (1)
2121  {
2122  unix_cli_process_event_type_t event_type;
2124  event_type = vlib_process_get_events (vm, &data);
2125 
2126  switch (event_type)
2127  {
2129  for (i = 0; i < vec_len (data); i++)
2130  unix_cli_process_input (cm, data[i]);
2131  break;
2132 
2134  /* Kill this process. */
2135  for (i = 0; i < vec_len (data); i++)
2136  unix_cli_kill (cm, data[i]);
2137  goto done;
2138  }
2139 
2140  if (data)
2141  _vec_len (data) = 0;
2142  }
2143 
2144 done:
2145  vec_free (data);
2146 
2147  vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED);
2148 
2149  /* Add node index so we can re-use this process later. */
2151 
2152  return 0;
2153 }
2154 
2155 /** Called when a CLI session file descriptor can be written to without
2156  * blocking. */
2157 static clib_error_t *
2159 {
2161  unix_cli_file_t *cf;
2162  int n;
2163 
2165 
2166  /* Flush output vector. */
2167  n = write (uf->file_descriptor,
2168  cf->output_vector, vec_len (cf->output_vector));
2169 
2170  if (n < 0 && errno != EAGAIN)
2171  return clib_error_return_unix (0, "write");
2172 
2173  else if (n > 0)
2174  unix_cli_del_pending_output (uf, cf, n);
2175 
2176  return /* no error */ 0;
2177 }
2178 
2179 /** Called when a CLI session file descriptor has data to be read. */
2180 static clib_error_t *
2182 {
2183  unix_main_t *um = &unix_main;
2185  unix_cli_file_t *cf;
2186  uword l;
2187  int n, n_read, n_try;
2188 
2190 
2191  n = n_try = 4096;
2192  while (n == n_try)
2193  {
2194  l = vec_len (cf->input_vector);
2195  vec_resize (cf->input_vector, l + n_try);
2196 
2197  n = read (uf->file_descriptor, cf->input_vector + l, n_try);
2198 
2199  /* Error? */
2200  if (n < 0 && errno != EAGAIN)
2201  return clib_error_return_unix (0, "read");
2202 
2203  n_read = n < 0 ? 0 : n;
2204  _vec_len (cf->input_vector) = l + n_read;
2205  }
2206 
2207  if (!(n < 0))
2209  cf->process_node_index,
2210  (n_read == 0
2213  /* event data */ uf->private_data);
2214 
2215  return /* no error */ 0;
2216 }
2217 
2218 /** Store a new CLI session.
2219  * @param name The name of the session.
2220  * @param fd The file descriptor for the session I/O.
2221  * @return The session ID.
2222  */
2223 static u32
2224 unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
2225 {
2226  unix_main_t *um = &unix_main;
2227  unix_cli_file_t *cf;
2228  unix_file_t template = { 0 };
2229  vlib_main_t *vm = um->vlib_main;
2230  vlib_node_t *n;
2231 
2232  name = (char *) format (0, "unix-cli-%s", name);
2233 
2235  {
2237 
2238  /* Find node and give it new name. */
2239  n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
2240  vec_free (n->name);
2241  n->name = (u8 *) name;
2242 
2243  vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
2244 
2245  _vec_len (cm->unused_cli_process_node_indices) = l - 1;
2246  }
2247  else
2248  {
2249  static vlib_node_registration_t r = {
2250  .function = unix_cli_process,
2251  .type = VLIB_NODE_TYPE_PROCESS,
2252  .process_log2_n_stack_bytes = 16,
2253  };
2254 
2255  r.name = name;
2256  vlib_register_node (vm, &r);
2257  vec_free (name);
2258 
2259  n = vlib_get_node (vm, r.index);
2260  }
2261 
2262  pool_get (cm->cli_file_pool, cf);
2263  memset (cf, 0, sizeof (*cf));
2264 
2265  template.read_function = unix_cli_read_ready;
2266  template.write_function = unix_cli_write_ready;
2267  template.file_descriptor = fd;
2268  template.private_data = cf - cm->cli_file_pool;
2269 
2270  cf->process_node_index = n->index;
2271  cf->unix_file_index = unix_file_add (um, &template);
2272  cf->output_vector = 0;
2273  cf->input_vector = 0;
2274 
2276 
2279  p->output_function_arg = cf - cm->cli_file_pool;
2280 
2281  return cf - cm->cli_file_pool;
2282 }
2283 
2284 /** Telnet listening socket has a new connection. */
2285 static clib_error_t *
2287 {
2288  unix_main_t *um = &unix_main;
2290  clib_socket_t *s = &um->cli_listen_socket;
2291  clib_socket_t client;
2292  char *client_name;
2293  clib_error_t *error;
2294  unix_cli_file_t *cf;
2295  u32 cf_index;
2296 
2297  error = clib_socket_accept (s, &client);
2298  if (error)
2299  return error;
2300 
2301  client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
2302 
2303  cf_index = unix_cli_file_add (cm, client_name, client.fd);
2304  cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2305 
2306  /* No longer need CLIB version of socket. */
2307  clib_socket_free (&client);
2308 
2309  vec_free (client_name);
2310 
2311  /* if we're supposed to run telnet session in character mode (default) */
2312  if (um->cli_line_mode == 0)
2313  {
2314  /*
2315  * Set telnet client character mode, echo on, suppress "go-ahead".
2316  * Technically these should be negotiated, but this works.
2317  */
2318  u8 charmode_option[] = {
2319  IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */
2320  IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */
2321  IAC, WILL, TELOPT_SGA, /* server willl supress GA */
2322  IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */
2323  IAC, WILL, TELOPT_ECHO, /* server will do echo */
2324  IAC, DONT, TELOPT_ECHO, /* client should not echo */
2325  IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */
2326  IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */
2327  IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */
2328  IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */
2329  };
2330 
2331  /* Enable history on this CLI */
2332  cf->history_limit = um->cli_history_limit;
2333  cf->has_history = cf->history_limit != 0;
2334 
2335  /* Make sure this session is in line mode */
2336  cf->line_mode = 0;
2337 
2338  /* We need CRLF */
2339  cf->crlf_mode = 1;
2340 
2341  /* Setup the pager */
2342  cf->no_pager = um->cli_no_pager;
2343 
2345 
2346  /* Send the telnet options */
2347  unix_vlib_cli_output_raw (cf, uf, charmode_option,
2348  ARRAY_LEN (charmode_option));
2349 
2350  /* In case the client doesn't negotiate terminal type, use
2351  * a timer to kick off the initial prompt. */
2352  timer_call (unix_cli_file_welcome_timer, cf_index, 1);
2353  }
2354 
2355  return error;
2356 }
2357 
2358 /** The system terminal has informed us that the window size
2359  * has changed.
2360  */
2361 static void
2363 {
2364  unix_main_t *um = &unix_main;
2367  cm->stdin_cli_file_index);
2369  struct winsize ws;
2370  (void) signum;
2371 
2372  /* Terminal resized, fetch the new size */
2373  if (ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws) < 0)
2374  {
2375  /* "Should never happen..." */
2376  clib_unix_warning ("TIOCGWINSZ");
2377  /* We can't trust ws.XXX... */
2378  return;
2379  }
2380  cf->width = ws.ws_col;
2381  cf->height = ws.ws_row;
2382 
2383  /* Reindex the pager buffer */
2385 
2386  /* Redraw the page */
2387  unix_cli_pager_redraw (cf, uf);
2388 }
2389 
2390 /** Handle configuration directives in the @em unix section. */
2391 static clib_error_t *
2393 {
2394  unix_main_t *um = &unix_main;
2396  int flags;
2397  clib_error_t *error = 0;
2398  unix_cli_file_t *cf;
2399  u32 cf_index;
2400  struct termios tio;
2401  struct sigaction sa;
2402  struct winsize ws;
2403  u8 *term;
2404 
2405  /* We depend on unix flags being set. */
2406  if ((error = vlib_call_config_function (vm, unix_config)))
2407  return error;
2408 
2409  if (um->flags & UNIX_FLAG_INTERACTIVE)
2410  {
2411  /* Set stdin to be non-blocking. */
2412  if ((flags = fcntl (UNIX_CLI_STDIN_FD, F_GETFL, 0)) < 0)
2413  flags = 0;
2414  (void) fcntl (UNIX_CLI_STDIN_FD, F_SETFL, flags | O_NONBLOCK);
2415 
2416  cf_index = unix_cli_file_add (cm, "stdin", UNIX_CLI_STDIN_FD);
2417  cf = pool_elt_at_index (cm->cli_file_pool, cf_index);
2418  cm->stdin_cli_file_index = cf_index;
2419 
2420  /* If stdin is a tty and we are using chacracter mode, enable
2421  * history on the CLI and set the tty line discipline accordingly. */
2422  if (isatty (UNIX_CLI_STDIN_FD) && um->cli_line_mode == 0)
2423  {
2424  /* Capture terminal resize events */
2425  memset (&sa, 0, sizeof (sa));
2426  sa.sa_handler = unix_cli_resize_interrupt;
2427  if (sigaction (SIGWINCH, &sa, 0) < 0)
2428  clib_panic ("sigaction");
2429 
2430  /* Retrieve the current terminal size */
2431  ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws);
2432  cf->width = ws.ws_col;
2433  cf->height = ws.ws_row;
2434 
2435  if (cf->width == 0 || cf->height == 0)
2436  /* We have a tty, but no size. Stick to line mode. */
2437  goto notty;
2438 
2439  /* Setup the history */
2440  cf->history_limit = um->cli_history_limit;
2441  cf->has_history = cf->history_limit != 0;
2442 
2443  /* Setup the pager */
2444  cf->no_pager = um->cli_no_pager;
2445 
2446  /* We're going to be in char by char mode */
2447  cf->line_mode = 0;
2448 
2449  /* Save the original tty state so we can restore it later */
2450  tcgetattr (UNIX_CLI_STDIN_FD, &um->tio_stdin);
2451  um->tio_isset = 1;
2452 
2453  /* Tweak the tty settings */
2454  tio = um->tio_stdin;
2455  /* echo off, canonical mode off, ext'd input processing off */
2456  tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
2457  tio.c_cc[VMIN] = 1; /* 1 byte at a time */
2458  tio.c_cc[VTIME] = 0; /* no timer */
2459  tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &tio);
2460 
2461  /* See if we can do ANSI/VT100 output */
2462  term = (u8 *) getenv ("TERM");
2463  if (term != NULL)
2465  strlen ((char *)
2466  term));
2467  }
2468  else
2469  {
2470  notty:
2471  /* No tty, so make sure these things are off */
2472  cf->no_pager = 1;
2473  cf->history_limit = 0;
2474  cf->has_history = 0;
2475  cf->line_mode = 1;
2476  }
2477 
2478  /* Send banner and initial prompt */
2479  unix_cli_file_welcome (cm, cf);
2480  }
2481 
2482  /* If we have socket config, LISTEN, otherwise, don't */
2483  clib_socket_t *s = &um->cli_listen_socket;
2484  if (s->config && s->config[0] != 0)
2485  {
2486  /* CLI listen. */
2487  unix_file_t template = { 0 };
2488 
2489  s->flags = SOCKET_IS_SERVER; /* listen, don't connect */
2490  error = clib_socket_init (s);
2491 
2492  if (error)
2493  return error;
2494 
2495  template.read_function = unix_cli_listen_read_ready;
2496  template.file_descriptor = s->fd;
2497 
2498  unix_file_add (um, &template);
2499  }
2500 
2501  /* Set CLI prompt. */
2502  if (!cm->cli_prompt)
2503  cm->cli_prompt = format (0, "VLIB: ");
2504 
2505  return 0;
2506 }
2507 
2508 /*?
2509  * This module has no configurable parameters.
2510 ?*/
2512 
2513 /** Called when VPP is shutting down, this restores the system
2514  * terminal state if previously saved.
2515  */
2516 static clib_error_t *
2518 {
2519  unix_main_t *um = &unix_main;
2520 
2521  /* If stdin is a tty and we saved the tty state, reset the tty state */
2522  if (isatty (UNIX_CLI_STDIN_FD) && um->tio_isset)
2523  tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &um->tio_stdin);
2524 
2525  return 0;
2526 }
2527 
2529 
2530 /** Set the CLI prompt.
2531  * @param prompt The C string to set the prompt to.
2532  * @note This setting is global; it impacts all current
2533  * and future CLI sessions.
2534  */
2535 void
2537 {
2538  char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s ";
2540  if (cm->cli_prompt)
2541  vec_free (cm->cli_prompt);
2542  cm->cli_prompt = format (0, fmt, prompt);
2543 }
2544 
2545 /** CLI command to quit the terminal session.
2546  * @note If this is a stdin session then this will
2547  * shutdown VPP also.
2548  */
2549 static clib_error_t *
2551  unformat_input_t * input, vlib_cli_command_t * cmd)
2552 {
2554 
2556  vlib_current_process (vm),
2559  return 0;
2560 }
2561 
2562 /*?
2563  * Terminates the current CLI session.
2564  *
2565  * If VPP is running in @em interactive mode and this is the console session
2566  * (that is, the session on @c stdin) then this will also terminate VPP.
2567 ?*/
2568 /* *INDENT-OFF* */
2569 VLIB_CLI_COMMAND (unix_cli_quit_command, static) = {
2570  .path = "quit",
2571  .short_help = "Exit CLI",
2572  .function = unix_cli_quit,
2573 };
2574 /* *INDENT-ON* */
2575 
2576 /** CLI command to execute a VPP command script. */
2577 static clib_error_t *
2579  unformat_input_t * input, vlib_cli_command_t * cmd)
2580 {
2581  char *file_name;
2582  int fd;
2583  unformat_input_t sub_input;
2584  clib_error_t *error;
2585 
2586  file_name = 0;
2587  fd = -1;
2588  error = 0;
2589 
2590  if (!unformat (input, "%s", &file_name))
2591  {
2592  error = clib_error_return (0, "expecting file name, got `%U'",
2593  format_unformat_error, input);
2594  goto done;
2595  }
2596 
2597  fd = open (file_name, O_RDONLY);
2598  if (fd < 0)
2599  {
2600  error = clib_error_return_unix (0, "failed to open `%s'", file_name);
2601  goto done;
2602  }
2603 
2604  /* Make sure its a regular file. */
2605  {
2606  struct stat s;
2607 
2608  if (fstat (fd, &s) < 0)
2609  {
2610  error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
2611  goto done;
2612  }
2613 
2614  if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
2615  {
2616  error = clib_error_return (0, "not a regular file `%s'", file_name);
2617  goto done;
2618  }
2619  }
2620 
2621  unformat_init_unix_file (&sub_input, fd);
2622 
2623  vlib_cli_input (vm, &sub_input, 0, 0);
2624  unformat_free (&sub_input);
2625 
2626 done:
2627  if (fd > 0)
2628  close (fd);
2629  vec_free (file_name);
2630 
2631  return error;
2632 }
2633 
2634 /*?
2635  * Executes a sequence of CLI commands which are read from a file.
2636  *
2637  * If a command is unrecognised or otherwise invalid then the usual CLI
2638  * feedback will be generated, however execution of subsequent commands
2639  * from the file will continue.
2640 ?*/
2641 /* *INDENT-OFF* */
2642 VLIB_CLI_COMMAND (cli_exec, static) = {
2643  .path = "exec",
2644  .short_help = "Execute commands from file",
2645  .function = unix_cli_exec,
2646  .is_mp_safe = 1,
2647 };
2648 /* *INDENT-ON* */
2649 
2650 /** CLI command to show various unix error statistics. */
2651 static clib_error_t *
2653  unformat_input_t * input, vlib_cli_command_t * cmd)
2654 {
2655  unix_main_t *um = &unix_main;
2656  clib_error_t *error = 0;
2657  int i, n_errors_to_show;
2658  unix_error_history_t *unix_errors = 0;
2659 
2660  n_errors_to_show = 1 << 30;
2661 
2663  {
2664  if (!unformat (input, "%d", &n_errors_to_show))
2665  {
2666  error =
2667  clib_error_return (0,
2668  "expecting integer number of errors to show, got `%U'",
2669  format_unformat_error, input);
2670  goto done;
2671  }
2672  }
2673 
2674  n_errors_to_show =
2675  clib_min (ARRAY_LEN (um->error_history), n_errors_to_show);
2676 
2677  i =
2678  um->error_history_index >
2679  0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1;
2680 
2681  while (n_errors_to_show > 0)
2682  {
2683  unix_error_history_t *eh = um->error_history + i;
2684 
2685  if (!eh->error)
2686  break;
2687 
2688  vec_add1 (unix_errors, eh[0]);
2689  n_errors_to_show -= 1;
2690  if (i == 0)
2691  i = ARRAY_LEN (um->error_history) - 1;
2692  else
2693  i--;
2694  }
2695 
2696  if (vec_len (unix_errors) == 0)
2697  vlib_cli_output (vm, "no Unix errors so far");
2698  else
2699  {
2700  vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors);
2701  for (i = vec_len (unix_errors) - 1; i >= 0; i--)
2702  {
2703  unix_error_history_t *eh = vec_elt_at_index (unix_errors, i);
2704  vlib_cli_output (vm, "%U: %U",
2705  format_time_interval, "h:m:s:u", eh->time,
2706  format_clib_error, eh->error);
2707  }
2708  vlib_cli_output (vm, "%U: time now",
2709  format_time_interval, "h:m:s:u", vlib_time_now (vm));
2710  }
2711 
2712 done:
2713  vec_free (unix_errors);
2714  return error;
2715 }
2716 
2717 /* *INDENT-OFF* */
2718 VLIB_CLI_COMMAND (cli_unix_show_errors, static) = {
2719  .path = "show unix-errors",
2720  .short_help = "Show Unix system call error history",
2721  .function = unix_show_errors,
2722 };
2723 /* *INDENT-ON* */
2724 
2725 /** CLI command to show session command history. */
2726 static clib_error_t *
2728  unformat_input_t * input, vlib_cli_command_t * cmd)
2729 {
2731  unix_cli_file_t *cf;
2732  int i, j;
2733 
2735 
2736  if (cf->has_history && cf->history_limit)
2737  {
2738  i = 1 + cf->command_number - vec_len (cf->command_history);
2739  for (j = 0; j < vec_len (cf->command_history); j++)
2740  vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]);
2741  }
2742  else
2743  {
2744  vlib_cli_output (vm, "History not enabled.\n");
2745  }
2746 
2747  return 0;
2748 }
2749 
2750 /*?
2751  * Displays the command history for the current session, if any.
2752 ?*/
2753 /* *INDENT-OFF* */
2754 VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = {
2755  .path = "history",
2756  .short_help = "Show current session command history",
2757  .function = unix_cli_show_history,
2758 };
2759 /* *INDENT-ON* */
2760 
2761 /** CLI command to show terminal status. */
2762 static clib_error_t *
2764  unformat_input_t * input, vlib_cli_command_t * cmd)
2765 {
2766  unix_main_t *um = &unix_main;
2768  unix_cli_file_t *cf;
2769  vlib_node_t *n;
2770 
2772  n = vlib_get_node (vm, cf->process_node_index);
2773 
2774  vlib_cli_output (vm, "Terminal name: %v\n", n->name);
2775  vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ?
2776  "line-by-line" : "char-by-char");
2777  vlib_cli_output (vm, "Terminal width: %d\n", cf->width);
2778  vlib_cli_output (vm, "Terminal height: %d\n", cf->height);
2779  vlib_cli_output (vm, "ANSI capable: %s\n",
2780  cf->ansi_capable ? "yes" : "no");
2781  vlib_cli_output (vm, "History enabled: %s%s\n",
2782  cf->has_history ? "yes" : "no", !cf->has_history
2783  || cf->history_limit ? "" :
2784  " (disabled by history limit)");
2785  if (cf->has_history)
2786  vlib_cli_output (vm, "History limit: %d\n", cf->history_limit);
2787  vlib_cli_output (vm, "Pager enabled: %s%s%s\n",
2788  cf->no_pager ? "no" : "yes",
2789  cf->no_pager
2790  || cf->height ? "" : " (disabled by terminal height)",
2791  cf->no_pager
2792  || um->cli_pager_buffer_limit ? "" :
2793  " (disabled by buffer limit)");
2794  if (!cf->no_pager)
2795  vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit);
2796  vlib_cli_output (vm, "CRLF mode: %s\n",
2797  cf->crlf_mode ? "CR+LF" : "LF");
2798 
2799  return 0;
2800 }
2801 
2802 /*?
2803  * Displays various information about the state of the current terminal
2804  * session.
2805  *
2806  * @cliexpar
2807  * @cliexstart{show terminal}
2808  * Terminal name: unix-cli-stdin
2809  * Terminal mode: char-by-char
2810  * Terminal width: 123
2811  * Terminal height: 48
2812  * ANSI capable: yes
2813  * History enabled: yes
2814  * History limit: 50
2815  * Pager enabled: yes
2816  * Pager limit: 100000
2817  * CRLF mode: LF
2818  * @cliexend
2819 ?*/
2820 /* *INDENT-OFF* */
2821 VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = {
2822  .path = "show terminal",
2823  .short_help = "Show current session terminal settings",
2824  .function = unix_cli_show_terminal,
2825 };
2826 /* *INDENT-ON* */
2827 
2828 /** CLI command to set terminal pager settings. */
2829 static clib_error_t *
2831  unformat_input_t * input,
2832  vlib_cli_command_t * cmd)
2833 {
2834  unix_main_t *um = &unix_main;
2836  unix_cli_file_t *cf;
2837  unformat_input_t _line_input, *line_input = &_line_input;
2838 
2839  if (!unformat_user (input, unformat_line_input, line_input))
2840  return 0;
2841 
2843 
2844  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2845  {
2846  if (unformat (line_input, "on"))
2847  cf->no_pager = 0;
2848  else if (unformat (line_input, "off"))
2849  cf->no_pager = 1;
2850  else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit))
2851  vlib_cli_output (vm,
2852  "Pager limit set to %u lines; note, this is global.\n",
2854  else
2855  return clib_error_return (0, "unknown parameter: `%U`",
2856  format_unformat_error, line_input);
2857  }
2858 
2859  unformat_free (line_input);
2860 
2861  return 0;
2862 }
2863 
2864 /*?
2865  * Enables or disables the terminal pager for this session. Generally
2866  * this defaults to enabled.
2867  *
2868  * Additionally allows the pager buffer size to be set; though note that
2869  * this value is set globally and not per session.
2870 ?*/
2871 /* *INDENT-OFF* */
2872 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = {
2873  .path = "set terminal pager",
2874  .short_help = "set terminal pager [on|off] [limit <lines>]",
2875  .function = unix_cli_set_terminal_pager,
2876 };
2877 /* *INDENT-ON* */
2878 
2879 /** CLI command to set terminal history settings. */
2880 static clib_error_t *
2882  unformat_input_t * input,
2883  vlib_cli_command_t * cmd)
2884 {
2886  unix_cli_file_t *cf;
2887  unformat_input_t _line_input, *line_input = &_line_input;
2888  u32 limit;
2889 
2890  if (!unformat_user (input, unformat_line_input, line_input))
2891  return 0;
2892 
2894 
2895  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
2896  {
2897  if (unformat (line_input, "on"))
2898  cf->has_history = 1;
2899  else if (unformat (line_input, "off"))
2900  cf->has_history = 0;
2901  else if (unformat (line_input, "limit %u", &cf->history_limit))
2902  ;
2903  else
2904  return clib_error_return (0, "unknown parameter: `%U`",
2905  format_unformat_error, line_input);
2906 
2907  /* If we reduced history size, or turned it off, purge the history */
2908  limit = cf->has_history ? cf->history_limit : 0;
2909 
2910  while (cf->command_history && vec_len (cf->command_history) >= limit)
2911  {
2912  vec_free (cf->command_history[0]);
2913  vec_delete (cf->command_history, 1, 0);
2914  }
2915  }
2916 
2917  unformat_free (line_input);
2918 
2919  return 0;
2920 }
2921 
2922 /*?
2923  * Enables or disables the command history function of the current
2924  * terminal. Generally this defaults to enabled.
2925  *
2926  * This command also allows the maximum size of the history buffer for
2927  * this session to be altered.
2928 ?*/
2929 /* *INDENT-OFF* */
2930 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = {
2931  .path = "set terminal history",
2932  .short_help = "set terminal history [on|off] [limit <lines>]",
2933  .function = unix_cli_set_terminal_history,
2934 };
2935 /* *INDENT-ON* */
2936 
2937 /** CLI command to set terminal ANSI settings. */
2938 static clib_error_t *
2940  unformat_input_t * input,
2941  vlib_cli_command_t * cmd)
2942 {
2944  unix_cli_file_t *cf;
2945 
2947 
2948  if (unformat (input, "on"))
2949  cf->ansi_capable = 1;
2950  else if (unformat (input, "off"))
2951  cf->ansi_capable = 0;
2952  else
2953  return clib_error_return (0, "unknown parameter: `%U`",
2954  format_unformat_error, input);
2955 
2956  return 0;
2957 }
2958 
2959 /*?
2960  * Enables or disables the use of ANSI control sequences by this terminal.
2961  * The default will vary based on terminal detection at the start of the
2962  * session.
2963  *
2964  * ANSI control sequences are used in a small number of places to provide,
2965  * for example, color text output and to control the cursor in the pager.
2966 ?*/
2967 /* *INDENT-OFF* */
2968 VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = {
2969  .path = "set terminal ansi",
2970  .short_help = "set terminal ansi [on|off]",
2971  .function = unix_cli_set_terminal_ansi,
2972 };
2973 /* *INDENT-ON* */
2974 
2975 static clib_error_t *
2977 {
2978  return 0;
2979 }
2980 
2982 
2983 /*
2984  * fd.io coding-style-patch-verification: ON
2985  *
2986  * Local Variables:
2987  * eval: (c-set-style "gnu")
2988  * End:
2989  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
unix_file_t * file_pool
Definition: unix.h:89
u32 history_limit
Maximum number of history entries this session will store.
Definition: cli.c:161
u32 command_number
Current command line counter.
Definition: cli.c:164
uword output_function_arg
Definition: node.h:546
u32 error_history_index
Definition: unix.h:99
void unformat_init_vector(unformat_input_t *input, u8 *vector_string)
Definition: unformat.c:1025
#define vec_foreach_index(var, v)
Iterate over vector indices.
u32 unix_file_index
The file index held by unix.c.
Definition: cli.c:142
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
static unix_cli_main_t unix_cli_main
CLI global state.
Definition: cli.c:439
#define clib_min(x, y)
Definition: clib.h:326
u32 pager_start
Line number of top of page.
Definition: cli.c:199
u32 flags
Definition: unix.h:54
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
static void unix_cli_pager_redraw(unix_cli_file_t *cf, unix_file_t *uf)
Redraw the currently displayed page of text.
Definition: cli.c:733
static unix_cli_parse_action_t unix_cli_match_action(unix_cli_parse_actions_t *a, u8 *input, u32 ilen, i32 *matched)
Search for a byte sequence in the action list.
Definition: cli.c:465
a
Definition: bitmap.h:516
u8 * input_vector
Vector of input saved by Unix input node to be processed by CLI process.
Definition: cli.c:149
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:604
static clib_error_t * unix_cli_set_terminal_history(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
CLI command to set terminal history settings.
Definition: cli.c:2881
Erase line to right & including cursor.
Definition: cli.c:260
static uword vlib_current_process(vlib_main_t *vm)
Definition: node_funcs.h:410
u8 * line
The line to print.
Definition: cli.c:99
static void unix_vlib_cli_output(uword cli_file_index, u8 *buffer, uword buffer_bytes)
VLIB CLI output function.
Definition: cli.c:928
u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
u32 flags
Definition: unix.h:83
#define ANSI_RESET
ANSI reset color settings.
Definition: cli.c:70
static void unix_cli_add_pending_output(unix_file_t *uf, unix_cli_file_t *cf, u8 *buffer, uword buffer_bytes)
Add bytes to the output vector and then flagg the I/O system that bytes are available to be sent...
Definition: cli.c:503
Carriage return, newline or enter.
Definition: cli.c:247
#define ANSI_RESTCURSOR
ANSI restore cursor position if previously saved.
Definition: cli.c:86
u32 width
Terminal width.
Definition: cli.c:202
u8 * cli_prompt
Prompt string for CLI.
Definition: cli.c:423
static clib_error_t * unix_cli_set_terminal_pager(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
CLI command to set terminal pager settings.
Definition: cli.c:2830
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
u32 cli_pager_buffer_limit
Definition: unix.h:122
#define NULL
Definition: clib.h:55
u8 ** command_history
Array of vectors of commands in the history.
Definition: cli.c:154
u32 index
Definition: node.h:237
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
static clib_error_t * unix_cli_config(vlib_main_t *vm, unformat_input_t *input)
Handle configuration directives in the unix section.
Definition: cli.c:2392
struct termios tio_stdin
Definition: unix.h:128
Action parser found a partial match.
Definition: cli.c:279
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
Unix CLI session.
Definition: cli.c:139
static void unix_cli_ansi_cursor(unix_cli_file_t *cf, unix_file_t *uf, u16 x, u16 y)
Uses an ANSI escape sequence to move the cursor.
Definition: cli.c:717
unix_cli_pager_index_t * pager_index
Index of line fragments in the pager buffer.
Definition: cli.c:196
Exit the pager session.
Definition: cli.c:268
struct _vlib_node_registration vlib_node_registration_t
u8 * current_command
The command currently pointed at by the history cursor.
Definition: cli.c:156
static clib_error_t * unix_cli_read_ready(unix_file_t *uf)
Called when a CLI session file descriptor has data to be read.
Definition: cli.c:2181
#define VLIB_MAIN_LOOP_EXIT_CLI
Definition: main.h:92
#define vlib_call_config_function(vm, x)
Definition: init.h:195
void clib_longjmp(clib_longjmp_t *save, uword return_value)
add_epi add_epi sub_epi sub_epi adds_epu subs_epu i16x8 y
Definition: vector_sse2.h:299
static void unix_cli_file_free(unix_cli_file_t *f)
Release storage used by a CLI session.
Definition: cli.c:236
u32 cursor
Position of the insert cursor on the current input line.
Definition: cli.c:175
clib_socket_t cli_listen_socket
Definition: unix.h:92
#define ANSI_SCROLLDN
ANSI scroll screen down one line.
Definition: cli.c:82
Clear and redraw the page on the terminal.
Definition: cli.c:276
clib_error_t * clib_socket_init(clib_socket_t *s)
Definition: socket.c:283
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
unix_cli_parse_action_t action
Action to take when matched.
Definition: cli.c:291
int log_fd
Definition: unix.h:110
#define SOCKET_IS_SERVER
Definition: socket.h:58
static uword unix_file_add(unix_main_t *um, unix_file_t *template)
Definition: unix.h:136
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Undo last erase action.
Definition: cli.c:264
u32 vlib_register_node(vlib_main_t *vm, vlib_node_registration_t *r)
Definition: node.c:452
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:559
static int unix_cli_line_edit(unix_cli_main_t *cm, unix_main_t *um, unix_cli_file_t *cf)
Process input bytes on a stream to provide line editing and command history in the CLI...
Definition: cli.c:1906
u8 ansi_capable
Can we do ANSI output?
Definition: cli.c:184
static clib_error_t * unix_cli_set_terminal_ansi(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
CLI command to set terminal ANSI settings.
Definition: cli.c:2939
static clib_error_t * unix_cli_init(vlib_main_t *vm)
Definition: cli.c:2976
u8 * format_timeval(u8 *s, va_list *args)
Definition: unix-formats.c:748
#define CSI
ANSI Control Sequence Introducer.
Definition: cli.c:65
static void unix_cli_cli_prompt(unix_cli_file_t *cf, unix_file_t *uf)
Output the CLI prompt.
Definition: cli.c:647
struct _socket_t clib_socket_t
u8 * output_vector
Vector of output pending write to file descriptor.
Definition: cli.c:145
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:527
static clib_error_t * unix_cli_show_terminal(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
CLI command to show terminal status.
Definition: cli.c:2763
#define always_inline
Definition: clib.h:84
#define vec_new(T, N)
Create new vector of given type and length (unspecified alignment, no header).
Definition: vec.h:270
Scroll to next line.
Definition: cli.c:270
int i32
Definition: types.h:81
u32 len
Length of input without final NUL.
Definition: cli.c:290
Mapping of input buffer strings to action values.
Definition: cli.c:287
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static void unix_cli_pager_prompt(unix_cli_file_t *cf, unix_file_t *uf)
Output a pager prompt and show number of buffered lines.
Definition: cli.c:656
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
u32 process_node_index
Process node identifier.
Definition: cli.c:208
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:201
u32 offset
Offset of the string in the line.
Definition: cli.c:131
Left arrow.
Definition: cli.c:253
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:977
static void unix_cli_del_pending_output(unix_file_t *uf, unix_cli_file_t *cf, uword n_bytes)
Delete all bytes from the output vector and flag the I/O system that no more bytes are available to b...
Definition: cli.c:523
u32 length
Length of the string in the line.
Definition: cli.c:134
vlib_main_t * vlib_main
Definition: unix.h:81
u32 * unused_cli_process_node_indices
Vec pool of unused session indices.
Definition: cli.c:429
#define UNIX_CLI_MAX_DEPTH_TELNET
Maximum depth into a byte stream from which to compile a Telnet protocol message. ...
Definition: cli.c:90
static u32 unix_cli_file_add(unix_cli_main_t *cm, char *name, int fd)
Store a new CLI session.
Definition: cli.c:2224
static clib_error_t * unix_config(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:310
static uword unix_cli_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Handle system events.
Definition: cli.c:2114
static clib_error_t * unix_cli_listen_read_ready(unix_file_t *uf)
Telnet listening socket has a new connection.
Definition: cli.c:2286
unix_cli_file_t * cli_file_pool
Vec pool of CLI sessions.
Definition: cli.c:426
static void unix_file_del(unix_main_t *um, unix_file_t *f)
Definition: unix.h:146
int search_mode
If non-zero then the CLI is searching in the history array.
Definition: cli.c:172
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
static clib_error_t * unix_show_errors(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
CLI command to show various unix error statistics.
Definition: cli.c:2652
static u8 unix_cli_terminal_type(u8 *term, uword len)
Identify whether a terminal type is ANSI capable.
Definition: cli.c:1015
Scroll to last line.
Definition: cli.c:273
int cli_line_mode
Definition: unix.h:113
static void unix_vlib_cli_output_cooked(unix_cli_file_t *cf, unix_file_t *uf, u8 *buffer, uword buffer_bytes)
Process a buffer for CRLF handling before outputting it to the CLI.
Definition: cli.c:610
u32 file_descriptor
Definition: unix.h:52
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:931
uword private_data
Definition: unix.h:59
static void unix_cli_pager_add_line(unix_cli_file_t *cf, u8 *line, word len_or_index)
Process and add a line to the pager index.
Definition: cli.c:794
void vlib_unix_cli_set_prompt(char *prompt)
Set the CLI prompt.
Definition: cli.c:2536
Jump cursor to start of right word.
Definition: cli.c:258
static void unix_cli_pager_prompt_erase(unix_cli_file_t *cf, unix_file_t *uf)
Erase the printed pager prompt.
Definition: cli.c:695
static unix_cli_parse_actions_t unix_cli_parse_strings[]
Patterns to match on a CLI input stream.
Definition: cli.c:310
Erase cursor left.
Definition: cli.c:249
#define clib_error_return_unix(e, args...)
Definition: error.h:114
int cli_no_banner
Definition: unix.h:119
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
static void unix_cli_pager_reindex(unix_cli_file_t *cf)
Reindex entire pager buffer.
Definition: cli.c:855
Scroll to previous page.
Definition: cli.c:275
void timer_call(timer_func_t *func, any arg, f64 dt)
Definition: timer.c:158
#define VLIB_CONFIG_FUNCTION(x, n,...)
Definition: init.h:118
static clib_error_t * unix_cli_exec(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
CLI command to execute a VPP command script.
Definition: cli.c:2578
static void clib_socket_free(clib_socket_t *s)
Definition: socket.h:133
u8 * input
Input string to match.
Definition: cli.c:289
static word unix_vlib_findchr(u8 chr, u8 *str, word len)
A bit like strchr with a buffer length limit.
Definition: cli.c:553
static unix_cli_banner_t unix_cli_banner[]
Plain welcome banner.
Definition: cli.c:105
Down arrow.
Definition: cli.c:252
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:575
Scroll to next page.
Definition: cli.c:274
word any
Definition: types.h:139
u32 cli_history_limit
Definition: unix.h:116
End key (jump to end of line)
Definition: cli.c:256
#define ANSI_BRED
ANSI Start bright red text.
Definition: cli.c:78
u8 * name
Definition: node.h:221
u8 * format_sockaddr(u8 *s, va_list *args)
Definition: unix-formats.c:229
unix_cli_process_event_type_t
CLI session events.
Definition: cli.c:413
static i32 unix_cli_process_telnet(unix_main_t *um, unix_cli_file_t *cf, unix_file_t *uf, u8 *input_vector, uword len)
A mostly no-op Telnet state machine.
Definition: cli.c:1102
u8 ** pager_vector
Pager buffer.
Definition: cli.c:193
unix_error_history_t error_history[128]
Definition: unix.h:98
vlib_parse_match_t vlib_parse_eval(u8 *input)
Definition: parse.c:407
#define ANSI_SAVECURSOR
ANSI save cursor position.
Definition: cli.c:84
svmdb_client_t * c
u32 runtime_index
Definition: node.h:240
static void unix_vlib_cli_output_raw(unix_cli_file_t *cf, unix_file_t *uf, u8 *buffer, uword buffer_bytes)
Send a buffer to the CLI stream if possible, enqueue it otherwise.
Definition: cli.c:580
u32 current_input_file_index
File pool index of current input.
Definition: cli.c:435
#define ANSI_CLEARLINE
ANSI clear line cursor is on.
Definition: cli.c:80
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
Jump cursor to start of left word.
Definition: cli.c:257
int cli_no_pager
Definition: unix.h:125
#define ANSI_BOLD
ANSI Start bold text.
Definition: cli.c:72
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:115
Search backwards in command history.
Definition: cli.c:262
Scroll to previous line.
Definition: cli.c:271
#define clib_memcpy(a, b, c)
Definition: string.h:64
clib_error_t * clib_socket_accept(clib_socket_t *server, clib_socket_t *client)
Definition: socket.c:379
#define clib_unix_warning(format, args...)
Definition: error.h:68
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:211
u32 line
Index into pager_vector.
Definition: cli.c:128
Search the pager buffer.
Definition: cli.c:277
#define ARRAY_LEN(x)
Definition: clib.h:59
void unformat_init_unix_file(unformat_input_t *input, int file_descriptor)
Definition: unformat.c:1052
#define ANSI_CLEAR
ANSI clear screen.
Definition: cli.c:68
static void unix_cli_file_welcome(unix_cli_main_t *cm, unix_cli_file_t *cf)
Emit initial welcome banner and prompt on a connection.
Definition: cli.c:1034
static void unix_cli_resize_interrupt(int signum)
The system terminal has informed us that the window size has changed.
Definition: cli.c:2362
#define CTL(c)
Given a capital ASCII letter character return a NUL terminated string with the control code for that ...
Definition: cli.c:303
u8 no_pager
Disable the pager?
Definition: cli.c:190
int tio_isset
Definition: unix.h:129
static unix_cli_banner_t unix_cli_banner_color[]
ANSI color welcome banner.
Definition: cli.c:114
void vlib_start_process(vlib_main_t *vm, uword process_index)
Definition: main.c:1298
Search forwards in command history.
Definition: cli.c:263
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
static clib_error_t * unix_cli_quit(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
CLI command to quit the terminal session.
Definition: cli.c:2550
unix_cli_parse_action_t
CLI actions.
Definition: cli.c:244
Home key (jump to start of line)
Definition: cli.c:255
static void unix_cli_process_input(unix_cli_main_t *cm, uword cli_file_index)
Process input to a CLI session.
Definition: cli.c:1983
unsigned int u32
Definition: types.h:88
static void unix_cli_file_welcome_timer(any arg, f64 delay)
A failsafe triggered on a timer to ensure we send the prompt to telnet sessions that fail to negotiat...
Definition: cli.c:1077
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:745
A CLI session wants to close.
Definition: cli.c:416
Clear the terminal.
Definition: cli.c:261
static clib_error_t * unix_cli_exit(vlib_main_t *vm)
Called when VPP is shutting down, this restores the system terminal state if previously saved...
Definition: cli.c:2517
#define vec_append(v1, v2)
Append v2 after v1.
Definition: vec.h:779
static void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Set node dispatch state.
Definition: node_funcs.h:146
u8 has_history
This session has command history.
Definition: cli.c:152
A CLI banner line.
Definition: cli.c:97
unix_main_t unix_main
Definition: main.c:57
#define ESC
ANSI escape code.
Definition: cli.c:62
static void unix_cli_pager_reset(unix_cli_file_t *f)
Resets the pager buffer and other data.
Definition: cli.c:215
Pager line index.
Definition: cli.c:125
u64 uword
Definition: types.h:112
u8 started
Has the session started?
Definition: cli.c:187
void(* file_update)(unix_file_t *file, unix_file_update_type_t update_type)
Definition: unix.h:94
unsigned short u16
Definition: types.h:57
static clib_error_t * unix_cli_write_ready(unix_file_t *uf)
Called when a CLI session file descriptor can be written to without blocking.
Definition: cli.c:2158
#define UNIX_FLAG_INTERACTIVE
Definition: unix.h:85
i64 word
Definition: types.h:111
clib_error_t * error
Definition: unix.h:68
CLI global state.
Definition: cli.c:420
Up arrow.
Definition: cli.c:251
#define UNIX_FILE_DATA_AVAILABLE_TO_WRITE
Definition: unix.h:55
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
Scroll to first line.
Definition: cli.c:272
Action parser did not find any match.
Definition: cli.c:280
unsigned char u8
Definition: types.h:56
Telnet control code.
Definition: cli.c:265
#define UNIX_CLI_STDIN_FD
Unix standard in.
Definition: cli.c:93
u8 * search_key
The string being searched for in the history.
Definition: cli.c:167
static void unix_cli_kill(unix_cli_main_t *cm, uword cli_file_index)
Destroy a CLI session.
Definition: cli.c:2084
Definition: unix.h:49
Erase cursor right.
Definition: cli.c:250
u8 * format_time_interval(u8 *s, va_list *args)
Definition: std-formats.c:122
static void unix_cli_pager_message(unix_cli_file_t *cf, unix_file_t *uf, char *message, char *postfix)
Output a pager "skipping" message.
Definition: cli.c:679
struct clib_bihash_value offset
template key/value backing page structure
A file descriptor has data to be read.
Definition: cli.c:415
static int unix_cli_line_process_one(unix_cli_main_t *cm, unix_main_t *um, unix_cli_file_t *cf, unix_file_t *uf, u8 input, unix_cli_parse_action_t action)
Process actionable input.
Definition: cli.c:1226
static clib_error_t * unix_cli_show_history(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
CLI command to show session command history.
Definition: cli.c:2727
static vlib_process_t * vlib_get_process_from_node(vlib_main_t *vm, vlib_node_t *node)
Definition: node_funcs.h:190
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
Enter pressed (CR, CRLF, LF, etc)
Definition: cli.c:267
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:58
#define clib_panic(format, args...)
Definition: error.h:72
#define vec_foreach(var, vec)
Vector iterator.
u8 crlf_mode
Set if the CRLF mode wants CR + LF.
Definition: cli.c:181
#define clib_error_return(e, args...)
Definition: error.h:111
clib_longjmp_t main_loop_exit
Definition: main.h:88
struct _unformat_input_t unformat_input_t
vlib_cli_output_function_t * output_function
Definition: node.h:545
u32 flags
Definition: vhost-user.h:75
i32 excursion
How far from the end of the history array the user has browsed.
Definition: cli.c:158
Scroll to next page.
Definition: cli.c:269
unformat_function_t unformat_line_input
Definition: format.h:281
u8 line_mode
Line mode or char mode.
Definition: cli.c:178
u32 length
The length of the line without terminating NUL.
Definition: cli.c:100
void vlib_cli_input(vlib_main_t *vm, unformat_input_t *input, vlib_cli_output_function_t *function, uword function_arg)
Definition: cli.c:538
u32 stdin_cli_file_index
The session index of the stdin cli.
Definition: cli.c:432
u64 n_total_errors
Definition: unix.h:100
Erase line to left of cursor.
Definition: cli.c:259
u32 height
Terminal height.
Definition: cli.c:205
Right arrow.
Definition: cli.c:254