Move MSWindows console "windows" to ui/win32/console_win32.[ch]
authorAlexis La Goutte <alexis.lagoutte@gmail.com>
Sun, 21 Oct 2012 14:45:23 +0000 (14:45 -0000)
committerAlexis La Goutte <alexis.lagoutte@gmail.com>
Sun, 21 Oct 2012 14:45:23 +0000 (14:45 -0000)
svn path=/trunk/; revision=45677

ui/Makefile.common
ui/gtk/main.c
ui/win32/console_win32.c [new file with mode: 0644]
ui/win32/console_win32.h [new file with mode: 0644]

index 5a8c96d1da60c85132a0dedd854e3c69b74811af..9392302d645b8ca70d86010444c02f620b5601f9 100644 (file)
@@ -53,7 +53,8 @@ WIRESHARK_UI_SRC = \
        help_url.c              \
        ssl_key_export.c        \
        text_import.c           \
        help_url.c              \
        ssl_key_export.c        \
        text_import.c           \
-       util.c
+       util.c                  \
+       win32/console_win32.c
 
 noinst_HEADERS = \
        alert_box.h             \
 
 noinst_HEADERS = \
        alert_box.h             \
@@ -73,4 +74,5 @@ noinst_HEADERS = \
        text_import_scanner.h   \
        ui_util.h               \
        utf8_entities.h         \
        text_import_scanner.h   \
        ui_util.h               \
        utf8_entities.h         \
-       util.h
+       util.h                  \
+       win32/console_win32.h
index 7997cc367ce4efd1abe8bb38d334ee0a01a3f4ce..a37cd96f7bcb9150d0a72df07cf8e911006213a6 100644 (file)
@@ -54,6 +54,7 @@
 #endif
 #include <fcntl.h>
 #include <conio.h>
 #endif
 #include <fcntl.h>
 #include <conio.h>
+#include <ui/win32/console_win32.h>
 #endif
 
 #ifdef HAVE_LIBPORTAUDIO
 #endif
 
 #ifdef HAVE_LIBPORTAUDIO
@@ -93,7 +94,6 @@
 #include "../ringbuffer.h"
 #include "ui/util.h"
 #include "../clopts_common.h"
 #include "../ringbuffer.h"
 #include "ui/util.h"
 #include "../clopts_common.h"
-#include "../console_io.h"
 #include "../cmdarg_err.h"
 #include "../version_info.h"
 #include "../merge.h"
 #include "../cmdarg_err.h"
 #include "../version_info.h"
 #include "../merge.h"
@@ -233,12 +233,6 @@ static gboolean have_capture_file = FALSE; /* XXX - is there an equivalent in cf
 
 static guint  tap_update_timer_id;
 
 
 static guint  tap_update_timer_id;
 
-#ifdef _WIN32
-static gboolean has_console;   /* TRUE if app has console */
-static gboolean console_wait;  /* "Press any key..." */
-static void destroy_console(void);
-static gboolean stdin_capture = FALSE; /* Don't grab stdin & stdout if TRUE */
-#endif
 static void console_log_handler(const char *log_domain,
     GLogLevelFlags log_level, const char *message, gpointer user_data);
 
 static void console_log_handler(const char *log_domain,
     GLogLevelFlags log_level, const char *message, gpointer user_data);
 
@@ -2134,7 +2128,7 @@ check_and_warn_user_startup(gchar *cf_name _U_)
 
 #ifdef _WIN32
   /* Warn the user if npf.sys isn't loaded. */
 
 #ifdef _WIN32
   /* Warn the user if npf.sys isn't loaded. */
-  if (!stdin_capture && !cf_name && !npf_sys_is_running() && recent.privs_warn_if_no_npf && get_os_major_version() >= 6) {
+  if (!get_stdin_capture() && !cf_name && !npf_sys_is_running() && recent.privs_warn_if_no_npf && get_os_major_version() >= 6) {
     priv_warning_dialog = simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
       "The NPF driver isn't running.  You may have trouble\n"
       "capturing or listing interfaces.");
     priv_warning_dialog = simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
       "The NPF driver isn't running.  You may have trouble\n"
       "capturing or listing interfaces.");
@@ -2372,7 +2366,7 @@ main(int argc, char *argv[])
 #ifdef _WIN32
       case 'i':
         if (strcmp(optarg, "-") == 0)
 #ifdef _WIN32
       case 'i':
         if (strcmp(optarg, "-") == 0)
-          stdin_capture = TRUE;
+          set_stdin_capture(TRUE);
         break;
 #endif
       case 'P':        /* Path settings - change these before the Preferences and alike are processed */
         break;
 #endif
       case 'P':        /* Path settings - change these before the Preferences and alike are processed */
@@ -3271,79 +3265,11 @@ WinMain (struct HINSTANCE__ *hInstance,
   /* RichEd20.DLL is needed for filter entries. */
   ws_load_library("riched20.dll");
 
   /* RichEd20.DLL is needed for filter entries. */
   ws_load_library("riched20.dll");
 
-  has_console = FALSE;
-  console_wait = FALSE;
+  set_has_console(FALSE);
+  set_console_wait(FALSE);
   return main (__argc, __argv);
 }
 
   return main (__argc, __argv);
 }
 
-/* The code to create and desstroy console windows should not be necessary,
-   at least as I read the GLib source code, as it looks as if GLib is, on
-   Win32, *supposed* to create a console window into which to display its
-   output.
-
-   That doesn't happen, however.  I suspect there's something completely
-   broken about that code in GLib-for-Win32, and that it may be related
-   to the breakage that forces us to just call "printf()" on the message
-   rather than passing the message on to "g_log_default_handler()"
-   (which is the routine that does the aforementioned non-functional
-   console window creation).  */
-
-/*
- * If this application has no console window to which its standard output
- * would go, create one.
- */
-void
-create_console(void)
-{
-  if (stdin_capture) {
-    /* We've been handed "-i -". Don't mess with stdio. */
-    return;
-  }
-
-  if (!has_console) {
-    /* We have no console to which to print the version string, so
-       create one and make it the standard input, output, and error. */
-
-    /*
-     * See if we have an existing console (i.e. we were run from a
-     * command prompt)
-     */
-    if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
-      if (AllocConsole()) {
-        console_wait = TRUE;
-        SetConsoleTitle(_T("Wireshark Debug Console"));
-      } else {
-        return;   /* couldn't create console */
-      }
-    }
-
-    ws_freopen("CONIN$", "r", stdin);
-    ws_freopen("CONOUT$", "w", stdout);
-    ws_freopen("CONOUT$", "w", stderr);
-    fprintf(stdout, "\n");
-    fprintf(stderr, "\n");
-
-    /* Now register "destroy_console()" as a routine to be called just
-       before the application exits, so that we can destroy the console
-       after the user has typed a key (so that the console doesn't just
-       disappear out from under them, giving the user no chance to see
-       the message(s) we put in there). */
-    atexit(destroy_console);
-
-    /* Well, we have a console now. */
-    has_console = TRUE;
-  }
-}
-
-static void
-destroy_console(void)
-{
-  if (console_wait) {
-    printf("\n\nPress any key to exit\n");
-    _getch();
-  }
-  FreeConsole();
-}
 #endif /* _WIN32 */
 
 
 #endif /* _WIN32 */
 
 
@@ -3374,7 +3300,7 @@ console_log_handler(const char *log_domain, GLogLevelFlags log_level,
     /* the user wants a console or the application will terminate immediately */
     create_console();
   }
     /* the user wants a console or the application will terminate immediately */
     create_console();
   }
-  if (has_console) {
+  if (get_has_console()) {
     /* For some unknown reason, the above doesn't appear to actually cause
        anything to be sent to the standard output, so we'll just splat the
        message out directly, just to make sure it gets out. */
     /* For some unknown reason, the above doesn't appear to actually cause
        anything to be sent to the standard output, so we'll just splat the
        message out directly, just to make sure it gets out. */
diff --git a/ui/win32/console_win32.c b/ui/win32/console_win32.c
new file mode 100644 (file)
index 0000000..02927cb
--- /dev/null
@@ -0,0 +1,157 @@
+/* console_win32.c
+ * Console support for MSWindows
+ *
+ * $Id$
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 2002, Jeffrey C. Foster <jfoste@woodward.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ *
+ */
+
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <glib.h>
+#include <wsutil/file_util.h>
+#include <windows.h>
+#include <tchar.h>
+
+#include "console_win32.h"
+#include "../../console_io.h"
+
+#ifdef _WIN32 /* Needed for console I/O */
+#if _MSC_VER < 1500
+/* AttachConsole() needs this #define! */
+#define _WIN32_WINNT 0x0501
+#endif
+#include <fcntl.h>
+#include <conio.h>
+#endif
+
+#ifdef _WIN32
+static gboolean has_console;   /* TRUE if app has console */
+static gboolean console_wait;  /* "Press any key..." */
+static gboolean stdin_capture = FALSE; /* Don't grab stdin & stdout if TRUE */
+#endif
+
+#ifdef _WIN32
+/* The code to create and desstroy console windows should not be necessary,
+   at least as I read the GLib source code, as it looks as if GLib is, on
+   Win32, *supposed* to create a console window into which to display its
+   output.
+
+   That doesn't happen, however.  I suspect there's something completely
+   broken about that code in GLib-for-Win32, and that it may be related
+   to the breakage that forces us to just call "printf()" on the message
+   rather than passing the message on to "g_log_default_handler()"
+   (which is the routine that does the aforementioned non-functional
+   console window creation).  */
+
+/*
+ * If this application has no console window to which its standard output
+ * would go, create one.
+ */
+void
+create_console(void)
+{
+  if (stdin_capture) {
+    /* We've been handed "-i -". Don't mess with stdio. */
+    return;
+  }
+
+  if (!has_console) {
+    /* We have no console to which to print the version string, so
+       create one and make it the standard input, output, and error. */
+
+    /*
+     * See if we have an existing console (i.e. we were run from a
+     * command prompt)
+     */
+    if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
+      if (AllocConsole()) {
+        console_wait = TRUE;
+        SetConsoleTitle(_T("Wireshark Debug Console"));
+      } else {
+        return;   /* couldn't create console */
+      }
+    }
+
+    ws_freopen("CONIN$", "r", stdin);
+    ws_freopen("CONOUT$", "w", stdout);
+    ws_freopen("CONOUT$", "w", stderr);
+    fprintf(stdout, "\n");
+    fprintf(stderr, "\n");
+
+    /* Now register "destroy_console()" as a routine to be called just
+       before the application exits, so that we can destroy the console
+       after the user has typed a key (so that the console doesn't just
+       disappear out from under them, giving the user no chance to see
+       the message(s) we put in there). */
+    atexit(destroy_console);
+
+    /* Well, we have a console now. */
+    has_console = TRUE;
+  }
+}
+
+void
+destroy_console(void)
+{
+  if (console_wait) {
+    printf("\n\nPress any key to exit\n");
+    _getch();
+  }
+  FreeConsole();
+}
+
+void
+set_console_wait(gboolean set_console_wait)
+{
+       console_wait = set_console_wait;
+}
+gboolean
+get_console_wait(void)
+{
+       return console_wait;
+}
+
+void
+set_has_console(gboolean set_has_console)
+{
+       has_console = has_console;
+}
+gboolean
+get_has_console(void)
+{
+       return has_console;
+}
+
+void
+set_stdin_capture(gboolean set_stdin_capture)
+{
+       stdin_capture = set_stdin_capture;
+}
+gboolean
+get_stdin_capture(void)
+{
+       return stdin_capture;
+}
+#endif /* _WIN32 */
\ No newline at end of file
diff --git a/ui/win32/console_win32.h b/ui/win32/console_win32.h
new file mode 100644 (file)
index 0000000..74f8c33
--- /dev/null
@@ -0,0 +1,79 @@
+/* console_win32.h
+ * Console support for MSWindows
+ *
+ * $Id$
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 2002, Jeffrey C. Foster <jfoste@woodward.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef __CONSOLE_WIN32_H__
+#define __CONSOLE_WIN32_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#ifdef _WIN32
+
+/** @file
+ * Win32 specific console.
+ */
+
+/** Create Windows console.
+ *
+ */
+void create_console(void);
+
+/** Destroy Windows console.
+ *
+ */
+void destroy_console(void);
+
+/** Set console wait.
+ * @param console_wait set/no set console wait
+ */
+void set_console_wait(gboolean console_wait);
+/** get console wait
+ * @return set/no set console wait
+ */
+gboolean get_console_wait(void);
+
+/** Set has console.
+ * @param has_console set/no set has_console
+ */
+void set_has_console(gboolean has_console);
+
+gboolean get_has_console(void);
+
+/** Set stdin capture.
+ * @param console_wait set/no stdin_capture
+ */
+void set_stdin_capture(gboolean set_stdin_capture);
+
+/** get stdin caputre
+ * @return set/no set stdin_capture
+ */
+gboolean get_stdin_capture(void);
+#endif/* _WIN32 */
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __CONSOLE_WIN32_H__ */