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