Unix - UNIX Terminal Line Discipline and Canonical vs. Raw Mode

1. Introduction

In UNIX and UNIX-like operating systems, a terminal provides an interface through which users interact with the computer by entering commands, typing text, and receiving output. When a user types characters on a keyboard, the operating system must determine how those characters should be processed before delivering them to an application. This processing is managed partly by a mechanism called terminal line discipline.

Terminal line discipline is responsible for controlling how input characters are handled, how special characters are interpreted, and how input is delivered to programs. For example, when a user types a command in a terminal, the system may allow them to correct typing mistakes using the Backspace key before sending the completed line to the application. In other situations, such as when using an interactive text editor or a terminal-based game, the application may need to receive each keystroke immediately. UNIX supports these different requirements through terminal input modes, particularly canonical mode and noncanonical mode. Raw mode is a commonly used configuration of noncanonical input.

2. What Is UNIX Terminal Line Discipline?

Terminal line discipline is a component of the terminal input and output processing system that determines how data is handled between a terminal device and a program. It is traditionally implemented within the terminal subsystem of the operating system and is configured through terminal settings.

When a user types characters, the terminal subsystem can perform several operations before passing the input to an application. These operations may include buffering characters, handling erase and kill characters, interpreting certain control characters, and processing special input conditions. The exact behavior depends on the terminal configuration.

For example, consider a user typing the following command:

Bash

ls -l

In a typical interactive shell, the terminal collects the characters as the user types. The user can correct a mistake before pressing Enter. Once the line is ready, the shell receives the input and interprets it as a command.

Terminal line discipline also supports functions such as input and output processing, character handling, and flow control. Settings such as ICANON, ECHO, ISIG, and IXON influence particular aspects of this behavior. These settings can be inspected and changed using terminal configuration utilities and system interfaces.

3. What Is Canonical Mode?

Canonical mode is a terminal input mode in which input is generally collected one line at a time. The terminal subsystem stores the characters entered by the user in an input buffer and normally makes the completed line available to the application when the user presses the configured line-delimiter character, usually Enter.

In this mode, the terminal can provide basic line-editing functions. For example, users can erase characters with Backspace or remove the current line using the configured line-kill character. The precise editing characters depend on the terminal settings.

Consider the following example:

Bash

cat

When a user runs this command in a typical terminal, cat reads input from the terminal. In canonical mode, the user can type a line of text and correct mistakes before pressing Enter. The completed line is then made available to cat, which displays the received text. Pressing Ctrl+D at the beginning of an empty line can indicate end-of-file to the program.

Canonical mode is useful for command-line shells, text input programs, and utilities that expect users to enter complete lines. It reduces the need for applications to implement basic keyboard editing themselves.

However, canonical mode is not ideal for every application. A program that needs to react immediately to individual keystrokes may have to change the terminal settings to use noncanonical input.

4. What Is Raw Mode?

Raw mode is a terminal configuration in which input is delivered to an application with much less of the terminal subsystem's usual processing. In common usage, raw mode allows an application to receive individual input characters without waiting for a complete line.

Unlike canonical mode, raw input does not normally wait for the user to press Enter before making characters available. Depending on the exact configuration, the application may also receive characters without the usual line-editing behavior, automatic echo, or signal generation associated with special control characters.

Strictly speaking, raw mode is not a single universal POSIX terminal setting. It is commonly established by changing several terminal attributes together. On many UNIX-like systems, utilities such as stty and terminal libraries such as termios provide ways to configure this behavior. The exact meaning of raw mode can differ between systems and libraries.

For example, a text editor running inside a terminal must respond when the user presses an arrow key, Backspace, or another control key. If it waited for Enter after every keystroke, normal interactive editing would be difficult. Raw or near-raw input lets the application read input promptly and decide how to interpret it.

Raw mode is useful for interactive text editors, terminal-based games, keyboard-driven interfaces, and programs that need direct control over keyboard input. Since the application takes greater responsibility for processing the input, it must handle tasks such as interpreting control sequences and managing editing behavior when needed.

5. Differences Between Canonical Mode and Raw Mode

Canonical and raw modes differ mainly in how terminal input is processed and delivered to an application.

Feature Canonical Mode Raw Mode
Input delivery Usually line by line Usually character by character
Waiting for Enter Generally required to complete a line Generally not required
Line editing Basic terminal line editing is available Usually handled by the application
Character echo Commonly enabled Commonly disabled
Special control characters May receive special treatment Often delivered without normal signal processing
Application responsibility Less responsibility for basic input editing More responsibility for input interpretation
Typical uses Shell commands and line-based input Interactive editors, games, and custom terminal interfaces

These differences are typical rather than absolute. Canonical mode can be configured in several ways, and raw mode behavior depends on the attributes selected by the application. For example, disabling echo does not automatically make a terminal fully raw, and disabling canonical mode alone does not disable every other form of terminal processing.

6. How to Inspect and Change Terminal Modes

UNIX provides utilities and programming interfaces for examining and configuring terminal behavior. One commonly used utility is stty.

To inspect the current terminal settings, use:

Bash

stty -a

This command displays the terminal's configured input, output, control, and local modes. On many systems, the output includes settings such as icanon, which controls canonical input, and echo, which controls whether input characters are echoed to the terminal.

To disable canonical mode and echo for a simple demonstration, a user can run:

Bash

stty -icanon -echo

In this configuration, input is no longer collected in the usual line-oriented manner, and typed characters are not automatically displayed. However, disabling these two settings does not create a fully raw terminal because other input processing and control-character behavior may remain active.

To restore the usual terminal settings after this demonstration, run:

Bash

stty sane

The sane option restores a conventional set of terminal settings, although it may not reproduce every setting that was active before the change. Therefore, programs that modify terminal modes should save the original configuration and restore it when they finish.

A safer interactive approach is to use the following command:

Bash

stty -icanon -echo; cat; stty sane

This illustrates the general idea, but it is not a fully reliable restoration method if the command is interrupted or the program terminates unexpectedly. Production programs should handle errors and interruptions carefully and restore the saved terminal settings.

In C programs, terminal attributes can be controlled through the termios interface. Functions such as tcgetattr() and tcsetattr() allow an application to read the existing settings and apply new ones. A program can disable canonical input by clearing the ICANON flag and disable echo by clearing the ECHO flag. It can also configure VMIN and VTIME to influence when noncanonical reads return.

7. Practical Applications of Terminal Line Discipline

Terminal line discipline plays an important role in many interactive UNIX applications. Command-line shells depend on conventional line-oriented input for entering and editing commands. Password prompts commonly disable echo so that the password characters are not displayed on the screen. Text editors often switch to noncanonical input to respond to individual keystrokes. Terminal-based games use similar configurations to support immediate keyboard interaction.

Remote terminal applications and terminal emulators also rely on appropriate input processing. They must distinguish ordinary characters from control sequences and transmit input in a way that the receiving application can interpret. Correct terminal configuration helps these programs remain responsive and behave as expected.

Understanding terminal line discipline is also valuable when troubleshooting terminal problems. If typed characters unexpectedly stop appearing, input seems to wait until Enter is pressed, or Ctrl+C no longer interrupts a program, the terminal may be using unusual settings. Inspecting the terminal attributes and restoring the appropriate configuration can often resolve these problems.

8. Conclusion

UNIX terminal line discipline controls how terminal input and output are processed before data reaches an application or is displayed to the user. Canonical mode provides line-based input and basic editing features, making it suitable for ordinary command-line interaction. Raw mode reduces the usual terminal processing and allows applications to handle individual keystrokes more directly, making it useful for interactive text editors, games, and specialized terminal interfaces.

The key distinction is that canonical mode emphasizes convenient line-based input, whereas raw mode gives applications greater control over input handling. Understanding these modes, the role of terminal attributes, and the tools used to configure them helps developers create reliable interactive programs and troubleshoot terminal behavior in UNIX systems.