]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/args.rs
comm: Implement synchronous channels
[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     #[cfg(not(test))] use ptr::RawPtr;
73
74     static mut global_args_ptr: uint = 0;
75     static mut lock: StaticNativeMutex = NATIVE_MUTEX_INIT;
76
77     #[cfg(not(test))]
78     pub unsafe fn init(argc: int, argv: **u8) {
79         let args = load_argc_and_argv(argc, argv);
80         put(args);
81     }
82
83     #[cfg(not(test))]
84     pub unsafe fn cleanup() {
85         rtassert!(take().is_some());
86         lock.destroy();
87     }
88
89     pub fn take() -> Option<~[~[u8]]> {
90         with_lock(|| unsafe {
91             let ptr = get_global_ptr();
92             let val = mem::replace(&mut *ptr, None);
93             val.as_ref().map(|s: &~~[~[u8]]| (**s).clone())
94         })
95     }
96
97     pub fn put(args: ~[~[u8]]) {
98         with_lock(|| unsafe {
99             let ptr = get_global_ptr();
100             rtassert!((*ptr).is_none());
101             (*ptr) = Some(~args.clone());
102         })
103     }
104
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())
109         })
110     }
111
112     fn with_lock<T>(f: || -> T) -> T {
113         unsafe {
114             let _guard = lock.lock();
115             f()
116         }
117     }
118
119     fn get_global_ptr() -> *mut Option<~~[~[u8]]> {
120         unsafe { cast::transmute(&global_args_ptr) }
121     }
122
123     // Copied from `os`.
124     #[cfg(not(test))]
125     unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~[u8]] {
126         use c_str::CString;
127         use ptr::RawPtr;
128         use {slice, libc};
129         use slice::CloneableVector;
130
131         slice::from_fn(argc as uint, |i| {
132             let cs = CString::new(*(argv as **libc::c_char).offset(i as int), false);
133             cs.as_bytes_no_nul().to_owned()
134         })
135     }
136
137     #[cfg(test)]
138     mod tests {
139         use prelude::*;
140         use super::*;
141         use unstable::finally::Finally;
142
143         #[test]
144         fn smoke_test() {
145             // Preserve the actual global state.
146             let saved_value = take();
147
148             let expected = ~[bytes!("happy").to_owned(), bytes!("today?").to_owned()];
149
150             put(expected.clone());
151             assert!(clone() == Some(expected.clone()));
152             assert!(take() == Some(expected.clone()));
153             assert!(take() == None);
154
155             (|| {
156             }).finally(|| {
157                 // Restore the actual global state.
158                 match saved_value {
159                     Some(ref args) => put(args.clone()),
160                     None => ()
161                 }
162             })
163         }
164     }
165 }
166
167 #[cfg(target_os = "macos", not(test))]
168 #[cfg(target_os = "win32", not(test))]
169 mod imp {
170     use option::Option;
171
172     pub unsafe fn init(_argc: int, _argv: **u8) {
173     }
174
175     pub fn cleanup() {
176     }
177
178     pub fn take() -> Option<~[~[u8]]> {
179         fail!()
180     }
181
182     pub fn put(_args: ~[~[u8]]) {
183         fail!()
184     }
185
186     pub fn clone() -> Option<~[~[u8]]> {
187         fail!()
188     }
189 }