4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
33 * version: DPDK.L.1.2.3-3
37 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
38 * All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions are met:
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * * Neither the name of the University of California, Berkeley nor the
48 * names of its contributors may be used to endorse or promote products
49 * derived from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
52 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
55 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 #ifndef _CMDLINE_PARSE_H_
64 #define _CMDLINE_PARSE_H_
71 #define offsetof(type, field) ((size_t) &( ((type *)0)->field) )
74 /* return status for parsing */
75 #define CMDLINE_PARSE_SUCCESS 0
76 #define CMDLINE_PARSE_AMBIGUOUS -1
77 #define CMDLINE_PARSE_NOMATCH -2
78 #define CMDLINE_PARSE_BAD_ARGS -3
80 /* return status for completion */
81 #define CMDLINE_PARSE_COMPLETE_FINISHED 0
82 #define CMDLINE_PARSE_COMPLETE_AGAIN 1
83 #define CMDLINE_PARSE_COMPLETED_BUFFER 2
86 * Stores a pointer to the ops struct, and the offset: the place to
87 * write the parsed result in the destination structure.
89 struct cmdline_token_hdr {
90 struct cmdline_token_ops *ops;
93 typedef struct cmdline_token_hdr cmdline_parse_token_hdr_t;
96 * A token is defined by this structure.
98 * parse() takes the token as first argument, then the source buffer
99 * starting at the token we want to parse. The 3rd arg is a pointer
100 * where we store the parsed data (as binary). It returns the number of
101 * parsed chars on success and a negative value on error.
103 * complete_get_nb() returns the number of possible values for this
104 * token if completion is possible. If it is NULL or if it returns 0,
105 * no completion is possible.
107 * complete_get_elt() copy in dstbuf (the size is specified in the
108 * parameter) the i-th possible completion for this token. returns 0
109 * on success or and a negative value on error.
111 * get_help() fills the dstbuf with the help for the token. It returns
112 * -1 on error and 0 on success.
114 struct cmdline_token_ops {
115 /** parse(token ptr, buf, res pts) */
116 int (*parse)(cmdline_parse_token_hdr_t *, const char *, void *);
117 /** return the num of possible choices for this token */
118 int (*complete_get_nb)(cmdline_parse_token_hdr_t *);
119 /** return the elt x for this token (token, idx, dstbuf, size) */
120 int (*complete_get_elt)(cmdline_parse_token_hdr_t *, int, char *, unsigned int);
121 /** get help for this token (token, dstbuf, size) */
122 int (*get_help)(cmdline_parse_token_hdr_t *, char *, unsigned int);
127 * Store a instruction, which is a pointer to a callback function and
128 * its parameter that is called when the instruction is parsed, a help
129 * string, and a list of token composing this instruction.
131 struct cmdline_inst {
132 /* f(parsed_struct, data) */
133 void (*f)(void *, struct cmdline *, void *);
135 const char *help_str;
136 cmdline_parse_token_hdr_t *tokens[];
138 typedef struct cmdline_inst cmdline_parse_inst_t;
141 * A context is identified by its name, and contains a list of
145 typedef cmdline_parse_inst_t *cmdline_parse_ctx_t;
148 * Try to parse a buffer according to the specified context. The
149 * argument buf must ends with "\n\0". The function returns
150 * CMDLINE_PARSE_AMBIGUOUS, CMDLINE_PARSE_NOMATCH or
151 * CMDLINE_PARSE_BAD_ARGS on error. Else it calls the associated
152 * function (defined in the context) and returns 0
153 * (CMDLINE_PARSE_SUCCESS).
155 int cmdline_parse(struct cmdline *cl, const char *buf);
158 * complete() must be called with *state==0 (try to complete) or
159 * with *state==-1 (just display choices), then called without
160 * modifying *state until it returns CMDLINE_PARSE_COMPLETED_BUFFER or
161 * CMDLINE_PARSE_COMPLETED_BUFFER.
163 * It returns < 0 on error.
166 * - CMDLINE_PARSE_COMPLETED_BUFFER on completion (one possible
167 * choice). In this case, the chars are appended in dst buffer.
168 * - CMDLINE_PARSE_COMPLETE_AGAIN if there is several possible
169 * choices. In this case, you must call the function again,
170 * keeping the value of state intact.
171 * - CMDLINE_PARSE_COMPLETED_BUFFER when the iteration is
172 * finished. The dst is not valid for this last call.
174 * The returned dst buf ends with \0.
176 int cmdline_complete(struct cmdline *cl, const char *buf, int *state,
177 char *dst, unsigned int size);
180 /* return true if(!c || iscomment(c) || isblank(c) ||
182 int cmdline_isendoftoken(char c);
188 #endif /* _CMDLINE_PARSE_H_ */