]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/dpdk/lib/librte_cmdline/cmdline_parse.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / dpdk / lib / librte_cmdline / cmdline_parse.h
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
36 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * * Neither the name of the University of California, Berkeley nor the
46 * names of its contributors may be used to endorse or promote products
47 * derived from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
50 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
53 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 #ifndef _CMDLINE_PARSE_H_
62 #define _CMDLINE_PARSE_H_
63
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67
68 #ifndef offsetof
69 #define offsetof(type, field) ((size_t) &( ((type *)0)->field) )
70 #endif
71
72 /* return status for parsing */
73 #define CMDLINE_PARSE_SUCCESS 0
74 #define CMDLINE_PARSE_AMBIGUOUS -1
75 #define CMDLINE_PARSE_NOMATCH -2
76 #define CMDLINE_PARSE_BAD_ARGS -3
77
78 /* return status for completion */
79 #define CMDLINE_PARSE_COMPLETE_FINISHED 0
80 #define CMDLINE_PARSE_COMPLETE_AGAIN 1
81 #define CMDLINE_PARSE_COMPLETED_BUFFER 2
82
83 /* maximum buffer size for parsed result */
84 #define CMDLINE_PARSE_RESULT_BUFSIZE 8192
85
86 /* maximum number of dynamic tokens */
87 #define CMDLINE_PARSE_DYNAMIC_TOKENS 128
88
89 /**
90 * Stores a pointer to the ops struct, and the offset: the place to
91 * write the parsed result in the destination structure.
92 */
93 struct cmdline_token_hdr {
94 struct cmdline_token_ops *ops;
95 unsigned int offset;
96 };
97 typedef struct cmdline_token_hdr cmdline_parse_token_hdr_t;
98
99 /**
100 * A token is defined by this structure.
101 *
102 * parse() takes the token as first argument, then the source buffer
103 * starting at the token we want to parse. The 3rd arg is a pointer
104 * where we store the parsed data (as binary). It returns the number of
105 * parsed chars on success and a negative value on error.
106 *
107 * complete_get_nb() returns the number of possible values for this
108 * token if completion is possible. If it is NULL or if it returns 0,
109 * no completion is possible.
110 *
111 * complete_get_elt() copy in dstbuf (the size is specified in the
112 * parameter) the i-th possible completion for this token. returns 0
113 * on success or and a negative value on error.
114 *
115 * get_help() fills the dstbuf with the help for the token. It returns
116 * -1 on error and 0 on success.
117 */
118 struct cmdline_token_ops {
119 /** parse(token ptr, buf, res pts, buf len) */
120 int (*parse)(cmdline_parse_token_hdr_t *, const char *, void *,
121 unsigned int);
122 /** return the num of possible choices for this token */
123 int (*complete_get_nb)(cmdline_parse_token_hdr_t *);
124 /** return the elt x for this token (token, idx, dstbuf, size) */
125 int (*complete_get_elt)(cmdline_parse_token_hdr_t *, int, char *,
126 unsigned int);
127 /** get help for this token (token, dstbuf, size) */
128 int (*get_help)(cmdline_parse_token_hdr_t *, char *, unsigned int);
129 };
130
131 struct cmdline;
132 /**
133 * Store a instruction, which is a pointer to a callback function and
134 * its parameter that is called when the instruction is parsed, a help
135 * string, and a list of token composing this instruction.
136 *
137 * When no tokens are defined (tokens[0] == NULL), they are retrieved
138 * dynamically by calling f() as follows:
139 *
140 * f((struct cmdline_token_hdr **)&token_hdr,
141 * NULL,
142 * (struct cmdline_token_hdr *[])tokens));
143 *
144 * The address of the resulting token is expected at the location pointed by
145 * the first argument. Can be set to NULL to end the list.
146 *
147 * The cmdline argument (struct cmdline *) is always NULL.
148 *
149 * The last argument points to the NULL-terminated list of dynamic tokens
150 * defined so far. Since token_hdr points to an index of that list, the
151 * current index can be derived as follows:
152 *
153 * int index = token_hdr - &(*tokens)[0];
154 */
155 struct cmdline_inst {
156 /* f(parsed_struct, data) */
157 void (*f)(void *, struct cmdline *, void *);
158 void *data;
159 const char *help_str;
160 cmdline_parse_token_hdr_t *tokens[];
161 };
162 typedef struct cmdline_inst cmdline_parse_inst_t;
163
164 /**
165 * A context is identified by its name, and contains a list of
166 * instruction
167 *
168 */
169 typedef cmdline_parse_inst_t *cmdline_parse_ctx_t;
170
171 /**
172 * Try to parse a buffer according to the specified context. The
173 * argument buf must ends with "\n\0". The function returns
174 * CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
175 * CMDLINE_PARSE_BAD_ARGS on error. Else it calls the associated
176 * function (defined in the context) and returns 0
177 * (CMDLINE_PARSE_SUCCESS).
178 */
179 int cmdline_parse(struct cmdline *cl, const char *buf);
180
181 /**
182 * complete() must be called with *state==0 (try to complete) or
183 * with *state==-1 (just display choices), then called without
184 * modifying *state until it returns CMDLINE_PARSE_COMPLETED_BUFFER or
185 * CMDLINE_PARSE_COMPLETED_BUFFER.
186 *
187 * It returns < 0 on error.
188 *
189 * Else it returns:
190 * - CMDLINE_PARSE_COMPLETED_BUFFER on completion (one possible
191 * choice). In this case, the chars are appended in dst buffer.
192 * - CMDLINE_PARSE_COMPLETE_AGAIN if there is several possible
193 * choices. In this case, you must call the function again,
194 * keeping the value of state intact.
195 * - CMDLINE_PARSE_COMPLETED_BUFFER when the iteration is
196 * finished. The dst is not valid for this last call.
197 *
198 * The returned dst buf ends with \0.
199 */
200 int cmdline_complete(struct cmdline *cl, const char *buf, int *state,
201 char *dst, unsigned int size);
202
203
204 /* return true if(!c || iscomment(c) || isblank(c) ||
205 * isendofline(c)) */
206 int cmdline_isendoftoken(char c);
207
208 /* return true if(!c || iscomment(c) || isendofline(c)) */
209 int cmdline_isendofcommand(char c);
210
211 #ifdef __cplusplus
212 }
213 #endif
214
215 #endif /* _CMDLINE_PARSE_H_ */