|
| 1 | +use alloc::{string::String, vec::Vec}; |
| 2 | +use std::io; |
| 3 | + |
| 4 | +use crate::crt_fd; |
| 5 | +use windows_sys::Win32::System::Diagnostics::Debug; |
| 6 | + |
| 7 | +pub const LK_UNLCK: i32 = 0; |
| 8 | +pub const LK_LOCK: i32 = 1; |
| 9 | +pub const LK_NBLCK: i32 = 2; |
| 10 | +pub const LK_RLCK: i32 = 3; |
| 11 | +pub const LK_NBRLCK: i32 = 4; |
| 12 | + |
| 13 | +unsafe extern "C" { |
| 14 | + fn _getch() -> i32; |
| 15 | + fn _getwch() -> u32; |
| 16 | + fn _getche() -> i32; |
| 17 | + fn _getwche() -> u32; |
| 18 | + fn _putch(c: u32) -> i32; |
| 19 | + fn _putwch(c: u16) -> u32; |
| 20 | + fn _ungetch(c: i32) -> i32; |
| 21 | + fn _ungetwch(c: u32) -> u32; |
| 22 | + fn _locking(fd: i32, mode: i32, nbytes: i64) -> i32; |
| 23 | + fn _heapmin() -> i32; |
| 24 | + fn _kbhit() -> i32; |
| 25 | + fn _setmode(fd: crt_fd::Borrowed<'_>, flags: i32) -> i32; |
| 26 | +} |
| 27 | + |
| 28 | +pub fn setmode_binary(fd: crt_fd::Borrowed<'_>) { |
| 29 | + unsafe { suppress_iph!(_setmode(fd, libc::O_BINARY)) }; |
| 30 | +} |
| 31 | + |
| 32 | +pub fn getch() -> Vec<u8> { |
| 33 | + vec![unsafe { _getch() } as u8] |
| 34 | +} |
| 35 | + |
| 36 | +pub fn getwch() -> String { |
| 37 | + let value = unsafe { _getwch() }; |
| 38 | + char::from_u32(value) |
| 39 | + .unwrap_or_else(|| panic!("invalid unicode {value:#x} from _getwch")) |
| 40 | + .to_string() |
| 41 | +} |
| 42 | + |
| 43 | +pub fn getche() -> Vec<u8> { |
| 44 | + vec![unsafe { _getche() } as u8] |
| 45 | +} |
| 46 | + |
| 47 | +pub fn getwche() -> String { |
| 48 | + let value = unsafe { _getwche() }; |
| 49 | + char::from_u32(value) |
| 50 | + .unwrap_or_else(|| panic!("invalid unicode {value:#x} from _getwche")) |
| 51 | + .to_string() |
| 52 | +} |
| 53 | + |
| 54 | +pub fn putch(c: u8) { |
| 55 | + unsafe { suppress_iph!(_putch(c.into())) }; |
| 56 | +} |
| 57 | + |
| 58 | +pub fn putwch(c: char) { |
| 59 | + unsafe { suppress_iph!(_putwch(c as u16)) }; |
| 60 | +} |
| 61 | + |
| 62 | +pub fn ungetch(c: u8) -> io::Result<()> { |
| 63 | + let ret = unsafe { suppress_iph!(_ungetch(c as i32)) }; |
| 64 | + if ret == -1 { |
| 65 | + Err(io::Error::from_raw_os_error(libc::ENOSPC)) |
| 66 | + } else { |
| 67 | + Ok(()) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +pub fn ungetwch(c: char) -> io::Result<()> { |
| 72 | + let ret = unsafe { suppress_iph!(_ungetwch(c as u32)) }; |
| 73 | + if ret == 0xFFFF { |
| 74 | + Err(io::Error::from_raw_os_error(libc::ENOSPC)) |
| 75 | + } else { |
| 76 | + Ok(()) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +pub fn kbhit() -> i32 { |
| 81 | + unsafe { _kbhit() } |
| 82 | +} |
| 83 | + |
| 84 | +pub fn locking(fd: i32, mode: i32, nbytes: i64) -> io::Result<()> { |
| 85 | + let ret = unsafe { suppress_iph!(_locking(fd, mode, nbytes)) }; |
| 86 | + if ret == -1 { |
| 87 | + Err(io::Error::last_os_error()) |
| 88 | + } else { |
| 89 | + Ok(()) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +pub fn heapmin() -> io::Result<()> { |
| 94 | + let ret = unsafe { suppress_iph!(_heapmin()) }; |
| 95 | + if ret == -1 { |
| 96 | + Err(io::Error::last_os_error()) |
| 97 | + } else { |
| 98 | + Ok(()) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +pub fn setmode(fd: crt_fd::Borrowed<'_>, flags: i32) -> io::Result<i32> { |
| 103 | + let ret = unsafe { suppress_iph!(_setmode(fd, flags)) }; |
| 104 | + if ret == -1 { |
| 105 | + Err(io::Error::last_os_error()) |
| 106 | + } else { |
| 107 | + Ok(ret) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +pub fn open_osfhandle(handle: isize, flags: i32) -> io::Result<i32> { |
| 112 | + let ret = unsafe { suppress_iph!(libc::open_osfhandle(handle, flags)) }; |
| 113 | + if ret == -1 { |
| 114 | + Err(io::Error::last_os_error()) |
| 115 | + } else { |
| 116 | + Ok(ret) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +pub fn get_error_mode() -> u32 { |
| 121 | + unsafe { suppress_iph!(Debug::GetErrorMode()) } |
| 122 | +} |
| 123 | + |
| 124 | +pub fn set_error_mode(mode: Debug::THREAD_ERROR_MODE) -> u32 { |
| 125 | + unsafe { suppress_iph!(Debug::SetErrorMode(mode)) } |
| 126 | +} |
0 commit comments