home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / dial / d_pstty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-28  |  5.6 KB  |  232 lines

  1. # include  "util.h"
  2. #ifdef SYS5
  3. # include <termio.h>
  4. # include <fcntl.h>
  5. #else
  6. # include  <sgtty.h>
  7. #endif SYS5
  8. # include  <sys/stat.h>
  9. # include  "d_proto.h"
  10. # include  "d_returns.h"
  11. # include  "d_structs.h"
  12.  
  13. /*
  14.  *  Routines for controlling the tty settings on the control line
  15.  *
  16.  *  Jun 81  D. Crocker      Rewritten to work on v6 & v7
  17.  *  Jul 81  D. Crocker      Make explicit TIOCHPCL call
  18.  *  Dec 82  Doug Kinston    Chmods no longer control return values (DPK@BRL)
  19.  *  Jan 84  D. Rockwell     Make d_ttrestore heed d_savalid
  20.  *                          Save d_savfd instead of FILE *
  21.  */
  22.  
  23. extern int errno;
  24.  
  25. LOCVAR int d_savfd;            /*  handle on the opened tty          */
  26. LOCVAR char d_savfname[25];      /*  name of tty                       */
  27. #ifdef SYS5
  28. LOCVAR struct termio d_savtty;
  29. #else
  30. LOCVAR struct sgttyb d_savtty;   /*  where to stash original setting   */
  31. #endif SYS5
  32. LOCVAR int d_savmode;            /*  protections on tty                */
  33. LOCVAR int d_savalid;
  34.  
  35.  
  36. d_ttsave (fp, fname)              /* save the current setting           */
  37.     FILE *fp;
  38.     char fname[];
  39. {
  40.     struct stat statbuf;
  41.     register int retval;
  42.  
  43.     d_savfd = fileno(fp);
  44.     if (fname != 0)
  45.     (void) strcpy (d_savfname, fname);
  46.     else
  47.     d_savfname[0] = '\0';
  48.  
  49. #ifdef D_DBGLOG
  50.     d_dbglog("d_ttsave", "saving terminal modes, fd = %d", d_savfd);
  51. #endif D_DBGLOG
  52.  
  53. #ifdef SYS5
  54.     if ((retval = ioctl (d_savfd, TCGETA, &d_savtty)) >= 0)
  55. #else
  56.     if ((retval = ioctl (d_savfd, TIOCGETP, &d_savtty)) >= 0)
  57. #endif SYS5
  58.     {
  59.     if ((retval = fstat (d_savfd, &statbuf)) >= 0)
  60.     {
  61.         d_savalid++;
  62.         d_savmode = statbuf.st_mode & ~S_IFMT;
  63.     }
  64.     }
  65.     return (retval);
  66. }
  67.  
  68.  
  69. d_ttrestore ()                    /*  restore initial setting           */
  70. {
  71.     int retval;
  72.  
  73. #ifdef D_DBGLOG
  74.     d_dbglog ("d_ttrestore", "restoring terminal characteristics, fd = %d",
  75.     d_savfd);
  76. #endif D_DBGLOG
  77.  
  78.     if (d_savalid == 0) {
  79. #ifdef D_DBGLOG
  80.     d_dbglog("d_ttrestore", "didn't have to");
  81. #endif D_DBGLOG
  82.         return(0);
  83.     }
  84. #ifdef SYS5
  85.     retval = ioctl (d_savfd, TCSETAW, &d_savtty);
  86. #else
  87.     retval = ioctl (d_savfd, TIOCSETP, &d_savtty);
  88. #endif
  89.     if (!isnull (d_savfname[0]))
  90.     chmod( d_savfname, d_savmode );
  91.  
  92.     d_savalid = 0;
  93.     return (retval);
  94. }
  95.  
  96. d_ttscript (baudrate)             /* tty will be following call script  */
  97.     int baudrate;
  98. {
  99. #ifdef SYS5
  100.     struct termio sttybuf;
  101.     extern unsigned short d_scbitc, d_scbiti, d_scbito, d_scbitl;
  102.     int rc;
  103. #else
  104.     struct sgttyb sttybuf;
  105.     extern int d_scon, d_scoff;
  106. #endif SYS5
  107.  
  108.     if (!isnull (d_savfname[0]))
  109.     chmod( d_savfname, 0600 );
  110.  
  111. #ifdef SYS5
  112.     ioctl(d_savfd, TCGETA, &sttybuf);
  113. #endif SYS5
  114.  
  115.     if (baudrate != 0)
  116.     {                             /*  use special speed                 */
  117. #ifdef SYS5
  118.     sttybuf.c_cflag = (baudrate & CBAUD);
  119. #else
  120.     sttybuf.sg_ispeed = baudrate;
  121.     sttybuf.sg_ospeed = baudrate;
  122. #endif SYS5
  123.     }
  124.     else
  125.     {                             /*  use current speed                 */
  126. #ifdef SYS5
  127.     sttybuf.c_cflag = (d_savtty.c_cflag & CBAUD);
  128. #else
  129.     sttybuf.sg_ispeed = d_savtty.sg_ispeed;
  130.     sttybuf.sg_ospeed = d_savtty.sg_ospeed;
  131. #endif SYS5
  132.     }
  133.  
  134. #ifdef SYS5
  135.     sttybuf.c_cc[VERASE] = '\010';    /* erase control-h */
  136.     sttybuf.c_cc[VKILL] = '\030';    /* kill control-x */
  137.     sttybuf.c_cc[VTIME] = 0;        /* delay */
  138.     sttybuf.c_cc[VMIN] = 1;        /* hi water mark */
  139.  
  140.     sttybuf.c_cflag |= d_scbitc;
  141.     sttybuf.c_iflag = d_scbiti;
  142.     sttybuf.c_oflag = d_scbito;
  143.     sttybuf.c_lflag = d_scbitl;
  144.  
  145. #else
  146.     sttybuf.sg_erase  = '\010';   /* erase = control-H                  */
  147.     sttybuf.sg_kill   = '\030';   /* kill = control-X                   */
  148.     sttybuf.sg_flags  |=  d_scon;
  149.                   /* force on  special bits for script  */
  150.     sttybuf.sg_flags  &=  ~((int) d_scoff);
  151.                   /* force off special bits for script  */
  152. #endif SYS5
  153.  
  154. #ifdef SYS5
  155.     rc = ioctl (d_savfd, TCSETAW, &sttybuf);
  156.     fcntl (d_savfd, F_SETFL, fcntl (d_savfd, F_GETFL, 0) & ~O_NDELAY);
  157.     return (rc);
  158. #else
  159.     return (ioctl (d_savfd, TIOCSETP, &sttybuf));
  160. #endif SYS5
  161. }
  162.  
  163.  
  164. d_ttproto (baudrate)
  165.     int baudrate;
  166. {
  167. #ifdef SYS5
  168.     struct termio sttybuf;
  169.     extern unsigned short d_prbitc, d_prbiti, d_prbito, d_prbitl;
  170.     int rc;
  171. #else
  172.     struct sgttyb sttybuf;
  173.     extern int d_pron, d_proff;
  174. #endif SYS5
  175.  
  176.     if (!isnull (d_savfname[0]))
  177.     chmod( d_savfname, 0600 );
  178.  
  179. #ifdef SYS5
  180.     ioctl (d_savfd, TCGETA, &sttybuf);
  181. #endif
  182.  
  183.     if (baudrate != 0)
  184.     {                             /*  use special speed                 */
  185. #ifdef SYS5
  186.     sttybuf.c_cflag = (baudrate & CBAUD);
  187. #else
  188.     sttybuf.sg_ispeed = baudrate;
  189.     sttybuf.sg_ospeed = baudrate;
  190. #endif SYS5
  191.     }
  192.     else
  193.     {                             /*  use current speed                 */
  194. #ifdef SYS5
  195.     sttybuf.c_cflag = (d_savtty.c_cflag & CBAUD);
  196. #else
  197.     sttybuf.sg_ispeed = d_savtty.sg_ispeed;
  198.     sttybuf.sg_ospeed = d_savtty.sg_ospeed;
  199. #endif SYS5
  200.     }
  201.  
  202. #ifdef SYS5
  203.     sttybuf.c_cc[VERASE] = '\010';    /* erase control-h */
  204.     sttybuf.c_cc[VKILL] = '\030';    /* kill control-x */
  205.     sttybuf.c_cc[VEOL] = '\n';
  206.     sttybuf.c_cc[VEOF] = '\004';
  207.  
  208.     sttybuf.c_cflag |= d_prbitc;
  209.     sttybuf.c_iflag = d_prbiti;
  210.     sttybuf.c_oflag = d_prbito;
  211.     sttybuf.c_lflag = d_prbitl;
  212.  
  213. #else
  214.  
  215.     sttybuf.sg_erase  = '\010';   /* erase = control-H                  */
  216.     sttybuf.sg_kill   = '\030';   /* kill = control-X                   */
  217.     sttybuf.sg_flags  |= d_pron;  
  218.                   /* force on  special bits for protocol*/
  219.     sttybuf.sg_flags  &=  ~((int) d_proff);
  220.                   /* force off special bits for protocol*/
  221.  
  222. #endif SYS5
  223.  
  224. #ifdef SYS5
  225.     rc = ioctl (d_savfd, TCSETAW, &sttybuf);
  226.     fcntl (d_savfd, F_SETFL, fcntl (d_savfd, F_GETFL, 0) & ~O_NDELAY);
  227.     return(rc);
  228. #else
  229.     return( ioctl (d_savfd, TIOCSETP, &sttybuf) );
  230. #endif SYS5
  231. }
  232.