exit system.c, mutt_system goes into lib-sys/
[apps/madmutt.git] / compress.c
index 1ac8355..aa453bb 100644 (file)
@@ -1,34 +1,33 @@
 /*
+ * Copyright notice from original mutt:
  * Copyright (C) 1997 Alain Penders <Alain@Finale-Dev.com>
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * This file is part of mutt-ng, see http://www.muttng.org/.
+ * It's licensed under the GNU General Public License,
+ * please see the file GPL in the top level source directory.
  */
 
-#include "mutt.h"
-
-#ifdef USE_COMPRESSED
-
-#include "mx.h"
-#include "mailbox.h"
-#include "mutt_curses.h"
-
 #include <errno.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/stat.h>
 
+#include <lib-lib/mem.h>
+#include <lib-lib/str.h>
+#include <lib-lib/macros.h>
+#include <lib-lib/file.h>
+#include <lib-lib/debug.h>
+
+#include <lib-sys/mutt_signal.h>
+#include <lib-sys/unix.h>
+
+#include <lib-ui/curses.h>
+
+#include "mutt.h"
+
+#include "mx.h"
+#include "mbox.h"
+
 typedef struct {
   const char *close;            /* close-hook  command */
   const char *open;             /* open-hook   command */
@@ -117,7 +116,7 @@ static COMPRESS_INFO *set_compress_info (CONTEXT * ctx)
   COMPRESS_INFO *ci;
 
   /* Now lets uncompress this thing */
-  ci = safe_malloc (sizeof (COMPRESS_INFO));
+  ci = p_new(COMPRESS_INFO, 1);
   ctx->compressinfo = (void *) ci;
   ci->append = find_compress_hook (M_APPENDHOOK, ctx->path);
   ci->open = find_compress_hook (M_OPENHOOK, ctx->path);
@@ -134,8 +133,7 @@ static void set_path (CONTEXT * ctx)
 
   /* Uncompress to /tmp */
   mutt_mktemp (tmppath);
-  ctx->path = safe_malloc (strlen (tmppath) + 1);
-  strcpy (ctx->path, tmppath);
+  ctx->path = p_dupstr(tmppath, m_strlen(tmppath));
 }
 
 static int get_size (const char *path)
@@ -193,7 +191,7 @@ static char *get_compression_cmd (const char *cmd, const CONTEXT * ctx)
 
   mutt_FormatString (expanded, sizeof (expanded), cmd,
                      compresshook_format_str, (unsigned long) ctx, 0);
-  return safe_strdup (expanded);
+  return m_strdup(expanded);
 }
 
 int mutt_check_mailbox_compressed (CONTEXT * ctx)
@@ -201,8 +199,8 @@ int mutt_check_mailbox_compressed (CONTEXT * ctx)
   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
 
   if (ci->size != get_size (ctx->realpath)) {
-    FREE (&ctx->compressinfo);
-    FREE (&ctx->realpath);
+    p_delete(&ctx->compressinfo);
+    p_delete(&ctx->realpath);
     mutt_error _("Mailbox was corrupted!");
 
     return (-1);
@@ -220,7 +218,7 @@ int mutt_open_read_compressed (CONTEXT * ctx)
 
   if (!ci->open) {
     ctx->magic = 0;
-    FREE (ctx->compressinfo);
+    p_delete(&ctx->compressinfo);
     return (-1);
   }
   if (!ci->close || access (ctx->path, W_OK) != 0)
@@ -235,11 +233,11 @@ int mutt_open_read_compressed (CONTEXT * ctx)
   cmd = get_compression_cmd (ci->open, ctx);
   if (cmd == NULL)
     return (-1);
-  dprint (2, (debugfile, "DecompressCmd: '%s'\n", cmd));
+  debug_print (2, ("DecompressCmd: '%s'\n", cmd));
 
   if ((fp = fopen (ctx->realpath, "r")) == NULL) {
     mutt_perror (ctx->realpath);
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
   mutt_block_signals ();
@@ -248,7 +246,7 @@ int mutt_open_read_compressed (CONTEXT * ctx)
     mutt_unblock_signals ();
     mutt_error _("Unable to lock mailbox!");
 
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
 
@@ -264,11 +262,11 @@ int mutt_open_read_compressed (CONTEXT * ctx)
   if (rc) {
     mutt_any_key_to_continue (NULL);
     ctx->magic = 0;
-    FREE (ctx->compressinfo);
+    p_delete(&ctx->compressinfo);
     mutt_error (_("Error executing: %s : unable to open the mailbox!\n"),
                 cmd);
   }
-  FREE (&cmd);
+  p_delete(&cmd);
   if (rc)
     return (-1);
 
@@ -282,7 +280,7 @@ int mutt_open_read_compressed (CONTEXT * ctx)
 
 void restore_path (CONTEXT * ctx)
 {
-  FREE (&ctx->path);
+  p_delete(&ctx->path);
   ctx->path = ctx->realpath;
 }
 
@@ -303,7 +301,7 @@ int mutt_open_append_compressed (CONTEXT * ctx)
       return (mutt_open_read_compressed (ctx));
 
     ctx->magic = 0;
-    FREE (&ctx->compressinfo);
+    p_delete(&ctx->compressinfo);
     return (-1);
   }
 
@@ -323,8 +321,7 @@ int mutt_open_append_compressed (CONTEXT * ctx)
 /* close a compressed mailbox */
 void mutt_fast_close_compressed (CONTEXT * ctx)
 {
-  dprint (2, (debugfile, "mutt_fast_close_compressed called on '%s'\n",
-              ctx->path));
+  debug_print (2, ("called on '%s'\n", ctx->path));
 
   if (ctx->compressinfo) {
     if (ctx->fp)
@@ -337,7 +334,7 @@ void mutt_fast_close_compressed (CONTEXT * ctx)
       remove_file (ctx);
 
     restore_path (ctx);
-    FREE (&ctx->compressinfo);
+    p_delete(&ctx->compressinfo);
   }
 }
 
@@ -358,7 +355,7 @@ int mutt_sync_compressed (CONTEXT * ctx)
 
   if ((fp = fopen (ctx->realpath, "a")) == NULL) {
     mutt_perror (ctx->realpath);
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
   mutt_block_signals ();
@@ -369,11 +366,11 @@ int mutt_sync_compressed (CONTEXT * ctx)
 
     store_size (ctx);
 
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
 
-  dprint (2, (debugfile, "CompressCommand: '%s'\n", cmd));
+  debug_print (2, ("CompressCommand: '%s'\n", cmd));
 
   endwin ();
   fflush (stdout);
@@ -391,7 +388,7 @@ int mutt_sync_compressed (CONTEXT * ctx)
   mutt_unblock_signals ();
   fclose (fp);
 
-  FREE (&cmd);
+  p_delete(&cmd);
 
   store_size (ctx);
 
@@ -405,8 +402,7 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
   char *cmd;
   COMPRESS_INFO *ci = (COMPRESS_INFO *) ctx->compressinfo;
 
-  dprint (2, (debugfile, "mutt_slow_close_compressed called on '%s'\n",
-              ctx->path));
+  debug_print (2, ("called on '%s'\n", ctx->path));
 
   if (!(ctx->append && ((append = get_append_command (ctx->realpath, ctx))
                         || (append = ci->close)))) {    /* if we can not or should not append,
@@ -434,7 +430,7 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
 
   if ((fp = fopen (ctx->realpath, "a")) == NULL) {
     mutt_perror (ctx->realpath);
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
   mutt_block_signals ();
@@ -443,11 +439,11 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
     mutt_unblock_signals ();
     mutt_error _("Unable to lock mailbox!");
 
-    FREE (&cmd);
+    p_delete(&cmd);
     return (-1);
   }
 
-  dprint (2, (debugfile, "CompressCmd: '%s'\n", cmd));
+  debug_print (2, ("CompressCmd: '%s'\n", cmd));
 
   endwin ();
   fflush (stdout);
@@ -464,7 +460,7 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
     mutt_error (_
                 (" %s: Error compressing mailbox!  Uncompressed one kept!\n"),
                 ctx->path);
-    FREE (&cmd);
+    p_delete(&cmd);
     mbox_unlock_compressed (ctx, fp);
     mutt_unblock_signals ();
     fclose (fp);
@@ -476,10 +472,19 @@ int mutt_slow_close_compressed (CONTEXT * ctx)
   fclose (fp);
   remove_file (ctx);
   restore_path (ctx);
-  FREE (&cmd);
-  FREE (&ctx->compressinfo);
+  p_delete(&cmd);
+  p_delete(&ctx->compressinfo);
 
   return (0);
 }
 
-#endif /* USE_COMPRESSED */
+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);
+}