home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / pcomm-2.0.2 / part02 / tty_ucb.c < prev   
Encoding:
C/C++ Source or Header  |  1993-04-13  |  5.1 KB  |  307 lines

  1. /*
  2.  * Berkeley specific routines for manipulating the TTY
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <sgtty.h>
  7. #include <fcntl.h>
  8. #include "dial_dir.h"
  9. #include "modem.h"
  10. #include "param.h"
  11.  
  12. static struct sgttyb hold;
  13.  
  14. /*
  15.  * Change the communication line settings to the new values.
  16.  */
  17.  
  18. void
  19. line_set()
  20. {
  21.     static int first = 1;
  22.     extern int fd;
  23.     struct sgttyb tbuf;
  24.     unsigned int baud;
  25.                     /* nothing to do! */
  26.     if (fd == -1)
  27.         return;
  28.  
  29.     if (first) {
  30.         ioctl(fd, TIOCGETP, &hold);
  31.         first = 0;
  32.     }
  33.                     /* get the current settings */
  34.     ioctl(fd, TIOCGETP, &tbuf);
  35.                     /* set some beginning values */
  36.     tbuf.sg_flags = CBREAK;
  37.  
  38.     if (*param->flow_ctrl == 'X')
  39.         tbuf.sg_flags |= TANDEM;
  40.     /*
  41.      * If the DTE speed is locked, then ignore all request to change
  42.      * the speed.
  43.      */
  44.     baud = modem->lock_sp[modem->t_cur];
  45.     if (baud == 0)
  46.         baud = dir->baud[0];
  47.                     /* the baud rate */
  48.     switch (baud) {
  49.         case 300:
  50.             tbuf.sg_ispeed = B300;
  51.             tbuf.sg_ospeed = B300;
  52.             break;
  53.         case 1200:
  54.             tbuf.sg_ispeed = B1200;
  55.             tbuf.sg_ospeed = B1200;
  56.             break;
  57.         case 2400:
  58.             tbuf.sg_ispeed = B2400;
  59.             tbuf.sg_ospeed = B2400;
  60.             break;
  61.         case 4800:
  62.             tbuf.sg_ispeed = B4800;
  63.             tbuf.sg_ospeed = B4800;
  64.             break;
  65.         case 9600:
  66.             tbuf.sg_ispeed = B9600;
  67.             tbuf.sg_ospeed = B9600;
  68.             break;
  69.         case 19200:
  70. #ifdef B19200
  71.             tbuf.sg_ispeed = B19200;
  72.             tbuf.sg_ospeed = B19200;
  73. #else /* B19200 */
  74. #ifdef EXTA
  75.             tbuf.sg_ispeed = EXTA;
  76.             tbuf.sg_ospeed = EXTA;
  77. #endif /* EXTA */
  78. #endif /* B19200 */
  79.             break;
  80.         case 38400:
  81. #ifdef B38400
  82.             tbuf.sg_ispeed = B38400;
  83.             tbuf.sg_ospeed = B38400;
  84. #else /* B38400 */
  85. #ifdef EXTB
  86.             tbuf.sg_ispeed = EXTB;
  87.             tbuf.sg_ospeed = EXTB;
  88. #endif /* EXTB */
  89. #endif /* B38400 */
  90.             break;
  91.     }
  92.                     /* the parity */
  93.     switch (dir->parity[0]) {
  94.         case 'N':
  95.             tbuf.sg_flags |= ANYP;
  96.             break;
  97.         case 'O':
  98.             tbuf.sg_flags |= ODDP;
  99.             break;
  100.         case 'E':
  101.             tbuf.sg_flags |= EVENP;
  102.             break;
  103.     }
  104.                     /* now set 'em! */
  105.     ioctl(fd, TIOCSETP, &tbuf);
  106.     ioctl(fd, TIOCHPCL, 0);
  107.     return;
  108. }
  109.  
  110. /*
  111.  * Put things back the way they were.
  112.  */
  113.  
  114. void
  115. reset_line()
  116. {
  117.     extern int fd;
  118.  
  119.     ioctl(fd, TIOCSETP, &hold);
  120.     return;
  121. }
  122.  
  123. /*
  124.  * Put the stdin/stdout in terminal mode.  We've divided up the
  125.  * responsibility for the line settings options between the serial port
  126.  * and the stdin and stdout.
  127.  */
  128.  
  129. void
  130. term_mode()
  131. {
  132.     struct sgttyb tbuf;
  133.  
  134.     ioctl(0, TIOCGETP, &tbuf);
  135.     
  136.     tbuf.sg_flags |= RAW;
  137.     tbuf.sg_flags &= ~(CRMOD|ECHO);
  138.  
  139.     if (dir->duplex[0] == 'H')
  140.         tbuf.sg_flags |= ECHO;
  141.  
  142.     ioctl(0, TIOCSETP, &tbuf);
  143.     return;
  144. }
  145.  
  146. /*
  147.  * Put the TTY driver in the mode suitable for xmodem transfers.
  148.  */
  149.  
  150. void
  151. xmodem_mode(fds)
  152. int fds;
  153. {
  154.     struct sgttyb tbuf;
  155.  
  156.     ioctl(fds, TIOCGETP, &tbuf);
  157.     /*
  158.      * Turn off the XON/XOFF flow control, turn off echoing, and
  159.      * switch to 8 bit no parity.
  160.      */
  161.     tbuf.sg_flags |= (RAW|ANYP);
  162.     tbuf.sg_flags &= ~ECHO;
  163.     ioctl(fds, TIOCSETP, &tbuf);
  164.     return;
  165. }
  166.  
  167. /*
  168.  * Put the TTY line in a mode suitable for the ASCII transfer.
  169.  */
  170.  
  171. void
  172. ascii_mode(up)
  173. int up;
  174. {
  175.     extern int fd;
  176.     struct sgttyb tbuf;
  177.  
  178.     ioctl(fd, TIOCGETP, &tbuf);
  179.  
  180.     tbuf.sg_flags |= (CBREAK|TANDEM);
  181.     tbuf.sg_flags &= ~(RAW|CRMOD|ECHO|CRDELAY);
  182.  
  183.     if (up) {
  184.                     /* CR delay times */
  185.         switch (param->cr_delay) {
  186.             case 0:
  187.                 break;
  188.             case 100:
  189.                 tbuf.sg_flags |= CR1;
  190.                 break;
  191.             case 150:
  192.                 tbuf.sg_flags |= CR2;
  193.                 break;
  194.         }
  195.     }
  196.  
  197.     ioctl(fd, TIOCSETP, &tbuf);
  198.     return;
  199. }
  200.  
  201. /*
  202.  * Flush the file descriptor.  Very messy... flushing the input causes a
  203.  * wait for the output to drain, and there is no output flushing.
  204.  */
  205.  
  206. int
  207. tty_flush(fds, mode)
  208. int fds, mode;
  209. {
  210.     int ret_code = 0;
  211.     struct sgttyb tbuf;
  212.  
  213.     switch(mode) {
  214.         case 0:            /* flush input queue */
  215.             ioctl(fds, TIOCGETP, &tbuf);
  216.             ioctl(fds, TIOCSETP, &tbuf);
  217.             break;
  218.         case 1:            /* flush output queue */
  219.             /* sorry! */
  220.             break;
  221.         case 2:            /* flush both input and output */
  222.             ioctl(fds, TIOCFLUSH, 0);
  223.             break;
  224.         default:
  225.             ret_code++;
  226.             break;
  227.     }
  228.     return(ret_code);
  229. }
  230.  
  231. /*
  232.  * Wait for the output to drain
  233.  */
  234.  
  235. int
  236. tty_drain(fds)
  237. int fds;
  238. {
  239.     struct sgttyb tbuf;
  240.                     /* this flushes the input too */
  241.     ioctl(fds, TIOCGETP, &tbuf);
  242.     return(ioctl(fds, TIOCSETP, &tbuf));
  243. }
  244.  
  245. /*
  246.  * Send a modem break
  247.  */
  248.  
  249. int
  250. tty_break(fds)
  251. int fds;
  252. {
  253.     unsigned int sleep();
  254.  
  255.     ioctl(fds, TIOCSBRK, (struct sgttyb *) 0);
  256.     sleep(1);
  257.     return(ioctl(fds, TIOCCBRK, (struct sgttyb *) 0));
  258. }
  259.  
  260. /*
  261.  * Fix the file descriptor so that a read is satisfied immediately.  When
  262.  * read() is called it returns the character in the queue, or an error if
  263.  * no key was pressed.
  264.  */
  265.  
  266. int
  267. tty_noblock(fds, on)
  268. int fds, on;
  269. {
  270.     int current;
  271.  
  272.     current = fcntl(fds, F_GETFL, 0);
  273.     if (on)
  274.         return(fcntl(fds, F_SETFL, current | FNDELAY));
  275.     else
  276.         return(fcntl(fds, F_SETFL, current & ~FNDELAY));
  277. }
  278.  
  279. /*
  280.  * Get the current baud rate of the terminal
  281.  */
  282.  
  283. int
  284. my_speed()
  285. {
  286.     static unsigned int speed[16] = {0, 50, 75, 110, 134, 150, 200, 300,
  287.     600, 1200, 1800, 2400, 4800, 9600, 19200, 38400};
  288.     struct sgttyb tbuf;
  289.  
  290.     ioctl(0, TIOCGETP, &tbuf);
  291.     return(speed[tbuf.sg_ispeed]);
  292. }
  293.  
  294. /*
  295.  * Restart any XON/XOFF flow control that may have stopped the tty 
  296.  */
  297.  
  298. void
  299. tty_restart()
  300. {
  301.     extern int fd;
  302.  
  303.     if (fd != -1 && *param->flow_ctrl == 'X')
  304.         ioctl(fd, TIOCFLUSH, (struct sgttyb *) 0);
  305.     return;
  306. }
  307.