X-Git-Url: http://git.droids-corp.org/?p=libcmdline.git;a=blobdiff_plain;f=src%2Flib%2Fcmdline_parse.c;h=aec9e804f451e493bd8d84a7f7b4c4fd40253a30;hp=85958d0e4a9e9917b83c607e5e3f0574f2f13e7b;hb=c60ed50ac1449a2b2bccc33598d1f68458a36fb1;hpb=6f32a68393e01b4179592b9f48255179e8ad55f7 diff --git a/src/lib/cmdline_parse.c b/src/lib/cmdline_parse.c index 85958d0..aec9e80 100644 --- a/src/lib/cmdline_parse.c +++ b/src/lib/cmdline_parse.c @@ -44,7 +44,7 @@ static int isblank2(char c) { - if (c == ' ' || + if (c == ' ' || c == '\t' ) return 1; return 0; @@ -53,7 +53,7 @@ isblank2(char c) static int isendofline(char c) { - if (c == '\n' || + if (c == '\n' || c == '\r' ) return 1; return 0; @@ -88,13 +88,13 @@ nb_common_chars(const char * s1, const char * s2) 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; @@ -106,7 +106,7 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf, unsigned int nb_match_to 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 || iparse(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) { @@ -153,7 +153,7 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf, unsigned int nb_match_to while (isblank2(*buf)) { buf++; } - + /* end of buf */ if ( isendofline(*buf) || iscomment(*buf) ) return 0; @@ -182,9 +182,9 @@ cmdline_parse(struct cmdline *cl, const char * buf) 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; @@ -236,7 +236,7 @@ cmdline_parse(struct cmdline *cl, const char * buf) while (isblank2(*curbuf)) { curbuf++; } - + /* if end of buf -> there is no garbage after inst */ if (isendofline(*curbuf) || iscomment(*curbuf)) { if (!f) { @@ -252,11 +252,11 @@ cmdline_parse(struct cmdline *cl, const char * buf) } } } - + inst_num ++; inst = ctx[inst_num]; } - + /* call func */ if (f) { f(result_buf, cl, data); @@ -267,12 +267,12 @@ cmdline_parse(struct cmdline *cl, const char * buf) 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; @@ -309,22 +309,22 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state, 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; @@ -341,16 +341,16 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state, 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]; @@ -361,15 +361,15 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state, /* 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; } @@ -387,18 +387,18 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state, 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++; @@ -441,7 +441,7 @@ cmdline_complete(struct cmdline *cl, const char *buf, int *state, else snprintf(dst+l, size-l, "[%s]: No help", tmpbuf); } - + return 1; } }