cmdline: various updates
authorIntel <intel.com>
Wed, 19 Dec 2012 23:00:00 +0000 (00:00 +0100)
committerThomas Monjalon <thomas.monjalon@6wind.com>
Thu, 25 Jul 2013 13:01:57 +0000 (15:01 +0200)
- Support of float number is removed.
- C++ compatibility is done
- More checks

Signed-off-by: Intel
22 files changed:
config/defconfig_i686-default-linuxapp-gcc
config/defconfig_i686-default-linuxapp-icc
config/defconfig_x86_64-default-linuxapp-gcc
config/defconfig_x86_64-default-linuxapp-icc
lib/librte_cmdline/cmdline.c
lib/librte_cmdline/cmdline_cirbuf.c
lib/librte_cmdline/cmdline_cirbuf.h
lib/librte_cmdline/cmdline_parse.c
lib/librte_cmdline/cmdline_parse_etheraddr.c
lib/librte_cmdline/cmdline_parse_etheraddr.h
lib/librte_cmdline/cmdline_parse_ipaddr.c
lib/librte_cmdline/cmdline_parse_ipaddr.h
lib/librte_cmdline/cmdline_parse_num.c
lib/librte_cmdline/cmdline_parse_num.h
lib/librte_cmdline/cmdline_parse_portlist.c
lib/librte_cmdline/cmdline_parse_portlist.h
lib/librte_cmdline/cmdline_parse_string.c
lib/librte_cmdline/cmdline_parse_string.h
lib/librte_cmdline/cmdline_rdline.c
lib/librte_cmdline/cmdline_rdline.h
lib/librte_cmdline/cmdline_socket.c
lib/librte_cmdline/cmdline_vt100.c

index bfa4b09..9c534ed 100644 (file)
@@ -192,6 +192,7 @@ CONFIG_RTE_MALLOC_PER_NUMA_NODE=y
 # Compile librte_cmdline
 #
 CONFIG_RTE_LIBRTE_CMDLINE=y
+CONFIG_RTE_LIBRTE_CMDLINE_DEBUG=n
 
 #
 # Compile librte_hash
index 96ea6a4..efac8f6 100644 (file)
@@ -192,6 +192,7 @@ CONFIG_RTE_MALLOC_PER_NUMA_NODE=y
 # Compile librte_cmdline
 #
 CONFIG_RTE_LIBRTE_CMDLINE=y
+CONFIG_RTE_LIBRTE_CMDLINE_DEBUG=n
 
 #
 # Compile librte_hash
index b2e6872..8989a9d 100644 (file)
@@ -192,6 +192,7 @@ CONFIG_RTE_MALLOC_PER_NUMA_NODE=y
 # Compile librte_cmdline
 #
 CONFIG_RTE_LIBRTE_CMDLINE=y
+CONFIG_RTE_LIBRTE_CMDLINE_DEBUG=n
 
 #
 # Compile librte_hash
index d05b31e..8dc32aa 100644 (file)
@@ -192,6 +192,7 @@ CONFIG_RTE_MALLOC_PER_NUMA_NODE=y
 # Compile librte_cmdline
 #
 CONFIG_RTE_LIBRTE_CMDLINE=y
+CONFIG_RTE_LIBRTE_CMDLINE_DEBUG=n
 
 #
 # Compile librte_hash
index 6a0facf..c8958d5 100644 (file)
@@ -104,7 +104,12 @@ int
 cmdline_write_char(struct rdline *rdl, char c)
 {
        int ret = -1;
-       struct cmdline *cl = rdl->opaque;
+       struct cmdline *cl;
+
+       if (!rdl)
+               return -1;
+
+       cl = rdl->opaque;
 
        if (cl->s_out >= 0)
                ret = write(cl->s_out, &c, 1);
@@ -116,6 +121,8 @@ cmdline_write_char(struct rdline *rdl, char c)
 void
 cmdline_set_prompt(struct cmdline *cl, const char *prompt)
 {
+       if (!cl || !prompt)
+               return;
        rte_snprintf(cl->prompt, sizeof(cl->prompt), "%s", prompt);
 }
 
@@ -123,6 +130,10 @@ struct cmdline *
 cmdline_new(cmdline_parse_ctx_t *ctx, const char *prompt, int s_in, int s_out)
 {
        struct cmdline *cl;
+
+       if (!ctx || !prompt)
+               return NULL;
+
        cl = malloc(sizeof(struct cmdline));
        if (cl == NULL)
                return NULL;
@@ -144,6 +155,10 @@ void
 cmdline_free(struct cmdline *cl)
 {
        dprintf("called\n");
+
+       if (!cl)
+               return;
+
        if (cl->s_in > 2)
                close(cl->s_in);
        if (cl->s_out != cl->s_in && cl->s_out > 2)
@@ -156,6 +171,9 @@ cmdline_printf(const struct cmdline *cl, const char *fmt, ...)
 {
        va_list ap;
 
+       if (!cl || !fmt)
+               return;
+
 #ifdef _GNU_SOURCE
        if (cl->s_out < 0)
                return;
@@ -192,6 +210,9 @@ cmdline_in(struct cmdline *cl, const char *buf, int size)
        int ret = 0;
        int i, same;
 
+       if (!cl || !buf)
+               return -1;
+
        for (i=0; i<size; i++) {
                ret = rdline_char_in(&cl->rdl, buf[i]);
 
@@ -221,6 +242,8 @@ cmdline_in(struct cmdline *cl, const char *buf, int size)
 void
 cmdline_quit(struct cmdline *cl)
 {
+       if (!cl)
+               return;
        rdline_quit(&cl->rdl);
 }
 
@@ -229,6 +252,9 @@ cmdline_interact(struct cmdline *cl)
 {
        char c;
 
+       if (!cl)
+               return;
+
        c = -1;
        while (1) {
                if (read(cl->s_in, &c, 1) < 0)
index f70a231..b8d87b1 100644 (file)
 
 #include <string.h>
 #include <errno.h>
+#include <stdio.h>
 
 #include "cmdline_cirbuf.h"
 
 
-void
+int
 cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen)
 {
+       if (!cbuf || !buf)
+               return -EINVAL;
        cbuf->maxlen = maxlen;
        cbuf->len = 0;
        cbuf->start = start;
        cbuf->end = start;
        cbuf->buf = buf;
+       return 0;
 }
 
 /* multiple add */
@@ -82,7 +86,7 @@ cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n)
 {
        unsigned int e;
 
-       if (!n || n > CIRBUF_GET_FREELEN(cbuf))
+       if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
                return -EINVAL;
 
        e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
@@ -113,7 +117,7 @@ cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n)
 {
        unsigned int e;
 
-       if (!n || n > CIRBUF_GET_FREELEN(cbuf))
+       if (!cbuf || !c || !n || n > CIRBUF_GET_FREELEN(cbuf))
                return -EINVAL;
 
        e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
@@ -232,8 +236,12 @@ __cirbuf_shift_right(struct cirbuf *cbuf)
 }
 
 /* XXX we could do a better algorithm here... */
-void cirbuf_align_left(struct cirbuf * cbuf)
+int
+cirbuf_align_left(struct cirbuf * cbuf)
 {
+       if (!cbuf)
+               return -EINVAL;
+
        if (cbuf->start < cbuf->maxlen/2) {
                while (cbuf->start != 0) {
                        __cirbuf_shift_left(cbuf);
@@ -244,11 +252,17 @@ void cirbuf_align_left(struct cirbuf * cbuf)
                        __cirbuf_shift_right(cbuf);
                }
        }
+
+       return 0;
 }
 
 /* XXX we could do a better algorithm here... */
-void cirbuf_align_right(struct cirbuf * cbuf)
+int
+cirbuf_align_right(struct cirbuf * cbuf)
 {
+       if (!cbuf)
+               return -EINVAL;
+
        if (cbuf->start >= cbuf->maxlen/2) {
                while (cbuf->end != cbuf->maxlen-1) {
                        __cirbuf_shift_left(cbuf);
@@ -259,6 +273,8 @@ void cirbuf_align_right(struct cirbuf * cbuf)
                        __cirbuf_shift_right(cbuf);
                }
        }
+
+       return 0;
 }
 
 /* buffer del */
@@ -266,7 +282,7 @@ void cirbuf_align_right(struct cirbuf * cbuf)
 int
 cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size)
 {
-       if (!size || size > CIRBUF_GET_LEN(cbuf))
+       if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
                return -EINVAL;
 
        cbuf->len -= size;
@@ -286,7 +302,7 @@ cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size)
 int
 cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size)
 {
-       if (!size || size > CIRBUF_GET_LEN(cbuf))
+       if (!cbuf || !size || size > CIRBUF_GET_LEN(cbuf))
                return -EINVAL;
 
        cbuf->len -= size;
@@ -364,6 +380,9 @@ cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size)
 {
        unsigned int n;
 
+       if (!cbuf || !c)
+               return -EINVAL;
+
        n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
 
        if (!n)
@@ -374,13 +393,20 @@ cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size)
                memcpy(c, cbuf->buf + cbuf->start , n);
        }
        else {
-               dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0,
-                       cbuf->maxlen - cbuf->start);
-               dprintf("s[%d] -> d[%d] (%d)\n", 0,cbuf->maxlen - cbuf->start,
-                       n - cbuf->maxlen + cbuf->start);
-               memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start);
-               memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf,
-                      n - cbuf->maxlen + cbuf->start);
+               /* check if we need to go from end to the beginning */
+               if (n <= cbuf->maxlen - cbuf->start) {
+                       dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start, n);
+                       memcpy(c, cbuf->buf + cbuf->start , n);
+               }
+               else {
+                       dprintf("s[%d] -> d[%d] (%d)\n", cbuf->start, 0,
+                               cbuf->maxlen - cbuf->start);
+                       dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->maxlen - cbuf->start,
+                               n - cbuf->maxlen + cbuf->start);
+                       memcpy(c, cbuf->buf + cbuf->start , cbuf->maxlen - cbuf->start);
+                       memcpy(c + cbuf->maxlen - cbuf->start, cbuf->buf,
+                                  n - cbuf->maxlen + cbuf->start);
+               }
        }
        return n;
 }
@@ -392,6 +418,9 @@ cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size)
 {
        unsigned int n;
 
+       if (!cbuf || !c)
+               return -EINVAL;
+
        n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
 
        if (!n)
@@ -402,15 +431,21 @@ cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size)
                memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
        }
        else {
-               dprintf("s[%d] -> d[%d] (%d)\n", 0,
-                       cbuf->maxlen - cbuf->start, cbuf->end + 1);
-               dprintf("s[%d] -> d[%d] (%d)\n",
-                       cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1);
-
-               memcpy(c + cbuf->maxlen - cbuf->start,
-                      cbuf->buf, cbuf->end + 1);
-               memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1,
-                      n - cbuf->end - 1);
+               /* check if we need to go from end to the beginning */
+               if (n <= cbuf->end + 1) {
+                       dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end - n + 1, n);
+                       memcpy(c, cbuf->buf + cbuf->end - n + 1, n);
+               }
+               else {
+                       dprintf("s[%d] -> d[%d] (%d)\n", 0,
+                               cbuf->maxlen - cbuf->start, cbuf->end + 1);
+                       dprintf("s[%d] -> d[%d] (%d)\n",
+                               cbuf->maxlen - n + cbuf->end + 1, 0, n - cbuf->end - 1);
+                       memcpy(c + cbuf->maxlen - cbuf->start,
+                                              cbuf->buf, cbuf->end + 1);
+                       memcpy(c, cbuf->buf + cbuf->maxlen - n + cbuf->end +1,
+                                  n - cbuf->end - 1);
+               }
        }
        return n;
 }
index b1391c2..eb83ea8 100644 (file)
@@ -77,9 +77,7 @@ struct cirbuf {
        char *buf;
 };
 
-/* #define CIRBUF_DEBUG */
-
-#ifdef CIRBUF_DEBUG
+#ifdef RTE_LIBRTE_CMDLINE_DEBUG
 #define dprintf(fmt, ...) printf("line %3.3d - " fmt, __LINE__, ##__VA_ARGS__)
 #else
 #define dprintf(args...) do {} while(0)
@@ -89,7 +87,7 @@ struct cirbuf {
 /**
  * Init the circular buffer
  */
-void cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen);
+int cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen);
 
 
 /**
@@ -233,12 +231,12 @@ int cirbuf_get_buf_tail(struct cirbuf *cbuf, char *c, unsigned int size);
 /**
  * Set the start of the data to the index 0 of the internal buffer.
  */
-void cirbuf_align_left(struct cirbuf *cbuf);
+int cirbuf_align_left(struct cirbuf *cbuf);
 
 /**
  * Set the end of the data to the last index of the internal buffer.
  */
-void cirbuf_align_right(struct cirbuf *cbuf);
+int cirbuf_align_right(struct cirbuf *cbuf);
 
 #ifdef __cplusplus
 }
index 8407681..03e0b53 100644 (file)
@@ -124,7 +124,7 @@ nb_common_chars(const char * s1, const char * s2)
 {
        unsigned int i=0;
 
-       while (*s1==*s2 && *s1 && *s2) {
+       while (*s1==*s2 && *s1) {
                s1++;
                s2++;
                i++;
@@ -228,11 +228,16 @@ cmdline_parse(struct cmdline *cl, const char * buf)
        int parse_it = 0;
        int err = CMDLINE_PARSE_NOMATCH;
        int tok;
-       cmdline_parse_ctx_t *ctx = cl->ctx;
+       cmdline_parse_ctx_t *ctx;
 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
        char debug_buf[BUFSIZ];
 #endif
 
+       if (!cl || !buf)
+               return CMDLINE_PARSE_BAD_ARGS;
+
+       ctx = cl->ctx;
+
        /*
         * - look if the buffer contains at least one line
         * - look if line contains only spaces or comments
@@ -342,7 +347,12 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,
        unsigned int nb_non_completable;
        int local_state = 0;
        const char *help_str;
-       cmdline_parse_ctx_t *ctx = cl->ctx;
+       cmdline_parse_ctx_t *ctx;
+
+       if (!cl || !buf || !state || !dst)
+               return -1;
+
+       ctx = cl->ctx;
 
        debug_printf("%s called\n", __func__);
        memset(&token_hdr, 0, sizeof(token_hdr));
@@ -423,6 +433,7 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state,
                                }
                        }
                next:
+                       debug_printf("next\n");
                        inst_num ++;
                        inst = ctx[inst_num];
                }
index 1b00075..c36a69d 100644 (file)
@@ -82,8 +82,9 @@ struct cmdline_token_ops cmdline_token_etheraddr_ops = {
        .get_help = cmdline_get_help_etheraddr,
 };
 
-
-#define ETHER_ADDRSTRLEN 18
+/* the format can be either XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX */
+#define ETHER_ADDRSTRLENLONG 18
+#define ETHER_ADDRSTRLENSHORT 15
 
 #ifdef __linux__
 #define ea_oct ether_addr_octet
@@ -140,17 +141,18 @@ cmdline_parse_etheraddr(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
                        const char *buf, void *res)
 {
        unsigned int token_len = 0;
-       char ether_str[ETHER_ADDRSTRLEN+1];
+       char ether_str[ETHER_ADDRSTRLENLONG+1];
        struct ether_addr *tmp;
 
-       if (! *buf)
+       if (!buf || ! *buf)
                return -1;
 
        while (!cmdline_isendoftoken(buf[token_len]))
                token_len++;
 
-       /* if token is too big... */
-       if (token_len >= ETHER_ADDRSTRLEN)
+       /* if token doesn't match possible string lengths... */
+       if ((token_len != ETHER_ADDRSTRLENLONG - 1) &&
+                       (token_len != ETHER_ADDRSTRLENSHORT - 1))
                return -1;
 
        rte_snprintf(ether_str, token_len+1, "%s", buf);
@@ -158,14 +160,19 @@ cmdline_parse_etheraddr(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
        tmp = my_ether_aton(ether_str);
        if (tmp == NULL)
                return -1;
-
-       memcpy(res, tmp, sizeof(struct ether_addr));
+       if (res)
+               memcpy(res, tmp, sizeof(struct ether_addr));
        return token_len;
 }
 
-int cmdline_get_help_etheraddr(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
+int
+cmdline_get_help_etheraddr(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
                               char *dstbuf, unsigned int size)
 {
-       rte_snprintf(dstbuf, size, "Ethernet address");
+       int ret;
+
+       ret = rte_snprintf(dstbuf, size, "Ethernet address");
+       if (ret < 0)
+               return -1;
        return 0;
 }
index 4c2817d..789a324 100644 (file)
 extern "C" {
 #endif
 
-struct cmdline_token_etheraddr_data {
-       uint8_t flags;
-};
-
 struct cmdline_token_etheraddr {
        struct cmdline_token_hdr hdr;
 };
@@ -82,15 +78,13 @@ int cmdline_parse_etheraddr(cmdline_parse_token_hdr_t *tk, const char *srcbuf,
 int cmdline_get_help_etheraddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
                               unsigned int size);
 
-/*
- * Warning! Not compatible with C++!
- */
-#define TOKEN_ETHERADDR_INITIALIZER(structure, field)      \
-{                                                          \
-       .hdr = {                                            \
-               .ops = &cmdline_token_etheraddr_ops,        \
-               .offset = offsetof(structure, field),       \
-       },                                                  \
+#define TOKEN_ETHERADDR_INITIALIZER(structure, field)       \
+{                                                           \
+       /* hdr */                                               \
+       {                                                       \
+               &cmdline_token_etheraddr_ops,   /* ops */           \
+               offsetof(structure, field),     /* offset */        \
+       },                                                      \
 }
 
 #ifdef __cplusplus
index f262b72..7659a1d 100644 (file)
@@ -105,6 +105,8 @@ struct cmdline_token_ops cmdline_token_ipaddr_ops = {
 
 #define INADDRSZ 4
 #define IN6ADDRSZ 16
+#define PREFIXMAX 128
+#define V4PREFIXMAX 32
 
 /*
  * WARNING: Don't even consider trying to compile this on a system where
@@ -207,10 +209,11 @@ inet_pton6(const char *src, unsigned char *dst)
 {
        static const char xdigits_l[] = "0123456789abcdef",
                xdigits_u[] = "0123456789ABCDEF";
-       unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
-       const char *xdigits, *curtok;
-       int ch, saw_xdigit, count_xdigit;
-       unsigned int val;
+       unsigned char tmp[IN6ADDRSZ], *tp = 0, *endp = 0, *colonp = 0;
+       const char *xdigits = 0, *curtok = 0;
+       int ch = 0, saw_xdigit = 0, count_xdigit = 0;
+       unsigned int val = 0;
+       unsigned dbloct_count = 0;
 
        memset((tp = tmp), '\0', IN6ADDRSZ);
        endp = tp + IN6ADDRSZ;
@@ -222,6 +225,7 @@ inet_pton6(const char *src, unsigned char *dst)
        curtok = src;
        saw_xdigit = count_xdigit = 0;
        val = 0;
+
        while ((ch = *src++) != '\0') {
                const char *pch;
 
@@ -255,6 +259,7 @@ inet_pton6(const char *src, unsigned char *dst)
                        saw_xdigit = 0;
                        count_xdigit = 0;
                        val = 0;
+                       dbloct_count++;
                        continue;
                }
                if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
@@ -262,6 +267,7 @@ inet_pton6(const char *src, unsigned char *dst)
                        tp += INADDRSZ;
                        saw_xdigit = 0;
                        count_xdigit = 0;
+                       dbloct_count += 2;
                        break;  /* '\0' was seen by inet_pton4(). */
                }
                return (0);
@@ -271,8 +277,13 @@ inet_pton6(const char *src, unsigned char *dst)
                        return (0);
                *tp++ = (unsigned char) ((val >> 8) & 0xff);
                *tp++ = (unsigned char) (val & 0xff);
+               dbloct_count++;
        }
        if (colonp != NULL) {
+               /* if we already have 8 double octets, having a colon means error */
+               if (dbloct_count == 8)
+                       return 0;
+
                /*
                 * Since some memmove()'s erroneously fail to handle
                 * overlapping regions, we'll do the shift by hand.
@@ -295,16 +306,18 @@ inet_pton6(const char *src, unsigned char *dst)
 int
 cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
 {
-       struct cmdline_token_ipaddr *tk2 = (struct cmdline_token_ipaddr *)tk;
+       struct cmdline_token_ipaddr *tk2;
        unsigned int token_len = 0;
        char ip_str[INET6_ADDRSTRLEN+4+1]; /* '+4' is for prefixlen (if any) */
        cmdline_ipaddr_t ipaddr;
        char *prefix, *prefix_end;
-       long prefixlen;
+       long prefixlen = 0;
 
-       if (! *buf)
+       if (!buf || !tk || ! *buf)
                return -1;
 
+       tk2 = (struct cmdline_token_ipaddr *)tk;
+
        while (!cmdline_isendoftoken(buf[token_len]))
                token_len++;
 
@@ -323,7 +336,8 @@ cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
                prefix ++;
                errno = 0;
                prefixlen = strtol(prefix, &prefix_end, 10);
-               if (errno || (*prefix_end != '\0') )
+               if (errno || (*prefix_end != '\0')
+                       || prefixlen < 0 || prefixlen > PREFIXMAX)
                        return -1;
                ipaddr.prefixlen = prefixlen;
        }
@@ -333,16 +347,17 @@ cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
 
        /* convert the IP addr */
        if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V4) &&
-           my_inet_pton(AF_INET, ip_str, &ipaddr.addr.ipv4) == 1) {
+           my_inet_pton(AF_INET, ip_str, &ipaddr.addr.ipv4) == 1 &&
+               prefixlen <= V4PREFIXMAX) {
                ipaddr.family = AF_INET;
-               if (res != NULL)
+               if (res)
                        memcpy(res, &ipaddr, sizeof(ipaddr));
                return token_len;
        }
        if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V6) &&
            my_inet_pton(AF_INET6, ip_str, &ipaddr.addr.ipv6) == 1) {
                ipaddr.family = AF_INET6;
-               if (res != NULL)
+               if (res)
                        memcpy(res, &ipaddr, sizeof(ipaddr));
                return token_len;
        }
@@ -353,7 +368,12 @@ cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
 int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
                            unsigned int size)
 {
-       struct cmdline_token_ipaddr *tk2 = (struct cmdline_token_ipaddr *)tk;
+       struct cmdline_token_ipaddr *tk2;
+
+       if (!tk || !dstbuf)
+               return -1;
+
+       tk2 = (struct cmdline_token_ipaddr *)tk;
 
        switch (tk2->ipaddr_data.flags) {
        case CMDLINE_IPADDR_V4:
index 35cc915..19d3fc1 100644 (file)
@@ -97,93 +97,87 @@ int cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *srcbuf,
 int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
                            unsigned int size);
 
-/*
- * Warning! Not compatible with C++!
- */
-#define TOKEN_IPADDR_INITIALIZER(structure, field)         \
-{                                                          \
-       .hdr = {                                            \
-               .ops = &cmdline_token_ipaddr_ops,           \
-               .offset = offsetof(structure, field),       \
-       },                                                  \
-       .ipaddr_data = {                                    \
-               .flags = CMDLINE_IPADDR_V4 |                \
-               CMDLINE_IPADDR_V6,                          \
-       },                                                  \
+#define TOKEN_IPADDR_INITIALIZER(structure, field)      \
+{                                                       \
+       /* hdr */                                           \
+       {                                                   \
+               &cmdline_token_ipaddr_ops,      /* ops */       \
+               offsetof(structure, field),     /* offset */    \
+       },                                                  \
+       /* ipaddr_data */                                   \
+       {                                                   \
+               CMDLINE_IPADDR_V4 |             /* flags */     \
+               CMDLINE_IPADDR_V6,                              \
+       },                                                  \
 }
 
-/*
- * Warning! Not compatible with C++!
- */
-#define TOKEN_IPV4_INITIALIZER(structure, field)           \
-{                                                          \
-       .hdr = {                                            \
-               .ops = &cmdline_token_ipaddr_ops,           \
-               .offset = offsetof(structure, field),       \
-       },                                                  \
-       .ipaddr_data = {                                    \
-               .flags = CMDLINE_IPADDR_V4,                 \
-       },                                                  \
+#define TOKEN_IPV4_INITIALIZER(structure, field)        \
+{                                                       \
+       /* hdr */                                           \
+       {                                                   \
+               &cmdline_token_ipaddr_ops,      /* ops */       \
+               offsetof(structure, field),     /* offset */    \
+       },                                                  \
+       /* ipaddr_data */                                   \
+       {                                                   \
+               CMDLINE_IPADDR_V4,              /* flags */     \
+       },                                                  \
 }
 
-/*
- * Warning! Not compatible with C++!
- */
-#define TOKEN_IPV6_INITIALIZER(structure, field)           \
-{                                                          \
-       .hdr = {                                            \
-               .ops = &cmdline_token_ipaddr_ops,           \
-               .offset = offsetof(structure, field),       \
-       },                                                  \
-       .ipaddr_data = {                                    \
-               .flags = CMDLINE_IPADDR_V6,                 \
-       },                                                  \
+#define TOKEN_IPV6_INITIALIZER(structure, field)        \
+{                                                       \
+       /* hdr */                                           \
+       {                                                   \
+               &cmdline_token_ipaddr_ops,      /* ops */       \
+               offsetof(structure, field),     /* offset */    \
+       },                                                  \
+       /* ipaddr_data */                                   \
+       {                                                   \
+               CMDLINE_IPADDR_V6,              /* flags */     \
+       },                                                  \
 }
 
-/*
- * Warning! Not compatible with C++!
- */
-#define TOKEN_IPNET_INITIALIZER(structure, field)          \
-{                                                          \
-       .hdr = {                                            \
-               .ops = &cmdline_token_ipaddr_ops,           \
-               .offset = offsetof(structure, field),       \
-       },                                                  \
-       .ipaddr_data = {                                    \
-               .flags = CMDLINE_IPADDR_V4 |                \
-               CMDLINE_IPADDR_V6 |                         \
-               CMDLINE_IPADDR_NETWORK,                     \
-       },                                                  \
+#define TOKEN_IPNET_INITIALIZER(structure, field)       \
+{                                                       \
+       /* hdr */                                           \
+       {                                                   \
+               &cmdline_token_ipaddr_ops,      /* ops */       \
+               offsetof(structure, field),     /* offset */    \
+       },                                                  \
+       /* ipaddr_data */                                   \
+       {                                                   \
+               CMDLINE_IPADDR_V4 |             /* flags */     \
+               CMDLINE_IPADDR_V6 |                             \
+               CMDLINE_IPADDR_NETWORK,                         \
+       },                                                  \
 }
 
-/*
- * Warning! Not compatible with C++!
- */
-#define TOKEN_IPV4NET_INITIALIZER(structure, field)        \
-{                                                          \
-       .hdr = {                                            \
-               .ops = &cmdline_token_ipaddr_ops,           \
-               .offset = offsetof(structure, field),       \
-       },                                                  \
-       .ipaddr_data = {                                    \
-               .flags = CMDLINE_IPADDR_V4 |                \
-               CMDLINE_IPADDR_NETWORK,                     \
-       },                                                  \
+#define TOKEN_IPV4NET_INITIALIZER(structure, field)     \
+{                                                       \
+       /* hdr */                                           \
+       {                                                   \
+               &cmdline_token_ipaddr_ops,      /* ops */       \
+               offsetof(structure, field),     /* offset */    \
+       },                                                  \
+       /* ipaddr_data */                                   \
+       {                                                   \
+               CMDLINE_IPADDR_V4 |             /* flags */     \
+               CMDLINE_IPADDR_NETWORK,                         \
+       },                                                  \
 }
 
-/*
- * Warning! Not compatible with C++!
- */
-#define TOKEN_IPV6NET_INITIALIZER(structure, field)        \
-{                                                          \
-       .hdr = {                                            \
-               .ops = &cmdline_token_ipaddr_ops,           \
-               .offset = offsetof(structure, field),       \
-       },                                                  \
-       .ipaddr_data = {                                    \
-               .flags = CMDLINE_IPADDR_V4 |                \
-               CMDLINE_IPADDR_NETWORK,                     \
-       },                                                  \
+#define TOKEN_IPV6NET_INITIALIZER(structure, field)     \
+{                                                       \
+       /* hdr */                                           \
+       {                                                   \
+               &cmdline_token_ipaddr_ops,      /* ops */       \
+               offsetof(structure, field),     /* offset */    \
+       },                                                  \
+       /* ipaddr_data */                                   \
+       {                                                   \
+               CMDLINE_IPADDR_V4 |             /* flags */     \
+               CMDLINE_IPADDR_NETWORK,                         \
+       },                                                  \
 }
 
 #ifdef __cplusplus
index 11421fb..47a838c 100644 (file)
@@ -90,8 +90,6 @@ enum num_parse_state_t {
        DEC_NEG,
        BIN,
        HEX,
-       FLOAT_POS,
-       FLOAT_NEG,
 
        ERROR,
 
@@ -102,17 +100,12 @@ enum num_parse_state_t {
        BIN_OK,
        DEC_NEG_OK,
        DEC_POS_OK,
-       FLOAT_POS_OK,
-       FLOAT_NEG_OK
 };
 
 /* Keep it sync with enum in .h */
 static const char * num_help[] = {
        "UINT8", "UINT16", "UINT32", "UINT64",
        "INT8", "INT16", "INT32", "INT64",
-#ifdef CMDLINE_HAVE_FLOAT
-       "FLOAT",
-#endif
 };
 
 static inline int
@@ -128,18 +121,24 @@ add_to_res(unsigned int c, uint64_t *res, unsigned int base)
 }
 
 
-/* parse an int or a float */
+/* parse an int */
 int
 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
 {
        struct cmdline_token_num_data nd;
        enum num_parse_state_t st = START;
-       const char * buf = srcbuf;
-       char c = *buf;
+       const char * buf;
+       char c;
        uint64_t res1 = 0;
-#ifdef CMDLINE_HAVE_FLOAT
-       uint64_t res2 = 0, res3 = 1;
-#endif
+
+       if (!tk)
+               return -1;
+
+       if (!srcbuf || !*srcbuf)
+               return -1;
+
+       buf = srcbuf;
+       c = *buf;
 
        memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
 
@@ -153,12 +152,6 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                        else if (c == '0') {
                                st = ZERO_OK;
                        }
-#ifdef CMDLINE_HAVE_FLOAT
-                       else if (c == '.') {
-                               st = FLOAT_POS;
-                               res1 = 0;
-                       }
-#endif
                        else if (c >= '1' && c <= '9') {
                                if (add_to_res(c - '0', &res1, 10) < 0)
                                        st = ERROR;
@@ -177,12 +170,6 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                        else if (c == 'b') {
                                st = BIN;
                        }
-#ifdef CMDLINE_HAVE_FLOAT
-                       else if (c == '.') {
-                               st = FLOAT_POS;
-                               res1 = 0;
-                       }
-#endif
                        else if (c >= '0' && c <= '7') {
                                if (add_to_res(c - '0', &res1, 10) < 0)
                                        st = ERROR;
@@ -201,12 +188,6 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                                else
                                        st = DEC_NEG_OK;
                        }
-#ifdef CMDLINE_HAVE_FLOAT
-                       else if (c == '.') {
-                               res1 = 0;
-                               st = FLOAT_NEG;
-                       }
-#endif
                        else {
                                st = ERROR;
                        }
@@ -217,11 +198,6 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                                if (add_to_res(c - '0', &res1, 10) < 0)
                                        st = ERROR;
                        }
-#ifdef CMDLINE_HAVE_FLOAT
-                       else if (c == '.') {
-                               st = FLOAT_NEG;
-                       }
-#endif
                        else {
                                st = ERROR;
                        }
@@ -232,11 +208,6 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                                if (add_to_res(c - '0', &res1, 10) < 0)
                                        st = ERROR;
                        }
-#ifdef CMDLINE_HAVE_FLOAT
-                       else if (c == '.') {
-                               st = FLOAT_POS;
-                       }
-#endif
                        else {
                                st = ERROR;
                        }
@@ -286,70 +257,12 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
                                st = ERROR;
                        }
                        break;
-
-#ifdef CMDLINE_HAVE_FLOAT
-               case FLOAT_POS:
-                       if (c >= '0' && c <= '9') {
-                               if (add_to_res(c - '0', &res2, 10) < 0)
-                                       st = ERROR;
-                               else
-                                       st = FLOAT_POS_OK;
-                               res3 = 10;
-                       }
-                       else {
-                               st = ERROR;
-                       }
-                       break;
-
-               case FLOAT_NEG:
-                       if (c >= '0' && c <= '9') {
-                               if (add_to_res(c - '0', &res2, 10) < 0)
-                                       st = ERROR;
-                               else
-                                       st = FLOAT_NEG_OK;
-                               res3 = 10;
-                       }
-                       else {
-                               st = ERROR;
-                       }
-                       break;
-
-               case FLOAT_POS_OK:
-                       if (c >= '0' && c <= '9') {
-                               if (add_to_res(c - '0', &res2, 10) < 0)
-                                       st = ERROR;
-                               if (add_to_res(0, &res3, 10) < 0)
-                                       st = ERROR;
-                       }
-                       else {
-                               st = ERROR;
-                       }
-                       break;
-
-               case FLOAT_NEG_OK:
-                       if (c >= '0' && c <= '9') {
-                               if (add_to_res(c - '0', &res2, 10) < 0)
-                                       st = ERROR;
-                               if (add_to_res(0, &res3, 10) < 0)
-                                       st = ERROR;
-                       }
-                       else {
-                               st = ERROR;
-                       }
-                       break;
-#endif
-
                default:
                        debug_printf("not impl ");
 
                }
 
-#ifdef CMDLINE_HAVE_FLOAT
-               debug_printf("(%"PRIu32")  (%"PRIu32")  (%"PRIu32")\n",
-                            res1, res2, res3);
-#else
-               debug_printf("(%"PRIu32")\n", res1);
-#endif
+               debug_printf("(%"PRIu64")\n", res1);
 
                buf ++;
                c = *buf;
@@ -366,47 +279,37 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
        case OCTAL_OK:
        case BIN_OK:
                if ( nd.type == INT8 && res1 <= INT8_MAX ) {
-                       if (res)
-                               *(int8_t *)res = (int8_t) res1;
+                       if (res) *(int8_t *)res = (int8_t) res1;
                        return (buf-srcbuf);
                }
                else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
-                       if (res)
-                               *(int16_t *)res = (int16_t) res1;
+                       if (res) *(int16_t *)res = (int16_t) res1;
                        return (buf-srcbuf);
                }
                else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
-                       if (res)
-                               *(int32_t *)res = (int32_t) res1;
+                       if (res) *(int32_t *)res = (int32_t) res1;
+                       return (buf-srcbuf);
+               }
+               else if ( nd.type == INT64 && res1 <= INT64_MAX ) {
+                       if (res) *(int64_t *)res = (int64_t) res1;
                        return (buf-srcbuf);
                }
                else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
-                       if (res)
-                               *(uint8_t *)res = (uint8_t) res1;
+                       if (res) *(uint8_t *)res = (uint8_t) res1;
                        return (buf-srcbuf);
                }
                else if (nd.type == UINT16  && res1 <= UINT16_MAX ) {
-                       if (res)
-                               *(uint16_t *)res = (uint16_t) res1;
+                       if (res) *(uint16_t *)res = (uint16_t) res1;
                        return (buf-srcbuf);
                }
-               else if ( nd.type == UINT32 ) {
-                       if (res)
-                               *(uint32_t *)res = (uint32_t) res1;
+               else if ( nd.type == UINT32 && res1 <= UINT32_MAX ) {
+                       if (res) *(uint32_t *)res = (uint32_t) res1;
                        return (buf-srcbuf);
                }
                else if ( nd.type == UINT64 ) {
-                       if (res)
-                               *(uint64_t *)res = res1;
-                       return (buf-srcbuf);
-               }
-#ifdef CMDLINE_HAVE_FLOAT
-               else if ( nd.type == FLOAT ) {
-                       if (res)
-                               *(float *)res = (float)res1;
+                       if (res) *(uint64_t *)res = res1;
                        return (buf-srcbuf);
                }
-#endif
                else {
                        return -1;
                }
@@ -414,59 +317,25 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
 
        case DEC_NEG_OK:
                if ( nd.type == INT8 && res1 <= INT8_MAX + 1 ) {
-                       if (res)
-                               *(int8_t *)res = (int8_t) (-res1);
+                       if (res) *(int8_t *)res = (int8_t) (-res1);
                        return (buf-srcbuf);
                }
                else if ( nd.type == INT16 && res1 <= (uint16_t)INT16_MAX + 1 ) {
-                       if (res)
-                               *(int16_t *)res = (int16_t) (-res1);
+                       if (res) *(int16_t *)res = (int16_t) (-res1);
                        return (buf-srcbuf);
                }
                else if ( nd.type == INT32 && res1 <= (uint32_t)INT32_MAX + 1 ) {
-                       if (res)
-                               *(int32_t *)res = (int32_t) (-res1);
+                       if (res) *(int32_t *)res = (int32_t) (-res1);
                        return (buf-srcbuf);
                }
-#ifdef CMDLINE_HAVE_FLOAT
-               else if ( nd.type == FLOAT ) {
-                       if (res)
-                               *(float *)res = - (float)res1;
+               else if ( nd.type == INT64 && res1 <= (uint64_t)INT64_MAX + 1 ) {
+                       if (res) *(int64_t *)res = (int64_t) (-res1);
                        return (buf-srcbuf);
                }
-#endif
                else {
                        return -1;
                }
                break;
-
-#ifdef CMDLINE_HAVE_FLOAT
-       case FLOAT_POS:
-       case FLOAT_POS_OK:
-               if ( nd.type == FLOAT ) {
-                       if (res)
-                               *(float *)res = (float)res1 + ((float)res2 / (float)res3);
-                       return (buf-srcbuf);
-
-               }
-               else {
-                       return -1;
-               }
-               break;
-
-       case FLOAT_NEG:
-       case FLOAT_NEG_OK:
-               if ( nd.type == FLOAT ) {
-                       if (res)
-                               *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
-                       return (buf-srcbuf);
-
-               }
-               else {
-                       return -1;
-               }
-               break;
-#endif
        default:
                debug_printf("error\n");
                return -1;
@@ -474,11 +343,15 @@ cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
 }
 
 
-/* parse an int or a float */
+/* parse an int */
 int
 cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
 {
        struct cmdline_token_num_data nd;
+       int ret;
+
+       if (!tk)
+               return -1;
 
        memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
 
@@ -486,7 +359,9 @@ cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int s
        /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
        /* return -1; */
 
-       rte_snprintf(dstbuf, size, "%s", num_help[nd.type]);
+       ret = rte_snprintf(dstbuf, size, "%s", num_help[nd.type]);
+       if (ret < 0)
+               return -1;
        dstbuf[size-1] = '\0';
        return 0;
 }
index 1a78927..02fdf94 100644 (file)
@@ -75,9 +75,6 @@ enum cmdline_numtype {
        INT16,
        INT32,
        INT64
-#ifndef NO_PARSE_FLOAT
-       ,FLOAT
-#endif
 };
 
 struct cmdline_token_num_data {
@@ -97,18 +94,17 @@ int cmdline_parse_num(cmdline_parse_token_hdr_t *tk,
 int cmdline_get_help_num(cmdline_parse_token_hdr_t *tk,
                         char *dstbuf, unsigned int size);
 
-/*
- * Warning! Not compatible with C++!
- */
-#define TOKEN_NUM_INITIALIZER(structure, field, numtype)   \
-{                                                         \
-       .hdr = {                                           \
-               .ops = &cmdline_token_num_ops,             \
-               .offset = offsetof(structure, field),      \
-       },                                                 \
-       .num_data = {                                      \
-               .type = numtype,                           \
-       },                                                 \
+#define TOKEN_NUM_INITIALIZER(structure, field, numtype)    \
+{                                                           \
+       /* hdr */                                               \
+       {                                                       \
+               &cmdline_token_num_ops,         /* ops */           \
+               offsetof(structure, field),     /* offset */        \
+       },                                                      \
+       /* num_data */                                          \
+       {                                                       \
+               numtype,                        /* type */          \
+       },                                                      \
 }
 
 #ifdef __cplusplus
index 9fa619d..d698019 100644 (file)
@@ -136,11 +136,13 @@ cmdline_parse_portlist(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
 {
        unsigned int token_len = 0;
        char portlist_str[PORTLIST_TOKEN_SIZE+1];
-       cmdline_portlist_t *pl = res;
+       cmdline_portlist_t *pl;
 
-       if (! *buf)
+       if (!buf || ! *buf)
                return (-1);
 
+       pl = res;
+
        while (!cmdline_isendoftoken(buf[token_len]) &&
            (token_len < PORTLIST_TOKEN_SIZE))
                token_len++;
@@ -148,24 +150,26 @@ cmdline_parse_portlist(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
        if (token_len >= PORTLIST_TOKEN_SIZE)
                return (-1);
 
-       if (pl == NULL)
-               return (token_len);
-
        rte_snprintf(portlist_str, token_len+1, "%s", buf);
 
-       pl->map = 0;
-       if (strcmp("all", portlist_str) == 0)
-               pl->map = UINT32_MAX;
-       else if (parse_ports(pl, portlist_str) != 0)
-               return (-1);
+       if (pl) {
+               pl->map = 0;
+               if (strcmp("all", portlist_str) == 0)
+                       pl->map = UINT32_MAX;
+               else if (parse_ports(pl, portlist_str) != 0)
+                       return (-1);
+       }
 
        return token_len;
 }
 
-int cmdline_get_help_portlist(cmdline_parse_token_hdr_t *tk, char *dstbuf,
-                           unsigned int size)
+int
+cmdline_get_help_portlist(__attribute__((unused)) cmdline_parse_token_hdr_t *tk,
+               char *dstbuf, unsigned int size)
 {
-       (void)tk;
-       rte_snprintf(dstbuf, size, "range of ports as 3,4-6,8-19,20");
+       int ret;
+       ret = rte_snprintf(dstbuf, size, "range of ports as 3,4-6,8-19,20");
+       if (ret < 0)
+               return -1;
        return 0;
 }
index fa89f30..752e68d 100644 (file)
@@ -74,13 +74,8 @@ typedef struct cmdline_portlist {
        uint32_t                map;
 } cmdline_portlist_t;
 
-struct cmdline_token_portlist_data {
-       uint8_t         flags;
-};
-
 struct cmdline_token_portlist {
        struct cmdline_token_hdr hdr;
-       struct cmdline_token_portlist_data range_data;
 };
 typedef struct cmdline_token_portlist cmdline_parse_token_portlist_t;
 
@@ -91,18 +86,13 @@ int cmdline_parse_portlist(cmdline_parse_token_hdr_t *tk,
 int cmdline_get_help_portlist(cmdline_parse_token_hdr_t *tk,
                         char *dstbuf, unsigned int size);
 
-/*
- * Warning! Not compatible with C++!
- */
-#define TOKEN_PORTLIST_INITIALIZER(structure, field)           \
-{                                                              \
-       .hdr = {                                                \
-               .ops = &cmdline_token_portlist_ops,             \
-               .offset = offsetof(structure, field),           \
-       },                                                      \
-       .range_data = {                                         \
-               .flags = 0,                                     \
-       },                                                      \
+#define TOKEN_PORTLIST_INITIALIZER(structure, field)        \
+{                                                           \
+       /* hdr */                                               \
+       {                                                       \
+               &cmdline_token_portlist_ops,    /* ops */           \
+               offsetof(structure, field),     /* offset */        \
+       },                                                      \
 }
 
 #ifdef __cplusplus
index 59d973f..9bd2d72 100644 (file)
@@ -108,14 +108,18 @@ get_next_token(const char *s)
 int
 cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
 {
-       struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk;
-       struct cmdline_token_string_data *sd = &tk2->string_data;
+       struct cmdline_token_string *tk2;
+       struct cmdline_token_string_data *sd;
        unsigned int token_len;
        const char *str;
 
-       if (! *buf)
+       if (!tk || !buf || ! *buf)
                return -1;
 
+       tk2 = (struct cmdline_token_string *)tk;
+
+       sd = &tk2->string_data;
+
        /* fixed string */
        if (sd->str) {
                str = sd->str;
@@ -143,7 +147,7 @@ cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
        }
        /* unspecified string */
        else {
-               token_len=0;
+               token_len = 0;
                while(!cmdline_isendoftoken(buf[token_len]) &&
                      token_len < (STR_TOKEN_SIZE-1))
                        token_len++;
@@ -160,15 +164,22 @@ cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
                *((char *)res + token_len) = 0;
        }
 
+
        return token_len;
 }
 
 int cmdline_complete_get_nb_string(cmdline_parse_token_hdr_t *tk)
 {
-       struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk;
-       struct cmdline_token_string_data *sd = &tk2->string_data;;
-       int ret=1;
+       struct cmdline_token_string *tk2;
+       struct cmdline_token_string_data *sd;
        const char *str;
+       int ret = 1;
+
+       if (!tk)
+               return -1;
+
+       tk2 = (struct cmdline_token_string *)tk;
+       sd = &tk2->string_data;
 
        if (!sd->str)
                return 0;
@@ -183,11 +194,17 @@ int cmdline_complete_get_nb_string(cmdline_parse_token_hdr_t *tk)
 int cmdline_complete_get_elt_string(cmdline_parse_token_hdr_t *tk, int idx,
                                    char *dstbuf, unsigned int size)
 {
-       struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk;
-       struct cmdline_token_string_data *sd = &tk2->string_data;;
+       struct cmdline_token_string *tk2;
+       struct cmdline_token_string_data *sd;
        const char *s;
        unsigned int len;
 
+       if (!tk || !dstbuf || idx < 0)
+               return -1;
+
+       tk2 = (struct cmdline_token_string *)tk;
+       sd = &tk2->string_data;
+
        s = sd->str;
 
        while (idx-- && s)
@@ -209,10 +226,16 @@ int cmdline_complete_get_elt_string(cmdline_parse_token_hdr_t *tk, int idx,
 int cmdline_get_help_string(cmdline_parse_token_hdr_t *tk, char *dstbuf,
                            unsigned int size)
 {
-       struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk;
-       struct cmdline_token_string_data *sd = &tk2->string_data;;
+       struct cmdline_token_string *tk2;
+       struct cmdline_token_string_data *sd;
        const char *s;
 
+       if (!tk || !dstbuf)
+               return -1;
+
+       tk2 = (struct cmdline_token_string *)tk;
+       sd = &tk2->string_data;
+
        s = sd->str;
 
        if (s) {
index 12c9b40..a903bf1 100644 (file)
@@ -91,18 +91,17 @@ int cmdline_complete_get_elt_string(cmdline_parse_token_hdr_t *tk, int idx,
 int cmdline_get_help_string(cmdline_parse_token_hdr_t *tk, char *dstbuf,
                            unsigned int size);
 
-/*
- * Warning! Not compatible with C++!
- */
 #define TOKEN_STRING_INITIALIZER(structure, field, string)  \
-{                                                          \
-       .hdr = {                                            \
-               .ops = &cmdline_token_string_ops,           \
-               .offset = offsetof(structure, field),       \
-       },                                                  \
-       .string_data = {                                    \
-               .str = string,                              \
-       },                                                  \
+{                                                           \
+       /* hdr */                                               \
+       {                                                       \
+               &cmdline_token_string_ops,      /* ops */           \
+               offsetof(structure, field),     /* offset */        \
+       },                                                      \
+       /* string_data */                                       \
+       {                                                       \
+               string,                         /* str */           \
+       },                                                      \
 }
 
 #ifdef __cplusplus
index 8b3e9e8..0f99e61 100644 (file)
@@ -64,6 +64,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <stdarg.h>
+#include <errno.h>
 #include <ctype.h>
 
 #include "cmdline_cirbuf.h"
@@ -73,11 +74,9 @@ static void rdline_puts(struct rdline *rdl, const char *buf);
 static void rdline_miniprintf(struct rdline *rdl,
                              const char *buf, unsigned int val);
 
-#ifndef NO_RDLINE_HISTORY
 static void rdline_remove_old_history_item(struct rdline *rdl);
 static void rdline_remove_first_history_item(struct rdline *rdl);
 static unsigned int rdline_get_history_size(struct rdline *rdl);
-#endif /* !NO_RDLINE_HISTORY */
 
 
 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
@@ -91,20 +90,20 @@ isblank2(char c)
        return 0;
 }
 
-void
+int
 rdline_init(struct rdline *rdl,
                 rdline_write_char_t *write_char,
                 rdline_validate_t *validate,
                 rdline_complete_t *complete)
 {
+       if (!rdl || !write_char || !validate || !complete)
+               return -EINVAL;
        memset(rdl, 0, sizeof(*rdl));
        rdl->validate = validate;
        rdl->complete = complete;
        rdl->write_char = write_char;
        rdl->status = RDLINE_INIT;
-#ifndef NO_RDLINE_HISTORY
-       cirbuf_init(&rdl->history, rdl->history_buf, 0, RDLINE_HISTORY_BUF_SIZE);
-#endif /* !NO_RDLINE_HISTORY */
+       return cirbuf_init(&rdl->history, rdl->history_buf, 0, RDLINE_HISTORY_BUF_SIZE);
 }
 
 void
@@ -112,6 +111,9 @@ rdline_newline(struct rdline *rdl, const char *prompt)
 {
        unsigned int i;
 
+       if (!rdl || !prompt)
+               return;
+
        vt100_init(&rdl->vt100);
        cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
        cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
@@ -124,46 +126,52 @@ rdline_newline(struct rdline *rdl, const char *prompt)
                rdl->write_char(rdl, rdl->prompt[i]);
        rdl->status = RDLINE_RUNNING;
 
-#ifndef NO_RDLINE_HISTORY
        rdl->history_cur_line = -1;
-#endif /* !NO_RDLINE_HISTORY */
 }
 
 void
 rdline_stop(struct rdline *rdl)
 {
+       if (!rdl)
+               return;
        rdl->status = RDLINE_INIT;
 }
 
 void
 rdline_quit(struct rdline *rdl)
 {
+       if (!rdl)
+               return;
        rdl->status = RDLINE_EXITED;
 }
 
 void
 rdline_restart(struct rdline *rdl)
 {
+       if (!rdl)
+               return;
        rdl->status = RDLINE_RUNNING;
 }
 
 void
 rdline_reset(struct rdline *rdl)
 {
+       if (!rdl)
+               return;
        vt100_init(&rdl->vt100);
        cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
        cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
 
        rdl->status = RDLINE_RUNNING;
 
-#ifndef NO_RDLINE_HISTORY
        rdl->history_cur_line = -1;
-#endif /* !NO_RDLINE_HISTORY */
 }
 
 const char *
 rdline_get_buffer(struct rdline *rdl)
 {
+       if (!rdl)
+               return NULL;
        unsigned int len_l, len_r;
        cirbuf_align_left(&rdl->left);
        cirbuf_align_left(&rdl->right);
@@ -201,6 +209,9 @@ rdline_redisplay(struct rdline *rdl)
        unsigned int i;
        char tmp;
 
+       if (!rdl)
+               return;
+
        rdline_puts(rdl, vt100_home);
        for (i=0 ; i<rdl->prompt_size ; i++)
                rdl->write_char(rdl, rdl->prompt[i]);
@@ -216,9 +227,10 @@ rdline_char_in(struct rdline *rdl, char c)
        unsigned int i;
        int cmd;
        char tmp;
-#ifndef NO_RDLINE_HISTORY
        char *buf;
-#endif
+
+       if (!rdl)
+               return -EINVAL;
 
        if (rdl->status == RDLINE_EXITED)
                return RDLINE_RES_EXITED;
@@ -231,6 +243,7 @@ rdline_char_in(struct rdline *rdl, char c)
 
        if (cmd >= 0) {
                switch (cmd) {
+               /* move caret 1 char to the left */
                case CMDLINE_KEY_CTRL_B:
                case CMDLINE_KEY_LEFT_ARR:
                        if (CIRBUF_IS_EMPTY(&rdl->left))
@@ -241,6 +254,7 @@ rdline_char_in(struct rdline *rdl, char c)
                        rdline_puts(rdl, vt100_left_arr);
                        break;
 
+               /* move caret 1 char to the right */
                case CMDLINE_KEY_CTRL_F:
                case CMDLINE_KEY_RIGHT_ARR:
                        if (CIRBUF_IS_EMPTY(&rdl->right))
@@ -251,6 +265,8 @@ rdline_char_in(struct rdline *rdl, char c)
                        rdline_puts(rdl, vt100_right_arr);
                        break;
 
+               /* move caret 1 word to the left */
+               /* keyboard equivalent: Alt+B */
                case CMDLINE_KEY_WLEFT:
                        while (! CIRBUF_IS_EMPTY(&rdl->left) &&
                               (tmp = cirbuf_get_tail(&rdl->left)) &&
@@ -268,6 +284,8 @@ rdline_char_in(struct rdline *rdl, char c)
                        }
                        break;
 
+               /* move caret 1 word to the right */
+               /* keyboard equivalent: Alt+F */
                case CMDLINE_KEY_WRIGHT:
                        while (! CIRBUF_IS_EMPTY(&rdl->right) &&
                               (tmp = cirbuf_get_head(&rdl->right)) &&
@@ -285,6 +303,33 @@ rdline_char_in(struct rdline *rdl, char c)
                        }
                        break;
 
+               /* move caret to the left */
+               case CMDLINE_KEY_CTRL_A:
+                       if (CIRBUF_IS_EMPTY(&rdl->left))
+                               break;
+                       rdline_miniprintf(rdl, vt100_multi_left,
+                                               CIRBUF_GET_LEN(&rdl->left));
+                       while (! CIRBUF_IS_EMPTY(&rdl->left)) {
+                               tmp = cirbuf_get_tail(&rdl->left);
+                               cirbuf_del_tail(&rdl->left);
+                               cirbuf_add_head(&rdl->right, tmp);
+                       }
+                       break;
+
+               /* move caret to the right */
+               case CMDLINE_KEY_CTRL_E:
+                       if (CIRBUF_IS_EMPTY(&rdl->right))
+                               break;
+                       rdline_miniprintf(rdl, vt100_multi_right,
+                                               CIRBUF_GET_LEN(&rdl->right));
+                       while (! CIRBUF_IS_EMPTY(&rdl->right)) {
+                               tmp = cirbuf_get_head(&rdl->right);
+                               cirbuf_del_head(&rdl->right);
+                               cirbuf_add_tail(&rdl->left, tmp);
+                       }
+                       break;
+
+               /* delete 1 char from the left */
                case CMDLINE_KEY_BKSPACE:
                        if(!cirbuf_del_tail_safe(&rdl->left)) {
                                rdline_puts(rdl, vt100_bs);
@@ -292,6 +337,20 @@ rdline_char_in(struct rdline *rdl, char c)
                        }
                        break;
 
+               /* delete 1 char from the right */
+               case CMDLINE_KEY_SUPPR:
+               case CMDLINE_KEY_CTRL_D:
+                       if (cmd == CMDLINE_KEY_CTRL_D &&
+                           CIRBUF_IS_EMPTY(&rdl->left) &&
+                           CIRBUF_IS_EMPTY(&rdl->right)) {
+                               return RDLINE_RES_EOF;
+                       }
+                       if (!cirbuf_del_head_safe(&rdl->right)) {
+                               display_right_buffer(rdl, 1);
+                       }
+                       break;
+
+               /* delete 1 word from the left */
                case CMDLINE_KEY_META_BKSPACE:
                case CMDLINE_KEY_CTRL_W:
                        while (! CIRBUF_IS_EMPTY(&rdl->left) && isblank2(cirbuf_get_tail(&rdl->left))) {
@@ -305,6 +364,7 @@ rdline_char_in(struct rdline *rdl, char c)
                        display_right_buffer(rdl, 1);
                        break;
 
+               /* delete 1 word from the right */
                case CMDLINE_KEY_META_D:
                        while (! CIRBUF_IS_EMPTY(&rdl->right) && isblank2(cirbuf_get_head(&rdl->right)))
                                cirbuf_del_head(&rdl->right);
@@ -313,43 +373,7 @@ rdline_char_in(struct rdline *rdl, char c)
                        display_right_buffer(rdl, 1);
                        break;
 
-               case CMDLINE_KEY_SUPPR:
-               case CMDLINE_KEY_CTRL_D:
-                       if (cmd == CMDLINE_KEY_CTRL_D &&
-                           CIRBUF_IS_EMPTY(&rdl->left) &&
-                           CIRBUF_IS_EMPTY(&rdl->right)) {
-                               return RDLINE_RES_EOF;
-                       }
-                       if (!cirbuf_del_head_safe(&rdl->right)) {
-                               display_right_buffer(rdl, 1);
-                       }
-                       break;
-
-               case CMDLINE_KEY_CTRL_A:
-                       if (CIRBUF_IS_EMPTY(&rdl->left))
-                               break;
-                       rdline_miniprintf(rdl, vt100_multi_left,
-                                           CIRBUF_GET_LEN(&rdl->left));
-                       while (! CIRBUF_IS_EMPTY(&rdl->left)) {
-                               tmp = cirbuf_get_tail(&rdl->left);
-                               cirbuf_del_tail(&rdl->left);
-                               cirbuf_add_head(&rdl->right, tmp);
-                       }
-                       break;
-
-               case CMDLINE_KEY_CTRL_E:
-                       if (CIRBUF_IS_EMPTY(&rdl->right))
-                               break;
-                       rdline_miniprintf(rdl, vt100_multi_right,
-                                           CIRBUF_GET_LEN(&rdl->right));
-                       while (! CIRBUF_IS_EMPTY(&rdl->right)) {
-                               tmp = cirbuf_get_head(&rdl->right);
-                               cirbuf_del_head(&rdl->right);
-                               cirbuf_add_tail(&rdl->left, tmp);
-                       }
-                       break;
-
-#ifndef NO_RDLINE_KILL_BUF
+               /* set kill buffer to contents on the right side of caret */
                case CMDLINE_KEY_CTRL_K:
                        cirbuf_get_buf_head(&rdl->right, rdl->kill_buf, RDLINE_BUF_SIZE);
                        rdl->kill_size = CIRBUF_GET_LEN(&rdl->right);
@@ -357,6 +381,7 @@ rdline_char_in(struct rdline *rdl, char c)
                        rdline_puts(rdl, vt100_clear_right);
                        break;
 
+               /* paste contents of kill buffer to the left side of caret */
                case CMDLINE_KEY_CTRL_Y:
                        i=0;
                        while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
@@ -368,17 +393,19 @@ rdline_char_in(struct rdline *rdl, char c)
                        }
                        display_right_buffer(rdl, 0);
                        break;
-#endif /* !NO_RDLINE_KILL_BUF */
 
+               /* clear and newline */
                case CMDLINE_KEY_CTRL_C:
                        rdline_puts(rdl, "\r\n");
                        rdline_newline(rdl, rdl->prompt);
                        break;
 
+               /* redisplay (helps when prompt is lost in other output) */
                case CMDLINE_KEY_CTRL_L:
                        rdline_redisplay(rdl);
                        break;
 
+               /* autocomplete */
                case CMDLINE_KEY_TAB:
                case CMDLINE_KEY_HELP:
                        cirbuf_align_left(&rdl->left);
@@ -434,15 +461,14 @@ rdline_char_in(struct rdline *rdl, char c)
                        }
                        return RDLINE_RES_COMPLETE;
 
+               /* complete buffer */
                case CMDLINE_KEY_RETURN:
                case CMDLINE_KEY_RETURN2:
                        rdline_get_buffer(rdl);
                        rdl->status = RDLINE_INIT;
                        rdline_puts(rdl, "\r\n");
-#ifndef NO_RDLINE_HISTORY
                        if (rdl->history_cur_line != -1)
                                rdline_remove_first_history_item(rdl);
-#endif
 
                        if (rdl->validate)
                                rdl->validate(rdl, rdl->left_buf, CIRBUF_GET_LEN(&rdl->left)+2);
@@ -451,7 +477,7 @@ rdline_char_in(struct rdline *rdl, char c)
                                return RDLINE_RES_EXITED;
                        return RDLINE_RES_VALIDATED;
 
-#ifndef NO_RDLINE_HISTORY
+               /* previous element in history */
                case CMDLINE_KEY_UP_ARR:
                case CMDLINE_KEY_CTRL_P:
                        if (rdl->history_cur_line == 0) {
@@ -474,6 +500,7 @@ rdline_char_in(struct rdline *rdl, char c)
                        rdline_redisplay(rdl);
                        break;
 
+               /* next element in history */
                case CMDLINE_KEY_DOWN_ARR:
                case CMDLINE_KEY_CTRL_N:
                        if (rdl->history_cur_line - 1 < 0)
@@ -490,7 +517,6 @@ rdline_char_in(struct rdline *rdl, char c)
                        rdline_redisplay(rdl);
 
                        break;
-#endif /* !NO_RDLINE_HISTORY */
 
 
                default:
@@ -519,7 +545,6 @@ rdline_char_in(struct rdline *rdl, char c)
 
 /* HISTORY */
 
-#ifndef NO_RDLINE_HISTORY
 static void
 rdline_remove_old_history_item(struct rdline * rdl)
 {
@@ -571,6 +596,9 @@ rdline_get_history_item(struct rdline * rdl, unsigned int idx)
 {
        unsigned int len, i, tmp;
 
+       if (!rdl)
+               return NULL;
+
        len = rdline_get_history_size(rdl);
        if ( idx >= len ) {
                return NULL;
@@ -594,6 +622,9 @@ rdline_add_history(struct rdline * rdl, const char * buf)
 {
        unsigned int len, i;
 
+       if (!rdl || !buf)
+               return -EINVAL;
+
        len = strnlen(buf, RDLINE_BUF_SIZE);
        for (i=0; i<len ; i++) {
                if (buf[i] == '\n') {
@@ -618,18 +649,11 @@ rdline_add_history(struct rdline * rdl, const char * buf)
 void
 rdline_clear_history(struct rdline * rdl)
 {
+       if (!rdl)
+               return;
        cirbuf_init(&rdl->history, rdl->history_buf, 0, RDLINE_HISTORY_BUF_SIZE);
 }
 
-#else /* !NO_RDLINE_HISTORY */
-
-int rdline_add_history(struct rdline * rdl, const char * buf) {return -1;}
-void rdline_clear_history(struct rdline * rdl) {}
-char * rdline_get_history_item(struct rdline * rdl, unsigned int i) {return NULL;}
-
-
-#endif /* !NO_RDLINE_HISTORY */
-
 
 /* STATIC USEFUL FUNCS */
 
index 5c73c4f..3bf9a7b 100644 (file)
@@ -125,17 +125,13 @@ struct rdline {
        char prompt[RDLINE_PROMPT_SIZE];
        unsigned int prompt_size;
 
-#ifndef NO_RDLINE_KILL_BUF
        char kill_buf[RDLINE_BUF_SIZE];
        unsigned int kill_size;
-#endif
 
-#ifndef NO_RDLINE_HISTORY
        /* history */
        struct cirbuf history;
        char history_buf[RDLINE_HISTORY_BUF_SIZE];
        int history_cur_line;
-#endif
 
        /* callbacks and func pointers */
        rdline_write_char_t *write_char;
@@ -159,7 +155,7 @@ struct rdline {
  * \param complete A pointer to the function to execute when the
  *                 user completes the buffer.
  */
-void rdline_init(struct rdline *rdl,
+int rdline_init(struct rdline *rdl,
                 rdline_write_char_t *write_char,
                 rdline_validate_t *validate,
                 rdline_complete_t *complete);
index d147dd6..a500835 100644 (file)
@@ -77,6 +77,11 @@ struct cmdline *
 cmdline_file_new(cmdline_parse_ctx_t *ctx, const char *prompt, const char *path)
 {
        int fd;
+
+       /* everything else is checked in cmdline_new() */
+       if (!path)
+               return NULL;
+
        fd = open(path, O_RDONLY, 0);
        if (fd < 0) {
                dprintf("open() failed\n");
@@ -102,7 +107,8 @@ cmdline_stdin_new(cmdline_parse_ctx_t *ctx, const char *prompt)
        cl = cmdline_new(ctx, prompt, 0, 1);
 
 #ifdef RTE_EXEC_ENV_LINUXAPP
-       memcpy(&cl->oldterm, &oldterm, sizeof(term));
+       if (cl)
+               memcpy(&cl->oldterm, &oldterm, sizeof(term));
 #endif
        return cl;
 }
@@ -110,6 +116,9 @@ cmdline_stdin_new(cmdline_parse_ctx_t *ctx, const char *prompt)
 void
 cmdline_stdin_exit(struct cmdline *cl)
 {
+       if (!cl)
+               return;
+
 #ifdef RTE_EXEC_ENV_LINUXAPP
        tcsetattr(fileno(stdin), TCSANOW, &cl->oldterm);
 #else
index 7468cd0..145a9c6 100644 (file)
@@ -101,6 +101,8 @@ const char *cmdline_vt100_commands[] = {
 void
 vt100_init(struct cmdline_vt100 *vt)
 {
+       if (!vt)
+               return;
        vt->state = CMDLINE_VT100_INIT;
 }
 
@@ -131,6 +133,9 @@ vt100_parser(struct cmdline_vt100 *vt, char ch)
        unsigned int size;
        uint8_t c = (uint8_t) ch;
 
+       if (!vt)
+               return -1;
+
        if (vt->bufpos >= CMDLINE_VT100_BUF_SIZE) {
                vt->state = CMDLINE_VT100_INIT;
                vt->bufpos = 0;