Downgrade "Received x bytes while expecting y." message to
[privoxy.git] / win32.c
1 const char win32_rcs[] = "$Id: win32.c,v 1.13 2007/01/31 16:25:24 fabiankeil 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.13  2007/01/31 16:25:24  fabiankeil
35  *    Update copyright range for the About message.
36  *
37  *    Revision 1.12  2006/08/12 03:54:37  david__schmidt
38  *    Windows service integration
39  *
40  *    Revision 1.11  2006/07/18 14:48:48  david__schmidt
41  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
42  *    with what was really the latest development (the v_3_0_branch branch)
43  *
44  *    Revision 1.9.2.2  2002/08/27 18:03:40  oes
45  *    Fixed stupid typo
46  *
47  *    Revision 1.9.2.1  2002/08/21 17:59:27  oes
48  *    Sync win32_blurb[] with our standard blurb
49  *
50  *    Revision 1.9  2002/03/31 17:19:00  jongfoster
51  *    Win32 only: Enabling STRICT to fix a VC++ compile warning.
52  *
53  *    Revision 1.8  2002/03/26 22:57:10  jongfoster
54  *    Web server name should begin www.
55  *
56  *    Revision 1.7  2002/03/24 12:03:47  jongfoster
57  *    Name change
58  *
59  *    Revision 1.6  2002/03/16 21:53:28  jongfoster
60  *    VC++ Heap debug option
61  *
62  *    Revision 1.5  2002/03/04 23:47:30  jongfoster
63  *    - Rewritten, simpler command-line pre-parser
64  *    - not using raise(SIGINT) any more
65  *
66  *    Revision 1.4  2001/11/30 21:29:33  jongfoster
67  *    Fixing a warning
68  *
69  *    Revision 1.3  2001/11/16 00:46:31  jongfoster
70  *    Fixing compiler warnings
71  *
72  *    Revision 1.2  2001/07/29 19:32:00  jongfoster
73  *    Renaming _main() [mingw32 only] to real_main(), for ANSI compliance.
74  *
75  *    Revision 1.1.1.1  2001/05/15 13:59:08  oes
76  *    Initial import of version 2.9.3 source tree
77  *
78  *
79  *********************************************************************/
80 \f
81
82 #include "config.h"
83
84 #ifdef _WIN32
85
86 #include <stdio.h>
87
88 #include "project.h"
89 #include "jcc.h"
90 #include "miscutil.h"
91
92 /* Uncomment this if you want to build Win32 as a console app */
93 /* #define _WIN_CONSOLE */
94
95 #ifndef STRICT
96 #define STRICT
97 #endif
98 #include <windows.h>
99
100 #include <stdarg.h>
101 #include <process.h>
102
103 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
104 /* Visual C++ Heap debugging */
105 #include <crtdbg.h>
106 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
107
108 #include "win32.h"
109
110 const char win32_h_rcs[] = WIN32_H_VERSION;
111
112 /**
113  * A short introductory text about Privoxy.  Used for the "About" box
114  * or the console startup message.
115  */
116 const char win32_blurb[] =
117 "Privoxy version " VERSION " for Windows\n"
118 "Copyright (C) 2000-2008 the Privoxy Team (" HOME_PAGE_URL ")\n"
119 "Based on the Internet Junkbuster by Junkbusters Corp.\n"
120 "This is free software; it may be used and copied under the\n"
121 "GNU General Public License: http://www.gnu.org/copyleft/gpl.html .\n"
122 "This program comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\n";
123
124 #ifdef _WIN_CONSOLE
125
126 /**
127  * Hide the console.  If set, the program will disconnect from the 
128  * console and run in the background.  This allows the command-prompt
129  * window to close.
130  */
131 int hideConsole     = 0;
132
133
134 #else /* ndef _WIN_CONSOLE */
135
136
137 /**
138  * The application instance handle.
139  */
140 HINSTANCE g_hInstance;
141
142
143 /**
144  * The command to show the window that was specified at startup.
145  */
146 int g_nCmdShow;
147
148 static void  __cdecl UserInterfaceThread(void *);
149
150
151 #endif /* ndef _WIN_CONSOLE */
152
153 /*********************************************************************
154  *
155  * Function    :  WinMain
156  *
157  * Description :  M$ Windows "main" routine:
158  *                parse the `lpCmdLine' param into main's argc and argv variables,
159  *                start the user interface thread (for the systray window), and
160  *                call main (i.e. patch execution into normal startup).
161  *
162  * Parameters  :
163  *          1  :  hInstance = instance handle of this execution
164  *          2  :  hPrevInstance = instance handle of previous execution
165  *          3  :  lpCmdLine = command line string which started us
166  *          4  :  nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
167  *
168  * Returns     :  `main' never returns, so WinMain will also never return.
169  *
170  *********************************************************************/
171 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
172 {
173 #if 0   /* See comment about __argc & __argv below */
174    int i;
175    int argc = 1;
176    const char *argv[3];
177    char szModule[MAX_PATH+1];
178 #endif
179
180    int res;
181 #ifndef _WIN_CONSOLE
182    HANDLE hInitCompleteEvent = NULL;
183 #endif
184
185
186 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
187 #if 0
188    /* Visual C++ Heap debugging */
189
190    /* Get current flag*/
191    int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
192
193    /* Turn on leak-checking bit */
194    tmpFlag |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF;
195
196    /* Turn off CRT block checking bit */
197    tmpFlag &= ~(_CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
198
199    /* Set flag to the new value */
200    _CrtSetDbgFlag( tmpFlag );
201 #endif
202 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
203
204
205 /************
206  * I couldn't figure out why the command line was being sorta parsed here
207  * instead of using the __argc & __argv globals usually defined in stdlib.h
208  *
209  * From what I can tell by looking at the MinWG source, it supports these
210  * globals, so i'd hope that the other compilers do so as well.
211  * Obviously, if i'm wrong i'll find out soon enough!  :)
212  ************/
213 #if 0
214    /*
215     * Cheat in parsing the command line.  We only ever have at most one
216     * paramater, which may optionally be specified inside double quotes.
217     */
218
219    if (lpCmdLine != NULL)
220    {
221       /* Make writable copy */
222       lpCmdLine = strdup(lpCmdLine);
223    }
224    if (lpCmdLine != NULL)
225    {
226       chomp(lpCmdLine);
227       i = strlen(lpCmdLine);
228       if ((i >= 2) && (lpCmdLine[0] == '\"') && (lpCmdLine[i - 1] == '\"'))
229       {
230          lpCmdLine[i - 1] = '\0';
231          lpCmdLine++;
232       }
233       if (lpCmdLine[0] == '\0')
234       {
235          lpCmdLine = NULL;
236       }
237    }
238
239    GetModuleFileName(hInstance, szModule, MAX_PATH);
240    argv[0] = szModule;
241    argv[1] = lpCmdLine;
242    argv[2] = NULL;
243    argc = ((lpCmdLine != NULL) ? 2 : 1);
244 #endif /* -END- 0 */
245
246
247 #ifndef _WIN_CONSOLE
248    /* Create a user-interface thread and wait for it to initialise */
249    hInitCompleteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
250    g_hInstance = hInstance;
251    g_nCmdShow = nCmdShow;
252    _beginthread(UserInterfaceThread, 0, &hInitCompleteEvent);
253    WaitForSingleObject(hInitCompleteEvent, INFINITE);
254    DeleteObject(hInitCompleteEvent);
255 #endif
256
257 #ifdef __MINGW32__
258    res = real_main( __argc, __argv );
259 #else
260    res = main( __argc, __argv );
261 #endif
262
263    return res;
264
265 }
266
267 #endif
268
269 /*********************************************************************
270  *
271  * Function    :  InitWin32
272  *
273  * Description :  Initialise windows, setting up the console or windows as appropriate.
274  *
275  * Parameters  :  None
276  *
277  * Returns     :  N/A
278  *
279  *********************************************************************/
280 void InitWin32(void)
281 {
282    WORD wVersionRequested;
283    WSADATA wsaData;
284
285 #ifdef _WIN_CONSOLE
286    SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
287    if (hideConsole)
288    {
289       FreeConsole();
290    }
291 #endif
292    wVersionRequested = MAKEWORD(2, 0);
293    if (WSAStartup(wVersionRequested, &wsaData) != 0)
294    {
295 #ifndef _WIN_CONSOLE
296       MessageBox(NULL, "Cannot initialize WinSock library", "Privoxy Error", 
297          MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);  
298 #endif
299       exit(1);
300    }
301
302 }
303
304
305 #ifndef _WIN_CONSOLE
306 #include <signal.h>
307 #include <assert.h>
308
309 #include "win32.h"
310 #include "w32log.h"
311
312
313 /*********************************************************************
314  *
315  * Function    :  UserInterfaceThread
316  *
317  * Description :  User interface thread.  WinMain will wait for us to set
318  *                the hInitCompleteEvent before patching over to `main'.
319  *                This ensures the systray window is active before beginning
320  *                operations.
321  *
322  * Parameters  :
323  *          1  :  pData = pointer to `hInitCompleteEvent'.
324  *
325  * Returns     :  N/A
326  *
327  *********************************************************************/
328 static void __cdecl UserInterfaceThread(void *pData)
329 {
330    MSG msg;
331    HANDLE hInitCompleteEvent = *((HANDLE *) pData);
332
333    /* Initialise */
334    InitLogWindow();
335    SetEvent(hInitCompleteEvent);
336
337    /* Enter a message processing loop */
338    while (GetMessage(&msg, (HWND) NULL, 0, 0))
339    {
340       TranslateMessage(&msg);
341       DispatchMessage(&msg);
342    }
343
344    /* Cleanup */
345    TermLogWindow();
346
347    /* Time to die... */
348    exit(0);
349
350 }
351
352
353 #endif /* ndef _WIN_CONSOLE */
354
355
356 /*
357   Local Variables:
358   tab-width: 3
359   end:
360 */