Skip to content

Instantly share code, notes, and snippets.

@z64

z64/term.odin Secret

Created February 16, 2023 23:30
Show Gist options
  • Save z64/f29dbd97fa3579896787f38c7386b184 to your computer and use it in GitHub Desktop.
Save z64/f29dbd97fa3579896787f38c7386b184 to your computer and use it in GitHub Desktop.
import "core:c"
import "core:os"
foreign import libc_ext "system:c"
foreign libc_ext {
tcgetattr :: proc(fd: os.Handle, termios_p: ^termios) -> c.int ---
tcsetattr :: proc(fd: os.Handle, optional_actions: TCSetAttr_Optional_Actions, termios_p: ^termios) -> c.int ---
ioctl :: proc(fd: os.Handle, command: c.int, #c_vararg args: ..any) -> c.int ---
strerror :: proc(errnum: c.int) -> cstring ---
}
TIOCGWINSZ :: 0x5413 // ioctl
cc_t :: c.uchar // termios
speed_t :: c.uint // termios
tcflag_t :: c.uint // termios
NCCS :: 32 // termios
winsize :: struct {
ws_row: c.ushort,
ws_col: c.ushort,
ws_xpixel: c.ushort,
ws_ypixel: c.ushort,
}
termios :: struct {
c_iflag: tcflag_t,
c_oflag: tcflag_t,
c_cflag: tcflag_t,
c_lflag: tcflag_t,
c_line: cc_t,
c_cc: [NCCS]cc_t,
c_ispeed: speed_t,
c_ospeed: speed_t,
}
TCSetAttr_Optional_Actions :: enum c.int {
NOW = 0,
DRAIN = 1,
FLUSH = 2,
}
Input_Flag :: enum tcflag_t {
IGNBRK = 0o0000001, /* Ignore break condition. */
BRKINT = 0o0000002, /* Signal interrupt on break. */
IGNPAR = 0o0000004, /* Ignore characters with parity errors. */
PARMRK = 0o0000010, /* Mark parity and framing errors. */
INPCK = 0o0000020, /* Enable input parity check. */
ISTRIP = 0o0000040, /* Strip 8th bit off characters. */
INLCR = 0o0000100, /* Map NL to CR on input. */
IGNCR = 0o0000200, /* Ignore CR. */
ICRNL = 0o0000400, /* Map CR to NL on input. */
IUCLC = 0o0001000, /* Map uppercase characters to lowercase on input (not in POSIX). */
IXON = 0o0002000, /* Enable start/stop output control. */
IXANY = 0o0004000, /* Enable any character to restart output. */
IXOFF = 0o0010000, /* Enable start/stop input control. */
IMAXBEL = 0o0020000, /* Ring bell when input queue is full (not in POSIX). */
IUTF8 = 0o0040000, /* Input is UTF8 (not in POSIX). */
}
Control_Flag :: enum tcflag_t {
CSIZE = 0o0000060,
CS5 = 0o0000000,
CS6 = 0o0000020,
CS7 = 0o0000040,
CS8 = 0o0000060,
CSTOPB = 0o0000100,
CREAD = 0o0000200,
PARENB = 0o0000400,
PARODD = 0o0001000,
HUPCL = 0o0002000,
CLOCAL = 0o0004000,
}
Output_Flag :: enum tcflag_t {
OPOST = 0o0000001, /* Post-process output. */
OLCUC = 0o0000002, /* Map lowercase characters to uppercase on output. (not in POSIX). */
ONLCR = 0o0000004, /* Map NL to CR-NL on output. */
OCRNL = 0o0000010, /* Map CR to NL on output. */
ONOCR = 0o0000020, /* No CR output at column 0. */
ONLRET = 0o0000040, /* NL performs CR function. */
OFILL = 0o0000100, /* Use fill characters for delay. */
OFDEL = 0o0000200, /* Fill is DEL. */
}
Local_Flag :: enum tcflag_t {
ISIG = 0o0000001, /* Enable signals. */
ICANON = 0o0000002, /* Canonical input (erase and kill processing). */
ECHO = 0o0000010, /* Enable echo. */
ECHOE = 0o0000020, /* Echo erase character as error-correcting backspace. */
ECHOK = 0o0000040, /* Echo KILL. */
ECHONL = 0o0000100, /* Echo NL. */
NOFLSH = 0o0000200, /* Disable flush after interrupt or quit. */
TOSTOP = 0o0000400, /* Send SIGTTOU for background output. */
IEXTEN = 0o0100000, /* Enable implementation-defined input */
}
CC_Index :: enum cc_t {
VINTR = 0,
VQUIT = 1,
VERASE = 2,
VKILL = 3,
VEOF = 4,
VTIME = 5,
VMIN = 6,
VSWTC = 7,
VSTART = 8,
VSTOP = 9,
VSUSP = 10,
VEOL = 11,
VREPRINT = 12,
VDISCARD = 13,
VWERASE = 14,
VLNEXT = 15,
VEOL2 = 16,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment