]> git.proxmox.com Git - mirror_frr.git/blobdiff - tools/permutations.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[mirror_frr.git] / tools / permutations.c
index 49f9416b21c2614ed4d51b11fd1d9759e2060c27..f51d4a4ec9a845a84e9d9117bcbc41ef2b5ae4d0 100644 (file)
  * 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 GNU Zebra; see the file COPYING.  If not, write to the Free
- * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- * 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include "command.h"
 #include "graph.h"
-#include "command_parse.h"
 #include "vector.h"
 
 #define USAGE "usage: permutations <cmdstr>"
 
-void
-permute (struct graph_node *);
-void
-pretty_print_graph (struct graph_node *start, int level);
+void permute(struct graph_node *);
+void pretty_print_graph(struct graph_node *start, int level);
 
-int main (int argc, char *argv[])
+int main(int argc, char *argv[])
 {
-  if (argc < 2)
-  {
-    fprintf(stdout, USAGE"\n");
-    exit(EXIT_SUCCESS);
-  }
-  struct cmd_element *cmd = calloc (1, sizeof (struct cmd_element));
-  cmd->string = strdup(argv[1]);
+       if (argc < 2) {
+               fprintf(stdout, USAGE "\n");
+               exit(EXIT_SUCCESS);
+       }
+       struct cmd_element *cmd = XCALLOC(MTYPE_TMP,
+                                         sizeof(struct cmd_element));
+       cmd->string = strdup(argv[1]);
 
-  struct graph *graph = graph_new();
-  struct cmd_token *token = new_cmd_token (START_TKN, cmd->attr, NULL, NULL);
-  graph_new_node (graph, token, NULL);
-  command_parse_format (graph, cmd);
+       struct graph *graph = graph_new();
+       struct cmd_token *token =
+               cmd_token_new(START_TKN, cmd->attr, NULL, NULL);
+       graph_new_node(graph, token, NULL);
+       cmd_graph_parse(graph, cmd);
 
-  permute (vector_slot (graph->nodes, 0));
-  pretty_print_graph (vector_slot (graph->nodes, 0), 0);
+       permute(vector_slot(graph->nodes, 0));
 }
 
-void
-permute (struct graph_node *start)
+void permute(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
-      permute (gn);
-  }
-  list_delete_node (position, listtail(position));
-}
+       static struct list *position = NULL;
+       if (!position)
+               position = list_new();
 
-void
-pretty_print_graph (struct graph_node *start, int level)
-{
-  // print this node
-  struct cmd_token *tok = start->data;
-  fprintf (stdout, "%s[%d] ", tok->text, tok->type);
+       struct cmd_token *stok = start->data;
+       struct graph_node *gnn;
+       struct listnode *ln;
 
-  int numto = vector_active (start->to);
-  if (numto)
-    {
-      if (numto > 1)
-        fprintf (stdout, "\n");
-      for (unsigned int i = 0; i < vector_active (start->to); i++)
-        {
-          struct graph_node *adj = vector_slot (start->to, i);
-          // if we're listing multiple children, indent!
-          if (numto > 1)
-            for (int j = 0; j < level+1; j++)
-              fprintf (stdout, "    ");
-          // if this node is a vararg, just print *
-          if (adj == start)
-            fprintf (stdout, "*");
-          else
-            pretty_print_graph (adj, numto > 1 ? level+1 : level);
-        }
-    }
-  else
-    fprintf(stdout, "\n");
+       // 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->attr == CMD_ATTR_HIDDEN
+                   || tok->attr == CMD_ATTR_DEPRECATED)
+                       continue;
+               else if (tok->type == END_TKN || gn == start) {
+                       fprintf(stdout, " ");
+                       for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) {
+                               struct cmd_token *tt = gnn->data;
+                               if (tt->type < SPECIAL_TKN)
+                                       fprintf(stdout, " %s", tt->text);
+                       }
+                       if (gn == start)
+                               fprintf(stdout, "...");
+                       fprintf(stdout, "\n");
+               } else {
+                       bool skip = false;
+                       if (stok->type == FORK_TKN && tok->type != FORK_TKN)
+                               for (ALL_LIST_ELEMENTS_RO(position, ln, gnn))
+                                       if (gnn == gn) {
+                                               skip = true;
+                                               break;
+                                       }
+                       if (!skip)
+                               permute(gn);
+               }
+       }
+       list_delete_node(position, listtail(position));
 }