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.
36 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
37 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are met:
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * * Neither the name of the University of California, Berkeley nor the
47 * names of its contributors may be used to endorse or promote products
48 * derived from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
51 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
54 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 #include <netinet/in.h>
72 #include <rte_string_fns.h>
74 #include "cmdline_rdline.h"
75 #include "cmdline_parse.h"
78 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
79 #define debug_printf printf
81 #define debug_printf(args...) do {} while(0)
84 #define CMDLINE_BUFFER_SIZE 64
86 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
115 cmdline_isendoftoken(char c)
117 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
123 nb_common_chars(const char * s1, const char * s2)
127 while (*s1==*s2 && *s1 && *s2) {
136 * try to match the buffer with an instruction (only the first
137 * nb_match_token tokens if != 0). Return 0 if we match all the
138 * tokens, else the number of matched tokens, else -1.
141 match_inst(cmdline_parse_inst_t *inst, const char *buf,
142 unsigned int nb_match_token, void * result_buf)
144 unsigned int token_num=0;
145 cmdline_parse_token_hdr_t * token_p;
148 struct cmdline_token_hdr token_hdr;
150 token_p = inst->tokens[token_num];
152 memcpy(&token_hdr, token_p, sizeof(token_hdr));
154 /* check if we match all tokens of inst */
155 while (token_p && (!nb_match_token || i<nb_match_token)) {
156 debug_printf("TK\n");
158 while (isblank2(*buf)) {
163 if ( isendofline(*buf) || iscomment(*buf) )
167 n = token_hdr.ops->parse(token_p, buf,
171 n = token_hdr.ops->parse(token_p, buf, NULL);
176 debug_printf("TK parsed (len=%d)\n", n);
181 token_p = inst->tokens[token_num];
183 memcpy(&token_hdr, token_p, sizeof(token_hdr));
190 /* in case we want to match a specific num of token */
191 if (nb_match_token) {
192 if (i == nb_match_token) {
198 /* we don't match all the tokens */
203 /* are there are some tokens more */
204 while (isblank2(*buf)) {
209 if ( isendofline(*buf) || iscomment(*buf) )
212 /* garbage after inst */
218 cmdline_parse(struct cmdline *cl, const char * buf)
220 unsigned int inst_num=0;
221 cmdline_parse_inst_t *inst;
223 char result_buf[BUFSIZ];
224 void (*f)(void *, struct cmdline *, void *) = NULL;
229 int err = CMDLINE_PARSE_NOMATCH;
231 cmdline_parse_ctx_t *ctx = cl->ctx;
232 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
233 char debug_buf[BUFSIZ];
237 * - look if the buffer contains at least one line
238 * - look if line contains only spaces or comments
239 * - count line length
242 while (! isendofline(*curbuf)) {
243 if ( *curbuf == '\0' ) {
244 debug_printf("Incomplete buf (len=%d)\n", linelen);
247 if ( iscomment(*curbuf) ) {
250 if ( ! isblank2(*curbuf) && ! comment) {
257 /* skip all endofline chars */
258 while (isendofline(buf[linelen])) {
263 if ( parse_it == 0 ) {
264 debug_printf("Empty line (len=%d)\n", linelen);
268 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
269 rte_snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
270 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
274 inst = ctx[inst_num];
276 debug_printf("INST %d\n", inst_num);
279 tok = match_inst(inst, buf, 0, result_buf);
281 if (tok > 0) /* we matched at least one token */
282 err = CMDLINE_PARSE_BAD_ARGS;
285 debug_printf("INST fully parsed\n");
287 while (isblank2(*curbuf)) {
291 /* if end of buf -> there is no garbage after inst */
292 if (isendofline(*curbuf) || iscomment(*curbuf)) {
294 memcpy(&f, &inst->f, sizeof(f));
295 memcpy(&data, &inst->data, sizeof(data));
298 /* more than 1 inst matches */
299 err = CMDLINE_PARSE_AMBIGUOUS;
301 debug_printf("Ambiguous cmd\n");
308 inst = ctx[inst_num];
313 f(result_buf, cl, data);
318 debug_printf("No match err=%d\n", err);
326 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
327 char *dst, unsigned int size)
329 const char *partial_tok = buf;
330 unsigned int inst_num = 0;
331 cmdline_parse_inst_t *inst;
332 cmdline_parse_token_hdr_t *token_p;
333 struct cmdline_token_hdr token_hdr;
334 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
335 unsigned int partial_tok_len;
341 unsigned int nb_completable;
342 unsigned int nb_non_completable;
344 const char *help_str;
345 cmdline_parse_ctx_t *ctx = cl->ctx;
347 debug_printf("%s called\n", __func__);
348 memset(&token_hdr, 0, sizeof(token_hdr));
350 /* count the number of complete token to parse */
351 for (i=0 ; buf[i] ; i++) {
352 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
354 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
355 partial_tok = buf+i+1;
357 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
359 /* first call -> do a first pass */
361 debug_printf("try complete <%s>\n", buf);
362 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
363 nb_token, partial_tok);
366 nb_non_completable = 0;
368 inst = ctx[inst_num];
370 /* parse the first tokens of the inst */
371 if (nb_token && match_inst(inst, buf, nb_token, NULL))
374 debug_printf("instruction match \n");
375 token_p = inst->tokens[nb_token];
377 memcpy(&token_hdr, token_p, sizeof(token_hdr));
379 /* non completable */
381 !token_hdr.ops->complete_get_nb ||
382 !token_hdr.ops->complete_get_elt ||
383 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
384 nb_non_completable++;
388 debug_printf("%d choices for this token\n", n);
389 for (i=0 ; i<n ; i++) {
390 if (token_hdr.ops->complete_get_elt(token_p, i,
395 /* we have at least room for one char */
396 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
397 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
398 tmpbuf[tmp_len] = ' ';
399 tmpbuf[tmp_len+1] = 0;
402 debug_printf(" choice <%s>\n", tmpbuf);
404 /* does the completion match the
405 * beginning of the word ? */
406 if (!strncmp(partial_tok, tmpbuf,
408 if (comp_len == -1) {
409 rte_snprintf(comp_buf, sizeof(comp_buf),
410 "%s", tmpbuf + partial_tok_len);
412 strnlen(tmpbuf + partial_tok_len,
413 sizeof(tmpbuf) - partial_tok_len);
418 nb_common_chars(comp_buf,
419 tmpbuf+partial_tok_len);
420 comp_buf[comp_len] = 0;
427 inst = ctx[inst_num];
430 debug_printf("total choices %d for this completion\n",
433 /* no possible completion */
434 if (nb_completable == 0 && nb_non_completable == 0)
437 /* if multichoice is not required */
438 if (*state == 0 && partial_tok_len > 0) {
439 /* one or several choices starting with the
442 if ((unsigned)(comp_len + 1) > size)
445 rte_snprintf(dst, size, "%s", comp_buf);
452 /* init state correctly */
456 debug_printf("Multiple choice STATE=%d\n", *state);
459 inst = ctx[inst_num];
461 /* we need to redo it */
462 inst = ctx[inst_num];
464 if (nb_token && match_inst(inst, buf, nb_token, NULL))
467 token_p = inst->tokens[nb_token];
469 memcpy(&token_hdr, token_p, sizeof(token_hdr));
471 /* one choice for this token */
473 !token_hdr.ops->complete_get_nb ||
474 !token_hdr.ops->complete_get_elt ||
475 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
476 if (local_state < *state) {
481 if (token_p && token_hdr.ops->get_help) {
482 token_hdr.ops->get_help(token_p, tmpbuf,
484 help_str = inst->help_str;
486 rte_snprintf(dst, size, "[%s]: %s", tmpbuf,
489 rte_snprintf(dst, size, "[%s]: No help",
493 rte_snprintf(dst, size, "[RETURN]");
498 /* several choices */
499 for (i=0 ; i<n ; i++) {
500 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
503 /* we have at least room for one char */
504 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
505 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
506 tmpbuf[tmp_len] = ' ';
507 tmpbuf[tmp_len + 1] = 0;
510 debug_printf(" choice <%s>\n", tmpbuf);
512 /* does the completion match the beginning of
514 if (!strncmp(partial_tok, tmpbuf,
516 if (local_state < *state) {
521 l=rte_snprintf(dst, size, "%s", tmpbuf);
522 if (l>=0 && token_hdr.ops->get_help) {
523 token_hdr.ops->get_help(token_p, tmpbuf,
525 help_str = inst->help_str;
527 rte_snprintf(dst+l, size-l, "[%s]: %s",
530 rte_snprintf(dst+l, size-l,
531 "[%s]: No help", tmpbuf);
539 inst = ctx[inst_num];