diff --git a/src/command.rs b/src/command.rs new file mode 100644 index 0000000000000000000000000000000000000000..871cdb3459120f99cde2c84efaa7628e8727a6c9 --- /dev/null +++ b/src/command.rs @@ -0,0 +1,28 @@ +use std::process::{Command,Output}; + +pub struct InstructionOut { + pub stdout: String, + pub stderr: String, +} + +pub fn run(input_command: Vec<&str>) -> Option<InstructionOut> { + let args = input_command.as_slice(); + let length = args.len(); + let output: Option<Output>; + if length ==0 { + output = Command::new("").output().ok(); + } else if length == 1 { + output = Command::new(&args[0]).output().ok(); + } else { + output = Command::new(&args[0]).args(&args[1..]).output().ok(); + }; + if output.is_some() { + let output = output.unwrap(); + Some(InstructionOut { + stdout: String::from_utf8(output.stdout).ok().expect("No stdout"), + stderr: String::from_utf8(output.stderr).ok().expect("No stderr"), + }) + } else { + None + } +} diff --git a/src/lib.rs b/src/lib.rs index cd3c8c0e7de5d64d3c4015a21acfa5e9ee7aa181..c59145e5fe270658569cf9fd31a10c22f3504a22 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,21 @@ +#![feature(convert)] +pub mod command; + use std::io; +use command::*; pub fn repl() { let mut input = String::new(); let _unused = io::stdin().read_line(&mut input); - println!("You typed: {}", input.trim()); + let out_wrap = run(input.trim().split_whitespace().collect::<Vec<&str>>()); + if out_wrap.is_some() { + let out = out_wrap.unwrap(); + if out.stdout.is_empty() { + println!("{}",out.stderr.trim()); + } else { + println!("{}",out.stdout.trim()); + } + } else { + println!("{} is not a valid command", input.trim()); + } }