WinTer 0.1.1
Windows Terminal Emulator
Loading...
Searching...
No Matches
pty.c File Reference

Platform-specific implementation of the PTY interface. More...

#include "pty.h"
#include <stdio.h>
Include dependency graph for pty.c:

Go to the source code of this file.

Functions

INTERNAL void safe_close_handle (HANDLE *h)
 Safely closes a Win32 HANDLE and sets the pointer to NULL.
b32 pty_init (struct pty_state_t *state, u16 columns, u16 rows)
 Initializes a Pseudo Console and establishes bidirectional communication pipes.
b32 pty_spawn (struct pty_state_t *state, LPCWSTR command_line)
 Spawns a shell process and attaches it to an initialized Pseudo Console.
void pty_cleanup (struct pty_state_t *state)
 Terminates the Pseudo Console session and releases all active system handles.

Detailed Description

Platform-specific implementation of the PTY interface.

Implements the full three-phase pseudo-terminal lifecycle for Windows: pipe creation, ConPTY initialization, and shell process spawning via the extended STARTUPINFOEXW / PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE API.

Implementation Notes
  • All Win32 handle variables are initialized to NULL and cleaned up on any failure path to prevent handle leaks.
  • HeapAlloc is used (rather than malloc) for the process thread attribute list because this memory is handed directly to Win32 APIs. Using the process heap signals intent and avoids any CRT heap / Win32 heap mismatch edge cases.
  • The pipe ends consumed by ConPTY (hInputRead, hOutputWrite) are closed immediately after CreatePseudoConsole succeeds. This is mandatory: a pipe only signals EOF when every process holding the write end has closed it. Keeping a duplicate write handle would cause our ReadFile loop to block indefinitely after the shell exits.

Definition in file pty.c.