home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / aix-rs6000 / elm2.3.11.AIX3.1.5.Z / elm2.3.11.AIX3.1.5 / src / curses.c < prev    next >
C/C++ Source or Header  |  1991-11-26  |  19KB  |  905 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: curses.c,v 4.1.1.3 90/07/12 20:18:02 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1.1.3 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    curses.c,v $
  17.  * Revision 4.1.1.3  90/07/12  20:18:02  syd
  18.  * Be sure that output characters are not negative integers.
  19.  * From: tct!chip@uunet.UU.NET (Chip Salzenberg)
  20.  * 
  21.  * Revision 4.1.1.2  90/06/26  20:09:09  syd
  22.  * Add 8 bit support via ascii_ctype
  23.  * From: Robert Claeson <prc@erbe.se>
  24.  * 
  25.  * Revision 4.1.1.1  90/06/05  21:05:22  syd
  26.  * Fixes where src/curses.c attempts to set the screen size via the ioctl
  27.  * TIOCGWINSIZ if it exsists. It should only use the row or col size as
  28.  * returned from the ioctl if they are not 0.
  29.  * From: muller%sdcc10@ucsd.edu (Keith Muller)
  30.  * 
  31.  * Revision 4.1  90/04/28  22:42:39  syd
  32.  * checkin of Elm 2.3 as of Release PL0
  33.  * 
  34.  *
  35.  ******************************************************************************/
  36.  
  37. /**  This library gives programs the ability to easily access the
  38.      termcap information and write screen oriented and raw input
  39.      programs.  The routines can be called as needed, except that
  40.      to use the cursor / screen routines there must be a call to
  41.      InitScreen() first.  The 'Raw' input routine can be used
  42.      independently, however.
  43.  
  44. **/
  45.  
  46. /** NOTE THE ADDITION OF: the #ifndef ELM stuff around routines that
  47.     we don't use.  This is for code size and compile time speed...
  48. **/
  49.  
  50. #include "headers.h"
  51.  
  52. #ifdef TERMIOS
  53. #  include <termios.h>
  54. # if __convex__
  55. #  include <sys/ioctl.h>    /* non-standard, for TIOCGWINSZ */
  56. # endif
  57. #else
  58. # ifdef TERMIO
  59. #  include <termio.h>
  60. # else
  61. #  include <sgtty.h>
  62. # endif
  63. #endif
  64.  
  65. #include <ctype.h>
  66.  
  67. #ifdef PTEM
  68. #  include <sys/types.h>
  69. #  include <sys/stream.h>
  70. #  include <sys/ptem.h>
  71. #endif
  72.  
  73. #ifdef BSD
  74. #undef tolower
  75. #endif
  76.  
  77. #define TTYIN    0
  78.  
  79. #ifdef SHORTNAMES
  80. # define _clearinverse    _clrinv
  81. # define _cleartoeoln    _clrtoeoln
  82. # define _cleartoeos    _clr2eos
  83. # define _transmit_off    xmit_off
  84. # define _transmit_on    xmit_on
  85. #endif
  86.  
  87. #ifdef TERMIOS
  88. struct termios _raw_tty,
  89.            _original_tty;
  90. #define    ttgetattr(fd,where)    tcgetattr((fd),(where))
  91. #define    ttsetattr(fd,where)    tcsetattr((fd),TCSADRAIN,(where))
  92. #else    /*TERMIOS*/
  93. # ifdef TERMIO
  94. struct termio _raw_tty, 
  95.               _original_tty;
  96. #define    ttgetattr(fd,where)    ioctl((fd),TCGETA,(where))
  97. #define    ttsetattr(fd,where)    ioctl((fd),TCSETAW,(where))
  98. # else
  99. struct sgttyb _raw_tty,
  100.           _original_tty;
  101. #define    ttgetattr(fd,where)    ioctl((fd),TIOCGETP,(where))
  102. #define    ttsetattr(fd,where)    ioctl((fd),TIOCSETP,(where))
  103. # endif    /*TERMIO*/
  104. #endif    /*TERMIOS*/
  105.  
  106. static int _inraw = 0;                  /* are we IN rawmode?    */
  107.  
  108. #define DEFAULT_LINES_ON_TERMINAL    24
  109. #define DEFAULT_COLUMNS_ON_TERMINAL    80
  110.  
  111. static int _memory_locked = 0;        /* are we IN memlock??   */
  112. static int _line  = -1,            /* initialize to "trash" */
  113.            _col   = -1;
  114.  
  115. static int _intransmit;            /* are we transmitting keys? */
  116.  
  117. static
  118. char *_clearscreen, *_moveto, *_up, *_down, *_right, *_left,
  119.      *_setbold, *_clearbold, *_setunderline, *_clearunderline, 
  120.      *_sethalfbright, *_clearhalfbright, *_setinverse, *_clearinverse,
  121.      *_cleartoeoln, *_cleartoeos, *_transmit_on, *_transmit_off,
  122.      *_set_memlock, *_clear_memlock;
  123.  
  124. static int _lines, _columns, _automargin, _eatnewlineglitch;
  125. int tabspacing;
  126.  
  127. static char _terminal[1024];              /* Storage for terminal entry */
  128. static char _capabilities[1024];           /* String for cursor motion */
  129.  
  130. static char *ptr = _capabilities;    /* for buffering         */
  131.  
  132. int    outchar();            /* char output for tputs */
  133. char  *tgetstr(),                    /* Get termcap capability */
  134.       *tgoto();                /* and the goto stuff    */
  135.  
  136. InitScreen()
  137. {
  138.     /* Set up all this fun stuff: returns zero if all okay, or;
  139.         -1 indicating no terminal name associated with this shell,
  140.         -2..-n  No termcap for this terminal type known
  141.    */
  142.  
  143.     int  tgetent(),      /* get termcap entry */
  144.          err;
  145.     char termname[40];
  146.     /* char *strcpy(), *getenv(); */
  147.     
  148.     if (getenv("TERM") == NULL) return(-1);
  149.  
  150.     if (strcpy(termname, getenv("TERM")) == NULL)
  151.         return(-1);
  152.  
  153.     if ((err = tgetent(_terminal, termname)) != 1)
  154.         return(err-2);
  155.  
  156.     _line  =  0;        /* where are we right now?? */
  157.     _col   =  0;        /* assume zero, zero...     */
  158.  
  159.     /* load in all those pesky values */
  160.     _clearscreen       = tgetstr("cl", &ptr);
  161.     _moveto            = tgetstr("cm", &ptr);
  162.     _up                = tgetstr("up", &ptr);
  163.     _down              = tgetstr("do", &ptr);
  164.     _right             = tgetstr("nd", &ptr);
  165.     _left              = tgetstr("bs", &ptr);
  166.     _setbold           = tgetstr("so", &ptr);
  167.     _clearbold         = tgetstr("se", &ptr);
  168.     _setunderline      = tgetstr("us", &ptr);
  169.     _clearunderline    = tgetstr("ue", &ptr);
  170.     _setinverse        = tgetstr("so", &ptr);
  171.     _clearinverse      = tgetstr("se", &ptr);
  172.     _sethalfbright     = tgetstr("hs", &ptr);
  173.     _clearhalfbright   = tgetstr("he", &ptr);
  174.     _cleartoeoln       = tgetstr("ce", &ptr);
  175.     _cleartoeos        = tgetstr("cd", &ptr);
  176.     _lines                 = tgetnum("li");
  177.     _columns       = tgetnum("co");
  178.     tabspacing       = ((tabspacing=tgetnum("it"))==-1 ? 8 : tabspacing);
  179.     _automargin       = tgetflag("am");
  180.     _eatnewlineglitch   = tgetflag("xn");
  181.     _transmit_on       = tgetstr("ks", &ptr);
  182.     _transmit_off      = tgetstr("ke", &ptr);
  183.     _set_memlock       = tgetstr("ml", &ptr);
  184.     _clear_memlock       = tgetstr("mu", &ptr);
  185.  
  186.  
  187.     if (!_left) {
  188.         _left = "\b";
  189.     }
  190.  
  191.     return(0);
  192. }
  193.  
  194. char *return_value_of(termcap_label)
  195. char *termcap_label;
  196. {
  197.     /** This will return the string kept by termcap for the 
  198.         specified capability. Modified to ensure that if 
  199.         tgetstr returns a pointer to a transient address    
  200.         that we won't bomb out with a later segmentation
  201.         fault (thanks to Dave@Infopro for this one!)
  202.  
  203.         Tweaked to remove padding sequences.
  204.      **/
  205.  
  206.     static char escape_sequence[20];
  207.     register int i=0,j=0;
  208.     char buffer[20];
  209.     char *myptr, *tgetstr();             /* Get termcap capability */
  210.  
  211.     if (strlen(termcap_label) < 2)
  212.       return(NULL);
  213.  
  214.     if (termcap_label[0] == 's' && termcap_label[1] == 'o')
  215.       {
  216.       if (_setinverse)
  217.         strcpy(escape_sequence, _setinverse);
  218.       else
  219.         return( (char *) NULL );
  220.       }
  221.     else if (termcap_label[0] == 's' && termcap_label[1] == 'e')
  222.       {
  223.       if (_clearinverse)
  224.         strcpy(escape_sequence, _clearinverse);
  225.       else
  226.         return( (char *) NULL );
  227.       }
  228.     else if ((myptr = tgetstr(termcap_label, &ptr)) == NULL)
  229.       return( (char *) NULL );
  230.     else
  231.       strcpy(escape_sequence, myptr);
  232.  
  233.     if (chloc(escape_sequence, '$') != -1) {
  234.       while (escape_sequence[i] != '\0') {
  235.         while (escape_sequence[i] != '$' && escape_sequence[i] != '\0')
  236.           buffer[j++] = escape_sequence[i++];
  237.         if (escape_sequence[i] == '$') {
  238.           while (escape_sequence[i] != '>') i++;
  239.           i++;
  240.         }
  241.       }
  242.       buffer[j] = '\0';
  243.       strcpy(escape_sequence, buffer);
  244.     }
  245.  
  246.     return( (char *) escape_sequence);
  247. }
  248.  
  249. transmit_functions(newstate)
  250. int newstate;
  251. {
  252.     /** turn function key transmission to ON | OFF **/
  253.  
  254.     if (newstate != _intransmit) {
  255.         _intransmit = ! _intransmit;
  256.         if (newstate == ON)
  257.           tputs(_transmit_on, 1, outchar);
  258.         else 
  259.           tputs(_transmit_off, 1, outchar);
  260.         fflush(stdout);      /* clear the output buffer */
  261.     }
  262. }
  263.  
  264. /****** now into the 'meat' of the routines...the cursor stuff ******/
  265.  
  266. ScreenSize(lines, columns)
  267. int *lines, *columns;
  268. {
  269.     /** returns the number of lines and columns on the display. **/
  270.  
  271. #ifdef TIOCGWINSZ
  272.     struct winsize w;
  273.  
  274.     if (ioctl(1,TIOCGWINSZ,&w) != -1) {
  275.         if (w.ws_row > 0)
  276.             _lines = w.ws_row;
  277.         if (w.ws_col > 0)
  278.             _columns = w.ws_col;
  279.     }
  280. #endif
  281.  
  282.     if (_lines == 0) _lines = DEFAULT_LINES_ON_TERMINAL;
  283.     if (_columns == 0) _columns = DEFAULT_COLUMNS_ON_TERMINAL;
  284.  
  285.     *lines = _lines - 1;        /* assume index from zero */
  286.     *columns = _columns;
  287. }
  288.  
  289. SetXYLocation(x,y)
  290. int x,y;
  291. {
  292.     /* declare where the cursor is on the screen - useful after using
  293.      * a function that moves cursor in predictable fasion but doesn't
  294.      * set the static x and y variables used in this source file -
  295.      * e.g. getpass().
  296.      */
  297.  
  298.     _line = x;
  299.     _col = y;
  300. }
  301.  
  302. GetXYLocation(x,y)
  303. int *x,*y;
  304. {
  305.     /* return the current cursor location on the screen */
  306.  
  307.     *x = _line;
  308.     *y = _col;
  309. }
  310.  
  311. ClearScreen()
  312. {
  313.     /* clear the screen: returns -1 if not capable */
  314.  
  315.     _line = 0;    /* clear leaves us at top... */
  316.     _col  = 0;
  317.  
  318.     if (!_clearscreen)
  319.         return(-1);
  320.  
  321.     tputs(_clearscreen, 1, outchar);
  322.     fflush(stdout);      /* clear the output buffer */
  323.     return(0);
  324. }
  325.  
  326. static
  327. CursorUp(n)
  328. int n;
  329. {
  330.     /** move the cursor up 'n' lines **/
  331.     /** Calling function must check that _up is not null before calling **/
  332.  
  333.     _line = (_line-n > 0? _line - n: 0);    /* up 'n' lines... */
  334.  
  335.     while (n-- > 0)
  336.         tputs(_up, 1, outchar);
  337.  
  338.     fflush(stdout);
  339.     return(0);
  340. }
  341.  
  342.  
  343. static
  344. CursorDown(n)
  345. int n;
  346. {
  347.     /** move the cursor down 'n' lines **/
  348.     /** Caller must check that _down is not null before calling **/
  349.  
  350.     _line = (_line+n <= LINES? _line + n: LINES);    /* down 'n' lines... */
  351.  
  352.     while (n-- > 0)
  353.         tputs(_down, 1, outchar);
  354.  
  355.     fflush(stdout);
  356.     return(0);
  357. }
  358.  
  359.  
  360. static
  361. CursorLeft(n)
  362. int n;
  363. {
  364.     /** move the cursor 'n' characters to the left **/
  365.     /** Caller must check that _left is not null before calling **/
  366.  
  367.     _col = (_col - n> 0? _col - n: 0);    /* left 'n' chars... */
  368.  
  369.     while (n-- > 0)
  370.         tputs(_left, 1, outchar);
  371.  
  372.     fflush(stdout);
  373.     return(0);
  374. }
  375.  
  376.  
  377. static
  378. CursorRight(n)
  379. int n;
  380. {
  381.     /** move the cursor 'n' characters to the right (nondestructive) **/
  382.     /** Caller must check that _right is not null before calling **/
  383.  
  384.     _col = (_col+n < COLUMNS? _col + n: COLUMNS);    /* right 'n' chars... */
  385.  
  386.     while (n-- > 0)
  387.         tputs(_right, 1, outchar);
  388.  
  389.     fflush(stdout);
  390.     return(0);
  391. }
  392.  
  393. static
  394. moveabsolute(col, row)
  395. {
  396.  
  397.     char *stuff, *tgoto();
  398.  
  399.     stuff = tgoto(_moveto, col, row);
  400.     tputs(stuff, 1, outchar);
  401.     fflush(stdout);
  402. }
  403.  
  404. MoveCursor(row, col)
  405. int row, col;
  406. {
  407.     /** move cursor to the specified row column on the screen.
  408.             0,0 is the top left! **/
  409.  
  410.     int scrollafter = 0;
  411.  
  412.     /* we don't want to change "rows" or we'll mangle scrolling... */
  413.  
  414.     if (col < 0)
  415.       col = 0;
  416.     if (col >= COLUMNS)
  417.       col = COLUMNS - 1;
  418.     if (row < 0)
  419.       row = 0;
  420.     if (row > LINES) {
  421.       if (col == 0)
  422.         scrollafter = row - LINES;
  423.       row = LINES;
  424.     }
  425.  
  426.     if (!_moveto)
  427.         return(-1);
  428.  
  429.     if (row == _line) {
  430.       if (col == _col)
  431.         return(0);                /* already there! */
  432.  
  433.       else if (abs(col - _col) < 5) {    /* within 5 spaces... */
  434.         if (col > _col && _right)
  435.           CursorRight(col - _col);
  436.         else if (col < _col &&  _left)
  437.           CursorLeft(_col - col);
  438.         else
  439.           moveabsolute(col, row);
  440.           }
  441.       else         /* move along to the new x,y loc */
  442.         moveabsolute(col, row);
  443.     }
  444.     else if (_line == row-1 && col == 0) {
  445.       if (_col != 0)
  446.         putchar('\r');
  447.       putchar('\n');
  448.       fflush(stdout);
  449.     }
  450.     else if (col == _col && abs(row - _line) < 5) {
  451.       if (row < _line && _up)
  452.         CursorUp(_line - row);
  453.       else if (row > _line && _down)
  454.         CursorDown(row - _line);
  455.       else
  456.         moveabsolute(col, row);
  457.     }
  458.     else 
  459.       moveabsolute(col, row);
  460.  
  461.     _line = row;    /* to ensure we're really there... */
  462.     _col  = col;
  463.  
  464.     if (scrollafter) {
  465.       putchar('\r');
  466.       while (scrollafter--)
  467.         putchar('\n');
  468.     }
  469.  
  470.     return(0);
  471. }
  472.  
  473. CarriageReturn()
  474. {
  475.     /** move the cursor to the beginning of the current line **/
  476.     Writechar('\r');
  477. }
  478.  
  479. NewLine()
  480. {
  481.     /** move the cursor to the beginning of the next line **/
  482.  
  483.     Writechar('\r');
  484.     Writechar('\n');
  485. }
  486.  
  487. StartBold()
  488. {
  489.     /** start boldface/standout mode **/
  490.  
  491.     if (!_setbold)
  492.         return(-1);
  493.  
  494.     tputs(_setbold, 1, outchar);
  495.     fflush(stdout);
  496.     return(0);
  497. }
  498.  
  499.  
  500. EndBold()
  501. {
  502.     /** compliment of startbold **/
  503.  
  504.     if (!_clearbold)
  505.         return(-1);
  506.  
  507.     tputs(_clearbold, 1, outchar);
  508.     fflush(stdout);
  509.     return(0);
  510. }
  511.  
  512. #ifndef ELM
  513.  
  514. StartUnderline()
  515. {
  516.     /** start underline mode **/
  517.  
  518.     if (!_setunderline)
  519.         return(-1);
  520.  
  521.     tputs(_setunderline, 1, outchar);
  522.     fflush(stdout);
  523.     return(0);
  524. }
  525.  
  526.  
  527. EndUnderline()
  528. {
  529.     /** the compliment of start underline mode **/
  530.  
  531.     if (!_clearunderline)
  532.         return(-1);
  533.  
  534.     tputs(_clearunderline, 1, outchar);
  535.     fflush(stdout);
  536.     return(0);
  537. }
  538.  
  539.  
  540. StartHalfbright()
  541. {
  542.     /** start half intensity mode **/
  543.  
  544.     if (!_sethalfbright)
  545.         return(-1);
  546.  
  547.     tputs(_sethalfbright, 1, outchar);
  548.     fflush(stdout);
  549.     return(0);
  550. }
  551.  
  552. EndHalfbright()
  553. {
  554.     /** compliment of starthalfbright **/
  555.  
  556.     if (!_clearhalfbright)
  557.         return(-1);
  558.  
  559.     tputs(_clearhalfbright, 1, outchar);
  560.     fflush(stdout);
  561.     return(0);
  562. }
  563.  
  564. StartInverse()
  565. {
  566.     /** set inverse video mode **/
  567.  
  568.     if (!_setinverse)
  569.         return(-1);
  570.  
  571.     tputs(_setinverse, 1, outchar);
  572.     fflush(stdout);
  573.     return(0);
  574. }
  575.  
  576.  
  577. EndInverse()
  578. {
  579.     /** compliment of startinverse **/
  580.  
  581.     if (!_clearinverse)
  582.         return(-1);
  583.  
  584.     tputs(_clearinverse, 1, outchar);
  585.     fflush(stdout);
  586.     return(0);
  587. }
  588.  
  589. int
  590. HasMemlock()
  591. {
  592.     /** returns TRUE iff memory locking is available (a terminal
  593.         feature that allows a specified portion of the screen to
  594.         be "locked" & not cleared/scrolled... **/
  595.  
  596.     return ( _set_memlock && _clear_memlock );
  597. }
  598.  
  599. static int _old_LINES;
  600.  
  601. int
  602. StartMemlock()
  603. {
  604.     /** mark the current line as the "last" line of the portion to 
  605.         be memory locked (always relative to the top line of the
  606.         screen) Note that this will alter LINES so that it knows
  607.         the top is locked.  This means that (plus) the program 
  608.         will scroll nicely but (minus) End memlock MUST be called
  609.         whenever we leave the locked-memory part of the program! **/
  610.  
  611.     if (! _set_memlock)
  612.       return(-1);
  613.  
  614.     if (! _memory_locked) {
  615.  
  616.       _old_LINES = LINES;
  617.       LINES -= _line;        /* we can't use this for scrolling */
  618.  
  619.       tputs(_set_memlock, 1, outchar);
  620.       fflush(stdout);
  621.       _memory_locked = TRUE;
  622.     }
  623.  
  624.     return(0);
  625. }
  626.  
  627. int
  628. EndMemlock()
  629. {
  630.     /** Clear the locked memory condition...  **/
  631.  
  632.     if (! _set_memlock)
  633.       return(-1);
  634.  
  635.     if (_memory_locked) {
  636.       LINES = _old_LINES;        /* back to old setting */
  637.   
  638.       tputs(_clear_memlock, 1, outchar);
  639.       fflush(stdout);
  640.       _memory_locked = FALSE;
  641.     }
  642.     return(0);
  643. }
  644.  
  645. #endif /* ndef ELM */
  646.  
  647. Writechar(ch)
  648. register int ch;
  649. {
  650.     /** write a character to the current screen location. **/
  651.  
  652.     static int wrappedlastchar = 0;
  653.     int justwrapped, nt;
  654.  
  655.     ch &= 0xFF;
  656.     justwrapped = 0;
  657.  
  658.     /* if return, just go to left column. */
  659.     if(ch == '\r') {
  660.       if (wrappedlastchar)
  661.         justwrapped = 1;                /* preserve wrap flag */
  662.       else {
  663.         putchar('\r');
  664.         _col = 0;
  665.       }
  666.     }
  667.  
  668.     /* if newline and terminal just did a newline without our asking,
  669.      * do nothing, else output a newline and increment the line count */
  670.     else if (ch == '\n') {
  671.       if (!wrappedlastchar) {
  672.         putchar('\n');
  673.         if (_line < LINES)
  674.           ++_line;
  675.       }
  676.     }
  677.  
  678.     /* if backspace, move back  one space  if not already in column 0 */
  679.     else if (ch == BACKSPACE) {
  680.       if(_col != 0) {
  681.         putchar('\b');
  682.         _col--;
  683.       } /* else BACKSPACE does nothing */
  684.     }
  685.  
  686.     /* if bell, ring the bell but don't advance the column */
  687.     else if (ch == '\007') {
  688.       putchar(ch);
  689.     }
  690.  
  691.     /* if a tab, output it */
  692.     else if (ch == '\t') {
  693.       putchar(ch);
  694.       if((nt=next_tab(_col+1)) > prev_tab(COLUMNS))
  695.         _col = COLUMNS-1;
  696.       else
  697.         _col = nt-1;
  698.     }
  699.  
  700.     else {
  701.       /* if some kind of non-printable character change to a '?' */
  702. #ifdef ASCII_CTYPE
  703.       if(!isascii(ch) || !isprint(ch))
  704. #else
  705.       if(!isprint(ch))
  706. #endif
  707.         ch = '?';
  708.  
  709.       /* if we only have one column left, simulate automargins if
  710.        * the terminal doesn't have them */
  711.       if (_col == COLUMNS - 1) {
  712.         putchar(ch);
  713.         if (!_automargin || _eatnewlineglitch) {
  714.           putchar('\r');
  715.           putchar('\n');
  716.         }
  717.         if (_line < LINES)
  718.           ++_line;
  719.         _col = 0;
  720.         justwrapped = 1;
  721.       }
  722.  
  723.       /* if we are here this means we have no interference from the
  724.        * right margin - just output the character and increment the
  725.        * column position. */
  726.       else {
  727.         putchar(ch);
  728.         _col++;
  729.       }
  730.     }
  731.  
  732.     wrappedlastchar = justwrapped;
  733.  
  734.     return(0);
  735. }
  736.  
  737. /*VARARGS2*/
  738.  
  739. Write_to_screen(line, argcount, arg1, arg2, arg3)
  740. char *line;
  741. int   argcount; 
  742. char *arg1, *arg2, *arg3;
  743. {
  744.     /** This routine writes to the screen at the current location.
  745.           when done, it increments lines & columns accordingly by
  746.         looking for "\n" sequences... **/
  747.  
  748.     switch (argcount) {
  749.     case 0 :
  750.         PutLine0(_line, _col, line);
  751.         break;
  752.     case 1 :
  753.         PutLine1(_line, _col, line, arg1);
  754.         break;
  755.     case 2 :
  756.         PutLine2(_line, _col, line, arg1, arg2);
  757.         break;
  758.     case 3 :
  759.         PutLine3(_line, _col, line, arg1, arg2, arg3);
  760.         break;
  761.     }
  762. }
  763.  
  764. PutLine0(x, y, line)
  765. int x,y;
  766. register char *line;
  767. {
  768.     /** Write a zero argument line at location x,y **/
  769.  
  770.     MoveCursor(x,y);
  771.     while(*line)
  772.       Writechar(*line++);
  773.     fflush(stdout);
  774. }
  775.  
  776. /*VARARGS2*/
  777. PutLine1(x,y, line, arg1)
  778. int x,y;
  779. char *line;
  780. char *arg1;
  781. {
  782.     /** write line at location x,y - one argument... **/
  783.  
  784.     char buffer[VERY_LONG_STRING];
  785.  
  786.     sprintf(buffer, line, arg1);
  787.  
  788.     PutLine0(x, y, buffer);
  789.         fflush(stdout);
  790. }
  791.  
  792. /*VARARGS2*/
  793. PutLine2(x,y, line, arg1, arg2)
  794. int x,y;
  795. char *line;
  796. char *arg1, *arg2;
  797. {
  798.     /** write line at location x,y - one argument... **/
  799.  
  800.     char buffer[VERY_LONG_STRING];
  801.  
  802.     sprintf(buffer, line, arg1, arg2);
  803.  
  804.     PutLine0(x, y, buffer);
  805.         fflush(stdout);
  806. }
  807.  
  808. /*VARARGS2*/
  809. PutLine3(x,y, line, arg1, arg2, arg3)
  810. int x,y;
  811. char *line;
  812. char *arg1, *arg2, *arg3;
  813. {
  814.     /** write line at location x,y - one argument... **/
  815.  
  816.     char buffer[VERY_LONG_STRING];
  817.  
  818.     sprintf(buffer, line, arg1, arg2, arg3);
  819.  
  820.     PutLine0(x, y, buffer);
  821.         fflush(stdout);
  822. }
  823.  
  824. CleartoEOLN()
  825. {
  826.     /** clear to end of line **/
  827.  
  828.     if (!_cleartoeoln)
  829.         return(-1);
  830.  
  831.     tputs(_cleartoeoln, 1, outchar);
  832.     fflush(stdout);  /* clear the output buffer */
  833.     return(0);
  834. }
  835.  
  836. CleartoEOS()
  837. {
  838.     /** clear to end of screen **/
  839.  
  840.     if (!_cleartoeos)
  841.         return(-1);
  842.  
  843.     tputs(_cleartoeos, 1, outchar);
  844.     fflush(stdout);  /* clear the output buffer */
  845.     return(0);
  846. }
  847.  
  848.  
  849. RawState()
  850. {
  851.     /** returns either 1 or 0, for ON or OFF **/
  852.  
  853.     return( _inraw );
  854. }
  855.  
  856. Raw(state)
  857. int state;
  858. {
  859.     /** state is either ON or OFF, as indicated by call **/
  860.  
  861.     if (state == OFF && _inraw) {
  862.       (void) ttsetattr(TTYIN,&_original_tty);
  863.       _inraw = 0;
  864.     }
  865.     else if (state == ON && ! _inraw) {
  866.  
  867.       (void) ttgetattr(TTYIN, &_original_tty);
  868.       (void) ttgetattr(TTYIN, &_raw_tty);    /** again! **/
  869.  
  870. #if !defined(TERMIO) && !defined(TERMIOS)
  871.       _raw_tty.sg_flags &= ~(ECHO);    /* echo off */
  872.       _raw_tty.sg_flags |= CBREAK;    /* raw on    */
  873. #else
  874.       _raw_tty.c_lflag &= ~(ICANON | ECHO);    /* noecho raw mode        */
  875.  
  876.       _raw_tty.c_cc[VMIN] = '\01';    /* minimum # of chars to queue    */
  877.       _raw_tty.c_cc[VTIME] = '\0';    /* minimum time to wait for input */
  878.  
  879. #endif
  880.       (void) ttsetattr(TTYIN, &_raw_tty);
  881.       _inraw = 1;
  882.     }
  883. }
  884.  
  885. int
  886. ReadCh()
  887. {
  888.     /** read a character with Raw mode set! **/
  889.  
  890.     register int result;
  891.     char ch;
  892.     result = read(0, &ch, 1);
  893.         return((result <= 0 ) ? EOF : ch);
  894. }
  895.  
  896. outchar(c)
  897. char c;
  898. {
  899.     /** output the given character.  From tputs... **/
  900.     /** Note: this CANNOT be a macro!              **/
  901.  
  902.     putc(c, stdout);
  903. }
  904.  
  905.