From 62e34a9f46bce0297808a2ec835ce43e5da0219e Mon Sep 17 00:00:00 2001 From: Ahurac Date: Wed, 13 Dec 2023 18:56:57 +0100 Subject: [PATCH 1/3] Gestion des erreurs : erreur command not found --- src/buffer.rs | 5 +++-- src/error.rs | 11 +++++++++++ src/main.rs | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/error.rs diff --git a/src/buffer.rs b/src/buffer.rs index 9424734..ceab8ae 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -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 { 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)); } } diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..a693759 --- /dev/null +++ b/src/error.rs @@ -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 +} diff --git a/src/main.rs b/src/main.rs index 67b8fda..ced5470 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod buffer; mod parser; mod job; mod variables; +mod error; use std::process::exit; use variables::Variables; -- 2.45.2 From b0a89b688fb9bcee86d6d0906a36073165cfbba0 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Wed, 13 Dec 2023 19:33:18 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Gestion=20des=20erreurs=20:=20seul=20le=20n?= =?UTF-8?q?om=20du=20programme=20est=20affich=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/error.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index a693759..b51b2a3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,14 @@ use std::env; +use std::path::Path; fn error(message: &str) { - eprintln!("{}: {}", env::args().next().unwrap(), message); + let argv0: String = env::args().next().unwrap(); + let program_name = Path::new(&argv0) + .file_name() + .unwrap() + .to_str() + .unwrap(); + eprintln!("{}: {}", program_name, message); } pub fn command_not_found(command: &String) -> i32 { -- 2.45.2 From 4a244817d6e08ba7f9a154204c693f0248cd1a9b Mon Sep 17 00:00:00 2001 From: Ahurac Date: Wed, 13 Dec 2023 19:44:12 +0100 Subject: [PATCH 3/3] README : ajout command not found --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fe6cad0..515bdb7 100644 --- a/README.md +++ b/README.md @@ -21,3 +21,4 @@ - [ ] Positional variables - [ ] `RANDOM` - [ ] Handle signals +- [x] Handle when command is not found -- 2.45.2