home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2000 January / PCW0001.ISO / software / hw / pc2000 / junkbust.exe / win32.c < prev   
Encoding:
C/C++ Source or Header  |  1999-10-11  |  4.7 KB  |  215 lines

  1. char *win32_rcs = "$Id: win32.c,v 1.11 1998/02/06 00:23:52 ACJC Exp $";
  2. /* Written and copyright 1997 Anonymous Coders and Junkbusters Corporation.
  3.  * Distributed under the GNU General Public License; see the README file.
  4.  * This code comes with NO WARRANTY. http://www.junkbusters.com/ht/en/gpl.html
  5.  */
  6. /* Win32 User Interface enchancements - Copyright 1999 Adam Lock <locka@iol.ie> */
  7. #ifdef _WIN32
  8.  
  9. #include <stdio.h>
  10. #ifdef REGEX
  11. #include "gnu_regex.h"
  12. #endif
  13. #include "jcc.h"
  14.  
  15. /* Uncomment this if you want to build Win32 as a console app */
  16. /* #define _WIN_CONSOLE */
  17.  
  18. #include <windows.h>
  19.  
  20. #include <stdarg.h>
  21. #include <process.h>
  22.  
  23. char *win32_blurb =
  24. "Internet Junkbuster Proxy(TM) Version " VERSION " for Windows is Copyright (C) 1997-8\n"
  25. "by Junkbusters Corp.  This is free software; it may be used and copied under\n"
  26. "the GNU General Public License: http://www.junkbusters.com/ht/en/gpl.html .\n"
  27. "This program comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\n"
  28. "\n"
  29. "For information about how to to configure the proxy and your browser, see\n"
  30. "        http://www.junkbusters.com/ht/en/ijbwin.html#v" VERSION_MAJOR "\n"
  31. "\n"
  32. "The Internet Junkbuster Proxy(TM) is running and ready to serve!\n"
  33. ;
  34.  
  35. #ifdef _WIN_CONSOLE
  36. extern int hideConsole;
  37. #else
  38.  
  39. HINSTANCE g_hInstance;
  40. int g_nCmdShow;
  41.  
  42. static void  __cdecl UserInterfaceThread(void *);
  43.  
  44. #endif
  45.  
  46. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  47. {
  48.     int argc = 0;
  49.     int i;
  50.     int res;
  51.     char **argv = NULL;
  52.     char *pszArgs = NULL;
  53.     char *pszLastTok;
  54.     char szModule[MAX_PATH+1];
  55. #ifndef _WIN_CONSOLE
  56.     HANDLE hInitCompleteEvent = NULL;
  57. #endif
  58.  
  59.     /* Split command line into arguments */
  60.     pszArgs = malloc(strlen(lpCmdLine) + 1);
  61.     strcpy(pszArgs, lpCmdLine);
  62.  
  63.     GetModuleFileName(hInstance, szModule, MAX_PATH);
  64.  
  65.     /* Count number of spaces */
  66.     argc = 1;
  67.     if (strlen(pszArgs) > 0)
  68.     {
  69.         pszLastTok = pszArgs;
  70.         do {
  71.             argc++;
  72.             pszLastTok = strchr(pszLastTok, ' ');
  73.         } while (pszLastTok);
  74.     }
  75.  
  76.     /* Allocate array of strings */
  77.     argv = malloc(sizeof(char *) * argc);
  78.  
  79.     /* step through command line replacing spaces with zeros, initialise array */
  80.     argv[0] = szModule;
  81.     i = 1;
  82.     pszLastTok = pszArgs;
  83.     do {
  84.         argv[i] = pszLastTok;
  85.         pszLastTok = strchr(pszLastTok, ' ');
  86.         if (pszLastTok) {
  87.             while (*pszLastTok != '\0' && *pszLastTok == ' ') {
  88.                 *pszLastTok = '\0';
  89.                 pszLastTok++;
  90.             }
  91.         }
  92.         i++;
  93.     } while (pszLastTok && *pszLastTok != '\0');
  94.  
  95. #ifndef _WIN_CONSOLE
  96.     /* Create a user-interface thread and wait for it to initialise */
  97.     hInitCompleteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  98.     g_hInstance = hInstance;
  99.     g_nCmdShow = nCmdShow;
  100.     _beginthread(UserInterfaceThread, 0, &hInitCompleteEvent);
  101.     WaitForSingleObject(hInitCompleteEvent, INFINITE);
  102.     DeleteObject(hInitCompleteEvent);
  103. #endif
  104.  
  105.     res = main(argc, argv);
  106.  
  107.     /* Cleanup */
  108.     free(argv);
  109.     free(pszArgs);
  110.  
  111.     return res;
  112. }
  113.  
  114. #endif
  115.  
  116. /*
  117.  * Initialise windows, setting up the console or windows as appropriate
  118.  */
  119. void InitWin32()
  120. {
  121.     WORD wVersionRequested;
  122.     WSADATA wsaData;
  123.  
  124. #ifdef _WIN_CONSOLE
  125.     SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
  126.     if (hideConsole) {
  127.         FreeConsole();
  128.     }
  129. #endif
  130.     wVersionRequested = MAKEWORD(2, 0);
  131.     if (WSAStartup(wVersionRequested, &wsaData) != 0) {
  132.         exit(1);
  133.     }
  134. }
  135.  
  136. #ifndef _WIN_CONSOLE
  137. #include <signal.h>
  138. #include <assert.h>
  139.  
  140. #include "w32log.h"
  141.  
  142. /*
  143.  * User interface thread
  144.  */
  145. void  __cdecl UserInterfaceThread(void *pData)
  146. {
  147.     MSG msg;
  148.     HANDLE hInitCompleteEvent = *((HANDLE *) pData);
  149.  
  150.     /* Initialise */
  151.     InitLogWindow();
  152.     SetEvent(hInitCompleteEvent);
  153.  
  154.     /* Enter a message processing loop */
  155.     while (GetMessage(&msg, (HWND) NULL, 0, 0)) 
  156.     { 
  157.         TranslateMessage(&msg); 
  158.         DispatchMessage(&msg); 
  159.     } 
  160.  
  161.     /* Cleanup */
  162.     TermLogWindow();
  163.  
  164.     /* Time to die... */
  165.     raise(SIGINT);
  166. }
  167.  
  168. #undef fputs
  169. #undef fprintf
  170. #undef fwrite
  171.  
  172. int w32_fputs(const char *string, FILE *stream)
  173. {
  174.     int ret;
  175.     if (stream != logfp && stream != stdout)
  176.     {
  177.         return fputs(string, stream);
  178.     }
  179.  
  180. #ifdef _WIN_CONSOLE
  181.     ret = fputs(string, stream);
  182. #else
  183.     ret = LogPutString(string);
  184. #endif
  185.  
  186.     return ret;
  187. }
  188.  
  189. int w32_fwrite(const void *buffer, size_t size, size_t count, FILE *stream)
  190. {
  191.     char *buf;
  192.     if (stream != logfp && stream != stdout)
  193.     {
  194.         return fwrite(buffer, size, count, stream);
  195.     }
  196.     buf = strdup(buffer);
  197.     w32_fputs(buf, stream);
  198.     free(buf);
  199.     return 1;
  200. }
  201.  
  202. int w32_fprintf( FILE *stream, const char *format, ...)
  203. {
  204.     char szBuffer[1000];
  205.     int ret;
  206.     va_list args;
  207.     va_start(args, format);
  208.     ret = vsprintf(szBuffer, format, args);
  209.     w32_fputs(szBuffer, stream);
  210.     va_end(args);
  211.     return ret;
  212. }
  213.  
  214. #endif
  215.