home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / IRIT / POLY3DHS.ZIP / CTRL-BRK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-05  |  3.3 KB  |  107 lines

  1. /*****************************************************************************
  2. * Module to trap ctrl-brk/hardware error and handle them gracefully.         *
  3. * Note the usage of GraphGen.c module is assumed.                 *
  4. *                                         *
  5. * Written by:  Gershon Elber                Ver 1.0, Jan. 1989   *
  6. *****************************************************************************/
  7.  
  8. #ifdef __MSDOS__
  9. #include <dos.h>
  10. #include <graphics.h>
  11. #else
  12. #include <signal.h>
  13. #endif /* __MSDOS__ */
  14.  
  15. #include <stdio.h>
  16. #include "program.h"
  17. #include "ctrl-brk.h"
  18.  
  19. #ifdef __MSDOS__
  20. static void far *OldCtrlBrk; /* Save here old int 1bh to be able to restore. */
  21. #endif /* __MSDOS__ */
  22.  
  23. static int WasCtrlBrkSetUp = FALSE;  /* TRUE if SetUpCtrlBrk rtn was called. */
  24. int WasCtrlBrk;
  25.  
  26. #ifdef __MSDOS__
  27.  
  28. /*****************************************************************************
  29. * Routine TrapCtrlBrk gain control if Control break was typed:             *
  30. *****************************************************************************/
  31. static void interrupt TrapCtrlBrk(void)
  32. {
  33.     WasCtrlBrk = TRUE;
  34. }
  35.  
  36. #endif /* __MSDOS__ */
  37.  
  38. /*****************************************************************************
  39. * Routine TrapCtrlC gain control if Control C was typed (DOS level):         *
  40. *****************************************************************************/
  41. static int TrapCtrlC(void)
  42. {
  43.     WasCtrlBrk = TRUE;
  44.  
  45. #ifndef __MSDOS__
  46.     printf("\n*** Break ***\n");
  47. #endif /* __MSDOS__ */
  48.  
  49.     return -1;                        /* Resume execution. */
  50. }
  51.  
  52. /*****************************************************************************
  53. * Routine SetUpCtrlBrk must be called once by main program:             *
  54. *****************************************************************************/
  55. void SetUpCtrlBrk(void)
  56. {
  57.     if (WasCtrlBrkSetUp) return;          /* No need to to it twice! */
  58.  
  59. #ifdef __MSDOS__
  60.     OldCtrlBrk = getvect(BIOS_CTRL_BRK);
  61.     setvect(BIOS_CTRL_BRK, TrapCtrlBrk);
  62.  
  63.     ctrlbrk(TrapCtrlC);
  64. #else
  65.     signal(SIGINT, TrapCtrlC);
  66.     signal(SIGQUIT, TrapCtrlC);
  67. #endif /* __MSDOS__ */
  68.  
  69.     WasCtrlBrkSetUp = TRUE;
  70. }
  71.  
  72. /*****************************************************************************
  73. * Routine RestoreCtrlBrk must be called before the program quit:         *
  74. *****************************************************************************/
  75. void RestoreCtrlBrk(void)
  76. {
  77. #ifdef __MSDOS__
  78.     if (WasCtrlBrkSetUp)
  79.     setvect(BIOS_CTRL_BRK, (void interrupt (*)()) OldCtrlBrk);
  80. #endif /* __MSDOS__ */
  81.  
  82.     WasCtrlBrkSetUp = FALSE;
  83. }
  84.  
  85. #ifdef __MSDOS__
  86.  
  87. /*****************************************************************************
  88. * Routine TrapHardWareError gain control if hardware fails - int 24.         *
  89. *****************************************************************************/
  90. static int TrapHardWareError(int errval, int ax, int bp, int si)
  91. {
  92.     return IGNORE;
  93. }
  94.  
  95. #endif /* __MSDOS__ */
  96.  
  97. /*****************************************************************************
  98. * Routine SetUpHardError must be called once by main program:             *
  99. * harderr set interrupt vector 0x024 to point to TrapHardWareError.         *
  100. *****************************************************************************/ 
  101. void SetUpHardErr(void)
  102. {
  103. #ifdef __MSDOS__
  104.     harderr(TrapHardWareError);
  105. #endif /* __MSDOS__ */
  106. }
  107.