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.
70 #include <netinet/in.h>
72 #include "cmdline_parse.h"
75 //#define debug_printf printf
76 #define debug_printf(args...) do {} while(0)
78 /* used internally for cmdline_help() and cmdline_complete() */
79 struct cmdline_preparse {
80 int nb_valid_tok; /* number of valid tokens in the buffer */
81 void *opaque; /* pointer to opaque data */
82 char comp_tok_buf[CMDLINE_MAX_TOKEN_SIZE]; /* token to complete */
83 int comp_tok_len; /* length of the token to complete */
84 int comp_tok_offset; /* offset of token to complete in the line buf */
87 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
116 cmdline_isendoftoken(char c)
118 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
124 nb_common_chars(const char * s1, const char * s2)
128 while (*s1==*s2 && *s1 && *s2) {
136 /* quote a string and escape original quotes */
137 int cmdline_quote_token(char *dst, unsigned dstlen, const char *src)
139 unsigned s = 0, d = 0;
141 /* the 2 quotes + '\0' */
146 while (src[s] != '\0') {
152 if (src[s] == '\\' && src[s+1] == '"')
165 /* Remove quote and stop when we reach the end of token. Return the
166 * number of "eaten" bytes from the source buffer, or a negative value
168 int cmdline_get_token(char *dst, unsigned dstlen, const char *src)
170 unsigned s = 0, d = 0;
174 while (isblank2(src[s]))
178 if (cmdline_isendoftoken(src[s]))
181 /* copy token and remove quotes */
182 while (src[s] != '\0') {
186 if (cmdline_isendoftoken(src[s]) && quoted == 0)
189 if (src[s] == '\\' && src[s+1] == '"') {
194 if (src[s] == '\\' && src[s+1] == '\\') {
207 /* not enough room in dst buffer */
211 /* end of string during quote */
219 /* return the nth token from src and copy it in dst. Return the offset
220 * of the token in src, or a negative value on error. Note: the index
221 * of the first token is 0. */
222 static int cmdline_get_nth_token(char *dst, unsigned dstlen, int n,
225 int ret = 0, offset = 0;
231 while (isblank2(src[offset]))
234 /* get the token starting at offset */
235 ret = cmdline_get_token(dst, dstlen, src + offset);
245 * try to match the buffer with an instruction (only the first
246 * nb_match_token tokens if != 0). Return 0 if we match all the
247 * tokens, else the number of matched tokens, else -1.
253 match_inst(cmdline_parse_inst_t *inst, const char *linebuf,
254 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size)
256 unsigned int token_num = 0;
257 cmdline_parse_token_hdr_t *token;
259 char token_str[CMDLINE_MAX_TOKEN_SIZE];
261 token = inst->tokens[token_num];
263 /* check if we match all tokens of inst */
266 /* we matched enough tokens, return success */
267 if (nb_match_token != 0 && token_num >= nb_match_token)
270 debug_printf("TK\n");
272 /* copy token and remove quotes */
273 n = cmdline_get_token(token_str, sizeof(token_str), linebuf);
277 /* parse this token */
279 res = token->ops->parse(token, token_str, NULL, 0);
282 void *rb = (char *)resbuf + token->offset;
284 /* not enough room to store result */
285 if (token->offset > resbuf_size)
288 rb_sz = resbuf_size - token->offset;
289 res = token->ops->parse(token, token_str, rb, rb_sz);
292 /* does not match this token */
296 debug_printf("TK parsed (len=%d)\n", n);
299 token = inst->tokens[token_num];
306 /* we don't match all the tokens */
310 /* are there are some tokens more */
311 while (isblank2(*linebuf))
314 /* end of buf, we match all inst */
315 if (isendofline(*linebuf) || iscomment(*linebuf))
318 /* garbage after inst */
323 /* Check if a line buffer is valid and can be parsed or completed. The
324 * parsing stops when \n or \0 is reached. The also function checks
325 * that tokens are correctly quoted. The number of tokens in the
326 * buffer is returned. */
327 static int cmdline_validate_linebuf(const char *linebuf)
329 int quoted = 0, comment = 0;
330 int i = 0, nbtok = 0, token = 0;
332 while (linebuf[i] != '\0') {
333 if (isendofline(linebuf[i]) && quoted == 0)
339 if (iscomment(linebuf[i]) && quoted == 0) {
346 if (isblank2(linebuf[i]) && quoted == 0)
349 if (!isblank2(linebuf[i]) && token == 0) {
354 if (linebuf[i] == '\\' && linebuf[i+1] == '"') {
358 if (linebuf[i] == '\\' && linebuf[i+1] == '\\') {
362 if (linebuf[i] == '"') {
370 return CMDLINE_PARSE_UNTERMINATED_QUOTE;
374 /* Try to parse a buffer according to the specified context. The
375 * argument linebuf must end with \n or \0. */
377 cmdline_parse(cmdline_parse_ctx_t *ctx, const char *linebuf, void *opaque)
379 cmdline_parse_inst_t **pinst;
380 cmdline_parse_inst_t *inst;
381 char result_buf[CMDLINE_MAX_DSTBUF_SIZE];
382 void (*f)(void *, struct cmdline *, void *) = NULL;
386 ret = cmdline_validate_linebuf(linebuf);
390 return CMDLINE_PARSE_EMPTY;
394 for (pinst = &ctx->insts[0]; *pinst != NULL; pinst++) {
396 debug_printf("INST\n");
399 ret = match_inst(inst, linebuf, 0, result_buf, sizeof(result_buf));
404 debug_printf("INST fully parsed\n");
406 /* if end of buf -> there is no garbage after inst */
408 /* more than 1 inst matches */
409 debug_printf("Ambiguous cmd\n");
410 return CMDLINE_PARSE_AMBIGUOUS;
418 return CMDLINE_PARSE_NOMATCH;
420 f(result_buf, opaque, data);
421 return CMDLINE_PARSE_SUCCESS;
424 /* called by cmdline_help() and cmdline_complete() to preparse the
425 * line buffer (the operations done are common to these functions) */
426 static int cmdline_preparse(struct cmdline_preparse *preparse, const char *buf)
428 int ret, len, nb_tok;
430 /* count the number of tokens in the line buffer */
431 ret = cmdline_validate_linebuf(buf);
436 /* if last token is not complete, decrement nb_valid_tok */
438 if (nb_tok == 0 || isblank2(buf[len - 1])) {
439 preparse->nb_valid_tok = nb_tok;
440 preparse->comp_tok_offset = len;
441 preparse->comp_tok_buf[0] = '\0';
442 preparse->comp_tok_len = 0;
445 preparse->nb_valid_tok = nb_tok - 1;
447 /* get the incomplete token (can be empty) and return its
448 * offset in the buffer */
449 preparse->comp_tok_offset =
450 cmdline_get_nth_token(preparse->comp_tok_buf,
451 sizeof(preparse->comp_tok_buf),
452 preparse->nb_valid_tok,
454 preparse->comp_tok_len = strlen(preparse->comp_tok_buf);
460 /* Display a contextual help using the write_buf() function pointer
461 * given as parameter (called with its opaque pointer). The contextual
462 * help depends on the buffer given. */
463 int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
464 cmdline_write_t *write_buf, void *opaque)
466 cmdline_parse_token_hdr_t *token;
467 cmdline_parse_inst_t **pinst;
468 cmdline_parse_inst_t *inst;
469 struct cmdline_preparse preparse;
470 char helpbuf[CMDLINE_MAX_DSTBUF_SIZE];
471 char tmpbuf[CMDLINE_MAX_DSTBUF_SIZE];
475 cmdline_preparse(&preparse, buf);
477 debug_printf("display contextual help\n");
479 for (pinst = &ctx->insts[0]; *pinst != NULL; pinst++) {
482 /* match the beginning of the command */
483 if (preparse.nb_valid_tok != 0 &&
484 match_inst(inst, buf, preparse.nb_valid_tok,
488 token = inst->tokens[preparse.nb_valid_tok];
492 n = snprintf(helpbuf, sizeof(helpbuf), "[RETURN]\n");
494 write_buf(opaque, helpbuf, n);
498 /* token matches, but no completion */
499 if (token->ops->complete_start == NULL ||
500 token->ops->complete_iterate == NULL)
505 /* store the incomplete token in tmpbuf */
506 n = preparse.comp_tok_len + 1;
507 if (n > sizeof(tmpbuf))
509 snprintf(tmpbuf, n, "%s", preparse.comp_tok_buf);
512 token->ops->complete_start(token, tmpbuf,
513 &preparse.opaque) < 0) {
514 /* cancel iteration, complete_start() returned
515 * a negative value, meaning no completion */
517 if (token->ops->complete_end != NULL)
518 token->ops->complete_end(token,
522 /* get token dynamic help string */
523 if ((token->ops->help == NULL) ||
524 (token->ops->help(token, tmpbuf, sizeof(tmpbuf)) < 0))
525 snprintf(tmpbuf, sizeof(tmpbuf), "unknown");
527 /* get instruction static help string */
528 help_str = inst->help_str;
529 if (help_str == NULL)
530 help_str = "No help";
532 /* send it to callback function */
533 n = snprintf(helpbuf, sizeof(helpbuf),
534 "[%s]: %s\n", tmpbuf, help_str);
535 if (n >= 0 && n < sizeof(helpbuf))
536 write_buf(opaque, helpbuf, n);
541 /* iterate over all possible completion for this inst */
542 while (token->ops->complete_iterate(token,
545 sizeof(tmpbuf)) >= 0) {
548 debug_printf(" choice <%s>\n", tmpbuf);
550 /* does the completion match the beginning of
552 if (strncmp(preparse.comp_tok_buf, tmpbuf,
553 preparse.comp_tok_len))
556 /* get the token and add it in help buffer */
557 n = snprintf(helpbuf, sizeof(helpbuf), " %s\n", tmpbuf);
558 if (n >= 0 && n < sizeof(helpbuf))
559 write_buf(opaque, helpbuf, n);
562 /* no more completion, go to next inst */
563 if ( token->ops->complete_end != NULL)
564 token->ops->complete_end(token, &preparse.opaque);
570 /* try to complete the buffer given as a parameter */
572 cmdline_complete(cmdline_parse_ctx_t *ctx, const char *buf,
573 char *dst, unsigned int dstsize)
575 cmdline_parse_token_hdr_t *token;
576 cmdline_parse_inst_t **pinst;
577 cmdline_parse_inst_t *inst;
578 struct cmdline_preparse preparse;
580 int nb_completion = 0;
581 int completion_len = CMDLINE_MAX_TOKEN_SIZE;
582 char completion_buf[CMDLINE_MAX_TOKEN_SIZE];
583 char tmpbuf[CMDLINE_MAX_TOKEN_SIZE];
586 debug_printf("%s called\n", __FUNCTION__);
588 /* fill the preparse structure that contains infos that will
589 * help us to complete the buffer */
590 ret = cmdline_preparse(&preparse, buf);
592 return CMDLINE_COMPLETE_NONE;
594 /* try to complete !! */
595 for (pinst = &ctx->insts[0]; *pinst != NULL; pinst++) {
598 debug_printf("INST\n");
600 /* try to match the first tokens */
601 if (preparse.nb_valid_tok != 0 &&
602 match_inst(inst, buf, preparse.nb_valid_tok,
607 token = inst->tokens[preparse.nb_valid_tok];
609 /* non completable */
611 token->ops->complete_start == NULL ||
612 token->ops->complete_iterate == NULL)
615 /* store the incomplete token in tmpbuf */
616 n = preparse.comp_tok_len + 1;
617 if (n > sizeof(tmpbuf))
619 snprintf(tmpbuf, n, "%s", preparse.comp_tok_buf);
621 /* non completable */
622 if (token->ops->complete_start(token, tmpbuf,
623 &preparse.opaque) < 0) {
624 if (token->ops->complete_end != NULL)
625 token->ops->complete_end(token,
630 /* all possible completion for this token */
633 ret = token->ops->complete_iterate(token,
641 debug_printf("Completion %s\n", tmpbuf);
643 /* we kept at least the room for one char */
647 debug_printf(" choice <%s>\n", tmpbuf);
649 /* does the completion match the beginning of
651 if (strncmp(preparse.comp_tok_buf, tmpbuf,
652 preparse.comp_tok_len))
655 /* first one, save the buffer */
656 if (nb_completion == 0) {
657 completion_len = snprintf(completion_buf,
658 sizeof(completion_buf),
662 n = nb_common_chars(completion_buf, tmpbuf);
663 if (n < completion_len)
668 /* we cannot add any char, just display help */
669 if (completion_len == preparse.comp_tok_len)
672 if (token->ops->complete_end != NULL)
673 token->ops->complete_end(token, &preparse.opaque);
675 if (completion_len == preparse.comp_tok_len)
679 debug_printf("nb_completion=%d, completion_len=%d\n",
680 nb_completion, completion_len);
682 /* one choice, append chars and return */
683 if (nb_completion == 1) {
684 snprintf(dst, dstsize, "%s",
685 completion_buf + preparse.comp_tok_len);
686 return CMDLINE_COMPLETE_APPEND;
689 /* many choices, but starting with same chars: append chars
691 if (nb_completion != 0 && completion_len > preparse.comp_tok_len) {
692 if (completion_len >= dstsize)
693 completion_len = dstsize - 1;
694 strncpy(dst, completion_buf + preparse.comp_tok_len,
695 completion_len - preparse.comp_tok_len);
696 dst[completion_len - preparse.comp_tok_len] = '\0';
697 return CMDLINE_COMPLETE_APPEND;
700 /* no match, nothing to do */
701 if (nb_match == 0 || nb_completion == 0)
702 return CMDLINE_COMPLETE_NONE;
704 return CMDLINE_COMPLETE_MANY;