std::rt: Support os::args
This commit is contained in:
parent
7a9a6e4591
commit
1b7c99655f
@ -40,6 +40,8 @@ use option::{Some, None};
|
||||
use os;
|
||||
use prelude::*;
|
||||
use ptr;
|
||||
use rt;
|
||||
use rt::TaskContext;
|
||||
use str;
|
||||
use uint;
|
||||
use unstable::finally::Finally;
|
||||
@ -1167,10 +1169,17 @@ pub fn real_args() -> ~[~str] {
|
||||
#[cfg(target_os = "android")]
|
||||
#[cfg(target_os = "freebsd")]
|
||||
pub fn real_args() -> ~[~str] {
|
||||
unsafe {
|
||||
let argc = rustrt::rust_get_argc();
|
||||
let argv = rustrt::rust_get_argv();
|
||||
load_argc_and_argv(argc, argv)
|
||||
if rt::context() == TaskContext {
|
||||
match rt::args::clone() {
|
||||
Some(args) => args,
|
||||
None => fail!("process arguments not initialized")
|
||||
}
|
||||
} else {
|
||||
unsafe {
|
||||
let argc = rustrt::rust_get_argc();
|
||||
let argv = rustrt::rust_get_argv();
|
||||
load_argc_and_argv(argc, argv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
125
src/libstd/rt/args.rs
Normal file
125
src/libstd/rt/args.rs
Normal file
@ -0,0 +1,125 @@
|
||||
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
//! Global storage for command line arguments
|
||||
//!
|
||||
//! The current incarnation of the Rust runtime expects for
|
||||
//! the processes `argc` and `argv` arguments to be stored
|
||||
//! in a globally-accessible location for use by the `os` module.
|
||||
//!
|
||||
//! XXX: Would be nice for this to not exist.
|
||||
//! XXX: This has a lot of C glue for lack of globals.
|
||||
|
||||
use libc;
|
||||
use option::{Option, Some, None};
|
||||
use str;
|
||||
use uint;
|
||||
use unstable::finally::Finally;
|
||||
use util;
|
||||
|
||||
/// One-time global initialization.
|
||||
pub unsafe fn init(argc: int, argv: **u8) {
|
||||
let args = load_argc_and_argv(argc, argv);
|
||||
put(args);
|
||||
}
|
||||
|
||||
/// One-time global cleanup.
|
||||
pub fn cleanup() {
|
||||
rtassert!(take().is_some());
|
||||
}
|
||||
|
||||
/// Take the global arguments from global storage.
|
||||
pub fn take() -> Option<~[~str]> {
|
||||
with_lock(|| unsafe {
|
||||
let ptr = get_global_ptr();
|
||||
let val = util::replace(&mut *ptr, None);
|
||||
val.map(|s: &~~[~str]| (**s).clone())
|
||||
})
|
||||
}
|
||||
|
||||
/// Give the global arguments to global storage.
|
||||
///
|
||||
/// It is an error if the arguments already exist.
|
||||
pub fn put(args: ~[~str]) {
|
||||
with_lock(|| unsafe {
|
||||
let ptr = get_global_ptr();
|
||||
rtassert!((*ptr).is_none());
|
||||
(*ptr) = Some(~args.clone());
|
||||
})
|
||||
}
|
||||
|
||||
/// Make a clone of the global arguments.
|
||||
pub fn clone() -> Option<~[~str]> {
|
||||
with_lock(|| unsafe {
|
||||
let ptr = get_global_ptr();
|
||||
(*ptr).map(|s: &~~[~str]| (**s).clone())
|
||||
})
|
||||
}
|
||||
|
||||
fn with_lock<T>(f: &fn() -> T) -> T {
|
||||
do (|| {
|
||||
unsafe {
|
||||
rust_take_global_args_lock();
|
||||
f()
|
||||
}
|
||||
}).finally {
|
||||
unsafe {
|
||||
rust_drop_global_args_lock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_global_ptr() -> *mut Option<~~[~str]> {
|
||||
unsafe { rust_get_global_args_ptr() }
|
||||
}
|
||||
|
||||
// Copied from `os`.
|
||||
unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~str] {
|
||||
let mut args = ~[];
|
||||
for uint::range(0, argc as uint) |i| {
|
||||
args.push(str::raw::from_c_str(*(argv as **libc::c_char).offset(i)));
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
extern {
|
||||
fn rust_take_global_args_lock();
|
||||
fn rust_drop_global_args_lock();
|
||||
fn rust_get_global_args_ptr() -> *mut Option<~~[~str]>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use option::{Some, None};
|
||||
use super::*;
|
||||
use unstable::finally::Finally;
|
||||
|
||||
#[test]
|
||||
fn smoke_test() {
|
||||
// Preserve the actual global state.
|
||||
let saved_value = take();
|
||||
|
||||
let expected = ~[~"happy", ~"today?"];
|
||||
|
||||
put(expected.clone());
|
||||
assert!(clone() == Some(expected.clone()));
|
||||
assert!(take() == Some(expected.clone()));
|
||||
assert!(take() == None);
|
||||
|
||||
do (|| {
|
||||
}).finally {
|
||||
// Restore the actual global state.
|
||||
match saved_value {
|
||||
Some(ref args) => put(args.clone()),
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -159,6 +159,9 @@ pub mod metrics;
|
||||
/// Just stuff
|
||||
pub mod util;
|
||||
|
||||
// Global command line argument storage
|
||||
pub mod args;
|
||||
|
||||
/// Set up a default runtime configuration, given compiler-supplied arguments.
|
||||
///
|
||||
/// This is invoked by the `start` _language item_ (unstable::lang) to
|
||||
@ -173,20 +176,28 @@ pub mod util;
|
||||
/// # Return value
|
||||
///
|
||||
/// The return value is used as the process return code. 0 on success, 101 on error.
|
||||
pub fn start(_argc: int, _argv: **u8, crate_map: *u8, main: ~fn()) -> int {
|
||||
pub fn start(argc: int, argv: **u8, crate_map: *u8, main: ~fn()) -> int {
|
||||
|
||||
init(crate_map);
|
||||
init(argc, argv, crate_map);
|
||||
let exit_code = run(main);
|
||||
cleanup();
|
||||
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
/// One-time runtime initialization. Currently all this does is set up logging
|
||||
/// based on the RUST_LOG environment variable.
|
||||
pub fn init(crate_map: *u8) {
|
||||
logging::init(crate_map);
|
||||
unsafe { rust_update_gc_metadata(crate_map) }
|
||||
/// One-time runtime initialization.
|
||||
///
|
||||
/// Initializes global state, including frobbing
|
||||
/// the crate's logging flags, registering GC
|
||||
/// metadata, and storing the process arguments.
|
||||
pub fn init(argc: int, argv: **u8, crate_map: *u8) {
|
||||
// XXX: Derefing these pointers is not safe.
|
||||
// Need to propagate the unsafety to `start`.
|
||||
unsafe {
|
||||
args::init(argc, argv);
|
||||
logging::init(crate_map);
|
||||
rust_update_gc_metadata(crate_map);
|
||||
}
|
||||
|
||||
extern {
|
||||
fn rust_update_gc_metadata(crate_map: *u8);
|
||||
@ -195,6 +206,7 @@ pub fn init(crate_map: *u8) {
|
||||
|
||||
/// One-time runtime cleanup.
|
||||
pub fn cleanup() {
|
||||
args::cleanup();
|
||||
global_heap::cleanup();
|
||||
}
|
||||
|
||||
|
@ -932,6 +932,24 @@ rust_get_num_cpus() {
|
||||
return get_num_cpus();
|
||||
}
|
||||
|
||||
static lock_and_signal global_args_lock;
|
||||
static uintptr_t global_args_ptr = 0;
|
||||
|
||||
extern "C" CDECL void
|
||||
rust_take_global_args_lock() {
|
||||
global_args_lock.lock();
|
||||
}
|
||||
|
||||
extern "C" CDECL void
|
||||
rust_drop_global_args_lock() {
|
||||
global_args_lock.unlock();
|
||||
}
|
||||
|
||||
extern "C" CDECL uintptr_t*
|
||||
rust_get_global_args_ptr() {
|
||||
return &global_args_ptr;
|
||||
}
|
||||
|
||||
//
|
||||
// Local Variables:
|
||||
// mode: C++
|
||||
|
@ -242,3 +242,4 @@ rust_drop_env_lock
|
||||
rust_update_log_settings
|
||||
rust_running_on_valgrind
|
||||
rust_get_num_cpus
|
||||
rust_get_global_args_ptr
|
Loading…
x
Reference in New Issue
Block a user