5112ab5c9bc04fc3a350ab72545eb05ba209850d
[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 an opaque
95  * pointer that will be given to complete_iterate() function. It can
96  * be used to store private data for this completion. For each
97  * complete_start() call, the user must call complete_end() at the end
98  * of iterations (if defined). Return a negative value if completion
99  * is not possible, or 0 on success.
100  *
101  * complete_iterate() copy in dstbuf (the size is specified in the
102  * parameter) the next possible completion for this token. Return 0 on
103  * success or a negative value on error (or when there is no more
104  * completion). Refer to cmdline_complete_string_iterate() for an
105  * example.
106  *
107  * complete_end() is called when the iteration on this token is finished,
108  * this function should free all things allocated during complete_start().
109  *
110  * help() fills the dstbuf with the help for the token. It returns
111  * -1 on error and 0 on success.
112  */
113 struct cmdline_token_ops {
114         /** parse(token ptr, buf, res pts) */
115         int (*parse)(cmdline_parse_token_hdr_t *, const char *, void *,
116                      unsigned int);
117         /** prepare a completion on this token */
118         int (*complete_start)(cmdline_parse_token_hdr_t *, void **);
119         /** fill dstbuf for this token (token, opaque, dstbuf, size) */
120         int (*complete_iterate)(cmdline_parse_token_hdr_t *, void **, char *,
121                                 unsigned int);
122         /* end of completion, used to free the opaque structure */
123         void (*complete_end)(cmdline_parse_token_hdr_t *, void **);
124         /** get help for this token (token, dstbuf, size) */
125         int (*help)(cmdline_parse_token_hdr_t *, char *, unsigned int);
126 };
127
128 struct cmdline;
129 /**
130  * Store a instruction, which is a pointer to a callback function and
131  * its parameter that is called when the instruction is parsed, a help
132  * string, and a list of token composing this instruction.
133  */
134 struct cmdline_inst {
135         /* f(parsed_struct, data) */
136         void (*f)(void *, struct cmdline *, void *);
137         void *data;
138         char *help_str;
139         cmdline_parse_token_hdr_t *tokens[];
140 };
141 typedef struct cmdline_inst cmdline_parse_inst_t;
142
143 /**
144  * A context is identified by its name, and contains a list of
145  * instruction
146  *
147  */
148 struct cmdline_parse_ctx {
149         const char *name;
150         cmdline_parse_inst_t *insts[];
151 };
152 typedef struct cmdline_parse_ctx cmdline_parse_ctx_t;
153
154 /* return status for parsing */
155 #define CMDLINE_PARSE_SUCCESS        0
156 #define CMDLINE_PARSE_EMPTY         -1
157 #define CMDLINE_PARSE_NOMATCH       -2
158 #define CMDLINE_PARSE_AMBIGUOUS     -3
159 #define CMDLINE_PARSE_UNTERMINATED_QUOTE -4
160
161 /**
162  * Try to parse a buffer according to the specified context. The
163  * argument linebuf must end with "\n\0".
164  *
165  * The function returns:
166  * - CMDLINE_PARSE_SUCCESS (0) on success
167  * - CMDLINE_PARSE_EMPTY if there is nothing to parse
168  * - CMDLINE_PARSE_NOMATCH if line does not match any command
169  * - CMDLINE_PARSE_AMBIGUOUS if several commands match
170  * - CMDLINE_PARSE_UNTERMINATED_QUOTE if a quote is used incorrectly
171  */
172 int cmdline_parse(cmdline_parse_ctx_t *ctx, const char *linebuf, void *opaque);
173
174 /* return status for completion */
175 #define CMDLINE_COMPLETE_APPEND    0
176 #define CMDLINE_COMPLETE_NONE     -1
177 #define CMDLINE_COMPLETE_MANY     -2
178
179 /**
180  * cmdline_complete() tries to complete the buffer given as a parameter.
181  *
182  * It returns:
183  *   - CMDLINE_COMPLETE_APPEND (0) on success, when a completion is
184  *     done (one possible choice). In this case, the chars are
185  *     appended in dst buffer.
186  *   - CMDLINE_COMPLETE_NONE: error, no possible completion
187  *   - CMDLINE_COMPLETE_MANY: error, many possble completions, need to call
188  *     cmdline_help() function to see all the possibilities.
189  */
190 int cmdline_complete(cmdline_parse_ctx_t *ctx, const char *buf,
191                      char *dst, unsigned int size);
192
193 /**
194  * callback given to rdline_help() to display the content of the
195  * help. The first argument is an opaque pointer. The other args
196  * are buffer and size.
197  */
198 typedef ssize_t (cmdline_write_t)(void *, void *, size_t);
199
200 /**
201  * Display a contextual help using the write_buf() function pointer
202  * given as parameter (called with its opaque pointer). The contextual
203  * help depends on the buffer given.
204  */
205 int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
206                  cmdline_write_t *write_buf, void *opaque);
207
208 /* return true if(!c || iscomment(c) || isblank(c) ||
209  * isendofline(c)) */
210 int cmdline_isendoftoken(char c);
211
212 /* quote a string and escape original quotes */
213 int cmdline_quote_token(char *dst, unsigned dstlen, const char *src);
214
215 /* remove quote and stop when we reach the end of token */
216 int cmdline_unquote_token(char *dst, unsigned dstlen, const char *src);
217
218 #endif /* _CMDLINE_PARSE_H_ */