]> git.proxmox.com Git - mirror_frr.git/blob - tools/permutations.c
Merge pull request #3010 from opensourcerouting/no-frr-thread-id
[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
65 // recursive dfs
66 listnode_add(position, start);
67 for (unsigned int i = 0; i < vector_active(start->to); i++) {
68 struct graph_node *gn = vector_slot(start->to, i);
69 struct cmd_token *tok = gn->data;
70 if (tok->attr == CMD_ATTR_HIDDEN
71 || tok->attr == CMD_ATTR_DEPRECATED)
72 continue;
73 else if (tok->type == END_TKN || gn == start) {
74 fprintf(stdout, " ");
75 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) {
76 struct cmd_token *tt = gnn->data;
77 if (tt->type < SPECIAL_TKN)
78 fprintf(stdout, " %s", tt->text);
79 }
80 if (gn == start)
81 fprintf(stdout, "...");
82 fprintf(stdout, "\n");
83 } else {
84 bool skip = false;
85 if (stok->type == FORK_TKN && tok->type != FORK_TKN)
86 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn))
87 if (gnn == gn) {
88 skip = true;
89 break;
90 }
91 if (!skip)
92 permute(gn);
93 }
94 }
95 list_delete_node(position, listtail(position));
96 }