- Support of float number is removed.
- C++ compatibility is done
- More checks
Signed-off-by: Intel
# Compile librte_cmdline
#
CONFIG_RTE_LIBRTE_CMDLINE=y
+CONFIG_RTE_LIBRTE_CMDLINE_DEBUG=n
#
# Compile librte_hash
# Compile librte_cmdline
#
CONFIG_RTE_LIBRTE_CMDLINE=y
+CONFIG_RTE_LIBRTE_CMDLINE_DEBUG=n
#
# Compile librte_hash
# Compile librte_cmdline
#
CONFIG_RTE_LIBRTE_CMDLINE=y
+CONFIG_RTE_LIBRTE_CMDLINE_DEBUG=n
#
# Compile librte_hash
# Compile librte_cmdline
#
CONFIG_RTE_LIBRTE_CMDLINE=y
+CONFIG_RTE_LIBRTE_CMDLINE_DEBUG=n
#
# Compile librte_hash
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);
void
cmdline_set_prompt(struct cmdline *cl, const char *prompt)
{
+ if (!cl || !prompt)
+ return;
rte_snprintf(cl->prompt, sizeof(cl->prompt), "%s", prompt);
}
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;
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)
{
va_list ap;
+ if (!cl || !fmt)
+ return;
+
#ifdef _GNU_SOURCE
if (cl->s_out < 0)
return;
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]);
void
cmdline_quit(struct cmdline *cl)
{
+ if (!cl)
+ return;
rdline_quit(&cl->rdl);
}
{
char c;
+ if (!cl)
+ return;
+
c = -1;
while (1) {
if (read(cl->s_in, &c, 1) < 0)
#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 */
{
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;
{
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;
}
/* 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);
__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);
__cirbuf_shift_right(cbuf);
}
}
+
+ return 0;
}
/* buffer del */
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;
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;
{
unsigned int n;
+ if (!cbuf || !c)
+ return -EINVAL;
+
n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
if (!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);
+ /* 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;
}
{
unsigned int n;
+ if (!cbuf || !c)
+ return -EINVAL;
+
n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
if (!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);
+ /* 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;
}
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)
/**
* 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);
/**
/**
* 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
}
{
unsigned int i=0;
- while (*s1==*s2 && *s1 && *s2) {
+ while (*s1==*s2 && *s1) {
s1++;
s2++;
i++;
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
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));
}
}
next:
+ debug_printf("next\n");
inst_num ++;
inst = ctx[inst_num];
}
.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
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);
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;
}
extern "C" {
#endif
-struct cmdline_token_etheraddr_data {
- uint8_t flags;
-};
-
struct cmdline_token_etheraddr {
struct cmdline_token_hdr hdr;
};
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
#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
{
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;
curtok = src;
saw_xdigit = count_xdigit = 0;
val = 0;
+
while ((ch = *src++) != '\0') {
const char *pch;
saw_xdigit = 0;
count_xdigit = 0;
val = 0;
+ dbloct_count++;
continue;
}
if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
tp += INADDRSZ;
saw_xdigit = 0;
count_xdigit = 0;
+ dbloct_count += 2;
break; /* '\0' was seen by inet_pton4(). */
}
return (0);
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.
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++;
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;
}
/* 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;
}
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:
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
DEC_NEG,
BIN,
HEX,
- FLOAT_POS,
- FLOAT_NEG,
ERROR,
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
}
-/* 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));
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;
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;
else
st = DEC_NEG_OK;
}
-#ifdef CMDLINE_HAVE_FLOAT
- else if (c == '.') {
- res1 = 0;
- st = FLOAT_NEG;
- }
-#endif
else {
st = ERROR;
}
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;
}
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;
}
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;
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;
}
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;
}
-/* 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));
/* 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;
}
INT16,
INT32,
INT64
-#ifndef NO_PARSE_FLOAT
- ,FLOAT
-#endif
};
struct cmdline_token_num_data {
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
{
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++;
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;
}
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;
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
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;
}
/* unspecified string */
else {
- token_len=0;
+ token_len = 0;
while(!cmdline_isendoftoken(buf[token_len]) &&
token_len < (STR_TOKEN_SIZE-1))
token_len++;
*((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;
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)
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) {
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
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
+#include <errno.h>
#include <ctype.h>
#include "cmdline_cirbuf.h"
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
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
{
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);
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);
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]);
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;
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))
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))
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)) &&
}
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)) &&
}
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);
}
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))) {
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);
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);
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) <
}
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);
}
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);
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) {
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)
rdline_redisplay(rdl);
break;
-#endif /* !NO_RDLINE_HISTORY */
default:
/* HISTORY */
-#ifndef NO_RDLINE_HISTORY
static void
rdline_remove_old_history_item(struct rdline * rdl)
{
{
unsigned int len, i, tmp;
+ if (!rdl)
+ return NULL;
+
len = rdline_get_history_size(rdl);
if ( idx >= len ) {
return NULL;
{
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') {
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 */
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;
* \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);
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");
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;
}
void
cmdline_stdin_exit(struct cmdline *cl)
{
+ if (!cl)
+ return;
+
#ifdef RTE_EXEC_ENV_LINUXAPP
tcsetattr(fileno(stdin), TCSANOW, &cl->oldterm);
#else
void
vt100_init(struct cmdline_vt100 *vt)
{
+ if (!vt)
+ return;
vt->state = CMDLINE_VT100_INIT;
}
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;