2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.8 1998/10/28 21:38:11 gerald Exp $
6 * Gilbert Ramirez <gram@verdict.uthscsa.edu>
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@zing.org>
10 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
46 static void printer_opts_file_cb(GtkWidget *w, gpointer te);
47 static void printer_opts_fs_cancel_cb(GtkWidget *w, gpointer data);
48 static void printer_opts_fs_ok_cb(GtkWidget *w, gpointer data);
49 static void printer_opts_toggle_format(GtkWidget *widget, gpointer data);
50 static void printer_opts_toggle_dest(GtkWidget *widget, gpointer data);
51 static void dumpit (FILE *fh, register const u_char *cp, register u_int length);
52 static void dumpit_ps (FILE *fh, register const u_char *cp, register u_int length);
53 static void ps_clean_string(unsigned char *out, const unsigned char *in,
60 /* Key for gtk_object_set_data */
61 #define PRINT_CMD_TE_KEY "printer_command_entry"
62 #define PRINT_FILE_TE_KEY "printer_file_entry"
64 GtkWidget * printer_prefs_show()
66 GtkWidget *main_vb, *main_tb, *button;
67 GtkWidget *format_hb, *format_lb;
68 GtkWidget *dest_hb, *dest_lb;
69 GtkWidget *cmd_lb, *cmd_te;
70 GtkWidget *file_bt_hb, *file_bt, *file_te;
71 GSList *format_grp, *dest_grp;
73 /* Enclosing containers for each row of widgets */
74 main_vb = gtk_vbox_new(FALSE, 5);
75 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
77 main_tb = gtk_table_new(4, 2, FALSE);
78 gtk_box_pack_start(GTK_BOX(main_vb), main_tb, FALSE, FALSE, 0);
79 gtk_table_set_row_spacings(GTK_TABLE(main_tb), 10);
80 gtk_table_set_col_spacings(GTK_TABLE(main_tb), 15);
81 gtk_widget_show(main_tb);
84 format_lb = gtk_label_new("Format:");
85 gtk_misc_set_alignment(GTK_MISC(format_lb), 1.0, 0.5);
86 gtk_table_attach_defaults(GTK_TABLE(main_tb), format_lb, 0, 1, 0, 1);
87 gtk_widget_show(format_lb);
89 format_hb = gtk_hbox_new(FALSE, 0);
90 gtk_table_attach_defaults(GTK_TABLE(main_tb), format_hb, 1, 2, 0, 1);
91 gtk_widget_show(format_hb);
93 button = gtk_radio_button_new_with_label(NULL, "Plain Text");
94 if (prefs.pr_format == PR_FMT_TEXT) {
95 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
97 format_grp = gtk_radio_button_group(GTK_RADIO_BUTTON(button));
98 gtk_box_pack_start(GTK_BOX(format_hb), button, FALSE, FALSE, 10);
99 gtk_widget_show(button);
101 button = gtk_radio_button_new_with_label(format_grp, "PostScript");
102 if (prefs.pr_format == PR_FMT_PS) {
103 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
105 gtk_signal_connect(GTK_OBJECT(button), "toggled",
106 GTK_SIGNAL_FUNC(printer_opts_toggle_format), NULL);
107 gtk_box_pack_start(GTK_BOX(format_hb), button, FALSE, FALSE, 10);
108 gtk_widget_show(button);
110 /* Output destination */
111 dest_lb = gtk_label_new("Print to:");
112 gtk_misc_set_alignment(GTK_MISC(dest_lb), 1.0, 0.5);
113 gtk_table_attach_defaults(GTK_TABLE(main_tb), dest_lb, 0, 1, 1, 2);
114 gtk_widget_show(dest_lb);
116 dest_hb = gtk_hbox_new(FALSE, 0);
117 gtk_table_attach_defaults(GTK_TABLE(main_tb), dest_hb, 1, 2, 1, 2);
118 gtk_widget_show(dest_hb);
120 button = gtk_radio_button_new_with_label(NULL, "Command");
121 if (prefs.pr_dest == PR_DEST_CMD) {
122 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
124 dest_grp = gtk_radio_button_group(GTK_RADIO_BUTTON(button));
125 gtk_box_pack_start(GTK_BOX(dest_hb), button, FALSE, FALSE, 10);
126 gtk_widget_show(button);
128 button = gtk_radio_button_new_with_label(dest_grp, "File");
129 if (prefs.pr_dest == PR_DEST_FILE) {
130 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
132 gtk_signal_connect(GTK_OBJECT(button), "toggled",
133 GTK_SIGNAL_FUNC(printer_opts_toggle_dest), NULL);
134 gtk_box_pack_start(GTK_BOX(dest_hb), button, FALSE, FALSE, 10);
135 gtk_widget_show(button);
137 /* Command text entry */
138 cmd_lb = gtk_label_new("Command:");
139 gtk_misc_set_alignment(GTK_MISC(cmd_lb), 1.0, 0.5);
140 gtk_table_attach_defaults(GTK_TABLE(main_tb), cmd_lb, 0, 1, 2, 3);
141 gtk_widget_show(cmd_lb);
143 cmd_te = gtk_entry_new();
144 gtk_object_set_data(GTK_OBJECT(main_vb), PRINT_CMD_TE_KEY, cmd_te);
145 if (prefs.pr_cmd) gtk_entry_set_text(GTK_ENTRY(cmd_te), prefs.pr_cmd);
146 gtk_table_attach_defaults(GTK_TABLE(main_tb), cmd_te, 1, 2, 2, 3);
147 gtk_widget_show(cmd_te);
149 /* File button and text entry */
150 file_bt_hb = gtk_hbox_new(FALSE, 0);
151 gtk_table_attach_defaults(GTK_TABLE(main_tb), file_bt_hb, 0, 1, 3, 4);
152 gtk_widget_show(file_bt_hb);
154 file_bt = gtk_button_new_with_label("File:");
155 gtk_box_pack_end(GTK_BOX(file_bt_hb), file_bt, FALSE, FALSE, 0);
156 gtk_widget_show(file_bt);
158 file_te = gtk_entry_new();
159 gtk_object_set_data(GTK_OBJECT(main_vb), PRINT_FILE_TE_KEY, file_te);
160 if (prefs.pr_file) gtk_entry_set_text(GTK_ENTRY(file_te), prefs.pr_file);
161 gtk_table_attach_defaults(GTK_TABLE(main_tb), file_te, 1, 2, 3, 4);
162 gtk_widget_show(file_te);
164 gtk_signal_connect_object(GTK_OBJECT(file_bt), "clicked",
165 GTK_SIGNAL_FUNC(printer_opts_file_cb), GTK_OBJECT(file_te));
167 gtk_widget_show(main_vb);
173 printer_opts_file_cb(GtkWidget *w, gpointer te) {
176 fs = gtk_file_selection_new ("Ethereal: Print to a File");
177 gtk_object_set_data(GTK_OBJECT(fs), PRINT_FILE_TE_KEY, te);
179 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(fs)->ok_button),
180 "clicked", (GtkSignalFunc) printer_opts_fs_ok_cb, fs);
182 /* Connect the cancel_button to destroy the widget */
183 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(fs)->cancel_button),
184 "clicked", (GtkSignalFunc) printer_opts_fs_cancel_cb, fs);
190 printer_opts_fs_ok_cb(GtkWidget *w, gpointer data) {
192 gtk_entry_set_text(GTK_ENTRY(gtk_object_get_data(GTK_OBJECT(data),
194 gtk_file_selection_get_filename (GTK_FILE_SELECTION(data)));
195 printer_opts_fs_cancel_cb(w, data);
199 printer_opts_fs_cancel_cb(GtkWidget *w, gpointer data) {
201 gtk_widget_destroy(GTK_WIDGET(data));
205 printer_prefs_ok(GtkWidget *w)
207 if(prefs.pr_cmd) g_free(prefs.pr_cmd);
209 g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_object_get_data(GTK_OBJECT(w),
210 PRINT_CMD_TE_KEY))));
212 if(prefs.pr_file) g_free(prefs.pr_file);
214 g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_object_get_data(GTK_OBJECT(w),
215 PRINT_FILE_TE_KEY))));
219 printer_prefs_save(GtkWidget *w)
225 printer_prefs_cancel(GtkWidget *w)
230 printer_opts_toggle_format(GtkWidget *widget, gpointer data)
232 if (GTK_TOGGLE_BUTTON (widget)->active) {
233 prefs.pr_format = PR_FMT_PS;
234 /* toggle file/cmd */
237 prefs.pr_format = PR_FMT_TEXT;
238 /* toggle file/cmd */
243 printer_opts_toggle_dest(GtkWidget *widget, gpointer data)
245 if (GTK_TOGGLE_BUTTON (widget)->active) {
246 prefs.pr_dest = PR_DEST_FILE;
249 prefs.pr_dest = PR_DEST_CMD;
253 /* ========================================================== */
254 void print_tree(const u_char *pd, frame_data *fd, GtkTree *tree)
259 /* Open the file or command for output */
260 if (prefs.pr_dest == PR_DEST_CMD) {
262 fh = popen(prefs.pr_cmd, "w");
266 fh = fopen(prefs.pr_file, "w");
270 g_error("Cannot open %s for output.\n", out);
274 /* Create the output */
275 if (prefs.pr_format == PR_FMT_TEXT) {
276 print_tree_text(fh, pd, fd, tree);
279 print_ps_preamble(fh);
280 print_tree_ps(fh, pd, fd, tree);
284 /* Close the file or command */
285 if (prefs.pr_dest == PR_DEST_CMD) {
293 /* Print a tree's data in plain text */
294 void print_tree_text(FILE *fh, const u_char *pd, frame_data *fd, GtkTree *tree)
296 GList *children, *child, *widgets, *label;
302 gint data_start, data_len;
304 /* Prepare the tabs for printing, depending on tree level */
305 num_spaces = tree->level * 4;
306 if (num_spaces > 40) {
309 for (i = 0; i < num_spaces; i++) {
312 /* The string is NUL-terminated */
313 space[num_spaces] = 0;
315 /* Get the children of this tree */
316 children = tree->children;
317 num_children = g_list_length(children);
319 for (i = 0; i < num_children; i++) {
320 /* Each child of the tree is a widget container */
321 child = g_list_nth(children, i);
322 widgets = gtk_container_children(GTK_CONTAINER(child->data));
324 /* And the container holds a label object, which holds text */
325 label = g_list_nth(widgets, 0);
326 gtk_label_get(GTK_LABEL(label->data), &text);
329 fprintf(fh, "%s%s\n", space, text);
331 /* Recurse into the subtree, if it exists */
332 subtree = (GTK_TREE_ITEM(child->data))->subtree;
334 print_tree_text(fh, pd, fd, GTK_TREE(subtree));
336 else if (strncmp("Data (", text, 6) == 0) {
337 data_start = (gint) gtk_object_get_data(GTK_OBJECT(child->data),
338 E_TREEINFO_START_KEY);
339 data_len = (gint) gtk_object_get_data(GTK_OBJECT(child->data),
341 dumpit(fh, &pd[data_start], data_len);
346 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
347 only slightly modified for ethereal by Gilbert Ramirez. */
349 void dumpit (FILE *fh, register const u_char *cp, register u_int length)
351 register int ad, i, j, k;
354 static u_char binhex[16] = {
355 '0', '1', '2', '3', '4', '5', '6', '7',
356 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
358 memset (line, ' ', sizeof line);
359 line[sizeof (line)-1] = 0;
360 for (ad=i=j=k=0; i<length; i++) {
362 line[j++] = binhex[c>>4];
363 line[j++] = binhex[c&0xf];
365 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
366 if ((i & 15) == 15) {
367 fprintf (fh, "\n%4x %s", ad, line);
368 /*if (i==15) printf (" %d", length);*/
369 memset (line, ' ', sizeof line);
370 line[sizeof (line)-1] = j = k = 0;
375 if (line[0] != ' ') fprintf (fh, "\n%4x %s", ad, line);
381 #define MAX_LINE_LENGTH 256
384 void dumpit_ps (FILE *fh, register const u_char *cp, register u_int length)
386 register int ad, i, j, k;
389 static u_char binhex[16] = {
390 '0', '1', '2', '3', '4', '5', '6', '7',
391 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
392 u_char psline[MAX_LINE_LENGTH];
394 memset (line, ' ', sizeof line);
395 line[sizeof (line)-1] = 0;
396 for (ad=i=j=k=0; i<length; i++) {
398 line[j++] = binhex[c>>4];
399 line[j++] = binhex[c&0xf];
401 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
402 if ((i & 15) == 15) {
403 ps_clean_string(psline, line, MAX_LINE_LENGTH);
404 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
405 memset (line, ' ', sizeof line);
406 line[sizeof (line)-1] = j = k = 0;
411 if (line[0] != ' ') {
412 ps_clean_string(psline, line, MAX_LINE_LENGTH);
413 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
419 /* Print a tree's data in PostScript */
420 void print_tree_ps(FILE *fh, const u_char *pd, frame_data *fd, GtkTree *tree)
422 GList *children, *child, *widgets, *label;
426 gint data_start, data_len;
427 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
429 /* Get the children of this tree */
430 children = tree->children;
431 num_children = g_list_length(children);
433 for (i = 0; i < num_children; i++) {
434 /* Each child of the tree is a widget container */
435 child = g_list_nth(children, i);
436 widgets = gtk_container_children(GTK_CONTAINER(child->data));
438 /* And the container holds a label object, which holds text */
439 label = g_list_nth(widgets, 0);
440 gtk_label_get(GTK_LABEL(label->data), &text);
443 ps_clean_string(psbuffer, text, MAX_LINE_LENGTH);
444 fprintf(fh, "%d (%s) putline\n", tree->level, psbuffer);
446 /* Recurse into the subtree, if it exists */
447 subtree = (GTK_TREE_ITEM(child->data))->subtree;
449 print_tree_ps(fh, pd, fd, GTK_TREE(subtree));
451 else if (strncmp("Data (", text, 6) == 0) {
452 data_start = (gint) gtk_object_get_data(GTK_OBJECT(child->data),
453 E_TREEINFO_START_KEY);
454 data_len = (gint) gtk_object_get_data(GTK_OBJECT(child->data),
457 dumpit_ps(fh, &pd[data_start], data_len);
463 void ps_clean_string(unsigned char *out, const unsigned char *in,
469 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {