[comp.unix.questions] Timed-out Input

bvickers@paris.ics.uci.edu (Brett Joseph Vickers) (02/17/90)

Greetings,

I have written a program using Xenix SysV and I am in the process of porting
it over to BSD, but I've hit a glitch.  The glitch occurs with my input
processing.  In the SysV version, I was using the termio structure to
set a key-in timeout (termio.c_cc[VTIME]=3;).  But when I moved over to BSD 
I was forced to use the sgttyb structure in conjunction with the ioctl() 
call.  So far, I'm using the CBREAK mode, but this alone does not allow 
me to "time out" my inputs.

Here is what I had in the System V version:

foo()
{
	struct termio temp;		/* Using termio */

	ioctl(0,TCGETA,&temp);
	temp.clflag &= ~(ICANON);
	temp.c_cc[VMIN] = 0;
	temp.c_cc[VTIME] = 3;		 /* Time out after .3 seconds */
	ioctl(0,TCSETA,&temp);
}

And what I've got in the BSD version:

bar()
{
	struct sgttyb temp;

	ioctl(0,TIOCGETP,&temp);
	temp.sg_flags |= CBREAK;
	ioctl(0,TIOCSETP,&temp);
}

As you can see, I know of no way to time out raw or cbreaked input.
Is there a way to do this with BSD?

Thank you,

bvickers@bonnie.ics.uci.edu

janne@enea.se (Jan Carlsson) (03/04/90)

In article <25DCE6F4.2232@paris.ics.uci.edu> bvickers@paris.ics.uci.edu (Brett Joseph Vickers) writes:
>As you can see, I know of no way to time out raw or cbreaked input.
>Is there a way to do this with BSD?

You can use select(2):

#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>

main()
{
	struct sgttyb temp;
	fd_set rset;
	struct timeval timeout;
	char c;

	ioctl(0,TIOCGETP,&temp);
	temp.sg_flags |= CBREAK;
	ioctl(0,TIOCSETP,&temp);

	FD_ZERO(&rset);
	FD_SET(0,&rset);
	timeout.tv_sec = 3L;
	timeout.tv_usec = 0L;

	if (select(1,&rset,NULL,NULL,&timeout) == 1 &&
	    FD_ISSET(0,&rset))
		read(0,&c,1);
	else
		printf("Timed out\n");
}
-- 
Jan Carlsson, Enea Data AB, Box 232, Nytorpsvaegen 5, S-183 23 Taeby, Sweden
Phone: +46 8 792 25 00  !  e-mail: janne@enea.se
Fax:   +46 8 768 43 88  !