home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / WPUTCH.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  149 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  WPUTCH.C - Redirect text output to a scrollable window
  5. **
  6. **  public domain demo by Gaines Wright
  7. **
  8. **  Note: This code has been tested to work with Borland C++ 4.0+,
  9. **        Microsoft C 7.0+, and Mix Power C 2.2.0+.
  10. **
  11. **        It will *not* work With Zortech C++ or Symantec C++ 6.x.
  12. **
  13. **        It compiles, but does not currently work correctly, with
  14. **        Watcom C 10.0+ or Symantec C++ 7.0+ - this will be fixed ASAP.
  15. */
  16.  
  17. #include <dos.h>
  18. #include <stdlib.h>
  19.  
  20. #if __POWERC | __TURBOC__
  21.   #pragma option -N-
  22.   #define IREGS unsigned r_bp, unsigned r_di, unsigned r_si, unsigned r_ds,\
  23.                 unsigned r_es, unsigned r_dx, unsigned r_cx, unsigned r_bx,\
  24.                 unsigned r_ax, unsigned r_ip, unsigned r_cs, unsigned r_flags
  25.   #ifdef __cplusplus
  26.     #define PAR ...
  27.   #else
  28.     #define PAR void
  29.   #endif
  30.   #ifdef __TURBOC__
  31.        void interrupt far (*oldint29)(PAR);
  32.   #else
  33.     void interrupt (far *oldint29)(PAR);
  34.   #endif
  35.   #define ICAST (void interrupt (far*)(PAR))
  36. #else
  37.   #if (defined(__ZTC__) && !defined(__SC__)) || \
  38.         (defined(__SC__) && __SC__ < 700)
  39.     #error
  40.   #endif
  41.  
  42.   #pragma check_stack(off)
  43.   #pragma check_pointer(off)
  44.   #define IREGS unsigned r_es, unsigned r_ds, unsigned r_di, unsigned r_si,\
  45.                 unsigned r_bp, unsigned r_sp, unsigned r_bx, unsigned r_dx,\
  46.                 unsigned r_cx, unsigned r_ax, unsigned r_ip, unsigned r_cs,\
  47.                 unsigned flags
  48.   void (interrupt far *oldint29)();
  49.   #if !defined(__WATCOMC__)
  50.     #define REGS _REGS
  51.   #endif
  52.   #define ICAST (void (__interrupt far*)())
  53.   #define getvect(a) _dos_getvect(a)
  54.   #define setvect(a,b) _dos_setvect(a,b)
  55.   #define interrupt __interrupt
  56. #endif
  57.  
  58. #define SCRPOS(x,y) ((x)*2+(y)*160)        /* For upper left corner at 0,0. */
  59. #define UP   6
  60. #define DOWN 7
  61.  
  62. char xtop,ytop,xbot,ybot;                  /* Window coordinates. */
  63. char attrib;                               /* Window attribute. */
  64. union REGS iregs,oregs;
  65. char far *topl,far *topr,far *ptr,far *fin;/* Window edge pointers */
  66. char far *screen=(char far*)0xb8000000l;   /* Screen pointer. */
  67.  
  68. void scrollwin(char dir,char lines,char at,char yt,char xt,char yb,char xb)
  69. {
  70.       iregs.h.ah=dir;
  71.       iregs.h.al=lines;
  72.       iregs.h.bh=at;
  73.       iregs.h.ch=yt;
  74.       iregs.h.cl=xt;
  75.       iregs.h.dh=yb;
  76.       iregs.h.dl=xb;
  77.       int86(0x10,&iregs,&oregs);
  78. }
  79.  
  80.  
  81. void setwindow(int xt,int yt,int xb,int yb,char a)
  82. {
  83.       xtop=xt;                            /* Set window coordinates. */
  84.       ytop=yt;
  85.       xbot=xb;
  86.       ybot=yb;
  87.       attrib=a;
  88.       topl=screen+SCRPOS(xtop,ytop); /* Initialize pointers to window edges. */
  89.       topr=screen+SCRPOS(xbot,ytop);
  90.       fin=screen+SCRPOS(xbot,ybot);
  91.       ptr=topl;
  92.       scrollwin(UP,0,attrib,ytop,xtop,ybot,xbot);   /* Clears window */
  93. }
  94.  
  95. void winputch(char c)                     /* Confines output to a window. */
  96. {
  97.       switch(c)
  98.       {
  99.       case '\r':
  100.             return;
  101.  
  102.       case '\n':
  103.             ptr=topr+1;
  104.             break;       /* Treat '\n' as "\r\n". */
  105.  
  106.       case '\t':
  107.             ptr+=16;
  108.             break;          /* Expand tabs. */
  109.  
  110.       default  :
  111.             *ptr++=c;
  112.             ptr++;
  113.             break;  /* Skip attribute byte. */
  114.       }
  115.       if(ptr>fin)                   /* If at bottom of window scroll up. */
  116.       {
  117.             scrollwin(UP,1,attrib,ytop,xtop,ybot,xbot);
  118.             ptr=topl;                     /* Reset back to left edge. */
  119.       }
  120.       else if(ptr>topr)                   /* Wrap around at window right edge. */
  121.       {
  122.             topl+=160;                    /* For a screen width of 80. */
  123.                   topr+=160;
  124.             ptr=topl;
  125.       }
  126. }
  127.       
  128. #ifdef __TURBOC__
  129. #pragma argsused /* shut up 'argument xxx is never used' warnings */
  130. #endif
  131.  
  132. void interrupt far newint29(IREGS)
  133. {
  134.       winputch((char)r_ax);
  135. }
  136.  
  137. main(void)
  138. {
  139.       oldint29=getvect(0x29);
  140.       setvect(0x29,ICAST newint29);
  141.       setwindow(0,0,79,24,7);                   /* Just to clear the screen. */
  142.       setwindow(0,4,45,9,15+(1<<4));
  143.       system("dir");
  144.       setwindow(30,18,74,23,15+(1<<4));
  145.       system("dir");
  146.       setvect(0x29,oldint29);
  147.       return 0;
  148. }
  149.