14
14
//! the processes `argc` and `argv` arguments to be stored
15
15
//! in a globally-accessible location for use by the `os` module.
16
16
//!
17
+ //! Only valid to call on linux. Mac and Windows use syscalls to
18
+ //! discover the command line arguments.
19
+ //!
17
20
//! XXX: Would be nice for this to not exist.
18
21
//! XXX: This has a lot of C glue for lack of globals.
19
22
20
- use libc;
21
- use option:: { Option , Some , None } ;
22
- use str;
23
- use uint;
24
- use unstable:: finally:: Finally ;
25
- use util;
23
+ use option:: Option ;
26
24
27
25
/// One-time global initialization.
28
26
pub unsafe fn init ( argc : int , argv : * * u8 ) {
29
- let args = load_argc_and_argv ( argc, argv) ;
30
- put ( args) ;
27
+ imp:: init ( argc, argv)
31
28
}
32
29
33
30
/// One-time global cleanup.
34
31
pub fn cleanup ( ) {
35
- rtassert ! ( take ( ) . is_some ( ) ) ;
32
+ imp :: cleanup ( )
36
33
}
37
34
38
35
/// Take the global arguments from global storage.
39
36
pub fn take ( ) -> Option < ~[ ~str ] > {
40
- with_lock ( || unsafe {
41
- let ptr = get_global_ptr ( ) ;
42
- let val = util:: replace ( & mut * ptr, None ) ;
43
- val. map ( |s : & ~~[ ~str ] | ( * * s) . clone ( ) )
44
- } )
37
+ imp:: take ( )
45
38
}
46
39
47
40
/// Give the global arguments to global storage.
48
41
///
49
42
/// It is an error if the arguments already exist.
50
43
pub fn put ( args : ~[ ~str ] ) {
51
- with_lock ( || unsafe {
52
- let ptr = get_global_ptr ( ) ;
53
- rtassert ! ( ( * ptr) . is_none( ) ) ;
54
- ( * ptr) = Some ( ~args. clone ( ) ) ;
55
- } )
44
+ imp:: put ( args)
56
45
}
57
46
58
47
/// Make a clone of the global arguments.
59
48
pub fn clone ( ) -> Option < ~[ ~str ] > {
60
- with_lock ( || unsafe {
61
- let ptr = get_global_ptr ( ) ;
62
- ( * ptr) . map ( |s : & ~~[ ~str ] | ( * * s) . clone ( ) )
63
- } )
49
+ imp:: clone ( )
64
50
}
65
51
66
- fn with_lock < T > ( f : & fn ( ) -> T ) -> T {
67
- do ( || {
68
- unsafe {
69
- rust_take_global_args_lock ( ) ;
70
- f ( )
71
- }
72
- } ) . finally {
73
- unsafe {
74
- rust_drop_global_args_lock ( ) ;
75
- }
76
- }
77
- }
52
+ #[ cfg( target_os = "linux" ) ]
53
+ #[ cfg( target_os = "android" ) ]
54
+ #[ cfg( target_os = "freebsd" ) ]
55
+ mod imp {
78
56
79
- fn get_global_ptr ( ) -> * mut Option < ~~[ ~str ] > {
80
- unsafe { rust_get_global_args_ptr ( ) }
81
- }
57
+ use libc;
58
+ use option:: { Option , Some , None } ;
59
+ use str;
60
+ use uint;
61
+ use unstable:: finally:: Finally ;
62
+ use util;
82
63
83
- // Copied from `os`.
84
- unsafe fn load_argc_and_argv ( argc : int , argv : * * u8 ) -> ~[ ~str ] {
85
- let mut args = ~[ ] ;
86
- for uint:: range( 0 , argc as uint) |i| {
87
- args. push ( str:: raw:: from_c_str ( * ( argv as * * libc:: c_char ) . offset ( i) ) ) ;
64
+ pub unsafe fn init ( argc : int , argv : * * u8 ) {
65
+ let args = load_argc_and_argv ( argc, argv) ;
66
+ put ( args) ;
88
67
}
89
- return args;
90
- }
91
-
92
- extern {
93
- fn rust_take_global_args_lock ( ) ;
94
- fn rust_drop_global_args_lock ( ) ;
95
- fn rust_get_global_args_ptr ( ) -> * mut Option < ~~[ ~str ] > ;
96
- }
97
68
98
- #[ cfg( test) ]
99
- mod tests {
100
- use option:: { Some , None } ;
101
- use super :: * ;
102
- use unstable:: finally:: Finally ;
69
+ pub fn cleanup ( ) {
70
+ rtassert ! ( take( ) . is_some( ) ) ;
71
+ }
103
72
104
- #[ test]
105
- fn smoke_test ( ) {
106
- // Preserve the actual global state.
107
- let saved_value = take ( ) ;
73
+ pub fn take ( ) -> Option < ~[ ~str ] > {
74
+ with_lock ( || unsafe {
75
+ let ptr = get_global_ptr ( ) ;
76
+ let val = util:: replace ( & mut * ptr, None ) ;
77
+ val. map ( |s : & ~~[ ~str ] | ( * * s) . clone ( ) )
78
+ } )
79
+ }
108
80
109
- let expected = ~[ ~"happy", ~"today?"] ;
81
+ pub fn put ( args : ~[ ~str ] ) {
82
+ with_lock ( || unsafe {
83
+ let ptr = get_global_ptr ( ) ;
84
+ rtassert ! ( ( * ptr) . is_none( ) ) ;
85
+ ( * ptr) = Some ( ~args. clone ( ) ) ;
86
+ } )
87
+ }
110
88
111
- put ( expected. clone ( ) ) ;
112
- assert ! ( clone( ) == Some ( expected. clone( ) ) ) ;
113
- assert ! ( take( ) == Some ( expected. clone( ) ) ) ;
114
- assert ! ( take( ) == None ) ;
89
+ pub fn clone ( ) -> Option < ~[ ~str ] > {
90
+ with_lock ( || unsafe {
91
+ let ptr = get_global_ptr ( ) ;
92
+ ( * ptr) . map ( |s : & ~~[ ~str ] | ( * * s) . clone ( ) )
93
+ } )
94
+ }
115
95
96
+ fn with_lock < T > ( f : & fn ( ) -> T ) -> T {
116
97
do ( || {
98
+ unsafe {
99
+ rust_take_global_args_lock ( ) ;
100
+ f ( )
101
+ }
117
102
} ) . finally {
118
- // Restore the actual global state.
119
- match saved_value {
120
- Some ( ref args) => put ( args. clone ( ) ) ,
121
- None => ( )
103
+ unsafe {
104
+ rust_drop_global_args_lock ( ) ;
105
+ }
106
+ }
107
+ }
108
+
109
+ fn get_global_ptr ( ) -> * mut Option < ~~[ ~str ] > {
110
+ unsafe { rust_get_global_args_ptr ( ) }
111
+ }
112
+
113
+ // Copied from `os`.
114
+ unsafe fn load_argc_and_argv ( argc : int , argv : * * u8 ) -> ~[ ~str ] {
115
+ let mut args = ~[ ] ;
116
+ for uint:: range( 0 , argc as uint) |i| {
117
+ args. push ( str:: raw:: from_c_str ( * ( argv as * * libc:: c_char ) . offset ( i) ) ) ;
118
+ }
119
+ return args;
120
+ }
121
+
122
+ extern {
123
+ fn rust_take_global_args_lock ( ) ;
124
+ fn rust_drop_global_args_lock ( ) ;
125
+ fn rust_get_global_args_ptr ( ) -> * mut Option < ~~[ ~str ] > ;
126
+ }
127
+
128
+ #[ cfg( test) ]
129
+ mod tests {
130
+ use option:: { Some , None } ;
131
+ use super :: * ;
132
+ use unstable:: finally:: Finally ;
133
+
134
+ #[ test]
135
+ fn smoke_test ( ) {
136
+ // Preserve the actual global state.
137
+ let saved_value = take ( ) ;
138
+
139
+ let expected = ~[ ~"happy", ~"today?"] ;
140
+
141
+ put ( expected. clone ( ) ) ;
142
+ assert ! ( clone( ) == Some ( expected. clone( ) ) ) ;
143
+ assert ! ( take( ) == Some ( expected. clone( ) ) ) ;
144
+ assert ! ( take( ) == None ) ;
145
+
146
+ do ( || {
147
+ } ) . finally {
148
+ // Restore the actual global state.
149
+ match saved_value {
150
+ Some ( ref args) => put ( args. clone ( ) ) ,
151
+ None => ( )
152
+ }
122
153
}
123
154
}
124
155
}
125
156
}
157
+
158
+ #[ cfg( target_os = "macos" ) ]
159
+ #[ cfg( target_os = "win32" ) ]
160
+ mod imp {
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
+ }
0 commit comments