aboutsummaryrefslogtreecommitdiffstats
path: root/ui/win32/console_win32.c
blob: 02927cb3b7cb893d66f5ff724932a25298544414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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 */