diff options
author | Jade Lovelace <lix@jade.fyi> | 2024-08-09 23:24:17 -0700 |
---|---|---|
committer | Jade Lovelace <lix@jade.fyi> | 2024-08-10 16:10:16 -0700 |
commit | 0c7619535112e19232384e15e82ffa6a5af7569d (patch) | |
tree | c9500d6bc74b50a8bb39f580ebf5389de10e786c /misc | |
parent | 292567e0b0a4681eb8ca803c26293d70857fe387 (diff) |
build: remove expect as a dependency
I was packaging Lix 2.91 for nixpkgs and was annoyed at the expect
dependency. Turns out that you can replace unbuffer with a pretty-short
Python script.
It became less short after I found out that Linux was converting \n to
\r\n in the terminal subsystem, which was not very funny, but is at
least solved by twiddling termios bits.
Change-Id: I8a2700abcbbf6a9902e01b05b40fa9340c0ab90c
Diffstat (limited to 'misc')
-rw-r--r-- | misc/meson.build | 6 | ||||
-rwxr-xr-x | misc/runinpty.py | 77 |
2 files changed, 83 insertions, 0 deletions
diff --git a/misc/meson.build b/misc/meson.build index a8f09722c..bf3c157f7 100644 --- a/misc/meson.build +++ b/misc/meson.build @@ -4,3 +4,9 @@ subdir('zsh') subdir('systemd') subdir('flake-registry') + +runinpty = configure_file( + copy : true, + input : meson.current_source_dir() / 'runinpty.py', + output : 'runinpty.py', +) diff --git a/misc/runinpty.py b/misc/runinpty.py new file mode 100755 index 000000000..a7649b735 --- /dev/null +++ b/misc/runinpty.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +# SPDX-FileCopyrightText: 2001, 2002, 2003, 2004, 2005, 2006 Python Software Foundation; All Rights Reserved +# SPDX-FileCopyrightText: 2024 Jade Lovelace +# SPDX-License-Identifier: LGPL-2.1-or-later +""" +This script exists to lose Lix a dependency on expect(1) for the ability to run +something in a pty. + +Yes, it could be replaced by script(1) but macOS and Linux script(1) have +diverged sufficiently badly that even specifying a subcommand to run is not the +same. +""" +import pty +import sys +import os +from termios import ONLCR, ONLRET, ONOCR, OPOST, TCSAFLUSH, tcgetattr, tcsetattr +from tty import setraw +import termios + +def setup_terminal(): + # does not matter which fd we use because we are in a fresh pty + modi = tcgetattr(pty.STDOUT_FILENO) + [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] = modi + + # Turning \n into \r\n is not cool, Linux! + oflag &= ~ONLCR + # I don't know what "implementation dependent postprocessing means" but it + # sounds bad + oflag &= ~OPOST + # Assume that NL performs the role of CR; do not insert CRs at column 0 + oflag |= ONLRET | ONOCR + + modi = [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] + + tcsetattr(pty.STDOUT_FILENO, TCSAFLUSH, modi) + + +def spawn(argv: list[str]): + """ + As opposed to pty.spawn, this one more seriously controls the pty settings. + Necessary to turn off such fun functionality as onlcr (LF to CRLF). + + This is essentially copy pasted from pty.spawn, since there is no way to + hook the child pre-execve + """ + pid, master_fd = pty.fork() + if pid == pty.CHILD: + setup_terminal() + os.execlp(argv[0], *argv) + + try: + mode = tcgetattr(pty.STDIN_FILENO) + setraw(pty.STDIN_FILENO) + restore = True + except termios.error: + restore = False + + try: + pty._copy(master_fd, pty._read, pty._read) # type: ignore + finally: + if restore: + tcsetattr(pty.STDIN_FILENO, TCSAFLUSH, mode) # type: ignore + + os.close(master_fd) + return os.waitpid(pid, 0)[1] + + +def main(): + if len(sys.argv) == 1: + print(f'Usage: {sys.argv[0]} [command args]', file=sys.stderr) + sys.exit(1) + + sys.exit(os.waitstatus_to_exitcode(spawn(sys.argv[1:]))) + + +if __name__ == '__main__': + main() |