]> git.lizzy.rs Git - rust.git/blob - src/libnative/io/c_windows.rs
3bd850b5aac7fcc2daeb8d2c8cd01dfcf90ce3f0
[rust.git] / src / libnative / io / c_windows.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 //! C definitions used by libnative that don't belong in liblibc
12
13 #![allow(type_overflow)]
14
15 use libc;
16
17 pub static WSADESCRIPTION_LEN: uint = 256;
18 pub static WSASYS_STATUS_LEN: uint = 128;
19 pub static FIONBIO: libc::c_long = 0x8004667e;
20 static FD_SETSIZE: uint = 64;
21 pub static MSG_DONTWAIT: libc::c_int = 0;
22 pub static ERROR_ILLEGAL_CHARACTER: libc::c_int = 582;
23 pub static ENABLE_ECHO_INPUT: libc::DWORD = 0x4;
24 pub static ENABLE_EXTENDED_FLAGS: libc::DWORD = 0x80;
25 pub static ENABLE_INSERT_MODE: libc::DWORD = 0x20;
26 pub static ENABLE_LINE_INPUT: libc::DWORD = 0x2;
27 pub static ENABLE_PROCESSED_INPUT: libc::DWORD = 0x1;
28 pub static ENABLE_QUICK_EDIT_MODE: libc::DWORD = 0x40;
29 pub static WSA_INVALID_EVENT: WSAEVENT = 0 as WSAEVENT;
30
31 pub static FD_ACCEPT: libc::c_long = 0x08;
32 pub static FD_MAX_EVENTS: uint = 10;
33 pub static WSA_INFINITE: libc::DWORD = libc::INFINITE;
34 pub static WSA_WAIT_TIMEOUT: libc::DWORD = libc::consts::os::extra::WAIT_TIMEOUT;
35 pub static WSA_WAIT_EVENT_0: libc::DWORD = libc::consts::os::extra::WAIT_OBJECT_0;
36 pub static WSA_WAIT_FAILED: libc::DWORD = libc::consts::os::extra::WAIT_FAILED;
37
38 #[repr(C)]
39 #[cfg(target_arch = "x86")]
40 pub struct WSADATA {
41     pub wVersion: libc::WORD,
42     pub wHighVersion: libc::WORD,
43     pub szDescription: [u8, ..WSADESCRIPTION_LEN + 1],
44     pub szSystemStatus: [u8, ..WSASYS_STATUS_LEN + 1],
45     pub iMaxSockets: u16,
46     pub iMaxUdpDg: u16,
47     pub lpVendorInfo: *mut u8,
48 }
49 #[repr(C)]
50 #[cfg(target_arch = "x86_64")]
51 pub struct WSADATA {
52     pub wVersion: libc::WORD,
53     pub wHighVersion: libc::WORD,
54     pub iMaxSockets: u16,
55     pub iMaxUdpDg: u16,
56     pub lpVendorInfo: *mut u8,
57     pub szDescription: [u8, ..WSADESCRIPTION_LEN + 1],
58     pub szSystemStatus: [u8, ..WSASYS_STATUS_LEN + 1],
59 }
60
61 pub type LPWSADATA = *mut WSADATA;
62
63 #[repr(C)]
64 pub struct WSANETWORKEVENTS {
65     pub lNetworkEvents: libc::c_long,
66     pub iErrorCode: [libc::c_int, ..FD_MAX_EVENTS],
67 }
68
69 pub type LPWSANETWORKEVENTS = *mut WSANETWORKEVENTS;
70
71 pub type WSAEVENT = libc::HANDLE;
72
73 #[repr(C)]
74 pub struct fd_set {
75     fd_count: libc::c_uint,
76     fd_array: [libc::SOCKET, ..FD_SETSIZE],
77 }
78
79 pub fn fd_set(set: &mut fd_set, s: libc::SOCKET) {
80     set.fd_array[set.fd_count as uint] = s;
81     set.fd_count += 1;
82 }
83
84 #[link(name = "ws2_32")]
85 extern "system" {
86     pub fn WSAStartup(wVersionRequested: libc::WORD,
87                       lpWSAData: LPWSADATA) -> libc::c_int;
88     pub fn WSAGetLastError() -> libc::c_int;
89     pub fn WSACloseEvent(hEvent: WSAEVENT) -> libc::BOOL;
90     pub fn WSACreateEvent() -> WSAEVENT;
91     pub fn WSAEventSelect(s: libc::SOCKET,
92                           hEventObject: WSAEVENT,
93                           lNetworkEvents: libc::c_long) -> libc::c_int;
94     pub fn WSASetEvent(hEvent: WSAEVENT) -> libc::BOOL;
95     pub fn WSAWaitForMultipleEvents(cEvents: libc::DWORD,
96                                     lphEvents: *const WSAEVENT,
97                                     fWaitAll: libc::BOOL,
98                                     dwTimeout: libc::DWORD,
99                                     fAltertable: libc::BOOL) -> libc::DWORD;
100     pub fn WSAEnumNetworkEvents(s: libc::SOCKET,
101                                 hEventObject: WSAEVENT,
102                                 lpNetworkEvents: LPWSANETWORKEVENTS)
103                                 -> libc::c_int;
104
105     pub fn ioctlsocket(s: libc::SOCKET, cmd: libc::c_long,
106                        argp: *mut libc::c_ulong) -> libc::c_int;
107     pub fn select(nfds: libc::c_int,
108                   readfds: *mut fd_set,
109                   writefds: *mut fd_set,
110                   exceptfds: *mut fd_set,
111                   timeout: *mut libc::timeval) -> libc::c_int;
112     pub fn getsockopt(sockfd: libc::SOCKET,
113                       level: libc::c_int,
114                       optname: libc::c_int,
115                       optval: *mut libc::c_char,
116                       optlen: *mut libc::c_int) -> libc::c_int;
117
118     pub fn CancelIo(hFile: libc::HANDLE) -> libc::BOOL;
119     pub fn CancelIoEx(hFile: libc::HANDLE,
120                       lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
121 }
122
123 pub mod compat {
124     use std::intrinsics::{atomic_store_relaxed, transmute};
125     use std::iter::Iterator;
126     use libc::types::os::arch::extra::{LPCWSTR, HMODULE, LPCSTR, LPVOID};
127
128     extern "system" {
129         fn GetModuleHandleW(lpModuleName: LPCWSTR) -> HMODULE;
130         fn GetProcAddress(hModule: HMODULE, lpProcName: LPCSTR) -> LPVOID;
131     }
132
133     // store_func() is idempotent, so using relaxed ordering for the atomics
134     // should be enough.  This way, calling a function in this compatibility
135     // layer (after it's loaded) shouldn't be any slower than a regular DLL
136     // call.
137     unsafe fn store_func(ptr: *mut uint, module: &str, symbol: &str, fallback: uint) {
138         let module: Vec<u16> = module.utf16_units().collect();
139         let module = module.append_one(0);
140         symbol.with_c_str(|symbol| {
141             let handle = GetModuleHandleW(module.as_ptr());
142             let func: uint = transmute(GetProcAddress(handle, symbol));
143             atomic_store_relaxed(ptr, if func == 0 {
144                 fallback
145             } else {
146                 func
147             })
148         })
149     }
150
151     /// Macro for creating a compatibility fallback for a Windows function
152     ///
153     /// # Example
154     /// ```
155     /// compat_fn!(adll32::SomeFunctionW(_arg: LPCWSTR) {
156     ///     // Fallback implementation
157     /// })
158     /// ```
159     ///
160     /// Note that arguments unused by the fallback implementation should not be called `_` as
161     /// they are used to be passed to the real function if available.
162     macro_rules! compat_fn(
163         ($module:ident::$symbol:ident($($argname:ident: $argtype:ty),*)
164                                       -> $rettype:ty $fallback:block) => (
165             #[inline(always)]
166             pub unsafe fn $symbol($($argname: $argtype),*) -> $rettype {
167                 static mut ptr: extern "system" fn($($argname: $argtype),*) -> $rettype = thunk;
168
169                 extern "system" fn thunk($($argname: $argtype),*) -> $rettype {
170                     unsafe {
171                         ::io::c::compat::store_func(&mut ptr as *mut _ as *mut uint,
172                                                     stringify!($module),
173                                                     stringify!($symbol),
174                                                     fallback as uint);
175                         ::std::intrinsics::atomic_load_relaxed(&ptr)($($argname),*)
176                     }
177                 }
178
179                 extern "system" fn fallback($($argname: $argtype),*) -> $rettype $fallback
180
181                 ::std::intrinsics::atomic_load_relaxed(&ptr)($($argname),*)
182             }
183         );
184
185         ($module:ident::$symbol:ident($($argname:ident: $argtype:ty),*) $fallback:block) => (
186             compat_fn!($module::$symbol($($argname: $argtype),*) -> () $fallback)
187         )
188     )
189
190     /// Compatibility layer for functions in `kernel32.dll`
191     ///
192     /// Latest versions of Windows this is needed for:
193     ///
194     /// * `CreateSymbolicLinkW`: Windows XP, Windows Server 2003
195     /// * `GetFinalPathNameByHandleW`: Windows XP, Windows Server 2003
196     pub mod kernel32 {
197         use libc::types::os::arch::extra::{DWORD, LPCWSTR, BOOLEAN, HANDLE};
198         use libc::consts::os::extra::ERROR_CALL_NOT_IMPLEMENTED;
199
200         extern "system" {
201             fn SetLastError(dwErrCode: DWORD);
202         }
203
204         compat_fn!(kernel32::CreateSymbolicLinkW(_lpSymlinkFileName: LPCWSTR,
205                                                  _lpTargetFileName: LPCWSTR,
206                                                  _dwFlags: DWORD) -> BOOLEAN {
207             unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); }
208             0
209         })
210
211         compat_fn!(kernel32::GetFinalPathNameByHandleW(_hFile: HANDLE,
212                                                        _lpszFilePath: LPCWSTR,
213                                                        _cchFilePath: DWORD,
214                                                        _dwFlags: DWORD) -> DWORD {
215             unsafe { SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD); }
216             0
217         })
218     }
219 }
220
221 extern "system" {
222     // FIXME - pInputControl should be PCONSOLE_READCONSOLE_CONTROL
223     pub fn ReadConsoleW(hConsoleInput: libc::HANDLE,
224                         lpBuffer: libc::LPVOID,
225                         nNumberOfCharsToRead: libc::DWORD,
226                         lpNumberOfCharsRead: libc::LPDWORD,
227                         pInputControl: libc::LPVOID) -> libc::BOOL;
228
229     pub fn WriteConsoleW(hConsoleOutput: libc::HANDLE,
230                          lpBuffer: libc::types::os::arch::extra::LPCVOID,
231                          nNumberOfCharsToWrite: libc::DWORD,
232                          lpNumberOfCharsWritten: libc::LPDWORD,
233                          lpReserved: libc::LPVOID) -> libc::BOOL;
234
235     pub fn GetConsoleMode(hConsoleHandle: libc::HANDLE,
236                           lpMode: libc::LPDWORD) -> libc::BOOL;
237
238     pub fn SetConsoleMode(hConsoleHandle: libc::HANDLE,
239                           lpMode: libc::DWORD) -> libc::BOOL;
240 }