home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / CLIBSRC3.ZIP / HARDERR.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  7.9 KB  |  230 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - harderr.cas
  3.  *
  4.  * function(s)
  5.  *        __harderr   - establishes a hardware error handler (Borland)
  6.  *        _harderr    - establishes a hardware error handler (MSC compatible)
  7.  *        _hardresume - hardware error handler function
  8.  *        _hardretn   - hardware error handler function
  9.  *        hentry      - calls user error handler
  10.  *-----------------------------------------------------------------------*/
  11.  
  12. /*
  13.  *      C/C++ Run Time Library - Version 5.0
  14.  *
  15.  *      Copyright (c) 1987, 1992 by Borland International
  16.  *      All Rights Reserved.
  17.  *
  18.  */
  19.  
  20.  
  21. #pragma inline
  22. #include <dos.h>
  23.  
  24. #ifdef __TINY__
  25. #define FARINT(f) ((void interrupt far (*)())MK_FP(_CS,f))
  26. #else
  27. #define FARINT(f) f
  28. #endif
  29.  
  30. static  int Hsav;
  31. static  int (*Hfunc)(unsigned di, unsigned ax, unsigned bp, unsigned si);
  32. static  void (far *MSCfunc)(unsigned deverror, unsigned errcode,
  33.                                 unsigned far *devhdr);
  34. static  int IsMSC;      /* true if we must use MSCfunc instead of Hfunc */
  35.  
  36. /*-----------------------------------------------------------------------*
  37.  
  38. Name            hentry     - calls user error handler
  39.  
  40. Usage           static void interrupt far hentry(bp, di, si, ds, es,
  41.                                                  dx, cx, bx, ax);
  42.  
  43. Description     calls the user hardware error handler
  44.  
  45. Return value    nothing
  46.  
  47. *------------------------------------------------------------------------*/
  48. #pragma argsused
  49. #pragma option -O-b     // turn off dead store optimization
  50.  
  51. static void interrupt far hentry(bp, di, si, ds, es, dx, cx, bx, ax)
  52. {
  53.         Hsav = _SP;     /* save the stack pointer */
  54.         if (IsMSC)
  55.                 (*MSCfunc)(ax, di, MK_FP(bp,si));
  56.         else
  57.                 ax = (*Hfunc)(di & 0xff, ax, bp, si);
  58. }
  59.  
  60.  
  61. /*-----------------------------------------------------------------------*
  62.  
  63. Name            __harderr - establishes a hardware error handler
  64.                 _harderr - Microsoft C compatible version
  65.  
  66. Usage           void __harderr(int (*fptr)());
  67.                 void _harderr(void (far *fptr)());
  68.  
  69. Related
  70. functions usage void _hardresume(int rescode);
  71.                 void _hardretn(int errcode);
  72.  
  73. Prototype in    dos.h
  74.  
  75. Description     __harderr establishes a hardware error handler for the
  76.                 current program. This handler is invoked whenever an interrupt
  77.                 0x24 occurs. (See the MS-DOS Programmer's Reference Manual for a
  78.                 discussion of the interrupt.)  The harderr macro in dos.h maps
  79.                 to the __harderr function.
  80.  
  81.                 The function pointed to by fptr will be called when such an
  82.                 interrupt occurs. The handler function will be called with the
  83.                 following arguments:
  84.  
  85.                    handler(int errval, int ax, int bp, int si);
  86.  
  87.                 errval is the error code set in the DI register by MS-DOS.
  88.                 ax, bp, and si are the values MS-DOS sets for the AX,
  89.                 BP, and SI registers, respectively.
  90.  
  91.                 ax indicates whether a disk error or other device error was
  92.                 encountered. If ax is non-negative, a disk error was
  93.                 encountered; otherwise, the error was a device error. For a
  94.                 disk error, ax ANDed with 0x00FF will give the failing drive
  95.                 number (1 = A, 2 = B, etc.).
  96.  
  97.                 bp and si together point to the device driver header of
  98.                 the failing driver.
  99.  
  100.                 The named function is not called directly. __harderr establishes
  101.                 a DOS interrupt handler that calls the function.
  102.  
  103.                 peek and peekb can be used to retrieve device information from
  104.                 this driver header. bp is the segment address, and si is
  105.                 the offset.
  106.  
  107.                 The handler may issue bdos calls 1 through 0xC, but any other
  108.                 bdos call will corrupt MS-DOS. In particular, any of the C
  109.                 standard I/O or UNIX-emulation I/O calls may not be used.
  110.  
  111.                 The driver header may not be altered via poke or pokeb.
  112.  
  113.                 The error handler may return or call _hardresume to return to
  114.                 MS-DOS. The return value of the handler or rescode (result code)
  115.                 of _hardresume contains an abort (2), retry (1), or ignore (0)
  116.                 indicator. The abort is accomplished by invoking DOS interrupt
  117.                 0x23, the control-break interrupt.
  118.  
  119.                 The error handler may return directly to the application
  120.                 program by calling _hardretn.
  121.  
  122.                 The Microsoft-compatible _harderr function is similar
  123.                 to __harderr, except that the handler is always a far
  124.                 function, the handler is called with slightly different
  125.                 parameters, and the handler is not expected to return
  126.                 a value:
  127.  
  128.                         void far handler (unsigned deverror, unsigned errcode,
  129.                                         unsigned far *devhdr);
  130.  
  131.  
  132. Return value    The handler must return 0 for ignore, 1 for retry,
  133.                 2 for abort, and 3 for fail (DOS 3.0 or later).  __harderr
  134.                 itself does not return a value.
  135.  
  136. Portability     Unique to MS-DOS.
  137.  
  138. See also        peek, poke, setjmp
  139.  
  140. *------------------------------------------------------------------------*/
  141. void    __harderr(int (*fptr)())        /* Borland version */
  142. {
  143.         Hfunc = fptr;
  144.         IsMSC = 0;
  145.         setvect(0x24, FARINT(hentry));
  146. }
  147.  
  148. void    _harderr(void (far *fptr)())    /* Microsoft C version */
  149. {
  150.         MSCfunc = fptr;
  151.         IsMSC = 1;
  152.         setvect(0x24, FARINT(hentry));
  153. }
  154.  
  155.  
  156. /*-----------------------------------------------------------------------*
  157.  
  158. Name            _hardresume - hardware error handler function
  159.  
  160. Usage           void _hardresume(int rescode);
  161.  
  162. Prototype in    dos.h
  163.  
  164. Description     see __harderr
  165.  
  166. *------------------------------------------------------------------------*/
  167. void    _hardresume(int axret)
  168. {
  169.         _AX = axret;
  170.         _SP = Hsav;
  171. asm     pop     bp
  172. asm     pop     di
  173. asm     pop     si
  174. asm     pop     ds
  175. asm     pop     es
  176. asm     pop     dx
  177. asm     pop     cx
  178. asm     pop     bx
  179. asm     inc     sp;             /* Don't restore ax */
  180. asm     inc     sp;
  181. asm     iret
  182. }
  183.  
  184.  
  185. /*-----------------------------------------------------------------------*
  186.  
  187. Name            _hardretn - hardware error handler function
  188.  
  189. Usage           void _hardretn(int errcode);
  190.  
  191. Prototype in    dos.h
  192.  
  193. Description     see __harderr
  194.  
  195. *------------------------------------------------------------------------*/
  196. void _hardretn(int retn)
  197. {
  198.         bdos(0x54,0,0);         /* Clean up DOS */
  199.         _SI = retn;             /* Save return code */
  200.         _BP = _SP = Hsav + 24;  /* Restore stack and make it addressable */
  201. asm     pop     ax              /* Get user's AX (AH = DOS function) */
  202.         if (_AH < 0x38)         /* Old-style DOS function? */
  203.         {
  204.                 /* If it's a function that can return an error condition,
  205.                  * set AL to 0xFF and ignore return code (retn).
  206.                  */
  207.                 if ((_AH >= 0x0f && _AH <= 0x13) || _AH == 0x16 || _AH == 0x17
  208.                   || _AH == 0x23 || _AH == 0x29)
  209.                         _AL = 0xff;
  210.         }
  211.         else
  212.         {
  213.                 /* It's a new-style function.  Set the carry flag
  214.                  * and put the return code in AL.
  215.                   */
  216. asm             or      byte ptr 22[bp], 1      /* Set carry flag */
  217.                 _AX = _SI;                      /* Copy retn to AL */
  218.         }
  219. asm     pop     bx
  220. asm     pop     cx
  221. asm     pop     dx
  222. asm     pop     si
  223. asm     pop     di
  224. asm     pop     bp
  225. asm     pop     ds
  226. asm     pop     es
  227. asm     iret                    /* Hop back to user */
  228. }
  229.  
  230.