2013-06-21 03:28:23 -05:00
|
|
|
// 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.
|
|
|
|
//!
|
2013-07-22 13:20:40 -05:00
|
|
|
//! Only valid to call on linux. Mac and Windows use syscalls to
|
|
|
|
//! discover the command line arguments.
|
|
|
|
//!
|
2013-06-21 03:28:23 -05:00
|
|
|
//! XXX: Would be nice for this to not exist.
|
|
|
|
//! XXX: This has a lot of C glue for lack of globals.
|
|
|
|
|
2013-07-22 13:20:40 -05:00
|
|
|
use option::Option;
|
2013-06-21 03:28:23 -05:00
|
|
|
|
|
|
|
/// One-time global initialization.
|
|
|
|
pub unsafe fn init(argc: int, argv: **u8) {
|
2013-07-22 13:20:40 -05:00
|
|
|
imp::init(argc, argv)
|
2013-06-21 03:28:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// One-time global cleanup.
|
|
|
|
pub fn cleanup() {
|
2013-07-22 13:20:40 -05:00
|
|
|
imp::cleanup()
|
2013-06-21 03:28:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Take the global arguments from global storage.
|
|
|
|
pub fn take() -> Option<~[~str]> {
|
2013-07-22 13:20:40 -05:00
|
|
|
imp::take()
|
2013-06-21 03:28:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Give the global arguments to global storage.
|
|
|
|
///
|
|
|
|
/// It is an error if the arguments already exist.
|
|
|
|
pub fn put(args: ~[~str]) {
|
2013-07-22 13:20:40 -05:00
|
|
|
imp::put(args)
|
2013-06-21 03:28:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Make a clone of the global arguments.
|
|
|
|
pub fn clone() -> Option<~[~str]> {
|
2013-07-22 13:20:40 -05:00
|
|
|
imp::clone()
|
2013-06-21 03:28:23 -05:00
|
|
|
}
|
|
|
|
|
2013-07-22 13:20:40 -05:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "android")]
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
mod imp {
|
2013-06-21 03:28:23 -05:00
|
|
|
|
2013-07-22 13:20:40 -05:00
|
|
|
use libc;
|
|
|
|
use option::{Option, Some, None};
|
|
|
|
use str;
|
|
|
|
use uint;
|
|
|
|
use unstable::finally::Finally;
|
|
|
|
use util;
|
2013-06-21 03:28:23 -05:00
|
|
|
|
2013-07-22 13:20:40 -05:00
|
|
|
pub unsafe fn init(argc: int, argv: **u8) {
|
|
|
|
let args = load_argc_and_argv(argc, argv);
|
|
|
|
put(args);
|
2013-06-21 03:28:23 -05:00
|
|
|
}
|
|
|
|
|
2013-07-22 13:20:40 -05:00
|
|
|
pub fn cleanup() {
|
|
|
|
rtassert!(take().is_some());
|
|
|
|
}
|
2013-06-21 03:28:23 -05:00
|
|
|
|
2013-07-22 13:20:40 -05:00
|
|
|
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())
|
|
|
|
})
|
|
|
|
}
|
2013-06-21 03:28:23 -05:00
|
|
|
|
2013-07-22 13:20:40 -05:00
|
|
|
pub fn put(args: ~[~str]) {
|
|
|
|
with_lock(|| unsafe {
|
|
|
|
let ptr = get_global_ptr();
|
|
|
|
rtassert!((*ptr).is_none());
|
|
|
|
(*ptr) = Some(~args.clone());
|
|
|
|
})
|
|
|
|
}
|
2013-06-21 03:28:23 -05:00
|
|
|
|
2013-07-22 13:20:40 -05:00
|
|
|
pub fn clone() -> Option<~[~str]> {
|
|
|
|
with_lock(|| unsafe {
|
|
|
|
let ptr = get_global_ptr();
|
|
|
|
(*ptr).map(|s: &~~[~str]| (**s).clone())
|
|
|
|
})
|
|
|
|
}
|
2013-06-21 03:28:23 -05:00
|
|
|
|
2013-07-22 13:20:40 -05:00
|
|
|
fn with_lock<T>(f: &fn() -> T) -> T {
|
2013-06-21 03:28:23 -05:00
|
|
|
do (|| {
|
2013-07-22 13:20:40 -05:00
|
|
|
unsafe {
|
|
|
|
rust_take_global_args_lock();
|
|
|
|
f()
|
|
|
|
}
|
2013-06-21 03:28:23 -05:00
|
|
|
}).finally {
|
2013-07-22 13:20:40 -05:00
|
|
|
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 => ()
|
|
|
|
}
|
2013-06-21 03:28:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 13:20:40 -05:00
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
#[cfg(target_os = "win32")]
|
|
|
|
mod imp {
|
|
|
|
|
|
|
|
pub unsafe fn init(_argc: int, _argv: **u8) {
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cleanup() {
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take() -> Option<~[~str]> {
|
|
|
|
fail!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn put(_args: ~[~str]) {
|
|
|
|
fail!()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clone() -> Option<~[~str]> {
|
|
|
|
fail!()
|
|
|
|
}
|
|
|
|
}
|