]> git.proxmox.com Git - mirror_frr.git/blame - tools/permutations.c
Merge pull request #3362 from pacovn/Coverity_1475469_null_check
[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
b45ac5f5
DL
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
96dcc565
QY
27#include "command.h"
28#include "graph.h"
96dcc565
QY
29#include "vector.h"
30
73baf6a3
QY
31#define USAGE "usage: permutations <cmdstr>"
32
d62a17ae 33void permute(struct graph_node *);
34void pretty_print_graph(struct graph_node *start, int level);
96dcc565 35
d62a17ae 36int main(int argc, char *argv[])
96dcc565 37{
d62a17ae 38 if (argc < 2) {
39 fprintf(stdout, USAGE "\n");
40 exit(EXIT_SUCCESS);
41 }
3b23c6c3
A
42 struct cmd_element *cmd = XCALLOC(MTYPE_TMP,
43 sizeof(struct cmd_element));
d62a17ae 44 cmd->string = strdup(argv[1]);
96dcc565 45
d62a17ae 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);
96dcc565 51
d62a17ae 52 permute(vector_slot(graph->nodes, 0));
96dcc565
QY
53}
54
d62a17ae 55void permute(struct graph_node *start)
96dcc565 56{
d62a17ae 57 static struct list *position = NULL;
58 if (!position)
59 position = list_new();
96dcc565 60
d62a17ae 61 struct cmd_token *stok = start->data;
62 struct graph_node *gnn;
63 struct listnode *ln;
e4e63cb6 64
d62a17ae 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));
943624d7 96}