summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-01-03 01:56:44 -0500
committerJesse Luehrs <doy@tozt.net>2022-01-03 01:57:54 -0500
commitacb062a2bc6cf103822e6b564d149677b7cf5c14 (patch)
treec6897db8c6ad00c355828162b2341b4ab7bfce17
parentb2874e0a14920f1b4efe515178b2bfa90b756d4d (diff)
downloadnbsh-acb062a2bc6cf103822e6b564d149677b7cf5c14.tar.gz
nbsh-acb062a2bc6cf103822e6b564d149677b7cf5c14.zip
clippy
-rw-r--r--src/builtins.rs2
-rw-r--r--src/command.rs71
2 files changed, 41 insertions, 32 deletions
diff --git a/src/builtins.rs b/src/builtins.rs
index 014eadd..18eb1aa 100644
--- a/src/builtins.rs
+++ b/src/builtins.rs
@@ -85,6 +85,8 @@ impl Child {
}
}
+// clippy can't tell that the type is necessary
+#[allow(clippy::unnecessary_wraps)]
fn cd(
exe: &crate::parse::Exe,
env: &crate::command::Env,
diff --git a/src/command.rs b/src/command.rs
index 604f138..2dbf1ce 100644
--- a/src/command.rs
+++ b/src/command.rs
@@ -131,39 +131,10 @@ impl Child {
pub async fn run() -> anyhow::Result<i32> {
let (code, pipeline) = read_data().await?;
- let env: Env = Env::new(code);
- let mut cmds: Vec<_> = pipeline.exes().iter().map(Command::new).collect();
- for i in 0..(cmds.len() - 1) {
- let (r, w) = pipe()?;
- cmds[i].stdout(w);
- cmds[i + 1].stdin(r);
- }
-
- let mut children = vec![];
- let mut pg_pid = None;
- for mut cmd in cmds.drain(..) {
- // Safety: setpgid is an async-signal-safe function
- unsafe {
- cmd.pre_exec(move || {
- setpgid_child(pg_pid)?;
- Ok(())
- });
- }
- let child = cmd.spawn(&env)?;
- if let Some(id) = child.id() {
- let child_pid = id_to_pid(id);
- setpgid_parent(child_pid, pg_pid)?;
- if pg_pid.is_none() {
- pg_pid = Some(child_pid);
- set_foreground_pg(child_pid)?;
- }
- }
- children.push(child);
- }
-
- let mut final_status = None;
-
+ let env = Env::new(code);
+ let children = spawn_children(&pipeline, &env)?;
let count = children.len();
+
let mut children: futures_util::stream::FuturesUnordered<_> =
children
.into_iter()
@@ -172,6 +143,7 @@ pub async fn run() -> anyhow::Result<i32> {
(child.status().await, i == count - 1)
})
.collect();
+ let mut final_status = None;
while let Some((status, last)) = children.next().await {
let status = status.unwrap_or_else(|_| {
async_std::process::ExitStatus::from_raw(1 << 8)
@@ -207,6 +179,41 @@ async fn read_data() -> anyhow::Result<(i32, crate::parse::Pipeline)> {
Ok((code, ast))
}
+fn spawn_children(
+ pipeline: &crate::parse::Pipeline,
+ env: &Env,
+) -> anyhow::Result<Vec<Child>> {
+ let mut cmds: Vec<_> = pipeline.exes().iter().map(Command::new).collect();
+ for i in 0..(cmds.len() - 1) {
+ let (r, w) = pipe()?;
+ cmds[i].stdout(w);
+ cmds[i + 1].stdin(r);
+ }
+
+ let mut children = vec![];
+ let mut pg_pid = None;
+ for mut cmd in cmds.drain(..) {
+ // Safety: setpgid is an async-signal-safe function
+ unsafe {
+ cmd.pre_exec(move || {
+ setpgid_child(pg_pid)?;
+ Ok(())
+ });
+ }
+ let child = cmd.spawn(env)?;
+ if let Some(id) = child.id() {
+ let child_pid = id_to_pid(id);
+ setpgid_parent(child_pid, pg_pid)?;
+ if pg_pid.is_none() {
+ pg_pid = Some(child_pid);
+ set_foreground_pg(child_pid)?;
+ }
+ }
+ children.push(child);
+ }
+ Ok(children)
+}
+
fn pipe() -> anyhow::Result<(std::fs::File, std::fs::File)> {
let (r, w) = nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC)?;
// Safety: these file descriptors were just returned by pipe2 above, which