All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, pbonzini@redhat.com, stefanha@redhat.com,
	lcapitulino@redhat.com
Subject: [Qemu-devel] [PATCH 07/16] qemu-io: Factor out qemuio_command
Date: Tue, 28 May 2013 17:27:27 +0200	[thread overview]
Message-ID: <1369754856-30036-8-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1369754856-30036-1-git-send-email-kwolf@redhat.com>

It's duplicated code. Move it to qemu-io-cmds.c because it's not
dependent on any static data of the qemu-io tool.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 cmd.c          | 43 +++++--------------------------------------
 cmd.h          |  3 ++-
 qemu-io-cmds.c | 24 ++++++++++++++++++++++++
 3 files changed, 31 insertions(+), 39 deletions(-)

diff --git a/cmd.c b/cmd.c
index d501aab..7ae978f 100644
--- a/cmd.c
+++ b/cmd.c
@@ -138,28 +138,11 @@ static char *get_prompt(void);
 
 void command_loop(void)
 {
-    int c, i, done = 0, fetchable = 0, prompted = 0;
+    int i, done = 0, fetchable = 0, prompted = 0;
     char *input;
-    char **v;
-    const cmdinfo_t *ct;
 
     for (i = 0; !done && i < ncmdline; i++) {
-        input = strdup(cmdline[i]);
-        if (!input) {
-            fprintf(stderr, _("cannot strdup command '%s': %s\n"),
-                    cmdline[i], strerror(errno));
-            exit(1);
-        }
-        v = breakline(input, &c);
-        if (c) {
-            ct = find_command(v[0]);
-            if (ct) {
-                done = command(ct, c, v);
-            } else {
-                fprintf(stderr, _("command \"%s\" not found\n"), v[0]);
-            }
-	}
-        doneline(input, v);
+        done = qemuio_command(cmdline[i]);
     }
     if (cmdline) {
         g_free(cmdline);
@@ -179,20 +162,13 @@ void command_loop(void)
         if (!fetchable) {
             continue;
         }
+
         input = fetchline();
         if (input == NULL) {
             break;
         }
-        v = breakline(input, &c);
-        if (c) {
-            ct = find_command(v[0]);
-            if (ct) {
-                done = command(ct, c, v);
-            } else {
-                fprintf(stderr, _("command \"%s\" not found\n"), v[0]);
-            }
-        }
-        doneline(input, v);
+        done = qemuio_command(input);
+        free(input);
 
         prompted = 0;
         fetchable = 0;
@@ -328,15 +304,6 @@ char **breakline(char *input, int *count)
     return rval;
 }
 
-void
-doneline(
-	char	*input,
-	char	**vec)
-{
-	free(input);
-	free(vec);
-}
-
 #define EXABYTES(x)	((long long)(x) << 60)
 #define PETABYTES(x)	((long long)(x) << 50)
 #define TERABYTES(x)	((long long)(x) << 40)
diff --git a/cmd.h b/cmd.h
index ccf6336..d676408 100644
--- a/cmd.h
+++ b/cmd.h
@@ -59,7 +59,6 @@ int command(const cmdinfo_t *ci, int argc, char **argv);
 
 /* from input.h */
 char **breakline(char *input, int *count);
-void doneline(char *input, char **vec);
 char *fetchline(void);
 
 void cvtstr(double value, char *str, size_t sz);
@@ -77,4 +76,6 @@ void timestr(struct timeval *tv, char *str, size_t sz, int flags);
 
 extern char *progname;
 
+bool qemuio_command(const char *cmd);
+
 #endif	/* __COMMAND_H__ */
diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c
index 57b751e..bdfe5f2 100644
--- a/qemu-io-cmds.c
+++ b/qemu-io-cmds.c
@@ -1807,6 +1807,30 @@ static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
     return 1;
 }
 
+bool qemuio_command(const char *cmd)
+{
+    char *input;
+    const cmdinfo_t *ct;
+    char **v;
+    int c;
+    bool done = false;
+
+    input = g_strdup(cmd);
+    v = breakline(input, &c);
+    if (c) {
+        ct = find_command(v[0]);
+        if (ct) {
+            done = command(ct, c, v);
+        } else {
+            fprintf(stderr, "command \"%s\" not found\n", v[0]);
+        }
+    }
+    g_free(input);
+    g_free(v);
+
+    return done;
+}
+
 static void __attribute((constructor)) init_qemuio_commands(void)
 {
     /* initialize commands */
-- 
1.8.1.4

  parent reply	other threads:[~2013-05-28 15:29 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-28 15:27 [Qemu-devel] [PATCH 00/16] Make qemu-io commands available in the monitor Kevin Wolf
2013-05-28 15:27 ` [Qemu-devel] [PATCH 01/16] qemu-io: Remove unused args_command Kevin Wolf
2013-05-28 16:18   ` Eric Blake
2013-05-28 15:27 ` [Qemu-devel] [PATCH 02/16] cutils: Support 'P' and 'E' suffixes in strtosz() Kevin Wolf
2013-05-28 16:29   ` Eric Blake
2013-05-28 15:27 ` [Qemu-devel] [PATCH 03/16] qemu-io: Make cvtnum() a wrapper around strtosz_suffix() Kevin Wolf
2013-05-28 16:42   ` Eric Blake
2013-05-28 15:27 ` [Qemu-devel] [PATCH 04/16] qemu-io: Handle cvtnum() errors in 'alloc' Kevin Wolf
2013-05-28 16:53   ` Eric Blake
2013-05-28 15:27 ` [Qemu-devel] [PATCH 05/16] qemu-io: Don't use global bs in command implementations Kevin Wolf
2013-05-28 15:37   ` Kevin Wolf
2013-05-28 17:02     ` Eric Blake
2013-06-05 12:28   ` Stefan Hajnoczi
2013-06-05 12:39     ` Kevin Wolf
2013-05-28 15:27 ` [Qemu-devel] [PATCH 06/16] qemu-io: Split off commands to qemu-io-cmds.c Kevin Wolf
2013-05-29 20:29   ` Eric Blake
2013-06-04 10:11     ` Kevin Wolf
2013-05-28 15:27 ` Kevin Wolf [this message]
2013-05-29 21:11   ` [Qemu-devel] [PATCH 07/16] qemu-io: Factor out qemuio_command Eric Blake
2013-06-05 12:33   ` Stefan Hajnoczi
2013-05-28 15:27 ` [Qemu-devel] [PATCH 08/16] qemu-io: Move 'help' function Kevin Wolf
2013-05-29 21:25   ` Eric Blake
2013-06-04 10:20     ` Kevin Wolf
2013-06-05 12:37   ` Stefan Hajnoczi
2013-05-28 15:27 ` [Qemu-devel] [PATCH 09/16] qemu-io: Move 'quit' function Kevin Wolf
2013-05-29 22:11   ` Eric Blake
2013-05-28 15:27 ` [Qemu-devel] [PATCH 10/16] qemu-io: Move qemu_strsep() to cutils.c Kevin Wolf
2013-05-29 22:12   ` Eric Blake
2013-05-28 15:27 ` [Qemu-devel] [PATCH 11/16] qemu-io: Move functions for registering and running commands Kevin Wolf
2013-05-30  2:27   ` Eric Blake
2013-06-05 12:42   ` Stefan Hajnoczi
2013-05-28 15:27 ` [Qemu-devel] [PATCH 12/16] qemu-io: Move command_loop() and friends Kevin Wolf
2013-05-28 15:27 ` [Qemu-devel] [PATCH 13/16] qemu-io: Move remaining helpers from cmd.c Kevin Wolf
2013-05-28 15:27 ` [Qemu-devel] [PATCH 14/16] qemu-io: Interface cleanup Kevin Wolf
2013-05-28 15:27 ` [Qemu-devel] [PATCH 15/16] qemu-io: Use the qemu version for -V Kevin Wolf
2013-05-28 15:27 ` [Qemu-devel] [PATCH 16/16] Make qemu-io commands available in the monitor Kevin Wolf
2013-05-28 16:07   ` Eric Blake
2013-05-29  8:13     ` Kevin Wolf
2013-05-29 12:05       ` Eric Blake
2013-05-29 17:51       ` Luiz Capitulino
2013-06-04 10:08         ` Kevin Wolf
2013-06-04 12:40           ` Luiz Capitulino
2013-06-04 12:49             ` Kevin Wolf
2013-06-04 13:09               ` Luiz Capitulino
2013-06-05 13:02 ` [Qemu-devel] [PATCH 00/16] " Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1369754856-30036-8-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.