home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / UE311C.ZIP / DISPLAY.C < prev    next >
C/C++ Source or Header  |  1991-10-25  |  37KB  |  1,675 lines

  1. /*
  2.  * The functions in this file handle redisplay. There are two halves, the
  3.  * ones that update the virtual display screen, and the ones that make the
  4.  * physical display screen the same as the virtual display screen. These
  5.  * functions use hints that are left in the windows by the commands.
  6.  *
  7.  */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "eproto.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14.  
  15. typedef struct    VIDEO {
  16.     int    v_flag;         /* Flags */
  17. #if    COLOR
  18.     int    v_fcolor;        /* current forground color */
  19.     int    v_bcolor;        /* current background color */
  20.     int    v_rfcolor;        /* requested forground color */
  21.     int    v_rbcolor;        /* requested background color */
  22. #endif
  23. #if    INSDEL && MEMMAP == 0
  24.     int    v_rline;        /* requested screen line # */
  25. #endif
  26.     char    v_text[1];        /* Screen data. */
  27. }    VIDEO;
  28.  
  29. #define VFCHG    0x0001            /* Changed flag         */
  30. #define VFEXT    0x0002            /* extended (beyond column 80)    */
  31. #define VFREV    0x0004            /* reverse video status     */
  32. #define VFREQ    0x0008            /* reverse video request    */
  33. #define VFCOL    0x0010            /* color change requested    */
  34.  
  35. static VIDEO   **vscreen;               /* Virtual screen. */
  36. #if    MEMMAP == 0
  37. static VIDEO   **pscreen;               /* Physical screen. */
  38. #endif
  39.  
  40. /*    some local function declarations    */
  41.  
  42. #if    PROTO
  43. #if    MEMMAP
  44. extern PASCAL NEAR updateline(int row, struct VIDEO *vp1);
  45. #else
  46. extern PASCAL NEAR updateline(int row, struct VIDEO *vp1, struct VIDEO *vp2);
  47. #endif
  48. #else
  49. extern PASCAL NEAR updateline();
  50. #endif
  51.  
  52. /*
  53.  * Initialize the data structures used by the display code. The edge vectors
  54.  * used to access the screens are set up. The operating system's terminal I/O
  55.  * channel is set up. All the other things get initialized at compile time.
  56.  * The original window has "WFCHG" set, so that it will get completely
  57.  * redrawn on the first call to "update".
  58.  */
  59.  
  60. PASCAL NEAR vtinit()
  61. {
  62.     register int i;
  63.     register VIDEO *vp;
  64.  
  65.     TTopen();        /* open the screen */
  66.     TTkopen();        /* open the keyboard */
  67.     TTrev(FALSE);
  68.  
  69.  
  70.     /* allocate the virtual screen pointer array */
  71.     vscreen = (VIDEO **) malloc(term.t_mrow*sizeof(VIDEO *));
  72.     if (vscreen == NULL)
  73.         meexit(1);
  74.  
  75. #if    MEMMAP == 0
  76.     /* allocate the physical shadow screen array */
  77.     pscreen = (VIDEO **) malloc(term.t_mrow*sizeof(VIDEO *));
  78.     if (pscreen == NULL)
  79.         meexit(1);
  80. #endif
  81.  
  82.     /* for every line in the display */
  83.     for (i = 0; i < term.t_mrow; ++i) {
  84.  
  85.         /* allocate a virtual screen line */
  86.         vp = (VIDEO *) malloc(sizeof(VIDEO)+term.t_mcol);
  87.         if (vp == NULL)
  88.             meexit(1);
  89.  
  90.         vp->v_flag = 0;        /* init change clags */
  91. #if    COLOR
  92.         vp->v_rfcolor = 7;    /* init fore/background colors */
  93.         vp->v_rbcolor = 0;
  94. #endif
  95.         /* connect virtual line to line array */
  96.         vscreen[i] = vp;
  97.  
  98. #if    MEMMAP == 0
  99.         /* allocate and initialize physical shadow screen line */
  100.         vp = (VIDEO *) malloc(sizeof(VIDEO)+term.t_mcol);
  101.         if (vp == NULL)
  102.             meexit(1);
  103.  
  104.         vp->v_flag = 0;
  105. #if    INSDEL
  106.         vp->v_rline = i;    /* set requested line position */
  107. #endif
  108.         pscreen[i] = vp;
  109. #endif
  110.     }
  111. }
  112.  
  113. #if    CLEAN
  114. /* free up all the dynamically allocated video structures */
  115.  
  116. PASCAL NEAR vtfree()
  117. {
  118.     int i;
  119.     for (i = 0; i < term.t_mrow; ++i) {
  120.         free(vscreen[i]);
  121. #if    MEMMAP == 0
  122.         free(pscreen[i]);
  123. #endif
  124.     }
  125.     free(vscreen);
  126. #if    MEMMAP == 0
  127.     free(pscreen);
  128. #endif
  129. }
  130. #endif
  131.  
  132. /*
  133.  * Clean up the virtual terminal system, in anticipation for a return to the
  134.  * operating system. Move down to the last line and clear it out (the next
  135.  * system prompt will be written in the line). Shut down the channel to the
  136.  * terminal.
  137.  */
  138. PASCAL NEAR vttidy()
  139. {
  140.     mlerase();
  141.     movecursor(term.t_nrow, 0);
  142.     TTflush();
  143.     TTclose();
  144.     TTkclose();
  145. }
  146.  
  147. /*
  148.  * Set the virtual cursor to the specified row and column on the virtual
  149.  * screen. There is no checking for nonsense values; this might be a good
  150.  * idea during the early stages.
  151.  */
  152. PASCAL NEAR vtmove(row, col)
  153.  
  154. int row, col;
  155.  
  156. {
  157.     vtrow = row;
  158.     vtcol = col;
  159. }
  160.  
  161. /* Write a character to the virtual screen. The virtual row and
  162.    column are updated. If we are not yet on left edge, don't print
  163.    it yet. If the line is too long put a "$" in the last column.
  164.    This routine only puts printing characters into the virtual
  165.    terminal buffers. Only column overflow is checked.
  166. */
  167.  
  168. PASCAL NEAR vtputc(c)
  169.  
  170. int c;
  171.  
  172. {
  173.     register VIDEO *vp;    /* ptr to line being updated */
  174.  
  175.     vp = vscreen[vtrow];
  176.  
  177.     if (c == '\t') {
  178.         do {
  179.             vtputc(' ');
  180.         } while (((vtcol + taboff) % (tabsize)) != 0);
  181.     } else if (vtcol >= term.t_ncol) {
  182.         ++vtcol;
  183.         vp->v_text[term.t_ncol - 1] = '$';
  184.     } else if (disphigh && c > 0x7f) {
  185.         vtputc('^');
  186.         vtputc('!');
  187.         c -= 0x80;
  188.         if (c == '\t') {
  189.             vtputc('^');
  190.             vtputc('I');
  191.         } else
  192.             vtputc(c);
  193.     } else if (c < 0x20 || c == 0x7F) {
  194.         vtputc('^');
  195.         vtputc(c ^ 0x40);
  196.     } else {
  197.         if (vtcol >= 0)
  198.             vp->v_text[vtcol] = c;
  199.         ++vtcol;
  200.     }
  201. }
  202.  
  203. /*
  204.  * Erase from the end of the software cursor to the end of the line on which
  205.  * the software cursor is located.
  206.  */
  207. PASCAL NEAR vteeol()
  208. {
  209.     register VIDEO    *vp;
  210.  
  211.     vp = vscreen[vtrow];
  212.     while (vtcol < term.t_ncol) {
  213.         if (vtcol >= 0)
  214.         vp->v_text[vtcol] = ' ';
  215.     vtcol++;
  216.     }
  217. }
  218.  
  219. /* upscreen:    user routine to force a screen update
  220.         always finishes complete update     */
  221.  
  222. PASCAL NEAR upscreen(f, n)
  223.  
  224. int f,n;    /* prefix flag and argument */
  225.  
  226. {
  227.     update(TRUE);
  228.     return(TRUE);
  229. }
  230.  
  231. /*
  232.  * Make sure that the display is right. This is a three part process. First,
  233.  * scan through all of the windows looking for dirty ones. Check the framing,
  234.  * and refresh the screen. Second, make sure that "currow" and "curcol" are
  235.  * correct for the current window. Third, make the virtual and physical
  236.  * screens the same.
  237.  */
  238. PASCAL NEAR update(force)
  239.  
  240. int force;    /* force update past type ahead? */
  241.  
  242. {
  243.     register WINDOW *wp;
  244.  
  245. #if    TYPEAH
  246.     if (force == FALSE && typahead())
  247.         return(TRUE);
  248. #endif
  249. #if    VISMAC == 0
  250.     if (force == FALSE && kbdmode == PLAY)
  251.         return(TRUE);
  252. #endif
  253.  
  254.     /* update any windows that need refreshing */
  255.     wp = wheadp;
  256.     while (wp != NULL) {
  257.         if (wp->w_flag) {
  258.             /* if the window has changed, service it */
  259.             reframe(wp);    /* check the framing */
  260.             if ((wp->w_flag & ~WFMODE) == WFEDIT)
  261.                 updone(wp);    /* update EDITed line */
  262.             else if (wp->w_flag & ~WFMOVE)
  263.                 updall(wp);    /* update all lines */
  264.             if (wp->w_flag & WFMODE)
  265.                 modeline(wp);    /* update modeline */
  266.             wp->w_flag = 0;
  267.             wp->w_force = 0;
  268.         }
  269.  
  270. #if    1    /* take this out before the release! */
  271.     if (wp->w_wndp == wheadp) {    /* erroneously circular list */
  272.         wp->w_wndp = (WINDOW *)NULL;
  273.         mlwrite("DAN!!! a bogus circular window list!!!");
  274.         TTgetc();
  275.     }
  276. #endif
  277.         /* on to the next window */
  278.         wp = wp->w_wndp;
  279.     }
  280.  
  281.     /* recalc the current hardware cursor location */
  282.     updpos();
  283.  
  284. #if    MEMMAP
  285.     /* update the cursor and flush the buffers */
  286.     movecursor(currow, curcol - lbound);
  287. #endif
  288.  
  289.     /* check for lines to de-extend */
  290.     upddex();
  291.  
  292.     /* if screen is garbage, re-plot it */
  293.     if (sgarbf != FALSE)
  294.         if (gflags & GFSDRAW)
  295.             sgarbf = FALSE;
  296.         else
  297.             updgar();
  298.  
  299.     /* update the virtual screen to the physical screen */
  300.     updupd(force);
  301.  
  302.     /* update the cursor and flush the buffers */
  303.     movecursor(currow, curcol - lbound);
  304.     TTflush();
  305.  
  306.     return(TRUE);
  307. }
  308.  
  309. /*    reframe:    check to see if the cursor is on in the window
  310.             and re-frame it if needed or wanted        */
  311.  
  312. PASCAL NEAR reframe(wp)
  313.  
  314. WINDOW *wp;
  315.  
  316. {
  317.     register LINE *lp;    /* search pointer */
  318.     register LINE *rp;    /* reverse search pointer */
  319.     register LINE *hp;    /* ptr to header line in buffer */
  320.     register LINE *tp;    /* temp debugging pointer */
  321.     register int i;        /* general index/# lines to scroll */
  322.     register int nlines;    /* number of lines in current window */
  323.  
  324.     /* figure out our window size */
  325.     nlines = wp->w_ntrows;
  326.     if (modeflag == FALSE)
  327.         nlines++;
  328.  
  329.     /* if not a requested reframe, check for a needed one */
  330.     if ((wp->w_flag & WFFORCE) == 0) {
  331.         lp = wp->w_linep;
  332.         for (i = 0; i < nlines; i++) {
  333.  
  334.             /* if the line is in the window, no reframe */
  335.             if (lp == wp->w_dotp)
  336.                 return(TRUE);
  337.  
  338.             /* if we are at the end of the file, reframe */
  339.             if (lp == wp->w_bufp->b_linep)
  340.                 break;
  341.  
  342.             /* on to the next line */
  343.             lp = lforw(lp);
  344.         }
  345.     }
  346.  
  347.     /* reaching here, we need a window refresh */
  348.     i = wp->w_force;
  349.  
  350.     /* if smooth scrolling is enabled,
  351.         first.. have we gone off the top? */
  352.     if (sscroll && ((wp->w_flag & WFFORCE) == 0)) {
  353.         /* search thru the buffer looking for the point */
  354.         tp = lp = rp = wp->w_linep;
  355.         hp = wp->w_bufp->b_linep;
  356.         while ((lp != hp) || (rp != hp)) {
  357.  
  358.             /* did we scroll downward? */
  359.             if (lp == wp->w_dotp) {
  360.                 i = nlines - 1;
  361.                 break;
  362.             }
  363.  
  364.             /* did we scroll upward? */
  365.             if (rp == wp->w_dotp) {
  366.                 i = 0;
  367.                 break;
  368.             }
  369.  
  370.             /* advance forward and back */
  371.             if (lp != hp)
  372.                 lp = lforw(lp);
  373.             if (rp != hp)
  374.                 rp = lback(rp);
  375.  
  376.             /* problems????? */
  377.             if (lp == tp || rp == tp) {
  378.                 mlforce("BUG IN SMOOTH SCROLL--GET DAN!\n");
  379.                 TTgetc();
  380.             }
  381.         }
  382.     /* how far back to reframe? */
  383.     } else if (i > 0) {    /* only one screen worth of lines max */
  384.         if (--i >= nlines)
  385.             i = nlines - 1;
  386.     } else if (i < 0) {    /* negative update???? */
  387.         i += nlines;
  388.         if (i < 0)
  389.             i = 0;
  390.     } else
  391.         i = nlines / 2;
  392.  
  393.     /* backup to new line at top of window */
  394.     lp = wp->w_dotp;
  395.     while (i != 0 && lback(lp) != wp->w_bufp->b_linep) {
  396.         --i;
  397.         if (i < 0) {
  398.             mlforce("OTHER BUG IN DISPLAY --- GET DAN!!!\n");
  399.             TTgetc();
  400.         }
  401.         lp = lback(lp);
  402.     }
  403.  
  404.     /* and reset the current line at top of window */
  405.     wp->w_linep = lp;
  406.     wp->w_flag |= WFHARD;
  407.     wp->w_flag &= ~WFFORCE;
  408.     return(TRUE);
  409. }
  410.  
  411. /*    updone: update the current line to the virtual screen        */
  412.  
  413. PASCAL NEAR updone(wp)
  414.  
  415. WINDOW *wp;    /* window to update current line in */
  416.  
  417. {
  418.     register LINE *lp;    /* line to update */
  419.     register int sline;    /* physical screen line to update */
  420.     register int i;
  421.  
  422.     /* search down the line we want */
  423.     lp = wp->w_linep;
  424.     sline = wp->w_toprow;
  425.     while (lp != wp->w_dotp) {
  426.         ++sline;
  427.         lp = lforw(lp);
  428.     }
  429.  
  430.     /* and update the virtual line */
  431.     vscreen[sline]->v_flag |= VFCHG;
  432.     vscreen[sline]->v_flag &= ~VFREQ;
  433.     taboff = wp->w_fcol;
  434.     vtmove(sline, -taboff);
  435.     for (i=0; i < llength(lp); ++i)
  436.         vtputc(lgetc(lp, i));
  437. #if    COLOR
  438.     vscreen[sline]->v_rfcolor = wp->w_fcolor;
  439.     vscreen[sline]->v_rbcolor = wp->w_bcolor;
  440. #endif
  441.     vteeol();
  442.     taboff = 0;
  443. }
  444.  
  445. /*    updall: update all the lines in a window on the virtual screen */
  446.  
  447. PASCAL NEAR updall(wp)
  448.  
  449. WINDOW *wp;    /* window to update lines in */
  450.  
  451. {
  452.     register LINE *lp;    /* line to update */
  453.     register int sline;    /* physical screen line to update */
  454.     register int i;
  455.     register int nlines;    /* number of lines in the current window */
  456.  
  457.     /* search down the lines, updating them */
  458.     lp = wp->w_linep;
  459.     sline = wp->w_toprow;
  460.     nlines = wp->w_ntrows;
  461.     if (modeflag == FALSE)
  462.         nlines++;
  463.     taboff = wp->w_fcol;
  464.     while (sline < wp->w_toprow + nlines) {
  465.  
  466.         /* and update the virtual line */
  467.         vscreen[sline]->v_flag |= VFCHG;
  468.         vscreen[sline]->v_flag &= ~VFREQ;
  469.         vtmove(sline, -taboff);
  470.         if (lp != wp->w_bufp->b_linep) {
  471.             /* if we are not at the end */
  472.             for (i=0; i < llength(lp); ++i)
  473.                 vtputc(lgetc(lp, i));
  474.             lp = lforw(lp);
  475.         }
  476.  
  477.         /* make sure we are on screen */
  478.         if (vtcol < 0)
  479.             vtcol = 0;
  480.  
  481.         /* on to the next one */
  482. #if    COLOR
  483.         vscreen[sline]->v_rfcolor = wp->w_fcolor;
  484.         vscreen[sline]->v_rbcolor = wp->w_bcolor;
  485. #endif
  486.         vteeol();
  487.         ++sline;
  488.     }
  489.     taboff = 0;
  490. }
  491.  
  492. /*    updpos: update the position of the hardware cursor and handle extended
  493.         lines. This is the only update for simple moves.    */
  494.  
  495. PASCAL NEAR updpos()
  496.  
  497. {
  498.     register LINE *lp;
  499.     register int c;
  500.     register int i;
  501.  
  502.     /* find the current row */
  503.     lp = curwp->w_linep;
  504.     currow = curwp->w_toprow;
  505.     while (lp != curwp->w_dotp) {
  506.         ++currow;
  507.         lp = lforw(lp);
  508.     }
  509.  
  510.     /* find the current column */
  511.     curcol = 0;
  512.     i = 0;
  513.     while (i < curwp->w_doto) {
  514.         c = lgetc(lp, i++);
  515.         if (c == '\t')
  516.             curcol += - (curcol % tabsize) + (tabsize - 1);
  517.         else {
  518.             if (disphigh && c > 0x7f) {
  519.                 curcol += 2;
  520.                 c -= 0x80;
  521.             }
  522.             if (c < 0x20 || c == 0x7f)
  523.                 ++curcol;
  524.         }
  525.         ++curcol;
  526.     }
  527.  
  528.     /* adjust by the current first column position */
  529.     curcol -= curwp->w_fcol;
  530.  
  531.     /* make sure it is not off the left side of the screen */
  532.     while (curcol < 0) {
  533.         if (curwp->w_fcol >= hjump) {
  534.             curcol += hjump;
  535.             curwp->w_fcol -= hjump;
  536.         } else {
  537.             curcol += curwp->w_fcol;
  538.             curwp->w_fcol = 0;
  539.         }
  540.         curwp->w_flag |= WFHARD | WFMODE;
  541.     }
  542.  
  543.     /* if horizontall scrolling is enabled, shift if needed */
  544.     if (hscroll) {
  545.         while (curcol >= term.t_ncol - 1) {
  546.             curcol -= hjump;
  547.             curwp->w_fcol += hjump;
  548.             curwp->w_flag |= WFHARD | WFMODE;
  549.         }
  550.     } else {
  551.     /* if extended, flag so and update the virtual line image */
  552.         if (curcol >=  term.t_ncol - 1) {
  553.             vscreen[currow]->v_flag |= (VFEXT | VFCHG);
  554.             updext();
  555.         } else
  556.             lbound = 0;
  557.     }
  558.  
  559.     /* update the current window if we have to move it around */
  560.     if (curwp->w_flag & WFHARD)
  561.         updall(curwp);
  562.     if (curwp->w_flag & WFMODE)
  563.         modeline(curwp);
  564.     curwp->w_flag = 0;
  565. }
  566.  
  567. /*    upddex: de-extend any line that derserves it        */
  568.  
  569. PASCAL NEAR upddex()
  570.  
  571. {
  572.     register WINDOW *wp;
  573.     register LINE *lp;
  574.     register int i,j;
  575.     register int nlines;    /* number of lines in the current window */
  576.  
  577.     wp = wheadp;
  578.  
  579.     while (wp != NULL) {
  580.         lp = wp->w_linep;
  581.         i = wp->w_toprow;
  582.         nlines = wp->w_ntrows;
  583.         if (modeflag == FALSE)
  584.             nlines++;
  585.  
  586.         while (i < wp->w_toprow + nlines) {
  587.             if (vscreen[i]->v_flag & VFEXT) {
  588.                 if ((wp != curwp) || (lp != wp->w_dotp) ||
  589.                    (curcol < term.t_ncol - 1)) {
  590.                     taboff = wp->w_fcol;
  591.                     vtmove(i, -taboff);
  592.                     for (j = 0; j < llength(lp); ++j)
  593.                         vtputc(lgetc(lp, j));
  594.                     vteeol();
  595.                     taboff = 0;
  596.  
  597.                     /* this line no longer is extended */
  598.                     vscreen[i]->v_flag &= ~VFEXT;
  599.                     vscreen[i]->v_flag |= VFCHG;
  600.                 }
  601.             }
  602.             lp = lforw(lp);
  603.             ++i;
  604.         }
  605.         /* and onward to the next window */
  606.         wp = wp->w_wndp;
  607.     }
  608. }
  609.  
  610. /*    updgar: if the screen is garbage, clear the physical screen and
  611.         the virtual screen and force a full update        */
  612.  
  613. PASCAL NEAR updgar()
  614.  
  615. {
  616.     register int i;
  617. #if    MEMMAP == 0
  618.     register int j;
  619.     register char *txt;
  620. #endif
  621.  
  622.     for (i = 0; i < term.t_nrow; ++i) {
  623.         vscreen[i]->v_flag |= VFCHG;
  624. #if    REVSTA
  625.         vscreen[i]->v_flag &= ~VFREV;
  626. #endif
  627. #if    COLOR
  628.         vscreen[i]->v_fcolor = gfcolor;
  629.         vscreen[i]->v_bcolor = gbcolor;
  630. #endif
  631. #if    MEMMAP == 0
  632.         txt = pscreen[i]->v_text;
  633.         for (j = 0; j < term.t_ncol; ++j)
  634.             txt[j] = ' ';
  635. #endif
  636.     }
  637.  
  638.     movecursor(0, 0);         /* Erase the screen. */
  639.     (*term.t_eeop)();
  640.     sgarbf = FALSE;          /* Erase-page clears */
  641.     mpresf = FALSE;          /* the message area. */
  642. #if    COLOR
  643.     mlerase();            /* needs to be cleared if colored */
  644. #endif
  645. }
  646.  
  647. /*    for simple screen size changes (no window re-allocation involved)
  648.     do the following things
  649. */
  650.  
  651. PASCAL NEAR update_size()
  652.  
  653. {
  654.     /* if we need the size update */
  655.     if ((first_screen->s_roworg != term.t_roworg) |
  656.         (first_screen->s_colorg != term.t_colorg) |
  657.         (first_screen->s_nrow != term.t_nrow) |
  658.         (first_screen->s_ncol != term.t_ncol)) {
  659.  
  660.         /* reset the terminal drivers size concept */
  661.         term.t_roworg = first_screen->s_roworg;
  662.         term.t_colorg = first_screen->s_colorg;
  663.         term.t_nrow = first_screen->s_nrow;
  664.         term.t_ncol = first_screen->s_ncol;
  665.  
  666.         /* make sure the update routines know we need a full update */
  667.         sgarbf = TRUE;
  668.     }
  669. }
  670.  
  671. /*    Display a pop up window.  Page it for the user.  Any key other
  672.     than a space gets pushed back into the input stream to be interpeted
  673.     later as a command.
  674. */
  675.  
  676. #if    PROTO
  677. int PASCAL NEAR pop(BUFFER *popbuf)
  678. #else
  679. int PASCAL NEAR pop(popbuf)
  680.  
  681. BUFFER *popbuf;
  682. #endif
  683. {
  684.     register int index;    /* index into the current output line */
  685.     register int llen;    /* length of the current output line */
  686.     register int cline;    /* current screen line number */
  687.     LINE *lp;    /* ptr to next line to display */
  688.     int numlines;    /* remaining number of lines to display */
  689.     int c;        /* input character */
  690.  
  691.     /* add the barrior line to the end of the pop up buffer */
  692.     addline(popbuf, "------------------------------------------");
  693.  
  694.     /* set up to scan pop up buffer */
  695.     lp = lforw(popbuf->b_linep);
  696.     mlerase();
  697.     numlines = term.t_nrow-2;
  698.     cline = 0;
  699.  
  700.     while (lp != popbuf->b_linep) {
  701.  
  702.         /* update the virtual screen image for this one line */
  703.         vtmove(cline, 0);
  704.         llen = llength(lp);
  705.         for (index = 0; index < llen; index++)
  706.             vtputc(lp->l_text[index]);
  707.         vteeol();
  708. #if    COLOR
  709.         vscreen[cline]->v_rfcolor = gfcolor;
  710.         vscreen[cline]->v_rbcolor = gbcolor;
  711. #endif
  712.         vscreen[cline]->v_flag &= ~VFREQ;
  713.         vscreen[cline++]->v_flag |= VFCHG|VFCOL;
  714.  
  715.         if (numlines-- < 1) {
  716.  
  717.             /* update the virtual screen to the physical screen */
  718.             updupd(FALSE);
  719.  
  720.             /* tell the user there is more */
  721.             mlwrite("--- more ---");
  722.             TTflush();
  723.  
  724.             /* and see if they want more */
  725.             if ((c = tgetc()) != ' ') {
  726.                 cpending = TRUE;
  727.                 charpending = c;
  728.                 upwind();
  729.                 return(TRUE);
  730.             }
  731.  
  732.             /* reset the line counters */
  733.             numlines = term.t_nrow-2;
  734.             cline = 0;
  735.         }
  736.  
  737.         /* on to the next line */
  738.         lp = lforw(lp);
  739.     }
  740.     if (numlines > 0) {
  741.  
  742.         /* update the virtual screen to the physical screen */
  743.         updupd(FALSE);
  744.         TTflush();
  745.  
  746.         if ((c = tgetc()) != ' ') {
  747.             cpending = TRUE;
  748.             charpending = c;
  749.         }
  750.     }
  751.     upwind();
  752.     return(TRUE);
  753. }
  754.  
  755. /*    updupd: update the physical screen from the virtual screen    */
  756.  
  757. PASCAL NEAR updupd(force)
  758.  
  759. int force;    /* forced update flag */
  760.  
  761. {
  762.     register VIDEO *vp1;
  763.     register int i;
  764.  
  765.     for (i = 0; i < term.t_nrow; ++i) {
  766.         vp1 = vscreen[i];
  767.  
  768.         /* for each line that needs to be updated*/
  769.         if ((vp1->v_flag & VFCHG) != 0) {
  770. #if    TYPEAH
  771.             if (force == FALSE && typahead())
  772.                 return(TRUE);
  773. #endif
  774. #if    MEMMAP
  775.             updateline(i, vp1);
  776. #else
  777.             updateline(i, vp1, pscreen[i]);
  778. #endif
  779.         }
  780.     }
  781.     return(TRUE);
  782. }
  783.  
  784. /*    updext: update the extended line which the cursor is currently
  785.         on at a column greater than the terminal width. The line
  786.         will be scrolled right or left to let the user see where
  787.         the cursor is
  788.                                 */
  789. PASCAL NEAR updext()
  790.  
  791. {
  792.     register int rcursor;    /* real cursor location */
  793.     register LINE *lp;    /* pointer to current line */
  794.     register int j;     /* index into line */
  795.  
  796.     /* calculate what column the real cursor will end up in */
  797.     rcursor = ((curcol - term.t_ncol) % term.t_scrsiz)
  798.             + term.t_margin;
  799.     lbound = curcol - rcursor + 1;
  800.     taboff = lbound + curwp->w_fcol;
  801.  
  802.     /* scan through the line outputing characters to the virtual screen */
  803.     /* once we reach the left edge                    */
  804.     vtmove(currow, -taboff); /* start scanning offscreen */
  805.     lp = curwp->w_dotp;        /* line to output */
  806.     for (j=0; j<llength(lp); ++j)    /* until the end-of-line */
  807.         vtputc(lgetc(lp, j));
  808.  
  809.     /* truncate the virtual line, restore tab offset */
  810.     vteeol();
  811.     taboff = 0;
  812.  
  813.     /* and put a '$' in column 1 */
  814.     vscreen[currow]->v_text[0] = '$';
  815. }
  816.  
  817. /*
  818.  * Update a single line. This does not know how to use insert or delete
  819.  * character sequences; we are using VT52 functionality. Update the physical
  820.  * row and column variables. It does try an exploit erase to end of line.
  821.  */
  822. #if    MEMMAP
  823. /*    UPDATELINE specific code for memory mapped displays */
  824.  
  825. PASCAL NEAR updateline(row, vp)
  826.  
  827. int row;        /* row of screen to update */
  828. struct VIDEO *vp;    /* virtual screen image */
  829.  
  830. {
  831.     if (vp->v_flag & VFREQ)
  832.         TTrev(TRUE);
  833. #if    COLOR
  834.     scwrite(row, vp->v_text, vp->v_rfcolor, vp->v_rbcolor);
  835.     vp->v_fcolor = vp->v_rfcolor;
  836.     vp->v_bcolor = vp->v_rbcolor;
  837. #else
  838.     scwrite(row, vp->v_text, 7, 0);
  839. #endif
  840.     if (vp->v_flag & VFREQ)
  841.         TTrev(FALSE);
  842.     vp->v_flag &= ~(VFCHG | VFCOL);    /* flag this line as changed */
  843.  
  844. }
  845.  
  846. #else
  847. PASCAL NEAR updateline(row, vp, pp)
  848.  
  849. int row;        /* row of screen to update */
  850. struct VIDEO *vp;    /* virtual screen image */
  851. struct VIDEO *pp;    /* physical screen image */
  852.  
  853. {
  854.  
  855.     register char *cp1;
  856.     register char *cp2;
  857.     register char *cp3;
  858.     register char *cp4;
  859.     register char *cp5;
  860.     register int nbflag;    /* non-blanks to the right flag? */
  861.     int rev;        /* reverse video flag */
  862.     int req;        /* reverse video request flag */
  863.     int upcol;        /* update column (KRS) */
  864.  
  865.     /* set up pointers to virtual and physical lines */
  866.     cp1 = &vp->v_text[0];
  867.     cp2 = &pp->v_text[0];
  868.  
  869. #if    COLOR
  870.     TTforg(vp->v_rfcolor);
  871.     TTbacg(vp->v_rbcolor);
  872. #endif
  873.  
  874. #if    REVSTA | COLOR
  875.     /* if we need to change the reverse video status of the
  876.        current line, we need to re-write the entire line     */
  877.     rev = (vp->v_flag & VFREV) == VFREV;
  878.     req = (vp->v_flag & VFREQ) == VFREQ;
  879.     if ((rev != req)
  880. #if    COLOR
  881.         || (vp->v_fcolor != vp->v_rfcolor) || (vp->v_bcolor != vp->v_rbcolor)
  882. #endif
  883. #if    HP150
  884.     /* the HP150 has some reverse video problems */
  885.         || req || rev
  886. #endif
  887.             ) {
  888.         movecursor(row, 0);    /* Go to start of line. */
  889.         /* set rev video if needed */
  890.         if (rev != req)
  891.             (*term.t_rev)(req);
  892.  
  893.         /* scan through the line and dump it to the screen and
  894.            the virtual screen array                */
  895.         cp3 = &vp->v_text[term.t_ncol];
  896.         while (cp1 < cp3) {
  897.             TTputc(*cp1);
  898.             ++ttcol;
  899.             *cp2++ = *cp1++;
  900.         }
  901.         /* turn rev video off */
  902.         if (rev != req)
  903.             (*term.t_rev)(FALSE);
  904.  
  905.         /* update the needed flags */
  906.         vp->v_flag &= ~VFCHG;
  907.         if (req)
  908.             vp->v_flag |= VFREV;
  909.         else
  910.             vp->v_flag &= ~VFREV;
  911. #if    COLOR
  912.         vp->v_fcolor = vp->v_rfcolor;
  913.         vp->v_bcolor = vp->v_rbcolor;
  914. #endif
  915.         return(TRUE);
  916.     }
  917. #endif
  918.  
  919.     upcol = 0;
  920.  
  921.     /* advance past any common chars at the left */
  922.     while (cp1 != &vp->v_text[term.t_ncol] && cp1[0] == cp2[0]) {
  923.         ++cp1;
  924.         ++upcol;
  925.         ++cp2;
  926.     }
  927.  
  928. #if    DBCS
  929.     /* don't optimize on the left in the middle of a 2 byte char */
  930.     if ((cp1 > &vp->v_text[0]) && is2byte(vp->v_text, cp1 - 1)) {
  931.         --cp1;
  932.         --upcol;
  933.         --cp2;
  934.     }
  935. #endif
  936.  
  937.  
  938. /* This can still happen, even though we only call this routine on changed
  939.  * lines. A hard update is always done when a line splits, a massive
  940.  * change is done, or a buffer is displayed twice. This optimizes out most
  941.  * of the excess updating. A lot of computes are used, but these tend to
  942.  * be hard operations that do a lot of update, so I don't really care.
  943.  */
  944.     /* if both lines are the same, no update needs to be done */
  945.     if (cp1 == &vp->v_text[term.t_ncol]) {
  946.         vp->v_flag &= ~VFCHG;        /* flag this line is changed */
  947.         return(TRUE);
  948.     }
  949.  
  950.     /* find out if there is a match on the right */
  951.     nbflag = FALSE;
  952.     cp3 = &vp->v_text[term.t_ncol];
  953.     cp4 = &pp->v_text[term.t_ncol];
  954.  
  955.     while (cp3[-1] == cp4[-1]) {
  956.         --cp3;
  957.         --cp4;
  958.         if (cp3[0] != ' ')        /* Note if any nonblank */
  959.             nbflag = TRUE;        /* in right match. */
  960.     }
  961.  
  962. #if    DBCS
  963.     /* don't stop in the middle of a 2 byte char */
  964.     if (is2byte(vp->v_text, cp3-1) || is2byte(pp->v_text, cp4-1)) {
  965.         ++cp3;
  966.         ++cp4;
  967.     }
  968. #endif
  969.  
  970.     cp5 = cp3;
  971.  
  972.     /* Erase to EOL ? */
  973.     if (nbflag == FALSE && eolexist == TRUE && (req != TRUE)) {
  974.         while (cp5!=cp1 && cp5[-1]==' ')
  975.             --cp5;
  976.  
  977.         if (cp3-cp5 <= 3)        /* Use only if erase is */
  978.             cp5 = cp3;        /* fewer characters. */
  979.     }
  980.  
  981.     /* move to the begining of the text to update */
  982.     movecursor(row, upcol);
  983.  
  984. #if    REVSTA
  985.     if (rev)
  986.         TTrev(TRUE);
  987. #endif
  988.  
  989.     while (cp1 != cp5) {        /* Ordinary. */
  990.         TTputc(*cp1);
  991.         ++ttcol;
  992.         *cp2++ = *cp1++;
  993.     }
  994.  
  995.     if (cp5 != cp3) {        /* Erase. */
  996.         TTeeol();
  997.         while (cp1 != cp3)
  998.             *cp2++ = *cp1++;
  999.     }
  1000. #if    REVSTA
  1001.     if (rev)
  1002.         TTrev(FALSE);
  1003. #endif
  1004.     vp->v_flag &= ~VFCHG;        /* flag this line as updated */
  1005.     return(TRUE);
  1006. }
  1007. #endif
  1008.  
  1009. /*
  1010.  * Redisplay the mode line for the window pointed to by the "wp". This is the
  1011.  * only routine that has any idea of how the modeline is formatted. You can
  1012.  * change the modeline format by hacking at this routine. Called by "update"
  1013.  * any time there is a dirty window.
  1014.  */
  1015. PASCAL NEAR modeline(wp)
  1016.  
  1017. WINDOW *wp;    /* window to update modeline for */
  1018.  
  1019. {
  1020.     register char *cp;
  1021.     register int c;
  1022.     register int n;        /* cursor position count */
  1023.     register BUFFER *bp;
  1024.     register int i;        /* loop index */
  1025.     register int lchar;     /* character to draw line in buffer with */
  1026.     register int firstm;    /* is this the first mode? */
  1027.     char tline[NLINE];        /* buffer for part of mode line */
  1028.  
  1029.     /* don't bother if there is none! */
  1030.     if (modeflag == FALSE)
  1031.         return;
  1032.  
  1033.     n = wp->w_toprow+wp->w_ntrows;        /* Location. */
  1034.  
  1035. /*
  1036.     Note that we assume that setting REVERSE will cause the terminal
  1037.     driver to draw with the inverted relationship of fcolor and
  1038.     bcolor, so that when we say to set the foreground color to "white"
  1039.     and background color to "black", the fact that "reverse" is
  1040.     enabled means that the terminal driver actually draws "black" on a
  1041.     background of "white".  Makes sense, no?  This way, devices for
  1042.     which the color controls are optional will still get the "reverse"
  1043.     signals.
  1044. */
  1045.  
  1046.     vscreen[n]->v_flag |= VFCHG | VFREQ | VFCOL;    /* Redraw next time. */
  1047. #if    COLOR
  1048.     vscreen[n]->v_rfcolor = 7;            /* black on */
  1049.     vscreen[n]->v_rbcolor = 0;            /* white.....*/
  1050. #endif
  1051.     vtmove(n, 0);                /* Seek to right line. */
  1052.     if (wp == curwp)            /* mark the current buffer */
  1053.         lchar = '=';
  1054.     else
  1055. #if    REVSTA
  1056.     if (revexist)
  1057.         lchar = ' ';
  1058.     else
  1059. #endif
  1060.         lchar = '-';
  1061.  
  1062.     bp = wp->w_bufp;
  1063.     if ((bp->b_flag&BFTRUNC) != 0)        /* "#" if truncated */
  1064.         vtputc('#');
  1065.     else
  1066.         vtputc(lchar);
  1067.  
  1068.     if ((bp->b_flag&BFCHG) != 0)        /* "*" if changed. */
  1069.         vtputc('*');
  1070.     else
  1071.         vtputc(lchar);
  1072.  
  1073.     if ((bp->b_flag&BFNAROW) != 0) {        /* "<>" if narrowed */
  1074.         vtputc('<');
  1075.         vtputc('>');
  1076.     } else {
  1077.         vtputc(lchar);
  1078.         vtputc(lchar);
  1079.     }
  1080.  
  1081.     n  = 4;
  1082.     strcpy(tline, " ");             /* Buffer name. */
  1083.     strcat(tline, PROGNAME);
  1084.     strcat(tline, " ");
  1085.     strcat(tline, VERSION);
  1086.     strcat(tline, " ");
  1087.  
  1088.     /* are we horizontally scrolled? */
  1089.     if (wp->w_fcol > 0) {
  1090.         strcat(tline, "[<");
  1091.         strcat(tline, int_asc(wp->w_fcol));
  1092.         strcat(tline, "]");
  1093.     }
  1094.  
  1095.     /* display the modes */
  1096.     strcat(tline, "(");
  1097.     firstm = TRUE;
  1098.     for (i = 0; i < NUMMODES; i++)    /* add in the mode flags */
  1099.         if (wp->w_bufp->b_mode & (1 << i)) {
  1100.             if (firstm != TRUE)
  1101.                 strcat(tline, " ");
  1102.             firstm = FALSE;
  1103.             strcat(tline, modename[i]);
  1104.         }
  1105.     strcat(tline,") ");
  1106.  
  1107.     cp = &tline[0];
  1108.     while ((c = *cp++) != 0) {
  1109.         vtputc(c);
  1110.         ++n;
  1111.     }
  1112.  
  1113. #if    0
  1114.     vtputc(lchar);
  1115.     vtputc((wp->w_flag&WFCOLR) != 0  ? 'C' : lchar);
  1116.     vtputc((wp->w_flag&WFMODE) != 0  ? 'M' : lchar);
  1117.     vtputc((wp->w_flag&WFHARD) != 0  ? 'H' : lchar);
  1118.     vtputc((wp->w_flag&WFEDIT) != 0  ? 'E' : lchar);
  1119.     vtputc((wp->w_flag&WFMOVE) != 0  ? 'V' : lchar);
  1120.     vtputc((wp->w_flag&WFFORCE) != 0 ? 'F' : lchar);
  1121.     vtputc(lchar);
  1122.     n += 8;
  1123. #endif
  1124.  
  1125.     vtputc(lchar);
  1126.     vtputc(lchar);
  1127.     vtputc(' ');
  1128.     n += 3;
  1129.     cp = &bp->b_bname[0];
  1130.  
  1131.     while ((c = *cp++) != 0) {
  1132.         vtputc(c);
  1133.         ++n;
  1134.     }
  1135.  
  1136.     vtputc(' ');
  1137.     vtputc(lchar);
  1138.     vtputc(lchar);
  1139.     n += 3;
  1140.  
  1141.     if (bp->b_fname[0] != 0) {    /* File name. */
  1142.         vtputc(' ');
  1143.         ++n;
  1144.         cp = TEXT34;
  1145. /*                   "File: " */
  1146.  
  1147.         while ((c = *cp++) != 0) {
  1148.             vtputc(c);
  1149.             ++n;
  1150.         }
  1151.  
  1152.         cp = &bp->b_fname[0];
  1153.  
  1154.         while ((c = *cp++) != 0) {
  1155.             vtputc(c);
  1156.             ++n;
  1157.             }
  1158.  
  1159.         vtputc(' ');
  1160.         ++n;
  1161.     }
  1162.  
  1163.     while (n < term.t_ncol) {    /* Pad to full width. */
  1164.         vtputc(lchar);
  1165.         ++n;
  1166.     }
  1167. }
  1168.  
  1169. PASCAL NEAR upmode()    /* update all the mode lines */
  1170.  
  1171. {
  1172.     register WINDOW *wp;
  1173.  
  1174.     wp = wheadp;
  1175.     while (wp != NULL) {
  1176.         wp->w_flag |= WFMODE;
  1177.         wp = wp->w_wndp;
  1178.     }
  1179. }
  1180.  
  1181. PASCAL NEAR upwind()    /* force hard updates on all windows */
  1182.  
  1183. {
  1184.     register WINDOW *wp;
  1185.  
  1186.     wp = wheadp;
  1187.     while (wp != NULL) {
  1188.         wp->w_flag |= WFHARD|WFMODE;
  1189.         wp = wp->w_wndp;
  1190.     }
  1191. }
  1192.  
  1193. /*
  1194.  * Send a command to the terminal to move the hardware cursor to row "row"
  1195.  * and column "col". The row and column arguments are origin 0. Optimize out
  1196.  * random calls. Update "ttrow" and "ttcol".
  1197.  */
  1198. PASCAL NEAR movecursor(row, col)
  1199.  
  1200. int row, col;
  1201.  
  1202. {
  1203.     if (row!=ttrow || col!=ttcol) {
  1204.         ttrow = row;
  1205.         ttcol = col;
  1206.         TTmove(row, col);
  1207.     }
  1208. }
  1209.  
  1210. /*
  1211.  * Erase the message line. This is a special routine because the message line
  1212.  * is not considered to be part of the virtual screen. It always works
  1213.  * immediately; the terminal buffer is flushed via a call to the flusher.
  1214.  */
  1215.  
  1216. PASCAL NEAR mlferase()
  1217.  
  1218. {
  1219.     register int save_discmd;
  1220.  
  1221.     save_discmd = discmd;
  1222.     discmd = TRUE;
  1223.     mlerase();
  1224.     discmd = save_discmd;;
  1225. }
  1226.  
  1227. PASCAL NEAR mlerase()
  1228.  
  1229. {
  1230.     int i;
  1231.     
  1232.     movecursor(term.t_nrow, 0);
  1233.     if (discmd == FALSE)
  1234.         return;
  1235.  
  1236. #if    COLOR
  1237.     TTforg(7);
  1238.     TTbacg(gbcolor);
  1239. #endif
  1240.  
  1241.     if (eolexist == TRUE)
  1242.         TTeeol();
  1243.     else {
  1244.         for (i = 0; i < term.t_ncol - 1; i++)
  1245.             TTputc(' ');
  1246.  
  1247.         /* force the move! */
  1248. /*        movecursor(term.t_nrow, 1);*/
  1249.         movecursor(term.t_nrow, 0);
  1250.     }
  1251.     TTflush();
  1252.     mpresf = FALSE;
  1253. }
  1254.  
  1255. /*
  1256.  * Write a message into the message line. Keep track of the physical cursor
  1257.  * position. A small class of printf like format items is handled. Assumes the
  1258.  * stack grows down; this assumption is made by the "+=" in the argument scan
  1259.  * loop. If  STACK_GROWS_UP  is set in estruct.h, then we'll assume that the
  1260.  * stack grows up and use "-=" instead of "+=". Set the "message line"
  1261.  *  flag TRUE.  Don't write beyond the end of the current terminal width.
  1262.  */
  1263.  
  1264. PASCAL NEAR mlout(c)
  1265.  
  1266. int c;    /* character to write */
  1267.  
  1268. {
  1269.     if (ttcol + 1 < term.t_ncol)
  1270.         TTputc(c);
  1271.     if (c != '\b')
  1272.         *lastptr++ = c;
  1273.     else if (lastptr > &lastmesg[0])
  1274.         --lastptr;
  1275. }
  1276.  
  1277. #if    VARARG
  1278. #if    VARG
  1279. CDECL NEAR mlwrite(va_alist)
  1280.  
  1281. va_dcl        /* variable argument list
  1282.             arg1 = format string
  1283.             arg2+ = arguments in that string */
  1284.  
  1285. {
  1286.     register int c;     /* current char in format string */
  1287.     register char *fmt;    /* ptr to format string */
  1288.     register va_list ap;    /* ptr to current data field */
  1289.     int arg_int;        /* integer argument */
  1290.     long arg_long;        /* long argument */
  1291.     char *arg_str;        /* string argument */
  1292.  
  1293.     /* if we are not currently echoing on the command line, abort this */
  1294.     if (discmd == FALSE)
  1295.         return;
  1296.  
  1297. #if    COLOR
  1298.     /* set up the proper colors for the command line */
  1299.     TTforg(7);
  1300.     TTbacg(gbcolor);
  1301. #endif
  1302.  
  1303.     /* point to the first argument */
  1304.     va_start(ap);
  1305.     fmt = va_arg(ap, char *);
  1306.  
  1307.     /* if we can not erase to end-of-line, do it manually */
  1308.     if (eolexist == FALSE) {
  1309.         mlerase();
  1310.         TTflush();
  1311.     }
  1312.  
  1313.     movecursor(term.t_nrow, 0);
  1314.      lastptr = &lastmesg[0];        /* setup to record message */
  1315.     while ((c = *fmt++) != 0) {
  1316.         if (c != '%') {
  1317.             mlout(c);
  1318.             ++ttcol;
  1319.         } else {
  1320.             c = *fmt++;
  1321.             switch (c) {
  1322.                 case 'd':
  1323.                     arg_int = va_arg(ap, int);
  1324.                     mlputi(arg_int, 10);
  1325.                     break;
  1326.  
  1327.                 case 'o':
  1328.                     arg_int = va_arg(ap, int);
  1329.                     mlputi(arg_int, 8);
  1330.                     break;
  1331.  
  1332.                 case 'x':
  1333.                     arg_int = va_arg(ap, int);
  1334.                     mlputi(arg_int, 16);
  1335.                     break;
  1336.  
  1337.                 case 'D':
  1338.                     arg_long = va_arg(ap, long);
  1339.                     mlputli(arg_long, 10);
  1340.                     break;
  1341.  
  1342.                 case 's':
  1343.                     arg_str = va_arg(ap, char *);
  1344.                     mlputs(arg_str);
  1345.                     break;
  1346.  
  1347.                 case 'f':
  1348.                     arg_int = va_arg(ap, int);
  1349.                     mlputf(arg_int);
  1350.                     break;
  1351.  
  1352.                 default:
  1353.                     mlout(c);
  1354.                     ++ttcol;
  1355.             }
  1356.         }
  1357.     }
  1358.  
  1359.     /* if we can, erase to the end of screen */
  1360.     if (eolexist == TRUE)
  1361.         TTeeol();
  1362.     TTflush();
  1363.     mpresf = TRUE;
  1364.     *lastptr = 0;    /* terminate lastmesg[] */
  1365.     va_end(ap);
  1366. }
  1367. #else
  1368. CDECL NEAR mlwrite(char *fmt, ...)
  1369. /* char * fmt;*/
  1370.  
  1371.         /* variable argument list
  1372.             arg1 = format string
  1373.             arg2+ = arguments in that string */
  1374.  
  1375. {
  1376.     register int c;     /* current char in format string */
  1377.     va_list ap;        /* ptr to current data field */
  1378.     int arg_int;        /* integer argument */
  1379.     long arg_long;        /* long argument */
  1380.     char *arg_str;        /* string argument */
  1381.  
  1382.     /* if we are not currently echoing on the command line, abort this */
  1383.     if (discmd == FALSE)
  1384.         return;
  1385.  
  1386. #if    COLOR
  1387.     /* set up the proper colors for the command line */
  1388.     TTforg(7);
  1389.     TTbacg(gbcolor);
  1390. #endif
  1391.  
  1392.     /* point to the first argument */
  1393.     va_start(ap, fmt);
  1394.  
  1395.     /* if we can not erase to end-of-line, do it manually */
  1396.     if (eolexist == FALSE) {
  1397.         mlerase();
  1398.         TTflush();
  1399.     }
  1400.  
  1401.     movecursor(term.t_nrow, 0);
  1402.      lastptr = &lastmesg[0];        /* setup to record message */
  1403.     while ((c = *fmt++) != 0) {
  1404.         if (c != '%') {
  1405.             mlout(c);
  1406.             ++ttcol;
  1407.         } else {
  1408.             c = *fmt++;
  1409.             switch (c) {
  1410.                 case 'd':
  1411.                     arg_int = va_arg(ap, int);
  1412.                     mlputi(arg_int, 10);
  1413.                     break;
  1414.  
  1415.                 case 'o':
  1416.                     arg_int = va_arg(ap, int);
  1417.                     mlputi(arg_int, 8);
  1418.                     break;
  1419.  
  1420.                 case 'x':
  1421.                     arg_int = va_arg(ap, int);
  1422.                     mlputi(arg_int, 16);
  1423.                     break;
  1424.  
  1425.                 case 'D':
  1426.                     arg_long = va_arg(ap, long);
  1427.                     mlputli(arg_long, 10);
  1428.                     break;
  1429.  
  1430.                 case 's':
  1431.                     arg_str = va_arg(ap, char *);
  1432.                     mlputs(arg_str);
  1433.                     break;
  1434.  
  1435.                 case 'f':
  1436.                     arg_int = va_arg(ap, int);
  1437.                     mlputf(arg_int);
  1438.                     break;
  1439.  
  1440.                 default:
  1441.                     mlout(c);
  1442.                     ++ttcol;
  1443.             }
  1444.         }
  1445.     }
  1446.  
  1447.     /* if we can, erase to the end of screen */
  1448.     if (eolexist == TRUE)
  1449.         TTeeol();
  1450.     TTflush();
  1451.     mpresf = TRUE;
  1452.     *lastptr = 0;    /* terminate lastmesg[] */
  1453.     va_end(ap);
  1454. }
  1455. #endif
  1456. #else
  1457.  
  1458. #if    STACK_GROWS_UP
  1459. #define    ADJUST(ptr, dtype)    ptr -= sizeof(dtype)
  1460. #else
  1461. #define    ADJUST(ptr, dtype)    ptr += sizeof(dtype)
  1462. #endif
  1463.  
  1464. CDECL NEAR mlwrite(fmt)
  1465.  
  1466. char *fmt;    /* format string for output */
  1467.  
  1468. {
  1469.     register int c;     /* current char in format string */
  1470.     register char *ap;    /* ptr to current data field */
  1471.  
  1472.     /* if we are not currently echoing on the command line, abort this */
  1473.     if (discmd == FALSE)
  1474.         return;
  1475.  
  1476. #if    COLOR
  1477.     /* set up the proper colors for the command line */
  1478.     TTforg(7);
  1479.     TTbacg(gbcolor);
  1480. #endif
  1481.  
  1482.     /* point to the first argument */
  1483.     ap = &fmt;
  1484.     ADJUST(ap, char *);
  1485.  
  1486.     /* if we can not erase to end-of-line, do it manually */
  1487.     if (eolexist == FALSE) {
  1488.         mlerase();
  1489.         TTflush();
  1490.     }
  1491.  
  1492.     movecursor(term.t_nrow, 0);
  1493.      lastptr = &lastmesg[0];        /* setup to record message */
  1494.     while ((c = *fmt++) != 0) {
  1495.         if (c != '%') {
  1496.             mlout(c);
  1497.             ++ttcol;
  1498.         } else {
  1499.             c = *fmt++;
  1500.             switch (c) {
  1501.                 case 'd':
  1502.                     mlputi(*(int *)ap, 10);
  1503.                             ADJUST(ap, int);
  1504.                     break;
  1505.  
  1506.                 case 'o':
  1507.                     mlputi(*(int *)ap,  8);
  1508.                     ADJUST(ap, int);
  1509.                     break;
  1510.  
  1511.                 case 'x':
  1512.                     mlputi(*(int *)ap, 16);
  1513.                     ADJUST(ap, int);
  1514.                     break;
  1515.  
  1516.                 case 'D':
  1517.                     mlputli(*(long *)ap, 10);
  1518.                     ADJUST(ap, long);
  1519.                     break;
  1520.  
  1521.                 case 's':
  1522.                     mlputs(*(char **)ap);
  1523.                     ADJUST(ap, char *);
  1524.                     break;
  1525.  
  1526.                 case 'f':
  1527.                     mlputf(*(int *)ap);
  1528.                     ADJUST(ap, int);
  1529.                     break;
  1530.  
  1531.                 default:
  1532.                     mlout(c);
  1533.                     ++ttcol;
  1534.             }
  1535.         }
  1536.     }
  1537.  
  1538.     /* if we can, erase to the end of screen */
  1539.     if (eolexist == TRUE)
  1540.         TTeeol();
  1541.     TTflush();
  1542.     mpresf = TRUE;
  1543.     *lastptr = 0;    /* terminate lastmesg[] */
  1544. }
  1545. #endif
  1546. /*    Force a string out to the message line regardless of the
  1547.     current $discmd setting. This is needed when $debug is TRUE
  1548.     and for the write-message and clear-message-line commands
  1549. */
  1550.  
  1551. PASCAL NEAR mlforce(s)
  1552.  
  1553. char *s;    /* string to force out */
  1554.  
  1555. {
  1556.     register int oldcmd;    /* original command display flag */
  1557.  
  1558.     oldcmd = discmd;    /* save the discmd value */
  1559.     discmd = TRUE;        /* and turn display on */
  1560.     mlwrite(s);        /* write the string out */
  1561.     discmd = oldcmd;    /* and restore the original setting */
  1562. }
  1563.  
  1564. /*
  1565.  * Write out a string. Update the physical cursor position. This assumes that
  1566.  * the characters in the string all have width "1"; if this is not the case
  1567.  * things will get screwed up a little.
  1568.  */
  1569.  
  1570. PASCAL NEAR mlputs(s)
  1571.  
  1572. char *s;
  1573.  
  1574. {
  1575.     register int c;
  1576.  
  1577.     while ((c = *s++) != 0) {
  1578.         mlout(c);
  1579.         ++ttcol;
  1580.     }
  1581. }
  1582.  
  1583. /*
  1584.  * Write out an integer, in the specified radix. Update the physical cursor
  1585.  * position.
  1586.  */
  1587. PASCAL NEAR mlputi(i, r)
  1588.  
  1589. int i, r;
  1590.  
  1591.     {
  1592.     register int q;
  1593.     static char hexdigits[] = "0123456789ABCDEF";
  1594.  
  1595.     if (i < 0)
  1596.     {
  1597.     i = -i;
  1598.     mlout('-');
  1599.     }
  1600.  
  1601.     q = i/r;
  1602.  
  1603.     if (q != 0)
  1604.     mlputi(q, r);
  1605.  
  1606.     mlout(hexdigits[i%r]);
  1607.     ++ttcol;
  1608.     }
  1609.  
  1610. /*
  1611.  * do the same except as a long integer.
  1612.  */
  1613. PASCAL NEAR mlputli(l, r)
  1614.  
  1615. long l;
  1616. int r;
  1617.  
  1618.     {
  1619.     register long q;
  1620.  
  1621.     if (l < 0)
  1622.     {
  1623.     l = -l;
  1624.     mlout('-');
  1625.     }
  1626.  
  1627.     q = l/r;
  1628.  
  1629.     if (q != 0)
  1630.     mlputli(q, r);
  1631.  
  1632.     mlout((int)(l%r)+'0');
  1633.     ++ttcol;
  1634.     }
  1635.  
  1636. /*
  1637.  *    write out a scaled integer with two decimal places
  1638.  */
  1639.  
  1640. PASCAL NEAR mlputf(s)
  1641.  
  1642. int s;    /* scaled integer to output */
  1643.  
  1644. {
  1645.     int i;    /* integer portion of number */
  1646.     int f;    /* fractional portion of number */
  1647.  
  1648.     /* break it up */
  1649.     i = s / 100;
  1650.     f = s % 100;
  1651.  
  1652.     /* send out the integer portion */
  1653.     mlputi(i, 10);
  1654.     mlout('.');
  1655.     mlout((f / 10) + '0');
  1656.     mlout((f % 10) + '0');
  1657.     ttcol += 3;
  1658. }       
  1659.  
  1660. #if RAINBOW
  1661.  
  1662. PASCAL NEAR putline(row, col, buf)
  1663.     int row, col;
  1664.     char buf[];
  1665.     {
  1666.     int n;
  1667.  
  1668.     n = strlen(buf);
  1669.     if (col + n - 1 > term.t_ncol)
  1670.     n = term.t_ncol - col + 1;
  1671.     Put_Data(row, col, n, buf);
  1672.     }
  1673. #endif
  1674.  
  1675.