Imported Upstream version 16.04
[deb_dpdk.git] / lib / librte_cmdline / cmdline_parse.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /*
35  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
36  * All rights reserved.
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions are met:
39  *
40  *     * Redistributions of source code must retain the above copyright
41  *       notice, this list of conditions and the following disclaimer.
42  *     * Redistributions in binary form must reproduce the above copyright
43  *       notice, this list of conditions and the following disclaimer in the
44  *       documentation and/or other materials provided with the distribution.
45  *     * Neither the name of the University of California, Berkeley nor the
46  *       names of its contributors may be used to endorse or promote products
47  *       derived from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
50  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
53  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60
61 #include <stdio.h>
62 #include <stdarg.h>
63 #include <errno.h>
64 #include <string.h>
65 #include <inttypes.h>
66 #include <ctype.h>
67 #include <termios.h>
68
69 #include <netinet/in.h>
70
71 #include <rte_string_fns.h>
72
73 #include "cmdline_rdline.h"
74 #include "cmdline_parse.h"
75 #include "cmdline.h"
76
77 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
78 #define debug_printf printf
79 #else
80 #define debug_printf(args...) do {} while(0)
81 #endif
82
83 #define CMDLINE_BUFFER_SIZE 64
84
85 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
86  * own. */
87 static int
88 isblank2(char c)
89 {
90         if (c == ' ' ||
91             c == '\t' )
92                 return 1;
93         return 0;
94 }
95
96 static int
97 isendofline(char c)
98 {
99         if (c == '\n' ||
100             c == '\r' )
101                 return 1;
102         return 0;
103 }
104
105 static int
106 iscomment(char c)
107 {
108         if (c == '#')
109                 return 1;
110         return 0;
111 }
112
113 int
114 cmdline_isendoftoken(char c)
115 {
116         if (!c || iscomment(c) || isblank2(c) || isendofline(c))
117                 return 1;
118         return 0;
119 }
120
121 static unsigned int
122 nb_common_chars(const char * s1, const char * s2)
123 {
124         unsigned int i=0;
125
126         while (*s1==*s2 && *s1) {
127                 s1++;
128                 s2++;
129                 i++;
130         }
131         return i;
132 }
133
134 /**
135  * try to match the buffer with an instruction (only the first
136  * nb_match_token tokens if != 0). Return 0 if we match all the
137  * tokens, else the number of matched tokens, else -1.
138  */
139 static int
140 match_inst(cmdline_parse_inst_t *inst, const char *buf,
141            unsigned int nb_match_token, void *resbuf, unsigned resbuf_size)
142 {
143         unsigned int token_num=0;
144         cmdline_parse_token_hdr_t * token_p;
145         unsigned int i=0;
146         int n = 0;
147         struct cmdline_token_hdr token_hdr;
148
149         token_p = inst->tokens[token_num];
150         if (token_p)
151                 memcpy(&token_hdr, token_p, sizeof(token_hdr));
152
153         /* check if we match all tokens of inst */
154         while (token_p && (!nb_match_token || i<nb_match_token)) {
155                 debug_printf("TK\n");
156                 /* skip spaces */
157                 while (isblank2(*buf)) {
158                         buf++;
159                 }
160
161                 /* end of buf */
162                 if ( isendofline(*buf) || iscomment(*buf) )
163                         break;
164
165                 if (resbuf == NULL) {
166                         n = token_hdr.ops->parse(token_p, buf, NULL, 0);
167                 } else {
168                         unsigned rb_sz;
169
170                         if (token_hdr.offset > resbuf_size) {
171                                 printf("Parse error(%s:%d): Token offset(%u) "
172                                         "exceeds maximum size(%u)\n",
173                                         __FILE__, __LINE__,
174                                         token_hdr.offset, resbuf_size);
175                                 return -ENOBUFS;
176                         }
177                         rb_sz = resbuf_size - token_hdr.offset;
178
179                         n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
180                                 token_hdr.offset, rb_sz);
181                 }
182
183                 if (n < 0)
184                         break;
185
186                 debug_printf("TK parsed (len=%d)\n", n);
187                 i++;
188                 buf += n;
189
190                 token_num ++;
191                 token_p = inst->tokens[token_num];
192                 if (token_p)
193                         memcpy(&token_hdr, token_p, sizeof(token_hdr));
194         }
195
196         /* does not match */
197         if (i==0)
198                 return -1;
199
200         /* in case we want to match a specific num of token */
201         if (nb_match_token) {
202                 if (i == nb_match_token) {
203                         return 0;
204                 }
205                 return i;
206         }
207
208         /* we don't match all the tokens */
209         if (token_p) {
210                 return i;
211         }
212
213         /* are there are some tokens more */
214         while (isblank2(*buf)) {
215                 buf++;
216         }
217
218         /* end of buf */
219         if ( isendofline(*buf) || iscomment(*buf) )
220                 return 0;
221
222         /* garbage after inst */
223         return i;
224 }
225
226
227 int
228 cmdline_parse(struct cmdline *cl, const char * buf)
229 {
230         unsigned int inst_num=0;
231         cmdline_parse_inst_t *inst;
232         const char *curbuf;
233         char result_buf[CMDLINE_PARSE_RESULT_BUFSIZE];
234         void (*f)(void *, struct cmdline *, void *) = NULL;
235         void *data = NULL;
236         int comment = 0;
237         int linelen = 0;
238         int parse_it = 0;
239         int err = CMDLINE_PARSE_NOMATCH;
240         int tok;
241         cmdline_parse_ctx_t *ctx;
242 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
243         char debug_buf[BUFSIZ];
244 #endif
245
246         if (!cl || !buf)
247                 return CMDLINE_PARSE_BAD_ARGS;
248
249         ctx = cl->ctx;
250
251         /*
252          * - look if the buffer contains at least one line
253          * - look if line contains only spaces or comments
254          * - count line length
255          */
256         curbuf = buf;
257         while (! isendofline(*curbuf)) {
258                 if ( *curbuf == '\0' ) {
259                         debug_printf("Incomplete buf (len=%d)\n", linelen);
260                         return 0;
261                 }
262                 if ( iscomment(*curbuf) ) {
263                         comment = 1;
264                 }
265                 if ( ! isblank2(*curbuf) && ! comment) {
266                         parse_it = 1;
267                 }
268                 curbuf++;
269                 linelen++;
270         }
271
272         /* skip all endofline chars */
273         while (isendofline(buf[linelen])) {
274                 linelen++;
275         }
276
277         /* empty line */
278         if ( parse_it == 0 ) {
279                 debug_printf("Empty line (len=%d)\n", linelen);
280                 return linelen;
281         }
282
283 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
284         snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
285         debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
286 #endif
287
288         /* parse it !! */
289         inst = ctx[inst_num];
290         while (inst) {
291                 debug_printf("INST %d\n", inst_num);
292
293                 /* fully parsed */
294                 tok = match_inst(inst, buf, 0, result_buf, sizeof(result_buf));
295
296                 if (tok > 0) /* we matched at least one token */
297                         err = CMDLINE_PARSE_BAD_ARGS;
298
299                 else if (!tok) {
300                         debug_printf("INST fully parsed\n");
301                         /* skip spaces */
302                         while (isblank2(*curbuf)) {
303                                 curbuf++;
304                         }
305
306                         /* if end of buf -> there is no garbage after inst */
307                         if (isendofline(*curbuf) || iscomment(*curbuf)) {
308                                 if (!f) {
309                                         memcpy(&f, &inst->f, sizeof(f));
310                                         memcpy(&data, &inst->data, sizeof(data));
311                                 }
312                                 else {
313                                         /* more than 1 inst matches */
314                                         err = CMDLINE_PARSE_AMBIGUOUS;
315                                         f=NULL;
316                                         debug_printf("Ambiguous cmd\n");
317                                         break;
318                                 }
319                         }
320                 }
321
322                 inst_num ++;
323                 inst = ctx[inst_num];
324         }
325
326         /* call func */
327         if (f) {
328                 f(result_buf, cl, data);
329         }
330
331         /* no match */
332         else {
333                 debug_printf("No match err=%d\n", err);
334                 return err;
335         }
336
337         return linelen;
338 }
339
340 int
341 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
342                  char *dst, unsigned int size)
343 {
344         const char *partial_tok = buf;
345         unsigned int inst_num = 0;
346         cmdline_parse_inst_t *inst;
347         cmdline_parse_token_hdr_t *token_p;
348         struct cmdline_token_hdr token_hdr;
349         char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
350         unsigned int partial_tok_len;
351         int comp_len = -1;
352         int tmp_len = -1;
353         int nb_token = 0;
354         unsigned int i, n;
355         int l;
356         unsigned int nb_completable;
357         unsigned int nb_non_completable;
358         int local_state = 0;
359         const char *help_str;
360         cmdline_parse_ctx_t *ctx;
361
362         if (!cl || !buf || !state || !dst)
363                 return -1;
364
365         ctx = cl->ctx;
366
367         debug_printf("%s called\n", __func__);
368         memset(&token_hdr, 0, sizeof(token_hdr));
369
370         /* count the number of complete token to parse */
371         for (i=0 ; buf[i] ; i++) {
372                 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
373                         nb_token++;
374                 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
375                         partial_tok = buf+i+1;
376         }
377         partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
378
379         /* first call -> do a first pass */
380         if (*state <= 0) {
381                 debug_printf("try complete <%s>\n", buf);
382                 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
383                              nb_token, partial_tok);
384
385                 nb_completable = 0;
386                 nb_non_completable = 0;
387
388                 inst = ctx[inst_num];
389                 while (inst) {
390                         /* parse the first tokens of the inst */
391                         if (nb_token && match_inst(inst, buf, nb_token, NULL, 0))
392                                 goto next;
393
394                         debug_printf("instruction match\n");
395                         token_p = inst->tokens[nb_token];
396                         if (token_p)
397                                 memcpy(&token_hdr, token_p, sizeof(token_hdr));
398
399                         /* non completable */
400                         if (!token_p ||
401                             !token_hdr.ops->complete_get_nb ||
402                             !token_hdr.ops->complete_get_elt ||
403                             (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
404                                 nb_non_completable++;
405                                 goto next;
406                         }
407
408                         debug_printf("%d choices for this token\n", n);
409                         for (i=0 ; i<n ; i++) {
410                                 if (token_hdr.ops->complete_get_elt(token_p, i,
411                                                                     tmpbuf,
412                                                                     sizeof(tmpbuf)) < 0)
413                                         continue;
414
415                                 /* we have at least room for one char */
416                                 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
417                                 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
418                                         tmpbuf[tmp_len] = ' ';
419                                         tmpbuf[tmp_len+1] = 0;
420                                 }
421
422                                 debug_printf("   choice <%s>\n", tmpbuf);
423
424                                 /* does the completion match the
425                                  * beginning of the word ? */
426                                 if (!strncmp(partial_tok, tmpbuf,
427                                              partial_tok_len)) {
428                                         if (comp_len == -1) {
429                                                 snprintf(comp_buf, sizeof(comp_buf),
430                                                          "%s", tmpbuf + partial_tok_len);
431                                                 comp_len =
432                                                         strnlen(tmpbuf + partial_tok_len,
433                                                                         sizeof(tmpbuf) - partial_tok_len);
434
435                                         }
436                                         else {
437                                                 comp_len =
438                                                         nb_common_chars(comp_buf,
439                                                                         tmpbuf+partial_tok_len);
440                                                 comp_buf[comp_len] = 0;
441                                         }
442                                         nb_completable++;
443                                 }
444                         }
445                 next:
446                         debug_printf("next\n");
447                         inst_num ++;
448                         inst = ctx[inst_num];
449                 }
450
451                 debug_printf("total choices %d for this completion\n",
452                              nb_completable);
453
454                 /* no possible completion */
455                 if (nb_completable == 0 && nb_non_completable == 0)
456                         return 0;
457
458                 /* if multichoice is not required */
459                 if (*state == 0 && partial_tok_len > 0) {
460                         /* one or several choices starting with the
461                            same chars */
462                         if (comp_len > 0) {
463                                 if ((unsigned)(comp_len + 1) > size)
464                                         return 0;
465
466                                 snprintf(dst, size, "%s", comp_buf);
467                                 dst[comp_len] = 0;
468                                 return 2;
469                         }
470                 }
471         }
472
473         /* init state correctly */
474         if (*state == -1)
475                 *state = 0;
476
477         debug_printf("Multiple choice STATE=%d\n", *state);
478
479         inst_num = 0;
480         inst = ctx[inst_num];
481         while (inst) {
482                 /* we need to redo it */
483                 inst = ctx[inst_num];
484
485                 if (nb_token && match_inst(inst, buf, nb_token, NULL, 0))
486                         goto next2;
487
488                 token_p = inst->tokens[nb_token];
489                 if (token_p)
490                         memcpy(&token_hdr, token_p, sizeof(token_hdr));
491
492                 /* one choice for this token */
493                 if (!token_p ||
494                     !token_hdr.ops->complete_get_nb ||
495                     !token_hdr.ops->complete_get_elt ||
496                     (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
497                         if (local_state < *state) {
498                                 local_state++;
499                                 goto next2;
500                         }
501                         (*state)++;
502                         if (token_p && token_hdr.ops->get_help) {
503                                 token_hdr.ops->get_help(token_p, tmpbuf,
504                                                         sizeof(tmpbuf));
505                                 help_str = inst->help_str;
506                                 if (help_str)
507                                         snprintf(dst, size, "[%s]: %s", tmpbuf,
508                                                  help_str);
509                                 else
510                                         snprintf(dst, size, "[%s]: No help",
511                                                  tmpbuf);
512                         }
513                         else {
514                                 snprintf(dst, size, "[RETURN]");
515                         }
516                         return 1;
517                 }
518
519                 /* several choices */
520                 for (i=0 ; i<n ; i++) {
521                         if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
522                                                             sizeof(tmpbuf)) < 0)
523                                 continue;
524                         /* we have at least room for one char */
525                         tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
526                         if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
527                                 tmpbuf[tmp_len] = ' ';
528                                 tmpbuf[tmp_len + 1] = 0;
529                         }
530
531                         debug_printf("   choice <%s>\n", tmpbuf);
532
533                         /* does the completion match the beginning of
534                          * the word ? */
535                         if (!strncmp(partial_tok, tmpbuf,
536                                      partial_tok_len)) {
537                                 if (local_state < *state) {
538                                         local_state++;
539                                         continue;
540                                 }
541                                 (*state)++;
542                                 l=snprintf(dst, size, "%s", tmpbuf);
543                                 if (l>=0 && token_hdr.ops->get_help) {
544                                         token_hdr.ops->get_help(token_p, tmpbuf,
545                                                                 sizeof(tmpbuf));
546                                         help_str = inst->help_str;
547                                         if (help_str)
548                                                 snprintf(dst+l, size-l, "[%s]: %s",
549                                                          tmpbuf, help_str);
550                                         else
551                                                 snprintf(dst+l, size-l,
552                                                          "[%s]: No help", tmpbuf);
553                                 }
554
555                                 return 1;
556                         }
557                 }
558         next2:
559                 inst_num ++;
560                 inst = ctx[inst_num];
561         }
562         return 0;
563 }