From: Ian Jackson Date: Wed, 2 Jun 2021 17:50:23 +0000 (+0100) Subject: More sophisticated handling of program name X-Git-Tag: otter-0.7.0~93 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=c5b1be346c17df4838787a2ea5fa33f478669a31;p=otter.git More sophisticated handling of program name Signed-off-by: Ian Jackson --- diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 59d3e167..10d6333b 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -8,7 +8,6 @@ pub type MgmtChannel = ClientMgmtChannel; // xxx ssh keys: need a force option to set key for non ssh: account // xxx make default account be ssh:: rather than unix:: if we are passed --ssh -// xxx make remote otter have a different error message prefix use otter::imports::*; @@ -1951,6 +1950,8 @@ mod mgmtchannel_proxy { #[throws(AE)] fn call(SCCA{ out, ma, args,.. }:SCCA) { + set_program_name("otter (remote)".into()); + let args = parse_args::(args, &subargs, &ok_id, None); let mut chan = connect_chan(&ma)?; diff --git a/src/config.rs b/src/config.rs index 4b7d1e78..b8d9d578 100644 --- a/src/config.rs +++ b/src/config.rs @@ -99,6 +99,8 @@ pub enum PathResolveContext { } impl Default for PathResolveContext { fn default() -> Self { Self::Noop } } +static PROGRAM_NAME: RwLock = parking_lot::const_rwlock(String::new()); + impl PathResolveMethod { #[throws(io::Error)] fn chdir(self, cd: &str) -> PathResolveContext { @@ -342,3 +344,26 @@ impl Default for WholeServerConfig { spec.resolve(default()).expect("empty spec into config") } } + +pub fn set_program_name(s: String) { + *PROGRAM_NAME.write() = s; +} + +pub fn program_name() -> String { + { + let set = PROGRAM_NAME.read(); + if set.len() > 0 { return set.clone() } + } + + let mut w = PROGRAM_NAME.write(); + if w.len() > 0 { return w.clone() } + + let new = env::args().next().expect("expected at least 0 arguments"); + let new = match new.rsplit_once('/') { + Some((_path,leaf)) => leaf.to_owned(), + None => new, + }; + let new = if new.len() > 0 { new } else { "otter".to_owned() }; + *w = new.clone(); + new +} diff --git a/src/utils.rs b/src/utils.rs index de0fced0..ce0b7832 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -673,18 +673,18 @@ impl anyhow::Error { fn d(&self) -> AnyhowDisplay<'_> { AnyhowDisplay(self) } fn end_process(self, estatus: u8) -> ! { - #[derive(Default,Debug)] struct Sol { any: bool } + #[derive(Default,Debug)] struct Sol { any: bool, progname: String } impl Sol { fn nl(&mut self) { if self.any { eprintln!("") }; self.any = false; } fn head(&mut self) { - if ! self.any { eprint!("otter: error"); } + if ! self.any { eprint!("{}: error", &self.progname); } self.any = true } } - let mut sol: Sol = default(); + let mut sol: Sol = Sol { any: false, progname: program_name() }; self.for_each(&mut |s|{ let long = s.len() > 80; if long && sol.any { sol.nl() }