From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58751) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UkCjz-0000AQ-R7 for qemu-devel@nongnu.org; Wed, 05 Jun 2013 08:23:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UkCh4-0004Np-Hz for qemu-devel@nongnu.org; Wed, 05 Jun 2013 08:20:04 -0400 Received: from mx1.redhat.com ([209.132.183.28]:50156) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UkCh4-0004Ne-8D for qemu-devel@nongnu.org; Wed, 05 Jun 2013 08:19:58 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r55CJvW4025457 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 5 Jun 2013 08:19:57 -0400 From: Kevin Wolf Date: Wed, 5 Jun 2013 14:19:32 +0200 Message-Id: <1370434781-28570-8-git-send-email-kwolf@redhat.com> In-Reply-To: <1370434781-28570-1-git-send-email-kwolf@redhat.com> References: <1370434781-28570-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH v2 07/16] qemu-io: Factor out qemuio_command List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, pbonzini@redhat.com, stefanha@redhat.com, lcapitulino@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 Reviewed-by: Eric Blake --- 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 0a3817a..8b12446 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