main.rs
97 lines
| 2.9 KiB
| application/rls-services+xml
|
RustLexer
|
r40003 | // Copyright 2018 Yuya Nishihara <yuya@tcha.org> | ||
// | ||||
// This software may be used and distributed according to the terms of the | ||||
// GNU General Public License version 2 or any later version. | ||||
|
r45172 | use chg::locator::{self, Locator}; | ||
|
r40156 | use chg::procutil; | ||
|
r44270 | use chg::{ChgClientExt, ChgUiHandler}; | ||
|
r40015 | use futures::sync::oneshot; | ||
use std::env; | ||||
use std::io; | ||||
use std::process; | ||||
|
r40324 | use std::time::Instant; | ||
|
r40015 | use tokio::prelude::*; | ||
|
r40324 | struct DebugLogger { | ||
start: Instant, | ||||
} | ||||
impl DebugLogger { | ||||
pub fn new() -> DebugLogger { | ||||
DebugLogger { | ||||
start: Instant::now(), | ||||
} | ||||
} | ||||
} | ||||
impl log::Log for DebugLogger { | ||||
fn enabled(&self, metadata: &log::Metadata) -> bool { | ||||
metadata.target().starts_with("chg::") | ||||
} | ||||
fn log(&self, record: &log::Record) { | ||||
if self.enabled(record.metadata()) { | ||||
// just make the output looks similar to chg of C | ||||
let l = format!("{}", record.level()).to_lowercase(); | ||||
let t = self.start.elapsed(); | ||||
|
r44270 | writeln!( | ||
io::stderr(), | ||||
"chg: {}: {}.{:06} {}", | ||||
l, | ||||
t.as_secs(), | ||||
t.subsec_micros(), | ||||
record.args() | ||||
) | ||||
.unwrap_or(()); | ||||
|
r40324 | } | ||
} | ||||
|
r44270 | fn flush(&self) {} | ||
|
r40324 | } | ||
|
r40003 | fn main() { | ||
|
r40324 | if env::var_os("CHGDEBUG").is_some() { | ||
log::set_boxed_logger(Box::new(DebugLogger::new())) | ||||
.expect("any logger should not be installed yet"); | ||||
log::set_max_level(log::LevelFilter::Debug); | ||||
} | ||||
|
r45161 | // TODO: add loop detection by $CHGINTERNALMARK | ||
|
r45184 | let umask = unsafe { procutil::get_umask() }; // not thread safe | ||
let code = run(umask).unwrap_or_else(|err| { | ||||
|
r40322 | writeln!(io::stderr(), "chg: abort: {}", err).unwrap_or(()); | ||
|
r40015 | 255 | ||
}); | ||||
process::exit(code); | ||||
|
r40003 | } | ||
|
r40015 | |||
|
r45184 | fn run(umask: u32) -> io::Result<i32> { | ||
|
r45172 | let mut loc = Locator::prepare_from_env()?; | ||
loc.set_early_args(locator::collect_early_args(env::args_os().skip(1))); | ||||
|
r40015 | let handler = ChgUiHandler::new(); | ||
let (result_tx, result_rx) = oneshot::channel(); | ||||
|
r45161 | let fut = loc | ||
.connect() | ||||
|
r45163 | .and_then(|(_, client)| client.attach_io(io::stdin(), io::stdout(), io::stderr())) | ||
|
r45174 | .and_then(move |client| client.set_umask(umask)) | ||
|
r40015 | .and_then(|client| { | ||
|
r40156 | let pid = client.server_spec().process_id.unwrap(); | ||
let pgid = client.server_spec().process_group_id; | ||||
procutil::setup_signal_handler_once(pid, pgid)?; | ||||
Ok(client) | ||||
}) | ||||
|
r44270 | .and_then(|client| client.run_command_chg(handler, env::args_os().skip(1))) | ||
|
r40015 | .map(|(_client, _handler, code)| { | ||
|
r40156 | procutil::restore_signal_handler_once()?; | ||
|
r40015 | Ok(code) | ||
}) | ||||
|
r44270 | .or_else(|err| Ok(Err(err))) // pass back error to caller | ||
|
r40015 | .map(|res| result_tx.send(res).unwrap()); | ||
tokio::run(fut); | ||||
|
r44270 | result_rx.wait().unwrap_or(Err(io::Error::new( | ||
io::ErrorKind::Other, | ||||
"no exit code set", | ||||
))) | ||||
|
r40015 | } | ||