Gestion des erreurs : erreur command not found

This commit is contained in:
Ahurac 2023-12-13 18:56:57 +01:00
parent 1554f17905
commit 62e34a9f46
3 changed files with 15 additions and 2 deletions

View file

@ -2,6 +2,7 @@ use std::io as stdio;
use stdio::stdin;
use crate::parser;
use crate::job;
use crate::error::command_not_found;
pub struct Buffer {
buffer: String,
@ -34,6 +35,7 @@ impl Buffer {
pub fn try_spawn(&mut self) -> Option<i32> {
if ! self.buffer.is_empty() {
self.argv = parser::parse(&self.buffer);
let command_name = self.argv[0].to_string();
let result = job::execute(&mut self.argv);
if result.is_ok() {
@ -43,8 +45,7 @@ impl Buffer {
return Some(result.code().unwrap());
}
} else {
eprintln!("{}", result.unwrap_err());
return Some(127);
return Some(command_not_found(&command_name));
}
}

11
src/error.rs Normal file
View file

@ -0,0 +1,11 @@
use std::env;
fn error(message: &str) {
eprintln!("{}: {}", env::args().next().unwrap(), message);
}
pub fn command_not_found(command: &String) -> i32 {
let message = command.to_owned() + ": command not found";
error(&message);
127
}

View file

@ -2,6 +2,7 @@ mod buffer;
mod parser;
mod job;
mod variables;
mod error;
use std::process::exit;
use variables::Variables;