1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
12 #include <rte_string_fns.h>
14 #include "cmdline_private.h"
16 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
17 #define debug_printf printf
19 #define debug_printf(args...) do {} while(0)
22 #define CMDLINE_BUFFER_SIZE 64
24 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
53 cmdline_isendoftoken(char c)
55 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
61 cmdline_isendofcommand(char c)
63 if (!c || iscomment(c) || isendofline(c))
69 nb_common_chars(const char * s1, const char * s2)
73 while (*s1==*s2 && *s1) {
81 /** Retrieve either static or dynamic token at a given index. */
82 static cmdline_parse_token_hdr_t *
83 get_token(cmdline_parse_inst_t *inst, unsigned int index)
85 cmdline_parse_token_hdr_t *token_p;
87 /* check presence of static tokens first */
88 if (inst->tokens[0] || !inst->f)
89 return inst->tokens[index];
90 /* generate dynamic token */
92 inst->f(&token_p, NULL, &inst->tokens[index]);
97 * try to match the buffer with an instruction (only the first
98 * nb_match_token tokens if != 0). Return 0 if we match all the
99 * tokens, else the number of matched tokens, else -1.
102 match_inst(cmdline_parse_inst_t *inst, const char *buf,
103 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size)
105 cmdline_parse_token_hdr_t *token_p = NULL;
108 struct cmdline_token_hdr token_hdr;
111 memset(resbuf, 0, resbuf_size);
112 /* check if we match all tokens of inst */
113 while (!nb_match_token || i < nb_match_token) {
114 token_p = get_token(inst, i);
117 memcpy(&token_hdr, token_p, sizeof(token_hdr));
119 debug_printf("TK\n");
121 while (isblank2(*buf)) {
126 if ( isendofline(*buf) || iscomment(*buf) )
129 if (resbuf == NULL) {
130 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
134 if (token_hdr.offset > resbuf_size) {
135 printf("Parse error(%s:%d): Token offset(%u) "
136 "exceeds maximum size(%u)\n",
138 token_hdr.offset, resbuf_size);
141 rb_sz = resbuf_size - token_hdr.offset;
143 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
144 token_hdr.offset, rb_sz);
150 debug_printf("TK parsed (len=%d)\n", n);
159 /* in case we want to match a specific num of token */
160 if (nb_match_token) {
161 if (i == nb_match_token) {
167 /* we don't match all the tokens */
172 /* are there are some tokens more */
173 while (isblank2(*buf)) {
178 if ( isendofline(*buf) || iscomment(*buf) )
181 /* garbage after inst */
187 __cmdline_parse(struct cmdline *cl, const char *buf, bool call_fn)
189 unsigned int inst_num=0;
190 cmdline_parse_inst_t *inst;
193 char buf[CMDLINE_PARSE_RESULT_BUFSIZE];
194 long double align; /* strong alignment constraint for buf */
195 } result, tmp_result;
196 void (*f)(void *, struct cmdline *, void *) = NULL;
201 int err = CMDLINE_PARSE_NOMATCH;
203 cmdline_parse_ctx_t *ctx;
204 char *result_buf = result.buf;
207 return CMDLINE_PARSE_BAD_ARGS;
212 * - look if the buffer contains at least one line
213 * - look if line contains only spaces or comments
214 * - count line length
217 while (! isendofline(*curbuf)) {
218 if ( *curbuf == '\0' ) {
219 debug_printf("Incomplete buf (len=%d)\n", linelen);
222 if ( iscomment(*curbuf) ) {
225 if ( ! isblank2(*curbuf) && ! comment) {
232 /* skip all endofline chars */
233 while (isendofline(buf[linelen])) {
238 if ( parse_it == 0 ) {
239 debug_printf("Empty line (len=%d)\n", linelen);
243 debug_printf("Parse line : len=%d, <%.*s>\n",
244 linelen, linelen > 64 ? 64 : linelen, buf);
247 inst = ctx[inst_num];
249 debug_printf("INST %d\n", inst_num);
252 tok = match_inst(inst, buf, 0, result_buf,
253 CMDLINE_PARSE_RESULT_BUFSIZE);
255 if (tok > 0) /* we matched at least one token */
256 err = CMDLINE_PARSE_BAD_ARGS;
259 debug_printf("INST fully parsed\n");
261 while (isblank2(*curbuf)) {
265 /* if end of buf -> there is no garbage after inst */
266 if (isendofline(*curbuf) || iscomment(*curbuf)) {
268 memcpy(&f, &inst->f, sizeof(f));
269 memcpy(&data, &inst->data, sizeof(data));
270 result_buf = tmp_result.buf;
273 /* more than 1 inst matches */
274 err = CMDLINE_PARSE_AMBIGUOUS;
276 debug_printf("Ambiguous cmd\n");
283 inst = ctx[inst_num];
288 debug_printf("No match err=%d\n", err);
292 /* call func if requested */
294 f(result.buf, cl, data);
300 cmdline_parse(struct cmdline *cl, const char *buf)
302 return __cmdline_parse(cl, buf, true);
306 cmdline_parse_check(struct cmdline *cl, const char *buf)
308 return __cmdline_parse(cl, buf, false);
312 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
313 char *dst, unsigned int size)
315 const char *partial_tok = buf;
316 unsigned int inst_num = 0;
317 cmdline_parse_inst_t *inst;
318 cmdline_parse_token_hdr_t *token_p;
319 struct cmdline_token_hdr token_hdr;
320 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
321 unsigned int partial_tok_len;
327 unsigned int nb_completable;
328 unsigned int nb_non_completable;
330 const char *help_str;
331 cmdline_parse_ctx_t *ctx;
333 if (!cl || !buf || !state || !dst)
338 debug_printf("%s called\n", __func__);
339 memset(&token_hdr, 0, sizeof(token_hdr));
341 /* count the number of complete token to parse */
342 for (i=0 ; buf[i] ; i++) {
343 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
345 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
346 partial_tok = buf+i+1;
348 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
350 /* first call -> do a first pass */
352 debug_printf("try complete <%s>\n", buf);
353 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
354 nb_token, partial_tok);
357 nb_non_completable = 0;
359 inst = ctx[inst_num];
361 /* parse the first tokens of the inst */
363 match_inst(inst, buf, nb_token, NULL, 0))
366 debug_printf("instruction match\n");
367 token_p = get_token(inst, nb_token);
369 memcpy(&token_hdr, token_p, sizeof(token_hdr));
371 /* non completable */
373 !token_hdr.ops->complete_get_nb ||
374 !token_hdr.ops->complete_get_elt ||
375 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
376 nb_non_completable++;
380 debug_printf("%d choices for this token\n", n);
381 for (i=0 ; i<n ; i++) {
382 if (token_hdr.ops->complete_get_elt(token_p, i,
387 /* we have at least room for one char */
388 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
389 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
390 tmpbuf[tmp_len] = ' ';
391 tmpbuf[tmp_len+1] = 0;
394 debug_printf(" choice <%s>\n", tmpbuf);
396 /* does the completion match the
397 * beginning of the word ? */
398 if (!strncmp(partial_tok, tmpbuf,
400 if (comp_len == -1) {
402 tmpbuf + partial_tok_len,
405 strnlen(tmpbuf + partial_tok_len,
406 sizeof(tmpbuf) - partial_tok_len);
411 nb_common_chars(comp_buf,
412 tmpbuf+partial_tok_len);
413 comp_buf[comp_len] = 0;
419 debug_printf("next\n");
421 inst = ctx[inst_num];
424 debug_printf("total choices %d for this completion\n",
427 /* no possible completion */
428 if (nb_completable == 0 && nb_non_completable == 0)
431 /* if multichoice is not required */
432 if (*state == 0 && partial_tok_len > 0) {
433 /* one or several choices starting with the
436 if ((unsigned)(comp_len + 1) > size)
439 strlcpy(dst, comp_buf, size);
446 /* init state correctly */
450 debug_printf("Multiple choice STATE=%d\n", *state);
453 inst = ctx[inst_num];
455 /* we need to redo it */
456 inst = ctx[inst_num];
459 match_inst(inst, buf, nb_token, NULL, 0))
462 token_p = get_token(inst, nb_token);
464 memcpy(&token_hdr, token_p, sizeof(token_hdr));
466 /* one choice for this token */
468 !token_hdr.ops->complete_get_nb ||
469 !token_hdr.ops->complete_get_elt ||
470 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
471 if (local_state < *state) {
476 if (token_p && token_hdr.ops->get_help) {
477 token_hdr.ops->get_help(token_p, tmpbuf,
479 help_str = inst->help_str;
481 snprintf(dst, size, "[%s]: %s", tmpbuf,
484 snprintf(dst, size, "[%s]: No help",
488 snprintf(dst, size, "[RETURN]");
493 /* several choices */
494 for (i=0 ; i<n ; i++) {
495 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
498 /* we have at least room for one char */
499 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
500 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
501 tmpbuf[tmp_len] = ' ';
502 tmpbuf[tmp_len + 1] = 0;
505 debug_printf(" choice <%s>\n", tmpbuf);
507 /* does the completion match the beginning of
509 if (!strncmp(partial_tok, tmpbuf,
511 if (local_state < *state) {
516 l=strlcpy(dst, tmpbuf, size);
517 if (l>=0 && token_hdr.ops->get_help) {
518 token_hdr.ops->get_help(token_p, tmpbuf,
520 help_str = inst->help_str;
522 snprintf(dst+l, size-l, "[%s]: %s",
525 snprintf(dst+l, size-l,
526 "[%s]: No help", tmpbuf);
534 inst = ctx[inst_num];