1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Inspection and manipulation of the program’s environment.

use core::{
    ffi::{c_char, CStr},
    fmt::Debug,
    ops::Deref,
};

/// An argument passed to this program.
#[repr(transparent)]
pub struct Arg(*const c_char);

impl Debug for Arg {
    #[inline]
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.deref().fmt(f)
    }
}

impl From<&'static CStr> for Arg {
    #[inline]
    fn from(arg: &'static CStr) -> Self {
        Self(arg.as_ptr())
    }
}

impl Deref for Arg {
    type Target = CStr;
    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe { CStr::from_ptr(self.0) }
    }
}

static mut ARGV: &'static [Arg] = &[];

/// Returns the arguments that this program was started with (normally passed
/// via `exec` or ckb-debugger).
///
/// (Not to be confused with **script args** when used as cell lock script or
/// type script. That has be loaded with `load_script`.)
#[inline]
pub fn argv() -> &'static [Arg] {
    unsafe { ARGV }
}

// For simulator and entry!.
#[doc(hidden)]
#[inline]
pub unsafe fn set_argv(argv: &'static [Arg]) {
    ARGV = argv
}