home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckv206.zip / ckcsig.h < prev    next >
C/C++ Source or Header  |  2002-01-21  |  6KB  |  215 lines

  1. /*  C K C S I G . H  */
  2.  
  3. /*  Definitions and prototypes for signal handling  */
  4.  
  5. /*
  6.   Author: Jeffrey Altman (jaltman@columbia.edu),
  7.   Columbia University Academic Information Systems, New York City.
  8.  
  9.   Copyright (C) 1985, 2002,
  10.     Trustees of Columbia University in the City of New York.
  11.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  12.     copyright text in the ckcmai.c module for disclaimer and permissions.
  13. */
  14. #ifdef OS2
  15. #ifndef NT
  16. #ifndef __HEV__                 /* INCL_SEMAPHORE may also define HEV */
  17. #define __HEV__
  18. typedef  ULONG    HEV;                  /* hev */
  19. typedef  HEV      *PHEV;
  20. #endif /* __HEV__ */
  21. #endif /* NT */
  22. struct _threadinfo {
  23.     int inuse;
  24.     int child;
  25.     int sibling;
  26. #ifdef NT
  27.     HANDLE id;
  28.     HANDLE handle;
  29.     HANDLE parent;
  30.     HANDLE CompletionSem ;
  31.     HANDLE DieSem ;
  32. #else /* NT */
  33.     TID id;
  34.     TID parent;
  35.     HEV CompletionSem;
  36.     HEV DieSem;
  37. #endif /* NT */
  38. };
  39. #endif /* OS2 */
  40.  
  41. #ifdef CK_ANSIC
  42. typedef SIGTYP (*ck_sigfunc)(void *);
  43. typedef SIGTYP (*ck_sighand)(int);
  44. #else
  45. typedef SIGTYP (*ck_sigfunc)();
  46. typedef SIGTYP (*ck_sighand)();
  47. #endif /* CK_ANSIC */
  48.  
  49. /* Macros for POSIX vs old-style signal handling. */
  50.  
  51. #ifdef CK_POSIX_SIG
  52. typedef sigjmp_buf ckjmpbuf;
  53. #else
  54. #ifdef NT
  55. #define NOCRYPT
  56. #include <windows.h>
  57. #ifdef NTASM
  58. typedef struct {
  59.     CONTEXT context;
  60.     DWORD retcode;
  61. } ckjmpbuf;
  62. #else /* NTASM */
  63. typedef jmp_buf ckjmpbuf;
  64. #endif /* NTASM */
  65. #else
  66. typedef jmp_buf ckjmpbuf;
  67. #endif
  68. #endif /* CK_POSIX_SIG */
  69. /*
  70.   Suppose you want to pass the address of a jmp_buf bar to a function foo.
  71.   Since jmp_buf is normally defined (typedef'd) as an array, you would do
  72.   it like this:  foo(bar), where foo = foo(jmp_buf bar).  But suppose a
  73.   jmp_buf is (say) a struct rather than an array.  Then you must do
  74.   foo(&bar) where foo is foo(jmp_buf * bar).  This is controlled here in
  75.   the traditional fashion, by ifdefs.  By default, we assume that jmp_buf
  76.   is an array.  Define the symbol JBNOTARRAY if jmp_buf is not an array.
  77. */
  78. #ifndef JBNOTARRAY
  79. #ifdef NT
  80. #define JBNOTARRAY
  81. #endif /* NT */
  82. #endif /* JBNOTARRAY */
  83.  
  84. #ifdef JBNOTARRAY
  85. typedef ckjmpbuf * ckjptr;
  86. #define ckjaddr(x) & x
  87. #define ckjdref(x) * x
  88. #ifdef CK_POSIX_SIG
  89. #define cksetjmp(x) sigsetjmp(x,1)
  90. #define cklongjmp(x,y) siglongjmp(x,y)
  91. #else
  92. #ifdef NT
  93. __inline int
  94. ck_ih(void) {
  95.     extern int TlsIndex;
  96. #ifdef NTSIG
  97.     struct _threadinfo * threadinfo;
  98.     threadinfo = (struct _threadinfo *) TlsGetValue(TlsIndex);
  99.     if (threadinfo) {
  100.         if (WaitAndResetSem(threadinfo->DieSem,0)) {
  101.             ckThreadDie(threadinfo);
  102.             return 1;                   /* This should never execute */
  103.         }
  104.     }
  105. #ifdef COMMENT
  106.     else debug( F100, "ck_ih() threadinfo is NULL","",0);
  107. #endif /* COMMENT */
  108. #endif /* NTSIG */
  109.     return 0;
  110. }
  111. #ifdef NTSIG
  112. #define cksetjmp(x) setjmp(x)
  113. #define cklongjmp(x,y) longjmp(x,y)
  114. #else /* NTSIG */
  115. #ifdef NTASM
  116. __inline DWORD
  117. cksetjmp( ckjptr jmp ) {
  118.     extern int isinterrupted;
  119.     jmp->retcode = 0;
  120.     memset( &jmp->context, 0, sizeof(CONTEXT) );
  121.     jmp->context.ContextFlags = CONTEXT_FULL ;
  122.     if ( !GetThreadContext( GetCurrentThread(), &jmp->context ) )
  123.       debug( F101, "cksetjmp GetThreadContext failed","",GetLastError());
  124.     debug(F101,"cksetjmp returns","",jmp->retcode);
  125.     isinterrupted = 0;
  126.     return (jmp->retcode);
  127. }
  128.  
  129. __inline void
  130. cklongjmp( ckjptr jmp, int retval ) {
  131.     extern HANDLE tidCommand;
  132.     extern int ttyfd, mdmtyp ;
  133.     extern DWORD CommandID;
  134.     extern int isinterrupted;
  135.  
  136.     connoi();
  137.     isinterrupted = 1;
  138.     jmp->retcode = ( retval ? retval : 1 );
  139.     debug(F101,"about to SetThreadContext for thread","", CommandID);
  140.     debug(F101,"from Thread","",GetCurrentThreadId());
  141.     if ( mdmtyp >= 0 ) {
  142.         PurgeComm( (HANDLE) ttyfd, PURGE_TXABORT | PURGE_RXABORT );
  143.     }
  144.     if (SetThreadContext( tidCommand, &jmp->context ))
  145.       debug(F100,"cklongjmp SetThreadContext success","",0);
  146.     else
  147.       debug(F101,"cklongjmp SetThreadContext failed","",GetLastError());
  148.     msleep(50);
  149.     cmini(1);                           /* Reset command parser */
  150.     putkey(13);                         /* Stuff a carriage return */
  151.    /* PostEventAvailSem(); */
  152. }
  153. #else /* NTASM */
  154. void crash( void ) ;
  155. #define cksetjmp(x) setjmp(x)
  156. __inline void
  157. cklongjmp( ckjptr jmp, int retval ) {
  158.     extern HANDLE tidCommand;
  159.     extern int ttyfd, mdmtyp;
  160.     extern DWORD CommandID;
  161.     CONTEXT context;
  162.  
  163.     if ( mdmtyp >= 0 ) {
  164.         PurgeComm( (HANDLE) ttyfd, PURGE_TXABORT | PURGE_RXABORT ) ;
  165.     }
  166.     memset( &context, 0, sizeof(CONTEXT) );
  167.     context.ContextFlags = CONTEXT_FULL;
  168.     if ( !GetThreadContext( tidCommand, &context ) )
  169.       debug( F101, "cklongjmp GetThreadContext failed","",GetLastError());
  170.  
  171.     /* Invalidate the instruction pointer */
  172.     context.Eip =  (unsigned long) crash;
  173.  
  174.     debug(F101,"about to SetThreadContext for thread","", CommandID);
  175.     debug(F101,"from Thread","",GetCurrentThreadId());
  176.     if (SetThreadContext( tidCommand, &context ))
  177.       debug(F100,"cklongjmp SetThreadContext success","",0);
  178.     else
  179.       debug(F101,"cklongjmp SetThreadContext failed","",GetLastError());
  180. }
  181. #endif /* NTASM */
  182. #endif /* NTSIG */
  183. #else /* NT */
  184. #define cksetjmp(x) setjmp(x)
  185. #define cklongjmp(x,y) longjmp(x,y)
  186. #endif /* NT */
  187. #endif /* CK_POSIX_SIG */
  188. #else  /* jmp_buf is an array */
  189. typedef ckjmpbuf ckjptr;
  190. #define ckjaddr(x) x
  191. #define ckjdref(x) x
  192. #ifdef CK_POSIX_SIG
  193. #define cksetjmp(x) sigsetjmp(x,1)
  194. #define cklongjmp(x,y) siglongjmp(x,y)
  195. #else
  196. #define cksetjmp(x) setjmp(x)
  197. #define cklongjmp(x,y) longjmp(x,y)
  198. #endif /* CK_POSIX_SIG */
  199. #endif /* JBNOTARRAY */
  200.  
  201. _PROTOTYP( int cc_execute, (ckjptr, ck_sigfunc, ck_sigfunc) );
  202. _PROTOTYP( int alrm_execute,
  203.           (ckjptr,
  204.            int /* timo */,
  205.            ck_sighand /* handler */,
  206.            ck_sigfunc, ck_sigfunc) );
  207. _PROTOTYP( int cc_alrm_execute,
  208.           (ckjptr,
  209.            int /* timo */,
  210.            ck_sighand /* handler */,
  211.            ck_sigfunc,
  212.            ck_sigfunc) );
  213.  
  214. /* End of ckusig.h */
  215.