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) {
143 * try to match the buffer with an instruction (only the first
144 * nb_match_token tokens if != 0). Return 0 if we match all the
145 * tokens, else the number of matched tokens, else -1.
148 match_inst(cmdline_parse_inst_t *inst, const char *buf,
149 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size,
150 cmdline_parse_token_hdr_t
151 *(*dyn_tokens)[CMDLINE_PARSE_DYNAMIC_TOKENS])
153 unsigned int token_num=0;
154 cmdline_parse_token_hdr_t * token_p;
157 struct cmdline_token_hdr token_hdr;
159 token_p = inst->tokens[token_num];
160 if (!token_p && dyn_tokens && inst->f) {
161 if (!(*dyn_tokens)[0])
162 inst->f(&(*dyn_tokens)[0], NULL, dyn_tokens);
163 token_p = (*dyn_tokens)[0];
166 memcpy(&token_hdr, token_p, sizeof(token_hdr));
168 /* check if we match all tokens of inst */
169 while (token_p && (!nb_match_token || i<nb_match_token)) {
170 debug_printf("TK\n");
172 while (isblank2(*buf)) {
177 if ( isendofline(*buf) || iscomment(*buf) )
180 if (resbuf == NULL) {
181 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
185 if (token_hdr.offset > resbuf_size) {
186 printf("Parse error(%s:%d): Token offset(%u) "
187 "exceeds maximum size(%u)\n",
189 token_hdr.offset, resbuf_size);
192 rb_sz = resbuf_size - token_hdr.offset;
194 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
195 token_hdr.offset, rb_sz);
201 debug_printf("TK parsed (len=%d)\n", n);
206 if (!inst->tokens[0]) {
207 if (token_num < (CMDLINE_PARSE_DYNAMIC_TOKENS - 1)) {
208 if (!(*dyn_tokens)[token_num])
209 inst->f(&(*dyn_tokens)[token_num],
212 token_p = (*dyn_tokens)[token_num];
216 token_p = inst->tokens[token_num];
218 memcpy(&token_hdr, token_p, sizeof(token_hdr));
225 /* in case we want to match a specific num of token */
226 if (nb_match_token) {
227 if (i == nb_match_token) {
233 /* we don't match all the tokens */
238 /* are there are some tokens more */
239 while (isblank2(*buf)) {
244 if ( isendofline(*buf) || iscomment(*buf) )
247 /* garbage after inst */
253 cmdline_parse(struct cmdline *cl, const char * buf)
255 unsigned int inst_num=0;
256 cmdline_parse_inst_t *inst;
258 char result_buf[CMDLINE_PARSE_RESULT_BUFSIZE];
259 cmdline_parse_token_hdr_t *dyn_tokens[CMDLINE_PARSE_DYNAMIC_TOKENS];
260 void (*f)(void *, struct cmdline *, void *) = NULL;
265 int err = CMDLINE_PARSE_NOMATCH;
267 cmdline_parse_ctx_t *ctx;
268 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
269 char debug_buf[BUFSIZ];
273 return CMDLINE_PARSE_BAD_ARGS;
276 memset(&dyn_tokens, 0, sizeof(dyn_tokens));
279 * - look if the buffer contains at least one line
280 * - look if line contains only spaces or comments
281 * - count line length
284 while (! isendofline(*curbuf)) {
285 if ( *curbuf == '\0' ) {
286 debug_printf("Incomplete buf (len=%d)\n", linelen);
289 if ( iscomment(*curbuf) ) {
292 if ( ! isblank2(*curbuf) && ! comment) {
299 /* skip all endofline chars */
300 while (isendofline(buf[linelen])) {
305 if ( parse_it == 0 ) {
306 debug_printf("Empty line (len=%d)\n", linelen);
310 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
311 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
312 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
316 inst = ctx[inst_num];
318 debug_printf("INST %d\n", inst_num);
321 tok = match_inst(inst, buf, 0, result_buf, sizeof(result_buf),
324 if (tok > 0) /* we matched at least one token */
325 err = CMDLINE_PARSE_BAD_ARGS;
328 debug_printf("INST fully parsed\n");
330 while (isblank2(*curbuf)) {
334 /* if end of buf -> there is no garbage after inst */
335 if (isendofline(*curbuf) || iscomment(*curbuf)) {
337 memcpy(&f, &inst->f, sizeof(f));
338 memcpy(&data, &inst->data, sizeof(data));
341 /* more than 1 inst matches */
342 err = CMDLINE_PARSE_AMBIGUOUS;
344 debug_printf("Ambiguous cmd\n");
351 inst = ctx[inst_num];
356 f(result_buf, cl, data);
361 debug_printf("No match err=%d\n", err);
369 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
370 char *dst, unsigned int size)
372 const char *partial_tok = buf;
373 unsigned int inst_num = 0;
374 cmdline_parse_inst_t *inst;
375 cmdline_parse_token_hdr_t *token_p;
376 struct cmdline_token_hdr token_hdr;
377 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
378 cmdline_parse_token_hdr_t *dyn_tokens[CMDLINE_PARSE_DYNAMIC_TOKENS];
379 unsigned int partial_tok_len;
385 unsigned int nb_completable;
386 unsigned int nb_non_completable;
388 const char *help_str;
389 cmdline_parse_ctx_t *ctx;
391 if (!cl || !buf || !state || !dst)
396 debug_printf("%s called\n", __func__);
397 memset(&token_hdr, 0, sizeof(token_hdr));
398 memset(&dyn_tokens, 0, sizeof(dyn_tokens));
400 /* count the number of complete token to parse */
401 for (i=0 ; buf[i] ; i++) {
402 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
404 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
405 partial_tok = buf+i+1;
407 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
409 /* first call -> do a first pass */
411 debug_printf("try complete <%s>\n", buf);
412 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
413 nb_token, partial_tok);
416 nb_non_completable = 0;
418 inst = ctx[inst_num];
420 /* parse the first tokens of the inst */
422 match_inst(inst, buf, nb_token, NULL, 0,
426 debug_printf("instruction match\n");
427 if (!inst->tokens[0]) {
429 (CMDLINE_PARSE_DYNAMIC_TOKENS - 1)) {
430 if (!dyn_tokens[nb_token])
431 inst->f(&dyn_tokens[nb_token],
434 token_p = dyn_tokens[nb_token];
438 token_p = inst->tokens[nb_token];
440 memcpy(&token_hdr, token_p, sizeof(token_hdr));
442 /* non completable */
444 !token_hdr.ops->complete_get_nb ||
445 !token_hdr.ops->complete_get_elt ||
446 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
447 nb_non_completable++;
451 debug_printf("%d choices for this token\n", n);
452 for (i=0 ; i<n ; i++) {
453 if (token_hdr.ops->complete_get_elt(token_p, i,
458 /* we have at least room for one char */
459 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
460 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
461 tmpbuf[tmp_len] = ' ';
462 tmpbuf[tmp_len+1] = 0;
465 debug_printf(" choice <%s>\n", tmpbuf);
467 /* does the completion match the
468 * beginning of the word ? */
469 if (!strncmp(partial_tok, tmpbuf,
471 if (comp_len == -1) {
472 snprintf(comp_buf, sizeof(comp_buf),
473 "%s", tmpbuf + partial_tok_len);
475 strnlen(tmpbuf + partial_tok_len,
476 sizeof(tmpbuf) - partial_tok_len);
481 nb_common_chars(comp_buf,
482 tmpbuf+partial_tok_len);
483 comp_buf[comp_len] = 0;
489 debug_printf("next\n");
491 inst = ctx[inst_num];
494 debug_printf("total choices %d for this completion\n",
497 /* no possible completion */
498 if (nb_completable == 0 && nb_non_completable == 0)
501 /* if multichoice is not required */
502 if (*state == 0 && partial_tok_len > 0) {
503 /* one or several choices starting with the
506 if ((unsigned)(comp_len + 1) > size)
509 snprintf(dst, size, "%s", comp_buf);
516 /* init state correctly */
520 debug_printf("Multiple choice STATE=%d\n", *state);
523 inst = ctx[inst_num];
525 /* we need to redo it */
526 inst = ctx[inst_num];
529 match_inst(inst, buf, nb_token, NULL, 0, &dyn_tokens))
532 if (!inst->tokens[0]) {
533 if (nb_token < (CMDLINE_PARSE_DYNAMIC_TOKENS - 1)) {
534 if (!dyn_tokens[nb_token])
535 inst->f(&dyn_tokens[nb_token],
538 token_p = dyn_tokens[nb_token];
542 token_p = inst->tokens[nb_token];
544 memcpy(&token_hdr, token_p, sizeof(token_hdr));
546 /* one choice for this token */
548 !token_hdr.ops->complete_get_nb ||
549 !token_hdr.ops->complete_get_elt ||
550 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
551 if (local_state < *state) {
556 if (token_p && token_hdr.ops->get_help) {
557 token_hdr.ops->get_help(token_p, tmpbuf,
559 help_str = inst->help_str;
561 snprintf(dst, size, "[%s]: %s", tmpbuf,
564 snprintf(dst, size, "[%s]: No help",
568 snprintf(dst, size, "[RETURN]");
573 /* several choices */
574 for (i=0 ; i<n ; i++) {
575 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
578 /* we have at least room for one char */
579 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
580 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
581 tmpbuf[tmp_len] = ' ';
582 tmpbuf[tmp_len + 1] = 0;
585 debug_printf(" choice <%s>\n", tmpbuf);
587 /* does the completion match the beginning of
589 if (!strncmp(partial_tok, tmpbuf,
591 if (local_state < *state) {
596 l=snprintf(dst, size, "%s", tmpbuf);
597 if (l>=0 && token_hdr.ops->get_help) {
598 token_hdr.ops->get_help(token_p, tmpbuf,
600 help_str = inst->help_str;
602 snprintf(dst+l, size-l, "[%s]: %s",
605 snprintf(dst+l, size-l,
606 "[%s]: No help", tmpbuf);
614 inst = ctx[inst_num];