From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
Anthony Liguori <aliguori@us.ibm.com>,
Stefan Hajnoczi <stefanha@redhat.com>
Subject: [Qemu-devel] [PULL 13/26] qemu-io: Move command_loop() and friends
Date: Fri, 7 Jun 2013 13:58:32 +0200 [thread overview]
Message-ID: <1370606325-10680-14-git-send-email-stefanha@redhat.com> (raw)
In-Reply-To: <1370606325-10680-1-git-send-email-stefanha@redhat.com>
From: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
cmd.c | 139 --------------------------------------------------------------
cmd.h | 9 ----
qemu-io.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 139 insertions(+), 148 deletions(-)
diff --git a/cmd.c b/cmd.c
index 6616d61..26d38a8 100644
--- a/cmd.c
+++ b/cmd.c
@@ -31,145 +31,6 @@
/* from libxcmd/command.c */
-static int ncmdline;
-static char **cmdline;
-
-
-void add_user_command(char *optarg)
-{
- cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
- cmdline[ncmdline-1] = optarg;
-}
-
-static void prep_fetchline(void *opaque)
-{
- int *fetchable = opaque;
-
- qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
- *fetchable= 1;
-}
-
-static char *get_prompt(void);
-
-void command_loop(void)
-{
- int i, done = 0, fetchable = 0, prompted = 0;
- char *input;
-
- for (i = 0; !done && i < ncmdline; i++) {
- done = qemuio_command(cmdline[i]);
- }
- if (cmdline) {
- g_free(cmdline);
- return;
- }
-
- while (!done) {
- if (!prompted) {
- printf("%s", get_prompt());
- fflush(stdout);
- qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
- prompted = 1;
- }
-
- main_loop_wait(false);
-
- if (!fetchable) {
- continue;
- }
-
- input = fetchline();
- if (input == NULL) {
- break;
- }
- done = qemuio_command(input);
- free(input);
-
- prompted = 0;
- fetchable = 0;
- }
- qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
-}
-
-/* from libxcmd/input.c */
-
-#if defined(ENABLE_READLINE)
-# include <readline/history.h>
-# include <readline/readline.h>
-#elif defined(ENABLE_EDITLINE)
-# include <histedit.h>
-#endif
-
-static char *
-get_prompt(void)
-{
- static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
-
- if (!prompt[0])
- snprintf(prompt, sizeof(prompt), "%s> ", progname);
- return prompt;
-}
-
-#if defined(ENABLE_READLINE)
-char *
-fetchline(void)
-{
- char *line;
-
- line = readline(get_prompt());
- if (line && *line)
- add_history(line);
- return line;
-}
-#elif defined(ENABLE_EDITLINE)
-static char *el_get_prompt(EditLine *e) { return get_prompt(); }
-char *
-fetchline(void)
-{
- static EditLine *el;
- static History *hist;
- HistEvent hevent;
- char *line;
- int count;
-
- if (!el) {
- hist = history_init();
- history(hist, &hevent, H_SETSIZE, 100);
- el = el_init(progname, stdin, stdout, stderr);
- el_source(el, NULL);
- el_set(el, EL_SIGNAL, 1);
- el_set(el, EL_PROMPT, el_get_prompt);
- el_set(el, EL_HIST, history, (const char *)hist);
- }
- line = strdup(el_gets(el, &count));
- if (line) {
- if (count > 0)
- line[count-1] = '\0';
- if (*line)
- history(hist, &hevent, H_ENTER, line);
- }
- return line;
-}
-#else
-# define MAXREADLINESZ 1024
-char *
-fetchline(void)
-{
- char *p, *line = malloc(MAXREADLINESZ);
-
- if (!line)
- return NULL;
- if (!fgets(line, MAXREADLINESZ, stdin)) {
- free(line);
- return NULL;
- }
- p = line + strlen(line);
- if (p != line && p[-1] == '\n')
- p[-1] = '\0';
- return line;
-}
-#endif
-
#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 0d01a33..da0c7cf 100644
--- a/cmd.h
+++ b/cmd.h
@@ -39,18 +39,11 @@ typedef struct cmdinfo {
helpfunc_t help;
} cmdinfo_t;
-typedef int (*checkfunc_t)(BlockDriverState *bs, const cmdinfo_t *ci);
-
void qemuio_add_command(const cmdinfo_t *ci);
-void add_user_command(char *optarg);
-void add_check_command(checkfunc_t cf);
-void command_loop(void);
int qemuio_command_usage(const cmdinfo_t *ci);
/* from input.h */
-char *fetchline(void);
-
void cvtstr(double value, char *str, size_t sz);
struct timeval tsub(struct timeval t1, struct timeval t2);
@@ -64,8 +57,6 @@ enum {
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.c b/qemu-io.c
index 3bf5aec..eec8cbc 100644
--- a/qemu-io.c
+++ b/qemu-io.c
@@ -29,6 +29,10 @@ char *progname;
BlockDriverState *qemuio_bs;
extern int qemuio_misalign;
+/* qemu-io commands passed using -c */
+static int ncmdline;
+static char **cmdline;
+
static int close_f(BlockDriverState *bs, int argc, char **argv)
{
bdrv_delete(bs);
@@ -174,6 +178,141 @@ static void usage(const char *name)
}
+#if defined(ENABLE_READLINE)
+# include <readline/history.h>
+# include <readline/readline.h>
+#elif defined(ENABLE_EDITLINE)
+# include <histedit.h>
+#endif
+
+static char *get_prompt(void)
+{
+ static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
+
+ if (!prompt[0]) {
+ snprintf(prompt, sizeof(prompt), "%s> ", progname);
+ }
+
+ return prompt;
+}
+
+#if defined(ENABLE_READLINE)
+static char *fetchline(void)
+{
+ char *line = readline(get_prompt());
+ if (line && *line) {
+ add_history(line);
+ }
+ return line;
+}
+#elif defined(ENABLE_EDITLINE)
+static char *el_get_prompt(EditLine *e)
+{
+ return get_prompt();
+}
+
+static char *fetchline(void)
+{
+ static EditLine *el;
+ static History *hist;
+ HistEvent hevent;
+ char *line;
+ int count;
+
+ if (!el) {
+ hist = history_init();
+ history(hist, &hevent, H_SETSIZE, 100);
+ el = el_init(progname, stdin, stdout, stderr);
+ el_source(el, NULL);
+ el_set(el, EL_SIGNAL, 1);
+ el_set(el, EL_PROMPT, el_get_prompt);
+ el_set(el, EL_HIST, history, (const char *)hist);
+ }
+ line = strdup(el_gets(el, &count));
+ if (line) {
+ if (count > 0) {
+ line[count-1] = '\0';
+ }
+ if (*line) {
+ history(hist, &hevent, H_ENTER, line);
+ }
+ }
+ return line;
+}
+#else
+# define MAXREADLINESZ 1024
+static char *fetchline(void)
+{
+ char *p, *line = g_malloc(MAXREADLINESZ);
+
+ if (!fgets(line, MAXREADLINESZ, stdin)) {
+ g_free(line);
+ return NULL;
+ }
+
+ p = line + strlen(line);
+ if (p != line && p[-1] == '\n') {
+ p[-1] = '\0';
+ }
+
+ return line;
+}
+#endif
+
+static void prep_fetchline(void *opaque)
+{
+ int *fetchable = opaque;
+
+ qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
+ *fetchable= 1;
+}
+
+static void command_loop(void)
+{
+ int i, done = 0, fetchable = 0, prompted = 0;
+ char *input;
+
+ for (i = 0; !done && i < ncmdline; i++) {
+ done = qemuio_command(cmdline[i]);
+ }
+ if (cmdline) {
+ g_free(cmdline);
+ return;
+ }
+
+ while (!done) {
+ if (!prompted) {
+ printf("%s", get_prompt());
+ fflush(stdout);
+ qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
+ prompted = 1;
+ }
+
+ main_loop_wait(false);
+
+ if (!fetchable) {
+ continue;
+ }
+
+ input = fetchline();
+ if (input == NULL) {
+ break;
+ }
+ done = qemuio_command(input);
+ g_free(input);
+
+ prompted = 0;
+ fetchable = 0;
+ }
+ qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
+}
+
+static void add_user_command(char *optarg)
+{
+ cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
+ cmdline[ncmdline-1] = optarg;
+}
+
int main(int argc, char **argv)
{
int readonly = 0;
--
1.8.1.4
next prev parent reply other threads:[~2013-06-07 11:59 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-07 11:58 [Qemu-devel] [PULL 00/26] Block patches Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 01/26] blockdev: reset werror/rerror on drive_del Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 02/26] qemu-io: Remove unused args_command Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 03/26] cutils: Support 'P' and 'E' suffixes in strtosz() Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 04/26] qemu-io: Make cvtnum() a wrapper around strtosz_suffix() Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 05/26] qemu-io: Handle cvtnum() errors in 'alloc' Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 06/26] qemu-io: Don't use global bs in command implementations Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 07/26] qemu-io: Split off commands to qemu-io-cmds.c Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 08/26] qemu-io: Factor out qemuio_command Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 09/26] qemu-io: Move 'help' function Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 10/26] qemu-io: Move 'quit' function Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 11/26] qemu-io: Move qemu_strsep() to cutils.c Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 12/26] qemu-io: Move functions for registering and running commands Stefan Hajnoczi
2013-06-07 11:58 ` Stefan Hajnoczi [this message]
2013-06-07 11:58 ` [Qemu-devel] [PULL 14/26] qemu-io: Move remaining helpers from cmd.c Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 15/26] qemu-io: Interface cleanup Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 16/26] qemu-io: Use the qemu version for -V Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 17/26] Make qemu-io commands available in HMP Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 18/26] blkdebug: Add BLKDBG_FLUSH_TO_OS/DISK events Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 19/26] ide-test: Add enum value for DEV Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 20/26] ide: Set BSY bit during FLUSH Stefan Hajnoczi
2013-06-07 12:10 ` Paolo Bonzini
2013-06-07 11:58 ` [Qemu-devel] [PULL 21/26] ide-test: Add FLUSH CACHE test case Stefan Hajnoczi
2013-06-14 17:54 ` Peter Maydell
2013-06-14 18:11 ` Peter Maydell
2013-06-14 18:18 ` Anthony Liguori
2013-06-17 12:45 ` Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 22/26] block: add snapshot info query function bdrv_query_snapshot_info_list() Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 23/26] block: add image info query function bdrv_query_image_info() Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 24/26] qmp: add ImageInfo in BlockDeviceInfo used by query-block Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 25/26] hmp: show ImageInfo in 'info block' Stefan Hajnoczi
2013-06-07 11:58 ` [Qemu-devel] [PULL 26/26] hmp: add parameters device and -v for info block Stefan Hajnoczi
2013-06-17 21:17 ` [Qemu-devel] [PULL 00/26] Block patches Anthony Liguori
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=1370606325-10680-14-git-send-email-stefanha@redhat.com \
--to=stefanha@redhat.com \
--cc=aliguori@us.ibm.com \
--cc=kwolf@redhat.com \
--cc=qemu-devel@nongnu.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).