From 802d66e382e1d332940788e34ea94f2f302d2cac Mon Sep 17 00:00:00 2001 From: Barry Naujok Date: Mon, 16 Jul 2007 03:57:04 +0000 Subject: [PATCH] Improve xfs_io and xfs_quota -c handling Merge of master-melb:xfs-cmds:29139a by kenmcd. Define command global command flag --- include/command.h | 4 ++- io/file.c | 3 ++- libxcmd/command.c | 64 +++++++++++++++++++++++++++-------------------- libxcmd/help.c | 2 +- libxcmd/quit.c | 2 +- quota/path.c | 2 ++ 6 files changed, 46 insertions(+), 31 deletions(-) diff --git a/include/command.h b/include/command.h index db0506010..4869edf64 100644 --- a/include/command.h +++ b/include/command.h @@ -18,6 +18,8 @@ #ifndef __COMMAND_H__ #define __COMMAND_H__ +#define CMD_FLAG_GLOBAL ((int)0x80000000) /* don't iterate "args" */ + typedef int (*cfunc_t)(int argc, char **argv); typedef void (*helpfunc_t)(void); @@ -52,6 +54,6 @@ extern const cmdinfo_t *find_command(const char *cmd); extern void command_loop(void); extern int command_usage(const cmdinfo_t *ci); -extern int command(int argc, char **argv); +extern int command(const cmdinfo_t *ci, int argc, char **argv); #endif /* __COMMAND_H__ */ diff --git a/io/file.c b/io/file.c index 55bf31ae4..debab0f66 100644 --- a/io/file.c +++ b/io/file.c @@ -103,7 +103,8 @@ file_init(void) print_cmd.cfunc = print_f; print_cmd.argmin = 0; print_cmd.argmax = 0; - print_cmd.flags = CMD_NOMAP_OK | CMD_NOFILE_OK | CMD_FOREIGN_OK; + print_cmd.flags = CMD_NOMAP_OK | CMD_NOFILE_OK | CMD_FOREIGN_OK | + CMD_FLAG_GLOBAL; print_cmd.oneline = _("list current open files and memory mappings"); add_command(&file_cmd); diff --git a/libxcmd/command.c b/libxcmd/command.c index dfde33b9e..ebf141345 100644 --- a/libxcmd/command.c +++ b/libxcmd/command.c @@ -70,20 +70,15 @@ command_usage( int command( + const cmdinfo_t *ct, int argc, char **argv) { - char *cmd; - const cmdinfo_t *ct; + char *cmd = argv[0]; - cmd = argv[0]; - ct = find_command(cmd); - if (!ct) { - fprintf(stderr, _("command \"%s\" not found\n"), cmd); - return 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, @@ -148,26 +143,35 @@ add_args_command( void command_loop(void) { - int c, i, j = 0, done = 0; - char *input; - char **v; + int c, i, j = 0, done = 0; + char *input; + char **v; + const cmdinfo_t *ct; for (i = 0; !done && i < ncmdline; i++) { - while (!done && (j = args_command(j))) { - 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) - done = command(c, v); - free(v); - free(input); + 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) { + if (ct->flags & CMD_FLAG_GLOBAL) + done = command(ct, c, v); + else { + j = 0; + while (!done && (j = args_command(j))) + done = command(ct, c, v); + } + } else + fprintf(stderr, _("command \"%s\" not found\n"), + v[0]); } - j = 0; /* reset */ + doneline(input, v); } if (cmdline) { free(cmdline); @@ -177,8 +181,14 @@ command_loop(void) if ((input = fetchline()) == NULL) break; v = breakline(input, &c); - if (c) - done = command(c, v); + 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); } } diff --git a/libxcmd/help.c b/libxcmd/help.c index c7a71b784..2e567b681 100644 --- a/libxcmd/help.c +++ b/libxcmd/help.c @@ -88,7 +88,7 @@ help_init(void) help_cmd.cfunc = help_f; help_cmd.argmin = 0; help_cmd.argmax = 1; - help_cmd.flags = -1; + help_cmd.flags = CMD_FLAG_GLOBAL; help_cmd.args = _("[command]"); help_cmd.oneline = _("help for one or all commands"); diff --git a/libxcmd/quit.c b/libxcmd/quit.c index 89498455a..83e48b265 100644 --- a/libxcmd/quit.c +++ b/libxcmd/quit.c @@ -38,7 +38,7 @@ quit_init(void) quit_cmd.cfunc = quit_f; quit_cmd.argmin = -1; quit_cmd.argmax = -1; - quit_cmd.flags = -1; + quit_cmd.flags = CMD_FLAG_GLOBAL; quit_cmd.oneline = _("exit the program"); add_command(&quit_cmd); diff --git a/quota/path.c b/quota/path.c index 5e9b88451..608cab5eb 100644 --- a/quota/path.c +++ b/quota/path.c @@ -122,6 +122,7 @@ path_init(void) path_cmd.cfunc = path_f; path_cmd.argmin = 0; path_cmd.argmax = 1; + path_cmd.flags = CMD_FLAG_GLOBAL; path_cmd.oneline = _("set current path, or show the list of paths"); print_cmd.name = _("print"); @@ -129,6 +130,7 @@ path_init(void) print_cmd.cfunc = print_f; print_cmd.argmin = 0; print_cmd.argmax = 0; + print_cmd.flags = CMD_FLAG_GLOBAL; print_cmd.oneline = _("list known mount points and projects"); if (expert) -- 2.47.2