1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
15 #include <netinet/in.h>
17 #include <rte_string_fns.h>
19 #include "cmdline_rdline.h"
20 #include "cmdline_parse.h"
23 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
24 #define debug_printf printf
26 #define debug_printf(args...) do {} while(0)
29 #define CMDLINE_BUFFER_SIZE 64
31 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
60 cmdline_isendoftoken(char c)
62 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
68 cmdline_isendofcommand(char c)
70 if (!c || iscomment(c) || isendofline(c))
76 nb_common_chars(const char * s1, const char * s2)
80 while (*s1==*s2 && *s1) {
88 /** Retrieve either static or dynamic token at a given index. */
89 static cmdline_parse_token_hdr_t *
90 get_token(cmdline_parse_inst_t *inst, unsigned int index)
92 cmdline_parse_token_hdr_t *token_p;
94 /* check presence of static tokens first */
95 if (inst->tokens[0] || !inst->f)
96 return inst->tokens[index];
97 /* generate dynamic token */
99 inst->f(&token_p, NULL, &inst->tokens[index]);
104 * try to match the buffer with an instruction (only the first
105 * nb_match_token tokens if != 0). Return 0 if we match all the
106 * tokens, else the number of matched tokens, else -1.
109 match_inst(cmdline_parse_inst_t *inst, const char *buf,
110 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size)
112 cmdline_parse_token_hdr_t *token_p = NULL;
115 struct cmdline_token_hdr token_hdr;
118 memset(resbuf, 0, resbuf_size);
119 /* check if we match all tokens of inst */
120 while (!nb_match_token || i < nb_match_token) {
121 token_p = get_token(inst, i);
124 memcpy(&token_hdr, token_p, sizeof(token_hdr));
126 debug_printf("TK\n");
128 while (isblank2(*buf)) {
133 if ( isendofline(*buf) || iscomment(*buf) )
136 if (resbuf == NULL) {
137 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
141 if (token_hdr.offset > resbuf_size) {
142 printf("Parse error(%s:%d): Token offset(%u) "
143 "exceeds maximum size(%u)\n",
145 token_hdr.offset, resbuf_size);
148 rb_sz = resbuf_size - token_hdr.offset;
150 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
151 token_hdr.offset, rb_sz);
157 debug_printf("TK parsed (len=%d)\n", n);
166 /* in case we want to match a specific num of token */
167 if (nb_match_token) {
168 if (i == nb_match_token) {
174 /* we don't match all the tokens */
179 /* are there are some tokens more */
180 while (isblank2(*buf)) {
185 if ( isendofline(*buf) || iscomment(*buf) )
188 /* garbage after inst */
194 cmdline_parse(struct cmdline *cl, const char * buf)
196 unsigned int inst_num=0;
197 cmdline_parse_inst_t *inst;
200 char buf[CMDLINE_PARSE_RESULT_BUFSIZE];
201 long double align; /* strong alignment constraint for buf */
202 } result, tmp_result;
203 void (*f)(void *, struct cmdline *, void *) = NULL;
208 int err = CMDLINE_PARSE_NOMATCH;
210 cmdline_parse_ctx_t *ctx;
211 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
212 char debug_buf[BUFSIZ];
214 char *result_buf = result.buf;
217 return CMDLINE_PARSE_BAD_ARGS;
222 * - look if the buffer contains at least one line
223 * - look if line contains only spaces or comments
224 * - count line length
227 while (! isendofline(*curbuf)) {
228 if ( *curbuf == '\0' ) {
229 debug_printf("Incomplete buf (len=%d)\n", linelen);
232 if ( iscomment(*curbuf) ) {
235 if ( ! isblank2(*curbuf) && ! comment) {
242 /* skip all endofline chars */
243 while (isendofline(buf[linelen])) {
248 if ( parse_it == 0 ) {
249 debug_printf("Empty line (len=%d)\n", linelen);
253 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
254 strlcpy(debug_buf, buf, (linelen > 64 ? 64 : linelen));
255 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
259 inst = ctx[inst_num];
261 debug_printf("INST %d\n", inst_num);
264 tok = match_inst(inst, buf, 0, result_buf,
265 CMDLINE_PARSE_RESULT_BUFSIZE);
267 if (tok > 0) /* we matched at least one token */
268 err = CMDLINE_PARSE_BAD_ARGS;
271 debug_printf("INST fully parsed\n");
273 while (isblank2(*curbuf)) {
277 /* if end of buf -> there is no garbage after inst */
278 if (isendofline(*curbuf) || iscomment(*curbuf)) {
280 memcpy(&f, &inst->f, sizeof(f));
281 memcpy(&data, &inst->data, sizeof(data));
282 result_buf = tmp_result.buf;
285 /* more than 1 inst matches */
286 err = CMDLINE_PARSE_AMBIGUOUS;
288 debug_printf("Ambiguous cmd\n");
295 inst = ctx[inst_num];
300 f(result.buf, cl, data);
305 debug_printf("No match err=%d\n", err);
313 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
314 char *dst, unsigned int size)
316 const char *partial_tok = buf;
317 unsigned int inst_num = 0;
318 cmdline_parse_inst_t *inst;
319 cmdline_parse_token_hdr_t *token_p;
320 struct cmdline_token_hdr token_hdr;
321 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
322 unsigned int partial_tok_len;
328 unsigned int nb_completable;
329 unsigned int nb_non_completable;
331 const char *help_str;
332 cmdline_parse_ctx_t *ctx;
334 if (!cl || !buf || !state || !dst)
339 debug_printf("%s called\n", __func__);
340 memset(&token_hdr, 0, sizeof(token_hdr));
342 /* count the number of complete token to parse */
343 for (i=0 ; buf[i] ; i++) {
344 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
346 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
347 partial_tok = buf+i+1;
349 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
351 /* first call -> do a first pass */
353 debug_printf("try complete <%s>\n", buf);
354 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
355 nb_token, partial_tok);
358 nb_non_completable = 0;
360 inst = ctx[inst_num];
362 /* parse the first tokens of the inst */
364 match_inst(inst, buf, nb_token, NULL, 0))
367 debug_printf("instruction match\n");
368 token_p = get_token(inst, nb_token);
370 memcpy(&token_hdr, token_p, sizeof(token_hdr));
372 /* non completable */
374 !token_hdr.ops->complete_get_nb ||
375 !token_hdr.ops->complete_get_elt ||
376 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
377 nb_non_completable++;
381 debug_printf("%d choices for this token\n", n);
382 for (i=0 ; i<n ; i++) {
383 if (token_hdr.ops->complete_get_elt(token_p, i,
388 /* we have at least room for one char */
389 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
390 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
391 tmpbuf[tmp_len] = ' ';
392 tmpbuf[tmp_len+1] = 0;
395 debug_printf(" choice <%s>\n", tmpbuf);
397 /* does the completion match the
398 * beginning of the word ? */
399 if (!strncmp(partial_tok, tmpbuf,
401 if (comp_len == -1) {
402 snprintf(comp_buf, sizeof(comp_buf),
403 "%s", 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];