[comp.sys.sgi] use of SGTTY.H and stuff involving IOCTLs

russo@chaos.utexas.edu (Thomas Russo) (12/19/89)

I've been trying to port a program which uses facilities of sgtty.h,
namely it uses the sgttyb structure to set CBREAK mode and turn off
echoing with a call to ioctl like this:

ioerr = ioctl(fileno(stdin), TIOCGETP, &tty_orig);	
		/* get std input characteristics */
...
		ttybuf = tty_new = tty_orig;						/* make a copy of tty control structure */
		tty_new.sg_flags = (tty_new.sg_flags & ~ECHO & ~CRMOD)
			| CBREAK;
...
		ioctl(fileno(stdin), TIOCSETP, &tty_new);
			/* set flags  */

Is there any slick way to take out this kind of
incompatibility when porting to the iris4d?   I can't find any
reference to CBREAK mode in the FM so RTFM is so far no good. 
Also, repeated reference is made to requests like TIOCSET[CP] and the
like, and I have no way of knowing what the correct functions are on
the iris.   Any hints?



Thomas Russo
Center for Nonlinear Dynamics, University of Texas at Austin          
russo@chaos.utexas.edu or phib421@utchpc.bitnet
------

ciemo@bananapc.wpd.sgi.com (Dave Ciemiewicz) (12/19/89)

In article <22483@ut-emx.UUCP>, russo@chaos.utexas.edu (Thomas Russo) writes:
> 
> I've been trying to port a program which uses facilities of sgtty.h,
> namely it uses the sgttyb structure to set CBREAK mode and turn off
> echoing with a call to ioctl like this:
> 
> ioerr = ioctl(fileno(stdin), TIOCGETP, &tty_orig);	
> 		/* get std input characteristics */
> ...
> 		ttybuf = tty_new = tty_orig;						/* make a copy of tty control
structure */
> 		tty_new.sg_flags = (tty_new.sg_flags & ~ECHO & ~CRMOD)
> 			| CBREAK;
> ...
> 		ioctl(fileno(stdin), TIOCSETP, &tty_new);
> 			/* set flags  */
> 
> Is there any slick way to take out this kind of
> incompatibility when porting to the iris4d?   I can't find any
> reference to CBREAK mode in the FM so RTFM is so far no good. 
> Also, repeated reference is made to requests like TIOCSET[CP] and the
> like, and I have no way of knowing what the correct functions are on
> the iris.   Any hints?
> 
> 
> 
> Thomas Russo
> Center for Nonlinear Dynamics, University of Texas at Austin          
> russo@chaos.utexas.edu or phib421@utchpc.bitnet
> ------

You should be using termio instead of sgtty.  As I understand it, sgtty is
rather archaic (and unsupported in some environments) and termio should be
used instead.  See termio(7) for many details.

For instance, your code should be something like:

	#include "termio.h"
	...
	ioerr = ioctl(fileno(stdin), TCGETA, &tty_orig);
	...
	tty_new.c_iflag &= ~ECHO;

	/* CRMOD emulation */
	tty_new.c_iflag &= ~ICRNL;
	tty_new.c_oflag &= ~OCRNL;

	/* CBREAK or 4.3BSD RAW mode emulation (see termio(7) man page
	** discussion of ICANON)
	*/
	tty_new.c_iflag &= ~ICANON;
	tty_new.c_cc[VMIN] = 1;		/* MIN characters */
	tty_new.c_cc[VTIME] = 1;	/* TIME in tenths of seconds */
	...

TIOC[GS]ETP were used to get and set the parameters.  Try matching
functionality
by looking at the termio man page and looking at any BSD source if it is
available to you.  The TCGETA and TCSETA[WF] commands are their corresponding
commands.

TIOCSETC was used to set the special characters.  termio.c_cc[] is used to
get and set the special control character settings.  Use TCSETA (or the
TCSETAW or TCSETAF variants) to set different character bindings (and control
when the bindings are set with respect to current output buffer).

I hope this is all correct, I haven't tried it.  I read termio(7) and looked
at some BSD game source to figure this one out.

If you have 4.3 BSD code available to you, look for code "#ifdef USG" which
ifdefs termio versus sgtty variations in code.

						--- Ciemo

moss@BRL.MIL ("Gary S. Moss", VLD/VMB) (12/20/89)

Dave Ciemiewicz has got the right idea, but based on some of my code that
seems to work I would make some changes.  I already sent some code to Thomas,
but for the benefit of other readers, take this for what its worth:

< For instance, your code should be something like:
< 	#include "termio.h"
< 	...
< 	ioerr = ioctl(fileno(stdin), TCGETA, &tty_orig);
< 	...
< 	tty_new.c_iflag &= ~ECHO;
It is my impression that turning off echo is an orthogonal operation, not
part of "CBREAK" mode.

< 	/* CRMOD emulation */
< 	tty_new.c_iflag &= ~ICRNL;
< 	tty_new.c_oflag &= ~OCRNL;
Carriage return to newline processing is not disabled in CBREAK mode as
far as I know.  Study of tty(4) on my Sun seems to confirm this.

< 	/* CBREAK or 4.3BSD RAW mode emulation (see termio(7) man page
< 	** discussion of ICANON)
< 	*/
< 	tty_new.c_iflag &= ~ICANON;
That should be:
	tty_new.c_lflag &= ~ICANON;

< 	tty_new.c_cc[VMIN] = 1;		/* MIN characters */
< 	tty_new.c_cc[VTIME] = 1;	/* TIME in tenths of seconds */
I set the VTIME field to 0, not sure if it matters, though termio(7)
on my 4D says to set them both to one to simulate BSD RAW mode.

Also, you want to enable signal processing.
        tty_new.c_lflag |= ISIG;           /* Signals ON. */

davea@quasar.wpd.sgi.com (David B. Anderson) (12/21/89)

In article <8912200949.aa18764@VMB.BRL.MIL> moss@BRL.MIL ("Gary S. Moss", VLD/VMB) writes:
>Dave Ciemiewicz has got the right idea, but based on some of my code that
>seems to work I would make some changes.  I already sent some code to Thomas,
>but for the benefit of other readers, take this for what its worth:
>< 	tty_new.c_cc[VMIN] = 1;		/* MIN characters */
>< 	tty_new.c_cc[VTIME] = 1;	/* TIME in tenths of seconds */
>I set the VTIME field to 0, not sure if it matters, though termio(7)
>on my 4D says to set them both to one to simulate BSD RAW mode.

From the post-release-3.2 termio man page:  (see also POSIX 1003.1 7.1.1.7)

If MIN and TIME are both greater than 0:

In this case, TIME serves as an inter-character timer activated after the
first character is received, and reset upon receipt of each character.  MIN
and TIME interact as follows:

As soon as one character is received the inter-character timer is started.

If MIN characters are received before the inter-character timer expires the
read is satisfied.

If the timer expires before MIN characters are received the characters
received to that point are returned to the user.

A read(2) operation will sleep until the MIN and TIME mechanisms are
activated by the receipt of the first character; thus, at least one
character must be returned.

---------------------
If MIN > 0, TIME = 0:

In this case, because TIME = 0, the timer plays no role and only MIN is
significant.  A read operation is not satisfied until MIN characters are
received.

---------------------
If MIN = 0, TIME > 0:

In this case, because MIN = 0, TIME no longer serves as an inter-character
timer, but now serves as a read timer that is activated as soon as the read
operation is processed (in canon).  A read operation is satisfied as soon
as a single character is received or the timer expires, in which case the
read operation will not return any characters.

---------------------
If MIN = 0, TIME = 0:

In this case, return is immediate.  If characters are present, they will be
returned to the user.

Regards,
[ David B. Anderson  Silicon Graphics  (415)335-1548  davea@sgi.com ]