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 *resbuf, unsigned resbuf_size)
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) )
165 if (resbuf == NULL) {
166 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
170 if (token_hdr.offset > resbuf_size) {
171 printf("Parse error(%s:%d): Token offset(%u) "
172 "exceeds maximum size(%u)\n",
174 token_hdr.offset, resbuf_size);
177 rb_sz = resbuf_size - token_hdr.offset;
179 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
180 token_hdr.offset, rb_sz);
186 debug_printf("TK parsed (len=%d)\n", n);
191 token_p = inst->tokens[token_num];
193 memcpy(&token_hdr, token_p, sizeof(token_hdr));
200 /* in case we want to match a specific num of token */
201 if (nb_match_token) {
202 if (i == nb_match_token) {
208 /* we don't match all the tokens */
213 /* are there are some tokens more */
214 while (isblank2(*buf)) {
219 if ( isendofline(*buf) || iscomment(*buf) )
222 /* garbage after inst */
228 cmdline_parse(struct cmdline *cl, const char * buf)
230 unsigned int inst_num=0;
231 cmdline_parse_inst_t *inst;
233 char result_buf[CMDLINE_PARSE_RESULT_BUFSIZE];
234 void (*f)(void *, struct cmdline *, void *) = NULL;
239 int err = CMDLINE_PARSE_NOMATCH;
241 cmdline_parse_ctx_t *ctx;
242 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
243 char debug_buf[BUFSIZ];
247 return CMDLINE_PARSE_BAD_ARGS;
252 * - look if the buffer contains at least one line
253 * - look if line contains only spaces or comments
254 * - count line length
257 while (! isendofline(*curbuf)) {
258 if ( *curbuf == '\0' ) {
259 debug_printf("Incomplete buf (len=%d)\n", linelen);
262 if ( iscomment(*curbuf) ) {
265 if ( ! isblank2(*curbuf) && ! comment) {
272 /* skip all endofline chars */
273 while (isendofline(buf[linelen])) {
278 if ( parse_it == 0 ) {
279 debug_printf("Empty line (len=%d)\n", linelen);
283 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
284 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
285 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
289 inst = ctx[inst_num];
291 debug_printf("INST %d\n", inst_num);
294 tok = match_inst(inst, buf, 0, result_buf, sizeof(result_buf));
296 if (tok > 0) /* we matched at least one token */
297 err = CMDLINE_PARSE_BAD_ARGS;
300 debug_printf("INST fully parsed\n");
302 while (isblank2(*curbuf)) {
306 /* if end of buf -> there is no garbage after inst */
307 if (isendofline(*curbuf) || iscomment(*curbuf)) {
309 memcpy(&f, &inst->f, sizeof(f));
310 memcpy(&data, &inst->data, sizeof(data));
313 /* more than 1 inst matches */
314 err = CMDLINE_PARSE_AMBIGUOUS;
316 debug_printf("Ambiguous cmd\n");
323 inst = ctx[inst_num];
328 f(result_buf, cl, data);
333 debug_printf("No match err=%d\n", err);
341 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
342 char *dst, unsigned int size)
344 const char *partial_tok = buf;
345 unsigned int inst_num = 0;
346 cmdline_parse_inst_t *inst;
347 cmdline_parse_token_hdr_t *token_p;
348 struct cmdline_token_hdr token_hdr;
349 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
350 unsigned int partial_tok_len;
356 unsigned int nb_completable;
357 unsigned int nb_non_completable;
359 const char *help_str;
360 cmdline_parse_ctx_t *ctx;
362 if (!cl || !buf || !state || !dst)
367 debug_printf("%s called\n", __func__);
368 memset(&token_hdr, 0, sizeof(token_hdr));
370 /* count the number of complete token to parse */
371 for (i=0 ; buf[i] ; i++) {
372 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
374 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
375 partial_tok = buf+i+1;
377 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
379 /* first call -> do a first pass */
381 debug_printf("try complete <%s>\n", buf);
382 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
383 nb_token, partial_tok);
386 nb_non_completable = 0;
388 inst = ctx[inst_num];
390 /* parse the first tokens of the inst */
391 if (nb_token && match_inst(inst, buf, nb_token, NULL, 0))
394 debug_printf("instruction match\n");
395 token_p = inst->tokens[nb_token];
397 memcpy(&token_hdr, token_p, sizeof(token_hdr));
399 /* non completable */
401 !token_hdr.ops->complete_get_nb ||
402 !token_hdr.ops->complete_get_elt ||
403 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
404 nb_non_completable++;
408 debug_printf("%d choices for this token\n", n);
409 for (i=0 ; i<n ; i++) {
410 if (token_hdr.ops->complete_get_elt(token_p, i,
415 /* we have at least room for one char */
416 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
417 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
418 tmpbuf[tmp_len] = ' ';
419 tmpbuf[tmp_len+1] = 0;
422 debug_printf(" choice <%s>\n", tmpbuf);
424 /* does the completion match the
425 * beginning of the word ? */
426 if (!strncmp(partial_tok, tmpbuf,
428 if (comp_len == -1) {
429 snprintf(comp_buf, sizeof(comp_buf),
430 "%s", tmpbuf + partial_tok_len);
432 strnlen(tmpbuf + partial_tok_len,
433 sizeof(tmpbuf) - partial_tok_len);
438 nb_common_chars(comp_buf,
439 tmpbuf+partial_tok_len);
440 comp_buf[comp_len] = 0;
446 debug_printf("next\n");
448 inst = ctx[inst_num];
451 debug_printf("total choices %d for this completion\n",
454 /* no possible completion */
455 if (nb_completable == 0 && nb_non_completable == 0)
458 /* if multichoice is not required */
459 if (*state == 0 && partial_tok_len > 0) {
460 /* one or several choices starting with the
463 if ((unsigned)(comp_len + 1) > size)
466 snprintf(dst, size, "%s", comp_buf);
473 /* init state correctly */
477 debug_printf("Multiple choice STATE=%d\n", *state);
480 inst = ctx[inst_num];
482 /* we need to redo it */
483 inst = ctx[inst_num];
485 if (nb_token && match_inst(inst, buf, nb_token, NULL, 0))
488 token_p = inst->tokens[nb_token];
490 memcpy(&token_hdr, token_p, sizeof(token_hdr));
492 /* one choice for this token */
494 !token_hdr.ops->complete_get_nb ||
495 !token_hdr.ops->complete_get_elt ||
496 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
497 if (local_state < *state) {
502 if (token_p && token_hdr.ops->get_help) {
503 token_hdr.ops->get_help(token_p, tmpbuf,
505 help_str = inst->help_str;
507 snprintf(dst, size, "[%s]: %s", tmpbuf,
510 snprintf(dst, size, "[%s]: No help",
514 snprintf(dst, size, "[RETURN]");
519 /* several choices */
520 for (i=0 ; i<n ; i++) {
521 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
524 /* we have at least room for one char */
525 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
526 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
527 tmpbuf[tmp_len] = ' ';
528 tmpbuf[tmp_len + 1] = 0;
531 debug_printf(" choice <%s>\n", tmpbuf);
533 /* does the completion match the beginning of
535 if (!strncmp(partial_tok, tmpbuf,
537 if (local_state < *state) {
542 l=snprintf(dst, size, "%s", tmpbuf);
543 if (l>=0 && token_hdr.ops->get_help) {
544 token_hdr.ops->get_help(token_p, tmpbuf,
546 help_str = inst->help_str;
548 snprintf(dst+l, size-l, "[%s]: %s",
551 snprintf(dst+l, size-l,
552 "[%s]: No help", tmpbuf);
560 inst = ctx[inst_num];