miniOS
x86_64 hobby kernel with SMP, VFS, and POSIX process model
Loading...
Searching...
No Matches
TTY Line Discipline

Kernel TTY: line discipline, canonical/raw input, termios, and ioctl. More...

Classes

struct  minios_termios
struct  minios_winsize

Macros

#define TTY_LFLAG_ISIG   0x0001u
#define TTY_LFLAG_ICANON   0x0002u
#define TTY_LFLAG_ECHO   0x0008u
#define VINTR   0
#define VEOF   1
#define TTY_NCCS   8
#define TCGETS   0x5401UL
#define TCSETS   0x5402UL
#define TCSETSW   0x5403UL
#define TCSETSF   0x5404UL
#define TIOCSCTTY   0x540EUL
#define TIOCGPGRP   0x540FUL /* get foreground process group id */
#define TIOCSPGRP   0x5410UL /* set foreground process group id */
#define TIOCGWINSZ   0x5413UL
#define TIOCNOTTY   0x5422UL

Functions

void tty_init (void)
 Sets up the canonical input ring buffer, initialises the default termios (c_lflag = ICANON|ECHO|ISIG, VINTR=^C, VEOF=^D), and sets the window size to SCREEN_ROWS x SCREEN_COLS. Must be called once during kernel boot before tty_keyboard_input() or tty_read() are used.
void tty_keyboard_input (char c)
 In canonical mode (ICANON set): appends to the line buffer; on newline or VEOF flushes the complete line to the read buffer. In raw mode: writes directly to the read buffer. Echoes to vt_write_byte() if ECHO is set. Sends SIGINT to the foreground process group if ISIG is set and == VINTR. Called from the keyboard interrupt handler (IRQ1).
void tty_inject_escape (const char *seq, int len)
int tty_has_data (void)
int tty_read (void *buf, uint32_t len, int nonblock)
 In canonical mode: blocks (or returns -EAGAIN if nonblock) until a complete line is available, then copies up to @len bytes. In raw mode: returns as many bytes as are immediately available up to @len.
int tty_write (const void *buf, uint32_t len)
 Forwards each byte to vt_write_byte(), which handles ANSI/VT escape sequence parsing and VGA cell rendering.
int tty_ioctl (uint64_t cmd, uint64_t arg)
 TCGETS: copies current termios to *arg. TCSETS/TCSETSW: copies termios from *arg into the kernel TTY state. TIOCGWINSZ: copies the window size (SCREEN_ROWS x SCREEN_COLS) to *arg. TIOCSCTTY/TIOCNOTTY: accepted (no-op in single-TTY kernel).
int tty_isatty_fd (int fd)
 Returns 1 if the fd's vfs_file entry has ftype == VFS_FILE_TYPE_CHAR and device_type == VFS_DEVICE_TTY, 0 otherwise. Used by the isatty(3) Newlib stub.

Detailed Description

Kernel TTY: line discipline, canonical/raw input, termios, and ioctl.

The TTY layer sits between the keyboard driver and userspace. It implements a POSIX-like line discipline: canonical buffering with echo, SIGINT on ^C (ISIG), and raw mode (ICANON cleared). Exposes the /dev/tty device via tty_read()/tty_write() and processes TCGETS/TCSETS/TIOCGWINSZ via tty_ioctl().

Macro Definition Documentation

◆ TCGETS

#define TCGETS   0x5401UL

◆ TCSETS

#define TCSETS   0x5402UL

◆ TCSETSF

#define TCSETSF   0x5404UL

◆ TCSETSW

#define TCSETSW   0x5403UL

◆ TIOCGPGRP

#define TIOCGPGRP   0x540FUL /* get foreground process group id */

◆ TIOCGWINSZ

#define TIOCGWINSZ   0x5413UL

◆ TIOCNOTTY

#define TIOCNOTTY   0x5422UL

◆ TIOCSCTTY

#define TIOCSCTTY   0x540EUL

◆ TIOCSPGRP

#define TIOCSPGRP   0x5410UL /* set foreground process group id */

◆ TTY_LFLAG_ECHO

#define TTY_LFLAG_ECHO   0x0008u

◆ TTY_LFLAG_ICANON

#define TTY_LFLAG_ICANON   0x0002u

◆ TTY_LFLAG_ISIG

#define TTY_LFLAG_ISIG   0x0001u

◆ TTY_NCCS

#define TTY_NCCS   8

◆ VEOF

#define VEOF   1

◆ VINTR

#define VINTR   0

Function Documentation

◆ tty_has_data()

int tty_has_data ( void )

tty_has_data() - Return non-zero if the TTY input ring buffer has unread bytes. Used by poll() to report POLLIN readiness for stdin.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tty_init()

void tty_init ( void )

Sets up the canonical input ring buffer, initialises the default termios (c_lflag = ICANON|ECHO|ISIG, VINTR=^C, VEOF=^D), and sets the window size to SCREEN_ROWS x SCREEN_COLS. Must be called once during kernel boot before tty_keyboard_input() or tty_read() are used.

tty_init() - Initialise the TTY subsystem.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tty_inject_escape()

void tty_inject_escape ( const char * seq,
int len )

tty_inject_escape() - Inject a multi-byte escape sequence directly into the TTY read buffer, bypassing echo and canonical buffering. @seq: Byte sequence to inject (e.g. "\x1b[A" for cursor-up). @len: Number of bytes to push.

Use for special keys (arrow keys, function keys) whose escape sequences must never be echoed to the screen and must bypass ICANON line buffering.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tty_ioctl()

int tty_ioctl ( uint64_t cmd,
uint64_t arg )

TCGETS: copies current termios to *arg. TCSETS/TCSETSW: copies termios from *arg into the kernel TTY state. TIOCGWINSZ: copies the window size (SCREEN_ROWS x SCREEN_COLS) to *arg. TIOCSCTTY/TIOCNOTTY: accepted (no-op in single-TTY kernel).

tty_ioctl() - Handle terminal ioctl commands.

Parameters
cmdIoctl command code (TCGETS=0x5401, TCSETS=0x5402, TCSETSW=0x5403, TIOCSCTTY=0x540E, TIOCGWINSZ=0x5413, TIOCNOTTY=0x5422).
argUser-space pointer to a struct minios_termios or struct minios_winsize, depending on @cmd.
Returns
0 on success, -EINVAL for unknown @cmd.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tty_isatty_fd()

int tty_isatty_fd ( int fd)

Returns 1 if the fd's vfs_file entry has ftype == VFS_FILE_TYPE_CHAR and device_type == VFS_DEVICE_TTY, 0 otherwise. Used by the isatty(3) Newlib stub.

tty_isatty_fd() - Test whether a file descriptor refers to the TTY device.

Parameters
fdFile descriptor to test.
Returns
1 if @fd is the TTY, 0 if not.

◆ tty_keyboard_input()

void tty_keyboard_input ( char c)

In canonical mode (ICANON set): appends to the line buffer; on newline or VEOF flushes the complete line to the read buffer. In raw mode: writes directly to the read buffer. Echoes to vt_write_byte() if ECHO is set. Sends SIGINT to the foreground process group if ISIG is set and == VINTR. Called from the keyboard interrupt handler (IRQ1).

tty_keyboard_input() - Feed a raw character from the keyboard ISR into the TTY.

Parameters
cRaw character byte from the PS/2 keyboard driver.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tty_read()

int tty_read ( void * buf,
uint32_t len,
int nonblock )

In canonical mode: blocks (or returns -EAGAIN if nonblock) until a complete line is available, then copies up to @len bytes. In raw mode: returns as many bytes as are immediately available up to @len.

tty_read() - Read bytes from the TTY input buffer.

Parameters
bufDestination buffer.
lenMaximum number of bytes to read.
nonblockNon-zero to return immediately if no data is available (O_NONBLOCK).
Returns
Number of bytes read, 0 on EOF (VEOF character), or -1 on error.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tty_write()

int tty_write ( const void * buf,
uint32_t len )

Forwards each byte to vt_write_byte(), which handles ANSI/VT escape sequence parsing and VGA cell rendering.

tty_write() - Write bytes to the TTY output (VGA terminal).

Parameters
bufSource buffer of bytes to write.
lenNumber of bytes.
Returns
Number of bytes written (@len on success, -1 on error).
Here is the call graph for this function:
Here is the caller graph for this function: