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) {
142 /** Retrieve either static or dynamic token at a given index. */
143 static cmdline_parse_token_hdr_t *
144 get_token(cmdline_parse_inst_t *inst, unsigned int index)
146 cmdline_parse_token_hdr_t *token_p;
148 /* check presence of static tokens first */
149 if (inst->tokens[0] || !inst->f)
150 return inst->tokens[index];
151 /* generate dynamic token */
153 inst->f(&token_p, NULL, &inst->tokens[index]);
158 * try to match the buffer with an instruction (only the first
159 * nb_match_token tokens if != 0). Return 0 if we match all the
160 * tokens, else the number of matched tokens, else -1.
163 match_inst(cmdline_parse_inst_t *inst, const char *buf,
164 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size)
166 cmdline_parse_token_hdr_t *token_p = NULL;
169 struct cmdline_token_hdr token_hdr;
172 memset(resbuf, 0, resbuf_size);
173 /* check if we match all tokens of inst */
174 while (!nb_match_token || i < nb_match_token) {
175 token_p = get_token(inst, i);
178 memcpy(&token_hdr, token_p, sizeof(token_hdr));
180 debug_printf("TK\n");
182 while (isblank2(*buf)) {
187 if ( isendofline(*buf) || iscomment(*buf) )
190 if (resbuf == NULL) {
191 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
195 if (token_hdr.offset > resbuf_size) {
196 printf("Parse error(%s:%d): Token offset(%u) "
197 "exceeds maximum size(%u)\n",
199 token_hdr.offset, resbuf_size);
202 rb_sz = resbuf_size - token_hdr.offset;
204 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
205 token_hdr.offset, rb_sz);
211 debug_printf("TK parsed (len=%d)\n", n);
220 /* in case we want to match a specific num of token */
221 if (nb_match_token) {
222 if (i == nb_match_token) {
228 /* we don't match all the tokens */
233 /* are there are some tokens more */
234 while (isblank2(*buf)) {
239 if ( isendofline(*buf) || iscomment(*buf) )
242 /* garbage after inst */
248 cmdline_parse(struct cmdline *cl, const char * buf)
250 unsigned int inst_num=0;
251 cmdline_parse_inst_t *inst;
254 char buf[CMDLINE_PARSE_RESULT_BUFSIZE];
255 long double align; /* strong alignment constraint for buf */
256 } result, tmp_result;
257 void (*f)(void *, struct cmdline *, void *) = NULL;
262 int err = CMDLINE_PARSE_NOMATCH;
264 cmdline_parse_ctx_t *ctx;
265 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
266 char debug_buf[BUFSIZ];
268 char *result_buf = result.buf;
271 return CMDLINE_PARSE_BAD_ARGS;
276 * - look if the buffer contains at least one line
277 * - look if line contains only spaces or comments
278 * - count line length
281 while (! isendofline(*curbuf)) {
282 if ( *curbuf == '\0' ) {
283 debug_printf("Incomplete buf (len=%d)\n", linelen);
286 if ( iscomment(*curbuf) ) {
289 if ( ! isblank2(*curbuf) && ! comment) {
296 /* skip all endofline chars */
297 while (isendofline(buf[linelen])) {
302 if ( parse_it == 0 ) {
303 debug_printf("Empty line (len=%d)\n", linelen);
307 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
308 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
309 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
313 inst = ctx[inst_num];
315 debug_printf("INST %d\n", inst_num);
318 tok = match_inst(inst, buf, 0, result_buf,
319 CMDLINE_PARSE_RESULT_BUFSIZE);
321 if (tok > 0) /* we matched at least one token */
322 err = CMDLINE_PARSE_BAD_ARGS;
325 debug_printf("INST fully parsed\n");
327 while (isblank2(*curbuf)) {
331 /* if end of buf -> there is no garbage after inst */
332 if (isendofline(*curbuf) || iscomment(*curbuf)) {
334 memcpy(&f, &inst->f, sizeof(f));
335 memcpy(&data, &inst->data, sizeof(data));
336 result_buf = tmp_result.buf;
339 /* more than 1 inst matches */
340 err = CMDLINE_PARSE_AMBIGUOUS;
342 debug_printf("Ambiguous cmd\n");
349 inst = ctx[inst_num];
354 f(result.buf, cl, data);
359 debug_printf("No match err=%d\n", err);
367 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
368 char *dst, unsigned int size)
370 const char *partial_tok = buf;
371 unsigned int inst_num = 0;
372 cmdline_parse_inst_t *inst;
373 cmdline_parse_token_hdr_t *token_p;
374 struct cmdline_token_hdr token_hdr;
375 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
376 unsigned int partial_tok_len;
382 unsigned int nb_completable;
383 unsigned int nb_non_completable;
385 const char *help_str;
386 cmdline_parse_ctx_t *ctx;
388 if (!cl || !buf || !state || !dst)
393 debug_printf("%s called\n", __func__);
394 memset(&token_hdr, 0, sizeof(token_hdr));
396 /* count the number of complete token to parse */
397 for (i=0 ; buf[i] ; i++) {
398 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
400 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
401 partial_tok = buf+i+1;
403 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
405 /* first call -> do a first pass */
407 debug_printf("try complete <%s>\n", buf);
408 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
409 nb_token, partial_tok);
412 nb_non_completable = 0;
414 inst = ctx[inst_num];
416 /* parse the first tokens of the inst */
418 match_inst(inst, buf, nb_token, NULL, 0))
421 debug_printf("instruction match\n");
422 token_p = get_token(inst, nb_token);
424 memcpy(&token_hdr, token_p, sizeof(token_hdr));
426 /* non completable */
428 !token_hdr.ops->complete_get_nb ||
429 !token_hdr.ops->complete_get_elt ||
430 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
431 nb_non_completable++;
435 debug_printf("%d choices for this token\n", n);
436 for (i=0 ; i<n ; i++) {
437 if (token_hdr.ops->complete_get_elt(token_p, i,
442 /* we have at least room for one char */
443 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
444 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
445 tmpbuf[tmp_len] = ' ';
446 tmpbuf[tmp_len+1] = 0;
449 debug_printf(" choice <%s>\n", tmpbuf);
451 /* does the completion match the
452 * beginning of the word ? */
453 if (!strncmp(partial_tok, tmpbuf,
455 if (comp_len == -1) {
456 snprintf(comp_buf, sizeof(comp_buf),
457 "%s", tmpbuf + partial_tok_len);
459 strnlen(tmpbuf + partial_tok_len,
460 sizeof(tmpbuf) - partial_tok_len);
465 nb_common_chars(comp_buf,
466 tmpbuf+partial_tok_len);
467 comp_buf[comp_len] = 0;
473 debug_printf("next\n");
475 inst = ctx[inst_num];
478 debug_printf("total choices %d for this completion\n",
481 /* no possible completion */
482 if (nb_completable == 0 && nb_non_completable == 0)
485 /* if multichoice is not required */
486 if (*state == 0 && partial_tok_len > 0) {
487 /* one or several choices starting with the
490 if ((unsigned)(comp_len + 1) > size)
493 snprintf(dst, size, "%s", comp_buf);
500 /* init state correctly */
504 debug_printf("Multiple choice STATE=%d\n", *state);
507 inst = ctx[inst_num];
509 /* we need to redo it */
510 inst = ctx[inst_num];
513 match_inst(inst, buf, nb_token, NULL, 0))
516 token_p = get_token(inst, nb_token);
518 memcpy(&token_hdr, token_p, sizeof(token_hdr));
520 /* one choice for this token */
522 !token_hdr.ops->complete_get_nb ||
523 !token_hdr.ops->complete_get_elt ||
524 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
525 if (local_state < *state) {
530 if (token_p && token_hdr.ops->get_help) {
531 token_hdr.ops->get_help(token_p, tmpbuf,
533 help_str = inst->help_str;
535 snprintf(dst, size, "[%s]: %s", tmpbuf,
538 snprintf(dst, size, "[%s]: No help",
542 snprintf(dst, size, "[RETURN]");
547 /* several choices */
548 for (i=0 ; i<n ; i++) {
549 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
552 /* we have at least room for one char */
553 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
554 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
555 tmpbuf[tmp_len] = ' ';
556 tmpbuf[tmp_len + 1] = 0;
559 debug_printf(" choice <%s>\n", tmpbuf);
561 /* does the completion match the beginning of
563 if (!strncmp(partial_tok, tmpbuf,
565 if (local_state < *state) {
570 l=snprintf(dst, size, "%s", tmpbuf);
571 if (l>=0 && token_hdr.ops->get_help) {
572 token_hdr.ops->get_help(token_p, tmpbuf,
574 help_str = inst->help_str;
576 snprintf(dst+l, size-l, "[%s]: %s",
579 snprintf(dst+l, size-l,
580 "[%s]: No help", tmpbuf);
588 inst = ctx[inst_num];