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)
151 unsigned int token_num=0;
152 cmdline_parse_token_hdr_t * token_p;
155 struct cmdline_token_hdr token_hdr;
157 token_p = inst->tokens[token_num];
159 memcpy(&token_hdr, token_p, sizeof(token_hdr));
161 /* check if we match all tokens of inst */
162 while (token_p && (!nb_match_token || i<nb_match_token)) {
163 debug_printf("TK\n");
165 while (isblank2(*buf)) {
170 if ( isendofline(*buf) || iscomment(*buf) )
173 if (resbuf == NULL) {
174 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
178 if (token_hdr.offset > resbuf_size) {
179 printf("Parse error(%s:%d): Token offset(%u) "
180 "exceeds maximum size(%u)\n",
182 token_hdr.offset, resbuf_size);
185 rb_sz = resbuf_size - token_hdr.offset;
187 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
188 token_hdr.offset, rb_sz);
194 debug_printf("TK parsed (len=%d)\n", n);
199 token_p = inst->tokens[token_num];
201 memcpy(&token_hdr, token_p, sizeof(token_hdr));
208 /* in case we want to match a specific num of token */
209 if (nb_match_token) {
210 if (i == nb_match_token) {
216 /* we don't match all the tokens */
221 /* are there are some tokens more */
222 while (isblank2(*buf)) {
227 if ( isendofline(*buf) || iscomment(*buf) )
230 /* garbage after inst */
236 cmdline_parse(struct cmdline *cl, const char * buf)
238 unsigned int inst_num=0;
239 cmdline_parse_inst_t *inst;
241 char result_buf[CMDLINE_PARSE_RESULT_BUFSIZE];
242 void (*f)(void *, struct cmdline *, void *) = NULL;
247 int err = CMDLINE_PARSE_NOMATCH;
249 cmdline_parse_ctx_t *ctx;
250 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
251 char debug_buf[BUFSIZ];
255 return CMDLINE_PARSE_BAD_ARGS;
260 * - look if the buffer contains at least one line
261 * - look if line contains only spaces or comments
262 * - count line length
265 while (! isendofline(*curbuf)) {
266 if ( *curbuf == '\0' ) {
267 debug_printf("Incomplete buf (len=%d)\n", linelen);
270 if ( iscomment(*curbuf) ) {
273 if ( ! isblank2(*curbuf) && ! comment) {
280 /* skip all endofline chars */
281 while (isendofline(buf[linelen])) {
286 if ( parse_it == 0 ) {
287 debug_printf("Empty line (len=%d)\n", linelen);
291 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
292 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
293 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
297 inst = ctx[inst_num];
299 debug_printf("INST %d\n", inst_num);
302 tok = match_inst(inst, buf, 0, result_buf, sizeof(result_buf));
304 if (tok > 0) /* we matched at least one token */
305 err = CMDLINE_PARSE_BAD_ARGS;
308 debug_printf("INST fully parsed\n");
310 while (isblank2(*curbuf)) {
314 /* if end of buf -> there is no garbage after inst */
315 if (isendofline(*curbuf) || iscomment(*curbuf)) {
317 memcpy(&f, &inst->f, sizeof(f));
318 memcpy(&data, &inst->data, sizeof(data));
321 /* more than 1 inst matches */
322 err = CMDLINE_PARSE_AMBIGUOUS;
324 debug_printf("Ambiguous cmd\n");
331 inst = ctx[inst_num];
336 f(result_buf, cl, data);
341 debug_printf("No match err=%d\n", err);
349 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
350 char *dst, unsigned int size)
352 const char *partial_tok = buf;
353 unsigned int inst_num = 0;
354 cmdline_parse_inst_t *inst;
355 cmdline_parse_token_hdr_t *token_p;
356 struct cmdline_token_hdr token_hdr;
357 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
358 unsigned int partial_tok_len;
364 unsigned int nb_completable;
365 unsigned int nb_non_completable;
367 const char *help_str;
368 cmdline_parse_ctx_t *ctx;
370 if (!cl || !buf || !state || !dst)
375 debug_printf("%s called\n", __func__);
376 memset(&token_hdr, 0, sizeof(token_hdr));
378 /* count the number of complete token to parse */
379 for (i=0 ; buf[i] ; i++) {
380 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
382 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
383 partial_tok = buf+i+1;
385 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
387 /* first call -> do a first pass */
389 debug_printf("try complete <%s>\n", buf);
390 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
391 nb_token, partial_tok);
394 nb_non_completable = 0;
396 inst = ctx[inst_num];
398 /* parse the first tokens of the inst */
399 if (nb_token && match_inst(inst, buf, nb_token, NULL, 0))
402 debug_printf("instruction match\n");
403 token_p = inst->tokens[nb_token];
405 memcpy(&token_hdr, token_p, sizeof(token_hdr));
407 /* non completable */
409 !token_hdr.ops->complete_get_nb ||
410 !token_hdr.ops->complete_get_elt ||
411 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
412 nb_non_completable++;
416 debug_printf("%d choices for this token\n", n);
417 for (i=0 ; i<n ; i++) {
418 if (token_hdr.ops->complete_get_elt(token_p, i,
423 /* we have at least room for one char */
424 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
425 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
426 tmpbuf[tmp_len] = ' ';
427 tmpbuf[tmp_len+1] = 0;
430 debug_printf(" choice <%s>\n", tmpbuf);
432 /* does the completion match the
433 * beginning of the word ? */
434 if (!strncmp(partial_tok, tmpbuf,
436 if (comp_len == -1) {
437 snprintf(comp_buf, sizeof(comp_buf),
438 "%s", tmpbuf + partial_tok_len);
440 strnlen(tmpbuf + partial_tok_len,
441 sizeof(tmpbuf) - partial_tok_len);
446 nb_common_chars(comp_buf,
447 tmpbuf+partial_tok_len);
448 comp_buf[comp_len] = 0;
454 debug_printf("next\n");
456 inst = ctx[inst_num];
459 debug_printf("total choices %d for this completion\n",
462 /* no possible completion */
463 if (nb_completable == 0 && nb_non_completable == 0)
466 /* if multichoice is not required */
467 if (*state == 0 && partial_tok_len > 0) {
468 /* one or several choices starting with the
471 if ((unsigned)(comp_len + 1) > size)
474 snprintf(dst, size, "%s", comp_buf);
481 /* init state correctly */
485 debug_printf("Multiple choice STATE=%d\n", *state);
488 inst = ctx[inst_num];
490 /* we need to redo it */
491 inst = ctx[inst_num];
493 if (nb_token && match_inst(inst, buf, nb_token, NULL, 0))
496 token_p = inst->tokens[nb_token];
498 memcpy(&token_hdr, token_p, sizeof(token_hdr));
500 /* one choice for this token */
502 !token_hdr.ops->complete_get_nb ||
503 !token_hdr.ops->complete_get_elt ||
504 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
505 if (local_state < *state) {
510 if (token_p && token_hdr.ops->get_help) {
511 token_hdr.ops->get_help(token_p, tmpbuf,
513 help_str = inst->help_str;
515 snprintf(dst, size, "[%s]: %s", tmpbuf,
518 snprintf(dst, size, "[%s]: No help",
522 snprintf(dst, size, "[RETURN]");
527 /* several choices */
528 for (i=0 ; i<n ; i++) {
529 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
532 /* we have at least room for one char */
533 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
534 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
535 tmpbuf[tmp_len] = ' ';
536 tmpbuf[tmp_len + 1] = 0;
539 debug_printf(" choice <%s>\n", tmpbuf);
541 /* does the completion match the beginning of
543 if (!strncmp(partial_tok, tmpbuf,
545 if (local_state < *state) {
550 l=snprintf(dst, size, "%s", tmpbuf);
551 if (l>=0 && token_hdr.ops->get_help) {
552 token_hdr.ops->get_help(token_p, tmpbuf,
554 help_str = inst->help_str;
556 snprintf(dst+l, size-l, "[%s]: %s",
559 snprintf(dst+l, size-l,
560 "[%s]: No help", tmpbuf);
568 inst = ctx[inst_num];