home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / bcfamily / source / setsig.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-12  |  3.1 KB  |  114 lines

  1. //
  2. //      *******************************************************************
  3. //        JdeBP C++ Library Routines          General Public Licence v1.00
  4. //            Copyright (c) 1991,1992     Jonathan de Boyne Pollard
  5. //      *******************************************************************
  6. //
  7. // Part of FamAPI.LIB
  8. //
  9.  
  10. #include "famapi.h"
  11. #include "dosdos.h"
  12.  
  13. //
  14. //    The function chain invoked by a signal handler starts at the latest
  15. //    setting of DosSetSigHandler() and *may* chain through to NoFunction() .
  16. //
  17. static void _APICALL NoFunction (USHORT, USHORT) { }
  18. static void far (_APICALL *CtrlBreakFunction)(USHORT, USHORT) = NoFunction ;
  19. static void far (_APICALL *CtrlCFunction)(USHORT, USHORT) = NoFunction ;
  20. static unsigned int CtrlBreakAction = 0 ;
  21. static unsigned int CtrlCAction = 0 ;
  22.  
  23. //
  24. //    The actual software interrupt handlers invoke the signal call chains.
  25. //
  26. static void (interrupt *WasInt23Handler)() = 0L ;
  27. static void interrupt Int23Handler()
  28. {
  29.     asm push ds ;
  30.     DosDosSetVect( 0x23, WasInt23Handler) ;
  31.     if (CtrlBreakAction != 1) (*CtrlBreakFunction)(0, 0) ;
  32.     if (CtrlCAction != 1) (*CtrlCFunction)(0, 0) ;
  33.     DosDosSetVect( 0x23, Int23Handler) ;
  34.     asm pop ds ;
  35. }
  36.  
  37. //
  38. //    Set signal handlers
  39. //
  40. //    We do not need to save and restore INT23 vector ourselves (unless we
  41. //    are actually in the middle of an INT23 handler), because DOS does that
  42. //    for us using a field in the PSP.
  43. //
  44. USHORT _APICALL
  45. DosSetSigHandler    ( void far (_APICALL *PtrSigHandler)(USHORT, USHORT),
  46.                       void far (_APICALL * far *PtrPrevSigHandler)(USHORT, USHORT),
  47.                       unsigned int far *PtrAction,
  48.                       unsigned int Action,
  49.                       unsigned int SigNumber )
  50. {
  51.     unsigned int far *PtrNewAction ;
  52.     void far (_APICALL * far *PtrNewFunction)(USHORT, USHORT) ;
  53.  
  54.     if (!WasInt23Handler)
  55.         DosDosGetVect( 0x23, &WasInt23Handler) ;
  56.  
  57.     //
  58.     //    We only support CTRL-BREAK and CTRL-C.  DOS does not support
  59.     //    broken pipes or user "flags".
  60.     //
  61.     switch (SigNumber) {
  62.         case SIG_CTRLBREAK :
  63.             PtrNewAction = &CtrlBreakAction ;
  64.             PtrNewFunction = &CtrlBreakFunction ;
  65.             break ;
  66.  
  67.         case SIG_CTRLC :
  68.             PtrNewAction = &CtrlCAction ;
  69.             PtrNewFunction = &CtrlCFunction ;
  70.             break ;
  71.  
  72.         default:
  73.             return ERROR_NOT_SUPPORTED ;
  74.     }
  75.  
  76.     switch (Action) {
  77.         case 0x0000:    //    Install default signal handler
  78.  
  79.             *PtrPrevSigHandler = (*PtrNewFunction);
  80.             *PtrAction = (*PtrNewAction) ;
  81.             (*PtrNewFunction) = NoFunction ;
  82.             (*PtrNewAction) = Action ;
  83.  
  84.             DosDosSetVect( 0x23, WasInt23Handler) ;
  85.             break ;
  86.  
  87.         case 0x0001:    //    Ignore signal
  88.  
  89.             *PtrPrevSigHandler = (*PtrNewFunction);
  90.             *PtrAction = (*PtrNewAction) ;
  91.             (*PtrNewFunction) = NoFunction ;
  92.             (*PtrNewAction) = Action ;
  93.  
  94.             DosDosSetVect( 0x23, Int23Handler) ;
  95.             break ;
  96.  
  97.         case 0x0002:    //    Set specified handler function
  98.  
  99.             *PtrPrevSigHandler = (*PtrNewFunction);
  100.             *PtrAction = (*PtrNewAction) ;
  101.             (*PtrNewFunction) = PtrSigHandler ;
  102.             (*PtrNewAction) = Action ;
  103.  
  104.             DosDosSetVect( 0x23, Int23Handler) ;
  105.             break ;
  106.  
  107.         case 0x0003:    // Disallow flags from other processes ...
  108.         case 0x0004:    // Reset pending signal
  109.         default:
  110.             return ERROR_NOT_SUPPORTED ;
  111.     }
  112.     return NO_ERROR ;
  113. }
  114.