2 * Copyright (c) <2010>, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
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.
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
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 * OF THE POSSIBILITY OF SUCH DAMAGE.
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:
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.
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.
68 #include <netinet/in.h>
70 #include "cmdline_parse.h"
73 //#define CMDLINE_DEBUG
74 //#define debug_printf printf
75 #define debug_printf(args...) do {} while(0)
77 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
106 cmdline_isendoftoken(char c)
108 if (!c || iscomment(c) || isblank2(c) || isendofline(c))
114 nb_common_chars(const char * s1, const char * s2)
118 while (*s1==*s2 && *s1 && *s2) {
127 * try to match the buffer with an instruction (only the first
128 * nb_match_token tokens if != 0). Return 0 if we match all the
129 * tokens, else the number of matched tokens, else -1.
132 match_inst(cmdline_parse_inst_t *inst, const char *buf, unsigned int nb_match_token,
135 unsigned int token_num=0;
136 cmdline_parse_token_hdr_t * token_p;
139 struct cmdline_token_hdr token_hdr;
141 token_p = inst->tokens[token_num];
143 memcpy(&token_hdr, token_p, sizeof(token_hdr));
145 /* check if we match all tokens of inst */
146 while (token_p && (!nb_match_token || i<nb_match_token)) {
147 debug_printf("TK\n");
149 while (isblank2(*buf)) {
154 if ( isendofline(*buf) || iscomment(*buf) )
158 n = token_hdr.ops->parse(token_p, buf,
162 n = token_hdr.ops->parse(token_p, buf, NULL);
167 debug_printf("TK parsed (len=%d)\n", n);
172 token_p = inst->tokens[token_num];
174 memcpy(&token_hdr, token_p, sizeof(token_hdr));
181 /* in case we want to match a specific num of token */
182 if (nb_match_token) {
183 if (i == nb_match_token) {
189 /* we don't match all the tokens */
194 /* are there are some tokens more */
195 while (isblank2(*buf)) {
200 if ( isendofline(*buf) || iscomment(*buf) )
203 /* garbage after inst */
209 cmdline_parse(struct cmdline *cl, const char * buf)
211 unsigned int inst_num=0;
212 cmdline_parse_inst_t *inst;
214 char result_buf[BUFSIZ]; /* XXX align, size zé in broblém */
215 void (*f)(void *, struct cmdline *, void *) = NULL;
220 int err = CMDLINE_PARSE_NOMATCH;
222 cmdline_parse_ctx_t *ctx = cl->ctx;
224 char debug_buf[BUFSIZ];
228 * - look if the buffer contains at least one line
229 * - look if line contains only spaces or comments
230 * - count line length
233 while (! isendofline(*curbuf)) {
234 if ( *curbuf == '\0' ) {
235 debug_printf("Incomplete buf (len=%d)\n", linelen);
238 if ( iscomment(*curbuf) ) {
241 if ( ! isblank2(*curbuf) && ! comment) {
248 /* skip all endofline chars */
249 while (isendofline(buf[linelen])) {
254 if ( parse_it == 0 ) {
255 debug_printf("Empty line (len=%d)\n", linelen);
260 snprintf(debug_buf, (linelen>64 ? 64 : linelen), "%s", buf);
261 debug_printf("Parse line : len=%d, <%s>\n", linelen, debug_buf);
265 inst = ctx[inst_num];
267 debug_printf("INST %d\n", inst_num);
270 tok = match_inst(inst, buf, 0, result_buf);
272 if (tok > 0) /* we matched at least one token */
273 err = CMDLINE_PARSE_BAD_ARGS;
276 debug_printf("INST fully parsed\n");
278 while (isblank2(*curbuf)) {
282 /* if end of buf -> there is no garbage after inst */
283 if (isendofline(*curbuf) || iscomment(*curbuf)) {
285 memcpy(&f, &inst->f, sizeof(f));
286 memcpy(&data, &inst->data, sizeof(data));
289 /* more than 1 inst matches */
290 err = CMDLINE_PARSE_AMBIGUOUS;
292 debug_printf("Ambiguous cmd\n");
299 inst = ctx[inst_num];
304 f(result_buf, cl, data);
309 debug_printf("No match err=%d\n", err);
317 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
318 char *dst, unsigned int size)
320 const char *incomplete_token = buf;
321 unsigned int inst_num = 0;
322 cmdline_parse_inst_t *inst;
323 cmdline_parse_token_hdr_t *token_p;
324 struct cmdline_token_hdr token_hdr;
325 char tmpbuf[64], completion_buf[64];
326 unsigned int incomplete_token_len;
327 int completion_len = -1;
331 unsigned int nb_completable;
332 unsigned int nb_non_completable;
335 cmdline_parse_ctx_t *ctx = cl->ctx;
337 debug_printf("%s called\n", __FUNCTION__);
338 memset(&token_hdr, 0, sizeof(token_hdr));
340 /* count the number of complete token to parse */
341 for (i=0 ; buf[i] ; i++) {
342 if (!isblank2(buf[i]) && isblank2(buf[i+1]))
344 if (isblank2(buf[i]) && !isblank2(buf[i+1]))
345 incomplete_token = buf+i+1;
347 incomplete_token_len = strlen(incomplete_token);
349 /* first call -> do a first pass */
351 debug_printf("try complete <%s>\n", buf);
352 debug_printf("there is %d complete tokens, <%s> is incomplete\n", nb_token, incomplete_token);
355 nb_non_completable = 0;
357 inst = ctx[inst_num];
359 /* parse the first tokens of the inst */
360 if (nb_token && match_inst(inst, buf, nb_token, NULL))
363 debug_printf("instruction match \n");
364 token_p = inst->tokens[nb_token];
366 memcpy(&token_hdr, token_p, sizeof(token_hdr));
368 /* non completable */
370 !token_hdr.ops->complete_get_nb ||
371 !token_hdr.ops->complete_get_elt ||
372 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
373 nb_non_completable++;
377 debug_printf("%d choices for this token\n", n);
378 for (i=0 ; i<n ; i++) {
379 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
381 strcat(tmpbuf, " "); /* we have at least room for one char */
382 debug_printf(" choice <%s>\n", tmpbuf);
383 /* does the completion match the beginning of the word ? */
384 if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
385 if (completion_len == -1) {
386 strcpy(completion_buf, tmpbuf+incomplete_token_len);
387 completion_len = strlen(tmpbuf+incomplete_token_len);
391 completion_len = nb_common_chars(completion_buf,
392 tmpbuf+incomplete_token_len);
393 completion_buf[completion_len] = 0;
400 inst = ctx[inst_num];
403 debug_printf("total choices %d for this completion\n", nb_completable);
405 /* no possible completion */
406 if (nb_completable == 0 && nb_non_completable == 0)
409 /* if multichoice is not required */
410 if (*state == 0 && incomplete_token_len > 0) {
411 /* one or several choices starting with the
413 if (completion_len > 0) {
414 if ((unsigned)(completion_len + 1) > size)
417 strcpy(dst, completion_buf);
423 /* init state correctly */
427 debug_printf("Multiple choice STATE=%d\n", *state);
430 inst = ctx[inst_num];
432 /* we need to redo it */
433 inst = ctx[inst_num];
435 if (nb_token && match_inst(inst, buf, nb_token, NULL))
438 token_p = inst->tokens[nb_token];
440 memcpy(&token_hdr, token_p, sizeof(token_hdr));
442 /* one choice for this token */
444 !token_hdr.ops->complete_get_nb ||
445 !token_hdr.ops->complete_get_elt ||
446 (n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
447 if (local_state < *state) {
452 if (token_p && token_hdr.ops->get_help) {
453 token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
454 help_str = inst->help_str;
456 snprintf(dst, size, "[%s]: %s", tmpbuf, help_str);
458 snprintf(dst, size, "[%s]: No help", tmpbuf);
461 snprintf(dst, size, "[RETURN]");
466 /* several choices */
467 for (i=0 ; i<n ; i++) {
468 if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
470 strcat(tmpbuf, " "); /* we have at least room for one char */
471 debug_printf(" choice <%s>\n", tmpbuf);
472 /* does the completion match the beginning of the word ? */
473 if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
474 if (local_state < *state) {
479 l=snprintf(dst, size, "%s", tmpbuf);
480 if (l>=0 && token_hdr.ops->get_help) {
481 token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
482 help_str = inst->help_str;
484 snprintf(dst+l, size-l, "[%s]: %s", tmpbuf, help_str);
486 snprintf(dst+l, size-l, "[%s]: No help", tmpbuf);
494 inst = ctx[inst_num];