home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / ISC365 / DOSFREE.CPP < prev    next >
C/C++ Source or Header  |  1993-07-31  |  5KB  |  237 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. //                                                                       //
  3. //            File: DosFree.cpp                                          //
  4. //            started on: 28/7/93                                        //
  5. //                                                                       //
  6. ///////////////////////////////////////////////////////////////////////////
  7. //                                                                       //
  8. //                                                                       //
  9. ///////////////////////////////////////////////////////////////////////////
  10.  
  11. #include "dosfree.h";
  12. #include <dos.h>;
  13.  
  14.  
  15. // Abort/Retry/Fail==> Fail.
  16. void TRAP24::isr(IREGS& regs)
  17. {
  18.     regs.h.al= 3;
  19. }
  20.  
  21. // start up.
  22. DOSFREE::DOSFREE()
  23. {
  24.     fRunning= 0;
  25. }
  26.  
  27. // run AppFunc as soon as possible.
  28. void DOSFREE::Go(void)
  29. {
  30.     fRunning= 1;
  31. }
  32.  
  33. // application should overload this function.
  34. void DOSFREE::AppFunc(void)
  35. {
  36. }
  37.  
  38. // local function - insures that AppFunc is running in "protected DOS" session.
  39. void DOSFREE::ShellFunc(void)
  40. {
  41.     static int fInFlag= 0;
  42.  
  43.     if (fInFlag|| !fRunning)
  44.        return;
  45.  
  46.     fInFlag++;
  47.  
  48.     REGS regs;
  49.     SREGS sregs;
  50.  
  51.     // now disable break_checking.
  52.  
  53.     regs.x.ax= 0x3300; // get break state.
  54.     intdos(®s, ®s);
  55.  
  56.     unsigned char bPrevBreak= regs.h.dl;
  57.  
  58.     regs.h.dl= 0;
  59.     regs.x.ax= 0x3301; // set break state.
  60.     intdos(®s, ®s);
  61.  
  62.     // set new psp.
  63.  
  64.     regs.x.ax= 0x5100;    // get current psp.
  65.     intdos(®s, ®s);
  66.  
  67.     unsigned wPrevPSP= regs.x.bx;
  68.  
  69.     regs.x.bx= _psp;
  70.     regs.x.ax= 0x5000;    // set current psp.
  71.     intdos(®s, ®s);
  72.  
  73.  
  74.     // set new DTA area.
  75.  
  76.     regs.x.ax= 0x2f00;              // get DTA area.
  77.     intdosx(®s, ®s, &sregs);
  78.     unsigned wPrevDTAoff= regs.x.bx;
  79.     unsigned wPrevDTAseg= sregs.es;
  80.  
  81.     sregs.ds= _psp;
  82.     regs.x.dx= 0x80;
  83.     regs.x.ax= 0x1a00;
  84.     intdosx(®s, ®s, &sregs);  // set DTA to psp:0x80.
  85.  
  86.  
  87.     // deactivate for next time (once per go)..
  88.     fRunning= 0;
  89.  
  90.  
  91.     // HANDLE EXTENDED ERRORS (and traps!).
  92.     // DOS 3.1 and up!
  93.  
  94.     regs.x.ax= 0x5900;                  // get extended error info.
  95.     regs.x.bx= 0;
  96.     intdosx(®s, ®s, &sregs);
  97.  
  98.  
  99.     struct {
  100.            unsigned ax;
  101.            unsigned bx;
  102.            unsigned cx;
  103.            unsigned dx;
  104.            unsigned si;
  105.            unsigned di;
  106.            unsigned ds;
  107.            unsigned es;
  108.            unsigned dummy[3];
  109.     } ErrorRegs;
  110.  
  111.  
  112.     ErrorRegs.ax= regs.x.ax;
  113.     ErrorRegs.bx= regs.x.bx;
  114.     ErrorRegs.cx= regs.x.cx;
  115.     ErrorRegs.dx= regs.x.dx;
  116.     ErrorRegs.si= regs.x.si;
  117.     ErrorRegs.di= regs.x.di;
  118.     ErrorRegs.es= sregs.es;
  119.     ErrorRegs.ds= sregs.ds;
  120.  
  121.     ErrorRegs.dummy[0]= ErrorRegs.dummy[1]= ErrorRegs.dummy[2]= 0;
  122.  
  123.  
  124.  
  125.     // SET TRAPS --> THIS MIGHT NOT WORK IN LARGER MEM MODELS!!!!!
  126.     TrapCBreak.activate(0x1b);
  127.     TrapMSDOSCriticalError.activate(0x23);
  128.     TrapAbortRetryFail.activate(0x24);
  129.  
  130.     //////////////////////////////////////////////////////////////////////
  131.  
  132.     // run application.
  133.     AppFunc();
  134.  
  135.     //////////////////////////////////////////////////////////////////////
  136.  
  137.     // RESTORE OLD TRAPS.
  138.  
  139.     TrapCBreak.deactivate();
  140.     TrapMSDOSCriticalError.deactivate();
  141.     TrapAbortRetryFail.deactivate();
  142.  
  143.     // RESTORE EXTENDED ERRORS .
  144.  
  145.     regs.x.dx= FP_OFF((void far *)&ErrorRegs);
  146.     sregs.ds=  FP_SEG((void far *)&ErrorRegs);
  147.     regs.x.ax= 0x5d0a;
  148.     intdosx(®s, ®s, &sregs);           // set extended error.
  149.  
  150.  
  151.     // reset to old DTA area.
  152.     sregs.ds=  wPrevDTAseg;
  153.     regs.x.dx= wPrevDTAoff;
  154.     regs.x.ax= 0x1a00;
  155.     intdosx(®s, ®s, &sregs);  // set DTA area to old DTA area.
  156.  
  157.  
  158.     // reset old psp.
  159.     regs.x.bx= wPrevPSP;
  160.     regs.x.ax= 0x5000;
  161.     intdos(®s, ®s); // set old psp.
  162.  
  163.  
  164.     // re-enable break_checking.
  165.  
  166.     regs.h.dl= bPrevBreak;
  167.     regs.x.ax= 0x3301;
  168.     intdos(®s, ®s);
  169.  
  170.     fInFlag--;
  171. }
  172.  
  173. // Timer for DOSFREE.
  174. INT8::INT8()
  175. {
  176.     activate(0x8);
  177.  
  178.     // now check for Dosfree.
  179.  
  180.     REGS regs;
  181.     SREGS sregs;
  182.  
  183.     regs.x.ax= 0x3400;
  184.     intdosx(®s, ®s, &sregs);
  185.  
  186.     // inDOS flag (*inDOS> 0 when forground is using DOS).
  187.     inDOS= (char far *)MK_FP(sregs.es, regs.x.bx);
  188. }
  189.  
  190. // Timer interrupt routine, checks for DOS-free on the forground session.
  191. void INT8::isr(void)
  192. {
  193.     // call old interrupt vector.
  194.     old_vect();
  195.  
  196.     static int fInFlag= 0;
  197.  
  198.     // disallow recursion.
  199.     if (fInFlag)
  200.        return;
  201.  
  202.     fInFlag++;
  203.  
  204.     // allow DOSFREE to hook into the Timer also.
  205.     TimerHook();
  206.  
  207.     // run the shell function (provided that DOS is free).
  208.     if (!*inDOS)
  209.        ShellFunc();
  210.  
  211.     fInFlag--;
  212. }
  213.  
  214. // DOS idle interrupt vector.
  215. void INT28::isr(void)
  216. {
  217.     // keep all the other TSRs in the background working.
  218.     old_vect();
  219.  
  220.     static int fInFlag= 0;
  221.  
  222.     // diallow any recursion.
  223.     if (fInFlag)
  224.        return;
  225.  
  226.     fInFlag++;
  227.  
  228.     // allow DOSFREE to hook into the DOS idle usage.
  229.     DosIdleHook();
  230.  
  231.     // run the shell function.
  232.     ShellFunc();
  233.  
  234.     fInFlag--;
  235. }
  236.  
  237.