Use ctermid() instead of hardcoding /dev/tty

This commit is contained in:
Kovid Goyal 2018-08-04 20:58:01 +05:30
parent 05b817f5f5
commit d964146f8c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
6 changed files with 14 additions and 8 deletions

3
glfw/wl_window.c vendored
View File

@ -1057,7 +1057,8 @@ void _glfwPlatformRequestWindowAttention(_GLFWwindow* window)
int _glfwPlatformWindowBell(_GLFWwindow* window)
{
// TODO: Use an actual Wayland API to implement this when one becomes available
int fd = open("/dev/tty", O_WRONLY | O_CLOEXEC);
static char tty[L_ctermid + 1];
int fd = open(ctermid(tty), O_WRONLY | O_CLOEXEC);
if (fd > -1) {
int ret = write(fd, "\x07", 1) == 1 ? GLFW_TRUE : GLFW_FALSE;
close(fd);

View File

@ -2,7 +2,9 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
import os
import sys
from kitty.cli import parse_args
from ..tui.handler import Handler
@ -62,7 +64,7 @@ def main(args):
data = None
if not sys.stdin.isatty():
data = sys.stdin.buffer.read()
sys.stdin = open('/dev/tty', 'r')
sys.stdin = open(os.ctermid(), 'r')
loop = Loop()
handler = Clipboard(data, args)
loop.loop(handler)

View File

@ -381,7 +381,7 @@ def main(args):
return
else:
text = sys.stdin.buffer.read().decode('utf-8')
sys.stdin = open('/dev/tty')
sys.stdin = open(os.ctermid())
try:
args, items = parse_hints_args(args[1:])
except SystemExit as e:

View File

@ -260,13 +260,13 @@ def main(args=sys.argv):
if args.print_window_size:
screen_size_function.ans = None
with open('/dev/tty') as tty:
with open(os.ctermid()) as tty:
ss = screen_size_function(tty)()
print('{}x{}'.format(ss.width, ss.height), end='')
raise SystemExit(0)
if not sys.stdout.isatty():
sys.stdout = open('/dev/tty', 'w')
sys.stdout = open(os.ctermid(), 'w')
screen_size = screen_size_function()
signal.signal(signal.SIGWINCH, lambda signum, frame: setattr(screen_size, 'changed', True))
if screen_size().width == 0:

View File

@ -2,11 +2,12 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
import os
import sys
from kitty.cli import parse_args
from ..tui.operations import styled
from ..tui.operations import styled
OPTIONS = '''\
--title
@ -17,7 +18,7 @@ The title for the error message.
def real_main(args):
error_message = sys.stdin.buffer.read().decode('utf-8')
sys.stdin = open('/dev/tty')
sys.stdin = open(os.ctermid())
msg = 'Show an error message'
args, items = parse_args(args, OPTIONS, '', msg, 'hints')
print(styled(args.title, fg_intense=True, fg='red', bold=True))

View File

@ -18,6 +18,7 @@
#include <termios.h>
#include <signal.h>
#include <fcntl.h>
#include <stdio.h>
#ifdef WITH_PROFILER
#include <gperftools/profiler.h>
#endif
@ -123,7 +124,8 @@ open_tty(PyObject *self UNUSED, PyObject *args) {
if (!PyArg_ParseTuple(args, "|p", &read_with_timeout)) return NULL;
int flags = O_RDWR | O_CLOEXEC | O_NOCTTY;
if (!read_with_timeout) flags |= O_NONBLOCK;
int fd = open("/dev/tty", flags);
static char ctty[L_ctermid+1];
int fd = open(ctermid(ctty), flags);
struct termios *termios_p = calloc(1, sizeof(struct termios));
if (!termios_p) return PyErr_NoMemory();
if (tcgetattr(fd, termios_p) != 0) { free(termios_p); PyErr_SetFromErrno(PyExc_OSError); return NULL; }