Turn "protocol_tree" and "fd" from global variables into members of a
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 24 Jul 1999 03:22:50 +0000 (03:22 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sat, 24 Jul 1999 03:22:50 +0000 (03:22 +0000)
"capture_file" structure, make a "select_packet()" routine to parallel
"unselect_packet()", and have "unselect_packet()" free the protocol tree
that the "protocol_tree" member of the "capture_file" passed to it
points to.

It should now be impossible to do a "Print Packet" operation if no
packet has been selected, so remove the check for that (we'll probably
just blow up if it happens; if it does, that means we probably forgot to
gray out "/File/Print Packet" somewhere, so we should fix that).

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@385 f5534014-38df-0310-8fa8-9805f1628bb7

ethereal.c
file.c
file.h

index 1b046d6e6b88d945f6191dc921b35149f9e380fa..374df144ff75380298f7c1458076f84bf2adcd95 100644 (file)
@@ -1,6 +1,6 @@
 /* ethereal.c
  *
- * $Id: ethereal.c,v 1.61 1999/07/24 02:42:51 guy Exp $
+ * $Id: ethereal.c,v 1.62 1999/07/24 03:22:50 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -110,12 +110,10 @@ static void print_close_cb(GtkWidget *close_bt, gpointer parent_w);
 FILE        *data_out_file = NULL;
 packet_info  pi;
 capture_file cf;
-proto_tree     *protocol_tree = NULL;
 GtkWidget   *file_sel, *packet_list, *tree_view, *byte_view, *prog_bar,
             *info_bar;
 GdkFont     *m_r_font, *m_b_font;
 guint        main_ctx, file_ctx;
-frame_data  *fd;
 gint         start_capture = 0;
 gchar        comp_info_str[256];
 gchar       *ethereal_path = NULL;
@@ -855,13 +853,8 @@ file_print_packet_cmd_cb(GtkWidget *widget, gpointer data) {
     return;
   }
 
-  if (protocol_tree == NULL) {
-    simple_dialog(ESD_TYPE_WARN, NULL,
-      "No packet is selected, so there's no packet to print.");
-    return;
-  }
   print_preamble(fh);
-  proto_tree_print(-1, (GNode*) protocol_tree, cf.pd, fd, fh);
+  proto_tree_print(-1, (GNode*) cf.protocol_tree, cf.pd, cf.fd, fh);
   print_finale(fh);
   close_print_dest(prefs.pr_dest == PR_DEST_FILE, fh);
 }
@@ -879,31 +872,7 @@ packet_list_select_cb(GtkWidget *w, gint row, gint col, gpointer evt) {
   }
 #endif
   blank_packetinfo();
-  gtk_text_freeze(GTK_TEXT(byte_view));
-  gtk_text_set_point(GTK_TEXT(byte_view), 0);
-  gtk_text_forward_delete(GTK_TEXT(byte_view),
-  gtk_text_get_length(GTK_TEXT(byte_view)));
-
-  /* get the frame data struct pointer for this frame */
-  fd = (frame_data *) gtk_clist_get_row_data(GTK_CLIST(w), row);
-  cf.selected_packet = g_list_index(cf.plist, (gpointer)fd);
-  cf.selected_row = row;
-  fseek(cf.fh, fd->file_off, SEEK_SET);
-  fread(cf.pd, sizeof(guint8), fd->cap_len, cf.fh);
-
-  /* create the logical protocol tree */
-  if (protocol_tree)
-      proto_tree_free(protocol_tree);
-  protocol_tree = proto_tree_create_root();
-  dissect_packet(cf.pd, fd, protocol_tree);
-
-  /* display the GUI protocol tree and hex dump */
-  proto_tree_draw(protocol_tree, tree_view);
-  packet_hex_print(GTK_TEXT(byte_view), cf.pd, fd->cap_len, -1, -1);
-  gtk_text_thaw(GTK_TEXT(byte_view));
-
-  /* A packet is selected, so "File/Print Packet" has something to print. */
-  set_menu_sensitivity("/File/Print Packet", TRUE);
+  select_packet(&cf, row);
 }
 
 void
@@ -930,7 +899,7 @@ tree_view_cb(GtkWidget *w) {
   gtk_text_set_point(GTK_TEXT(byte_view), 0);
   gtk_text_forward_delete(GTK_TEXT(byte_view),
     gtk_text_get_length(GTK_TEXT(byte_view)));
-  packet_hex_print(GTK_TEXT(byte_view), cf.pd, fd->cap_len, 
+  packet_hex_print(GTK_TEXT(byte_view), cf.pd, cf.fd->cap_len, 
                   tree_selected_start, 
                   tree_selected_len);
   
diff --git a/file.c b/file.c
index 37e1830b027597a42e3f0f6f22079ccebbcb8096..7fe931006205804cce152df123f23e02665ee2f9 100644 (file)
--- a/file.c
+++ b/file.c
@@ -1,7 +1,7 @@
 /* file.c
  * File I/O routines
  *
- * $Id: file.c,v 1.43 1999/07/24 02:42:50 guy Exp $
+ * $Id: file.c,v 1.44 1999/07/24 03:22:49 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -72,6 +72,7 @@
 #include "print.h"
 #include "file.h"
 #include "util.h"
+#include "gtkpacket.h"
 #include "dfilter.h"
 
 #include "packet-ncp.h"
@@ -675,16 +676,61 @@ change_time_formats(capture_file *cf)
   gtk_clist_thaw(GTK_CLIST(packet_list));
 }
 
+/* Select the packet on a given row. */
+void
+select_packet(capture_file *cf, int row)
+{
+  /* Clear out whatever's currently in the hex dump. */
+  gtk_text_freeze(GTK_TEXT(byte_view));
+  gtk_text_set_point(GTK_TEXT(byte_view), 0);
+  gtk_text_forward_delete(GTK_TEXT(byte_view),
+  gtk_text_get_length(GTK_TEXT(byte_view)));
+
+  /* Get the frame data struct pointer for this frame. */
+  cf->fd = (frame_data *) gtk_clist_get_row_data(GTK_CLIST(packet_list), row);
+
+  /* Get the data in that frame. */
+  fseek(cf->fh, cf->fd->file_off, SEEK_SET);
+  fread(cf->pd, sizeof(guint8), cf->fd->cap_len, cf->fh);
+
+  /* Mark that frame as the selected frame. */
+  cf->selected_packet = g_list_index(cf->plist, (gpointer)cf->fd);
+
+  /* Create the logical protocol tree. */
+  if (cf->protocol_tree)
+      proto_tree_free(cf->protocol_tree);
+  cf->protocol_tree = proto_tree_create_root();
+  dissect_packet(cf->pd, cf->fd, cf->protocol_tree);
+
+  /* Display the GUI protocol tree and hex dump. */
+  proto_tree_draw(cf->protocol_tree, tree_view);
+  packet_hex_print(GTK_TEXT(byte_view), cf->pd, cf->fd->cap_len, -1, -1);
+  gtk_text_thaw(GTK_TEXT(byte_view));
+
+  /* A packet is selected, so "File/Print Packet" has something to print. */
+  set_menu_sensitivity("/File/Print Packet", TRUE);
+}
+
 /* Unselect the selected packet, if any. */
 void
 unselect_packet(capture_file *cf)
 {
   cf->selected_packet = -1;    /* nothing there to be selected */
   cf->selected_row = -1;
+
+  /* Destroy the protocol tree for that packet. */
+  if (cf->protocol_tree != NULL) {
+    proto_tree_free(cf->protocol_tree);
+    cf->protocol_tree = NULL;
+  }
+
+  /* Clear the hex dump. */
   gtk_text_freeze(GTK_TEXT(byte_view));
   gtk_text_set_point(GTK_TEXT(byte_view), 0);
   gtk_text_forward_delete(GTK_TEXT(byte_view),
     gtk_text_get_length(GTK_TEXT(byte_view)));
+
+  /* Clear the protocol tree view. */
   gtk_text_thaw(GTK_TEXT(byte_view));
   gtk_tree_clear_items(GTK_TREE(tree_view), 0,
     g_list_length(GTK_TREE(tree_view)->children));
diff --git a/file.h b/file.h
index cebe6911a057b9d4695de49a9cdf7a3a12ec63fe..0bd34faad2c3d8daa9e129a9c087d3018ff0d4bb 100644 (file)
--- a/file.h
+++ b/file.h
@@ -1,7 +1,7 @@
 /* file.h
  * Definitions for file structures and routines
  *
- * $Id: file.h,v 1.22 1999/07/24 02:42:50 guy Exp $
+ * $Id: file.h,v 1.23 1999/07/24 03:22:50 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -78,6 +78,8 @@ typedef struct _capture_file {
   column_info  cinfo;    /* Column formatting information */
   int        selected_packet;   /* Index in packet list of currently selected packet, if any */
   int        selected_row;   /* Row in packet display of currently selected packet, if any */
+  frame_data *fd;        /* Frame data for currently selected packet */
+  proto_tree *protocol_tree; /* Protocol tree for currently selected packet */
   FILE       *print_fh;  /* File we're printing to */
 } capture_file;
 
@@ -103,6 +105,7 @@ int  tail_cap_file(char *, capture_file *);
 int print_packets(capture_file *cf, int to_file, const char *dest);
 void filter_packets(capture_file *);
 void change_time_formats(capture_file *);
+void select_packet(capture_file *, int);
 void unselect_packet(capture_file *);
 
 /* Moves or copies a file. Returns 0 on failure, 1 on success */