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