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