Fix a comment typo
[privoxy.git] / win32.c
1 /*********************************************************************
2  *
3  * File        :  $Source: /cvsroot/ijbswa/current/win32.c,v $
4  *
5  * Purpose     :  Win32 User Interface initialization and message loop
6  *
7  * Copyright   :  Written by and Copyright (C) 2001-2002 members of
8  *                the Privoxy team.  https://www.privoxy.org/
9  *
10  *                Written by and Copyright (C) 1999 Adam Lock
11  *                <locka@iol.ie>
12  *
13  *                This program is free software; you can redistribute it
14  *                and/or modify it under the terms of the GNU General
15  *                Public License as published by the Free Software
16  *                Foundation; either version 2 of the License, or (at
17  *                your option) any later version.
18  *
19  *                This program is distributed in the hope that it will
20  *                be useful, but WITHOUT ANY WARRANTY; without even the
21  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
22  *                PARTICULAR PURPOSE.  See the GNU General Public
23  *                License for more details.
24  *
25  *                The GNU General Public License should be included with
26  *                this file.  If not, you can view it at
27  *                http://www.gnu.org/copyleft/gpl.html
28  *                or write to the Free Software Foundation, Inc., 59
29  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
30  *
31  *********************************************************************/
32
33
34 #include "config.h"
35
36 #ifdef _WIN32
37
38 #include <stdio.h>
39
40 #include "project.h"
41 #include "jcc.h"
42 #include "miscutil.h"
43
44 /* Uncomment this if you want to build Win32 as a console app */
45 /* #define _WIN_CONSOLE */
46
47 #ifndef STRICT
48 #define STRICT
49 #endif
50 #include <windows.h>
51
52 #include <stdarg.h>
53 #include <process.h>
54
55 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
56 /* Visual C++ Heap debugging */
57 #include <crtdbg.h>
58 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
59
60 #include "win32.h"
61
62 /**
63  * A short introductory text about Privoxy.  Used for the "About" box
64  * or the console startup message.
65  */
66 const char win32_blurb[] =
67 "Privoxy version " VERSION " for Windows\n"
68 "Copyright (C) 2000-2019 the Privoxy Team (" HOME_PAGE_URL ")\n"
69 "Based on the Internet Junkbuster by Junkbusters Corp.\n"
70 "This is free software; it may be used and copied under the\n"
71 "GNU General Public License, version 2: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html\n"
72 "This program comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\n";
73
74 #ifdef _WIN_CONSOLE
75
76 /**
77  * Hide the console.  If set, the program will disconnect from the
78  * console and run in the background.  This allows the command-prompt
79  * window to close.
80  */
81 int hideConsole     = 0;
82
83
84 #else /* ndef _WIN_CONSOLE */
85
86
87 /**
88  * The application instance handle.
89  */
90 HINSTANCE g_hInstance;
91
92
93 /**
94  * The command to show the window that was specified at startup.
95  */
96 int g_nCmdShow;
97
98 static void  __cdecl UserInterfaceThread(void *);
99
100
101 #endif /* ndef _WIN_CONSOLE */
102
103 /*********************************************************************
104  *
105  * Function    :  WinMain
106  *
107  * Description :  M$ Windows "main" routine:
108  *                parse the `lpCmdLine' param into main's argc and argv variables,
109  *                start the user interface thread (for the systray window), and
110  *                call main (i.e. patch execution into normal startup).
111  *
112  * Parameters  :
113  *          1  :  hInstance = instance handle of this execution
114  *          2  :  hPrevInstance = instance handle of previous execution
115  *          3  :  lpCmdLine = command line string which started us
116  *          4  :  nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
117  *
118  * Returns     :  `main' never returns, so WinMain will also never return.
119  *
120  *********************************************************************/
121 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
122 {
123 #if 0   /* See comment about __argc & __argv below */
124    int i;
125    int argc = 1;
126    const char *argv[3];
127    char szModule[MAX_PATH+1];
128 #endif
129
130    int res;
131 #ifndef _WIN_CONSOLE
132    HANDLE hInitCompleteEvent = NULL;
133 #endif
134
135
136 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
137 #if 0
138    /* Visual C++ Heap debugging */
139
140    /* Get current flag*/
141    int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
142
143    /* Turn on leak-checking bit */
144    tmpFlag |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF;
145
146    /* Turn off CRT block checking bit */
147    tmpFlag &= ~(_CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
148
149    /* Set flag to the new value */
150    _CrtSetDbgFlag(tmpFlag);
151 #endif
152 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
153
154
155 /************
156  * I couldn't figure out why the command line was being sorta parsed here
157  * instead of using the __argc & __argv globals usually defined in stdlib.h
158  *
159  * From what I can tell by looking at the MinWG source, it supports these
160  * globals, so i'd hope that the other compilers do so as well.
161  * Obviously, if i'm wrong i'll find out soon enough!  :)
162  ************/
163 #if 0
164    /*
165     * Cheat in parsing the command line.  We only ever have at most one
166     * parameter, which may optionally be specified inside double quotes.
167     */
168
169    if (lpCmdLine != NULL)
170    {
171       /* Make writable copy */
172       lpCmdLine = strdup(lpCmdLine);
173    }
174    if (lpCmdLine != NULL)
175    {
176       chomp(lpCmdLine);
177       i = strlen(lpCmdLine);
178       if ((i >= 2) && (lpCmdLine[0] == '\"') && (lpCmdLine[i - 1] == '\"'))
179       {
180          lpCmdLine[i - 1] = '\0';
181          lpCmdLine++;
182       }
183       if (lpCmdLine[0] == '\0')
184       {
185          lpCmdLine = NULL;
186       }
187    }
188
189    GetModuleFileName(hInstance, szModule, MAX_PATH);
190    argv[0] = szModule;
191    argv[1] = lpCmdLine;
192    argv[2] = NULL;
193    argc = ((lpCmdLine != NULL) ? 2 : 1);
194 #endif /* -END- 0 */
195
196
197 #ifndef _WIN_CONSOLE
198    /* Create a user-interface thread and wait for it to initialise */
199    hInitCompleteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
200    g_hInstance = hInstance;
201    g_nCmdShow = nCmdShow;
202    _beginthread(UserInterfaceThread, 0, &hInitCompleteEvent);
203    WaitForSingleObject(hInitCompleteEvent, INFINITE);
204    CloseHandle(hInitCompleteEvent);
205 #endif
206
207 #ifdef __MINGW32__
208    res = real_main(__argc, __argv);
209 #else
210    res = main(__argc, __argv);
211 #endif
212
213    return res;
214
215 }
216
217 #endif
218
219 /*********************************************************************
220  *
221  * Function    :  InitWin32
222  *
223  * Description :  Initialise windows, setting up the console or windows as appropriate.
224  *
225  * Parameters  :  None
226  *
227  * Returns     :  N/A
228  *
229  *********************************************************************/
230 void InitWin32(void)
231 {
232    WORD wVersionRequested;
233    WSADATA wsaData;
234
235 #ifdef _WIN_CONSOLE
236    SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
237    if (hideConsole)
238    {
239       FreeConsole();
240    }
241 #endif
242    wVersionRequested = MAKEWORD(2, 0);
243    if (WSAStartup(wVersionRequested, &wsaData) != 0)
244    {
245 #ifndef _WIN_CONSOLE
246       MessageBox(NULL, "Cannot initialize WinSock library", "Privoxy Error",
247          MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
248 #endif
249       exit(1);
250    }
251
252 }
253
254
255 #ifndef _WIN_CONSOLE
256 #include <signal.h>
257 #include <assert.h>
258
259 #include "win32.h"
260 #include "w32log.h"
261
262
263 /*********************************************************************
264  *
265  * Function    :  UserInterfaceThread
266  *
267  * Description :  User interface thread.  WinMain will wait for us to set
268  *                the hInitCompleteEvent before patching over to `main'.
269  *                This ensures the systray window is active before beginning
270  *                operations.
271  *
272  * Parameters  :
273  *          1  :  pData = pointer to `hInitCompleteEvent'.
274  *
275  * Returns     :  N/A
276  *
277  *********************************************************************/
278 static void __cdecl UserInterfaceThread(void *pData)
279 {
280    MSG msg;
281    HANDLE hInitCompleteEvent = *((HANDLE *) pData);
282
283    /* Initialise */
284    InitLogWindow();
285    SetEvent(hInitCompleteEvent);
286
287    /* Enter a message processing loop */
288    while (GetMessage(&msg, (HWND) NULL, 0, 0))
289    {
290       TranslateMessage(&msg);
291       DispatchMessage(&msg);
292    }
293
294    /* Cleanup */
295    TermLogWindow();
296
297    /* Time to die... */
298    exit(0);
299
300 }
301
302
303 #endif /* ndef _WIN_CONSOLE */
304
305
306 /*
307   Local Variables:
308   tab-width: 3
309   end:
310 */