Handle fallout in os

os::env(), os::args(), and related functions now use Vec<T> instead of
~[T].
This commit is contained in:
Kevin Ballard 2014-05-03 22:42:27 -07:00
parent 001a8741b4
commit f340fb9b12
2 changed files with 47 additions and 44 deletions

View File

@ -169,7 +169,7 @@ fn with_env_lock<T>(f: || -> T) -> T {
///
/// Invalid UTF-8 bytes are replaced with \uFFFD. See `str::from_utf8_lossy()`
/// for details.
pub fn env() -> ~[(~str,~str)] {
pub fn env() -> Vec<(~str,~str)> {
env_as_bytes().move_iter().map(|(k,v)| {
let k = str::from_utf8_lossy(k).into_owned();
let v = str::from_utf8_lossy(v).into_owned();
@ -179,7 +179,7 @@ pub fn env() -> ~[(~str,~str)] {
/// Returns a vector of (variable, value) byte-vector pairs for all the
/// environment variables of the current process.
pub fn env_as_bytes() -> ~[(~[u8],~[u8])] {
pub fn env_as_bytes() -> Vec<(~[u8],~[u8])> {
unsafe {
#[cfg(windows)]
unsafe fn get_env_pairs() -> Vec<~[u8]> {
@ -224,16 +224,16 @@ pub fn env_as_bytes() -> ~[(~[u8],~[u8])] {
fn env_convert(input: Vec<~[u8]>) -> Vec<(~[u8], ~[u8])> {
let mut pairs = Vec::new();
for p in input.iter() {
let vs: ~[&[u8]] = p.splitn(1, |b| *b == '=' as u8).collect();
let key = vs[0].to_owned();
let val = if vs.len() < 2 { box [] } else { vs[1].to_owned() };
let mut it = p.splitn(1, |b| *b == '=' as u8);
let key = it.next().unwrap().to_owned();
let val = it.next().unwrap_or(&[]).to_owned();
pairs.push((key, val));
}
pairs
}
with_env_lock(|| {
let unparsed_environ = get_env_pairs();
env_convert(unparsed_environ).move_iter().collect()
env_convert(unparsed_environ)
})
}
}
@ -416,7 +416,7 @@ pub fn dll_filename(base: &str) -> ~str {
pub fn self_exe_name() -> Option<Path> {
#[cfg(target_os = "freebsd")]
fn load_self() -> Option<~[u8]> {
fn load_self() -> Option<Vec<u8>> {
unsafe {
use libc::funcs::bsd44::*;
use libc::consts::os::extra::*;
@ -436,23 +436,23 @@ pub fn self_exe_name() -> Option<Path> {
if err != 0 { return None; }
if sz == 0 { return None; }
v.set_len(sz as uint - 1); // chop off trailing NUL
Some(v.move_iter().collect())
Some(v)
}
}
#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
fn load_self() -> Option<~[u8]> {
fn load_self() -> Option<Vec<u8>> {
use std::io;
match io::fs::readlink(&Path::new("/proc/self/exe")) {
Ok(path) => Some(path.as_vec().to_owned()),
Ok(path) => Some(path.into_vec()),
Err(..) => None
}
}
#[cfg(target_os = "macos")]
fn load_self() -> Option<~[u8]> {
fn load_self() -> Option<Vec<u8>> {
unsafe {
use libc::funcs::extra::_NSGetExecutablePath;
let mut sz: u32 = 0;
@ -462,19 +462,19 @@ pub fn self_exe_name() -> Option<Path> {
let err = _NSGetExecutablePath(v.as_mut_ptr() as *mut i8, &mut sz);
if err != 0 { return None; }
v.set_len(sz as uint - 1); // chop off trailing NUL
Some(v.move_iter().collect())
Some(v)
}
}
#[cfg(windows)]
fn load_self() -> Option<~[u8]> {
fn load_self() -> Option<Vec<u8>> {
use str::OwnedStr;
unsafe {
use os::win32::fill_utf16_buf_and_decode;
fill_utf16_buf_and_decode(|buf, sz| {
libc::GetModuleFileNameW(0u as libc::DWORD, buf, sz)
}).map(|s| s.into_bytes())
}).map(|s| s.into_strbuf().into_bytes())
}
}
@ -789,12 +789,12 @@ pub fn get_exit_status() -> int {
}
#[cfg(target_os = "macos")]
unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> ~[~[u8]] {
unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> Vec<~[u8]> {
use c_str::CString;
Vec::from_fn(argc as uint, |i| {
CString::new(*argv.offset(i as int), false).as_bytes_no_nul().to_owned()
}).move_iter().collect()
})
}
/**
@ -803,7 +803,7 @@ unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> ~[~[u8]] {
* Returns a list of the command line arguments.
*/
#[cfg(target_os = "macos")]
fn real_args_as_bytes() -> ~[~[u8]] {
fn real_args_as_bytes() -> Vec<~[u8]> {
unsafe {
let (argc, argv) = (*_NSGetArgc() as int,
*_NSGetArgv() as **c_char);
@ -814,7 +814,7 @@ fn real_args_as_bytes() -> ~[~[u8]] {
#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
#[cfg(target_os = "freebsd")]
fn real_args_as_bytes() -> ~[~[u8]] {
fn real_args_as_bytes() -> Vec<~[u8]> {
use rt;
match rt::args::clone() {
@ -824,12 +824,12 @@ fn real_args_as_bytes() -> ~[~[u8]] {
}
#[cfg(not(windows))]
fn real_args() -> ~[~str] {
fn real_args() -> Vec<~str> {
real_args_as_bytes().move_iter().map(|v| str::from_utf8_lossy(v).into_owned()).collect()
}
#[cfg(windows)]
fn real_args() -> ~[~str] {
fn real_args() -> Vec<~str> {
use slice;
use option::Expect;
@ -855,11 +855,11 @@ fn real_args() -> ~[~str] {
LocalFree(szArgList as *c_void);
}
return args.move_iter().collect();
return args
}
#[cfg(windows)]
fn real_args_as_bytes() -> ~[~[u8]] {
fn real_args_as_bytes() -> Vec<~[u8]> {
real_args().move_iter().map(|s| s.into_bytes()).collect()
}
@ -883,13 +883,13 @@ extern "system" {
///
/// The arguments are interpreted as utf-8, with invalid bytes replaced with \uFFFD.
/// See `str::from_utf8_lossy` for details.
pub fn args() -> ~[~str] {
pub fn args() -> Vec<~str> {
real_args()
}
/// Returns the arguments which this program was started with (normally passed
/// via the command line) as byte vectors.
pub fn args_as_bytes() -> ~[~[u8]] {
pub fn args_as_bytes() -> Vec<~[u8]> {
real_args_as_bytes()
}

View File

@ -21,6 +21,7 @@
//! FIXME #7756: This has a lot of C glue for lack of globals.
use option::Option;
use vec::Vec;
#[cfg(test)] use option::{Some, None};
#[cfg(test)] use realstd;
#[cfg(test)] use realargs = realstd::rt::args;
@ -36,10 +37,10 @@ pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
#[cfg(test)] pub unsafe fn cleanup() { realargs::cleanup() }
/// Take the global arguments from global storage.
#[cfg(not(test))] pub fn take() -> Option<~[~[u8]]> { imp::take() }
#[cfg(test)] pub fn take() -> Option<~[~[u8]]> {
#[cfg(not(test))] pub fn take() -> Option<Vec<~[u8]>> { imp::take() }
#[cfg(test)] pub fn take() -> Option<Vec<~[u8]>> {
match realargs::take() {
realstd::option::Some(a) => Some(a),
realstd::option::Some(v) => Some(unsafe{ ::cast::transmute(v) }),
realstd::option::None => None,
}
}
@ -47,14 +48,14 @@ pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
/// Give the global arguments to global storage.
///
/// It is an error if the arguments already exist.
#[cfg(not(test))] pub fn put(args: ~[~[u8]]) { imp::put(args) }
#[cfg(test)] pub fn put(args: ~[~[u8]]) { realargs::put(args) }
#[cfg(not(test))] pub fn put(args: Vec<~[u8]>) { imp::put(args) }
#[cfg(test)] pub fn put(args: Vec<~[u8]>) { realargs::put(unsafe { ::cast::transmute(args) }) }
/// Make a clone of the global arguments.
#[cfg(not(test))] pub fn clone() -> Option<~[~[u8]]> { imp::clone() }
#[cfg(test)] pub fn clone() -> Option<~[~[u8]]> {
#[cfg(not(test))] pub fn clone() -> Option<Vec<~[u8]>> { imp::clone() }
#[cfg(test)] pub fn clone() -> Option<Vec<~[u8]>> {
match realargs::clone() {
realstd::option::Some(a) => Some(a),
realstd::option::Some(v) => Some(unsafe { ::cast::transmute(v) }),
realstd::option::None => None,
}
}
@ -70,6 +71,7 @@ mod imp {
use owned::Box;
use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
use mem;
use vec::Vec;
#[cfg(not(test))] use ptr::RawPtr;
static mut global_args_ptr: uint = 0;
@ -87,15 +89,15 @@ mod imp {
lock.destroy();
}
pub fn take() -> Option<~[~[u8]]> {
pub fn take() -> Option<Vec<~[u8]>> {
with_lock(|| unsafe {
let ptr = get_global_ptr();
let val = mem::replace(&mut *ptr, None);
val.as_ref().map(|s: &Box<~[~[u8]]>| (**s).clone())
val.as_ref().map(|s: &Box<Vec<~[u8]>>| (**s).clone())
})
}
pub fn put(args: ~[~[u8]]) {
pub fn put(args: Vec<~[u8]>) {
with_lock(|| unsafe {
let ptr = get_global_ptr();
rtassert!((*ptr).is_none());
@ -103,10 +105,10 @@ mod imp {
})
}
pub fn clone() -> Option<~[~[u8]]> {
pub fn clone() -> Option<Vec<~[u8]>> {
with_lock(|| unsafe {
let ptr = get_global_ptr();
(*ptr).as_ref().map(|s: &Box<~[~[u8]]>| (**s).clone())
(*ptr).as_ref().map(|s: &Box<Vec<~[u8]>>| (**s).clone())
})
}
@ -117,13 +119,13 @@ mod imp {
}
}
fn get_global_ptr() -> *mut Option<Box<~[~[u8]]>> {
fn get_global_ptr() -> *mut Option<Box<Vec<~[u8]>>> {
unsafe { cast::transmute(&global_args_ptr) }
}
// Copied from `os`.
#[cfg(not(test))]
unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~[u8]] {
unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> Vec<~[u8]> {
use c_str::CString;
use ptr::RawPtr;
use libc;
@ -133,7 +135,7 @@ mod imp {
Vec::from_fn(argc as uint, |i| {
let cs = CString::new(*(argv as **libc::c_char).offset(i as int), false);
cs.as_bytes_no_nul().to_owned()
}).move_iter().collect()
})
}
#[cfg(test)]
@ -147,7 +149,7 @@ mod imp {
// Preserve the actual global state.
let saved_value = take();
let expected = box [bytes!("happy").to_owned(), bytes!("today?").to_owned()];
let expected = vec![bytes!("happy").to_owned(), bytes!("today?").to_owned()];
put(expected.clone());
assert!(clone() == Some(expected.clone()));
@ -170,6 +172,7 @@ mod imp {
#[cfg(target_os = "win32", not(test))]
mod imp {
use option::Option;
use vec::Vec;
pub unsafe fn init(_argc: int, _argv: **u8) {
}
@ -177,15 +180,15 @@ mod imp {
pub fn cleanup() {
}
pub fn take() -> Option<~[~[u8]]> {
pub fn take() -> Option<Vec<~[u8]>> {
fail!()
}
pub fn put(_args: ~[~[u8]]) {
pub fn put(_args: Vec<~[u8]>) {
fail!()
}
pub fn clone() -> Option<~[~[u8]]> {
pub fn clone() -> Option<Vec<~[u8]>> {
fail!()
}
}