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.
23 use collections::vec::Vec;
25 /// One-time global initialization.
26 pub unsafe fn init(argc: int, argv: *const *const u8) { imp::init(argc, argv) }
28 /// One-time global cleanup.
29 pub unsafe fn cleanup() { imp::cleanup() }
31 /// Take the global arguments from global storage.
32 pub fn take() -> Option<Vec<Vec<u8>>> { imp::take() }
34 /// Give the global arguments to global storage.
36 /// It is an error if the arguments already exist.
37 pub fn put(args: Vec<Vec<u8>>) { imp::put(args) }
39 /// Make a clone of the global arguments.
40 pub fn clone() -> Option<Vec<Vec<u8>>> { imp::clone() }
42 #[cfg(target_os = "linux")]
43 #[cfg(target_os = "android")]
44 #[cfg(target_os = "freebsd")]
48 use alloc::boxed::Box;
49 use collections::vec::Vec;
53 use mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
55 static mut global_args_ptr: uint = 0;
56 static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
58 pub unsafe fn init(argc: int, argv: *const *const u8) {
59 let args = load_argc_and_argv(argc, argv);
63 pub unsafe fn cleanup() {
64 rtassert!(take().is_some());
68 pub fn take() -> Option<Vec<Vec<u8>>> {
70 let ptr = get_global_ptr();
71 let val = mem::replace(&mut *ptr, None);
72 val.as_ref().map(|s: &Box<Vec<Vec<u8>>>| (**s).clone())
76 pub fn put(args: Vec<Vec<u8>>) {
78 let ptr = get_global_ptr();
79 rtassert!((*ptr).is_none());
80 (*ptr) = Some(box args.clone());
84 pub fn clone() -> Option<Vec<Vec<u8>>> {
86 let ptr = get_global_ptr();
87 (*ptr).as_ref().map(|s: &Box<Vec<Vec<u8>>>| (**s).clone())
91 fn with_lock<T>(f: || -> T) -> T {
93 let _guard = lock.lock();
98 fn get_global_ptr() -> *mut Option<Box<Vec<Vec<u8>>>> {
99 unsafe { mem::transmute(&global_args_ptr) }
102 unsafe fn load_argc_and_argv(argc: int, argv: *const *const u8) -> Vec<Vec<u8>> {
103 Vec::from_fn(argc as uint, |i| {
104 let base = *argv.offset(i as int);
106 while *base.offset(len) != 0 { len += 1; }
107 slice::raw::buf_as_slice(base, len as uint, |slice| {
108 Vec::from_slice(slice)
116 use std::finally::Finally;
122 // Preserve the actual global state.
123 let saved_value = take();
126 Vec::from_slice(b"happy"),
127 Vec::from_slice(b"today?"),
130 put(expected.clone());
131 assert!(clone() == Some(expected.clone()));
132 assert!(take() == Some(expected.clone()));
133 assert!(take() == None);
137 // Restore the actual global state.
139 Some(ref args) => put(args.clone()),
147 #[cfg(target_os = "macos")]
148 #[cfg(target_os = "ios")]
149 #[cfg(target_os = "win32")]
151 use core::prelude::*;
152 use collections::vec::Vec;
154 pub unsafe fn init(_argc: int, _argv: *const *const u8) {
160 pub fn take() -> Option<Vec<Vec<u8>>> {
164 pub fn put(_args: Vec<Vec<u8>>) {
168 pub fn clone() -> Option<Vec<Vec<u8>>> {