Skip to content

Commit

Permalink
Ignore termios fields we don't modify
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Jun 16, 2024
1 parent 6cabc78 commit cab0b8f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,18 @@ internal object SyscallHandlerJnaLinux : SyscallHandlerJvmPosix() {
oflag = termios.c_oflag.toUInt(),
cflag = termios.c_cflag.toUInt(),
lflag = termios.c_lflag.toUInt(),
cline = termios.c_line,
cc = termios.c_cc.copyOf(),
ispeed = termios.c_ispeed.toUInt(),
ospeed = termios.c_ospeed.toUInt(),
)
}

override fun setStdinTermios(termios: Termios) {
val nativeTermios = PosixLibC.termios()
libC.tcgetattr(STDIN_FILENO, nativeTermios)
nativeTermios.c_iflag = termios.iflag.toInt()
nativeTermios.c_oflag = termios.oflag.toInt()
nativeTermios.c_cflag = termios.cflag.toInt()
nativeTermios.c_lflag = termios.lflag.toInt()
nativeTermios.c_line = termios.cline
termios.cc.copyInto(nativeTermios.c_cc)
nativeTermios.c_ispeed = termios.ispeed.toInt()
nativeTermios.c_ospeed = termios.ospeed.toInt()
libC.tcsetattr(STDIN_FILENO, TCSADRAIN, nativeTermios)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ private interface MacosLibC : Library {
@JvmField
var c_lflag: NativeLong = NativeLong()

@JvmField
var c_line: Byte = 0

@JvmField
var c_cc: ByteArray = ByteArray(20)

Expand Down Expand Up @@ -100,23 +97,18 @@ internal object SyscallHandlerJnaMacos : SyscallHandlerJvmPosix() {
oflag = termios.c_oflag.toInt().toUInt(),
cflag = termios.c_cflag.toInt().toUInt(),
lflag = termios.c_lflag.toInt().toUInt(),
cline = termios.c_line,
cc = termios.c_cc.copyOf(),
ispeed = termios.c_ispeed.toInt().toUInt(),
ospeed = termios.c_ospeed.toInt().toUInt(),
)
}

override fun setStdinTermios(termios: Termios) {
val nativeTermios = MacosLibC.termios()
libC.tcgetattr(STDIN_FILENO, nativeTermios)
nativeTermios.c_iflag = NativeLong(termios.iflag.toLong())
nativeTermios.c_oflag = NativeLong(termios.oflag.toLong())
nativeTermios.c_cflag = NativeLong(termios.cflag.toLong())
nativeTermios.c_lflag = NativeLong(termios.lflag.toLong())
nativeTermios.c_line = termios.cline
termios.cc.copyInto(nativeTermios.c_cc)
nativeTermios.c_ispeed = NativeLong(termios.ispeed.toLong())
nativeTermios.c_ospeed = NativeLong(termios.ospeed.toLong())
libC.tcsetattr(STDIN_FILENO, TCSANOW, nativeTermios)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private object PosixLibC {
// external fun tcgetattr(fd: Int, termios: termios): Int
//
// @CFunction("tcsetattr")
// external fun tcsetattr(fd: Int, cmd: Int, termios: termios)
// external fun tcsetattr(fd: Int, cmd: Int, termios: termios): Int
}

@Platforms(Platform.LINUX::class, Platform.MACOS::class)
Expand Down Expand Up @@ -119,29 +119,30 @@ internal class SyscallHandlerNativeImagePosix : SyscallHandlerJvmPosix() {
persists.
*/
// val termios = StackValue.get(PosixLibC.termios::class.java)
// if(PosixLibC.tcgetattr(STDIN_FILENO, termios) != 0) return null
// if (PosixLibC.tcgetattr(STDIN_FILENO, termios) != 0) {
// throw RuntimeException("Error reading terminal attributes")
// }
// return Termios(
// iflag = termios.c_iflag.toUInt(),
// oflag = termios.c_oflag.toUInt(),
// cflag = termios.c_cflag.toUInt(),
// lflag = termios.c_lflag.toUInt(),
// cline = termios.c_line,
// cc = ByteArray(PosixLibC.NCCS()) { termios.c_cc.read(it) },
// ispeed = termios.c_ispeed.toUInt(),
// ospeed = termios.c_ospeed.toUInt(),
// )
}

override fun setStdinTermios(termios: Termios) {
// val nativeTermios = StackValue.get(PosixLibC.termios::class.java)
// if (PosixLibC.tcgetattr(STDIN_FILENO, nativeTermios) != 0) {
// throw RuntimeException("Error reading terminal attributes")
// }
// nativeTermios.c_iflag = termios.iflag.toInt()
// nativeTermios.c_oflag = termios.oflag.toInt()
// nativeTermios.c_cflag = termios.cflag.toInt()
// nativeTermios.c_lflag = termios.lflag.toInt()
// nativeTermios.c_line = termios.cline
// termios.cc.forEachIndexed { i, b -> nativeTermios.c_cc.write(i, b) }
// nativeTermios.c_ispeed = termios.ispeed.toInt()
// nativeTermios.c_ospeed = termios.ospeed.toInt()
// PosixLibC.tcsetattr(STDIN_FILENO, PosixLibC.TCSADRAIN(), nativeTermios)
// if (PosixLibC.tcsetattr(STDIN_FILENO, PosixLibC.TCSADRAIN(), nativeTermios) != 0) {
// throw RuntimeException("Error setting terminal attributes")
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ internal abstract class SyscallHandlerPosix : SyscallHandler {
val oflag: UInt,
val cflag: UInt,
val lflag: UInt,
val cline: Byte,
val cc: ByteArray,
val ispeed: UInt,
val ospeed: UInt,
)

abstract fun getStdinTermios(): Termios
Expand All @@ -169,13 +166,10 @@ internal abstract class SyscallHandlerPosix : SyscallHandler {
oflag = orig.oflag,
cflag = orig.cflag or CS8,
lflag = orig.lflag and (ECHO or ICANON or IEXTEN or ISIG).inv(),
cline = orig.cline,
cc = orig.cc.copyOf().also {
it[VMIN] = 0 // min wait time on read
it[VTIME] = 1 // max wait time on read, in 10ths of a second
},
ispeed = orig.ispeed,
ospeed = orig.ospeed,
)
setStdinTermios(new)
when (mouseTracking) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,25 @@ internal object SyscallHandlerNativePosix : SyscallHandlerPosix() {
oflag = termios.c_oflag.convert(),
cflag = termios.c_cflag.convert(),
lflag = termios.c_lflag.convert(),
cline = termios.c_line.convert(),
cc = ByteArray(NCCS) { termios.c_cc[it].convert() },
ispeed = termios.c_ispeed.convert(),
ospeed = termios.c_ospeed.convert(),
)
}

override fun setStdinTermios(termios: Termios): Unit = memScoped {
val nativeTermios = alloc<termios>()
// different platforms have different fields in termios, so we need to read the current
// struct before we set the fields we carre about.
if (tcgetattr(STDIN_FILENO, nativeTermios.ptr) != 0) {
throw RuntimeException("Error reading terminal attributes")
}
nativeTermios.c_iflag = termios.iflag.convert()
nativeTermios.c_oflag = termios.oflag.convert()
nativeTermios.c_cflag = termios.cflag.convert()
nativeTermios.c_lflag = termios.lflag.convert()
nativeTermios.c_line = termios.cline.convert()
repeat(NCCS) { nativeTermios.c_cc[it] = termios.cc[it].convert() }
nativeTermios.c_ispeed = termios.ispeed.convert()
nativeTermios.c_ospeed = termios.ospeed.convert()
tcsetattr(platform.posix.STDIN_FILENO, TCSADRAIN, nativeTermios.ptr)
if (tcsetattr(platform.posix.STDIN_FILENO, TCSADRAIN, nativeTermios.ptr) != 0) {
throw RuntimeException("Error setting terminal attributes")
}
}

override fun readRawByte(t0: ComparableTimeMark, timeout: Duration): Char = memScoped {
Expand Down

0 comments on commit cab0b8f

Please sign in to comment.