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)
506 token->ops->complete_start(token, &preparse.opaque) < 0) {
507 /* cancel iteration, complete_start() returned
508 * a negative value, meaning no completion */
510 if (token->ops->complete_end != NULL)
511 token->ops->complete_end(token,
515 /* get token dynamic help string */
516 if ((token->ops->help == NULL) ||
517 (token->ops->help(token, tmpbuf, sizeof(tmpbuf)) < 0))
518 snprintf(tmpbuf, sizeof(tmpbuf), "unknown");
520 /* get instruction static help string */
521 help_str = inst->help_str;
522 if (help_str == NULL)
523 help_str = "No help";
525 /* send it to callback function */
526 n = snprintf(helpbuf, sizeof(helpbuf),
527 "[%s]: %s\n", tmpbuf, help_str);
528 if (n >= 0 && n < sizeof(helpbuf))
529 write_buf(opaque, helpbuf, n);
534 /* iterate over all possible completion for this inst */
535 while (token->ops->complete_iterate(token,
538 sizeof(tmpbuf)) == 0) {
541 debug_printf(" choice <%s>\n", tmpbuf);
543 /* does the completion match the beginning of
545 if (strncmp(preparse.comp_tok_buf, tmpbuf,
546 preparse.comp_tok_len))
549 /* get the token and add it in help buffer */
550 n = snprintf(helpbuf, sizeof(helpbuf), " %s\n", tmpbuf);
551 if (n >= 0 && n < sizeof(helpbuf))
552 write_buf(opaque, helpbuf, n);
555 /* no more completion, go to next inst */
556 if ( token->ops->complete_end != NULL)
557 token->ops->complete_end(token, &preparse.opaque);
563 /* try to complete the buffer given as a parameter */
565 cmdline_complete(cmdline_parse_ctx_t *ctx, const char *buf,
566 char *dst, unsigned int dstsize)
568 cmdline_parse_token_hdr_t *token;
569 cmdline_parse_inst_t **pinst;
570 cmdline_parse_inst_t *inst;
571 struct cmdline_preparse preparse;
573 int nb_completion = 0;
574 int completion_len = CMDLINE_MAX_TOKEN_SIZE;
575 char completion_buf[CMDLINE_MAX_TOKEN_SIZE];
576 char tmpbuf[CMDLINE_MAX_TOKEN_SIZE];
579 debug_printf("%s called\n", __FUNCTION__);
581 /* fill the preparse structure that contains infos that will
582 * help us to complete the buffer */
583 ret = cmdline_preparse(&preparse, buf);
585 return CMDLINE_COMPLETE_NONE;
587 /* try to complete !! */
588 for (pinst = &ctx->insts[0]; *pinst != NULL; pinst++) {
591 debug_printf("INST\n");
593 /* try to match the first tokens */
594 if (preparse.nb_valid_tok != 0 &&
595 match_inst(inst, buf, preparse.nb_valid_tok,
600 token = inst->tokens[preparse.nb_valid_tok];
602 /* non completable */
604 token->ops->complete_start == NULL ||
605 token->ops->complete_iterate == NULL)
608 /* non completable */
609 if (token->ops->complete_start(token, &preparse.opaque) < 0) {
610 if (token->ops->complete_end != NULL)
611 token->ops->complete_end(token,
616 /* all possible completion for this token */
619 ret = token->ops->complete_iterate(token,
627 debug_printf("Completion %s\n", tmpbuf);
629 /* we kept at least the room for one char */
630 /* XXX to be updated for non-final completion */
634 debug_printf(" choice <%s>\n", tmpbuf);
636 /* does the completion match the beginning of
638 if (strncmp(preparse.comp_tok_buf, tmpbuf,
639 preparse.comp_tok_len))
642 /* first one, save the buffer */
643 if (nb_completion == 0) {
644 completion_len = snprintf(completion_buf,
645 sizeof(completion_buf),
649 n = nb_common_chars(completion_buf, tmpbuf);
650 if (n < completion_len)
655 /* we cannot add any char, just display help */
656 if (completion_len == preparse.comp_tok_len)
659 if (token->ops->complete_end != NULL)
660 token->ops->complete_end(token, &preparse.opaque);
662 if (completion_len == preparse.comp_tok_len)
666 debug_printf("nb_completion=%d, completion_len=%d\n",
667 nb_completion, completion_len);
669 /* one choice, append chars and return */
670 if (nb_completion == 1) {
671 snprintf(dst, dstsize, "%s",
672 completion_buf + preparse.comp_tok_len);
673 return CMDLINE_COMPLETE_APPEND;
676 /* many choices, but starting with same chars: append chars
678 if (nb_completion != 0 && completion_len > preparse.comp_tok_len) {
679 if (completion_len >= dstsize)
680 completion_len = dstsize - 1;
681 strncpy(dst, completion_buf + preparse.comp_tok_len,
683 dst[completion_len] = '\0';
684 return CMDLINE_COMPLETE_APPEND;
687 /* no match, nothing to do */
688 if (nb_match == 0 || nb_completion == 0)
689 return CMDLINE_COMPLETE_NONE;
691 return CMDLINE_COMPLETE_MANY;