From patchwork Fri Jun 7 11:58:31 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 249699 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 3A0132C008F for ; Fri, 7 Jun 2013 22:58:33 +1000 (EST) Received: from localhost ([::1]:44801 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UkvNd-0005i8-Ez for incoming@patchwork.ozlabs.org; Fri, 07 Jun 2013 08:02:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57337) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UkvKE-0001M7-N7 for qemu-devel@nongnu.org; Fri, 07 Jun 2013 07:59:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UkvKA-0004KV-53 for qemu-devel@nongnu.org; Fri, 07 Jun 2013 07:59:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:10415) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UkvK9-0004KG-Qr for qemu-devel@nongnu.org; Fri, 07 Jun 2013 07:59:18 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r57BxHXg000337 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 7 Jun 2013 07:59:17 -0400 Received: from localhost (ovpn-112-30.ams2.redhat.com [10.36.112.30]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r57BxFMO001305; Fri, 7 Jun 2013 07:59:16 -0400 From: Stefan Hajnoczi To: Date: Fri, 7 Jun 2013 13:58:31 +0200 Message-Id: <1370606325-10680-13-git-send-email-stefanha@redhat.com> In-Reply-To: <1370606325-10680-1-git-send-email-stefanha@redhat.com> References: <1370606325-10680-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Anthony Liguori , Stefan Hajnoczi Subject: [Qemu-devel] [PULL 12/26] qemu-io: Move functions for registering and running commands X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Kevin Wolf Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake Signed-off-by: Stefan Hajnoczi --- cmd.c | 113 --------------------------------- cmd.h | 11 +--- qemu-io-cmds.c | 192 ++++++++++++++++++++++++++++++++++++++++++--------------- qemu-io.c | 10 +-- 4 files changed, 148 insertions(+), 178 deletions(-) diff --git a/cmd.c b/cmd.c index f6bf2c5..6616d61 100644 --- a/cmd.c +++ b/cmd.c @@ -31,94 +31,9 @@ /* from libxcmd/command.c */ -cmdinfo_t *cmdtab; -int ncmds; - -static checkfunc_t check_func; static int ncmdline; static char **cmdline; -static int -compare(const void *a, const void *b) -{ - return strcmp(((const cmdinfo_t *)a)->name, - ((const cmdinfo_t *)b)->name); -} - -void add_command(const cmdinfo_t *ci) -{ - cmdtab = g_realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab)); - cmdtab[ncmds - 1] = *ci; - qsort(cmdtab, ncmds, sizeof(*cmdtab), compare); -} - -static int -check_command( - const cmdinfo_t *ci) -{ - if (check_func) - return check_func(qemuio_bs, ci); - return 1; -} - -void -add_check_command( - checkfunc_t cf) -{ - check_func = cf; -} - -int -command_usage( - const cmdinfo_t *ci) -{ - printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline); - return 0; -} - -int -command( - const cmdinfo_t *ct, - int argc, - char **argv) -{ - char *cmd = argv[0]; - - if (!check_command(ct)) - return 0; - - if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) { - if (ct->argmax == -1) - fprintf(stderr, - _("bad argument count %d to %s, expected at least %d arguments\n"), - argc-1, cmd, ct->argmin); - else if (ct->argmin == ct->argmax) - fprintf(stderr, - _("bad argument count %d to %s, expected %d arguments\n"), - argc-1, cmd, ct->argmin); - else - fprintf(stderr, - _("bad argument count %d to %s, expected between %d and %d arguments\n"), - argc-1, cmd, ct->argmin, ct->argmax); - return 0; - } - optind = 0; - return ct->cfunc(qemuio_bs, argc, argv); -} - -const cmdinfo_t * -find_command( - const char *cmd) -{ - cmdinfo_t *ct; - - for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { - if (strcmp(ct->name, cmd) == 0 || - (ct->altname && strcmp(ct->altname, cmd) == 0)) - return (const cmdinfo_t *)ct; - } - return NULL; -} void add_user_command(char *optarg) { @@ -255,34 +170,6 @@ fetchline(void) } #endif -char **breakline(char *input, int *count) -{ - int c = 0; - char *p; - char **rval = calloc(sizeof(char *), 1); - char **tmp; - - while (rval && (p = qemu_strsep(&input, " ")) != NULL) { - if (!*p) { - continue; - } - c++; - tmp = realloc(rval, sizeof(*rval) * (c + 1)); - if (!tmp) { - free(rval); - rval = NULL; - c = 0; - break; - } else { - rval = tmp; - } - rval[c - 1] = p; - rval[c] = NULL; - } - *count = c; - return rval; -} - #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 5b6f61b..0d01a33 100644 --- a/cmd.h +++ b/cmd.h @@ -39,23 +39,16 @@ typedef struct cmdinfo { helpfunc_t help; } cmdinfo_t; -extern cmdinfo_t *cmdtab; -extern int ncmds; - typedef int (*checkfunc_t)(BlockDriverState *bs, const cmdinfo_t *ci); -void add_command(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); -const cmdinfo_t *find_command(const char *cmd); - void command_loop(void); -int command_usage(const cmdinfo_t *ci); -int command(const cmdinfo_t *ci, int argc, char **argv); +int qemuio_command_usage(const cmdinfo_t *ci); /* from input.h */ -char **breakline(char *input, int *count); char *fetchline(void); void cvtstr(double value, char *str, size_t sz); diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index fa8d9a0..8acc866 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -16,6 +16,110 @@ int qemuio_misalign; +static cmdinfo_t *cmdtab; +static int ncmds; + +static int compare_cmdname(const void *a, const void *b) +{ + return strcmp(((const cmdinfo_t *)a)->name, + ((const cmdinfo_t *)b)->name); +} + +void qemuio_add_command(const cmdinfo_t *ci) +{ + cmdtab = g_realloc(cmdtab, ++ncmds * sizeof(*cmdtab)); + cmdtab[ncmds - 1] = *ci; + qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname); +} + +int qemuio_command_usage(const cmdinfo_t *ci) +{ + printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline); + return 0; +} + +static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct) +{ + if (ct->flags & CMD_FLAG_GLOBAL) { + return 1; + } + if (!(ct->flags & CMD_NOFILE_OK) && !bs) { + fprintf(stderr, "no file open, try 'help open'\n"); + return 0; + } + return 1; +} + +static int command(const cmdinfo_t *ct, int argc, char **argv) +{ + char *cmd = argv[0]; + + if (!init_check_command(qemuio_bs, ct)) { + return 0; + } + + if (argc - 1 < ct->argmin || (ct->argmax != -1 && argc - 1 > ct->argmax)) { + if (ct->argmax == -1) { + fprintf(stderr, + "bad argument count %d to %s, expected at least %d arguments\n", + argc-1, cmd, ct->argmin); + } else if (ct->argmin == ct->argmax) { + fprintf(stderr, + "bad argument count %d to %s, expected %d arguments\n", + argc-1, cmd, ct->argmin); + } else { + fprintf(stderr, + "bad argument count %d to %s, expected between %d and %d arguments\n", + argc-1, cmd, ct->argmin, ct->argmax); + } + return 0; + } + optind = 0; + return ct->cfunc(qemuio_bs, argc, argv); +} + +static const cmdinfo_t *find_command(const char *cmd) +{ + cmdinfo_t *ct; + + for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) { + if (strcmp(ct->name, cmd) == 0 || + (ct->altname && strcmp(ct->altname, cmd) == 0)) + { + return (const cmdinfo_t *)ct; + } + } + return NULL; +} + +static char **breakline(char *input, int *count) +{ + int c = 0; + char *p; + char **rval = g_malloc0(sizeof(char *)); + char **tmp; + + while (rval && (p = qemu_strsep(&input, " ")) != NULL) { + if (!*p) { + continue; + } + c++; + tmp = g_realloc(rval, sizeof(*rval) * (c + 1)); + if (!tmp) { + g_free(rval); + rval = NULL; + c = 0; + break; + } else { + rval = tmp; + } + rval[c - 1] = p; + rval[c] = NULL; + } + *count = c; + return rval; +} + static int64_t cvtnum(const char *s) { char *end; @@ -467,12 +571,12 @@ static int read_f(BlockDriverState *bs, int argc, char **argv) vflag = 1; break; default: - return command_usage(&read_cmd); + return qemuio_command_usage(&read_cmd); } } if (optind != argc - 2) { - return command_usage(&read_cmd); + return qemuio_command_usage(&read_cmd); } if (bflag && pflag) { @@ -494,7 +598,7 @@ static int read_f(BlockDriverState *bs, int argc, char **argv) } if (!Pflag && (lflag || sflag)) { - return command_usage(&read_cmd); + return qemuio_command_usage(&read_cmd); } if (!lflag) { @@ -629,12 +733,12 @@ static int readv_f(BlockDriverState *bs, int argc, char **argv) vflag = 1; break; default: - return command_usage(&readv_cmd); + return qemuio_command_usage(&readv_cmd); } } if (optind > argc - 2) { - return command_usage(&readv_cmd); + return qemuio_command_usage(&readv_cmd); } @@ -769,12 +873,12 @@ static int write_f(BlockDriverState *bs, int argc, char **argv) zflag = 1; break; default: - return command_usage(&write_cmd); + return qemuio_command_usage(&write_cmd); } } if (optind != argc - 2) { - return command_usage(&write_cmd); + return qemuio_command_usage(&write_cmd); } if (bflag + pflag + zflag > 1) { @@ -911,12 +1015,12 @@ static int writev_f(BlockDriverState *bs, int argc, char **argv) } break; default: - return command_usage(&writev_cmd); + return qemuio_command_usage(&writev_cmd); } } if (optind > argc - 2) { - return command_usage(&writev_cmd); + return qemuio_command_usage(&writev_cmd); } offset = cvtnum(argv[optind]); @@ -1023,12 +1127,12 @@ static int multiwrite_f(BlockDriverState *bs, int argc, char **argv) } break; default: - return command_usage(&writev_cmd); + return qemuio_command_usage(&writev_cmd); } } if (optind > argc - 2) { - return command_usage(&writev_cmd); + return qemuio_command_usage(&writev_cmd); } nr_reqs = 1; @@ -1257,13 +1361,13 @@ static int aio_read_f(BlockDriverState *bs, int argc, char **argv) break; default: g_free(ctx); - return command_usage(&aio_read_cmd); + return qemuio_command_usage(&aio_read_cmd); } } if (optind > argc - 2) { g_free(ctx); - return command_usage(&aio_read_cmd); + return qemuio_command_usage(&aio_read_cmd); } ctx->offset = cvtnum(argv[optind]); @@ -1349,13 +1453,13 @@ static int aio_write_f(BlockDriverState *bs, int argc, char **argv) break; default: g_free(ctx); - return command_usage(&aio_write_cmd); + return qemuio_command_usage(&aio_write_cmd); } } if (optind > argc - 2) { g_free(ctx); - return command_usage(&aio_write_cmd); + return qemuio_command_usage(&aio_write_cmd); } ctx->offset = cvtnum(argv[optind]); @@ -1547,12 +1651,12 @@ static int discard_f(BlockDriverState *bs, int argc, char **argv) qflag = 1; break; default: - return command_usage(&discard_cmd); + return qemuio_command_usage(&discard_cmd); } } if (optind != argc - 2) { - return command_usage(&discard_cmd); + return qemuio_command_usage(&discard_cmd); } offset = cvtnum(argv[optind]); @@ -1860,18 +1964,6 @@ static const cmdinfo_t help_cmd = { .oneline = "help for one or all commands", }; -static int init_check_command(BlockDriverState *bs, const cmdinfo_t *ct) -{ - if (ct->flags & CMD_FLAG_GLOBAL) { - return 1; - } - if (!(ct->flags & CMD_NOFILE_OK) && !bs) { - fprintf(stderr, "no file open, try 'help open'\n"); - return 0; - } - return 1; -} - bool qemuio_command(const char *cmd) { char *input; @@ -1899,26 +1991,24 @@ bool qemuio_command(const char *cmd) static void __attribute((constructor)) init_qemuio_commands(void) { /* initialize commands */ - add_command(&help_cmd); - add_command(&read_cmd); - add_command(&readv_cmd); - add_command(&write_cmd); - add_command(&writev_cmd); - add_command(&multiwrite_cmd); - add_command(&aio_read_cmd); - add_command(&aio_write_cmd); - add_command(&aio_flush_cmd); - add_command(&flush_cmd); - add_command(&truncate_cmd); - add_command(&length_cmd); - add_command(&info_cmd); - add_command(&discard_cmd); - add_command(&alloc_cmd); - add_command(&map_cmd); - add_command(&break_cmd); - add_command(&resume_cmd); - add_command(&wait_break_cmd); - add_command(&abort_cmd); - - add_check_command(init_check_command); + qemuio_add_command(&help_cmd); + qemuio_add_command(&read_cmd); + qemuio_add_command(&readv_cmd); + qemuio_add_command(&write_cmd); + qemuio_add_command(&writev_cmd); + qemuio_add_command(&multiwrite_cmd); + qemuio_add_command(&aio_read_cmd); + qemuio_add_command(&aio_write_cmd); + qemuio_add_command(&aio_flush_cmd); + qemuio_add_command(&flush_cmd); + qemuio_add_command(&truncate_cmd); + qemuio_add_command(&length_cmd); + qemuio_add_command(&info_cmd); + qemuio_add_command(&discard_cmd); + qemuio_add_command(&alloc_cmd); + qemuio_add_command(&map_cmd); + qemuio_add_command(&break_cmd); + qemuio_add_command(&resume_cmd); + qemuio_add_command(&wait_break_cmd); + qemuio_add_command(&abort_cmd); } diff --git a/qemu-io.c b/qemu-io.c index 8f6c57e..3bf5aec 100644 --- a/qemu-io.c +++ b/qemu-io.c @@ -122,7 +122,7 @@ static int open_f(BlockDriverState *bs, int argc, char **argv) growable = 1; break; default: - return command_usage(&open_cmd); + return qemuio_command_usage(&open_cmd); } } @@ -131,7 +131,7 @@ static int open_f(BlockDriverState *bs, int argc, char **argv) } if (optind != argc - 1) { - return command_usage(&open_cmd); + return qemuio_command_usage(&open_cmd); } return openfile(argv[optind], flags, growable); @@ -262,9 +262,9 @@ int main(int argc, char **argv) bdrv_init(); /* initialize commands */ - add_command(&quit_cmd); - add_command(&open_cmd); - add_command(&close_cmd); + qemuio_add_command(&quit_cmd); + qemuio_add_command(&open_cmd); + qemuio_add_command(&close_cmd); /* open the device */ if (!readonly) {