doc: whitespace changes in licenses
[dpdk.git] / lib / librte_cmdline / cmdline_parse.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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 * result_buf)
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 (result_buf)
166                         n = token_hdr.ops->parse(token_p, buf,
167                                                  (char *)result_buf +
168                                                  token_hdr.offset);
169                 else
170                         n = token_hdr.ops->parse(token_p, buf, NULL);
171
172                 if (n < 0)
173                         break;
174
175                 debug_printf("TK parsed (len=%d)\n", n);
176                 i++;
177                 buf += n;
178
179                 token_num ++;
180                 token_p = inst->tokens[token_num];
181                 if (token_p)
182                         memcpy(&token_hdr, token_p, sizeof(token_hdr));
183         }
184
185         /* does not match */
186         if (i==0)
187                 return -1;
188
189         /* in case we want to match a specific num of token */
190         if (nb_match_token) {
191                 if (i == nb_match_token) {
192                         return 0;
193                 }
194                 return i;
195         }
196
197         /* we don't match all the tokens */
198         if (token_p) {
199                 return i;
200         }
201
202         /* are there are some tokens more */
203         while (isblank2(*buf)) {
204                 buf++;
205         }
206
207         /* end of buf */
208         if ( isendofline(*buf) || iscomment(*buf) )
209                 return 0;
210
211         /* garbage after inst */
212         return i;
213 }
214
215
216 int
217 cmdline_parse(struct cmdline *cl, const char * buf)
218 {
219         unsigned int inst_num=0;
220         cmdline_parse_inst_t *inst;
221         const char *curbuf;
222         char result_buf[BUFSIZ];
223         void (*f)(void *, struct cmdline *, void *) = NULL;
224         void *data = NULL;
225         int comment = 0;
226         int linelen = 0;
227         int parse_it = 0;
228         int err = CMDLINE_PARSE_NOMATCH;
229         int tok;
230         cmdline_parse_ctx_t *ctx;
231 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
232         char debug_buf[BUFSIZ];
233 #endif
234
235         if (!cl || !buf)
236                 return CMDLINE_PARSE_BAD_ARGS;
237
238         ctx = cl->ctx;
239
240         /*
241          * - look if the buffer contains at least one line
242          * - look if line contains only spaces or comments
243          * - count line length
244          */
245         curbuf = buf;
246         while (! isendofline(*curbuf)) {
247                 if ( *curbuf == '\0' ) {
248                         debug_printf("Incomplete buf (len=%d)\n", linelen);
249                         return 0;
250                 }
251                 if ( iscomment(*curbuf) ) {
252                         comment = 1;
253                 }
254                 if ( ! isblank2(*curbuf) && ! comment) {
255                         parse_it = 1;
256                 }
257                 curbuf++;
258                 linelen++;
259         }
260
261         /* skip all endofline chars */
262         while (isendofline(buf[linelen])) {
263                 linelen++;
264         }
265
266         /* empty line */
267         if ( parse_it == 0 ) {
268                 debug_printf("Empty line (len=%d)\n", linelen);
269                 return linelen;
270         }
271
272 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
273         rte_snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
274         debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
275 #endif
276
277         /* parse it !! */
278         inst = ctx[inst_num];
279         while (inst) {
280                 debug_printf("INST %d\n", inst_num);
281
282                 /* fully parsed */
283                 tok = match_inst(inst, buf, 0, result_buf);
284
285                 if (tok > 0) /* we matched at least one token */
286                         err = CMDLINE_PARSE_BAD_ARGS;
287
288                 else if (!tok) {
289                         debug_printf("INST fully parsed\n");
290                         /* skip spaces */
291                         while (isblank2(*curbuf)) {
292                                 curbuf++;
293                         }
294
295                         /* if end of buf -> there is no garbage after inst */
296                         if (isendofline(*curbuf) || iscomment(*curbuf)) {
297                                 if (!f) {
298                                         memcpy(&f, &inst->f, sizeof(f));
299                                         memcpy(&data, &inst->data, sizeof(data));
300                                 }
301                                 else {
302                                         /* more than 1 inst matches */
303                                         err = CMDLINE_PARSE_AMBIGUOUS;
304                                         f=NULL;
305                                         debug_printf("Ambiguous cmd\n");
306                                         break;
307                                 }
308                         }
309                 }
310
311                 inst_num ++;
312                 inst = ctx[inst_num];
313         }
314
315         /* call func */
316         if (f) {
317                 f(result_buf, cl, data);
318         }
319
320         /* no match */
321         else {
322                 debug_printf("No match err=%d\n", err);
323                 return err;
324         }
325
326         return linelen;
327 }
328
329 int
330 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
331                  char *dst, unsigned int size)
332 {
333         const char *partial_tok = buf;
334         unsigned int inst_num = 0;
335         cmdline_parse_inst_t *inst;
336         cmdline_parse_token_hdr_t *token_p;
337         struct cmdline_token_hdr token_hdr;
338         char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
339         unsigned int partial_tok_len;
340         int comp_len = -1;
341         int tmp_len = -1;
342         int nb_token = 0;
343         unsigned int i, n;
344         int l;
345         unsigned int nb_completable;
346         unsigned int nb_non_completable;
347         int local_state = 0;
348         const char *help_str;
349         cmdline_parse_ctx_t *ctx;
350
351         if (!cl || !buf || !state || !dst)
352                 return -1;
353
354         ctx = cl->ctx;
355
356         debug_printf("%s called\n", __func__);
357         memset(&token_hdr, 0, sizeof(token_hdr));
358
359         /* count the number of complete token to parse */
360         for (i=0 ; buf[i] ; i++) {
361                 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
362                         nb_token++;
363                 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
364                         partial_tok = buf+i+1;
365         }
366         partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
367
368         /* first call -> do a first pass */
369         if (*state <= 0) {
370                 debug_printf("try complete <%s>\n", buf);
371                 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
372                              nb_token, partial_tok);
373
374                 nb_completable = 0;
375                 nb_non_completable = 0;
376
377                 inst = ctx[inst_num];
378                 while (inst) {
379                         /* parse the first tokens of the inst */
380                         if (nb_token && match_inst(inst, buf, nb_token, NULL))
381                                 goto next;
382
383                         debug_printf("instruction match \n");
384                         token_p = inst->tokens[nb_token];
385                         if (token_p)
386                                 memcpy(&token_hdr, token_p, sizeof(token_hdr));
387
388                         /* non completable */
389                         if (!token_p ||
390                             !token_hdr.ops->complete_get_nb ||
391                             !token_hdr.ops->complete_get_elt ||
392                             (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
393                                 nb_non_completable++;
394                                 goto next;
395                         }
396
397                         debug_printf("%d choices for this token\n", n);
398                         for (i=0 ; i<n ; i++) {
399                                 if (token_hdr.ops->complete_get_elt(token_p, i,
400                                                                     tmpbuf,
401                                                                     sizeof(tmpbuf)) < 0)
402                                         continue;
403
404                                 /* we have at least room for one char */
405                                 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
406                                 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
407                                         tmpbuf[tmp_len] = ' ';
408                                         tmpbuf[tmp_len+1] = 0;
409                                 }
410
411                                 debug_printf("   choice <%s>\n", tmpbuf);
412
413                                 /* does the completion match the
414                                  * beginning of the word ? */
415                                 if (!strncmp(partial_tok, tmpbuf,
416                                              partial_tok_len)) {
417                                         if (comp_len == -1) {
418                                                 rte_snprintf(comp_buf, sizeof(comp_buf),
419                                                          "%s", tmpbuf + partial_tok_len);
420                                                 comp_len =
421                                                         strnlen(tmpbuf + partial_tok_len,
422                                                                         sizeof(tmpbuf) - partial_tok_len);
423
424                                         }
425                                         else {
426                                                 comp_len =
427                                                         nb_common_chars(comp_buf,
428                                                                         tmpbuf+partial_tok_len);
429                                                 comp_buf[comp_len] = 0;
430                                         }
431                                         nb_completable++;
432                                 }
433                         }
434                 next:
435                         debug_printf("next\n");
436                         inst_num ++;
437                         inst = ctx[inst_num];
438                 }
439
440                 debug_printf("total choices %d for this completion\n",
441                              nb_completable);
442
443                 /* no possible completion */
444                 if (nb_completable == 0 && nb_non_completable == 0)
445                         return 0;
446
447                 /* if multichoice is not required */
448                 if (*state == 0 && partial_tok_len > 0) {
449                         /* one or several choices starting with the
450                            same chars */
451                         if (comp_len > 0) {
452                                 if ((unsigned)(comp_len + 1) > size)
453                                         return 0;
454
455                                 rte_snprintf(dst, size, "%s", comp_buf);
456                                 dst[comp_len] = 0;
457                                 return 2;
458                         }
459                 }
460         }
461
462         /* init state correctly */
463         if (*state == -1)
464                 *state = 0;
465
466         debug_printf("Multiple choice STATE=%d\n", *state);
467
468         inst_num = 0;
469         inst = ctx[inst_num];
470         while (inst) {
471                 /* we need to redo it */
472                 inst = ctx[inst_num];
473
474                 if (nb_token && match_inst(inst, buf, nb_token, NULL))
475                         goto next2;
476
477                 token_p = inst->tokens[nb_token];
478                 if (token_p)
479                         memcpy(&token_hdr, token_p, sizeof(token_hdr));
480
481                 /* one choice for this token */
482                 if (!token_p ||
483                     !token_hdr.ops->complete_get_nb ||
484                     !token_hdr.ops->complete_get_elt ||
485                     (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
486                         if (local_state < *state) {
487                                 local_state++;
488                                 goto next2;
489                         }
490                         (*state)++;
491                         if (token_p && token_hdr.ops->get_help) {
492                                 token_hdr.ops->get_help(token_p, tmpbuf,
493                                                         sizeof(tmpbuf));
494                                 help_str = inst->help_str;
495                                 if (help_str)
496                                         rte_snprintf(dst, size, "[%s]: %s", tmpbuf,
497                                                  help_str);
498                                 else
499                                         rte_snprintf(dst, size, "[%s]: No help",
500                                                  tmpbuf);
501                         }
502                         else {
503                                 rte_snprintf(dst, size, "[RETURN]");
504                         }
505                         return 1;
506                 }
507
508                 /* several choices */
509                 for (i=0 ; i<n ; i++) {
510                         if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
511                                                             sizeof(tmpbuf)) < 0)
512                                 continue;
513                         /* we have at least room for one char */
514                         tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
515                         if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
516                                 tmpbuf[tmp_len] = ' ';
517                                 tmpbuf[tmp_len + 1] = 0;
518                         }
519
520                         debug_printf("   choice <%s>\n", tmpbuf);
521
522                         /* does the completion match the beginning of
523                          * the word ? */
524                         if (!strncmp(partial_tok, tmpbuf,
525                                      partial_tok_len)) {
526                                 if (local_state < *state) {
527                                         local_state++;
528                                         continue;
529                                 }
530                                 (*state)++;
531                                 l=rte_snprintf(dst, size, "%s", tmpbuf);
532                                 if (l>=0 && token_hdr.ops->get_help) {
533                                         token_hdr.ops->get_help(token_p, tmpbuf,
534                                                                 sizeof(tmpbuf));
535                                         help_str = inst->help_str;
536                                         if (help_str)
537                                                 rte_snprintf(dst+l, size-l, "[%s]: %s",
538                                                          tmpbuf, help_str);
539                                         else
540                                                 rte_snprintf(dst+l, size-l,
541                                                          "[%s]: No help", tmpbuf);
542                                 }
543
544                                 return 1;
545                         }
546                 }
547         next2:
548                 inst_num ++;
549                 inst = ctx[inst_num];
550         }
551         return 0;
552 }
553