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