4 * Copyright(c) 2010-2012 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.
36 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
37 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are met:
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * * Neither the name of the University of California, Berkeley nor the
47 * names of its contributors may be used to endorse or promote products
48 * derived from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
51 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
54 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 #include <netinet/in.h>
72 #include <rte_string_fns.h>
74 #include "cmdline_rdline.h"
75 #include "cmdline_parse.h"
78 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
79 #define debug_printf printf
81 #define debug_printf(args...) do {} while(0)
84 #define CMDLINE_BUFFER_SIZE 64
86 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
115 cmdline_isendoftoken(char c)
117 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
123 nb_common_chars(const char * s1, const char * s2)
127 while (*s1==*s2 && *s1) {
136 * try to match the buffer with an instruction (only the first
137 * nb_match_token tokens if != 0). Return 0 if we match all the
138 * tokens, else the number of matched tokens, else -1.
141 match_inst(cmdline_parse_inst_t *inst, const char *buf,
142 unsigned int nb_match_token, void * result_buf)
144 unsigned int token_num=0;
145 cmdline_parse_token_hdr_t * token_p;
148 struct cmdline_token_hdr token_hdr;
150 token_p = inst->tokens[token_num];
152 memcpy(&token_hdr, token_p, sizeof(token_hdr));
154 /* check if we match all tokens of inst */
155 while (token_p && (!nb_match_token || i<nb_match_token)) {
156 debug_printf("TK\n");
158 while (isblank2(*buf)) {
163 if ( isendofline(*buf) || iscomment(*buf) )
167 n = token_hdr.ops->parse(token_p, buf,
171 n = token_hdr.ops->parse(token_p, buf, NULL);
176 debug_printf("TK parsed (len=%d)\n", n);
181 token_p = inst->tokens[token_num];
183 memcpy(&token_hdr, token_p, sizeof(token_hdr));
190 /* in case we want to match a specific num of token */
191 if (nb_match_token) {
192 if (i == nb_match_token) {
198 /* we don't match all the tokens */
203 /* are there are some tokens more */
204 while (isblank2(*buf)) {
209 if ( isendofline(*buf) || iscomment(*buf) )
212 /* garbage after inst */
218 cmdline_parse(struct cmdline *cl, const char * buf)
220 unsigned int inst_num=0;
221 cmdline_parse_inst_t *inst;
223 char result_buf[BUFSIZ];
224 void (*f)(void *, struct cmdline *, void *) = NULL;
229 int err = CMDLINE_PARSE_NOMATCH;
231 cmdline_parse_ctx_t *ctx;
232 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
233 char debug_buf[BUFSIZ];
237 return CMDLINE_PARSE_BAD_ARGS;
242 * - look if the buffer contains at least one line
243 * - look if line contains only spaces or comments
244 * - count line length
247 while (! isendofline(*curbuf)) {
248 if ( *curbuf == '\0' ) {
249 debug_printf("Incomplete buf (len=%d)\n", linelen);
252 if ( iscomment(*curbuf) ) {
255 if ( ! isblank2(*curbuf) && ! comment) {
262 /* skip all endofline chars */
263 while (isendofline(buf[linelen])) {
268 if ( parse_it == 0 ) {
269 debug_printf("Empty line (len=%d)\n", linelen);
273 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
274 rte_snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
275 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
279 inst = ctx[inst_num];
281 debug_printf("INST %d\n", inst_num);
284 tok = match_inst(inst, buf, 0, result_buf);
286 if (tok > 0) /* we matched at least one token */
287 err = CMDLINE_PARSE_BAD_ARGS;
290 debug_printf("INST fully parsed\n");
292 while (isblank2(*curbuf)) {
296 /* if end of buf -> there is no garbage after inst */
297 if (isendofline(*curbuf) || iscomment(*curbuf)) {
299 memcpy(&f, &inst->f, sizeof(f));
300 memcpy(&data, &inst->data, sizeof(data));
303 /* more than 1 inst matches */
304 err = CMDLINE_PARSE_AMBIGUOUS;
306 debug_printf("Ambiguous cmd\n");
313 inst = ctx[inst_num];
318 f(result_buf, cl, data);
323 debug_printf("No match err=%d\n", err);
331 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
332 char *dst, unsigned int size)
334 const char *partial_tok = buf;
335 unsigned int inst_num = 0;
336 cmdline_parse_inst_t *inst;
337 cmdline_parse_token_hdr_t *token_p;
338 struct cmdline_token_hdr token_hdr;
339 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
340 unsigned int partial_tok_len;
346 unsigned int nb_completable;
347 unsigned int nb_non_completable;
349 const char *help_str;
350 cmdline_parse_ctx_t *ctx;
352 if (!cl || !buf || !state || !dst)
357 debug_printf("%s called\n", __func__);
358 memset(&token_hdr, 0, sizeof(token_hdr));
360 /* count the number of complete token to parse */
361 for (i=0 ; buf[i] ; i++) {
362 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
364 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
365 partial_tok = buf+i+1;
367 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
369 /* first call -> do a first pass */
371 debug_printf("try complete <%s>\n", buf);
372 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
373 nb_token, partial_tok);
376 nb_non_completable = 0;
378 inst = ctx[inst_num];
380 /* parse the first tokens of the inst */
381 if (nb_token && match_inst(inst, buf, nb_token, NULL))
384 debug_printf("instruction match \n");
385 token_p = inst->tokens[nb_token];
387 memcpy(&token_hdr, token_p, sizeof(token_hdr));
389 /* non completable */
391 !token_hdr.ops->complete_get_nb ||
392 !token_hdr.ops->complete_get_elt ||
393 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
394 nb_non_completable++;
398 debug_printf("%d choices for this token\n", n);
399 for (i=0 ; i<n ; i++) {
400 if (token_hdr.ops->complete_get_elt(token_p, i,
405 /* we have at least room for one char */
406 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
407 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
408 tmpbuf[tmp_len] = ' ';
409 tmpbuf[tmp_len+1] = 0;
412 debug_printf(" choice <%s>\n", tmpbuf);
414 /* does the completion match the
415 * beginning of the word ? */
416 if (!strncmp(partial_tok, tmpbuf,
418 if (comp_len == -1) {
419 rte_snprintf(comp_buf, sizeof(comp_buf),
420 "%s", tmpbuf + partial_tok_len);
422 strnlen(tmpbuf + partial_tok_len,
423 sizeof(tmpbuf) - partial_tok_len);
428 nb_common_chars(comp_buf,
429 tmpbuf+partial_tok_len);
430 comp_buf[comp_len] = 0;
436 debug_printf("next\n");
438 inst = ctx[inst_num];
441 debug_printf("total choices %d for this completion\n",
444 /* no possible completion */
445 if (nb_completable == 0 && nb_non_completable == 0)
448 /* if multichoice is not required */
449 if (*state == 0 && partial_tok_len > 0) {
450 /* one or several choices starting with the
453 if ((unsigned)(comp_len + 1) > size)
456 rte_snprintf(dst, size, "%s", comp_buf);
463 /* init state correctly */
467 debug_printf("Multiple choice STATE=%d\n", *state);
470 inst = ctx[inst_num];
472 /* we need to redo it */
473 inst = ctx[inst_num];
475 if (nb_token && match_inst(inst, buf, nb_token, NULL))
478 token_p = inst->tokens[nb_token];
480 memcpy(&token_hdr, token_p, sizeof(token_hdr));
482 /* one choice for this token */
484 !token_hdr.ops->complete_get_nb ||
485 !token_hdr.ops->complete_get_elt ||
486 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
487 if (local_state < *state) {
492 if (token_p && token_hdr.ops->get_help) {
493 token_hdr.ops->get_help(token_p, tmpbuf,
495 help_str = inst->help_str;
497 rte_snprintf(dst, size, "[%s]: %s", tmpbuf,
500 rte_snprintf(dst, size, "[%s]: No help",
504 rte_snprintf(dst, size, "[RETURN]");
509 /* several choices */
510 for (i=0 ; i<n ; i++) {
511 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
514 /* we have at least room for one char */
515 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
516 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
517 tmpbuf[tmp_len] = ' ';
518 tmpbuf[tmp_len + 1] = 0;
521 debug_printf(" choice <%s>\n", tmpbuf);
523 /* does the completion match the beginning of
525 if (!strncmp(partial_tok, tmpbuf,
527 if (local_state < *state) {
532 l=rte_snprintf(dst, size, "%s", tmpbuf);
533 if (l>=0 && token_hdr.ops->get_help) {
534 token_hdr.ops->get_help(token_p, tmpbuf,
536 help_str = inst->help_str;
538 rte_snprintf(dst+l, size-l, "[%s]: %s",
541 rte_snprintf(dst+l, size-l,
542 "[%s]: No help", tmpbuf);
550 inst = ctx[inst_num];