diff options
author | Jesse Luehrs <doy@tozt.net> | 2022-01-07 23:42:32 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2022-01-08 00:00:57 -0500 |
commit | 9dd1d992bd2344d8824e69d8f47c9009a6caf021 (patch) | |
tree | a2b3682c41a182bbb3b031e5c458ff93c460e53f /src/pipeline | |
parent | a2d265ce727caf4e8cd4bad034ef8ae830346fc3 (diff) | |
download | nbsh-9dd1d992bd2344d8824e69d8f47c9009a6caf021.tar.gz nbsh-9dd1d992bd2344d8824e69d8f47c9009a6caf021.zip |
large refactor
Diffstat (limited to 'src/pipeline')
-rw-r--r-- | src/pipeline/builtins/command.rs | 16 | ||||
-rw-r--r-- | src/pipeline/builtins/mod.rs | 6 | ||||
-rw-r--r-- | src/pipeline/command.rs | 26 | ||||
-rw-r--r-- | src/pipeline/mod.rs | 6 |
4 files changed, 30 insertions, 24 deletions
diff --git a/src/pipeline/builtins/command.rs b/src/pipeline/builtins/command.rs index c0fa86d..03fe8a2 100644 --- a/src/pipeline/builtins/command.rs +++ b/src/pipeline/builtins/command.rs @@ -8,12 +8,16 @@ pub struct Command { impl Command { pub fn new(exe: crate::parse::Exe) -> Result<Self, crate::parse::Exe> { - if let Some(f) = super::BUILTINS.get(exe.exe()) { - Ok(Self { - exe, - f, - io: Io::new(), - }) + if let Some(s) = exe.exe().to_str() { + if let Some(f) = super::BUILTINS.get(s) { + Ok(Self { + exe, + f, + io: Io::new(), + }) + } else { + Err(exe) + } } else { Err(exe) } diff --git a/src/pipeline/builtins/mod.rs b/src/pipeline/builtins/mod.rs index 003892d..e608ae7 100644 --- a/src/pipeline/builtins/mod.rs +++ b/src/pipeline/builtins/mod.rs @@ -62,7 +62,7 @@ fn cd( let dir = exe .args() - .into_iter() + .iter() .map(std::convert::AsRef::as_ref) .next() .unwrap_or(""); @@ -137,8 +137,8 @@ fn echo( } }; } - let count = exe.args().count(); - for (i, arg) in exe.args().enumerate() { + let count = exe.args().len(); + for (i, arg) in exe.args().iter().enumerate() { write_stdout!(arg.as_bytes()); if i == count - 1 { write_stdout!(b"\n"); diff --git a/src/pipeline/command.rs b/src/pipeline/command.rs index 746a340..f9dda06 100644 --- a/src/pipeline/command.rs +++ b/src/pipeline/command.rs @@ -2,27 +2,22 @@ use crate::pipeline::prelude::*; pub struct Command { inner: Inner, - exe: String, + exe: std::path::PathBuf, redirects: Vec<crate::parse::Redirect>, pre_exec: Option< Box<dyn FnMut() -> std::io::Result<()> + Send + Sync + 'static>, >, } -pub enum Inner { - Binary(async_std::process::Command), - Builtin(super::builtins::Command), -} - impl Command { pub fn new(exe: crate::parse::Exe) -> Self { - let exe_str = exe.exe().to_string(); + let exe_path = exe.exe().to_path_buf(); let redirects = exe.redirects().to_vec(); Self { inner: super::builtins::Command::new(exe).map_or_else( |exe| Self::new_binary(exe).inner, Inner::Builtin, ), - exe: exe_str, + exe: exe_path, redirects, pre_exec: None, } @@ -30,25 +25,25 @@ impl Command { #[allow(clippy::needless_pass_by_value)] pub fn new_binary(exe: crate::parse::Exe) -> Self { - let exe_str = exe.exe().to_string(); + let exe_path = exe.exe().to_path_buf(); let redirects = exe.redirects().to_vec(); let mut cmd = async_std::process::Command::new(exe.exe()); cmd.args(exe.args()); Self { inner: Inner::Binary(cmd), - exe: exe_str, + exe: exe_path, redirects, pre_exec: None, } } pub fn new_builtin(exe: crate::parse::Exe) -> Self { - let exe_str = exe.exe().to_string(); + let exe_path = exe.exe().to_path_buf(); let redirects = exe.redirects().to_vec(); Self { inner: super::builtins::Command::new(exe) .map_or_else(|_| todo!(), Inner::Builtin), - exe: exe_str, + exe: exe_path, redirects, pre_exec: None, } @@ -132,7 +127,7 @@ impl Command { anyhow::anyhow!( "{}: {}", crate::format::io_error(&e), - exe + exe.display() ) })?)) } @@ -147,6 +142,11 @@ impl Command { } } +pub enum Inner { + Binary(async_std::process::Command), + Builtin(super::builtins::Command), +} + pub enum Child<'a> { Binary(async_std::process::Child), Builtin(super::builtins::Child<'a>), diff --git a/src/pipeline/mod.rs b/src/pipeline/mod.rs index 015eeef..a54951c 100644 --- a/src/pipeline/mod.rs +++ b/src/pipeline/mod.rs @@ -37,7 +37,8 @@ async fn run_with_env( env: &mut Env, shell_write: &async_std::fs::File, ) -> anyhow::Result<()> { - let pipeline = crate::parse::Pipeline::parse(env.pipeline().unwrap())?; + let pipeline = + crate::parse::ast::Pipeline::parse(env.pipeline().unwrap())?; let (children, pg) = spawn_children(pipeline, env)?; let status = wait_children(children, pg, env, shell_write).await; env.set_status(status); @@ -61,9 +62,10 @@ async fn write_event( } fn spawn_children( - pipeline: crate::parse::Pipeline, + pipeline: crate::parse::ast::Pipeline, env: &Env, ) -> anyhow::Result<(Vec<Child>, Option<nix::unistd::Pid>)> { + let pipeline = pipeline.eval(env); let mut cmds: Vec<_> = pipeline.into_exes().map(Command::new).collect(); for i in 0..(cmds.len() - 1) { let (r, w) = pipe()?; |