2 * Copyright (c) 2009-2015, 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.
29 * Copyright (c) <2010>, Intel Corporation
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
36 * - Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
39 * - Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in
41 * the documentation and/or other materials provided with the
44 * - Neither the name of Intel Corporation nor the names of its
45 * contributors may be used to endorse or promote products derived
46 * from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
57 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59 * OF THE POSSIBILITY OF SUCH DAMAGE.
69 #include "ucg_cmd_parse.h"
72 //#define debug_printf printf
73 #define debug_printf(args...) do {} while(0)
75 /* used internally for ucg_cmd_help() and ucg_cmd_complete() */
77 int nb_valid_tok; /* number of valid tokens in the buffer */
78 void *opaque; /* pointer to opaque data */
79 char comp_tok_buf[UCG_CMD_MAX_TOKEN_SIZE]; /* token to complete */
80 size_t comp_tok_len; /* length of the token to complete */
81 size_t comp_tok_offset; /* offset of token to complete in the line buf */
84 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
89 if (c == ' ' || c == '\t' )
97 if (c == '\n' || c == '\r' )
111 ucg_cmd_isendoftoken(char c)
113 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
119 nb_common_chars(const char * s1, const char * s2)
123 while (*s1==*s2 && *s1 && *s2) {
131 /* quote a string and escape original quotes */
132 int ucg_cmd_quote_token(char *dst, unsigned dstlen, const char *src)
134 unsigned s = 0, d = 0;
136 /* the 2 quotes + '\0' */
141 while (src[s] != '\0') {
147 if (src[s] == '\\' && src[s+1] == '"')
160 /* Remove quotes and stop when we reach the end of token. Return the
161 * number of "eaten" bytes from the source buffer, or a negative value
163 int ucg_cmd_get_token(char *dst, unsigned dstlen, const char *src)
165 unsigned s = 0, d = 0;
169 while (isblank2(src[s]))
173 if (ucg_cmd_isendoftoken(src[s]))
176 /* copy token and remove quotes */
177 while (src[s] != '\0') {
181 if (ucg_cmd_isendoftoken(src[s]) && quoted == 0)
184 if (src[s] == '\\' && src[s+1] == '"') {
189 if (src[s] == '\\' && src[s+1] == '\\') {
202 /* not enough room in dst buffer */
206 /* end of string during quote */
214 /* return the nth token from src and copy it in dst. Return the offset
215 * of the token in src, or a negative value on error. Note: the index
216 * of the first token is 0. */
217 static int cmd_get_nth_token(char *dst, unsigned dstlen, int n,
220 int ret = 0, offset = 0;
226 while (isblank2(src[offset]))
229 /* get the token starting at offset */
230 ret = ucg_cmd_get_token(dst, dstlen, src + offset);
240 * try to match the buffer with an instruction (only the first
241 * nb_match_token tokens if != 0).
243 * Return 0 if we match all the tokens, else the number of matched
244 * tokens, or a negative value on error.
247 match_inst(const ucg_cmd_inst_t *inst, const char *linebuf,
248 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size)
250 unsigned int token_num = 0;
251 ucg_cmd_tk_hdr_t *token;
253 char token_str[UCG_CMD_MAX_TOKEN_SIZE];
255 token = inst->tokens[token_num];
257 /* check if we match all tokens of inst */
260 /* we matched enough tokens, return success */
261 if (nb_match_token != 0 && token_num >= nb_match_token)
264 debug_printf("TK\n");
266 /* copy token and remove quotes */
267 n = ucg_cmd_get_token(token_str, sizeof(token_str), linebuf);
271 /* parse this token */
273 res = token->ops->parse(token, token_str, NULL, 0);
276 void *rb = (char *)resbuf + token->offset;
278 /* not enough room to store result */
279 if (token->offset > resbuf_size)
282 rb_sz = resbuf_size - token->offset;
283 res = token->ops->parse(token, token_str, rb, rb_sz);
286 /* does not match this token */
290 debug_printf("TK parsed (len=%d)\n", n);
293 token = inst->tokens[token_num];
300 /* we don't match all the tokens */
304 /* are there are some tokens more */
305 while (isblank2(*linebuf))
308 /* end of buf, we match all inst */
309 if (*linebuf == '\0' || isendofline(*linebuf) || iscomment(*linebuf))
312 /* garbage after inst */
317 /* Check if a line buffer is valid and can be parsed or completed. The
318 * parsing stops when \n or \0 is reached. The also function checks
319 * that tokens are correctly quoted. The number of tokens in the
320 * buffer is returned. */
321 static int validate_linebuf(const char *linebuf)
323 int quoted = 0, comment = 0;
324 int i = 0, nbtok = 0, token = 0;
326 while (linebuf[i] != '\0') {
327 if (isendofline(linebuf[i]) && quoted == 0)
333 if (iscomment(linebuf[i]) && quoted == 0) {
340 if (isblank2(linebuf[i]) && quoted == 0)
343 if (!isblank2(linebuf[i]) && token == 0) {
348 if (linebuf[i] == '\\' && linebuf[i+1] == '"') {
352 if (linebuf[i] == '\\' && linebuf[i+1] == '\\') {
356 if (linebuf[i] == '"') {
364 return UCG_CMD_PARSE_UNTERMINATED_QUOTE;
368 /* Try to parse a buffer according to the specified context. The
369 * argument linebuf must end with \n or \0. */
371 ucg_cmd_parse(struct ucg_cmd *cl, const char *linebuf,
374 ucg_cmd_ctx_t *ctx = cl->ctx;
375 const ucg_cmd_inst_t **pinst;
376 const ucg_cmd_inst_t *inst;
377 char result_buf[UCG_CMD_MAX_DSTBUF_SIZE];
378 void (*f)(void *, struct ucg_cmd *, void *) = NULL;
382 ret = validate_linebuf(linebuf);
386 return UCG_CMD_PARSE_EMPTY;
390 for (pinst = &ctx->insts[0]; *pinst != NULL; pinst++) {
392 debug_printf("INST\n");
395 ret = match_inst(inst, linebuf, 0, result_buf,
401 debug_printf("INST fully parsed\n");
403 /* if end of buf -> there is no garbage after inst */
405 /* more than 1 inst matches */
406 debug_printf("Ambiguous cmd\n");
407 return UCG_CMD_PARSE_AMBIGUOUS;
415 return UCG_CMD_PARSE_NOMATCH;
417 f(result_buf, opaque, data);
418 return UCG_CMD_PARSE_SUCCESS;
421 /* called by ucg_cmd_help() and ucg_cmd_complete() to preparse the
422 * line buffer (the operations done are common to these functions) */
423 static int cmd_preparse(struct cmd_preparse *preparse, const char *buf)
425 int ret, len, nb_tok;
427 /* count the number of tokens in the line buffer */
428 ret = validate_linebuf(buf);
433 /* if last token is not complete, decrement nb_valid_tok */
435 if (nb_tok == 0 || isblank2(buf[len - 1])) {
436 preparse->nb_valid_tok = nb_tok;
437 preparse->comp_tok_offset = len;
438 preparse->comp_tok_buf[0] = '\0';
439 preparse->comp_tok_len = 0;
442 preparse->nb_valid_tok = nb_tok - 1;
444 /* get the incomplete token (can be empty) and return its
445 * offset in the buffer */
446 preparse->comp_tok_offset =
447 cmd_get_nth_token(preparse->comp_tok_buf,
448 sizeof(preparse->comp_tok_buf),
449 preparse->nb_valid_tok,
451 preparse->comp_tok_len = strlen(preparse->comp_tok_buf);
457 /* Display a contextual help in the command line. The contextual help
458 * depends on the buffer given. */
459 void ucg_cmd_help(struct ucg_cmd *cl, const char *buf)
461 ucg_cmd_ctx_t *ctx = cl->ctx;
462 ucg_cmd_tk_hdr_t *token;
463 const ucg_cmd_inst_t **pinst;
464 const ucg_cmd_inst_t *inst;
465 struct cmd_preparse preparse;
466 char tmpbuf[UCG_CMD_MAX_DSTBUF_SIZE];
471 cmd_preparse(&preparse, buf);
473 debug_printf("display contextual help\n");
475 for (pinst = &ctx->insts[0]; *pinst != NULL; pinst++) {
478 /* get instruction static help string */
479 help_str = inst->help_str;
480 if (help_str == NULL)
481 help_str = "No help";
483 /* match the beginning of the command */
484 if (preparse.nb_valid_tok != 0 &&
485 match_inst(inst, buf, preparse.nb_valid_tok,
489 token = inst->tokens[preparse.nb_valid_tok];
493 ucg_cmd_printf(cl, "%-20s %s\n", "<Return>", help_str);
497 /* token matches, but no completion */
498 if (token->ops->complete_start == NULL ||
499 token->ops->complete_iterate == NULL)
504 /* store the incomplete token in tmpbuf */
505 n = preparse.comp_tok_len + 1;
506 if (n > sizeof(tmpbuf))
508 snprintf(tmpbuf, sizeof(tmpbuf), "%s", preparse.comp_tok_buf);
511 token->ops->complete_start(token, tmpbuf,
512 &preparse.opaque) < 0) {
513 /* cancel iteration, complete_start() returned
514 * a negative value, meaning no completion */
516 if (token->ops->complete_end != NULL)
517 token->ops->complete_end(token,
521 debug_printf(" iterate = %d\n", iterate);
524 /* get token dynamic help string */
525 if ((token->ops->help == NULL) ||
526 (token->ops->help(token, tmpbuf,
527 sizeof(tmpbuf)) < 0))
528 snprintf(tmpbuf, sizeof(tmpbuf), "unknown");
530 ucg_cmd_printf(cl, "%-20s %s\n", tmpbuf, help_str);
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 /* get the token and add it in help buffer */
544 ucg_cmd_printf(cl, "%-20s %s\n", tmpbuf,
545 iterate != 0? help_str : "''");
547 /* don't display help next time */
551 /* no more completion, go to next inst */
552 if (token->ops->complete_end != NULL)
553 token->ops->complete_end(token, &preparse.opaque);
557 /* try to complete the buffer given as a parameter */
559 ucg_cmd_complete(struct ucg_cmd *cl, const char *buf,
560 char *dst, unsigned int dstsize)
562 ucg_cmd_ctx_t *ctx = cl->ctx;
563 ucg_cmd_tk_hdr_t *token;
564 const ucg_cmd_inst_t **pinst;
565 const ucg_cmd_inst_t *inst;
566 struct cmd_preparse preparse;
568 int nb_completion = 0;
569 char completion_buf[UCG_CMD_MAX_TOKEN_SIZE];
570 char tmpbuf[UCG_CMD_MAX_TOKEN_SIZE];
572 size_t n, completion_len = UCG_CMD_MAX_TOKEN_SIZE;
574 debug_printf("%s called\n", __FUNCTION__);
576 /* fill the preparse structure that contains infos that will
577 * help us to complete the buffer */
578 ret = cmd_preparse(&preparse, buf);
580 return UCG_CMD_COMPLETE_NONE;
582 /* try to complete ! */
583 for (pinst = &ctx->insts[0]; *pinst != NULL; pinst++) {
586 debug_printf("INST\n");
588 /* try to match the first tokens */
589 if (preparse.nb_valid_tok != 0 &&
590 match_inst(inst, buf, preparse.nb_valid_tok,
595 token = inst->tokens[preparse.nb_valid_tok];
597 /* non completable */
599 token->ops->complete_start == NULL ||
600 token->ops->complete_iterate == NULL)
603 /* store the incomplete token in tmpbuf */
604 n = preparse.comp_tok_len + 1;
605 if (n > sizeof(tmpbuf))
607 snprintf(tmpbuf, n, "%s", preparse.comp_tok_buf);
609 /* non completable */
610 if (token->ops->complete_start(token, tmpbuf,
611 &preparse.opaque) < 0) {
612 if (token->ops->complete_end != NULL)
613 token->ops->complete_end(token,
618 /* all possible completion for this token */
621 ret = token->ops->complete_iterate(token,
629 debug_printf("Completion %s\n", tmpbuf);
631 /* we kept at least the room for one char */
635 debug_printf(" choice <%s>\n", tmpbuf);
637 /* does the completion match the beginning of
639 if (strncmp(preparse.comp_tok_buf, tmpbuf,
640 preparse.comp_tok_len))
643 /* first one, save the buffer */
644 if (nb_completion == 0) {
645 completion_len = snprintf(completion_buf,
646 sizeof(completion_buf),
650 n = nb_common_chars(completion_buf, tmpbuf);
651 if (n < completion_len)
656 /* we cannot add any char, just display help */
657 if (completion_len == preparse.comp_tok_len)
660 if (token->ops->complete_end != NULL)
661 token->ops->complete_end(token, &preparse.opaque);
663 if (completion_len == preparse.comp_tok_len)
667 debug_printf("nb_completion=%d, completion_len=%d\n",
668 (int)nb_completion, (int)completion_len);
670 /* one choice, append chars and return */
671 if (nb_completion == 1) {
672 snprintf(dst, dstsize, "%s",
673 completion_buf + preparse.comp_tok_len);
674 return UCG_CMD_COMPLETE_APPEND;
677 /* many choices, but starting with same chars: append chars
679 if (nb_completion != 0 && completion_len > preparse.comp_tok_len) {
680 if (completion_len >= dstsize)
681 completion_len = dstsize - 1;
682 strncpy(dst, completion_buf + preparse.comp_tok_len,
683 completion_len - preparse.comp_tok_len);
684 dst[completion_len - preparse.comp_tok_len] = '\0';
685 return UCG_CMD_COMPLETE_APPEND;
688 /* no match, nothing to do */
689 if (nb_match == 0 || nb_completion == 0)
690 return UCG_CMD_COMPLETE_NONE;
692 return UCG_CMD_COMPLETE_MANY;