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;
259 char buf[CMDLINE_PARSE_RESULT_BUFSIZE];
260 long double align; /* strong alignment constraint for buf */
262 cmdline_parse_token_hdr_t *dyn_tokens[CMDLINE_PARSE_DYNAMIC_TOKENS];
263 void (*f)(void *, struct cmdline *, void *) = NULL;
268 int err = CMDLINE_PARSE_NOMATCH;
270 cmdline_parse_ctx_t *ctx;
271 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
272 char debug_buf[BUFSIZ];
276 return CMDLINE_PARSE_BAD_ARGS;
279 memset(&dyn_tokens, 0, sizeof(dyn_tokens));
282 * - look if the buffer contains at least one line
283 * - look if line contains only spaces or comments
284 * - count line length
287 while (! isendofline(*curbuf)) {
288 if ( *curbuf == '\0' ) {
289 debug_printf("Incomplete buf (len=%d)\n", linelen);
292 if ( iscomment(*curbuf) ) {
295 if ( ! isblank2(*curbuf) && ! comment) {
302 /* skip all endofline chars */
303 while (isendofline(buf[linelen])) {
308 if ( parse_it == 0 ) {
309 debug_printf("Empty line (len=%d)\n", linelen);
313 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
314 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
315 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
319 inst = ctx[inst_num];
321 debug_printf("INST %d\n", inst_num);
324 tok = match_inst(inst, buf, 0, result.buf, sizeof(result.buf),
327 if (tok > 0) /* we matched at least one token */
328 err = CMDLINE_PARSE_BAD_ARGS;
331 debug_printf("INST fully parsed\n");
333 while (isblank2(*curbuf)) {
337 /* if end of buf -> there is no garbage after inst */
338 if (isendofline(*curbuf) || iscomment(*curbuf)) {
340 memcpy(&f, &inst->f, sizeof(f));
341 memcpy(&data, &inst->data, sizeof(data));
344 /* more than 1 inst matches */
345 err = CMDLINE_PARSE_AMBIGUOUS;
347 debug_printf("Ambiguous cmd\n");
354 inst = ctx[inst_num];
359 f(result.buf, cl, data);
364 debug_printf("No match err=%d\n", err);
372 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
373 char *dst, unsigned int size)
375 const char *partial_tok = buf;
376 unsigned int inst_num = 0;
377 cmdline_parse_inst_t *inst;
378 cmdline_parse_token_hdr_t *token_p;
379 struct cmdline_token_hdr token_hdr;
380 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
381 cmdline_parse_token_hdr_t *dyn_tokens[CMDLINE_PARSE_DYNAMIC_TOKENS];
382 unsigned int partial_tok_len;
388 unsigned int nb_completable;
389 unsigned int nb_non_completable;
391 const char *help_str;
392 cmdline_parse_ctx_t *ctx;
394 if (!cl || !buf || !state || !dst)
399 debug_printf("%s called\n", __func__);
400 memset(&token_hdr, 0, sizeof(token_hdr));
401 memset(&dyn_tokens, 0, sizeof(dyn_tokens));
403 /* count the number of complete token to parse */
404 for (i=0 ; buf[i] ; i++) {
405 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
407 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
408 partial_tok = buf+i+1;
410 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
412 /* first call -> do a first pass */
414 debug_printf("try complete <%s>\n", buf);
415 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
416 nb_token, partial_tok);
419 nb_non_completable = 0;
421 inst = ctx[inst_num];
423 /* parse the first tokens of the inst */
425 match_inst(inst, buf, nb_token, NULL, 0,
429 debug_printf("instruction match\n");
430 if (!inst->tokens[0]) {
432 (CMDLINE_PARSE_DYNAMIC_TOKENS - 1)) {
433 if (!dyn_tokens[nb_token])
434 inst->f(&dyn_tokens[nb_token],
437 token_p = dyn_tokens[nb_token];
441 token_p = inst->tokens[nb_token];
443 memcpy(&token_hdr, token_p, sizeof(token_hdr));
445 /* non completable */
447 !token_hdr.ops->complete_get_nb ||
448 !token_hdr.ops->complete_get_elt ||
449 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
450 nb_non_completable++;
454 debug_printf("%d choices for this token\n", n);
455 for (i=0 ; i<n ; i++) {
456 if (token_hdr.ops->complete_get_elt(token_p, i,
461 /* we have at least room for one char */
462 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
463 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
464 tmpbuf[tmp_len] = ' ';
465 tmpbuf[tmp_len+1] = 0;
468 debug_printf(" choice <%s>\n", tmpbuf);
470 /* does the completion match the
471 * beginning of the word ? */
472 if (!strncmp(partial_tok, tmpbuf,
474 if (comp_len == -1) {
475 snprintf(comp_buf, sizeof(comp_buf),
476 "%s", tmpbuf + partial_tok_len);
478 strnlen(tmpbuf + partial_tok_len,
479 sizeof(tmpbuf) - partial_tok_len);
484 nb_common_chars(comp_buf,
485 tmpbuf+partial_tok_len);
486 comp_buf[comp_len] = 0;
492 debug_printf("next\n");
494 inst = ctx[inst_num];
497 debug_printf("total choices %d for this completion\n",
500 /* no possible completion */
501 if (nb_completable == 0 && nb_non_completable == 0)
504 /* if multichoice is not required */
505 if (*state == 0 && partial_tok_len > 0) {
506 /* one or several choices starting with the
509 if ((unsigned)(comp_len + 1) > size)
512 snprintf(dst, size, "%s", comp_buf);
519 /* init state correctly */
523 debug_printf("Multiple choice STATE=%d\n", *state);
526 inst = ctx[inst_num];
528 /* we need to redo it */
529 inst = ctx[inst_num];
532 match_inst(inst, buf, nb_token, NULL, 0, &dyn_tokens))
535 if (!inst->tokens[0]) {
536 if (nb_token < (CMDLINE_PARSE_DYNAMIC_TOKENS - 1)) {
537 if (!dyn_tokens[nb_token])
538 inst->f(&dyn_tokens[nb_token],
541 token_p = dyn_tokens[nb_token];
545 token_p = inst->tokens[nb_token];
547 memcpy(&token_hdr, token_p, sizeof(token_hdr));
549 /* one choice for this token */
551 !token_hdr.ops->complete_get_nb ||
552 !token_hdr.ops->complete_get_elt ||
553 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
554 if (local_state < *state) {
559 if (token_p && token_hdr.ops->get_help) {
560 token_hdr.ops->get_help(token_p, tmpbuf,
562 help_str = inst->help_str;
564 snprintf(dst, size, "[%s]: %s", tmpbuf,
567 snprintf(dst, size, "[%s]: No help",
571 snprintf(dst, size, "[RETURN]");
576 /* several choices */
577 for (i=0 ; i<n ; i++) {
578 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
581 /* we have at least room for one char */
582 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
583 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
584 tmpbuf[tmp_len] = ' ';
585 tmpbuf[tmp_len + 1] = 0;
588 debug_printf(" choice <%s>\n", tmpbuf);
590 /* does the completion match the beginning of
592 if (!strncmp(partial_tok, tmpbuf,
594 if (local_state < *state) {
599 l=snprintf(dst, size, "%s", tmpbuf);
600 if (l>=0 && token_hdr.ops->get_help) {
601 token_hdr.ops->get_help(token_p, tmpbuf,
603 help_str = inst->help_str;
605 snprintf(dst+l, size-l, "[%s]: %s",
608 snprintf(dst+l, size-l,
609 "[%s]: No help", tmpbuf);
617 inst = ctx[inst_num];