Terminal Mode - MIT/GNU Scheme 9.1
Description: MIT/GNU Scheme 9.1

Previous: Blocking Mode, Up: Port Primitives

#### 14.9.6 Terminal Mode

A port that reads from or writes to a terminal has a terminal mode; this is either cooked or raw. This mode is independent of the blocking mode: each can be changed independent of the other. Furthermore, a terminal I/O port has independent terminal modes both for input and for output.

A terminal port in cooked mode provides some standard processing to make the terminal easy to communicate with. For example, under unix, cooked mode on input reads from the terminal a line at a time and provides rubout processing within the line, while cooked mode on output might translate linefeeds to carriage-return/linefeed pairs. In general, the precise meaning of cooked mode is operating-system dependent, and furthermore might be customizable by means of operating system utilities. The basic idea is that cooked mode does whatever is necessary to make the terminal handle all of the usual user-interface conventions for the operating system, while keeping the program's interaction with the port as normal as possible.

A terminal port in raw mode disables all of that processing. In raw mode, characters are directly read from and written to the device without any translation or interpretation by the operating system. On input, characters are available as soon as they are typed, and are not echoed on the terminal by the operating system. In general, programs that put ports in raw mode have to know the details of interacting with the terminal. In particular, raw mode is used for writing programs such as text editors.

Terminal ports are initially in cooked mode; this can be changed at any time with the procedures defined in this section.

These procedures represent cooked mode by the symbol `cooked`, and raw mode by the symbol `raw`. Additionally, the value `#f` represents “no mode”; it is the terminal mode of a port that is not a terminal. An argument called mode must be one of these three values. A port argument to any of these procedures may be any port, even if that port does not support terminal mode; in that case, the port is not modified in any way.

— procedure: port/input-terminal-mode port

> Returns the input terminal mode of port.

— procedure: port/set-input-terminal-mode port mode

> Changes the input terminal mode of port to be mode. Returns an unspecified value.

— procedure: port/with-input-terminal-mode port mode thunk

> Thunk must be a procedure of no arguments. `port/with-input-terminal-mode` binds the input terminal mode of port to be mode, executes thunk, restores the input terminal mode of port to what it was when `port/with-input-terminal-mode` was called, and returns the value that was yielded by thunk. This binding is performed by `dynamic-wind`, which guarantees that the input terminal mode is restored if thunk escapes from its continuation.

— procedure: port/output-terminal-mode port

> Returns the output terminal mode of port.

— procedure: port/set-output-terminal-mode port mode

> Changes the output terminal mode of port to be mode. Returns an unspecified value.

— procedure: port/with-output-terminal-mode port mode thunk

> Thunk must be a procedure of no arguments. `port/with-output-terminal-mode` binds the output terminal mode of port to be mode, executes thunk, restores the output terminal mode of port to what it was when `port/with-output-terminal-mode` was called, and returns the value that was yielded by thunk. This binding is performed by `dynamic-wind`, which guarantees that the output terminal mode is restored if thunk escapes from its continuation.