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