1 /*
2  * Input matching routines for CLI backend.
3  *
4  * --
5  * Copyright (C) 2016 Cumulus Networks, Inc.
6  *
7  * This file is part of GNU Zebra.
8  *
9  * GNU Zebra is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; either version 2, or (at your option) any
12  * later version.
13  *
14  * GNU Zebra is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; see the file COPYING; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #ifndef _ZEBRA_COMMAND_MATCH_H
25 #define _ZEBRA_COMMAND_MATCH_H
26 
27 #include "graph.h"
28 #include "linklist.h"
29 #include "command.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /* These definitions exist in command.c in the current engine but should be
36  * relocated here in the new engine
37  */
38 enum cmd_filter_type { FILTER_RELAXED, FILTER_STRICT };
39 
40 /* matcher result value */
41 enum matcher_rv {
42 	MATCHER_NO_MATCH,
43 	MATCHER_INCOMPLETE,
44 	MATCHER_AMBIGUOUS,
45 	MATCHER_OK,
46 };
47 
48 /* completion match types */
49 enum match_type {
50 	trivial_match, // the input is null
51 	no_match,      // the input does not match
52 	partly_match,  // the input matches but is incomplete
53 	exact_match    // the input matches and is complete
54 };
55 
56 /* Defines which matcher_rv values constitute an error. Should be used with
57  * matcher_rv return values to do basic error checking.
58  */
59 #define MATCHER_ERROR(matcher_rv)                                              \
60 	((matcher_rv) == MATCHER_INCOMPLETE                                    \
61 	 || (matcher_rv) == MATCHER_NO_MATCH                                   \
62 	 || (matcher_rv) == MATCHER_AMBIGUOUS)
63 
64 /**
65  * Attempt to find an exact command match for a line of user input.
66  *
67  * @param[in] cmdgraph command graph to match against
68  * @param[in] vline vectorized input string
69  * @param[out] argv pointer to argument list if successful match, NULL
70  * otherwise. The elements of this list are pointers to struct cmd_token
71  * and represent the sequence of tokens matched by the inpu. The ->arg
72  * field of each token points to a copy of the input matched on it. These
73  * may be safely deleted or modified.
74  * @param[out] element pointer to matched cmd_element if successful match,
75  * or NULL when MATCHER_ERROR(rv) is true. The cmd_element may *not* be
76  * safely deleted or modified; it is the instance initialized on startup.
77  * @return matcher status
78  */
79 enum matcher_rv command_match(struct graph *cmdgraph, vector vline,
80 			      struct list **argv,
81 			      const struct cmd_element **element);
82 
83 /**
84  * Compiles possible completions for a given line of user input.
85  *
86  * @param[in] start the start node of the DFA to match against
87  * @param[in] vline vectorized input string
88  * @param[out] completions pointer to list of cmd_token representing
89  * acceptable next inputs, or NULL when MATCHER_ERROR(rv) is true.
90  * The elements of this list are pointers to struct cmd_token and take on a
91  * variety of forms depending on the passed vline. If the last element in vline
92  * is NULL, all previous elements are considered to be complete words (the case
93  * when a space is the last token of the line) and completions are generated
94  * based on what could follow that input. If the last element in vline is not
95  * NULL and each sequential element matches the corresponding tokens of one or
96  * more commands exactly (e.g. 'encapv4' and not 'en') the same result is
97  * generated. If the last element is not NULL and the best possible match is a
98  * partial match, then the result generated will be all possible continuations
99  * of that element (e.g. 'encapv4', 'encapv6', etc for input 'en').
100  * @return matcher status
101  */
102 enum matcher_rv command_complete(struct graph *cmdgraph, vector vline,
103 				 struct list **completions);
104 
105 #ifdef __cplusplus
106 }
107 #endif
108 
109 #endif /* _ZEBRA_COMMAND_MATCH_H */
110