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