#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
-#include <vppinfra/timer.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <limits.h>
+#include <netinet/tcp.h>
+#include <math.h>
+#include <vppinfra/macros.h>
/** ANSI escape code. */
#define ESC "\x1b"
/** Maximum depth into a byte stream from which to compile a Telnet
* protocol message. This is a safety measure. */
-#define UNIX_CLI_MAX_DEPTH_TELNET 24
+#define UNIX_CLI_MAX_DEPTH_TELNET 32
/** Maximum terminal width we will accept */
#define UNIX_CLI_MAX_TERMINAL_WIDTH 512
/** Process node identifier */
u32 process_node_index;
+
+ /** The current direction of cursor travel.
+ * This is important since when advancing left-to-right, at the
+ * right hand edge of the console the terminal typically defers
+ * wrapping the cursor to the next line until a character is
+ * actually displayed.
+ * This messes up our heuristic for whether to use ANSI to return
+ * the cursor to the end of the line and instead we have to
+ * nudge the cursor to the next line.
+ * A Value of @c 0 means we're advancing left-to-right; @c 1 means
+ * the opposite.
+ */
+ u8 cursor_direction;
+
+ /** Macro tables for this session */
+ clib_macro_main_t macro_main;
} unix_cli_file_t;
/** Resets the pager buffer and other data.
UNIX_CLI_PARSE_ACTION_WORDRIGHT, /**< Jump cursor to start of right word */
UNIX_CLI_PARSE_ACTION_ERASELINELEFT, /**< Erase line to left of cursor */
UNIX_CLI_PARSE_ACTION_ERASELINERIGHT, /**< Erase line to right & including cursor */
+ UNIX_CLI_PARSE_ACTION_ERASEWORDLEFT, /**< Erase word left */
UNIX_CLI_PARSE_ACTION_CLEAR, /**< Clear the terminal */
UNIX_CLI_PARSE_ACTION_REVSEARCH, /**< Search backwards in command history */
UNIX_CLI_PARSE_ACTION_FWDSEARCH, /**< Search forwards in command history */
_(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT),
_(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT),
_(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT),
+ _(CTL ('W'), UNIX_CLI_PARSE_ACTION_ERASEWORDLEFT),
_(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK),
_(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR),
_(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */
#undef _
-/** CLI session events. */
+/** CLI session telnet negotiation timer events. */
typedef enum
{
- UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */
- UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */
-} unix_cli_process_event_type_t;
+ UNIX_CLI_NEW_SESSION_EVENT_ADD, /**< Add a CLI session to the new session list */
+} unix_cli_timeout_event_type_t;
+
+/** Each new session is stored on a list with a deadline after which
+ * a prompt is issued, in case the session TELNET negotiation fails to
+ * complete. */
+typedef struct
+{
+ uword cf_index; /**< Session index of the new session. */
+ f64 deadline; /**< Deadline after which the new session must have a prompt. */
+} unix_cli_new_session_t;
/** CLI global state. */
typedef struct
/** File pool index of current input. */
u32 current_input_file_index;
+
+ /** New session process node identifier */
+ u32 new_session_process_node_index;
+
+ /** List of new sessions */
+ unix_cli_new_session_t *new_sessions;
+
+ /** system default macro table */
+ clib_macro_main_t macro_main;
+
} unix_cli_main_t;
/** CLI global state */
static unix_cli_main_t unix_cli_main;
+/** Return the macro main / tables we should use for this session
+ */
+static clib_macro_main_t *
+get_macro_main (void)
+{
+ unix_cli_main_t *cm = &unix_cli_main;
+ vlib_main_t *vm = vlib_get_main ();
+ vlib_process_t *cp = vlib_get_current_process (vm);
+ unix_cli_file_t *cf;
+
+ if (pool_is_free_index (cm->cli_file_pool, cp->output_function_arg))
+ return (&cm->macro_main);
+
+ cf = pool_elt_at_index (cm->cli_file_pool, cp->output_function_arg);
+
+ return (&cf->macro_main);
+}
+
/**
* @brief Search for a byte sequence in the action list.
*
* @param str The buffer in which to search for the value.
* @param len The depth into the buffer to search.
*
- * @return The index of the first occurence of \c chr. If \c chr is not
+ * @return The index of the first occurrence of \c chr. If \c chr is not
* found then \c len instead.
*/
always_inline word
start = end;
}
}
+
+ /* Use the last character to determine the last direction of the cursor. */
+ if (buffer_bytes > 0)
+ cf->cursor_direction = (buffer[buffer_bytes - 1] == (u8) '\b');
+}
+
+/** @brief Moves the terminal cursor one character to the left, with
+ * special handling when it reaches the left edge of the terminal window.
+ *
+ * Ordinarily we can simply send a '\b' to move the cursor left, however
+ * most terminals will not reverse-wrap to the end of the previous line
+ * if the cursor is in the left-most column. To counter this we must
+ * check the cursor position + prompt length modulo terminal width and
+ * if available use some other means, such as ANSI terminal escape
+ * sequences, to move the cursor.
+ *
+ * @param cf Unix CLI session of the desired stream to write to.
+ * @param uf The Unix file structure of the desired stream to write to.
+ */
+static void
+unix_vlib_cli_output_cursor_left (unix_cli_file_t * cf, clib_file_t * uf)
+{
+ unix_cli_main_t *cm = &unix_cli_main;
+ static u8 *ansi = 0; /* assumes no reentry */
+ u32 position;
+
+ if (!cf->is_interactive || !cf->ansi_capable || !cf->width)
+ {
+ /* No special handling for dumb terminals */
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
+ return;
+ }
+
+ position = ((u32) vec_len (cm->cli_prompt) + cf->cursor) % cf->width;
+
+ if (position != 0)
+ {
+ /* No special handling required if we're not at the left edge */
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
+ return;
+ }
+
+ if (!cf->cursor_direction)
+ {
+ /* Special handling for when we are at the left edge but
+ * the cursor was going left-to-right, but in this situation
+ * xterm-like terminals actually hide the cursor off the right
+ * edge. A \b here seems to jump one char too many, so let's
+ * force the cursor onto the next line instead.
+ */
+ if (cf->cursor < vec_len (cf->current_command))
+ unix_vlib_cli_output_cooked (cf, uf, &cf->current_command[cf->cursor],
+ 1);
+ else
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1);
+ }
+
+ /* Relocate the cursor at the right hand edge one line above */
+ ansi = format (ansi, CSI "A" CSI "%dC", cf->width - 1);
+ unix_vlib_cli_output_cooked (cf, uf, ansi, vec_len (ansi));
+ vec_reset_length (ansi); /* keep the vec around for next time */
+ cf->cursor_direction = 1; /* going backwards now */
}
/** @brief Output the CLI prompt */
*
* If instead @c line is @c NULL then @c len_or_index is taken to mean the
* index of an existing line in the pager buffer; this simply means that the
- * input line does not need to be cloned since we alreayd have it. This is
+ * input line does not need to be cloned since we already have it. This is
* typical if we are reindexing the pager buffer.
*
* @param cf The CLI session whose pager we are adding to.
*/
unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1);
- if (!um->cli_no_banner)
+ if (!um->cli_no_banner && (um->flags & UNIX_FLAG_NOBANNER) == 0)
{
- if (cf->ansi_capable)
+ if (cf->ansi_capable && (um->flags & UNIX_FLAG_NOCOLOR) == 0)
{
banner = unix_cli_banner_color;
len = ARRAY_LEN (unix_cli_banner_color);
}
-/** @brief A failsafe triggered on a timer to ensure we send the prompt
- * to telnet sessions that fail to negotiate the terminal type. */
-static void
-unix_cli_file_welcome_timer (any arg, f64 delay)
+/**
+ * @brief A failsafe manager that ensures CLI sessions issue an initial
+ * prompt if TELNET negotiation fails.
+ */
+static uword
+unix_cli_new_session_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
+ vlib_frame_t * f)
{
unix_cli_main_t *cm = &unix_cli_main;
- unix_cli_file_t *cf;
- (void) delay;
+ uword event_type, *event_data = 0;
+ f64 wait = 10.0;
- /* Check the connection didn't close already */
- if (pool_is_free_index (cm->cli_file_pool, (uword) arg))
- return;
+ while (1)
+ {
+ if (vec_len (cm->new_sessions) > 0)
+ wait = vlib_process_wait_for_event_or_clock (vm, wait);
+ else
+ vlib_process_wait_for_event (vm);
+
+ event_type = vlib_process_get_events (vm, &event_data);
+
+ switch (event_type)
+ {
+ case ~0: /* no events => timeout */
+ break;
+
+ case UNIX_CLI_NEW_SESSION_EVENT_ADD:
+ {
+ /* Add an identifier to the new session list */
+ unix_cli_new_session_t ns;
+
+ ns.cf_index = event_data[0];
+ ns.deadline = vlib_time_now (vm) + 1.0;
- cf = pool_elt_at_index (cm->cli_file_pool, (uword) arg);
+ vec_add1 (cm->new_sessions, ns);
- if (!cf->started)
- unix_cli_file_welcome (cm, cf);
+ if (wait > 0.1)
+ wait = 0.1; /* force a re-evaluation soon, but not too soon */
+ }
+ break;
+
+ default:
+ clib_warning ("BUG: unknown event type 0x%wx", event_type);
+ break;
+ }
+
+ vec_reset_length (event_data);
+
+ if (vlib_process_suspend_time_is_zero (wait))
+ {
+ /* Scan the list looking for expired deadlines.
+ * Emit needed prompts and remove from the list.
+ * While scanning, look for the nearest new deadline
+ * for the next iteration.
+ * Since the list is ordered with newest sessions first
+ * we can make assumptions about expired sessions being
+ * contiguous at the beginning and the next deadline is the
+ * next entry on the list, if any.
+ */
+ f64 now = vlib_time_now (vm);
+ unix_cli_new_session_t *nsp;
+ word index = 0;
+
+ wait = INFINITY;
+
+ vec_foreach (nsp, cm->new_sessions)
+ {
+ if (vlib_process_suspend_time_is_zero (nsp->deadline - now))
+ {
+ /* Deadline reached */
+ unix_cli_file_t *cf;
+
+ /* Mark the highwater */
+ index++;
+
+ /* Check the connection didn't close already */
+ if (pool_is_free_index (cm->cli_file_pool, nsp->cf_index))
+ continue;
+
+ cf = pool_elt_at_index (cm->cli_file_pool, nsp->cf_index);
+
+ if (!cf->started)
+ unix_cli_file_welcome (cm, cf);
+ }
+ else
+ {
+ wait = nsp->deadline - now;
+ break;
+ }
+ }
+
+ if (index)
+ {
+ /* We have sessions to remove */
+ vec_delete (cm->new_sessions, index, 0);
+ }
+ }
+ }
+
+ return 0;
}
+
/** @brief A mostly no-op Telnet state machine.
* Process Telnet command bytes in a way that ensures we're mostly
* transparent to the Telnet protocol. That is, it's mostly a no-op.
case DO:
case DONT:
/* Expect 3 bytes */
- if (vec_len (input_vector) < 3)
+ if (len < 3)
return -1; /* want more bytes */
consume = 2;
if (cf->search_mode == 0)
{
/* Erase the current command (if any) */
- for (j = 0; j < (vec_len (cf->current_command)); j++)
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
+ for (; cf->cursor > 0; cf->cursor--)
+ {
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ }
vec_reset_length (cf->search_key);
vec_reset_length (cf->current_command);
+
if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH)
cf->search_mode = -1;
else
cf->search_mode = 1;
- cf->cursor = 0;
}
else
{
case UNIX_CLI_PARSE_ACTION_ERASELINELEFT:
/* Erase the command from the cursor to the start */
- /* Shimmy forwards to the new end of line position */
+ j = cf->cursor;
+ /* Shimmy backwards to the new end of line position */
delta = vec_len (cf->current_command) - cf->cursor;
- for (j = cf->cursor; j > delta; j--)
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
+ for (; cf->cursor > delta; cf->cursor--)
+ unix_vlib_cli_output_cursor_left (cf, uf);
/* Zap from here to the end of what is currently displayed */
- for (; j < (vec_len (cf->current_command)); j++)
+ for (; cf->cursor < vec_len (cf->current_command); cf->cursor++)
unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
/* Get back to the start of the line */
- for (j = 0; j < (vec_len (cf->current_command)); j++)
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
+ for (; cf->cursor > 0; cf->cursor--)
+ unix_vlib_cli_output_cursor_left (cf, uf);
- j = vec_len (cf->current_command) - cf->cursor;
- memmove (cf->current_command, cf->current_command + cf->cursor, j);
- _vec_len (cf->current_command) = j;
+ /* Delete the desired text from the command */
+ memmove (cf->current_command, cf->current_command + j, delta);
+ _vec_len (cf->current_command) = delta;
/* Print the new contents */
- unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j);
+ unix_vlib_cli_output_cooked (cf, uf, cf->current_command, delta);
+ cf->cursor = delta; /* for backspace tracking */
+
/* Shimmy back to the start */
- for (j = 0; j < (vec_len (cf->current_command)); j++)
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
- cf->cursor = 0;
+ for (; cf->cursor > 0; cf->cursor--)
+ unix_vlib_cli_output_cursor_left (cf, uf);
cf->search_mode = 0;
break;
case UNIX_CLI_PARSE_ACTION_ERASELINERIGHT:
/* Erase the command from the cursor to the end */
+ j = cf->cursor;
/* Zap from cursor to end of what is currently displayed */
- for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
+ for (; cf->cursor < (vec_len (cf->current_command)); cf->cursor++)
unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
/* Get back to where we were */
- for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
+ for (; cf->cursor > j; cf->cursor--)
+ unix_vlib_cli_output_cursor_left (cf, uf);
/* Truncate the line at the cursor */
_vec_len (cf->current_command) = cf->cursor;
cf->search_mode = 0;
break;
+ case UNIX_CLI_PARSE_ACTION_ERASEWORDLEFT:
+ /* calculate num of caracter to be erased */
+ delta = 0;
+ while (cf->cursor > delta
+ && cf->current_command[cf->cursor - delta - 1] == ' ')
+ delta++;
+ while (cf->cursor > delta
+ && cf->current_command[cf->cursor - delta - 1] != ' ')
+ delta++;
+
+ if (vec_len (cf->current_command))
+ {
+ if (cf->cursor > 0)
+ {
+ /* move cursor left delta times */
+ for (j = delta; j > 0; j--, cf->cursor--)
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ save = cf->current_command + cf->cursor;
+
+ /* redraw remainder of line */
+ memmove (cf->current_command + cf->cursor,
+ cf->current_command + cf->cursor + delta,
+ _vec_len (cf->current_command) - cf->cursor - delta);
+ unix_vlib_cli_output_cooked (cf, uf,
+ cf->current_command + cf->cursor,
+ _vec_len (cf->current_command) -
+ cf->cursor);
+ cf->cursor += _vec_len (cf->current_command) - cf->cursor;
+
+ /* print delta amount of blank spaces,
+ * then finally fix the cursor position */
+ for (j = delta; j > 0; j--, cf->cursor--)
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ for (j = delta; j > 0; j--, cf->cursor++)
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
+ for (; (cf->current_command + cf->cursor) > save; cf->cursor--)
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ _vec_len (cf->current_command) -= delta;
+ }
+ }
+ cf->search_mode = 0;
+ cf->excursion = 0;
+ vec_reset_length (cf->search_key);
+ break;
+
case UNIX_CLI_PARSE_ACTION_LEFT:
if (cf->cursor > 0)
{
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
+ unix_vlib_cli_output_cursor_left (cf, uf);
cf->cursor--;
}
break;
cf->search_mode = 0;
/* Erase the command */
- for (j = cf->cursor; j < (vec_len (cf->current_command)); j++)
+ for (; cf->cursor < vec_len (cf->current_command); cf->cursor++)
unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
- for (j = 0; j < (vec_len (cf->current_command)); j++)
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
+ for (; cf->cursor > 0; cf->cursor--)
+ {
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ }
vec_reset_length (cf->current_command);
if (vec_len (cf->command_history))
{
case UNIX_CLI_PARSE_ACTION_HOME:
if (vec_len (cf->current_command) && cf->cursor > 0)
{
- while (cf->cursor)
- {
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
- cf->cursor--;
- }
+ for (; cf->cursor > 0; cf->cursor--)
+ unix_vlib_cli_output_cursor_left (cf, uf);
}
cf->search_mode = 0;
case UNIX_CLI_PARSE_ACTION_WORDLEFT:
if (vec_len (cf->current_command) && cf->cursor > 0)
{
- j = cf->cursor;
-
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
- j--;
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ cf->cursor--;
- while (j && isspace (cf->current_command[j]))
+ while (cf->cursor && isspace (cf->current_command[cf->cursor]))
{
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
- j--;
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ cf->cursor--;
}
- while (j && !isspace (cf->current_command[j]))
+ while (cf->cursor && !isspace (cf->current_command[cf->cursor]))
{
- if (isspace (cf->current_command[j - 1]))
+ if (isspace (cf->current_command[cf->cursor - 1]))
break;
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
- j--;
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ cf->cursor--;
}
- cf->cursor = j;
}
cf->search_mode = 0;
{
if (cf->cursor == vec_len (cf->current_command))
{
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
- _vec_len (cf->current_command)--;
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ cf->cursor--;
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
+ cf->cursor++;
+ unix_vlib_cli_output_cursor_left (cf, uf);
cf->cursor--;
+ _vec_len (cf->current_command)--;
}
else if (cf->cursor > 0)
{
memmove (cf->current_command + cf->cursor - 1,
cf->current_command + cf->cursor, j);
_vec_len (cf->current_command)--;
- cf->cursor--;
+
/* redraw the rest of the line */
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ cf->cursor--;
unix_vlib_cli_output_cooked (cf, uf,
cf->current_command + cf->cursor,
j);
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3);
+ cf->cursor += j;
+ /* erase last char */
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
+ cf->cursor++;
+
/* and shift the terminal cursor back where it should be */
+ j += 2; /* account for old string length and offset position */
while (--j)
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
+ {
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ cf->cursor--;
+ }
}
}
cf->search_mode = 0;
unix_vlib_cli_output_cooked (cf, uf,
cf->current_command + cf->cursor,
j);
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2);
+ cf->cursor += j;
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
+ cf->cursor++;
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ cf->cursor--;
/* and shift the terminal cursor back where it should be */
if (j)
{
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ cf->cursor--;
while (--j)
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
+ {
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ cf->cursor--;
+ }
}
}
}
unix_vlib_cli_output_raw (cf, uf,
cm->cli_prompt, vec_len (cm->cli_prompt));
- unix_vlib_cli_output_raw (cf, uf,
- cf->current_command,
- vec_len (cf->current_command));
- for (j = cf->cursor; j < vec_len (cf->current_command); j++)
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1);
+ unix_vlib_cli_output_cooked (cf, uf,
+ cf->current_command,
+ vec_len (cf->current_command));
+ j = cf->cursor;
+ cf->cursor = vec_len (cf->current_command);
+ for (; cf->cursor > j; cf->cursor--)
+ unix_vlib_cli_output_cursor_left (cf, uf);
break;
vlib_cli_get_possible_completions (cf->current_command);
if (vec_len (possible_commands) == 1)
{
- u32 j = cf->cursor;
u8 *completed = possible_commands[0];
+ j = cf->cursor;
/* find the last word of current_command */
while (j >= 1 && !isspace (cf->current_command[j - 1]))
{
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ cf->cursor--;
j--;
- unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
}
_vec_len (cf->current_command) = j;
vec_append (cf->current_command, completed);
/* echo to the terminal */
- unix_vlib_cli_output_raw (cf, uf, completed, vec_len (completed));
+ unix_vlib_cli_output_cooked (cf, uf, completed,
+ vec_len (completed));
/* add one trailing space if needed */
if (vec_len (save) == 0)
{
vec_add1 (cf->current_command, ' ');
- unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
}
cf->cursor = vec_len (cf->current_command);
{
u8 **possible_command;
uword max_command_len = 0, min_command_len = ~0;
- u32 i, j;
+ u32 i;
vec_foreach (possible_command, possible_commands)
{
if (vec_len (*possible_command) > max_command_len)
- {
- max_command_len = vec_len (*possible_command);
- }
+ max_command_len = vec_len (*possible_command);
if (vec_len (*possible_command) < min_command_len)
- {
- min_command_len = vec_len (*possible_command);
- }
+ min_command_len = vec_len (*possible_command);
}
unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1);
i = 0;
}
- unix_vlib_cli_output_raw (cf, uf, *possible_command,
- vec_len (*possible_command));
+ unix_vlib_cli_output_cooked (cf, uf, *possible_command,
+ vec_len (*possible_command));
for (j = vec_len (*possible_command); j < max_command_len + 2;
j++)
{
- unix_vlib_cli_output_raw (cf, uf, (u8 *) " ", 1);
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
}
i += max_command_len + 2;
}
/* rewrite prompt */
unix_cli_cli_prompt (cf, uf);
- unix_vlib_cli_output_raw (cf, uf, cf->current_command,
- vec_len (cf->current_command));
+ unix_vlib_cli_output_cooked (cf, uf, cf->current_command,
+ vec_len (cf->current_command));
/* count length of last word */
j = cf->cursor;
{
u8 common = '\0';
int stop = 0;
+
vec_foreach (possible_command, possible_commands)
{
if (common == '\0')
break;
}
}
+
if (!stop)
{
vec_add1 (cf->current_command, common);
cf->cursor++;
- unix_vlib_cli_output_raw (cf, uf, (u8 *) & common, 1);
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) & common, 1);
}
else
{
if (vec_len (save) > 0)
{
/* restore remaining input if tab was hit in the middle of a line */
- unix_vlib_cli_output_raw (cf, uf, save, vec_len (save));
- for (j = 0; j < vec_len (save); j++)
- {
- unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
- }
+ unix_vlib_cli_output_cooked (cf, uf, save, vec_len (save));
+ cf->cursor += vec_len (save);
+ for (j = 0; j < vec_len (save); j++, cf->cursor--)
+ unix_vlib_cli_output_cursor_left (cf, uf);
vec_append (cf->current_command, save);
vec_free (save);
}
case UNIX_CLI_PARSE_ACTION_PAGER_NEXT:
case UNIX_CLI_PARSE_ACTION_PAGER_PGDN:
/* show next page of the buffer */
- if (cf->height + cf->pager_start < vec_len (cf->pager_index))
+ if (cf->height + cf->pager_start <= vec_len (cf->pager_index))
{
u8 *line = NULL;
unix_cli_pager_index_t *pi = NULL;
case UNIX_CLI_PARSE_ACTION_PAGER_DN:
case UNIX_CLI_PARSE_ACTION_PAGER_CRLF:
/* display the next line of the buffer */
- if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1))
+ if (cf->height + cf->pager_start <= vec_len (cf->pager_index))
{
u8 *line;
unix_cli_pager_index_t *pi;
{
/* no-op for now */
}
- else if (cf->has_history && cf->search_mode && isprint (input))
+ else if (cf->has_history && cf->search_mode != 0 && isprint (input))
{
int k, limit, offset;
u8 *item;
goto next;
found_at_offset:
- for (j = 0; j < vec_len (cf->current_command); j++)
- unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3);
+ for (; cf->cursor > 0; cf->cursor--)
+ {
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1);
+ unix_vlib_cli_output_cursor_left (cf, uf);
+ }
vec_validate (cf->current_command, vec_len (item) - 1);
clib_memcpy (cf->current_command, item, vec_len (item));
cf->cursor++;
/* Echo the character back to the client */
- unix_vlib_cli_output_raw (cf, uf, &input, 1);
+ unix_vlib_cli_output_cooked (cf, uf, &input, 1);
}
else
{
cf->current_command[cf->cursor] = input;
/* Redraw the line */
j++;
- unix_vlib_cli_output_raw (cf, uf,
- cf->current_command + cf->cursor, j);
+ unix_vlib_cli_output_cooked (cf, uf,
+ cf->current_command + cf->cursor,
+ j);
+ cf->cursor += j;
+ j--;
/* Put terminal cursor back */
- while (--j)
- unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1);
- cf->cursor++;
+ for (; j > 0; j--, cf->cursor--)
+ unix_vlib_cli_output_cursor_left (cf, uf);
}
}
else
if (cf->line_mode)
{
vec_delete (cf->input_vector, i, 0);
+ vec_free (cf->current_command);
cf->current_command = cf->input_vector;
return 0;
}
format_timeval, 0 /* current bat-time */ ,
0 /* current bat-format */ ,
cli_file_index, cf->current_command);
+ if ((vec_len (cf->current_command) > 0) &&
+ (cf->current_command[vec_len (cf->current_command) - 1] != '\n'))
+ lv = format (lv, "\n");
int rv __attribute__ ((unused)) = write (um->log_fd, lv, vec_len (lv));
}
+ /* Run the command through the macro processor */
+ if (vec_len (cf->current_command))
+ {
+ u8 *expanded;
+ vec_validate (cf->current_command, vec_len (cf->current_command));
+ cf->current_command[vec_len (cf->current_command) - 1] = 0;
+ /* The macro expander expects proper C-strings, not vectors */
+ expanded = (u8 *) clib_macro_eval (&cf->macro_main,
+ (i8 *) cf->current_command,
+ 1 /* complain */ ,
+ 0 /* level */ ,
+ 8 /* max_level */ );
+ /* Macro processor NULL terminates the return */
+ _vec_len (expanded) -= 1;
+ vec_reset_length (cf->current_command);
+ vec_append (cf->current_command, expanded);
+ vec_free (expanded);
+ }
+
/* Build an unformat structure around our command */
unformat_init_vector (&input, cf->current_command);
clib_file_t *uf;
int i;
+ /* Validate cli_file_index */
+ if (pool_is_free_index (cm->cli_file_pool, cli_file_index))
+ return;
+
+ vec_foreach_index (i, cm->new_sessions)
+ {
+ unix_cli_new_session_t *ns = vec_elt_at_index (cm->new_sessions, i);
+
+ if (ns->cf_index == cli_file_index)
+ {
+ vec_del1 (cm->new_sessions, i);
+ break;
+ }
+ }
+
cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index);
uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
vec_free (cf->command_history[i]);
vec_free (cf->command_history);
+ vec_free (cf->input_vector);
clib_file_del (fm, uf);
unix_cli_file_free (cf);
+ clib_macro_free (&cf->macro_main);
pool_put (cm->cli_file_pool, cf);
}
{
unix_main_t *um = &unix_main;
clib_file_main_t *fm = &file_main;
+ vlib_node_main_t *nm = &vlib_get_main ()->node_main;
unix_cli_file_t *cf;
clib_file_t template = { 0 };
vlib_main_t *vm = um->vlib_main;
- vlib_node_t *n;
+ vlib_node_t *n = 0;
u8 *file_desc = 0;
file_desc = format (0, "%s", name);
if (vec_len (cm->unused_cli_process_node_indices) > 0)
{
uword l = vec_len (cm->unused_cli_process_node_indices);
+ int i;
+ vlib_main_t *this_vlib_main;
+ u8 *old_name = 0;
- /* Find node and give it new name. */
- n = vlib_get_node (vm, cm->unused_cli_process_node_indices[l - 1]);
- vec_free (n->name);
- n->name = (u8 *) name;
+ /*
+ * Nodes are bulk-copied, so node name pointers are shared.
+ * Find the cli node in all graph replicas, and give all of them
+ * the same new name.
+ * Then, throw away the old shared name-vector.
+ */
+ for (i = 0; i < vlib_get_n_threads (); i++)
+ {
+ this_vlib_main = vlib_get_main_by_index (i);
+ if (this_vlib_main == 0)
+ continue;
+ n = vlib_get_node (this_vlib_main,
+ cm->unused_cli_process_node_indices[l - 1]);
+ old_name = n->name;
+ n->name = (u8 *) name;
+ }
+ ASSERT (old_name);
+ hash_unset (nm->node_by_name, old_name);
+ hash_set (nm->node_by_name, name, n->index);
+ vec_free (old_name);
vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING);
-
_vec_len (cm->unused_cli_process_node_indices) = l - 1;
}
else
static vlib_node_registration_t r = {
.function = unix_cli_process,
.type = VLIB_NODE_TYPE_PROCESS,
- .process_log2_n_stack_bytes = 16,
+ .process_log2_n_stack_bytes = 18,
};
r.name = name;
+
+ vlib_worker_thread_barrier_sync (vm);
+
vlib_register_node (vm, &r);
vec_free (name);
n = vlib_get_node (vm, r.index);
+ vlib_worker_thread_node_runtime_update ();
+ vlib_worker_thread_barrier_release (vm);
}
pool_get (cm->cli_file_pool, cf);
- memset (cf, 0, sizeof (*cf));
+ clib_memset (cf, 0, sizeof (*cf));
+ clib_macro_init (&cf->macro_main);
template.read_function = unix_cli_read_ready;
template.write_function = unix_cli_write_ready;
cf->clib_file_index = clib_file_add (fm, &template);
cf->output_vector = 0;
cf->input_vector = 0;
+ vec_validate (cf->current_command, 0);
+ _vec_len (cf->current_command) = 0;
vlib_start_process (vm, n->runtime_index);
clib_error_t *error;
unix_cli_file_t *cf;
u32 cf_index;
+ int one;
error = clib_socket_accept (s, &client);
if (error)
return error;
+ /* Disable Nagle, ignore any errors doing so eg on PF_LOCAL socket */
+ one = 1;
+ (void) setsockopt (client.fd, IPPROTO_TCP, TCP_NODELAY,
+ (void *) &one, sizeof (one));
+
client_name = (char *) format (0, "%U%c", format_sockaddr, &client.peer, 0);
cf_index = unix_cli_file_add (cm, client_name, client.fd);
unix_vlib_cli_output_raw (cf, uf, charmode_option,
ARRAY_LEN (charmode_option));
- /* In case the client doesn't negotiate terminal type, use
- * a timer to kick off the initial prompt. */
- timer_call (unix_cli_file_welcome_timer, cf_index, 1);
+ if (cm->new_session_process_node_index == ~0)
+ {
+ /* Create thw new session deadline process */
+ cm->new_session_process_node_index =
+ vlib_process_create (um->vlib_main, "unix-cli-new-session",
+ unix_cli_new_session_process,
+ 16 /* log2_n_stack_bytes */ );
+ }
+
+ /* In case the client doesn't negotiate terminal type, register
+ * our session with a process that will emit the prompt if
+ * a deadline passes */
+ vlib_process_signal_event (um->vlib_main,
+ cm->new_session_process_node_index,
+ UNIX_CLI_NEW_SESSION_EVENT_ADD, cf_index);
+
}
return error;
if (isatty (STDIN_FILENO) && um->cli_line_mode == 0)
{
/* Capture terminal resize events */
- memset (&sa, 0, sizeof (sa));
+ clib_memset (&sa, 0, sizeof (sa));
sa.sa_handler = unix_cli_resize_interrupt;
if (sigaction (SIGWINCH, &sa, 0) < 0)
clib_panic ("sigaction");
/* Retrieve the current terminal size */
- ioctl (STDIN_FILENO, TIOCGWINSZ, &ws);
- cf->width = ws.ws_col;
- cf->height = ws.ws_row;
+ if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0)
+ {
+ cf->width = ws.ws_col;
+ cf->height = ws.ws_row;
+ }
if (cf->width == 0 || cf->height == 0)
{
tio = um->tio_stdin;
/* echo off, canonical mode off, ext'd input processing off */
tio.c_lflag &= ~(ECHO | ICANON | IEXTEN);
+ /* disable XON/XOFF, so ^S invokes the history search */
+ tio.c_iflag &= ~(IXON | IXOFF);
tio.c_cc[VMIN] = 1; /* 1 byte at a time */
tio.c_cc[VTIME] = 0; /* no timer */
+ tio.c_cc[VSTOP] = _POSIX_VDISABLE; /* not ^S */
+ tio.c_cc[VSTART] = _POSIX_VDISABLE; /* not ^Q */
tcsetattr (STDIN_FILENO, TCSAFLUSH, &tio);
/* See if we can do ANSI/VT100 output */
while (i && tmp[--i] != '/')
;
- tmp[i] = 0;
+ tmp[i] = '\0';
if (i)
vlib_unix_recursive_mkdir ((char *) tmp);
cm->cli_prompt = format (0, fmt, prompt);
}
+static unix_cli_file_t *
+unix_cli_file_if_exists (unix_cli_main_t * cm)
+{
+ if (!cm->cli_file_pool)
+ return 0;
+ return pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
+}
+
+static unix_cli_file_t *
+unix_cli_file_if_interactive (unix_cli_main_t * cm)
+{
+ unix_cli_file_t *cf;
+ if ((cf = unix_cli_file_if_exists (cm)) && !cf->is_interactive)
+ return 0;
+ return cf;
+}
+
/** CLI command to quit the terminal session.
* @note If this is a stdin session then this will
* shutdown VPP also.
unformat_input_t * input, vlib_cli_command_t * cmd)
{
unix_cli_main_t *cm = &unix_cli_main;
- unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool,
- cm->current_input_file_index);
+ unix_cli_file_t *cf;
+
+ if (!(cf = unix_cli_file_if_exists (cm)))
+ return clib_error_return (0, "invalid session");
/* Cosmetic: suppress the final prompt from appearing before we die */
cf->is_interactive = 0;
int fd;
unformat_input_t sub_input;
clib_error_t *error;
-
+ unix_cli_main_t *cm = &unix_cli_main;
+ unix_cli_file_t *cf;
+ u8 *file_data = 0;
file_name = 0;
fd = -1;
error = 0;
+ struct stat s;
+
if (!unformat (input, "%s", &file_name))
{
}
/* Make sure its a regular file. */
- {
- struct stat s;
+ if (fstat (fd, &s) < 0)
+ {
+ error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
+ goto done;
+ }
- if (fstat (fd, &s) < 0)
- {
- error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
- goto done;
- }
+ if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
+ {
+ error = clib_error_return (0, "not a regular file `%s'", file_name);
+ goto done;
+ }
- if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
- {
- error = clib_error_return (0, "not a regular file `%s'", file_name);
- goto done;
- }
- }
+ /* Read the file */
+ vec_validate (file_data, s.st_size);
+
+ if (read (fd, file_data, s.st_size) != s.st_size)
+ {
+ error = clib_error_return_unix (0, "Failed to read %d bytes from '%s'",
+ s.st_size, file_name);
+ vec_free (file_data);
+ goto done;
+ }
+
+ /* The macro expander expects a c string... */
+ vec_add1 (file_data, 0);
+
+ unformat_init_vector (&sub_input, file_data);
+
+ /* Run the file contents through the macro processor */
+ if (vec_len (sub_input.buffer) > 1)
+ {
+ u8 *expanded;
+ clib_macro_main_t *mm = 0;
- unformat_init_clib_file (&sub_input, fd);
+ /* Initial config process? Use the global macro table. */
+ if (pool_is_free_index
+ (cm->cli_file_pool, cm->current_input_file_index))
+ mm = &cm->macro_main;
+ else
+ {
+ /* Otherwise, use the per-cli-process macro table */
+ cf = pool_elt_at_index (cm->cli_file_pool,
+ cm->current_input_file_index);
+ mm = &cf->macro_main;
+ }
+
+ expanded = (u8 *) clib_macro_eval (mm,
+ (i8 *) sub_input.buffer,
+ 1 /* complain */ ,
+ 0 /* level */ ,
+ 8 /* max_level */ );
+ /* Macro processor NULL terminates the return */
+ _vec_len (expanded) -= 1;
+ vec_reset_length (sub_input.buffer);
+ vec_append (sub_input.buffer, expanded);
+ vec_free (expanded);
+ }
vlib_cli_input (vm, &sub_input, 0, 0);
unformat_free (&sub_input);
done:
- if (fd > 0)
+ if (fd >= 0)
close (fd);
vec_free (file_name);
"Read", "Write", "Error", "File Name", "Description");
/* *INDENT-OFF* */
- pool_foreach (f, fm->file_pool,(
+ pool_foreach (f, fm->file_pool)
{
int rv;
s = format (s, "/proc/self/fd/%d%c", f->file_descriptor, 0);
f->read_events, f->write_events, f->error_events,
path, f->description);
vec_reset_length (s);
- }));
+ }
/* *INDENT-ON* */
vec_free (s);
unix_cli_file_t *cf;
int i, j;
- cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
-
- if (!cf->is_interactive)
+ if (!(cf = unix_cli_file_if_interactive (cm)))
return clib_error_return (0, "invalid for non-interactive sessions");
if (cf->has_history && cf->history_limit)
unix_cli_file_t *cf;
vlib_node_t *n;
- cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
+ if (!(cf = unix_cli_file_if_exists (cm)))
+ return clib_error_return (0, "invalid session");
+
n = vlib_get_node (vm, cf->process_node_index);
vlib_cli_output (vm, "Terminal name: %v\n", n->name);
unformat_input_t * input,
vlib_cli_command_t * cmd)
{
- //unix_main_t *um = &unix_main;
unix_cli_main_t *cm = &unix_cli_main;
clib_file_main_t *fm = &file_main;
unix_cli_file_t *cf;
#define fl(x, y) ( (x) ? toupper((y)) : tolower((y)) )
/* *INDENT-OFF* */
- pool_foreach (cf, cm->cli_file_pool, ({
+ pool_foreach (cf, cm->cli_file_pool) {
uf = pool_elt_at_index (fm->file_pool, cf->clib_file_index);
n = vlib_get_node (vm, cf->process_node_index);
vlib_cli_output (vm,
fl (cf->line_mode, 'l'),
fl (cf->has_epipe, 'p'),
fl (cf->ansi_capable, 'a'));
- }));
+ }
/* *INDENT-ON* */
#undef fl
unformat_input_t _line_input, *line_input = &_line_input;
clib_error_t *error = 0;
- cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
-
- if (!cf->is_interactive)
+ if (!(cf = unix_cli_file_if_interactive (cm)))
return clib_error_return (0, "invalid for non-interactive sessions");
if (!unformat_user (input, unformat_line_input, line_input))
u32 limit;
clib_error_t *error = 0;
- cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
-
- if (!cf->is_interactive)
+ if (!(cf = unix_cli_file_if_interactive (cm)))
return clib_error_return (0, "invalid for non-interactive sessions");
if (!unformat_user (input, unformat_line_input, line_input))
goto done;
}
- /* If we reduced history size, or turned it off, purge the history */
- limit = cf->has_history ? cf->history_limit : 0;
+ }
- while (cf->command_history && vec_len (cf->command_history) >= limit)
- {
- vec_free (cf->command_history[0]);
- vec_delete (cf->command_history, 1, 0);
- }
+ /* If we reduced history size, or turned it off, purge the history */
+ limit = cf->has_history ? cf->history_limit : 0;
+ if (limit < vec_len (cf->command_history))
+ {
+ u32 i;
+
+ /* How many items to remove from the start of history */
+ limit = vec_len (cf->command_history) - limit;
+
+ for (i = 0; i < limit; i++)
+ vec_free (cf->command_history[i]);
+
+ vec_delete (cf->command_history, limit, 0);
}
done:
unix_cli_main_t *cm = &unix_cli_main;
unix_cli_file_t *cf;
- cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index);
-
- if (!cf->is_interactive)
+ if (!(cf = unix_cli_file_if_interactive (cm)))
return clib_error_return (0, "invalid for non-interactive sessions");
if (unformat (input, "on"))
};
/* *INDENT-ON* */
+
+#define MAX_CLI_WAIT 86400
+/** CLI command to wait <sec> seconds. Useful for exec script. */
+static clib_error_t *
+unix_wait_cmd (vlib_main_t * vm,
+ unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+ unformat_input_t _line_input, *line_input = &_line_input;
+ f64 sec = 1.0;
+
+ if (!unformat_user (input, unformat_line_input, line_input))
+ return 0;
+
+ while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (line_input, "%f", &sec))
+ ;
+ else
+ return clib_error_return (0, "unknown parameter: `%U`",
+ format_unformat_error, input);
+ }
+
+ if (sec <= 0 || sec > MAX_CLI_WAIT || floor (sec * 1000) / 1000 != sec)
+ return clib_error_return (0,
+ "<sec> must be a positive value and less than 86400 (one day) with no more than msec precision.");
+
+ vlib_process_wait_for_event_or_clock (vm, sec);
+ vlib_cli_output (vm, "waited %.3f sec.", sec);
+
+ unformat_free (line_input);
+ return 0;
+}
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (cli_unix_wait_cmd, static) = {
+ .path = "wait",
+ .short_help = "wait <sec>",
+ .function = unix_wait_cmd,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+echo_cmd (vlib_main_t * vm,
+ unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+ unformat_input_t _line_input, *line_input = &_line_input;
+
+ /* Get a line of input. */
+ if (!unformat_user (input, unformat_line_input, line_input))
+ {
+ vlib_cli_output (vm, "");
+ return 0;
+ }
+
+ vlib_cli_output (vm, "%v", line_input->buffer);
+
+ unformat_free (line_input);
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (cli_unix_echo_cmd, static) = {
+ .path = "echo",
+ .short_help = "echo <rest-of-line>",
+ .function = echo_cmd,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+define_cmd_fn (vlib_main_t * vm,
+ unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+ u8 *macro_name;
+ unformat_input_t _line_input, *line_input = &_line_input;
+ clib_macro_main_t *mm = get_macro_main ();
+ clib_error_t *error;
+
+ if (!unformat (input, "%s", ¯o_name))
+ return clib_error_return (0, "missing variable name...");
+
+ /* Remove white space */
+ (void) unformat (input, "");
+
+ /* Get a line of input. */
+ if (!unformat_user (input, unformat_line_input, line_input))
+ {
+ error = clib_error_return (0, "missing value for '%s'...", macro_name);
+ vec_free (macro_name);
+ return error;
+ }
+ /* the macro expander expects c-strings, not vectors... */
+ vec_add1 (line_input->buffer, 0);
+ clib_macro_set_value (mm, (char *) macro_name, (char *) line_input->buffer);
+ vec_free (macro_name);
+ unformat_free (line_input);
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (define_cmd, static) = {
+ .path = "define",
+ .short_help = "define <variable-name> <value>",
+ .function = define_cmd_fn,
+};
+
+/* *INDENT-ON* */
+
+static clib_error_t *
+undefine_cmd_fn (vlib_main_t * vm,
+ unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+ u8 *macro_name;
+ clib_macro_main_t *mm = get_macro_main ();
+
+ if (!unformat (input, "%s", ¯o_name))
+ return clib_error_return (0, "missing variable name...");
+
+ if (clib_macro_unset (mm, (char *) macro_name))
+ vlib_cli_output (vm, "%s wasn't set...", macro_name);
+
+ vec_free (macro_name);
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (undefine_cmd, static) = {
+ .path = "undefine",
+ .short_help = "undefine <variable-name>",
+ .function = undefine_cmd_fn,
+};
+/* *INDENT-ON* */
+
+static clib_error_t *
+show_macro_cmd_fn (vlib_main_t * vm,
+ unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+ clib_macro_main_t *mm = get_macro_main ();
+ int evaluate = 1;
+
+ if (unformat (input, "noevaluate %=", &evaluate, 0))
+ ;
+ else if (unformat (input, "noeval %=", &evaluate, 0))
+ ;
+
+ vlib_cli_output (vm, "%U", format_clib_macro_main, mm, evaluate);
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_macro, static) = {
+ .path = "show macro",
+ .short_help = "show macro [noevaluate]",
+ .function = show_macro_cmd_fn,
+};
+/* *INDENT-ON* */
+
static clib_error_t *
unix_cli_init (vlib_main_t * vm)
{
+ unix_cli_main_t *cm = &unix_cli_main;
+
+ /* Breadcrumb to indicate the new session process
+ * has not been started */
+ cm->new_session_process_node_index = ~0;
+ clib_macro_init (&cm->macro_main);
+
return 0;
}