Merge pull request 'Message d'erreur command not found' (#5) from feature/command_not_found into main

Reviewed-on: https://gitea.ahur.ac/ahurac/rash/pulls/5
This commit is contained in:
Ahurac 2023-12-12 11:03:57 +01:00
commit a48b6d734d
3 changed files with 20 additions and 22 deletions

View file

@ -2,8 +2,6 @@ use std::io as stdio;
use stdio::stdin; use stdio::stdin;
use crate::parser; use crate::parser;
use crate::job; use crate::job;
use std::process::ExitStatus;
use std::io::Result;
pub struct Buffer { pub struct Buffer {
buffer: String, buffer: String,
@ -33,12 +31,25 @@ impl Buffer {
} }
} }
pub fn try_spawn(&mut self) -> Option<Result<ExitStatus>> { pub fn try_spawn(&mut self) -> Option<i32> {
if ! self.buffer.is_empty() { if ! self.buffer.is_empty() {
self.argv = parser::parse(&self.buffer); self.argv = parser::parse(&self.buffer);
Some(job::execute(&mut self.argv)) let result = job::execute(&mut self.argv);
if result.is_ok() {
let result = result.unwrap();
if result.code().is_some() {
return Some(result.code().unwrap());
} else {
None
}
} else {
eprintln!("{}", result.unwrap_err());
return Some(127);
}
} else { } else {
None return None;
} }
} }
} }

View file

@ -18,6 +18,6 @@ pub fn execute(argv: &mut Vec<String>) -> Result<ExitStatus> {
if child.is_ok() { if child.is_ok() {
child.unwrap().wait() child.unwrap().wait()
} else { } else {
Err(Error::new(ErrorKind::Other, "failed to spawn child process")) Err(Error::new(ErrorKind::Other, "command not found"))
} }
} }

View file

@ -3,18 +3,14 @@ mod parser;
mod job; mod job;
mod variables; mod variables;
use std::io::Result; use std::process::exit;
use std::process::{
ExitStatus,
exit,
};
use variables::Variables; use variables::Variables;
use buffer::Buffer; use buffer::Buffer;
fn main() { fn main() {
let mut buffer = Buffer::new(); let mut buffer = Buffer::new();
let mut bytes_read: usize = 1; let mut bytes_read: usize = 1;
let mut exit_status: Option<Result<ExitStatus>>; let mut exit_status: Option<i32>;
let mut exit_code: i32 = 0; let mut exit_code: i32 = 0;
let mut variables = Variables::new(); let mut variables = Variables::new();
@ -25,16 +21,7 @@ fn main() {
exit_status = buffer.try_spawn(); exit_status = buffer.try_spawn();
if exit_status.is_some() { if exit_status.is_some() {
let exit_status = exit_status.unwrap(); exit_code = exit_status.unwrap();
if exit_status.is_ok() {
let exit_status = exit_status.unwrap();
if exit_status.code().is_some() {
exit_code = exit_status.code().unwrap();
}
} else {
exit_code = 1;
}
} }
} }