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;
}
}