examples: use new API to create control threads
[dpdk.git] / lib / librte_cmdline / cmdline_parse.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
4  * All rights reserved.
5  */
6
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <errno.h>
10 #include <string.h>
11 #include <inttypes.h>
12 #include <ctype.h>
13 #include <termios.h>
14
15 #include <netinet/in.h>
16
17 #include <rte_string_fns.h>
18
19 #include "cmdline_rdline.h"
20 #include "cmdline_parse.h"
21 #include "cmdline.h"
22
23 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
24 #define debug_printf printf
25 #else
26 #define debug_printf(args...) do {} while(0)
27 #endif
28
29 #define CMDLINE_BUFFER_SIZE 64
30
31 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
32  * own. */
33 static int
34 isblank2(char c)
35 {
36         if (c == ' ' ||
37             c == '\t' )
38                 return 1;
39         return 0;
40 }
41
42 static int
43 isendofline(char c)
44 {
45         if (c == '\n' ||
46             c == '\r' )
47                 return 1;
48         return 0;
49 }
50
51 static int
52 iscomment(char c)
53 {
54         if (c == '#')
55                 return 1;
56         return 0;
57 }
58
59 int
60 cmdline_isendoftoken(char c)
61 {
62         if (!c || iscomment(c) || isblank2(c) || isendofline(c))
63                 return 1;
64         return 0;
65 }
66
67 int
68 cmdline_isendofcommand(char c)
69 {
70         if (!c || iscomment(c) || isendofline(c))
71                 return 1;
72         return 0;
73 }
74
75 static unsigned int
76 nb_common_chars(const char * s1, const char * s2)
77 {
78         unsigned int i=0;
79
80         while (*s1==*s2 && *s1) {
81                 s1++;
82                 s2++;
83                 i++;
84         }
85         return i;
86 }
87
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)
91 {
92         cmdline_parse_token_hdr_t *token_p;
93
94         /* check presence of static tokens first */
95         if (inst->tokens[0] || !inst->f)
96                 return inst->tokens[index];
97         /* generate dynamic token */
98         token_p = NULL;
99         inst->f(&token_p, NULL, &inst->tokens[index]);
100         return token_p;
101 }
102
103 /**
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.
107  */
108 static int
109 match_inst(cmdline_parse_inst_t *inst, const char *buf,
110            unsigned int nb_match_token, void *resbuf, unsigned resbuf_size)
111 {
112         cmdline_parse_token_hdr_t *token_p = NULL;
113         unsigned int i=0;
114         int n = 0;
115         struct cmdline_token_hdr token_hdr;
116
117         if (resbuf != NULL)
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);
122                 if (!token_p)
123                         break;
124                 memcpy(&token_hdr, token_p, sizeof(token_hdr));
125
126                 debug_printf("TK\n");
127                 /* skip spaces */
128                 while (isblank2(*buf)) {
129                         buf++;
130                 }
131
132                 /* end of buf */
133                 if ( isendofline(*buf) || iscomment(*buf) )
134                         break;
135
136                 if (resbuf == NULL) {
137                         n = token_hdr.ops->parse(token_p, buf, NULL, 0);
138                 } else {
139                         unsigned rb_sz;
140
141                         if (token_hdr.offset > resbuf_size) {
142                                 printf("Parse error(%s:%d): Token offset(%u) "
143                                         "exceeds maximum size(%u)\n",
144                                         __FILE__, __LINE__,
145                                         token_hdr.offset, resbuf_size);
146                                 return -ENOBUFS;
147                         }
148                         rb_sz = resbuf_size - token_hdr.offset;
149
150                         n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
151                                 token_hdr.offset, rb_sz);
152                 }
153
154                 if (n < 0)
155                         break;
156
157                 debug_printf("TK parsed (len=%d)\n", n);
158                 i++;
159                 buf += n;
160         }
161
162         /* does not match */
163         if (i==0)
164                 return -1;
165
166         /* in case we want to match a specific num of token */
167         if (nb_match_token) {
168                 if (i == nb_match_token) {
169                         return 0;
170                 }
171                 return i;
172         }
173
174         /* we don't match all the tokens */
175         if (token_p) {
176                 return i;
177         }
178
179         /* are there are some tokens more */
180         while (isblank2(*buf)) {
181                 buf++;
182         }
183
184         /* end of buf */
185         if ( isendofline(*buf) || iscomment(*buf) )
186                 return 0;
187
188         /* garbage after inst */
189         return i;
190 }
191
192
193 int
194 cmdline_parse(struct cmdline *cl, const char * buf)
195 {
196         unsigned int inst_num=0;
197         cmdline_parse_inst_t *inst;
198         const char *curbuf;
199         union {
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;
204         void *data = NULL;
205         int comment = 0;
206         int linelen = 0;
207         int parse_it = 0;
208         int err = CMDLINE_PARSE_NOMATCH;
209         int tok;
210         cmdline_parse_ctx_t *ctx;
211 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
212         char debug_buf[BUFSIZ];
213 #endif
214         char *result_buf = result.buf;
215
216         if (!cl || !buf)
217                 return CMDLINE_PARSE_BAD_ARGS;
218
219         ctx = cl->ctx;
220
221         /*
222          * - look if the buffer contains at least one line
223          * - look if line contains only spaces or comments
224          * - count line length
225          */
226         curbuf = buf;
227         while (! isendofline(*curbuf)) {
228                 if ( *curbuf == '\0' ) {
229                         debug_printf("Incomplete buf (len=%d)\n", linelen);
230                         return 0;
231                 }
232                 if ( iscomment(*curbuf) ) {
233                         comment = 1;
234                 }
235                 if ( ! isblank2(*curbuf) && ! comment) {
236                         parse_it = 1;
237                 }
238                 curbuf++;
239                 linelen++;
240         }
241
242         /* skip all endofline chars */
243         while (isendofline(buf[linelen])) {
244                 linelen++;
245         }
246
247         /* empty line */
248         if ( parse_it == 0 ) {
249                 debug_printf("Empty line (len=%d)\n", linelen);
250                 return linelen;
251         }
252
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);
256 #endif
257
258         /* parse it !! */
259         inst = ctx[inst_num];
260         while (inst) {
261                 debug_printf("INST %d\n", inst_num);
262
263                 /* fully parsed */
264                 tok = match_inst(inst, buf, 0, result_buf,
265                                  CMDLINE_PARSE_RESULT_BUFSIZE);
266
267                 if (tok > 0) /* we matched at least one token */
268                         err = CMDLINE_PARSE_BAD_ARGS;
269
270                 else if (!tok) {
271                         debug_printf("INST fully parsed\n");
272                         /* skip spaces */
273                         while (isblank2(*curbuf)) {
274                                 curbuf++;
275                         }
276
277                         /* if end of buf -> there is no garbage after inst */
278                         if (isendofline(*curbuf) || iscomment(*curbuf)) {
279                                 if (!f) {
280                                         memcpy(&f, &inst->f, sizeof(f));
281                                         memcpy(&data, &inst->data, sizeof(data));
282                                         result_buf = tmp_result.buf;
283                                 }
284                                 else {
285                                         /* more than 1 inst matches */
286                                         err = CMDLINE_PARSE_AMBIGUOUS;
287                                         f=NULL;
288                                         debug_printf("Ambiguous cmd\n");
289                                         break;
290                                 }
291                         }
292                 }
293
294                 inst_num ++;
295                 inst = ctx[inst_num];
296         }
297
298         /* call func */
299         if (f) {
300                 f(result.buf, cl, data);
301         }
302
303         /* no match */
304         else {
305                 debug_printf("No match err=%d\n", err);
306                 return err;
307         }
308
309         return linelen;
310 }
311
312 int
313 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
314                  char *dst, unsigned int size)
315 {
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;
323         int comp_len = -1;
324         int tmp_len = -1;
325         int nb_token = 0;
326         unsigned int i, n;
327         int l;
328         unsigned int nb_completable;
329         unsigned int nb_non_completable;
330         int local_state = 0;
331         const char *help_str;
332         cmdline_parse_ctx_t *ctx;
333
334         if (!cl || !buf || !state || !dst)
335                 return -1;
336
337         ctx = cl->ctx;
338
339         debug_printf("%s called\n", __func__);
340         memset(&token_hdr, 0, sizeof(token_hdr));
341
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]))
345                         nb_token++;
346                 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
347                         partial_tok = buf+i+1;
348         }
349         partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
350
351         /* first call -> do a first pass */
352         if (*state <= 0) {
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);
356
357                 nb_completable = 0;
358                 nb_non_completable = 0;
359
360                 inst = ctx[inst_num];
361                 while (inst) {
362                         /* parse the first tokens of the inst */
363                         if (nb_token &&
364                             match_inst(inst, buf, nb_token, NULL, 0))
365                                 goto next;
366
367                         debug_printf("instruction match\n");
368                         token_p = get_token(inst, nb_token);
369                         if (token_p)
370                                 memcpy(&token_hdr, token_p, sizeof(token_hdr));
371
372                         /* non completable */
373                         if (!token_p ||
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++;
378                                 goto next;
379                         }
380
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,
384                                                                     tmpbuf,
385                                                                     sizeof(tmpbuf)) < 0)
386                                         continue;
387
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;
393                                 }
394
395                                 debug_printf("   choice <%s>\n", tmpbuf);
396
397                                 /* does the completion match the
398                                  * beginning of the word ? */
399                                 if (!strncmp(partial_tok, tmpbuf,
400                                              partial_tok_len)) {
401                                         if (comp_len == -1) {
402                                                 snprintf(comp_buf, sizeof(comp_buf),
403                                                          "%s", tmpbuf + partial_tok_len);
404                                                 comp_len =
405                                                         strnlen(tmpbuf + partial_tok_len,
406                                                                         sizeof(tmpbuf) - partial_tok_len);
407
408                                         }
409                                         else {
410                                                 comp_len =
411                                                         nb_common_chars(comp_buf,
412                                                                         tmpbuf+partial_tok_len);
413                                                 comp_buf[comp_len] = 0;
414                                         }
415                                         nb_completable++;
416                                 }
417                         }
418                 next:
419                         debug_printf("next\n");
420                         inst_num ++;
421                         inst = ctx[inst_num];
422                 }
423
424                 debug_printf("total choices %d for this completion\n",
425                              nb_completable);
426
427                 /* no possible completion */
428                 if (nb_completable == 0 && nb_non_completable == 0)
429                         return 0;
430
431                 /* if multichoice is not required */
432                 if (*state == 0 && partial_tok_len > 0) {
433                         /* one or several choices starting with the
434                            same chars */
435                         if (comp_len > 0) {
436                                 if ((unsigned)(comp_len + 1) > size)
437                                         return 0;
438
439                                 strlcpy(dst, comp_buf, size);
440                                 dst[comp_len] = 0;
441                                 return 2;
442                         }
443                 }
444         }
445
446         /* init state correctly */
447         if (*state == -1)
448                 *state = 0;
449
450         debug_printf("Multiple choice STATE=%d\n", *state);
451
452         inst_num = 0;
453         inst = ctx[inst_num];
454         while (inst) {
455                 /* we need to redo it */
456                 inst = ctx[inst_num];
457
458                 if (nb_token &&
459                     match_inst(inst, buf, nb_token, NULL, 0))
460                         goto next2;
461
462                 token_p = get_token(inst, nb_token);
463                 if (token_p)
464                         memcpy(&token_hdr, token_p, sizeof(token_hdr));
465
466                 /* one choice for this token */
467                 if (!token_p ||
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) {
472                                 local_state++;
473                                 goto next2;
474                         }
475                         (*state)++;
476                         if (token_p && token_hdr.ops->get_help) {
477                                 token_hdr.ops->get_help(token_p, tmpbuf,
478                                                         sizeof(tmpbuf));
479                                 help_str = inst->help_str;
480                                 if (help_str)
481                                         snprintf(dst, size, "[%s]: %s", tmpbuf,
482                                                  help_str);
483                                 else
484                                         snprintf(dst, size, "[%s]: No help",
485                                                  tmpbuf);
486                         }
487                         else {
488                                 snprintf(dst, size, "[RETURN]");
489                         }
490                         return 1;
491                 }
492
493                 /* several choices */
494                 for (i=0 ; i<n ; i++) {
495                         if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
496                                                             sizeof(tmpbuf)) < 0)
497                                 continue;
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;
503                         }
504
505                         debug_printf("   choice <%s>\n", tmpbuf);
506
507                         /* does the completion match the beginning of
508                          * the word ? */
509                         if (!strncmp(partial_tok, tmpbuf,
510                                      partial_tok_len)) {
511                                 if (local_state < *state) {
512                                         local_state++;
513                                         continue;
514                                 }
515                                 (*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,
519                                                                 sizeof(tmpbuf));
520                                         help_str = inst->help_str;
521                                         if (help_str)
522                                                 snprintf(dst+l, size-l, "[%s]: %s",
523                                                          tmpbuf, help_str);
524                                         else
525                                                 snprintf(dst+l, size-l,
526                                                          "[%s]: No help", tmpbuf);
527                                 }
528
529                                 return 1;
530                         }
531                 }
532         next2:
533                 inst_num ++;
534                 inst = ctx[inst_num];
535         }
536         return 0;
537 }