]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/grammar_sandbox.c
lib: Add matching and argv support
[mirror_frr.git] / lib / grammar_sandbox.c
index 24f882c89f57e27c7883fe780df9a4588539ea76..6eee9c8b657e22b45153e5954c3f97333bf6f786 100644 (file)
@@ -14,11 +14,11 @@ DEFUN (grammar_test,
        GRAMMAR_STR
        "command to pass to new parser\n")
 {
-
-  const char* command = argv_concat(argv, argc, 0);
-  cmd_parse_format(command, "lol", nodegraph);
+  char* command = argv_concat(argv, argc, 0);
+  struct cmd_element *cmd = malloc(sizeof(struct cmd_element));
+  cmd->string = command;
+  parse_command_format(nodegraph, cmd);
   walk_graph(nodegraph, 0);
-
   return CMD_SUCCESS;
 }
 
@@ -32,15 +32,15 @@ DEFUN (grammar_test_show,
   return CMD_SUCCESS;
 }
 
-DEFUN (grammar_test_match,
-       grammar_test_match_cmd,
-       "grammar match .COMMAND",
+DEFUN (grammar_test_complete,
+       grammar_test_complete_cmd,
+       "grammar complete .COMMAND",
        GRAMMAR_STR
-       "attempt to match input on DFA\n"
-       "command to match")
+       "attempt to complete input on DFA\n"
+       "command to complete")
 {
   const char* command = argv_concat(argv, argc, 0);
-  struct list *result = match_command(nodegraph, FILTER_STRICT, command);
+  struct list *result = match_command_complete (nodegraph, command, FILTER_STRICT);
 
   if (result->count == 0) // invalid command
     fprintf(stderr, "%% Unknown command\n");
@@ -53,7 +53,7 @@ DEFUN (grammar_test_match,
     // print possible next hops, if any
     for (ALL_LIST_ELEMENTS_RO(result,node,cnode)) {
       if (cnode->type == END_GN)
-        fprintf(stderr, "<cr>");
+        fprintf(stderr, "<cr>\n");
       else
         fprintf(stderr, "%s\n", describe_node(cnode, desc, 50));
     }
@@ -64,6 +64,39 @@ DEFUN (grammar_test_match,
   return CMD_SUCCESS;
 }
 
+DEFUN (grammar_test_match,
+       grammar_test_match_cmd,
+       "grammar match .COMMAND",
+       GRAMMAR_STR
+       "attempt to match input on DFA\n"
+       "command to match")
+{
+  const char* command = argv_concat(argv, argc, 0);
+  struct cmd_element *element = match_command (nodegraph, command, FILTER_STRICT);
+
+  if (element)
+    fprintf(stderr, "Matched: %s\n", element->string);
+  else {
+    fprintf(stderr, "Returned NULL\n");
+    return CMD_SUCCESS;
+  }
+
+  struct list *argvv = match_build_argv (command, element);
+  fprintf(stderr, "num args: %d\n", argvv->count);
+
+  struct listnode *ln;
+  struct graph_node *gn;
+  for (ALL_LIST_ELEMENTS_RO(argvv,ln,gn)) {
+    fprintf(stderr, "node text: %s\n", gn->text);
+    if (gn->arg)
+      fprintf(stderr, "node arg: %s\n", gn->arg);
+    else
+      fprintf(stderr, "No arg.\n");
+  }
+
+  return CMD_SUCCESS;
+}
+
 
 void grammar_sandbox_init(void);
 void grammar_sandbox_init() {
@@ -72,4 +105,5 @@ void grammar_sandbox_init() {
   install_element (ENABLE_NODE, &grammar_test_cmd);
   install_element (ENABLE_NODE, &grammar_test_show_cmd);
   install_element (ENABLE_NODE, &grammar_test_match_cmd);
+  install_element (ENABLE_NODE, &grammar_test_complete_cmd);
 }