cmdline: fix completion in some conditions (partial token len > 1)
[libcmdline.git] / src / lib / cmdline_parse.c
index 8e73e65..18ea76e 100644 (file)
@@ -502,8 +502,15 @@ int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
                else
                        iterate = 1;
 
+               /* store the incomplete token in tmpbuf */
+               n = preparse.comp_tok_len + 1;
+               if (n > sizeof(tmpbuf))
+                       n = sizeof(tmpbuf);
+               snprintf(tmpbuf, n, "%s", preparse.comp_tok_buf);
+
                if (iterate == 1 &&
-                   token->ops->complete_start(token, &preparse.opaque) < 0) {
+                   token->ops->complete_start(token, tmpbuf,
+                                              &preparse.opaque) < 0) {
                        /* cancel iteration, complete_start() returned
                         * a negative value, meaning no completion  */
                        iterate = 0;
@@ -605,8 +612,15 @@ cmdline_complete(cmdline_parse_ctx_t *ctx, const char *buf,
                    token->ops->complete_iterate == NULL)
                        continue;
 
+               /* store the incomplete token in tmpbuf */
+               n = preparse.comp_tok_len + 1;
+               if (n > sizeof(tmpbuf))
+                       n = sizeof(tmpbuf);
+               snprintf(tmpbuf, n, "%s", preparse.comp_tok_buf);
+
                /* non completable */
-               if (token->ops->complete_start(token, &preparse.opaque) < 0) {
+               if (token->ops->complete_start(token, tmpbuf,
+                                              &preparse.opaque) < 0) {
                        if (token->ops->complete_end != NULL)
                                token->ops->complete_end(token,
                                                         &preparse.opaque);
@@ -679,8 +693,8 @@ cmdline_complete(cmdline_parse_ctx_t *ctx, const char *buf,
                if (completion_len >= dstsize)
                        completion_len = dstsize - 1;
                strncpy(dst, completion_buf + preparse.comp_tok_len,
-                       completion_len);
-               dst[completion_len] = '\0';
+                       completion_len - preparse.comp_tok_len);
+               dst[completion_len - preparse.comp_tok_len] = '\0';
                return CMDLINE_COMPLETE_APPEND;
        }