]> git.proxmox.com Git - mirror_frr.git/blame - tools/permutations.c
*: make consistent & update GPLv2 file headers
[mirror_frr.git] / tools / permutations.c
CommitLineData
73baf6a3
QY
1/*
2 * Generates all possible matching inputs for a command string.
3 * --
4 * Copyright (C) 2016 Cumulus Networks, Inc.
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
896014f4
DL
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
73baf6a3
QY
21 */
22
96dcc565
QY
23#include "command.h"
24#include "graph.h"
96dcc565
QY
25#include "vector.h"
26
73baf6a3
QY
27#define USAGE "usage: permutations <cmdstr>"
28
96dcc565 29void
73baf6a3 30permute (struct graph_node *);
943624d7
QY
31void
32pretty_print_graph (struct graph_node *start, int level);
96dcc565
QY
33
34int main (int argc, char *argv[])
35{
73baf6a3
QY
36 if (argc < 2)
37 {
38 fprintf(stdout, USAGE"\n");
39 exit(EXIT_SUCCESS);
40 }
96dcc565
QY
41 struct cmd_element *cmd = calloc (1, sizeof (struct cmd_element));
42 cmd->string = strdup(argv[1]);
43
44 struct graph *graph = graph_new();
5894e76d 45 struct cmd_token *token = cmd_token_new (START_TKN, cmd->attr, NULL, NULL);
96dcc565 46 graph_new_node (graph, token, NULL);
5894e76d 47 cmd_graph_parse (graph, cmd);
96dcc565 48
73baf6a3 49 permute (vector_slot (graph->nodes, 0));
96dcc565
QY
50}
51
96dcc565 52void
73baf6a3 53permute (struct graph_node *start)
96dcc565
QY
54{
55 static struct list *position = NULL;
56 if (!position) position = list_new ();
57
e4e63cb6
QY
58 struct cmd_token *stok = start->data;
59 struct graph_node *gnn;
60 struct listnode *ln;
61
96dcc565
QY
62 // recursive dfs
63 listnode_add (position, start);
64 for (unsigned int i = 0; i < vector_active (start->to); i++)
65 {
66 struct graph_node *gn = vector_slot (start->to, i);
67 struct cmd_token *tok = gn->data;
e4e63cb6
QY
68 if (tok->attr == CMD_ATTR_HIDDEN ||
69 tok->attr == CMD_ATTR_DEPRECATED)
70 continue;
71 else if (tok->type == END_TKN || gn == start)
96dcc565 72 {
e4e63cb6 73 fprintf (stdout, " ");
96dcc565
QY
74 for (ALL_LIST_ELEMENTS_RO (position,ln,gnn))
75 {
76 struct cmd_token *tt = gnn->data;
0bf5b1cb 77 if (tt->type < SPECIAL_TKN)
e4e63cb6 78 fprintf (stdout, " %s", tt->text);
96dcc565 79 }
e4e63cb6
QY
80 if (gn == start)
81 fprintf (stdout, "...");
96dcc565
QY
82 fprintf (stdout, "\n");
83 }
84 else
943624d7 85 {
e4e63cb6
QY
86 bool skip = false;
87 if (stok->type == FORK_TKN && tok->type != FORK_TKN)
88 for (ALL_LIST_ELEMENTS_RO (position, ln, gnn))
9bc22111
HWC
89 if (gnn == gn)
90 {
91 skip = true;
e4e63cb6 92 break;
9bc22111 93 }
e4e63cb6
QY
94 if (!skip)
95 permute (gn);
943624d7 96 }
e4e63cb6
QY
97 }
98 list_delete_node (position, listtail(position));
943624d7 99}