]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/dpdk/lib/librte_cmdline/cmdline_parse.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / dpdk / 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 int
122 cmdline_isendofcommand(char c)
123 {
124 if (!c || iscomment(c) || isendofline(c))
125 return 1;
126 return 0;
127 }
128
129 static unsigned int
130 nb_common_chars(const char * s1, const char * s2)
131 {
132 unsigned int i=0;
133
134 while (*s1==*s2 && *s1) {
135 s1++;
136 s2++;
137 i++;
138 }
139 return i;
140 }
141
142 /**
143 * try to match the buffer with an instruction (only the first
144 * nb_match_token tokens if != 0). Return 0 if we match all the
145 * tokens, else the number of matched tokens, else -1.
146 */
147 static int
148 match_inst(cmdline_parse_inst_t *inst, const char *buf,
149 unsigned int nb_match_token, void *resbuf, unsigned resbuf_size,
150 cmdline_parse_token_hdr_t
151 *(*dyn_tokens)[CMDLINE_PARSE_DYNAMIC_TOKENS])
152 {
153 unsigned int token_num=0;
154 cmdline_parse_token_hdr_t * token_p;
155 unsigned int i=0;
156 int n = 0;
157 struct cmdline_token_hdr token_hdr;
158
159 token_p = inst->tokens[token_num];
160 if (!token_p && dyn_tokens && inst->f) {
161 if (!(*dyn_tokens)[0])
162 inst->f(&(*dyn_tokens)[0], NULL, dyn_tokens);
163 token_p = (*dyn_tokens)[0];
164 }
165 if (token_p)
166 memcpy(&token_hdr, token_p, sizeof(token_hdr));
167
168 /* check if we match all tokens of inst */
169 while (token_p && (!nb_match_token || i<nb_match_token)) {
170 debug_printf("TK\n");
171 /* skip spaces */
172 while (isblank2(*buf)) {
173 buf++;
174 }
175
176 /* end of buf */
177 if ( isendofline(*buf) || iscomment(*buf) )
178 break;
179
180 if (resbuf == NULL) {
181 n = token_hdr.ops->parse(token_p, buf, NULL, 0);
182 } else {
183 unsigned rb_sz;
184
185 if (token_hdr.offset > resbuf_size) {
186 printf("Parse error(%s:%d): Token offset(%u) "
187 "exceeds maximum size(%u)\n",
188 __FILE__, __LINE__,
189 token_hdr.offset, resbuf_size);
190 return -ENOBUFS;
191 }
192 rb_sz = resbuf_size - token_hdr.offset;
193
194 n = token_hdr.ops->parse(token_p, buf, (char *)resbuf +
195 token_hdr.offset, rb_sz);
196 }
197
198 if (n < 0)
199 break;
200
201 debug_printf("TK parsed (len=%d)\n", n);
202 i++;
203 buf += n;
204
205 token_num ++;
206 if (!inst->tokens[0]) {
207 if (token_num < (CMDLINE_PARSE_DYNAMIC_TOKENS - 1)) {
208 if (!(*dyn_tokens)[token_num])
209 inst->f(&(*dyn_tokens)[token_num],
210 NULL,
211 dyn_tokens);
212 token_p = (*dyn_tokens)[token_num];
213 } else
214 token_p = NULL;
215 } else
216 token_p = inst->tokens[token_num];
217 if (token_p)
218 memcpy(&token_hdr, token_p, sizeof(token_hdr));
219 }
220
221 /* does not match */
222 if (i==0)
223 return -1;
224
225 /* in case we want to match a specific num of token */
226 if (nb_match_token) {
227 if (i == nb_match_token) {
228 return 0;
229 }
230 return i;
231 }
232
233 /* we don't match all the tokens */
234 if (token_p) {
235 return i;
236 }
237
238 /* are there are some tokens more */
239 while (isblank2(*buf)) {
240 buf++;
241 }
242
243 /* end of buf */
244 if ( isendofline(*buf) || iscomment(*buf) )
245 return 0;
246
247 /* garbage after inst */
248 return i;
249 }
250
251
252 int
253 cmdline_parse(struct cmdline *cl, const char * buf)
254 {
255 unsigned int inst_num=0;
256 cmdline_parse_inst_t *inst;
257 const char *curbuf;
258 union {
259 char buf[CMDLINE_PARSE_RESULT_BUFSIZE];
260 long double align; /* strong alignment constraint for buf */
261 } result, tmp_result;
262 cmdline_parse_token_hdr_t *dyn_tokens[CMDLINE_PARSE_DYNAMIC_TOKENS];
263 void (*f)(void *, struct cmdline *, void *) = NULL;
264 void *data = NULL;
265 int comment = 0;
266 int linelen = 0;
267 int parse_it = 0;
268 int err = CMDLINE_PARSE_NOMATCH;
269 int tok;
270 cmdline_parse_ctx_t *ctx;
271 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
272 char debug_buf[BUFSIZ];
273 #endif
274
275 if (!cl || !buf)
276 return CMDLINE_PARSE_BAD_ARGS;
277
278 ctx = cl->ctx;
279 memset(&dyn_tokens, 0, sizeof(dyn_tokens));
280
281 /*
282 * - look if the buffer contains at least one line
283 * - look if line contains only spaces or comments
284 * - count line length
285 */
286 curbuf = buf;
287 while (! isendofline(*curbuf)) {
288 if ( *curbuf == '\0' ) {
289 debug_printf("Incomplete buf (len=%d)\n", linelen);
290 return 0;
291 }
292 if ( iscomment(*curbuf) ) {
293 comment = 1;
294 }
295 if ( ! isblank2(*curbuf) && ! comment) {
296 parse_it = 1;
297 }
298 curbuf++;
299 linelen++;
300 }
301
302 /* skip all endofline chars */
303 while (isendofline(buf[linelen])) {
304 linelen++;
305 }
306
307 /* empty line */
308 if ( parse_it == 0 ) {
309 debug_printf("Empty line (len=%d)\n", linelen);
310 return linelen;
311 }
312
313 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
314 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
315 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
316 #endif
317
318 /* parse it !! */
319 inst = ctx[inst_num];
320 while (inst) {
321 debug_printf("INST %d\n", inst_num);
322
323 /* fully parsed */
324 tok = match_inst(inst, buf, 0, tmp_result.buf,
325 sizeof(tmp_result.buf), &dyn_tokens);
326
327 if (tok > 0) /* we matched at least one token */
328 err = CMDLINE_PARSE_BAD_ARGS;
329
330 else if (!tok) {
331 debug_printf("INST fully parsed\n");
332 memcpy(&result, &tmp_result,
333 sizeof(result));
334 /* skip spaces */
335 while (isblank2(*curbuf)) {
336 curbuf++;
337 }
338
339 /* if end of buf -> there is no garbage after inst */
340 if (isendofline(*curbuf) || iscomment(*curbuf)) {
341 if (!f) {
342 memcpy(&f, &inst->f, sizeof(f));
343 memcpy(&data, &inst->data, sizeof(data));
344 }
345 else {
346 /* more than 1 inst matches */
347 err = CMDLINE_PARSE_AMBIGUOUS;
348 f=NULL;
349 debug_printf("Ambiguous cmd\n");
350 break;
351 }
352 }
353 }
354
355 inst_num ++;
356 inst = ctx[inst_num];
357 }
358
359 /* call func */
360 if (f) {
361 f(result.buf, cl, data);
362 }
363
364 /* no match */
365 else {
366 debug_printf("No match err=%d\n", err);
367 return err;
368 }
369
370 return linelen;
371 }
372
373 int
374 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
375 char *dst, unsigned int size)
376 {
377 const char *partial_tok = buf;
378 unsigned int inst_num = 0;
379 cmdline_parse_inst_t *inst;
380 cmdline_parse_token_hdr_t *token_p;
381 struct cmdline_token_hdr token_hdr;
382 char tmpbuf[CMDLINE_BUFFER_SIZE], comp_buf[CMDLINE_BUFFER_SIZE];
383 cmdline_parse_token_hdr_t *dyn_tokens[CMDLINE_PARSE_DYNAMIC_TOKENS];
384 unsigned int partial_tok_len;
385 int comp_len = -1;
386 int tmp_len = -1;
387 int nb_token = 0;
388 unsigned int i, n;
389 int l;
390 unsigned int nb_completable;
391 unsigned int nb_non_completable;
392 int local_state = 0;
393 const char *help_str;
394 cmdline_parse_ctx_t *ctx;
395
396 if (!cl || !buf || !state || !dst)
397 return -1;
398
399 ctx = cl->ctx;
400
401 debug_printf("%s called\n", __func__);
402 memset(&token_hdr, 0, sizeof(token_hdr));
403 memset(&dyn_tokens, 0, sizeof(dyn_tokens));
404
405 /* count the number of complete token to parse */
406 for (i=0 ; buf[i] ; i++) {
407 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
408 nb_token++;
409 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
410 partial_tok = buf+i+1;
411 }
412 partial_tok_len = strnlen(partial_tok, RDLINE_BUF_SIZE);
413
414 /* first call -> do a first pass */
415 if (*state <= 0) {
416 debug_printf("try complete <%s>\n", buf);
417 debug_printf("there is %d complete tokens, <%s> is incomplete\n",
418 nb_token, partial_tok);
419
420 nb_completable = 0;
421 nb_non_completable = 0;
422
423 inst = ctx[inst_num];
424 while (inst) {
425 /* parse the first tokens of the inst */
426 if (nb_token &&
427 match_inst(inst, buf, nb_token, NULL, 0,
428 &dyn_tokens))
429 goto next;
430
431 debug_printf("instruction match\n");
432 if (!inst->tokens[0]) {
433 if (nb_token <
434 (CMDLINE_PARSE_DYNAMIC_TOKENS - 1)) {
435 if (!dyn_tokens[nb_token])
436 inst->f(&dyn_tokens[nb_token],
437 NULL,
438 &dyn_tokens);
439 token_p = dyn_tokens[nb_token];
440 } else
441 token_p = NULL;
442 } else
443 token_p = inst->tokens[nb_token];
444 if (token_p)
445 memcpy(&token_hdr, token_p, sizeof(token_hdr));
446
447 /* non completable */
448 if (!token_p ||
449 !token_hdr.ops->complete_get_nb ||
450 !token_hdr.ops->complete_get_elt ||
451 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
452 nb_non_completable++;
453 goto next;
454 }
455
456 debug_printf("%d choices for this token\n", n);
457 for (i=0 ; i<n ; i++) {
458 if (token_hdr.ops->complete_get_elt(token_p, i,
459 tmpbuf,
460 sizeof(tmpbuf)) < 0)
461 continue;
462
463 /* we have at least room for one char */
464 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
465 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
466 tmpbuf[tmp_len] = ' ';
467 tmpbuf[tmp_len+1] = 0;
468 }
469
470 debug_printf(" choice <%s>\n", tmpbuf);
471
472 /* does the completion match the
473 * beginning of the word ? */
474 if (!strncmp(partial_tok, tmpbuf,
475 partial_tok_len)) {
476 if (comp_len == -1) {
477 snprintf(comp_buf, sizeof(comp_buf),
478 "%s", tmpbuf + partial_tok_len);
479 comp_len =
480 strnlen(tmpbuf + partial_tok_len,
481 sizeof(tmpbuf) - partial_tok_len);
482
483 }
484 else {
485 comp_len =
486 nb_common_chars(comp_buf,
487 tmpbuf+partial_tok_len);
488 comp_buf[comp_len] = 0;
489 }
490 nb_completable++;
491 }
492 }
493 next:
494 debug_printf("next\n");
495 inst_num ++;
496 inst = ctx[inst_num];
497 }
498
499 debug_printf("total choices %d for this completion\n",
500 nb_completable);
501
502 /* no possible completion */
503 if (nb_completable == 0 && nb_non_completable == 0)
504 return 0;
505
506 /* if multichoice is not required */
507 if (*state == 0 && partial_tok_len > 0) {
508 /* one or several choices starting with the
509 same chars */
510 if (comp_len > 0) {
511 if ((unsigned)(comp_len + 1) > size)
512 return 0;
513
514 snprintf(dst, size, "%s", comp_buf);
515 dst[comp_len] = 0;
516 return 2;
517 }
518 }
519 }
520
521 /* init state correctly */
522 if (*state == -1)
523 *state = 0;
524
525 debug_printf("Multiple choice STATE=%d\n", *state);
526
527 inst_num = 0;
528 inst = ctx[inst_num];
529 while (inst) {
530 /* we need to redo it */
531 inst = ctx[inst_num];
532
533 if (nb_token &&
534 match_inst(inst, buf, nb_token, NULL, 0, &dyn_tokens))
535 goto next2;
536
537 if (!inst->tokens[0]) {
538 if (nb_token < (CMDLINE_PARSE_DYNAMIC_TOKENS - 1)) {
539 if (!dyn_tokens[nb_token])
540 inst->f(&dyn_tokens[nb_token],
541 NULL,
542 &dyn_tokens);
543 token_p = dyn_tokens[nb_token];
544 } else
545 token_p = NULL;
546 } else
547 token_p = inst->tokens[nb_token];
548 if (token_p)
549 memcpy(&token_hdr, token_p, sizeof(token_hdr));
550
551 /* one choice for this token */
552 if (!token_p ||
553 !token_hdr.ops->complete_get_nb ||
554 !token_hdr.ops->complete_get_elt ||
555 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
556 if (local_state < *state) {
557 local_state++;
558 goto next2;
559 }
560 (*state)++;
561 if (token_p && token_hdr.ops->get_help) {
562 token_hdr.ops->get_help(token_p, tmpbuf,
563 sizeof(tmpbuf));
564 help_str = inst->help_str;
565 if (help_str)
566 snprintf(dst, size, "[%s]: %s", tmpbuf,
567 help_str);
568 else
569 snprintf(dst, size, "[%s]: No help",
570 tmpbuf);
571 }
572 else {
573 snprintf(dst, size, "[RETURN]");
574 }
575 return 1;
576 }
577
578 /* several choices */
579 for (i=0 ; i<n ; i++) {
580 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
581 sizeof(tmpbuf)) < 0)
582 continue;
583 /* we have at least room for one char */
584 tmp_len = strnlen(tmpbuf, sizeof(tmpbuf));
585 if (tmp_len < CMDLINE_BUFFER_SIZE - 1) {
586 tmpbuf[tmp_len] = ' ';
587 tmpbuf[tmp_len + 1] = 0;
588 }
589
590 debug_printf(" choice <%s>\n", tmpbuf);
591
592 /* does the completion match the beginning of
593 * the word ? */
594 if (!strncmp(partial_tok, tmpbuf,
595 partial_tok_len)) {
596 if (local_state < *state) {
597 local_state++;
598 continue;
599 }
600 (*state)++;
601 l=snprintf(dst, size, "%s", tmpbuf);
602 if (l>=0 && token_hdr.ops->get_help) {
603 token_hdr.ops->get_help(token_p, tmpbuf,
604 sizeof(tmpbuf));
605 help_str = inst->help_str;
606 if (help_str)
607 snprintf(dst+l, size-l, "[%s]: %s",
608 tmpbuf, help_str);
609 else
610 snprintf(dst+l, size-l,
611 "[%s]: No help", tmpbuf);
612 }
613
614 return 1;
615 }
616 }
617 next2:
618 inst_num ++;
619 inst = ctx[inst_num];
620 }
621 return 0;
622 }