]> git.proxmox.com Git - mirror_frr.git/blob - tools/permutations.c
Merge pull request #9410 from idryzhov/static-show-run-nb
[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 || tok->attr == CMD_ATTR_DEPRECATED)
85 continue;
86 else if (tok->type == END_TKN || gn == start) {
87 fprintf(stdout, " ");
88 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) {
89 struct cmd_token *tt = gnn->data;
90 if (tt->type < SPECIAL_TKN)
91 fprintf(stdout, " %s", tt->text);
92 }
93 if (gn == start)
94 fprintf(stdout, "...");
95 fprintf(stdout, "\n");
96 } else {
97 bool skip = false;
98
99 if (tok->type == NEG_ONLY_TKN && !is_neg)
100 continue;
101 if (stok->type == FORK_TKN && tok->type != FORK_TKN)
102 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn))
103 if (gnn == gn) {
104 skip = true;
105 break;
106 }
107 if (!skip)
108 permute(gn);
109 }
110 }
111 list_delete_node(position, listtail(position));
112 }