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) {
143 * try to match the buffer with an instruction (only the first
144 * nb_match_token tokens if != 0). Return 0 if we match all the
145 * tokens, else the number of matched tokens, else -1.
148 match_inst(cmdline_parse_inst_t *inst, const char *buf,
149 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size,
150 cmdline_parse_token_hdr_t
151 *(*dyn_tokens)[CMDLINE_PARSE_DYNAMIC_TOKENS])
153 unsigned int token_num=0;
154 cmdline_parse_token_hdr_t * token_p;
157 struct cmdline_token_hdr token_hdr;
159 token_p = inst->tokens[token_num];
160 if (!token_p && dyn_tokens && inst->f) {
161 if (!(*dyn_tokens)[0])
162 inst->f(&(*dyn_tokens)[0], NULL, dyn_tokens);
163 token_p = (*dyn_tokens)[0];
166 memcpy(&token_hdr, token_p, sizeof(token_hdr));
168 /* check if we match all tokens of inst */
169 while (token_p && (!nb_match_token || i<nb_match_token)) {
170 debug_printf("TK\n");
172 while (isblank2(*buf)) {
177 if ( isendofline(*buf) || iscomment(*buf) )
180 if (resbuf == NULL) {
181 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
185 if (token_hdr.offset > resbuf_size) {
186 printf("Parse error(%s:%d): Token offset(%u) "
187 "exceeds maximum size(%u)\n",
189 token_hdr.offset, resbuf_size);
192 rb_sz = resbuf_size - token_hdr.offset;
194 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
195 token_hdr.offset, rb_sz);
201 debug_printf("TK parsed (len=%d)\n", n);
206 if (!inst->tokens[0]) {
207 if (token_num < (CMDLINE_PARSE_DYNAMIC_TOKENS - 1)) {
208 if (!(*dyn_tokens)[token_num])
209 inst->f(&(*dyn_tokens)[token_num],
212 token_p = (*dyn_tokens)[token_num];
216 token_p = inst->tokens[token_num];
218 memcpy(&token_hdr, token_p, sizeof(token_hdr));
225 /* in case we want to match a specific num of token */
226 if (nb_match_token) {
227 if (i == nb_match_token) {
233 /* we don't match all the tokens */
238 /* are there are some tokens more */
239 while (isblank2(*buf)) {
244 if ( isendofline(*buf) || iscomment(*buf) )
247 /* garbage after inst */
253 cmdline_parse(struct cmdline *cl, const char * buf)
255 unsigned int inst_num=0;
256 cmdline_parse_inst_t *inst;
259 char buf[CMDLINE_PARSE_RESULT_BUFSIZE];
260 long double align; /* strong alignment constraint for buf */
261 } result, tmp_result;
262 cmdline_parse_token_hdr_t *dyn_tokens[CMDLINE_PARSE_DYNAMIC_TOKENS];
263 void (*f)(void *, struct cmdline *, void *) = NULL;
268 int err = CMDLINE_PARSE_NOMATCH;
270 cmdline_parse_ctx_t *ctx;
271 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
272 char debug_buf[BUFSIZ];
276 return CMDLINE_PARSE_BAD_ARGS;
279 memset(&dyn_tokens, 0, sizeof(dyn_tokens));
282 * - look if the buffer contains at least one line
283 * - look if line contains only spaces or comments
284 * - count line length
287 while (! isendofline(*curbuf)) {
288 if ( *curbuf == '\0' ) {
289 debug_printf("Incomplete buf (len=%d)\n", linelen);
292 if ( iscomment(*curbuf) ) {
295 if ( ! isblank2(*curbuf) && ! comment) {
302 /* skip all endofline chars */
303 while (isendofline(buf[linelen])) {
308 if ( parse_it == 0 ) {
309 debug_printf("Empty line (len=%d)\n", linelen);
313 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
314 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
315 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
319 inst = ctx[inst_num];
321 debug_printf("INST %d\n", inst_num);
324 tok = match_inst(inst, buf, 0, tmp_result.buf,
325 sizeof(tmp_result.buf), &dyn_tokens);
327 if (tok > 0) /* we matched at least one token */
328 err = CMDLINE_PARSE_BAD_ARGS;
331 debug_printf("INST fully parsed\n");
332 memcpy(&result, &tmp_result,
335 while (isblank2(*curbuf)) {
339 /* if end of buf -> there is no garbage after inst */
340 if (isendofline(*curbuf) || iscomment(*curbuf)) {
342 memcpy(&f, &inst->f, sizeof(f));
343 memcpy(&data, &inst->data, sizeof(data));
346 /* more than 1 inst matches */
347 err = CMDLINE_PARSE_AMBIGUOUS;
349 debug_printf("Ambiguous cmd\n");
356 inst = ctx[inst_num];
361 f(result.buf, cl, data);
366 debug_printf("No match err=%d\n", err);
374 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
375 char *dst, unsigned int size)
377 const char *partial_tok = buf;
378 unsigned int inst_num = 0;
379 cmdline_parse_inst_t *inst;
380 cmdline_parse_token_hdr_t *token_p;
381 struct cmdline_token_hdr token_hdr;
382 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
383 cmdline_parse_token_hdr_t *dyn_tokens[CMDLINE_PARSE_DYNAMIC_TOKENS];
384 unsigned int partial_tok_len;
390 unsigned int nb_completable;
391 unsigned int nb_non_completable;
393 const char *help_str;
394 cmdline_parse_ctx_t *ctx;
396 if (!cl || !buf || !state || !dst)
401 debug_printf("%s called\n", __func__);
402 memset(&token_hdr, 0, sizeof(token_hdr));
403 memset(&dyn_tokens, 0, sizeof(dyn_tokens));
405 /* count the number of complete token to parse */
406 for (i=0 ; buf[i] ; i++) {
407 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
409 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
410 partial_tok = buf+i+1;
412 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
414 /* first call -> do a first pass */
416 debug_printf("try complete <%s>\n", buf);
417 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
418 nb_token, partial_tok);
421 nb_non_completable = 0;
423 inst = ctx[inst_num];
425 /* parse the first tokens of the inst */
427 match_inst(inst, buf, nb_token, NULL, 0,
431 debug_printf("instruction match\n");
432 if (!inst->tokens[0]) {
434 (CMDLINE_PARSE_DYNAMIC_TOKENS - 1)) {
435 if (!dyn_tokens[nb_token])
436 inst->f(&dyn_tokens[nb_token],
439 token_p = dyn_tokens[nb_token];
443 token_p = inst->tokens[nb_token];
445 memcpy(&token_hdr, token_p, sizeof(token_hdr));
447 /* non completable */
449 !token_hdr.ops->complete_get_nb ||
450 !token_hdr.ops->complete_get_elt ||
451 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
452 nb_non_completable++;
456 debug_printf("%d choices for this token\n", n);
457 for (i=0 ; i<n ; i++) {
458 if (token_hdr.ops->complete_get_elt(token_p, i,
463 /* we have at least room for one char */
464 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
465 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
466 tmpbuf[tmp_len] = ' ';
467 tmpbuf[tmp_len+1] = 0;
470 debug_printf(" choice <%s>\n", tmpbuf);
472 /* does the completion match the
473 * beginning of the word ? */
474 if (!strncmp(partial_tok, tmpbuf,
476 if (comp_len == -1) {
477 snprintf(comp_buf, sizeof(comp_buf),
478 "%s", tmpbuf + partial_tok_len);
480 strnlen(tmpbuf + partial_tok_len,
481 sizeof(tmpbuf) - partial_tok_len);
486 nb_common_chars(comp_buf,
487 tmpbuf+partial_tok_len);
488 comp_buf[comp_len] = 0;
494 debug_printf("next\n");
496 inst = ctx[inst_num];
499 debug_printf("total choices %d for this completion\n",
502 /* no possible completion */
503 if (nb_completable == 0 && nb_non_completable == 0)
506 /* if multichoice is not required */
507 if (*state == 0 && partial_tok_len > 0) {
508 /* one or several choices starting with the
511 if ((unsigned)(comp_len + 1) > size)
514 snprintf(dst, size, "%s", comp_buf);
521 /* init state correctly */
525 debug_printf("Multiple choice STATE=%d\n", *state);
528 inst = ctx[inst_num];
530 /* we need to redo it */
531 inst = ctx[inst_num];
534 match_inst(inst, buf, nb_token, NULL, 0, &dyn_tokens))
537 if (!inst->tokens[0]) {
538 if (nb_token < (CMDLINE_PARSE_DYNAMIC_TOKENS - 1)) {
539 if (!dyn_tokens[nb_token])
540 inst->f(&dyn_tokens[nb_token],
543 token_p = dyn_tokens[nb_token];
547 token_p = inst->tokens[nb_token];
549 memcpy(&token_hdr, token_p, sizeof(token_hdr));
551 /* one choice for this token */
553 !token_hdr.ops->complete_get_nb ||
554 !token_hdr.ops->complete_get_elt ||
555 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
556 if (local_state < *state) {
561 if (token_p && token_hdr.ops->get_help) {
562 token_hdr.ops->get_help(token_p, tmpbuf,
564 help_str = inst->help_str;
566 snprintf(dst, size, "[%s]: %s", tmpbuf,
569 snprintf(dst, size, "[%s]: No help",
573 snprintf(dst, size, "[RETURN]");
578 /* several choices */
579 for (i=0 ; i<n ; i++) {
580 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
583 /* we have at least room for one char */
584 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
585 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
586 tmpbuf[tmp_len] = ' ';
587 tmpbuf[tmp_len + 1] = 0;
590 debug_printf(" choice <%s>\n", tmpbuf);
592 /* does the completion match the beginning of
594 if (!strncmp(partial_tok, tmpbuf,
596 if (local_state < *state) {
601 l=snprintf(dst, size, "%s", tmpbuf);
602 if (l>=0 && token_hdr.ops->get_help) {
603 token_hdr.ops->get_help(token_p, tmpbuf,
605 help_str = inst->help_str;
607 snprintf(dst+l, size-l, "[%s]: %s",
610 snprintf(dst+l, size-l,
611 "[%s]: No help", tmpbuf);
619 inst = ctx[inst_num];