|
| 1 | +// Copyright 2016 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 initialization and retreival of command line arguments. |
| 12 | +//! |
| 13 | +//! On some platforms these are stored during runtime startup, |
| 14 | +//! and on some they are retrieved from the system on demand. |
| 15 | +
|
| 16 | +#![allow(dead_code)] // runtime init functions not used during testing |
| 17 | + |
| 18 | +use ffi::OsString; |
| 19 | +use marker::PhantomData; |
| 20 | +use vec; |
| 21 | + |
| 22 | +/// One-time global initialization. |
| 23 | +pub unsafe fn init(argc: isize, argv: *const *const u8) { imp::init(argc, argv) } |
| 24 | + |
| 25 | +/// One-time global cleanup. |
| 26 | +pub unsafe fn cleanup() { imp::cleanup() } |
| 27 | + |
| 28 | +/// Returns the command line arguments |
| 29 | +pub fn args() -> Args { |
| 30 | + imp::args() |
| 31 | +} |
| 32 | + |
| 33 | +pub struct Args { |
| 34 | + iter: vec::IntoIter<OsString>, |
| 35 | + _dont_send_or_sync_me: PhantomData<*mut ()>, |
| 36 | +} |
| 37 | + |
| 38 | +impl Iterator for Args { |
| 39 | + type Item = OsString; |
| 40 | + fn next(&mut self) -> Option<OsString> { self.iter.next() } |
| 41 | + fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() } |
| 42 | +} |
| 43 | + |
| 44 | +impl ExactSizeIterator for Args { |
| 45 | + fn len(&self) -> usize { self.iter.len() } |
| 46 | +} |
| 47 | + |
| 48 | +impl DoubleEndedIterator for Args { |
| 49 | + fn next_back(&mut self) -> Option<OsString> { self.iter.next_back() } |
| 50 | +} |
| 51 | + |
| 52 | +mod imp { |
| 53 | + use os::unix::prelude::*; |
| 54 | + use mem; |
| 55 | + use ffi::OsString; |
| 56 | + use marker::PhantomData; |
| 57 | + use slice; |
| 58 | + use str; |
| 59 | + use super::Args; |
| 60 | + |
| 61 | + use sys_common::mutex::Mutex; |
| 62 | + |
| 63 | + static mut GLOBAL_ARGS_PTR: usize = 0; |
| 64 | + static LOCK: Mutex = Mutex::new(); |
| 65 | + |
| 66 | + pub unsafe fn init(argc: isize, argv: *const *const u8) { |
| 67 | + let mut args: Vec<Vec<u8>> = Vec::new(); |
| 68 | + for i in 0..argc { |
| 69 | + let len = *(argv.offset(i * 2)) as usize; |
| 70 | + let ptr = *(argv.offset(i * 2 + 1)); |
| 71 | + args.push(slice::from_raw_parts(ptr, len).to_vec()); |
| 72 | + } |
| 73 | + |
| 74 | + LOCK.lock(); |
| 75 | + let ptr = get_global_ptr(); |
| 76 | + assert!((*ptr).is_none()); |
| 77 | + (*ptr) = Some(box args); |
| 78 | + LOCK.unlock(); |
| 79 | + } |
| 80 | + |
| 81 | + pub unsafe fn cleanup() { |
| 82 | + LOCK.lock(); |
| 83 | + *get_global_ptr() = None; |
| 84 | + LOCK.unlock(); |
| 85 | + } |
| 86 | + |
| 87 | + pub fn args() -> Args { |
| 88 | + let bytes = clone().unwrap_or(Vec::new()); |
| 89 | + let v: Vec<OsString> = bytes.into_iter().map(|v| { |
| 90 | + OsStringExt::from_vec(v) |
| 91 | + }).collect(); |
| 92 | + Args { iter: v.into_iter(), _dont_send_or_sync_me: PhantomData } |
| 93 | + } |
| 94 | + |
| 95 | + fn clone() -> Option<Vec<Vec<u8>>> { |
| 96 | + unsafe { |
| 97 | + LOCK.lock(); |
| 98 | + let ptr = get_global_ptr(); |
| 99 | + let ret = (*ptr).as_ref().map(|s| (**s).clone()); |
| 100 | + LOCK.unlock(); |
| 101 | + return ret |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + fn get_global_ptr() -> *mut Option<Box<Vec<Vec<u8>>>> { |
| 106 | + unsafe { mem::transmute(&GLOBAL_ARGS_PTR) } |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments