grammar_sandbox: add into daemons if DEV_BUILD

Also adds "grammar access <node>" to test/dump an existing command node
(e.g. BGP_NODE).

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
This commit is contained in:
David Lamparter 2016-12-16 08:11:37 +01:00
parent 818a5168a1
commit af2567b646
5 changed files with 109 additions and 91 deletions

View file

@ -26,6 +26,7 @@ libzebra_la_SOURCES = \
imsg-buffer.c imsg.c skiplist.c \
qobj.c wheel.c \
event_counter.c \
grammar_sandbox.c \
strlcpy.c \
strlcat.c
@ -53,7 +54,7 @@ noinst_HEADERS = \
noinst_PROGRAMS = grammar_sandbox
grammar_sandbox_SOURCES = grammar_sandbox.c
grammar_sandbox_SOURCES = grammar_sandbox_main.c
grammar_sandbox_LDADD = libzebra.la
EXTRA_DIST = \

View file

@ -523,7 +523,7 @@ compare_completions (const void *fst, const void *snd)
* @param completions linked list of cmd_token
* @return deduplicated and sorted vector with
*/
static vector
vector
completions_to_vec (struct list *completions)
{
vector comps = vector_init (VECTOR_MIN_SIZE);
@ -2398,6 +2398,10 @@ cmd_init (int terminal)
vrf_install_commands ();
}
srandom(time(NULL));
#ifdef DEV_BUILD
grammar_sandbox_init();
#endif
}
struct cmd_token *

View file

@ -422,6 +422,9 @@ extern void cmd_terminate (void);
extern void cmd_exit (struct vty *vty);
extern int cmd_list_cmds (struct vty *vty, int do_permute);
/* NOT safe for general use; call this only if DEV_BUILD! */
extern void grammar_sandbox_init (void);
/* memory management for cmd_token */
struct cmd_token *
new_cmd_token (enum cmd_token_type, u_char attr, char *, char *);
@ -430,6 +433,7 @@ del_cmd_token (struct cmd_token *);
struct cmd_token *
copy_cmd_token (struct cmd_token *);
extern vector completions_to_vec (struct list *completions);
extern void command_parse_format (struct graph *graph, struct cmd_element *cmd);
/* Export typical functions. */

View file

@ -3,11 +3,6 @@
*
* This unit defines a number of commands in the old engine that can
* be used to test and interact with the new engine.
*
* This shim should be removed upon integration. It is currently hooked in
* vtysh/vtysh.c. It has no header, vtysh.c merely includes this entire unit
* since it clutters up the makefiles less and this is only a temporary shim.
*
* --
* Copyright (C) 2016 Cumulus Networks, Inc.
*
@ -51,13 +46,9 @@ pretty_print_dot (FILE *ofd, unsigned opts, struct graph_node *start,
struct graph_node **visited, size_t *visitpos);
void
init_cmdgraph (struct vty *, struct graph **);
vector
completions_to_vec (struct list *);
int
compare_completions (const void *, const void *);
/** shim interface commands **/
struct graph *nodegraph;
struct graph *nodegraph = NULL, *nodegraph_free = NULL;
DEFUN (grammar_test,
grammar_test_cmd,
@ -280,11 +271,41 @@ DEFUN (grammar_init_graph,
GRAMMAR_STR
"(re)initialize graph\n")
{
graph_delete_graph (nodegraph);
if (nodegraph_free)
graph_delete_graph (nodegraph_free);
nodegraph_free = NULL;
init_cmdgraph (vty, &nodegraph);
return CMD_SUCCESS;
}
extern vector cmdvec;
DEFUN (grammar_access,
grammar_access_cmd,
"grammar access (0-65535)",
GRAMMAR_STR
"access node graph\n"
"node number\n")
{
if (nodegraph_free)
graph_delete_graph (nodegraph_free);
nodegraph_free = NULL;
struct cmd_node *cnode;
cnode = vector_slot (cmdvec, atoi (argv[2]->arg));
if (!cnode)
{
vty_out (vty, "%% no such node%s", VTY_NEWLINE);
return CMD_WARNING;
}
vty_out (vty, "node %d%s", (int)cnode->node, VTY_NEWLINE);
nodegraph = cnode->cmdgraph;
return CMD_SUCCESS;
}
/* this is called in vtysh.c to set up the testing shim */
void grammar_sandbox_init(void) {
init_cmdgraph (NULL, &nodegraph);
@ -297,6 +318,7 @@ void grammar_sandbox_init(void) {
install_element (ENABLE_NODE, &grammar_test_complete_cmd);
install_element (ENABLE_NODE, &grammar_test_doc_cmd);
install_element (ENABLE_NODE, &grammar_init_graph_cmd);
install_element (ENABLE_NODE, &grammar_access_cmd);
}
#define item(x) { x, #x }
@ -463,86 +485,9 @@ init_cmdgraph (struct vty *vty, struct graph **graph)
{
// initialize graph, add start noe
*graph = graph_new ();
nodegraph_free = *graph;
struct cmd_token *token = new_cmd_token (START_TKN, 0, NULL, NULL);
graph_new_node (*graph, token, (void (*)(void *)) &del_cmd_token);
if (vty)
vty_out (vty, "initialized graph%s", VTY_NEWLINE);
}
int
compare_completions (const void *fst, const void *snd)
{
struct cmd_token *first = *(struct cmd_token **) fst,
*secnd = *(struct cmd_token **) snd;
return strcmp (first->text, secnd->text);
}
vector
completions_to_vec (struct list *completions)
{
vector comps = vector_init (VECTOR_MIN_SIZE);
struct listnode *ln;
struct cmd_token *token;
unsigned int i, exists;
for (ALL_LIST_ELEMENTS_RO(completions,ln,token))
{
// linear search for token in completions vector
exists = 0;
for (i = 0; i < vector_active (comps) && !exists; i++)
{
struct cmd_token *curr = vector_slot (comps, i);
exists = !strcmp (curr->text, token->text) &&
!strcmp (curr->desc, token->desc);
}
if (!exists)
vector_set (comps, copy_cmd_token (token));
}
// sort completions
qsort (comps->index,
vector_active (comps),
sizeof (void *),
&compare_completions);
return comps;
}
static void vty_do_exit(void)
{
printf ("\nend.\n");
exit (0);
}
struct thread_master *master;
int main(int argc, char **argv)
{
struct thread thread;
master = thread_master_create ();
zlog_default = openzlog ("grammar_sandbox", ZLOG_NONE, 0,
LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
zlog_set_level (NULL, ZLOG_DEST_STDOUT, LOG_DEBUG);
zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
/* Library inits. */
cmd_init (1);
host.name = strdup ("test");
vty_init (master);
memory_init ();
grammar_sandbox_init();
vty_stdio (vty_do_exit);
/* Fetch next active thread. */
while (thread_fetch (master, &thread))
thread_call (&thread);
/* Not reached. */
exit (0);
}

View file

@ -0,0 +1,64 @@
/*
* Testing shim and API examples for the new CLI backend.
*
* Minimal main() to run grammar_sandbox standalone.
* [split off grammar_sandbox.c 2017-01-23]
* --
* Copyright (C) 2016 Cumulus Networks, Inc.
* Copyright (C) 2017 David Lamparter for NetDEF, Inc.
*
* This file is part of FreeRangeRouting (FRR).
*
* FRR is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2, or (at your option) any later version.
*
* FRR is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with FRR; see the file COPYING. If not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "command.h"
#include "memory_vty.h"
static void vty_do_exit(void)
{
printf ("\nend.\n");
exit (0);
}
struct thread_master *master;
int main(int argc, char **argv)
{
struct thread thread;
master = thread_master_create ();
zlog_default = openzlog ("grammar_sandbox", ZLOG_NONE, 0,
LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
zlog_set_level (NULL, ZLOG_DEST_SYSLOG, ZLOG_DISABLED);
zlog_set_level (NULL, ZLOG_DEST_STDOUT, LOG_DEBUG);
zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
/* Library inits. */
cmd_init (1);
host.name = strdup ("test");
vty_init (master);
memory_init ();
vty_stdio (vty_do_exit);
/* Fetch next active thread. */
while (thread_fetch (master, &thread))
thread_call (&thread);
/* Not reached. */
exit (0);
}