2020-08-28 11:08:24 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-04-15 20:43:19 -04:00
|
|
|
use super::io::std_file_resource;
|
2020-09-14 12:48:57 -04:00
|
|
|
use super::io::StreamResource;
|
|
|
|
use super::io::StreamResourceHolder;
|
|
|
|
use deno_core::error::bad_resource_id;
|
|
|
|
use deno_core::error::last_os_error;
|
|
|
|
use deno_core::error::resource_unavailable;
|
|
|
|
use deno_core::error::AnyError;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2020-09-14 12:48:57 -04:00
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
use deno_core::error::not_supported;
|
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;
|
|
|
|
#[cfg(windows)]
|
|
|
|
const RAW_MODE_MASK: DWORD = wincon::ENABLE_LINE_INPUT
|
|
|
|
| wincon::ENABLE_ECHO_INPUT
|
|
|
|
| wincon::ENABLE_PROCESSED_INPUT;
|
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-09-14 12:48:57 -04:00
|
|
|
return Err(last_os_error());
|
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)
|
|
|
|
}
|
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
|
|
|
super::reg_json_sync(rt, "op_set_raw", op_set_raw);
|
|
|
|
super::reg_json_sync(rt, "op_isatty", op_isatty);
|
|
|
|
super::reg_json_sync(rt, "op_console_size", op_console_size);
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SetRawArgs {
|
|
|
|
rid: u32,
|
|
|
|
mode: bool,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_set_raw(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-02-26 01:01:24 -05:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-19 19:17:35 -04:00
|
|
|
super::global_state(state).check_unstable("Deno.setRaw");
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-02-26 01:01:24 -05:00
|
|
|
let args: SetRawArgs = serde_json::from_value(args)?;
|
|
|
|
let rid = args.rid;
|
|
|
|
let is_raw = args.mode;
|
|
|
|
|
|
|
|
// 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-09-10 09:57:45 -04:00
|
|
|
let resource_holder =
|
|
|
|
state.resource_table.get_mut::<StreamResourceHolder>(rid);
|
2020-03-11 18:19:24 -04:00
|
|
|
if resource_holder.is_none() {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(bad_resource_id());
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
2020-04-16 10:29:28 -04:00
|
|
|
let resource_holder = resource_holder.unwrap();
|
2020-02-26 01:01:24 -05:00
|
|
|
|
|
|
|
// For now, only stdin.
|
2020-04-16 10:29:28 -04:00
|
|
|
let handle = match &mut resource_holder.resource {
|
2020-06-26 03:14:25 -04:00
|
|
|
StreamResource::Stdin(..) => std::io::stdin().as_raw_handle(),
|
2020-04-16 10:29:28 -04:00
|
|
|
StreamResource::FsFile(ref mut option_file_metadata) => {
|
|
|
|
if let Some((tokio_file, metadata)) = option_file_metadata.take() {
|
|
|
|
match tokio_file.try_into_std() {
|
|
|
|
Ok(std_file) => {
|
|
|
|
let raw_handle = std_file.as_raw_handle();
|
|
|
|
// Turn the std_file handle back into a tokio file, put it back
|
|
|
|
// in the resource table.
|
|
|
|
let tokio_file = tokio::fs::File::from_std(std_file);
|
|
|
|
resource_holder.resource =
|
|
|
|
StreamResource::FsFile(Some((tokio_file, metadata)));
|
|
|
|
// return the result.
|
|
|
|
raw_handle
|
|
|
|
}
|
|
|
|
Err(tokio_file) => {
|
|
|
|
// This function will return an error containing the file if
|
|
|
|
// some operation is in-flight.
|
|
|
|
resource_holder.resource =
|
|
|
|
StreamResource::FsFile(Some((tokio_file, metadata)));
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(resource_unavailable());
|
2020-04-16 10:29:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(resource_unavailable());
|
2020-04-16 10:29:28 -04:00
|
|
|
}
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(bad_resource_id());
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if handle == handleapi::INVALID_HANDLE_VALUE {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(last_os_error());
|
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
|
|
|
}
|
|
|
|
let mut original_mode: DWORD = 0;
|
2020-03-04 18:52:08 -05:00
|
|
|
if unsafe { consoleapi::GetConsoleMode(handle, &mut original_mode) }
|
|
|
|
== FALSE
|
|
|
|
{
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(last_os_error());
|
2020-03-04 18:52:08 -05:00
|
|
|
}
|
2020-02-26 01:01:24 -05:00
|
|
|
let new_mode = if is_raw {
|
|
|
|
original_mode & !RAW_MODE_MASK
|
|
|
|
} else {
|
|
|
|
original_mode | RAW_MODE_MASK
|
|
|
|
};
|
2020-03-04 18:52:08 -05:00
|
|
|
if unsafe { consoleapi::SetConsoleMode(handle, new_mode) } == FALSE {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(last_os_error());
|
2020-03-04 18:52:08 -05:00
|
|
|
}
|
2020-02-26 01:01:24 -05:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
let resource_holder =
|
|
|
|
state.resource_table.get_mut::<StreamResourceHolder>(rid);
|
2020-03-11 18:19:24 -04:00
|
|
|
if resource_holder.is_none() {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(bad_resource_id());
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if is_raw {
|
2020-03-11 18:19:24 -04:00
|
|
|
let (raw_fd, maybe_tty_mode) =
|
|
|
|
match &mut resource_holder.unwrap().resource {
|
|
|
|
StreamResource::Stdin(_, ref mut metadata) => {
|
|
|
|
(std::io::stdin().as_raw_fd(), &mut metadata.mode)
|
|
|
|
}
|
2020-04-15 20:43:19 -04:00
|
|
|
StreamResource::FsFile(Some((f, ref mut metadata))) => {
|
2020-04-16 10:29:28 -04:00
|
|
|
(f.as_raw_fd(), &mut metadata.tty.mode)
|
2020-03-11 18:19:24 -04:00
|
|
|
}
|
2020-09-14 12:48:57 -04:00
|
|
|
StreamResource::FsFile(None) => return Err(resource_unavailable()),
|
2020-03-11 18:19:24 -04:00
|
|
|
_ => {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(not_supported());
|
2020-03-11 18:19:24 -04:00
|
|
|
}
|
|
|
|
};
|
2020-02-26 01:01:24 -05:00
|
|
|
|
|
|
|
if maybe_tty_mode.is_some() {
|
|
|
|
// Already raw. Skip.
|
2020-08-28 11:08:24 -04:00
|
|
|
return Ok(json!({}));
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let original_mode = termios::tcgetattr(raw_fd)?;
|
|
|
|
let mut raw = original_mode.clone();
|
|
|
|
// Save original mode.
|
|
|
|
maybe_tty_mode.replace(original_mode);
|
|
|
|
|
|
|
|
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
|
|
|
|
| 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)?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
2020-02-26 01:01:24 -05:00
|
|
|
} else {
|
|
|
|
// Try restore saved mode.
|
2020-03-11 18:19:24 -04:00
|
|
|
let (raw_fd, maybe_tty_mode) =
|
|
|
|
match &mut resource_holder.unwrap().resource {
|
|
|
|
StreamResource::Stdin(_, ref mut metadata) => {
|
|
|
|
(std::io::stdin().as_raw_fd(), &mut metadata.mode)
|
|
|
|
}
|
2020-04-15 20:43:19 -04:00
|
|
|
StreamResource::FsFile(Some((f, ref mut metadata))) => {
|
2020-04-16 10:29:28 -04:00
|
|
|
(f.as_raw_fd(), &mut metadata.tty.mode)
|
2020-03-11 18:19:24 -04:00
|
|
|
}
|
2020-04-15 20:43:19 -04:00
|
|
|
StreamResource::FsFile(None) => {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(resource_unavailable());
|
2020-04-15 20:43:19 -04:00
|
|
|
}
|
2020-03-11 18:19:24 -04:00
|
|
|
_ => {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(bad_resource_id());
|
2020-03-11 18:19:24 -04:00
|
|
|
}
|
|
|
|
};
|
2020-02-26 01:01:24 -05:00
|
|
|
|
|
|
|
if let Some(mode) = maybe_tty_mode.take() {
|
|
|
|
termios::tcsetattr(raw_fd, termios::SetArg::TCSADRAIN, &mode)?;
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct IsattyArgs {
|
|
|
|
rid: u32,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_isatty(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-02-26 01:01:24 -05:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-02-26 01:01:24 -05:00
|
|
|
let args: IsattyArgs = serde_json::from_value(args)?;
|
|
|
|
let rid = args.rid;
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
let isatty: bool = std_file_resource(state, rid as u32, move |r| match r {
|
|
|
|
Ok(std_file) => {
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
use winapi::um::consoleapi;
|
2020-04-15 20:43:19 -04:00
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
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.
|
|
|
|
Ok(unsafe { consoleapi::GetConsoleMode(handle, &mut test_mode) != 0 })
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
2020-09-05 20:34:02 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
let raw_fd = std_file.as_raw_fd();
|
|
|
|
Ok(unsafe { libc::isatty(raw_fd as libc::c_int) == 1 })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(StreamResource::FsFile(_)) => unreachable!(),
|
|
|
|
Err(StreamResource::Stdin(..)) => Ok(atty::is(atty::Stream::Stdin)),
|
|
|
|
_ => Ok(false),
|
|
|
|
})?;
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(isatty))
|
2020-02-26 01:01:24 -05:00
|
|
|
}
|
2020-07-10 10:07:12 -04:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ConsoleSizeArgs {
|
|
|
|
rid: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct ConsoleSize {
|
|
|
|
columns: u32,
|
|
|
|
rows: u32,
|
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_console_size(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-07-10 10:07:12 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-19 19:17:35 -04:00
|
|
|
super::global_state(state).check_unstable("Deno.consoleSize");
|
2020-09-10 09:57:45 -04:00
|
|
|
|
2020-07-10 10:07:12 -04:00
|
|
|
let args: ConsoleSizeArgs = serde_json::from_value(args)?;
|
|
|
|
let rid = args.rid;
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
let size = std_file_resource(state, rid as u32, move |r| match r {
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(std_file) => {
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
use std::os::windows::io::AsRawHandle;
|
|
|
|
let handle = std_file.as_raw_handle();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut bufinfo: winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO =
|
|
|
|
std::mem::zeroed();
|
|
|
|
|
|
|
|
if winapi::um::wincon::GetConsoleScreenBufferInfo(
|
|
|
|
handle,
|
|
|
|
&mut bufinfo,
|
|
|
|
) == 0
|
|
|
|
{
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(last_os_error());
|
2020-07-10 10:07:12 -04:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(ConsoleSize {
|
|
|
|
columns: bufinfo.dwSize.X as u32,
|
|
|
|
rows: bufinfo.dwSize.Y as u32,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 10:07:12 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::io::AsRawFd;
|
2020-07-10 10:07:12 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
let fd = std_file.as_raw_fd();
|
|
|
|
unsafe {
|
|
|
|
let mut size: libc::winsize = std::mem::zeroed();
|
|
|
|
if libc::ioctl(fd, libc::TIOCGWINSZ, &mut size as *mut _) != 0 {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(last_os_error());
|
2020-07-10 10:07:12 -04:00
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
|
|
|
|
// TODO (caspervonb) return a tuple instead
|
|
|
|
Ok(ConsoleSize {
|
|
|
|
columns: size.ws_col as u32,
|
|
|
|
rows: size.ws_row as u32,
|
|
|
|
})
|
2020-07-10 10:07:12 -04:00
|
|
|
}
|
|
|
|
}
|
2020-08-28 11:08:24 -04:00
|
|
|
}
|
2020-09-14 12:48:57 -04:00
|
|
|
Err(_) => Err(bad_resource_id()),
|
2020-08-28 11:08:24 -04:00
|
|
|
})?;
|
2020-07-10 10:07:12 -04:00
|
|
|
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(size))
|
2020-07-10 10:07:12 -04:00
|
|
|
}
|