yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-qemu-io-Move-functions-for-registering-and-running-c.patch

34b321
From df682d833654f2f42595d6156341439644a8848d Mon Sep 17 00:00:00 2001
34b321
From: John Snow <jsnow@redhat.com>
34b321
Date: Mon, 23 Nov 2015 17:38:30 +0100
34b321
Subject: [PATCH 11/27] qemu-io: Move functions for registering and running
34b321
 commands
34b321
34b321
RH-Author: John Snow <jsnow@redhat.com>
34b321
Message-id: <1448300320-7772-12-git-send-email-jsnow@redhat.com>
34b321
Patchwork-id: 68439
34b321
O-Subject: [RHEL-7.3 qemu-kvm PATCH v2 11/21] qemu-io: Move functions for registering and running commands
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
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 c2cdf5c5892165cbe7d3567bff5930521bc52669)
34b321
Signed-off-by: John Snow <jsnow@redhat.com>
34b321
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
34b321
34b321
Conflicts:
34b321
    qemu-io-cmds.c: Conflicts over sleep_f, again.
34b321
34b321
Signed-off-by: John Snow <jsnow@redhat.com>
34b321
---
34b321
 cmd.c          | 113 ---------------------------------
34b321
 cmd.h          |  11 +---
34b321
 qemu-io-cmds.c | 194 +++++++++++++++++++++++++++++++++++++++++----------------
34b321
 qemu-io.c      |  10 +--
34b321
 4 files changed, 149 insertions(+), 179 deletions(-)
34b321
34b321
diff --git a/cmd.c b/cmd.c
34b321
index f6bf2c5..6616d61 100644
34b321
--- a/cmd.c
34b321
+++ b/cmd.c
34b321
@@ -31,94 +31,9 @@
34b321
 
34b321
 /* from libxcmd/command.c */
34b321
 
34b321
-cmdinfo_t	*cmdtab;
34b321
-int		ncmds;
34b321
-
34b321
-static checkfunc_t	check_func;
34b321
 static int		ncmdline;
34b321
 static char		**cmdline;
34b321
 
34b321
-static int
34b321
-compare(const void *a, const void *b)
34b321
-{
34b321
-	return strcmp(((const cmdinfo_t *)a)->name,
34b321
-		      ((const cmdinfo_t *)b)->name);
34b321
-}
34b321
-
34b321
-void add_command(const cmdinfo_t *ci)
34b321
-{
34b321
-    cmdtab = g_realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
34b321
-    cmdtab[ncmds - 1] = *ci;
34b321
-    qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
34b321
-}
34b321
-
34b321
-static int
34b321
-check_command(
34b321
-	const cmdinfo_t	*ci)
34b321
-{
34b321
-	if (check_func)
34b321
-		return check_func(qemuio_bs, ci);
34b321
-	return 1;
34b321
-}
34b321
-
34b321
-void
34b321
-add_check_command(
34b321
-	checkfunc_t	cf)
34b321
-{
34b321
-	check_func = cf;
34b321
-}
34b321
-
34b321
-int
34b321
-command_usage(
34b321
-	const cmdinfo_t *ci)
34b321
-{
34b321
-	printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
34b321
-	return 0;
34b321
-}
34b321
-
34b321
-int
34b321
-command(
34b321
-	const cmdinfo_t	*ct,
34b321
-	int		argc,
34b321
-	char		**argv)
34b321
-{
34b321
-	char		*cmd = argv[0];
34b321
-
34b321
-	if (!check_command(ct))
34b321
-		return 0;
34b321
-
34b321
-	if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
34b321
-		if (ct->argmax == -1)
34b321
-			fprintf(stderr,
34b321
-	_("bad argument count %d to %s, expected at least %d arguments\n"),
34b321
-				argc-1, cmd, ct->argmin);
34b321
-		else if (ct->argmin == ct->argmax)
34b321
-			fprintf(stderr,
34b321
-	_("bad argument count %d to %s, expected %d arguments\n"),
34b321
-				argc-1, cmd, ct->argmin);
34b321
-		else
34b321
-			fprintf(stderr,
34b321
-	_("bad argument count %d to %s, expected between %d and %d arguments\n"),
34b321
-			argc-1, cmd, ct->argmin, ct->argmax);
34b321
-		return 0;
34b321
-	}
34b321
-	optind = 0;
34b321
-	return ct->cfunc(qemuio_bs, argc, argv);
34b321
-}
34b321
-
34b321
-const cmdinfo_t *
34b321
-find_command(
34b321
-	const char	*cmd)
34b321
-{
34b321
-	cmdinfo_t	*ct;
34b321
-
34b321
-	for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
34b321
-		if (strcmp(ct->name, cmd) == 0 ||
34b321
-		    (ct->altname && strcmp(ct->altname, cmd) == 0))
34b321
-			return (const cmdinfo_t *)ct;
34b321
-	}
34b321
-	return NULL;
34b321
-}
34b321
 
34b321
 void add_user_command(char *optarg)
34b321
 {
34b321
@@ -255,34 +170,6 @@ fetchline(void)
34b321
 }
34b321
 #endif
34b321
 
34b321
-char **breakline(char *input, int *count)
34b321
-{
34b321
-    int c = 0;
34b321
-    char *p;
34b321
-    char **rval = calloc(sizeof(char *), 1);
34b321
-    char **tmp;
34b321
-
34b321
-    while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
34b321
-        if (!*p) {
34b321
-            continue;
34b321
-        }
34b321
-        c++;
34b321
-        tmp = realloc(rval, sizeof(*rval) * (c + 1));
34b321
-        if (!tmp) {
34b321
-            free(rval);
34b321
-            rval = NULL;
34b321
-            c = 0;
34b321
-            break;
34b321
-        } else {
34b321
-            rval = tmp;
34b321
-        }
34b321
-        rval[c - 1] = p;
34b321
-        rval[c] = NULL;
34b321
-    }
34b321
-    *count = c;
34b321
-    return rval;
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 5b6f61b..0d01a33 100644
34b321
--- a/cmd.h
34b321
+++ b/cmd.h
34b321
@@ -39,23 +39,16 @@ typedef struct cmdinfo {
34b321
 	helpfunc_t      help;
34b321
 } cmdinfo_t;
34b321
 
34b321
-extern cmdinfo_t	*cmdtab;
34b321
-extern int		ncmds;
34b321
-
34b321
 typedef int (*checkfunc_t)(BlockDriverState *bs, const cmdinfo_t *ci);
34b321
 
34b321
-void add_command(const cmdinfo_t *ci);
34b321
+void qemuio_add_command(const cmdinfo_t *ci);
34b321
 void add_user_command(char *optarg);
34b321
 void add_check_command(checkfunc_t cf);
34b321
 
34b321
-const cmdinfo_t *find_command(const char *cmd);
34b321
-
34b321
 void command_loop(void);
34b321
-int command_usage(const cmdinfo_t *ci);
34b321
-int command(const cmdinfo_t *ci, int argc, char **argv);
34b321
+int qemuio_command_usage(const cmdinfo_t *ci);
34b321
 
34b321
 /* from input.h */
34b321
-char **breakline(char *input, int *count);
34b321
 char *fetchline(void);
34b321
 
34b321
 void cvtstr(double value, char *str, size_t sz);
34b321
diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c
34b321
index a8e891a..27a903f 100644
34b321
--- a/qemu-io-cmds.c
34b321
+++ b/qemu-io-cmds.c
34b321
@@ -17,6 +17,110 @@
34b321
 
34b321
 int qemuio_misalign;
34b321
 
34b321
+static cmdinfo_t *cmdtab;
34b321
+static int ncmds;
34b321
+
34b321
+static int compare_cmdname(const void *a, const void *b)
34b321
+{
34b321
+    return strcmp(((const cmdinfo_t *)a)->name,
34b321
+                  ((const cmdinfo_t *)b)->name);
34b321
+}
34b321
+
34b321
+void qemuio_add_command(const cmdinfo_t *ci)
34b321
+{
34b321
+    cmdtab = g_realloc(cmdtab, ++ncmds * sizeof(*cmdtab));
34b321
+    cmdtab[ncmds - 1] = *ci;
34b321
+    qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname);
34b321
+}
34b321
+
34b321
+int qemuio_command_usage(const cmdinfo_t *ci)
34b321
+{
34b321
+    printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
34b321
+    return 0;
34b321
+}
34b321
+
34b321
+static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
34b321
+{
34b321
+    if (ct->flags & CMD_FLAG_GLOBAL) {
34b321
+        return 1;
34b321
+    }
34b321
+    if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
34b321
+        fprintf(stderr, "no file open, try 'help open'\n");
34b321
+        return 0;
34b321
+    }
34b321
+    return 1;
34b321
+}
34b321
+
34b321
+static int command(const cmdinfo_t *ct, int argc, char **argv)
34b321
+{
34b321
+    char *cmd = argv[0];
34b321
+
34b321
+    if (!init_check_command(qemuio_bs, ct)) {
34b321
+        return 0;
34b321
+    }
34b321
+
34b321
+    if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) {
34b321
+        if (ct->argmax == -1) {
34b321
+            fprintf(stderr,
34b321
+                    "bad argument count %d to %s, expected at least %d arguments\n",
34b321
+                    argc-1, cmd, ct->argmin);
34b321
+        } else if (ct->argmin == ct->argmax) {
34b321
+            fprintf(stderr,
34b321
+                    "bad argument count %d to %s, expected %d arguments\n",
34b321
+                    argc-1, cmd, ct->argmin);
34b321
+        } else {
34b321
+            fprintf(stderr,
34b321
+                    "bad argument count %d to %s, expected between %d and %d arguments\n",
34b321
+                    argc-1, cmd, ct->argmin, ct->argmax);
34b321
+        }
34b321
+        return 0;
34b321
+    }
34b321
+    optind = 0;
34b321
+    return ct->cfunc(qemuio_bs, argc, argv);
34b321
+}
34b321
+
34b321
+static const cmdinfo_t *find_command(const char *cmd)
34b321
+{
34b321
+    cmdinfo_t *ct;
34b321
+
34b321
+    for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
34b321
+        if (strcmp(ct->name, cmd) == 0 ||
34b321
+            (ct->altname && strcmp(ct->altname, cmd) == 0))
34b321
+        {
34b321
+            return (const cmdinfo_t *)ct;
34b321
+        }
34b321
+    }
34b321
+    return NULL;
34b321
+}
34b321
+
34b321
+static char **breakline(char *input, int *count)
34b321
+{
34b321
+    int c = 0;
34b321
+    char *p;
34b321
+    char **rval = g_malloc0(sizeof(char *));
34b321
+    char **tmp;
34b321
+
34b321
+    while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
34b321
+        if (!*p) {
34b321
+            continue;
34b321
+        }
34b321
+        c++;
34b321
+        tmp = g_realloc(rval, sizeof(*rval) * (c + 1));
34b321
+        if (!tmp) {
34b321
+            g_free(rval);
34b321
+            rval = NULL;
34b321
+            c = 0;
34b321
+            break;
34b321
+        } else {
34b321
+            rval = tmp;
34b321
+        }
34b321
+        rval[c - 1] = p;
34b321
+        rval[c] = NULL;
34b321
+    }
34b321
+    *count = c;
34b321
+    return rval;
34b321
+}
34b321
+
34b321
 static int64_t cvtnum(const char *s)
34b321
 {
34b321
     char *end;
34b321
@@ -468,12 +572,12 @@ static int read_f(BlockDriverState *bs, int argc, char **argv)
34b321
             vflag = 1;
34b321
             break;
34b321
         default:
34b321
-            return command_usage(&read_cmd);
34b321
+            return qemuio_command_usage(&read_cmd);
34b321
         }
34b321
     }
34b321
 
34b321
     if (optind != argc - 2) {
34b321
-        return command_usage(&read_cmd);
34b321
+        return qemuio_command_usage(&read_cmd);
34b321
     }
34b321
 
34b321
     if (bflag && pflag) {
34b321
@@ -495,7 +599,7 @@ static int read_f(BlockDriverState *bs, int argc, char **argv)
34b321
     }
34b321
 
34b321
     if (!Pflag && (lflag || sflag)) {
34b321
-        return command_usage(&read_cmd);
34b321
+        return qemuio_command_usage(&read_cmd);
34b321
     }
34b321
 
34b321
     if (!lflag) {
34b321
@@ -630,12 +734,12 @@ static int readv_f(BlockDriverState *bs, int argc, char **argv)
34b321
             vflag = 1;
34b321
             break;
34b321
         default:
34b321
-            return command_usage(&readv_cmd);
34b321
+            return qemuio_command_usage(&readv_cmd);
34b321
         }
34b321
     }
34b321
 
34b321
     if (optind > argc - 2) {
34b321
-        return command_usage(&readv_cmd);
34b321
+        return qemuio_command_usage(&readv_cmd);
34b321
     }
34b321
 
34b321
 
34b321
@@ -770,12 +874,12 @@ static int write_f(BlockDriverState *bs, int argc, char **argv)
34b321
             zflag = 1;
34b321
             break;
34b321
         default:
34b321
-            return command_usage(&write_cmd);
34b321
+            return qemuio_command_usage(&write_cmd);
34b321
         }
34b321
     }
34b321
 
34b321
     if (optind != argc - 2) {
34b321
-        return command_usage(&write_cmd);
34b321
+        return qemuio_command_usage(&write_cmd);
34b321
     }
34b321
 
34b321
     if (bflag + pflag + zflag > 1) {
34b321
@@ -912,12 +1016,12 @@ static int writev_f(BlockDriverState *bs, int argc, char **argv)
34b321
             }
34b321
             break;
34b321
         default:
34b321
-            return command_usage(&writev_cmd);
34b321
+            return qemuio_command_usage(&writev_cmd);
34b321
         }
34b321
     }
34b321
 
34b321
     if (optind > argc - 2) {
34b321
-        return command_usage(&writev_cmd);
34b321
+        return qemuio_command_usage(&writev_cmd);
34b321
     }
34b321
 
34b321
     offset = cvtnum(argv[optind]);
34b321
@@ -1024,12 +1128,12 @@ static int multiwrite_f(BlockDriverState *bs, int argc, char **argv)
34b321
             }
34b321
             break;
34b321
         default:
34b321
-            return command_usage(&writev_cmd);
34b321
+            return qemuio_command_usage(&writev_cmd);
34b321
         }
34b321
     }
34b321
 
34b321
     if (optind > argc - 2) {
34b321
-        return command_usage(&writev_cmd);
34b321
+        return qemuio_command_usage(&writev_cmd);
34b321
     }
34b321
 
34b321
     nr_reqs = 1;
34b321
@@ -1258,13 +1362,13 @@ static int aio_read_f(BlockDriverState *bs, int argc, char **argv)
34b321
             break;
34b321
         default:
34b321
             g_free(ctx);
34b321
-            return command_usage(&aio_read_cmd);
34b321
+            return qemuio_command_usage(&aio_read_cmd);
34b321
         }
34b321
     }
34b321
 
34b321
     if (optind > argc - 2) {
34b321
         g_free(ctx);
34b321
-        return command_usage(&aio_read_cmd);
34b321
+        return qemuio_command_usage(&aio_read_cmd);
34b321
     }
34b321
 
34b321
     ctx->offset = cvtnum(argv[optind]);
34b321
@@ -1350,13 +1454,13 @@ static int aio_write_f(BlockDriverState *bs, int argc, char **argv)
34b321
             break;
34b321
         default:
34b321
             g_free(ctx);
34b321
-            return command_usage(&aio_write_cmd);
34b321
+            return qemuio_command_usage(&aio_write_cmd);
34b321
         }
34b321
     }
34b321
 
34b321
     if (optind > argc - 2) {
34b321
         g_free(ctx);
34b321
-        return command_usage(&aio_write_cmd);
34b321
+        return qemuio_command_usage(&aio_write_cmd);
34b321
     }
34b321
 
34b321
     ctx->offset = cvtnum(argv[optind]);
34b321
@@ -1556,12 +1660,12 @@ static int discard_f(BlockDriverState *bs, int argc, char **argv)
34b321
             qflag = 1;
34b321
             break;
34b321
         default:
34b321
-            return command_usage(&discard_cmd);
34b321
+            return qemuio_command_usage(&discard_cmd);
34b321
         }
34b321
     }
34b321
 
34b321
     if (optind != argc - 2) {
34b321
-        return command_usage(&discard_cmd);
34b321
+        return qemuio_command_usage(&discard_cmd);
34b321
     }
34b321
 
34b321
     offset = cvtnum(argv[optind]);
34b321
@@ -1876,18 +1980,6 @@ static const cmdinfo_t help_cmd = {
34b321
     .oneline    = "help for one or all commands",
34b321
 };
34b321
 
34b321
-static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct)
34b321
-{
34b321
-    if (ct->flags & CMD_FLAG_GLOBAL) {
34b321
-        return 1;
34b321
-    }
34b321
-    if (!(ct->flags & CMD_NOFILE_OK) && !bs) {
34b321
-        fprintf(stderr, "no file open, try 'help open'\n");
34b321
-        return 0;
34b321
-    }
34b321
-    return 1;
34b321
-}
34b321
-
34b321
 bool qemuio_command(const char *cmd)
34b321
 {
34b321
     char *input;
34b321
@@ -1915,27 +2007,25 @@ bool qemuio_command(const char *cmd)
34b321
 static void __attribute((constructor)) init_qemuio_commands(void)
34b321
 {
34b321
     /* initialize commands */
34b321
-    add_command(&help_cmd);
34b321
-    add_command(&read_cmd);
34b321
-    add_command(&readv_cmd);
34b321
-    add_command(&write_cmd);
34b321
-    add_command(&writev_cmd);
34b321
-    add_command(&multiwrite_cmd);
34b321
-    add_command(&aio_read_cmd);
34b321
-    add_command(&aio_write_cmd);
34b321
-    add_command(&aio_flush_cmd);
34b321
-    add_command(&flush_cmd);
34b321
-    add_command(&truncate_cmd);
34b321
-    add_command(&length_cmd);
34b321
-    add_command(&info_cmd);
34b321
-    add_command(&discard_cmd);
34b321
-    add_command(&alloc_cmd);
34b321
-    add_command(&map_cmd);
34b321
-    add_command(&break_cmd);
34b321
-    add_command(&resume_cmd);
34b321
-    add_command(&wait_break_cmd);
34b321
-    add_command(&abort_cmd);
34b321
-    add_command(&sleep_cmd);
34b321
-
34b321
-    add_check_command(init_check_command);
34b321
+    qemuio_add_command(&help_cmd);
34b321
+    qemuio_add_command(&read_cmd);
34b321
+    qemuio_add_command(&readv_cmd);
34b321
+    qemuio_add_command(&write_cmd);
34b321
+    qemuio_add_command(&writev_cmd);
34b321
+    qemuio_add_command(&multiwrite_cmd);
34b321
+    qemuio_add_command(&aio_read_cmd);
34b321
+    qemuio_add_command(&aio_write_cmd);
34b321
+    qemuio_add_command(&aio_flush_cmd);
34b321
+    qemuio_add_command(&flush_cmd);
34b321
+    qemuio_add_command(&truncate_cmd);
34b321
+    qemuio_add_command(&length_cmd);
34b321
+    qemuio_add_command(&info_cmd);
34b321
+    qemuio_add_command(&discard_cmd);
34b321
+    qemuio_add_command(&alloc_cmd);
34b321
+    qemuio_add_command(&map_cmd);
34b321
+    qemuio_add_command(&break_cmd);
34b321
+    qemuio_add_command(&resume_cmd);
34b321
+    qemuio_add_command(&wait_break_cmd);
34b321
+    qemuio_add_command(&abort_cmd);
34b321
+    qemuio_add_command(&sleep_cmd);
34b321
 }
34b321
diff --git a/qemu-io.c b/qemu-io.c
34b321
index b86bfbf..97af39e 100644
34b321
--- a/qemu-io.c
34b321
+++ b/qemu-io.c
34b321
@@ -155,7 +155,7 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
34b321
             qemu_opts_del(qopts);
34b321
             break;
34b321
         default:
34b321
-            return command_usage(&open_cmd);
34b321
+            return qemuio_command_usage(&open_cmd);
34b321
         }
34b321
     }
34b321
 
34b321
@@ -164,7 +164,7 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
34b321
     }
34b321
 
34b321
     if (optind != argc - 1) {
34b321
-        return command_usage(&open_cmd);
34b321
+        return qemuio_command_usage(&open_cmd);
34b321
     }
34b321
 
34b321
     return openfile(argv[optind], flags, growable, opts);
34b321
@@ -300,9 +300,9 @@ int main(int argc, char **argv)
34b321
     bdrv_init();
34b321
 
34b321
     /* initialize commands */
34b321
-    add_command(&quit_cmd);
34b321
-    add_command(&open_cmd);
34b321
-    add_command(&close_cmd);
34b321
+    qemuio_add_command(&quit_cmd);
34b321
+    qemuio_add_command(&open_cmd);
34b321
+    qemuio_add_command(&close_cmd);
34b321
 
34b321
     /* open the device */
34b321
     if (!readonly) {
34b321
-- 
34b321
1.8.3.1
34b321