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 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/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 a30723d..13dd9f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,21 +3,20 @@ use std::process::exit; mod input; mod output; mod parser; +mod job; 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() { + let _ = job::execute(argv.unwrap()); } }