home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / tctnt / tsrsaf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  2.4 KB  |  73 lines

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