home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / pico / osdep / raw.brk < prev    next >
Text File  |  1997-02-05  |  5KB  |  217 lines

  1. /*
  2.  * TTY setup routines. These are the BSD-style routines.
  3.  */
  4.  
  5. static struct sgttyb  _raw_tty,     _original_tty;
  6. static struct ltchars _raw_ltchars, _original_ltchars;
  7. static struct tchars  _raw_tchars,  _original_tchars;
  8. static int            _raw_lmode,   _original_lmode;
  9.  
  10. /*
  11.  * current raw state
  12.  */
  13. static short _inraw = 0;
  14.  
  15. /*
  16.  *  Set up the tty driver
  17.  *
  18.  * Args: state -- which state to put it in. 1 means go into raw (cbreak),
  19.  *                0 out of raw.
  20.  *
  21.  * Result: returns 0 if successful and -1 if not.
  22.  */
  23. int
  24. Raw(state)
  25. int state;
  26. {
  27.     /** state is either ON or OFF, as indicated by call **/
  28.     /* Check return code only on first call. If it fails we're done for and
  29.        if it goes OK the others will probably go OK too. */
  30.  
  31.     if(state == 0 && _inraw){
  32.         /*----- restore state to original -----*/
  33.     if(ioctl(STDIN_FD, TIOCSETP, &_original_tty) < 0)
  34.           return(-1);
  35.  
  36.     (void)ioctl(STDIN_FD, TIOCSLTC, &_original_ltchars);
  37.     (void)ioctl(STDIN_FD, TIOCSETC, &_original_tchars);
  38.         (void)ioctl(STDIN_FD, TIOCLSET, &_original_lmode);
  39.         _inraw = 0;
  40.     }
  41.     else if(state == 1 && ! _inraw){
  42.         /*----- Go into raw mode (cbreak actually) ----*/
  43.         if(ioctl(STDIN_FD, TIOCGETP, &_original_tty) < 0)
  44.           return(-1);
  45.  
  46.     (void)ioctl(STDIN_FD, TIOCGETP, &_raw_tty);   
  47.         (void)ioctl(STDIN_FD, TIOCGETC, &_original_tchars);
  48.     (void)ioctl(STDIN_FD, TIOCGETC, &_raw_tchars);
  49.     (void)ioctl(STDIN_FD, TIOCGLTC, &_original_ltchars);
  50.     (void)ioctl(STDIN_FD, TIOCGLTC, &_raw_ltchars);
  51.         (void)ioctl(STDIN_FD, TIOCLGET, &_original_lmode);
  52.         (void)ioctl(STDIN_FD, TIOCLGET, &_raw_lmode);
  53.  
  54.     _raw_tty.sg_flags &= ~(ECHO);    /* echo off */
  55.     _raw_tty.sg_flags |= CBREAK;    /* raw on    */
  56.         _raw_tty.sg_flags &= ~CRMOD;    /* Turn off CR -> LF mapping */
  57.  
  58.     _raw_tchars.t_intrc = -1;    /* Turn off ^C and ^D */
  59.     _raw_tchars.t_eofc  = -1;
  60.  
  61.     _raw_ltchars.t_lnextc = -1;    /* Turn off ^V so we can use it */
  62.     _raw_ltchars.t_dsuspc = -1;    /* Turn off ^Y so we can use it */
  63.     _raw_ltchars.t_suspc  = -1;    /* Turn off ^Z; we just read 'em */
  64.     _raw_ltchars.t_werasc = -1;    /* Turn off ^w word erase */
  65.     _raw_ltchars.t_rprntc = -1;    /* Turn off ^R reprint line */
  66.         _raw_ltchars.t_flushc = -1;    /* Turn off ^O output flush */
  67.             
  68.     (void)ioctl(STDIN_FD, TIOCSETP, &_raw_tty);
  69.     (void)ioctl(STDIN_FD, TIOCSLTC, &_raw_ltchars);
  70.         (void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
  71.         (void)ioctl(STDIN_FD, TIOCLSET, &_raw_lmode);
  72.     _inraw = 1;
  73.     }
  74.  
  75.     return(0);
  76. }
  77.  
  78.  
  79. /*
  80.  *  Set up the tty driver to use XON/XOFF flow control
  81.  *
  82.  * Args: state -- True to make sure XON/XOFF turned on, FALSE off.
  83.  *
  84.  * Result: none.
  85.  */
  86. void
  87. xonxoff_proc(state)
  88. int state;
  89. {
  90.     if(_inraw){
  91.     if(state){
  92.         if(_raw_tchars.t_startc == -1 || _raw_tchars.t_stopc == -1){
  93.         _raw_tchars.t_startc = 'Q' - '@'; /* Turn ON ^S/^Q */
  94.         _raw_tchars.t_stopc  = 'S' - '@';
  95.         (void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
  96.         }
  97.     }
  98.     else{
  99.         if(!(_raw_tchars.t_startc == -1 && _raw_tchars.t_stopc == -1)){
  100.         _raw_tchars.t_startc = -1;    /* Turn off ^S/^Q */
  101.         _raw_tchars.t_stopc  = -1;
  102.         (void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
  103.         }
  104.     }
  105.     }
  106. }
  107.  
  108.  
  109. /*
  110.  *  Set up the tty driver to do LF->CR translation
  111.  *
  112.  * Args: state -- True to turn on translation, false to write raw LF's
  113.  *
  114.  * Result: none.
  115.  */
  116. void
  117. crlf_proc(state)
  118. int state;
  119. {
  120.     if(_inraw){
  121.     if(state){                /* turn ON NL->CR on output */
  122.         if(!(_raw_tty.sg_flags & CRMOD)){
  123.         _raw_tty.sg_flags |= CRMOD;
  124.         (void)ioctl(STDIN_FD, TIOCSETP, &_raw_tty);
  125.         }
  126.     }
  127.     else{                    /* turn OFF NL-CR on output */
  128.         if(_raw_tty.sg_flags & CRMOD){
  129.         _raw_tty.sg_flags &= ~CRMOD;
  130.         (void)ioctl(STDIN_FD, TIOCSETP, &_raw_tty);
  131.         }
  132.     }
  133.     }
  134. }
  135.  
  136.  
  137. /*
  138.  *  Set up the tty driver to hanle interrupt char
  139.  *
  140.  * Args: state -- True to turn on interrupt char, false to not
  141.  *
  142.  * Result: tty driver that'll send us SIGINT or not
  143.  */
  144. void
  145. intr_proc(state)
  146. int state;
  147. {
  148.     if(_inraw){
  149.     if(state){
  150.         _raw_tchars.t_intrc = ctrl('C');
  151.         (void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
  152.     }
  153.     else{
  154.         _raw_tchars.t_intrc = -1;
  155.         (void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
  156.     }
  157.     }
  158. }
  159.  
  160.  
  161. /*
  162.  * Discard any pending input characters
  163.  *
  164.  * Args:  none
  165.  *
  166.  * Result: pending input buffer flushed
  167.  */
  168. void
  169. flush_input()
  170. {
  171. #ifdef    TIOCFLUSH
  172. #ifdef    FREAD
  173.     int i = FREAD;
  174. #else
  175.     int i = 1;
  176. #endif
  177.     ioctl(STDIN_FD, TIOCFLUSH, &i);
  178. #endif    /* TIOCFLUSH */
  179. }
  180.  
  181.  
  182. /*
  183.  * Turn off hi bit stripping
  184.  */
  185. void
  186. bit_strip_off()
  187. {
  188.     _raw_lmode |= LPASS8;
  189. #ifdef    NXT
  190.     _raw_lmode |= LPASS8OUT;
  191. #endif
  192.     (void)ioctl(STDIN_FD, TIOCLSET, &_raw_lmode);
  193. }
  194.  
  195.  
  196. /*
  197.  * Turn off quit character (^\) if possible
  198.  */
  199. void
  200. quit_char_off()
  201. {
  202.     _raw_tchars.t_quitc = -1;
  203.     (void)ioctl(STDIN_FD, TIOCSETC, &_raw_tchars);
  204. }
  205.  
  206.  
  207. /*
  208.  * Returns TRUE if tty is < 4800, 0 otherwise.
  209.  */
  210. int
  211. ttisslow()
  212. {
  213.     return((int)_raw_tty.sg_ispeed < B4800);
  214. }
  215.  
  216.  
  217.