Reworked some TTY support functions. Added tcgetattr() function.

tcsendbreak() now ignores the "duration" parameter.


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@9052 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2004-09-23 23:52:36 +00:00
parent 98ac7a9709
commit 2f524aed3a

View File

@ -1,45 +1,56 @@
/* /*
** Copyright 2003, Daniel Reinhold, danielre@users.sf.net. All rights reserved. ** Copyright 2003, Daniel Reinhold, danielre@users.sf.net. All rights reserved.
** Distributed under the terms of the OpenBeOS License. ** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the Haiku License.
*/ */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h> #include <termios.h>
#include <unistd.h>
#include <errno.h>
/* /** get the attributes of the TTY device at fd */
* tcsetattr - set the attributes for the tty device at fd
* (using the structure in the last arg for the values)
*/
int int
tcsetattr(int fd, int opt, const struct termios *tp) tcgetattr(int fd, struct termios *termios)
{ {
return ioctl(fd, TCGETA, termios);
}
/** set the attributes for the TTY device at fd */
int
tcsetattr(int fd, int opt, const struct termios *termios)
{
int method;
switch (opt) { switch (opt) {
case TCSANOW: case TCSANOW:
// set the attributes immediately // set the attributes immediately
return ioctl(fd, TCSETA, tp); method = TCSETA;
break;
case TCSADRAIN: case TCSADRAIN:
// wait for ouput to finish before setting the attributes // wait for ouput to finish before setting the attributes
return ioctl(fd, TCSETAW, tp); method = TCSETAW;
break;
case TCSAFLUSH: case TCSAFLUSH:
// wait for ouput to finish and then flush the input buffer method = TCSETAF;
// before setting the attributes break;
return ioctl(fd, TCSETAF, tp);
default: default:
// no other valid options // no other valid options
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
return ioctl(fd, method, termios);
} }
/* tcdrain - wait for all output to be transmitted */ /** wait for all output to be transmitted */
int int
tcdrain(int fd) tcdrain(int fd)
{ {
@ -53,87 +64,73 @@ tcdrain(int fd)
} }
/* tcflow - suspend or restart transmission */ /** suspend or restart transmission */
int int
tcflow(int fd, int action) tcflow(int fd, int action)
{ {
/* action will be one of the following: switch (action) {
* TCIOFF - input off (stops input) case TCIOFF:
* TCION - input on (restart input) case TCION:
* TCOOFF - output off (stops output) case TCOOFF:
* TCOON - output on (restart output) case TCOON:
*/ break;
default:
errno = EINVAL;
return -1;
}
return ioctl(fd, TCXONC, action); return ioctl(fd, TCXONC, action);
} }
/* tcflush - flush all pending data (input or output) */ /** flush all pending data (input or output) */
int int
tcflush(int fd, int queue_selector) tcflush(int fd, int queueSelector)
{ {
return ioctl(fd, TCFLSH, queue_selector); return ioctl(fd, TCFLSH, queueSelector);
} }
/* tcsendbreak - send zero bits for the specified duration */ /** send zero bits for the specified duration */
int int
tcsendbreak(int fd, int duration) tcsendbreak(int fd, int duration)
{ {
if (duration == 0) // Posix spec says this should take ~ 0.25 to 0.5 seconds.
// Posix spec says this should take ~ 0.25 to 0.5 seconds // As the interpretation of the duration is undefined, we'll just ignore it
return ioctl(fd, TCSBRK, 0); return ioctl(fd, TCSBRK, 0);
/* Posix does not specify how long the transmission time
* should last if 'duration' is non-zero -- i.e. it is up
* to each implementation to decide what to do...
*
* For simplicity, here is it assumed that a positive duration
* of N will mean "N time intervals" where each interval lasts
* from 0.25 to 0.5 seconds. A negative duration will be treated
* as an error.
*/
if (duration < 0) {
errno = EINVAL;
return -1;
}
while (duration-- > 0)
if (ioctl(fd, TCSBRK, 0) < 0)
return -1;
return 0;
} }
/*
* The following four "speed control" functions are not supported
* in the BeOS. But the interface is still part of the Posix
* terminal IO specification, so they are implemented here as
* stub functions for basic compliance.
*/
speed_t speed_t
cfgetispeed(const struct termios *term) cfgetispeed(const struct termios *termios)
{ {
return (speed_t)0; return termios->c_ispeed;
} }
int int
cfsetispeed( struct termios *term, speed_t speed) cfsetispeed(struct termios *termios, speed_t speed)
{ {
termios->c_ispeed = speed;
return 0; return 0;
} }
speed_t speed_t
cfgetospeed(const struct termios *term) cfgetospeed(const struct termios *termios)
{ {
return (speed_t)0; return termios->c_ospeed;
} }
int int
cfsetospeed( struct termios *term, speed_t speed) cfsetospeed(struct termios *termios, speed_t speed)
{ {
termios->c_ospeed = speed;
return 0; return 0;
} }