cmdline (merge-intel): cosmetic fixes, conform to coding rules
[libcmdline.git] / src / lib / cmdline_parse.c
index d1b01f7..215077e 100644 (file)
@@ -1,3 +1,37 @@
+/*-
+ * Copyright (c) <2010>, Intel Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in
+ *   the documentation and/or other materials provided with the
+ *   distribution.
+ *
+ * - Neither the name of Intel Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived
+ *   from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 /*
  * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
  * All rights reserved.
@@ -29,6 +63,7 @@
 #include <string.h>
 #include <inttypes.h>
 #include <ctype.h>
+#include <termios.h>
 
 #include <netinet/in.h>
 
@@ -94,8 +129,8 @@ nb_common_chars(const char * s1, const char * s2)
  * tokens, else the number of matched tokens, else -1.
  */
 static int
-match_inst(cmdline_parse_inst_t *inst, const char *buf, unsigned int nb_match_token,
-          void * result_buf)
+match_inst(cmdline_parse_inst_t *inst, const char *buf,
+          unsigned int nb_match_token, void * result_buf)
 {
        unsigned int token_num=0;
        cmdline_parse_token_hdr_t * token_p;
@@ -119,9 +154,16 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf, unsigned int nb_match_to
                if ( isendofline(*buf) || iscomment(*buf) )
                        break;
 
-               n = token_hdr.ops->parse(token_p, buf, (result_buf ? result_buf+token_hdr.offset : NULL));
-               if ( n < 0 )
+               if (result_buf)
+                       n = token_hdr.ops->parse(token_p, buf,
+                                                (char *)result_buf +
+                                                token_hdr.offset);
+               else
+                       n = token_hdr.ops->parse(token_p, buf, NULL);
+
+               if (n < 0)
                        break;
+
                debug_printf("TK parsed (len=%d)\n", n);
                i++;
                buf += n;
@@ -169,7 +211,7 @@ cmdline_parse(struct cmdline *cl, const char * buf)
        unsigned int inst_num=0;
        cmdline_parse_inst_t *inst;
        const char *curbuf;
-       char result_buf[BUFSIZ]; /* XXX align, size zé in broblém */
+       char result_buf[BUFSIZ];
        void (*f)(void *, struct cmdline *, void *) = NULL;
        void *data = NULL;
        int comment = 0;
@@ -275,14 +317,14 @@ int
 cmdline_complete(struct cmdline *cl, const char *buf, int *state,
                 char *dst, unsigned int size)
 {
-       const char *incomplete_token = buf;
+       const char *partial_tok = buf;
        unsigned int inst_num = 0;
        cmdline_parse_inst_t *inst;
        cmdline_parse_token_hdr_t *token_p;
        struct cmdline_token_hdr token_hdr;
-       char tmpbuf[64], completion_buf[64];
-       unsigned int incomplete_token_len;
-       int completion_len = -1;
+       char tmpbuf[64], comp_buf[64];
+       unsigned int partial_tok_len;
+       int comp_len = -1;
        int nb_token = 0;
        unsigned int i, n;
        int l;
@@ -300,14 +342,15 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,
                if (!isblank2(buf[i]) && isblank2(buf[i+1]))
                        nb_token++;
                if (isblank2(buf[i]) && !isblank2(buf[i+1]))
-                       incomplete_token = buf+i+1;
+                       partial_tok = buf+i+1;
        }
-       incomplete_token_len = strlen(incomplete_token);
+       partial_tok_len = strlen(partial_tok);
 
        /* first call -> do a first pass */
        if (*state <= 0) {
                debug_printf("try complete <%s>\n", buf);
-               debug_printf("there is %d complete tokens, <%s> is incomplete\n", nb_token, incomplete_token);
+               debug_printf("there is %d complete tokens, <%s> is incomplete\n",
+                            nb_token, partial_tok);
 
                nb_completable = 0;
                nb_non_completable = 0;
@@ -334,21 +377,33 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,
 
                        debug_printf("%d choices for this token\n", n);
                        for (i=0 ; i<n ; i++) {
-                               if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
+                               if (token_hdr.ops->complete_get_elt(token_p, i,
+                                                                   tmpbuf,
+                                                                   sizeof(tmpbuf)) < 0)
                                        continue;
-                               strcat(tmpbuf, " "); /* we have at least room for one char */
+
+                               /* we have at least room for one char */
+                               strcat(tmpbuf, " ");
+
                                debug_printf("   choice <%s>\n", tmpbuf);
-                               /* does the completion match the beginning of the word ? */
-                               if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
-                                       if (completion_len == -1) {
-                                               strcpy(completion_buf, tmpbuf+incomplete_token_len);
-                                               completion_len = strlen(tmpbuf+incomplete_token_len);
+
+                               /* does the completion match the
+                                * beginning of the word ? */
+                               if (!strncmp(partial_tok, tmpbuf,
+                                            partial_tok_len)) {
+                                       if (comp_len == -1) {
+                                               strcpy(comp_buf,
+                                                      tmpbuf + partial_tok_len);
+                                               comp_len =
+                                                       strlen(tmpbuf +
+                                                              partial_tok_len);
 
                                        }
                                        else {
-                                               completion_len = nb_common_chars(completion_buf,
-                                                                                tmpbuf+incomplete_token_len);
-                                               completion_buf[completion_len] = 0;
+                                               comp_len =
+                                                       nb_common_chars(comp_buf,
+                                                                       tmpbuf+partial_tok_len);
+                                               comp_buf[comp_len] = 0;
                                        }
                                        nb_completable++;
                                }
@@ -358,21 +413,22 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,
                        inst = ctx[inst_num];
                }
 
-               debug_printf("total choices %d for this completion\n", nb_completable);
+               debug_printf("total choices %d for this completion\n",
+                            nb_completable);
 
                /* no possible completion */
                if (nb_completable == 0 && nb_non_completable == 0)
                        return 0;
 
                /* if multichoice is not required */
-               if (*state == 0 && incomplete_token_len > 0) {
+               if (*state == 0 && partial_tok_len > 0) {
                        /* one or several choices starting with the
                           same chars */
-                       if (completion_len > 0) {
-                               if ((unsigned)(completion_len + 1) > size)
+                       if (comp_len > 0) {
+                               if ((unsigned)(comp_len + 1) > size)
                                        return 0;
 
-                               strcpy(dst, completion_buf);
+                               strcpy(dst, comp_buf);
                                return 2;
                        }
                }
@@ -408,12 +464,15 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,
                        }
                        (*state)++;
                        if (token_p && token_hdr.ops->get_help) {
-                               token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
+                               token_hdr.ops->get_help(token_p, tmpbuf,
+                                                       sizeof(tmpbuf));
                                help_str = inst->help_str;
                                if (help_str)
-                                       snprintf(dst, size, "[%s]: %s", tmpbuf, help_str);
+                                       snprintf(dst, size, "[%s]: %s", tmpbuf,
+                                                help_str);
                                else
-                                       snprintf(dst, size, "[%s]: No help", tmpbuf);
+                                       snprintf(dst, size, "[%s]: No help",
+                                                tmpbuf);
                        }
                        else {
                                snprintf(dst, size, "[RETURN]");
@@ -423,12 +482,18 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,
 
                /* several choices */
                for (i=0 ; i<n ; i++) {
-                       if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf, sizeof(tmpbuf)) < 0)
+                       if (token_hdr.ops->complete_get_elt(token_p, i, tmpbuf,
+                                                           sizeof(tmpbuf)) < 0)
                                continue;
-                       strcat(tmpbuf, " "); /* we have at least room for one char */
+                       /* we have at least room for one char */
+                       strcat(tmpbuf, " ");
+
                        debug_printf("   choice <%s>\n", tmpbuf);
-                       /* does the completion match the beginning of the word ? */
-                       if (!strncmp(incomplete_token, tmpbuf, incomplete_token_len)) {
+
+                       /* does the completion match the beginning of
+                        * the word ? */
+                       if (!strncmp(partial_tok, tmpbuf,
+                                    partial_tok_len)) {
                                if (local_state < *state) {
                                        local_state++;
                                        continue;
@@ -436,12 +501,15 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,
                                (*state)++;
                                l=snprintf(dst, size, "%s", tmpbuf);
                                if (l>=0 && token_hdr.ops->get_help) {
-                                       token_hdr.ops->get_help(token_p, tmpbuf, sizeof(tmpbuf));
+                                       token_hdr.ops->get_help(token_p, tmpbuf,
+                                                               sizeof(tmpbuf));
                                        help_str = inst->help_str;
                                        if (help_str)
-                                               snprintf(dst+l, size-l, "[%s]: %s", tmpbuf, help_str);
+                                               snprintf(dst+l, size-l, "[%s]: %s",
+                                                        tmpbuf, help_str);
                                        else
-                                               snprintf(dst+l, size-l, "[%s]: No help", tmpbuf);
+                                               snprintf(dst+l, size-l,
+                                                        "[%s]: No help", tmpbuf);
                                }
 
                                return 1;