home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / dtx9203 / borhot / tsrsaf.c < prev   
Encoding:
C/C++ Source or Header  |  1992-03-25  |  2.7 KB  |  82 lines

  1. /* ------------------------------------------------------ */
  2. /*                      TSRSAF.C                          */
  3. /*   Writing Safe Terminate-and-Stay Resident Programs    */
  4. /*              (c) 1991 Borland International            */
  5. /*                   All rights reserved.                 */
  6. /* ------------------------------------------------------ */
  7. /*            veröffentlicht in DOS toolbox 3'92          */
  8. /* ------------------------------------------------------ */
  9.  
  10. #include <dos.h>       // for FP_SEG(), FP_OFF(),
  11.                        // getvect(), keep(),
  12.                        // setvect(), and _psp.
  13. #include <conio.h>     // for wherex(), wherey(),
  14.                        // cputs() and gotoxy()
  15.  
  16. #define INTR 0x09
  17. #define ATTR 0x7900
  18.  
  19. // It is difficult to use printf() or any int21 function
  20. // lower than 0x0C from a TSR.
  21. // Therefore we will use cprintf.
  22.  
  23. extern unsigned _heaplen = 1024;
  24. extern unsigned _stklen  = 512;
  25. unsigned int mystack[3000];
  26.                       // This is the stack which we will use
  27. unsigned int oldSS;   // We'll have to save SS...
  28. unsigned int oldSP;   // and SP.
  29.  
  30. // _9FLAG will be set so that we do not re-enter ourselves.
  31. // If we re-enter, we will not be able to restore the old
  32. // stack.
  33. unsigned int x, y, _9FLAG = 0;
  34.  
  35. // A user defined type for use with getvect() & setvect()
  36. typedef void interrupt far (*intvect)(...);
  37.  
  38. intvect oldhandler;            // to store old interrput
  39.  
  40. /* ------------------------------------------------------ */
  41.  
  42. void interrupt handler(void) {
  43.                              //  to enable C++ compilation.
  44.  
  45.   if (_9FLAG) {              // if were already in here,
  46.     oldhandler();
  47.     return;
  48.   }
  49.  
  50.   _9FLAG = 1;
  51.   x      = wherex();            // get cursor position
  52.   y      = wherey();
  53.   oldSP  = _SP;                 // Save the stack
  54.   oldSS  = _SS;
  55.   _SS    = FP_SEG(mystack);     // switch to our stack
  56.  
  57.   // Since the stack grows to lower addresses, we put the
  58.   // end of our buffer into SP.
  59.   _SP = FP_OFF(&mystack[2998]);
  60.  
  61.   gotoxy(72, 1);                // Switch cursor position
  62.   cputs("Hello");               // put "Hello" on the screen
  63.   oldhandler();
  64.   _SS = oldSS;
  65.   _SP = oldSP;
  66.   gotoxy(x, y);
  67.   _9FLAG = 0;                   // Leaving our handler: now
  68.                                 // ok to enter the function.
  69. } // end of handler()
  70.  
  71. /* ------------------------------------------------------ */
  72.  
  73. int main(void)
  74. {
  75.   oldhandler = (intvect) getvect(INTR);
  76.   setvect(INTR, (intvect)handler);
  77.   keep(0, (_SS + (_SP/16) - _psp));
  78.   return 0;
  79. }
  80. /* ------------------------------------------------------ */
  81. /*                 Ende von TSRSAF.C                      */
  82.