Converted UDP fields to new proto_tree functions.
[obnox/wireshark/wip.git] / print.c
1 /* print.c
2  * Routines for printing packet analysis trees.
3  *
4  * $Id: print.c,v 1.13 1999/07/15 15:32:43 gram Exp $
5  *
6  * Gilbert Ramirez <gram@verdict.uthscsa.edu>
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
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.
17  * 
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.
22  * 
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.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <gtk/gtk.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
38 #endif
39
40 #include "ethereal.h"
41 #include "packet.h"
42 #include "gtkpacket.h"
43 #include "prefs.h"
44 #include "print.h"
45 #include "ps.h"
46
47 static void printer_opts_file_cb(GtkWidget *w, gpointer te);
48 static void printer_opts_fs_cancel_cb(GtkWidget *w, gpointer data);
49 static void printer_opts_fs_ok_cb(GtkWidget *w, gpointer data);
50 static void printer_opts_toggle_format(GtkWidget *widget, gpointer data);
51 static void printer_opts_toggle_dest(GtkWidget *widget, gpointer data);
52 static void proto_tree_print_node_text(GNode *node, gpointer data);
53 static void dumpit (FILE *fh, register const u_char *cp, register u_int length);
54 static void proto_tree_print_node_ps(GNode *node, gpointer data);
55 static void ps_clean_string(unsigned char *out, const unsigned char *in,
56                         int outbuf_size);
57 static void dumpit_ps (FILE *fh, register const u_char *cp, register u_int length);
58
59 extern e_prefs prefs;
60 extern int proto_data; /* in packet-data.c */
61
62 /* #include "ps.c" */
63
64 /* Key for gtk_object_set_data */
65 #define PRINT_CMD_TE_KEY  "printer_command_entry"
66 #define PRINT_FILE_TE_KEY "printer_file_entry"
67
68 GtkWidget * printer_prefs_show()
69 {
70         GtkWidget       *main_vb, *main_tb, *button;
71         GtkWidget       *format_hb, *format_lb;
72         GtkWidget       *dest_hb, *dest_lb;
73         GtkWidget       *cmd_lb, *cmd_te;
74         GtkWidget       *file_bt_hb, *file_bt, *file_te;
75         GSList          *format_grp, *dest_grp;
76
77         /* Enclosing containers for each row of widgets */
78   main_vb = gtk_vbox_new(FALSE, 5);
79   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
80
81         main_tb = gtk_table_new(4, 2, FALSE);
82         gtk_box_pack_start(GTK_BOX(main_vb), main_tb, FALSE, FALSE, 0);
83   gtk_table_set_row_spacings(GTK_TABLE(main_tb), 10);
84   gtk_table_set_col_spacings(GTK_TABLE(main_tb), 15);
85   gtk_widget_show(main_tb);
86
87         /* Output format */
88         format_lb = gtk_label_new("Format:");
89   gtk_misc_set_alignment(GTK_MISC(format_lb), 1.0, 0.5);
90   gtk_table_attach_defaults(GTK_TABLE(main_tb), format_lb, 0, 1, 0, 1);
91         gtk_widget_show(format_lb);
92
93         format_hb = gtk_hbox_new(FALSE, 0);
94   gtk_table_attach_defaults(GTK_TABLE(main_tb), format_hb, 1, 2, 0, 1);
95         gtk_widget_show(format_hb);
96
97         button = gtk_radio_button_new_with_label(NULL, "Plain Text");
98         if (prefs.pr_format == PR_FMT_TEXT) {
99                 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
100         }
101         format_grp = gtk_radio_button_group(GTK_RADIO_BUTTON(button));
102         gtk_box_pack_start(GTK_BOX(format_hb), button, FALSE, FALSE, 10);
103         gtk_widget_show(button);
104
105         button = gtk_radio_button_new_with_label(format_grp, "PostScript");
106         if (prefs.pr_format == PR_FMT_PS) {
107                 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
108         }
109         gtk_signal_connect(GTK_OBJECT(button), "toggled",
110                         GTK_SIGNAL_FUNC(printer_opts_toggle_format), NULL);
111         gtk_box_pack_start(GTK_BOX(format_hb), button, FALSE, FALSE, 10);
112         gtk_widget_show(button);
113
114         /* Output destination */
115         dest_lb = gtk_label_new("Print to:");
116   gtk_misc_set_alignment(GTK_MISC(dest_lb), 1.0, 0.5);
117   gtk_table_attach_defaults(GTK_TABLE(main_tb), dest_lb, 0, 1, 1, 2);
118         gtk_widget_show(dest_lb);
119
120         dest_hb = gtk_hbox_new(FALSE, 0);
121   gtk_table_attach_defaults(GTK_TABLE(main_tb), dest_hb, 1, 2, 1, 2);
122         gtk_widget_show(dest_hb);
123
124         button = gtk_radio_button_new_with_label(NULL, "Command");
125         if (prefs.pr_dest == PR_DEST_CMD) {
126                 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
127         }
128         dest_grp = gtk_radio_button_group(GTK_RADIO_BUTTON(button));
129         gtk_box_pack_start(GTK_BOX(dest_hb), button, FALSE, FALSE, 10);
130         gtk_widget_show(button);
131
132         button = gtk_radio_button_new_with_label(dest_grp, "File");
133         if (prefs.pr_dest == PR_DEST_FILE) {
134                 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), TRUE);
135         }
136         gtk_signal_connect(GTK_OBJECT(button), "toggled",
137                         GTK_SIGNAL_FUNC(printer_opts_toggle_dest), NULL);
138         gtk_box_pack_start(GTK_BOX(dest_hb), button, FALSE, FALSE, 10);
139         gtk_widget_show(button);
140
141         /* Command text entry */
142         cmd_lb = gtk_label_new("Command:");
143   gtk_misc_set_alignment(GTK_MISC(cmd_lb), 1.0, 0.5);
144   gtk_table_attach_defaults(GTK_TABLE(main_tb), cmd_lb, 0, 1, 2, 3);
145         gtk_widget_show(cmd_lb);
146
147         cmd_te = gtk_entry_new();
148         gtk_object_set_data(GTK_OBJECT(main_vb), PRINT_CMD_TE_KEY, cmd_te);
149         if (prefs.pr_cmd) gtk_entry_set_text(GTK_ENTRY(cmd_te), prefs.pr_cmd);
150   gtk_table_attach_defaults(GTK_TABLE(main_tb), cmd_te, 1, 2, 2, 3);
151         gtk_widget_show(cmd_te);
152
153         /* File button and text entry */
154         file_bt_hb = gtk_hbox_new(FALSE, 0);
155   gtk_table_attach_defaults(GTK_TABLE(main_tb), file_bt_hb, 0, 1, 3, 4);
156         gtk_widget_show(file_bt_hb);
157
158         file_bt = gtk_button_new_with_label("File:");
159         gtk_box_pack_end(GTK_BOX(file_bt_hb), file_bt, FALSE, FALSE, 0);
160         gtk_widget_show(file_bt);
161
162         file_te = gtk_entry_new();
163         gtk_object_set_data(GTK_OBJECT(main_vb), PRINT_FILE_TE_KEY, file_te);
164         if (prefs.pr_file) gtk_entry_set_text(GTK_ENTRY(file_te), prefs.pr_file);
165   gtk_table_attach_defaults(GTK_TABLE(main_tb), file_te, 1, 2, 3, 4);
166         gtk_widget_show(file_te);
167
168         gtk_signal_connect(GTK_OBJECT(file_bt), "clicked",
169                         GTK_SIGNAL_FUNC(printer_opts_file_cb), GTK_OBJECT(file_te));
170
171         gtk_widget_show(main_vb);
172         return(main_vb);
173 }
174
175
176 static void
177 printer_opts_file_cb(GtkWidget *file_bt, gpointer file_te) {
178   GtkWidget *fs;
179
180   fs = gtk_file_selection_new ("Ethereal: Print to a File");
181         gtk_object_set_data(GTK_OBJECT(fs), PRINT_FILE_TE_KEY, file_te);
182
183   gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(fs)->ok_button),
184     "clicked", (GtkSignalFunc) printer_opts_fs_ok_cb, fs);
185
186   /* Connect the cancel_button to destroy the widget */
187   gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(fs)->cancel_button),
188     "clicked", (GtkSignalFunc) printer_opts_fs_cancel_cb, fs);
189
190   gtk_widget_show(fs);
191 }
192
193 static void
194 printer_opts_fs_ok_cb(GtkWidget *w, gpointer data) {
195           
196         gtk_entry_set_text(GTK_ENTRY(gtk_object_get_data(GTK_OBJECT(data),
197         PRINT_FILE_TE_KEY)),
198                 gtk_file_selection_get_filename (GTK_FILE_SELECTION(data)));
199         printer_opts_fs_cancel_cb(w, data);
200 }
201
202 static void
203 printer_opts_fs_cancel_cb(GtkWidget *w, gpointer data) {
204           
205         gtk_widget_destroy(GTK_WIDGET(data));
206
207
208 void
209 printer_prefs_ok(GtkWidget *w)
210 {
211         if(prefs.pr_cmd) g_free(prefs.pr_cmd);
212         prefs.pr_cmd =  
213                 g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_object_get_data(GTK_OBJECT(w),
214     PRINT_CMD_TE_KEY))));
215
216         if(prefs.pr_file) g_free(prefs.pr_file);
217         prefs.pr_file =  
218                 g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_object_get_data(GTK_OBJECT(w),
219     PRINT_FILE_TE_KEY))));
220 }
221
222 void
223 printer_prefs_save(GtkWidget *w)
224 {
225         printer_prefs_ok(w);
226 }
227
228 void
229 printer_prefs_cancel(GtkWidget *w)
230 {
231 }
232
233 static void
234 printer_opts_toggle_format(GtkWidget *widget, gpointer data)
235 {
236                 if (GTK_TOGGLE_BUTTON (widget)->active) {
237                         prefs.pr_format = PR_FMT_PS;
238                         /* toggle file/cmd */
239                 }
240                 else {
241                         prefs.pr_format = PR_FMT_TEXT;
242                         /* toggle file/cmd */
243                 }
244 }
245
246 static void
247 printer_opts_toggle_dest(GtkWidget *widget, gpointer data)
248 {
249                 if (GTK_TOGGLE_BUTTON (widget)->active) {
250                         prefs.pr_dest = PR_DEST_FILE;
251                 }
252                 else {
253                         prefs.pr_dest = PR_DEST_CMD;
254                 }
255 }
256
257 /* ========================================================== */
258
259 typedef struct {
260         int             level;
261         FILE            *fh;
262         const guint8    *pd;
263 } print_data;
264
265 void proto_tree_print(GNode *protocol_tree, const u_char *pd, frame_data *fd)
266 {
267         FILE    *fh;
268         char    *out;
269         print_data data;
270
271         /* Open the file or command for output */
272         if (prefs.pr_dest == PR_DEST_CMD) {
273                 out = prefs.pr_cmd;
274                 fh = popen(prefs.pr_cmd, "w");
275         }
276         else {
277                 out = prefs.pr_file;
278                 fh = fopen(prefs.pr_file, "w");
279         }
280
281         if (!fh) {
282                 g_error("Cannot open %s for output.\n", out);
283                 return;
284         }
285
286         /* Create the output */
287         data.level = 0;
288         data.fh = fh;
289         data.pd = pd;
290         if (prefs.pr_format == PR_FMT_TEXT) {
291                 g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
292                         proto_tree_print_node_text, &data);
293         } else {
294                 print_ps_preamble(fh);
295                 g_node_children_foreach((GNode*) protocol_tree, G_TRAVERSE_ALL,
296                         proto_tree_print_node_ps, &data);
297                 print_ps_finale(fh);
298         }
299
300         /* Close the file or command */
301         if (prefs.pr_dest == PR_DEST_CMD) {
302                 pclose(fh);
303         }
304         else {
305                 fclose(fh);
306         }
307 }
308
309 /* Print a tree's data, and any child nodes, in plain text */
310 static
311 void proto_tree_print_node_text(GNode *node, gpointer data)
312 {
313         field_info      *fi = (field_info*) (node->data);
314         print_data      *pdata = (print_data*) data;
315         int             i;
316         int              num_spaces;
317         char            space[41];
318         gchar           label_str[ITEM_LABEL_LENGTH];
319         gchar           *label_ptr;
320
321         if (!fi->visible)
322                 return;
323
324         /* was a free format label produced? */
325         if (fi->representation) {
326                 label_ptr = fi->representation;
327         }
328         else { /* no, make a generic label */
329                 label_ptr = label_str;
330                 proto_item_fill_label(fi, label_str);
331         }
332                 
333         /* Prepare the tabs for printing, depending on tree level */
334         num_spaces = pdata->level * 4;
335         if (num_spaces > 40) {
336                 num_spaces = 40;
337         }
338         for (i = 0; i < num_spaces; i++) {
339                 space[i] = ' ';
340         }
341         /* The string is NUL-terminated */
342         space[num_spaces] = 0;
343
344         /* Print the text */
345         fprintf(pdata->fh, "%s%s\n", space, label_ptr);
346
347         /* If it's uninterpreted data, dump it. */
348         if (fi->hfinfo->id == proto_data)
349                 dumpit(pdata->fh, &pdata->pd[fi->start], fi->length);
350
351         /* Recurse into the subtree, if it exists */
352         if (g_node_n_children(node) > 0) {
353                 pdata->level++;
354                 g_node_children_foreach(node, G_TRAVERSE_ALL,
355                         proto_tree_print_node_text, pdata);
356                 pdata->level--;
357         }
358 }
359
360 /* This routine was created by Dan Lasley <DLASLEY@PROMUS.com>, and
361 only slightly modified for ethereal by Gilbert Ramirez. */
362 static
363 void dumpit (FILE *fh, register const u_char *cp, register u_int length)
364 {
365         register int ad, i, j, k;
366         u_char c;
367         u_char line[60];
368                 static u_char binhex[16] = {
369                         '0', '1', '2', '3', '4', '5', '6', '7',
370                         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
371
372         memset (line, ' ', sizeof line);
373         line[sizeof (line)-1] = 0;
374         for (ad=i=j=k=0; i<length; i++) {
375                 c = *cp++;
376                 line[j++] = binhex[c>>4];
377                 line[j++] = binhex[c&0xf];
378                 if (i&1) j++;
379                 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
380                 if ((i & 15) == 15) {
381                         fprintf (fh, "\n%4x  %s", ad, line);
382                         /*if (i==15) printf (" %d", length);*/
383                         memset (line, ' ', sizeof line);
384                         line[sizeof (line)-1] = j = k = 0;
385                         ad += 16;
386                 }
387         }
388
389         if (line[0] != ' ') fprintf (fh, "\n%4x  %s", ad, line);
390         fprintf(fh, "\n");
391         return;
392
393 }
394
395 #define MAX_LINE_LENGTH 256
396
397 /* Print a node's data, and any child nodes, in PostScript */
398 static
399 void proto_tree_print_node_ps(GNode *node, gpointer data)
400 {
401         field_info      *fi = (field_info*) (node->data);
402         print_data      *pdata = (print_data*) data;
403         gchar           label_str[ITEM_LABEL_LENGTH];
404         gchar           *label_ptr;
405         char            psbuffer[MAX_LINE_LENGTH]; /* static sized buffer! */
406
407         if (!fi->visible)
408                 return;
409
410         /* was a free format label produced? */
411         if (fi->representation) {
412                 label_ptr = fi->representation;
413         }
414         else { /* no, make a generic label */
415                 label_ptr = label_str;
416                 proto_item_fill_label(fi, label_str);
417         }
418                 
419         /* Print the text */
420         ps_clean_string(psbuffer, label_ptr, MAX_LINE_LENGTH);
421         fprintf(pdata->fh, "%d (%s) putline\n", pdata->level, psbuffer);
422
423         /* If it's uninterpreted data, dump it. */
424         if (fi->hfinfo->id == proto_data) {
425                 print_ps_hex(pdata->fh);
426                 dumpit_ps(pdata->fh, &pdata->pd[fi->start], fi->length);
427         }
428
429         /* Recurse into the subtree, if it exists */
430         if (g_node_n_children(node) > 0) {
431                 pdata->level++;
432                 g_node_children_foreach(node, G_TRAVERSE_ALL,
433                         proto_tree_print_node_ps, pdata);
434                 pdata->level--;
435         }
436 }
437
438 static
439 void ps_clean_string(unsigned char *out, const unsigned char *in,
440                         int outbuf_size)
441 {
442         int rd, wr;
443         char c;
444
445         for (rd = 0, wr = 0 ; wr < outbuf_size; rd++, wr++ ) {
446                 c = in[rd];
447                 switch (c) {
448                         case '(':
449                         case ')':
450                         case '\\':
451                                 out[wr] = '\\';
452                                 out[++wr] = c;
453                                 break;
454
455                         default:
456                                 out[wr] = c;
457                                 break;
458                 }
459
460                 if (c == 0) {
461                         break;
462                 }
463         }
464 }
465
466 static
467 void dumpit_ps (FILE *fh, register const u_char *cp, register u_int length)
468 {
469         register int ad, i, j, k;
470         u_char c;
471         u_char line[60];
472                 static u_char binhex[16] = {
473                         '0', '1', '2', '3', '4', '5', '6', '7',
474                         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
475                 u_char psline[MAX_LINE_LENGTH];
476
477         memset (line, ' ', sizeof line);
478         line[sizeof (line)-1] = 0;
479         for (ad=i=j=k=0; i<length; i++) {
480                 c = *cp++;
481                 line[j++] = binhex[c>>4];
482                 line[j++] = binhex[c&0xf];
483                 if (i&1) j++;
484                 line[42+k++] = c >= ' ' && c < 0x7f ? c : '.';
485                 if ((i & 15) == 15) {
486                                                 ps_clean_string(psline, line, MAX_LINE_LENGTH);
487                         fprintf (fh, "(%4x  %s) hexdump\n", ad, psline);
488                         memset (line, ' ', sizeof line);
489                         line[sizeof (line)-1] = j = k = 0;
490                         ad += 16;
491                 }
492         }
493
494         if (line[0] != ' ') {
495                         ps_clean_string(psline, line, MAX_LINE_LENGTH);
496                         fprintf (fh, "(%4x  %s) hexdump\n", ad, psline);
497                 }
498         return;
499
500 }