From fe6b47b9e90c532ad0c80914f42b1856eebae154 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sat, 12 May 2018 20:14:22 -0400 Subject: lib: add cli preprocessor for `|` actions This patch adds a CLI preprocessor function that activates when `|` is found in the command. This is the start of adding support for some text processing utilities intended for inline use. The first one implemented here is `| include`, which provides grep-like filtering of command output. Signed-off-by: Quentin Young --- lib/command.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'lib/command.c') diff --git a/lib/command.c b/lib/command.c index 97e60a5929..5b2783d326 100644 --- a/lib/command.c +++ b/lib/command.c @@ -1174,6 +1174,55 @@ DECLARE_KOOH(cmd_execute_done, (struct vty * vty, const char *cmd_exec), DEFINE_KOOH(cmd_execute_done, (struct vty * vty, const char *cmd_exec), (vty, cmd_exec)); +/* + * cmd_execute hook subscriber to handle `|` actions. + */ +static int handle_pipe_action(struct vty *vty, const char *cmd_in, + char **cmd_out) +{ + /* look for `|` */ + char *orig, *working, *token; + char *pipe = strstr(cmd_in, "| "); + + if (!pipe) + return 0; + + /* duplicate string for processing purposes, not including pipe */ + orig = working = XSTRDUP(MTYPE_TMP, pipe + 2); + + /* retrieve action */ + token = strsep(&working, " "); + + /* match result to known actions */ + if (strmatch(token, "include")) { + /* the remaining text should be a regexp */ + char *regexp = working; + bool succ = vty_set_include(vty, regexp); + if (!succ) { + vty_out(vty, "%% Bad regexp '%s'", regexp); + goto fail; + } + cmd_out = XSTRDUP(MTYPE_TMP, cmd_in); + *(strstr(cmd_in, "|")) = '\0'; + } else { + vty_out(vty, "%% Unknown action '%s'", token); + goto fail; + } + +fail: + XFREE(MTYPE_TMP, orig); + return 0; +} + +static int handle_pipe_action_done(struct vty *vty, const char *cmd_exec) +{ + if (vty->filter) { + vty_set_include(vty, NULL); + vty_out(vty, "\n"); + } + return 0; +} + int cmd_execute(struct vty *vty, const char *cmd, const struct cmd_element **matched, int vtysh) { @@ -2720,6 +2769,10 @@ void cmd_init(int terminal) uname(&names); qobj_init(); + /* register command preprocessors */ + hook_register(cmd_execute, handle_pipe_action); + hook_register(cmd_execute_done, handle_pipe_action_done); + varhandlers = list_new(); /* Allocate initial top vector of commands. */ -- cgit v1.2.3