home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / com / utils / elm / sources / curses.c < prev    next >
C/C++ Source or Header  |  1992-04-06  |  21KB  |  965 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, *_initscreen, *_exitscreen;
  123.  
  124. static int _lines, _columns, _automargin, _eatnewlineglitch, initialized;
  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)
  149.             strcpy(termname, "ansi");
  150.         else
  151.         strcpy(termname, getenv("TERM"));
  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.     _initscreen        = tgetstr("ti", &ptr);
  161.     _exitscreen        = tgetstr("te", &ptr);
  162.     _clearscreen       = tgetstr("cl", &ptr);
  163.     _moveto            = tgetstr("cm", &ptr);
  164.     _up                = tgetstr("up", &ptr);
  165.     _down              = tgetstr("do", &ptr);
  166.     _right             = tgetstr("nd", &ptr);
  167.     _left              = tgetstr("bs", &ptr);
  168.     _setbold           = tgetstr("so", &ptr);
  169.     _clearbold         = tgetstr("se", &ptr);
  170.     _setunderline      = tgetstr("us", &ptr);
  171.     _clearunderline    = tgetstr("ue", &ptr);
  172.     _setinverse        = tgetstr("so", &ptr);
  173.     _clearinverse      = tgetstr("se", &ptr);
  174.     _sethalfbright     = tgetstr("hs", &ptr);
  175.     _clearhalfbright   = tgetstr("he", &ptr);
  176.     _cleartoeoln       = tgetstr("ce", &ptr);
  177.     _cleartoeos        = tgetstr("cd", &ptr);
  178.     _lines                 = tgetnum("li");
  179.     _columns       = tgetnum("co");
  180.     tabspacing       = ((tabspacing=tgetnum("it"))==-1 ? 8 : tabspacing);
  181.     _automargin       = tgetflag("am");
  182.     _eatnewlineglitch   = tgetflag("xn");
  183.     _transmit_on       = tgetstr("ks", &ptr);
  184.     _transmit_off      = tgetstr("ke", &ptr);
  185.     _set_memlock       = tgetstr("ml", &ptr);
  186.     _clear_memlock       = tgetstr("mu", &ptr);
  187.  
  188.  
  189.     if (!_left) {
  190.         _left = "\b";
  191.     }
  192.  
  193.     tputs(_initscreen, 1, outchar);
  194.     fflush(stdout);
  195.         initialized = 1;
  196.  
  197.     return(0);
  198. }
  199.  
  200. ExitScreen()
  201. {
  202.         if ( initialized )
  203.         {
  204.         tputs(_exitscreen, 1, outchar);
  205.         fflush(stdout);
  206.         }
  207. }
  208.  
  209. char *return_value_of(termcap_label)
  210. char *termcap_label;
  211. {
  212.     /** This will return the string kept by termcap for the
  213.         specified capability. Modified to ensure that if
  214.         tgetstr returns a pointer to a transient address
  215.         that we won't bomb out with a later segmentation
  216.         fault (thanks to Dave@Infopro for this one!)
  217.  
  218.         Tweaked to remove padding sequences.
  219.      **/
  220.  
  221.     static char escape_sequence[20];
  222.     register int i=0,j=0;
  223.     char buffer[20];
  224.     char *myptr, *tgetstr();             /* Get termcap capability */
  225.  
  226.     if (strlen(termcap_label) < 2)
  227.       return(NULL);
  228.  
  229.     if (termcap_label[0] == 's' && termcap_label[1] == 'o')
  230.       {
  231.       if (_setinverse)
  232.         strcpy(escape_sequence, _setinverse);
  233.       else
  234.         return( (char *) NULL );
  235.       }
  236.     else if (termcap_label[0] == 's' && termcap_label[1] == 'e')
  237.       {
  238.       if (_clearinverse)
  239.         strcpy(escape_sequence, _clearinverse);
  240.       else
  241.         return( (char *) NULL );
  242.       }
  243.     else if ((myptr = tgetstr(termcap_label, &ptr)) == NULL)
  244.       return( (char *) NULL );
  245.     else
  246.       strcpy(escape_sequence, myptr);
  247.  
  248.     if (chloc(escape_sequence, '$') != -1) {
  249.       while (escape_sequence[i] != '\0') {
  250.         while (escape_sequence[i] != '$' && escape_sequence[i] != '\0')
  251.           buffer[j++] = escape_sequence[i++];
  252.         if (escape_sequence[i] == '$') {
  253.           while (escape_sequence[i] != '>') i++;
  254.           i++;
  255.         }
  256.       }
  257.       buffer[j] = '\0';
  258.       strcpy(escape_sequence, buffer);
  259.     }
  260.  
  261.     return( (char *) escape_sequence);
  262. }
  263.  
  264. transmit_functions(newstate)
  265. int newstate;
  266. {
  267.     /** turn function key transmission to ON | OFF **/
  268.  
  269.     if (newstate != _intransmit) {
  270.         _intransmit = ! _intransmit;
  271.         if (newstate == ON)
  272.           tputs(_transmit_on, 1, outchar);
  273.         else
  274.           tputs(_transmit_off, 1, outchar);
  275.         fflush(stdout);      /* clear the output buffer */
  276.     }
  277. }
  278.  
  279. /****** now into the 'meat' of the routines...the cursor stuff ******/
  280.  
  281. ScreenSize(lines, columns)
  282. int *lines, *columns;
  283. {
  284.     /** returns the number of lines and columns on the display. **/
  285.  
  286. #ifdef OS2
  287.         _ScreenSize(&_lines, &_columns);
  288. #endif
  289. #ifdef TIOCGWINSZ
  290.     struct winsize w;
  291.  
  292.     if (ioctl(1,TIOCGWINSZ,&w) != -1) {
  293.         if (w.ws_row > 0)
  294.             _lines = w.ws_row;
  295.         if (w.ws_col > 0)
  296.             _columns = w.ws_col;
  297.     }
  298. #endif
  299.  
  300.     if (_lines == 0) _lines = DEFAULT_LINES_ON_TERMINAL;
  301.     if (_columns == 0) _columns = DEFAULT_COLUMNS_ON_TERMINAL;
  302.  
  303.     *lines = _lines - 1;        /* assume index from zero */
  304.     *columns = _columns;
  305. }
  306.  
  307. SetXYLocation(x,y)
  308. int x,y;
  309. {
  310.     /* declare where the cursor is on the screen - useful after using
  311.      * a function that moves cursor in predictable fasion but doesn't
  312.      * set the static x and y variables used in this source file -
  313.      * e.g. getpass().
  314.      */
  315.  
  316.     _line = x;
  317.     _col = y;
  318. }
  319.  
  320. GetXYLocation(x,y)
  321. int *x,*y;
  322. {
  323.     /* return the current cursor location on the screen */
  324.  
  325.     *x = _line;
  326.     *y = _col;
  327. }
  328.  
  329. ClearScreen()
  330. {
  331.     /* clear the screen: returns -1 if not capable */
  332.  
  333.     _line = 0;    /* clear leaves us at top... */
  334.     _col  = 0;
  335.  
  336.     if (!_clearscreen)
  337.         return(-1);
  338.  
  339.     tputs(_clearscreen, 1, outchar);
  340.     fflush(stdout);      /* clear the output buffer */
  341.     return(0);
  342. }
  343.  
  344. static
  345. CursorUp(n)
  346. int n;
  347. {
  348.     /** move the cursor up 'n' lines **/
  349.     /** Calling function must check that _up is not null before calling **/
  350.  
  351.     _line = (_line-n > 0? _line - n: 0);    /* up 'n' lines... */
  352.  
  353.     while (n-- > 0)
  354.         tputs(_up, 1, outchar);
  355.  
  356.     fflush(stdout);
  357.     return(0);
  358. }
  359.  
  360.  
  361. static
  362. CursorDown(n)
  363. int n;
  364. {
  365.     /** move the cursor down 'n' lines **/
  366.     /** Caller must check that _down is not null before calling **/
  367.  
  368.     _line = (_line+n <= LINES? _line + n: LINES);    /* down 'n' lines... */
  369.  
  370.     while (n-- > 0)
  371.         tputs(_down, 1, outchar);
  372.  
  373.     fflush(stdout);
  374.     return(0);
  375. }
  376.  
  377.  
  378. static
  379. CursorLeft(n)
  380. int n;
  381. {
  382.     /** move the cursor 'n' characters to the left **/
  383.     /** Caller must check that _left is not null before calling **/
  384.  
  385.     _col = (_col - n> 0? _col - n: 0);    /* left 'n' chars... */
  386.  
  387.     while (n-- > 0)
  388.         tputs(_left, 1, outchar);
  389.  
  390.     fflush(stdout);
  391.     return(0);
  392. }
  393.  
  394.  
  395. static
  396. CursorRight(n)
  397. int n;
  398. {
  399.     /** move the cursor 'n' characters to the right (nondestructive) **/
  400.     /** Caller must check that _right is not null before calling **/
  401.  
  402.     _col = (_col+n < COLUMNS? _col + n: COLUMNS);    /* right 'n' chars... */
  403.  
  404.     while (n-- > 0)
  405.         tputs(_right, 1, outchar);
  406.  
  407.     fflush(stdout);
  408.     return(0);
  409. }
  410.  
  411. static
  412. moveabsolute(col, row)
  413. {
  414.  
  415.     char *stuff, *tgoto();
  416.  
  417.     stuff = tgoto(_moveto, col, row);
  418.     tputs(stuff, 1, outchar);
  419.     fflush(stdout);
  420. }
  421.  
  422. MoveCursor(row, col)
  423. int row, col;
  424. {
  425.     /** move cursor to the specified row column on the screen.
  426.             0,0 is the top left! **/
  427.  
  428.     int scrollafter = 0;
  429.  
  430.     /* we don't want to change "rows" or we'll mangle scrolling... */
  431.  
  432.     if (col < 0)
  433.       col = 0;
  434.     if (col >= COLUMNS)
  435.       col = COLUMNS - 1;
  436.     if (row < 0)
  437.       row = 0;
  438.     if (row > LINES) {
  439.       if (col == 0)
  440.         scrollafter = row - LINES;
  441.       row = LINES;
  442.     }
  443.  
  444.     if (!_moveto)
  445.         return(-1);
  446.  
  447.     if (row == _line) {
  448.       if (col == _col)
  449.         return(0);                /* already there! */
  450.  
  451.       else if (abs(col - _col) < 5) {    /* within 5 spaces... */
  452.         if (col > _col && _right)
  453.           CursorRight(col - _col);
  454.         else if (col < _col &&  _left)
  455.           CursorLeft(_col - col);
  456.         else
  457.           moveabsolute(col, row);
  458.           }
  459.       else         /* move along to the new x,y loc */
  460.         moveabsolute(col, row);
  461.     }
  462.     else if (_line == row-1 && col == 0) {
  463.       if (_col != 0)
  464.         putchar('\r');
  465.       putchar('\n');
  466.       fflush(stdout);
  467.     }
  468.     else if (col == _col && abs(row - _line) < 5) {
  469.       if (row < _line && _up)
  470.         CursorUp(_line - row);
  471.       else if (row > _line && _down)
  472.         CursorDown(row - _line);
  473.       else
  474.         moveabsolute(col, row);
  475.     }
  476.     else
  477.       moveabsolute(col, row);
  478.  
  479.     _line = row;    /* to ensure we're really there... */
  480.     _col  = col;
  481.  
  482.     if (scrollafter) {
  483.       putchar('\r');
  484.       while (scrollafter--)
  485.         putchar('\n');
  486.     }
  487.  
  488.     return(0);
  489. }
  490.  
  491. CarriageReturn()
  492. {
  493.     /** move the cursor to the beginning of the current line **/
  494.     Writechar('\r');
  495. }
  496.  
  497. NewLine()
  498. {
  499.     /** move the cursor to the beginning of the next line **/
  500.  
  501.     Writechar('\r');
  502.     Writechar('\n');
  503. }
  504.  
  505. StartBold()
  506. {
  507.     /** start boldface/standout mode **/
  508.  
  509.     if (!_setbold)
  510.         return(-1);
  511.  
  512.     tputs(_setbold, 1, outchar);
  513.     fflush(stdout);
  514.     return(0);
  515. }
  516.  
  517.  
  518. EndBold()
  519. {
  520.     /** compliment of startbold **/
  521.  
  522.     if (!_clearbold)
  523.         return(-1);
  524.  
  525.     tputs(_clearbold, 1, outchar);
  526.     fflush(stdout);
  527.     return(0);
  528. }
  529.  
  530. #ifndef ELM
  531.  
  532. StartUnderline()
  533. {
  534.     /** start underline mode **/
  535.  
  536.     if (!_setunderline)
  537.         return(-1);
  538.  
  539.     tputs(_setunderline, 1, outchar);
  540.     fflush(stdout);
  541.     return(0);
  542. }
  543.  
  544.  
  545. EndUnderline()
  546. {
  547.     /** the compliment of start underline mode **/
  548.  
  549.     if (!_clearunderline)
  550.         return(-1);
  551.  
  552.     tputs(_clearunderline, 1, outchar);
  553.     fflush(stdout);
  554.     return(0);
  555. }
  556.  
  557.  
  558. StartHalfbright()
  559. {
  560.     /** start half intensity mode **/
  561.  
  562.     if (!_sethalfbright)
  563.         return(-1);
  564.  
  565.     tputs(_sethalfbright, 1, outchar);
  566.     fflush(stdout);
  567.     return(0);
  568. }
  569.  
  570. EndHalfbright()
  571. {
  572.     /** compliment of starthalfbright **/
  573.  
  574.     if (!_clearhalfbright)
  575.         return(-1);
  576.  
  577.     tputs(_clearhalfbright, 1, outchar);
  578.     fflush(stdout);
  579.     return(0);
  580. }
  581.  
  582. StartInverse()
  583. {
  584.     /** set inverse video mode **/
  585.  
  586.     if (!_setinverse)
  587.         return(-1);
  588.  
  589.     tputs(_setinverse, 1, outchar);
  590.     fflush(stdout);
  591.     return(0);
  592. }
  593.  
  594.  
  595. EndInverse()
  596. {
  597.     /** compliment of startinverse **/
  598.  
  599.     if (!_clearinverse)
  600.         return(-1);
  601.  
  602.     tputs(_clearinverse, 1, outchar);
  603.     fflush(stdout);
  604.     return(0);
  605. }
  606.  
  607. int
  608. HasMemlock()
  609. {
  610.     /** returns TRUE iff memory locking is available (a terminal
  611.         feature that allows a specified portion of the screen to
  612.         be "locked" & not cleared/scrolled... **/
  613.  
  614.     return ( _set_memlock && _clear_memlock );
  615. }
  616.  
  617. static int _old_LINES;
  618.  
  619. int
  620. StartMemlock()
  621. {
  622.     /** mark the current line as the "last" line of the portion to
  623.         be memory locked (always relative to the top line of the
  624.         screen) Note that this will alter LINES so that it knows
  625.         the top is locked.  This means that (plus) the program
  626.         will scroll nicely but (minus) End memlock MUST be called
  627.         whenever we leave the locked-memory part of the program! **/
  628.  
  629.     if (! _set_memlock)
  630.       return(-1);
  631.  
  632.     if (! _memory_locked) {
  633.  
  634.       _old_LINES = LINES;
  635.       LINES -= _line;        /* we can't use this for scrolling */
  636.  
  637.       tputs(_set_memlock, 1, outchar);
  638.       fflush(stdout);
  639.       _memory_locked = TRUE;
  640.     }
  641.  
  642.     return(0);
  643. }
  644.  
  645. int
  646. EndMemlock()
  647. {
  648.     /** Clear the locked memory condition...  **/
  649.  
  650.     if (! _set_memlock)
  651.       return(-1);
  652.  
  653.     if (_memory_locked) {
  654.       LINES = _old_LINES;        /* back to old setting */
  655.  
  656.       tputs(_clear_memlock, 1, outchar);
  657.       fflush(stdout);
  658.       _memory_locked = FALSE;
  659.     }
  660.     return(0);
  661. }
  662.  
  663. #endif /* ndef ELM */
  664.  
  665. Writechar(ch)
  666. register int ch;
  667. {
  668.     /** write a character to the current screen location. **/
  669.  
  670.     static int wrappedlastchar = 0;
  671.     int justwrapped, nt;
  672.  
  673.     ch &= 0xFF;
  674.     justwrapped = 0;
  675.  
  676.     /* if return, just go to left column. */
  677.     if(ch == '\r') {
  678.       if (wrappedlastchar)
  679.         justwrapped = 1;                /* preserve wrap flag */
  680.       else {
  681.         putchar('\r');
  682.         _col = 0;
  683.       }
  684.     }
  685.  
  686.     /* if newline and terminal just did a newline without our asking,
  687.      * do nothing, else output a newline and increment the line count */
  688.     else if (ch == '\n') {
  689.       if (!wrappedlastchar) {
  690.         putchar('\n');
  691.         if (_line < LINES)
  692.           ++_line;
  693.       }
  694.     }
  695.  
  696.     /* if backspace, move back  one space  if not already in column 0 */
  697.     else if (ch == BACKSPACE) {
  698.       if(_col != 0) {
  699.         putchar('\b');
  700.         _col--;
  701.       } /* else BACKSPACE does nothing */
  702.     }
  703.  
  704.     /* if bell, ring the bell but don't advance the column */
  705.     else if (ch == '\007') {
  706.       putchar(ch);
  707.     }
  708.  
  709.     /* if a tab, output it */
  710.     else if (ch == '\t') {
  711.       putchar(ch);
  712.       if((nt=next_tab(_col+1)) > prev_tab(COLUMNS))
  713.         _col = COLUMNS-1;
  714.       else
  715.         _col = nt-1;
  716.     }
  717.  
  718.     else {
  719.       /* if some kind of non-printable character change to a '?' */
  720. #ifdef ASCII_CTYPE
  721.       if(!isascii(ch) || !isprint(ch))
  722. #else
  723.       if(!isprint(ch))
  724. #endif
  725.         ch = '?';
  726.  
  727.       /* if we only have one column left, simulate automargins if
  728.        * the terminal doesn't have them */
  729.       if (_col == COLUMNS - 1) {
  730.         putchar(ch);
  731.         if (!_automargin || _eatnewlineglitch) {
  732.           putchar('\r');
  733.           putchar('\n');
  734.         }
  735.         if (_line < LINES)
  736.           ++_line;
  737.         _col = 0;
  738.         justwrapped = 1;
  739.       }
  740.  
  741.       /* if we are here this means we have no interference from the
  742.        * right margin - just output the character and increment the
  743.        * column position. */
  744.       else {
  745.         putchar(ch);
  746.         _col++;
  747.       }
  748.     }
  749.  
  750.     wrappedlastchar = justwrapped;
  751.  
  752.     return(0);
  753. }
  754.  
  755. /*VARARGS2*/
  756.  
  757. Write_to_screen(line, argcount, arg1, arg2, arg3)
  758. char *line;
  759. int   argcount;
  760. char *arg1, *arg2, *arg3;
  761. {
  762.     /** This routine writes to the screen at the current location.
  763.           when done, it increments lines & columns accordingly by
  764.         looking for "\n" sequences... **/
  765.  
  766.     switch (argcount) {
  767.     case 0 :
  768.         PutLine0(_line, _col, line);
  769.         break;
  770.     case 1 :
  771.         PutLine1(_line, _col, line, arg1);
  772.         break;
  773.     case 2 :
  774.         PutLine2(_line, _col, line, arg1, arg2);
  775.         break;
  776.     case 3 :
  777.         PutLine3(_line, _col, line, arg1, arg2, arg3);
  778.         break;
  779.     }
  780. }
  781.  
  782. PutLine0(x, y, line)
  783. int x,y;
  784. register char *line;
  785. {
  786.     /** Write a zero argument line at location x,y **/
  787.  
  788.     MoveCursor(x,y);
  789.     while(*line)
  790.       Writechar(*line++);
  791.     fflush(stdout);
  792. }
  793.  
  794. /*VARARGS2*/
  795. PutLine1(x,y, line, arg1)
  796. int x,y;
  797. char *line;
  798. char *arg1;
  799. {
  800.     /** write line at location x,y - one argument... **/
  801.  
  802.     char buffer[VERY_LONG_STRING];
  803.  
  804.     sprintf(buffer, line, arg1);
  805.  
  806.     PutLine0(x, y, buffer);
  807.         fflush(stdout);
  808. }
  809.  
  810. /*VARARGS2*/
  811. PutLine2(x,y, line, arg1, arg2)
  812. int x,y;
  813. char *line;
  814. char *arg1, *arg2;
  815. {
  816.     /** write line at location x,y - one argument... **/
  817.  
  818.     char buffer[VERY_LONG_STRING];
  819.  
  820.     sprintf(buffer, line, arg1, arg2);
  821.  
  822.     PutLine0(x, y, buffer);
  823.         fflush(stdout);
  824. }
  825.  
  826. /*VARARGS2*/
  827. PutLine3(x,y, line, arg1, arg2, arg3)
  828. int x,y;
  829. char *line;
  830. char *arg1, *arg2, *arg3;
  831. {
  832.     /** write line at location x,y - one argument... **/
  833.  
  834.     char buffer[VERY_LONG_STRING];
  835.  
  836.     sprintf(buffer, line, arg1, arg2, arg3);
  837.  
  838.     PutLine0(x, y, buffer);
  839.         fflush(stdout);
  840. }
  841.  
  842. CleartoEOLN()
  843. {
  844.     /** clear to end of line **/
  845.  
  846.     if (!_cleartoeoln)
  847.         return(-1);
  848.  
  849.     tputs(_cleartoeoln, 1, outchar);
  850.     fflush(stdout);  /* clear the output buffer */
  851.     return(0);
  852. }
  853.  
  854. CleartoEOS()
  855. {
  856.     /** clear to end of screen **/
  857.  
  858.     if (!_cleartoeos)
  859. #ifdef OS2
  860.         {
  861.           int i;
  862.           printf("\033[s\033[K");
  863.           for ( i = _line + 1; i < _lines; i++ )
  864.             printf("\033[%d;1H\033[K", i + 1);
  865.           printf("\033[u");
  866.           fflush(stdout);
  867.         }
  868. #else
  869.         return(-1);
  870. #endif
  871.  
  872.     tputs(_cleartoeos, 1, outchar);
  873.     fflush(stdout);  /* clear the output buffer */
  874.     return(0);
  875. }
  876.  
  877.  
  878. RawState()
  879. {
  880.     /** returns either 1 or 0, for ON or OFF **/
  881.  
  882.     return( _inraw );
  883. }
  884.  
  885. Raw(state)
  886. int state;
  887. {
  888.     /** state is either ON or OFF, as indicated by call **/
  889.  
  890.     if (state == OFF && _inraw) {
  891.       (void) ttsetattr(TTYIN,&_original_tty);
  892.       _inraw = 0;
  893. #ifdef OS2
  894.           setmode(1, O_TEXT);
  895. #endif
  896.     }
  897.     else if (state == ON && ! _inraw) {
  898.  
  899.       (void) ttgetattr(TTYIN, &_original_tty);
  900.       (void) ttgetattr(TTYIN, &_raw_tty);    /** again! **/
  901.  
  902. #if !defined(TERMIO) && !defined(TERMIOS)
  903.       _raw_tty.sg_flags &= ~(ECHO);    /* echo off */
  904.       _raw_tty.sg_flags |= CBREAK;    /* raw on    */
  905. #else
  906.       _raw_tty.c_lflag &= ~(ICANON | ECHO);    /* noecho raw mode        */
  907.  
  908.       _raw_tty.c_cc[VMIN] = '\01';    /* minimum # of chars to queue    */
  909.       _raw_tty.c_cc[VTIME] = '\0';    /* minimum time to wait for input */
  910.  
  911. #endif
  912.       (void) ttsetattr(TTYIN, &_raw_tty);
  913.       _inraw = 1;
  914. #ifdef OS2
  915.           setmode(1, O_BINARY);
  916. #endif
  917.     }
  918. }
  919.  
  920. #ifdef OS2
  921. char cbuffer[1024];
  922. int cbufsize;
  923.  
  924. tflush()
  925. {
  926.         if ( cbufsize ) {
  927.           write(1, cbuffer, cbufsize);
  928.           cbufsize = 0;
  929.         }
  930. }
  931. #endif
  932.  
  933. int
  934. ReadCh()
  935. {
  936.     /** read a character with Raw mode set! **/
  937.  
  938.     register int result;
  939.     char ch;
  940. #ifdef OS2
  941.         tflush();
  942.         return readkey();
  943. #else
  944.     result = read(0, &ch, 1);
  945.         return((result <= 0 ) ? EOF : ch);
  946. #endif
  947. }
  948.  
  949. outchar(c)
  950. char c;
  951. {
  952.     /** output the given character.  From tputs... **/
  953.     /** Note: this CANNOT be a macro!              **/
  954.  
  955. #ifdef OS2
  956.         cbuffer[cbufsize++] = c;
  957.  
  958.         if ( cbufsize == sizeof(cbuffer) )
  959.           tflush();
  960. #else
  961.     putc(c, stdout);
  962. #endif
  963. }
  964.  
  965.