]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/args.rs
6f26e3bd9efe8d76c934b06e32a8db27772176f3
[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 //! XXX: Would be nice for this to not exist.
21 //! XXX: This has a lot of C glue for lack of globals.
22
23 use option::Option;
24
25 /// One-time global initialization.
26 pub unsafe fn init(argc: int, argv: **u8) {
27     imp::init(argc, argv)
28 }
29
30 /// One-time global cleanup.
31 pub fn cleanup() {
32     imp::cleanup()
33 }
34
35 /// Take the global arguments from global storage.
36 pub fn take() -> Option<~[~str]> {
37     imp::take()
38 }
39
40 /// Give the global arguments to global storage.
41 ///
42 /// It is an error if the arguments already exist.
43 pub fn put(args: ~[~str]) {
44     imp::put(args)
45 }
46
47 /// Make a clone of the global arguments.
48 pub fn clone() -> Option<~[~str]> {
49     imp::clone()
50 }
51
52 #[cfg(target_os = "linux")]
53 #[cfg(target_os = "android")]
54 #[cfg(target_os = "freebsd")]
55 mod imp {
56     use libc;
57     use option::{Option, Some, None};
58     use iterator::{Iterator, range};
59     use str;
60     use unstable::finally::Finally;
61     use util;
62
63     pub unsafe fn init(argc: int, argv: **u8) {
64         let args = load_argc_and_argv(argc, argv);
65         put(args);
66     }
67
68     pub fn cleanup() {
69         rtassert!(take().is_some());
70     }
71
72     pub fn take() -> Option<~[~str]> {
73         with_lock(|| unsafe {
74             let ptr = get_global_ptr();
75             let val = util::replace(&mut *ptr, None);
76             val.map(|s: &~~[~str]| (**s).clone())
77         })
78     }
79
80     pub fn put(args: ~[~str]) {
81         with_lock(|| unsafe {
82             let ptr = get_global_ptr();
83             rtassert!((*ptr).is_none());
84             (*ptr) = Some(~args.clone());
85         })
86     }
87
88     pub fn clone() -> Option<~[~str]> {
89         with_lock(|| unsafe {
90             let ptr = get_global_ptr();
91             (*ptr).map(|s: &~~[~str]| (**s).clone())
92         })
93     }
94
95     fn with_lock<T>(f: &fn() -> T) -> T {
96         do (|| {
97             unsafe {
98                 rust_take_global_args_lock();
99                 f()
100             }
101         }).finally {
102             unsafe {
103                 rust_drop_global_args_lock();
104             }
105         }
106     }
107
108     fn get_global_ptr() -> *mut Option<~~[~str]> {
109         unsafe { rust_get_global_args_ptr() }
110     }
111
112     // Copied from `os`.
113     unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~str] {
114         let mut args = ~[];
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)));
117         }
118         args
119     }
120
121     extern {
122         fn rust_take_global_args_lock();
123         fn rust_drop_global_args_lock();
124         fn rust_get_global_args_ptr() -> *mut Option<~~[~str]>;
125     }
126
127     #[cfg(test)]
128     mod tests {
129         use option::{Some, None};
130         use super::*;
131         use unstable::finally::Finally;
132
133         #[test]
134         fn smoke_test() {
135             // Preserve the actual global state.
136             let saved_value = take();
137
138             let expected = ~[~"happy", ~"today?"];
139
140             put(expected.clone());
141             assert!(clone() == Some(expected.clone()));
142             assert!(take() == Some(expected.clone()));
143             assert!(take() == None);
144
145             do (|| {
146             }).finally {
147                 // Restore the actual global state.
148                 match saved_value {
149                     Some(ref args) => put(args.clone()),
150                     None => ()
151                 }
152             }
153         }
154     }
155 }
156
157 #[cfg(target_os = "macos")]
158 #[cfg(target_os = "win32")]
159 mod imp {
160     use option::Option;
161
162     pub unsafe fn init(_argc: int, _argv: **u8) {
163     }
164
165     pub fn cleanup() {
166     }
167
168     pub fn take() -> Option<~[~str]> {
169         fail!()
170     }
171
172     pub fn put(_args: ~[~str]) {
173         fail!()
174     }
175
176     pub fn clone() -> Option<~[~str]> {
177         fail!()
178     }
179 }