dovecot-2.2: doveadm: Moved commands related code to doveadm-cmd...

dovecot at dovecot.org dovecot at dovecot.org
Tue Nov 11 23:08:54 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/c54b6bbf215a
changeset: 18059:c54b6bbf215a
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Nov 12 01:04:27 2014 +0200
description:
doveadm: Moved commands related code to doveadm-cmd.[ch]
Commands are already split for command line-only commands and commands that
can be run via doveadm-server also.

diffstat:

 src/doveadm/Makefile.am   |    2 +
 src/doveadm/doveadm-cmd.c |  108 ++++++++++++++++++++++++++++++++++++++++++++++
 src/doveadm/doveadm-cmd.h |   46 +++++++++++++++++++
 src/doveadm/doveadm.c     |   96 ++--------------------------------------
 src/doveadm/doveadm.h     |   35 +--------------
 5 files changed, 162 insertions(+), 125 deletions(-)

diffs (truncated from 390 to 300 lines):

diff -r e4a3e8554663 -r c54b6bbf215a src/doveadm/Makefile.am
--- a/src/doveadm/Makefile.am	Wed Nov 12 00:52:44 2014 +0200
+++ b/src/doveadm/Makefile.am	Wed Nov 12 01:04:27 2014 +0200
@@ -86,6 +86,7 @@
 	$(common) \
 	doveadm.c \
 	doveadm-auth.c \
+	doveadm-cmd.c \
 	doveadm-dict.c \
 	doveadm-director.c \
 	doveadm-dump.c \
@@ -123,6 +124,7 @@
 pkginc_libdir = $(pkgincludedir)
 pkginc_lib_HEADERS = \
 	doveadm.h \
+	doveadm-cmd.h \
 	doveadm-dsync.h \
 	doveadm-dump.h \
 	doveadm-mail.h \
diff -r e4a3e8554663 -r c54b6bbf215a src/doveadm/doveadm-cmd.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/doveadm/doveadm-cmd.c	Wed Nov 12 01:04:27 2014 +0200
@@ -0,0 +1,108 @@
+/* Copyright (c) 2009-2014 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "doveadm-cmd.h"
+
+static struct doveadm_cmd *doveadm_commands[] = {
+	&doveadm_cmd_stop,
+	&doveadm_cmd_reload,
+	&doveadm_cmd_who,
+	&doveadm_cmd_penalty,
+	&doveadm_cmd_kick,
+	&doveadm_cmd_mailbox_mutf7,
+	&doveadm_cmd_sis_deduplicate,
+	&doveadm_cmd_sis_find,
+	&doveadm_cmd_stats_dump
+};
+
+ARRAY_TYPE(doveadm_cmd) doveadm_cmds;
+
+void doveadm_register_cmd(const struct doveadm_cmd *cmd)
+{
+	array_append(&doveadm_cmds, cmd, 1);
+}
+
+static const struct doveadm_cmd *
+doveadm_cmd_find_multi_word(const struct doveadm_cmd *cmd,
+			    const char *cmdname, int *_argc, char **_argv[])
+{
+	int argc = *_argc;
+	char **argv = *_argv;
+	const struct doveadm_cmd *subcmd;
+	unsigned int len;
+
+	if (argc < 2)
+		return NULL;
+
+	len = strlen(argv[1]);
+	if (strncmp(cmdname, argv[1], len) != 0)
+		return NULL;
+
+	argc--; argv++;
+	if (cmdname[len] == ' ') {
+		/* more args */
+		subcmd = doveadm_cmd_find_multi_word(cmd, cmdname + len + 1,
+						     &argc, &argv);
+		if (subcmd == NULL)
+			return NULL;
+	} else {
+		if (cmdname[len] != '\0')
+			return NULL;
+	}
+
+	*_argc = argc;
+	*_argv = argv;
+	return cmd;
+}
+
+const struct doveadm_cmd *
+doveadm_cmd_find(const char *cmd_name, int *argc, char **argv[])
+{
+	const struct doveadm_cmd *cmd, *subcmd;
+	unsigned int cmd_name_len;
+
+	i_assert(*argc > 0);
+
+	cmd_name_len = strlen(cmd_name);
+	array_foreach(&doveadm_cmds, cmd) {
+		if (strcmp(cmd->name, cmd_name) == 0)
+			return cmd;
+
+		/* see if it matches a multi-word command */
+		if (strncmp(cmd->name, cmd_name, cmd_name_len) == 0 &&
+		    cmd->name[cmd_name_len] == ' ') {
+			const char *subcmd_name = cmd->name + cmd_name_len + 1;
+
+			subcmd = doveadm_cmd_find_multi_word(cmd, subcmd_name,
+							     argc, argv);
+			if (subcmd != NULL)
+				return subcmd;
+		}
+	}
+	return NULL;
+}
+
+void doveadm_cmds_init(void)
+{
+	unsigned int i;
+
+	i_array_init(&doveadm_cmds, 32);
+	for (i = 0; i < N_ELEMENTS(doveadm_commands); i++)
+		doveadm_register_cmd(doveadm_commands[i]);
+
+	doveadm_register_auth_commands();
+	doveadm_register_director_commands();
+	doveadm_register_instance_commands();
+	doveadm_register_mount_commands();
+	doveadm_register_proxy_commands();
+	doveadm_register_log_commands();
+	doveadm_register_replicator_commands();
+	doveadm_register_dict_commands();
+	doveadm_register_fs_commands();
+}
+
+void doveadm_cmds_deinit(void)
+{
+	array_free(&doveadm_cmds);
+}
diff -r e4a3e8554663 -r c54b6bbf215a src/doveadm/doveadm-cmd.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/doveadm/doveadm-cmd.h	Wed Nov 12 01:04:27 2014 +0200
@@ -0,0 +1,46 @@
+#ifndef DOVEADM_CMD_H
+#define DOVEADM_CMD_H
+
+typedef void doveadm_command_t(int argc, char *argv[]);
+
+struct doveadm_cmd {
+	doveadm_command_t *cmd;
+	const char *name;
+	const char *short_usage;
+};
+ARRAY_DEFINE_TYPE(doveadm_cmd, struct doveadm_cmd);
+extern ARRAY_TYPE(doveadm_cmd) doveadm_cmds;
+
+extern struct doveadm_cmd doveadm_cmd_stop;
+extern struct doveadm_cmd doveadm_cmd_reload;
+extern struct doveadm_cmd doveadm_cmd_dump;
+extern struct doveadm_cmd doveadm_cmd_pw;
+extern struct doveadm_cmd doveadm_cmd_who;
+extern struct doveadm_cmd doveadm_cmd_penalty;
+extern struct doveadm_cmd doveadm_cmd_kick;
+extern struct doveadm_cmd doveadm_cmd_mailbox_mutf7;
+extern struct doveadm_cmd doveadm_cmd_sis_deduplicate;
+extern struct doveadm_cmd doveadm_cmd_sis_find;
+extern struct doveadm_cmd doveadm_cmd_stats_dump;
+extern struct doveadm_cmd doveadm_cmd_stats_top;
+extern struct doveadm_cmd doveadm_cmd_zlibconnect;
+
+void doveadm_register_cmd(const struct doveadm_cmd *cmd);
+
+const struct doveadm_cmd *
+doveadm_cmd_find(const char *cmd_name, int *argc, char **argv[]);
+
+void doveadm_register_auth_commands(void);
+void doveadm_register_director_commands(void);
+void doveadm_register_proxy_commands(void);
+void doveadm_register_log_commands(void);
+void doveadm_register_instance_commands(void);
+void doveadm_register_mount_commands(void);
+void doveadm_register_replicator_commands(void);
+void doveadm_register_dict_commands(void);
+void doveadm_register_fs_commands(void);
+
+void doveadm_cmds_init(void);
+void doveadm_cmds_deinit(void);
+
+#endif
diff -r e4a3e8554663 -r c54b6bbf215a src/doveadm/doveadm.c
--- a/src/doveadm/doveadm.c	Wed Nov 12 00:52:44 2014 +0200
+++ b/src/doveadm/doveadm.c	Wed Nov 12 01:04:27 2014 +0200
@@ -28,8 +28,6 @@
 
 int doveadm_exit_code = 0;
 
-static ARRAY(struct doveadm_cmd) doveadm_cmds;
-
 static void failure_exit_callback(int *status)
 {
 	enum fatal_exit_status fatal_status = *status;
@@ -46,11 +44,6 @@
 	}
 }
 
-void doveadm_register_cmd(const struct doveadm_cmd *cmd)
-{
-	array_append(&doveadm_cmds, cmd, 1);
-}
-
 static void
 doveadm_usage_compress_lines(FILE *out, const char *str, const char *prefix)
 {
@@ -199,66 +192,6 @@
 	i_fatal("execv(%s) failed: %m", argv[0]);
 }
 
-static const struct doveadm_cmd *
-doveadm_cmd_find_multi_word(const struct doveadm_cmd *cmd,
-			    const char *cmdname, int *_argc, char **_argv[])
-{
-	int argc = *_argc;
-	char **argv = *_argv;
-	const struct doveadm_cmd *subcmd;
-	unsigned int len;
-
-	if (argc < 2)
-		return NULL;
-
-	len = strlen(argv[1]);
-	if (strncmp(cmdname, argv[1], len) != 0)
-		return NULL;
-
-	argc--; argv++;
-	if (cmdname[len] == ' ') {
-		/* more args */
-		subcmd = doveadm_cmd_find_multi_word(cmd, cmdname + len + 1,
-						     &argc, &argv);
-		if (subcmd == NULL)
-			return NULL;
-	} else {
-		if (cmdname[len] != '\0')
-			return NULL;
-	}
-
-	*_argc = argc;
-	*_argv = argv;
-	return cmd;
-}
-
-static const struct doveadm_cmd *
-doveadm_cmd_find(const char *cmd_name, int *argc, char **argv[])
-{
-	const struct doveadm_cmd *cmd, *subcmd;
-	unsigned int cmd_name_len;
-
-	i_assert(*argc > 0);
-
-	cmd_name_len = strlen(cmd_name);
-	array_foreach(&doveadm_cmds, cmd) {
-		if (strcmp(cmd->name, cmd_name) == 0)
-			return cmd;
-
-		/* see if it matches a multi-word command */
-		if (strncmp(cmd->name, cmd_name, cmd_name_len) == 0 &&
-		    cmd->name[cmd_name_len] == ' ') {
-			const char *subcmd_name = cmd->name + cmd_name_len + 1;
-
-			subcmd = doveadm_cmd_find_multi_word(cmd, subcmd_name,
-							     argc, argv);
-			if (subcmd != NULL)
-				return subcmd;
-		}
-	}
-	return NULL;
-}
-
 static bool doveadm_try_run(const char *cmd_name, int argc, char *argv[])
 {
 	const struct doveadm_cmd *cmd;
@@ -312,21 +245,12 @@
 					pool_datastack_create());
 }
 
-static struct doveadm_cmd *doveadm_commands[] = {
+static struct doveadm_cmd *doveadm_cmdline_commands[] = {
 	&doveadm_cmd_help,
 	&doveadm_cmd_config,
 	&doveadm_cmd_exec,
-	&doveadm_cmd_stop,
-	&doveadm_cmd_reload,
 	&doveadm_cmd_dump,
 	&doveadm_cmd_pw,
-	&doveadm_cmd_who,
-	&doveadm_cmd_penalty,
-	&doveadm_cmd_kick,
-	&doveadm_cmd_mailbox_mutf7,
-	&doveadm_cmd_sis_deduplicate,
-	&doveadm_cmd_sis_find,
-	&doveadm_cmd_stats_dump,
 	&doveadm_cmd_stats_top,
 	&doveadm_cmd_zlibconnect
 };
@@ -374,10 +298,9 @@
 		doveadm_read_settings();
 	}
 
-	i_array_init(&doveadm_cmds, 32);


More information about the dovecot-cvs mailing list