From 99ce65ad9a01d3079a6f3088ce3036ae375d6915 Mon Sep 17 00:00:00 2001 From: Hippolyte Chauvin Date: Sun, 29 Oct 2023 21:42:25 +0100 Subject: [PATCH 1/4] Fonction execute --- src/job.rs | 28 ++++++++++++++++++++++++++++ src/main.rs | 5 ++++- 2 files changed, 32 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..3177ee4 --- /dev/null +++ b/src/job.rs @@ -0,0 +1,28 @@ +use std::io::Result; +use std::process::{Command, Child, ExitStatus}; + +pub struct Job { + id: usize, + argv: Vec, + is_background: bool, +} + +pub fn build_job(argv: &Vec) -> Job { + Job { + id: 1, + argv: argv.to_vec(), + is_background: false, + } +} + +impl Job { + pub fn execute(&self) -> ExitStatus { + let mut argv_iter = self.argv.iter(); + + let child_result: Result = Command::new(argv_iter.next().unwrap()) + .args(argv_iter) + .spawn(); + + child_result.unwrap().wait().unwrap() + } +} diff --git a/src/main.rs b/src/main.rs index 8c9d1ab..d273a5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,14 @@ use std::io; use std::io::Write; mod parser; +mod job; fn main() { let mut buffer = String::new(); let mut result_bytes_read: io::Result; let mut bytes_read: usize = 1; let mut command_vec: Vec; + let mut job: job::Job; while bytes_read != 0 { buffer.clear(); @@ -25,7 +27,8 @@ fn main() { bytes_read = result_bytes_read.unwrap(); if bytes_read != 0 { command_vec = parser::parse(&buffer); - println!("{:?}", command_vec); + job = job::build_job(&command_vec); + job.execute(); } } } From 9022d04ca9b64b41e0d1fbc00912fbd9adf37255 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Sun, 10 Dec 2023 20:57:54 +0100 Subject: [PATCH 2/4] =?UTF-8?q?Moins=20de=20lignes=20dans=20`main.rs`,=20d?= =?UTF-8?q?=C3=A9l=C3=A9gation=20de=20l'appel=20de=20`parse`=20=C3=A0=20`s?= =?UTF-8?q?truct=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 3/4] =?UTF-8?q?Possibilit=C3=A9=20d'ex=C3=A9cuter=20des=20?= =?UTF-8?q?commandes=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()); } } From 9e5936b4923d3e010db07d41441f02e2dfcdb2d9 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Mon, 11 Dec 2023 12:28:16 +0100 Subject: [PATCH 4/4] README : ajout to-do --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8b9fff7..6d9b1be 100644 --- a/README.md +++ b/README.md @@ -2,5 +2,21 @@ [![Please don't upload to GitHub](https://nogithub.codeberg.page/badge.svg)](https://nogithub.codeberg.page) -(The only purpose of this is learning Rust!) +## To-do +- [x] Read commands from `stdin` +- [ ] Exit with last child termination code +- [ ] Handle background jobs +- [ ] Handle pipes +- [ ] Handle double quotes +- [ ] Handle variables +- [ ] Interaction with environment variables +- [ ] Handle aliases +- [ ] Builtins + - [ ] `cd` + - [ ] `exit` +- [ ] Special variables + - [ ] `_` + - [ ] `PS1` + - [ ] Positional variables +- [ ] Handle signals