1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-lexer-test FIXME #15877
13 //! Windows specific console TTY implementation
15 //! This module contains the implementation of a Windows specific console TTY.
16 //! Also converts between UTF-16 and UTF-8. Windows has very poor support for
17 //! UTF-8 and some functions will fail. In particular ReadFile and ReadConsole
18 //! will fail when the codepage is set to UTF-8 and a Unicode character is
22 //! This implementation does not account for codepoints that are split across
23 //! multiple reads and writes. Also, this implementation does not expose a way
24 //! to read/write UTF-16 directly. When/if Rust receives a Reader/Writer
25 //! wrapper that performs encoding/decoding, this implementation should switch
26 //! to working in raw UTF-16, with such a wrapper around it.
28 use super::c::{ReadConsoleW, WriteConsoleW, GetConsoleMode, SetConsoleMode};
29 use super::c::{ERROR_ILLEGAL_CHARACTER};
30 use super::c::{ENABLE_ECHO_INPUT, ENABLE_EXTENDED_FLAGS};
31 use super::c::{ENABLE_INSERT_MODE, ENABLE_LINE_INPUT};
32 use super::c::{ENABLE_PROCESSED_INPUT, ENABLE_QUICK_EDIT_MODE};
33 use libc::{c_int, HANDLE, LPDWORD, DWORD, LPVOID};
34 use libc::{get_osfhandle, CloseHandle};
35 use libc::types::os::arch::extra::LPCVOID;
36 use std::io::MemReader;
38 use std::rt::rtio::{IoResult, IoError, RtioTTY};
39 use std::str::{from_utf16, from_utf8};
41 fn invalid_encoding() -> IoError {
43 code: ERROR_ILLEGAL_CHARACTER as uint,
45 detail: Some("text was not valid unicode".to_string()),
49 pub fn is_tty(fd: c_int) -> bool {
50 let mut out: DWORD = 0;
51 // If this function doesn't fail then fd is a TTY
52 match unsafe { GetConsoleMode(get_osfhandle(fd) as HANDLE,
53 &mut out as LPDWORD) } {
59 pub struct WindowsTTY {
66 pub fn new(fd: c_int) -> WindowsTTY {
67 // If the file descriptor is one of stdin, stderr, or stdout
68 // then it should not be closed by us
69 let closeme = match fd {
73 let handle = unsafe { get_osfhandle(fd) as HANDLE };
76 utf8: MemReader::new(Vec::new()),
82 impl Drop for WindowsTTY {
85 // Nobody cares about the return value
86 let _ = unsafe { CloseHandle(self.handle) };
91 impl RtioTTY for WindowsTTY {
92 fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
93 // Read more if the buffer is empty
95 let mut utf16 = Vec::from_elem(0x1000, 0u16);
96 let mut num: DWORD = 0;
97 match unsafe { ReadConsoleW(self.handle,
98 utf16.as_mut_ptr() as LPVOID,
102 0 => return Err(super::last_error()),
105 utf16.truncate(num as uint);
106 let utf8 = match from_utf16(utf16.as_slice()) {
107 Some(utf8) => utf8.into_bytes(),
108 None => return Err(invalid_encoding()),
110 self.utf8 = MemReader::new(utf8);
112 // MemReader shouldn't error here since we just filled it
113 Ok(self.utf8.read(buf).unwrap())
116 fn write(&mut self, buf: &[u8]) -> IoResult<()> {
117 let utf16 = match from_utf8(buf) {
118 Some(utf8) => utf8.to_utf16(),
119 None => return Err(invalid_encoding()),
121 let mut num: DWORD = 0;
122 match unsafe { WriteConsoleW(self.handle,
123 utf16.as_ptr() as LPCVOID,
127 0 => Err(super::last_error()),
132 fn set_raw(&mut self, raw: bool) -> IoResult<()> {
134 // Somebody needs to decide on which of these flags we want
135 match unsafe { SetConsoleMode(self.handle,
138 false => ENABLE_ECHO_INPUT | ENABLE_EXTENDED_FLAGS |
139 ENABLE_INSERT_MODE | ENABLE_LINE_INPUT |
140 ENABLE_PROCESSED_INPUT | ENABLE_QUICK_EDIT_MODE,
142 0 => Err(super::last_error()),
147 fn get_winsize(&mut self) -> IoResult<(int, int)> {
149 // Get console buffer via CreateFile with CONOUT$
150 // Make a CONSOLE_SCREEN_BUFFER_INFO
151 // Call GetConsoleScreenBufferInfo
152 // Maybe call GetLargestConsoleWindowSize instead?
156 // Let us magically declare this as a TTY
157 fn isatty(&self) -> bool { true }