]>
git.proxmox.com Git - mirror_frr.git/blob - tools/permutations.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generates all possible matching inputs for a command string.
5 * Copyright (C) 2016 Cumulus Networks, Inc.
16 #define USAGE "usage: permutations <cmdstr>"
18 void permute(struct graph_node
*);
19 void pretty_print_graph(struct graph_node
*start
, int level
);
21 int main(int argc
, char *argv
[])
24 fprintf(stdout
, USAGE
"\n");
27 struct cmd_element
*cmd
= XCALLOC(MTYPE_TMP
,
28 sizeof(struct cmd_element
));
29 cmd
->string
= strdup(argv
[1]);
31 struct graph
*graph
= graph_new();
32 struct cmd_token
*token
=
33 cmd_token_new(START_TKN
, cmd
->attr
, NULL
, NULL
);
34 graph_new_node(graph
, token
, NULL
);
35 cmd_graph_parse(graph
, cmd
);
37 permute(vector_slot(graph
->nodes
, 0));
40 void permute(struct graph_node
*start
)
42 static struct list
*position
= NULL
;
44 position
= list_new();
46 struct cmd_token
*stok
= start
->data
;
47 struct graph_node
*gnn
;
52 listnode_add(position
, start
);
54 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
)) {
55 struct cmd_token
*tok
= gnn
->data
;
57 if (tok
->type
== WORD_TKN
&& !strcmp(tok
->text
, "no")) {
61 if (tok
->type
< SPECIAL_TKN
)
65 for (unsigned int i
= 0; i
< vector_active(start
->to
); i
++) {
66 struct graph_node
*gn
= vector_slot(start
->to
, i
);
67 struct cmd_token
*tok
= gn
->data
;
68 if (tok
->attr
& CMD_ATTR_HIDDEN
)
70 else if (tok
->type
== END_TKN
|| gn
== start
) {
72 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
)) {
73 struct cmd_token
*tt
= gnn
->data
;
74 if (tt
->type
< SPECIAL_TKN
)
75 fprintf(stdout
, " %s", tt
->text
);
78 fprintf(stdout
, "...");
79 fprintf(stdout
, "\n");
83 if (tok
->type
== NEG_ONLY_TKN
&& !is_neg
)
85 if (stok
->type
== FORK_TKN
&& tok
->type
!= FORK_TKN
)
86 for (ALL_LIST_ELEMENTS_RO(position
, ln
, gnn
))
95 list_delete_node(position
, listtail(position
));