Skip to content

Commit

Permalink
Add simulations for termios and tty.
Browse files Browse the repository at this point in the history
  • Loading branch information
antarcticrainforest committed Jul 22, 2024
1 parent 0e1355e commit c9fa0ed
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
75 changes: 75 additions & 0 deletions assets/unix_mock/src/termios/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Simulation of the termios unix library for Windows."""

# Constants for when argument in tcsetattr()
TCSANOW = 0
TCSADRAIN = 1
TCSAFLUSH = 2

# Constants for tcflush() argument
TCIFLUSH = 0
TCOFLUSH = 1
TCIOFLUSH = 2

# Indexes for the mode array
IFLAG = 0
OFLAG = 1
CFLAG = 2
LFLAG = 3
ISPEED = 4
OSPEED = 5
CC = 6

# Control characters indexes
VINTR = 0
VQUIT = 1
VERASE = 2
VKILL = 3
VEOF = 4
VTIME = 5
VMIN = 6
VSWTCH = 7
VSTART = 8
VSTOP = 9
VSUSP = 10
VEOL = 11
VREPRINT = 12
VDISCARD = 13
VWERASE = 14
VLNEXT = 15
VEOL2 = 16

# Sample mode array to simulate termios attributes
default_mode = [0] * 7
default_mode[IFLAG] = 0
default_mode[OFLAG] = 0
default_mode[CFLAG] = 0
default_mode[LFLAG] = 0
default_mode[ISPEED] = 0
default_mode[OSPEED] = 0
default_mode[CC] = [0] * 32


def tcgetattr(fd):
"""Get the parameters associated with the terminal."""
# This is a mock implementation, returning a default mode array.
return default_mode


def tcsetattr(fd, when, attributes):
"""Set the parameters associated with the terminal."""
# This is a mock implementation. We won't actually change any attributes.
pass


def tcflush(fd, queue):
"""Discard queued data on the terminal."""
# This is a mock implementation. We won't actually flush anything.
pass


def ioctl(fd, request, argp):
"""Control device parameters."""
if request == 0x5413: # TIOCGWINSZ
# Assume a default terminal size (rows, cols, x pixels, y pixels)
return (24, 80, 640, 480)
raise NotImplementedError("ioctl request not supported in this mock.")
33 changes: 33 additions & 0 deletions assets/unix_mock/src/tty/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Simulation of the tty unix library for Windows."""

import termios

IFLAG = termios.IFLAG
OFLAG = termios.OFLAG
CFLAG = termios.CFLAG
LFLAG = termios.LFLAG
CC = termios.CC


def setraw(fd, when=termios.TCSANOW):
"""Put terminal into raw mode."""
mode = termios.tcgetattr(fd)
mode[termios.IFLAG] = mode[termios.IFLAG] & ~(
termios.BRKINT | termios.ICRNL | termios.INPCK | termios.ISTRIP | termios.IXON
)
mode[termios.OFLAG] = mode[termios.OFLAG] & ~(termios.OPOST)
mode[termios.CFLAG] = mode[termios.CFLAG] & ~(termios.CSIZE | termios.PARENB)
mode[termios.CFLAG] = mode[termios.CFLAG] | termios.CS8
mode[termios.LFLAG] = mode[termios.LFLAG] & ~(
termios.ECHO | termios.ICANON | termios.IEXTEN | termios.ISIG
)
mode[termios.CC][termios.VMIN] = 1
mode[termios.CC][termios.VTIME] = 0
termios.tcsetattr(fd, when, mode)


def setcbreak(fd, when=termios.TCSANOW):
"""Put terminal into cbreak mode."""
mode = termios.tcgetattr(fd)
mode[termios.LFLAG] = mode[termios.LFLAG] & ~(termios.ICANON)
termios.tcsetattr(fd, when, mode)

0 comments on commit c9fa0ed

Please sign in to comment.