2 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <netinet/in.h>
35 #include "cmdline_parse.h"
38 //#define CMDLINE_DEBUG
39 //#define debug_printf printf
40 #define debug_printf(args...) do {} while(0)
42 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
71 cmdline_isendoftoken(char c)
73 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
79 nb_common_chars(const char * s1, const char * s2)
83 while (*s1==*s2 && *s1 && *s2) {
92 * try to match the buffer with an instruction (only the first
93 * nb_match_token tokens if != 0). Return 0 if we match all the
94 * tokens, else the number of matched tokens, else -1.
97 match_inst(cmdline_parse_inst_t *inst, const char *buf, unsigned int nb_match_token,
100 unsigned int token_num=0;
101 cmdline_parse_token_hdr_t * token_p;
104 struct cmdline_token_hdr token_hdr;
106 token_p = inst->tokens[token_num];
108 memcpy(&token_hdr, token_p, sizeof(token_hdr));
110 /* check if we match all tokens of inst */
111 while (token_p && (!nb_match_token || i<nb_match_token)) {
112 debug_printf("TK\n");
114 while (isblank2(*buf)) {
119 if ( isendofline(*buf) || iscomment(*buf) )
122 n = token_hdr.ops->parse(token_p, buf, (result_buf ? result_buf+token_hdr.offset : NULL));
125 debug_printf("TK parsed (len=%d)\n", n);
130 token_p = inst->tokens[token_num];
132 memcpy(&token_hdr, token_p, sizeof(token_hdr));
139 /* in case we want to match a specific num of token */
140 if (nb_match_token) {
141 if (i == nb_match_token) {
147 /* we don't match all the tokens */
152 /* are there are some tokens more */
153 while (isblank2(*buf)) {
158 if ( isendofline(*buf) || iscomment(*buf) )
161 /* garbage after inst */
167 cmdline_parse(struct cmdline *cl, const char * buf)
169 unsigned int inst_num=0;
170 cmdline_parse_inst_t *inst;
172 char result_buf[BUFSIZ]; /* XXX align, size zé in broblém */
173 void (*f)(void *, struct cmdline *, void *) = NULL;
178 int err = CMDLINE_PARSE_NOMATCH;
180 cmdline_parse_ctx_t *ctx = cl->ctx;
182 char debug_buf[BUFSIZ];
186 * - look if the buffer contains at least one line
187 * - look if line contains only spaces or comments
188 * - count line length
191 while (! isendofline(*curbuf)) {
192 if ( *curbuf == '\0' ) {
193 debug_printf("Incomplete buf (len=%d)\n", linelen);
196 if ( iscomment(*curbuf) ) {
199 if ( ! isblank2(*curbuf) && ! comment) {
206 /* skip all endofline chars */
207 while (isendofline(buf[linelen])) {
212 if ( parse_it == 0 ) {
213 debug_printf("Empty line (len=%d)\n", linelen);
218 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
219 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
223 inst = ctx[inst_num];
225 debug_printf("INST %d\n", inst_num);
228 tok = match_inst(inst, buf, 0, result_buf);
230 if (tok > 0) /* we matched at least one token */
231 err = CMDLINE_PARSE_BAD_ARGS;
234 debug_printf("INST fully parsed\n");
236 while (isblank2(*curbuf)) {
240 /* if end of buf -> there is no garbage after inst */
241 if (isendofline(*curbuf) || iscomment(*curbuf)) {
243 memcpy(&f, &inst->f, sizeof(f));
244 memcpy(&data, &inst->data, sizeof(data));
247 /* more than 1 inst matches */
248 err = CMDLINE_PARSE_AMBIGUOUS;
250 debug_printf("Ambiguous cmd\n");
257 inst = ctx[inst_num];
262 f(result_buf, cl, data);
267 debug_printf("No match err=%d\n", err);
275 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
276 char *dst, unsigned int size)
278 const char *incomplete_token = buf;
279 unsigned int inst_num = 0;
280 cmdline_parse_inst_t *inst;
281 cmdline_parse_token_hdr_t *token_p;
282 struct cmdline_token_hdr token_hdr;
283 char tmpbuf[64], completion_buf[64];
284 unsigned int incomplete_token_len;
285 int completion_len = -1;
289 unsigned int nb_completable;
290 unsigned int nb_non_completable;
293 cmdline_parse_ctx_t *ctx = cl->ctx;
295 debug_printf("%s called\n", __FUNCTION__);
296 memset(&token_hdr, 0, sizeof(token_hdr));
298 /* count the number of complete token to parse */
299 for (i=0 ; buf[i] ; i++) {
300 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
302 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
303 incomplete_token = buf+i+1;
305 incomplete_token_len = strlen(incomplete_token);
307 /* first call -> do a first pass */
309 debug_printf("try complete <%s>\n", buf);
310 debug_printf("there is %d complete tokens, <%s> is incomplete\n", nb_token, incomplete_token);
313 nb_non_completable = 0;
315 inst = ctx[inst_num];
317 /* parse the first tokens of the inst */
318 if (nb_token && match_inst(inst, buf, nb_token, NULL))
321 debug_printf("instruction match \n");
322 token_p = inst->tokens[nb_token];
324 memcpy(&token_hdr, token_p, sizeof(token_hdr));
326 /* non completable */
328 !token_hdr.ops->complete_get_nb ||
329 !token_hdr.ops->complete_get_elt ||
330 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
331 nb_non_completable++;
335 debug_printf("%d choices for this token\n", n);
336 for (i=0 ; i<n ; i++) {
337 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
339 strcat(tmpbuf, " "); /* we have at least room for one char */
340 debug_printf(" choice <%s>\n", tmpbuf);
341 /* does the completion match the beginning of the word ? */
342 if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
343 if (completion_len == -1) {
344 strcpy(completion_buf, tmpbuf+incomplete_token_len);
345 completion_len = strlen(tmpbuf+incomplete_token_len);
349 completion_len = nb_common_chars(completion_buf,
350 tmpbuf+incomplete_token_len);
351 completion_buf[completion_len] = 0;
358 inst = ctx[inst_num];
361 debug_printf("total choices %d for this completion\n", nb_completable);
363 /* no possible completion */
364 if (nb_completable == 0 && nb_non_completable == 0)
367 /* if multichoice is not required */
368 if (*state == 0 && incomplete_token_len > 0) {
369 /* one or several choices starting with the
371 if (completion_len > 0) {
372 if ((unsigned)(completion_len + 1) > size)
375 strcpy(dst, completion_buf);
381 /* init state correctly */
385 debug_printf("Multiple choice STATE=%d\n", *state);
388 inst = ctx[inst_num];
390 /* we need to redo it */
391 inst = ctx[inst_num];
393 if (nb_token && match_inst(inst, buf, nb_token, NULL))
396 token_p = inst->tokens[nb_token];
398 memcpy(&token_hdr, token_p, sizeof(token_hdr));
400 /* one choice for this token */
402 !token_hdr.ops->complete_get_nb ||
403 !token_hdr.ops->complete_get_elt ||
404 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
405 if (local_state < *state) {
410 if (token_p && token_hdr.ops->get_help) {
411 token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
412 help_str = inst->help_str;
414 snprintf(dst, size, "[%s]: %s", tmpbuf, help_str);
416 snprintf(dst, size, "[%s]: No help", tmpbuf);
419 snprintf(dst, size, "[RETURN]");
424 /* several choices */
425 for (i=0 ; i<n ; i++) {
426 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
428 strcat(tmpbuf, " "); /* we have at least room for one char */
429 debug_printf(" choice <%s>\n", tmpbuf);
430 /* does the completion match the beginning of the word ? */
431 if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
432 if (local_state < *state) {
437 l=snprintf(dst, size, "%s", tmpbuf);
438 if (l>=0 && token_hdr.ops->get_help) {
439 token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
440 help_str = inst->help_str;
442 snprintf(dst+l, size-l, "[%s]: %s", tmpbuf, help_str);
444 snprintf(dst+l, size-l, "[%s]: No help", tmpbuf);
452 inst = ctx[inst_num];