added compression support for capture file output. The Save/As dialog now has a check...
[obnox/wireshark/wip.git] / file.c
diff --git a/file.c b/file.c
index c1a6ced6058e0931a131d25484dc8c9fc6244dad..9b4f9977e6ea1b1db3dfeb9e1965db2579d5eb72 100644 (file)
--- a/file.c
+++ b/file.c
@@ -1,7 +1,7 @@
 /* file.c
  * File I/O routines
  *
- * $Id: file.c,v 1.375 2004/04/16 20:20:54 guy Exp $
+ * $Id$
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
 #include <errno.h>
 #include <signal.h>
 
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
 
-#ifdef NEED_SNPRINTF_H
-# include "snprintf.h"
-#endif
-
 #ifdef NEED_STRERROR_H
 #include "strerror.h"
 #endif
 #include <epan/filesystem.h>
 
 #include "color.h"
-#include "column.h"
+#include "color_filters.h"
+#include <epan/column.h>
 #include <epan/packet.h>
-#include "range.h"
+#include "packet-range.h"
 #include "print.h"
 #include "file.h"
-#include "menu.h"
+#include "fileset.h"
 #include "util.h"
+#include "merge.h"
 #include "alert_box.h"
 #include "simple_dialog.h"
 #include "progress_dlg.h"
 #include "ui_util.h"
-#include "statusbar.h"
-#include "prefs.h"
+#include <epan/prefs.h>
 #include <epan/dfilter/dfilter.h>
 #include <epan/conversation.h>
-#include "globals.h"
 #include <epan/epan_dissect.h>
-#include "tap.h"
+#include <epan/tap.h>
+#include "stat_menu.h"
 #include "tap_dfilter_dlg.h"
-#include "packet-data.h"
+#include <epan/dissectors/packet-data.h>
+#include <epan/timestamp.h>
+
+
+/* Win32 needs the O_BINARY flag for open() */
+#ifndef O_BINARY
+#define O_BINARY       0
+#endif
 
 #ifdef HAVE_LIBPCAP
 gboolean auto_scroll_live;
 #endif
 
-static guint32 firstsec, firstusec;
-static guint32 prevsec, prevusec;
+static nstime_t first_ts;
+static nstime_t prev_ts;
 static guint32 cum_bytes = 0;
 
+static void cf_reset_state(capture_file *cf);
+
 static void read_packet(capture_file *cf, long offset);
 
 static void rescan_packets(capture_file *cf, const char *action, const char *action_item,
@@ -119,10 +121,10 @@ static gboolean find_packet(capture_file *cf,
 static void cf_open_failure_alert_box(const char *filename, int err,
                                      gchar *err_info, gboolean for_writing,
                                      int file_type);
-static char *file_rename_error_message(int err);
+static const char *file_rename_error_message(int err);
 static void cf_write_failure_alert_box(const char *filename, int err);
 static void cf_close_failure_alert_box(const char *filename, int err);
-static   gboolean copy_binary_file(char *from_filename, char *to_filename);
+static   gboolean copy_binary_file(const char *from_filename, const char *to_filename);
 
 /* Update the progress bar this many times when reading a file. */
 #define N_PROGBAR_UPDATES      100
@@ -131,40 +133,96 @@ static   gboolean copy_binary_file(char *from_filename, char *to_filename);
    XXX - is this the right number? */
 #define        FRAME_DATA_CHUNK_SIZE   1024
 
-typedef struct {
-       int             level;
-       FILE            *fh;
-       GSList          *src_list;
-       gboolean        print_all_levels;
-       gboolean        print_hex_for_data;
-       char_enc        encoding;
-       gint            format;         /* text or PostScript */
-} print_data;
 
-int
-cf_open(char *fname, gboolean is_tempfile, capture_file *cf)
+/* one callback for now, we could have a list later */
+static cf_callback_t cf_cb = NULL;
+static gpointer cf_cb_user_data = NULL;
+
+void
+cf_callback_invoke(int event, gpointer data)
+{
+    g_assert(cf_cb != NULL);
+    cf_cb(event, data, cf_cb_user_data);
+}
+
+
+void
+cf_callback_add(cf_callback_t func, gpointer user_data)
+{
+    /* More than one callback listener is currently not implemented,
+       but should be easy to do. */
+    g_assert(cf_cb == NULL);
+    cf_cb = func;
+    cf_cb_user_data = user_data;
+}
+
+void
+cf_callback_remove(cf_callback_t func _U_)
+{
+    g_assert(cf_cb != NULL);
+    cf_cb = NULL;
+    cf_cb_user_data = NULL;
+}
+
+void
+cf_timestamp_auto_precision(capture_file *cf)
+{
+       int prec = timestamp_get_precision();
+
+
+       /* don't try to get the file's precision if none is opened */
+       if(cf->state == FILE_CLOSED) {
+               return;
+       }
+
+       /* if we are in auto mode, set precision of current file */
+       if(prec == TS_PREC_AUTO ||
+         prec == TS_PREC_AUTO_SEC ||
+         prec == TS_PREC_AUTO_DSEC ||
+         prec == TS_PREC_AUTO_CSEC ||
+         prec == TS_PREC_AUTO_MSEC ||
+         prec == TS_PREC_AUTO_USEC ||
+         prec == TS_PREC_AUTO_NSEC)
+       {
+               switch(wtap_file_tsprecision(cf->wth)) {
+               case(WTAP_FILE_TSPREC_SEC):
+                       timestamp_set_precision(TS_PREC_AUTO_SEC);
+                       break;
+               case(WTAP_FILE_TSPREC_DSEC):
+                       timestamp_set_precision(TS_PREC_AUTO_DSEC);
+                       break;
+               case(WTAP_FILE_TSPREC_CSEC):
+                       timestamp_set_precision(TS_PREC_AUTO_CSEC);
+                       break;
+               case(WTAP_FILE_TSPREC_MSEC):
+                       timestamp_set_precision(TS_PREC_AUTO_MSEC);
+                       break;
+               case(WTAP_FILE_TSPREC_USEC):
+                       timestamp_set_precision(TS_PREC_AUTO_USEC);
+                       break;
+               case(WTAP_FILE_TSPREC_NSEC):
+                       timestamp_set_precision(TS_PREC_AUTO_NSEC);
+                       break;
+               default:
+                       g_assert_not_reached();
+               }
+       }
+}
+
+
+cf_status_t
+cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
 {
   wtap       *wth;
-  int         err;
   gchar       *err_info;
-  int         fd;
-  struct stat cf_stat;
 
-  wth = wtap_open_offline(fname, &err, &err_info, TRUE);
+  wth = wtap_open_offline(fname, err, &err_info, TRUE);
   if (wth == NULL)
     goto fail;
 
-  /* Find the size of the file. */
-  fd = wtap_fd(wth);
-  if (fstat(fd, &cf_stat) < 0) {
-    err = errno;
-    wtap_close(wth);
-    goto fail;
-  }
-
   /* The open succeeded.  Close whatever capture file we had open,
      and fill in the information for this file. */
-  cf_close(cf);
+  cf_reset_state(cf);
 
   /* Initialize all data structures used for dissection. */
   init_dissection();
@@ -173,8 +231,7 @@ cf_open(char *fname, gboolean is_tempfile, capture_file *cf)
   cf->state = FILE_READ_IN_PROGRESS;
 
   cf->wth = wth;
-  cf->filed = fd;
-  cf->f_len = cf_stat.st_size;
+  cf->f_datalen = 0;
 
   /* Set the file name because we need it to set the follow stream filter.
      XXX - is that still true?  We need it for other reasons, though,
@@ -187,14 +244,12 @@ cf_open(char *fname, gboolean is_tempfile, capture_file *cf)
   /* If it's a temporary capture buffer file, mark it as not saved. */
   cf->user_saved = !is_tempfile;
 
-  cf->cd_t      = wtap_file_type(cf->wth);
+  cf->cd_t        = wtap_file_type(cf->wth);
   cf->count     = 0;
   cf->displayed_count = 0;
   cf->marked_count = 0;
   cf->drops_known = FALSE;
   cf->drops     = 0;
-  cf->esec      = 0;
-  cf->eusec     = 0;
   cf->snap      = wtap_snapshot_length(cf->wth);
   if (cf->snap == 0) {
     /* Snapshot length not known. */
@@ -202,8 +257,9 @@ cf_open(char *fname, gboolean is_tempfile, capture_file *cf)
     cf->snap = WTAP_MAX_PACKET_SIZE;
   } else
     cf->has_snap = TRUE;
-  firstsec = 0, firstusec = 0;
-  prevsec = 0, prevusec = 0;
+  nstime_set_zero(&cf->elapsed_time);
+  nstime_set_zero(&first_ts);
+  nstime_set_zero(&prev_ts);
 
   cf->plist_chunk = g_mem_chunk_new("frame_data_chunk",
        sizeof(frame_data),
@@ -211,24 +267,32 @@ cf_open(char *fname, gboolean is_tempfile, capture_file *cf)
        G_ALLOC_AND_FREE);
   g_assert(cf->plist_chunk);
 
-  return (0);
+  /* change the time formats now, as we might have a new precision */
+  cf_change_time_formats(cf);
+
+  fileset_file_opened(fname);
+
+  return CF_OK;
 
 fail:
-  cf_open_failure_alert_box(fname, err, err_info, FALSE, 0);
-  return (err);
+  cf_open_failure_alert_box(fname, *err, err_info, FALSE, 0);
+  return CF_ERROR;
 }
 
-/* Reset everything to a pristine state */
-void
-cf_close(capture_file *cf)
+
+/*
+ * Reset the state for the currently closed file, but don't do the
+ * UI callbacks; this is for use in "cf_open()", where we don't
+ * want the UI to go from "file open" to "file closed" back to
+ * "file open", we want it to go from "old file open" to "new file
+ * open and being read".
+ */
+static void
+cf_reset_state(capture_file *cf)
 {
   /* Die if we're in the middle of reading a file. */
   g_assert(cf->state != FILE_READ_IN_PROGRESS);
 
-  /* Destroy all windows, which refer to the
-     capture file we're closing. */
-  destroy_cfile_wins();
-
   if (cf->wth) {
     wtap_close(cf->wth);
     cf->wth = NULL;
@@ -254,7 +318,7 @@ cf_close(capture_file *cf)
   }
   cf->plist = NULL;
   cf->plist_end = NULL;
-  unselect_packet(cf); /* nothing to select */
+  cf_unselect_packet(cf);      /* nothing to select */
   cf->first_displayed = NULL;
   cf->last_displayed = NULL;
 
@@ -267,123 +331,68 @@ cf_close(capture_file *cf)
   packet_list_clear();
   packet_list_thaw();
 
-  cf->f_len = 0;
+  cf->f_datalen = 0;
   cf->count = 0;
-  cf->esec  = 0;
-  cf->eusec = 0;
-
-  /* Clear any file-related status bar messages.
-     XXX - should be "clear *ALL* file-related status bar messages;
-     will there ever be more than one on the stack? */
-  statusbar_pop_file_msg();
+  nstime_set_zero(&cf->elapsed_time);
 
-  /* Restore the standard title bar message. */
-  set_main_window_name("The Ethereal Network Analyzer");
-
-  /* Disable all menu items that make sense only if you have a capture. */
-  set_menus_for_capture_file(FALSE);
-  set_menus_for_unsaved_capture_file(FALSE);
-  set_menus_for_captured_packets(FALSE);
-  set_menus_for_selected_packet(cf);
-  set_menus_for_capture_in_progress(FALSE);
-  set_menus_for_selected_tree_row(cf);
+  reset_tap_listeners();
 
   /* We have no file open. */
   cf->state = FILE_CLOSED;
-}
 
-/* Set the file name in the status line, in the name for the main window,
-   and in the name for the main window's icon. */
-static void
-set_display_filename(capture_file *cf)
-{
-  gchar  *name_ptr;
-  size_t  msg_len;
-  static const gchar done_fmt_nodrops[] = " File: %s %s %02u:%02u:%02u";
-  static const gchar done_fmt_drops[] = " File: %s %s %02u:%02u:%02u Drops: %u";
-  gchar  *done_msg;
-  gchar  *win_name_fmt = "%s - Ethereal";
-  gchar  *win_name;
-  gchar  *size_str;
-
-  name_ptr = cf_get_display_name(cf);
-       
-  if (!cf->is_tempfile) {
-    /* Add this filename to the list of recent files in the "Recent Files" submenu */
-    add_menu_recent_capture_file(cf->filename);
-  }
+  fileset_file_closed();
+}
 
-  if (cf->f_len/1024/1024 > 10) {
-    size_str = g_strdup_printf("%ld MB", cf->f_len/1024/1024);
-  } else if (cf->f_len/1024 > 10) {
-    size_str = g_strdup_printf("%ld KB", cf->f_len/1024);
-  } else {
-    size_str = g_strdup_printf("%ld bytes", cf->f_len);
-  }
+/* Reset everything to a pristine state */
+void
+cf_close(capture_file *cf)
+{
+  cf_reset_state(cf);
 
-  if (cf->drops_known) {
-    done_msg = g_strdup_printf(done_fmt_drops, name_ptr, size_str, 
-        cf->esec/3600, cf->esec%3600/60, cf->esec%60, cf->drops);
-  } else {
-    done_msg = g_strdup_printf(done_fmt_nodrops, name_ptr, size_str,
-        cf->esec/3600, cf->esec%3600/60, cf->esec%60);
-  }
-  statusbar_push_file_msg(done_msg);
-  g_free(done_msg);
+  cleanup_dissection();
 
-  msg_len = strlen(name_ptr) + strlen(win_name_fmt) + 1;
-  win_name = g_malloc(msg_len);
-  snprintf(win_name, msg_len, win_name_fmt, name_ptr);
-  set_main_window_name(win_name);
-  g_free(win_name);
+  cf_callback_invoke(cf_cb_file_closed, cf);
 }
 
-read_status_t
+cf_read_status_t
 cf_read(capture_file *cf)
 {
-  int        err;
-  gchar      *err_info;
-  gchar      *name_ptr, *load_msg, *load_fmt = "%s";
-  char       *errmsg;
-  char        errmsg_errno[1024+1];
-  gchar       err_str[2048+1];
-  long        data_offset;
-  progdlg_t  *progbar = NULL;
-  gboolean    stop_flag;
-  /*
-   * XXX - should be "off_t", but Wiretap would need more work to handle
-   * the full size of "off_t" on platforms where it's more than a "long"
-   * as well.
-   */
-  long        file_pos;
-  float       prog_val;
-  int         fd;
-  struct stat cf_stat;
-  GTimeVal    start_time;
-  gchar       status_str[100];
-  int         progbar_nextstep;
-  int         progbar_quantum;
+  int         err;
+  gchar       *err_info;
+  const gchar *name_ptr;
+  const char  *errmsg;
+  char         errmsg_errno[1024+1];
+  gchar        err_str[2048+1];
+  long         data_offset;
+  progdlg_t   *progbar = NULL;
+  gboolean     stop_flag;
+  gint64       size, file_pos;
+  float        prog_val;
+  GTimeVal     start_time;
+  gchar        status_str[100];
+  gint64       progbar_nextstep;
+  gint64       progbar_quantum;
 
   cum_bytes=0;
+
   reset_tap_listeners();
   tap_dfilter_dlg_update();
-  name_ptr = get_basename(cf->filename);
 
-  load_msg = g_strdup_printf(" Loading: %s", name_ptr);
-  statusbar_push_file_msg(load_msg);
-  g_free(load_msg);
+  cf_callback_invoke(cf_cb_file_read_start, cf);
 
-  load_msg = g_strdup_printf(load_fmt, name_ptr);
+  name_ptr = get_basename(cf->filename);
+
+  /* Find the size of the file. */
+  size = wtap_file_size(cf->wth, NULL);
 
   /* Update the progress bar when it gets to this value. */
   progbar_nextstep = 0;
   /* When we reach the value that triggers a progress bar update,
      bump that value by this amount. */
-  progbar_quantum = cf->f_len/N_PROGBAR_UPDATES;
-
-#ifndef O_BINARY
-#define O_BINARY       0
-#endif
+  if (size >= 0)     
+    progbar_quantum = size/N_PROGBAR_UPDATES;
+  else
+    progbar_quantum = 0;
 
   packet_list_freeze();
 
@@ -391,61 +400,58 @@ cf_read(capture_file *cf)
   g_get_current_time(&start_time);
 
   while ((wtap_read(cf->wth, &err, &err_info, &data_offset))) {
-    /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
-       when we update it, we have to run the GTK+ main loop to get it
-       to repaint what's pending, and doing so may involve an "ioctl()"
-       to see if there's any pending input from an X server, and doing
-       that for every packet can be costly, especially on a big file. */
-    if (data_offset >= progbar_nextstep) {
-        file_pos = lseek(cf->filed, 0, SEEK_CUR);
-        prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
-        if (prog_val > 1.0) {
-          /* The file probably grew while we were reading it.
-             Update "cf->f_len", and try again. */
-          fd = wtap_fd(cf->wth);
-          if (fstat(fd, &cf_stat) >= 0) {
-            cf->f_len = cf_stat.st_size;
-            prog_val = (gfloat) file_pos / (gfloat) cf->f_len;
+    if (size >= 0) {
+      /* Update the progress bar, but do it only N_PROGBAR_UPDATES times;
+         when we update it, we have to run the GTK+ main loop to get it
+         to repaint what's pending, and doing so may involve an "ioctl()"
+         to see if there's any pending input from an X server, and doing
+         that for every packet can be costly, especially on a big file. */
+      if (data_offset >= progbar_nextstep) {
+          file_pos = wtap_read_so_far(cf->wth, NULL);
+          prog_val = (gfloat) file_pos / (gfloat) size;
+          if (prog_val > 1.0) {
+            /* The file probably grew while we were reading it.
+               Update file size, and try again. */
+            size = wtap_file_size(cf->wth, NULL);
+            if (size >= 0)
+              prog_val = (gfloat) file_pos / (gfloat) size;
+            /* If it's still > 1, either "wtap_file_size()" failed (in which
+               case there's not much we can do about it), or the file
+               *shrank* (in which case there's not much we can do about
+               it); just clip the progress value at 1.0. */
+            if (prog_val > 1.0)
+              prog_val = 1.0;
           }
-          /* If it's still > 1, either the "fstat()" failed (in which
-             case there's not much we can do about it), or the file
-             *shrank* (in which case there's not much we can do about
-             it); just clip the progress value at 1.0. */
-          if (prog_val > 1.0)
-            prog_val = 1.0;
-        }
-        if (progbar == NULL) {
-          /* Create the progress bar if necessary */
-          progbar = delayed_create_progress_dlg("Loading", load_msg,
-            &stop_flag, &start_time, prog_val);
-          if (progbar != NULL)
-            g_free(load_msg);
-        }
-        if (progbar != NULL) {
-          g_snprintf(status_str, sizeof(status_str),
-                     "%luKB of %luKB", file_pos / 1024, cf->f_len / 1024);
-          update_progress_dlg(progbar, prog_val, status_str);
-        }
-        progbar_nextstep += progbar_quantum;
+          if (progbar == NULL) {
+            /* Create the progress bar if necessary */
+            progbar = delayed_create_progress_dlg("Loading", name_ptr,
+              &stop_flag, &start_time, prog_val);
+          }
+          if (progbar != NULL) {
+            g_snprintf(status_str, sizeof(status_str),
+                       "%" PRId64 "KB of %" PRId64 "KB",
+                       file_pos / 1024, size / 1024);
+            update_progress_dlg(progbar, prog_val, status_str);
+          }
+         progbar_nextstep += progbar_quantum;
+      }
     }
 
     if (stop_flag) {
       /* Well, the user decided to abort the read.  Destroy the progress
-         bar, close the capture file, and return READ_ABORTED so our caller
+         bar, close the capture file, and return CF_READ_ABORTED so our caller
         can do whatever is appropriate when that happens. */
       destroy_progress_dlg(progbar);
       cf->state = FILE_READ_ABORTED;   /* so that we're allowed to close it */
       packet_list_thaw();              /* undo our freeze */
       cf_close(cf);
-      return (READ_ABORTED);
+      return CF_READ_ABORTED;
     }
     read_packet(cf, data_offset);
   }
 
   /* We're done reading the file; destroy the progress bar if it was created. */
-  if (progbar == NULL)
-    g_free(load_msg);
-  else
+  if (progbar != NULL)
     destroy_progress_dlg(progbar);
 
   /* We're done reading sequentially through the file. */
@@ -467,16 +473,7 @@ cf_read(capture_file *cf)
   cf->current_frame = cf->first_displayed;
   packet_list_thaw();
 
-  statusbar_pop_file_msg();
-  set_display_filename(cf);
-
-  /* Enable menu items that make sense if you have a capture file you've
-     finished reading. */
-  set_menus_for_capture_file(TRUE);
-  set_menus_for_unsaved_capture_file(!cf->user_saved);
-
-  /* Enable menu items that make sense if you have some captured packets. */
-  set_menus_for_captured_packets(TRUE);
+  cf_callback_invoke(cf_cb_file_read_finished, cf);
 
   /* If we have any displayed packets to select, select the first of those
      packets by making the first row the selected row. */
@@ -490,11 +487,15 @@ cf_read(capture_file *cf)
     switch (err) {
 
     case WTAP_ERR_UNSUPPORTED_ENCAP:
-      errmsg = "The capture file is for a network type that Ethereal doesn't support.";
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+               "The capture file has a packet with a network type that Ethereal doesn't support.\n(%s)",
+               err_info);
+      g_free(err_info);
+      errmsg = errmsg_errno;
       break;
 
     case WTAP_ERR_CANT_READ:
-      errmsg = "An attempt to read from the file failed for"
+      errmsg = "An attempt to read from the capture file failed for"
                " some unknown reason.";
       break;
 
@@ -504,53 +505,38 @@ cf_read(capture_file *cf)
       break;
 
     case WTAP_ERR_BAD_RECORD:
-      snprintf(errmsg_errno, sizeof(errmsg_errno),
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
                "The capture file appears to be damaged or corrupt.\n(%s)",
                err_info);
+      g_free(err_info);
       errmsg = errmsg_errno;
       break;
 
     default:
-      snprintf(errmsg_errno, sizeof(errmsg_errno),
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
               "An error occurred while reading the"
               " capture file: %s.", wtap_strerror(err));
       errmsg = errmsg_errno;
       break;
     }
-    snprintf(err_str, sizeof err_str, errmsg);
+    g_snprintf(err_str, sizeof err_str, errmsg);
     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_str);
-    return (READ_ERROR);
+    return CF_READ_ERROR;
   } else
-    return (READ_SUCCESS);
+    return CF_READ_OK;
 }
 
 #ifdef HAVE_LIBPCAP
-int
-cf_start_tail(char *fname, gboolean is_tempfile, capture_file *cf)
+cf_status_t
+cf_start_tail(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
 {
-  int     err;
-  gchar *capture_msg;
-
-  err = cf_open(fname, is_tempfile, cf);
-  if (err == 0) {
-    /* Disable menu items that make no sense if you're currently running
-       a capture. */
-    set_menus_for_capture_in_progress(TRUE);
-
-    /* Enable menu items that make sense if you have some captured
-       packets (yes, I know, we don't have any *yet*). */
-    set_menus_for_captured_packets(TRUE);
-
-    capture_msg = g_strdup_printf(" %s: <live capture in progress>", cf->iface);
+  cf_status_t cf_status;
 
-    statusbar_push_file_msg(capture_msg);
-
-    g_free(capture_msg);
-  }
-  return err;
+  cf_status = cf_open(cf, fname, is_tempfile, err);
+  return cf_status;
 }
 
-read_status_t
+cf_read_status_t
 cf_continue_tail(capture_file *cf, int to_read, int *err)
 {
   long data_offset = 0;
@@ -579,27 +565,30 @@ cf_continue_tail(capture_file *cf, int to_read, int *err)
     packet_list_moveto_end();
 
   if (cf->state == FILE_READ_ABORTED) {
-    /* Well, the user decided to exit Ethereal.  Return READ_ABORTED
+    /* Well, the user decided to exit Ethereal.  Return CF_READ_ABORTED
        so that our caller can kill off the capture child process;
        this will cause an EOF on the pipe from the child, so
        "cf_finish_tail()" will be called, and it will clean up
        and exit. */
-    return READ_ABORTED;
+    return CF_READ_ABORTED;
   } else if (*err != 0) {
     /* We got an error reading the capture file.
        XXX - pop up a dialog box? */
-    return (READ_ERROR);
+    return CF_READ_ERROR;
   } else
-    return (READ_SUCCESS);
+    return CF_READ_OK;
 }
 
-read_status_t
+cf_read_status_t
 cf_finish_tail(capture_file *cf, int *err)
 {
   gchar *err_info;
   long data_offset;
-  int         fd;
-  struct stat cf_stat;
+
+  if(cf->wth == NULL) {
+    cf_close(cf);
+    return CF_READ_ERROR;
+  }
 
   packet_list_freeze();
 
@@ -617,10 +606,10 @@ cf_finish_tail(capture_file *cf, int *err)
     /* Well, the user decided to abort the read.  We're only called
        when the child capture process closes the pipe to us (meaning
        it's probably exited), so we can just close the capture
-       file; we return READ_ABORTED so our caller can do whatever
+       file; we return CF_READ_ABORTED so our caller can do whatever
        is appropriate when that happens. */
     cf_close(cf);
-    return READ_ABORTED;
+    return CF_READ_ABORTED;
   }
 
   packet_list_thaw();
@@ -632,13 +621,6 @@ cf_finish_tail(capture_file *cf, int *err)
   /* We're done reading sequentially through the file. */
   cf->state = FILE_READ_DONE;
 
-  /* we have to update the f_len field */
-  /* Find the size of the file. */
-  fd = wtap_fd(cf->wth);
-  if (fstat(fd, &cf_stat) >= 0) {
-      cf->f_len = cf_stat.st_size;
-  }
-
   /* We're done reading sequentially through the file; close the
      sequential I/O side, to free up memory it requires. */
   wtap_sequential_close(cf->wth);
@@ -653,43 +635,26 @@ cf_finish_tail(capture_file *cf, int *err)
      WTAP_ENCAP_PER_PACKET). */
   cf->lnk_t = wtap_file_encap(cf->wth);
 
-  /* Pop the "<live capture in progress>" message off the status bar. */
-  statusbar_pop_file_msg();
-
-  set_display_filename(cf);
-
-  /* Enable menu items that make sense if you're not currently running
-     a capture. */
-  set_menus_for_capture_in_progress(FALSE);
-
-  /* Enable menu items that make sense if you have a capture file
-     you've finished reading. */
-  set_menus_for_capture_file(TRUE);
-  set_menus_for_unsaved_capture_file(!cf->user_saved);
-
   if (*err != 0) {
     /* We got an error reading the capture file.
        XXX - pop up a dialog box? */
-    return (READ_ERROR);
+    return CF_READ_ERROR;
   } else {
-    return (READ_SUCCESS);
+    return CF_READ_OK;
   }
 }
 #endif /* HAVE_LIBPCAP */
 
-gchar *
+const gchar *
 cf_get_display_name(capture_file *cf)
 {
-  gchar *displayname;
+  const gchar *displayname;
 
   /* Return a name to use in displays */
   if (!cf->is_tempfile) {
     /* Get the last component of the file name, and use that. */
     if (cf->filename){
       displayname = get_basename(cf->filename);
-      
-      /* Add this filename to the list of recent files in the "Recent Files" submenu */
-      add_menu_recent_capture_file(cf->filename);
     } else {
       displayname="(No file)";
     }
@@ -701,26 +666,53 @@ cf_get_display_name(capture_file *cf)
   return displayname;
 }
 
-typedef struct {
-  color_filter_t *colorf;
-  epan_dissect_t *edt;
-} apply_color_filter_args;
+/* XXX - use a macro instead? */
+int
+cf_packet_count(capture_file *cf)
+{
+    return cf->count;
+}
 
-/*
- * If no color filter has been applied, apply this one.
- * (The "if no color filter has been applied" is to handle the case where
- * more than one color filter matches the packet.)
- */
-static void
-apply_color_filter(gpointer filter_arg, gpointer argp)
+/* XXX - use a macro instead? */
+gboolean
+cf_is_tempfile(capture_file *cf)
 {
-  color_filter_t *colorf = filter_arg;
-  apply_color_filter_args *args = argp;
+    return cf->is_tempfile;
+}
 
-  if (colorf->c_colorfilter != NULL && args->colorf == NULL) {
-    if (dfilter_apply_edt(colorf->c_colorfilter, args->edt))
-      args->colorf = colorf;
-  }
+void cf_set_tempfile(capture_file *cf, gboolean is_tempfile)
+{
+    cf->is_tempfile = is_tempfile;
+}
+
+
+/* XXX - use a macro instead? */
+void cf_set_drops_known(capture_file *cf, gboolean drops_known)
+{
+    cf->drops_known = drops_known;
+}
+
+/* XXX - use a macro instead? */
+void cf_set_drops(capture_file *cf, guint32 drops)
+{
+    cf->drops = drops;
+}
+
+/* XXX - use a macro instead? */
+gboolean cf_get_drops_known(capture_file *cf)
+{
+    return cf->drops_known;
+}
+
+/* XXX - use a macro instead? */
+guint32 cf_get_drops(capture_file *cf)
+{
+    return cf->drops;
+}
+
+void cf_set_rfcode(capture_file *cf, dfilter_t *rfcode)
+{
+    cf->rfcode = rfcode;
 }
 
 static int
@@ -728,7 +720,6 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
        union wtap_pseudo_header *pseudo_header, const guchar *buf,
        gboolean refilter)
 {
-  apply_color_filter_args args;
   gint          row;
   gboolean     create_proto_tree = FALSE;
   epan_dissect_t *edt;
@@ -736,49 +727,40 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
   /* just add some value here until we know if it is being displayed or not */
   fdata->cum_bytes  = cum_bytes + fdata->pkt_len;
 
-  /* We don't yet have a color filter to apply. */
-  args.colorf = NULL;
-
   /* If we don't have the time stamp of the first packet in the
      capture, it's because this is the first packet.  Save the time
      stamp of this packet as the time stamp of the first packet. */
-  if (!firstsec && !firstusec) {
-    firstsec  = fdata->abs_secs;
-    firstusec = fdata->abs_usecs;
+  if (nstime_is_zero(&first_ts)) {
+    first_ts  = fdata->abs_ts;
   }
   /* if this frames is marked as a reference time frame, reset
      firstsec and firstusec to this frame */
   if(fdata->flags.ref_time){
-    firstsec  = fdata->abs_secs;
-    firstusec = fdata->abs_usecs;
+    first_ts = fdata->abs_ts;
   }
 
   /* If we don't have the time stamp of the previous displayed packet,
      it's because this is the first displayed packet.  Save the time
      stamp of this packet as the time stamp of the previous displayed
      packet. */
-  if (!prevsec && !prevusec) {
-    prevsec  = fdata->abs_secs;
-    prevusec = fdata->abs_usecs;
+  if (nstime_is_zero(&prev_ts)) {
+    prev_ts = fdata->abs_ts;
   }
 
   /* Get the time elapsed between the first packet and this packet. */
-  compute_timestamp_diff(&fdata->rel_secs, &fdata->rel_usecs,
-     fdata->abs_secs, fdata->abs_usecs, firstsec, firstusec);
+  nstime_delta(&fdata->rel_ts, &fdata->abs_ts, &first_ts);
 
   /* If it's greater than the current elapsed time, set the elapsed time
      to it (we check for "greater than" so as not to be confused by
      time moving backwards). */
-  if ((gint32)cf->esec < fdata->rel_secs
-  || ((gint32)cf->esec == fdata->rel_secs && (gint32)cf->eusec < fdata->rel_usecs)) {
-    cf->esec = fdata->rel_secs;
-    cf->eusec = fdata->rel_usecs;
+  if ((gint32)cf->elapsed_time.secs < fdata->rel_ts.secs
+  || ((gint32)cf->elapsed_time.secs == fdata->rel_ts.secs && (gint32)cf->elapsed_time.nsecs < fdata->rel_ts.nsecs)) {
+    cf->elapsed_time = fdata->rel_ts;
   }
 
   /* Get the time elapsed between the previous displayed packet and
      this packet. */
-  compute_timestamp_diff(&fdata->del_secs, &fdata->del_usecs,
-       fdata->abs_secs, fdata->abs_usecs, prevsec, prevusec);
+  nstime_delta(&fdata->del_ts, &fdata->abs_ts, &prev_ts);
 
   /* If either
 
@@ -791,7 +773,7 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
      allocate a protocol tree root node, so that we'll construct
      a protocol tree against which a filter expression can be
      evaluated. */
-  if ((cf->dfcode != NULL && refilter) || filter_list != NULL
+  if ((cf->dfcode != NULL && refilter) || color_filters_used()
         || num_tap_filters != 0)
          create_proto_tree = TRUE;
 
@@ -801,8 +783,8 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
   if (cf->dfcode != NULL && refilter) {
       epan_dissect_prime_dfilter(edt, cf->dfcode);
   }
-  if (filter_list) {
-      filter_list_prime_edt(edt);
+  if (color_filters_used()) {
+      color_filters_prime_edt(edt);
   }
   tap_queue_init(edt);
   epan_dissect_run(edt, pseudo_header, buf, fdata, &cf->cinfo);
@@ -814,24 +796,11 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
      If we don't have a display filter, set "passed_dfilter" to 1. */
   if (cf->dfcode != NULL) {
     if (refilter) {
-      if (cf->dfcode != NULL)
-        fdata->flags.passed_dfilter = dfilter_apply_edt(cf->dfcode, edt) ? 1 : 0;
-      else
-        fdata->flags.passed_dfilter = 1;
+      fdata->flags.passed_dfilter = dfilter_apply_edt(cf->dfcode, edt) ? 1 : 0;
     }
   } else
     fdata->flags.passed_dfilter = 1;
 
-  /* If we have color filters, and the frame is to be displayed, apply
-     the color filters. */
-  if (fdata->flags.passed_dfilter) {
-    if (filter_list != NULL) {
-      args.edt = edt;
-      g_slist_foreach(filter_list, apply_color_filter, &args);
-    }
-  }
-
-
   if( (fdata->flags.passed_dfilter) 
    || (edt->pi.fd->flags.ref_time) ){
     /* This frame either passed the display filter list or is marked as
@@ -853,14 +822,14 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
        XXX - we must do this before we add the row to the display,
        as, if the display's GtkCList's selection mode is
        GTK_SELECTION_BROWSE, when the first entry is added to it,
-       "select_packet()" will be called, and it will fetch the row
+       "cf_select_packet()" will be called, and it will fetch the row
        data for the 0th row, and will get a null pointer rather than
        "fdata", as "gtk_clist_append()" won't yet have returned and
        thus "gtk_clist_set_row_data()" won't yet have been called.
 
        We thus need to leave behind bread crumbs so that
-       "select_packet()" can find this frame.  See the comment
-       in "select_packet()". */
+       "cf_select_packet()" can find this frame.  See the comment
+       in "cf_select_packet()". */
     if (cf->first_displayed == NULL)
       cf->first_displayed = fdata;
 
@@ -869,30 +838,18 @@ add_packet_to_packet_list(frame_data *fdata, capture_file *cf,
 
     row = packet_list_append(cf->cinfo.col_data, fdata);
 
-    /* If the packet matches a color filter,
-     * store matching color_filter_t object in frame data. */
-    if (filter_list != NULL && (args.colorf != NULL)) {
-      /* add the matching colorfilter to the frame data */
-      fdata->color_filter = args.colorf;
-      /* If packet is marked, use colors from preferences */
-      if (fdata->flags.marked) {
-          packet_list_set_colors(row, &prefs.gui_marked_fg, &prefs.gui_marked_bg);
-      } else /* if (filter_list != NULL && (args.colorf != NULL)) */ {
-          packet_list_set_colors(row, &(args.colorf->fg_color),
-             &(args.colorf->bg_color));
-      }
-    } else {
-      /* No color filter match */
-      fdata->color_filter = NULL;
+    /* colorize packet: if packet is marked, use preferences, 
+       otherwise try to apply color filters */
       if (fdata->flags.marked) {
+          fdata->color_filter = NULL;
           packet_list_set_colors(row, &prefs.gui_marked_fg, &prefs.gui_marked_bg);
+      } else {
+          fdata->color_filter = color_filters_colorize_packet(row, edt);
       }
-    }
 
     /* Set the time of the previous displayed frame to the time of this
        frame. */
-    prevsec = fdata->abs_secs;
-    prevusec = fdata->abs_usecs;
+    prev_ts = fdata->abs_ts;
 
     cf->displayed_count++;
   } else {
@@ -918,6 +875,7 @@ read_packet(capture_file *cf, long offset)
   /* Allocate the next list entry, and add it to the list. */
   fdata = g_mem_chunk_alloc(cf->plist_chunk);
 
+  fdata->num = 0;
   fdata->next = NULL;
   fdata->prev = NULL;
   fdata->pfd  = NULL;
@@ -925,13 +883,13 @@ read_packet(capture_file *cf, long offset)
   fdata->cap_len  = phdr->caplen;
   fdata->file_off = offset;
   fdata->lnk_t = phdr->pkt_encap;
-  fdata->abs_secs  = phdr->ts.tv_sec;
-  fdata->abs_usecs = phdr->ts.tv_usec;
   fdata->flags.encoding = CHAR_ASCII;
   fdata->flags.visited = 0;
   fdata->flags.marked = 0;
   fdata->flags.ref_time = 0;
 
+  fdata->abs_ts  = *((nstime_t *) &phdr->ts);
+
   passed = TRUE;
   if (cf->rfcode) {
     edt = epan_dissect_new(TRUE, FALSE);
@@ -950,6 +908,7 @@ read_packet(capture_file *cf, long offset)
     cf->plist_end = fdata;
 
     cf->count++;
+    cf->f_datalen = offset + phdr->caplen;
     fdata->num = cf->count;
     add_packet_to_packet_list(fdata, cf, pseudo_header, buf, TRUE);
   } else {
@@ -965,16 +924,220 @@ read_packet(capture_file *cf, long offset)
   }
 }
 
-gboolean
-filter_packets(capture_file *cf, gchar *dftext, gboolean force)
+cf_status_t
+cf_merge_files(char **out_filenamep, int in_file_count,
+               char *const *in_filenames, int file_type, gboolean do_append)
 {
-  dfilter_t *dfcode;
-  char      *filter_new = dftext ? dftext : "";
-  char      *filter_old = cf->dfilter ? cf->dfilter : "";
+  merge_in_file_t  *in_files;
+  wtap             *wth;
+  char             *out_filename;
+  char              tmpname[128+1];
+  int               out_fd;
+  wtap_dumper      *pdh;
+  int               open_err, read_err, write_err, close_err;
+  gchar            *err_info;
+  int               err_fileno; 
+  int               i;
+  char              errmsg_errno[1024+1];
+  gchar             err_str[2048+1];
+  const char       *errmsg;
+  gboolean          got_read_error = FALSE, got_write_error = FALSE;
+  long              data_offset;
+  progdlg_t        *progbar = NULL;
+  gboolean          stop_flag;
+  gint64            f_len, file_pos;
+  float             prog_val;
+  GTimeVal          start_time;
+  gchar             status_str[100];
+  gint64            progbar_nextstep;
+  gint64            progbar_quantum;
+
+  /* open the input files */
+  if (!merge_open_in_files(in_file_count, in_filenames, &in_files,
+                           &open_err, &err_info, &err_fileno)) {
+    free(in_files);
+    cf_open_failure_alert_box(in_filenames[err_fileno], open_err, err_info,
+                              FALSE, 0);
+    return CF_ERROR;
+  }
+
+  if (*out_filenamep != NULL) {
+    out_filename = *out_filenamep;
+    out_fd = open(out_filename, O_CREAT|O_TRUNC|O_BINARY, 0600);
+    if (out_fd == -1)
+      open_err = errno;
+  } else {
+    out_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
+    if (out_fd == -1)
+      open_err = errno;
+    out_filename = g_strdup(tmpname);
+    *out_filenamep = out_filename;
+  }
+  if (out_fd == -1) {
+    err_info = NULL;
+    merge_close_in_files(in_file_count, in_files);
+    free(in_files);
+    cf_open_failure_alert_box(out_filename, open_err, NULL, TRUE, file_type);
+    return CF_ERROR;
+  }
+
+  pdh = wtap_dump_fdopen(out_fd, file_type,
+      merge_select_frame_type(in_file_count, in_files),
+      merge_max_snapshot_length(in_file_count, in_files), 
+         FALSE /* compressed */, &open_err);
+  if (pdh == NULL) {
+    close(out_fd);
+    merge_close_in_files(in_file_count, in_files);
+    free(in_files);
+    cf_open_failure_alert_box(out_filename, open_err, err_info, TRUE,
+                              file_type);
+    return CF_ERROR;
+  }
+
+  /* Get the sum of the sizes of all the files. */
+  f_len = 0;
+  for (i = 0; i < in_file_count; i++)
+    f_len += in_files[i].size;
+
+  /* Update the progress bar when it gets to this value. */
+  progbar_nextstep = 0;
+  /* When we reach the value that triggers a progress bar update,
+     bump that value by this amount. */
+  progbar_quantum = f_len/N_PROGBAR_UPDATES;
+
+  stop_flag = FALSE;
+  g_get_current_time(&start_time);
+
+  /* do the merge (or append) */
+  for (;;) {
+    if (do_append)
+      wth = merge_append_read_packet(in_file_count, in_files, &read_err,
+                                     &err_info);
+    else
+      wth = merge_read_packet(in_file_count, in_files, &read_err,
+                              &err_info);
+    if (wth == NULL) {
+      if (read_err != 0)
+        got_read_error = TRUE;
+      break;
+    }
+
+    /* Get the sum of the data offsets in all of the files. */
+    data_offset = 0;
+    for (i = 0; i < in_file_count; i++)
+      data_offset += in_files[i].data_offset;
+
+    if (data_offset >= progbar_nextstep) {
+        /* Get the sum of the seek positions in all of the files. */
+        file_pos = 0;
+        for (i = 0; i < in_file_count; i++)
+          file_pos += wtap_read_so_far(in_files[i].wth, NULL);
+        prog_val = (gfloat) file_pos / (gfloat) f_len;
+        if (prog_val > 1.0) {
+          /* Some file probably grew while we were reading it.
+             That "shouldn't happen", so we'll just clip the progress
+             value at 1.0. */
+          prog_val = 1.0;
+        }
+        if (progbar == NULL) {
+          /* Create the progress bar if necessary */
+          progbar = delayed_create_progress_dlg("Merging", "files",
+            &stop_flag, &start_time, prog_val);
+        }
+        if (progbar != NULL) {
+          g_snprintf(status_str, sizeof(status_str),
+                     "%" PRId64 "KB of %" PRId64 "KB",
+                     file_pos / 1024, f_len / 1024);
+          update_progress_dlg(progbar, prog_val, status_str);
+        }
+        progbar_nextstep += progbar_quantum;
+    }
+
+    if (!wtap_dump(pdh, wtap_phdr(wth), wtap_pseudoheader(wth),
+         wtap_buf_ptr(wth), &write_err)) {
+      got_write_error = TRUE;
+      break;
+    }
+  }
+
+  /* We're done merging the files; destroy the progress bar if it was created. */
+  if (progbar != NULL)
+    destroy_progress_dlg(progbar);
+
+  merge_close_in_files(in_file_count, in_files);
+  if (!got_read_error && !got_write_error) {
+    if (!wtap_dump_close(pdh, &write_err))
+      got_write_error = TRUE;
+  } else
+    wtap_dump_close(pdh, &close_err);
+
+  if (got_read_error) {
+    /*
+     * Find the file on which we got the error, and report the error.
+     */
+    for (i = 0; i < in_file_count; i++) {
+      if (in_files[i].state == GOT_ERROR) {
+       /* Put up a message box noting that a read failed somewhere along
+          the line. */
+       switch (read_err) {
+
+       case WTAP_ERR_UNSUPPORTED_ENCAP:
+         g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+                  "The capture file %%s has a packet with a network type that Ethereal doesn't support.\n(%s)",
+                  err_info);
+         g_free(err_info);
+         errmsg = errmsg_errno;
+         break;
+
+       case WTAP_ERR_CANT_READ:
+         errmsg = "An attempt to read from the capture file %s failed for"
+                  " some unknown reason.";
+         break;
+
+       case WTAP_ERR_SHORT_READ:
+         errmsg = "The capture file %s appears to have been cut short"
+                  " in the middle of a packet.";
+         break;
+
+       case WTAP_ERR_BAD_RECORD:
+         g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+                  "The capture file %%s appears to be damaged or corrupt.\n(%s)",
+                  err_info);
+         g_free(err_info);
+         errmsg = errmsg_errno;
+         break;
+
+       default:
+         g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+                  "An error occurred while reading the"
+                  " capture file %%s: %s.", wtap_strerror(read_err));
+         errmsg = errmsg_errno;
+         break;
+       }
+       g_snprintf(err_str, sizeof err_str, errmsg, in_files[i].filename);
+        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, err_str);
+      }
+    }
+  }
+
+  if (got_write_error) {
+    /* Put up an alert box for the write error. */
+    cf_write_failure_alert_box(out_filename, write_err);
+  }
+
+  return (!got_read_error && !got_write_error) ? CF_OK : CF_ERROR;
+}
+
+cf_status_t
+cf_filter_packets(capture_file *cf, gchar *dftext, gboolean force)
+{
+  dfilter_t  *dfcode;
+  const char *filter_new = dftext ? dftext : "";
+  const char *filter_old = cf->dfilter ? cf->dfilter : "";
 
   /* if new filter equals old one, do nothing unless told to do so */
   if (!force && strcmp(filter_new, filter_old) == 0) {
-    return TRUE;
+    return CF_OK;
   }
 
   if (dftext == NULL) {
@@ -988,15 +1151,20 @@ filter_packets(capture_file *cf, gchar *dftext, gboolean force)
     dftext = g_strdup(dftext);
     if (!dfilter_compile(dftext, &dfcode)) {
       /* The attempt failed; report an error. */
+      gchar *safe_dftext = simple_dialog_format_message(dftext);
+      gchar *safe_dfilter_error_msg = simple_dialog_format_message(
+         dfilter_error_msg);
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, 
           "%s%s%s\n"
           "\n"
-          "The display filter \"%s\" is not a valid display filter.\n"
+          "The following display filter isn't a valid display filter:\n%s\n"
           "See the help for a description of the display filter syntax.",
-          simple_dialog_primary_start(), dfilter_error_msg,
-          simple_dialog_primary_end(), dftext);
+          simple_dialog_primary_start(), safe_dfilter_error_msg,
+          simple_dialog_primary_end(), safe_dftext);
+      g_free(safe_dfilter_error_msg);
+      g_free(safe_dftext);
       g_free(dftext);
-      return FALSE;
+      return CF_ERROR;
     }
 
     /* Was it empty? */
@@ -1022,23 +1190,23 @@ filter_packets(capture_file *cf, gchar *dftext, gboolean force)
   } else {
     rescan_packets(cf, "Filtering", dftext, TRUE, FALSE);
   }
-  return TRUE;
+  return CF_OK;
 }
 
 void
-colorize_packets(capture_file *cf)
+cf_colorize_packets(capture_file *cf)
 {
   rescan_packets(cf, "Colorizing", "all packets", FALSE, FALSE);
 }
 
 void
-reftime_packets(capture_file *cf)
+cf_reftime_packets(capture_file *cf)
 {
   rescan_packets(cf, "Updating Reftime", "all packets", FALSE, FALSE);
 }
 
 void
-redissect_packets(capture_file *cf)
+cf_redissect_packets(capture_file *cf)
 {
   rescan_packets(cf, "Reprocessing", "all packets", TRUE, TRUE);
 }
@@ -1116,10 +1284,8 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
   /* Iterate through the list of frames.  Call a routine for each frame
      to check whether it should be displayed and, if so, add it to
      the display list. */
-  firstsec = 0;
-  firstusec = 0;
-  prevsec = 0;
-  prevusec = 0;
+  nstime_set_zero(&first_ts);
+  nstime_set_zero(&prev_ts);
 
   /* Update the progress bar when it gets to this value. */
   progbar_nextstep = 0;
@@ -1307,7 +1473,7 @@ rescan_packets(capture_file *cf, const char *action, const char *action_item,
 
   if (selected_row == -1) {
     /* There are no frames displayed at all. */
-    unselect_packet(cf);
+    cf_unselect_packet(cf);
   } else {
     /* Either the frame that was selected passed the filter, or we've
        found the nearest displayed frame to that frame.  Select it, make
@@ -1322,7 +1488,7 @@ typedef enum {
   PSP_FAILED
 } psp_return_t;
 
-psp_return_t
+static psp_return_t
 process_specified_packets(capture_file *cf, packet_range_t *range,
     const char *string1, const char *string2,
     gboolean (*callback)(capture_file *, frame_data *,
@@ -1419,6 +1585,7 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
       ret = PSP_FAILED;
       break;
     }
+    /* Process the packet */
     if (!callback(cf, fdata, &pseudo_header, pd, callback_args)) {
       /* Callback failed.  We assume it reported the error appropriately. */
       ret = PSP_FAILED;
@@ -1437,8 +1604,9 @@ process_specified_packets(capture_file *cf, packet_range_t *range,
 static gboolean
 retap_packet(capture_file *cf _U_, frame_data *fdata,
              union wtap_pseudo_header *pseudo_header, const guint8 *pd,
-             void *argsp _U_)
+             void *argsp)
 {
+  column_info *cinfo = argsp;
   epan_dissect_t *edt;
 
   /* If we have tap listeners, allocate a protocol tree root node, so that
@@ -1446,15 +1614,15 @@ retap_packet(capture_file *cf _U_, frame_data *fdata,
      be evaluated. */
   edt = epan_dissect_new(num_tap_filters != 0, FALSE);
   tap_queue_init(edt);
-  epan_dissect_run(edt, pseudo_header, pd, fdata, NULL);
+  epan_dissect_run(edt, pseudo_header, pd, fdata, cinfo);
   tap_push_tapped_queue(edt);
   epan_dissect_free(edt);
 
   return TRUE;
 }
 
-int
-retap_packets(capture_file *cf)
+cf_read_status_t
+cf_retap_packets(capture_file *cf, gboolean do_columns)
 {
   packet_range_t range;
 
@@ -1467,30 +1635,31 @@ retap_packets(capture_file *cf)
   packet_range_process_init(&range);
   switch (process_specified_packets(cf, &range, "Refiltering statistics on",
                                     "all packets", retap_packet,
-                                    NULL)) {
+                                    do_columns ? &cf->cinfo : NULL)) {
   case PSP_FINISHED:
     /* Completed successfully. */
-    break;
+    return CF_OK;
 
   case PSP_STOPPED:
     /* Well, the user decided to abort the refiltering.
-       Return FALSE so our caller knows they did that. */
-    return FALSE;
+       Return CF_READ_ABORTED so our caller knows they did that. */
+    return CF_READ_ABORTED;
 
   case PSP_FAILED:
     /* Error while retapping. */
-    return FALSE;
+    return CF_READ_ERROR;
   }
 
-  return TRUE;
+  g_assert_not_reached();
+  return CF_READ_OK;
 }
 
 typedef struct {
   print_args_t *print_args;
-  FILE         *print_fh;
   gboolean      print_header_line;
   char         *header_line_buf;
   int           header_line_buf_len;
+  gboolean      print_formfeed;
   gboolean      print_separator;
   char         *line_buf;
   int           line_buf_len;
@@ -1510,23 +1679,44 @@ print_packet(capture_file *cf, frame_data *fdata,
   int             column_len;
   int             cp_off;
   gboolean        proto_tree_needed;
+  char            bookmark_name[9+10+1];       /* "__frameNNNNNNNNNN__\0" */
+  char            bookmark_title[6+10+1];      /* "Frame NNNNNNNNNN__\0" */
 
+  /* Create the protocol tree, and make it visible, if we're printing
+     the dissection or the hex data.
+     XXX - do we need it if we're just printing the hex data? */
   proto_tree_needed = 
       args->print_args->print_dissections != print_dissections_none || args->print_args->print_hex;
-
-  /* Fill in the column information, but don't bother creating
-     the logical protocol tree. */
   edt = epan_dissect_new(proto_tree_needed, proto_tree_needed);
-  epan_dissect_run(edt, pseudo_header, pd, fdata, &cf->cinfo);
-  epan_dissect_fill_in_columns(edt);
 
-  if (args->print_separator)
-    print_line(args->print_fh, 0, args->print_args->format, "");
+  /* Fill in the column information if we're printing the summary
+     information. */
+  if (args->print_args->print_summary) {
+    epan_dissect_run(edt, pseudo_header, pd, fdata, &cf->cinfo);
+    epan_dissect_fill_in_columns(edt);
+  } else
+    epan_dissect_run(edt, pseudo_header, pd, fdata, NULL);
+
+  if (args->print_formfeed) {
+    if (!new_page(args->print_args->stream))
+      goto fail;
+  } else {
+      if (args->print_separator) {
+        if (!print_line(args->print_args->stream, 0, ""))
+          goto fail;
+      }
+  }
+
+  /*
+   * We generate bookmarks, if the output format supports them.
+   * The name is "__frameN__".
+   */
+  g_snprintf(bookmark_name, sizeof bookmark_name, "__frame%u__", fdata->num);
 
   if (args->print_args->print_summary) {
     if (args->print_header_line) {
-      print_line(args->print_fh, 0, args->print_args->format,
-                 args->header_line_buf);
+      if (!print_line(args->print_args->stream, 0, args->header_line_buf))
+        goto fail;
       args->print_header_line = FALSE; /* we might not need to print any more */
     }
     cp = &args->line_buf[0];
@@ -1557,17 +1747,37 @@ print_packet(capture_file *cf, frame_data *fdata,
         *cp++ = ' ';
     }
     *cp = '\0';
-    print_line(args->print_fh, 0, args->print_args->format, args->line_buf);
+
+    /*
+     * Generate a bookmark, using the summary line as the title.
+     */
+    if (!print_bookmark(args->print_args->stream, bookmark_name,
+                        args->line_buf))
+      goto fail;
+
+    if (!print_line(args->print_args->stream, 0, args->line_buf))
+      goto fail;
+  } else {
+    /*
+     * Generate a bookmark, using "Frame N" as the title, as we're not
+     * printing the summary line.
+     */
+    g_snprintf(bookmark_title, sizeof bookmark_title, "Frame %u", fdata->num);
+    if (!print_bookmark(args->print_args->stream, bookmark_name,
+                        bookmark_title))
+      goto fail;
   } /* if (print_summary) */
-  
+
   if (args->print_args->print_dissections != print_dissections_none) {
     if (args->print_args->print_summary) {
       /* Separate the summary line from the tree with a blank line. */
-      print_line(args->print_fh, 0, args->print_args->format, "");
+      if (!print_line(args->print_args->stream, 0, ""))
+        goto fail;
     }
 
     /* Print the information in that tree. */
-    proto_tree_print(args->print_args, edt, args->print_fh);
+    if (!proto_tree_print(args->print_args, edt, args->print_args->stream))
+      goto fail;
 
     /* Print a blank line if we print anything after this (aka more than one packet). */
     args->print_separator = TRUE;
@@ -1578,22 +1788,32 @@ print_packet(capture_file *cf, frame_data *fdata,
 
   if (args->print_args->print_hex) {
     /* Print the full packet data as hex. */
-    print_hex_data(args->print_fh, args->print_args->format, edt);
+    if (!print_hex_data(args->print_args->stream, edt))
+      goto fail;
 
     /* Print a blank line if we print anything after this (aka more than one packet). */
     args->print_separator = TRUE;
 
     /* Print a header line if we print any more packet summaries */
     args->print_header_line = TRUE;
-  } /* if (print_summary) */
+  } /* if (args->print_args->print_dissections != print_dissections_none) */
 
   epan_dissect_free(edt);
 
-  return !ferror(args->print_fh);
+  /* do we want to have a formfeed between each packet from now on? */
+  if(args->print_args->print_formfeed) {
+    args->print_formfeed = TRUE;
+  }
+
+  return TRUE;
+
+fail:
+  epan_dissect_free(edt);
+  return FALSE;
 }
 
-pp_return_t
-print_packets(capture_file *cf, print_args_t *print_args)
+cf_print_status_t
+cf_print_packets(capture_file *cf, print_args_t *print_args)
 {
   int         i;
   print_callback_args_t callback_args;
@@ -1604,25 +1824,21 @@ print_packets(capture_file *cf, print_args_t *print_args)
   int         line_len;
   psp_return_t ret;
 
-  callback_args.print_fh = open_print_dest(print_args->to_file,
-                                           print_args->dest);
-  if (callback_args.print_fh == NULL)
-    return PP_OPEN_ERROR;      /* attempt to open destination failed */
-
-  print_preamble(callback_args.print_fh, print_args->format);
-  if (ferror(callback_args.print_fh)) {
-    close_print_dest(print_args->to_file, callback_args.print_fh);
-    return PP_WRITE_ERROR;
-  }
-
   callback_args.print_args = print_args;
   callback_args.print_header_line = TRUE;
   callback_args.header_line_buf = NULL;
   callback_args.header_line_buf_len = 256;
+  callback_args.print_formfeed = FALSE;
   callback_args.print_separator = FALSE;
   callback_args.line_buf = NULL;
   callback_args.line_buf_len = 256;
   callback_args.col_widths = NULL;
+
+  if (!print_preamble(print_args->stream, cf->filename)) {
+    destroy_print_stream(print_args->stream);
+    return CF_PRINT_WRITE_ERROR;
+  }
+
   if (print_args->print_summary) {
     /* We're printing packet summaries.  Allocate the header line buffer
        and get the column widths. */
@@ -1662,9 +1878,9 @@ print_packets(capture_file *cf, print_args_t *print_args)
       }
 
       /* Right-justify the packet number column. */
-      if (cf->cinfo.col_fmt[i] == COL_NUMBER)
+/*      if (cf->cinfo.col_fmt[i] == COL_NUMBER)
         sprintf(cp, "%*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
-      else
+      else*/
         sprintf(cp, "%-*s", callback_args.col_widths[i], cf->cinfo.col_title[i]);
       cp += column_len;
       if (i != cf->cinfo.num_cols - 1)
@@ -1713,26 +1929,238 @@ print_packets(capture_file *cf, print_args_t *print_args)
        will get printed if we're piping to a print program; we'd
        have to write to a file and then hand that to the print
        program to make it actually not print anything. */
-    close_print_dest(print_args->to_file, callback_args.print_fh);
-    return PP_WRITE_ERROR;
+    destroy_print_stream(print_args->stream);
+    return CF_PRINT_WRITE_ERROR;
+  }
+
+  if (!print_finale(print_args->stream)) {
+    destroy_print_stream(print_args->stream);
+    return CF_PRINT_WRITE_ERROR;
+  }
+
+  if (!destroy_print_stream(print_args->stream))
+    return CF_PRINT_WRITE_ERROR;
+
+  return CF_PRINT_OK;
+}
+
+static gboolean
+write_pdml_packet(capture_file *cf _U_, frame_data *fdata,
+                  union wtap_pseudo_header *pseudo_header, const guint8 *pd,
+                 void *argsp)
+{
+  FILE *fh = argsp;
+  epan_dissect_t *edt;
+
+  /* Create the protocol tree, but don't fill in the column information. */
+  edt = epan_dissect_new(TRUE, TRUE);
+  epan_dissect_run(edt, pseudo_header, pd, fdata, NULL);
+
+  /* Write out the information in that tree. */
+  proto_tree_write_pdml(edt, fh);
+
+  epan_dissect_free(edt);
+
+  return !ferror(fh);
+}
+
+cf_print_status_t
+cf_write_pdml_packets(capture_file *cf, print_args_t *print_args)
+{
+  FILE        *fh;
+  psp_return_t ret;
+
+  fh = fopen(print_args->file, "w");
+  if (fh == NULL)
+    return CF_PRINT_OPEN_ERROR;        /* attempt to open destination failed */
+
+  write_pdml_preamble(fh);
+  if (ferror(fh)) {
+    fclose(fh);
+    return CF_PRINT_WRITE_ERROR;
+  }
+
+  /* Iterate through the list of packets, printing the packets we were
+     told to print. */
+  ret = process_specified_packets(cf, &print_args->range, "Writing PDML",
+                                  "selected packets", write_pdml_packet,
+                                  fh);
+
+  switch (ret) {
+
+  case PSP_FINISHED:
+    /* Completed successfully. */
+    break;
+
+  case PSP_STOPPED:
+    /* Well, the user decided to abort the printing. */
+    break;
+
+  case PSP_FAILED:
+    /* Error while printing. */
+    fclose(fh);
+    return CF_PRINT_WRITE_ERROR;
+  }
+
+  write_pdml_finale(fh);
+  if (ferror(fh)) {
+    fclose(fh);
+    return CF_PRINT_WRITE_ERROR;
+  }
+
+  /* XXX - check for an error */
+  fclose(fh);
+
+  return CF_PRINT_OK;
+}
+
+static gboolean
+write_psml_packet(capture_file *cf, frame_data *fdata,
+                  union wtap_pseudo_header *pseudo_header, const guint8 *pd,
+                 void *argsp)
+{
+  FILE *fh = argsp;
+  epan_dissect_t *edt;
+
+  /* Fill in the column information, but don't create the protocol tree. */
+  edt = epan_dissect_new(FALSE, FALSE);
+  epan_dissect_run(edt, pseudo_header, pd, fdata, &cf->cinfo);
+  epan_dissect_fill_in_columns(edt);
+
+  /* Write out the information in that tree. */
+  proto_tree_write_psml(edt, fh);
+
+  epan_dissect_free(edt);
+
+  return !ferror(fh);
+}
+
+cf_print_status_t
+cf_write_psml_packets(capture_file *cf, print_args_t *print_args)
+{
+  FILE        *fh;
+  psp_return_t ret;
+
+  fh = fopen(print_args->file, "w");
+  if (fh == NULL)
+    return CF_PRINT_OPEN_ERROR;        /* attempt to open destination failed */
+
+  write_psml_preamble(fh);
+  if (ferror(fh)) {
+    fclose(fh);
+    return CF_PRINT_WRITE_ERROR;
+  }
+
+  /* Iterate through the list of packets, printing the packets we were
+     told to print. */
+  ret = process_specified_packets(cf, &print_args->range, "Writing PSML",
+                                  "selected packets", write_psml_packet,
+                                  fh);
+
+  switch (ret) {
+
+  case PSP_FINISHED:
+    /* Completed successfully. */
+    break;
+
+  case PSP_STOPPED:
+    /* Well, the user decided to abort the printing. */
+    break;
+
+  case PSP_FAILED:
+    /* Error while printing. */
+    fclose(fh);
+    return CF_PRINT_WRITE_ERROR;
   }
 
-  print_finale(callback_args.print_fh, print_args->format);
-  if (ferror(callback_args.print_fh)) {
-    close_print_dest(print_args->to_file, callback_args.print_fh);
-    return PP_WRITE_ERROR;
+  write_psml_finale(fh);
+  if (ferror(fh)) {
+    fclose(fh);
+    return CF_PRINT_WRITE_ERROR;
   }
 
-  close_print_dest(print_args->to_file, callback_args.print_fh);
+  /* XXX - check for an error */
+  fclose(fh);
 
-  return PP_OK;
+  return CF_PRINT_OK;
+}
+
+static gboolean
+write_csv_packet(capture_file *cf, frame_data *fdata,
+                 union wtap_pseudo_header *pseudo_header, const guint8 *pd,
+                 void *argsp)
+{
+  FILE *fh = argsp;
+  epan_dissect_t *edt;
+
+  /* Fill in the column information, but don't create the protocol tree. */
+  edt = epan_dissect_new(FALSE, FALSE);
+  epan_dissect_run(edt, pseudo_header, pd, fdata, &cf->cinfo);
+  epan_dissect_fill_in_columns(edt);
+
+  /* Write out the information in that tree. */
+  proto_tree_write_csv(edt, fh);
+
+  epan_dissect_free(edt);
+
+  return !ferror(fh);
+}
+
+cf_print_status_t
+cf_write_csv_packets(capture_file *cf, print_args_t *print_args)
+{
+  FILE        *fh;
+  psp_return_t ret;
+
+  fh = fopen(print_args->file, "w");
+  if (fh == NULL)
+    return CF_PRINT_OPEN_ERROR; /* attempt to open destination failed */
+
+  write_csv_preamble(fh);
+  if (ferror(fh)) {
+    fclose(fh);
+    return CF_PRINT_WRITE_ERROR;
+  }
+
+  /* Iterate through the list of packets, printing the packets we were
+     told to print. */
+  ret = process_specified_packets(cf, &print_args->range, "Writing CSV",
+                                  "selected packets", write_csv_packet,
+                                  fh);
+
+  switch (ret) {
+
+  case PSP_FINISHED:
+    /* Completed successfully. */
+    break;
+
+  case PSP_STOPPED:
+    /* Well, the user decided to abort the printing. */
+    break;
+
+  case PSP_FAILED:
+    /* Error while printing. */
+    fclose(fh);
+    return CF_PRINT_WRITE_ERROR;
+  }
+
+  write_csv_finale(fh);
+  if (ferror(fh)) {
+    fclose(fh);
+    return CF_PRINT_WRITE_ERROR;
+  }
+
+  /* XXX - check for an error */
+  fclose(fh);
+
+  return CF_PRINT_OK;
 }
 
 /* Scan through the packet list and change all columns that use the
    "command-line-specified" time stamp format to use the current
    value of that format. */
 void
-change_time_formats(capture_file *cf)
+cf_change_time_formats(capture_file *cf)
 {
   frame_data *fdata;
   progdlg_t  *progbar = NULL;
@@ -1748,6 +2176,10 @@ change_time_formats(capture_file *cf)
   int         first, last;
   gboolean    sorted_by_frame_column;
 
+
+  /* adjust timestamp precision if auto is selected */
+  cf_timestamp_auto_precision(cf);
+
   /* Are there any columns with time stamps in the "command-line-specified"
      format?
 
@@ -1898,7 +2330,7 @@ typedef struct {
 } match_data;
 
 gboolean
-find_packet_protocol_tree(capture_file *cf, const char *string)
+cf_find_packet_protocol_tree(capture_file *cf, const char *string)
 {
   match_data           mdata;
 
@@ -1947,7 +2379,7 @@ match_subtree_text(proto_node *node, gpointer data)
   }
 
   /* Don't match invisible entries. */
-  if (!fi->visible)
+  if (PROTO_ITEM_IS_HIDDEN(node))
     return;
 
   /* was a free format label produced? */
@@ -1982,7 +2414,7 @@ match_subtree_text(proto_node *node, gpointer data)
 }
 
 gboolean
-find_packet_summary_line(capture_file *cf, const char *string)
+cf_find_packet_summary_line(capture_file *cf, const char *string)
 {
   match_data           mdata;
 
@@ -2043,7 +2475,7 @@ typedef struct {
 } cbs_t;       /* "Counted byte string" */
 
 gboolean
-find_packet_data(capture_file *cf, const guint8 *string, size_t string_size)
+cf_find_packet_data(capture_file *cf, const guint8 *string, size_t string_size)
 {
   cbs_t info;
 
@@ -2192,7 +2624,7 @@ match_binary(capture_file *cf, frame_data *fdata, void *criterion)
 }
 
 gboolean
-find_packet_dfilter(capture_file *cf, dfilter_t *sfcode)
+cf_find_packet_dfilter(capture_file *cf, dfilter_t *sfcode)
 {
   return find_packet(cf, match_dfilter, sfcode);
 }
@@ -2296,21 +2728,44 @@ find_packet(capture_file *cf,
            * we need an API for popping up alert boxes with
            * {Verb} and "Cancel".
            */
-          simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
-                        "%sBeginning of capture exceeded!%s\n\n"
-                        "Search is continued from the end of the capture.",
-                        simple_dialog_primary_start(), simple_dialog_primary_end());
-          fdata = cf->plist_end;       /* wrap around */
+
+          if (prefs.gui_find_wrap)
+          {
+              simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
+                            "%sBeginning of capture exceeded!%s\n\n"
+                            "Search is continued from the end of the capture.",
+                            simple_dialog_primary_start(), simple_dialog_primary_end());
+              fdata = cf->plist_end;   /* wrap around */
+          }
+          else
+          {
+              simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
+                            "%sBeginning of capture exceeded!%s\n\n"
+                            "Try searching forwards.",
+                            simple_dialog_primary_start(), simple_dialog_primary_end());
+              fdata = start_fd;        /* stay on previous packet */
+          }
         }
       } else {
         /* Go on to the next frame. */
         fdata = fdata->next;
         if (fdata == NULL) {
-          simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
-                        "%sEnd of capture exceeded!%s\n\n"
-                        "Search is continued from the start of the capture.",
-                        simple_dialog_primary_start(), simple_dialog_primary_end());
-          fdata = cf->plist;   /* wrap around */
+          if (prefs.gui_find_wrap)
+          {
+              simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
+                            "%sEnd of capture exceeded!%s\n\n"
+                            "Search is continued from the start of the capture.",
+                            simple_dialog_primary_start(), simple_dialog_primary_end());
+              fdata = cf->plist;       /* wrap around */
+          }
+          else
+          {
+              simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
+                            "%sEnd of capture exceeded!%s\n\n"
+                            "Try searching backwards.",
+                            simple_dialog_primary_start(), simple_dialog_primary_end());
+              fdata = start_fd;     /* stay on previous packet */
+          }
         }
       }
 
@@ -2362,7 +2817,7 @@ find_packet(capture_file *cf,
 }
 
 gboolean
-goto_frame(capture_file *cf, guint fnumber)
+cf_goto_frame(capture_file *cf, guint fnumber)
 {
   frame_data *fdata;
   int row;
@@ -2373,14 +2828,14 @@ goto_frame(capture_file *cf, guint fnumber)
   if (fdata == NULL) {
     /* we didn't find a packet with that packet number */
     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                 "There is no packet with that packet number.");
+                 "There is no packet with the packet number %u.", fnumber);
     return FALSE;      /* we failed to go to that packet */
   }
   if (!fdata->flags.passed_dfilter) {
     /* that packet currently isn't displayed */
     /* XXX - add it to the set of displayed packets? */
     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                 "That packet is not currently being displayed.");
+                 "The packet number %u isn't currently being displayed.", fnumber);
     return FALSE;      /* we failed to go to that packet */
   }
 
@@ -2395,7 +2850,7 @@ goto_frame(capture_file *cf, guint fnumber)
 }
 
 gboolean
-goto_top_frame(capture_file *cf)
+cf_goto_top_frame(capture_file *cf)
 {
   frame_data *fdata;
   int row;
@@ -2423,7 +2878,7 @@ goto_top_frame(capture_file *cf)
 }
 
 gboolean
-goto_bottom_frame(capture_file *cf)
+cf_goto_bottom_frame(capture_file *cf)
 {
   frame_data *fdata;
   int row;
@@ -2449,9 +2904,31 @@ goto_bottom_frame(capture_file *cf)
   return TRUE; /* we got to that packet */
 }
 
+/*
+ * Go to frame specified by currently selected protocol tree item.
+ */
+gboolean
+cf_goto_framenum(capture_file *cf)
+{
+  header_field_info       *hfinfo;
+  guint32                 framenum;
+
+  if (cf->finfo_selected) {
+    hfinfo = cf->finfo_selected->hfinfo;
+    g_assert(hfinfo);
+    if (hfinfo->type == FT_FRAMENUM) {
+      framenum = fvalue_get_integer(&cf->finfo_selected->value);
+      if (framenum != 0)
+        return cf_goto_frame(cf, framenum);
+      }
+  }
+
+  return FALSE;
+}
+
 /* Select the packet on a given row. */
 void
-select_packet(capture_file *cf, int row)
+cf_select_packet(capture_file *cf, int row)
 {
   frame_data *fdata;
   int err;
@@ -2467,10 +2944,10 @@ select_packet(capture_file *cf, int row)
        our version and the vanilla GTK+ version).
 
        This means that a "select-row" signal is emitted; this causes
-       "packet_list_select_cb()" to be called, which causes "select_packet()"
+       "packet_list_select_cb()" to be called, which causes "cf_select_packet()"
        to be called.
 
-       "select_packet()" fetches, above, the data associated with the
+       "cf_select_packet()" fetches, above, the data associated with the
        row that was selected; however, as "gtk_clist_append()", which
        called "real_insert_row()", hasn't yet returned, we haven't yet
        associated any data with that row, so we get back a null pointer.
@@ -2512,19 +2989,12 @@ select_packet(capture_file *cf, int row)
   epan_dissect_run(cf->edt, &cf->pseudo_header, cf->pd, cf->current_frame,
           NULL);
 
-  /* Display the GUI protocol tree and hex dump.
-     XXX - why do we dump core if we call "proto_tree_draw()"
-     before calling "add_byte_views()"? */
-  add_main_byte_views(cf->edt);
-  main_proto_tree_draw(cf->edt->tree);
-
-  /* A packet is selected. */
-  set_menus_for_selected_packet(cf);
+  cf_callback_invoke(cf_cb_packet_selected, cf);
 }
 
 /* Unselect the selected packet, if any. */
 void
-unselect_packet(capture_file *cf)
+cf_unselect_packet(capture_file *cf)
 {
   /* Destroy the epan_dissect_t for the unselected packet. */
   if (cf->edt != NULL) {
@@ -2532,44 +3002,48 @@ unselect_packet(capture_file *cf)
     cf->edt = NULL;
   }
 
-  /* Clear out the display of that packet. */
-  clear_tree_and_hex_views();
-
   /* No packet is selected. */
   cf->current_frame = NULL;
-  set_menus_for_selected_packet(cf);
+
+  cf_callback_invoke(cf_cb_packet_unselected, cf);
 
   /* No protocol tree means no selected field. */
-  unselect_field(cf);
+  cf_unselect_field(cf);
 }
 
 /* Unset the selected protocol tree field, if any. */
 void
-unselect_field(capture_file *cf)
+cf_unselect_field(capture_file *cf)
 {
-  statusbar_pop_field_msg();
   cf->finfo_selected = NULL;
-  set_menus_for_selected_tree_row(cf);
+
+  cf_callback_invoke(cf_cb_field_unselected, cf);
 }
 
 /*
  * Mark a particular frame.
  */
 void
-mark_frame(capture_file *cf, frame_data *frame)
+cf_mark_frame(capture_file *cf, frame_data *frame)
 {
-  frame->flags.marked = TRUE;
-  cf->marked_count++;
+  if (! frame->flags.marked) {
+    frame->flags.marked = TRUE;
+    if (cf->count > cf->marked_count)
+      cf->marked_count++;
+  }
 }
 
 /*
  * Unmark a particular frame.
  */
 void
-unmark_frame(capture_file *cf, frame_data *frame)
+cf_unmark_frame(capture_file *cf, frame_data *frame)
 {
-  frame->flags.marked = FALSE;
-  cf->marked_count--;
+  if (frame->flags.marked) {
+    frame->flags.marked = FALSE;
+    if (cf->marked_count > 0)
+      cf->marked_count--;
+  }
 }
 
 typedef struct {
@@ -2594,8 +3068,7 @@ save_packet(capture_file *cf _U_, frame_data *fdata,
   int           err;
 
   /* init the wtap header for saving */
-  hdr.ts.tv_sec  = fdata->abs_secs;
-  hdr.ts.tv_usec = fdata->abs_usecs;
+  hdr.ts         = *(struct wtap_nstime *) &fdata->abs_ts;
   hdr.caplen     = fdata->cap_len;
   hdr.len        = fdata->pkt_len;
   hdr.pkt_encap  = fdata->lnk_t;
@@ -2608,42 +3081,20 @@ save_packet(capture_file *cf _U_, frame_data *fdata,
   return TRUE;
 }
 
-gboolean
-cf_save(char *fname, capture_file *cf, packet_range_t *range, guint save_format)
+cf_status_t
+cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_format, gboolean compressed)
 {
   gchar        *from_filename;
-  gchar        *name_ptr, *save_msg, *save_fmt = " Saving: %s...";
-  size_t        msg_len;
   int           err;
   gboolean      do_copy;
   wtap_dumper  *pdh;
-  struct stat   infile, outfile;
   save_callback_args_t callback_args;
 
-  name_ptr = get_basename(fname);
-  msg_len = strlen(name_ptr) + strlen(save_fmt) + 2;
-  save_msg = g_malloc(msg_len);
-  snprintf(save_msg, msg_len, save_fmt, name_ptr);
-  statusbar_push_file_msg(save_msg);
-  g_free(save_msg);
+  cf_callback_invoke(cf_cb_file_safe_started, (gpointer) fname);
 
-  /*
-   * Check that the from file is not the same as to file
-   * We do it here so we catch all cases ...
-   * Unfortunately, the file requester gives us an absolute file
-   * name and the read file name may be relative (if supplied on
-   * the command line). From Joerg Mayer.
-   *
-   * This is a bit tricky on win32. The st_ino field is documented as:
-   * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
-   * but it *is* set to zero if stat() returns without an error,
-   * so this is working, but maybe not quite the way expected. ULFL
-   */
-   infile.st_ino = 1;   /* These prevent us from getting equality         */
-   outfile.st_ino = 2;  /* If one or other of the files is not accessible */
-   stat(cf->filename, &infile);
-   stat(fname, &outfile);
-   if (infile.st_ino == outfile.st_ino) {
+  /* don't write over an existing file. */
+  /* this should've been already checked by our caller, just to be sure... */
+  if (file_exists(fname)) {
     simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
       "%sCapture file: \"%s\" already exists!%s\n\n"
       "Please choose a different filename.",
@@ -2666,7 +3117,7 @@ cf_save(char *fname, capture_file *cf, packet_range_t *range, guint save_format)
       /* The file being saved is a temporary file from a live
          capture, so it doesn't need to stay around under that name;
         first, try renaming the capture buffer file to the new name. */
-#ifndef WIN32
+#ifndef _WIN32
       if (rename(cf->filename, fname) == 0) {
        /* That succeeded - there's no need to copy the source file. */
        from_filename = NULL;
@@ -2710,7 +3161,8 @@ cf_save(char *fname, capture_file *cf, packet_range_t *range, guint save_format)
     /* Either we're filtering packets, or we're saving in a different
        format; we can't do that by copying or moving the capture file,
        we have to do it by writing the packets out in Wiretap. */
-    pdh = wtap_dump_open(fname, save_format, cf->lnk_t, cf->snap, &err);
+    pdh = wtap_dump_open(fname, save_format, cf->lnk_t, cf->snap, 
+               compressed, &err);
     if (pdh == NULL) {
       cf_open_failure_alert_box(fname, err, NULL, TRUE, save_format);
       goto fail;
@@ -2756,8 +3208,7 @@ cf_save(char *fname, capture_file *cf, packet_range_t *range, guint save_format)
     }
   }
 
-  /* Pop the "Saving:" message off the status bar. */
-  statusbar_pop_file_msg();
+  cf_callback_invoke(cf_cb_file_safe_finished, NULL);
 
   if (packet_range_process_all(range)) {
     /* We saved the entire capture, not just some packets from it.
@@ -2772,34 +3223,33 @@ cf_save(char *fname, capture_file *cf, packet_range_t *range, guint save_format)
        time if the file is large. */
     cf->user_saved = TRUE;
 
-    if ((err = cf_open(fname, FALSE, cf)) == 0) {
+    if ((cf_open(cf, fname, FALSE, &err)) == CF_OK) {
       /* XXX - report errors if this fails?
          What should we return if it fails or is aborted? */
       switch (cf_read(cf)) {
 
-      case READ_SUCCESS:
-      case READ_ERROR:
+      case CF_READ_OK:
+      case CF_READ_ERROR:
        /* Just because we got an error, that doesn't mean we were unable
           to read any of the file; we handle what we could get from the
           file. */
        break;
 
-      case READ_ABORTED:
+      case CF_READ_ABORTED:
        /* The user bailed out of re-reading the capture file; the
           capture file has been closed - just return (without
           changing any menu settings; "cf_close()" set them
           correctly for the "no capture file open" state). */
        break;
       }
-      set_menus_for_unsaved_capture_file(FALSE);
+      cf_callback_invoke(cf_cb_file_safe_reload_finished, NULL);
     }
   }
-  return TRUE;
+  return CF_OK;
 
 fail:
-  /* Pop the "Saving:" message off the status bar. */
-  statusbar_pop_file_msg();
-  return FALSE;
+  cf_callback_invoke(cf_cb_file_safe_failed, NULL);
+  return CF_ERROR;
 }
 
 static void
@@ -2819,21 +3269,21 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
     case WTAP_ERR_RANDOM_OPEN_PIPE:
       /* Seen only when opening a capture file for reading. */
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                   "The file \"%s\" is a pipe or FIFO; Ethereal cannot read pipe or FIFO files.",
+                   "The file \"%s\" is a pipe or FIFO; Ethereal can't read pipe or FIFO files.",
                    filename);
       break;
 
     case WTAP_ERR_FILE_UNKNOWN_FORMAT:
       /* Seen only when opening a capture file for reading. */
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                   "The file \"%s\" is not a capture file in a format Ethereal understands.",
+                   "The file \"%s\" isn't a capture file in a format Ethereal understands.",
                    filename);
       break;
 
     case WTAP_ERR_UNSUPPORTED:
       /* Seen only when opening a capture file for reading. */
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                   "The file \"%s\" is not a capture file in a format Ethereal understands.\n"
+                   "The file \"%s\" isn't a capture file in a format Ethereal understands.\n"
                    "(%s)",
                    filename, err_info);
       g_free(err_info);
@@ -2842,7 +3292,7 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
     case WTAP_ERR_CANT_WRITE_TO_PIPE:
       /* Seen only when opening a capture file for writing. */
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                   "The file \"%s\" is a pipe, and %s capture files cannot be "
+                   "The file \"%s\" is a pipe, and %s capture files can't be "
                    "written to a pipe.",
                    filename, wtap_file_type_string(file_type));
       break;
@@ -2850,13 +3300,13 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
     case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
       /* Seen only when opening a capture file for writing. */
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                   "Ethereal does not support writing capture files in that format.");
+                   "Ethereal doesn't support writing capture files in that format.");
       break;
 
     case WTAP_ERR_UNSUPPORTED_ENCAP:
       if (for_writing) {
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                     "Ethereal cannot save this capture in that format.");
+                     "Ethereal can't save this capture in that format.");
       } else {
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                      "The file \"%s\" is a capture for a network type that Ethereal doesn't support.\n"
@@ -2869,7 +3319,7 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
     case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
       if (for_writing) {
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                     "Ethereal cannot save this capture in that format.");
+                     "Ethereal can't save this capture in that format.");
       } else {
        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                      "The file \"%s\" is a capture for a network type that Ethereal doesn't support.",
@@ -2911,6 +3361,11 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
                    filename);
       break;
 
+    case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
+      simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
+                   "Gzip compression not supported by this file type.");
+      break;
+
     default:
       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    "The file \"%s\" could not be %s: %s.",
@@ -2925,24 +3380,24 @@ cf_open_failure_alert_box(const char *filename, int err, gchar *err_info,
   }
 }
 
-static char *
+static const char *
 file_rename_error_message(int err)
 {
-  char *errmsg;
+  const char *errmsg;
   static char errmsg_errno[1024+1];
 
   switch (err) {
 
   case ENOENT:
-    errmsg = "The path to the file \"%s\" does not exist.";
+    errmsg = "The path to the file \"%s\" doesn't exist.";
     break;
 
   case EACCES:
-    errmsg = "You do not have permission to move the capture file to \"%s\".";
+    errmsg = "You don't have permission to move the capture file to \"%s\".";
     break;
 
   default:
-    snprintf(errmsg_errno, sizeof(errmsg_errno),
+    g_snprintf(errmsg_errno, sizeof(errmsg_errno),
                    "The file \"%%s\" could not be moved: %s.",
                                wtap_strerror(err));
     errmsg = errmsg_errno;
@@ -2952,22 +3407,26 @@ file_rename_error_message(int err)
 }
 
 char *
-cf_read_error_message(int err, gchar *err_info)
+cf_read_error_message(int err, const gchar *err_info)
 {
   static char errmsg_errno[1024+1];
 
   switch (err) {
 
-  case WTAP_ERR_UNSUPPORTED:
   case WTAP_ERR_UNSUPPORTED_ENCAP:
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+               "The file \"%%s\" has a packet with a network type that Ethereal doesn't support.\n(%s)",
+               err_info);
+      break;
+
   case WTAP_ERR_BAD_RECORD:
-    snprintf(errmsg_errno, sizeof(errmsg_errno),
+    g_snprintf(errmsg_errno, sizeof(errmsg_errno),
             "An error occurred while reading from the file \"%%s\": %s.\n(%s)",
             wtap_strerror(err), err_info);
     break;
 
   default:
-    snprintf(errmsg_errno, sizeof(errmsg_errno),
+    g_snprintf(errmsg_errno, sizeof(errmsg_errno),
             "An error occurred while reading from the file \"%%s\": %s.",
             wtap_strerror(err));
     break;
@@ -3025,6 +3484,57 @@ cf_close_failure_alert_box(const char *filename, int err)
   }
 }
 
+/* Reload the current capture file. */
+void
+cf_reload(capture_file *cf) {
+  gchar *filename;
+  gboolean is_tempfile;
+  int err;
+
+  /* If the file could be opened, "cf_open()" calls "cf_close()"
+     to get rid of state for the old capture file before filling in state
+     for the new capture file.  "cf_close()" will remove the file if
+     it's a temporary file; we don't want that to happen (for one thing,
+     it'd prevent subsequent reopens from working).  Remember whether it's
+     a temporary file, mark it as not being a temporary file, and then
+     reopen it as the type of file it was.
+
+     Also, "cf_close()" will free "cf->filename", so we must make
+     a copy of it first. */
+  filename = g_strdup(cf->filename);
+  is_tempfile = cf->is_tempfile;
+  cf->is_tempfile = FALSE;
+  if (cf_open(cf, filename, is_tempfile, &err) == CF_OK) {
+    switch (cf_read(cf)) {
+
+    case CF_READ_OK:
+    case CF_READ_ERROR:
+      /* Just because we got an error, that doesn't mean we were unable
+         to read any of the file; we handle what we could get from the
+         file. */
+      break;
+
+    case CF_READ_ABORTED:
+      /* The user bailed out of re-reading the capture file; the
+         capture file has been closed - just free the capture file name
+         string and return (without changing the last containing
+         directory). */
+      g_free(filename);
+      return;
+    }
+  } else {
+    /* The open failed, so "cf->is_tempfile" wasn't set to "is_tempfile".
+       Instead, the file was left open, so we should restore "cf->is_tempfile"
+       ourselves.
+
+       XXX - change the menu?  Presumably "cf_open()" will do that;
+       make sure it does! */
+    cf->is_tempfile = is_tempfile;
+  }
+  /* "cf_open()" made a copy of the file name we handed it, so
+     we should free up our copy. */
+  g_free(filename);
+}
 
 /* Copies a file in binary mode, for those operating systems that care about
  * such things.
@@ -3032,7 +3542,7 @@ cf_close_failure_alert_box(const char *filename, int err)
  * displays a simple dialog window with the error message.
  */
 static gboolean
-copy_binary_file(char *from_filename, char *to_filename)
+copy_binary_file(const char *from_filename, const char *to_filename)
 {
   int           from_fd, to_fd, nread, nwritten, err;
   guint8        pd[65536];