]>
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 | ||
96dcc565 QY |
23 | #include "command.h" |
24 | #include "graph.h" | |
96dcc565 QY |
25 | #include "vector.h" |
26 | ||
73baf6a3 QY |
27 | #define USAGE "usage: permutations <cmdstr>" |
28 | ||
d62a17ae | 29 | void permute(struct graph_node *); |
30 | void pretty_print_graph(struct graph_node *start, int level); | |
96dcc565 | 31 | |
d62a17ae | 32 | int main(int argc, char *argv[]) |
96dcc565 | 33 | { |
d62a17ae | 34 | if (argc < 2) { |
35 | fprintf(stdout, USAGE "\n"); | |
36 | exit(EXIT_SUCCESS); | |
37 | } | |
38 | struct cmd_element *cmd = calloc(1, sizeof(struct cmd_element)); | |
39 | cmd->string = strdup(argv[1]); | |
96dcc565 | 40 | |
d62a17ae | 41 | struct graph *graph = graph_new(); |
42 | struct cmd_token *token = | |
43 | cmd_token_new(START_TKN, cmd->attr, NULL, NULL); | |
44 | graph_new_node(graph, token, NULL); | |
45 | cmd_graph_parse(graph, cmd); | |
96dcc565 | 46 | |
d62a17ae | 47 | permute(vector_slot(graph->nodes, 0)); |
96dcc565 QY |
48 | } |
49 | ||
d62a17ae | 50 | void permute(struct graph_node *start) |
96dcc565 | 51 | { |
d62a17ae | 52 | static struct list *position = NULL; |
53 | if (!position) | |
54 | position = list_new(); | |
96dcc565 | 55 | |
d62a17ae | 56 | struct cmd_token *stok = start->data; |
57 | struct graph_node *gnn; | |
58 | struct listnode *ln; | |
e4e63cb6 | 59 | |
d62a17ae | 60 | // recursive dfs |
61 | listnode_add(position, start); | |
62 | for (unsigned int i = 0; i < vector_active(start->to); i++) { | |
63 | struct graph_node *gn = vector_slot(start->to, i); | |
64 | struct cmd_token *tok = gn->data; | |
65 | if (tok->attr == CMD_ATTR_HIDDEN | |
66 | || tok->attr == CMD_ATTR_DEPRECATED) | |
67 | continue; | |
68 | else if (tok->type == END_TKN || gn == start) { | |
69 | fprintf(stdout, " "); | |
70 | for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) { | |
71 | struct cmd_token *tt = gnn->data; | |
72 | if (tt->type < SPECIAL_TKN) | |
73 | fprintf(stdout, " %s", tt->text); | |
74 | } | |
75 | if (gn == start) | |
76 | fprintf(stdout, "..."); | |
77 | fprintf(stdout, "\n"); | |
78 | } else { | |
79 | bool skip = false; | |
80 | if (stok->type == FORK_TKN && tok->type != FORK_TKN) | |
81 | for (ALL_LIST_ELEMENTS_RO(position, ln, gnn)) | |
82 | if (gnn == gn) { | |
83 | skip = true; | |
84 | break; | |
85 | } | |
86 | if (!skip) | |
87 | permute(gn); | |
88 | } | |
89 | } | |
90 | list_delete_node(position, listtail(position)); | |
943624d7 | 91 | } |