From 96dcc565e6e590eddd124cdcf3634b68b9579a85 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 2 Oct 2016 19:13:59 +0000 Subject: [PATCH] tools: add command permutations generator Signed-off-by: Quentin Young --- tools/.gitignore | 3 ++- tools/Makefile.am | 8 +++++++ tools/permutations.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tools/permutations.c diff --git a/tools/.gitignore b/tools/.gitignore index dd5bf7c67c..60c4c0650f 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -3,4 +3,5 @@ *~ *.loT - +.libs +*.o diff --git a/tools/Makefile.am b/tools/Makefile.am index 125bfee2c2..c5dbba5a8b 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -1,3 +1,11 @@ +AM_CPPFLAGS = -I.. -I$(top_srcdir) -I$(top_srcdir)/lib -I$(top_builddir)/lib +DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\" +AM_CFLAGS = $(WERROR) + +bin_PROGRAMS = permutations +permutations_SOURCES = permutations.c +permutations_LDADD = ../lib/libzebra.la + sbin_SCRIPTS = quagga-reload.py quagga EXTRA_DIST = quagga.service quagga-reload.py quagga diff --git a/tools/permutations.c b/tools/permutations.c new file mode 100644 index 0000000000..4862781296 --- /dev/null +++ b/tools/permutations.c @@ -0,0 +1,57 @@ +#include "command.h" +#include "graph.h" +#include "command_parse.h" +#include "vector.h" + +void +pretty_print_graph (struct graph_node *); + +int main (int argc, char *argv[]) +{ + struct cmd_element *cmd = calloc (1, sizeof (struct cmd_element)); + cmd->string = strdup(argv[1]); + + struct graph *graph = graph_new(); + struct cmd_token *token = new_cmd_token (START_TKN, NULL, NULL); + graph_new_node (graph, token, NULL); + command_parse_format (graph, cmd); + + pretty_print_graph (vector_slot (graph->nodes, 0)); +} + +/** + * Pretty-prints a graph, assuming it is a tree. + * + * @param start the node to take as the root + * @param level indent level for recursive calls, always pass 0 + */ + +void +pretty_print_graph (struct graph_node *start) +{ + static struct list *position = NULL; + if (!position) position = list_new (); + + // recursive dfs + listnode_add (position, start); + for (unsigned int i = 0; i < vector_active (start->to); i++) + { + struct graph_node *gn = vector_slot (start->to, i); + struct cmd_token *tok = gn->data; + if (tok->type == END_TKN) + { + struct graph_node *gnn; + struct listnode *ln; + for (ALL_LIST_ELEMENTS_RO (position,ln,gnn)) + { + struct cmd_token *tt = gnn->data; + if (tt->type < SELECTOR_TKN) + fprintf (stdout, "%s ", tt->text); + } + fprintf (stdout, "\n"); + } + else + pretty_print_graph (gn); + } + list_delete_node (position, listtail(position)); +} -- 2.39.5