From: Pierre Habouzit Date: Sat, 18 Nov 2006 13:15:06 +0000 (+0100) Subject: no more list2_t for mx's anymore either. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=2ce8de509852f47a90f834b3e8d047e054810309;p=apps%2Fmadmutt.git no more list2_t for mx's anymore either. also drop the stupid mx_*_reg functions, rather use exter const mx_t's. Signed-off-by: Pierre Habouzit --- diff --git a/compress.c b/compress.c index e97fc8d..f4b8b67 100644 --- a/compress.c +++ b/compress.c @@ -461,13 +461,17 @@ int mutt_slow_close_compressed (CONTEXT * ctx) return (0); } -mx_t* compress_reg_mx (void) { - mx_t* fmt = p_new(mx_t, 1); - fmt->type = M_COMPRESSED; - fmt->local = 1; - fmt->mx_is_magic = mbox_is_magic; - fmt->mx_check_empty = mbox_check_empty; - fmt->mx_access = access; - fmt->mx_open_mailbox = mutt_open_read_compressed; - return (fmt); -} +mx_t const compress_mx = { + M_COMPRESSED, + 1, + mbox_is_magic, + mbox_check_empty, + access, + mutt_open_read_compressed, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, +}; diff --git a/compress.h b/compress.h index 50ac597..708e3ee 100644 --- a/compress.h +++ b/compress.h @@ -21,6 +21,6 @@ int mutt_test_compress_command (const char *); int mutt_check_mailbox_compressed (CONTEXT *); void mutt_fast_close_compressed (CONTEXT *); -mx_t* compress_reg_mx (void); +extern mx_t const compress_mx; #endif /* _COMPRESS_H */ diff --git a/imap/mx_imap.c b/imap/mx_imap.c index 2d9e333..17147e5 100644 --- a/imap/mx_imap.c +++ b/imap/mx_imap.c @@ -56,19 +56,17 @@ static int imap_commit_message (MESSAGE* msg, CONTEXT* ctx) { return (r); } -mx_t* imap_reg_mx (void) { - mx_t* fmt = p_new(mx_t, 1); - - /* make up mx_t record... */ - fmt->type = M_IMAP; - fmt->mx_is_magic = imap_is_magic; - fmt->mx_access = imap_access; - fmt->mx_open_mailbox = imap_open_mailbox; - fmt->mx_open_new_message = imap_open_new_message; - fmt->mx_acl_check = acl_check_imap; - fmt->mx_fastclose_mailbox = imap_close_mailbox; - fmt->mx_sync_mailbox = imap_sync_mailbox; - fmt->mx_check_mailbox = _imap_check_mailbox; - fmt->mx_commit_message = imap_commit_message; - return (fmt); -} +mx_t const imap_mx = { + M_IMAP, + 0, + imap_is_magic, + NULL, + imap_access, + imap_open_mailbox, + imap_open_new_message, + acl_check_imap, + _imap_check_mailbox, + imap_close_mailbox, + imap_sync_mailbox, + imap_commit_message, +}; diff --git a/imap/mx_imap.h b/imap/mx_imap.h index e4758c3..f2e5ddd 100644 --- a/imap/mx_imap.h +++ b/imap/mx_imap.h @@ -13,7 +13,8 @@ #include "mx.h" -int imap_is_magic (const char*, struct stat*); -mx_t* imap_reg_mx (void); +extern mx_t const imap_mx; + +int imap_is_magic(const char*, struct stat*); #endif /* !_IMAP_MX_H */ diff --git a/main.c b/main.c index 4551a3d..e3778f5 100644 --- a/main.c +++ b/main.c @@ -665,7 +665,6 @@ int main (int argc, char **argv) start_curses (); /* set defaults and read init files */ - mx_init (); mutt_init (flags & M_NOSYSRC, commands); string_list_wipe(&commands); diff --git a/mbox.c b/mbox.c index 442a406..1c10657 100644 --- a/mbox.c +++ b/mbox.c @@ -1070,28 +1070,32 @@ static int mmdf_commit_message (MESSAGE* msg, CONTEXT* ctx) { return (commit_message (msg, ctx, 0)); } -static mx_t* reg_mx (void) { - mx_t* fmt = p_new(mx_t, 1); - fmt->local = 1; - fmt->mx_check_empty = mbox_check_empty; - fmt->mx_is_magic = mbox_is_magic; - fmt->mx_access = access; - fmt->mx_open_mailbox = mbox_open_mailbox; - fmt->mx_open_new_message = mbox_open_new_message; - fmt->mx_sync_mailbox = mbox_sync_mailbox; - fmt->mx_check_mailbox = mbox_check_mailbox; - return (fmt); -} +mx_t const mbox_mx = { + M_MBOX, + 1, + mbox_is_magic, + mbox_check_empty, + access, + mbox_open_mailbox, + mbox_open_new_message, + NULL, + mbox_check_mailbox, + NULL, + mbox_sync_mailbox, + mbox_commit_message, +}; -mx_t* mbox_reg_mx (void) { - mx_t* fmt = reg_mx (); - fmt->type = M_MBOX; - fmt->mx_commit_message = mbox_commit_message; - return (fmt); -} -mx_t* mmdf_reg_mx (void) { - mx_t* fmt = reg_mx (); - fmt->type = M_MMDF; - fmt->mx_commit_message = mmdf_commit_message; - return (fmt); -} +mx_t const mmdf_mx = { + M_MMDF, + 1, + mbox_is_magic, + mbox_check_empty, + access, + mbox_open_mailbox, + mbox_open_new_message, + NULL, + mbox_check_mailbox, + NULL, + mbox_sync_mailbox, + mmdf_commit_message, +}; diff --git a/mbox.h b/mbox.h index 238d838..fc9a6fd 100644 --- a/mbox.h +++ b/mbox.h @@ -17,6 +17,9 @@ #include "mx.h" +extern mx_t const mbox_mx; +extern mx_t const mmdf_mx; + /* TODO all of these must disappear to achieve good information hiding */ #define MMDF_SEP "\001\001\001\001\n" @@ -26,7 +29,4 @@ int mbox_lock_mailbox (CONTEXT *, int, int); int mbox_check_empty (const char*); int mbox_is_magic (const char*, struct stat*); -mx_t* mbox_reg_mx (void); -mx_t* mmdf_reg_mx (void); - #endif diff --git a/mh.c b/mh.c index f40bbd1..bfaaab2 100644 --- a/mh.c +++ b/mh.c @@ -1848,15 +1848,6 @@ static int maildir_is_magic (const char* path, struct stat* st) { return (-1); } -/* routines common to maildir and mh */ -static mx_t* reg_mx (void) { - mx_t* fmt = p_new(mx_t, 1); - fmt->local = 1; - fmt->mx_access = access; - fmt->mx_sync_mailbox = mh_sync_mailbox; - return (fmt); -} - static int mh_commit (MESSAGE* msg, CONTEXT* ctx) { return (mh_commit_message (msg, ctx, NULL)); } @@ -1865,26 +1856,32 @@ static int maildir_commit (MESSAGE* msg, CONTEXT* ctx) { return (maildir_commit_message (msg, ctx, NULL)); } -mx_t* mh_reg_mx (void) { - mx_t* fmt = reg_mx (); - fmt->type = M_MH; - fmt->mx_check_empty = mh_check_empty; - fmt->mx_is_magic = mh_is_magic; - fmt->mx_open_mailbox = mh_read_dir; - fmt->mx_open_new_message = mh_open_new_message; - fmt->mx_check_mailbox = mh_check_mailbox; - fmt->mx_commit_message = mh_commit; - return (fmt); -} +mx_t const maildir_mx = { + M_MAILDIR, + 1, + maildir_is_magic, + maildir_check_empty, + access, + maildir_read_dir, + maildir_open_new_message, + NULL, + maildir_check_mailbox, + NULL, + mh_sync_mailbox, + maildir_commit +}; -mx_t* maildir_reg_mx (void) { - mx_t* fmt = reg_mx (); - fmt->type = M_MAILDIR; - fmt->mx_check_empty = maildir_check_empty; - fmt->mx_is_magic = maildir_is_magic; - fmt->mx_open_mailbox = maildir_read_dir; - fmt->mx_open_new_message = maildir_open_new_message; - fmt->mx_check_mailbox = maildir_check_mailbox; - fmt->mx_commit_message = maildir_commit; - return (fmt); -} +mx_t const mh_mx = { + M_MH, + 1, + mh_is_magic, + mh_check_empty, + access, + mh_read_dir, + mh_open_new_message, + NULL, + mh_check_mailbox, + NULL, + mh_sync_mailbox, + mh_commit +}; diff --git a/mh.h b/mh.h index 4764f7a..77722a8 100644 --- a/mh.h +++ b/mh.h @@ -17,15 +17,13 @@ #include "mx.h" +extern mx_t const maildir_mx; +extern mx_t const mh_mx; + /* TODO all of these must disappear to achieve good information hiding */ int mh_buffy (const char *); FILE *maildir_open_find_message (const char *, const char *); int mh_valid_message (const char *); -/* these are the only publicly visible for usage */ - -mx_t* maildir_reg_mx (void); -mx_t* mh_reg_mx (void); - #endif /* !_MH_H */ diff --git a/mx.c b/mx.c index fde597c..2f127c1 100644 --- a/mx.c +++ b/mx.c @@ -46,11 +46,21 @@ #include "lib/list.h" -static list2_t* MailboxFormats = NULL; -#define MX_COMMAND(idx,cmd) ((mx_t*) MailboxFormats->data[idx])->cmd -#define MX_IDX(idx) (idx >= 0 && idx < MailboxFormats->length) +static mx_t const *mxfmts[] = { + &mbox_mx, + &mmdf_mx, + &mh_mx, + &maildir_mx, + &imap_mx, + &pop_mx, +#ifdef USE_NNTP + &nntp_mx, +#endif + &compress_mx, +}; -#define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0) +#define MX_IDX(idx) (idx >= 0 && idx < countof(mxfmts)) +#define mutt_is_spool(s) (m_strcmp(Spoolfile, s) == 0) #ifdef USE_DOTLOCK /* parameters: @@ -119,29 +129,29 @@ static int undotlock_file (const char *path) #endif /* USE_DOTLOCK */ -/* looks up index of type for path in MailboxFormats */ +/* looks up index of type for path in mxfmts */ static int mx_get_idx (const char* path) { - int i = 0, t = 0; - struct stat st; - - /* first, test all non-local folders to avoid stat() call */ - for (i = 0; i < MailboxFormats->length; i++) { - if (!MX_COMMAND(i,local)) - t = MX_COMMAND(i,mx_is_magic)(path, NULL); - if (t >= 1) - return (t-1); - } - if (stat (path, &st) == 0) { - /* if stat() succeeded, keep testing until success and - * pass stat() info so that we only need to do it once */ - for (i = 0; i < MailboxFormats->length; i++) { - if (MX_COMMAND(i,local)) - t = MX_COMMAND(i,mx_is_magic)(path, &st); - if (t >= 1) - return (t-1); + int i = 0, t = 0; + struct stat st; + + /* first, test all non-local folders to avoid stat() call */ + for (i = 0; i < countof(mxfmts); i++) { + if (!mxfmts[i]->local) + t = mxfmts[i]->mx_is_magic(path, NULL); + if (t >= 1) + return (t-1); } - } - return (-1); + if (stat (path, &st) == 0) { + /* if stat() succeeded, keep testing until success and + * pass stat() info so that we only need to do it once */ + for (i = 0; i < countof(mxfmts); i++) { + if (mxfmts[i]->local) + t = mxfmts[i]->mx_is_magic(path, &st); + if (t >= 1) + return (t-1); + } + } + return (-1); } /* Args: @@ -314,14 +324,14 @@ int mx_get_magic (const char *path) { if (m_strlen(path) == 0) return (-1); if ((i = mx_get_idx (path)) >= 0) - return (MX_COMMAND(i,type)); + return (mxfmts[i]->type); return (-1); } int mx_is_local (int m) { if (!MX_IDX(m)) return (0); - return (MX_COMMAND(m,local)); + return (mxfmts[m]->local); } /* @@ -350,8 +360,8 @@ int mx_access (const char *path, int flags) { int i = 0; - if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_access)) - return (MX_COMMAND(i,mx_access)(path,flags)); + if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_access) + return (mxfmts[i]->mx_access(path,flags)); return (0); } @@ -534,7 +544,7 @@ CONTEXT *mx_open_mailbox (const char *path, int flags, CONTEXT * pctx) if (!ctx->quiet) mutt_message (_("Reading %s..."), ctx->path); - rc = MX_COMMAND(ctx->magic-1,mx_open_mailbox)(ctx); + rc = mxfmts[ctx->magic-1]->mx_open_mailbox(ctx); if (rc == 0) { if ((flags & M_NOSORT) == 0) { @@ -565,8 +575,8 @@ void mx_fastclose_mailbox (CONTEXT * ctx) if (!ctx) return; - if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox)) - MX_COMMAND(ctx->magic-1,mx_fastclose_mailbox(ctx)); + if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_fastclose_mailbox) + mxfmts[ctx->magic-1]->mx_fastclose_mailbox(ctx); if (ctx->subj_hash) hash_destroy (&ctx->subj_hash, NULL); if (ctx->id_hash) @@ -598,7 +608,7 @@ static int sync_mailbox (CONTEXT * ctx, int *index_hint) if (MX_IDX(ctx->magic-1)) /* the 1 is only of interest for IMAP and means EXPUNGE */ - rc = MX_COMMAND(ctx->magic-1,mx_sync_mailbox(ctx,1,index_hint)); + rc = mxfmts[ctx->magic-1]->mx_sync_mailbox(ctx,1,index_hint); if (rc == 0 && ctx->compressinfo) return mutt_sync_compressed (ctx); @@ -1083,7 +1093,7 @@ MESSAGE *mx_open_new_message (CONTEXT * dest, HEADER * hdr, int flags) if (msg->received == 0) time (&msg->received); - if (MX_COMMAND(dest->magic-1,mx_open_new_message)(msg, dest, hdr) == 0) { + if (mxfmts[dest->magic-1]->mx_open_new_message(msg, dest, hdr) == 0) { if (dest->magic == M_MMDF) fputs (MMDF_SEP, msg->fp); @@ -1115,8 +1125,8 @@ int mx_check_mailbox (CONTEXT * ctx, int *index_hint, int lock) { if (ctx) { if (ctx->locked) lock = 0; - if (MX_IDX(ctx->magic-1) && MX_COMMAND(ctx->magic-1,mx_check_mailbox)) - return (MX_COMMAND(ctx->magic-1,mx_check_mailbox)(ctx, index_hint, lock)); + if (MX_IDX(ctx->magic-1) && mxfmts[ctx->magic-1]->mx_check_mailbox) + return (mxfmts[ctx->magic-1]->mx_check_mailbox(ctx, index_hint, lock)); } return (-1); @@ -1189,9 +1199,9 @@ int mx_commit_message (MESSAGE * msg, CONTEXT * ctx) { if (!(msg->write && ctx->append)) { return -1; } - if (!ctx || !MX_IDX(ctx->magic-1) || !MX_COMMAND(ctx->magic-1,mx_commit_message)) + if (!ctx || !MX_IDX(ctx->magic-1) || !mxfmts[ctx->magic-1]->mx_commit_message) return (-1); - return (MX_COMMAND(ctx->magic-1,mx_commit_message) (msg, ctx)); + return (mxfmts[ctx->magic-1]->mx_commit_message (msg, ctx)); } /* close a pointer to a message */ @@ -1320,8 +1330,8 @@ void mx_update_context (CONTEXT * ctx, int new_messages) int mx_check_empty (const char *path) { int i = 0; - if ((i = mx_get_idx (path)) >= 0 && MX_COMMAND(i,mx_check_empty)) - return (MX_COMMAND(i,mx_check_empty)(path)); + if ((i = mx_get_idx (path)) >= 0 && mxfmts[i]->mx_check_empty) + return (mxfmts[i]->mx_check_empty(path)); errno = EINVAL; return (-1); } @@ -1330,22 +1340,9 @@ int mx_acl_check (CONTEXT* ctx, int flag) { if (!ctx || !MX_IDX(ctx->magic-1)) return (0); /* if no acl_check defined for module, assume permission is granted */ - if (!MX_COMMAND(ctx->magic-1,mx_acl_check)) + if (!mxfmts[ctx->magic-1]->mx_acl_check) return (1); - return (MX_COMMAND(ctx->magic-1,mx_acl_check)(ctx,flag)); -} - -void mx_init (void) { - list_push_back (&MailboxFormats, (void*) mbox_reg_mx ()); - list_push_back (&MailboxFormats, (void*) mmdf_reg_mx ()); - list_push_back (&MailboxFormats, (void*) mh_reg_mx ()); - list_push_back (&MailboxFormats, (void*) maildir_reg_mx ()); - list_push_back (&MailboxFormats, (void*) imap_reg_mx ()); - list_push_back (&MailboxFormats, (void*) pop_reg_mx ()); -#ifdef USE_NNTP - list_push_back (&MailboxFormats, (void*) nntp_reg_mx ()); -#endif - list_push_back (&MailboxFormats, (void*) compress_reg_mx ()); + return (mxfmts[ctx->magic-1]->mx_acl_check(ctx,flag)); } int mx_rebuild_cache (void) { diff --git a/mx.h b/mx.h index 60f7782..efc732b 100644 --- a/mx.h +++ b/mx.h @@ -20,10 +20,8 @@ #include #include "mutt.h" -/* - * supported mailbox formats - * in mx_init() the registration order must be exactly as given here!!!1! - */ +/* supported mailbox formats + XXX: has to be in the same order than mxfmts in mx.c */ enum { M_MBOX = 1, M_MMDF, @@ -72,7 +70,7 @@ typedef struct { time_t received; /* the time at which this message was received */ } MESSAGE; -typedef struct { +typedef struct mx_t { /* folder magic */ int type; /* may we stat() it? */ @@ -100,9 +98,6 @@ typedef struct { int (*mx_commit_message) (MESSAGE*, CONTEXT*); } mx_t; -/* called from main: init all folder types */ -void mx_init (void); - /* flags for mx_open_mailbox() */ #define M_NOSORT (1<<0) /* do not sort the mailbox after opening it */ #define M_APPEND (1<<1) /* open mailbox for appending messages */ diff --git a/nntp/mx_nntp.c b/nntp/mx_nntp.c index 8943c9f..08eee9a 100644 --- a/nntp/mx_nntp.c +++ b/nntp/mx_nntp.c @@ -30,17 +30,17 @@ static int acl_check_nntp (CONTEXT* ctx, int bit) { } } -/* called by nntp_init(); don't call elsewhere */ -mx_t* nntp_reg_mx (void) { - mx_t* fmt = p_new(mx_t, 1); - - /* make up mx_t record... */ - fmt->type = M_NNTP; - fmt->mx_is_magic = nntp_is_magic; - fmt->mx_open_mailbox = nntp_open_mailbox; - fmt->mx_acl_check = acl_check_nntp; - fmt->mx_fastclose_mailbox = nntp_fastclose_mailbox; - fmt->mx_sync_mailbox = nntp_sync_mailbox; - fmt->mx_check_mailbox = nntp_check_mailbox; - return (fmt); -} +mx_t const nntp_mx = { + M_NNTP, + 0, + nntp_is_magic, + NULL, + NULL, + nntp_open_mailbox, + NULL, + acl_check_nntp, + nntp_check_mailbox, + nntp_fastclose_mailbox, + nntp_sync_mailbox, + NULL, +}; diff --git a/nntp/mx_nntp.h b/nntp/mx_nntp.h index 28926a5..9e44ca1 100644 --- a/nntp/mx_nntp.h +++ b/nntp/mx_nntp.h @@ -13,6 +13,6 @@ #include "mx.h" -mx_t* nntp_reg_mx (void); +extern mx_t const nntp_mx; #endif /* !_NNTP_MX_H */ diff --git a/pop/mx_pop.c b/pop/mx_pop.c index fd4ab46..b75ba14 100644 --- a/pop/mx_pop.c +++ b/pop/mx_pop.c @@ -29,16 +29,17 @@ static int acl_check_pop (CONTEXT* ctx __attribute__ ((unused)), int bit) { } } -mx_t* pop_reg_mx (void) { - mx_t* fmt = p_new(mx_t, 1); - - /* make up mx_t record... */ - fmt->type = M_POP; - fmt->mx_is_magic = pop_is_magic; - fmt->mx_open_mailbox = pop_open_mailbox; - fmt->mx_acl_check = acl_check_pop; - fmt->mx_fastclose_mailbox = pop_close_mailbox; - fmt->mx_sync_mailbox = pop_sync_mailbox; - fmt->mx_check_mailbox = pop_check_mailbox; - return (fmt); -} +mx_t const pop_mx = { + M_POP, + 0, + pop_is_magic, + NULL, + NULL, + pop_open_mailbox, + NULL, + acl_check_pop, + pop_check_mailbox, + pop_close_mailbox, + pop_sync_mailbox, + NULL, +}; diff --git a/pop/mx_pop.h b/pop/mx_pop.h index cbf57a2..0c75157 100644 --- a/pop/mx_pop.h +++ b/pop/mx_pop.h @@ -13,6 +13,6 @@ #include "mx.h" -mx_t* pop_reg_mx (void); +extern mx_t const pop_mx; #endif /* !_POP_MX_H */