From f4fb13c96e1bda50c8cac2a2ede79f7afd0599df Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 13:17:54 +0100 Subject: [PATCH 01/13] Refactoring de la variable result_bytes_read dans main.rs --- src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index d2220e6..94ef145 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ mod parser; fn main() { let mut buffer = String::new(); - let mut result_bytes_read: io::Result; + let mut bytes_read_result: io::Result; let mut bytes_read: usize = 1; while bytes_read != 0 { @@ -15,13 +15,13 @@ fn main() { if io::stdout().flush().is_err() { eprintln!("error: can't fully flush stdout or reached EOF"); } - result_bytes_read = io::stdin().read_line(&mut buffer); + bytes_read_result = io::stdin().read_line(&mut buffer); - if result_bytes_read.is_err() { + if bytes_read_result.is_err() { eprintln!("error: invalid UTF-8 characters were read"); bytes_read = 1; } else { - bytes_read = result_bytes_read.unwrap(); + bytes_read = bytes_read_result.unwrap(); if bytes_read != 0 { let command_vec: Vec = parser::parse(&buffer); println!("{:?}", command_vec); From c972102a38a24bf281e89756d66c5d20fb033a74 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 13:34:33 +0100 Subject: [PATCH 02/13] Ajout : module io, modification des use --- src/io.rs | 3 +++ src/main.rs | 16 +++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 src/io.rs diff --git a/src/io.rs b/src/io.rs new file mode 100644 index 0000000..4c95642 --- /dev/null +++ b/src/io.rs @@ -0,0 +1,3 @@ +pub fn run() -> i32 { + todo!(); +} diff --git a/src/main.rs b/src/main.rs index 94ef145..364adc2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,27 @@ -use std::io; -use std::io::Write; +use std::io as stdio; +use stdio::{ + stdin, + stdout, + Write, +}; +use std::process::exit; mod parser; +mod io; fn main() { let mut buffer = String::new(); - let mut bytes_read_result: io::Result; + let mut bytes_read_result: stdio::Result; let mut bytes_read: usize = 1; while bytes_read != 0 { buffer.clear(); print!("$ "); - if io::stdout().flush().is_err() { + if stdout().flush().is_err() { eprintln!("error: can't fully flush stdout or reached EOF"); } - bytes_read_result = io::stdin().read_line(&mut buffer); + bytes_read_result = stdin().read_line(&mut buffer); if bytes_read_result.is_err() { eprintln!("error: invalid UTF-8 characters were read"); From 7dc3f6277d936549ca4173865c6c9032d0e1ce01 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 13:34:59 +0100 Subject: [PATCH 03/13] Utilisation du nouveau module io --- src/main.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main.rs b/src/main.rs index 364adc2..14c038f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,10 @@ fn main() { let mut bytes_read_result: stdio::Result; let mut bytes_read: usize = 1; + let last_exit_code: i32 = io::run(); + + exit(last_exit_code); + while bytes_read != 0 { buffer.clear(); From fc57c7f0d886471bb8b84492c5a304a470eec13b Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 13:36:24 +0100 Subject: [PATCH 04/13] Ajout des fichiers in et out dans le module io --- src/io/in.rs | 0 src/{io.rs => io/mod.rs} | 0 src/io/out.rs | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/io/in.rs rename src/{io.rs => io/mod.rs} (100%) create mode 100644 src/io/out.rs diff --git a/src/io/in.rs b/src/io/in.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/io.rs b/src/io/mod.rs similarity index 100% rename from src/io.rs rename to src/io/mod.rs diff --git a/src/io/out.rs b/src/io/out.rs new file mode 100644 index 0000000..e69de29 From 80e80fab8d28f6843d738b5d80a567b2bc0f2686 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 13:37:15 +0100 Subject: [PATCH 05/13] Passage du module parser en mode dossier --- src/{parser.rs => parser/mod.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{parser.rs => parser/mod.rs} (100%) diff --git a/src/parser.rs b/src/parser/mod.rs similarity index 100% rename from src/parser.rs rename to src/parser/mod.rs From 03d44852c373f0078e8faf45f49ca4fca8a1a191 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 13:48:25 +0100 Subject: [PATCH 06/13] Grand remaniement du code en modules partie 1 --- src/io/mod.rs | 38 +++++++++++++++++++++++++++++++++++++- src/{ => io}/parser/mod.rs | 0 src/main.rs | 34 ---------------------------------- 3 files changed, 37 insertions(+), 35 deletions(-) rename src/{ => io}/parser/mod.rs (100%) diff --git a/src/io/mod.rs b/src/io/mod.rs index 4c95642..dbbb0a4 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -1,3 +1,39 @@ +use std::io; +use io::{ + stdin, + stdout, + Write, +}; + +mod parser; + pub fn run() -> i32 { - todo!(); + let mut buffer = String::new(); + let mut bytes_read_result: io::Result; + let mut bytes_read: usize = 1; + + while bytes_read != 0 { + buffer.clear(); + + print!("$ "); + if stdout().flush().is_err() { + eprintln!("error: can't fully flush stdout or reached EOF"); + } + bytes_read_result = stdin().read_line(&mut buffer); + + if bytes_read_result.is_err() { + eprintln!("error: invalid UTF-8 characters were read"); + bytes_read = 1; + } else { + bytes_read = bytes_read_result.unwrap(); + if bytes_read != 0 { + let command_vec: Vec = parser::parse(&buffer); + println!("{:?}", command_vec); + } + } + } + + println!(); + + 0 } diff --git a/src/parser/mod.rs b/src/io/parser/mod.rs similarity index 100% rename from src/parser/mod.rs rename to src/io/parser/mod.rs diff --git a/src/main.rs b/src/main.rs index 14c038f..c05e11f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,43 +1,9 @@ -use std::io as stdio; -use stdio::{ - stdin, - stdout, - Write, -}; use std::process::exit; -mod parser; mod io; fn main() { - let mut buffer = String::new(); - let mut bytes_read_result: stdio::Result; - let mut bytes_read: usize = 1; - let last_exit_code: i32 = io::run(); exit(last_exit_code); - - while bytes_read != 0 { - buffer.clear(); - - print!("$ "); - if stdout().flush().is_err() { - eprintln!("error: can't fully flush stdout or reached EOF"); - } - bytes_read_result = stdin().read_line(&mut buffer); - - if bytes_read_result.is_err() { - eprintln!("error: invalid UTF-8 characters were read"); - bytes_read = 1; - } else { - bytes_read = bytes_read_result.unwrap(); - if bytes_read != 0 { - let command_vec: Vec = parser::parse(&buffer); - println!("{:?}", command_vec); - } - } - } - - println!(); } From d8fb7a5478fa4ac5d1bfb0ff35e089321bc851ed Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 13:50:20 +0100 Subject: [PATCH 07/13] Importer les modules in.rs et out.rs dans io/mod.rs --- src/io/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/io/mod.rs b/src/io/mod.rs index dbbb0a4..7b8987e 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -5,6 +5,9 @@ use io::{ Write, }; +mod r#in; +mod r#out; + mod parser; pub fn run() -> i32 { From 6ca8606081cd61a57dc310722dcbf006e4fa5f19 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 14:10:45 +0100 Subject: [PATCH 08/13] =?UTF-8?q?Impl=C3=A9mentation=20de=20Buffer=20et=20?= =?UTF-8?q?utilisation=20dans=20io/mod.rs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/io/in.rs | 24 ++++++++++++++++++++++++ src/io/mod.rs | 21 +++++---------------- src/io/out.rs | 9 +++++++++ 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/io/in.rs b/src/io/in.rs index e69de29..ba176e5 100644 --- a/src/io/in.rs +++ b/src/io/in.rs @@ -0,0 +1,24 @@ +use std::io as stdio; +use stdio::stdin; + +pub struct Buffer { + buffer: String, +} + +impl Buffer { + pub fn new() -> Self { + Self { + buffer: String::new(), + } + } + + pub fn read_line(&mut self) -> stdio::Result { + self.buffer.clear(); + let result: stdio::Result = stdin().read_line(&mut self.buffer); + if result.is_err() { + eprintln!("error: invalid UTF-8 characters were read"); + } + + result + } +} diff --git a/src/io/mod.rs b/src/io/mod.rs index 7b8987e..e706f45 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -1,9 +1,4 @@ use std::io; -use io::{ - stdin, - stdout, - Write, -}; mod r#in; mod r#out; @@ -11,27 +6,21 @@ mod r#out; mod parser; pub fn run() -> i32 { - let mut buffer = String::new(); + let mut buffer = r#in::Buffer::new(); let mut bytes_read_result: io::Result; let mut bytes_read: usize = 1; while bytes_read != 0 { - buffer.clear(); - - print!("$ "); - if stdout().flush().is_err() { - eprintln!("error: can't fully flush stdout or reached EOF"); - } - bytes_read_result = stdin().read_line(&mut buffer); + out::print_ps1(); + bytes_read_result = buffer.read_line(); if bytes_read_result.is_err() { - eprintln!("error: invalid UTF-8 characters were read"); bytes_read = 1; } else { bytes_read = bytes_read_result.unwrap(); if bytes_read != 0 { - let command_vec: Vec = parser::parse(&buffer); - println!("{:?}", command_vec); + //let command_vec: Vec = parser::parse(&buffer); + //println!("{:?}", command_vec); } } } diff --git a/src/io/out.rs b/src/io/out.rs index e69de29..3888585 100644 --- a/src/io/out.rs +++ b/src/io/out.rs @@ -0,0 +1,9 @@ +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"); + } +} From 7a92b46df53144d9752b70b3d28b03f1a6d0affc Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 14:42:25 +0100 Subject: [PATCH 09/13] Nettoyage du code --- src/io/in.rs | 17 +++++++++++++++-- src/io/mod.rs | 20 +++++++------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/io/in.rs b/src/io/in.rs index ba176e5..849dd68 100644 --- a/src/io/in.rs +++ b/src/io/in.rs @@ -12,13 +12,26 @@ impl Buffer { } } - pub fn read_line(&mut self) -> stdio::Result { + pub fn read_line(&mut self) -> usize { self.buffer.clear(); + let result: stdio::Result = stdin().read_line(&mut self.buffer); + if result.is_err() { eprintln!("error: invalid UTF-8 characters were read"); + self.buffer.clear(); + 1 + } else { + self.buffer = self.buffer.trim().to_string(); + result.unwrap() } + } - result + pub fn is_empty(&self) -> bool { + self.buffer.is_empty() + } + + pub fn get_buffer(&self) -> &String { + &self.buffer } } diff --git a/src/io/mod.rs b/src/io/mod.rs index e706f45..99768cf 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -1,27 +1,21 @@ -use std::io; - mod r#in; mod r#out; - mod parser; pub fn run() -> i32 { let mut buffer = r#in::Buffer::new(); - let mut bytes_read_result: io::Result; let mut bytes_read: usize = 1; + let mut command_line: String; + let mut argv: Vec; while bytes_read != 0 { out::print_ps1(); - bytes_read_result = buffer.read_line(); + bytes_read = buffer.read_line(); - if bytes_read_result.is_err() { - bytes_read = 1; - } else { - bytes_read = bytes_read_result.unwrap(); - if bytes_read != 0 { - //let command_vec: Vec = parser::parse(&buffer); - //println!("{:?}", command_vec); - } + if ! buffer.is_empty() { + command_line = buffer.get_buffer().to_string(); + argv = parser::parse(&command_line); + println!("{:?}", argv); } } From 3176382386fcbe34ea8b45602dadf8a7ae0d1b97 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 14:48:13 +0100 Subject: [PATCH 10/13] =?UTF-8?q?Refactoring=20pour=20tout=20remettre=20?= =?UTF-8?q?=C3=A0=20la=20racine=20car=20c'est=20plus=20pratique?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/{io/in.rs => input.rs} | 0 src/io/mod.rs | 25 ------------------------- src/main.rs | 24 +++++++++++++++++++++--- src/{io/out.rs => output.rs} | 0 src/{io/parser/mod.rs => parser.rs} | 0 5 files changed, 21 insertions(+), 28 deletions(-) rename src/{io/in.rs => input.rs} (100%) delete mode 100644 src/io/mod.rs rename src/{io/out.rs => output.rs} (100%) rename src/{io/parser/mod.rs => parser.rs} (100%) diff --git a/src/io/in.rs b/src/input.rs similarity index 100% rename from src/io/in.rs rename to src/input.rs diff --git a/src/io/mod.rs b/src/io/mod.rs deleted file mode 100644 index 99768cf..0000000 --- a/src/io/mod.rs +++ /dev/null @@ -1,25 +0,0 @@ -mod r#in; -mod r#out; -mod parser; - -pub fn run() -> i32 { - let mut buffer = r#in::Buffer::new(); - let mut bytes_read: usize = 1; - let mut command_line: String; - let mut argv: Vec; - - while bytes_read != 0 { - out::print_ps1(); - bytes_read = buffer.read_line(); - - if ! buffer.is_empty() { - command_line = buffer.get_buffer().to_string(); - argv = parser::parse(&command_line); - println!("{:?}", argv); - } - } - - println!(); - - 0 -} diff --git a/src/main.rs b/src/main.rs index c05e11f..a30723d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,27 @@ use std::process::exit; -mod io; +mod input; +mod output; +mod parser; fn main() { - let last_exit_code: i32 = io::run(); + let mut buffer = input::Buffer::new(); + let mut bytes_read: usize = 1; + let mut command_line: String; + let mut argv: Vec; - exit(last_exit_code); + while bytes_read != 0 { + output::print_ps1(); + bytes_read = buffer.read_line(); + + if ! buffer.is_empty() { + command_line = buffer.get_buffer().to_string(); + argv = parser::parse(&command_line); + println!("{:?}", argv); + } + } + + println!(); + + exit(0); } diff --git a/src/io/out.rs b/src/output.rs similarity index 100% rename from src/io/out.rs rename to src/output.rs diff --git a/src/io/parser/mod.rs b/src/parser.rs similarity index 100% rename from src/io/parser/mod.rs rename to src/parser.rs From b77b887081d2fe3a85d082e12840e142766cb62c Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Wed, 1 Nov 2023 15:00:33 +0100 Subject: [PATCH 11/13] Suppression de la fonction trim inutile dans le parser depuis le remaniement --- src/parser.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index 3824914..97a4bf6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -33,7 +33,7 @@ fn parse_initial_state(char_splitted_command_line: &mut Vec, current_arg: } fn split_string_in_chars(string_to_split: &String) -> Vec { - string_to_split.trim().chars().rev().collect() + string_to_split.chars().rev().collect() } pub fn parse(command_line: &String) -> Vec { From 9022d04ca9b64b41e0d1fbc00912fbd9adf37255 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Sun, 10 Dec 2023 20:57:54 +0100 Subject: [PATCH 12/13] =?UTF-8?q?Moins=20de=20lignes=20dans=20`main.rs`,?= =?UTF-8?q?=20d=C3=A9l=C3=A9gation=20de=20l'appel=20de=20`parse`=20=C3=A0?= =?UTF-8?q?=20`struct=20Buffer`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/input.rs | 13 +++++++------ src/main.rs | 10 ++++------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/input.rs b/src/input.rs index 849dd68..93ac102 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,5 +1,6 @@ use std::io as stdio; use stdio::stdin; +use crate::parser; pub struct Buffer { buffer: String, @@ -27,11 +28,11 @@ impl Buffer { } } - pub fn is_empty(&self) -> bool { - self.buffer.is_empty() - } - - pub fn get_buffer(&self) -> &String { - &self.buffer + pub fn parse(&self) -> Option> { + if ! self.buffer.is_empty() { + Some(parser::parse(&self.buffer)) + } else { + None + } } } diff --git a/src/main.rs b/src/main.rs index a30723d..8458530 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,17 +7,15 @@ mod parser; fn main() { let mut buffer = input::Buffer::new(); let mut bytes_read: usize = 1; - let mut command_line: String; - let mut argv: Vec; + let mut argv: Option>; while bytes_read != 0 { output::print_ps1(); bytes_read = buffer.read_line(); - if ! buffer.is_empty() { - command_line = buffer.get_buffer().to_string(); - argv = parser::parse(&command_line); - println!("{:?}", argv); + argv = buffer.parse(); + if argv.is_some() { + println!("{:?}", argv.unwrap()); } } From 2e45fabc681fe56c2feac0f4a255f84de73eff13 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Sun, 10 Dec 2023 22:29:07 +0100 Subject: [PATCH 13/13] =?UTF-8?q?Possibilit=C3=A9=20d'ex=C3=A9cuter=20des?= =?UTF-8?q?=20commandes=20!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/job.rs | 23 +++++++++++++++++++++++ src/main.rs | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/job.rs diff --git a/src/job.rs b/src/job.rs new file mode 100644 index 0000000..028ed75 --- /dev/null +++ b/src/job.rs @@ -0,0 +1,23 @@ +use std::process::{ + Command, + ExitStatus, +}; +use std::io::{ + Result, + Error, + ErrorKind, +}; + +pub fn execute(mut argv: Vec) -> Result { + let mut command = Command::new(&argv[0]); + argv.remove(0); + command.args(argv); + + let child = command.spawn(); + + if child.is_ok() { + child.unwrap().wait() + } else { + Err(Error::new(ErrorKind::Other, "failed to spawn child process")) + } +} diff --git a/src/main.rs b/src/main.rs index 8458530..13dd9f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::process::exit; mod input; mod output; mod parser; +mod job; fn main() { let mut buffer = input::Buffer::new(); @@ -15,7 +16,7 @@ fn main() { argv = buffer.parse(); if argv.is_some() { - println!("{:?}", argv.unwrap()); + let _ = job::execute(argv.unwrap()); } }