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 nb_common_chars(const char * s1, const char * s2)
126 while (*s1==*s2 && *s1) {
135 * try to match the buffer with an instruction (only the first
136 * nb_match_token tokens if != 0). Return 0 if we match all the
137 * tokens, else the number of matched tokens, else -1.
140 match_inst(cmdline_parse_inst_t *inst, const char *buf,
141 unsigned int nb_match_token, void * result_buf)
143 unsigned int token_num=0;
144 cmdline_parse_token_hdr_t * token_p;
147 struct cmdline_token_hdr token_hdr;
149 token_p = inst->tokens[token_num];
151 memcpy(&token_hdr, token_p, sizeof(token_hdr));
153 /* check if we match all tokens of inst */
154 while (token_p && (!nb_match_token || i<nb_match_token)) {
155 debug_printf("TK\n");
157 while (isblank2(*buf)) {
162 if ( isendofline(*buf) || iscomment(*buf) )
166 n = token_hdr.ops->parse(token_p, buf,
170 n = token_hdr.ops->parse(token_p, buf, NULL);
175 debug_printf("TK parsed (len=%d)\n", n);
180 token_p = inst->tokens[token_num];
182 memcpy(&token_hdr, token_p, sizeof(token_hdr));
189 /* in case we want to match a specific num of token */
190 if (nb_match_token) {
191 if (i == nb_match_token) {
197 /* we don't match all the tokens */
202 /* are there are some tokens more */
203 while (isblank2(*buf)) {
208 if ( isendofline(*buf) || iscomment(*buf) )
211 /* garbage after inst */
217 cmdline_parse(struct cmdline *cl, const char * buf)
219 unsigned int inst_num=0;
220 cmdline_parse_inst_t *inst;
222 char result_buf[BUFSIZ];
223 void (*f)(void *, struct cmdline *, void *) = NULL;
228 int err = CMDLINE_PARSE_NOMATCH;
230 cmdline_parse_ctx_t *ctx;
231 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
232 char debug_buf[BUFSIZ];
236 return CMDLINE_PARSE_BAD_ARGS;
241 * - look if the buffer contains at least one line
242 * - look if line contains only spaces or comments
243 * - count line length
246 while (! isendofline(*curbuf)) {
247 if ( *curbuf == '\0' ) {
248 debug_printf("Incomplete buf (len=%d)\n", linelen);
251 if ( iscomment(*curbuf) ) {
254 if ( ! isblank2(*curbuf) && ! comment) {
261 /* skip all endofline chars */
262 while (isendofline(buf[linelen])) {
267 if ( parse_it == 0 ) {
268 debug_printf("Empty line (len=%d)\n", linelen);
272 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
273 rte_snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
274 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
278 inst = ctx[inst_num];
280 debug_printf("INST %d\n", inst_num);
283 tok = match_inst(inst, buf, 0, result_buf);
285 if (tok > 0) /* we matched at least one token */
286 err = CMDLINE_PARSE_BAD_ARGS;
289 debug_printf("INST fully parsed\n");
291 while (isblank2(*curbuf)) {
295 /* if end of buf -> there is no garbage after inst */
296 if (isendofline(*curbuf) || iscomment(*curbuf)) {
298 memcpy(&f, &inst->f, sizeof(f));
299 memcpy(&data, &inst->data, sizeof(data));
302 /* more than 1 inst matches */
303 err = CMDLINE_PARSE_AMBIGUOUS;
305 debug_printf("Ambiguous cmd\n");
312 inst = ctx[inst_num];
317 f(result_buf, cl, data);
322 debug_printf("No match err=%d\n", err);
330 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
331 char *dst, unsigned int size)
333 const char *partial_tok = buf;
334 unsigned int inst_num = 0;
335 cmdline_parse_inst_t *inst;
336 cmdline_parse_token_hdr_t *token_p;
337 struct cmdline_token_hdr token_hdr;
338 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
339 unsigned int partial_tok_len;
345 unsigned int nb_completable;
346 unsigned int nb_non_completable;
348 const char *help_str;
349 cmdline_parse_ctx_t *ctx;
351 if (!cl || !buf || !state || !dst)
356 debug_printf("%s called\n", __func__);
357 memset(&token_hdr, 0, sizeof(token_hdr));
359 /* count the number of complete token to parse */
360 for (i=0 ; buf[i] ; i++) {
361 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
363 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
364 partial_tok = buf+i+1;
366 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
368 /* first call -> do a first pass */
370 debug_printf("try complete <%s>\n", buf);
371 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
372 nb_token, partial_tok);
375 nb_non_completable = 0;
377 inst = ctx[inst_num];
379 /* parse the first tokens of the inst */
380 if (nb_token && match_inst(inst, buf, nb_token, NULL))
383 debug_printf("instruction match \n");
384 token_p = inst->tokens[nb_token];
386 memcpy(&token_hdr, token_p, sizeof(token_hdr));
388 /* non completable */
390 !token_hdr.ops->complete_get_nb ||
391 !token_hdr.ops->complete_get_elt ||
392 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
393 nb_non_completable++;
397 debug_printf("%d choices for this token\n", n);
398 for (i=0 ; i<n ; i++) {
399 if (token_hdr.ops->complete_get_elt(token_p, i,
404 /* we have at least room for one char */
405 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
406 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
407 tmpbuf[tmp_len] = ' ';
408 tmpbuf[tmp_len+1] = 0;
411 debug_printf(" choice <%s>\n", tmpbuf);
413 /* does the completion match the
414 * beginning of the word ? */
415 if (!strncmp(partial_tok, tmpbuf,
417 if (comp_len == -1) {
418 rte_snprintf(comp_buf, sizeof(comp_buf),
419 "%s", tmpbuf + partial_tok_len);
421 strnlen(tmpbuf + partial_tok_len,
422 sizeof(tmpbuf) - partial_tok_len);
427 nb_common_chars(comp_buf,
428 tmpbuf+partial_tok_len);
429 comp_buf[comp_len] = 0;
435 debug_printf("next\n");
437 inst = ctx[inst_num];
440 debug_printf("total choices %d for this completion\n",
443 /* no possible completion */
444 if (nb_completable == 0 && nb_non_completable == 0)
447 /* if multichoice is not required */
448 if (*state == 0 && partial_tok_len > 0) {
449 /* one or several choices starting with the
452 if ((unsigned)(comp_len + 1) > size)
455 rte_snprintf(dst, size, "%s", comp_buf);
462 /* init state correctly */
466 debug_printf("Multiple choice STATE=%d\n", *state);
469 inst = ctx[inst_num];
471 /* we need to redo it */
472 inst = ctx[inst_num];
474 if (nb_token && match_inst(inst, buf, nb_token, NULL))
477 token_p = inst->tokens[nb_token];
479 memcpy(&token_hdr, token_p, sizeof(token_hdr));
481 /* one choice for this token */
483 !token_hdr.ops->complete_get_nb ||
484 !token_hdr.ops->complete_get_elt ||
485 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
486 if (local_state < *state) {
491 if (token_p && token_hdr.ops->get_help) {
492 token_hdr.ops->get_help(token_p, tmpbuf,
494 help_str = inst->help_str;
496 rte_snprintf(dst, size, "[%s]: %s", tmpbuf,
499 rte_snprintf(dst, size, "[%s]: No help",
503 rte_snprintf(dst, size, "[RETURN]");
508 /* several choices */
509 for (i=0 ; i<n ; i++) {
510 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
513 /* we have at least room for one char */
514 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
515 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
516 tmpbuf[tmp_len] = ' ';
517 tmpbuf[tmp_len + 1] = 0;
520 debug_printf(" choice <%s>\n", tmpbuf);
522 /* does the completion match the beginning of
524 if (!strncmp(partial_tok, tmpbuf,
526 if (local_state < *state) {
531 l=rte_snprintf(dst, size, "%s", tmpbuf);
532 if (l>=0 && token_hdr.ops->get_help) {
533 token_hdr.ops->get_help(token_p, tmpbuf,
535 help_str = inst->help_str;
537 rte_snprintf(dst+l, size-l, "[%s]: %s",
540 rte_snprintf(dst+l, size-l,
541 "[%s]: No help", tmpbuf);
549 inst = ctx[inst_num];