diff --git a/src/header/wchar/mod.rs b/src/header/wchar/mod.rs index 0f231b1080..21bab30676 100644 --- a/src/header/wchar/mod.rs +++ b/src/header/wchar/mod.rs @@ -1,4 +1,6 @@ -//! wchar implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/wchar.h.html +//! `wchar.h` implementation. +//! +//! See . use core::{char, ffi::VaList as va_list, mem, ptr, slice, usize}; @@ -15,7 +17,13 @@ use crate::{ wctype::*, }, iter::{NulTerminated, NulTerminatedInclusive}, - platform::{self, ERRNO, types::*}, + platform::{ + self, ERRNO, + types::{ + c_char, c_double, c_int, c_long, c_longlong, c_uchar, c_ulong, c_ulonglong, c_void, + intmax_t, size_t, uintmax_t, wchar_t, wint_t, + }, + }, }; mod lookaheadreader; @@ -23,10 +31,12 @@ mod utf8; mod wprintf; mod wscanf; +/// See . #[repr(C)] #[derive(Clone, Copy)] pub struct mbstate_t; +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn btowc(c: c_int) -> wint_t { //Check for EOF @@ -47,6 +57,7 @@ pub unsafe extern "C" fn btowc(c: c_int) -> wint_t { wc as wint_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fgetwc(stream: *mut FILE) -> wint_t { // TODO: Process locale @@ -94,6 +105,7 @@ pub unsafe extern "C" fn fgetwc(stream: *mut FILE) -> wint_t { wc as wint_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fgetws(ws: *mut wchar_t, n: c_int, stream: *mut FILE) -> *mut wchar_t { //TODO: lock @@ -113,6 +125,7 @@ pub unsafe extern "C" fn fgetws(ws: *mut wchar_t, n: c_int, stream: *mut FILE) - ws } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fputwc(wc: wchar_t, stream: *mut FILE) -> wint_t { //Convert wchar_t to multibytes first @@ -128,6 +141,7 @@ pub unsafe extern "C" fn fputwc(wc: wchar_t, stream: *mut FILE) -> wint_t { wc as wint_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fputws(ws: *const wchar_t, stream: *mut FILE) -> c_int { let mut i = 0; @@ -143,34 +157,41 @@ pub unsafe extern "C" fn fputws(ws: *const wchar_t, stream: *mut FILE) -> c_int } } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fwide(stream: *mut FILE, mode: c_int) -> c_int { (*stream).try_set_orientation(mode) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn getwc(stream: *mut FILE) -> wint_t { fgetwc(stream) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn getwchar() -> wint_t { fgetwc(stdin) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn mbsinit(ps: *const mbstate_t) -> c_int { //Add a check for the state maybe if ps.is_null() { 1 } else { 0 } } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn mbrlen(s: *const c_char, n: size_t, ps: *mut mbstate_t) -> size_t { static mut INTERNAL: mbstate_t = mbstate_t; mbrtowc(ptr::null_mut(), s, n, &raw mut INTERNAL) } -//Only works for UTF8 at the moment +/// See . +/// +/// Only works for UTF8 at the moment. #[unsafe(no_mangle)] pub unsafe extern "C" fn mbrtowc( pwc: *mut wchar_t, @@ -191,8 +212,9 @@ pub unsafe extern "C" fn mbrtowc( } } -//Convert a multibyte string to a wide string with a limited amount of bytes -//Required for in POSIX.1-2008 +/// See . +/// +/// Convert a multibyte string to a wide string with a limited amount of bytes. #[unsafe(no_mangle)] pub unsafe extern "C" fn mbsnrtowcs( dst_ptr: *mut wchar_t, @@ -251,7 +273,9 @@ pub unsafe extern "C" fn mbsnrtowcs( dst_offset } -//Convert a multibyte string to a wide string +/// See . +/// +/// Convert a multibyte string to a wide string. #[unsafe(no_mangle)] pub unsafe extern "C" fn mbsrtowcs( dst: *mut wchar_t, @@ -262,16 +286,19 @@ pub unsafe extern "C" fn mbsrtowcs( mbsnrtowcs(dst, src, size_t::max_value(), len, ps) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn putwc(wc: wchar_t, stream: *mut FILE) -> wint_t { fputwc(wc, &mut *stream) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn putwchar(wc: wchar_t) -> wint_t { fputwc(wc, &mut *stdout) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn vswscanf( s: *const wchar_t, @@ -282,6 +309,7 @@ pub unsafe extern "C" fn vswscanf( wscanf::scanf(reader, format, __valist) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn swscanf( s: *const wchar_t, @@ -291,6 +319,8 @@ pub unsafe extern "C" fn swscanf( vswscanf(s, format, __valist.as_va_list()) } +/// See . +/// /// Push wide character `wc` back onto `stream` so it'll be read next #[unsafe(no_mangle)] pub unsafe extern "C" fn ungetwc(wc: wint_t, stream: &mut FILE) -> wint_t { @@ -318,6 +348,7 @@ pub unsafe extern "C" fn ungetwc(wc: wint_t, stream: &mut FILE) -> wint_t { wc } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn vfwprintf( stream: *mut FILE, @@ -331,6 +362,8 @@ pub unsafe extern "C" fn vfwprintf( wprintf::wprintf(&mut *stream, WStr::from_ptr(format), arg) } + +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn fwprintf( stream: *mut FILE, @@ -340,15 +373,19 @@ pub unsafe extern "C" fn fwprintf( vfwprintf(stream, format, __valist.as_va_list()) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn vwprintf(format: *const wchar_t, arg: va_list) -> c_int { vfwprintf(&mut *stdout, format, arg) } + +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wprintf(format: *const wchar_t, mut __valist: ...) -> c_int { vfwprintf(&mut *stdout, format, __valist.as_va_list()) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn vswprintf( s: *mut wchar_t, @@ -361,6 +398,8 @@ pub unsafe extern "C" fn vswprintf( eprintln!("vswprintf not implemented"); -1 } + +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn swprintf( s: *mut wchar_t, @@ -371,17 +410,21 @@ pub unsafe extern "C" fn swprintf( vswprintf(s, n, format, __valist.as_va_list()) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcpcpy(d: *mut wchar_t, s: *const wchar_t) -> *mut wchar_t { return (wcscpy(d, s)).offset(wcslen(s) as isize); } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcpncpy(d: *mut wchar_t, s: *const wchar_t, n: size_t) -> *mut wchar_t { return (wcsncpy(d, s, n)).offset(wcsnlen(s, n) as isize); } -//widechar to multibyte +/// See . +/// +/// widechar to multibyte. #[unsafe(no_mangle)] pub unsafe extern "C" fn wcrtomb(s: *mut c_char, wc: wchar_t, ps: *mut mbstate_t) -> size_t { let mut buffer: [c_char; MB_CUR_MAX as usize] = [0; MB_CUR_MAX as usize]; @@ -394,6 +437,7 @@ pub unsafe extern "C" fn wcrtomb(s: *mut c_char, wc: wchar_t, ps: *mut mbstate_t utf8::wcrtomb(s_cpy, wc_cpy, ps) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsdup(s: *const wchar_t) -> *mut wchar_t { let l = wcslen(s); @@ -408,6 +452,7 @@ pub unsafe extern "C" fn wcsdup(s: *const wchar_t) -> *mut wchar_t { wmemcpy(d, s, l + 1) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsrtombs( s: *mut c_char, @@ -422,12 +467,13 @@ pub unsafe extern "C" fn wcsrtombs( wcsnrtombs(s, ws, size_t::MAX, n, st) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcscat(ws1: *mut wchar_t, ws2: *const wchar_t) -> *mut wchar_t { wcsncat(ws1, ws2, usize::MAX) } -/// See . +/// See . /// /// # Safety /// The caller is required to ensure that `ws` is a valid pointer to a buffer @@ -447,17 +493,20 @@ pub unsafe extern "C" fn wcschr(ws: *const wchar_t, wc: wchar_t) -> *mut wchar_t ptr.cast_mut() } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcscmp(ws1: *const wchar_t, ws2: *const wchar_t) -> c_int { wcsncmp(ws1, ws2, usize::MAX) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcscoll(ws1: *const wchar_t, ws2: *const wchar_t) -> c_int { //TODO: locale comparison wcscmp(ws1, ws2) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcscpy(ws1: *mut wchar_t, ws2: *const wchar_t) -> *mut wchar_t { let mut i = 0; @@ -480,11 +529,13 @@ unsafe fn inner_wcsspn(mut wcs: *const wchar_t, set: *const wchar_t, reject: boo count } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcscspn(wcs: *const wchar_t, set: *const wchar_t) -> size_t { inner_wcsspn(wcs, set, true) } +/// See . // #[unsafe(no_mangle)] pub extern "C" fn wcsftime( wcs: *mut wchar_t, @@ -495,11 +546,13 @@ pub extern "C" fn wcsftime( unimplemented!(); } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcslen(ws: *const wchar_t) -> size_t { unsafe { NulTerminated::new(ws).unwrap() }.count() } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsncat( ws1: *mut wchar_t, @@ -521,6 +574,7 @@ pub unsafe extern "C" fn wcsncat( ws1 } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsncmp(ws1: *const wchar_t, ws2: *const wchar_t, n: size_t) -> c_int { for i in 0..n { @@ -535,6 +589,7 @@ pub unsafe extern "C" fn wcsncmp(ws1: *const wchar_t, ws2: *const wchar_t, n: si 0 } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsncpy( ws1: *mut wchar_t, @@ -557,6 +612,7 @@ pub unsafe extern "C" fn wcsncpy( ws1 } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsnlen(mut s: *const wchar_t, maxlen: size_t) -> size_t { let mut len = 0; @@ -573,6 +629,7 @@ pub unsafe extern "C" fn wcsnlen(mut s: *const wchar_t, maxlen: size_t) -> size_ return len; } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsnrtombs( mut dest: *mut c_char, @@ -621,6 +678,7 @@ pub unsafe extern "C" fn wcsnrtombs( written } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcspbrk(mut wcs: *const wchar_t, set: *const wchar_t) -> *mut wchar_t { wcs = wcs.add(wcscspn(wcs, set)); @@ -633,6 +691,7 @@ pub unsafe extern "C" fn wcspbrk(mut wcs: *const wchar_t, set: *const wchar_t) - } } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsrchr(ws1: *const wchar_t, wc: wchar_t) -> *mut wchar_t { let mut last_matching_wc = 0 as *const wchar_t; @@ -648,11 +707,13 @@ pub unsafe extern "C" fn wcsrchr(ws1: *const wchar_t, wc: wchar_t) -> *mut wchar last_matching_wc as *mut wchar_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsspn(wcs: *const wchar_t, set: *const wchar_t) -> size_t { inner_wcsspn(wcs, set, false) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcsstr(ws1: *const wchar_t, ws2: *const wchar_t) -> *mut wchar_t { // Get length of ws2, not including null terminator @@ -689,6 +750,7 @@ macro_rules! skipws { }; } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstod(mut ptr: *const wchar_t, end: *mut *mut wchar_t) -> c_double { const RADIX: u32 = 10; @@ -729,6 +791,7 @@ pub unsafe extern "C" fn wcstod(mut ptr: *const wchar_t, end: *mut *mut wchar_t) result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstok( mut wcs: *mut wchar_t, @@ -815,6 +878,7 @@ macro_rules! strto_impl { }}; } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstol( mut ptr: *const wchar_t, @@ -829,6 +893,7 @@ pub unsafe extern "C" fn wcstol( result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstoll( mut ptr: *const wchar_t, @@ -843,6 +908,7 @@ pub unsafe extern "C" fn wcstoll( result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstoimax( mut ptr: *const wchar_t, @@ -857,6 +923,7 @@ pub unsafe extern "C" fn wcstoimax( result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstoul( mut ptr: *const wchar_t, @@ -871,6 +938,7 @@ pub unsafe extern "C" fn wcstoul( result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstoull( mut ptr: *const wchar_t, @@ -885,6 +953,7 @@ pub unsafe extern "C" fn wcstoull( result } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcstoumax( mut ptr: *const wchar_t, @@ -899,11 +968,16 @@ pub unsafe extern "C" fn wcstoumax( result } +/// See . +/// +/// Marked legacy in issue 6. +/// Encouraged to use `wcsstr` instead, which this implementation simply forwards to. #[unsafe(no_mangle)] pub unsafe extern "C" fn wcswcs(ws1: *const wchar_t, ws2: *const wchar_t) -> *mut wchar_t { wcsstr(ws1, ws2) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wcswidth(pwcs: *const wchar_t, n: size_t) -> c_int { let mut total_width = 0; @@ -917,16 +991,19 @@ pub unsafe extern "C" fn wcswidth(pwcs: *const wchar_t, n: size_t) -> c_int { total_width } +/// See . // #[unsafe(no_mangle)] pub extern "C" fn wcsxfrm(ws1: *mut wchar_t, ws2: *const wchar_t, n: size_t) -> size_t { unimplemented!(); } +/// See . #[unsafe(no_mangle)] pub extern "C" fn wctob(c: wint_t) -> c_int { if c <= 0x7F { c as c_int } else { EOF } } +/// See . #[unsafe(no_mangle)] pub extern "C" fn wcwidth(wc: wchar_t) -> c_int { match char::from_u32(wc as u32) { @@ -938,6 +1015,7 @@ pub extern "C" fn wcwidth(wc: wchar_t) -> c_int { } } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wmemchr(ws: *const wchar_t, wc: wchar_t, n: size_t) -> *mut wchar_t { for i in 0..n { @@ -948,6 +1026,7 @@ pub unsafe extern "C" fn wmemchr(ws: *const wchar_t, wc: wchar_t, n: size_t) -> ptr::null_mut() } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wmemcmp(ws1: *const wchar_t, ws2: *const wchar_t, n: size_t) -> c_int { for i in 0..n { @@ -960,6 +1039,7 @@ pub unsafe extern "C" fn wmemcmp(ws1: *const wchar_t, ws2: *const wchar_t, n: si 0 } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wmemcpy( ws1: *mut wchar_t, @@ -973,6 +1053,7 @@ pub unsafe extern "C" fn wmemcpy( ) as *mut wchar_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wmemmove( ws1: *mut wchar_t, @@ -986,6 +1067,7 @@ pub unsafe extern "C" fn wmemmove( ) as *mut wchar_t } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wmemset(ws: *mut wchar_t, wc: wchar_t, n: size_t) -> *mut wchar_t { for i in 0..n { @@ -994,6 +1076,7 @@ pub unsafe extern "C" fn wmemset(ws: *mut wchar_t, wc: wchar_t, n: size_t) -> *m ws } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn vwscanf(format: *const wchar_t, __valist: va_list) -> c_int { let mut file = (*stdin).lock(); @@ -1006,13 +1089,15 @@ pub unsafe extern "C" fn vwscanf(format: *const wchar_t, __valist: va_list) -> c wscanf::scanf(reader, format, __valist) } +/// See . #[unsafe(no_mangle)] pub unsafe extern "C" fn wscanf(format: *const wchar_t, mut __valist: ...) -> c_int { vwscanf(format, __valist.as_va_list()) } +/// See . #[unsafe(no_mangle)] -pub extern "C" fn wcscasecmp(mut s1: *const wchar_t, mut s2: *const wchar_t) -> c_int { +pub unsafe extern "C" fn wcscasecmp(mut s1: *const wchar_t, mut s2: *const wchar_t) -> c_int { unsafe { while *s1 != 0 && *s2 != 0 { if towlower(*s1 as wint_t) != towlower(*s2 as wint_t) { @@ -1026,8 +1111,13 @@ pub extern "C" fn wcscasecmp(mut s1: *const wchar_t, mut s2: *const wchar_t) -> } } +/// See . #[unsafe(no_mangle)] -pub extern "C" fn wcsncasecmp(mut s1: *const wchar_t, mut s2: *const wchar_t, n: size_t) -> c_int { +pub unsafe extern "C" fn wcsncasecmp( + mut s1: *const wchar_t, + mut s2: *const wchar_t, + n: size_t, +) -> c_int { if n == 0 { return 0; }