summaryrefslogtreecommitdiff
path: root/lib/command.c
diff options
context:
space:
mode:
authorQuentin Young <qlyoung@cumulusnetworks.com>2018-05-12 20:14:22 -0400
committerQuentin Young <qlyoung@cumulusnetworks.com>2018-06-06 16:15:34 +0000
commitfe6b47b9e90c532ad0c80914f42b1856eebae154 (patch)
tree83095321e768cdf7eaf74d454e9556c2d829299f /lib/command.c
parent01e24c4a69767eb12d3c23c9f8b58063cb04f4e1 (diff)
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 <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'lib/command.c')
-rw-r--r--lib/command.c53
1 files changed, 53 insertions, 0 deletions
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. */