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