home *** CD-ROM | disk | FTP | other *** search
- char *win32_rcs = "$Id: win32.c,v 1.11 1998/02/06 00:23:52 ACJC Exp $";
- /* Written and copyright 1997 Anonymous Coders and Junkbusters Corporation.
- * Distributed under the GNU General Public License; see the README file.
- * This code comes with NO WARRANTY. http://www.junkbusters.com/ht/en/gpl.html
- */
- /* Win32 User Interface enchancements - Copyright 1999 Adam Lock <locka@iol.ie> */
- #ifdef _WIN32
-
- #include <stdio.h>
- #ifdef REGEX
- #include "gnu_regex.h"
- #endif
- #include "jcc.h"
-
- /* Uncomment this if you want to build Win32 as a console app */
- /* #define _WIN_CONSOLE */
-
- #include <windows.h>
-
- #include <stdarg.h>
- #include <process.h>
-
- char *win32_blurb =
- "Internet Junkbuster Proxy(TM) Version " VERSION " for Windows is Copyright (C) 1997-8\n"
- "by Junkbusters Corp. This is free software; it may be used and copied under\n"
- "the GNU General Public License: http://www.junkbusters.com/ht/en/gpl.html .\n"
- "This program comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\n"
- "\n"
- "For information about how to to configure the proxy and your browser, see\n"
- " http://www.junkbusters.com/ht/en/ijbwin.html#v" VERSION_MAJOR "\n"
- "\n"
- "The Internet Junkbuster Proxy(TM) is running and ready to serve!\n"
- ;
-
- #ifdef _WIN_CONSOLE
- extern int hideConsole;
- #else
-
- HINSTANCE g_hInstance;
- int g_nCmdShow;
-
- static void __cdecl UserInterfaceThread(void *);
-
- #endif
-
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- int argc = 0;
- int i;
- int res;
- char **argv = NULL;
- char *pszArgs = NULL;
- char *pszLastTok;
- char szModule[MAX_PATH+1];
- #ifndef _WIN_CONSOLE
- HANDLE hInitCompleteEvent = NULL;
- #endif
-
- /* Split command line into arguments */
- pszArgs = malloc(strlen(lpCmdLine) + 1);
- strcpy(pszArgs, lpCmdLine);
-
- GetModuleFileName(hInstance, szModule, MAX_PATH);
-
- /* Count number of spaces */
- argc = 1;
- if (strlen(pszArgs) > 0)
- {
- pszLastTok = pszArgs;
- do {
- argc++;
- pszLastTok = strchr(pszLastTok, ' ');
- } while (pszLastTok);
- }
-
- /* Allocate array of strings */
- argv = malloc(sizeof(char *) * argc);
-
- /* step through command line replacing spaces with zeros, initialise array */
- argv[0] = szModule;
- i = 1;
- pszLastTok = pszArgs;
- do {
- argv[i] = pszLastTok;
- pszLastTok = strchr(pszLastTok, ' ');
- if (pszLastTok) {
- while (*pszLastTok != '\0' && *pszLastTok == ' ') {
- *pszLastTok = '\0';
- pszLastTok++;
- }
- }
- i++;
- } while (pszLastTok && *pszLastTok != '\0');
-
- #ifndef _WIN_CONSOLE
- /* Create a user-interface thread and wait for it to initialise */
- hInitCompleteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- g_hInstance = hInstance;
- g_nCmdShow = nCmdShow;
- _beginthread(UserInterfaceThread, 0, &hInitCompleteEvent);
- WaitForSingleObject(hInitCompleteEvent, INFINITE);
- DeleteObject(hInitCompleteEvent);
- #endif
-
- res = main(argc, argv);
-
- /* Cleanup */
- free(argv);
- free(pszArgs);
-
- return res;
- }
-
- #endif
-
- /*
- * Initialise windows, setting up the console or windows as appropriate
- */
- void InitWin32()
- {
- WORD wVersionRequested;
- WSADATA wsaData;
-
- #ifdef _WIN_CONSOLE
- SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
- if (hideConsole) {
- FreeConsole();
- }
- #endif
- wVersionRequested = MAKEWORD(2, 0);
- if (WSAStartup(wVersionRequested, &wsaData) != 0) {
- exit(1);
- }
- }
-
- #ifndef _WIN_CONSOLE
- #include <signal.h>
- #include <assert.h>
-
- #include "w32log.h"
-
- /*
- * User interface thread
- */
- void __cdecl UserInterfaceThread(void *pData)
- {
- MSG msg;
- HANDLE hInitCompleteEvent = *((HANDLE *) pData);
-
- /* Initialise */
- InitLogWindow();
- SetEvent(hInitCompleteEvent);
-
- /* Enter a message processing loop */
- while (GetMessage(&msg, (HWND) NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
-
- /* Cleanup */
- TermLogWindow();
-
- /* Time to die... */
- raise(SIGINT);
- }
-
- #undef fputs
- #undef fprintf
- #undef fwrite
-
- int w32_fputs(const char *string, FILE *stream)
- {
- int ret;
- if (stream != logfp && stream != stdout)
- {
- return fputs(string, stream);
- }
-
- #ifdef _WIN_CONSOLE
- ret = fputs(string, stream);
- #else
- ret = LogPutString(string);
- #endif
-
- return ret;
- }
-
- int w32_fwrite(const void *buffer, size_t size, size_t count, FILE *stream)
- {
- char *buf;
- if (stream != logfp && stream != stdout)
- {
- return fwrite(buffer, size, count, stream);
- }
- buf = strdup(buffer);
- w32_fputs(buf, stream);
- free(buf);
- return 1;
- }
-
- int w32_fprintf( FILE *stream, const char *format, ...)
- {
- char szBuffer[1000];
- int ret;
- va_list args;
- va_start(args, format);
- ret = vsprintf(szBuffer, format, args);
- w32_fputs(szBuffer, stream);
- va_end(args);
- return ret;
- }
-
- #endif
-