2013-04-17 19:55:21 -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.
|
|
|
|
|
2013-10-17 19:04:51 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
This modules provides bindings to the local event loop's TTY interface, using it
|
|
|
|
to have synchronous, but non-blocking versions of stdio. These handles can be
|
|
|
|
inspected for information about terminal dimensions or related information
|
|
|
|
about the stream or terminal that it is attached to.
|
|
|
|
|
|
|
|
# Example
|
|
|
|
|
|
|
|
```rust
|
|
|
|
use std::rt::io;
|
|
|
|
|
|
|
|
let mut out = io::stdout();
|
|
|
|
out.write(bytes!("Hello, world!"));
|
|
|
|
```
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-10-20 17:42:24 -05:00
|
|
|
use fmt;
|
2013-10-07 15:25:06 -05:00
|
|
|
use libc;
|
|
|
|
use option::{Option, Some, None};
|
|
|
|
use result::{Ok, Err};
|
2013-10-24 19:30:36 -05:00
|
|
|
use rt::io::buffered::{LineBufferedWriter, BufferedWriter};
|
2013-10-22 11:36:30 -05:00
|
|
|
use rt::rtio::{IoFactory, RtioTTY, RtioFileStream, with_local_io,
|
|
|
|
CloseAsynchronously};
|
|
|
|
use super::{Reader, Writer, io_error, IoError, OtherIoError};
|
|
|
|
|
|
|
|
// And so begins the tale of acquiring a uv handle to a stdio stream on all
|
|
|
|
// platforms in all situations. Our story begins by splitting the world into two
|
|
|
|
// categories, windows and unix. Then one day the creators of unix said let
|
|
|
|
// there be redirection! And henceforth there was redirection away from the
|
|
|
|
// console for standard I/O streams.
|
|
|
|
//
|
|
|
|
// After this day, the world split into four factions:
|
|
|
|
//
|
|
|
|
// 1. Unix with stdout on a terminal.
|
|
|
|
// 2. Unix with stdout redirected.
|
|
|
|
// 3. Windows with stdout on a terminal.
|
|
|
|
// 4. Windows with stdout redirected.
|
|
|
|
//
|
|
|
|
// Many years passed, and then one day the nation of libuv decided to unify this
|
|
|
|
// world. After months of toiling, uv created three ideas: TTY, Pipe, File.
|
|
|
|
// These three ideas propagated throughout the lands and the four great factions
|
|
|
|
// decided to settle among them.
|
|
|
|
//
|
|
|
|
// The groups of 1, 2, and 3 all worked very hard towards the idea of TTY. Upon
|
|
|
|
// doing so, they even enhanced themselves further then their Pipe/File
|
|
|
|
// brethren, becoming the dominant powers.
|
|
|
|
//
|
|
|
|
// The group of 4, however, decided to work independently. They abandoned the
|
|
|
|
// common TTY belief throughout, and even abandoned the fledgling Pipe belief.
|
|
|
|
// The members of the 4th faction decided to only align themselves with File.
|
|
|
|
//
|
|
|
|
// tl;dr; TTY works on everything but when windows stdout is redirected, in that
|
|
|
|
// case pipe also doesn't work, but magically file does!
|
|
|
|
enum StdSource {
|
|
|
|
TTY(~RtioTTY),
|
|
|
|
File(~RtioFileStream),
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-17 19:04:51 -05:00
|
|
|
#[fixed_stack_segment] #[inline(never)]
|
2013-10-22 11:36:30 -05:00
|
|
|
fn src<T>(fd: libc::c_int, readable: bool, f: &fn(StdSource) -> T) -> T {
|
2013-10-16 19:05:28 -05:00
|
|
|
do with_local_io |io| {
|
2013-10-22 11:36:30 -05:00
|
|
|
let fd = unsafe { libc::dup(fd) };
|
|
|
|
match io.tty_open(fd, readable) {
|
|
|
|
Ok(tty) => Some(f(TTY(tty))),
|
|
|
|
Err(_) => {
|
|
|
|
// It's not really that desirable if these handles are closed
|
|
|
|
// synchronously, and because they're squirreled away in a task
|
|
|
|
// structure the destructors will be run when the task is
|
|
|
|
// attempted to get destroyed. This means that if we run a
|
|
|
|
// synchronous destructor we'll attempt to do some scheduling
|
|
|
|
// operations which will just result in sadness.
|
|
|
|
Some(f(File(io.fs_from_raw_fd(fd, CloseAsynchronously))))
|
2013-10-16 19:05:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}.unwrap()
|
2013-10-07 15:25:06 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-17 19:04:51 -05:00
|
|
|
/// Creates a new non-blocking handle to the stdin of the current process.
|
|
|
|
///
|
|
|
|
/// See `stdout()` for notes about this function.
|
2013-10-18 16:01:22 -05:00
|
|
|
#[fixed_stack_segment] #[inline(never)]
|
2013-10-17 19:04:51 -05:00
|
|
|
pub fn stdin() -> StdReader {
|
2013-10-22 11:36:30 -05:00
|
|
|
do src(libc::STDIN_FILENO, true) |src| { StdReader { inner: src } }
|
2013-10-17 19:04:51 -05:00
|
|
|
}
|
|
|
|
|
2013-10-07 15:25:06 -05:00
|
|
|
/// Creates a new non-blocking handle to the stdout of the current process.
|
|
|
|
///
|
|
|
|
/// Note that this is a fairly expensive operation in that at least one memory
|
|
|
|
/// allocation is performed. Additionally, this must be called from a runtime
|
|
|
|
/// task context because the stream returned will be a non-blocking object using
|
|
|
|
/// the local scheduler to perform the I/O.
|
|
|
|
pub fn stdout() -> StdWriter {
|
2013-10-22 11:36:30 -05:00
|
|
|
do src(libc::STDOUT_FILENO, false) |src| { StdWriter { inner: src } }
|
2013-10-07 15:25:06 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-07 15:25:06 -05:00
|
|
|
/// Creates a new non-blocking handle to the stderr of the current process.
|
|
|
|
///
|
|
|
|
/// See `stdout()` for notes about this function.
|
|
|
|
pub fn stderr() -> StdWriter {
|
2013-10-22 11:36:30 -05:00
|
|
|
do src(libc::STDERR_FILENO, false) |src| { StdWriter { inner: src } }
|
2013-10-07 15:25:06 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-24 19:30:36 -05:00
|
|
|
/// Executes a closure with the local task's handle on stdout. By default, this
|
|
|
|
/// stream is a buffering stream, so the handled yielded to the given closure
|
|
|
|
/// can be used to flush the stdout stream (if necessary). The buffering used is
|
|
|
|
/// line-buffering when stdout is attached to a terminal, and a fixed sized
|
|
|
|
/// buffer if it is not attached to a terminal.
|
|
|
|
///
|
|
|
|
/// Note that handles generated via the `stdout()` function all output to the
|
|
|
|
/// same stream, and output among all task may be interleaved as a result of
|
|
|
|
/// this. This is provided to have access to the default stream for `print` and
|
|
|
|
/// `println` (and the related macros) for this task.
|
|
|
|
///
|
|
|
|
/// Also note that logging macros do not use this stream. Using the logging
|
|
|
|
/// macros will emit output to stderr.
|
|
|
|
pub fn with_task_stdout(f: &fn(&mut Writer)) {
|
|
|
|
use rt::local::Local;
|
|
|
|
use rt::task::Task;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
// Logging may require scheduling operations, so we can't remove the
|
|
|
|
// task from TLS right now, hence the unsafe borrow. Sad.
|
|
|
|
let task: *mut Task = Local::unsafe_borrow();
|
|
|
|
|
|
|
|
match (*task).stdout_handle {
|
|
|
|
Some(ref mut handle) => f(*handle),
|
|
|
|
None => {
|
|
|
|
let handle = stdout();
|
|
|
|
let mut handle = if handle.isatty() {
|
|
|
|
~LineBufferedWriter::new(handle) as ~Writer
|
|
|
|
} else {
|
|
|
|
// The default capacity is very large, 64k, but this is just
|
|
|
|
// a stdout stream, and possibly per task, so let's not make
|
|
|
|
// this too expensive.
|
|
|
|
~BufferedWriter::with_capacity(4096, handle) as ~Writer
|
|
|
|
};
|
|
|
|
f(handle);
|
|
|
|
(*task).stdout_handle = Some(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-07 15:25:06 -05:00
|
|
|
/// Prints a string to the stdout of the current process. No newline is emitted
|
|
|
|
/// after the string is printed.
|
|
|
|
pub fn print(s: &str) {
|
2013-10-24 19:30:36 -05:00
|
|
|
do with_task_stdout |io| {
|
|
|
|
io.write(s.as_bytes());
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-10-07 15:25:06 -05:00
|
|
|
/// Prints a string as a line. to the stdout of the current process. A literal
|
|
|
|
/// `\n` character is printed to the console after the string.
|
|
|
|
pub fn println(s: &str) {
|
2013-10-24 19:30:36 -05:00
|
|
|
do with_task_stdout |io| {
|
|
|
|
io.write(s.as_bytes());
|
|
|
|
io.write(['\n' as u8]);
|
|
|
|
}
|
2013-10-07 15:25:06 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-20 17:42:24 -05:00
|
|
|
/// Similar to `print`, but takes a `fmt::Arguments` structure to be compatible
|
|
|
|
/// with the `format_args!` macro.
|
|
|
|
pub fn print_args(fmt: &fmt::Arguments) {
|
2013-10-24 19:30:36 -05:00
|
|
|
do with_task_stdout |io| {
|
|
|
|
fmt::write(io, fmt);
|
|
|
|
}
|
2013-10-20 17:42:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Similar to `println`, but takes a `fmt::Arguments` structure to be
|
|
|
|
/// compatible with the `format_args!` macro.
|
|
|
|
pub fn println_args(fmt: &fmt::Arguments) {
|
2013-10-24 19:30:36 -05:00
|
|
|
do with_task_stdout |io| {
|
|
|
|
fmt::writeln(io, fmt);
|
|
|
|
}
|
2013-10-20 17:42:24 -05:00
|
|
|
}
|
|
|
|
|
2013-10-07 15:25:06 -05:00
|
|
|
/// Representation of a reader of a standard input stream
|
|
|
|
pub struct StdReader {
|
2013-10-22 11:36:30 -05:00
|
|
|
priv inner: StdSource
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for StdReader {
|
2013-10-07 15:25:06 -05:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
2013-10-22 11:36:30 -05:00
|
|
|
let ret = match self.inner {
|
|
|
|
TTY(ref mut tty) => tty.read(buf),
|
|
|
|
File(ref mut file) => file.read(buf).map_move(|i| i as uint),
|
|
|
|
};
|
|
|
|
match ret {
|
2013-10-07 15:25:06 -05:00
|
|
|
Ok(amt) => Some(amt as uint),
|
|
|
|
Err(e) => {
|
|
|
|
io_error::cond.raise(e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-10-07 15:25:06 -05:00
|
|
|
fn eof(&mut self) -> bool { false }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-10-07 15:25:06 -05:00
|
|
|
/// Representation of a writer to a standard output stream
|
|
|
|
pub struct StdWriter {
|
2013-10-22 11:36:30 -05:00
|
|
|
priv inner: StdSource
|
2013-10-16 13:47:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl StdWriter {
|
|
|
|
/// Gets the size of this output window, if possible. This is typically used
|
|
|
|
/// when the writer is attached to something like a terminal, this is used
|
|
|
|
/// to fetch the dimensions of the terminal.
|
|
|
|
///
|
|
|
|
/// If successful, returns Some((width, height)).
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// This function will raise on the `io_error` condition if an error
|
|
|
|
/// happens.
|
|
|
|
pub fn winsize(&mut self) -> Option<(int, int)> {
|
2013-10-22 11:36:30 -05:00
|
|
|
match self.inner {
|
|
|
|
TTY(ref mut tty) => {
|
|
|
|
match tty.get_winsize() {
|
|
|
|
Ok(p) => Some(p),
|
|
|
|
Err(e) => {
|
|
|
|
io_error::cond.raise(e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
File(*) => {
|
|
|
|
io_error::cond.raise(IoError {
|
|
|
|
kind: OtherIoError,
|
|
|
|
desc: "stream is not a tty",
|
|
|
|
detail: None,
|
|
|
|
});
|
2013-10-16 13:47:12 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Controls whether this output stream is a "raw stream" or simply a normal
|
|
|
|
/// stream.
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// This function will raise on the `io_error` condition if an error
|
|
|
|
/// happens.
|
|
|
|
pub fn set_raw(&mut self, raw: bool) {
|
2013-10-22 11:36:30 -05:00
|
|
|
match self.inner {
|
|
|
|
TTY(ref mut tty) => {
|
|
|
|
match tty.set_raw(raw) {
|
|
|
|
Ok(()) => {},
|
|
|
|
Err(e) => io_error::cond.raise(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
File(*) => {
|
|
|
|
io_error::cond.raise(IoError {
|
|
|
|
kind: OtherIoError,
|
|
|
|
desc: "stream is not a tty",
|
|
|
|
detail: None,
|
|
|
|
});
|
|
|
|
}
|
2013-10-16 13:47:12 -05:00
|
|
|
}
|
|
|
|
}
|
2013-10-17 19:04:51 -05:00
|
|
|
|
|
|
|
/// Returns whether this tream is attached to a TTY instance or not.
|
|
|
|
///
|
|
|
|
/// This is similar to libc's isatty() function
|
2013-10-22 11:36:30 -05:00
|
|
|
pub fn isatty(&self) -> bool {
|
|
|
|
match self.inner {
|
|
|
|
TTY(ref tty) => tty.isatty(),
|
|
|
|
File(*) => false,
|
|
|
|
}
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for StdWriter {
|
2013-10-07 15:25:06 -05:00
|
|
|
fn write(&mut self, buf: &[u8]) {
|
2013-10-22 11:36:30 -05:00
|
|
|
let ret = match self.inner {
|
|
|
|
TTY(ref mut tty) => tty.write(buf),
|
|
|
|
File(ref mut file) => file.write(buf),
|
|
|
|
};
|
|
|
|
match ret {
|
2013-10-07 15:25:06 -05:00
|
|
|
Ok(()) => {}
|
|
|
|
Err(e) => io_error::cond.raise(e)
|
|
|
|
}
|
|
|
|
}
|
2013-10-16 13:47:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoke() {
|
|
|
|
// Just make sure we can acquire handles
|
|
|
|
stdin();
|
|
|
|
stdout();
|
|
|
|
stderr();
|
2013-10-07 15:25:06 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|