/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */

#ident	"$Header: termios.c 1.5 89/10/26 $"

/*
 * Library to map POSIX termios procedures to DYNIX/ptx(R) ioctls.
 */

#include <termios.h>
#include <errno.h>
#include <sys/types.h>

extern int errno;


/*
 * cfgetospeed - Return output baud rate stored in the termios struct
 *		 passed to the routine.
 */

speed_t
cfgetospeed(termios_p)
	struct termios *termios_p;
{
	return(termios_p->c_cflag & CBAUD);
}

/*
 * cfsetospeed - Set output baud rate in termios struct to that provided
 *		 to the routine.
 */

int
cfsetospeed(termios_p, speed)
	struct termios *termios_p;
	speed_t speed;
{
	switch (speed) {
	case B0:
	case B50:
	case B75:
	case B110:
	case B134:
	case B150:
	case B200:
	case B300:
	case B600:
	case B1200:
	case B1800:
	case B2400:
	case B4800:
	case B9600:
	case B19200:
    /*	case EXTA:			EXTA is the same as B19200.  */
	case B38400:
    /*	case EXTB:			EXTB is the same as B38400.  */
		termios_p->c_cflag &= ~CBAUD;
		termios_p->c_cflag |= speed;
		return(0);
	default:
		return(-1);
	}
}

/*
 * cfgetispeed - Return input baud rate stored in the termios struct
 *		 passed to the routine.
 */

speed_t
cfgetispeed(termios_p)
	struct termios *termios_p;
{

	return(cfgetospeed(termios_p));
}

/*
 * cfsetispeed - Set input baud rate in termios struct to that provided
 *		 to the routine.
 */

int
cfsetispeed(termios_p, speed)
	struct termios *termios_p;
	speed_t speed;
{
	if (speed == B0)
		return(0);
	else
		return(cfsetospeed(termios_p, speed));
}

/*
 * tcgetattr - Get a termios structure.  Same as TCGETP ioctl.
 */

int
tcgetattr(fildes, termios_p)
	int fildes;
	struct termios *termios_p;
{
	return(ioctl(fildes, TCGETP, termios_p));
}

/*
 * tcsetattr - Implement actions specified by termios struct.
 *	       Same as a TCSETP ioctl.
 */

int
tcsetattr(fildes, optional_actions, termios_p)
	int fildes;
	int optional_actions;
	struct termios *termios_p;
{
	switch (optional_actions) {
	case TCSANOW:
		return(ioctl(fildes, TCSETP, termios_p));
	case TCSADRAIN:
		return(ioctl(fildes, TCSETPW, termios_p));
	case TCSAFLUSH:
		return(ioctl(fildes, TCSETPF, termios_p));
	default:
		errno = EINVAL;
		return(-1);
	}

	/* In error case, what if fildes was wrong?
	 * Which errno would we wish to return?
	 */
}

/*
 * tcsendbreak - Wait for output to drain, send a break.
 *		 Same as TCSBRK ioctl with arg of 0.
 */

int
tcsendbreak(fildes, duration)
	int fildes;
	int duration;
{
	return(ioctl(fildes, TCSBRK, 0));
}

/*
 * tcdrain - Wait for output to drain.
 *	     Same as TCSBRK ioctl with arg of 1.
 */

int
tcdrain(fildes)
	int fildes;
{
	return(ioctl(fildes, TCSBRK, 1));
}

/*
 * tcflush - Flush input and/or output queues.
 *	     Implements TCFLSH ioctl.
 */

int
tcflush(fildes, queue_selector)
	int fildes;
	int queue_selector;
{
	switch (queue_selector) {
	case TCIFLUSH:
		return(ioctl(fildes, TCFLSH, 0));
	case TCOFLUSH:
		return(ioctl(fildes, TCFLSH, 1));
	case TCIOFLUSH:
		return(ioctl(fildes, TCFLSH, 2));
	default: 
		errno = EINVAL;
		return(-1);
	}
}

/*
 * tcflow - Start/stop control.
 *	    Implements TCXONC ioctl.
 */

int
tcflow(fildes, action)
	int fildes;
	int action;
{
	switch (action) {
	case TCOOFF:
		return(ioctl(fildes, TCXONC, 0));
	case TCOON:
		return(ioctl(fildes, TCXONC, 1));
	case TCIOFF:
		return(ioctl(fildes, TCXONC, 2));
	case TCION:
		return(ioctl(fildes, TCXONC, 3));
	default: 
		errno = EINVAL;
		return(-1);
	}
}

/*
 * tcgetpgrp - Get foreground process group.
 *	       Implements TIOCGPGRP.
 */

pid_t
tcgetpgrp(fildes)
	int fildes;
{
	pid_t p;
	if (ioctl(fildes, TIOCGPGRP, &p) == -1)
		return(-1);
	else
		return(p);

}

/*
 * tcsetpgrp - Set process group.
 *	       Implements TIOCSPGRP.
 */

int
tcsetpgrp(fildes, pgrp_id)
	int fildes;
	pid_t pgrp_id;
{
	return(ioctl(fildes, TIOCSPGRP, &pgrp_id));
}
