]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/tty.rs
complete openbsd support for `std::env`
[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 old_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 mem;
36 use ptr;
37 use str::from_utf8;
38 use super::c::{ENABLE_ECHO_INPUT, ENABLE_EXTENDED_FLAGS};
39 use super::c::{ENABLE_INSERT_MODE, ENABLE_LINE_INPUT};
40 use super::c::{ENABLE_PROCESSED_INPUT, ENABLE_QUICK_EDIT_MODE};
41 use super::c::{CONSOLE_SCREEN_BUFFER_INFO};
42 use super::c::{ReadConsoleW, WriteConsoleW, GetConsoleMode, SetConsoleMode};
43 use super::c::{GetConsoleScreenBufferInfo};
44
45 fn invalid_encoding() -> IoError {
46     IoError {
47         kind: old_io::InvalidInput,
48         desc: "text was not valid unicode",
49         detail: None,
50     }
51 }
52
53 pub fn is_tty(fd: c_int) -> bool {
54     let mut out: DWORD = 0;
55     // If this function doesn't panic then fd is a TTY
56     match unsafe { GetConsoleMode(get_osfhandle(fd) as HANDLE,
57                                   &mut out as LPDWORD) } {
58         0 => false,
59         _ => true,
60     }
61 }
62
63 pub struct TTY {
64     closeme: bool,
65     handle: HANDLE,
66     utf8: MemReader,
67 }
68
69 impl TTY {
70     pub fn new(fd: c_int) -> IoResult<TTY> {
71         if is_tty(fd) {
72             // If the file descriptor is one of stdin, stderr, or stdout
73             // then it should not be closed by us
74             let closeme = match fd {
75                 0...2 => false,
76                 _ => true,
77             };
78             let handle = unsafe { get_osfhandle(fd) as HANDLE };
79             Ok(TTY {
80                 handle: handle,
81                 utf8: MemReader::new(Vec::new()),
82                 closeme: closeme,
83             })
84         } else {
85             Err(IoError {
86                 kind: old_io::MismatchedFileTypeForOperation,
87                 desc: "invalid handle provided to function",
88                 detail: None,
89             })
90         }
91     }
92
93     pub fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
94         // Read more if the buffer is empty
95         if self.utf8.eof() {
96             let mut utf16: Vec<u16> = repeat(0u16).take(0x1000).collect();
97             let mut num: DWORD = 0;
98             match unsafe { ReadConsoleW(self.handle,
99                                          utf16.as_mut_ptr() as LPVOID,
100                                          utf16.len() as u32,
101                                          &mut num as LPDWORD,
102                                          ptr::null_mut()) } {
103                 0 => return Err(super::last_error()),
104                 _ => (),
105             };
106             utf16.truncate(num as uint);
107             let utf8 = match String::from_utf16(utf16.as_slice()) {
108                 Ok(utf8) => utf8.into_bytes(),
109                 Err(..) => return Err(invalid_encoding()),
110             };
111             self.utf8 = MemReader::new(utf8);
112         }
113         // MemReader shouldn't error here since we just filled it
114         Ok(self.utf8.read(buf).unwrap())
115     }
116
117     pub fn write(&mut self, buf: &[u8]) -> IoResult<()> {
118         let utf16 = match from_utf8(buf).ok() {
119             Some(utf8) => {
120                 utf8.utf16_units().collect::<Vec<u16>>()
121             }
122             None => return Err(invalid_encoding()),
123         };
124         let mut num: DWORD = 0;
125         match unsafe { WriteConsoleW(self.handle,
126                                      utf16.as_ptr() as LPCVOID,
127                                      utf16.len() as u32,
128                                      &mut num as LPDWORD,
129                                      ptr::null_mut()) } {
130             0 => Err(super::last_error()),
131             _ => Ok(()),
132         }
133     }
134
135     pub fn set_raw(&mut self, raw: bool) -> IoResult<()> {
136         // FIXME
137         // Somebody needs to decide on which of these flags we want
138         match unsafe { SetConsoleMode(self.handle,
139             match raw {
140                 true => 0,
141                 false => ENABLE_ECHO_INPUT | ENABLE_EXTENDED_FLAGS |
142                          ENABLE_INSERT_MODE | ENABLE_LINE_INPUT |
143                          ENABLE_PROCESSED_INPUT | ENABLE_QUICK_EDIT_MODE,
144             }) } {
145             0 => Err(super::last_error()),
146             _ => Ok(()),
147         }
148     }
149
150     pub fn get_winsize(&mut self) -> IoResult<(int, int)> {
151         let mut info: CONSOLE_SCREEN_BUFFER_INFO = unsafe { mem::zeroed() };
152         match unsafe { GetConsoleScreenBufferInfo(self.handle, &mut info as *mut _) } {
153             0 => Err(super::last_error()),
154             _ => Ok(((info.srWindow.Right + 1 - info.srWindow.Left) as int,
155                      (info.srWindow.Bottom + 1 - info.srWindow.Top) as int)),
156         }
157     }
158 }
159
160 impl Drop for TTY {
161     fn drop(&mut self) {
162         if self.closeme {
163             // Nobody cares about the return value
164             let _ = unsafe { CloseHandle(self.handle) };
165         }
166     }
167 }