4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * 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 FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
36 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions are met:
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * * Neither the name of the University of California, Berkeley nor the
46 * names of its contributors may be used to endorse or promote products
47 * derived from this software without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
50 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
53 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 #include <netinet/in.h>
71 #include <rte_string_fns.h>
73 #include "cmdline_rdline.h"
74 #include "cmdline_parse.h"
77 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
78 #define debug_printf printf
80 #define debug_printf(args...) do {} while(0)
83 #define CMDLINE_BUFFER_SIZE 64
85 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
114 cmdline_isendoftoken(char c)
116 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
122 cmdline_isendofcommand(char c)
124 if (!c || iscomment(c) || isendofline(c))
130 nb_common_chars(const char * s1, const char * s2)
134 while (*s1==*s2 && *s1) {
142 /** Retrieve either static or dynamic token at a given index. */
143 static cmdline_parse_token_hdr_t *
144 get_token(cmdline_parse_inst_t *inst,
146 cmdline_parse_token_hdr_t
147 *(*dyn_tokens)[CMDLINE_PARSE_DYNAMIC_TOKENS])
149 /* check presence of static tokens first */
150 if (inst->tokens[0] || !inst->f)
151 return inst->tokens[index];
152 /* for dynamic tokens, make sure index does not overflow */
153 if (index >= CMDLINE_PARSE_DYNAMIC_TOKENS - 1)
155 /* in case token is already generated, return it */
156 if ((*dyn_tokens)[index])
157 return (*dyn_tokens)[index];
159 inst->f(&(*dyn_tokens)[index], NULL, dyn_tokens);
160 /* return immediately if there are no more tokens to expect */
161 if (!(*dyn_tokens)[index])
163 /* terminate list with a NULL entry */
164 (*dyn_tokens)[index + 1] = NULL;
165 return (*dyn_tokens)[index];
169 * try to match the buffer with an instruction (only the first
170 * nb_match_token tokens if != 0). Return 0 if we match all the
171 * tokens, else the number of matched tokens, else -1.
174 match_inst(cmdline_parse_inst_t *inst, const char *buf,
175 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size,
176 cmdline_parse_token_hdr_t
177 *(*dyn_tokens)[CMDLINE_PARSE_DYNAMIC_TOKENS])
179 unsigned int token_num=0;
180 cmdline_parse_token_hdr_t * token_p;
183 struct cmdline_token_hdr token_hdr;
185 token_p = get_token(inst, token_num, dyn_tokens);
187 memcpy(&token_hdr, token_p, sizeof(token_hdr));
189 /* check if we match all tokens of inst */
190 while (token_p && (!nb_match_token || i<nb_match_token)) {
191 debug_printf("TK\n");
193 while (isblank2(*buf)) {
198 if ( isendofline(*buf) || iscomment(*buf) )
201 if (resbuf == NULL) {
202 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
206 if (token_hdr.offset > resbuf_size) {
207 printf("Parse error(%s:%d): Token offset(%u) "
208 "exceeds maximum size(%u)\n",
210 token_hdr.offset, resbuf_size);
213 rb_sz = resbuf_size - token_hdr.offset;
215 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
216 token_hdr.offset, rb_sz);
222 debug_printf("TK parsed (len=%d)\n", n);
227 token_p = get_token(inst, token_num, dyn_tokens);
229 memcpy(&token_hdr, token_p, sizeof(token_hdr));
236 /* in case we want to match a specific num of token */
237 if (nb_match_token) {
238 if (i == nb_match_token) {
244 /* we don't match all the tokens */
249 /* are there are some tokens more */
250 while (isblank2(*buf)) {
255 if ( isendofline(*buf) || iscomment(*buf) )
258 /* garbage after inst */
264 cmdline_parse(struct cmdline *cl, const char * buf)
266 unsigned int inst_num=0;
267 cmdline_parse_inst_t *inst;
270 char buf[CMDLINE_PARSE_RESULT_BUFSIZE];
271 long double align; /* strong alignment constraint for buf */
272 } result, tmp_result;
273 cmdline_parse_token_hdr_t *dyn_tokens[CMDLINE_PARSE_DYNAMIC_TOKENS];
274 void (*f)(void *, struct cmdline *, void *) = NULL;
279 int err = CMDLINE_PARSE_NOMATCH;
281 cmdline_parse_ctx_t *ctx;
282 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
283 char debug_buf[BUFSIZ];
287 return CMDLINE_PARSE_BAD_ARGS;
292 * - look if the buffer contains at least one line
293 * - look if line contains only spaces or comments
294 * - count line length
297 while (! isendofline(*curbuf)) {
298 if ( *curbuf == '\0' ) {
299 debug_printf("Incomplete buf (len=%d)\n", linelen);
302 if ( iscomment(*curbuf) ) {
305 if ( ! isblank2(*curbuf) && ! comment) {
312 /* skip all endofline chars */
313 while (isendofline(buf[linelen])) {
318 if ( parse_it == 0 ) {
319 debug_printf("Empty line (len=%d)\n", linelen);
323 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
324 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
325 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
329 inst = ctx[inst_num];
330 dyn_tokens[0] = NULL;
332 debug_printf("INST %d\n", inst_num);
335 tok = match_inst(inst, buf, 0, tmp_result.buf,
336 sizeof(tmp_result.buf), &dyn_tokens);
338 if (tok > 0) /* we matched at least one token */
339 err = CMDLINE_PARSE_BAD_ARGS;
342 debug_printf("INST fully parsed\n");
343 memcpy(&result, &tmp_result,
346 while (isblank2(*curbuf)) {
350 /* if end of buf -> there is no garbage after inst */
351 if (isendofline(*curbuf) || iscomment(*curbuf)) {
353 memcpy(&f, &inst->f, sizeof(f));
354 memcpy(&data, &inst->data, sizeof(data));
357 /* more than 1 inst matches */
358 err = CMDLINE_PARSE_AMBIGUOUS;
360 debug_printf("Ambiguous cmd\n");
367 inst = ctx[inst_num];
368 dyn_tokens[0] = NULL;
373 f(result.buf, cl, data);
378 debug_printf("No match err=%d\n", err);
386 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
387 char *dst, unsigned int size)
389 const char *partial_tok = buf;
390 unsigned int inst_num = 0;
391 cmdline_parse_inst_t *inst;
392 cmdline_parse_token_hdr_t *token_p;
393 struct cmdline_token_hdr token_hdr;
394 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
395 cmdline_parse_token_hdr_t *dyn_tokens[CMDLINE_PARSE_DYNAMIC_TOKENS];
396 unsigned int partial_tok_len;
402 unsigned int nb_completable;
403 unsigned int nb_non_completable;
405 const char *help_str;
406 cmdline_parse_ctx_t *ctx;
408 if (!cl || !buf || !state || !dst)
413 debug_printf("%s called\n", __func__);
414 memset(&token_hdr, 0, sizeof(token_hdr));
416 /* count the number of complete token to parse */
417 for (i=0 ; buf[i] ; i++) {
418 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
420 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
421 partial_tok = buf+i+1;
423 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
425 /* first call -> do a first pass */
427 debug_printf("try complete <%s>\n", buf);
428 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
429 nb_token, partial_tok);
432 nb_non_completable = 0;
434 inst = ctx[inst_num];
435 dyn_tokens[0] = NULL;
437 /* parse the first tokens of the inst */
439 match_inst(inst, buf, nb_token, NULL, 0,
443 debug_printf("instruction match\n");
444 token_p = get_token(inst, nb_token, &dyn_tokens);
446 memcpy(&token_hdr, token_p, sizeof(token_hdr));
448 /* non completable */
450 !token_hdr.ops->complete_get_nb ||
451 !token_hdr.ops->complete_get_elt ||
452 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
453 nb_non_completable++;
457 debug_printf("%d choices for this token\n", n);
458 for (i=0 ; i<n ; i++) {
459 if (token_hdr.ops->complete_get_elt(token_p, i,
464 /* we have at least room for one char */
465 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
466 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
467 tmpbuf[tmp_len] = ' ';
468 tmpbuf[tmp_len+1] = 0;
471 debug_printf(" choice <%s>\n", tmpbuf);
473 /* does the completion match the
474 * beginning of the word ? */
475 if (!strncmp(partial_tok, tmpbuf,
477 if (comp_len == -1) {
478 snprintf(comp_buf, sizeof(comp_buf),
479 "%s", tmpbuf + partial_tok_len);
481 strnlen(tmpbuf + partial_tok_len,
482 sizeof(tmpbuf) - partial_tok_len);
487 nb_common_chars(comp_buf,
488 tmpbuf+partial_tok_len);
489 comp_buf[comp_len] = 0;
495 debug_printf("next\n");
497 inst = ctx[inst_num];
498 dyn_tokens[0] = NULL;
501 debug_printf("total choices %d for this completion\n",
504 /* no possible completion */
505 if (nb_completable == 0 && nb_non_completable == 0)
508 /* if multichoice is not required */
509 if (*state == 0 && partial_tok_len > 0) {
510 /* one or several choices starting with the
513 if ((unsigned)(comp_len + 1) > size)
516 snprintf(dst, size, "%s", comp_buf);
523 /* init state correctly */
527 debug_printf("Multiple choice STATE=%d\n", *state);
530 inst = ctx[inst_num];
531 dyn_tokens[0] = NULL;
533 /* we need to redo it */
534 inst = ctx[inst_num];
537 match_inst(inst, buf, nb_token, NULL, 0, &dyn_tokens))
540 token_p = get_token(inst, nb_token, &dyn_tokens);
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 */
577 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
578 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
579 tmpbuf[tmp_len] = ' ';
580 tmpbuf[tmp_len + 1] = 0;
583 debug_printf(" choice <%s>\n", tmpbuf);
585 /* does the completion match the beginning of
587 if (!strncmp(partial_tok, tmpbuf,
589 if (local_state < *state) {
594 l=snprintf(dst, size, "%s", tmpbuf);
595 if (l>=0 && token_hdr.ops->get_help) {
596 token_hdr.ops->get_help(token_p, tmpbuf,
598 help_str = inst->help_str;
600 snprintf(dst+l, size-l, "[%s]: %s",
603 snprintf(dst+l, size-l,
604 "[%s]: No help", tmpbuf);
612 inst = ctx[inst_num];
613 dyn_tokens[0] = NULL;