1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
14 #include <netinet/in.h>
16 #include <rte_string_fns.h>
18 #include "cmdline_private.h"
20 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
21 #define debug_printf printf
23 #define debug_printf(args...) do {} while(0)
26 #define CMDLINE_BUFFER_SIZE 64
28 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
57 cmdline_isendoftoken(char c)
59 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
65 cmdline_isendofcommand(char c)
67 if (!c || iscomment(c) || isendofline(c))
73 nb_common_chars(const char * s1, const char * s2)
77 while (*s1==*s2 && *s1) {
85 /** Retrieve either static or dynamic token at a given index. */
86 static cmdline_parse_token_hdr_t *
87 get_token(cmdline_parse_inst_t *inst, unsigned int index)
89 cmdline_parse_token_hdr_t *token_p;
91 /* check presence of static tokens first */
92 if (inst->tokens[0] || !inst->f)
93 return inst->tokens[index];
94 /* generate dynamic token */
96 inst->f(&token_p, NULL, &inst->tokens[index]);
101 * try to match the buffer with an instruction (only the first
102 * nb_match_token tokens if != 0). Return 0 if we match all the
103 * tokens, else the number of matched tokens, else -1.
106 match_inst(cmdline_parse_inst_t *inst, const char *buf,
107 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size)
109 cmdline_parse_token_hdr_t *token_p = NULL;
112 struct cmdline_token_hdr token_hdr;
115 memset(resbuf, 0, resbuf_size);
116 /* check if we match all tokens of inst */
117 while (!nb_match_token || i < nb_match_token) {
118 token_p = get_token(inst, i);
121 memcpy(&token_hdr, token_p, sizeof(token_hdr));
123 debug_printf("TK\n");
125 while (isblank2(*buf)) {
130 if ( isendofline(*buf) || iscomment(*buf) )
133 if (resbuf == NULL) {
134 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
138 if (token_hdr.offset > resbuf_size) {
139 printf("Parse error(%s:%d): Token offset(%u) "
140 "exceeds maximum size(%u)\n",
142 token_hdr.offset, resbuf_size);
145 rb_sz = resbuf_size - token_hdr.offset;
147 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
148 token_hdr.offset, rb_sz);
154 debug_printf("TK parsed (len=%d)\n", n);
163 /* in case we want to match a specific num of token */
164 if (nb_match_token) {
165 if (i == nb_match_token) {
171 /* we don't match all the tokens */
176 /* are there are some tokens more */
177 while (isblank2(*buf)) {
182 if ( isendofline(*buf) || iscomment(*buf) )
185 /* garbage after inst */
191 cmdline_parse(struct cmdline *cl, const char * buf)
193 unsigned int inst_num=0;
194 cmdline_parse_inst_t *inst;
197 char buf[CMDLINE_PARSE_RESULT_BUFSIZE];
198 long double align; /* strong alignment constraint for buf */
199 } result, tmp_result;
200 void (*f)(void *, struct cmdline *, void *) = NULL;
205 int err = CMDLINE_PARSE_NOMATCH;
207 cmdline_parse_ctx_t *ctx;
208 char *result_buf = result.buf;
211 return CMDLINE_PARSE_BAD_ARGS;
216 * - look if the buffer contains at least one line
217 * - look if line contains only spaces or comments
218 * - count line length
221 while (! isendofline(*curbuf)) {
222 if ( *curbuf == '\0' ) {
223 debug_printf("Incomplete buf (len=%d)\n", linelen);
226 if ( iscomment(*curbuf) ) {
229 if ( ! isblank2(*curbuf) && ! comment) {
236 /* skip all endofline chars */
237 while (isendofline(buf[linelen])) {
242 if ( parse_it == 0 ) {
243 debug_printf("Empty line (len=%d)\n", linelen);
247 debug_printf("Parse line : len=%d, <%.*s>\n",
248 linelen, linelen > 64 ? 64 : linelen, buf);
251 inst = ctx[inst_num];
253 debug_printf("INST %d\n", inst_num);
256 tok = match_inst(inst, buf, 0, result_buf,
257 CMDLINE_PARSE_RESULT_BUFSIZE);
259 if (tok > 0) /* we matched at least one token */
260 err = CMDLINE_PARSE_BAD_ARGS;
263 debug_printf("INST fully parsed\n");
265 while (isblank2(*curbuf)) {
269 /* if end of buf -> there is no garbage after inst */
270 if (isendofline(*curbuf) || iscomment(*curbuf)) {
272 memcpy(&f, &inst->f, sizeof(f));
273 memcpy(&data, &inst->data, sizeof(data));
274 result_buf = tmp_result.buf;
277 /* more than 1 inst matches */
278 err = CMDLINE_PARSE_AMBIGUOUS;
280 debug_printf("Ambiguous cmd\n");
287 inst = ctx[inst_num];
292 f(result.buf, cl, data);
297 debug_printf("No match err=%d\n", err);
305 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
306 char *dst, unsigned int size)
308 const char *partial_tok = buf;
309 unsigned int inst_num = 0;
310 cmdline_parse_inst_t *inst;
311 cmdline_parse_token_hdr_t *token_p;
312 struct cmdline_token_hdr token_hdr;
313 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
314 unsigned int partial_tok_len;
320 unsigned int nb_completable;
321 unsigned int nb_non_completable;
323 const char *help_str;
324 cmdline_parse_ctx_t *ctx;
326 if (!cl || !buf || !state || !dst)
331 debug_printf("%s called\n", __func__);
332 memset(&token_hdr, 0, sizeof(token_hdr));
334 /* count the number of complete token to parse */
335 for (i=0 ; buf[i] ; i++) {
336 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
338 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
339 partial_tok = buf+i+1;
341 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
343 /* first call -> do a first pass */
345 debug_printf("try complete <%s>\n", buf);
346 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
347 nb_token, partial_tok);
350 nb_non_completable = 0;
352 inst = ctx[inst_num];
354 /* parse the first tokens of the inst */
356 match_inst(inst, buf, nb_token, NULL, 0))
359 debug_printf("instruction match\n");
360 token_p = get_token(inst, nb_token);
362 memcpy(&token_hdr, token_p, sizeof(token_hdr));
364 /* non completable */
366 !token_hdr.ops->complete_get_nb ||
367 !token_hdr.ops->complete_get_elt ||
368 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
369 nb_non_completable++;
373 debug_printf("%d choices for this token\n", n);
374 for (i=0 ; i<n ; i++) {
375 if (token_hdr.ops->complete_get_elt(token_p, i,
380 /* we have at least room for one char */
381 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
382 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
383 tmpbuf[tmp_len] = ' ';
384 tmpbuf[tmp_len+1] = 0;
387 debug_printf(" choice <%s>\n", tmpbuf);
389 /* does the completion match the
390 * beginning of the word ? */
391 if (!strncmp(partial_tok, tmpbuf,
393 if (comp_len == -1) {
395 tmpbuf + partial_tok_len,
398 strnlen(tmpbuf + partial_tok_len,
399 sizeof(tmpbuf) - partial_tok_len);
404 nb_common_chars(comp_buf,
405 tmpbuf+partial_tok_len);
406 comp_buf[comp_len] = 0;
412 debug_printf("next\n");
414 inst = ctx[inst_num];
417 debug_printf("total choices %d for this completion\n",
420 /* no possible completion */
421 if (nb_completable == 0 && nb_non_completable == 0)
424 /* if multichoice is not required */
425 if (*state == 0 && partial_tok_len > 0) {
426 /* one or several choices starting with the
429 if ((unsigned)(comp_len + 1) > size)
432 strlcpy(dst, comp_buf, size);
439 /* init state correctly */
443 debug_printf("Multiple choice STATE=%d\n", *state);
446 inst = ctx[inst_num];
448 /* we need to redo it */
449 inst = ctx[inst_num];
452 match_inst(inst, buf, nb_token, NULL, 0))
455 token_p = get_token(inst, nb_token);
457 memcpy(&token_hdr, token_p, sizeof(token_hdr));
459 /* one choice for this token */
461 !token_hdr.ops->complete_get_nb ||
462 !token_hdr.ops->complete_get_elt ||
463 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
464 if (local_state < *state) {
469 if (token_p && token_hdr.ops->get_help) {
470 token_hdr.ops->get_help(token_p, tmpbuf,
472 help_str = inst->help_str;
474 snprintf(dst, size, "[%s]: %s", tmpbuf,
477 snprintf(dst, size, "[%s]: No help",
481 snprintf(dst, size, "[RETURN]");
486 /* several choices */
487 for (i=0 ; i<n ; i++) {
488 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
491 /* we have at least room for one char */
492 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
493 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
494 tmpbuf[tmp_len] = ' ';
495 tmpbuf[tmp_len + 1] = 0;
498 debug_printf(" choice <%s>\n", tmpbuf);
500 /* does the completion match the beginning of
502 if (!strncmp(partial_tok, tmpbuf,
504 if (local_state < *state) {
509 l=strlcpy(dst, tmpbuf, size);
510 if (l>=0 && token_hdr.ops->get_help) {
511 token_hdr.ops->get_help(token_p, tmpbuf,
513 help_str = inst->help_str;
515 snprintf(dst+l, size-l, "[%s]: %s",
518 snprintf(dst+l, size-l,
519 "[%s]: No help", tmpbuf);
527 inst = ctx[inst_num];