2013-10-22 17:13:18 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-02-26 11:58:41 -06:00
|
|
|
use libc;
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io::IoError;
|
2014-03-10 23:27:34 -05:00
|
|
|
use std::ptr;
|
2013-11-04 18:42:05 -06:00
|
|
|
use std::rt::rtio::RtioTTY;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-12-12 19:47:48 -06:00
|
|
|
use homing::{HomingIO, HomeHandle};
|
2013-11-04 18:42:05 -06:00
|
|
|
use stream::StreamWatcher;
|
2013-12-12 19:47:48 -06:00
|
|
|
use super::{UvError, UvHandle, uv_error_to_io_error};
|
|
|
|
use uvio::UvIoFactory;
|
2013-10-22 17:13:18 -05:00
|
|
|
use uvll;
|
|
|
|
|
2013-11-04 18:42:05 -06:00
|
|
|
pub struct TtyWatcher{
|
|
|
|
tty: *uvll::uv_tty_t,
|
|
|
|
stream: StreamWatcher,
|
2013-12-12 19:47:48 -06:00
|
|
|
home: HomeHandle,
|
2013-11-04 18:42:05 -06:00
|
|
|
fd: libc::c_int,
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-04 18:42:05 -06:00
|
|
|
impl TtyWatcher {
|
2013-12-12 19:47:48 -06:00
|
|
|
pub fn new(io: &mut UvIoFactory, fd: libc::c_int, readable: bool)
|
2013-11-04 18:42:05 -06:00
|
|
|
-> Result<TtyWatcher, UvError>
|
2013-10-22 17:13:18 -05:00
|
|
|
{
|
2013-11-07 17:24:30 -06:00
|
|
|
// libuv may succeed in giving us a handle (via uv_tty_init), but if the
|
|
|
|
// handle isn't actually connected to a terminal there are frequently
|
|
|
|
// many problems in using it with libuv. To get around this, always
|
|
|
|
// return a failure if the specified file descriptor isn't actually a
|
|
|
|
// TTY.
|
|
|
|
//
|
|
|
|
// Related:
|
|
|
|
// - https://github.com/joyent/libuv/issues/982
|
|
|
|
// - https://github.com/joyent/libuv/issues/988
|
2013-11-18 18:26:03 -06:00
|
|
|
let guess = unsafe { uvll::guess_handle(fd) };
|
2014-04-18 21:09:31 -05:00
|
|
|
if guess != uvll::UV_TTY as libc::c_int {
|
2013-11-07 17:24:30 -06:00
|
|
|
return Err(UvError(uvll::EBADF));
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-12-04 10:51:47 -06:00
|
|
|
// libuv was recently changed to not close the stdio file descriptors,
|
|
|
|
// but it did not change the behavior for windows. Until this issue is
|
|
|
|
// fixed, we need to dup the stdio file descriptors because otherwise
|
|
|
|
// uv_close will close them
|
|
|
|
let fd = if cfg!(windows) && fd <= libc::STDERR_FILENO {
|
|
|
|
unsafe { libc::dup(fd) }
|
|
|
|
} else { fd };
|
|
|
|
|
2013-11-07 17:24:30 -06:00
|
|
|
// If this file descriptor is indeed guessed to be a tty, then go ahead
|
|
|
|
// with attempting to open it as a tty.
|
|
|
|
let handle = UvHandle::alloc(None::<TtyWatcher>, uvll::UV_TTY);
|
2014-03-10 23:27:34 -05:00
|
|
|
let mut watcher = TtyWatcher {
|
|
|
|
tty: handle,
|
|
|
|
stream: StreamWatcher::new(handle),
|
|
|
|
home: io.make_handle(),
|
|
|
|
fd: fd,
|
|
|
|
};
|
2013-11-04 18:42:05 -06:00
|
|
|
match unsafe {
|
2013-12-12 19:47:48 -06:00
|
|
|
uvll::uv_tty_init(io.uv_loop(), handle, fd as libc::c_int,
|
2013-11-03 12:39:39 -06:00
|
|
|
readable as libc::c_int)
|
2013-11-04 18:42:05 -06:00
|
|
|
} {
|
2014-03-10 23:27:34 -05:00
|
|
|
0 => Ok(watcher),
|
2013-10-22 17:13:18 -05:00
|
|
|
n => {
|
2014-03-10 23:27:34 -05:00
|
|
|
// On windows, libuv returns errors before initializing the
|
|
|
|
// handle, so our only cleanup is to free the handle itself
|
|
|
|
if cfg!(windows) {
|
|
|
|
unsafe { uvll::free_handle(handle); }
|
|
|
|
watcher.tty = ptr::null();
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
Err(UvError(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-04 18:42:05 -06:00
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-04 18:42:05 -06:00
|
|
|
impl RtioTTY for TtyWatcher {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<uint, IoError> {
|
2013-11-06 01:29:11 -06:00
|
|
|
let _m = self.fire_homing_missile();
|
2013-11-04 18:42:05 -06:00
|
|
|
self.stream.read(buf).map_err(uv_error_to_io_error)
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2013-11-04 18:42:05 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<(), IoError> {
|
2013-11-06 01:29:11 -06:00
|
|
|
let _m = self.fire_homing_missile();
|
2014-04-27 17:45:16 -05:00
|
|
|
self.stream.write(buf, false).map_err(uv_error_to_io_error)
|
2013-11-04 18:42:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_raw(&mut self, raw: bool) -> Result<(), IoError> {
|
2013-10-22 17:13:18 -05:00
|
|
|
let raw = raw as libc::c_int;
|
2013-11-06 01:29:11 -06:00
|
|
|
let _m = self.fire_homing_missile();
|
2013-11-04 18:42:05 -06:00
|
|
|
match unsafe { uvll::uv_tty_set_mode(self.tty, raw) } {
|
2013-10-22 17:13:18 -05:00
|
|
|
0 => Ok(()),
|
2013-11-04 18:42:05 -06:00
|
|
|
n => Err(uv_error_to_io_error(UvError(n)))
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 18:42:05 -06:00
|
|
|
#[allow(unused_mut)]
|
|
|
|
fn get_winsize(&mut self) -> Result<(int, int), IoError> {
|
2013-10-22 17:13:18 -05:00
|
|
|
let mut width: libc::c_int = 0;
|
|
|
|
let mut height: libc::c_int = 0;
|
|
|
|
let widthptr: *libc::c_int = &width;
|
|
|
|
let heightptr: *libc::c_int = &width;
|
|
|
|
|
2013-11-06 01:29:11 -06:00
|
|
|
let _m = self.fire_homing_missile();
|
2013-11-04 18:42:05 -06:00
|
|
|
match unsafe { uvll::uv_tty_get_winsize(self.tty,
|
2013-11-03 12:39:39 -06:00
|
|
|
widthptr, heightptr) } {
|
2013-10-22 17:13:18 -05:00
|
|
|
0 => Ok((width as int, height as int)),
|
2013-11-04 18:42:05 -06:00
|
|
|
n => Err(uv_error_to_io_error(UvError(n)))
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-18 18:26:03 -06:00
|
|
|
|
|
|
|
fn isatty(&self) -> bool {
|
|
|
|
unsafe { uvll::guess_handle(self.fd) == uvll::UV_TTY as libc::c_int }
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2013-11-04 18:42:05 -06:00
|
|
|
impl UvHandle<uvll::uv_tty_t> for TtyWatcher {
|
|
|
|
fn uv_handle(&self) -> *uvll::uv_tty_t { self.tty }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HomingIO for TtyWatcher {
|
2013-12-12 19:47:48 -06:00
|
|
|
fn home<'a>(&'a mut self) -> &'a mut HomeHandle { &mut self.home }
|
2013-11-04 18:42:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TtyWatcher {
|
|
|
|
fn drop(&mut self) {
|
2014-03-10 23:27:34 -05:00
|
|
|
if !self.tty.is_null() {
|
|
|
|
let _m = self.fire_homing_missile();
|
|
|
|
self.close_async_();
|
|
|
|
}
|
2013-11-04 18:42:05 -06:00
|
|
|
}
|
|
|
|
}
|