cmdline: add the incomplete token string as an argument of iter_start()
[libcmdline.git] / src / lib / cmdline_parse.h
1 /*-
2  * Copyright (c) <2010>, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in
14  *   the documentation and/or other materials provided with the
15  *   distribution.
16  *
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
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32  * OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 /*
36  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
37  * All rights reserved.
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions are met:
40  *
41  *     * Redistributions of source code must retain the above copyright
42  *       notice, this list of conditions and the following disclaimer.
43  *     * Redistributions in binary form must reproduce the above copyright
44  *       notice, this list of conditions and the following disclaimer in the
45  *       documentation and/or other materials provided with the distribution.
46  *     * Neither the name of the University of California, Berkeley nor the
47  *       names of its contributors may be used to endorse or promote products
48  *       derived from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
51  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
54  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60  */
61
62 #ifndef _CMDLINE_PARSE_H_
63 #define _CMDLINE_PARSE_H_
64
65 #include <sys/types.h>
66
67 #ifndef offsetof
68 #define offsetof(type, field)  ((size_t) &( ((type *)0)->field) )
69 #endif
70
71 #define CMDLINE_MAX_TOKEN_SIZE 128 /* including '\0' */
72 #define CMDLINE_MAX_DSTBUF_SIZE 1024
73
74 /**
75  * Stores a pointer to the ops struct, and the offset: the place to
76  * write the parsed result in the destination structure.
77  */
78 struct cmdline_token_hdr {
79         struct cmdline_token_ops *ops;
80         unsigned int offset;
81 };
82 typedef struct cmdline_token_hdr cmdline_parse_token_hdr_t;
83
84 /**
85  * A token is defined by this structure.
86  *
87  * parse() takes the token as first argument, then the source buffer
88  * containing the token we want to parse. The 3rd arg is a pointer
89  * where we store the parsed data (as binary), and the 4th arg is the
90  * size of this area. It returns 0 on success and a negative value on
91  * error.
92  *
93  * complete_start() prepares a completion operation. The first
94  * argument is the token to complete. The second argument is the token
95  * to complete, and the third arg is an opaque pointer that will be
96  * given to complete_iterate() function. It can be used to store
97  * private data for this completion. For each complete_start() call,
98  * the user must call complete_end() at the end of iterations (if
99  * defined). Return a negative value if completion is not possible, or
100  * 0 on success.
101  *
102  * complete_iterate() copy in dstbuf (the size is specified in the
103  * parameter) the next possible completion for this token. Return 0 on
104  * success or a negative value on error (or when there is no more
105  * completion). Refer to cmdline_complete_string_iterate() for an
106  * example.
107  *
108  * complete_end() is called when the iteration on this token is finished,
109  * this function should free all things allocated during complete_start().
110  *
111  * help() fills the dstbuf with the help for the token. It returns
112  * -1 on error and 0 on success.
113  */
114 struct cmdline_token_ops {
115         /** parse(token ptr, buf, res pts) */
116         int (*parse)(cmdline_parse_token_hdr_t *, const char *, void *,
117                      unsigned int);
118         /** prepare a completion on this token */
119         int (*complete_start)(cmdline_parse_token_hdr_t *, const char *,
120                               void **);
121         /** fill dstbuf for this token (token, opaque, dstbuf, size) */
122         int (*complete_iterate)(cmdline_parse_token_hdr_t *, void **, char *,
123                                 unsigned int);
124         /* end of completion, used to free the opaque structure */
125         void (*complete_end)(cmdline_parse_token_hdr_t *, void **);
126         /** get help for this token (token, dstbuf, size) */
127         int (*help)(cmdline_parse_token_hdr_t *, char *, unsigned int);
128 };
129
130 struct cmdline;
131 /**
132  * Store a instruction, which is a pointer to a callback function and
133  * its parameter that is called when the instruction is parsed, a help
134  * string, and a list of token composing this instruction.
135  */
136 struct cmdline_inst {
137         /* f(parsed_struct, data) */
138         void (*f)(void *, struct cmdline *, void *);
139         void *data;
140         char *help_str;
141         cmdline_parse_token_hdr_t *tokens[];
142 };
143 typedef struct cmdline_inst cmdline_parse_inst_t;
144
145 /**
146  * A context is identified by its name, and contains a list of
147  * instruction
148  *
149  */
150 struct cmdline_parse_ctx {
151         const char *name;
152         cmdline_parse_inst_t *insts[];
153 };
154 typedef struct cmdline_parse_ctx cmdline_parse_ctx_t;
155
156 /* return status for parsing */
157 #define CMDLINE_PARSE_SUCCESS        0
158 #define CMDLINE_PARSE_EMPTY         -1
159 #define CMDLINE_PARSE_NOMATCH       -2
160 #define CMDLINE_PARSE_AMBIGUOUS     -3
161 #define CMDLINE_PARSE_UNTERMINATED_QUOTE -4
162
163 /**
164  * Try to parse a buffer according to the specified context. The
165  * argument linebuf must end with "\n\0".
166  *
167  * The function returns:
168  * - CMDLINE_PARSE_SUCCESS (0) on success
169  * - CMDLINE_PARSE_EMPTY if there is nothing to parse
170  * - CMDLINE_PARSE_NOMATCH if line does not match any command
171  * - CMDLINE_PARSE_AMBIGUOUS if several commands match
172  * - CMDLINE_PARSE_UNTERMINATED_QUOTE if a quote is used incorrectly
173  */
174 int cmdline_parse(cmdline_parse_ctx_t *ctx, const char *linebuf, void *opaque);
175
176 /* return status for completion */
177 #define CMDLINE_COMPLETE_APPEND    0
178 #define CMDLINE_COMPLETE_NONE     -1
179 #define CMDLINE_COMPLETE_MANY     -2
180
181 /**
182  * cmdline_complete() tries to complete the buffer given as a parameter.
183  *
184  * It returns:
185  *   - CMDLINE_COMPLETE_APPEND (0) on success, when a completion is
186  *     done (one possible choice). In this case, the chars are
187  *     appended in dst buffer.
188  *   - CMDLINE_COMPLETE_NONE: error, no possible completion
189  *   - CMDLINE_COMPLETE_MANY: error, many possble completions, need to call
190  *     cmdline_help() function to see all the possibilities.
191  */
192 int cmdline_complete(cmdline_parse_ctx_t *ctx, const char *buf,
193                      char *dst, unsigned int size);
194
195 /**
196  * callback given to rdline_help() to display the content of the
197  * help. The first argument is an opaque pointer. The other args
198  * are buffer and size.
199  */
200 typedef ssize_t (cmdline_write_t)(void *, void *, size_t);
201
202 /**
203  * Display a contextual help using the write_buf() function pointer
204  * given as parameter (called with its opaque pointer). The contextual
205  * help depends on the buffer given.
206  */
207 int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
208                  cmdline_write_t *write_buf, void *opaque);
209
210 /* return true if(!c || iscomment(c) || isblank(c) ||
211  * isendofline(c)) */
212 int cmdline_isendoftoken(char c);
213
214 /* quote a string and escape original quotes */
215 int cmdline_quote_token(char *dst, unsigned dstlen, const char *src);
216
217 /* remove quote and stop when we reach the end of token */
218 int cmdline_unquote_token(char *dst, unsigned dstlen, const char *src);
219
220 #endif /* _CMDLINE_PARSE_H_ */