@@ -33,16 +33,16 @@ use super::c::{ENABLE_PROCESSED_INPUT, ENABLE_QUICK_EDIT_MODE};
3333use libc:: { c_int, HANDLE , LPDWORD , DWORD , LPVOID } ;
3434use libc:: { get_osfhandle, CloseHandle } ;
3535use libc:: types:: os:: arch:: extra:: LPCVOID ;
36- use std :: io:: MemReader ;
37- use std :: ptr ;
38- use std :: rt :: rtio :: { IoResult , IoError , RtioTTY } ;
39- use std :: str:: from_utf8;
36+ use io:: { mod , IoError , IoResult , MemReader } ;
37+ use prelude :: * ;
38+ use ptr ;
39+ use str:: from_utf8;
4040
4141fn invalid_encoding ( ) -> IoError {
4242 IoError {
43- code : ERROR_ILLEGAL_CHARACTER as uint ,
44- extra : 0 ,
45- detail : Some ( "text was not valid unicode" . to_string ( ) ) ,
43+ kind : io :: InvalidInput ,
44+ desc : "text was not valid unicode" ,
45+ detail : None ,
4646 }
4747}
4848
@@ -56,40 +56,37 @@ pub fn is_tty(fd: c_int) -> bool {
5656 }
5757}
5858
59- pub struct WindowsTTY {
59+ pub struct TTY {
6060 closeme : bool ,
6161 handle : HANDLE ,
6262 utf8 : MemReader ,
6363}
6464
65- impl 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 {
70- 0 ...2 => false ,
71- _ => true ,
72- } ;
73- let handle = unsafe { get_osfhandle ( fd) as HANDLE } ;
74- WindowsTTY {
75- handle : handle,
76- utf8 : MemReader :: new ( Vec :: new ( ) ) ,
77- closeme : closeme,
65+ impl TTY {
66+ pub fn new ( fd : c_int ) -> IoResult < TTY > {
67+ if is_tty ( fd) {
68+ // If the file descriptor is one of stdin, stderr, or stdout
69+ // then it should not be closed by us
70+ let closeme = match fd {
71+ 0 ...2 => false ,
72+ _ => true ,
73+ } ;
74+ let handle = unsafe { get_osfhandle ( fd) as HANDLE } ;
75+ Ok ( TTY {
76+ handle : handle,
77+ utf8 : MemReader :: new ( Vec :: new ( ) ) ,
78+ closeme : closeme,
79+ } )
80+ } else {
81+ Err ( IoError {
82+ kind : io:: MismatchedFileTypeForOperation ,
83+ desc : "invalid handle provided to function" ,
84+ detail : None ,
85+ } )
7886 }
7987 }
80- }
8188
82- impl Drop for WindowsTTY {
83- fn drop ( & mut self ) {
84- if self . closeme {
85- // Nobody cares about the return value
86- let _ = unsafe { CloseHandle ( self . handle ) } ;
87- }
88- }
89- }
90-
91- impl RtioTTY for WindowsTTY {
92- fn read ( & mut self , buf : & mut [ u8 ] ) -> IoResult < uint > {
89+ pub fn read ( & mut self , buf : & mut [ u8 ] ) -> IoResult < uint > {
9390 // Read more if the buffer is empty
9491 if self . utf8 . eof ( ) {
9592 let mut utf16 = Vec :: from_elem ( 0x1000 , 0u16 ) ;
@@ -113,7 +110,7 @@ impl RtioTTY for WindowsTTY {
113110 Ok ( self . utf8 . read ( buf) . unwrap ( ) )
114111 }
115112
116- fn write ( & mut self , buf : & [ u8 ] ) -> IoResult < ( ) > {
113+ pub fn write ( & mut self , buf : & [ u8 ] ) -> IoResult < ( ) > {
117114 let utf16 = match from_utf8 ( buf) {
118115 Some ( utf8) => {
119116 utf8. as_slice ( ) . utf16_units ( ) . collect :: < Vec < u16 > > ( )
@@ -131,7 +128,7 @@ impl RtioTTY for WindowsTTY {
131128 }
132129 }
133130
134- fn set_raw ( & mut self , raw : bool ) -> IoResult < ( ) > {
131+ pub fn set_raw ( & mut self , raw : bool ) -> IoResult < ( ) > {
135132 // FIXME
136133 // Somebody needs to decide on which of these flags we want
137134 match unsafe { SetConsoleMode ( self . handle ,
@@ -146,7 +143,7 @@ impl RtioTTY for WindowsTTY {
146143 }
147144 }
148145
149- fn get_winsize ( & mut self ) -> IoResult < ( int , int ) > {
146+ pub fn get_winsize ( & mut self ) -> IoResult < ( int , int ) > {
150147 // FIXME
151148 // Get console buffer via CreateFile with CONOUT$
152149 // Make a CONSOLE_SCREEN_BUFFER_INFO
@@ -156,5 +153,14 @@ impl RtioTTY for WindowsTTY {
156153 }
157154
158155 // Let us magically declare this as a TTY
159- fn isatty ( & self ) -> bool { true }
156+ pub fn isatty ( & self ) -> bool { true }
157+ }
158+
159+ impl Drop for TTY {
160+ fn drop ( & mut self ) {
161+ if self . closeme {
162+ // Nobody cares about the return value
163+ let _ = unsafe { CloseHandle ( self . handle ) } ;
164+ }
165+ }
160166}
0 commit comments