提交 c2cdf5c5 编写于 作者: K Kevin Wolf 提交者: Stefan Hajnoczi

qemu-io: Move functions for registering and running commands

Signed-off-by: NKevin Wolf <kwolf@redhat.com>
Reviewed-by: NEric Blake <eblake@redhat.com>
Signed-off-by: NStefan Hajnoczi <stefanha@redhat.com>
上级 a38ed811
...@@ -31,94 +31,9 @@ ...@@ -31,94 +31,9 @@
/* from libxcmd/command.c */ /* from libxcmd/command.c */
cmdinfo_t *cmdtab;
int ncmds;
static checkfunc_t check_func;
static int ncmdline; static int ncmdline;
static char **cmdline; 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) void add_user_command(char *optarg)
{ {
...@@ -255,34 +170,6 @@ fetchline(void) ...@@ -255,34 +170,6 @@ fetchline(void)
} }
#endif #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 EXABYTES(x) ((long long)(x) << 60)
#define PETABYTES(x) ((long long)(x) << 50) #define PETABYTES(x) ((long long)(x) << 50)
#define TERABYTES(x) ((long long)(x) << 40) #define TERABYTES(x) ((long long)(x) << 40)
......
...@@ -39,23 +39,16 @@ typedef struct cmdinfo { ...@@ -39,23 +39,16 @@ typedef struct cmdinfo {
helpfunc_t help; helpfunc_t help;
} cmdinfo_t; } cmdinfo_t;
extern cmdinfo_t *cmdtab;
extern int ncmds;
typedef int (*checkfunc_t)(BlockDriverState *bs, const cmdinfo_t *ci); 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_user_command(char *optarg);
void add_check_command(checkfunc_t cf); void add_check_command(checkfunc_t cf);
const cmdinfo_t *find_command(const char *cmd);
void command_loop(void); void command_loop(void);
int command_usage(const cmdinfo_t *ci); int qemuio_command_usage(const cmdinfo_t *ci);
int command(const cmdinfo_t *ci, int argc, char **argv);
/* from input.h */ /* from input.h */
char **breakline(char *input, int *count);
char *fetchline(void); char *fetchline(void);
void cvtstr(double value, char *str, size_t sz); void cvtstr(double value, char *str, size_t sz);
......
...@@ -16,6 +16,110 @@ ...@@ -16,6 +16,110 @@
int qemuio_misalign; 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) static int64_t cvtnum(const char *s)
{ {
char *end; char *end;
...@@ -467,12 +571,12 @@ static int read_f(BlockDriverState *bs, int argc, char **argv) ...@@ -467,12 +571,12 @@ static int read_f(BlockDriverState *bs, int argc, char **argv)
vflag = 1; vflag = 1;
break; break;
default: default:
return command_usage(&read_cmd); return qemuio_command_usage(&read_cmd);
} }
} }
if (optind != argc - 2) { if (optind != argc - 2) {
return command_usage(&read_cmd); return qemuio_command_usage(&read_cmd);
} }
if (bflag && pflag) { if (bflag && pflag) {
...@@ -494,7 +598,7 @@ static int read_f(BlockDriverState *bs, int argc, char **argv) ...@@ -494,7 +598,7 @@ static int read_f(BlockDriverState *bs, int argc, char **argv)
} }
if (!Pflag && (lflag || sflag)) { if (!Pflag && (lflag || sflag)) {
return command_usage(&read_cmd); return qemuio_command_usage(&read_cmd);
} }
if (!lflag) { if (!lflag) {
...@@ -629,12 +733,12 @@ static int readv_f(BlockDriverState *bs, int argc, char **argv) ...@@ -629,12 +733,12 @@ static int readv_f(BlockDriverState *bs, int argc, char **argv)
vflag = 1; vflag = 1;
break; break;
default: default:
return command_usage(&readv_cmd); return qemuio_command_usage(&readv_cmd);
} }
} }
if (optind > argc - 2) { 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) ...@@ -769,12 +873,12 @@ static int write_f(BlockDriverState *bs, int argc, char **argv)
zflag = 1; zflag = 1;
break; break;
default: default:
return command_usage(&write_cmd); return qemuio_command_usage(&write_cmd);
} }
} }
if (optind != argc - 2) { if (optind != argc - 2) {
return command_usage(&write_cmd); return qemuio_command_usage(&write_cmd);
} }
if (bflag + pflag + zflag > 1) { if (bflag + pflag + zflag > 1) {
...@@ -911,12 +1015,12 @@ static int writev_f(BlockDriverState *bs, int argc, char **argv) ...@@ -911,12 +1015,12 @@ static int writev_f(BlockDriverState *bs, int argc, char **argv)
} }
break; break;
default: default:
return command_usage(&writev_cmd); return qemuio_command_usage(&writev_cmd);
} }
} }
if (optind > argc - 2) { if (optind > argc - 2) {
return command_usage(&writev_cmd); return qemuio_command_usage(&writev_cmd);
} }
offset = cvtnum(argv[optind]); offset = cvtnum(argv[optind]);
...@@ -1023,12 +1127,12 @@ static int multiwrite_f(BlockDriverState *bs, int argc, char **argv) ...@@ -1023,12 +1127,12 @@ static int multiwrite_f(BlockDriverState *bs, int argc, char **argv)
} }
break; break;
default: default:
return command_usage(&writev_cmd); return qemuio_command_usage(&writev_cmd);
} }
} }
if (optind > argc - 2) { if (optind > argc - 2) {
return command_usage(&writev_cmd); return qemuio_command_usage(&writev_cmd);
} }
nr_reqs = 1; nr_reqs = 1;
...@@ -1257,13 +1361,13 @@ static int aio_read_f(BlockDriverState *bs, int argc, char **argv) ...@@ -1257,13 +1361,13 @@ static int aio_read_f(BlockDriverState *bs, int argc, char **argv)
break; break;
default: default:
g_free(ctx); g_free(ctx);
return command_usage(&aio_read_cmd); return qemuio_command_usage(&aio_read_cmd);
} }
} }
if (optind > argc - 2) { if (optind > argc - 2) {
g_free(ctx); g_free(ctx);
return command_usage(&aio_read_cmd); return qemuio_command_usage(&aio_read_cmd);
} }
ctx->offset = cvtnum(argv[optind]); ctx->offset = cvtnum(argv[optind]);
...@@ -1349,13 +1453,13 @@ static int aio_write_f(BlockDriverState *bs, int argc, char **argv) ...@@ -1349,13 +1453,13 @@ static int aio_write_f(BlockDriverState *bs, int argc, char **argv)
break; break;
default: default:
g_free(ctx); g_free(ctx);
return command_usage(&aio_write_cmd); return qemuio_command_usage(&aio_write_cmd);
} }
} }
if (optind > argc - 2) { if (optind > argc - 2) {
g_free(ctx); g_free(ctx);
return command_usage(&aio_write_cmd); return qemuio_command_usage(&aio_write_cmd);
} }
ctx->offset = cvtnum(argv[optind]); ctx->offset = cvtnum(argv[optind]);
...@@ -1547,12 +1651,12 @@ static int discard_f(BlockDriverState *bs, int argc, char **argv) ...@@ -1547,12 +1651,12 @@ static int discard_f(BlockDriverState *bs, int argc, char **argv)
qflag = 1; qflag = 1;
break; break;
default: default:
return command_usage(&discard_cmd); return qemuio_command_usage(&discard_cmd);
} }
} }
if (optind != argc - 2) { if (optind != argc - 2) {
return command_usage(&discard_cmd); return qemuio_command_usage(&discard_cmd);
} }
offset = cvtnum(argv[optind]); offset = cvtnum(argv[optind]);
...@@ -1860,18 +1964,6 @@ static const cmdinfo_t help_cmd = { ...@@ -1860,18 +1964,6 @@ static const cmdinfo_t help_cmd = {
.oneline = "help for one or all commands", .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) bool qemuio_command(const char *cmd)
{ {
char *input; char *input;
...@@ -1899,26 +1991,24 @@ bool qemuio_command(const char *cmd) ...@@ -1899,26 +1991,24 @@ bool qemuio_command(const char *cmd)
static void __attribute((constructor)) init_qemuio_commands(void) static void __attribute((constructor)) init_qemuio_commands(void)
{ {
/* initialize commands */ /* initialize commands */
add_command(&help_cmd); qemuio_add_command(&help_cmd);
add_command(&read_cmd); qemuio_add_command(&read_cmd);
add_command(&readv_cmd); qemuio_add_command(&readv_cmd);
add_command(&write_cmd); qemuio_add_command(&write_cmd);
add_command(&writev_cmd); qemuio_add_command(&writev_cmd);
add_command(&multiwrite_cmd); qemuio_add_command(&multiwrite_cmd);
add_command(&aio_read_cmd); qemuio_add_command(&aio_read_cmd);
add_command(&aio_write_cmd); qemuio_add_command(&aio_write_cmd);
add_command(&aio_flush_cmd); qemuio_add_command(&aio_flush_cmd);
add_command(&flush_cmd); qemuio_add_command(&flush_cmd);
add_command(&truncate_cmd); qemuio_add_command(&truncate_cmd);
add_command(&length_cmd); qemuio_add_command(&length_cmd);
add_command(&info_cmd); qemuio_add_command(&info_cmd);
add_command(&discard_cmd); qemuio_add_command(&discard_cmd);
add_command(&alloc_cmd); qemuio_add_command(&alloc_cmd);
add_command(&map_cmd); qemuio_add_command(&map_cmd);
add_command(&break_cmd); qemuio_add_command(&break_cmd);
add_command(&resume_cmd); qemuio_add_command(&resume_cmd);
add_command(&wait_break_cmd); qemuio_add_command(&wait_break_cmd);
add_command(&abort_cmd); qemuio_add_command(&abort_cmd);
add_check_command(init_check_command);
} }
...@@ -122,7 +122,7 @@ static int open_f(BlockDriverState *bs, int argc, char **argv) ...@@ -122,7 +122,7 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
growable = 1; growable = 1;
break; break;
default: 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) ...@@ -131,7 +131,7 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
} }
if (optind != argc - 1) { if (optind != argc - 1) {
return command_usage(&open_cmd); return qemuio_command_usage(&open_cmd);
} }
return openfile(argv[optind], flags, growable); return openfile(argv[optind], flags, growable);
...@@ -262,9 +262,9 @@ int main(int argc, char **argv) ...@@ -262,9 +262,9 @@ int main(int argc, char **argv)
bdrv_init(); bdrv_init();
/* initialize commands */ /* initialize commands */
add_command(&quit_cmd); qemuio_add_command(&quit_cmd);
add_command(&open_cmd); qemuio_add_command(&open_cmd);
add_command(&close_cmd); qemuio_add_command(&close_cmd);
/* open the device */ /* open the device */
if (!readonly) { if (!readonly) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册