]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
libxcmd: check CMD_FLAG_GLOBAL inside args_command()
authorDave Chinner <dchinner@redhat.com>
Thu, 12 Jan 2017 20:12:41 +0000 (14:12 -0600)
committerEric Sandeen <sandeen@redhat.com>
Thu, 12 Jan 2017 20:12:41 +0000 (14:12 -0600)
Rather than having multiple methods of executing commands from the
CLI, use CMD_FLAG_GLOBAL to indicate a one-shot command rather than
an iterative command from args_command(). This simplifies the main
loop processing.

To make it more obvious what this CMD_FLAG_GLOBAL flag does, rename
it to CMD_FLAG_ONESHOT to indicate that the command should only ever
be executed once and not iterated.

Signed-Off-By: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
include/command.h
io/file.c
io/init.c
libxcmd/command.c
libxcmd/help.c
libxcmd/quit.c
quota/path.c
quota/report.c

index 81d5a4dbb7f34209eb3acb0332fb1924dfa05e40..58bfcaac44a01945675a3262fc56934c7b199616 100644 (file)
 
 #include <sys/time.h>
 
-#define CMD_FLAG_GLOBAL                (1<<31) /* don't iterate "args" */
+/*
+ * A "oneshot" command ony runs once per command execution. It does
+ * not iterate the command args function callout and so can be used
+ * for functions like "help" that should only ever be run once.
+ */
+#define CMD_FLAG_ONESHOT       (1<<31)
 #define CMD_FLAG_FOREIGN_OK    (1<<30) /* command not restricted to XFS */
 
 typedef int (*cfunc_t)(int argc, char **argv);
index d4bc4f8fc1d585cd6fc8017d6243b89ff0f7a973..8e3f07122922a16c90d1b43e5b9b591212ae0f7b 100644 (file)
--- a/io/file.c
+++ b/io/file.c
@@ -104,7 +104,7 @@ file_init(void)
        print_cmd.argmin = 0;
        print_cmd.argmax = 0;
        print_cmd.flags = CMD_NOMAP_OK | CMD_NOFILE_OK | CMD_FOREIGN_OK |
-                               CMD_FLAG_GLOBAL;
+                               CMD_FLAG_ONESHOT;
        print_cmd.oneline = _("list current open files and memory mappings");
 
        add_command(&file_cmd);
index ee7683ef99c6c267ee01046fb1b08a4fe04a6c20..10ae223aff727d4e37be1b5f3c8dec7fb35358a7 100644 (file)
--- a/io/init.c
+++ b/io/init.c
@@ -105,7 +105,7 @@ static int
 init_check_command(
        const cmdinfo_t *ct)
 {
-       if (ct->flags & CMD_FLAG_GLOBAL)
+       if (ct->flags & CMD_FLAG_ONESHOT)
                return 1;
 
        if (!file && !(ct->flags & CMD_NOFILE_OK)) {
index dd0034cc6d832ae5eb9ae6d68f9647b06fada3d9..dce8361ce3eafd9563d20a8b1094fbd13223b000 100644 (file)
@@ -124,10 +124,20 @@ add_user_command(char *optarg)
        cmdline[ncmdline-1] = optarg;
 }
 
+/*
+ * To detect one-shot commands, they will return a negative index. If we
+ * get a negative index on entry, we've already run the one-shot command,
+ * so we abort straight away.
+ */
 static int
 args_command(
-       int     index)
+       const cmdinfo_t *ct,
+       int             index)
 {
+       if (index < 0)
+               return 0;
+       if (ct->flags & CMD_FLAG_ONESHOT)
+               return -1;
        if (args_func)
                return args_func(index);
        return 0;
@@ -160,13 +170,9 @@ command_loop(void)
                if (c) {
                        ct = find_command(v[0]);
                        if (ct) {
-                               if (ct->flags & CMD_FLAG_GLOBAL)
+                               j = 0;
+                               while (!done && (j = args_command(ct, j)))
                                        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]);
index 8894c7931f89f481c9f1dfe3beec00daadbb15cb..bc31d6df1d8a410129e175269607a3e7fa92dff4 100644 (file)
@@ -89,7 +89,7 @@ help_init(void)
        help_cmd.cfunc = help_f;
        help_cmd.argmin = 0;
        help_cmd.argmax = 1;
-       help_cmd.flags = CMD_FLAG_GLOBAL | CMD_ALL_FSTYPES;
+       help_cmd.flags = CMD_FLAG_ONESHOT | CMD_ALL_FSTYPES;
        help_cmd.args = _("[command]");
        help_cmd.oneline = _("help for one or all commands");
 
index e0af91629b810a4b6f188de5756a7896afa6ab79..19431015aee28ddaec3e82b3bc0a523f73cf12c3 100644 (file)
@@ -39,7 +39,7 @@ quit_init(void)
        quit_cmd.cfunc = quit_f;
        quit_cmd.argmin = -1;
        quit_cmd.argmax = -1;
-       quit_cmd.flags = CMD_FLAG_GLOBAL | CMD_ALL_FSTYPES;
+       quit_cmd.flags = CMD_FLAG_ONESHOT | CMD_ALL_FSTYPES;
        quit_cmd.oneline = _("exit the program");
 
        add_command(&quit_cmd);
index 57d14f0b55111d51bdc046a28e2c321face49f81..330a3bef6aa96a2fbe5865f0cfc6449cfd2e6145 100644 (file)
@@ -141,7 +141,7 @@ path_init(void)
        path_cmd.cfunc = path_f;
        path_cmd.argmin = 0;
        path_cmd.argmax = 1;
-       path_cmd.flags = CMD_FLAG_GLOBAL | CMD_FLAG_FOREIGN_OK;
+       path_cmd.flags = CMD_FLAG_ONESHOT | CMD_FLAG_FOREIGN_OK;
        path_cmd.oneline = _("set current path, or show the list of paths");
 
        print_cmd.name = "print";
@@ -149,7 +149,7 @@ path_init(void)
        print_cmd.cfunc = print_f;
        print_cmd.argmin = 0;
        print_cmd.argmax = 0;
-       print_cmd.flags = CMD_FLAG_GLOBAL | CMD_FLAG_FOREIGN_OK;
+       print_cmd.flags = CMD_FLAG_ONESHOT | CMD_FLAG_FOREIGN_OK;
        print_cmd.oneline = _("list known mount points and projects");
 
        if (expert)
index 3833dd6e811b9d636ccd1b69ab7bd06749a5e7c0..d0509c25fe34f3d841d4ae84d04f30e606019df9 100644 (file)
@@ -778,7 +778,7 @@ report_init(void)
        report_cmd.args = _("[-bir] [-gpu] [-ahnt] [-f file]");
        report_cmd.oneline = _("report filesystem quota information");
        report_cmd.help = report_help;
-       report_cmd.flags = CMD_FLAG_GLOBAL | CMD_FLAG_FOREIGN_OK;
+       report_cmd.flags = CMD_FLAG_ONESHOT | CMD_FLAG_FOREIGN_OK;
 
        if (expert) {
                add_command(&dump_cmd);