cmdline: display help and completion on different lines
authorOlivier Matz <zer0@droids-corp.org>
Fri, 18 Feb 2011 17:18:36 +0000 (18:18 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Sun, 13 Mar 2011 10:09:24 +0000 (11:09 +0100)
Signed-off-by: Olivier Matz <zer0@droids-corp.org>
src/lib/cmdline_parse.c

index 2822dcb..8e73e65 100644 (file)
@@ -470,7 +470,7 @@ int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
        char helpbuf[CMDLINE_MAX_DSTBUF_SIZE];
        char tmpbuf[CMDLINE_MAX_DSTBUF_SIZE];
        char *help_str;
-       int n, len, iterate, iterate_count;
+       int n, iterate;
 
        cmdline_preparse(&preparse, buf);
 
@@ -512,10 +512,27 @@ int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
                                                         &preparse.opaque);
                }
 
+               /* get token dynamic help string */
+               if ((token->ops->help == NULL) ||
+                   (token->ops->help(token, tmpbuf, sizeof(tmpbuf)) < 0))
+                       snprintf(tmpbuf, sizeof(tmpbuf), "unknown");
+
+               /* get instruction static help string */
+               help_str = inst->help_str;
+               if (help_str == NULL)
+                       help_str = "No help";
+
+               /* send it to callback function */
+               n = snprintf(helpbuf, sizeof(helpbuf),
+                            "[%s]: %s\n", tmpbuf, help_str);
+               if (n >= 0 && n < sizeof(helpbuf))
+                       write_buf(opaque, helpbuf, n);
+
+               if (iterate == 0)
+                       continue;
+
                /* iterate over all possible completion for this inst */
-               iterate_count = 0;
-               while (iterate == 1 &&
-                      token->ops->complete_iterate(token,
+               while (token->ops->complete_iterate(token,
                                                    &preparse.opaque,
                                                    tmpbuf,
                                                    sizeof(tmpbuf)) == 0) {
@@ -530,53 +547,14 @@ int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf,
                                continue;
 
                        /* get the token and add it in help buffer */
-                       n = snprintf(helpbuf, sizeof(helpbuf), "%s ", tmpbuf);
-                       if (n < 0 || n >= sizeof(helpbuf))
-                               continue;
-                       len = n;
-
-                       iterate_count++;
-
-                       /* get token dynamic help */
-                       if ((token->ops->help == NULL) ||
-                           (token->ops->help(token, tmpbuf, sizeof(tmpbuf)) < 0))
-                               snprintf(tmpbuf, sizeof(tmpbuf), "unknown");
-
-                       /* get instruction static help string */
-                       help_str = inst->help_str;
-                       if (help_str == NULL)
-                               help_str = "No help";
-
-                       /* send it to callback function */
-                       n = snprintf(helpbuf + len, sizeof(helpbuf) - len,
-                                    "[%s]: %s\n", tmpbuf, help_str);
-                       if (n >= 0)
-                               write_buf(opaque, helpbuf, n + len);
+                       n = snprintf(helpbuf, sizeof(helpbuf), "  %s\n", tmpbuf);
+                       if (n >= 0 && n < sizeof(helpbuf))
+                               write_buf(opaque, helpbuf, n);
                }
 
                /* no more completion, go to next inst */
-               if (iterate == 1 && token->ops->complete_end != NULL)
+               if ( token->ops->complete_end != NULL)
                        token->ops->complete_end(token, &preparse.opaque);
-
-               if (iterate == 0 || iterate_count == 0) {
-                       /* get token dynamic help string */
-                       if ((token->ops->help == NULL) ||
-                           (token->ops->help(token, tmpbuf, sizeof(tmpbuf)) < 0))
-                               snprintf(tmpbuf, sizeof(tmpbuf), "unknown");
-
-                       /* get instruction static help string */
-                       help_str = inst->help_str;
-                       if (help_str == NULL)
-                               help_str = "No help";
-
-                       /* send it to callback function */
-                       n = snprintf(helpbuf, sizeof(helpbuf),
-                                    "[%s]: %s\n", tmpbuf, help_str);
-                       if (n >= 0)
-                               write_buf(opaque, helpbuf, n);
-
-                       continue;
-               }
        }
 
        return 0;