From deebdb3d6dbd3a0dc032149e75da7e66c1985321 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Mon, 11 Dec 2023 14:39:59 +0100 Subject: [PATCH 1/4] README : variable `RANDOM` dans le to-do --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5f9f82a..caf8b63 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,5 @@ - [ ] `_` - [ ] `PS1` - [ ] Positional variables + - [ ] `RANDOM` - [ ] Handle signals From 7f2755c3cb74bb15d10296cbfbf6196e4a6a2a63 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Mon, 11 Dec 2023 16:03:36 +0100 Subject: [PATCH 2/4] Gros debloatage --- src/input.rs | 10 ++++++++-- src/job.rs | 2 +- src/main.rs | 11 ++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/input.rs b/src/input.rs index 93ac102..dd60eb5 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,15 +1,20 @@ use std::io as stdio; use stdio::stdin; use crate::parser; +use crate::job; +use std::process::ExitStatus; +use std::io::Result; pub struct Buffer { buffer: String, + argv: Vec, } impl Buffer { pub fn new() -> Self { Self { buffer: String::new(), + argv: vec![], } } @@ -28,9 +33,10 @@ impl Buffer { } } - pub fn parse(&self) -> Option> { + pub fn try_spawn(&mut self) -> Option> { if ! self.buffer.is_empty() { - Some(parser::parse(&self.buffer)) + self.argv = parser::parse(&self.buffer); + Some(job::execute(&mut self.argv)) } else { None } diff --git a/src/job.rs b/src/job.rs index 028ed75..b26fbd7 100644 --- a/src/job.rs +++ b/src/job.rs @@ -8,7 +8,7 @@ use std::io::{ ErrorKind, }; -pub fn execute(mut argv: Vec) -> Result { +pub fn execute(argv: &mut Vec) -> Result { let mut command = Command::new(&argv[0]); argv.remove(0); command.args(argv); diff --git a/src/main.rs b/src/main.rs index 14b3743..455d236 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,25 +12,22 @@ mod job; fn main() { let mut buffer = input::Buffer::new(); let mut bytes_read: usize = 1; - let mut argv: Option>; - let mut exit_status: Result; + let mut exit_status: Option>; let mut exit_code: i32 = 0; while bytes_read != 0 { output::print_ps1(); bytes_read = buffer.read_line(); - argv = buffer.parse(); - if argv.is_some() { - exit_status = job::execute(argv.unwrap()); + exit_status = buffer.try_spawn(); + if exit_status.is_some() { + let exit_status = 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; } } else { exit_code = 1; From 27c6a9b71d62275bcdf2ff0659ee8938b013e1d8 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Mon, 11 Dec 2023 19:39:04 +0100 Subject: [PATCH 3/4] =?UTF-8?q?variables=20:=20ajout=20du=20support=20des?= =?UTF-8?q?=20variables,=20print=20du=20PS1=20dans=20ce=20fichier=20d?= =?UTF-8?q?=C3=A9sormais?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 14 ++++++++------ src/output.rs | 9 --------- src/variables.rs | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 15 deletions(-) delete mode 100644 src/output.rs create mode 100644 src/variables.rs diff --git a/src/main.rs b/src/main.rs index 455d236..f1433ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,24 @@ +mod input; +mod parser; +mod job; +mod variables; + use std::io::Result; use std::process::{ ExitStatus, exit, }; - -mod input; -mod output; -mod parser; -mod job; +use variables::Variables; fn main() { let mut buffer = input::Buffer::new(); let mut bytes_read: usize = 1; let mut exit_status: Option>; let mut exit_code: i32 = 0; + let mut variables = Variables::new(); while bytes_read != 0 { - output::print_ps1(); + variables.print_ps1(); bytes_read = buffer.read_line(); exit_status = buffer.try_spawn(); diff --git a/src/output.rs b/src/output.rs deleted file mode 100644 index 3888585..0000000 --- a/src/output.rs +++ /dev/null @@ -1,9 +0,0 @@ -use std::io::stdout; -use std::io::Write; - -pub fn print_ps1() { - print!("$ "); - if stdout().flush().is_err() { - eprintln!("error: can't fully flush stdout or reached EOF"); - } -} diff --git a/src/variables.rs b/src/variables.rs new file mode 100644 index 0000000..2c2af3b --- /dev/null +++ b/src/variables.rs @@ -0,0 +1,39 @@ +use std::collections::HashMap; +use std::io; +use std::io::Write; +use std::env; + +pub struct Variables { + variables: HashMap, +} + +impl Variables { + pub fn new() -> Self { + Self { + variables: HashMap::new(), + } + } + + pub fn get(&self, name: &str) -> String { + let content = env::var(name); + + if content.is_ok() { + content.unwrap() + } else { + let content = self.variables.get(name); + + if content.is_some() { + content.unwrap().to_string() + } else { + "".to_string() + } + } + } + + pub fn print_ps1(&self) { + print!("{}", self.get("PS1")); + if io::stdout().flush().is_err() { + eprintln!("error: can't fully flush stdout or reached EOF"); + } + } +} From 3a35115c6166ee5e410d1e043a650c7246b5d5dd Mon Sep 17 00:00:00 2001 From: Ahurac Date: Mon, 11 Dec 2023 19:43:10 +0100 Subject: [PATCH 4/4] README : coche support PS1 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index caf8b63..fe6cad0 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ - [ ] `exit` - [ ] Special variables - [ ] `_` - - [ ] `PS1` + - [x] `PS1` - [ ] Positional variables - [ ] `RANDOM` - [ ] Handle signals