]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib, vtysh: reduce code duplication
authorIgor Ryzhov <iryzhov@nfware.com>
Wed, 2 Jun 2021 21:29:51 +0000 (00:29 +0300)
committerIgor Ryzhov <iryzhov@nfware.com>
Tue, 8 Jun 2021 17:50:10 +0000 (20:50 +0300)
Signed-off-by: Igor Ryzhov <iryzhov@nfware.com>
lib/command.c
lib/command.h
vtysh/vtysh.c

index 5cf1a4f57aa4d443744001945b3a6af24d15ce2c..6f0bb47dc804e5e723c050b7230e8de0847d04d1 100644 (file)
@@ -2231,11 +2231,7 @@ DEFUN (no_banner_motd,
        return CMD_SUCCESS;
 }
 
-DEFUN(find,
-      find_cmd,
-      "find REGEX...",
-      "Find CLI command matching a regular expression\n"
-      "Search pattern (POSIX regex)\n")
+int cmd_find_cmds(struct vty *vty, struct cmd_token **argv, int argc)
 {
        const struct cmd_node *node;
        const struct cmd_element *cli;
@@ -2313,6 +2309,15 @@ done:
        return CMD_SUCCESS;
 }
 
+DEFUN(find,
+      find_cmd,
+      "find REGEX...",
+      "Find CLI command matching a regular expression\n"
+      "Search pattern (POSIX regex)\n")
+{
+       return cmd_find_cmds(vty, argv, argc);
+}
+
 #if defined(DEV_BUILD) && defined(HAVE_SCRIPTING)
 DEFUN(script,
       script_cmd,
index 51da4c52e63a18dde2832e99c73e73193405c796..1536bb144915093a53a13ba7b6cda8e98fa0beec 100644 (file)
@@ -574,6 +574,7 @@ extern void cmd_init_config_callbacks(void (*start_config_cb)(void),
 extern void cmd_terminate(void);
 extern void cmd_exit(struct vty *vty);
 extern int cmd_list_cmds(struct vty *vty, int do_permute);
+extern int cmd_find_cmds(struct vty *vty, struct cmd_token **argv, int argc);
 
 extern int cmd_domainname_set(const char *domainname);
 extern int cmd_hostname_set(const char *hostname);
index 111c2dbc03db4d1e9f45ccc2df6538675619cb4a..9db4a754702caded0bfb8980a9f30376c8971570 100644 (file)
@@ -3619,79 +3619,7 @@ DEFUN(find,
       "Find CLI command matching a regular expression\n"
       "Search pattern (POSIX regex)\n")
 {
-       const struct cmd_node *node;
-       const struct cmd_element *cli;
-       vector clis;
-       regex_t exp = {};
-       char *pattern = argv_concat(argv, argc, 1);
-       int cr = regcomp(&exp, pattern, REG_NOSUB | REG_EXTENDED);
-
-       XFREE(MTYPE_TMP, pattern);
-
-       if (cr != 0) {
-               switch (cr) {
-               case REG_BADBR:
-                       vty_out(vty, "%% Invalid \\{...\\} expression\n");
-                       break;
-               case REG_BADRPT:
-                       vty_out(vty, "%% Bad repetition operator\n");
-                       break;
-               case REG_BADPAT:
-                       vty_out(vty, "%% Regex syntax error\n");
-                       break;
-               case REG_ECOLLATE:
-                       vty_out(vty, "%% Invalid collating element\n");
-                       break;
-               case REG_ECTYPE:
-                       vty_out(vty, "%% Invalid character class name\n");
-                       break;
-               case REG_EESCAPE:
-                       vty_out(vty,
-                               "%% Regex ended with escape character (\\)\n");
-                       break;
-               case REG_ESUBREG:
-                       vty_out(vty,
-                               "%% Invalid number in \\digit construction\n");
-                       break;
-               case REG_EBRACK:
-                       vty_out(vty, "%% Unbalanced square brackets\n");
-                       break;
-               case REG_EPAREN:
-                       vty_out(vty, "%% Unbalanced parentheses\n");
-                       break;
-               case REG_EBRACE:
-                       vty_out(vty, "%% Unbalanced braces\n");
-                       break;
-               case REG_ERANGE:
-                       vty_out(vty,
-                               "%% Invalid endpoint in range expression\n");
-                       break;
-               case REG_ESPACE:
-                       vty_out(vty, "%% Failed to compile (out of memory)\n");
-                       break;
-               }
-
-               goto done;
-       }
-
-
-       for (unsigned int i = 0; i < vector_active(cmdvec); i++) {
-               node = vector_slot(cmdvec, i);
-               if (!node)
-                       continue;
-               clis = node->cmd_vector;
-               for (unsigned int j = 0; j < vector_active(clis); j++) {
-                       cli = vector_slot(clis, j);
-
-                       if (regexec(&exp, cli->string, 0, NULL, 0) == 0)
-                               vty_out(vty, "  (%s)  %s\n",
-                                       node->name, cli->string);
-               }
-       }
-
-done:
-       regfree(&exp);
-       return CMD_SUCCESS;
+       return cmd_find_cmds(vty, argv, argc);
 }
 
 DEFUN_HIDDEN(show_cli_graph_vtysh,