]> git.puffer.fish Git - mirror/frr.git/commitdiff
tools: add command permutations generator
authorQuentin Young <qlyoung@cumulusnetworks.com>
Sun, 2 Oct 2016 19:13:59 +0000 (19:13 +0000)
committerQuentin Young <qlyoung@cumulusnetworks.com>
Sun, 2 Oct 2016 19:13:59 +0000 (19:13 +0000)
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
tools/.gitignore
tools/Makefile.am
tools/permutations.c [new file with mode: 0644]

index dd5bf7c67c9b06f0250e3299242e152d00e3f827..60c4c0650f9dc51e892314b4624743029b3c9c89 100644 (file)
@@ -3,4 +3,5 @@
 
 *~
 *.loT
-
+.libs
+*.o
index 125bfee2c2ca1b5430325ec2cce118f8b168f763..c5dbba5a8ba1cc9651f9d5a6b70547f52c11e046 100644 (file)
@@ -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 (file)
index 0000000..4862781
--- /dev/null
@@ -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));
+}