home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / ckosig.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  4KB  |  137 lines

  1. /* C K O S I G  --  Kermit signal handling for OS/2 and Win32 systems */
  2.  
  3. /*
  4.   Author: Jeffrey Altman (jaltman@secure-endpoints.com),
  5.             Secure Endpoints Inc., New York City.
  6.  
  7.   Copyright (C) 1985, 2004, Trustees of Columbia University in the City of New
  8.   York.
  9. */
  10.  
  11. #include "ckcsym.h"
  12. #include "ckcasc.h"                     /* ASCII character symbols */
  13. #include "ckcdeb.h"                     /* Debug & other symbols */
  14. #include "ckcker.h"                     /* Kermit symbols */
  15. #include "ckcnet.h"                     /* Network symbols */
  16. #ifndef NOSPL
  17. #include "ckuusr.h"
  18. #endif /* NOSPL */
  19.  
  20. #include <signal.h>
  21. #ifndef NOCCTRAP
  22. #ifdef NT
  23. #include <setjmpex.h>
  24. #else /* NT */
  25. #include <setjmp.h>
  26. #endif /* NT */
  27. #endif /* NOCCTRAP */
  28. #include "ckusig.h"
  29.  
  30. #ifdef NT
  31. #include <windows.h>
  32. #else /* NT */
  33. #define INCL_WIN
  34. #define INCL_VIO
  35. #define INCL_ERRORS
  36. #define INCL_DOSPROCESS
  37. #define INCL_DOSSEMAPHORES
  38. #define INCL_DOSDEVIOCTL
  39. #define INCL_WINCLIPBOARD
  40. #define INCL_DOSDATETIME
  41. #include <os2.h>
  42. #undef COMMENT                /* COMMENT is defined in os2.h */
  43. #endif /* NT */
  44. #include "ckocon.h"
  45.  
  46. #define THRDSTKSIZ      (2*131072)
  47.  
  48. int
  49. cc_execute( jmp_buf * sj_buf, ck_sigfunc dofunc, ck_sigfunc failfunc )
  50. {
  51.     int rc = 0 ;
  52.     TID tidThread ;
  53.     ULONG semid ;
  54.     ULONG ccindex ;
  55.    #ifdef NT
  56.     HANDLE hevThread ;
  57.    #else /* NT */
  58.     HEV hevThread ;
  59.    #endif /* NT */
  60.  
  61.     /* create an event semaphore for new thread        */
  62.    #ifdef NT
  63.     hevThread = CreateEvent( NULL, TRUE, FALSE, NULL ) ;
  64.    #else
  65.     DosCreateEventSem( NULL, &hevThread, DC_SEM_SHARED, FALSE ) ;
  66.    #endif /* NT */
  67.     CreateCtrlCSem( FALSE, &ccindex ) ;
  68.     CreateCtrlCMuxWait( ccindex, hevThread ) ;
  69.  
  70.     /* begin new thread with dofunc                    */
  71.     tidThread = (TID) _beginthread( dofunc,
  72.                                                                          #ifndef NT
  73.                                                                           0,
  74.                                                                          #endif /* NT */
  75.                                                                           THRDSTKSIZ, (void *) &hevThread ) ;
  76.  
  77.     /* wait for the event semaphore or Ctrl-C          */
  78.     /*    semaphore to be set                          */
  79.     /* when Ctrl-C semaphore is set execute failfunc   */
  80.     /*    and return -1                                */
  81.     /* else if event semaphore is set return 0         */
  82.     semid = WaitCtrlCMuxWait( ccindex, SEM_INDEFINITE_WAIT ) ;
  83.     if ( semid == 1 )  /* Ctrl-C */
  84.     {
  85.         ResetCtrlCSem(ccindex) ;
  86.        #ifdef NT
  87.         KillThread( tidThread ) ;
  88.        #else /* NT */
  89.         DosKillThread( tidThread ) ;
  90.        #endif /* NT */
  91.         rc = -1 ;
  92.         (*failfunc)(0) ;
  93.     } else if ( semid == 2 )  /* Thread completed successfully */
  94.     {
  95.         ResetSem( hevThread ) ;
  96.     }
  97.  
  98.     CloseCtrlCMuxWait(ccindex) ;
  99.     CloseCtrlCSem( ccindex ) ;
  100.    #ifdef NT
  101.     CloseHandle( hevThread ) ;
  102.    #else /* NT */
  103.     DosCloseEventSem( hevThread );
  104.    #endif /* NT */
  105.     return rc ;
  106. }
  107.  
  108. int
  109. alrm_execute(jmp_buf * sj_buf,
  110.              int timo,
  111.              ck_sighand handler,
  112.              ck_sigfunc dofunc,
  113.              ck_sigfunc failfunc
  114.              )
  115. {
  116.  
  117.     int rc = 0;
  118.     int savalrm = 0;
  119. _PROTOTYP( SIGTYP (*savhandler), (int) );
  120.  
  121.     savalrm = alarm(timo);
  122.     savhandler = signal( SIGALRM, handler );
  123.     if (
  124.         setjmp(*sj_buf)
  125.         ) {                             /* Alarm trap returns to here. */
  126.         (*failfunc)(NULL) ;
  127.         rc = -1 ;
  128.     } else {
  129.         (*dofunc)(NULL) ;
  130.     }
  131.     alarm(savalrm) ;
  132.     if ( savhandler )
  133.         signal( SIGALRM, savhandler ) ;
  134.     return rc ;
  135. }
  136.  
  137.