1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Global storage for command line arguments
13 //! The current incarnation of the Rust runtime expects for
14 //! the processes `argc` and `argv` arguments to be stored
15 //! in a globally-accessible location for use by the `os` module.
17 //! Only valid to call on linux. Mac and Windows use syscalls to
18 //! discover the command line arguments.
20 //! FIXME #7756: Would be nice for this to not exist.
21 //! FIXME #7756: This has a lot of C glue for lack of globals.
24 #[cfg(test)] use option::{Some, None};
25 #[cfg(test)] use realstd;
26 #[cfg(test)] use realargs = realstd::rt::args;
28 /// One-time global initialization.
30 pub unsafe fn init(argc: int, argv: **u8) { imp::init(argc, argv) }
32 pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
34 /// One-time global cleanup.
35 #[cfg(not(test))] pub unsafe fn cleanup() { imp::cleanup() }
36 #[cfg(test)] pub unsafe fn cleanup() { realargs::cleanup() }
38 /// Take the global arguments from global storage.
39 #[cfg(not(test))] pub fn take() -> Option<~[~[u8]]> { imp::take() }
40 #[cfg(test)] pub fn take() -> Option<~[~[u8]]> {
41 match realargs::take() {
42 realstd::option::Some(a) => Some(a),
43 realstd::option::None => None,
47 /// Give the global arguments to global storage.
49 /// It is an error if the arguments already exist.
50 #[cfg(not(test))] pub fn put(args: ~[~[u8]]) { imp::put(args) }
51 #[cfg(test)] pub fn put(args: ~[~[u8]]) { realargs::put(args) }
53 /// Make a clone of the global arguments.
54 #[cfg(not(test))] pub fn clone() -> Option<~[~[u8]]> { imp::clone() }
55 #[cfg(test)] pub fn clone() -> Option<~[~[u8]]> {
56 match realargs::clone() {
57 realstd::option::Some(a) => Some(a),
58 realstd::option::None => None,
62 #[cfg(target_os = "linux")]
63 #[cfg(target_os = "android")]
64 #[cfg(target_os = "freebsd")]
68 use option::{Option, Some, None};
71 use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
74 static mut global_args_ptr: uint = 0;
75 static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
78 pub unsafe fn init(argc: int, argv: **u8) {
79 let args = load_argc_and_argv(argc, argv);
84 pub unsafe fn cleanup() {
85 rtassert!(take().is_some());
89 pub fn take() -> Option<~[~[u8]]> {
91 let ptr = get_global_ptr();
92 let val = mem::replace(&mut *ptr, None);
93 val.as_ref().map(|s: &~~[~[u8]]| (**s).clone())
97 pub fn put(args: ~[~[u8]]) {
99 let ptr = get_global_ptr();
100 rtassert!((*ptr).is_none());
101 (*ptr) = Some(~args.clone());
105 pub fn clone() -> Option<~[~[u8]]> {
106 with_lock(|| unsafe {
107 let ptr = get_global_ptr();
108 (*ptr).as_ref().map(|s: &~~[~[u8]]| (**s).clone())
112 fn with_lock<T>(f: || -> T) -> T {
114 let _guard = lock.lock();
119 fn get_global_ptr() -> *mut Option<~~[~[u8]]> {
120 unsafe { cast::transmute(&global_args_ptr) }
125 unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~[u8]] {
128 use slice::CloneableVector;
130 slice::from_fn(argc as uint, |i| {
131 let cs = CString::new(*(argv as **libc::c_char).offset(i as int), false);
132 cs.as_bytes_no_nul().to_owned()
140 use unstable::finally::Finally;
144 // Preserve the actual global state.
145 let saved_value = take();
147 let expected = ~[bytes!("happy").to_owned(), bytes!("today?").to_owned()];
149 put(expected.clone());
150 assert!(clone() == Some(expected.clone()));
151 assert!(take() == Some(expected.clone()));
152 assert!(take() == None);
156 // Restore the actual global state.
158 Some(ref args) => put(args.clone()),
166 #[cfg(target_os = "macos", not(test))]
167 #[cfg(target_os = "win32", not(test))]
171 pub unsafe fn init(_argc: int, _argv: **u8) {
177 pub fn take() -> Option<~[~[u8]]> {
181 pub fn put(_args: ~[~[u8]]) {
185 pub fn clone() -> Option<~[~[u8]]> {