Skip to content

Commit

Permalink
*: add ->node_exit to struct cmd_node
Browse files Browse the repository at this point in the history
Rather than doing a f*gly hack for the RPKI code, let's do an on-exit
hook in cmd_node.  Also allows replacing some special-casing in the vty
code.

Signed-off-by: David Lamparter <equinox@diac24.net>
  • Loading branch information
eqvinox committed Apr 2, 2020
1 parent bfec662 commit 220748b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 67 deletions.
52 changes: 3 additions & 49 deletions bgpd/bgp_rpki.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static struct rtr_mgr_group *get_connected_group(void);
static void print_prefix_table(struct vty *vty);
static void install_cli_commands(void);
static int config_write(struct vty *vty);
static void overwrite_exit_commands(void);
static void config_on_exit(struct vty *vty);
static void free_cache(struct cache *cache);
static struct rtr_mgr_group *get_groups(void);
#if defined(FOUND_SSH)
Expand Down Expand Up @@ -149,6 +149,7 @@ static struct cmd_node rpki_node = {
.parent_node = CONFIG_NODE,
.prompt = "%s(config-rpki)# ",
.config_write = config_write,
.node_exit = config_on_exit,
};
static const struct route_map_rule_cmd route_match_rpki_cmd = {
"rpki", route_match, route_match_compile, route_match_free};
Expand Down Expand Up @@ -1400,35 +1401,9 @@ DEFUN (show_rpki_cache_connection,
return CMD_SUCCESS;
}

DEFUN_NOSH (rpki_exit,
rpki_exit_cmd,
"exit",
"Exit rpki configuration and restart rpki session\n")
static void config_on_exit(struct vty *vty)
{
reset(false);

vty->node = CONFIG_NODE;
return CMD_SUCCESS;
}

DEFUN_NOSH (rpki_quit,
rpki_quit_cmd,
"quit",
"Exit rpki configuration mode\n")
{
return rpki_exit(self, vty, argc, argv);
}

DEFUN_NOSH (rpki_end,
rpki_end_cmd,
"end",
"End rpki configuration, restart rpki session and change to enable mode.\n")
{
int ret = reset(false);

vty_config_exit(vty);
vty->node = ENABLE_NODE;
return ret == SUCCESS ? CMD_SUCCESS : CMD_WARNING;
}

DEFUN (rpki_reset,
Expand Down Expand Up @@ -1522,32 +1497,11 @@ DEFUN (no_match_rpki,
return CMD_SUCCESS;
}

static void overwrite_exit_commands(void)
{
unsigned int i;
vector cmd_vector = rpki_node.cmd_vector;

for (i = 0; i < cmd_vector->active; ++i) {
struct cmd_element *cmd = vector_lookup(cmd_vector, i);

if (strcmp(cmd->string, "exit") == 0
|| strcmp(cmd->string, "quit") == 0
|| strcmp(cmd->string, "end") == 0) {
uninstall_element(RPKI_NODE, cmd);
}
}

install_element(RPKI_NODE, &rpki_exit_cmd);
install_element(RPKI_NODE, &rpki_quit_cmd);
install_element(RPKI_NODE, &rpki_end_cmd);
}

static void install_cli_commands(void)
{
// TODO: make config write work
install_node(&rpki_node);
install_default(RPKI_NODE);
overwrite_exit_commands();
install_element(CONFIG_NODE, &rpki_cmd);
install_element(ENABLE_NODE, &rpki_cmd);

Expand Down
35 changes: 17 additions & 18 deletions lib/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ const char *cmd_domainname_get(void)
return host.domainname;
}

static void root_on_exit(struct vty *vty);

/* Standard command node structures. */
static struct cmd_node auth_node = {
.name = "auth",
Expand All @@ -110,6 +112,7 @@ static struct cmd_node view_node = {
.name = "view",
.node = VIEW_NODE,
.prompt = "%s> ",
.node_exit = root_on_exit,
};

static struct cmd_node auth_enable_node = {
Expand All @@ -122,6 +125,7 @@ static struct cmd_node enable_node = {
.name = "enable",
.node = ENABLE_NODE,
.prompt = "%s# ",
.node_exit = root_on_exit,
};

static int config_write_host(struct vty *vty);
Expand All @@ -131,6 +135,7 @@ static struct cmd_node config_node = {
.parent_node = ENABLE_NODE,
.prompt = "%s(config)# ",
.config_write = config_write_host,
.node_exit = vty_config_exit,
};

static const struct facility_map {
Expand Down Expand Up @@ -1382,28 +1387,22 @@ DEFUN (config_exit,
return CMD_SUCCESS;
}

static void root_on_exit(struct vty *vty)
{
if (vty_shell(vty))
exit(0);
else
vty->status = VTY_CLOSE;
}

void cmd_exit(struct vty *vty)
{
struct cmd_node *cnode = vector_lookup(cmdvec, vty->node);

switch (vty->node) {
case VIEW_NODE:
case ENABLE_NODE:
if (vty_shell(vty))
exit(0);
else
vty->status = VTY_CLOSE;
break;
case CONFIG_NODE:
vty->node = ENABLE_NODE;
vty_config_exit(vty);
break;
default:
if (cnode->parent_node)
vty->node = cnode->parent_node;
break;
}

if (cnode->node_exit)
cnode->node_exit(vty);
if (cnode->parent_node)
vty->node = cnode->parent_node;
if (vty->xpath_index > 0)
vty->xpath_index--;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ struct cmd_node {
/* Node's configuration write function */
int (*config_write)(struct vty *);

/* called when leaving the node on a VTY session */
void (*node_exit)(struct vty *);

/* Node's command graph */
struct graph *cmdgraph;

Expand Down

0 comments on commit 220748b

Please sign in to comment.