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