use std::cell::Cell;
use super::{Cmd, Pipeline, Redir};
thread_local! {
static FED: Cell<bool> = const { Cell::new(false) };
}
struct FedGuard(bool);
impl Drop for FedGuard {
fn drop(&mut self) {
FED.with(|c| c.set(self.0));
}
}
pub(crate) fn fed() -> bool {
FED.with(Cell::get)
}
pub(super) fn with_stage<T>(pipeline: &Pipeline, position: usize, run: impl FnOnce() -> T) -> T {
let marked = position > 0 || redirs(&pipeline.commands[position]).iter().any(feeds_stdin);
let prev = fed();
FED.with(|c| c.set(prev || marked));
let _guard = FedGuard(prev);
run()
}
fn redirs(cmd: &Cmd) -> &[Redir] {
match cmd {
Cmd::Simple(s) => &s.redirs,
Cmd::Subshell { redirs, .. }
| Cmd::BraceGroup { redirs, .. }
| Cmd::For { redirs, .. }
| Cmd::While { redirs, .. }
| Cmd::Until { redirs, .. }
| Cmd::If { redirs, .. }
| Cmd::Case { redirs, .. }
| Cmd::DoubleBracket { redirs, .. } => redirs,
Cmd::FunctionDef { .. } => &[],
}
}
fn feeds_stdin(redir: &Redir) -> bool {
match redir {
Redir::Read { fd: 0, target } | Redir::ReadWrite { fd: 0, target } => target.eval() != "/dev/null",
Redir::HereStr(_) | Redir::HereDoc { .. } => true,
Redir::DupFd { src: 0, dst } => dst != "-",
Redir::Read { .. } | Redir::ReadWrite { .. } | Redir::Write { .. } | Redir::DupFd { .. } => false,
}
}
#[cfg(test)]
#[path = "stdin_fed_tests.rs"]
mod tests;