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.
33 * version: DPDK.L.1.2.3-3
37 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
38 * All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions are met:
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * * Neither the name of the University of California, Berkeley nor the
48 * names of its contributors may be used to endorse or promote products
49 * derived from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
52 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
55 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 #include <netinet/in.h>
73 #include <rte_string_fns.h>
75 #include "cmdline_rdline.h"
76 #include "cmdline_parse.h"
79 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
80 #define debug_printf printf
82 #define debug_printf(args...) do {} while(0)
85 #define CMDLINE_BUFFER_SIZE 64
87 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
116 cmdline_isendoftoken(char c)
118 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
124 nb_common_chars(const char * s1, const char * s2)
128 while (*s1==*s2 && *s1 && *s2) {
137 * try to match the buffer with an instruction (only the first
138 * nb_match_token tokens if != 0). Return 0 if we match all the
139 * tokens, else the number of matched tokens, else -1.
142 match_inst(cmdline_parse_inst_t *inst, const char *buf,
143 unsigned int nb_match_token, void * result_buf)
145 unsigned int token_num=0;
146 cmdline_parse_token_hdr_t * token_p;
149 struct cmdline_token_hdr token_hdr;
151 token_p = inst->tokens[token_num];
153 memcpy(&token_hdr, token_p, sizeof(token_hdr));
155 /* check if we match all tokens of inst */
156 while (token_p && (!nb_match_token || i<nb_match_token)) {
157 debug_printf("TK\n");
159 while (isblank2(*buf)) {
164 if ( isendofline(*buf) || iscomment(*buf) )
168 n = token_hdr.ops->parse(token_p, buf,
172 n = token_hdr.ops->parse(token_p, buf, NULL);
177 debug_printf("TK parsed (len=%d)\n", n);
182 token_p = inst->tokens[token_num];
184 memcpy(&token_hdr, token_p, sizeof(token_hdr));
191 /* in case we want to match a specific num of token */
192 if (nb_match_token) {
193 if (i == nb_match_token) {
199 /* we don't match all the tokens */
204 /* are there are some tokens more */
205 while (isblank2(*buf)) {
210 if ( isendofline(*buf) || iscomment(*buf) )
213 /* garbage after inst */
219 cmdline_parse(struct cmdline *cl, const char * buf)
221 unsigned int inst_num=0;
222 cmdline_parse_inst_t *inst;
224 char result_buf[BUFSIZ];
225 void (*f)(void *, struct cmdline *, void *) = NULL;
230 int err = CMDLINE_PARSE_NOMATCH;
232 cmdline_parse_ctx_t *ctx = cl->ctx;
233 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
234 char debug_buf[BUFSIZ];
238 * - look if the buffer contains at least one line
239 * - look if line contains only spaces or comments
240 * - count line length
243 while (! isendofline(*curbuf)) {
244 if ( *curbuf == '\0' ) {
245 debug_printf("Incomplete buf (len=%d)\n", linelen);
248 if ( iscomment(*curbuf) ) {
251 if ( ! isblank2(*curbuf) && ! comment) {
258 /* skip all endofline chars */
259 while (isendofline(buf[linelen])) {
264 if ( parse_it == 0 ) {
265 debug_printf("Empty line (len=%d)\n", linelen);
269 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
270 rte_snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
271 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
275 inst = ctx[inst_num];
277 debug_printf("INST %d\n", inst_num);
280 tok = match_inst(inst, buf, 0, result_buf);
282 if (tok > 0) /* we matched at least one token */
283 err = CMDLINE_PARSE_BAD_ARGS;
286 debug_printf("INST fully parsed\n");
288 while (isblank2(*curbuf)) {
292 /* if end of buf -> there is no garbage after inst */
293 if (isendofline(*curbuf) || iscomment(*curbuf)) {
295 memcpy(&f, &inst->f, sizeof(f));
296 memcpy(&data, &inst->data, sizeof(data));
299 /* more than 1 inst matches */
300 err = CMDLINE_PARSE_AMBIGUOUS;
302 debug_printf("Ambiguous cmd\n");
309 inst = ctx[inst_num];
314 f(result_buf, cl, data);
319 debug_printf("No match err=%d\n", err);
327 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
328 char *dst, unsigned int size)
330 const char *partial_tok = buf;
331 unsigned int inst_num = 0;
332 cmdline_parse_inst_t *inst;
333 cmdline_parse_token_hdr_t *token_p;
334 struct cmdline_token_hdr token_hdr;
335 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
336 unsigned int partial_tok_len;
342 unsigned int nb_completable;
343 unsigned int nb_non_completable;
345 const char *help_str;
346 cmdline_parse_ctx_t *ctx = cl->ctx;
348 debug_printf("%s called\n", __func__);
349 memset(&token_hdr, 0, sizeof(token_hdr));
351 /* count the number of complete token to parse */
352 for (i=0 ; buf[i] ; i++) {
353 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
355 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
356 partial_tok = buf+i+1;
358 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
360 /* first call -> do a first pass */
362 debug_printf("try complete <%s>\n", buf);
363 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
364 nb_token, partial_tok);
367 nb_non_completable = 0;
369 inst = ctx[inst_num];
371 /* parse the first tokens of the inst */
372 if (nb_token && match_inst(inst, buf, nb_token, NULL))
375 debug_printf("instruction match \n");
376 token_p = inst->tokens[nb_token];
378 memcpy(&token_hdr, token_p, sizeof(token_hdr));
380 /* non completable */
382 !token_hdr.ops->complete_get_nb ||
383 !token_hdr.ops->complete_get_elt ||
384 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
385 nb_non_completable++;
389 debug_printf("%d choices for this token\n", n);
390 for (i=0 ; i<n ; i++) {
391 if (token_hdr.ops->complete_get_elt(token_p, i,
396 /* we have at least room for one char */
397 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
398 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
399 tmpbuf[tmp_len] = ' ';
400 tmpbuf[tmp_len+1] = 0;
403 debug_printf(" choice <%s>\n", tmpbuf);
405 /* does the completion match the
406 * beginning of the word ? */
407 if (!strncmp(partial_tok, tmpbuf,
409 if (comp_len == -1) {
410 rte_snprintf(comp_buf, sizeof(comp_buf),
411 "%s", tmpbuf + partial_tok_len);
413 strnlen(tmpbuf + partial_tok_len,
414 sizeof(tmpbuf) - partial_tok_len);
419 nb_common_chars(comp_buf,
420 tmpbuf+partial_tok_len);
421 comp_buf[comp_len] = 0;
428 inst = ctx[inst_num];
431 debug_printf("total choices %d for this completion\n",
434 /* no possible completion */
435 if (nb_completable == 0 && nb_non_completable == 0)
438 /* if multichoice is not required */
439 if (*state == 0 && partial_tok_len > 0) {
440 /* one or several choices starting with the
443 if ((unsigned)(comp_len + 1) > size)
446 rte_snprintf(dst, size, "%s", comp_buf);
453 /* init state correctly */
457 debug_printf("Multiple choice STATE=%d\n", *state);
460 inst = ctx[inst_num];
462 /* we need to redo it */
463 inst = ctx[inst_num];
465 if (nb_token && match_inst(inst, buf, nb_token, NULL))
468 token_p = inst->tokens[nb_token];
470 memcpy(&token_hdr, token_p, sizeof(token_hdr));
472 /* one choice for this token */
474 !token_hdr.ops->complete_get_nb ||
475 !token_hdr.ops->complete_get_elt ||
476 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
477 if (local_state < *state) {
482 if (token_p && token_hdr.ops->get_help) {
483 token_hdr.ops->get_help(token_p, tmpbuf,
485 help_str = inst->help_str;
487 rte_snprintf(dst, size, "[%s]: %s", tmpbuf,
490 rte_snprintf(dst, size, "[%s]: No help",
494 rte_snprintf(dst, size, "[RETURN]");
499 /* several choices */
500 for (i=0 ; i<n ; i++) {
501 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
504 /* we have at least room for one char */
505 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
506 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
507 tmpbuf[tmp_len] = ' ';
508 tmpbuf[tmp_len + 1] = 0;
511 debug_printf(" choice <%s>\n", tmpbuf);
513 /* does the completion match the beginning of
515 if (!strncmp(partial_tok, tmpbuf,
517 if (local_state < *state) {
522 l=rte_snprintf(dst, size, "%s", tmpbuf);
523 if (l>=0 && token_hdr.ops->get_help) {
524 token_hdr.ops->get_help(token_p, tmpbuf,
526 help_str = inst->help_str;
528 rte_snprintf(dst+l, size-l, "[%s]: %s",
531 rte_snprintf(dst+l, size-l,
532 "[%s]: No help", tmpbuf);
540 inst = ctx[inst_num];