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 //! XXX: Would be nice for this to not exist.
21 //! XXX: This has a lot of C glue for lack of globals.
25 /// One-time global initialization.
26 pub unsafe fn init(argc: int, argv: **u8) {
30 /// One-time global cleanup.
35 /// Take the global arguments from global storage.
36 pub fn take() -> Option<~[~str]> {
40 /// Give the global arguments to global storage.
42 /// It is an error if the arguments already exist.
43 pub fn put(args: ~[~str]) {
47 /// Make a clone of the global arguments.
48 pub fn clone() -> Option<~[~str]> {
52 #[cfg(target_os = "linux")]
53 #[cfg(target_os = "android")]
54 #[cfg(target_os = "freebsd")]
57 use option::{Option, Some, None};
58 use iterator::{Iterator, range};
60 use unstable::finally::Finally;
63 pub unsafe fn init(argc: int, argv: **u8) {
64 let args = load_argc_and_argv(argc, argv);
69 rtassert!(take().is_some());
72 pub fn take() -> Option<~[~str]> {
74 let ptr = get_global_ptr();
75 let val = util::replace(&mut *ptr, None);
76 val.map(|s: &~~[~str]| (**s).clone())
80 pub fn put(args: ~[~str]) {
82 let ptr = get_global_ptr();
83 rtassert!((*ptr).is_none());
84 (*ptr) = Some(~args.clone());
88 pub fn clone() -> Option<~[~str]> {
90 let ptr = get_global_ptr();
91 (*ptr).map(|s: &~~[~str]| (**s).clone())
95 fn with_lock<T>(f: &fn() -> T) -> T {
98 rust_take_global_args_lock();
103 rust_drop_global_args_lock();
108 fn get_global_ptr() -> *mut Option<~~[~str]> {
109 unsafe { rust_get_global_args_ptr() }
113 unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~str] {
115 for i in range(0u, argc as uint) {
116 args.push(str::raw::from_c_str(*(argv as **libc::c_char).offset(i as int)));
122 fn rust_take_global_args_lock();
123 fn rust_drop_global_args_lock();
124 fn rust_get_global_args_ptr() -> *mut Option<~~[~str]>;
129 use option::{Some, None};
131 use unstable::finally::Finally;
135 // Preserve the actual global state.
136 let saved_value = take();
138 let expected = ~[~"happy", ~"today?"];
140 put(expected.clone());
141 assert!(clone() == Some(expected.clone()));
142 assert!(take() == Some(expected.clone()));
143 assert!(take() == None);
147 // Restore the actual global state.
149 Some(ref args) => put(args.clone()),
157 #[cfg(target_os = "macos")]
158 #[cfg(target_os = "win32")]
162 pub unsafe fn init(_argc: int, _argv: **u8) {
168 pub fn take() -> Option<~[~str]> {
172 pub fn put(_args: ~[~str]) {
176 pub fn clone() -> Option<~[~str]> {