2 * Routines for printing packet analysis trees.
4 * $Id: print.c,v 1.5 1998/10/10 03:32:19 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>
45 static void printer_opts_file_cb(GtkWidget *w, gpointer te);
46 static void printer_opts_fs_cancel_cb(GtkWidget *w, gpointer data);
47 static void printer_opts_fs_ok_cb(GtkWidget *w, gpointer data);
48 static void printer_opts_toggle_format(GtkWidget *widget, gpointer data);
49 static void printer_opts_toggle_dest(GtkWidget *widget, gpointer data);
50 static void dumpit (FILE *fh, register const u_char *cp, register u_int length);
51 static void dumpit_ps (FILE *fh, register const u_char *cp, register u_int length);
52 static void ps_clean_string(unsigned char *out, const unsigned char *in,
59 /* Key for gtk_object_set_data */
60 const gchar *print_prefs_key = "printer_prefs_data";
61 GtkWidget * printer_prefs_show()
63 GtkWidget *main_vb, *button;
64 GtkWidget *format_hb, *format_lb;
65 GtkWidget *dest_hb, *dest_lb;
66 GtkWidget *cmd_hb, *cmd_lb, *cmd_te;
67 GtkWidget *file_hb, *file_bt, *file_te;
68 GSList *format_grp, *dest_grp;
69 pr_opts *temp_pr_opts = g_malloc(sizeof(pr_opts));
71 /* Make a working copy of the printer data */
72 memcpy(temp_pr_opts, &printer_opts, sizeof(pr_opts));
73 /* temp_pr_opts->cmd = g_strdup(printer_opts->cmd);
74 temp_pr_opts->file = g_strdup(printer_opts->file);*/
76 /* Container for each row of widgets */
77 main_vb = gtk_vbox_new(FALSE, 3);
78 gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
79 gtk_widget_show(main_vb);
80 gtk_object_set_data(GTK_OBJECT(main_vb), print_prefs_key,
84 format_hb = gtk_hbox_new(FALSE, 1);
85 gtk_container_add(GTK_CONTAINER(main_vb), format_hb);
86 gtk_widget_show(format_hb);
88 format_lb = gtk_label_new("Format:");
89 gtk_box_pack_start(GTK_BOX(format_hb), format_lb, FALSE, FALSE, 3);
90 gtk_widget_show(format_lb);
92 button = gtk_radio_button_new_with_label(NULL, "Plain Text");
93 if (printer_opts.output_format == 0) {
94 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
96 format_grp = gtk_radio_button_group(GTK_RADIO_BUTTON(button));
97 gtk_box_pack_start(GTK_BOX(format_hb), button, TRUE, TRUE, 0);
98 gtk_widget_show(button);
100 button = gtk_radio_button_new_with_label(format_grp, "PostScript");
101 if (printer_opts.output_format == 1) {
102 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
104 gtk_signal_connect(GTK_OBJECT(button), "toggled",
105 GTK_SIGNAL_FUNC(printer_opts_toggle_format),
106 (gpointer)temp_pr_opts);
107 gtk_box_pack_start(GTK_BOX(format_hb), button, TRUE, TRUE, 0);
108 gtk_widget_show(button);
110 /* Output destination */
111 dest_hb = gtk_hbox_new(FALSE, 1);
112 gtk_container_add(GTK_CONTAINER(main_vb), dest_hb);
113 gtk_widget_show(dest_hb);
115 dest_lb = gtk_label_new("Print to:");
116 gtk_box_pack_start(GTK_BOX(dest_hb), dest_lb, FALSE, FALSE, 3);
117 gtk_widget_show(dest_lb);
119 button = gtk_radio_button_new_with_label(NULL, "Command");
120 if (printer_opts.output_dest == 0) {
121 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
123 dest_grp = gtk_radio_button_group(GTK_RADIO_BUTTON(button));
124 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
125 gtk_box_pack_start(GTK_BOX(dest_hb), button, TRUE, TRUE, 0);
126 gtk_widget_show(button);
128 button = gtk_radio_button_new_with_label(dest_grp, "File");
129 if (printer_opts.output_dest == 1) {
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),
134 (gpointer)temp_pr_opts);
135 gtk_box_pack_start(GTK_BOX(dest_hb), button, TRUE, TRUE, 0);
136 gtk_widget_show(button);
138 /* Command text entry */
139 cmd_hb = gtk_hbox_new(FALSE, 1);
140 gtk_container_add(GTK_CONTAINER(main_vb), cmd_hb);
141 gtk_widget_show(cmd_hb);
143 cmd_lb = gtk_label_new("Command:");
144 gtk_box_pack_start(GTK_BOX(cmd_hb), cmd_lb, FALSE, FALSE, 3);
145 gtk_widget_show(cmd_lb);
147 cmd_te = gtk_entry_new();
148 temp_pr_opts->cmd_te = cmd_te;
149 gtk_entry_set_text(GTK_ENTRY(cmd_te), printer_opts.cmd);
150 gtk_box_pack_start(GTK_BOX(cmd_hb), cmd_te, TRUE, TRUE, 3);
151 gtk_widget_show(cmd_te);
153 /* File button and text entry */
154 file_hb = gtk_hbox_new(FALSE, 1);
155 gtk_container_add(GTK_CONTAINER(main_vb), file_hb);
156 gtk_widget_show(file_hb);
158 file_bt = gtk_button_new_with_label("File:");
159 gtk_box_pack_start(GTK_BOX(file_hb), file_bt, FALSE, FALSE, 3);
160 gtk_widget_show(file_bt);
162 file_te = gtk_entry_new();
163 temp_pr_opts->file_te = file_te;
164 gtk_entry_set_text(GTK_ENTRY(file_te), printer_opts.file);
165 gtk_box_pack_start(GTK_BOX(file_hb), file_te, TRUE, TRUE, 3);
166 gtk_widget_show(file_te);
168 gtk_signal_connect_object(GTK_OBJECT(file_bt), "clicked",
169 GTK_SIGNAL_FUNC(printer_opts_file_cb), GTK_OBJECT(file_te));
177 printer_opts_file_cb(GtkWidget *w, gpointer te) {
178 GtkWidget *fs, **w_list;
180 w_list = g_malloc(2 * sizeof(GtkWidget *));
182 fs = gtk_file_selection_new ("Ethereal: Print to a File");
184 w_list[1] = (GtkWidget *) te;
186 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(fs)->ok_button),
187 "clicked", (GtkSignalFunc) printer_opts_fs_ok_cb, w_list);
189 /* Connect the cancel_button to destroy the widget */
190 gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(fs)->cancel_button),
191 "clicked", (GtkSignalFunc) printer_opts_fs_cancel_cb, w_list);
197 printer_opts_fs_ok_cb(GtkWidget *w, gpointer data) {
198 GtkWidget **w_list = (GtkWidget **) data;
200 gtk_entry_set_text(GTK_ENTRY(w_list[1]),
201 gtk_file_selection_get_filename (GTK_FILE_SELECTION(w_list[0])));
202 printer_opts_fs_cancel_cb(w, data);
206 printer_opts_fs_cancel_cb(GtkWidget *w, gpointer data) {
207 GtkWidget **w_list = (GtkWidget **) data;
209 gtk_widget_destroy(w_list[0]);
214 printer_prefs_ok(GtkWidget *w)
216 pr_opts *data = gtk_object_get_data(GTK_OBJECT(w), print_prefs_key);
218 printer_opts.output_format = ((pr_opts*)data)->output_format;
219 printer_opts.output_dest = ((pr_opts*)data)->output_dest;
221 free(printer_opts.cmd);
223 g_strdup(gtk_entry_get_text(GTK_ENTRY(((pr_opts*)data)->cmd_te)));
225 free(printer_opts.file);
227 g_strdup(gtk_entry_get_text(GTK_ENTRY(((pr_opts*)data)->file_te)));
233 printer_prefs_cancel(GtkWidget *w)
235 pr_opts *data = gtk_object_get_data(GTK_OBJECT(w), print_prefs_key);
241 printer_opts_toggle_format(GtkWidget *widget, gpointer data)
243 if (GTK_TOGGLE_BUTTON (widget)->active) {
244 ((pr_opts*)data)->output_format = 1;
245 /* toggle file/cmd */
248 ((pr_opts*)data)->output_format = 0;
249 /* toggle file/cmd */
254 printer_opts_toggle_dest(GtkWidget *widget, gpointer data)
256 if (GTK_TOGGLE_BUTTON (widget)->active) {
257 ((pr_opts*)data)->output_dest = 1;
260 ((pr_opts*)data)->output_dest = 0;
264 /* ========================================================== */
265 void print_tree(const u_char *pd, frame_data *fd, GtkTree *tree)
270 /* Open the file or command for output */
271 if (printer_opts.output_dest == 0) {
272 out = printer_opts.cmd;
273 fh = popen(printer_opts.cmd, "w");
276 out = printer_opts.file;
277 fh = fopen(printer_opts.file, "w");
281 g_error("Cannot open %s for output.\n", out);
285 /* Create the output */
286 if (printer_opts.output_format == 0) {
287 print_tree_text(fh, pd, fd, tree);
290 print_ps_preamble(fh);
291 print_tree_ps(fh, pd, fd, tree);
295 /* Close the file or command */
296 if (printer_opts.output_dest == 0) {
304 /* Print a tree's data in plain text */
305 void print_tree_text(FILE *fh, const u_char *pd, frame_data *fd, GtkTree *tree)
307 GList *children, *child, *widgets, *label;
313 gint data_start, data_len;
315 /* Prepare the tabs for printing, depending on tree level */
316 num_spaces = tree->level * 4;
317 if (num_spaces > 40) {
320 for (i = 0; i < num_spaces; i++) {
323 /* The string is NUL-terminated */
324 space[num_spaces] = 0;
326 /* Get the children of this tree */
327 children = tree->children;
328 num_children = g_list_length(children);
330 for (i = 0; i < num_children; i++) {
331 /* Each child of the tree is a widget container */
332 child = g_list_nth(children, i);
333 widgets = gtk_container_children(GTK_CONTAINER(child->data));
335 /* And the container holds a label object, which holds text */
336 label = g_list_nth(widgets, 0);
337 gtk_label_get(GTK_LABEL(label->data), &text);
340 fprintf(fh, "%s%s\n", space, text);
342 /* Recurse into the subtree, if it exists */
343 subtree = (GTK_TREE_ITEM(child->data))->subtree;
345 print_tree_text(fh, pd, fd, GTK_TREE(subtree));
347 else if (strcmp("Data", text) == 0) {
348 decode_start_len(GTK_TREE_ITEM(child->data), &data_start, &data_len);
349 dumpit(fh, &pd[data_start], data_len);
354 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
355 only slightly modified for ethereal by Gilbert Ramirez. */
357 void dumpit (FILE *fh, register const u_char *cp, register u_int length)
359 register int ad, i, j, k;
362 static u_char binhex[16] = {
363 '0', '1', '2', '3', '4', '5', '6', '7',
364 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
366 memset (line, ' ', sizeof line);
367 line[sizeof (line)-1] = 0;
368 for (ad=i=j=k=0; i<length; i++) {
370 line[j++] = binhex[c>>4];
371 line[j++] = binhex[c&0xf];
373 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
374 if ((i & 15) == 15) {
375 fprintf (fh, "\n%4x %s", ad, line);
376 /*if (i==15) printf (" %d", length);*/
377 memset (line, ' ', sizeof line);
378 line[sizeof (line)-1] = j = k = 0;
383 if (line[0] != ' ') fprintf (fh, "\n%4x %s", ad, line);
389 #define MAX_LINE_LENGTH 256
392 void dumpit_ps (FILE *fh, register const u_char *cp, register u_int length)
394 register int ad, i, j, k;
397 static u_char binhex[16] = {
398 '0', '1', '2', '3', '4', '5', '6', '7',
399 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
400 u_char psline[MAX_LINE_LENGTH];
402 memset (line, ' ', sizeof line);
403 line[sizeof (line)-1] = 0;
404 for (ad=i=j=k=0; i<length; i++) {
406 line[j++] = binhex[c>>4];
407 line[j++] = binhex[c&0xf];
409 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
410 if ((i & 15) == 15) {
411 ps_clean_string(psline, line, MAX_LINE_LENGTH);
412 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
413 memset (line, ' ', sizeof line);
414 line[sizeof (line)-1] = j = k = 0;
419 if (line[0] != ' ') {
420 ps_clean_string(psline, line, MAX_LINE_LENGTH);
421 fprintf (fh, "(%4x %s) hexdump\n", ad, psline);
427 /* Print a tree's data in PostScript */
428 void print_tree_ps(FILE *fh, const u_char *pd, frame_data *fd, GtkTree *tree)
430 GList *children, *child, *widgets, *label;
434 gint data_start, data_len;
435 char psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
437 /* Get the children of this tree */
438 children = tree->children;
439 num_children = g_list_length(children);
441 for (i = 0; i < num_children; i++) {
442 /* Each child of the tree is a widget container */
443 child = g_list_nth(children, i);
444 widgets = gtk_container_children(GTK_CONTAINER(child->data));
446 /* And the container holds a label object, which holds text */
447 label = g_list_nth(widgets, 0);
448 gtk_label_get(GTK_LABEL(label->data), &text);
451 ps_clean_string(psbuffer, text, MAX_LINE_LENGTH);
452 fprintf(fh, "%d (%s) putline\n", tree->level, psbuffer);
454 /* Recurse into the subtree, if it exists */
455 subtree = (GTK_TREE_ITEM(child->data))->subtree;
457 print_tree_ps(fh, pd, fd, GTK_TREE(subtree));
459 else if (strcmp("Data", text) == 0) {
460 decode_start_len(GTK_TREE_ITEM(child->data), &data_start, &data_len);
462 dumpit_ps(fh, &pd[data_start], data_len);
468 void ps_clean_string(unsigned char *out, const unsigned char *in,
474 for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {