]>
Commit | Line | Data |
---|---|---|
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 | 33 | void permute(struct graph_node *); |
34 | void pretty_print_graph(struct graph_node *start, int level); | |
96dcc565 | 35 | |
d62a17ae | 36 | int 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 | 55 | void 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; | |
90c8406c | 64 | bool is_neg = false; |
e4e63cb6 | 65 | |
d62a17ae | 66 | // recursive dfs |
67 | listnode_add(position, start); | |
90c8406c DL |
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 | ||
d62a17ae | 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; | |
90c8406c DL |
98 | |
99 | if (tok->type == NEG_ONLY_TKN && !is_neg) | |
100 | continue; | |
d62a17ae | 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)); | |
943624d7 | 112 | } |