]> git.proxmox.com Git - mirror_frr.git/blob - tools/permutations.c
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / tools / permutations.c
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 *
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
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "command.h"
28 #include "graph.h"
29 #include "vector.h"
30
31 #define USAGE "usage: permutations <cmdstr>"
32
33 void permute(struct graph_node *);
34 void pretty_print_graph(struct graph_node *start, int level);
35
36 int main(int argc, char *argv[])
37 {
38 if (argc < 2) {
39 fprintf(stdout, USAGE "\n");
40 exit(EXIT_SUCCESS);
41 }
42 struct cmd_element *cmd = XCALLOC(MTYPE_TMP,
43 sizeof(struct cmd_element));
44 cmd->string = strdup(argv[1]);
45
46 struct graph *graph = graph_new();
47 struct cmd_token *token =
48 cmd_token_new(START_TKN, cmd->attr, NULL, NULL);
49 graph_new_node(graph, token, NULL);
50 cmd_graph_parse(graph, cmd);
51
52 permute(vector_slot(graph->nodes, 0));
53 }
54
55 void permute(struct graph_node *start)
56 {
57 static struct list *position = NULL;
58 if (!position)
59 position = list_new();
60
61 struct cmd_token *stok = start->data;
62 struct graph_node *gnn;
63 struct listnode *ln;
64 bool is_neg = false;
65
66 // recursive dfs
67 listnode_add(position, start);
68
69 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) {
70 struct cmd_token *tok = gnn->data;
71
72 if (tok->type == WORD_TKN && !strcmp(tok->text, "no")) {
73 is_neg = true;
74 break;
75 }
76 if (tok->type < SPECIAL_TKN)
77 break;
78 }
79
80 for (unsigned int i = 0; i < vector_active(start->to); i++) {
81 struct graph_node *gn = vector_slot(start->to, i);
82 struct cmd_token *tok = gn->data;
83 if (tok->attr & CMD_ATTR_HIDDEN)
84 continue;
85 else if (tok->type == END_TKN || gn == start) {
86 fprintf(stdout, " ");
87 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) {
88 struct cmd_token *tt = gnn->data;
89 if (tt->type < SPECIAL_TKN)
90 fprintf(stdout, " %s", tt->text);
91 }
92 if (gn == start)
93 fprintf(stdout, "...");
94 fprintf(stdout, "\n");
95 } else {
96 bool skip = false;
97
98 if (tok->type == NEG_ONLY_TKN && !is_neg)
99 continue;
100 if (stok->type == FORK_TKN && tok->type != FORK_TKN)
101 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn))
102 if (gnn == gn) {
103 skip = true;
104 break;
105 }
106 if (!skip)
107 permute(gn);
108 }
109 }
110 list_delete_node(position, listtail(position));
111 }