2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2021-01-14 23:32:27 -05:00
|
|
|
use super::io::StdFileResource;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2021-05-02 19:22:57 -04:00
|
|
|
use deno_core::Extension;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-13 13:45:53 -05:00
|
|
|
use std::io::Error;
|
2020-09-14 12:48:57 -04:00
|
|
|
|
2020-02-26 01:01:24 -05:00
|
|
|
#[cfg(unix)]
|
|
|
|
use nix::sys::termios;
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
#[cfg(windows)]
|
|
|
|
use deno_core::error::custom_error;
|
2020-02-26 01:01:24 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
use winapi::shared::minwindef::DWORD;
|
|
|
|
#[cfg(windows)]
|
|
|
|
use winapi::um::wincon;
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-02-26 01:01:24 -05:00
|
|
|
#[cfg(windows)]
|
|
|
|
fn get_windows_handle(
|
|
|
|
f: &std::fs::File,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<std::os::windows::io::RawHandle, AnyError> {
|
2020-02-26 01:01:24 -05:00
|
|
|
use std::os::windows::io::AsRawHandle;
|
|
|
|
use winapi::um::handleapi;
|
|
|
|
|
|
|
|
let handle = f.as_raw_handle();
|
|
|
|
if handle == handleapi::INVALID_HANDLE_VALUE {
|
2020-12-13 13:45:53 -05:00
|
|
|
return Err(Error::last_os_error().into());
|
2020-02-26 01:01:24 -05:00
|
|
|
} else if handle.is_null() {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(custom_error("ReferenceError", "null handle"));
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
|
|
|
Ok(handle)
|
|
|
|
}
|
|
|
|
|
2021-05-02 19:22:57 -04:00
|
|
|
pub fn init() -> Extension {
|
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
2022-09-28 09:03:56 -04:00
|
|
|
op_stdin_set_raw::decl(),
|
2022-03-14 13:44:15 -04:00
|
|
|
op_isatty::decl(),
|
|
|
|
op_console_size::decl(),
|
2021-05-02 19:22:57 -04:00
|
|
|
])
|
|
|
|
.build()
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
|
|
|
|
2022-09-23 00:05:45 -04:00
|
|
|
#[op(fast)]
|
2022-09-28 09:03:56 -04:00
|
|
|
fn op_stdin_set_raw(
|
2022-09-23 00:05:45 -04:00
|
|
|
state: &mut OpState,
|
|
|
|
is_raw: bool,
|
2020-11-30 11:08:03 -05:00
|
|
|
cbreak: bool,
|
2022-09-23 00:05:45 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2022-09-28 09:03:56 -04:00
|
|
|
let rid = 0; // stdin is always rid=0
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-02-26 01:01:24 -05:00
|
|
|
// From https://github.com/kkawakam/rustyline/blob/master/src/tty/windows.rs
|
|
|
|
// and https://github.com/kkawakam/rustyline/blob/master/src/tty/unix.rs
|
|
|
|
// and https://github.com/crossterm-rs/crossterm/blob/e35d4d2c1cc4c919e36d242e014af75f6127ab50/src/terminal/sys/windows.rs
|
|
|
|
// Copyright (c) 2015 Katsu Kawakami & Rustyline authors. MIT license.
|
|
|
|
// Copyright (c) 2019 Timon. MIT license.
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
use std::os::windows::io::AsRawHandle;
|
2020-03-04 18:52:08 -05:00
|
|
|
use winapi::shared::minwindef::FALSE;
|
2020-02-26 01:01:24 -05:00
|
|
|
use winapi::um::{consoleapi, handleapi};
|
|
|
|
|
2020-11-30 11:08:03 -05:00
|
|
|
if cbreak {
|
2022-05-10 10:13:08 -04:00
|
|
|
return Err(deno_core::error::not_supported());
|
2020-11-30 11:08:03 -05:00
|
|
|
}
|
2020-12-16 11:14:12 -05:00
|
|
|
|
2022-07-13 11:16:42 -04:00
|
|
|
StdFileResource::with_file(state, rid, move |std_file| {
|
|
|
|
let handle = std_file.as_raw_handle();
|
2020-12-16 11:14:12 -05:00
|
|
|
|
2022-07-13 11:16:42 -04:00
|
|
|
if handle == handleapi::INVALID_HANDLE_VALUE {
|
|
|
|
return Err(Error::last_os_error().into());
|
|
|
|
} else if handle.is_null() {
|
|
|
|
return Err(custom_error("ReferenceError", "null handle"));
|
|
|
|
}
|
|
|
|
let mut original_mode: DWORD = 0;
|
2022-07-15 12:30:25 -04:00
|
|
|
// SAFETY: winapi call
|
2022-07-13 11:16:42 -04:00
|
|
|
if unsafe { consoleapi::GetConsoleMode(handle, &mut original_mode) }
|
|
|
|
== FALSE
|
|
|
|
{
|
|
|
|
return Err(Error::last_os_error().into());
|
|
|
|
}
|
2022-12-05 09:46:04 -05:00
|
|
|
|
|
|
|
const RAW_MODE_MASK: DWORD = wincon::ENABLE_LINE_INPUT
|
|
|
|
| wincon::ENABLE_ECHO_INPUT
|
|
|
|
| wincon::ENABLE_PROCESSED_INPUT;
|
2022-07-13 11:16:42 -04:00
|
|
|
let new_mode = if is_raw {
|
2022-12-05 09:46:04 -05:00
|
|
|
original_mode & !RAW_MODE_MASK | wincon::ENABLE_VIRTUAL_TERMINAL_INPUT
|
2022-07-13 11:16:42 -04:00
|
|
|
} else {
|
2022-12-05 09:46:04 -05:00
|
|
|
original_mode | RAW_MODE_MASK & !wincon::ENABLE_VIRTUAL_TERMINAL_INPUT
|
2022-07-13 11:16:42 -04:00
|
|
|
};
|
2022-12-05 09:46:04 -05:00
|
|
|
|
2022-07-15 12:30:25 -04:00
|
|
|
// SAFETY: winapi call
|
2022-07-13 11:16:42 -04:00
|
|
|
if unsafe { consoleapi::SetConsoleMode(handle, new_mode) } == FALSE {
|
|
|
|
return Err(Error::last_os_error().into());
|
|
|
|
}
|
2020-02-26 01:01:24 -05:00
|
|
|
|
2022-07-13 11:16:42 -04:00
|
|
|
Ok(())
|
|
|
|
})
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
|
2022-07-13 11:16:42 -04:00
|
|
|
StdFileResource::with_file_and_metadata(
|
|
|
|
state,
|
|
|
|
rid,
|
|
|
|
move |std_file, meta_data| {
|
|
|
|
let raw_fd = std_file.as_raw_fd();
|
|
|
|
|
|
|
|
if is_raw {
|
2022-09-04 22:33:06 -04:00
|
|
|
let mut raw = {
|
|
|
|
let mut meta_data = meta_data.lock();
|
|
|
|
let maybe_tty_mode = &mut meta_data.tty.mode;
|
|
|
|
if maybe_tty_mode.is_none() {
|
|
|
|
// Save original mode.
|
|
|
|
let original_mode = termios::tcgetattr(raw_fd)?;
|
|
|
|
maybe_tty_mode.replace(original_mode);
|
|
|
|
}
|
|
|
|
maybe_tty_mode.clone().unwrap()
|
|
|
|
};
|
2022-07-13 11:16:42 -04:00
|
|
|
|
|
|
|
raw.input_flags &= !(termios::InputFlags::BRKINT
|
|
|
|
| termios::InputFlags::ICRNL
|
|
|
|
| termios::InputFlags::INPCK
|
|
|
|
| termios::InputFlags::ISTRIP
|
|
|
|
| termios::InputFlags::IXON);
|
|
|
|
|
|
|
|
raw.control_flags |= termios::ControlFlags::CS8;
|
|
|
|
|
|
|
|
raw.local_flags &= !(termios::LocalFlags::ECHO
|
|
|
|
| termios::LocalFlags::ICANON
|
|
|
|
| termios::LocalFlags::IEXTEN);
|
|
|
|
if !cbreak {
|
|
|
|
raw.local_flags &= !(termios::LocalFlags::ISIG);
|
|
|
|
}
|
|
|
|
raw.control_chars[termios::SpecialCharacterIndices::VMIN as usize] =
|
|
|
|
1;
|
|
|
|
raw.control_chars[termios::SpecialCharacterIndices::VTIME as usize] =
|
|
|
|
0;
|
|
|
|
termios::tcsetattr(raw_fd, termios::SetArg::TCSADRAIN, &raw)?;
|
|
|
|
} else {
|
|
|
|
// Try restore saved mode.
|
2022-09-04 22:33:06 -04:00
|
|
|
if let Some(mode) = meta_data.lock().tty.mode.take() {
|
2022-07-13 11:16:42 -04:00
|
|
|
termios::tcsetattr(raw_fd, termios::SetArg::TCSADRAIN, &mode)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
)
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 00:05:45 -04:00
|
|
|
#[op(fast)]
|
|
|
|
fn op_isatty(
|
|
|
|
state: &mut OpState,
|
|
|
|
rid: u32,
|
|
|
|
out: &mut [u8],
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
StdFileResource::with_file(state, rid, move |std_file| {
|
2022-05-11 12:48:38 -04:00
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
use winapi::shared::minwindef::FALSE;
|
|
|
|
use winapi::um::consoleapi;
|
|
|
|
|
|
|
|
let handle = get_windows_handle(std_file)?;
|
|
|
|
let mut test_mode: DWORD = 0;
|
|
|
|
// If I cannot get mode out of console, it is not a console.
|
2022-06-25 18:13:24 -04:00
|
|
|
// TODO(bartlomieju):
|
|
|
|
#[allow(clippy::undocumented_unsafe_blocks)]
|
2022-09-23 00:05:45 -04:00
|
|
|
{
|
|
|
|
out[0] = unsafe {
|
|
|
|
consoleapi::GetConsoleMode(handle, &mut test_mode) != FALSE
|
|
|
|
} as u8;
|
|
|
|
}
|
2022-05-11 12:48:38 -04:00
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
let raw_fd = std_file.as_raw_fd();
|
2022-06-25 18:13:24 -04:00
|
|
|
// TODO(bartlomieju):
|
|
|
|
#[allow(clippy::undocumented_unsafe_blocks)]
|
2022-09-23 00:05:45 -04:00
|
|
|
{
|
|
|
|
out[0] = unsafe { libc::isatty(raw_fd as libc::c_int) == 1 } as u8;
|
|
|
|
}
|
2021-03-18 20:55:31 -04:00
|
|
|
}
|
2022-09-23 00:05:45 -04:00
|
|
|
Ok(())
|
|
|
|
})
|
2020-07-10 10:07:12 -04:00
|
|
|
}
|
|
|
|
|
2022-09-23 00:05:45 -04:00
|
|
|
#[op(fast)]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_console_size(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2022-09-23 00:05:45 -04:00
|
|
|
result: &mut [u32],
|
|
|
|
) -> Result<(), AnyError> {
|
2022-10-25 18:23:21 -04:00
|
|
|
fn check_console_size(
|
|
|
|
state: &mut OpState,
|
|
|
|
result: &mut [u32],
|
|
|
|
rid: u32,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
StdFileResource::with_file(state, rid, move |std_file| {
|
2022-12-12 20:52:10 -05:00
|
|
|
let size = console_size(std_file)?;
|
|
|
|
result[0] = size.cols;
|
|
|
|
result[1] = size.rows;
|
|
|
|
Ok(())
|
2022-10-25 18:23:21 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut last_result = Ok(());
|
|
|
|
// Since stdio might be piped we try to get the size of the console for all
|
|
|
|
// of them and return the first one that succeeds.
|
|
|
|
for rid in [0, 1, 2] {
|
|
|
|
last_result = check_console_size(state, result, rid);
|
|
|
|
if last_result.is_ok() {
|
|
|
|
return last_result;
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2022-10-25 18:23:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
last_result
|
2020-07-10 10:07:12 -04:00
|
|
|
}
|
2022-12-12 20:52:10 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
|
|
|
pub struct ConsoleSize {
|
|
|
|
pub cols: u32,
|
|
|
|
pub rows: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn console_size(
|
|
|
|
std_file: &std::fs::File,
|
|
|
|
) -> Result<ConsoleSize, std::io::Error> {
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
use std::os::windows::io::AsRawHandle;
|
|
|
|
let handle = std_file.as_raw_handle();
|
|
|
|
|
|
|
|
// SAFETY: winapi calls
|
|
|
|
unsafe {
|
|
|
|
let mut bufinfo: winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO =
|
|
|
|
std::mem::zeroed();
|
|
|
|
|
|
|
|
if winapi::um::wincon::GetConsoleScreenBufferInfo(handle, &mut bufinfo)
|
|
|
|
== 0
|
|
|
|
{
|
|
|
|
return Err(Error::last_os_error());
|
|
|
|
}
|
|
|
|
Ok(ConsoleSize {
|
|
|
|
cols: bufinfo.dwSize.X as u32,
|
|
|
|
rows: bufinfo.dwSize.Y as u32,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
|
|
|
|
let fd = std_file.as_raw_fd();
|
|
|
|
// SAFETY: libc calls
|
|
|
|
unsafe {
|
|
|
|
let mut size: libc::winsize = std::mem::zeroed();
|
|
|
|
if libc::ioctl(fd, libc::TIOCGWINSZ, &mut size as *mut _) != 0 {
|
|
|
|
return Err(Error::last_os_error());
|
|
|
|
}
|
|
|
|
Ok(ConsoleSize {
|
|
|
|
cols: size.ws_col as u32,
|
|
|
|
rows: size.ws_row as u32,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|