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;
171 /* check if we match all tokens of inst */
172 while (!nb_match_token || i < nb_match_token) {
173 token_p = get_token(inst, i);
176 memcpy(&token_hdr, token_p, sizeof(token_hdr));
178 debug_printf("TK\n");
180 while (isblank2(*buf)) {
185 if ( isendofline(*buf) || iscomment(*buf) )
188 if (resbuf == NULL) {
189 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
193 if (token_hdr.offset > resbuf_size) {
194 printf("Parse error(%s:%d): Token offset(%u) "
195 "exceeds maximum size(%u)\n",
197 token_hdr.offset, resbuf_size);
200 rb_sz = resbuf_size - token_hdr.offset;
202 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
203 token_hdr.offset, rb_sz);
209 debug_printf("TK parsed (len=%d)\n", n);
218 /* in case we want to match a specific num of token */
219 if (nb_match_token) {
220 if (i == nb_match_token) {
226 /* we don't match all the tokens */
231 /* are there are some tokens more */
232 while (isblank2(*buf)) {
237 if ( isendofline(*buf) || iscomment(*buf) )
240 /* garbage after inst */
246 cmdline_parse(struct cmdline *cl, const char * buf)
248 unsigned int inst_num=0;
249 cmdline_parse_inst_t *inst;
252 char buf[CMDLINE_PARSE_RESULT_BUFSIZE];
253 long double align; /* strong alignment constraint for buf */
254 } result, tmp_result;
255 void (*f)(void *, struct cmdline *, void *) = NULL;
260 int err = CMDLINE_PARSE_NOMATCH;
262 cmdline_parse_ctx_t *ctx;
263 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
264 char debug_buf[BUFSIZ];
268 return CMDLINE_PARSE_BAD_ARGS;
273 * - look if the buffer contains at least one line
274 * - look if line contains only spaces or comments
275 * - count line length
278 while (! isendofline(*curbuf)) {
279 if ( *curbuf == '\0' ) {
280 debug_printf("Incomplete buf (len=%d)\n", linelen);
283 if ( iscomment(*curbuf) ) {
286 if ( ! isblank2(*curbuf) && ! comment) {
293 /* skip all endofline chars */
294 while (isendofline(buf[linelen])) {
299 if ( parse_it == 0 ) {
300 debug_printf("Empty line (len=%d)\n", linelen);
304 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
305 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
306 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
310 inst = ctx[inst_num];
312 debug_printf("INST %d\n", inst_num);
315 tok = match_inst(inst, buf, 0, tmp_result.buf,
316 sizeof(tmp_result.buf));
318 if (tok > 0) /* we matched at least one token */
319 err = CMDLINE_PARSE_BAD_ARGS;
322 debug_printf("INST fully parsed\n");
323 memcpy(&result, &tmp_result,
326 while (isblank2(*curbuf)) {
330 /* if end of buf -> there is no garbage after inst */
331 if (isendofline(*curbuf) || iscomment(*curbuf)) {
333 memcpy(&f, &inst->f, sizeof(f));
334 memcpy(&data, &inst->data, sizeof(data));
337 /* more than 1 inst matches */
338 err = CMDLINE_PARSE_AMBIGUOUS;
340 debug_printf("Ambiguous cmd\n");
347 inst = ctx[inst_num];
352 f(result.buf, cl, data);
357 debug_printf("No match err=%d\n", err);
365 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
366 char *dst, unsigned int size)
368 const char *partial_tok = buf;
369 unsigned int inst_num = 0;
370 cmdline_parse_inst_t *inst;
371 cmdline_parse_token_hdr_t *token_p;
372 struct cmdline_token_hdr token_hdr;
373 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
374 unsigned int partial_tok_len;
380 unsigned int nb_completable;
381 unsigned int nb_non_completable;
383 const char *help_str;
384 cmdline_parse_ctx_t *ctx;
386 if (!cl || !buf || !state || !dst)
391 debug_printf("%s called\n", __func__);
392 memset(&token_hdr, 0, sizeof(token_hdr));
394 /* count the number of complete token to parse */
395 for (i=0 ; buf[i] ; i++) {
396 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
398 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
399 partial_tok = buf+i+1;
401 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
403 /* first call -> do a first pass */
405 debug_printf("try complete <%s>\n", buf);
406 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
407 nb_token, partial_tok);
410 nb_non_completable = 0;
412 inst = ctx[inst_num];
414 /* parse the first tokens of the inst */
416 match_inst(inst, buf, nb_token, NULL, 0))
419 debug_printf("instruction match\n");
420 token_p = get_token(inst, nb_token);
422 memcpy(&token_hdr, token_p, sizeof(token_hdr));
424 /* non completable */
426 !token_hdr.ops->complete_get_nb ||
427 !token_hdr.ops->complete_get_elt ||
428 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
429 nb_non_completable++;
433 debug_printf("%d choices for this token\n", n);
434 for (i=0 ; i<n ; i++) {
435 if (token_hdr.ops->complete_get_elt(token_p, i,
440 /* we have at least room for one char */
441 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
442 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
443 tmpbuf[tmp_len] = ' ';
444 tmpbuf[tmp_len+1] = 0;
447 debug_printf(" choice <%s>\n", tmpbuf);
449 /* does the completion match the
450 * beginning of the word ? */
451 if (!strncmp(partial_tok, tmpbuf,
453 if (comp_len == -1) {
454 snprintf(comp_buf, sizeof(comp_buf),
455 "%s", tmpbuf + partial_tok_len);
457 strnlen(tmpbuf + partial_tok_len,
458 sizeof(tmpbuf) - partial_tok_len);
463 nb_common_chars(comp_buf,
464 tmpbuf+partial_tok_len);
465 comp_buf[comp_len] = 0;
471 debug_printf("next\n");
473 inst = ctx[inst_num];
476 debug_printf("total choices %d for this completion\n",
479 /* no possible completion */
480 if (nb_completable == 0 && nb_non_completable == 0)
483 /* if multichoice is not required */
484 if (*state == 0 && partial_tok_len > 0) {
485 /* one or several choices starting with the
488 if ((unsigned)(comp_len + 1) > size)
491 snprintf(dst, size, "%s", comp_buf);
498 /* init state correctly */
502 debug_printf("Multiple choice STATE=%d\n", *state);
505 inst = ctx[inst_num];
507 /* we need to redo it */
508 inst = ctx[inst_num];
511 match_inst(inst, buf, nb_token, NULL, 0))
514 token_p = get_token(inst, nb_token);
516 memcpy(&token_hdr, token_p, sizeof(token_hdr));
518 /* one choice for this token */
520 !token_hdr.ops->complete_get_nb ||
521 !token_hdr.ops->complete_get_elt ||
522 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
523 if (local_state < *state) {
528 if (token_p && token_hdr.ops->get_help) {
529 token_hdr.ops->get_help(token_p, tmpbuf,
531 help_str = inst->help_str;
533 snprintf(dst, size, "[%s]: %s", tmpbuf,
536 snprintf(dst, size, "[%s]: No help",
540 snprintf(dst, size, "[RETURN]");
545 /* several choices */
546 for (i=0 ; i<n ; i++) {
547 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
550 /* we have at least room for one char */
551 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
552 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
553 tmpbuf[tmp_len] = ' ';
554 tmpbuf[tmp_len + 1] = 0;
557 debug_printf(" choice <%s>\n", tmpbuf);
559 /* does the completion match the beginning of
561 if (!strncmp(partial_tok, tmpbuf,
563 if (local_state < *state) {
568 l=snprintf(dst, size, "%s", tmpbuf);
569 if (l>=0 && token_hdr.ops->get_help) {
570 token_hdr.ops->get_help(token_p, tmpbuf,
572 help_str = inst->help_str;
574 snprintf(dst+l, size-l, "[%s]: %s",
577 snprintf(dst+l, size-l,
578 "[%s]: No help", tmpbuf);
586 inst = ctx[inst_num];