diff --git a/src/execute.rs b/src/execute.rs new file mode 100644 index 0000000..5368dde --- /dev/null +++ b/src/execute.rs @@ -0,0 +1,48 @@ +use std::process::ExitStatus; + +use crate::parse::Command; + +pub fn execute(cmds: &[Command<'_>]) { + for cmd in cmds { + execute_command(cmd); + } +} + +fn execute_command(cmd: &Command<'_>) -> ExitStatus { + match cmd { + Command::Single(cmd) => { + if cmd.exclamation { + todo!() + } + if cmd.time { + todo!() + } + if cmd.terminator.is_some() { + todo!() + } + if !cmd.rest.is_empty() { + todo!() + } + let cmds = &cmd.initial; + if !cmds.rest.is_empty() { + todo!() + } + + let program = &cmds.initial.tokens[0].value; + let mut proc = std::process::Command::new(program.as_ref()); + proc.args( + cmds.initial.tokens[1..] + .iter() + .map(|t| -> &str { &t.value.as_ref() }), + ); + + let mut proc = proc.spawn().unwrap(); + + let status = proc.wait().unwrap(); + + status + } + Command::Subshell(_) => todo!(), + Command::Block(_) => todo!(), + } +} diff --git a/src/lib.rs b/src/lib.rs index 69b1248..04698c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,17 @@ pub mod parse; +pub mod execute; use std::sync::Arc; use anyhow::{anyhow, bail, Context, Result}; +use execute::execute; pub fn bash_it(args: impl Iterator) -> Result<()> { let (filename, src) = parse_args_into_src(args)?; - parse::parse(filename, &src)?; + let command = parse::parse(filename, &src)?; + + execute(&command); Ok(()) } diff --git a/src/main.rs b/src/main.rs index 5a72a4d..684cb6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,3 @@ fn main() -> anyhow::Result<()> { - brash::bash_it(std::env::args()) + brash::bash_it(std::env::args().skip(1)) } diff --git a/src/parse.rs b/src/parse.rs index fe9fb5f..f44f722 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -4,10 +4,27 @@ use std::{borrow::Cow, iter, sync::Arc}; use anyhow::{bail, Result}; -pub fn parse(filename: Arc, src: &str) -> Result<()> { - let tokens = lex(filename, src)?; - dbg!(tokens); - Ok(()) +pub fn parse(filename: Arc, src: &str) -> Result> { + let mut tokens = lex(filename, src)?; + dbg!(&tokens); + + if tokens.get(0).map_or(false, |t| t.value.is_empty()) { + // bad hack or whatever, everything in here is a bad hack + tokens.remove(0); + } + + let command = SimpleCommand { tokens }; + + Ok(vec![Command::Single(Pipeline { + time: false, + exclamation: false, + initial: CommandList { + initial: command, + rest: vec![], + }, + rest: vec![], + terminator: None, + })]) } /*