cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
34b321
From 828d09abacc6ae7f0643957301cc3ddee269d7a0 Mon Sep 17 00:00:00 2001
34b321
From: John Snow <jsnow@redhat.com>
34b321
Date: Mon, 23 Nov 2015 17:38:31 +0100
34b321
Subject: [PATCH 12/27] qemu-io: Move command_loop() and friends
34b321
34b321
RH-Author: John Snow <jsnow@redhat.com>
34b321
Message-id: <1448300320-7772-13-git-send-email-jsnow@redhat.com>
34b321
Patchwork-id: 68441
34b321
O-Subject: [RHEL-7.3 qemu-kvm PATCH v2 12/21] qemu-io: Move command_loop() and friends
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
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
34b321
(cherry picked from commit d1174f13e78e2f43f7ae33d59b62b0b94468c8db)
34b321
Signed-off-by: John Snow <jsnow@redhat.com>
34b321
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
34b321
---
34b321
 cmd.c     | 139 --------------------------------------------------------------
34b321
 cmd.h     |   9 ----
34b321
 qemu-io.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34b321
 3 files changed, 139 insertions(+), 148 deletions(-)
34b321
34b321
diff --git a/cmd.c b/cmd.c
34b321
index 6616d61..26d38a8 100644
34b321
--- a/cmd.c
34b321
+++ b/cmd.c
34b321
@@ -31,145 +31,6 @@
34b321
 
34b321
 /* from libxcmd/command.c */
34b321
 
34b321
-static int		ncmdline;
34b321
-static char		**cmdline;
34b321
-
34b321
-
34b321
-void add_user_command(char *optarg)
34b321
-{
34b321
-    cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
34b321
-    cmdline[ncmdline-1] = optarg;
34b321
-}
34b321
-
34b321
-static void prep_fetchline(void *opaque)
34b321
-{
34b321
-    int *fetchable = opaque;
34b321
-
34b321
-    qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
34b321
-    *fetchable= 1;
34b321
-}
34b321
-
34b321
-static char *get_prompt(void);
34b321
-
34b321
-void command_loop(void)
34b321
-{
34b321
-    int i, done = 0, fetchable = 0, prompted = 0;
34b321
-    char *input;
34b321
-
34b321
-    for (i = 0; !done && i < ncmdline; i++) {
34b321
-        done = qemuio_command(cmdline[i]);
34b321
-    }
34b321
-    if (cmdline) {
34b321
-        g_free(cmdline);
34b321
-        return;
34b321
-    }
34b321
-
34b321
-    while (!done) {
34b321
-        if (!prompted) {
34b321
-            printf("%s", get_prompt());
34b321
-            fflush(stdout);
34b321
-            qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
34b321
-            prompted = 1;
34b321
-        }
34b321
-
34b321
-        main_loop_wait(false);
34b321
-
34b321
-        if (!fetchable) {
34b321
-            continue;
34b321
-        }
34b321
-
34b321
-        input = fetchline();
34b321
-        if (input == NULL) {
34b321
-            break;
34b321
-        }
34b321
-        done = qemuio_command(input);
34b321
-        free(input);
34b321
-
34b321
-        prompted = 0;
34b321
-        fetchable = 0;
34b321
-    }
34b321
-    qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
34b321
-}
34b321
-
34b321
-/* from libxcmd/input.c */
34b321
-
34b321
-#if defined(ENABLE_READLINE)
34b321
-# include <readline/history.h>
34b321
-# include <readline/readline.h>
34b321
-#elif defined(ENABLE_EDITLINE)
34b321
-# include <histedit.h>
34b321
-#endif
34b321
-
34b321
-static char *
34b321
-get_prompt(void)
34b321
-{
34b321
-	static char	prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
34b321
-
34b321
-	if (!prompt[0])
34b321
-		snprintf(prompt, sizeof(prompt), "%s> ", progname);
34b321
-	return prompt;
34b321
-}
34b321
-
34b321
-#if defined(ENABLE_READLINE)
34b321
-char *
34b321
-fetchline(void)
34b321
-{
34b321
-	char	*line;
34b321
-
34b321
-	line = readline(get_prompt());
34b321
-	if (line && *line)
34b321
-		add_history(line);
34b321
-	return line;
34b321
-}
34b321
-#elif defined(ENABLE_EDITLINE)
34b321
-static char *el_get_prompt(EditLine *e) { return get_prompt(); }
34b321
-char *
34b321
-fetchline(void)
34b321
-{
34b321
-	static EditLine	*el;
34b321
-	static History	*hist;
34b321
-	HistEvent	hevent;
34b321
-	char		*line;
34b321
-	int		count;
34b321
-
34b321
-	if (!el) {
34b321
-		hist = history_init();
34b321
-		history(hist, &hevent, H_SETSIZE, 100);
34b321
-		el = el_init(progname, stdin, stdout, stderr);
34b321
-		el_source(el, NULL);
34b321
-		el_set(el, EL_SIGNAL, 1);
34b321
-		el_set(el, EL_PROMPT, el_get_prompt);
34b321
-		el_set(el, EL_HIST, history, (const char *)hist);
34b321
-	}
34b321
-	line = strdup(el_gets(el, &count));
34b321
-	if (line) {
34b321
-		if (count > 0)
34b321
-			line[count-1] = '\0';
34b321
-		if (*line)
34b321
-			history(hist, &hevent, H_ENTER, line);
34b321
-	}
34b321
-	return line;
34b321
-}
34b321
-#else
34b321
-# define MAXREADLINESZ	1024
34b321
-char *
34b321
-fetchline(void)
34b321
-{
34b321
-	char	*p, *line = malloc(MAXREADLINESZ);
34b321
-
34b321
-	if (!line)
34b321
-		return NULL;
34b321
-	if (!fgets(line, MAXREADLINESZ, stdin)) {
34b321
-		free(line);
34b321
-		return NULL;
34b321
-	}
34b321
-	p = line + strlen(line);
34b321
-	if (p != line && p[-1] == '\n')
34b321
-		p[-1] = '\0';
34b321
-	return line;
34b321
-}
34b321
-#endif
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 0d01a33..da0c7cf 100644
34b321
--- a/cmd.h
34b321
+++ b/cmd.h
34b321
@@ -39,18 +39,11 @@ typedef struct cmdinfo {
34b321
 	helpfunc_t      help;
34b321
 } cmdinfo_t;
34b321
 
34b321
-typedef int (*checkfunc_t)(BlockDriverState *bs, const cmdinfo_t *ci);
34b321
-
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
-void command_loop(void);
34b321
 int qemuio_command_usage(const cmdinfo_t *ci);
34b321
 
34b321
 /* from input.h */
34b321
-char *fetchline(void);
34b321
-
34b321
 void cvtstr(double value, char *str, size_t sz);
34b321
 
34b321
 struct timeval tsub(struct timeval t1, struct timeval t2);
34b321
@@ -64,8 +57,6 @@ enum {
34b321
 
34b321
 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.c b/qemu-io.c
34b321
index 97af39e..da9944e 100644
34b321
--- a/qemu-io.c
34b321
+++ b/qemu-io.c
34b321
@@ -34,6 +34,10 @@ BlockDriverState *qemuio_bs;
34b321
 
34b321
 extern int qemuio_misalign;
34b321
 
34b321
+/* qemu-io commands passed using -c */
34b321
+static int ncmdline;
34b321
+static char **cmdline;
34b321
+
34b321
 static int close_f(BlockDriverState *bs, int argc, char **argv)
34b321
 {
34b321
     bdrv_unref(bs);
34b321
@@ -207,6 +211,141 @@ static void usage(const char *name)
34b321
 }
34b321
 
34b321
 
34b321
+#if defined(ENABLE_READLINE)
34b321
+# include <readline/history.h>
34b321
+# include <readline/readline.h>
34b321
+#elif defined(ENABLE_EDITLINE)
34b321
+# include <histedit.h>
34b321
+#endif
34b321
+
34b321
+static char *get_prompt(void)
34b321
+{
34b321
+    static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
34b321
+
34b321
+    if (!prompt[0]) {
34b321
+        snprintf(prompt, sizeof(prompt), "%s> ", progname);
34b321
+    }
34b321
+
34b321
+    return prompt;
34b321
+}
34b321
+
34b321
+#if defined(ENABLE_READLINE)
34b321
+static char *fetchline(void)
34b321
+{
34b321
+    char *line = readline(get_prompt());
34b321
+    if (line && *line) {
34b321
+        add_history(line);
34b321
+    }
34b321
+    return line;
34b321
+}
34b321
+#elif defined(ENABLE_EDITLINE)
34b321
+static char *el_get_prompt(EditLine *e)
34b321
+{
34b321
+    return get_prompt();
34b321
+}
34b321
+
34b321
+static char *fetchline(void)
34b321
+{
34b321
+    static EditLine *el;
34b321
+    static History *hist;
34b321
+    HistEvent hevent;
34b321
+    char *line;
34b321
+    int count;
34b321
+
34b321
+    if (!el) {
34b321
+        hist = history_init();
34b321
+        history(hist, &hevent, H_SETSIZE, 100);
34b321
+        el = el_init(progname, stdin, stdout, stderr);
34b321
+        el_source(el, NULL);
34b321
+        el_set(el, EL_SIGNAL, 1);
34b321
+        el_set(el, EL_PROMPT, el_get_prompt);
34b321
+        el_set(el, EL_HIST, history, (const char *)hist);
34b321
+    }
34b321
+    line = strdup(el_gets(el, &count));
34b321
+    if (line) {
34b321
+        if (count > 0) {
34b321
+            line[count-1] = '\0';
34b321
+        }
34b321
+        if (*line) {
34b321
+            history(hist, &hevent, H_ENTER, line);
34b321
+        }
34b321
+    }
34b321
+    return line;
34b321
+}
34b321
+#else
34b321
+# define MAXREADLINESZ 1024
34b321
+static char *fetchline(void)
34b321
+{
34b321
+    char *p, *line = g_malloc(MAXREADLINESZ);
34b321
+
34b321
+    if (!fgets(line, MAXREADLINESZ, stdin)) {
34b321
+        g_free(line);
34b321
+        return NULL;
34b321
+    }
34b321
+
34b321
+    p = line + strlen(line);
34b321
+    if (p != line && p[-1] == '\n') {
34b321
+        p[-1] = '\0';
34b321
+    }
34b321
+
34b321
+    return line;
34b321
+}
34b321
+#endif
34b321
+
34b321
+static void prep_fetchline(void *opaque)
34b321
+{
34b321
+    int *fetchable = opaque;
34b321
+
34b321
+    qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
34b321
+    *fetchable= 1;
34b321
+}
34b321
+
34b321
+static void command_loop(void)
34b321
+{
34b321
+    int i, done = 0, fetchable = 0, prompted = 0;
34b321
+    char *input;
34b321
+
34b321
+    for (i = 0; !done && i < ncmdline; i++) {
34b321
+        done = qemuio_command(cmdline[i]);
34b321
+    }
34b321
+    if (cmdline) {
34b321
+        g_free(cmdline);
34b321
+        return;
34b321
+    }
34b321
+
34b321
+    while (!done) {
34b321
+        if (!prompted) {
34b321
+            printf("%s", get_prompt());
34b321
+            fflush(stdout);
34b321
+            qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
34b321
+            prompted = 1;
34b321
+        }
34b321
+
34b321
+        main_loop_wait(false);
34b321
+
34b321
+        if (!fetchable) {
34b321
+            continue;
34b321
+        }
34b321
+
34b321
+        input = fetchline();
34b321
+        if (input == NULL) {
34b321
+            break;
34b321
+        }
34b321
+        done = qemuio_command(input);
34b321
+        g_free(input);
34b321
+
34b321
+        prompted = 0;
34b321
+        fetchable = 0;
34b321
+    }
34b321
+    qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
34b321
+}
34b321
+
34b321
+static void add_user_command(char *optarg)
34b321
+{
34b321
+    cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
34b321
+    cmdline[ncmdline-1] = optarg;
34b321
+}
34b321
+
34b321
 int main(int argc, char **argv)
34b321
 {
34b321
     int readonly = 0;
34b321
-- 
34b321
1.8.3.1
34b321