]> git.proxmox.com Git - mirror_frr.git/blame - tools/permutations.c
lib: Fix `show running-config` and `write terminal`
[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 *
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
96dcc565
QY
24#include "command.h"
25#include "graph.h"
26#include "command_parse.h"
27#include "vector.h"
28
73baf6a3
QY
29#define USAGE "usage: permutations <cmdstr>"
30
96dcc565 31void
73baf6a3 32permute (struct graph_node *);
96dcc565
QY
33
34int main (int argc, char *argv[])
35{
73baf6a3
QY
36 if (argc < 2)
37 {
38 fprintf(stdout, USAGE"\n");
39 exit(EXIT_SUCCESS);
40 }
41
96dcc565
QY
42 struct cmd_element *cmd = calloc (1, sizeof (struct cmd_element));
43 cmd->string = strdup(argv[1]);
44
45 struct graph *graph = graph_new();
46 struct cmd_token *token = new_cmd_token (START_TKN, NULL, NULL);
47 graph_new_node (graph, token, NULL);
48 command_parse_format (graph, cmd);
49
73baf6a3 50 permute (vector_slot (graph->nodes, 0));
96dcc565
QY
51}
52
96dcc565 53void
73baf6a3 54permute (struct graph_node *start)
96dcc565
QY
55{
56 static struct list *position = NULL;
57 if (!position) position = list_new ();
58
59 // recursive dfs
60 listnode_add (position, start);
61 for (unsigned int i = 0; i < vector_active (start->to); i++)
62 {
63 struct graph_node *gn = vector_slot (start->to, i);
64 struct cmd_token *tok = gn->data;
65 if (tok->type == END_TKN)
66 {
67 struct graph_node *gnn;
68 struct listnode *ln;
69 for (ALL_LIST_ELEMENTS_RO (position,ln,gnn))
70 {
71 struct cmd_token *tt = gnn->data;
72 if (tt->type < SELECTOR_TKN)
73 fprintf (stdout, "%s ", tt->text);
74 }
75 fprintf (stdout, "\n");
76 }
77 else
73baf6a3 78 permute (gn);
96dcc565
QY
79 }
80 list_delete_node (position, listtail(position));
81}