2 * Copyright (c) <2010>, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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
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.
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:
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.
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.
69 #include <netinet/in.h>
71 #include "cmdline_parse.h"
74 //#define CMDLINE_DEBUG
75 //#define debug_printf printf
76 #define debug_printf(args...) do {} while(0)
78 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
107 cmdline_isendoftoken(char c)
109 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
115 nb_common_chars(const char * s1, const char * s2)
119 while (*s1==*s2 && *s1 && *s2) {
127 /* quote a string and escape original quotes */
128 int cmdline_quote_token(char *dst, unsigned dstlen, const char *src)
130 unsigned s = 0, d = 0;
132 /* the 2 quotes + '\0' */
137 while (src[s] != '\0') {
143 if (src[s] == '\\' && src[s+1] == '"')
156 /* remove quote and stop when we reach the end of token */
157 // XXX ret val comment
158 int cmdline_unquote_token(char *dst, unsigned dstlen,
161 unsigned s = 0, d = 0;
164 while (src[s] != '\0') {
168 if (cmdline_isendoftoken(src[s]) && quoted == 0)
171 if (src[s] == '\\' && src[s+1] == '"') {
176 if (src[s] == '\\' && src[s+1] == '\\') {
199 * try to match the buffer with an instruction (only the first
200 * nb_match_token tokens if != 0). Return 0 if we match all the
201 * tokens, else the number of matched tokens, else -1.
204 match_inst(cmdline_parse_inst_t *inst, const char *buf,
205 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size)
207 unsigned int token_num=0;
208 cmdline_parse_token_hdr_t * token_p;
211 struct cmdline_token_hdr token_hdr;
214 token_p = inst->tokens[token_num];
216 memcpy(&token_hdr, token_p, sizeof(token_hdr));
218 /* check if we match all tokens of inst */
219 while (token_p && (!nb_match_token || i<nb_match_token)) {
220 debug_printf("TK\n");
222 while (isblank2(*buf)) {
227 if ( isendofline(*buf) || iscomment(*buf) )
230 n = cmdline_unquote_token(token_str, sizeof(token_str), buf);
235 res = token_hdr.ops->parse(token_p, token_str, NULL, 0);
238 void *rb = (char *)resbuf + token_hdr.offset;
239 if (token_hdr.offset > resbuf_size)
241 rb_sz = resbuf_size - token_hdr.offset;
242 res = token_hdr.ops->parse(token_p, token_str,
249 debug_printf("TK parsed (len=%d)\n", n);
254 token_p = inst->tokens[token_num];
256 memcpy(&token_hdr, token_p, sizeof(token_hdr));
263 /* in case we want to match a specific num of token */
264 if (nb_match_token) {
265 if (i == nb_match_token) {
271 /* we don't match all the tokens */
276 /* are there are some tokens more */
277 while (isblank2(*buf)) {
282 if ( isendofline(*buf) || iscomment(*buf) )
285 /* garbage after inst */
291 cmdline_parse(struct cmdline *cl, const char * buf)
293 unsigned int inst_num=0;
294 cmdline_parse_inst_t *inst;
296 char result_buf[CMDLINE_MAX_DSTBUF_SIZE];
297 void (*f)(void *, struct cmdline *, void *) = NULL;
302 int err = CMDLINE_PARSE_NOMATCH;
304 cmdline_parse_ctx_t *ctx = cl->ctx;
306 char debug_buf[BUFSIZ];
310 * - look if the buffer contains at least one line
311 * - look if line contains only spaces or comments
312 * - count line length
315 while (! isendofline(*curbuf)) {
316 if ( *curbuf == '\0' ) {
317 debug_printf("Incomplete buf (len=%d)\n", linelen);
320 if ( iscomment(*curbuf) ) {
323 if ( ! isblank2(*curbuf) && ! comment) {
330 /* skip all endofline chars */
331 while (isendofline(buf[linelen])) {
336 if ( parse_it == 0 ) {
337 debug_printf("Empty line (len=%d)\n", linelen);
342 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
343 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
347 inst = ctx[inst_num];
349 debug_printf("INST %d\n", inst_num);
352 tok = match_inst(inst, buf, 0, result_buf, sizeof(result_buf));
354 if (tok > 0) /* we matched at least one token */
355 err = CMDLINE_PARSE_BAD_ARGS;
358 debug_printf("INST fully parsed\n");
360 while (isblank2(*curbuf)) {
364 /* if end of buf -> there is no garbage after inst */
365 if (isendofline(*curbuf) || iscomment(*curbuf)) {
367 memcpy(&f, &inst->f, sizeof(f));
368 memcpy(&data, &inst->data, sizeof(data));
371 /* more than 1 inst matches */
372 err = CMDLINE_PARSE_AMBIGUOUS;
374 debug_printf("Ambiguous cmd\n");
379 else if (tok == -EINVAL) {
380 err = CMDLINE_PARSE_UNTERMINATED_QUOTE;
382 debug_printf("Unterminated quote\n");
387 inst = ctx[inst_num];
392 f(result_buf, cl, data);
397 debug_printf("No match err=%d\n", err);
405 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
406 char *dst, unsigned int size)
408 const char *partial_tok = buf;
409 unsigned int inst_num = 0;
410 cmdline_parse_inst_t *inst;
411 cmdline_parse_token_hdr_t *token_p;
412 struct cmdline_token_hdr token_hdr;
413 char tmpbuf[64], comp_buf[64];
414 unsigned int partial_tok_len;
419 unsigned int nb_completable;
420 unsigned int nb_non_completable;
423 cmdline_parse_ctx_t *ctx = cl->ctx;
425 debug_printf("%s called\n", __FUNCTION__);
426 memset(&token_hdr, 0, sizeof(token_hdr));
428 /* count the number of complete token to parse */
429 for (i=0 ; buf[i] ; i++) {
430 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
432 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
433 partial_tok = buf+i+1;
435 partial_tok_len = strlen(partial_tok);
437 /* first call -> do a first pass */
439 debug_printf("try complete <%s>\n", buf);
440 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
441 nb_token, partial_tok);
444 nb_non_completable = 0;
446 inst = ctx[inst_num];
448 /* parse the first tokens of the inst */
449 if (nb_token && match_inst(inst, buf, nb_token, NULL, 0))
452 debug_printf("instruction match \n");
453 token_p = inst->tokens[nb_token];
455 memcpy(&token_hdr, token_p, sizeof(token_hdr));
457 /* non completable */
459 !token_hdr.ops->complete_get_nb ||
460 !token_hdr.ops->complete_get_elt ||
461 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
462 nb_non_completable++;
466 debug_printf("%d choices for this token\n", n);
467 for (i=0 ; i<n ; i++) {
468 if (token_hdr.ops->complete_get_elt(token_p, i,
473 /* we have at least room for one char */
476 debug_printf(" choice <%s>\n", tmpbuf);
478 /* does the completion match the
479 * beginning of the word ? */
480 if (!strncmp(partial_tok, tmpbuf,
482 if (comp_len == -1) {
484 tmpbuf + partial_tok_len);
492 nb_common_chars(comp_buf,
493 tmpbuf+partial_tok_len);
494 comp_buf[comp_len] = 0;
501 inst = ctx[inst_num];
504 debug_printf("total choices %d for this completion\n",
507 /* no possible completion */
508 if (nb_completable == 0 && nb_non_completable == 0)
511 /* if multichoice is not required */
512 if (*state == 0 && partial_tok_len > 0) {
513 /* one or several choices starting with the
516 if ((unsigned)(comp_len + 1) > size)
519 strcpy(dst, comp_buf);
525 /* init state correctly */
529 debug_printf("Multiple choice STATE=%d\n", *state);
532 inst = ctx[inst_num];
534 /* we need to redo it */
535 inst = ctx[inst_num];
537 if (nb_token && match_inst(inst, buf, nb_token, NULL, 0))
540 token_p = inst->tokens[nb_token];
542 memcpy(&token_hdr, token_p, sizeof(token_hdr));
544 /* one choice for this token */
546 !token_hdr.ops->complete_get_nb ||
547 !token_hdr.ops->complete_get_elt ||
548 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
549 if (local_state < *state) {
554 if (token_p && token_hdr.ops->get_help) {
555 token_hdr.ops->get_help(token_p, tmpbuf,
557 help_str = inst->help_str;
559 snprintf(dst, size, "[%s]: %s", tmpbuf,
562 snprintf(dst, size, "[%s]: No help",
566 snprintf(dst, size, "[RETURN]");
571 /* several choices */
572 for (i=0 ; i<n ; i++) {
573 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
576 /* we have at least room for one char */
579 debug_printf(" choice <%s>\n", tmpbuf);
581 /* does the completion match the beginning of
583 if (!strncmp(partial_tok, tmpbuf,
585 if (local_state < *state) {
590 l=snprintf(dst, size, "%s", tmpbuf);
591 if (l>=0 && token_hdr.ops->get_help) {
592 token_hdr.ops->get_help(token_p, tmpbuf,
594 help_str = inst->help_str;
596 snprintf(dst+l, size-l, "[%s]: %s",
599 snprintf(dst+l, size-l,
600 "[%s]: No help", tmpbuf);
608 inst = ctx[inst_num];