From 18c2d3940d64558ee513766e08528024ca69e2d4 Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Wed, 22 Aug 2018 13:38:42 +0100 Subject: [PATCH] add name and magic to MxOps --- compress.c | 30 +++++++++++--------- context.h | 2 +- imap/imap.c | 2 ++ maildir/mh.c | 4 +++ mbox/mbox.c | 4 +++ mx.c | 64 ++++++++++++++++++++---------------------- mx.h | 4 ++- nntp/nntp.c | 2 ++ notmuch/mutt_notmuch.c | 2 ++ pop/pop.c | 2 ++ 10 files changed, 67 insertions(+), 49 deletions(-) diff --git a/compress.c b/compress.c index 7371bcc14..b2ecff4f9 100644 --- a/compress.c +++ b/compress.c @@ -62,13 +62,13 @@ struct Header; */ struct CompressInfo { - const char *append; /**< append-hook command */ - const char *close; /**< close-hook command */ - const char *open; /**< open-hook command */ - off_t size; /**< size of the compressed file */ - struct MxOps *child_ops; /**< callbacks of de-compressed file */ - int locked; /**< if realpath is locked */ - FILE *lockfp; /**< fp used for locking */ + const char *append; /**< append-hook command */ + const char *close; /**< close-hook command */ + const char *open; /**< open-hook command */ + off_t size; /**< size of the compressed file */ + const struct MxOps *child_ops; /**< callbacks of de-compressed file */ + int locked; /**< if realpath is locked */ + FILE *lockfp; /**< fp used for locking */ }; /** @@ -587,7 +587,7 @@ static int comp_mbox_close(struct Context *ctx) if (!ci) return -1; - struct MxOps *ops = ci->child_ops; + const struct MxOps *ops = ci->child_ops; if (!ops) { free_compress_info(ctx); @@ -667,7 +667,7 @@ static int comp_mbox_check(struct Context *ctx, int *index_hint) if (!ci) return -1; - struct MxOps *ops = ci->child_ops; + const struct MxOps *ops = ci->child_ops; if (!ops) return -1; @@ -702,7 +702,7 @@ static int comp_msg_open(struct Context *ctx, struct Message *msg, int msgno) if (!ci) return -1; - struct MxOps *ops = ci->child_ops; + const struct MxOps *ops = ci->child_ops; if (!ops) return -1; @@ -722,7 +722,7 @@ static int comp_msg_close(struct Context *ctx, struct Message *msg) if (!ci) return -1; - struct MxOps *ops = ci->child_ops; + const struct MxOps *ops = ci->child_ops; if (!ops) return -1; @@ -742,7 +742,7 @@ static int comp_msg_commit(struct Context *ctx, struct Message *msg) if (!ci) return -1; - struct MxOps *ops = ci->child_ops; + const struct MxOps *ops = ci->child_ops; if (!ops) return -1; @@ -762,7 +762,7 @@ static int comp_msg_open_new(struct Context *ctx, struct Message *msg, struct He if (!ci) return -1; - struct MxOps *ops = ci->child_ops; + const struct MxOps *ops = ci->child_ops; if (!ops) return -1; @@ -842,7 +842,7 @@ static int comp_mbox_sync(struct Context *ctx, int *index_hint) return -1; } - struct MxOps *ops = ci->child_ops; + const struct MxOps *ops = ci->child_ops; if (!ops) return -1; @@ -900,6 +900,8 @@ int mutt_comp_valid_command(const char *cmd) * The message functions are delegated to mbox. */ struct MxOps mx_comp_ops = { + .magic = MUTT_COMPRESSED, + .name = "compressed", .mbox_open = comp_mbox_open, .mbox_open_append = comp_mbox_open_append, .mbox_check = comp_mbox_check, diff --git a/context.h b/context.h index 92c224d19..6b02c2b84 100644 --- a/context.h +++ b/context.h @@ -102,7 +102,7 @@ struct Context /* driver hooks */ void *data; /**< driver specific data */ - struct MxOps *mx_ops; + const struct MxOps *mx_ops; }; #endif /* _MUTT_CONTEXT_H */ diff --git a/imap/imap.c b/imap/imap.c index 1c2902e38..4c921b050 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -2724,6 +2724,8 @@ static int imap_tags_commit(struct Context *ctx, struct Header *hdr, char *buf) * struct mx_imap_ops - Mailbox callback functions for IMAP mailboxes */ struct MxOps mx_imap_ops = { + .magic = MUTT_IMAP, + .name = "imap", .mbox_open = imap_mbox_open, .mbox_open_append = imap_mbox_open_append, .mbox_check = imap_mbox_check, diff --git a/maildir/mh.c b/maildir/mh.c index 7fa01119a..131ae479e 100644 --- a/maildir/mh.c +++ b/maildir/mh.c @@ -2928,6 +2928,8 @@ bool mx_is_mh(const char *path) * struct mx_maildir_ops - Mailbox callback functions for Maildir mailboxes */ struct MxOps mx_maildir_ops = { + .magic = MUTT_MAILDIR, + .name = "maildir", .mbox_open = maildir_mbox_open, .mbox_open_append = maildir_mbox_open_append, .mbox_check = maildir_mbox_check, @@ -2945,6 +2947,8 @@ struct MxOps mx_maildir_ops = { * struct mx_mh_ops - Mailbox callback functions for MH mailboxes */ struct MxOps mx_mh_ops = { + .magic = MUTT_MH, + .name = "mh", .mbox_open = mh_mbox_open, .mbox_open_append = mh_mbox_open_append, .mbox_check = mh_mbox_check, diff --git a/mbox/mbox.c b/mbox/mbox.c index 55850b6f9..2607effae 100644 --- a/mbox/mbox.c +++ b/mbox/mbox.c @@ -1327,6 +1327,8 @@ bail: /* Come here in case of disaster */ * struct mx_mbox_ops - Mailbox callback functions for mbox mailboxes */ struct MxOps mx_mbox_ops = { + .magic = MUTT_MBOX, + .name = "mbox", .mbox_open = mbox_mbox_open, .mbox_open_append = mbox_mbox_open_append, .mbox_check = mbox_mbox_check, @@ -1344,6 +1346,8 @@ struct MxOps mx_mbox_ops = { * struct mx_mmdf_ops - Mailbox callback functions for MMDF mailboxes */ struct MxOps mx_mmdf_ops = { + .magic = MUTT_MMDF, + .name = "mmdf", .mbox_open = mbox_mbox_open, .mbox_open_append = mbox_mbox_open_append, .mbox_check = mbox_mbox_check, diff --git a/mx.c b/mx.c index 2c88b0407..0e4de572b 100644 --- a/mx.c +++ b/mx.c @@ -89,46 +89,44 @@ unsigned char Move; ///< Config: Move emails from Spoolfile to Mbox when read char *Trash; ///< Config: Folder to put deleted emails /** - * mx_get_ops - Get mailbox operations - * @param magic Mailbox magic number - * @retval ptr Mailbox function - * @retval NULL Error + * mx_ops - All the Mailbox backends */ -struct MxOps *mx_get_ops(enum MailboxType magic) -{ - switch (magic) - { -#ifdef USE_IMAP - case MUTT_IMAP: - return &mx_imap_ops; -#endif - case MUTT_MAILDIR: - return &mx_maildir_ops; - case MUTT_MBOX: - return &mx_mbox_ops; - case MUTT_MH: - return &mx_mh_ops; - case MUTT_MMDF: - return &mx_mmdf_ops; -#ifdef USE_POP - case MUTT_POP: - return &mx_pop_ops; -#endif +const struct MxOps *mx_ops[] = { #ifdef USE_COMPRESSED - case MUTT_COMPRESSED: - return &mx_comp_ops; + &mx_comp_ops, +#endif +#ifdef USE_IMAP + &mx_imap_ops, #endif + &mx_maildir_ops, + &mx_mbox_ops, + &mx_mh_ops, + &mx_mmdf_ops, #ifdef USE_NNTP - case MUTT_NNTP: - return &mx_nntp_ops; + &mx_nntp_ops, #endif #ifdef USE_NOTMUCH - case MUTT_NOTMUCH: - return &mx_notmuch_ops; + &mx_notmuch_ops, #endif - default: - return NULL; - } +#ifdef USE_POP + &mx_pop_ops, +#endif + NULL, +}; + +/** + * mx_get_ops - Get mailbox operations + * @param magic Mailbox magic number + * @retval ptr Mailbox function + * @retval NULL Error + */ +const struct MxOps *mx_get_ops(int magic) +{ + for (const struct MxOps **ops = mx_ops; *ops; ops++) + if ((*ops)->magic == magic) + return *ops; + + return NULL; } /** diff --git a/mx.h b/mx.h index 771f06d5a..5ef407807 100644 --- a/mx.h +++ b/mx.h @@ -51,6 +51,8 @@ extern char * Trash; */ struct MxOps { + const int magic; + const char *name; /** * mbox_open - Open a mailbox * @param ctx Mailbox to open @@ -155,7 +157,7 @@ void mx_alloc_memory(struct Context *ctx); void mx_update_context(struct Context *ctx, int new_messages); void mx_update_tables(struct Context *ctx, bool committing); -struct MxOps *mx_get_ops(enum MailboxType magic); +const struct MxOps *mx_get_ops(enum MailboxType magic); /* This variable is backing for a config item */ WHERE short MboxType; ///< Config: Default type for creating new mailboxes diff --git a/nntp/nntp.c b/nntp/nntp.c index c57414c68..abc7f5d25 100644 --- a/nntp/nntp.c +++ b/nntp/nntp.c @@ -2630,6 +2630,8 @@ int nntp_compare_order(const void *a, const void *b) * struct mx_nntp_ops - Mailbox callback functions for NNTP mailboxes */ struct MxOps mx_nntp_ops = { + .magic = MUTT_NNTP, + .name = "nntp", .mbox_open = nntp_mbox_open, .mbox_open_append = NULL, .mbox_check = nntp_mbox_check, diff --git a/notmuch/mutt_notmuch.c b/notmuch/mutt_notmuch.c index 93fa85fc0..f2ed83ecc 100644 --- a/notmuch/mutt_notmuch.c +++ b/notmuch/mutt_notmuch.c @@ -2765,6 +2765,8 @@ static int nm_msg_commit(struct Context *ctx, struct Message *msg) * struct mx_notmuch_ops - Mailbox callback functions for Notmuch mailboxes */ struct MxOps mx_notmuch_ops = { + .magic = MUTT_NOTMUCH, + .name = "notmuch", .mbox_open = nm_mbox_open, /* calls init_context() */ .mbox_open_append = NULL, .mbox_check = nm_mbox_check, diff --git a/pop/pop.c b/pop/pop.c index ade7b0e40..e3a80dfd2 100644 --- a/pop/pop.c +++ b/pop/pop.c @@ -1061,6 +1061,8 @@ fail: * mx_pop_ops - Mailbox callback functions for POP mailboxes */ struct MxOps mx_pop_ops = { + .magic = MUTT_POP, + .name = "pop", .mbox_open = pop_mbox_open, .mbox_open_append = NULL, .mbox_check = pop_mbox_check, -- 2.40.0