home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fax-3.2.1 / lib / libfax / tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-31  |  2.3 KB  |  116 lines

  1. /*
  2.   This file is part of the NetFax system.
  3.  
  4.   (c) Copyright 1989 by David M. Siegel and Sundar Narasimhan.
  5.       All rights reserved.
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation.
  10.  
  11.     This program is distributed in the hope that it will be useful, 
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <fcntl.h>
  23. #include <termios.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26.  
  27. #include "log.h"
  28. #include "tty.h"
  29.  
  30. int tty_fc(fd, state)
  31.      int fd;
  32.      fc_state state;
  33. {
  34.     struct termios t;
  35.     
  36.     if (tcgetattr(fd, &t) < 0) {
  37.     log(L_EMERG, "tty_fc: can't get attrs: %m");
  38.     return (-1);
  39.     }
  40.  
  41.     switch (state) {
  42.       case FC_OUTPUT_ON:
  43.     t.c_iflag |= IXON;
  44.     t.c_iflag &= ~IXOFF;
  45.     break;
  46.       case FC_INPUT_ON:
  47.     t.c_iflag |= IXOFF;
  48.     t.c_iflag &= ~IXON;
  49.     break;
  50.       case FC_BOTH_ON:
  51.     t.c_iflag |= (IXON|IXOFF);
  52.     break;
  53.       case FC_BOTH_OFF:
  54.     t.c_iflag &= ~(IXON|IXOFF);
  55.     break;
  56.     }
  57.  
  58.     if (tcsetattr(fd, TCSANOW, &t) < 0) {
  59.     log(L_EMERG, "tty_fc: can't set attrs: %m");
  60.     return (-1);
  61.     }
  62.  
  63.     return (0);
  64. }    
  65.  
  66. static int condition_tty(fd)
  67.      int fd;
  68. {
  69.     struct termios t;
  70.  
  71.     if (tcgetattr(fd, &t) < 0) {
  72.     log(L_EMERG, "condition_tty: can't get attrs: %m");
  73.     return (-1);
  74.     }
  75.  
  76.     t.c_iflag = IXON|IXOFF;
  77.     t.c_oflag = 0;
  78.     t.c_cflag = CS8|CREAD|CLOCAL;
  79.     t.c_lflag = 0;
  80.  
  81.  
  82.     cfsetispeed(&t, B19200);
  83.     cfsetospeed(&t, B19200);
  84.  
  85.     if (tcsetattr(fd, TCSANOW, &t) < 0) {
  86.     log(L_EMERG, "condition_tty: can't set attrs: %m");
  87.     return (-1);
  88.     }
  89.  
  90.     return (0);
  91. }
  92.  
  93. int tty_open(filename)
  94.      char *filename;
  95. {
  96.     int fd;
  97.     
  98.     if ((fd = open(filename, O_RDWR|O_NDELAY)) < 0) {
  99.     log(L_EMERG, "can't open fax modem file: %m");
  100.     return (-1);
  101.     }
  102.     
  103.     if (condition_tty(fd) < 0) {
  104.     close(fd);
  105.     return (-1);
  106.     }
  107.  
  108.     return (fd);
  109. }
  110.  
  111. int tty_close(fd)
  112.      int fd;
  113. {
  114.     return (close(fd));
  115. }
  116.