/**********************/
-void
-cmdline_valid_buffer(struct rdline *rdl, const char *buf, unsigned int size)
+void
+cmdline_valid_buffer(struct rdline *rdl, const char *buf, unsigned int size)
{
struct cmdline *cl = rdl->opaque;
int ret;
cmdline_printf(cl, "Bad arguments\n");
}
-int
+int
cmdline_complete_buffer(struct rdline *rdl, const char *buf,
char *dstbuf, unsigned int dstsize,
int *state)
void
cmdline_set_prompt(struct cmdline *cl, const char *prompt)
{
- snprintf(cl->prompt, sizeof(cl->prompt), prompt);
+ snprintf(cl->prompt, sizeof(cl->prompt), prompt);
}
struct cmdline *
cl->s_out = s_out;
cl->ctx = ctx;
- rdline_init(&cl->rdl, cmdline_write_char,
+ rdline_init(&cl->rdl, cmdline_write_char,
cmdline_valid_buffer, cmdline_complete_buffer);
cl->rdl.opaque = cl;
cmdline_set_prompt(cl, prompt);
{
va_list ap;
char buffer[BUFSIZ];
-
+
va_start(ap, fmt);
vsnprintf(buffer, 512, fmt, ap);
va_end(ap);
#include "cmdline_cirbuf.h"
-void
+void
cirbuf_init(struct cirbuf *cbuf, char *buf, unsigned int start, unsigned int maxlen)
{
cbuf->maxlen = maxlen;
return -EINVAL;
e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
-
+
if (n < cbuf->start + e) {
dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->start - n + e, n);
memcpy(cbuf->buf + cbuf->start - n + e, c, n);
return -EINVAL;
e = CIRBUF_IS_EMPTY(cbuf) ? 1 : 0;
-
+
if (n < cbuf->maxlen - cbuf->end - 1 + e) {
dprintf("s[%d] -> d[%d] (%d)\n", 0, cbuf->end + !e, n);
memcpy(cbuf->buf + cbuf->end + !e, c, n);
/* add at head */
-static inline void
+static inline void
__cirbuf_add_head(struct cirbuf * cbuf, char c)
{
if (!CIRBUF_IS_EMPTY(cbuf)) {
cbuf->len ++;
}
-int
+int
cirbuf_add_head_safe(struct cirbuf * cbuf, char c)
{
if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
- __cirbuf_add_head(cbuf, c);
+ __cirbuf_add_head(cbuf, c);
return 0;
}
return -EINVAL;
void
cirbuf_add_head(struct cirbuf * cbuf, char c)
{
- __cirbuf_add_head(cbuf, c);
+ __cirbuf_add_head(cbuf, c);
}
/* add at tail */
-static inline void
+static inline void
__cirbuf_add_tail(struct cirbuf * cbuf, char c)
{
if (!CIRBUF_IS_EMPTY(cbuf)) {
cbuf->len ++;
}
-int
+int
cirbuf_add_tail_safe(struct cirbuf * cbuf, char c)
{
if (cbuf && !CIRBUF_IS_FULL(cbuf)) {
- __cirbuf_add_tail(cbuf, c);
+ __cirbuf_add_tail(cbuf, c);
return 0;
}
return -EINVAL;
void
cirbuf_add_tail(struct cirbuf * cbuf, char c)
{
- __cirbuf_add_tail(cbuf, c);
+ __cirbuf_add_tail(cbuf, c);
}
char tmp = cbuf->buf[cbuf->start];
for (i=0 ; i<cbuf->len ; i++) {
- cbuf->buf[(cbuf->start+i)%cbuf->maxlen] =
+ cbuf->buf[(cbuf->start+i)%cbuf->maxlen] =
cbuf->buf[(cbuf->start+i+1)%cbuf->maxlen];
}
cbuf->buf[(cbuf->start-1+cbuf->maxlen)%cbuf->maxlen] = tmp;
char tmp = cbuf->buf[cbuf->end];
for (i=0 ; i<cbuf->len ; i++) {
- cbuf->buf[(cbuf->end+cbuf->maxlen-i)%cbuf->maxlen] =
+ cbuf->buf[(cbuf->end+cbuf->maxlen-i)%cbuf->maxlen] =
cbuf->buf[(cbuf->end+cbuf->maxlen-i-1)%cbuf->maxlen];
}
cbuf->buf[(cbuf->end+1)%cbuf->maxlen] = tmp;
cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size)
{
unsigned int n = (size < CIRBUF_GET_LEN(cbuf)) ? size : CIRBUF_GET_LEN(cbuf);
-
+
if (!n)
return 0;
/* get head or get tail */
-char
+char
cirbuf_get_head(struct cirbuf * cbuf)
{
return cbuf->buf[cbuf->start];
/* get head or get tail */
-char
+char
cirbuf_get_tail(struct cirbuf * cbuf)
{
return cbuf->buf[cbuf->end];
#define CIRBUF_GET_MAXLEN(cirbuf) ((cirbuf)->maxlen)
/**
- * return the number of free elts
+ * return the number of free elts
*/
#define CIRBUF_GET_FREELEN(cirbuf) ((cirbuf)->maxlen - (cirbuf)->len)
i ++, e=(c)->buf[((c)->start+i)%((c)->maxlen)])
-/**
+/**
* Add a character at head of the circular buffer. Return 0 on success, or
* a negative value on error.
*/
int cirbuf_add_head_safe(struct cirbuf *cbuf, char c);
-/**
+/**
* Add a character at head of the circular buffer. You _must_ check that you
* have enough free space in the buffer before calling this func.
*/
void cirbuf_add_head(struct cirbuf *cbuf, char c);
-/**
+/**
* Add a character at tail of the circular buffer. Return 0 on success, or
* a negative value on error.
*/
int cirbuf_add_tail_safe(struct cirbuf *cbuf, char c);
-/**
+/**
* Add a character at tail of the circular buffer. You _must_ check that you
* have enough free space in the buffer before calling this func.
*/
void cirbuf_add_tail(struct cirbuf *cbuf, char c);
-/**
+/**
* Remove a char at the head of the circular buffer. Return 0 on
* success, or a negative value on error.
*/
int cirbuf_del_head_safe(struct cirbuf *cbuf);
-/**
+/**
* Remove a char at the head of the circular buffer. You _must_ check
* that buffer is not empty before calling the function.
*/
void cirbuf_del_head(struct cirbuf *cbuf);
-/**
+/**
* Remove a char at the tail of the circular buffer. Return 0 on
* success, or a negative value on error.
*/
int cirbuf_del_tail_safe(struct cirbuf *cbuf);
-/**
+/**
* Remove a char at the tail of the circular buffer. You _must_ check
* that buffer is not empty before calling the function.
*/
*/
char cirbuf_get_tail(struct cirbuf *cbuf);
-/**
+/**
* Add a buffer at head of the circular buffer. 'c' is a pointer to a
* buffer, and n is the number of char to add. Return the number of
* copied bytes on success, or a negative value on error.
*/
int cirbuf_add_buf_head(struct cirbuf *cbuf, const char *c, unsigned int n);
-/**
+/**
* Add a buffer at tail of the circular buffer. 'c' is a pointer to a
* buffer, and n is the number of char to add. Return the number of
* copied bytes on success, or a negative value on error.
*/
int cirbuf_add_buf_tail(struct cirbuf *cbuf, const char *c, unsigned int n);
-/**
+/**
* Remove chars at the head of the circular buffer. Return 0 on
* success, or a negative value on error.
*/
int cirbuf_del_buf_head(struct cirbuf *cbuf, unsigned int size);
-/**
+/**
* Remove chars at the tail of the circular buffer. Return 0 on
* success, or a negative value on error.
*/
int cirbuf_del_buf_tail(struct cirbuf *cbuf, unsigned int size);
-/**
+/**
* Copy a maximum of 'size' characters from the head of the circular
* buffer to a flat one pointed by 'c'. Return the number of copied
* chars.
*/
int cirbuf_get_buf_head(struct cirbuf *cbuf, char *c, unsigned int size);
-/**
+/**
* Copy a maximum of 'size' characters from the tail of the circular
* buffer to a flat one pointed by 'c'. Return the number of copied
* chars.
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);
-/**
+/**
* Set the end of the data to the last index of the internal buffer.
*/
void cirbuf_align_right(struct cirbuf *cbuf);
static int
isblank2(char c)
{
- if (c == ' ' ||
+ if (c == ' ' ||
c == '\t' )
return 1;
return 0;
static int
isendofline(char c)
{
- if (c == '\n' ||
+ if (c == '\n' ||
c == '\r' )
return 1;
return 0;
return i;
}
-/**
+/**
* try to match the buffer with an instruction (only the first
* nb_match_token tokens if != 0). Return 0 if we match all the
* tokens, else the number of matched tokens, else -1.
*/
static int
-match_inst(cmdline_parse_inst_t *inst, const char *buf, unsigned int nb_match_token,
+match_inst(cmdline_parse_inst_t *inst, const char *buf, unsigned int nb_match_token,
void * result_buf)
{
unsigned int token_num=0;
token_p = inst->tokens[token_num];
if (token_p)
memcpy(&token_hdr, token_p, sizeof(token_hdr));
-
+
/* check if we match all tokens of inst */
while (token_p && (!nb_match_token || i<nb_match_token)) {
debug_printf("TK\n");
while (isblank2(*buf)) {
buf++;
}
-
+
/* end of buf */
if ( isendofline(*buf) || iscomment(*buf) )
break;
-
+
n = token_hdr.ops->parse(token_p, buf, (result_buf ? result_buf+token_hdr.offset : NULL));
if ( n < 0 )
break;
debug_printf("TK parsed (len=%d)\n", n);
i++;
buf += n;
-
+
token_num ++;
token_p = inst->tokens[token_num];
if (token_p)
memcpy(&token_hdr, token_p, sizeof(token_hdr));
}
-
+
/* does not match */
if (i==0)
return -1;
-
+
/* in case we want to match a specific num of token */
if (nb_match_token) {
if (i == nb_match_token) {
while (isblank2(*buf)) {
buf++;
}
-
+
/* end of buf */
if ( isendofline(*buf) || iscomment(*buf) )
return 0;
char debug_buf[BUFSIZ];
#endif
- /*
+ /*
* - look if the buffer contains at least one line
- * - look if line contains only spaces or comments
+ * - look if line contains only spaces or comments
* - count line length
*/
curbuf = buf;
while (isblank2(*curbuf)) {
curbuf++;
}
-
+
/* if end of buf -> there is no garbage after inst */
if (isendofline(*curbuf) || iscomment(*curbuf)) {
if (!f) {
}
}
}
-
+
inst_num ++;
inst = ctx[inst_num];
}
-
+
/* call func */
if (f) {
f(result_buf, cl, data);
debug_printf("No match err=%d\n", err);
return err;
}
-
+
return linelen;
}
-int
-cmdline_complete(struct cmdline *cl, const char *buf, int *state,
+int
+cmdline_complete(struct cmdline *cl, const char *buf, int *state,
char *dst, unsigned int size)
{
const char *incomplete_token = buf;
nb_completable = 0;
nb_non_completable = 0;
-
+
inst = ctx[inst_num];
while (inst) {
/* parse the first tokens of the inst */
if (nb_token && match_inst(inst, buf, nb_token, NULL))
goto next;
-
+
debug_printf("instruction match \n");
token_p = inst->tokens[nb_token];
if (token_p)
memcpy(&token_hdr, token_p, sizeof(token_hdr));
/* non completable */
- if (!token_p ||
- !token_hdr.ops->complete_get_nb ||
- !token_hdr.ops->complete_get_elt ||
+ if (!token_p ||
+ !token_hdr.ops->complete_get_nb ||
+ !token_hdr.ops->complete_get_elt ||
(n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
nb_non_completable++;
goto next;
if (completion_len == -1) {
strcpy(completion_buf, tmpbuf+incomplete_token_len);
completion_len = strlen(tmpbuf+incomplete_token_len);
-
+
}
else {
- completion_len = nb_common_chars(completion_buf,
+ completion_len = nb_common_chars(completion_buf,
tmpbuf+incomplete_token_len);
completion_buf[completion_len] = 0;
}
nb_completable++;
}
- }
+ }
next:
inst_num ++;
inst = ctx[inst_num];
/* no possible completion */
if (nb_completable == 0 && nb_non_completable == 0)
return 0;
-
+
/* if multichoice is not required */
if (*state == 0 && incomplete_token_len > 0) {
/* one or several choices starting with the
same chars */
- if (completion_len > 0) {
+ if (completion_len > 0) {
if (completion_len + 1 > size)
return 0;
-
+
strcpy(dst, completion_buf);
return 2;
}
while (inst) {
/* we need to redo it */
inst = ctx[inst_num];
-
+
if (nb_token && match_inst(inst, buf, nb_token, NULL))
goto next2;
-
+
token_p = inst->tokens[nb_token];
if (token_p)
memcpy(&token_hdr, token_p, sizeof(token_hdr));
/* one choice for this token */
- if (!token_p ||
- !token_hdr.ops->complete_get_nb ||
- !token_hdr.ops->complete_get_elt ||
+ if (!token_p ||
+ !token_hdr.ops->complete_get_nb ||
+ !token_hdr.ops->complete_get_elt ||
(n = token_hdr.ops->complete_get_nb(token_p)) == 0) {
if (local_state < *state) {
local_state++;
else
snprintf(dst+l, size-l, "[%s]: No help", tmpbuf);
}
-
+
return 1;
}
}
* parsed chars on success and a negative value on error.
*
* complete_get_nb() returns the number of possible values for this
- * token if completion is possible. If it is NULL or if it returns 0,
+ * token if completion is possible. If it is NULL or if it returns 0,
* no completion is possible.
*
* complete_get_elt() copy in dstbuf (the size is specified in the
int (*complete_get_elt)(cmdline_parse_token_hdr_t *, int, char *, unsigned int);
/** get help for this token (token, dstbuf, size) */
int (*get_help)(cmdline_parse_token_hdr_t *, char *, unsigned int);
-};
+};
struct cmdline;
/**
/**
* A context is identified by its name, and contains a list of
- * instruction
+ * instruction
*
*/
typedef cmdline_parse_inst_t *cmdline_parse_ctx_t;
* modifying *state until it returns CMDLINE_PARSE_COMPLETED_BUFFER or
* CMDLINE_PARSE_COMPLETED_BUFFER.
*
- * It returns < 0 on error.
+ * It returns < 0 on error.
*
* Else it returns:
* - CMDLINE_PARSE_COMPLETED_BUFFER on completion (one possible
#define ETHER_ADDRSTRLEN 18
-int
+int
cmdline_parse_etheraddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
{
unsigned int token_len = 0;
while (!cmdline_isendoftoken(buf[token_len]))
token_len++;
-
+
/* if token is too big... */
if (token_len >= ETHER_ADDRSTRLEN)
return -1;
};
-int
+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;
while (!cmdline_isendoftoken(buf[token_len]))
token_len++;
-
+
/* if token is too big... */
if (token_len >= INET6_ADDRSTRLEN+4)
return -1;
ipaddr->prefixlen = prefixlen;
}
else {
- ipaddr->prefixlen = 0;
+ ipaddr->prefixlen = 0;
}
/* convert the IP addr */
return token_len;
}
return -1;
-
+
}
int cmdline_get_help_ipaddr(cmdline_parse_token_hdr_t *tk, char *dstbuf,
.offset = offsetof(structure, field), \
}, \
.ipaddr_data = { \
- .flags = CMDLINE_IPADDR_V4 | \
- CMDLINE_IPADDR_V6, \
+ .flags = CMDLINE_IPADDR_V4 | \
+ CMDLINE_IPADDR_V6, \
}, \
}
.offset = offsetof(structure, field), \
}, \
.ipaddr_data = { \
- .flags = CMDLINE_IPADDR_V4, \
+ .flags = CMDLINE_IPADDR_V4, \
}, \
}
.offset = offsetof(structure, field), \
}, \
.ipaddr_data = { \
- .flags = CMDLINE_IPADDR_V6, \
+ .flags = CMDLINE_IPADDR_V6, \
}, \
}
.offset = offsetof(structure, field), \
}, \
.ipaddr_data = { \
- .flags = CMDLINE_IPADDR_V4 | \
- CMDLINE_IPADDR_V6 | \
- CMDLINE_IPADDR_NETWORK, \
+ .flags = CMDLINE_IPADDR_V4 | \
+ CMDLINE_IPADDR_V6 | \
+ CMDLINE_IPADDR_NETWORK, \
}, \
}
.offset = offsetof(structure, field), \
}, \
.ipaddr_data = { \
- .flags = CMDLINE_IPADDR_V4 | \
- CMDLINE_IPADDR_NETWORK, \
+ .flags = CMDLINE_IPADDR_V4 | \
+ CMDLINE_IPADDR_NETWORK, \
}, \
}
.offset = offsetof(structure, field), \
}, \
.ipaddr_data = { \
- .flags = CMDLINE_IPADDR_V4 | \
- CMDLINE_IPADDR_NETWORK, \
+ .flags = CMDLINE_IPADDR_V4 | \
+ CMDLINE_IPADDR_NETWORK, \
}, \
}
-
-
#endif /* _PARSE_IPADDR_H_ */
#endif
};
-static inline int
+static inline int
add_to_res(unsigned int c, unsigned int *res, unsigned int base)
{
/* overflow */
/* parse an int or a float */
-int
+int
cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
{
struct cmdline_token_num_data nd;
st = ERROR;
else
st = OCTAL_OK;
- }
+ }
else {
st = ERROR;
}
break;
case BIN:
- st = BIN_OK;
+ st = BIN_OK;
/* no break */
case BIN_OK:
if (c >= '0' && c <= '1') {
if (c >= '0' && c <= '9') {
if (add_to_res(c - '0', &res2, 10) < 0)
st = ERROR;
- else
+ else
st = FLOAT_POS_OK;
res3 = 10;
}
if (c >= '0' && c <= '9') {
if (add_to_res(c - '0', &res2, 10) < 0)
st = ERROR;
- else
+ else
st = FLOAT_NEG_OK;
res3 = 10;
}
default:
debug_printf("not impl ");
-
+
}
/* XXX uint32_t et %d */
if (buf-srcbuf > 127)
return -1;
}
-
+
switch (st) {
case ZERO_OK:
case DEC_POS_OK:
if (res)
*(float *)res = (float)res1 + ((float)res2 / (float)res3);
return (buf-srcbuf);
-
+
}
else {
return -1;
if (res)
*(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
return (buf-srcbuf);
-
+
}
else {
return -1;
/* parse an int or a float */
-int
+int
cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
{
struct cmdline_token_num_data nd;
memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
-
+
/* should not happen.... don't so this test */
/* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
/* return -1; */
extern struct cmdline_token_ops cmdline_token_num_ops;
-int cmdline_parse_num(cmdline_parse_token_hdr_t *tk,
+int cmdline_parse_num(cmdline_parse_token_hdr_t *tk,
const char *srcbuf, void *res);
-int cmdline_get_help_num(cmdline_parse_token_hdr_t *tk,
+int cmdline_get_help_num(cmdline_parse_token_hdr_t *tk,
char *dstbuf, unsigned int size);
#define TOKEN_NUM_INITIALIZER(structure, field, numtype) \
static unsigned int
get_token_len(const char *s)
{
- char c;
+ char c;
unsigned int i=0;
c = s[i];
- while (c!='#' && c!='\0') {
- i++;
+ while (c!='#' && c!='\0') {
+ i++;
c = s[i];
}
- return i;
+ return i;
}
static const char *
return NULL;
}
-int
+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;
if (! *buf)
return -1;
- /* fixed string */
- if (sd->str) {
+ /* fixed string */
+ if (sd->str) {
str = sd->str;
- do {
- token_len = get_token_len(str);
-
- /* if token is too big... */
- if (token_len >= STR_TOKEN_SIZE - 1) {
+ do {
+ token_len = get_token_len(str);
+
+ /* if token is too big... */
+ if (token_len >= STR_TOKEN_SIZE - 1) {
continue;
- }
-
- if ( strncmp(buf, str, token_len) ) {
+ }
+
+ if ( strncmp(buf, str, token_len) ) {
continue;
- }
-
- if ( !cmdline_isendoftoken(*(buf+token_len)) ) {
+ }
+
+ if ( !cmdline_isendoftoken(*(buf+token_len)) ) {
continue;
- }
+ }
- break;
- } while ( (str = get_next_token(str)) != NULL );
+ break;
+ } while ( (str = get_next_token(str)) != NULL );
if (!str)
return -1;
- }
- /* unspecified string */
- else {
- token_len=0;
- while(!cmdline_isendoftoken(buf[token_len]) &&
- token_len < (STR_TOKEN_SIZE-1))
- token_len++;
-
- /* return if token too long */
- if (token_len >= STR_TOKEN_SIZE - 1) {
- return -1;
- }
- }
-
+ }
+ /* unspecified string */
+ else {
+ token_len=0;
+ while(!cmdline_isendoftoken(buf[token_len]) &&
+ token_len < (STR_TOKEN_SIZE-1))
+ token_len++;
+
+ /* return if token too long */
+ if (token_len >= STR_TOKEN_SIZE - 1) {
+ return -1;
+ }
+ }
+
if (res) {
/* we are sure that token_len is < STR_TOKEN_SIZE-1 */
strncpy(res, buf, token_len);
*((char *)res + token_len) = 0;
}
- return token_len;
+ return token_len;
}
int cmdline_complete_get_nb_string(cmdline_parse_token_hdr_t *tk)
return ret;
}
-int cmdline_complete_get_elt_string(cmdline_parse_token_hdr_t *tk, int idx,
+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 *tk2 = (struct cmdline_token_string *)tk;
struct cmdline_token_string_data *sd = &tk2->string_data;;
const char *s;
-
+
s = sd->str;
if (s) {
else {
strncpy(dstbuf, ANYSTRING_HELP, size);
}
-
+
dstbuf[size-1] = '\0';
return 0;
int cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *srcbuf,
void *res);
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,
+int cmdline_complete_get_elt_string(cmdline_parse_token_hdr_t *tk, int idx,
char *dstbuf, unsigned int size);
int cmdline_get_help_string(cmdline_parse_token_hdr_t *tk, char *dstbuf,
unsigned int size);
.offset = offsetof(structure, field), \
}, \
.string_data = { \
- .str = string, \
+ .str = string, \
}, \
}
#include "cmdline_rdline.h"
static void rdline_puts(struct rdline *rdl, const char *buf);
-static void rdline_miniprintf(struct rdline *rdl,
+static void rdline_miniprintf(struct rdline *rdl,
const char *buf, unsigned int val);
#ifndef NO_RDLINE_HISTORY
static int
isblank2(char c)
{
- if (c == ' ' ||
+ if (c == ' ' ||
c == '\t' )
return 1;
return 0;
}
-void rdline_init(struct rdline *rdl,
+void rdline_init(struct rdline *rdl,
rdline_write_char_t *write_char,
rdline_validate_t *validate,
rdline_complete_t *complete)
rdl->write_char(rdl, rdl->prompt[i]);
rdl->status = RDLINE_RUNNING;
-#ifndef NO_RDLINE_HISTORY
+#ifndef NO_RDLINE_HISTORY
rdl->history_cur_line = -1;
#endif /* !NO_RDLINE_HISTORY */
}
-void
+void
rdline_stop(struct rdline *rdl)
{
rdl->status = RDLINE_INIT;
CIRBUF_FOREACH(&rdl->right, i, tmp) {
rdl->write_char(rdl, tmp);
}
- rdline_miniprintf(rdl, vt100_multi_left,
+ rdline_miniprintf(rdl, vt100_multi_left,
CIRBUF_GET_LEN(&rdl->right));
}
}
#ifndef NO_RDLINE_HISTORY
char *buf;
#endif
-
+
if (rdl->status != RDLINE_RUNNING)
return -1;
break;
case CMDLINE_KEY_WLEFT:
- while (! CIRBUF_IS_EMPTY(&rdl->left) &&
- (tmp = cirbuf_get_tail(&rdl->left)) &&
+ while (! CIRBUF_IS_EMPTY(&rdl->left) &&
+ (tmp = cirbuf_get_tail(&rdl->left)) &&
isblank2(tmp)) {
rdline_puts(rdl, vt100_left_arr);
cirbuf_del_tail(&rdl->left);
cirbuf_add_head(&rdl->right, tmp);
}
- while (! CIRBUF_IS_EMPTY(&rdl->left) &&
- (tmp = cirbuf_get_tail(&rdl->left)) &&
+ while (! CIRBUF_IS_EMPTY(&rdl->left) &&
+ (tmp = cirbuf_get_tail(&rdl->left)) &&
!isblank2(tmp)) {
rdline_puts(rdl, vt100_left_arr);
cirbuf_del_tail(&rdl->left);
cirbuf_add_head(&rdl->right, tmp);
- }
+ }
break;
case CMDLINE_KEY_WRIGHT:
- while (! CIRBUF_IS_EMPTY(&rdl->right) &&
- (tmp = cirbuf_get_head(&rdl->right)) &&
+ while (! CIRBUF_IS_EMPTY(&rdl->right) &&
+ (tmp = cirbuf_get_head(&rdl->right)) &&
isblank2(tmp)) {
rdline_puts(rdl, vt100_right_arr);
cirbuf_del_head(&rdl->right);
cirbuf_add_tail(&rdl->left, tmp);
}
- while (! CIRBUF_IS_EMPTY(&rdl->right) &&
- (tmp = cirbuf_get_head(&rdl->right)) &&
+ while (! CIRBUF_IS_EMPTY(&rdl->right) &&
+ (tmp = cirbuf_get_head(&rdl->right)) &&
!isblank2(tmp)) {
rdline_puts(rdl, vt100_right_arr);
cirbuf_del_head(&rdl->right);
cirbuf_add_tail(&rdl->left, tmp);
- }
+ }
break;
case CMDLINE_KEY_BKSPACE:
case CMDLINE_KEY_SUPPR:
case CMDLINE_KEY_CTRL_D:
- if (cmd == CMDLINE_KEY_CTRL_D &&
+ if (cmd == CMDLINE_KEY_CTRL_D &&
CIRBUF_IS_EMPTY(&rdl->left) &&
CIRBUF_IS_EMPTY(&rdl->right)) {
return -2;
case CMDLINE_KEY_CTRL_A:
if (CIRBUF_IS_EMPTY(&rdl->left))
break;
- rdline_miniprintf(rdl, vt100_multi_left,
+ rdline_miniprintf(rdl, vt100_multi_left,
CIRBUF_GET_LEN(&rdl->left));
while (! CIRBUF_IS_EMPTY(&rdl->left)) {
tmp = cirbuf_get_tail(&rdl->left);
case CMDLINE_KEY_CTRL_E:
if (CIRBUF_IS_EMPTY(&rdl->right))
break;
- rdline_miniprintf(rdl, vt100_multi_right,
+ rdline_miniprintf(rdl, vt100_multi_right,
CIRBUF_GET_LEN(&rdl->right));
while (! CIRBUF_IS_EMPTY(&rdl->right)) {
tmp = cirbuf_get_head(&rdl->right);
case CMDLINE_KEY_CTRL_Y:
i=0;
while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
- RDLINE_BUF_SIZE &&
+ RDLINE_BUF_SIZE &&
i < rdl->kill_size) {
cirbuf_add_tail(&rdl->left, rdl->kill_buf[i]);
rdl->write_char(rdl, rdl->kill_buf[i]);
case CMDLINE_KEY_TAB:
case CMDLINE_KEY_HELP:
cirbuf_align_left(&rdl->left);
- rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\0';
+ rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\0';
if (rdl->complete) {
char tmp_buf[BUFSIZ];
int complete_state;
if (ret == 2) {
i=0;
while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
- RDLINE_BUF_SIZE &&
+ RDLINE_BUF_SIZE &&
i < tmp_size) {
cirbuf_add_tail(&rdl->left, tmp_buf[i]);
rdl->write_char(rdl, tmp_buf[i]);
if (rdl->validate)
rdl->validate(rdl, rdl->left_buf, CIRBUF_GET_LEN(&rdl->left)+2);
return 1;
-
+
#ifndef NO_RDLINE_HISTORY
case CMDLINE_KEY_UP_ARR:
if (rdl->history_cur_line == 0) {
rdline_add_history(rdl, rdline_get_buffer(rdl));
rdl->history_cur_line = 0;
}
-
+
buf = rdline_get_history_item(rdl, rdl->history_cur_line + 1);
if (!buf)
break;
-
+
rdl->history_cur_line ++;
vt100_init(&rdl->vt100);
cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
case CMDLINE_KEY_DOWN_ARR:
if (rdl->history_cur_line - 1 < 0)
break;
-
+
rdl->history_cur_line --;
buf = rdline_get_history_item(rdl, rdl->history_cur_line);
if (!buf)
return 0;
}
-
+
if (! isprint(c))
return 0;
/* standard chars */
if (CIRBUF_GET_LEN(&rdl->left) + CIRBUF_GET_LEN(&rdl->right) >= RDLINE_BUF_SIZE)
return 0;
-
+
if (cirbuf_add_tail_safe(&rdl->left, c))
return 0;
return NULL;
}
-int
+int
rdline_add_history(struct rdline * rdl, const char * buf)
{
unsigned int len, i;
cirbuf_add_buf_tail(&rdl->history, buf, len);
cirbuf_add_tail(&rdl->history, 0);
-
+
return 0;
}
/* STATIC USEFUL FUNCS */
-static void
+static void
rdline_puts(struct rdline * rdl, const char * buf)
{
char c;
}
/* a very very basic printf with one arg and one format 'u' */
-static void
+static void
rdline_miniprintf(struct rdline *rdl, const char * buf, unsigned int val)
{
char c, started=0, div=100;
* microcontrollers (8 bits). Indeed, we don't use any malloc that is
* sometimes not implemented (or just not recommended) on such
* systems.
- *
+ *
* Obviously, it does not support as many things as the GNU readline,
* but at least it supports some interresting features like a kill
* buffer and a command history.
* time, even on a monothread program, since it works with callbacks.
*
* The lib is designed for a client-side or a server-side use:
- * - server-side: the server receives all data from a socket, including
- * control chars, like arrows, tabulations, ... The client is
+ * - server-side: the server receives all data from a socket, including
+ * control chars, like arrows, tabulations, ... The client is
* very simple, it can be a telnet or a minicom through a serial line.
* - client-side: the client receives its data through its stdin for
- * instance.
+ * instance.
*/
#include <cmdline_cirbuf.h>
* of your program.
* \param rdl A pointer to an uninitialized struct rdline
* \param write_char The function used by the function to write a character
- * \param validate A pointer to the function to execute when the
+ * \param validate A pointer to the function to execute when the
* user validates the buffer.
- * \param complete A pointer to the function to execute when the
+ * \param complete A pointer to the function to execute when the
* user completes the buffer.
*/
-void rdline_init(struct rdline *rdl,
+void rdline_init(struct rdline *rdl,
rdline_write_char_t *write_char,
rdline_validate_t *validate,
rdline_complete_t *complete);
/**
- * append a char to the readline buffer.
+ * append a char to the readline buffer.
* Return 1 when the line has been validated.
* Return 2 when the user asked to complete the buffer.
* Return -1 if it is not running.
bzero(&servAddr, sizeof(servAddr));
servAddr.sun_family = AF_UNIX;
memcpy(servAddr.sun_path, filename , strlen(filename));
-
+
unlink(filename);
if(bind(s, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) {
dprintf("bind() failed\n");
{
const char *cmd;
unsigned int i = 0;
-
+
for (i=0 ; i<sizeof(cmdline_vt100_commands)/sizeof(const char *) ; i++) {
cmd = *(cmdline_vt100_commands + i);
goto match_command;
}
break;
-
+
default:
vt->bufpos = 0;
break;
}
return -2;
-
+
match_command:
return match_command(vt->buf, size);
}
void vt100_init(struct cmdline_vt100 *vt);
/**
- * Input a new character.
+ * Input a new character.
* Return -1 if the character is not part of a control sequence
* Return -2 if c is not the last char of a control sequence
* Else return the index in vt100_commands[]