Advanced Terminal Control
When requesting a pseudo-terminal (PTY), you can configure detailed terminal behavior using terminal modes. This allows fine-grained control over input processing, output formatting, signal handling, and echo behavior.
When You Need Terminal Modes
Terminal modes control how the remote terminal processes input and output. You can customize terminal modes for:
Controlling how output is processed (ANSI codes, line endings)
Raw vs. canonical (line-buffered) mode
Custom control character mappings
Flow control configuration
Basic Terminal Mode Configuration
Use TerminalModesBuilder to create custom terminal configurations:
Terminal Mode Categories
Terminal modes are organized into several categories:
Control Characters (Input Processing)
Define special character mappings for terminal control:
Mode | Default | Description | Common Use |
|---|---|---|---|
| ^C (3) | Interrupt signal (SIGINT) | Ctrl-C to terminate |
| ^\ (28) | Quit signal (SIGQUIT) | Ctrl-\ to quit with core dump |
| ^? (127) | Erase previous character | Backspace behavior |
| ^U (21) | Kill entire line | Ctrl-U to clear line |
| ^D (4) | End-of-file | Ctrl-D to signal EOF |
| - | End of line | Alternative to Enter |
| ^Q (17) | Resume output | XON flow control |
| ^S (19) | Pause output | XOFF flow control |
| ^Z (26) | Suspend signal (SIGTSTP) | Ctrl-Z to background |
Input Flags
Control how input is processed:
Mode | Description | When to Use |
|---|---|---|
| Ignore parity errors | Serial communication |
| Enable parity checking | Serial communication |
| Strip 8th bit | 7-bit systems |
| Map NL to CR on input | Line ending conversion |
| Ignore carriage return | Unix line endings only |
| Map CR to NL on input | Windows/Unix compatibility |
| Enable XON/XOFF flow control | Flow control |
| Enable input flow control | Prevent buffer overflow |
Local Flags (Terminal Behavior)
Control major terminal features:
Mode | Description | When to Enable | When to Disable |
|---|---|---|---|
| Enable signal generation | Interactive shells | Raw input mode |
| Canonical (line-buffered) mode | Line editing | Character-by-character input |
| Echo input characters | Interactive input | Password entry, automation |
| Visual character erase | User-friendly editing | Raw mode |
| Echo kill character | Show line clear | Silent operations |
| Echo newline even if ECHO off | Debugging | Normal operation |
| Don't flush after interrupt | Keep data | Standard behavior |
| Extended input processing | Full features | Simple processing |
| Echo control chars as ^X | Visible controls | Clean output |
Output Flags
Control output formatting:
Mode | Description | When to Use | |
|---|---|---|---|
| Enable output processing | Normal terminal | Raw output |
| Map NL to CR-NL | Windows compatibility | Unix systems |
| Map CR to NL | Unusual case | Standard operation |
| No CR at column 0 | Special formatting | Normal output |
Common Terminal Mode Recipes
Recipe 1: Disable All Flow Control
Prevent XON/XOFF interfering with binary data:
Recipe 2: Clean Output Processing
Configure proper line ending handling:
Recipe 3: Raw Output Mode
Get output exactly as produced without terminal processing:
TerminalModesBuilder API Reference
SetCharacter()
Set control character values (0-255):
Common control character codes:
^C(Ctrl-C) = 3^D(Ctrl-D) = 4^H(Backspace) = 8^S(Ctrl-S) = 19^Q(Ctrl-Q) = 17^Z(Ctrl-Z) = 26DEL= 127
SetFlag()
Enable or disable boolean mode flags:
SetMode()
Set mode with custom uint32 value:
SetSpeed()
Set both input and output baud rate:
Common baud rates: 9600, 19200, 38400, 57600, 115200
Advanced Example: Custom Terminal Configuration
Debugging Terminal Modes
To see what terminal modes are configured on the remote system:
This shows all terminal settings on the remote server, useful for understanding defaults and troubleshooting issues.
Best Practices
Start with defaults:
// Empty modes = server defaults var options = new CommandExecutionOptions { RequestPty = true };Only customize what you need:
// Minimal changes for specific need var modes = new TerminalModesBuilder() .SetFlag(TerminalMode.ECHO, false) // Only disable echo .Build();Test terminal behavior:
Different servers may interpret modes differently
Test with your target environment
Use appropriate recipes:
Don't disable signals in interactive shells
Don't enable echo for password input
Consider terminal type:
options.TerminalType = TerminalType.Xterm256Color; // Match capabilitiesDocument custom configurations:
// Comment why you're using specific modes .SetFlag(TerminalMode.ECHO, false) // Required for silent password prompt
Common Issues
Issue: Line endings are wrong
Solution: Configure line ending translation:
Issue: Binary data is corrupted
Solution: Disable flow control and processing:
Issue: Need interactive input
Solution: This library does not support interactive terminal I/O. You cannot type input or respond to prompts interactively. Use non-interactive alternatives or pass input via command arguments.
Terminal Mode Reference
See the complete list of all 50+ terminal modes in TerminalMode.cs:
Control Characters (opcodes 1-18): VINTR, VQUIT, VERASE, VKILL, VEOF, VEOL, VSTART, VSTOP, VSUSP, etc.
Input Flags (opcodes 30-41): IGNPAR, INPCK, ISTRIP, INLCR, IGNCR, ICRNL, IXON, IXOFF, etc.
Local Flags (opcodes 50-62): ISIG, ICANON, ECHO, ECHOE, ECHOK, NOFLSH, IEXTEN, ECHOCTL, etc.
Output Flags (opcodes 70-75): OPOST, ONLCR, OCRNL, ONOCR, ONLRET, etc.
Control Flags (opcodes 90-93): CS7, CS8, PARENB, PARODD
Speed Settings (opcodes 128-129): TTY_OP_ISPEED, TTY_OP_OSPEED
See Also
TerminalModesBuilder(TerminalModesBuilder.cs:10) - Build terminal mode configurationsTerminalModeenum (TerminalMode.cs:12) - All available terminal modesCommandExecutionOptions(CommandExecutionOptions.cs:11) - Configure PTY requestsCommand Execution - Using PTY for command execution
Session Lifecycle - Understanding session states