Moving three variable declarations to jcc.c from project.h
[privoxy.git] / src / win32.c
1 const char win32_rcs[] = "$Id: win32.c,v 1.9 2002/03/31 17:19:00 jongfoster Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/win32.c,v $
5  *
6  * Purpose     :  Win32 User Interface initialization and message loop
7  *
8  * Copyright   :  Written by and Copyright (C) 2001-2002 members of
9  *                the Privoxy team.  http://www.privoxy.org/
10  *
11  *                Written by and Copyright (C) 1999 Adam Lock
12  *                <locka@iol.ie>
13  *
14  *                This program is free software; you can redistribute it 
15  *                and/or modify it under the terms of the GNU General
16  *                Public License as published by the Free Software
17  *                Foundation; either version 2 of the License, or (at
18  *                your option) any later version.
19  *
20  *                This program is distributed in the hope that it will
21  *                be useful, but WITHOUT ANY WARRANTY; without even the
22  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
23  *                PARTICULAR PURPOSE.  See the GNU General Public
24  *                License for more details.
25  *
26  *                The GNU General Public License should be included with
27  *                this file.  If not, you can view it at
28  *                http://www.gnu.org/copyleft/gpl.html
29  *                or write to the Free Software Foundation, Inc., 59
30  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
31  *
32  * Revisions   :
33  *    $Log: win32.c,v $
34  *    Revision 1.9  2002/03/31 17:19:00  jongfoster
35  *    Win32 only: Enabling STRICT to fix a VC++ compile warning.
36  *
37  *    Revision 1.8  2002/03/26 22:57:10  jongfoster
38  *    Web server name should begin www.
39  *
40  *    Revision 1.7  2002/03/24 12:03:47  jongfoster
41  *    Name change
42  *
43  *    Revision 1.6  2002/03/16 21:53:28  jongfoster
44  *    VC++ Heap debug option
45  *
46  *    Revision 1.5  2002/03/04 23:47:30  jongfoster
47  *    - Rewritten, simpler command-line pre-parser
48  *    - not using raise(SIGINT) any more
49  *
50  *    Revision 1.4  2001/11/30 21:29:33  jongfoster
51  *    Fixing a warning
52  *
53  *    Revision 1.3  2001/11/16 00:46:31  jongfoster
54  *    Fixing compiler warnings
55  *
56  *    Revision 1.2  2001/07/29 19:32:00  jongfoster
57  *    Renaming _main() [mingw32 only] to real_main(), for ANSI compliance.
58  *
59  *    Revision 1.1.1.1  2001/05/15 13:59:08  oes
60  *    Initial import of version 2.9.3 source tree
61  *
62  *
63  *********************************************************************/
64 \f
65
66 #include "config.h"
67
68 #ifdef _WIN32
69
70 #include <stdio.h>
71
72 #include "project.h"
73 #include "jcc.h"
74 #include "miscutil.h"
75
76 /* Uncomment this if you want to build Win32 as a console app */
77 /* #define _WIN_CONSOLE */
78
79 #ifndef STRICT
80 #define STRICT
81 #endif
82 #include <windows.h>
83
84 #include <stdarg.h>
85 #include <process.h>
86
87 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
88 /* Visual C++ Heap debugging */
89 #include <crtdbg.h>
90 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
91
92 #include "win32.h"
93
94 const char win32_h_rcs[] = WIN32_H_VERSION;
95
96 const char win32_blurb[] =
97 "Privoxy version " VERSION " for Windows\n"
98 "Copyright (C) 2000-2002 by members of the Privoxy Team\n"
99 "Copyright (C) 1997-8 by Junkbusters Corp.\n"
100 "This is free software; it may be used and copied under the\n"
101 "GNU General Public License: http://www.gnu.org/copyleft/gpl.html .\n"
102 "This program comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\n"
103 "\n"
104 "For information about how to to configure the proxy and your browser, see\n"
105 "        " HOME_PAGE_URL "\n"
106 "\n";
107
108 #ifdef _WIN_CONSOLE
109
110 int hideConsole     = 0;
111
112 #else
113
114 HINSTANCE g_hInstance;
115 int g_nCmdShow;
116
117 static void  __cdecl UserInterfaceThread(void *);
118
119 #endif
120
121
122 /*********************************************************************
123  *
124  * Function    :  WinMain
125  *
126  * Description :  M$ Windows "main" routine:
127  *                parse the `lpCmdLine' param into main's argc and argv variables,
128  *                start the user interface thread (for the systray window), and
129  *                call main (i.e. patch execution into normal startup).
130  *
131  * Parameters  :
132  *          1  :  hInstance = instance handle of this execution
133  *          2  :  hPrevInstance = instance handle of previous execution
134  *          3  :  lpCmdLine = command line string which started us
135  *          4  :  nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
136  *
137  * Returns     :  `main' never returns, so WinMain will also never return.
138  *
139  *********************************************************************/
140 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
141 {
142    int i;
143    int res;
144    int argc = 1;
145    const char *argv[3];
146    char szModule[MAX_PATH+1];
147 #ifndef _WIN_CONSOLE
148    HANDLE hInitCompleteEvent = NULL;
149 #endif
150
151
152 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
153 #if 0
154    /* Visual C++ Heap debugging */
155
156    /* Get current flag*/
157    int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
158
159    /* Turn on leak-checking bit */
160    tmpFlag |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF;
161
162    /* Turn off CRT block checking bit */
163    tmpFlag &= ~(_CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
164
165    /* Set flag to the new value */
166    _CrtSetDbgFlag( tmpFlag );
167 #endif
168 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
169
170    /*
171     * Cheat in parsing the command line.  We only ever have at most one
172     * paramater, which may optionally be specified inside double quotes.
173     */
174
175    if (lpCmdLine != NULL)
176    {
177       /* Make writable copy */
178       lpCmdLine = strdup(lpCmdLine);
179    }
180    if (lpCmdLine != NULL)
181    {
182       chomp(lpCmdLine);
183       i = strlen(lpCmdLine);
184       if ((i >= 2) && (lpCmdLine[0] == '\"') && (lpCmdLine[i - 1] == '\"'))
185       {
186          lpCmdLine[i - 1] = '\0';
187          lpCmdLine++;
188       }
189       if (lpCmdLine[0] == '\0')
190       {
191          lpCmdLine = NULL;
192       }
193    }
194
195    GetModuleFileName(hInstance, szModule, MAX_PATH);
196    argv[0] = szModule;
197    argv[1] = lpCmdLine;
198    argv[2] = NULL;
199    argc = ((lpCmdLine != NULL) ? 2 : 1);
200
201 #ifndef _WIN_CONSOLE
202    /* Create a user-interface thread and wait for it to initialise */
203    hInitCompleteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
204    g_hInstance = hInstance;
205    g_nCmdShow = nCmdShow;
206    _beginthread(UserInterfaceThread, 0, &hInitCompleteEvent);
207    WaitForSingleObject(hInitCompleteEvent, INFINITE);
208    DeleteObject(hInitCompleteEvent);
209 #endif
210
211 #ifdef __MINGW32__
212    res = real_main( argc, argv );
213 #else
214    res = main( argc, argv );
215 #endif
216
217    return res;
218
219 }
220
221 #endif
222
223 /*********************************************************************
224  *
225  * Function    :  InitWin32
226  *
227  * Description :  Initialise windows, setting up the console or windows as appropriate.
228  *
229  * Parameters  :  None
230  *
231  * Returns     :  N/A
232  *
233  *********************************************************************/
234 void InitWin32(void)
235 {
236    WORD wVersionRequested;
237    WSADATA wsaData;
238
239 #ifdef _WIN_CONSOLE
240    SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
241    if (hideConsole)
242    {
243       FreeConsole();
244    }
245 #endif
246    wVersionRequested = MAKEWORD(2, 0);
247    if (WSAStartup(wVersionRequested, &wsaData) != 0)
248    {
249 #ifndef _WIN_CONSOLE
250       MessageBox(NULL, "Cannot initialize WinSock library", "Privoxy Error", 
251          MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);  
252 #endif
253       exit(1);
254    }
255
256 }
257
258
259 #ifndef _WIN_CONSOLE
260 #include <signal.h>
261 #include <assert.h>
262
263 #include "win32.h"
264 #include "w32log.h"
265
266
267 /*********************************************************************
268  *
269  * Function    :  UserInterfaceThread
270  *
271  * Description :  User interface thread.  WinMain will wait for us to set
272  *                the hInitCompleteEvent before patching over to `main'.
273  *                This ensures the systray window is active before beginning
274  *                operations.
275  *
276  * Parameters  :
277  *          1  :  pData = pointer to `hInitCompleteEvent'.
278  *
279  * Returns     :  N/A
280  *
281  *********************************************************************/
282 static void __cdecl UserInterfaceThread(void *pData)
283 {
284    MSG msg;
285    HANDLE hInitCompleteEvent = *((HANDLE *) pData);
286
287    /* Initialise */
288    InitLogWindow();
289    SetEvent(hInitCompleteEvent);
290
291    /* Enter a message processing loop */
292    while (GetMessage(&msg, (HWND) NULL, 0, 0))
293    {
294       TranslateMessage(&msg);
295       DispatchMessage(&msg);
296    }
297
298    /* Cleanup */
299    TermLogWindow();
300
301    /* Time to die... */
302    exit(0);
303
304 }
305
306
307 #endif
308
309
310 /*
311   Local Variables:
312   tab-width: 3
313   end:
314 */