summaryrefslogtreecommitdiffstats
path: root/src/vlib
diff options
context:
space:
mode:
Diffstat (limited to 'src/vlib')
-rw-r--r--src/vlib/unix/cli.c143
1 files changed, 142 insertions, 1 deletions
diff --git a/src/vlib/unix/cli.c b/src/vlib/unix/cli.c
index b553e12bac2..6b02fdf9bf5 100644
--- a/src/vlib/unix/cli.c
+++ b/src/vlib/unix/cli.c
@@ -61,6 +61,7 @@
#include <limits.h>
#include <netinet/tcp.h>
#include <math.h>
+#include <vppinfra/macros.h>
/** ANSI escape code. */
#define ESC "\x1b"
@@ -491,6 +492,10 @@ typedef struct
/** List of new sessions */
unix_cli_new_session_t *new_sessions;
+
+ /* Macro expander */
+ clib_macro_main_t macro_main;
+
} unix_cli_main_t;
/** CLI global state */
@@ -2563,6 +2568,23 @@ more:
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 (&cm->macro_main,
+ (i8 *) cf->current_command,
+ 1 /* complain */ );
+ /* 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);
@@ -2896,6 +2918,8 @@ unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd)
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);
@@ -3906,6 +3930,123 @@ VLIB_CLI_COMMAND (cli_unix_wait_cmd, static) = {
/* *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;
+ unix_cli_main_t *cm = &unix_cli_main;
+ clib_error_t *error;
+
+ if (!unformat (input, "%s", &macro_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 (&cm->macro_main, (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;
+ unix_cli_main_t *cm = &unix_cli_main;
+
+ if (!unformat (input, "%s", &macro_name))
+ return clib_error_return (0, "missing variable name...");
+
+ if (clib_macro_unset (&cm->macro_main, (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)
+{
+ unix_cli_main_t *cm = &unix_cli_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, &cm->macro_main,
+ 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;
@@ -3913,7 +4054,7 @@ unix_cli_init (vlib_main_t * vm)
/* 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;
}