yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
34b321
From c73b87743086200c3f9ad485dcd4e71bdee27881 Mon Sep 17 00:00:00 2001
34b321
From: John Snow <jsnow@redhat.com>
34b321
Date: Mon, 23 Nov 2015 17:38:26 +0100
34b321
Subject: [PATCH 07/27] qemu-io: Factor out qemuio_command
34b321
34b321
RH-Author: John Snow <jsnow@redhat.com>
34b321
Message-id: <1448300320-7772-8-git-send-email-jsnow@redhat.com>
34b321
Patchwork-id: 68438
34b321
O-Subject: [RHEL-7.3 qemu-kvm PATCH v2 07/21] qemu-io: Factor out qemuio_command
34b321
Bugzilla: 1272523
34b321
RH-Acked-by: Thomas Huth <thuth@redhat.com>
34b321
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
34b321
RH-Acked-by: Max Reitz <mreitz@redhat.com>
34b321
34b321
From: Kevin Wolf <kwolf@redhat.com>
34b321
34b321
It's duplicated code. Move it to qemu-io-cmds.c because it's not
34b321
dependent on any static data of the qemu-io tool.
34b321
34b321
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
34b321
Reviewed-by: Eric Blake <eblake@redhat.com>
34b321
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
34b321
(cherry picked from commit dd5832967ac3fe96bd5bf9f199639176998ead69)
34b321
Signed-off-by: John Snow <jsnow@redhat.com>
34b321
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
34b321
---
34b321
 cmd.c          | 43 +++++--------------------------------------
34b321
 cmd.h          |  3 ++-
34b321
 qemu-io-cmds.c | 24 ++++++++++++++++++++++++
34b321
 3 files changed, 31 insertions(+), 39 deletions(-)
34b321
34b321
diff --git a/cmd.c b/cmd.c
34b321
index d501aab..7ae978f 100644
34b321
--- a/cmd.c
34b321
+++ b/cmd.c
34b321
@@ -138,28 +138,11 @@ static char *get_prompt(void);
34b321
 
34b321
 void command_loop(void)
34b321
 {
34b321
-    int c, i, done = 0, fetchable = 0, prompted = 0;
34b321
+    int i, done = 0, fetchable = 0, prompted = 0;
34b321
     char *input;
34b321
-    char **v;
34b321
-    const cmdinfo_t *ct;
34b321
 
34b321
     for (i = 0; !done && i < ncmdline; i++) {
34b321
-        input = strdup(cmdline[i]);
34b321
-        if (!input) {
34b321
-            fprintf(stderr, _("cannot strdup command '%s': %s\n"),
34b321
-                    cmdline[i], strerror(errno));
34b321
-            exit(1);
34b321
-        }
34b321
-        v = breakline(input, &c);
34b321
-        if (c) {
34b321
-            ct = find_command(v[0]);
34b321
-            if (ct) {
34b321
-                done = command(ct, c, v);
34b321
-            } else {
34b321
-                fprintf(stderr, _("command \"%s\" not found\n"), v[0]);
34b321
-            }
34b321
-	}
34b321
-        doneline(input, v);
34b321
+        done = qemuio_command(cmdline[i]);
34b321
     }
34b321
     if (cmdline) {
34b321
         g_free(cmdline);
34b321
@@ -179,20 +162,13 @@ void command_loop(void)
34b321
         if (!fetchable) {
34b321
             continue;
34b321
         }
34b321
+
34b321
         input = fetchline();
34b321
         if (input == NULL) {
34b321
             break;
34b321
         }
34b321
-        v = breakline(input, &c);
34b321
-        if (c) {
34b321
-            ct = find_command(v[0]);
34b321
-            if (ct) {
34b321
-                done = command(ct, c, v);
34b321
-            } else {
34b321
-                fprintf(stderr, _("command \"%s\" not found\n"), v[0]);
34b321
-            }
34b321
-        }
34b321
-        doneline(input, v);
34b321
+        done = qemuio_command(input);
34b321
+        free(input);
34b321
 
34b321
         prompted = 0;
34b321
         fetchable = 0;
34b321
@@ -328,15 +304,6 @@ char **breakline(char *input, int *count)
34b321
     return rval;
34b321
 }
34b321
 
34b321
-void
34b321
-doneline(
34b321
-	char	*input,
34b321
-	char	**vec)
34b321
-{
34b321
-	free(input);
34b321
-	free(vec);
34b321
-}
34b321
-
34b321
 #define EXABYTES(x)	((long long)(x) << 60)
34b321
 #define PETABYTES(x)	((long long)(x) << 50)
34b321
 #define TERABYTES(x)	((long long)(x) << 40)
34b321
diff --git a/cmd.h b/cmd.h
34b321
index ccf6336..d676408 100644
34b321
--- a/cmd.h
34b321
+++ b/cmd.h
34b321
@@ -59,7 +59,6 @@ int command(const cmdinfo_t *ci, int argc, char **argv);
34b321
 
34b321
 /* from input.h */
34b321
 char **breakline(char *input, int *count);
34b321
-void doneline(char *input, char **vec);
34b321
 char *fetchline(void);
34b321
 
34b321
 void cvtstr(double value, char *str, size_t sz);
34b321
@@ -77,4 +76,6 @@ void timestr(struct timeval *tv, char *str, size_t sz, int flags);
34b321
 
34b321
 extern char *progname;
34b321
 
34b321
+bool qemuio_command(const char *cmd);
34b321
+
34b321
 #endif	/* __COMMAND_H__ */
34b321
diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c
34b321
index efa6070..09e4099 100644
34b321
--- a/qemu-io-cmds.c
34b321
+++ b/qemu-io-cmds.c
34b321
@@ -1824,6 +1824,30 @@ static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
34b321
     return 1;
34b321
 }
34b321
 
34b321
+bool qemuio_command(const char *cmd)
34b321
+{
34b321
+    char *input;
34b321
+    const cmdinfo_t *ct;
34b321
+    char **v;
34b321
+    int c;
34b321
+    bool done = false;
34b321
+
34b321
+    input = g_strdup(cmd);
34b321
+    v = breakline(input, &c);
34b321
+    if (c) {
34b321
+        ct = find_command(v[0]);
34b321
+        if (ct) {
34b321
+            done = command(ct, c, v);
34b321
+        } else {
34b321
+            fprintf(stderr, "command \"%s\" not found\n", v[0]);
34b321
+        }
34b321
+    }
34b321
+    g_free(input);
34b321
+    g_free(v);
34b321
+
34b321
+    return done;
34b321
+}
34b321
+
34b321
 static void __attribute((constructor)) init_qemuio_commands(void)
34b321
 {
34b321
     /* initialize commands */
34b321
-- 
34b321
1.8.3.1
34b321