diff options
author | Jesse Luehrs <doy@tozt.net> | 2022-01-08 01:06:15 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2022-01-08 01:06:15 -0500 |
commit | 49c8dadc0c44a3000d2a3f53dd3e06539e797747 (patch) | |
tree | f82bc4570d7a825ceff4986613e78be1753a288f | |
parent | 9f7e4412f334dca01f19d275af6a3eaa5b449dba (diff) | |
download | nbsh-49c8dadc0c44a3000d2a3f53dd3e06539e797747.tar.gz nbsh-49c8dadc0c44a3000d2a3f53dd3e06539e797747.zip |
add support for special env vars
-rw-r--r-- | src/env.rs | 23 | ||||
-rw-r--r-- | src/info.rs | 4 |
2 files changed, 25 insertions, 2 deletions
@@ -87,12 +87,12 @@ impl Env { pub fn var(&self, k: &str) -> String { match self { - Self::V0(env) => { + Self::V0(env) => self.special_var(k).unwrap_or_else(|| { env.vars.get(std::ffi::OsStr::new(k)).map_or_else( || "".to_string(), |v| v.to_str().unwrap().to_string(), ) - } + }), } } @@ -118,6 +118,25 @@ impl Env { pub fn from_bytes(bytes: &[u8]) -> Self { bincode::deserialize(bytes).unwrap() } + + fn special_var(&self, k: &str) -> Option<String> { + match self { + Self::V0(env) => Some(match k { + "$" => crate::info::pid(), + "?" => { + let status = env.latest_status; + status + .signal() + .map_or_else( + || status.code().unwrap(), + |signal| signal + 128, + ) + .to_string() + } + _ => return None, + }), + } + } } #[allow(clippy::trivially_copy_pass_by_ref)] diff --git a/src/info.rs b/src/info.rs index e441b16..bd94205 100644 --- a/src/info.rs +++ b/src/info.rs @@ -29,6 +29,10 @@ pub fn time(offset: time::UtcOffset) -> anyhow::Result<String> { )) } +pub fn pid() -> String { + nix::unistd::getpid().to_string() +} + // the time crate is currently unable to get the local offset on unix due to // soundness concerns, so we have to do it manually/: // |