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