]> git.proxmox.com Git - mirror_frr.git/blob - tools/permutations.c
release: FRR 3.0-rc1
[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
19 * along with GNU Zebra; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24 #include "command.h"
25 #include "graph.h"
26 #include "vector.h"
27
28 #define USAGE "usage: permutations <cmdstr>"
29
30 void permute(struct graph_node *);
31 void pretty_print_graph(struct graph_node *start, int level);
32
33 int main(int argc, char *argv[])
34 {
35 if (argc < 2) {
36 fprintf(stdout, USAGE "\n");
37 exit(EXIT_SUCCESS);
38 }
39 struct cmd_element *cmd = calloc(1, sizeof(struct cmd_element));
40 cmd->string = strdup(argv[1]);
41
42 struct graph *graph = graph_new();
43 struct cmd_token *token =
44 new_cmd_token(START_TKN, cmd->attr, NULL, NULL);
45 graph_new_node(graph, token, NULL);
46 command_parse_format(graph, cmd);
47
48 permute(vector_slot(graph->nodes, 0));
49 }
50
51 void permute(struct graph_node *start)
52 {
53 static struct list *position = NULL;
54 if (!position)
55 position = list_new();
56
57 struct cmd_token *stok = start->data;
58 struct graph_node *gnn;
59 struct listnode *ln;
60
61 // recursive dfs
62 listnode_add(position, start);
63 for (unsigned int i = 0; i < vector_active(start->to); i++) {
64 struct graph_node *gn = vector_slot(start->to, i);
65 struct cmd_token *tok = gn->data;
66 if (tok->attr == CMD_ATTR_HIDDEN
67 || tok->attr == CMD_ATTR_DEPRECATED)
68 continue;
69 else if (tok->type == END_TKN || gn == start) {
70 fprintf(stdout, " ");
71 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) {
72 struct cmd_token *tt = gnn->data;
73 if (tt->type < SPECIAL_TKN)
74 fprintf(stdout, " %s", tt->text);
75 }
76 if (gn == start)
77 fprintf(stdout, "...");
78 fprintf(stdout, "\n");
79 } else {
80 bool skip = false;
81 if (stok->type == FORK_TKN && tok->type != FORK_TKN)
82 for (ALL_LIST_ELEMENTS_RO(position, ln, gnn))
83 if (gnn == gn) {
84 skip = true;
85 break;
86 }
87 if (!skip)
88 permute(gn);
89 }
90 }
91 list_delete_node(position, listtail(position));
92 }