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