[comp.unix.programmer] raw mode vs cooked mode

A.J.Rasiah@massey.ac.nz (Ajit Rasiah ) (02/20/91)

Hello,

I would like to write a program which will get characters from the terminal
in "raw" mode. The default is "cooked" mode. Can someone point me in the
right direction? I am using a Sun Workstation running UNIX 4.2 BSD.

Please reply to A.J.Rasiah@massey.ac.nz

Many thanks in advance.

mike (02/25/91)

In an article, A.J.Rasiah@massey.ac.nz writes:
>I would like to write a program which will get characters from the terminal
>in "raw" mode. The default is "cooked" mode. Can someone point me in the
>right direction? I am using a Sun Workstation running UNIX 4.2 BSD.

This should be in a FAQ somewhere.  If your system groks termio, then
here is what you could have:

	struct termio	old, new;

		ioctl(0,TCGETA,&old);	/* save current state */
		ioctl(0,TCGETA,&new);	/* save current state */

		new.c_lflag &= ~ICANON; /* no canonical input */
		new.c_lflag &= ~ISIG;   /* no signals */
		new.c_cc[VMIN] = 1;	/* return one character */
		new.c_cc[VTIME] = 0;	/* no timeout */

		ioctl(0,TCSETA,&new);	/* change current state */

			.
			.
			.

		ioctl(0,TCSETA,&old);	/* restore previous state */

If you want to grab cursor keys, function keys, etc. then you're better
off using curses (although if you are *really* interested, I can give you
the algorithm for snagging ``special'' keys as well).

-- 
Michael Stefanik, MGI Inc., Los Angeles| Opinions stated are not even my own.
Title of the week: Systems Engineer    | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
Remember folks: If you can't flame MS-DOS, then what _can_ you flame?

guy@auspex.auspex.com (Guy Harris) (02/27/91)

>>I would like to write a program which will get characters from the terminal
>>in "raw" mode. The default is "cooked" mode. Can someone point me in the
>>right direction? I am using a Sun Workstation running UNIX 4.2 BSD.
>
>This should be in a FAQ somewhere.  If your system groks termio, then
>here is what you could have:

It's what he should have *if*, by "raw mode", he means "a mode in which
the parity and bits-per-character are the same as 'normal', characters
are made available to a program reading from the device as they're
typed, and the only characters that are treated specially are the XON
and XOFF characters."

That happens to be what a lot of people who want some flavor of "less
cooked" mode want.  Others may want a raw 8-bit data path with *no*
characters treated specially (for, say, binary data transfer), or may
want the interrupt characters to be treated specially.  I have no idea
what the person who asked the question wanted; they didn't give any
details, they just asked for "raw" mode.

The "old" tty driver (V7, and its pre-4.3-reno BSD descendant)
had RAW for the raw 8-bit data path, and CBREAK for "make characters
available as they're typed, and don't treat the erase/kill/<RETURN>/etc
characters specially."  To turn of the interrupt characters as well, you
could set them to '\377', as in CBREAK mode input was stripped to 7 bits
(unless you turned PASS8 on, in 4.3BSD or later).

(RAW also turned off any special character handling on *output*.)

The "new" tty driver (S3, S5, 4.3-reno and successors, POSIX) does a
better job of specifying those modes; you have much finer control over
what the driver does.

(Also, since they said the Sun was running "UNIX 4.2 BSD", I suspect it
was running a SunOS release prior to 4.0, which means that, unless he
was working in the S5 environment, the system *doesn't* grok "termio" -
it has the "old" tty driver.)