Compare commits
No commits in common. "ea9eb49697a12b698b6dce8cadc3f24ae3a3147c" and "1de0e1674e88b348d4b349a46635674e8eb58ee9" have entirely different histories.
ea9eb49697
...
1de0e1674e
4 changed files with 18 additions and 33 deletions
|
@ -1,23 +1,17 @@
|
||||||
use crate::error;
|
|
||||||
|
|
||||||
pub struct ExitCode {
|
pub struct ExitCode {
|
||||||
exit_code: u8,
|
exit_code: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ExitCode {
|
|
||||||
pub fn new(exit_code: u8) -> Self {
|
|
||||||
Self { exit_code }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get(&self) -> u8 {
|
|
||||||
self.exit_code
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Command {
|
pub trait Command {
|
||||||
fn spawn(&self) -> ExitCode;
|
fn spawn(&self) -> ExitCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ExitCode {
|
||||||
|
fn new(exit_code: u8) -> Self {
|
||||||
|
Self { exit_code }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct UnixProgram {
|
pub struct UnixProgram {
|
||||||
argv: Vec<String>,
|
argv: Vec<String>,
|
||||||
}
|
}
|
||||||
|
@ -38,18 +32,21 @@ impl Command for UnixProgram {
|
||||||
let mut argv = self.argv.clone();
|
let mut argv = self.argv.clone();
|
||||||
let program = argv.remove(0);
|
let program = argv.remove(0);
|
||||||
|
|
||||||
let mut command = std::process::Command::new(&program);
|
let mut command = std::process::Command::new(program);
|
||||||
command.args(argv);
|
command.args(argv);
|
||||||
|
|
||||||
let handle = command.spawn();
|
let handle = command.spawn();
|
||||||
|
|
||||||
if handle.is_ok() {
|
if handle.is_ok() {
|
||||||
let exit_code = handle
|
let exit_code = match handle
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.wait()
|
.wait()
|
||||||
.expect("error waiting for the child")
|
.expect("error waiting for the child")
|
||||||
.code()
|
.code()
|
||||||
.unwrap_or(1);
|
{
|
||||||
|
Some(code) => code,
|
||||||
|
None => 1,
|
||||||
|
};
|
||||||
|
|
||||||
let exit_code = match u8::try_from(exit_code) {
|
let exit_code = match u8::try_from(exit_code) {
|
||||||
Ok(code) => ExitCode::new(code),
|
Ok(code) => ExitCode::new(code),
|
||||||
|
@ -58,7 +55,6 @@ impl Command for UnixProgram {
|
||||||
|
|
||||||
exit_code
|
exit_code
|
||||||
} else {
|
} else {
|
||||||
error::print_error(format!("{}: command not found", program));
|
|
||||||
ExitCode::new(127)
|
ExitCode::new(127)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
use crate::command::{Command, ExitCode, UnixProgram};
|
use crate::command::Command;
|
||||||
|
use crate::command::UnixProgram;
|
||||||
use crate::interface::{get_user_input, print_prompt};
|
use crate::interface::{get_user_input, print_prompt};
|
||||||
|
|
||||||
fn exit(code: &ExitCode) {
|
fn exit() {
|
||||||
let code = i32::from(code.get());
|
|
||||||
println!("exit");
|
println!("exit");
|
||||||
std::process::exit(code);
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
let mut current_exit_code = ExitCode::new(0);
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
print_prompt();
|
print_prompt();
|
||||||
|
|
||||||
|
@ -24,11 +22,11 @@ pub fn run() {
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
if !argv.is_empty() {
|
if !argv.is_empty() {
|
||||||
current_exit_code = UnixProgram::new().argv(argv).spawn();
|
UnixProgram::new().argv(argv).spawn();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println!();
|
println!();
|
||||||
exit(¤t_exit_code);
|
exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
pub fn print_error(message: String) {
|
|
||||||
let name = std::env::args().next().unwrap();
|
|
||||||
let name = Path::new(&name).file_name().unwrap().to_str().unwrap();
|
|
||||||
|
|
||||||
eprintln!("{}: {}", name, message);
|
|
||||||
}
|
|
|
@ -1,6 +1,5 @@
|
||||||
mod command;
|
mod command;
|
||||||
mod control;
|
mod control;
|
||||||
mod error;
|
|
||||||
mod interface;
|
mod interface;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
Loading…
Reference in a new issue