]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/args.rs
Add externfn macro and correctly label fixed_stack_segments
[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     #[cfg(stage0)]
122     mod macro_hack {
123     #[macro_escape];
124     macro_rules! externfn(
125         (fn $name:ident () $(-> $ret_ty:ty),*) => (
126             extern {
127                 fn $name() $(-> $ret_ty),*;
128             }
129         )
130     )
131     }
132
133     externfn!(fn rust_take_global_args_lock())
134     externfn!(fn rust_drop_global_args_lock())
135     externfn!(fn rust_get_global_args_ptr() -> *mut Option<~~[~str]>)
136
137     #[cfg(test)]
138     mod tests {
139         use option::{Some, None};
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 = ~[~"happy", ~"today?"];
149
150             put(expected.clone());
151             assert!(clone() == Some(expected.clone()));
152             assert!(take() == Some(expected.clone()));
153             assert!(take() == None);
154
155             do (|| {
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")]
168 #[cfg(target_os = "win32")]
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<~[~str]> {
179         fail!()
180     }
181
182     pub fn put(_args: ~[~str]) {
183         fail!()
184     }
185
186     pub fn clone() -> Option<~[~str]> {
187         fail!()
188     }
189 }