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 <rte_string_fns.h>
16 #include "cmdline_private.h"
18 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
19 #define debug_printf printf
21 #define debug_printf(args...) do {} while(0)
24 #define CMDLINE_BUFFER_SIZE 64
26 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
55 cmdline_isendoftoken(char c)
57 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
63 cmdline_isendofcommand(char c)
65 if (!c || iscomment(c) || isendofline(c))
71 nb_common_chars(const char * s1, const char * s2)
75 while (*s1==*s2 && *s1) {
83 /** Retrieve either static or dynamic token at a given index. */
84 static cmdline_parse_token_hdr_t *
85 get_token(cmdline_parse_inst_t *inst, unsigned int index)
87 cmdline_parse_token_hdr_t *token_p;
89 /* check presence of static tokens first */
90 if (inst->tokens[0] || !inst->f)
91 return inst->tokens[index];
92 /* generate dynamic token */
94 inst->f(&token_p, NULL, &inst->tokens[index]);
99 * try to match the buffer with an instruction (only the first
100 * nb_match_token tokens if != 0). Return 0 if we match all the
101 * tokens, else the number of matched tokens, else -1.
104 match_inst(cmdline_parse_inst_t *inst, const char *buf,
105 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size)
107 cmdline_parse_token_hdr_t *token_p = NULL;
110 struct cmdline_token_hdr token_hdr;
113 memset(resbuf, 0, resbuf_size);
114 /* check if we match all tokens of inst */
115 while (!nb_match_token || i < nb_match_token) {
116 token_p = get_token(inst, i);
119 memcpy(&token_hdr, token_p, sizeof(token_hdr));
121 debug_printf("TK\n");
123 while (isblank2(*buf)) {
128 if ( isendofline(*buf) || iscomment(*buf) )
131 if (resbuf == NULL) {
132 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
136 if (token_hdr.offset > resbuf_size) {
137 printf("Parse error(%s:%d): Token offset(%u) "
138 "exceeds maximum size(%u)\n",
140 token_hdr.offset, resbuf_size);
143 rb_sz = resbuf_size - token_hdr.offset;
145 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
146 token_hdr.offset, rb_sz);
152 debug_printf("TK parsed (len=%d)\n", n);
161 /* in case we want to match a specific num of token */
162 if (nb_match_token) {
163 if (i == nb_match_token) {
169 /* we don't match all the tokens */
174 /* are there are some tokens more */
175 while (isblank2(*buf)) {
180 if ( isendofline(*buf) || iscomment(*buf) )
183 /* garbage after inst */
189 cmdline_parse(struct cmdline *cl, const char * buf)
191 unsigned int inst_num=0;
192 cmdline_parse_inst_t *inst;
195 char buf[CMDLINE_PARSE_RESULT_BUFSIZE];
196 long double align; /* strong alignment constraint for buf */
197 } result, tmp_result;
198 void (*f)(void *, struct cmdline *, void *) = NULL;
203 int err = CMDLINE_PARSE_NOMATCH;
205 cmdline_parse_ctx_t *ctx;
206 char *result_buf = result.buf;
209 return CMDLINE_PARSE_BAD_ARGS;
214 * - look if the buffer contains at least one line
215 * - look if line contains only spaces or comments
216 * - count line length
219 while (! isendofline(*curbuf)) {
220 if ( *curbuf == '\0' ) {
221 debug_printf("Incomplete buf (len=%d)\n", linelen);
224 if ( iscomment(*curbuf) ) {
227 if ( ! isblank2(*curbuf) && ! comment) {
234 /* skip all endofline chars */
235 while (isendofline(buf[linelen])) {
240 if ( parse_it == 0 ) {
241 debug_printf("Empty line (len=%d)\n", linelen);
245 debug_printf("Parse line : len=%d, <%.*s>\n",
246 linelen, linelen > 64 ? 64 : linelen, buf);
249 inst = ctx[inst_num];
251 debug_printf("INST %d\n", inst_num);
254 tok = match_inst(inst, buf, 0, result_buf,
255 CMDLINE_PARSE_RESULT_BUFSIZE);
257 if (tok > 0) /* we matched at least one token */
258 err = CMDLINE_PARSE_BAD_ARGS;
261 debug_printf("INST fully parsed\n");
263 while (isblank2(*curbuf)) {
267 /* if end of buf -> there is no garbage after inst */
268 if (isendofline(*curbuf) || iscomment(*curbuf)) {
270 memcpy(&f, &inst->f, sizeof(f));
271 memcpy(&data, &inst->data, sizeof(data));
272 result_buf = tmp_result.buf;
275 /* more than 1 inst matches */
276 err = CMDLINE_PARSE_AMBIGUOUS;
278 debug_printf("Ambiguous cmd\n");
285 inst = ctx[inst_num];
290 f(result.buf, cl, data);
295 debug_printf("No match err=%d\n", err);
303 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
304 char *dst, unsigned int size)
306 const char *partial_tok = buf;
307 unsigned int inst_num = 0;
308 cmdline_parse_inst_t *inst;
309 cmdline_parse_token_hdr_t *token_p;
310 struct cmdline_token_hdr token_hdr;
311 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
312 unsigned int partial_tok_len;
318 unsigned int nb_completable;
319 unsigned int nb_non_completable;
321 const char *help_str;
322 cmdline_parse_ctx_t *ctx;
324 if (!cl || !buf || !state || !dst)
329 debug_printf("%s called\n", __func__);
330 memset(&token_hdr, 0, sizeof(token_hdr));
332 /* count the number of complete token to parse */
333 for (i=0 ; buf[i] ; i++) {
334 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
336 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
337 partial_tok = buf+i+1;
339 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
341 /* first call -> do a first pass */
343 debug_printf("try complete <%s>\n", buf);
344 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
345 nb_token, partial_tok);
348 nb_non_completable = 0;
350 inst = ctx[inst_num];
352 /* parse the first tokens of the inst */
354 match_inst(inst, buf, nb_token, NULL, 0))
357 debug_printf("instruction match\n");
358 token_p = get_token(inst, nb_token);
360 memcpy(&token_hdr, token_p, sizeof(token_hdr));
362 /* non completable */
364 !token_hdr.ops->complete_get_nb ||
365 !token_hdr.ops->complete_get_elt ||
366 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
367 nb_non_completable++;
371 debug_printf("%d choices for this token\n", n);
372 for (i=0 ; i<n ; i++) {
373 if (token_hdr.ops->complete_get_elt(token_p, i,
378 /* we have at least room for one char */
379 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
380 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
381 tmpbuf[tmp_len] = ' ';
382 tmpbuf[tmp_len+1] = 0;
385 debug_printf(" choice <%s>\n", tmpbuf);
387 /* does the completion match the
388 * beginning of the word ? */
389 if (!strncmp(partial_tok, tmpbuf,
391 if (comp_len == -1) {
393 tmpbuf + partial_tok_len,
396 strnlen(tmpbuf + partial_tok_len,
397 sizeof(tmpbuf) - partial_tok_len);
402 nb_common_chars(comp_buf,
403 tmpbuf+partial_tok_len);
404 comp_buf[comp_len] = 0;
410 debug_printf("next\n");
412 inst = ctx[inst_num];
415 debug_printf("total choices %d for this completion\n",
418 /* no possible completion */
419 if (nb_completable == 0 && nb_non_completable == 0)
422 /* if multichoice is not required */
423 if (*state == 0 && partial_tok_len > 0) {
424 /* one or several choices starting with the
427 if ((unsigned)(comp_len + 1) > size)
430 strlcpy(dst, comp_buf, size);
437 /* init state correctly */
441 debug_printf("Multiple choice STATE=%d\n", *state);
444 inst = ctx[inst_num];
446 /* we need to redo it */
447 inst = ctx[inst_num];
450 match_inst(inst, buf, nb_token, NULL, 0))
453 token_p = get_token(inst, nb_token);
455 memcpy(&token_hdr, token_p, sizeof(token_hdr));
457 /* one choice for this token */
459 !token_hdr.ops->complete_get_nb ||
460 !token_hdr.ops->complete_get_elt ||
461 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
462 if (local_state < *state) {
467 if (token_p && token_hdr.ops->get_help) {
468 token_hdr.ops->get_help(token_p, tmpbuf,
470 help_str = inst->help_str;
472 snprintf(dst, size, "[%s]: %s", tmpbuf,
475 snprintf(dst, size, "[%s]: No help",
479 snprintf(dst, size, "[RETURN]");
484 /* several choices */
485 for (i=0 ; i<n ; i++) {
486 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
489 /* we have at least room for one char */
490 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
491 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
492 tmpbuf[tmp_len] = ' ';
493 tmpbuf[tmp_len + 1] = 0;
496 debug_printf(" choice <%s>\n", tmpbuf);
498 /* does the completion match the beginning of
500 if (!strncmp(partial_tok, tmpbuf,
502 if (local_state < *state) {
507 l=strlcpy(dst, tmpbuf, size);
508 if (l>=0 && token_hdr.ops->get_help) {
509 token_hdr.ops->get_help(token_p, tmpbuf,
511 help_str = inst->help_str;
513 snprintf(dst+l, size-l, "[%s]: %s",
516 snprintf(dst+l, size-l,
517 "[%s]: No help", tmpbuf);
525 inst = ctx[inst_num];