[net.unix] CBREAK in S5/~ICANON in V7/4.x

guy@sun.uucp (Guy Harris) (07/22/85)

(Posted to "net.unix" and "net.unix-wizards" as well, because this seems to
be one of the Most Asked Questions Of All Time.)

> >Many of the programs I'm trying to port require CBREAK and no
> >ECHO modes.  How is this done under SVR2?
> 
> In Unix System V, the terminal modes have quite a lot of flexibility in
> determining when a read() can complete.  They are documented in excruciating
> detail in termio(7) (in the UNIX System Administrator's Manual, alas, not
> the Programmer's Manual where they belong - gh).
>
> What you basically want to do is turn off ICANON (in the "c_lflag" word -
> gh) and set VMIN and VTIME as your application requires.  CBREAK is, I
> believe, MIN=1, TIME=0.

This is correct.

> "The MIN and TIME values are stored in the position for the EOF and EOL
> characters, respectively.  The time value represents tenths of seconds."
> To turn off echo modes, turn off that bit (also in the "c_lflag" word - gh),
> as well.  (It may be that this is automatic, but I don't think so.)

It isn't.  You have to disable echoing and canonicalization independently.

> The parameters are changed via ioctl() operations.  I strongly recommend
> using TCSETAW instead of TCSETA, because some computer systems actually
> implement a no-wait mode stomper, which can garble some characters being
> output at the time.
> -- 
> Ron Heiby

In the V7 driver (straight and as modified in 4.xBSD), "TIOCSETP" (or
"stty", which just does a "TIOCSETP") waits for output to drain and flushes
input.  "TIOCSETN" does not wait and does not flush input or output.

In the S5 (and S3) driver, "TCSETA" acts like "TIOCSETN", "TCSETAF" acts
like "TIOCSETP", and "TCSETAW" is like "TCSETAF" only it doesn't flush the
input.

For the benefit of people moving S3/S5 programs to V7/4.xBSD, you can
probably ignore whatever VMIN and VTIME are set to (unless VMIN is set to 0,
in which case VTIME acts as a read timeout).  Turning off ICANON is
equivalent to turning on CBREAK; turning off ISIG is equivalent to setting
the interrupt and quit characters to '\377' (disabling them, in other
words); turning off ECHO is, surprise surprise, equivalent to turning off
ECHO.

	Guy Harris

mark@cbosgd.UUCP (Mark Horton) (07/24/85)

This is all pretty confusing to most people, I'll bet.  It's even
more confusing if you do what I suspect the origional poster did:
look up stty in their manual (and in /usr/include/sgtty.h) and
notice that CBREAK and TANDEM just aren't there, and neither is
TIOCSETN.  Chances are that the program they are trying to port
from V7/4BSD to SIII/SV uses these.

What's going on here is this.  sgtty is the interface to the
tty driver used in V6 and V7.  4BSD is upward compatible with
this interface, although CBERAK and TIOCSETN were in V7.
System III changed the interface radically - it's now possible
to get much more detailed control of the tty driver, although
it's now at a lower level.  (Instead of turning on CBREAK, you
have to clear ICANON and set VMIN and VTIME, being sure to save
the old values for when you come back.)  SIII/SV have an upward
compatibility package in sgtty, which is how SIII (called UNIX 3.0
at the time) could be upward compatible with the previous release,
UNIX 2.0.  However, UNIX 2.0 was not the same as V7, in particular
it didn't hvae CBREAK or TIOCSETN.  So to this day, the compatibility
package doesn't have them either.

The compatibility package has other problems.  For example, when you
use it, it clears your ECHOE bit, which will annoy most people.
So it's strongly recommended that you don't use the compatibility
package, instead ifdef the program to handle either the SV or V7
tty drivers.

If you're hopelessly confused by all this, don't worry.  There is a
package that handles all this stuff for you.  It's called curses.
You can ask curses for CBREAK mode, for example, by calling
	crmode();
You can turn off echo by calling
	noecho();
These calls do the appropriate thing on either 4.2BSD or System V.
There are a lot more portability features in the System V curses;
these are not all present in the 4.2 curses, but SV curses will
run on 4.2 if you have the appropriate licenses.

In general, if you're writing a program that wants to turn off echo
and get characters one at a time, chances are it's screen oriented.
I encourage you to use curses, your application will be more portable.
(There are curses implementions for 4.2BSD, System V, MS DOS, and QNX.)