cmdline: display "[Return]" when there is no more token to complete
[libcmdline.git] / src / lib / cmdline_parse.c
index 8e73e65..ceb22d6 100644 (file)
@@ -312,7 +312,7 @@ match_inst(cmdline_parse_inst_t *inst, const char *linebuf,
                linebuf++;
 
        /* end of buf, we match all inst  */
-       if (isendofline(*linebuf) || iscomment(*linebuf))
+       if (*linebuf == '\0' || isendofline(*linebuf) || iscomment(*linebuf))
                return 0;
 
        /* garbage after inst */
@@ -489,7 +489,7 @@ int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
 
                /* end of inst */
                if (token == NULL) {
-                       n = snprintf(helpbuf, sizeof(helpbuf), "[RETURN]\n");
+                       n = snprintf(helpbuf, sizeof(helpbuf), "[Return]\n");
                        if (n > 0)
                                write_buf(opaque, helpbuf, n);
                        continue;
@@ -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;
@@ -535,7 +542,7 @@ int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
                while (token->ops->complete_iterate(token,
                                                    &preparse.opaque,
                                                    tmpbuf,
-                                                   sizeof(tmpbuf)) == 0) {
+                                                   sizeof(tmpbuf)) >= 0) {
 
 
                        debug_printf("   choice <%s>\n", tmpbuf);
@@ -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);
@@ -627,7 +641,6 @@ cmdline_complete(cmdline_parse_ctx_t *ctx, const char *buf,
                        debug_printf("Completion %s\n", tmpbuf);
 
                        /* we kept at least the room for one char */
-                       /* XXX to be updated for non-final completion */
                        if (ret == 0)
                                strcat(tmpbuf, " ");
 
@@ -679,8 +692,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;
        }