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