]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/args.rs
fc9e571b270596ae8ed3630f186dd157475d0370
[rust.git] / src / libstd / rt / args.rs
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.
4 //
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.
10
11 //! Global storage for command line arguments
12 //!
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.
16 //!
17 //! Only valid to call on linux. Mac and Windows use syscalls to
18 //! discover the command line arguments.
19 //!
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.
22
23 use option::Option;
24 #[cfg(test)] use option::{Some, None};
25 #[cfg(test)] use realstd;
26 #[cfg(test)] use realargs = realstd::rt::args;
27
28 /// One-time global initialization.
29 #[cfg(not(test))]
30 pub unsafe fn init(argc: int, argv: **u8) { imp::init(argc, argv) }
31 #[cfg(test)]
32 pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
33
34 /// One-time global cleanup.
35 #[cfg(not(test))] pub unsafe fn cleanup() { imp::cleanup() }
36 #[cfg(test)]      pub unsafe fn cleanup() { realargs::cleanup() }
37
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,
44     }
45 }
46
47 /// Give the global arguments to global storage.
48 ///
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) }
52
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,
59     }
60 }
61
62 #[cfg(target_os = "linux")]
63 #[cfg(target_os = "android")]
64 #[cfg(target_os = "freebsd")]
65 mod imp {
66     use cast;
67     use clone::Clone;
68     use option::{Option, Some, None};
69     use iter::Iterator;
70     use unstable::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
71     use mem;
72
73     static mut global_args_ptr: uint = 0;
74     static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
75
76     #[cfg(not(test))]
77     pub unsafe fn init(argc: int, argv: **u8) {
78         let args = load_argc_and_argv(argc, argv);
79         put(args);
80     }
81
82     #[cfg(not(test))]
83     pub unsafe fn cleanup() {
84         rtassert!(take().is_some());
85         lock.destroy();
86     }
87
88     pub fn take() -> Option<~[~[u8]]> {
89         with_lock(|| unsafe {
90             let ptr = get_global_ptr();
91             let val = mem::replace(&mut *ptr, None);
92             val.as_ref().map(|s: &~~[~[u8]]| (**s).clone())
93         })
94     }
95
96     pub fn put(args: ~[~[u8]]) {
97         with_lock(|| unsafe {
98             let ptr = get_global_ptr();
99             rtassert!((*ptr).is_none());
100             (*ptr) = Some(~args.clone());
101         })
102     }
103
104     pub fn clone() -> Option<~[~[u8]]> {
105         with_lock(|| unsafe {
106             let ptr = get_global_ptr();
107             (*ptr).as_ref().map(|s: &~~[~[u8]]| (**s).clone())
108         })
109     }
110
111     fn with_lock<T>(f: || -> T) -> T {
112         unsafe {
113             let _guard = lock.lock();
114             f()
115         }
116     }
117
118     fn get_global_ptr() -> *mut Option<~~[~[u8]]> {
119         unsafe { cast::transmute(&global_args_ptr) }
120     }
121
122     // Copied from `os`.
123     #[cfg(not(test))]
124     unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~[u8]] {
125         use c_str::CString;
126         use ptr::RawPtr;
127         use {slice, libc};
128         use slice::CloneableVector;
129
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()
133         })
134     }
135
136     #[cfg(test)]
137     mod tests {
138         use prelude::*;
139         use super::*;
140         use unstable::finally::Finally;
141
142         #[test]
143         fn smoke_test() {
144             // Preserve the actual global state.
145             let saved_value = take();
146
147             let expected = ~[bytes!("happy").to_owned(), bytes!("today?").to_owned()];
148
149             put(expected.clone());
150             assert!(clone() == Some(expected.clone()));
151             assert!(take() == Some(expected.clone()));
152             assert!(take() == None);
153
154             (|| {
155             }).finally(|| {
156                 // Restore the actual global state.
157                 match saved_value {
158                     Some(ref args) => put(args.clone()),
159                     None => ()
160                 }
161             })
162         }
163     }
164 }
165
166 #[cfg(target_os = "macos", not(test))]
167 #[cfg(target_os = "win32", not(test))]
168 mod imp {
169     use option::Option;
170
171     pub unsafe fn init(_argc: int, _argv: **u8) {
172     }
173
174     pub fn cleanup() {
175     }
176
177     pub fn take() -> Option<~[~[u8]]> {
178         fail!()
179     }
180
181     pub fn put(_args: ~[~[u8]]) {
182         fail!()
183     }
184
185     pub fn clone() -> Option<~[~[u8]]> {
186         fail!()
187     }
188 }