Fix more "no previous declaration" warnings
[obnox/wireshark/wip.git] / gtk / main.c
1 /* main.c
2  *
3  * $Id$
4  *
5  * Ethereal - Network traffic analyzer
6  * By Gerald Combs <gerald@ethereal.com>
7  * Copyright 1998 Gerald Combs
8  *
9  * Richard Sharpe, 13-Feb-1999, added support for initializing structures
10  *                              needed by dissect routines
11  * Jeff Foster,    2001/03/12,  added support tabbed hex display windowss
12  *
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <gtk/gtk.h>
34
35 #include <string.h>
36 #include <ctype.h>
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41
42 #ifdef HAVE_IO_H
43 #include <io.h> /* open/close on win32 */
44 #endif
45
46 #ifdef NEED_STRERROR_H
47 #include "strerror.h"
48 #endif
49
50 #ifdef NEED_GETOPT_H
51 #include "getopt.h"
52 #endif
53
54 #ifdef _WIN32 /* Needed for console I/O */
55 #include <fcntl.h>
56 #include <conio.h>
57 #endif
58
59 #include <epan/epan.h>
60 #include <epan/filesystem.h>
61 #include <epan/epan_dissect.h>
62 #include <epan/timestamp.h>
63 #include <epan/packet.h>
64 #include <epan/plugins.h>
65 #include <epan/dfilter/dfilter.h>
66 #include <epan/strutil.h>
67 #include <epan/addr_resolv.h>
68
69 /* general (not GTK specific) */
70 #include "svnversion.h"
71 #include "file.h"
72 #include "summary.h"
73 #include "filters.h"
74 #include "disabled_protos.h"
75 #include <epan/prefs.h>
76 #include "filter_dlg.h"
77 #include "layout_prefs.h"
78 #include "color.h"
79 #include "color_filters.h"
80 #include "print.h"
81 #include "simple_dialog.h"
82 #include "register.h"
83 #include <epan/prefs-int.h>
84 #include "ringbuffer.h"
85 #include "../ui_util.h"     /* beware: ui_util.h exists twice! */
86 #include <epan/tap.h>
87 #include "util.h"
88 #include "clopts_common.h"
89 #include "version_info.h"
90 #include "merge.h"
91
92 #ifdef HAVE_LIBPCAP
93 #include <pcap.h>
94 #include "pcap-util.h"
95 #include "capture.h"
96 #endif
97
98 #ifdef _WIN32
99 #include "capture-wpcap.h"
100 #include "capture_wpcap_packet.h"
101 #endif
102
103 #if GTK_MAJOR_VERSION < 2 && GTK_MINOR_VERSION < 3
104 #include "ethclist.h"
105 #endif
106
107 /* GTK related */
108 #include "statusbar.h"
109 #include "alert_box.h"
110 #include "dlg_utils.h"
111 #include "gtkglobals.h"
112 #include "colors.h"
113 #include "ui_util.h"        /* beware: ui_util.h exists twice! */
114 #include "compat_macros.h"
115
116 #include "main.h"
117 #include "menu.h"
118 #include "../main_window.h"
119 #include "../menu.h"
120 #include "file_dlg.h"
121 #include <epan/column.h>
122 #include "proto_draw.h"
123 #include "keys.h"
124 #include "packet_win.h"
125 #include "toolbar.h"
126 #include "find_dlg.h"
127 #include "packet_list.h"
128 #include "recent.h"
129 #include "follow_dlg.h"
130 #include "font_utils.h"
131 #include "about_dlg.h"
132 #include "help_dlg.h"
133 #include "decode_as_dlg.h"
134 #include "webbrowser.h"
135 #include "capture_dlg.h"
136 #if 0
137 #include "../image/eicon3d64.xpm"
138 #endif
139 #include "capture_ui_utils.h"
140 #include "log.h"
141 #include "../epan/emem.h"
142
143
144
145 /*
146  * Files under personal and global preferences directories in which
147  * GTK settings for Ethereal are stored.
148  */
149 #define RC_FILE "gtkrc"
150
151 #ifdef HAVE_LIBPCAP
152 #define DEF_READY_MESSAGE " Ready to load or capture"
153 #else
154 #define DEF_READY_MESSAGE " Ready to load file"
155 #endif
156
157 capture_file cfile;
158 GtkWidget   *main_display_filter_widget=NULL;
159 GtkWidget   *top_level = NULL, *tree_view, *byte_nb_ptr, *tv_scrollw;
160 static GtkWidget   *main_pane_v1, *main_pane_v2, *main_pane_h1, *main_pane_h2;
161 static GtkWidget   *main_first_pane, *main_second_pane;
162 static GtkWidget   *status_pane;
163 static GtkWidget   *menubar, *main_vbox, *main_tb, *pkt_scrollw, *stat_hbox, *filter_tb;
164 static GtkWidget        *info_bar;
165 static GtkWidget    *packets_bar = NULL;
166 static GtkWidget    *welcome_pane;
167 static guint    main_ctx, file_ctx, help_ctx;
168 static guint        packets_ctx;
169 static gchar        *packets_str = NULL;
170 GString *comp_info_str, *runtime_info_str;
171 gchar       *ethereal_path = NULL;
172 gboolean have_capture_file = FALSE; /* XXX - is there an aquivalent in cfile? */
173
174 #ifdef _WIN32
175 static gboolean has_console;    /* TRUE if app has console */
176 static void destroy_console(void);
177 #endif
178 static void console_log_handler(const char *log_domain,
179     GLogLevelFlags log_level, const char *message, gpointer user_data);
180
181 #ifdef HAVE_LIBPCAP
182 static gboolean list_link_layer_types;
183 capture_options global_capture_opts;
184 capture_options *capture_opts = &global_capture_opts;
185 #endif
186
187
188 static void create_main_window(gint, gint, gint, e_prefs*);
189 static void show_main_window(gboolean);
190 static void file_quit_answered_cb(gpointer dialog, gint btn, gpointer data);
191 static void main_save_window_geometry(GtkWidget *widget);
192
193 #define E_DFILTER_CM_KEY          "display_filter_combo"
194 #define E_DFILTER_FL_KEY          "display_filter_list"
195
196
197
198 /* Match selected byte pattern */
199 static void
200 match_selected_cb_do(gpointer data, int action, gchar *text)
201 {
202     GtkWidget           *filter_te;
203     char                *cur_filter, *new_filter;
204
205     if (!text)
206         return;
207     g_assert(data);
208     filter_te = OBJECT_GET_DATA(data, E_DFILTER_TE_KEY);
209     g_assert(filter_te);
210
211     cur_filter = gtk_editable_get_chars(GTK_EDITABLE(filter_te), 0, -1);
212
213     switch (action&MATCH_SELECTED_MASK) {
214
215     case MATCH_SELECTED_REPLACE:
216         new_filter = g_strdup(text);
217         break;
218
219     case MATCH_SELECTED_AND:
220         if ((!cur_filter) || (0 == strlen(cur_filter)))
221             new_filter = g_strdup(text);
222         else
223             new_filter = g_strconcat("(", cur_filter, ") && (", text, ")", NULL);
224         break;
225
226     case MATCH_SELECTED_OR:
227         if ((!cur_filter) || (0 == strlen(cur_filter)))
228             new_filter = g_strdup(text);
229         else
230             new_filter = g_strconcat("(", cur_filter, ") || (", text, ")", NULL);
231         break;
232
233     case MATCH_SELECTED_NOT:
234         new_filter = g_strconcat("!(", text, ")", NULL);
235         break;
236
237     case MATCH_SELECTED_AND_NOT:
238         if ((!cur_filter) || (0 == strlen(cur_filter)))
239             new_filter = g_strconcat("!(", text, ")", NULL);
240         else
241             new_filter = g_strconcat("(", cur_filter, ") && !(", text, ")", NULL);
242         break;
243
244     case MATCH_SELECTED_OR_NOT:
245         if ((!cur_filter) || (0 == strlen(cur_filter)))
246             new_filter = g_strconcat("!(", text, ")", NULL);
247         else
248             new_filter = g_strconcat("(", cur_filter, ") || !(", text, ")", NULL);
249         break;
250
251     default:
252         g_assert_not_reached();
253         new_filter = NULL;
254         break;
255     }
256
257     /* Free up the copy we got of the old filter text. */
258     g_free(cur_filter);
259
260     /* create a new one and set the display filter entry accordingly */
261     gtk_entry_set_text(GTK_ENTRY(filter_te), new_filter);
262
263     /* Run the display filter so it goes in effect. */
264     if (action&MATCH_SELECTED_APPLY_NOW)
265         main_filter_packets(&cfile, new_filter, FALSE);
266
267     /* Free up the new filter text. */
268     g_free(new_filter);
269
270     /* Free up the generated text we were handed. */
271     g_free(text);
272 }
273
274 void
275 match_selected_ptree_cb(GtkWidget *w, gpointer data, MATCH_SELECTED_E action)
276 {
277     if (cfile.finfo_selected)
278         match_selected_cb_do((data ? data : w),
279             action,
280             proto_construct_dfilter_string(cfile.finfo_selected, cfile.edt));
281 }
282
283
284 static void selected_ptree_info_answered_cb(gpointer dialog _U_, gint btn, gpointer data)
285 {
286     gchar *selected_proto_url;
287     gchar *proto_abbrev = data;
288
289
290     switch(btn) {
291     case(ESD_BTN_OK):
292         if (cfile.finfo_selected) {
293             /* open wiki page using the protocol abbreviation */
294             selected_proto_url = g_strdup_printf("http://wiki.ethereal.com/Protocols/%s", proto_abbrev);
295             browser_open_url(selected_proto_url);
296             g_free(selected_proto_url);
297         }
298         break;
299     case(ESD_BTN_CANCEL):
300         break;
301     default:
302         g_assert_not_reached();
303     }
304 }
305
306
307 void 
308 selected_ptree_info_cb(GtkWidget *widget _U_, gpointer data _U_)
309 {
310     int field_id;
311     const gchar *proto_abbrev;
312     gpointer  dialog;
313
314
315     if (cfile.finfo_selected) {
316         /* convert selected field to protocol abbreviation */
317         /* XXX - could this conversion be simplified? */
318         field_id = cfile.finfo_selected->hfinfo->id;
319         /* if the selected field isn't a protocol, get it's parent */
320         if(!proto_registrar_is_protocol(field_id)) {
321             field_id = proto_registrar_get_parent(cfile.finfo_selected->hfinfo->id);
322         }
323
324         proto_abbrev = proto_registrar_get_abbrev(field_id);
325
326         /* ask the user if the wiki page really should be opened */
327         dialog = simple_dialog(ESD_TYPE_CONFIRMATION, ESD_BTNS_OK_CANCEL,
328                     PRIMARY_TEXT_START "Open Ethereal Wiki page of protocol \"%s\"?" PRIMARY_TEXT_END "\n"
329                     "\n"
330                     "This will open the \"%s\" related Ethereal Wiki page in your Web browser.\n"
331                     "\n"
332                     "The Ethereal Wiki is a collaborative approach to provide information\n"
333                     "about Ethereal in several ways (not limited to protocol specifics).\n"
334                     "\n"
335                     "This Wiki is new, so the page of the selected protocol\n"
336                     "may not exist and/or may not contain valuable information.\n"
337                     "\n"
338                     "As everyone can edit the Wiki and add new content (or extend existing),\n"
339                     "you are encouraged to add information if you can.\n"
340                     "\n"
341                     "Hint 1: If you are new to wiki editing, try out editing the Sandbox first!\n"
342                     "\n"
343                     "Hint 2: If you want to add a new protocol page, you should use the ProtocolTemplate,\n"
344                     "which will save you a lot of editing and will give a consistent look over the pages.",
345                     proto_abbrev, proto_abbrev);
346         simple_dialog_set_cb(dialog, selected_ptree_info_answered_cb, (gpointer) proto_abbrev);
347     }
348 }
349
350
351 void 
352 selected_ptree_ref_cb(GtkWidget *widget _U_, gpointer data _U_)
353 {
354     int field_id;
355     const gchar *proto_abbrev;
356     gchar *selected_proto_url;
357
358
359     if (cfile.finfo_selected) {
360         /* convert selected field to protocol abbreviation */
361         /* XXX - could this conversion be simplified? */
362         field_id = cfile.finfo_selected->hfinfo->id;
363         /* if the selected field isn't a protocol, get it's parent */
364         if(!proto_registrar_is_protocol(field_id)) {
365             field_id = proto_registrar_get_parent(cfile.finfo_selected->hfinfo->id);
366         }
367
368         proto_abbrev = proto_registrar_get_abbrev(field_id);
369
370         /* open reference page using the protocol abbreviation */
371         selected_proto_url = g_strdup_printf("http://www.ethereal.com/docs/dfref/%c/%s", proto_abbrev[0], proto_abbrev);
372         browser_open_url(selected_proto_url);
373         g_free(selected_proto_url);
374     }
375 }
376
377
378 static gchar *
379 get_text_from_packet_list(gpointer data)
380 {
381     gint        row = GPOINTER_TO_INT(OBJECT_GET_DATA(data, E_MPACKET_LIST_ROW_KEY));
382     gint        column = GPOINTER_TO_INT(OBJECT_GET_DATA(data, E_MPACKET_LIST_COL_KEY));
383     frame_data *fdata = (frame_data *)packet_list_get_row_data(row);
384     epan_dissect_t *edt;
385     gchar      *buf=NULL;
386     int         len;
387     int         err;
388     gchar       *err_info;
389
390     if (fdata != NULL) {
391         if (!wtap_seek_read(cfile.wth, fdata->file_off, &cfile.pseudo_header,
392                        cfile.pd, fdata->cap_len, &err, &err_info)) {
393             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
394                           cf_read_error_message(err, err_info), cfile.filename);
395             return NULL;
396         }
397
398         edt = epan_dissect_new(FALSE, FALSE);
399         epan_dissect_run(edt, &cfile.pseudo_header, cfile.pd, fdata,
400                          &cfile.cinfo);
401         epan_dissect_fill_in_columns(edt);
402
403         if (strlen(cfile.cinfo.col_expr[column]) != 0 &&
404             strlen(cfile.cinfo.col_expr_val[column]) != 0) {
405             len = strlen(cfile.cinfo.col_expr[column]) +
406                   strlen(cfile.cinfo.col_expr_val[column]) + 5;
407             buf = g_malloc0(len);
408             g_snprintf(buf, len, "%s == %s", cfile.cinfo.col_expr[column],
409                      cfile.cinfo.col_expr_val[column]);
410         }
411
412         epan_dissect_free(edt);
413     }
414
415     return buf;
416 }
417
418 void
419 match_selected_plist_cb(GtkWidget *w _U_, gpointer data, MATCH_SELECTED_E action)
420 {
421     match_selected_cb_do(data,
422         action,
423         get_text_from_packet_list(data));
424 }
425
426
427
428 /* XXX: use a preference for this setting! */
429 static guint dfilter_combo_max_recent = 10;
430
431 /* add a display filter to the combo box */
432 /* Note: a new filter string will replace an old identical one */
433 static gboolean
434 dfilter_combo_add(GtkWidget *filter_cm, char *s) {
435   GList     *li;
436   GList     *dfilter_list = OBJECT_GET_DATA(filter_cm, E_DFILTER_FL_KEY);
437
438
439   /* GtkCombos don't let us get at their list contents easily, so we maintain
440      our own filter list, and feed it to gtk_combo_set_popdown_strings when
441      a new filter is added. */
442     li = g_list_first(dfilter_list);
443     while (li) {
444         /* If the filter is already in the list, remove the old one and 
445                  * append the new one at the latest position (at g_list_append() below) */
446                 if (li->data && strcmp(s, li->data) == 0) {
447           dfilter_list = g_list_remove(dfilter_list, li->data);
448                   break;
449                 }
450       li = li->next;
451     }
452
453     dfilter_list = g_list_append(dfilter_list, s);
454     OBJECT_SET_DATA(filter_cm, E_DFILTER_FL_KEY, dfilter_list);
455     gtk_combo_set_popdown_strings(GTK_COMBO(filter_cm), dfilter_list);
456     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(filter_cm)->entry), g_list_last(dfilter_list)->data);
457
458     return TRUE;
459 }
460
461
462 /* write all non empty display filters (until maximum count) 
463  * of the combo box GList to the user's recent file */
464 void
465 dfilter_recent_combo_write_all(FILE *rf) {
466   GtkWidget *filter_cm = OBJECT_GET_DATA(top_level, E_DFILTER_CM_KEY);
467   GList     *dfilter_list = OBJECT_GET_DATA(filter_cm, E_DFILTER_FL_KEY);
468   GList     *li;
469   guint      max_count = 0;
470
471
472   /* write all non empty display filter strings to the recent file (until max count) */
473   li = g_list_first(dfilter_list);
474   while ( li && (max_count++ <= dfilter_combo_max_recent) ) {
475     if (strlen(li->data)) {
476       fprintf (rf, RECENT_KEY_DISPLAY_FILTER ": %s\n", (char *)li->data);
477     }
478     li = li->next;
479   }
480 }
481
482 /* empty the combobox entry field */
483 void
484 dfilter_combo_add_empty(void) {
485   GtkWidget *filter_cm = OBJECT_GET_DATA(top_level, E_DFILTER_CM_KEY);
486
487   gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(filter_cm)->entry), "");
488 }
489
490
491 /* add a display filter coming from the user's recent file to the dfilter combo box */
492 gboolean
493 dfilter_combo_add_recent(gchar *s) {
494   GtkWidget *filter_cm = OBJECT_GET_DATA(top_level, E_DFILTER_CM_KEY);
495   char      *dup;
496
497   dup = g_strdup(s);
498   if (!dfilter_combo_add(filter_cm, dup)) {
499     g_free(dup);
500     return FALSE;
501   }
502
503   return TRUE;
504 }
505
506
507 /* call cf_filter_packets() and add this filter string to the recent filter list */
508 gboolean
509 main_filter_packets(capture_file *cf, const gchar *dftext, gboolean force)
510 {
511   GtkCombo  *filter_cm = OBJECT_GET_DATA(top_level, E_DFILTER_CM_KEY);
512   GList     *dfilter_list = OBJECT_GET_DATA(filter_cm, E_DFILTER_FL_KEY);
513   GList     *li;
514   gboolean   add_filter = TRUE;
515   gboolean   free_filter = TRUE;
516   char      *s;
517   cf_status_t cf_status;
518
519   /* we'll crash later on if dftext is NULL */
520   g_assert(dftext != NULL);
521   
522   s = g_strdup(dftext);
523
524   /* GtkCombos don't let us get at their list contents easily, so we maintain
525      our own filter list, and feed it to gtk_combo_set_popdown_strings when
526      a new filter is added. */
527   cf_status = cf_filter_packets(cf, s, force);
528   if (cf_status == CF_OK) {
529     li = g_list_first(dfilter_list);
530     while (li) {
531       if (li->data && strcmp(s, li->data) == 0)
532         add_filter = FALSE;
533       li = li->next;
534     }
535
536     if (add_filter) {
537       /* trim list size first */
538       while (g_list_length(dfilter_list) >= dfilter_combo_max_recent) {
539         dfilter_list = g_list_remove(dfilter_list, g_list_first(dfilter_list)->data);
540       }
541
542       free_filter = FALSE;
543       dfilter_list = g_list_append(dfilter_list, s);
544       OBJECT_SET_DATA(filter_cm, E_DFILTER_FL_KEY, dfilter_list);
545       gtk_combo_set_popdown_strings(filter_cm, dfilter_list);
546       gtk_entry_set_text(GTK_ENTRY(filter_cm->entry), g_list_last(dfilter_list)->data);
547     }
548   }
549   if (free_filter)
550     g_free(s);
551
552   return (cf_status == CF_OK);
553 }
554
555
556 /* Run the current display filter on the current packet set, and
557    redisplay. */
558 static void
559 filter_activate_cb(GtkWidget *w _U_, gpointer data)
560 {
561   const char *s;
562
563   s = gtk_entry_get_text(GTK_ENTRY(data));
564
565   main_filter_packets(&cfile, s, FALSE);
566 }
567
568 /* redisplay with no display filter */
569 static void
570 filter_reset_cb(GtkWidget *w, gpointer data _U_)
571 {
572   GtkWidget *filter_te = NULL;
573
574   if ((filter_te = OBJECT_GET_DATA(w, E_DFILTER_TE_KEY))) {
575     gtk_entry_set_text(GTK_ENTRY(filter_te), "");
576   }
577   main_filter_packets(&cfile, "", FALSE);
578 }
579
580 /* mark as reference time frame */
581 static void
582 set_frame_reftime(gboolean set, frame_data *frame, gint row) {
583   if (row == -1)
584     return;
585   if (set) {
586     frame->flags.ref_time=1;
587   } else {
588     frame->flags.ref_time=0;
589   }
590   cf_reftime_packets(&cfile);
591 }
592
593 void 
594 reftime_frame_cb(GtkWidget *w _U_, gpointer data _U_, REFTIME_ACTION_E action)
595 {
596
597   switch(action){
598   case REFTIME_TOGGLE:
599     if (cfile.current_frame) {
600       /* XXX hum, should better have a "cfile->current_row" here ... */
601       set_frame_reftime(!cfile.current_frame->flags.ref_time,
602                      cfile.current_frame,
603                      packet_list_find_row_from_data(cfile.current_frame));
604     }
605     break;
606   case REFTIME_FIND_NEXT:
607     find_previous_next_frame_with_filter("frame.ref_time", FALSE);
608     break;
609   case REFTIME_FIND_PREV:
610     find_previous_next_frame_with_filter("frame.ref_time", TRUE);
611     break;
612   }
613 }
614
615 #if GTK_MAJOR_VERSION < 2
616 static void
617 tree_view_select_row_cb(GtkCTree *ctree, GList *node, gint column _U_,
618                         gpointer user_data _U_)
619 #else
620 static void
621 tree_view_selection_changed_cb(GtkTreeSelection *sel, gpointer user_data _U_)
622 #endif
623 {
624     field_info   *finfo;
625     gchar        *help_str = NULL;
626     gchar         len_str[2+10+1+5+1]; /* ", {N} bytes\0",
627                                           N < 4294967296 */
628     gboolean      has_blurb = FALSE;
629     guint         length = 0, byte_len;
630     GtkWidget    *byte_view;
631     const guint8 *byte_data;
632 #if GTK_MAJOR_VERSION >= 2
633     GtkTreeModel *model;
634     GtkTreeIter   iter;
635 #endif
636
637 #if GTK_MAJOR_VERSION >= 2
638     /* if nothing is selected */
639     if (!gtk_tree_selection_get_selected(sel, &model, &iter))
640     {
641         /*
642          * Which byte view is displaying the current protocol tree
643          * row's data?
644          */
645         byte_view = get_notebook_bv_ptr(byte_nb_ptr);
646         if (byte_view == NULL)
647             return;     /* none */
648
649         byte_data = get_byte_view_data_and_length(byte_view, &byte_len);
650         if (byte_data == NULL)
651             return;     /* none */
652
653         cf_unselect_field(&cfile);
654         packet_hex_print(GTK_TEXT_VIEW(byte_view), byte_data,
655                          cfile.current_frame, NULL, byte_len);
656         return;
657     }
658     gtk_tree_model_get(model, &iter, 1, &finfo, -1);
659 #else
660     g_assert(node);
661     finfo = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
662 #endif
663     if (!finfo) return;
664
665     set_notebook_page(byte_nb_ptr, finfo->ds_tvb);
666
667     byte_view = get_notebook_bv_ptr(byte_nb_ptr);
668     byte_data = get_byte_view_data_and_length(byte_view, &byte_len);
669     g_assert(byte_data != NULL);
670
671     cfile.finfo_selected = finfo;
672     set_menus_for_selected_tree_row(&cfile);
673
674     if (finfo->hfinfo) {
675         if (finfo->hfinfo->blurb != NULL &&
676             finfo->hfinfo->blurb[0] != '\0') {
677             has_blurb = TRUE;
678             length = strlen(finfo->hfinfo->blurb);
679         } else {
680             length = strlen(finfo->hfinfo->name);
681         }
682         if (finfo->length == 0) {
683             len_str[0] = '\0';
684         } else if (finfo->length == 1) {
685             strcpy (len_str, ", 1 byte");
686         } else {
687             g_snprintf (len_str, sizeof len_str, ", %d bytes", finfo->length);
688         }
689         statusbar_pop_field_msg();      /* get rid of current help msg */
690         if (length) {
691             help_str = g_strdup_printf("%s (%s)%s",
692                     (has_blurb) ? finfo->hfinfo->blurb : finfo->hfinfo->name,
693                     finfo->hfinfo->abbrev, len_str);
694             statusbar_push_field_msg(help_str);
695             g_free(help_str);
696         } else {
697             /*
698              * Don't show anything if the field name is zero-length;
699              * the pseudo-field for "proto_tree_add_text()" is such
700              * a field, and we don't want "Text (text)" showing up
701              * on the status line if you've selected such a field.
702              *
703              * XXX - there are zero-length fields for which we *do*
704              * want to show the field name.
705              *
706              * XXX - perhaps the name and abbrev field should be null
707              * pointers rather than null strings for that pseudo-field,
708              * but we'd have to add checks for null pointers in some
709              * places if we did that.
710              *
711              * Or perhaps protocol tree items added with
712              * "proto_tree_add_text()" should have -1 as the field index,
713              * with no pseudo-field being used, but that might also
714              * require special checks for -1 to be added.
715              */
716             statusbar_push_field_msg("");
717         }
718     }
719
720 #if GTK_MAJOR_VERSION < 2
721     packet_hex_print(GTK_TEXT(byte_view), byte_data, cfile.current_frame,
722                      finfo, byte_len);
723 #else
724     packet_hex_print(GTK_TEXT_VIEW(byte_view), byte_data, cfile.current_frame,
725                      finfo, byte_len);
726 #endif
727 }
728
729 #if GTK_MAJOR_VERSION < 2
730 static void
731 tree_view_unselect_row_cb(GtkCTree *ctree _U_, GList *node _U_, gint column _U_,
732                           gpointer user_data _U_)
733 {
734         GtkWidget       *byte_view;
735         const guint8    *data;
736         guint           len;
737
738         /*
739          * Which byte view is displaying the current protocol tree
740          * row's data?
741          */
742         byte_view = get_notebook_bv_ptr(byte_nb_ptr);
743         if (byte_view == NULL)
744                 return; /* none */
745
746         data = get_byte_view_data_and_length(byte_view, &len);
747         if (data == NULL)
748                 return; /* none */
749
750         cf_unselect_field(&cfile);
751         packet_hex_print(GTK_TEXT(byte_view), data, cfile.current_frame,
752                 NULL, len);
753 }
754 #endif
755
756 void collapse_all_cb(GtkWidget *widget _U_, gpointer data _U_) {
757   if (cfile.edt->tree)
758     collapse_all_tree(cfile.edt->tree, tree_view);
759 }
760
761 void expand_all_cb(GtkWidget *widget _U_, gpointer data _U_) {
762   if (cfile.edt->tree)
763     expand_all_tree(cfile.edt->tree, tree_view);
764 }
765
766 void expand_tree_cb(GtkWidget *widget _U_, gpointer data _U_) {
767 #if GTK_MAJOR_VERSION < 2    
768   GtkCTreeNode *node;
769 #else
770   GtkTreePath  *path;
771 #endif
772
773 #if GTK_MAJOR_VERSION < 2
774   node = gtk_ctree_find_by_row_data(GTK_CTREE(tree_view), NULL, cfile.finfo_selected);
775   if(node) {
776     /* the mouse position is at an entry, expand that one */
777   gtk_ctree_expand_recursive(GTK_CTREE(tree_view), node);
778   }
779 #else
780   path = tree_find_by_field_info(GTK_TREE_VIEW(tree_view), cfile.finfo_selected);
781   if(path) {
782     /* the mouse position is at an entry, expand that one */
783   gtk_tree_view_expand_row(GTK_TREE_VIEW(tree_view), path, TRUE);
784   gtk_tree_path_free(path);
785   }
786 #endif
787 }
788
789 void resolve_name_cb(GtkWidget *widget _U_, gpointer data _U_) {
790   if (cfile.edt->tree) {
791     guint32 tmp = g_resolv_flags;
792     g_resolv_flags = RESOLV_ALL;
793     proto_tree_draw(cfile.edt->tree, tree_view);
794     g_resolv_flags = tmp;
795   }
796 }
797
798 /*
799  * Push a message referring to file access onto the statusbar.
800  */
801 void
802 statusbar_push_file_msg(gchar *msg)
803 {
804     /*g_warning("statusbar_push: %s", msg);*/
805         gtk_statusbar_push(GTK_STATUSBAR(info_bar), file_ctx, msg);
806 }
807
808 /*
809  * Pop a message referring to file access off the statusbar.
810  */
811 void
812 statusbar_pop_file_msg(void)
813 {
814     /*g_warning("statusbar_pop");*/
815         gtk_statusbar_pop(GTK_STATUSBAR(info_bar), file_ctx);
816 }
817
818 /*
819  * XXX - do we need multiple statusbar contexts?
820  */
821
822 /*
823  * Push a message referring to the currently-selected field onto the statusbar.
824  */
825 void
826 statusbar_push_field_msg(gchar *msg)
827 {
828         gtk_statusbar_push(GTK_STATUSBAR(info_bar), help_ctx, msg);
829 }
830
831 /*
832  * Pop a message referring to the currently-selected field off the statusbar.
833  */
834 void
835 statusbar_pop_field_msg(void)
836 {
837         gtk_statusbar_pop(GTK_STATUSBAR(info_bar), help_ctx);
838 }
839
840 /*
841  * update the packets statusbar to the current values
842  */
843 void packets_bar_update(void)
844 {
845
846     if(packets_bar) {
847         /* remove old status */
848         if(packets_str) {
849             g_free(packets_str);
850                 gtk_statusbar_pop(GTK_STATUSBAR(packets_bar), packets_ctx);
851         }
852
853         /* do we have any packets? */
854         if(cfile.count) {
855             if(cfile.drops_known) {
856                 packets_str = g_strdup_printf(" P: %u D: %u M: %u Drops: %u", 
857                     cfile.count, cfile.displayed_count, cfile.marked_count, cfile.drops);
858             } else {
859                 packets_str = g_strdup_printf(" P: %u D: %u M: %u", 
860                     cfile.count, cfile.displayed_count, cfile.marked_count);
861             }
862         } else {
863             packets_str = g_strdup(" No Packets");
864         }
865             gtk_statusbar_push(GTK_STATUSBAR(packets_bar), packets_ctx, packets_str);
866     }
867 }
868
869 void
870 main_set_for_capture_file(gboolean have_capture_file_in)
871 {
872     have_capture_file = have_capture_file_in;
873
874     main_widgets_show_or_hide();
875 }
876
877 gboolean
878 main_do_quit(void)
879 {
880         /* get the current geometry, before writing it to disk */
881         main_save_window_geometry(top_level);
882
883         /* write user's recent file to disk
884          * It is no problem to write this file, even if we do not quit */
885         write_recent();
886
887         /* XXX - should we check whether the capture file is an
888            unsaved temporary file for a live capture and, if so,
889            pop up a "do you want to exit without saving the capture
890            file?" dialog, and then just return, leaving said dialog
891            box to forcibly quit if the user clicks "OK"?
892
893            If so, note that this should be done in a subroutine that
894            returns TRUE if we do so, and FALSE otherwise, and if it
895            returns TRUE we should return TRUE without nuking anything.
896
897            Note that, if we do that, we might also want to check if
898            an "Update list of packets in real time" capture is in
899            progress and, if so, ask whether they want to terminate
900            the capture and discard it, and return TRUE, before nuking
901            any child capture, if they say they don't want to do so. */
902
903 #ifdef HAVE_LIBPCAP
904         /* Nuke any child capture in progress. */
905         capture_kill_child(capture_opts);
906 #endif
907
908         /* Are we in the middle of reading a capture? */
909         if (cfile.state == FILE_READ_IN_PROGRESS) {
910                 /* Yes, so we can't just close the file and quit, as
911                    that may yank the rug out from under the read in
912                    progress; instead, just set the state to
913                    "FILE_READ_ABORTED" and return - the code doing the read
914                    will check for that and, if it sees that, will clean
915                    up and quit. */
916                 cfile.state = FILE_READ_ABORTED;
917
918                 /* Say that the window should *not* be deleted;
919                    that'll be done by the code that cleans up. */
920                 return TRUE;
921         } else {
922                 /* Close any capture file we have open; on some OSes, you
923                    can't unlink a temporary capture file if you have it
924                    open.
925                    "cf_close()" will unlink it after closing it if
926                    it's a temporary file.
927
928                    We do this here, rather than after the main loop returns,
929                    as, after the main loop returns, the main window may have
930                    been destroyed (if this is called due to a "destroy"
931                    even on the main window rather than due to the user
932                    selecting a menu item), and there may be a crash
933                    or other problem when "cf_close()" tries to
934                    clean up stuff in the main window.
935
936                    XXX - is there a better place to put this?
937                    Or should we have a routine that *just* closes the
938                    capture file, and doesn't do anything with the UI,
939                    which we'd call here, and another routine that
940                    calls that routine and also cleans up the UI, which
941                    we'd call elsewhere? */
942                 cf_close(&cfile);
943
944                 /* Exit by leaving the main loop, so that any quit functions
945                    we registered get called. */
946                 gtk_main_quit();
947
948                 /* Say that the window should be deleted. */
949                 return FALSE;
950         }
951 }
952
953 static gboolean
954 main_window_delete_event_cb(GtkWidget *widget _U_, GdkEvent *event _U_, gpointer data _U_)
955 {
956   gpointer dialog;
957
958   if((cfile.state != FILE_CLOSED) && !cfile.user_saved && prefs.gui_ask_unsaved) {
959 #if GTK_MAJOR_VERSION >= 2
960     gtk_window_present(GTK_WINDOW(top_level));
961 #endif
962     /* user didn't saved his current file, ask him */
963     dialog = simple_dialog(ESD_TYPE_CONFIRMATION, ESD_BTNS_SAVE_DONTSAVE_CANCEL,
964                 PRIMARY_TEXT_START "Save capture file before program quit?" PRIMARY_TEXT_END "\n\n"
965                 "If you quit the program without saving, your capture data will be discarded.");
966     simple_dialog_set_cb(dialog, file_quit_answered_cb, NULL);
967     return TRUE;
968   } else {
969     /* unchanged file, just exit */
970     /* "main_do_quit()" indicates whether the main window should be deleted. */
971     return main_do_quit();
972   }
973 }
974
975
976
977 static void
978 main_load_window_geometry(GtkWidget *widget)
979 {
980     window_geometry_t geom;
981
982     geom.set_pos        = prefs.gui_geometry_save_position;
983     geom.x              = recent.gui_geometry_main_x;
984     geom.y              = recent.gui_geometry_main_y;
985     geom.set_size       = prefs.gui_geometry_save_size;
986     if (recent.gui_geometry_main_width > 0 &&
987         recent.gui_geometry_main_height > 0) {
988         geom.width          = recent.gui_geometry_main_width;
989         geom.height         = recent.gui_geometry_main_height;
990         geom.set_maximized  = prefs.gui_geometry_save_maximized;
991     } else {
992         /* We assume this means the width and height weren't set in
993            the "recent" file (or that there is no "recent" file),
994            and weren't set to a default value, so we don't set the
995            size.  (The "recent" file code rejects non-positive width
996            and height values.) */
997        geom.set_size = FALSE;
998     }
999     geom.maximized      = recent.gui_geometry_main_maximized;
1000
1001     window_set_geometry(widget, &geom);
1002
1003     if (recent.has_gui_geometry_main_upper_pane && recent.gui_geometry_main_upper_pane)
1004         gtk_paned_set_position(GTK_PANED(main_first_pane),  recent.gui_geometry_main_upper_pane);
1005     if (recent.has_gui_geometry_main_lower_pane && recent.gui_geometry_main_lower_pane)
1006         gtk_paned_set_position(GTK_PANED(main_second_pane), recent.gui_geometry_main_lower_pane);
1007     if (recent.has_gui_geometry_main_lower_pane && recent.gui_geometry_status_pane)
1008         gtk_paned_set_position(GTK_PANED(status_pane),      recent.gui_geometry_status_pane);
1009 }
1010
1011
1012 static void
1013 main_save_window_geometry(GtkWidget *widget)
1014 {
1015     window_geometry_t geom;
1016
1017     window_get_geometry(widget, &geom);
1018
1019     if (prefs.gui_geometry_save_position) {
1020         recent.gui_geometry_main_x = geom.x;
1021             recent.gui_geometry_main_y = geom.y;
1022     }
1023
1024     if (prefs.gui_geometry_save_size) {
1025         recent.gui_geometry_main_width  = geom.width, 
1026         recent.gui_geometry_main_height = geom.height;
1027     }
1028
1029 #if GTK_MAJOR_VERSION >= 2
1030     if(prefs.gui_geometry_save_maximized) {
1031         recent.gui_geometry_main_maximized = geom.maximized;
1032     }
1033
1034     recent.gui_geometry_main_upper_pane     = gtk_paned_get_position(GTK_PANED(main_first_pane));
1035     recent.gui_geometry_main_lower_pane     = gtk_paned_get_position(GTK_PANED(main_second_pane));
1036     recent.gui_geometry_status_pane         = gtk_paned_get_position(GTK_PANED(status_pane));
1037 #endif
1038 }
1039
1040 static void file_quit_answered_cb(gpointer dialog _U_, gint btn, gpointer data _U_)
1041 {
1042     switch(btn) {
1043     case(ESD_BTN_SAVE):
1044         /* save file first */
1045         file_save_as_cmd(after_save_exit, NULL);
1046         break;
1047     case(ESD_BTN_DONT_SAVE):
1048         main_do_quit();
1049         break;
1050     case(ESD_BTN_CANCEL):
1051         break;
1052     default:
1053         g_assert_not_reached();
1054     }
1055 }
1056
1057 void
1058 file_quit_cmd_cb(GtkWidget *widget _U_, gpointer data _U_)
1059 {
1060   gpointer dialog;
1061
1062   if((cfile.state != FILE_CLOSED) && !cfile.user_saved && prefs.gui_ask_unsaved) {
1063     /* user didn't saved his current file, ask him */
1064     dialog = simple_dialog(ESD_TYPE_CONFIRMATION, ESD_BTNS_SAVE_DONTSAVE_CANCEL,
1065                 PRIMARY_TEXT_START "Save capture file before program quit?" PRIMARY_TEXT_END "\n\n"
1066                 "If you quit the program without saving, your capture data will be discarded.");
1067     simple_dialog_set_cb(dialog, file_quit_answered_cb, NULL);
1068   } else {
1069     /* unchanged file, just exit */
1070     main_do_quit();
1071   }
1072 }
1073
1074 static void
1075 print_usage(gboolean print_ver) {
1076
1077   FILE *output;
1078
1079 #ifdef _WIN32
1080   create_console();
1081 #endif
1082
1083   if (print_ver) {
1084     output = stdout;
1085     fprintf(output, "This is "PACKAGE " " VERSION
1086 #ifdef SVNVERSION
1087         " (" SVNVERSION ")"
1088 #endif
1089         "\n (C) 1998-2005 Gerald Combs <gerald@ethereal.com>"
1090         "\n\n%s\n\n%s\n",
1091         comp_info_str->str, runtime_info_str->str);
1092   } else {
1093     output = stderr;
1094   }
1095 #ifdef HAVE_LIBPCAP
1096   fprintf(output, "\n%s [ -vh ] [ -klLnpQS ] [ -a <capture autostop condition> ] ...\n", PACKAGE);        
1097   fprintf(output, "\t[ -b <capture ring buffer option> ] ...\n");
1098   fprintf(output, "\t[ -B capture buffer size (Win32 only) ]\n"); 
1099   fprintf(output, "\t[ -c <capture packet count> ] [ -f <capture filter> ]\n");
1100   fprintf(output, "\t[ -g <packet number> ]\n");
1101   fprintf(output, "\t[ -i <capture interface> ] [ -m <font> ] [ -N <name resolving flags> ]\n");
1102   fprintf(output, "\t[ -o <preference/recent setting> ] ...\n");
1103   fprintf(output, "\t[ -r <infile> ] [ -R <read (display) filter> ] [ -s <capture snaplen> ]\n");
1104   fprintf(output, "\t[ -t <time stamp format> ]\n");
1105   fprintf(output, "\t[ -w <savefile> ] [ -y <capture link type> ] [ -z <statistics> ]\n");
1106   fprintf(output, "\t[ <infile> ]\n");
1107 #else
1108   fprintf(output, "\n%s [ -vh ] [ -n ] [ -g <packet number> ] [ -m <font> ]\n", PACKAGE);
1109   fprintf(output, "\t[ -N <resolving flags> ] [ -o <preference/recent setting> ...\n");
1110   fprintf(output, "\t[ -r <infile> ] [ -R <read (display) filter> ]\n");
1111   fprintf(output, "\t[ -t <time stamp format> ]\n");
1112   fprintf(output, "\t[ -z <statistics ] [ <infile> ]\n");
1113 #endif
1114
1115 #ifdef _WIN32
1116   destroy_console();
1117 #endif
1118 }
1119
1120 static void
1121 show_version(void)
1122 {
1123 #ifdef _WIN32
1124   create_console();
1125 #endif
1126
1127   printf(PACKAGE " " VERSION
1128 #ifdef SVNVERSION
1129       " (" SVNVERSION ")"
1130 #endif
1131       "\n\n%s\n\n%s\n",
1132       comp_info_str->str, runtime_info_str->str);
1133
1134 #ifdef _WIN32
1135   destroy_console();
1136 #endif
1137 }
1138
1139 #if defined(_WIN32) || GTK_MAJOR_VERSION < 2 || ! defined USE_THREADS
1140 /* 
1141    Once every 3 seconds we get a callback here which we use to update
1142    the tap extensions. Since Gtk1 is single threaded we dont have to
1143    worry about any locking or critical regions.
1144  */
1145 static gint
1146 update_cb(gpointer data _U_)
1147 {
1148         draw_tap_listeners(FALSE);
1149         return 1;
1150 }
1151 #else
1152
1153 /* if these three functions are copied to gtk1 ethereal, since gtk1 does not
1154    use threads all updte_thread_mutex can be dropped and protect/unprotect 
1155    would just be empty functions.
1156
1157    This allows gtk2-rpcstat.c and friends to be copied unmodified to 
1158    gtk1-ethereal and it will just work.
1159  */
1160 static GStaticMutex update_thread_mutex = G_STATIC_MUTEX_INIT;
1161 gpointer
1162 update_thread(gpointer data _U_)
1163 {
1164     while(1){
1165         GTimeVal tv1, tv2;
1166         g_get_current_time(&tv1);
1167         g_static_mutex_lock(&update_thread_mutex);
1168         gdk_threads_enter();
1169         draw_tap_listeners(FALSE);
1170         gdk_threads_leave();
1171         g_static_mutex_unlock(&update_thread_mutex);
1172         g_thread_yield();
1173         g_get_current_time(&tv2);
1174         if( ((tv1.tv_sec + 2) * 1000000 + tv1.tv_usec) >
1175             (tv2.tv_sec * 1000000 + tv2.tv_usec) ){
1176             g_usleep(((tv1.tv_sec + 2) * 1000000 + tv1.tv_usec) -
1177                      (tv2.tv_sec * 1000000 + tv2.tv_usec));
1178         }
1179     }
1180     return NULL;
1181 }
1182 #endif
1183 void
1184 protect_thread_critical_region(void)
1185 {
1186 #if !defined(_WIN32) && GTK_MAJOR_VERSION >= 2 && defined USE_THREADS
1187     g_static_mutex_lock(&update_thread_mutex);
1188 #endif
1189 }
1190 void
1191 unprotect_thread_critical_region(void)
1192 {
1193 #if !defined(_WIN32) && GTK_MAJOR_VERSION >= 2 && defined USE_THREADS
1194     g_static_mutex_unlock(&update_thread_mutex);
1195 #endif
1196 }
1197
1198 /* Set the file name in the status line, in the name for the main window,
1199    and in the name for the main window's icon. */
1200 static void
1201 set_display_filename(capture_file *cf)
1202 {
1203   const gchar *name_ptr;
1204   gchar       *status_msg;
1205   gchar       *size_str;
1206   gchar       *win_name;
1207
1208   name_ptr = cf_get_display_name(cf);
1209         
1210   if (!cf->is_tempfile && cf->filename) {
1211     /* Add this filename to the list of recent files in the "Recent Files" submenu */
1212     add_menu_recent_capture_file(cf->filename);
1213   }
1214
1215   /* convert file size */
1216   if (cf->f_len/1024/1024 > 10) {
1217     size_str = g_strdup_printf("%ld MB", cf->f_len/1024/1024);
1218   } else if (cf->f_len/1024 > 10) {
1219     size_str = g_strdup_printf("%ld KB", cf->f_len/1024);
1220   } else {
1221     size_str = g_strdup_printf("%ld Bytes", cf->f_len);
1222   }
1223
1224   /* statusbar */
1225   status_msg = g_strdup_printf(" File: \"%s\" %s %02u:%02u:%02u", 
1226     (cf->filename) ? cf->filename : "", size_str,
1227     cf->esec/3600, cf->esec%3600/60, cf->esec%60);
1228   g_free(size_str);
1229   statusbar_push_file_msg(status_msg);
1230   g_free(status_msg);
1231
1232   /* window title */
1233   win_name = g_strdup_printf("%s - Ethereal", name_ptr);
1234   set_main_window_name(win_name);
1235   g_free(win_name);
1236 }
1237
1238
1239 static void
1240 main_cf_cb_file_closed(capture_file *cf)
1241 {
1242     /* Destroy all windows, which refer to the
1243        capture file we're closing. */
1244     destroy_cfile_wins();
1245
1246     /* Clear any file-related status bar messages.
1247        XXX - should be "clear *ALL* file-related status bar messages;
1248        will there ever be more than one on the stack? */
1249     statusbar_pop_file_msg();
1250
1251     /* go back to "No packets" */
1252     packets_bar_update();
1253
1254     /* Restore the standard title bar message. */
1255     set_main_window_name("The Ethereal Network Analyzer");
1256
1257     /* Disable all menu items that make sense only if you have a capture. */
1258     set_menus_for_capture_file(FALSE);
1259     set_menus_for_unsaved_capture_file(FALSE);
1260     set_menus_for_captured_packets(FALSE);
1261     set_menus_for_selected_packet(cf);
1262     set_menus_for_capture_in_progress(FALSE);
1263     set_menus_for_selected_tree_row(cf);
1264
1265     /* Set up main window for no capture file. */
1266     main_set_for_capture_file(FALSE);
1267 }
1268
1269 static void
1270 main_cf_cb_file_read_start(capture_file *cf)
1271 {
1272   const gchar *name_ptr;
1273   gchar       *load_msg;
1274
1275   name_ptr = get_basename(cf->filename);
1276
1277   load_msg = g_strdup_printf(" Loading: %s", name_ptr);
1278   statusbar_push_file_msg(load_msg);
1279   g_free(load_msg);
1280 }
1281
1282 static void
1283 main_cf_cb_file_read_finished(capture_file *cf)
1284 {
1285     statusbar_pop_file_msg();
1286     set_display_filename(cf);
1287
1288     /* Enable menu items that make sense if you have a capture file you've
1289      finished reading. */
1290     set_menus_for_capture_file(TRUE);
1291     set_menus_for_unsaved_capture_file(!cf->user_saved);
1292
1293     /* Enable menu items that make sense if you have some captured packets. */
1294     set_menus_for_captured_packets(TRUE);
1295
1296     /* Set up main window for a capture file. */
1297     main_set_for_capture_file(TRUE);
1298 }
1299
1300 #ifdef HAVE_LIBPCAP
1301 static void
1302 main_cf_cb_live_capture_prepared(capture_options *capture_opts)
1303 {
1304     gchar *title;
1305
1306
1307     if(capture_opts->iface) {
1308         title = g_strdup_printf("%s: Capturing - Ethereal",
1309                             get_interface_descriptive_name(capture_opts->iface));
1310     } else {
1311         title = g_strdup_printf("Capturing - Ethereal");
1312     }
1313     set_main_window_name(title);
1314     g_free(title);
1315
1316     /* Disable menu items that make no sense if you're currently running
1317        a capture. */
1318     set_menus_for_capture_in_progress(TRUE);
1319
1320     /* update statusbar */
1321     statusbar_push_file_msg("Waiting for capture input data ...");
1322
1323     /* Don't set up main window for a capture file. */
1324     main_set_for_capture_file(FALSE);
1325 }
1326
1327 static void
1328 main_cf_cb_live_capture_update_started(capture_options *capture_opts)
1329 {
1330     gchar *capture_msg;
1331
1332
1333     /* Enable menu items that make sense if you have some captured
1334        packets (yes, I know, we don't have any *yet*). */
1335     set_menus_for_captured_packets(TRUE);
1336
1337     statusbar_pop_file_msg();
1338
1339     capture_msg = g_strdup_printf(" %s: <live capture in progress> to file: %s", 
1340         get_interface_descriptive_name(capture_opts->iface), 
1341         (capture_opts->save_file) ? capture_opts->save_file : "");
1342
1343     statusbar_push_file_msg(capture_msg);
1344
1345     g_free(capture_msg);
1346
1347     /* Set up main window for a capture file. */
1348     main_set_for_capture_file(TRUE);
1349 }
1350
1351 static void
1352 main_cf_cb_live_capture_update_continue(capture_file *cf)
1353 {
1354     gchar *capture_msg;
1355
1356
1357     statusbar_pop_file_msg();
1358
1359     if (cf->f_len/1024/1024 > 10) {
1360         capture_msg = g_strdup_printf(" %s: <live capture in progress> File: %s %ld MB", 
1361             get_interface_descriptive_name(capture_opts->iface), 
1362             capture_opts->save_file,
1363             cf->f_len/1024/1024);
1364     } else if (cf->f_len/1024 > 10) {
1365         capture_msg = g_strdup_printf(" %s: <live capture in progress> File: %s %ld KB", 
1366             get_interface_descriptive_name(capture_opts->iface), 
1367             capture_opts->save_file,
1368             cf->f_len/1024);
1369     } else {
1370         capture_msg = g_strdup_printf(" %s: <live capture in progress> File: %s %ld Bytes", 
1371             get_interface_descriptive_name(capture_opts->iface), 
1372             capture_opts->save_file,
1373             cf->f_len);
1374     }
1375
1376     statusbar_push_file_msg(capture_msg);
1377 }
1378
1379 static void
1380 main_cf_cb_live_capture_update_finished(capture_file *cf)
1381 {
1382     /* Pop the "<live capture in progress>" message off the status bar. */
1383     statusbar_pop_file_msg();
1384
1385     set_display_filename(cf);
1386
1387     /* Enable menu items that make sense if you're not currently running
1388      a capture. */
1389     set_menus_for_capture_in_progress(FALSE);
1390
1391     /* Enable menu items that make sense if you have a capture file
1392      you've finished reading. */
1393     set_menus_for_capture_file(TRUE);
1394     set_menus_for_unsaved_capture_file(!cf->user_saved);
1395
1396     /* Set up main window for a capture file. */
1397     main_set_for_capture_file(TRUE);
1398 }
1399
1400 static void
1401 main_cf_cb_live_capture_fixed_started(capture_options *capture_opts)
1402 {
1403     gchar *capture_msg;
1404
1405
1406     /* Enable menu items that make sense if you have some captured
1407        packets (yes, I know, we don't have any *yet*). */
1408     /*set_menus_for_captured_packets(TRUE);*/
1409
1410     statusbar_pop_file_msg();
1411
1412     capture_msg = g_strdup_printf(" %s: <live capture in progress> to file: %s", 
1413         get_interface_descriptive_name(capture_opts->iface), 
1414         (capture_opts->save_file) ? capture_opts->save_file : "");
1415
1416     statusbar_push_file_msg(capture_msg);
1417     gtk_statusbar_push(GTK_STATUSBAR(packets_bar), packets_ctx, " <capturing>");
1418
1419     g_free(capture_msg);
1420
1421     /* Don't set up main window for a capture file. */
1422     main_set_for_capture_file(FALSE);
1423 }
1424
1425 static void
1426 main_cf_cb_live_capture_fixed_finished(capture_file *cf _U_)
1427 {
1428     /* Pop the "<live capture in progress>" message off the status bar. */
1429     statusbar_pop_file_msg();
1430
1431     /* Pop the "<capturing>" message off the status bar */
1432         gtk_statusbar_pop(GTK_STATUSBAR(packets_bar), packets_ctx);
1433
1434     /*set_display_filename(cf);*/
1435
1436     /* Enable menu items that make sense if you're not currently running
1437      a capture. */
1438     set_menus_for_capture_in_progress(FALSE);
1439
1440     /* We don't have loaded the capture file, this will be done later.
1441      * For now we still have simply a blank screen. */
1442 }
1443 #endif
1444
1445 static void
1446 main_cf_cb_packet_selected(gpointer data)
1447 {
1448     capture_file *cf = data;
1449
1450     /* Display the GUI protocol tree and hex dump.
1451       XXX - why do we dump core if we call "proto_tree_draw()"
1452       before calling "add_byte_views()"? */
1453     add_main_byte_views(cf->edt);
1454     main_proto_tree_draw(cf->edt->tree);
1455
1456     /* A packet is selected. */
1457     set_menus_for_selected_packet(cf);
1458 }
1459
1460 static void
1461 main_cf_cb_packet_unselected(capture_file *cf)
1462 {
1463     /* Clear out the display of that packet. */
1464     clear_tree_and_hex_views();
1465
1466     /* No packet is selected. */
1467     set_menus_for_selected_packet(cf);
1468 }
1469
1470 static void
1471 main_cf_cb_field_unselected(capture_file *cf)
1472 {
1473     statusbar_pop_field_msg();
1474     set_menus_for_selected_tree_row(cf);
1475 }
1476
1477 static void
1478 main_cf_cb_file_safe_started(gchar * filename)
1479 {
1480     const gchar  *name_ptr;
1481     gchar        *save_msg;
1482
1483     name_ptr = get_basename(filename);
1484
1485     save_msg = g_strdup_printf(" Saving: %s...", name_ptr);
1486
1487     statusbar_push_file_msg(save_msg);
1488     g_free(save_msg);
1489 }
1490
1491 static void
1492 main_cf_cb_file_safe_finished(gpointer data _U_)
1493 {
1494     /* Pop the "Saving:" message off the status bar. */
1495     statusbar_pop_file_msg();
1496 }
1497
1498 static void
1499 main_cf_cb_file_safe_failed(gpointer data _U_)
1500 {
1501     /* Pop the "Saving:" message off the status bar. */
1502     statusbar_pop_file_msg();
1503 }
1504
1505 static void
1506 main_cf_cb_file_safe_reload_finished(gpointer data _U_)
1507 {
1508     set_menus_for_unsaved_capture_file(FALSE);
1509 }
1510
1511 static void main_cf_callback(gint event, gpointer data, gpointer user_data _U_)
1512 {
1513     switch(event) {
1514     case(cf_cb_file_closed):
1515         main_cf_cb_file_closed(data);
1516         break;
1517     case(cf_cb_file_read_start):
1518         main_cf_cb_file_read_start(data);
1519         break;
1520     case(cf_cb_file_read_finished):
1521         main_cf_cb_file_read_finished(data);
1522         break;
1523 #ifdef HAVE_LIBPCAP
1524     case(cf_cb_live_capture_prepared):
1525         main_cf_cb_live_capture_prepared(data);
1526         break;
1527     case(cf_cb_live_capture_update_started):
1528         main_cf_cb_live_capture_update_started(data);
1529         break;
1530     case(cf_cb_live_capture_update_continue):
1531         main_cf_cb_live_capture_update_continue(data);
1532         break;
1533     case(cf_cb_live_capture_fixed_started):
1534         main_cf_cb_live_capture_fixed_started(data);
1535         break;
1536     case(cf_cb_live_capture_update_finished):
1537         main_cf_cb_live_capture_update_finished(data);
1538         break;
1539     case(cf_cb_live_capture_fixed_finished):
1540         main_cf_cb_live_capture_fixed_finished(data);
1541         break;
1542 #endif
1543     case(cf_cb_packet_selected):
1544         main_cf_cb_packet_selected(data);
1545         break;
1546     case(cf_cb_packet_unselected):
1547         main_cf_cb_packet_unselected(data);
1548         break;
1549     case(cf_cb_field_unselected):
1550         main_cf_cb_field_unselected(data);
1551         break;
1552     case(cf_cb_file_safe_started):
1553         main_cf_cb_file_safe_started(data);
1554         break;
1555     case(cf_cb_file_safe_finished):
1556         main_cf_cb_file_safe_finished(data);
1557         break;
1558     case(cf_cb_file_safe_reload_finished):
1559         main_cf_cb_file_safe_reload_finished(data);
1560         break;
1561     case(cf_cb_file_safe_failed):
1562         main_cf_cb_file_safe_failed(data);
1563         break;
1564     default:
1565         g_warning("main_cf_callback: event %u unknown", event);
1566         g_assert_not_reached();
1567     }
1568 }
1569
1570 /* And now our feature presentation... [ fade to music ] */
1571 int
1572 main(int argc, char *argv[])
1573 {
1574 #ifdef HAVE_LIBPCAP
1575   const char          *command_name;
1576 #endif
1577   char                *s;
1578   int                  i;
1579   int                  opt;
1580   extern char         *optarg;
1581   gboolean             arg_error = FALSE;
1582
1583 #ifdef _WIN32
1584   WSADATA              wsaData;
1585 #endif  /* _WIN32 */
1586
1587   char                *rf_path;
1588   int                  rf_open_errno;
1589   char                *gpf_path, *pf_path;
1590   char                *cf_path, *df_path;
1591   char                *gdp_path, *dp_path;
1592   int                  gpf_open_errno, gpf_read_errno;
1593   int                  pf_open_errno, pf_read_errno;
1594   int                  cf_open_errno, df_open_errno;
1595   int                  gdp_open_errno, gdp_read_errno;
1596   int                  dp_open_errno, dp_read_errno;
1597   int                  err;
1598 #ifdef HAVE_LIBPCAP
1599   gboolean             start_capture = FALSE;
1600   GList               *if_list;
1601   if_info_t           *if_info;
1602   GList               *lt_list, *lt_entry;
1603   data_link_info_t    *data_link_info;
1604   gchar                err_str[PCAP_ERRBUF_SIZE];
1605   gchar               *cant_get_if_list_errstr;
1606   gboolean             stats_known;
1607   struct pcap_stat     stats;
1608 #else
1609   gboolean             capture_option_specified = FALSE;
1610 #endif
1611   gint                 pl_size = 280, tv_size = 95, bv_size = 75;
1612   gchar               *rc_file, *cf_name = NULL, *rfilter = NULL;
1613   dfilter_t           *rfcode = NULL;
1614   gboolean             rfilter_parse_failed = FALSE;
1615   e_prefs             *prefs;
1616   char                 badopt;
1617   GtkWidget           *splash_win = NULL;
1618   gboolean             capture_child; /* True if this is the child for "-S" */
1619   GLogLevelFlags       log_flags;
1620   guint                go_to_packet = 0;
1621   int                  optind_initial;
1622
1623 #define OPTSTRING_INIT "a:b:c:f:g:Hhi:klLm:nN:o:pQr:R:Ss:t:w:vy:z:"
1624
1625 #ifdef HAVE_LIBPCAP
1626 #ifdef _WIN32
1627 #define OPTSTRING_CHILD "W:Z:"
1628 #define OPTSTRING_WIN32 "B:"
1629 #else
1630 #define OPTSTRING_CHILD "W:"
1631 #define OPTSTRING_WIN32 ""
1632 #endif  /* _WIN32 */
1633 #else
1634 #define OPTSTRING_CHILD ""
1635 #define OPTSTRING_WIN32 ""
1636 #endif  /* HAVE_LIBPCAP */
1637
1638   char optstring[sizeof(OPTSTRING_INIT) + sizeof(OPTSTRING_CHILD) + sizeof(OPTSTRING_WIN32) - 2] =
1639     OPTSTRING_INIT OPTSTRING_WIN32;
1640
1641   /* initialize memory allocation subsystem */
1642   ep_init_chunk();
1643
1644   /*** create the compile and runtime version strings ***/
1645 #ifdef _WIN32
1646   /* Load wpcap if possible. Do this before collecting the run-time version information */
1647   load_wpcap();
1648
1649   /* ... and also load the packet.dll from wpcap */
1650   wpcap_packet_load();
1651
1652   /* Start windows sockets */
1653   WSAStartup( MAKEWORD( 1, 1 ), &wsaData );
1654 #endif  /* _WIN32 */
1655
1656   /* Assemble the compile-time version information string */
1657   comp_info_str = g_string_new("Compiled ");
1658   g_string_append(comp_info_str, "with ");
1659   g_string_sprintfa(comp_info_str,
1660 #ifdef GTK_MAJOR_VERSION
1661                     "GTK+ %d.%d.%d", GTK_MAJOR_VERSION, GTK_MINOR_VERSION,
1662                     GTK_MICRO_VERSION);
1663 #else
1664                     "GTK+ (version unknown)");
1665 #endif
1666
1667   g_string_append(comp_info_str, ", ");
1668   get_compiled_version_info(comp_info_str);
1669
1670   /* Assemble the run-time version information string */
1671   runtime_info_str = g_string_new("Running ");
1672   get_runtime_version_info(runtime_info_str);
1673
1674
1675   /*** "pre-scan" the command line parameters, if we have "console only" parameters ***/
1676   /* (e.g. don't start GTK+, if we only have to show the command line help) */
1677   optind_initial = optind;
1678   while ((opt = getopt(argc, argv, optstring)) != -1) {
1679     switch (opt) {
1680       case 'h':        /* Print help and exit */
1681         print_usage(TRUE);
1682         exit(0);
1683         break;
1684       case 'v':        /* Show version and exit */
1685         show_version();
1686         exit(0);
1687         break;
1688       case 'G':        /* dump various field or other infos, see handle_dashG_option() */
1689   /* If invoked with the "-G" flag, we dump out information based on
1690      the argument to the "-G" flag; if no argument is specified,
1691      for backwards compatibility we dump out a glossary of display
1692      filter symbols.
1693
1694      We must do this before calling "gtk_init()", because "gtk_init()"
1695      tries to open an X display, and we don't want to have to do any X
1696      stuff just to do a build.
1697
1698      Given that we call "gtk_init()" before doing the regular argument
1699      list processing, so that it can handle X and GTK+ arguments and
1700      remove them from the list at which we look, this means we must do
1701      this before doing the regular argument list processing, as well.
1702
1703      This means that:
1704
1705         you must give the "-G" flag as the first flag on the command line;
1706
1707         you must give it as "-G", nothing more, nothing less;
1708
1709         the first argument after the "-G" flag, if present, will be used
1710         to specify the information to dump;
1711
1712         arguments after that will not be used. */        
1713         handle_dashG_option(argc, argv, "ethereal");
1714         /* will never return! */
1715         exit(0);
1716         break;
1717     }
1718   }
1719
1720   /* set getopt index back to initial value, so it will start with the first command line parameter again */
1721   /* (XXX - this seems to be portable, but time will tell) */
1722   optind = optind_initial;
1723
1724
1725   /* Set the current locale according to the program environment.
1726    * We haven't localized anything, but some GTK widgets are localized
1727    * (the file selection dialogue, for example).
1728    * This also sets the C-language locale to the native environment. */
1729   gtk_set_locale();
1730
1731   /* Let GTK get its args (will need an X server, so do this after command line only commands handled) */
1732   gtk_init (&argc, &argv);
1733
1734   cf_callback_add(main_cf_callback, NULL);
1735
1736 #if GTK_MAJOR_VERSION < 2 && GTK_MINOR_VERSION < 3
1737   /* initialize our GTK eth_clist_type */
1738   init_eth_clist_type();
1739 #endif
1740
1741   ethereal_path = argv[0];
1742
1743   /* Arrange that if we have no console window, and a GLib message logging
1744      routine is called to log a message, we pop up a console window.
1745
1746      We do that by inserting our own handler for all messages logged
1747      to the default domain; that handler pops up a console if necessary,
1748      and then calls the default handler. */
1749
1750   /* We might want to have component specific log levels later ... */
1751
1752   /* the default_log_handler will use stdout, which makes trouble with the */
1753   /* capture child, as it uses stdout for it's sync_pipe */
1754   /* so do the filtering in the console_log_handler and not here */
1755   log_flags = 
1756                     G_LOG_LEVEL_ERROR|
1757                     G_LOG_LEVEL_CRITICAL|
1758                     G_LOG_LEVEL_WARNING|
1759                     G_LOG_LEVEL_MESSAGE|
1760                     G_LOG_LEVEL_INFO|
1761                     G_LOG_LEVEL_DEBUG|
1762                     G_LOG_FLAG_FATAL|G_LOG_FLAG_RECURSION;
1763
1764   g_log_set_handler(NULL,
1765                     log_flags,
1766                     console_log_handler, NULL /* user_data */);
1767
1768 #ifdef HAVE_LIBPCAP
1769   g_log_set_handler(LOG_DOMAIN_CAPTURE,
1770                     log_flags,
1771             console_log_handler, NULL /* user_data */);
1772   g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD,
1773                     log_flags,
1774             console_log_handler, NULL /* user_data */);
1775
1776   /* Set the initial values in the capture_opts. This might be overwritten 
1777      by preference settings and then again by the command line parameters. */
1778   capture_opts_init(capture_opts, &cfile);
1779
1780   capture_opts->snaplen             = MIN_PACKET_SIZE;
1781   capture_opts->has_ring_num_files  = TRUE;
1782
1783   command_name = get_basename(ethereal_path);
1784   /* Set "capture_child" to indicate whether this is going to be a child
1785      process for a "-S" capture. */
1786   capture_child = (strcmp(command_name, CHILD_NAME) == 0);
1787   if (capture_child) {
1788     strcat(optstring, OPTSTRING_CHILD);
1789   }
1790 #endif
1791
1792   /* We want a splash screen only if we're not a child process.
1793      We won't come till here, if we had a "console only" command line parameter. */
1794 #ifdef HAVE_LIBPCAP
1795   if (!capture_child)
1796 #endif
1797     splash_win = splash_new("Loading Ethereal ...");
1798
1799   splash_update(splash_win, "Registering dissectors ...");
1800
1801   /* Register all dissectors; we must do this before checking for the
1802      "-G" flag, as the "-G" flag dumps information registered by the
1803      dissectors, and we must do it before we read the preferences, in
1804      case any dissectors register preferences. */
1805   epan_init(PLUGIN_DIR,register_all_protocols,register_all_protocol_handoffs,
1806             failure_alert_box,open_failure_alert_box,read_failure_alert_box);
1807
1808   splash_update(splash_win, "Registering tap listeners ...");
1809
1810   /* Register all tap listeners; we do this before we parse the arguments,
1811      as the "-z" argument can specify a registered tap. */
1812
1813   /* we register the plugin taps before the other taps because
1814           stats_tree taps plugins will be registered as tap listeners
1815           by stats_tree_stat.c and need to registered before that */
1816
1817 #ifdef HAVE_PLUGINS
1818   register_all_plugin_tap_listeners();
1819 #endif
1820
1821   register_all_tap_listeners();
1822   
1823   splash_update(splash_win, "Loading module preferences ...");
1824
1825   /* Now register the preferences for any non-dissector modules.
1826      We must do that before we read the preferences as well. */
1827   prefs_register_modules();
1828
1829   /* multithread support currently doesn't seem to work in win32 gtk2.0.6 */
1830 #if !defined(_WIN32) && GTK_MAJOR_VERSION >= 2 && defined(G_THREADS_ENABLED) && defined USE_THREADS
1831   {
1832       GThread *ut;
1833       g_thread_init(NULL);
1834       gdk_threads_init();
1835       ut=g_thread_create(update_thread, NULL, FALSE, NULL);
1836       g_thread_set_priority(ut, G_THREAD_PRIORITY_LOW);
1837   }
1838 #else  /* _WIN32 || GTK1.2 || !G_THREADS_ENABLED || !USE_THREADS */
1839   /* this is to keep tap extensions updating once every 3 seconds */
1840   gtk_timeout_add(3000, (GtkFunction)update_cb,(gpointer)NULL);
1841 #endif /* !_WIN32 && GTK2 && G_THREADS_ENABLED */
1842
1843 #if HAVE_GNU_ADNS
1844   gtk_timeout_add(750, (GtkFunction) host_name_lookup_process, NULL);
1845 #endif
1846
1847   splash_update(splash_win, "Loading configuration files ...");
1848
1849   /* Read the preference files. */
1850   prefs = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
1851                      &pf_open_errno, &pf_read_errno, &pf_path);
1852   if (gpf_path != NULL) {
1853     if (gpf_open_errno != 0) {
1854       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
1855         "Could not open global preferences file\n\"%s\": %s.", gpf_path,
1856         strerror(gpf_open_errno));
1857     }
1858     if (gpf_read_errno != 0) {
1859       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
1860         "I/O error reading global preferences file\n\"%s\": %s.", gpf_path,
1861         strerror(gpf_read_errno));
1862     }
1863   }
1864   if (pf_path != NULL) {
1865     if (pf_open_errno != 0) {
1866       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
1867         "Could not open your preferences file\n\"%s\": %s.", pf_path,
1868         strerror(pf_open_errno));
1869     }
1870     if (pf_read_errno != 0) {
1871       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
1872         "I/O error reading your preferences file\n\"%s\": %s.", pf_path,
1873         strerror(pf_read_errno));
1874     }
1875     g_free(pf_path);
1876     pf_path = NULL;
1877   }
1878
1879 #ifdef _WIN32
1880   /* if the user wants a console to be always there, well, we should open one for him */
1881   if (prefs->gui_console_open == console_open_always) {
1882     create_console();
1883   }
1884 #endif
1885
1886 #ifdef HAVE_LIBPCAP
1887   /* If this is a capture child process, it should pay no attention
1888      to the "prefs.capture_prom_mode" setting in the preferences file;
1889      it should do what the parent process tells it to do, and if
1890      the parent process wants it not to run in promiscuous mode, it'll
1891      tell it so with a "-p" flag.
1892
1893      Otherwise, set promiscuous mode from the preferences setting. */
1894   /* the same applies to other preferences settings as well. */
1895   if (capture_child) {
1896     auto_scroll_live             = FALSE;
1897   } else {
1898     capture_opts->promisc_mode   = prefs->capture_prom_mode;
1899     capture_opts->show_info      = prefs->capture_show_info;
1900     capture_opts->real_time_mode = prefs->capture_real_time;
1901     auto_scroll_live             = prefs->capture_auto_scroll;
1902   }
1903
1904 #endif /* HAVE_LIBPCAP */
1905
1906   /* Set the name resolution code's flags from the preferences. */
1907   g_resolv_flags = prefs->name_resolve;
1908
1909   /* Read the capture filter file. */
1910   read_filter_list(CFILTER_LIST, &cf_path, &cf_open_errno);
1911   if (cf_path != NULL) {
1912       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
1913         "Could not open your capture filter file\n\"%s\": %s.", cf_path,
1914         strerror(cf_open_errno));
1915       g_free(cf_path);
1916   }
1917
1918   /* Read the display filter file. */
1919   read_filter_list(DFILTER_LIST, &df_path, &df_open_errno);
1920   if (df_path != NULL) {
1921       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
1922         "Could not open your display filter file\n\"%s\": %s.", df_path,
1923         strerror(df_open_errno));
1924       g_free(df_path);
1925   }
1926
1927   /* Read the disabled protocols file. */
1928   read_disabled_protos_list(&gdp_path, &gdp_open_errno, &gdp_read_errno,
1929                             &dp_path, &dp_open_errno, &dp_read_errno);
1930   if (gdp_path != NULL) {
1931     if (gdp_open_errno != 0) {
1932       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
1933         "Could not open global disabled protocols file\n\"%s\": %s.",
1934         gdp_path, strerror(gdp_open_errno));
1935     }
1936     if (gdp_read_errno != 0) {
1937       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
1938         "I/O error reading global disabled protocols file\n\"%s\": %s.",
1939         gdp_path, strerror(gdp_read_errno));
1940     }
1941     g_free(gdp_path);
1942   }
1943   if (dp_path != NULL) {
1944     if (dp_open_errno != 0) {
1945       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
1946         "Could not open your disabled protocols file\n\"%s\": %s.", dp_path,
1947         strerror(dp_open_errno));
1948     }
1949     if (dp_read_errno != 0) {
1950       simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
1951         "I/O error reading your disabled protocols file\n\"%s\": %s.", dp_path,
1952         strerror(dp_read_errno));
1953     }
1954     g_free(dp_path);
1955   }
1956
1957   /* Read the (static part) of the recent file. Only the static part of it will be read, */
1958   /* as we don't have the gui now to fill the recent lists which is done in the dynamic part. */
1959   /* We have to do this already here, so command line parameters can overwrite these values. */
1960   recent_read_static(&rf_path, &rf_open_errno);
1961
1962   init_cap_file(&cfile);
1963
1964   /* Now get our args */
1965   while ((opt = getopt(argc, argv, optstring)) != -1) {
1966     switch (opt) {
1967       /*** capture option specific ***/
1968       case 'a':        /* autostop criteria */
1969       case 'b':        /* Ringbuffer option */
1970       case 'c':        /* Capture xxx packets */
1971       case 'f':        /* capture filter */
1972       case 'k':        /* Start capture immediately */
1973       case 'H':        /* Hide capture info dialog box */
1974       case 'i':        /* Use interface xxx */
1975       case 'p':        /* Don't capture in promiscuous mode */
1976       case 'Q':        /* Quit after capture (just capture to file) */
1977       case 's':        /* Set the snapshot (capture) length */
1978       case 'S':        /* "Sync" mode: used for following file ala tail -f */
1979       case 'w':        /* Write to capture file xxx */
1980       case 'y':        /* Set the pcap data link type */
1981 #ifdef _WIN32
1982       case 'B':        /* Buffer size */
1983       /* Hidden option supporting Sync mode */
1984       case 'Z':        /* Write to pipe FD XXX */
1985 #endif /* _WIN32 */
1986 #ifdef HAVE_LIBPCAP
1987         capture_opts_add_opt(capture_opts, "ethereal", opt, optarg, &start_capture);
1988 #else
1989         capture_option_specified = TRUE;
1990         arg_error = TRUE;
1991 #endif
1992         break;
1993 #ifdef HAVE_LIBPCAP
1994       /* This is a hidden option supporting Sync mode, so we don't set
1995        * the error flags for the user in the non-libpcap case.
1996        */
1997       case 'W':        /* Write to capture file FD xxx */
1998         capture_opts_add_opt(capture_opts, "ethereal", opt, optarg, &start_capture);
1999         break;
2000 #endif
2001
2002       /*** all non capture option specific ***/
2003       case 'g':        /* Go to packet */
2004         go_to_packet = get_positive_int("Ethereal", optarg, "go to packet");
2005         break;
2006       case 'l':        /* Automatic scrolling in live capture mode */
2007 #ifdef HAVE_LIBPCAP
2008         auto_scroll_live = TRUE;
2009 #else
2010         capture_option_specified = TRUE;
2011         arg_error = TRUE;
2012 #endif
2013         break;
2014       case 'L':        /* Print list of link-layer types and exit */
2015 #ifdef HAVE_LIBPCAP
2016         list_link_layer_types = TRUE;
2017 #else
2018         capture_option_specified = TRUE;
2019         arg_error = TRUE;
2020 #endif
2021         break;
2022       case 'm':        /* Fixed-width font for the display */
2023         if (prefs->PREFS_GUI_FONT_NAME != NULL)
2024           g_free(prefs->PREFS_GUI_FONT_NAME);
2025         prefs->PREFS_GUI_FONT_NAME = g_strdup(optarg);
2026         break;
2027       case 'n':        /* No name resolution */
2028         g_resolv_flags = RESOLV_NONE;
2029         break;
2030       case 'N':        /* Select what types of addresses/port #s to resolve */
2031         if (g_resolv_flags == RESOLV_ALL)
2032           g_resolv_flags = RESOLV_NONE;
2033         badopt = string_to_name_resolve(optarg, &g_resolv_flags);
2034         if (badopt != '\0') {
2035           fprintf(stderr, "ethereal: -N specifies unknown resolving option '%c'; valid options are 'm', 'n', and 't'\n",
2036                         badopt);
2037           exit(1);
2038         }
2039         break;
2040       case 'o':        /* Override preference from command line */
2041         switch (prefs_set_pref(optarg)) {
2042         case PREFS_SET_OK:
2043           break;
2044         case PREFS_SET_SYNTAX_ERR:
2045           fprintf(stderr, "ethereal: Invalid -o flag \"%s\"\n", optarg);
2046           exit(1);
2047           break;
2048         case PREFS_SET_NO_SUCH_PREF:
2049           /* not a preference, might be a recent setting */
2050           switch (recent_set_arg(optarg)) {
2051             case PREFS_SET_OK:
2052               break;
2053             case PREFS_SET_SYNTAX_ERR:
2054               /* shouldn't happen, checked already above */
2055               fprintf(stderr, "ethereal: Invalid -o flag \"%s\"\n", optarg);
2056               exit(1);
2057               break;
2058             case PREFS_SET_NO_SUCH_PREF:
2059             case PREFS_SET_OBSOLETE:
2060               fprintf(stderr, "ethereal: -o flag \"%s\" specifies unknown preference/recent value\n",
2061                     optarg);
2062               exit(1);
2063               break;
2064             default:
2065               g_assert_not_reached();
2066             }
2067           break;
2068         case PREFS_SET_OBSOLETE:
2069           fprintf(stderr, "ethereal: -o flag \"%s\" specifies obsolete preference\n",
2070                         optarg);
2071           exit(1);
2072           break;
2073         default:
2074           g_assert_not_reached();
2075         }
2076         break;
2077       case 'r':        /* Read capture file xxx */
2078         /* We may set "last_open_dir" to "cf_name", and if we change
2079            "last_open_dir" later, we free the old value, so we have to
2080            set "cf_name" to something that's been allocated. */
2081         cf_name = g_strdup(optarg);
2082         break;
2083       case 'R':        /* Read file filter */
2084         rfilter = optarg;
2085         break;
2086       case 't':        /* Time stamp type */
2087         if (strcmp(optarg, "r") == 0)
2088           set_timestamp_setting(TS_RELATIVE);
2089         else if (strcmp(optarg, "a") == 0)
2090           set_timestamp_setting(TS_ABSOLUTE);
2091         else if (strcmp(optarg, "ad") == 0)
2092           set_timestamp_setting(TS_ABSOLUTE_WITH_DATE);
2093         else if (strcmp(optarg, "d") == 0)
2094           set_timestamp_setting(TS_DELTA);
2095         else {
2096           fprintf(stderr, "ethereal: Invalid time stamp type \"%s\"\n",
2097             optarg);
2098           fprintf(stderr, "It must be \"r\" for relative, \"a\" for absolute,\n");
2099           fprintf(stderr, "\"ad\" for absolute with date, or \"d\" for delta.\n");
2100           exit(1);
2101         }
2102         break;
2103       case 'z':
2104         /* We won't call the init function for the tap this soon
2105            as it would disallow MATE's fields (which are registered
2106            by the preferences set callback) from being used as
2107            part of a tap filter.  Instead, we just add the argument
2108            to a list of tap arguments. */
2109         if (!process_tap_cmd_arg(optarg)) {
2110           fprintf(stderr,"ethereal: invalid -z argument.\n");
2111           fprintf(stderr,"  -z argument must be one of :\n");
2112           list_tap_cmd_args();
2113           exit(1);
2114         }
2115         break;
2116       default:
2117       case '?':        /* Bad flag - print usage message */
2118         arg_error = TRUE;
2119         break;
2120     }
2121   }
2122   argc -= optind;
2123   argv += optind;
2124   if (argc >= 1) {
2125     if (cf_name != NULL) {
2126       /*
2127        * Input file name specified with "-r" *and* specified as a regular
2128        * command-line argument.
2129        */
2130       arg_error = TRUE;
2131     } else {
2132       /*
2133        * Input file name not specified with "-r", and a command-line argument
2134        * was specified; treat it as the input file name.
2135        *
2136        * Yes, this is different from tethereal, where non-flag command-line
2137        * arguments are a filter, but this works better on GUI desktops
2138        * where a command can be specified to be run to open a particular
2139        * file - yes, you could have "-r" as the last part of the command,
2140        * but that's a bit ugly.
2141        */
2142       cf_name = g_strdup(argv[0]);
2143     }
2144     argc--;
2145     argv++;
2146   }
2147
2148
2149
2150   if (argc != 0) {
2151     /*
2152      * Extra command line arguments were specified; complain.
2153      */
2154     fprintf(stderr, "Invalid argument: %s\n", argv[0]);
2155     arg_error = TRUE;
2156   }
2157
2158   if (arg_error) {
2159 #ifndef HAVE_LIBPCAP
2160     if (capture_option_specified) {
2161       fprintf(stderr, "This version of Ethereal was not built with support for capturing packets.\n");
2162     }
2163 #endif
2164     print_usage(FALSE);
2165     exit(1);
2166   }
2167
2168 #ifdef HAVE_LIBPCAP
2169   if (start_capture && list_link_layer_types) {
2170     /* Specifying *both* is bogus. */
2171     fprintf(stderr, "ethereal: You can't specify both -L and a live capture.\n");
2172     exit(1);
2173   }
2174
2175   if (list_link_layer_types) {
2176     /* We're supposed to list the link-layer types for an interface;
2177        did the user also specify a capture file to be read? */
2178     if (cf_name) {
2179       /* Yes - that's bogus. */
2180       fprintf(stderr, "ethereal: You can't specify -L and a capture file to be read.\n");
2181       exit(1);
2182     }
2183     /* No - did they specify a ring buffer option? */
2184     if (capture_opts->multi_files_on) {
2185       fprintf(stderr, "ethereal: Ring buffer requested, but a capture isn't being done.\n");
2186       exit(1);
2187     }
2188   } else {
2189     /* We're supposed to do a live capture; did the user also specify
2190        a capture file to be read? */
2191     if (start_capture && cf_name) {
2192       /* Yes - that's bogus. */
2193       fprintf(stderr, "ethereal: You can't specify both a live capture and a capture file to be read.\n");
2194       exit(1);
2195     }
2196
2197     /* No - was the ring buffer option specified and, if so, does it make
2198        sense? */
2199     if (capture_opts->multi_files_on) {
2200       /* Ring buffer works only under certain conditions:
2201          a) ring buffer does not work with temporary files;
2202          b) real_time_mode and multi_files_on are mutually exclusive -
2203             real_time_mode takes precedence;
2204          c) it makes no sense to enable the ring buffer if the maximum
2205             file size is set to "infinite". */
2206       if (capture_opts->save_file == NULL) {
2207         fprintf(stderr, "ethereal: Ring buffer requested, but capture isn't being saved to a permanent file.\n");
2208         capture_opts->multi_files_on = FALSE;
2209       }
2210 /*      if (capture_opts->real_time_mode) {
2211         fprintf(stderr, "ethereal: Ring buffer requested, but an \"Update list of packets in real time\" capture is being done.\n");
2212         capture_opts->multi_files_on = FALSE;
2213       }*/
2214       if (!capture_opts->has_autostop_filesize && !capture_opts->has_file_duration) {
2215         fprintf(stderr, "ethereal: Ring buffer requested, but no maximum capture file size or duration were specified.\n");
2216 /* XXX - this must be redesigned as the conditions changed */
2217 /*      capture_opts->multi_files_on = FALSE;*/
2218       }
2219     }
2220   }
2221
2222   if (start_capture || list_link_layer_types) {
2223     /* Did the user specify an interface to use? */
2224     if (capture_opts->iface == NULL) {
2225       /* No - is a default specified in the preferences file? */
2226       if (prefs->capture_device != NULL) {
2227           /* Yes - use it. */
2228           capture_opts->iface = g_strdup(prefs->capture_device);
2229       } else {
2230         /* No - pick the first one from the list of interfaces. */
2231         if_list = get_interface_list(&err, err_str);
2232         if (if_list == NULL) {
2233           switch (err) {
2234
2235           case CANT_GET_INTERFACE_LIST:
2236               cant_get_if_list_errstr = cant_get_if_list_error_message(err_str);
2237               fprintf(stderr, "%s\n", cant_get_if_list_errstr);
2238               g_free(cant_get_if_list_errstr);
2239               break;
2240
2241           case NO_INTERFACES_FOUND:
2242               fprintf(stderr, "ethereal: There are no interfaces on which a capture can be done\n");
2243               break;
2244           }
2245           exit(2);
2246         }
2247         if_info = if_list->data;        /* first interface */
2248         capture_opts->iface = g_strdup(if_info->name);
2249         free_interface_list(if_list);
2250       }
2251     }
2252   }
2253
2254   if (list_link_layer_types) {
2255     /* Get the list of link-layer types for the capture device. */
2256     lt_list = get_pcap_linktype_list(capture_opts->iface, err_str);
2257     if (lt_list == NULL) {
2258       if (err_str[0] != '\0') {
2259         fprintf(stderr, "ethereal: The list of data link types for the capture device could not be obtained (%s).\n"
2260           "Please check to make sure you have sufficient permissions, and that\n"
2261           "you have the proper interface or pipe specified.\n", err_str);
2262       } else
2263         fprintf(stderr, "ethereal: The capture device has no data link types.\n");
2264       exit(2);
2265     }
2266     fprintf(stderr, "Data link types (use option -y to set):\n");
2267     for (lt_entry = lt_list; lt_entry != NULL;
2268          lt_entry = g_list_next(lt_entry)) {
2269       data_link_info = lt_entry->data;
2270       fprintf(stderr, "  %s", data_link_info->name);
2271       if (data_link_info->description != NULL)
2272         fprintf(stderr, " (%s)", data_link_info->description);
2273       else
2274         fprintf(stderr, " (not supported)");
2275       putchar('\n');
2276     }
2277     free_pcap_linktype_list(lt_list);
2278     exit(0);
2279   }
2280
2281   if (capture_opts->has_snaplen) {
2282     if (capture_opts->snaplen < 1)
2283       capture_opts->snaplen = WTAP_MAX_PACKET_SIZE;
2284     else if (capture_opts->snaplen < MIN_PACKET_SIZE)
2285       capture_opts->snaplen = MIN_PACKET_SIZE;
2286   }
2287
2288   /* Check the value range of the ringbuffer_num_files parameter */
2289   if (capture_opts->ring_num_files > RINGBUFFER_MAX_NUM_FILES)
2290     capture_opts->ring_num_files = RINGBUFFER_MAX_NUM_FILES;
2291 #if RINGBUFFER_MIN_NUM_FILES > 0
2292   else if (capture_opts->num_files < RINGBUFFER_MIN_NUM_FILES)
2293     capture_opts->ring_num_files = RINGBUFFER_MIN_NUM_FILES;
2294 #endif
2295 #endif /* HAVE_LIBPCAP */
2296
2297   /* Notify all registered modules that have had any of their preferences
2298      changed either from one of the preferences file or from the command
2299      line that their preferences have changed. */
2300   prefs_apply_all();
2301
2302   /* disabled protocols as per configuration file */
2303   if (gdp_path == NULL && dp_path == NULL) {
2304     set_disabled_protos_list();
2305   }
2306
2307   /* Build the column format array */
2308   col_setup(&cfile.cinfo, prefs->num_cols);
2309   for (i = 0; i < cfile.cinfo.num_cols; i++) {
2310     cfile.cinfo.col_fmt[i] = get_column_format(i);
2311     cfile.cinfo.col_title[i] = g_strdup(get_column_title(i));
2312     cfile.cinfo.fmt_matx[i] = (gboolean *) g_malloc0(sizeof(gboolean) *
2313       NUM_COL_FMTS);
2314     get_column_format_matches(cfile.cinfo.fmt_matx[i], cfile.cinfo.col_fmt[i]);
2315     cfile.cinfo.col_data[i] = NULL;
2316     if (cfile.cinfo.col_fmt[i] == COL_INFO)
2317       cfile.cinfo.col_buf[i] = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_INFO_LEN);
2318     else
2319       cfile.cinfo.col_buf[i] = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
2320     cfile.cinfo.col_fence[i] = 0;
2321     cfile.cinfo.col_expr[i] = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
2322     cfile.cinfo.col_expr_val[i] = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
2323   }
2324
2325   for (i = 0; i < cfile.cinfo.num_cols; i++) {
2326       int j;
2327
2328       for (j = 0; j < NUM_COL_FMTS; j++) {
2329          if (!cfile.cinfo.fmt_matx[i][j])
2330              continue;
2331          
2332          if (cfile.cinfo.col_first[j] == -1)
2333              cfile.cinfo.col_first[j] = i;
2334          cfile.cinfo.col_last[j] = i;
2335       }
2336   }
2337
2338   /* read in rc file from global and personal configuration paths. */
2339   rc_file = get_datafile_path(RC_FILE);
2340   gtk_rc_parse(rc_file);
2341   rc_file = get_persconffile_path(RC_FILE, FALSE);
2342   gtk_rc_parse(rc_file);
2343
2344 #ifdef HAVE_LIBPCAP
2345   font_init(capture_child);
2346 #else
2347   font_init(FALSE);
2348 #endif
2349
2350   /* close the splash screen, as we are going to open the main window now */
2351   splash_destroy(splash_win);
2352
2353
2354 #ifdef HAVE_LIBPCAP
2355   /* Is this a "child" ethereal, which is only supposed to pop up a
2356      capture box to let us stop the capture, and run a capture
2357      to a file that our parent will read? */
2358   if (capture_child) {
2359     /* This is the child process of a capture session,
2360        so just do the low-level work of a capture - don't create
2361        a temporary file and fork off *another* child process (so don't
2362        call "capture_start()"). */
2363
2364     /* Pop up any queued-up alert boxes. */
2365     display_queued_messages();
2366
2367     /* Now start the capture. 
2368        After the capture is done; there's nothing more for us to do. */
2369
2370     /* XXX - hand these stats to the parent process */
2371     if(capture_loop_start(capture_opts, &stats_known, &stats) == TRUE) {
2372         /* capture ok */
2373         gtk_exit(0);
2374     } else {
2375         /* capture failed */
2376         gtk_exit(1);
2377     }
2378   }
2379 #endif
2380
2381   /***********************************************************************/
2382   /* Everything is prepared now, preferences and command line was read in,
2383        we are NOT a child window for a synced capture. */
2384
2385   /* Pop up the main window. */
2386   create_main_window(pl_size, tv_size, bv_size, prefs);
2387
2388   /* Read the dynamic part of the recent file, as we have the gui now ready for it. */
2389   recent_read_dynamic(&rf_path, &rf_open_errno);
2390   color_filters_enable(recent.packet_list_colorize);
2391
2392   /* rearrange all the widgets as we now have all recent settings ready for this */
2393   main_widgets_rearrange();
2394
2395   /* Fill in column titles.  This must be done after the top level window
2396      is displayed.
2397
2398      XXX - is that still true, with fixed-width columns? */
2399   packet_list_set_column_titles();
2400
2401   menu_recent_read_finished();
2402
2403   switch (user_font_apply()) {
2404   case FA_SUCCESS:
2405       break;
2406   case FA_FONT_NOT_RESIZEABLE:
2407       /* "user_font_apply()" popped up an alert box. */
2408       /* turn off zooming - font can't be resized */
2409   case FA_FONT_NOT_AVAILABLE:
2410       /* XXX - did we successfully load the un-zoomed version earlier?
2411       If so, this *probably* means the font is available, but not at
2412       this particular zoom level, but perhaps some other failure
2413       occurred; I'm not sure you can determine which is the case,
2414       however. */
2415       /* turn off zooming - zoom level is unavailable */
2416   default:
2417       /* in any other case than FA_SUCCESS, turn off zooming */
2418       recent.gui_zoom_level = 0;        
2419       /* XXX: would it be a good idea to disable zooming (insensitive GUI)? */
2420   }
2421
2422   dnd_init(top_level);
2423
2424   colors_init();
2425   color_filters_init();
2426   decode_as_init();
2427
2428   /* the window can be sized only, if it's not already shown, so do it now! */
2429   main_load_window_geometry(top_level);
2430
2431   /* If we were given the name of a capture file, read it in now;
2432      we defer it until now, so that, if we can't open it, and pop
2433      up an alert box, the alert box is more likely to come up on
2434      top of the main window - but before the preference-file-error
2435      alert box, so, if we get one of those, it's more likely to come
2436      up on top of us. */
2437   if (cf_name) {
2438     show_main_window(TRUE);
2439     if (rfilter != NULL) {
2440       if (!dfilter_compile(rfilter, &rfcode)) {
2441         bad_dfilter_alert_box(rfilter);
2442         rfilter_parse_failed = TRUE;
2443       }
2444     }
2445     if (!rfilter_parse_failed) {
2446       if (cf_open(&cfile, cf_name, FALSE, &err) == CF_OK) {
2447         /* "cf_open()" succeeded, so it closed the previous
2448            capture file, and thus destroyed any previous read filter
2449            attached to "cf". */
2450
2451         cfile.rfcode = rfcode;
2452         /* Open tap windows; we do so after creating the main window,
2453            to avoid GTK warnings, and after successfully opening the
2454            capture file, so we know we have something to tap, and
2455            after registering all dissectors, so that MATE will have
2456            registered its field array and we can have a filter with
2457            one of MATE's late-registered fields as part of the tap's
2458            filter. */
2459         start_requested_taps();
2460
2461         /* Read the capture file. */
2462         switch (cf_read(&cfile)) {
2463
2464         case CF_READ_OK:
2465         case CF_READ_ERROR:
2466           /* Just because we got an error, that doesn't mean we were unable
2467              to read any of the file; we handle what we could get from the
2468              file. */
2469           /* if the user told us to jump to a specific packet, do it now */
2470           if(go_to_packet != 0) {
2471             cf_goto_frame(&cfile, go_to_packet);
2472           }
2473           break;
2474
2475         case CF_READ_ABORTED:
2476           /* Exit now. */
2477           gtk_exit(0);
2478           break;
2479         }
2480         /* Save the name of the containing directory specified in the
2481            path name, if any; we can write over cf_name, which is a
2482            good thing, given that "get_dirname()" does write over its
2483            argument. */
2484         s = get_dirname(cf_name);
2485         /* we might already set this from the recent file, don't overwrite this */
2486         if(get_last_open_dir() == NULL) 
2487           set_last_open_dir(s);
2488         g_free(cf_name);
2489         cf_name = NULL;
2490       } else {
2491         if (rfcode != NULL)
2492           dfilter_free(rfcode);
2493         cfile.rfcode = NULL;
2494         set_menus_for_capture_in_progress(FALSE);
2495       }
2496     }
2497   } else {
2498 #ifdef HAVE_LIBPCAP
2499     if (start_capture) {
2500       if (capture_opts->save_file != NULL) {
2501         /* Save the directory name for future file dialogs. */
2502         /* (get_dirname overwrites filename) */
2503         s = get_dirname(g_strdup(capture_opts->save_file));  
2504         set_last_open_dir(s);
2505         g_free(s);
2506       }
2507       /* "-k" was specified; start a capture. */
2508       show_main_window(TRUE);
2509       if (capture_start(capture_opts)) {
2510         /* The capture started.  Open tap windows; we do so after creating
2511            the main window, to avoid GTK warnings, and after successfully
2512            opening the capture file, so we know we have something to tap,
2513            and after registering all dissectors, so that MATE will have
2514            registered its field array and we can have a filter with
2515            one of MATE's late-registered fields as part of the tap's
2516            filter. */
2517         start_requested_taps();
2518       }
2519     }
2520     else {
2521       show_main_window(FALSE);
2522       set_menus_for_capture_in_progress(FALSE);
2523     }
2524
2525     /* if the user didn't supplied a capture filter, use the one to filter out remote connections like SSH */
2526     if (!start_capture && (capture_opts->cfilter == NULL || strlen(capture_opts->cfilter) == 0)) {
2527       if (capture_opts->cfilter) {
2528         g_free(capture_opts->cfilter);
2529       }
2530       capture_opts->cfilter = g_strdup(get_conn_cfilter());
2531     }
2532 #else /* HAVE_LIBPCAP */
2533     show_main_window(FALSE);
2534     set_menus_for_capture_in_progress(FALSE);
2535 #endif /* HAVE_LIBPCAP */
2536   }
2537
2538   gtk_main();
2539
2540   epan_cleanup();
2541   g_free(rc_file);
2542
2543 #ifdef _WIN32
2544   /* hide the (unresponsive) main window, while asking the user to close the console window */
2545   gtk_widget_hide(top_level);
2546
2547   /* Shutdown windows sockets */
2548   WSACleanup();
2549
2550   /* For some unknown reason, the "atexit()" call in "create_console()"
2551      doesn't arrange that "destroy_console()" be called when we exit,
2552      so we call it here if a console was created. */
2553   destroy_console();
2554 #endif
2555
2556   gtk_exit(0);
2557
2558   /* This isn't reached, but we need it to keep GCC from complaining
2559      that "main()" returns without returning a value - it knows that
2560      "exit()" never returns, but it doesn't know that "gtk_exit()"
2561      doesn't, as GTK+ doesn't declare it with the attribute
2562      "noreturn". */
2563   return 0;     /* not reached */
2564 }
2565
2566 #ifdef _WIN32
2567
2568 /* We build this as a GUI subsystem application on Win32, so
2569    "WinMain()", not "main()", gets called.
2570
2571    Hack shamelessly stolen from the Win32 port of the GIMP. */
2572 #ifdef __GNUC__
2573 #define _stdcall  __attribute__((stdcall))
2574 #endif
2575
2576 int _stdcall
2577 WinMain (struct HINSTANCE__ *hInstance,
2578          struct HINSTANCE__ *hPrevInstance,
2579          char               *lpszCmdLine,
2580          int                 nCmdShow)
2581 {
2582   has_console = FALSE;
2583   return main (__argc, __argv);
2584 }
2585
2586 /*
2587  * If this application has no console window to which its standard output
2588  * would go, create one.
2589  */
2590 void
2591 create_console(void)
2592 {
2593   if (!has_console) {
2594     /* We have no console to which to print the version string, so
2595        create one and make it the standard input, output, and error. */
2596     if (!AllocConsole())
2597       return;   /* couldn't create console */
2598     freopen("CONIN$", "r", stdin);
2599     freopen("CONOUT$", "w", stdout);
2600     freopen("CONOUT$", "w", stderr);
2601
2602     /* Well, we have a console now. */
2603     has_console = TRUE;
2604
2605     /* Now register "destroy_console()" as a routine to be called just
2606        before the application exits, so that we can destroy the console
2607        after the user has typed a key (so that the console doesn't just
2608        disappear out from under them, giving the user no chance to see
2609        the message(s) we put in there). */
2610     atexit(destroy_console);
2611   }
2612 }
2613
2614 static void
2615 destroy_console(void)
2616 {
2617   if (has_console) {
2618     printf("\n\nPress any key to exit\n");
2619     _getch();
2620     FreeConsole();
2621   }
2622 }
2623 #endif /* _WIN32 */
2624
2625
2626 /* This routine should not be necessary, at least as I read the GLib
2627    source code, as it looks as if GLib is, on Win32, *supposed* to
2628    create a console window into which to display its output.
2629
2630    That doesn't happen, however.  I suspect there's something completely
2631    broken about that code in GLib-for-Win32, and that it may be related
2632    to the breakage that forces us to just call "printf()" on the message
2633    rather than passing the message on to "g_log_default_handler()"
2634    (which is the routine that does the aforementioned non-functional
2635    console window creation). */
2636 static void
2637 console_log_handler(const char *log_domain, GLogLevelFlags log_level,
2638                     const char *message, gpointer user_data _U_)
2639 {
2640   time_t curr;
2641   struct tm *today;
2642   const char *level;
2643
2644
2645   /* change this, if you want to see more verbose log output */
2646   /* XXX - make this a pref value */
2647   if( (log_level & G_LOG_LEVEL_MASK) > G_LOG_LEVEL_WARNING) {
2648     return;
2649   }
2650
2651   /* create a "timestamp" */
2652   time(&curr);
2653   today = localtime(&curr);    
2654
2655 #ifdef _WIN32
2656   if (prefs.gui_console_open != console_open_never) {
2657     create_console();
2658   }
2659   if (has_console) {
2660     /* For some unknown reason, the above doesn't appear to actually cause
2661        anything to be sent to the standard output, so we'll just splat the
2662        message out directly, just to make sure it gets out. */
2663 #endif
2664     switch(log_level & G_LOG_LEVEL_MASK) {
2665     case G_LOG_LEVEL_ERROR:
2666         level = "Err ";
2667         break;
2668     case G_LOG_LEVEL_CRITICAL:
2669         level = "Crit";
2670         break;
2671     case G_LOG_LEVEL_WARNING:
2672         level = "Warn";
2673         break;
2674     case G_LOG_LEVEL_MESSAGE:
2675         level = "Msg ";
2676         break;
2677     case G_LOG_LEVEL_INFO:
2678         level = "Info";
2679         break;
2680     case G_LOG_LEVEL_DEBUG:
2681         level = "Dbg ";
2682         break;
2683     default:
2684         fprintf(stderr, "unknown log_level %u\n", log_level);
2685         level = NULL;
2686         g_assert_not_reached();
2687     }
2688
2689     /* don't use printf (stdout), as the capture child uses stdout for it's sync_pipe */
2690     fprintf(stderr, "%02u:%02u:%02u %8s %s %s\n",
2691             today->tm_hour, today->tm_min, today->tm_sec,
2692             log_domain != NULL ? log_domain : "",
2693             level, message);
2694 #ifdef _WIN32
2695   } else {
2696     g_log_default_handler(log_domain, log_level, message, user_data);
2697   }
2698 #endif
2699 }
2700
2701
2702 static GtkWidget *info_bar_new(void)
2703 {
2704     /* tip: tooltips don't work on statusbars! */
2705     info_bar = gtk_statusbar_new();
2706     main_ctx = gtk_statusbar_get_context_id(GTK_STATUSBAR(info_bar), "main");
2707     file_ctx = gtk_statusbar_get_context_id(GTK_STATUSBAR(info_bar), "file");
2708     help_ctx = gtk_statusbar_get_context_id(GTK_STATUSBAR(info_bar), "help");
2709 #if GTK_MAJOR_VERSION >= 2
2710     gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(info_bar), FALSE);
2711 #endif
2712     gtk_statusbar_push(GTK_STATUSBAR(info_bar), main_ctx, DEF_READY_MESSAGE);
2713
2714     return info_bar;
2715 }
2716
2717 static GtkWidget *packets_bar_new(void)
2718 {
2719     /* tip: tooltips don't work on statusbars! */
2720     packets_bar = gtk_statusbar_new();
2721     packets_ctx = gtk_statusbar_get_context_id(GTK_STATUSBAR(packets_bar), "packets");
2722     packets_bar_update();
2723
2724     return packets_bar;
2725 }
2726
2727
2728 /*
2729  * Helper for main_widgets_rearrange()
2730  */
2731 static void foreach_remove_a_child(GtkWidget *widget, gpointer data) {
2732     gtk_container_remove(GTK_CONTAINER(data), widget);
2733 }
2734
2735 static GtkWidget *main_widget_layout(gint layout_content)
2736 {
2737     switch(layout_content) {
2738     case(layout_pane_content_none):
2739         return NULL;
2740         break;
2741     case(layout_pane_content_plist):
2742         return pkt_scrollw;
2743         break;
2744     case(layout_pane_content_pdetails):
2745         return tv_scrollw;
2746         break;
2747     case(layout_pane_content_pbytes):
2748         return byte_nb_ptr;
2749         break;
2750     default:
2751         g_assert_not_reached();
2752         return NULL;
2753     }
2754 }
2755
2756
2757 /*
2758  * Rearrange the main window widgets
2759  */
2760 void main_widgets_rearrange(void) {
2761     GtkWidget *first_pane_widget1, *first_pane_widget2;
2762     GtkWidget *second_pane_widget1, *second_pane_widget2;
2763     gboolean split_top_left;
2764
2765     /* be a bit faster */
2766     gtk_widget_hide(main_vbox);
2767
2768     /* be sure, we don't loose a widget while rearranging */
2769     gtk_widget_ref(menubar);
2770     gtk_widget_ref(main_tb);
2771     gtk_widget_ref(filter_tb);
2772     gtk_widget_ref(pkt_scrollw);
2773     gtk_widget_ref(tv_scrollw);
2774     gtk_widget_ref(byte_nb_ptr);
2775     gtk_widget_ref(stat_hbox);
2776     gtk_widget_ref(info_bar);
2777     gtk_widget_ref(packets_bar);
2778     gtk_widget_ref(status_pane);
2779     gtk_widget_ref(main_pane_v1);
2780     gtk_widget_ref(main_pane_v2);
2781     gtk_widget_ref(main_pane_h1);
2782     gtk_widget_ref(main_pane_h2);
2783     gtk_widget_ref(welcome_pane);
2784
2785     /* empty all containers participating */
2786     gtk_container_foreach(GTK_CONTAINER(main_vbox),     foreach_remove_a_child, main_vbox);
2787     gtk_container_foreach(GTK_CONTAINER(stat_hbox),     foreach_remove_a_child, stat_hbox);
2788     gtk_container_foreach(GTK_CONTAINER(status_pane),   foreach_remove_a_child, status_pane);
2789     gtk_container_foreach(GTK_CONTAINER(main_pane_v1),  foreach_remove_a_child, main_pane_v1);
2790     gtk_container_foreach(GTK_CONTAINER(main_pane_v2),  foreach_remove_a_child, main_pane_v2);
2791     gtk_container_foreach(GTK_CONTAINER(main_pane_h1),  foreach_remove_a_child, main_pane_h1);
2792     gtk_container_foreach(GTK_CONTAINER(main_pane_h2),  foreach_remove_a_child, main_pane_h2);
2793
2794     /* add the menubar always at the top */
2795     gtk_box_pack_start(GTK_BOX(main_vbox), menubar, FALSE, TRUE, 0);
2796
2797     /* main toolbar */
2798     gtk_box_pack_start(GTK_BOX(main_vbox), main_tb, FALSE, TRUE, 0);
2799
2800     /* filter toolbar in toolbar area */
2801     if (!prefs.filter_toolbar_show_in_statusbar) {
2802         gtk_box_pack_start(GTK_BOX(main_vbox), filter_tb, FALSE, TRUE, 1);
2803     }
2804
2805     /* fill the main layout panes */
2806     switch(prefs.gui_layout_type) {
2807     case(layout_type_5):
2808         main_first_pane  = main_pane_v1;
2809         main_second_pane = main_pane_v2;
2810         split_top_left = FALSE;
2811         break;
2812     case(layout_type_2):
2813         main_first_pane  = main_pane_v1;
2814         main_second_pane = main_pane_h1;
2815         split_top_left = FALSE;
2816         break;
2817     case(layout_type_1):
2818         main_first_pane  = main_pane_v1;
2819         main_second_pane = main_pane_h1;
2820         split_top_left = TRUE;
2821         break;
2822     case(layout_type_4):
2823         main_first_pane  = main_pane_h1;
2824         main_second_pane = main_pane_v1;
2825         split_top_left = FALSE;
2826         break;
2827     case(layout_type_3):
2828         main_first_pane  = main_pane_h1;
2829         main_second_pane = main_pane_v1;
2830         split_top_left = TRUE;
2831         break;
2832     case(layout_type_6):
2833         main_first_pane  = main_pane_h1;
2834         main_second_pane = main_pane_h2;
2835         split_top_left = FALSE;
2836         break;
2837     default:
2838         main_first_pane = NULL;
2839         main_second_pane = NULL;
2840         split_top_left = FALSE;
2841         g_assert_not_reached();
2842     }
2843     if (split_top_left) {
2844         first_pane_widget1 = main_second_pane;
2845         second_pane_widget1 = main_widget_layout(prefs.gui_layout_content_1);
2846         second_pane_widget2 = main_widget_layout(prefs.gui_layout_content_2);
2847         first_pane_widget2 = main_widget_layout(prefs.gui_layout_content_3);
2848     } else {
2849         first_pane_widget1 = main_widget_layout(prefs.gui_layout_content_1);
2850         first_pane_widget2 = main_second_pane;
2851         second_pane_widget1 = main_widget_layout(prefs.gui_layout_content_2);
2852         second_pane_widget2 = main_widget_layout(prefs.gui_layout_content_3);
2853     }
2854     if (first_pane_widget1 != NULL)
2855         gtk_paned_add1(GTK_PANED(main_first_pane), first_pane_widget1);
2856     if (first_pane_widget2 != NULL)
2857         gtk_paned_add2(GTK_PANED(main_first_pane), first_pane_widget2);
2858     if (second_pane_widget1 != NULL)
2859         gtk_paned_pack1(GTK_PANED(main_second_pane), second_pane_widget1, TRUE, TRUE);
2860     if (second_pane_widget2 != NULL)
2861         gtk_paned_pack2(GTK_PANED(main_second_pane), second_pane_widget2, FALSE, FALSE);
2862
2863     gtk_container_add(GTK_CONTAINER(main_vbox), main_first_pane);
2864
2865     /* welcome pane */
2866     gtk_box_pack_start(GTK_BOX(main_vbox), welcome_pane, TRUE, TRUE, 0);
2867
2868     /* statusbar hbox */
2869     gtk_box_pack_start(GTK_BOX(main_vbox), stat_hbox, FALSE, TRUE, 0);
2870
2871     /* filter toolbar in statusbar hbox */
2872     if (prefs.filter_toolbar_show_in_statusbar) {
2873         gtk_box_pack_start(GTK_BOX(stat_hbox), filter_tb, FALSE, TRUE, 1);
2874     }
2875
2876     /* statusbar */
2877     gtk_box_pack_start(GTK_BOX(stat_hbox), status_pane, TRUE, TRUE, 0);
2878     gtk_paned_pack1(GTK_PANED(status_pane), info_bar, FALSE, FALSE);
2879     gtk_paned_pack2(GTK_PANED(status_pane), packets_bar, FALSE, FALSE);
2880
2881     /* hide widgets on users recent settings */
2882     main_widgets_show_or_hide();
2883
2884     gtk_widget_show(main_vbox);
2885 }
2886
2887 static void
2888 is_widget_visible(GtkWidget *widget, gpointer data)
2889 {
2890     gboolean *is_visible = data;
2891
2892     if (!*is_visible) {
2893         if (GTK_WIDGET_VISIBLE(widget))
2894             *is_visible = TRUE;
2895     }
2896 }
2897
2898
2899 #if 0
2900 /* XXX - There seems to be some disagreement about if and how this feature should be implemented.
2901    As I currently don't have the time to continue this, it's temporarily disabled. - ULFL */
2902 GtkWidget *
2903 welcome_item(gchar *stock_item, gchar * label, gchar * message, GtkSignalFunc callback, void *callback_data)
2904 {
2905     GtkWidget *w, *item_hb;
2906 #if GTK_MAJOR_VERSION >= 2
2907     gchar *formatted_message;
2908 #endif
2909
2910
2911     item_hb = gtk_hbox_new(FALSE, 1);
2912
2913     w = BUTTON_NEW_FROM_STOCK(stock_item);
2914     WIDGET_SET_SIZE(w, 60, 60);
2915 #if GTK_MAJOR_VERSION >= 2
2916     gtk_button_set_label(GTK_BUTTON(w), label);
2917 #endif
2918     gtk_box_pack_start(GTK_BOX(item_hb), w, FALSE, FALSE, 0);
2919     SIGNAL_CONNECT(w, "clicked", callback, callback_data);
2920
2921     w = gtk_label_new(message);
2922         gtk_misc_set_alignment (GTK_MISC(w), 0.0, 0.5);
2923 #if GTK_MAJOR_VERSION >= 2
2924     formatted_message = g_strdup_printf("<span weight=\"bold\" size=\"x-large\">%s</span>", message);
2925     gtk_label_set_markup(GTK_LABEL(w), formatted_message);
2926     g_free(formatted_message);
2927 #endif
2928
2929     gtk_box_pack_start(GTK_BOX(item_hb), w, FALSE, FALSE, 10);
2930
2931     return item_hb;
2932 }
2933
2934
2935 /* XXX - the layout has to be improved */
2936 GtkWidget *
2937 welcome_new(void)
2938 {
2939     GtkWidget *welcome_scrollw, *welcome_hb, *welcome_vb, *item_hb;
2940     GtkWidget *w, *icon;
2941     gchar * message;
2942
2943
2944     welcome_scrollw = scrolled_window_new(NULL, NULL);
2945
2946     welcome_hb = gtk_hbox_new(FALSE, 1);
2947         /*gtk_container_border_width(GTK_CONTAINER(welcome_hb), 20);*/
2948
2949     welcome_vb = gtk_vbox_new(FALSE, 1);
2950
2951     item_hb = gtk_hbox_new(FALSE, 1);
2952
2953     icon = xpm_to_widget_from_parent(top_level, eicon3d64_xpm);
2954     gtk_box_pack_start(GTK_BOX(item_hb), icon, FALSE, FALSE, 5);
2955
2956 #if GTK_MAJOR_VERSION < 2
2957     message = "Welcome to Ethereal!";
2958 #else
2959     message = "<span weight=\"bold\" size=\"25000\">" "Welcome to Ethereal!" "</span>";
2960 #endif
2961     w = gtk_label_new(message);
2962 #if GTK_MAJOR_VERSION >= 2
2963     gtk_label_set_markup(GTK_LABEL(w), message);
2964 #endif
2965     gtk_misc_set_alignment (GTK_MISC(w), 0.0, 0.5);
2966     gtk_box_pack_start(GTK_BOX(item_hb), w, TRUE, TRUE, 5);
2967
2968     gtk_box_pack_start(GTK_BOX(welcome_vb), item_hb, TRUE, FALSE, 5);
2969
2970     w = gtk_label_new("What would you like to do?");
2971     gtk_box_pack_start(GTK_BOX(welcome_vb), w, FALSE, FALSE, 10);
2972     gtk_misc_set_alignment (GTK_MISC(w), 0.0, 0.0);
2973
2974 #ifdef HAVE_LIBPCAP
2975     item_hb = welcome_item(ETHEREAL_STOCK_CAPTURE_START, 
2976         "Capture",
2977         "Capture live data from your network", 
2978         GTK_SIGNAL_FUNC(capture_prep_cb), NULL);
2979     gtk_box_pack_start(GTK_BOX(welcome_vb), item_hb, TRUE, FALSE, 5);
2980 #endif
2981
2982     item_hb = welcome_item(GTK_STOCK_OPEN, 
2983         "Open",
2984         "Open a previously captured file",
2985         GTK_SIGNAL_FUNC(file_open_cmd_cb), NULL);
2986     gtk_box_pack_start(GTK_BOX(welcome_vb), item_hb, TRUE, FALSE, 5);
2987
2988 #if (GLIB_MAJOR_VERSION >= 2)
2989     item_hb = welcome_item(GTK_STOCK_HOME, 
2990         "Home",
2991         "Visit the Ethereal homepage",
2992         GTK_SIGNAL_FUNC(topic_cb), GINT_TO_POINTER(ONLINEPAGE_HOME));
2993     gtk_box_pack_start(GTK_BOX(welcome_vb), item_hb, TRUE, FALSE, 5);
2994 #endif
2995
2996     /* the end */
2997     w = gtk_label_new("");
2998     gtk_box_pack_start(GTK_BOX(welcome_vb), w, TRUE, TRUE, 0);
2999
3000     w = gtk_label_new("");
3001     gtk_box_pack_start(GTK_BOX(welcome_hb), w, TRUE, TRUE, 0);
3002
3003     gtk_box_pack_start(GTK_BOX(welcome_hb), welcome_vb, TRUE, TRUE, 0);
3004
3005     w = gtk_label_new("");
3006     gtk_box_pack_start(GTK_BOX(welcome_hb), w, TRUE, TRUE, 0);
3007
3008     gtk_widget_show_all(welcome_hb);
3009
3010     gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(welcome_scrollw),
3011                                           welcome_hb);
3012     gtk_widget_show_all(welcome_scrollw);
3013
3014     return welcome_scrollw;
3015 }
3016 #endif /* 0 */
3017
3018 static GtkWidget *
3019 welcome_new(void)
3020 {
3021     /* this is just a dummy to fill up window space, simply showing nothing */
3022     return scrolled_window_new(NULL, NULL);
3023 }
3024
3025
3026
3027 /*
3028  * XXX - this doesn't appear to work with the paned widgets in
3029  * GTK+ 1.2[.x]; if you hide one of the panes, the splitter remains
3030  * and the other pane doesn't grow to take up the rest of the pane.
3031  * It does appear to work with GTK+ 2.x.
3032  */
3033 void
3034 main_widgets_show_or_hide(void)
3035 {
3036     gboolean main_second_pane_show;
3037
3038     if (recent.main_toolbar_show) {
3039         gtk_widget_show(main_tb);
3040     } else {
3041         gtk_widget_hide(main_tb);
3042     }
3043
3044     /*
3045      * Show the status hbox if either:
3046      *
3047      *    1) we're showing the filter toolbar and we want it in the status
3048      *       line
3049      *
3050      * or
3051      *
3052      *    2) we're showing the status bar.
3053      */
3054     if ((recent.filter_toolbar_show && prefs.filter_toolbar_show_in_statusbar) ||
3055          recent.statusbar_show) {
3056         gtk_widget_show(stat_hbox);
3057     } else {
3058         gtk_widget_hide(stat_hbox);
3059     }
3060
3061     if (recent.statusbar_show) {
3062         gtk_widget_show(status_pane);
3063     } else {
3064         gtk_widget_hide(status_pane);
3065     }
3066
3067     if (recent.filter_toolbar_show) {
3068         gtk_widget_show(filter_tb);
3069     } else {
3070         gtk_widget_hide(filter_tb);
3071     }
3072
3073     if (recent.packet_list_show && have_capture_file) {
3074         gtk_widget_show(pkt_scrollw);
3075     } else {
3076         gtk_widget_hide(pkt_scrollw);
3077     }
3078
3079     if (recent.tree_view_show && have_capture_file) {
3080         gtk_widget_show(tv_scrollw);
3081     } else {
3082         gtk_widget_hide(tv_scrollw);
3083     }
3084
3085     if (recent.byte_view_show && have_capture_file) {
3086         gtk_widget_show(byte_nb_ptr);
3087     } else {
3088         gtk_widget_hide(byte_nb_ptr);
3089     }
3090
3091     if (have_capture_file) {
3092         gtk_widget_show(main_first_pane);
3093     } else {
3094         gtk_widget_hide(main_first_pane);
3095     }
3096
3097     /*
3098      * Is anything in "main_second_pane" visible?
3099      * If so, show it, otherwise hide it.
3100      */
3101     main_second_pane_show = FALSE;
3102     gtk_container_foreach(GTK_CONTAINER(main_second_pane), is_widget_visible,
3103                           &main_second_pane_show);
3104     if (main_second_pane_show) {
3105         gtk_widget_show(main_second_pane);
3106     } else {
3107         gtk_widget_hide(main_second_pane);
3108     }
3109
3110     if (!have_capture_file) {
3111         if(welcome_pane) {
3112             gtk_widget_show(welcome_pane);
3113         }
3114     } else {
3115         gtk_widget_hide(welcome_pane);
3116     }
3117 }
3118
3119
3120 #if GTK_MAJOR_VERSION >= 2
3121 /* called, when the window state changes (minimized, maximized, ...) */
3122 static int
3123 window_state_event_cb (GtkWidget *widget _U_,
3124                        GdkEvent *event,
3125                        gpointer  data _U_)
3126 {
3127     GdkWindowState new_window_state = ((GdkEventWindowState*)event)->new_window_state;
3128
3129     if( (event->type) == (GDK_WINDOW_STATE)) {
3130         if(!(new_window_state & GDK_WINDOW_STATE_ICONIFIED)) {
3131             /* we might have dialogs popped up while we where iconified,
3132                show em now */
3133             display_queued_messages();
3134         }
3135     }
3136     return FALSE;
3137 }
3138 #endif
3139
3140
3141 static void
3142 create_main_window (gint pl_size, gint tv_size, gint bv_size, e_prefs *prefs)
3143 {
3144     GtkWidget     
3145                   *filter_bt, *filter_cm, *filter_te,
3146                   *filter_add_expr_bt,
3147                   *filter_apply,
3148                   *filter_reset;
3149     GList         *dfilter_list = NULL;
3150     GtkTooltips   *tooltips;
3151     GtkAccelGroup *accel;
3152         gchar         *title;
3153     /* Display filter construct dialog has an Apply button, and "OK" not
3154        only sets our text widget, it activates it (i.e., it causes us to
3155        filter the capture). */
3156     static construct_args_t args = {
3157         "Ethereal: Display Filter",
3158         TRUE,
3159         TRUE
3160     };
3161
3162     /* use user-defined title if preference is set */
3163     title = create_user_window_title("The Ethereal Network Analyzer");
3164
3165     /* Main window */
3166     top_level = window_new(GTK_WINDOW_TOPLEVEL, title);
3167     g_free(title);
3168
3169     tooltips = gtk_tooltips_new();
3170
3171 #ifdef _WIN32 
3172 #if GTK_MAJOR_VERSION < 2
3173     /* has to be done, after top_level window is created */
3174     app_font_gtk1_init(top_level);
3175 #endif
3176 #endif
3177     
3178     gtk_widget_set_name(top_level, "main window");
3179     SIGNAL_CONNECT(top_level, "delete_event", main_window_delete_event_cb,
3180                    NULL);
3181 #if GTK_MAJOR_VERSION >= 2
3182     SIGNAL_CONNECT(GTK_OBJECT(top_level), "window_state_event",
3183                          G_CALLBACK (window_state_event_cb), NULL);
3184 #endif
3185
3186     gtk_window_set_policy(GTK_WINDOW(top_level), TRUE, TRUE, FALSE);
3187
3188     /* Container for menu bar, toolbar(s), paned windows and progress/info box */
3189     main_vbox = gtk_vbox_new(FALSE, 1);
3190     gtk_container_border_width(GTK_CONTAINER(main_vbox), 1);
3191     gtk_container_add(GTK_CONTAINER(top_level), main_vbox);
3192     gtk_widget_show(main_vbox);
3193
3194     /* Menu bar */
3195     menubar = main_menu_new(&accel);
3196     gtk_window_add_accel_group(GTK_WINDOW(top_level), accel);
3197     gtk_widget_show(menubar);
3198
3199     /* Main Toolbar */
3200     main_tb = toolbar_new();
3201     gtk_widget_show (main_tb);
3202
3203     /* Packet list */
3204     pkt_scrollw = packet_list_new(prefs);
3205     WIDGET_SET_SIZE(packet_list, -1, pl_size);
3206     gtk_widget_show(pkt_scrollw);
3207
3208     /* Tree view */
3209     tv_scrollw = main_tree_view_new(prefs, &tree_view);
3210     WIDGET_SET_SIZE(tv_scrollw, -1, tv_size);
3211     gtk_widget_show(tv_scrollw);
3212
3213 #if GTK_MAJOR_VERSION < 2
3214     SIGNAL_CONNECT(tree_view, "tree-select-row", tree_view_select_row_cb, NULL);
3215     SIGNAL_CONNECT(tree_view, "tree-unselect-row", tree_view_unselect_row_cb,
3216                    NULL);
3217 #else
3218     SIGNAL_CONNECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view)),
3219                    "changed", tree_view_selection_changed_cb, NULL);
3220 #endif
3221     SIGNAL_CONNECT(tree_view, "button_press_event", popup_menu_handler,
3222                    OBJECT_GET_DATA(popup_menu_object, PM_TREE_VIEW_KEY));
3223     gtk_widget_show(tree_view);
3224
3225     /* Byte view. */
3226     byte_nb_ptr = byte_view_new();
3227     WIDGET_SET_SIZE(byte_nb_ptr, -1, bv_size);
3228     gtk_widget_show(byte_nb_ptr);
3229
3230     SIGNAL_CONNECT(byte_nb_ptr, "button_press_event", popup_menu_handler,
3231                    OBJECT_GET_DATA(popup_menu_object, PM_HEXDUMP_KEY));
3232
3233
3234     /* Panes for the packet list, tree, and byte view */
3235     main_pane_v1 = gtk_vpaned_new();
3236     gtk_widget_show(main_pane_v1);
3237     main_pane_v2 = gtk_vpaned_new();
3238     gtk_widget_show(main_pane_v2);
3239     main_pane_h1 = gtk_hpaned_new();
3240     gtk_widget_show(main_pane_h1);
3241     main_pane_h2 = gtk_hpaned_new();
3242     gtk_widget_show(main_pane_h2);
3243
3244     /* filter toolbar */
3245 #if GTK_MAJOR_VERSION < 2
3246     filter_tb = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
3247                                GTK_TOOLBAR_BOTH);
3248 #else
3249     filter_tb = gtk_toolbar_new();
3250     gtk_toolbar_set_orientation(GTK_TOOLBAR(filter_tb),
3251                                 GTK_ORIENTATION_HORIZONTAL);
3252 #endif /* GTK_MAJOR_VERSION */
3253     gtk_widget_show(filter_tb);
3254
3255     /* Create the "Filter:" button */
3256     filter_bt = BUTTON_NEW_FROM_STOCK(ETHEREAL_STOCK_DISPLAY_FILTER_ENTRY);
3257     SIGNAL_CONNECT(filter_bt, "clicked", display_filter_construct_cb, &args);
3258     gtk_widget_show(filter_bt);
3259     OBJECT_SET_DATA(top_level, E_FILT_BT_PTR_KEY, filter_bt);
3260
3261     gtk_toolbar_append_widget(GTK_TOOLBAR(filter_tb), filter_bt, 
3262         "Open the \"Display Filter\" dialog, to edit/apply filters", "Private");
3263
3264     /* Create the filter combobox */
3265     filter_cm = gtk_combo_new();
3266     dfilter_list = NULL;
3267     gtk_combo_disable_activate(GTK_COMBO(filter_cm));
3268     gtk_combo_set_case_sensitive(GTK_COMBO(filter_cm), TRUE);
3269     OBJECT_SET_DATA(filter_cm, E_DFILTER_FL_KEY, dfilter_list);
3270     filter_te = GTK_COMBO(filter_cm)->entry;
3271     main_display_filter_widget=filter_te;
3272     OBJECT_SET_DATA(filter_bt, E_FILT_TE_PTR_KEY, filter_te);
3273     OBJECT_SET_DATA(filter_te, E_DFILTER_CM_KEY, filter_cm);
3274     OBJECT_SET_DATA(top_level, E_DFILTER_CM_KEY, filter_cm);
3275     SIGNAL_CONNECT(filter_te, "activate", filter_activate_cb, filter_te);
3276     SIGNAL_CONNECT(filter_te, "changed", filter_te_syntax_check_cb, NULL);
3277     WIDGET_SET_SIZE(filter_cm, 400, -1);
3278     gtk_widget_show(filter_cm);
3279     gtk_toolbar_append_widget(GTK_TOOLBAR(filter_tb), filter_cm, 
3280         NULL, NULL);
3281     /* setting a tooltip for a combobox will do nothing, so add it to the corresponding text entry */
3282     gtk_tooltips_set_tip(tooltips, filter_te, 
3283         "Enter a display filter, or choose one of your recently used filters. "
3284         "The background color of this field is changed by a continuous syntax check (green is valid, red is invalid).", 
3285         NULL);
3286
3287     /* Create the "Add Expression..." button, to pop up a dialog
3288        for constructing filter comparison expressions. */
3289     filter_add_expr_bt = BUTTON_NEW_FROM_STOCK(ETHEREAL_STOCK_ADD_EXPRESSION);
3290     OBJECT_SET_DATA(filter_tb, E_FILT_FILTER_TE_KEY, filter_te);
3291     SIGNAL_CONNECT(filter_add_expr_bt, "clicked", filter_add_expr_bt_cb, filter_tb);
3292     gtk_widget_show(filter_add_expr_bt);
3293     gtk_toolbar_append_widget(GTK_TOOLBAR(filter_tb), filter_add_expr_bt, 
3294         "Add an expression to this filter string", "Private");
3295
3296     /* Create the "Clear" button */
3297     filter_reset = BUTTON_NEW_FROM_STOCK(GTK_STOCK_CLEAR);
3298     OBJECT_SET_DATA(filter_reset, E_DFILTER_TE_KEY, filter_te);
3299     SIGNAL_CONNECT(filter_reset, "clicked", filter_reset_cb, NULL);
3300     gtk_widget_show(filter_reset);
3301     gtk_toolbar_append_widget(GTK_TOOLBAR(filter_tb), filter_reset, 
3302         "Clear this filter string and update the display", "Private");
3303
3304     /* Create the "Apply" button */
3305     filter_apply = BUTTON_NEW_FROM_STOCK(GTK_STOCK_APPLY);
3306     OBJECT_SET_DATA(filter_apply, E_DFILTER_CM_KEY, filter_cm);
3307     SIGNAL_CONNECT(filter_apply, "clicked", filter_activate_cb, filter_te);
3308     gtk_widget_show(filter_apply);
3309     gtk_toolbar_append_widget(GTK_TOOLBAR(filter_tb), filter_apply, 
3310         "Apply this filter string to the display", "Private");
3311
3312     /* Sets the text entry widget pointer as the E_DILTER_TE_KEY data
3313      * of any widget that ends up calling a callback which needs
3314      * that text entry pointer */
3315     set_menu_object_data("/File/Open...", E_DFILTER_TE_KEY, filter_te);
3316     set_menu_object_data("/Analyze/Display Filters...", E_FILT_TE_PTR_KEY,
3317                          filter_te);
3318     set_menu_object_data("/Analyze/Follow TCP Stream", E_DFILTER_TE_KEY,
3319                          filter_te);
3320     set_menu_object_data("/Analyze/Apply as Filter/Selected", E_DFILTER_TE_KEY,
3321                          filter_te);
3322     set_menu_object_data("/Analyze/Apply as Filter/Not Selected", E_DFILTER_TE_KEY,
3323                          filter_te);
3324     set_menu_object_data("/Analyze/Apply as Filter/... and Selected", E_DFILTER_TE_KEY,
3325                          filter_te);
3326     set_menu_object_data("/Analyze/Apply as Filter/... or Selected", E_DFILTER_TE_KEY,
3327                          filter_te);
3328     set_menu_object_data("/Analyze/Apply as Filter/... and not Selected", E_DFILTER_TE_KEY,
3329                          filter_te);
3330     set_menu_object_data("/Analyze/Apply as Filter/... or not Selected", E_DFILTER_TE_KEY,
3331                          filter_te);
3332     set_menu_object_data("/Analyze/Prepare a Filter/Selected", E_DFILTER_TE_KEY,
3333                          filter_te);
3334     set_menu_object_data("/Analyze/Prepare a Filter/Not Selected", E_DFILTER_TE_KEY,
3335                          filter_te);
3336     set_menu_object_data("/Analyze/Prepare a Filter/... and Selected", E_DFILTER_TE_KEY,
3337                          filter_te);
3338     set_menu_object_data("/Analyze/Prepare a Filter/... or Selected", E_DFILTER_TE_KEY,
3339                          filter_te);
3340     set_menu_object_data("/Analyze/Prepare a Filter/... and not Selected", E_DFILTER_TE_KEY,
3341                          filter_te);
3342     set_menu_object_data("/Analyze/Prepare a Filter/... or not Selected", E_DFILTER_TE_KEY,
3343                          filter_te);
3344     set_toolbar_object_data(E_DFILTER_TE_KEY, filter_te);
3345     OBJECT_SET_DATA(popup_menu_object, E_DFILTER_TE_KEY, filter_te);
3346     OBJECT_SET_DATA(popup_menu_object, E_MPACKET_LIST_KEY, packet_list);
3347
3348     /* info (main) statusbar */
3349     info_bar = info_bar_new();
3350     gtk_widget_show(info_bar);
3351
3352     /* packets statusbar */
3353     packets_bar = packets_bar_new();
3354     gtk_widget_show(packets_bar);
3355
3356     /* Filter/status hbox */
3357     stat_hbox = gtk_hbox_new(FALSE, 1);
3358     gtk_container_border_width(GTK_CONTAINER(stat_hbox), 0);
3359     gtk_widget_show(stat_hbox);
3360
3361     /* Pane for the statusbar */
3362     status_pane = gtk_hpaned_new();
3363     gtk_widget_show(status_pane);
3364
3365     /* Pane for the welcome screen */
3366     welcome_pane = welcome_new();
3367     gtk_widget_show(welcome_pane);
3368 }
3369
3370 static void
3371 show_main_window(gboolean doing_work)
3372 {
3373   main_set_for_capture_file(doing_work);
3374
3375   /*** we have finished all init things, show the main window ***/
3376   gtk_widget_show(top_level);
3377
3378   /* the window can be maximized only, if it's visible, so do it after show! */
3379   main_load_window_geometry(top_level);
3380
3381   /* process all pending GUI events before continue */
3382   while (gtk_events_pending()) gtk_main_iteration();
3383
3384   /* Pop up any queued-up alert boxes. */
3385   display_queued_messages();
3386 }