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

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