Use openpty-rustix instead of nix

Follow upstream libraries and use rustix to reduce the amount of
dependencies in the future.

Co-authored-by: Christian Duerr <contact@christianduerr.com>
This commit is contained in:
Kirill Chibisov 2023-10-11 03:23:19 +04:00 committed by GitHub
parent e1859e80f6
commit b4130ddf24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 27 deletions

25
Cargo.lock generated
View File

@ -100,11 +100,11 @@ dependencies = [
"libc",
"log",
"miow",
"nix 0.27.1",
"parking_lot",
"piper",
"polling",
"regex-automata",
"rustix-openpty",
"serde",
"serde_json",
"serde_yaml",
@ -1314,17 +1314,6 @@ dependencies = [
"memoffset 0.7.1",
]
[[package]]
name = "nix"
version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053"
dependencies = [
"bitflags 2.4.0",
"cfg-if",
"libc",
]
[[package]]
name = "nom"
version = "7.1.3"
@ -1658,11 +1647,23 @@ checksum = "f25469e9ae0f3d0047ca8b93fc56843f38e6774f0914a107ff8b41be8be8e0b7"
dependencies = [
"bitflags 2.4.0",
"errno",
"itoa",
"libc",
"linux-raw-sys",
"windows-sys 0.48.0",
]
[[package]]
name = "rustix-openpty"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a25c3aad9fc1424eb82c88087789a7d938e1829724f3e4043163baf0d13cfc12"
dependencies = [
"errno",
"libc",
"rustix",
]
[[package]]
name = "ryu"
version = "1.0.15"

View File

@ -33,7 +33,7 @@ unicode-width = "0.1"
vte = { version = "0.12.0", default-features = false, features = ["ansi", "serde"] }
[target.'cfg(unix)'.dependencies]
nix = { version = "0.27.1", default-features = false, features = ["term"] }
rustix-openpty = "0.1.1"
signal-hook = "0.3.10"
[target.'cfg(windows)'.dependencies]

View File

@ -12,12 +12,13 @@ use std::process::{Child, Command, Stdio};
use std::sync::Arc;
use std::{env, ptr};
use libc::{self, c_int, winsize, TIOCSCTTY};
use libc::{self, c_int, TIOCSCTTY};
use log::error;
use nix::pty::openpty;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use nix::sys::termios::{self, InputFlags, SetArg};
use polling::{Event, PollMode, Poller};
use rustix_openpty::openpty;
use rustix_openpty::rustix::termios::Winsize;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use rustix_openpty::rustix::termios::{self, InputModes, OptionalActions};
use signal_hook::consts as sigconsts;
use signal_hook::low_level::pipe as signal_pipe;
@ -39,14 +40,14 @@ macro_rules! die {
}
/// Get raw fds for master/slave ends of a new PTY.
fn make_pty(size: winsize) -> Result<(OwnedFd, OwnedFd)> {
fn make_pty(size: Winsize) -> Result<(OwnedFd, OwnedFd)> {
let mut window_size = size;
window_size.ws_xpixel = 0;
window_size.ws_ypixel = 0;
let ends = openpty(Some(&window_size), None)?;
let ends = openpty(None, Some(&window_size))?;
Ok((ends.master, ends.slave))
Ok((ends.controller, ends.user))
}
/// Really only needed on BSD, but should be fine elsewhere.
@ -201,8 +202,8 @@ pub fn new(config: &PtyConfig, window_size: WindowSize, window_id: u64) -> Resul
#[cfg(any(target_os = "linux", target_os = "macos"))]
if let Ok(mut termios) = termios::tcgetattr(&master) {
// Set character encoding to UTF-8.
termios.input_flags.set(InputFlags::IUTF8, true);
let _ = termios::tcsetattr(&master, SetArg::TCSANOW, &termios);
termios.input_modes.set(InputModes::IUTF8, true);
let _ = termios::tcsetattr(&master, OptionalActions::Now, &termios);
}
let user = ShellUser::from_env()?;
@ -404,20 +405,20 @@ impl OnResize for Pty {
}
}
/// Types that can produce a `libc::winsize`.
/// Types that can produce a `Winsize`.
pub trait ToWinsize {
/// Get a `libc::winsize`.
fn to_winsize(self) -> winsize;
/// Get a `Winsize`.
fn to_winsize(self) -> Winsize;
}
impl ToWinsize for WindowSize {
fn to_winsize(self) -> winsize {
fn to_winsize(self) -> Winsize {
let ws_row = self.num_lines as libc::c_ushort;
let ws_col = self.num_cols as libc::c_ushort;
let ws_xpixel = ws_col * self.cell_width as libc::c_ushort;
let ws_ypixel = ws_row * self.cell_height as libc::c_ushort;
winsize { ws_row, ws_col, ws_xpixel, ws_ypixel }
Winsize { ws_row, ws_col, ws_xpixel, ws_ypixel }
}
}