init commit

This commit is contained in:
Zoritle
2021-01-18 14:26:44 +08:00
commit 60e3190020
10 changed files with 304 additions and 0 deletions

76
src/main.rs Normal file
View File

@@ -0,0 +1,76 @@
use std::{
env,
process::{exit, Command},
};
use winapi::{
shared::minwindef::{BOOL, DWORD, FALSE, TRUE},
um::{consoleapi, wincon},
};
unsafe extern "system" fn routine_handler(evt: DWORD) -> BOOL {
match evt {
wincon::CTRL_C_EVENT => TRUE, //eprintln!("ctrl_c handled!"),
wincon::CTRL_BREAK_EVENT => TRUE, //eprintln!("ctrl_break handled!"),
wincon::CTRL_CLOSE_EVENT => TRUE, //eprintln!("ctrl_close handled!"),
wincon::CTRL_LOGOFF_EVENT => TRUE, //eprintln!("ctrl_logoff handled!"),
wincon::CTRL_SHUTDOWN_EVENT => TRUE, //eprintln!("ctrl_shutdown handled!"),
other => {
eprintln!("unknown event number: {}, unhandled!", other);
return FALSE;
}
}
}
mod shims;
use shims::Shim;
const EXIT_FAILED_LOAD_SHIM: i32 = 1;
const EXIT_FAILED_SPAWN_PROG: i32 = 2;
const EXIT_FAILED_WAIT_PROG: i32 = 3;
const EXIT_PROG_TERMINATED: i32 = 4;
fn main() {
let res: BOOL = unsafe { consoleapi::SetConsoleCtrlHandler(Some(routine_handler), TRUE) };
if res == FALSE {
eprintln!("shim: register Ctrl handler failed.");
}
let calling_args: Vec<_> = env::args().skip(1).collect();
let shim = match Shim::init() {
Ok(v) => v,
Err(e) => {
eprintln!("Error while loading shim: {}", e);
exit(EXIT_FAILED_LOAD_SHIM);
}
};
let args = if let Some(mut shim_args) = shim.args {
shim_args.extend_from_slice(calling_args.as_slice());
shim_args
} else {
calling_args
};
let mut cmd = match Command::new(&shim.target_path).args(&args).spawn() {
Ok(v) => v,
Err(e) => {
eprintln!(
"Error while spawning target program `{}`: {}",
shim.target_path.to_string_lossy(),
e
);
exit(EXIT_FAILED_SPAWN_PROG);
}
};
let status = match cmd.wait() {
Ok(v) => v,
Err(e) => {
eprintln!(
"Error while waiting target program `{}`: {}",
shim.target_path.to_string_lossy(),
e
);
exit(EXIT_FAILED_WAIT_PROG);
}
};
exit(status.code().unwrap_or(EXIT_PROG_TERMINATED))
}

89
src/shims.rs Normal file
View File

@@ -0,0 +1,89 @@
use fs_err as fs;
use std::{
collections::HashMap,
env,
io::{Error, ErrorKind},
path::{Path, PathBuf},
};
pub struct Shim {
pub target_path: PathBuf,
pub args: Option<Vec<String>>,
}
impl Shim {
pub fn init() -> Result<Self, Error> {
let shim_path = get_shim_file_path()?;
let kvs = parse_shim_file(&shim_path)?;
let target_path = match kvs.get("path") {
Some(p) => PathBuf::from(p),
None => {
return Err(Error::new(
ErrorKind::NotFound,
format!("no path key in {}", shim_path.to_string_lossy()),
))
}
};
let args = kvs.get("args").map(|a| {
a.split_whitespace()
.map(|s| s.to_string())
.collect::<Vec<_>>()
});
Ok(Self { target_path, args })
}
}
fn get_shim_file_path() -> Result<PathBuf, Error> {
let mut current_exe = env::current_exe().map_err(|e| {
Error::new(
ErrorKind::Other,
format!("acquiring shim executable path: {}", e),
)
})?;
if !current_exe.set_extension("shim") {
return Err(Error::new(
ErrorKind::Other,
format!("{} is not a file", current_exe.to_string_lossy()),
));
}
Ok(current_exe)
}
use unicode_bom::Bom;
fn parse_shim_file(shim_path: &Path) -> Result<HashMap<String, String>, Error> {
let mut kvs = HashMap::new();
let raw_content = fs::read_to_string(shim_path).map_err(|e| {
Error::new(
ErrorKind::Other,
format!("reading {}: {}", shim_path.to_string_lossy(), e),
)
})?;
//NOTE: expedient trick for utf-8 with bom
let bom = Bom::from(raw_content.as_bytes());
for line in raw_content[bom.len()..]
.lines()
.filter(|l| !l.trim().is_empty())
{
let mut components = line.split("=");
let key = match components.next() {
Some(k) => k.trim(),
None => {
return Err(Error::new(
ErrorKind::InvalidData,
format!("invalid line in shim file: {}", line),
));
}
};
let value = match components.next() {
Some(v) => v.trim(),
None => {
return Err(Error::new(
ErrorKind::InvalidData,
format!("invalid line in shim file: {}", line),
));
}
};
kvs.insert(key.to_string(), value.to_string());
}
Ok(kvs)
}