home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / INTERNET / UPC2S1.ZIP / CATCHER.C < prev    next >
C/C++ Source or Header  |  1993-09-29  |  7KB  |  212 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    c a t c h e r . c                                               */
  3. /*                                                                    */
  4. /*    Ctrl-Break handler for UUPC/extended                            */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  9. /*       Wonderworks.                                                 */
  10. /*                                                                    */
  11. /*       All rights reserved except those explicitly granted by       */
  12. /*       the UUPC/extended license agreement.                         */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: catcher.c 1.3 1993/09/29 04:49:20 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: catcher.c $
  24.  *     Revision 1.3  1993/09/29  04:49:20  ahd
  25.  *     Use actual signal handler number for resetting handler
  26.  *
  27.  *     Revision 1.2  1993/09/20  04:38:11  ahd
  28.  *     TCP/IP support from Dave Watt
  29.  *     't' protocol support
  30.  *     OS/2 2.x support
  31.  *
  32.  */
  33.  
  34. /*--------------------------------------------------------------------*/
  35. /*    Since C I/O functions are not safe inside signal routines,      */
  36. /*    the code uses conditionals to use system-level DOS and OS/2     */
  37. /*    services.  Another option is to set global flags and do any     */
  38. /*    I/O operations outside the signal handler.                      */
  39. /*--------------------------------------------------------------------*/
  40.  
  41. /*--------------------------------------------------------------------*/
  42. /*                        System include files                        */
  43. /*--------------------------------------------------------------------*/
  44.  
  45. #include <stdio.h>
  46. #include <signal.h>
  47. #include <process.h>
  48. #include <stdlib.h>
  49.  
  50. #if defined(WIN32) || defined(_Windows)
  51. #include "winsock.h"
  52. #endif
  53.  
  54. /*--------------------------------------------------------------------*/
  55. /*                    UUPC/extended include files                     */
  56. /*--------------------------------------------------------------------*/
  57.  
  58. #include "lib.h"
  59. #include "timestmp.h"
  60. #include "catcher.h"
  61. #include "safeio.h"
  62.  
  63. #if defined(_Windows)
  64. #include "pwinsock.h"
  65. #endif
  66.  
  67. /*--------------------------------------------------------------------*/
  68. /*                          Global variables                          */
  69. /*--------------------------------------------------------------------*/
  70.  
  71. boolean terminate_processing = FALSE;
  72. boolean interactive_processing = TRUE;
  73. boolean norecovery = TRUE;
  74.  
  75. #if defined(WIN32) || defined(_Windows)
  76. boolean winsockActive = FALSE;      // Set/reset in ulibip.c
  77. #endif
  78.  
  79. int panic_rc = 69;
  80.  
  81. #define INVALID_CHAR '*'
  82.  
  83. /*--------------------------------------------------------------------*/
  84. /*    c t r l c h a n d l e r                                         */
  85. /*                                                                    */
  86. /*    Handles SIGINT (CTRL+C) interrupt; from MicroSoft Programmer's  */
  87. /*    Workbench QuickHelp samples                                     */
  88. /*--------------------------------------------------------------------*/
  89.  
  90. #ifdef __TURBOC__
  91. #pragma argsused
  92. #endif
  93.  
  94. void
  95. #ifdef __TURBOC__
  96. __cdecl
  97. #endif
  98. ctrlchandler( int sig )
  99. {
  100.     int ch = INVALID_CHAR;
  101.  
  102. /*--------------------------------------------------------------------*/
  103. /*                  Disallow CTRL+C during handler.                   */
  104. /*--------------------------------------------------------------------*/
  105.  
  106.     signal( sig, SIG_IGN );
  107.  
  108. /*--------------------------------------------------------------------*/
  109. /*          Don't ask if the program doesn't think we should          */
  110. /*--------------------------------------------------------------------*/
  111.  
  112.     if ( ! interactive_processing )
  113.     {
  114.  
  115.       safeout( "\r\n" );
  116.       safeout( compilen );
  117.       panic_rc = 100;
  118.       terminate_processing = interactive_processing = TRUE;
  119.       safeout(": *** Termination in progress ***\r\n");
  120.  
  121. #if defined(WIN32) || defined(_Windows)
  122.       if (winsockActive)
  123.       {
  124.          if (WSAIsBlocking())
  125.          {
  126.             printmsg(15, "catcher:  sockets are blocking");
  127.             WSACancelBlockingCall();
  128.          }
  129.          else {
  130.              printmsg(15, "catcher:  sockets are not blocking");
  131.          }
  132.       } /* if (winsockActive) */
  133. #endif
  134.  
  135. #ifdef __OS2__
  136.       signal( sig, (void (__cdecl *)(int))ctrlchandler );
  137. #else
  138.       signal( sig, ctrlchandler );
  139. #endif
  140.  
  141.       return;
  142.     }
  143.  
  144.     if ( terminate_processing )
  145.       safeout( "Termination already in progress ... answer Y to SCRAM program");
  146.  
  147. /*--------------------------------------------------------------------*/
  148. /*                   Ask user if he/she/it is sure                    */
  149. /*--------------------------------------------------------------------*/
  150.  
  151.    while ( ch == INVALID_CHAR )
  152.    {
  153.       safeout( "\r\n" );
  154.       safeout( compilen );
  155.       safeout( ": Abort processing? (Y/N) " );
  156.       safeflush();            /* Flush any queued characters         */
  157.       ch = safein();
  158.  
  159.       switch( ch )
  160.       {
  161.  
  162.          case 'y':
  163.          case 'Y':
  164.             if ( terminate_processing || norecovery )
  165.             {
  166.                safeout("\n\rProgram aborted.\r\n");
  167.                _exit(100);
  168.             }
  169.  
  170.             terminate_processing = TRUE;  /* Controlled shutdown  */
  171.             panic_rc = 100;
  172.             safeout("\n\r*** Termination in progress ***\r\n");
  173. #if 0
  174. #ifdef WIN32
  175.             if (IsNetwork()) {
  176.                if (WSAIsBlocking()) {
  177.                   printmsg(15, "catcher:  sockets are blocking");
  178.                   WSACancelBlockingCall();
  179.                } else {
  180.                    printmsg(15, "catcher:  sockets are not blocking");
  181.                }
  182.             }
  183. #endif
  184. #endif
  185.             break;
  186.  
  187.         case 'N':
  188.         case 'n':
  189.            safeout("\r\nResuming execution\r\n");
  190.            break;
  191.  
  192.         default:
  193.            safeout(" -- Invalid response\a");
  194.            ch = INVALID_CHAR;
  195.            break;
  196.  
  197.       } /* switch  */
  198.    } /* for */
  199.  
  200. /*--------------------------------------------------------------------*/
  201. /*    The CTRL+C interrupt must be reset to our handler since by      */
  202. /*    default it is reset to the system handler.                      */
  203. /*--------------------------------------------------------------------*/
  204.  
  205. #ifdef __OS2__
  206.       signal( sig, (void (__cdecl *)(int))ctrlchandler );
  207. #else
  208.       signal( sig, ctrlchandler );
  209. #endif
  210.  
  211. } /* catcher */
  212.