]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/tty.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / libstd / sys / windows / tty.rs
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.
4 //
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.
10
11 // ignore-lexer-test FIXME #15877
12
13 //! Windows specific console TTY implementation
14 //!
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 panic. In particular ReadFile and ReadConsole
18 //! will panic when the codepage is set to UTF-8 and a Unicode character is
19 //! entered.
20 //!
21 //! FIXME
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.
27
28 use prelude::v1::*;
29
30 use io::{self, IoError, IoResult, MemReader};
31 use iter::repeat;
32 use libc::types::os::arch::extra::LPCVOID;
33 use libc::{c_int, HANDLE, LPDWORD, DWORD, LPVOID};
34 use libc::{get_osfhandle, CloseHandle};
35 use ptr;
36 use str::from_utf8;
37 use super::c::{ENABLE_ECHO_INPUT, ENABLE_EXTENDED_FLAGS};
38 use super::c::{ENABLE_INSERT_MODE, ENABLE_LINE_INPUT};
39 use super::c::{ENABLE_PROCESSED_INPUT, ENABLE_QUICK_EDIT_MODE};
40 use super::c::{ERROR_ILLEGAL_CHARACTER};
41 use super::c::{ReadConsoleW, WriteConsoleW, GetConsoleMode, SetConsoleMode};
42
43 fn invalid_encoding() -> IoError {
44     IoError {
45         kind: io::InvalidInput,
46         desc: "text was not valid unicode",
47         detail: None,
48     }
49 }
50
51 pub fn is_tty(fd: c_int) -> bool {
52     let mut out: DWORD = 0;
53     // If this function doesn't panic then fd is a TTY
54     match unsafe { GetConsoleMode(get_osfhandle(fd) as HANDLE,
55                                   &mut out as LPDWORD) } {
56         0 => false,
57         _ => true,
58     }
59 }
60
61 pub struct TTY {
62     closeme: bool,
63     handle: HANDLE,
64     utf8: MemReader,
65 }
66
67 impl TTY {
68     pub fn new(fd: c_int) -> IoResult<TTY> {
69         if is_tty(fd) {
70             // If the file descriptor is one of stdin, stderr, or stdout
71             // then it should not be closed by us
72             let closeme = match fd {
73                 0...2 => false,
74                 _ => true,
75             };
76             let handle = unsafe { get_osfhandle(fd) as HANDLE };
77             Ok(TTY {
78                 handle: handle,
79                 utf8: MemReader::new(Vec::new()),
80                 closeme: closeme,
81             })
82         } else {
83             Err(IoError {
84                 kind: io::MismatchedFileTypeForOperation,
85                 desc: "invalid handle provided to function",
86                 detail: None,
87             })
88         }
89     }
90
91     pub fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
92         // Read more if the buffer is empty
93         if self.utf8.eof() {
94             let mut utf16: Vec<u16> = repeat(0u16).take(0x1000).collect();
95             let mut num: DWORD = 0;
96             match unsafe { ReadConsoleW(self.handle,
97                                          utf16.as_mut_ptr() as LPVOID,
98                                          utf16.len() as u32,
99                                          &mut num as LPDWORD,
100                                          ptr::null_mut()) } {
101                 0 => return Err(super::last_error()),
102                 _ => (),
103             };
104             utf16.truncate(num as uint);
105             let utf8 = match String::from_utf16(utf16.as_slice()) {
106                 Ok(utf8) => utf8.into_bytes(),
107                 Err(..) => return Err(invalid_encoding()),
108             };
109             self.utf8 = MemReader::new(utf8);
110         }
111         // MemReader shouldn't error here since we just filled it
112         Ok(self.utf8.read(buf).unwrap())
113     }
114
115     pub fn write(&mut self, buf: &[u8]) -> IoResult<()> {
116         let utf16 = match from_utf8(buf).ok() {
117             Some(utf8) => {
118                 utf8.utf16_units().collect::<Vec<u16>>()
119             }
120             None => return Err(invalid_encoding()),
121         };
122         let mut num: DWORD = 0;
123         match unsafe { WriteConsoleW(self.handle,
124                                      utf16.as_ptr() as LPCVOID,
125                                      utf16.len() as u32,
126                                      &mut num as LPDWORD,
127                                      ptr::null_mut()) } {
128             0 => Err(super::last_error()),
129             _ => Ok(()),
130         }
131     }
132
133     pub fn set_raw(&mut self, raw: bool) -> IoResult<()> {
134         // FIXME
135         // Somebody needs to decide on which of these flags we want
136         match unsafe { SetConsoleMode(self.handle,
137             match raw {
138                 true => 0,
139                 false => ENABLE_ECHO_INPUT | ENABLE_EXTENDED_FLAGS |
140                          ENABLE_INSERT_MODE | ENABLE_LINE_INPUT |
141                          ENABLE_PROCESSED_INPUT | ENABLE_QUICK_EDIT_MODE,
142             }) } {
143             0 => Err(super::last_error()),
144             _ => Ok(()),
145         }
146     }
147
148     pub fn get_winsize(&mut self) -> IoResult<(int, int)> {
149         // FIXME
150         // Get console buffer via CreateFile with CONOUT$
151         // Make a CONSOLE_SCREEN_BUFFER_INFO
152         // Call GetConsoleScreenBufferInfo
153         // Maybe call GetLargestConsoleWindowSize instead?
154         Err(super::unimpl())
155     }
156
157     // Let us magically declare this as a TTY
158     pub fn isatty(&self) -> bool { true }
159 }
160
161 impl Drop for TTY {
162     fn drop(&mut self) {
163         if self.closeme {
164             // Nobody cares about the return value
165             let _ = unsafe { CloseHandle(self.handle) };
166         }
167     }
168 }