no more list2_t for mx's anymore either.
[apps/madmutt.git] / mx.c
diff --git a/mx.c b/mx.c
index fde597c..2f127c1 100644 (file)
--- a/mx.c
+++ b/mx.c
 
 #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) {