home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / HARDERR.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  5.2 KB  |  178 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - harderr.cas
  3.  *
  4.  * function(s)
  5.  *        harderr    - establishes a hardware error handler
  6.  *        hardresume - hardware error handler function
  7.  *        hardreturn - hardware error handler function
  8.  *        hentry     - calls user error handler
  9.  *-----------------------------------------------------------------------*/
  10.  
  11. /*[]------------------------------------------------------------[]*/
  12. /*|                                                              |*/
  13. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  14. /*|                                                              |*/
  15. /*|                                                              |*/
  16. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  17. /*|     All Rights Reserved.                                     |*/
  18. /*|                                                              |*/
  19. /*[]------------------------------------------------------------[]*/
  20.  
  21.  
  22. #pragma inline
  23. #include <dos.h>
  24.  
  25. static    int Hsav;
  26. static    int (*Hfunc)(unsigned di, unsigned ax, unsigned bp, unsigned si);
  27.  
  28. /*-----------------------------------------------------------------------*
  29.  
  30. Name        hentry     - calls user error handler
  31.  
  32. Usage        static void interrupt far hentry(bp, di, si, ds, es,
  33.                          dx, cx, bx, ax);
  34.  
  35. Description    calls the user hardware error handler
  36.  
  37. Return value    nothing
  38.  
  39. *------------------------------------------------------------------------*/
  40. #pragma argsused
  41. static void interrupt far hentry(bp, di, si, ds, es, dx, cx, bx, ax)
  42. {
  43.     Hsav = _SP;    /* save the stack pointer */
  44.     ax = (*Hfunc)(di & 0xff, ax, bp, si);
  45. }
  46.  
  47.  
  48. /*-----------------------------------------------------------------------*
  49.  
  50. Name        harderr - establishes a hardware error handler
  51.  
  52. Usage        void harderr(int (*fptr)());
  53.  
  54. Related
  55. functions usage    void hardresume(int rescode);
  56.         void hardretn(int errcode);
  57.  
  58. Prototype in    dos.h
  59.  
  60. Description     harderr establishes a hardware error handler for the
  61.         current program. This handler is invoked whenever an interrupt
  62.         0x24 occurs. (See the MS-DOS Programmer's Reference Manual for a
  63.         discussion of the interrupt.)
  64.  
  65.         The function pointed to by fptr will be called when such an
  66.         interrupt occurs. The handler function will be called with the
  67.         following arguments:
  68.  
  69.            handler(int errval, int ax, int bp, int si);
  70.  
  71.         errval is the error code set in the DI register by MS-DOS.
  72.         ax, bp, and si are the values MS-DOS sets for the AX,
  73.         BP, and SI registers, respectively.
  74.  
  75.         ax indicates whether a disk error or other device error was
  76.         encountered. If ax is non-negative, a disk error was
  77.         encountered; otherwise, the error was a device error. For a
  78.         disk error, ax ANDed with 0x00FF will give the failing drive
  79.         number (1 = A, 2 = B, etc.).
  80.  
  81.         bp and si together point to the device driver header of
  82.         the failing driver.
  83.  
  84.         The named function is not called directly. harderr establishes
  85.         a DOS interrupt handler that calls the function.
  86.  
  87.         peek and peekb can be used to retrieve device information from
  88.         this driver header. bp is the segment address, and si is
  89.         the offset.
  90.  
  91.         The handler may issue bdos calls 1 through 0xC, but any other
  92.         bdos call will corrupt MS-DOS. In particular, any of the C
  93.         standard I/O or UNIX-emulation I/O calls may not be used.
  94.  
  95.           The driver header may not be altered via poke or pokeb.
  96.  
  97.         The error handler may return or call hardresume to return to
  98.         MS-DOS. The return value of the handler or rescode (result code)
  99.           of hardresume contains an abort (2), retry (1), or ignore (0)
  100.         indicator. The abort is accomplished by invoking DOS interrupt
  101.         0x23, the control-break interrupt.
  102.  
  103.         The error handler may return directly to the application
  104.         program by calling hardretn.
  105.  
  106. Return value    The handler must return 0 for ignore, 1 for retry,
  107.         and 2 for abort.
  108.  
  109. Portability    Unique to MS-DOS.
  110.  
  111. See also    peek, poke, setjmp
  112.  
  113. *------------------------------------------------------------------------*/
  114. void    harderr(int (*fptr)())
  115. {
  116.     Hfunc = fptr;
  117.     setvect(0x24, hentry);
  118. }
  119.  
  120.  
  121. /*-----------------------------------------------------------------------*
  122.  
  123. Name        hardresume - hardware error handler function
  124.  
  125. Usage        void hardresume(int rescode);
  126.  
  127. Prototype in    dos.h
  128.  
  129. Description    see harderr
  130.  
  131. *------------------------------------------------------------------------*/
  132. void    hardresume(int axret)
  133. {
  134.     _AX = axret;
  135.     _SP = Hsav;
  136. asm    pop    bp
  137. asm    pop    di
  138. asm    pop    si
  139. asm    pop    ds
  140. asm    pop    es
  141. asm    pop    dx
  142. asm    pop    cx
  143. asm    pop    bx
  144. asm    inc    sp;        /* Don't restore ax */
  145. asm    inc    sp;
  146. asm    iret
  147. }
  148.  
  149.  
  150. /*-----------------------------------------------------------------------*
  151.  
  152. Name        hardretn - hardware error handler function
  153.  
  154. Usage        void hardretn(int errcode);
  155.  
  156. Prototype in    dos.h
  157.  
  158. Description    see harderr
  159.  
  160. *------------------------------------------------------------------------*/
  161. void hardretn(int retn)
  162. {
  163.     bdos(0x54,0,0);        /* Clean up DOS */
  164.     _AX = retn;
  165.     _BP = _SP = Hsav + 26;    /* Restore stack and make it addressable */
  166. asm    or    byte ptr 20[bp], 1    /* Set carry flag */
  167. asm    pop    bx
  168. asm    pop    cx
  169. asm    pop    dx
  170. asm    pop    si
  171. asm    pop    di
  172. asm    pop    bp
  173. asm    pop    ds
  174. asm    pop    es
  175. asm    iret            /* Hop back to user */
  176. }
  177.  
  178.