use std::fs::{self, File};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command as SysCommand, ExitStatus, Stdio};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use crate::shell::ast::{Arg, Command, Pipeline, Redirect, ScriptNode, SeqOp};
use crate::shell::builtins::{is_inproc_builtin, run_inproc_builtin};
use crate::shell::environment::{expand_arg_glob, ShellCommandHandler, ShellEnv};
#[cfg(unix)]
use nix::errno::Errno;
#[cfg(unix)]
use nix::sys::signal::{self, Signal};
#[cfg(unix)]
use nix::unistd::Pid;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
#[derive(Debug, Clone)]
pub struct CommandResult {
pub stdout: String,
pub stderr: String,
pub exit_code: i32,
pub timed_out: bool,
}
#[derive(Debug, Clone)]
pub struct ScriptResult {
pub exit_code: i32,
pub stdout: String,
pub stderr: String,
pub timed_out: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CaptureStream {
Stdout,
Stderr,
}
struct OpenSink {
file: Option<File>,
path: PathBuf,
capture: Option<CaptureStream>,
}
impl OpenSink {
fn stdio(&self) -> Result<Stdio, String> {
self.file
.as_ref()
.ok_or_else(|| "output sink is closed".to_string())?
.try_clone()
.map(Stdio::from)
.map_err(|error| format!("failed to clone output sink: {error}"))
}
fn write_all(&self, data: &[u8]) -> Result<(), String> {
let mut file = self
.file
.as_ref()
.ok_or_else(|| "output sink is closed".to_string())?;
file.write_all(data)
.map_err(|error| format!("failed to write '{}': {error}", self.path.display()))
}
}
impl Drop for OpenSink {
fn drop(&mut self) {
self.file.take();
if self.capture.is_some() {
let _ = fs::remove_file(&self.path);
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum OutputTarget {
Pipe,
Sink(usize),
}
struct RedirectLayout {
stdin_path: Option<PathBuf>,
stdout: OutputTarget,
stderr: OutputTarget,
sinks: Vec<OpenSink>,
}
impl RedirectLayout {
fn captured(&self) -> Result<(Vec<u8>, Vec<u8>), String> {
let mut stdout = Vec::new();
let mut stderr = Vec::new();
for sink in &self.sinks {
let Some(stream) = sink.capture else {
continue;
};
let data = fs::read(&sink.path)
.map_err(|error| format!("failed to read '{}': {error}", sink.path.display()))?;
match stream {
CaptureStream::Stdout => stdout.extend(data),
CaptureStream::Stderr => stderr.extend(data),
}
}
Ok((stdout, stderr))
}
fn write(&self, target: OutputTarget, data: &[u8]) -> Result<(), String> {
match target {
OutputTarget::Pipe => Err("in-process pipeline output was not buffered".to_string()),
OutputTarget::Sink(index) => self
.sinks
.get(index)
.ok_or_else(|| "invalid output sink".to_string())?
.write_all(data),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Negation {
None,
Shell,
Lit,
Crash,
Even,
}
#[derive(Clone)]
struct ParsedCommand {
command: Command,
env: ShellEnv,
args: Vec<String>,
negation: Negation,
env_prefix_used: bool,
env_print: bool,
}
struct StageRecord {
result: CommandResult,
stdout_bytes: Vec<u8>,
stdout_visible: bool,
}
struct RunningCommand {
child: Child,
parsed: ParsedCommand,
redirects: RedirectLayout,
}
struct RunningPipeline {
commands: Vec<RunningCommand>,
complete: bool,
}
impl Drop for RunningPipeline {
fn drop(&mut self) {
if self.complete {
return;
}
for command in &mut self.commands {
if command.child.try_wait().ok().flatten().is_none() {
terminate_process(&mut command.child);
let _ = command.child.wait();
}
}
}
}
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
pub fn execute_script_node(
script: &ScriptNode,
shenv: &mut ShellEnv,
timeout: Option<Duration>,
) -> Result<ScriptResult, String> {
let deadline = timeout
.filter(|duration| !duration.is_zero())
.and_then(|duration| Instant::now().checked_add(duration));
execute_script_node_before(script, shenv, deadline)
}
fn execute_script_node_before(
script: &ScriptNode,
shenv: &mut ShellEnv,
deadline: Option<Instant>,
) -> Result<ScriptResult, String> {
match script {
ScriptNode::Pipeline(pipeline) => match execute_pipeline(pipeline, shenv, deadline) {
Ok(result) => Ok(result),
Err(error) => Ok(ScriptResult {
exit_code: if pipeline.negate { 0 } else { 127 },
stdout: String::new(),
stderr: format!("{error}\n"),
timed_out: false,
}),
},
ScriptNode::Seq(lhs, op, rhs) => {
let left = execute_script_node_before(lhs, shenv, deadline)?;
if left.timed_out {
return Ok(left);
}
let execute_right = match op {
SeqOp::Semicolon => true,
SeqOp::Ampersand => {
return Err("unsupported shell operator: '&'".to_string());
}
SeqOp::Or => left.exit_code != 0,
SeqOp::And => left.exit_code == 0,
};
if !execute_right {
return Ok(left);
}
let right = execute_script_node_before(rhs, shenv, deadline)?;
let mut stdout = left.stdout;
stdout.push_str(&right.stdout);
let mut stderr = left.stderr;
stderr.push_str(&right.stderr);
Ok(ScriptResult {
exit_code: right.exit_code,
stdout,
stderr,
timed_out: right.timed_out,
})
}
}
}
fn execute_pipeline(
pipeline: &Pipeline,
shenv: &mut ShellEnv,
deadline: Option<Instant>,
) -> Result<ScriptResult, String> {
let mut parsed = pipeline
.commands
.iter()
.map(|command| parse_command(command, shenv))
.collect::<Result<Vec<_>, _>>()?;
let command_count = parsed.len();
let mut records = Vec::with_capacity(command_count);
let mut input = None;
let mut external = Vec::new();
let mut timed_out = false;
for (index, command) in parsed.drain(..).enumerate() {
let command_name = command.args.first().map(String::as_str).unwrap_or("env");
let embedded = command.env.commands.get(command_name).cloned();
let builtin = command.env_print || is_inproc_builtin(command_name);
if embedded.is_none() && !builtin {
external.push(command);
continue;
}
if !external.is_empty() {
let execution = execute_external_segment(&external, input.take(), false, deadline);
let (next_input, segment_timed_out) = match execution {
Ok((mut completed, next_input, segment_timed_out)) => {
records.append(&mut completed);
(next_input, segment_timed_out)
}
Err(error) => {
let negation = external
.last()
.map_or(Negation::None, |command| command.negation);
records.push(failed_stage(error, negation, false));
(Vec::new(), false)
}
};
external.clear();
input = Some(next_input);
if segment_timed_out {
timed_out = true;
break;
}
}
let is_pipeline_last = index + 1 == command_count;
let negation = command.negation;
let record = match execute_in_process_stage(
command,
embedded,
input.take(),
command_count > 1,
is_pipeline_last,
deadline,
shenv,
) {
Ok(record) => record,
Err(error) => failed_stage(error, negation, is_pipeline_last),
};
timed_out |= record.result.timed_out;
input = Some(record.stdout_bytes.clone());
records.push(record);
if timed_out {
break;
}
}
if !timed_out && !external.is_empty() {
match execute_external_segment(&external, input.take(), true, deadline) {
Ok((mut completed, _, segment_timed_out)) => {
timed_out = segment_timed_out;
records.append(&mut completed);
}
Err(error) => {
let negation = external
.last()
.map_or(Negation::None, |command| command.negation);
records.push(failed_stage(error, negation, true));
}
}
}
let mut stdout = String::new();
let mut stderr = String::new();
for record in &records {
if record.stdout_visible {
stdout.push_str(&record.result.stdout);
}
stderr.push_str(&record.result.stderr);
}
let mut exit_code = if pipeline.pipe_fail {
records
.iter()
.rev()
.find(|record| record.result.exit_code != 0)
.map(|record| record.result.exit_code)
.unwrap_or(0)
} else {
records
.last()
.map(|record| record.result.exit_code)
.unwrap_or(0)
};
if pipeline.negate {
exit_code = i32::from(exit_code == 0);
}
Ok(ScriptResult {
exit_code,
stdout,
stderr,
timed_out,
})
}
fn failed_stage(error: String, negation: Negation, stdout_visible: bool) -> StageRecord {
let mut exit_code = 127;
apply_negation(&mut exit_code, negation);
StageRecord {
result: CommandResult {
stdout: String::new(),
stderr: format!("{error}\n"),
exit_code,
timed_out: false,
},
stdout_bytes: Vec::new(),
stdout_visible,
}
}
fn parse_command(command: &Command, shenv: &ShellEnv) -> Result<ParsedCommand, String> {
let mut args = command.args.clone();
let mut env = shenv.clone();
let mut not_count = 0usize;
let mut saw_lit_not = false;
let mut crash = false;
let mut env_prefix_used = false;
let mut pos = 0usize;
while pos < args.len() {
match args[pos].as_str() {
"not" => {
saw_lit_not = true;
not_count += 1;
pos += 1;
if pos < args.len() && args[pos].as_str() == "--crash" {
crash = true;
pos += 1;
}
}
"!" => {
not_count += 1;
pos += 1;
}
"env" => {
env_prefix_used = true;
pos += 1;
while pos < args.len() {
let value = args[pos].as_str();
if value == "-i" {
env.env.clear();
pos += 1;
} else if value == "-u" {
let name = args
.get(pos + 1)
.ok_or_else(|| "'env -u' requires a variable name".to_string())?;
env.env.remove(name.as_str());
pos += 2;
} else if let Some((key, value)) = value.split_once('=') {
env.env.insert(key.to_string(), value.to_string());
pos += 1;
} else {
break;
}
}
}
_ => break,
}
}
let env_print = env_prefix_used && pos == args.len();
if pos == args.len() && !env_print {
return Err("empty command".to_string());
}
let mut expanded = Vec::new();
for (index, arg) in args.split_off(pos).into_iter().enumerate() {
let arg = expand_late_argument(arg, &env.cwd, env.normalize_slashes)?;
let matches = expand_arg_glob(&arg, &env.cwd);
if index == 0 {
expanded.extend(matches.into_iter().take(1));
} else {
expanded.extend(matches);
}
}
let negation = if not_count == 0 {
Negation::None
} else if not_count & 1 == 0 {
Negation::Even
} else if crash {
Negation::Crash
} else if saw_lit_not {
Negation::Lit
} else {
Negation::Shell
};
Ok(ParsedCommand {
command: command.clone(),
env,
args: expanded,
negation,
env_prefix_used,
env_print,
})
}
fn execute_in_process_stage(
mut parsed: ParsedCommand,
embedded: Option<Arc<dyn ShellCommandHandler>>,
piped_input: Option<Vec<u8>>,
in_pipeline: bool,
is_pipeline_last: bool,
deadline: Option<Instant>,
shenv: &mut ShellEnv,
) -> Result<StageRecord, String> {
let redirects = resolve_redirects(&parsed.command.redirects, &parsed.env.cwd, true)?;
let input = if let Some(path) = &redirects.stdin_path {
fs::read(path).map_err(|error| format!("failed to read '{}': {error}", path.display()))?
} else {
piped_input.unwrap_or_default()
};
if deadline.is_some_and(|deadline| Instant::now() >= deadline) {
return Ok(StageRecord {
result: CommandResult {
stdout: String::new(),
stderr: String::new(),
exit_code: -1,
timed_out: true,
},
stdout_bytes: Vec::new(),
stdout_visible: is_pipeline_last,
});
}
let output = if parsed.env_print {
let mut entries = parsed
.env
.env
.iter()
.map(|(key, value)| format!("{key}={value}"))
.collect::<Vec<_>>();
entries.sort_unstable();
let stdout = if entries.is_empty() {
Vec::new()
} else {
(entries.join("\n") + "\n").into_bytes()
};
crate::shell::environment::ShellCommandOutput {
stdout,
stderr: Vec::new(),
exit_code: 0,
}
} else if let Some(handler) = embedded {
handler.execute(&parsed.args, &input, &parsed.env.cwd, &parsed.env.env)?
} else {
let name = parsed.args.first().map(String::as_str).unwrap_or("");
if in_pipeline && !matches!(name, "echo" | "@echo" | ":") {
return Err(format!("builtin '{name}' cannot be part of a pipeline"));
}
if parsed.env_prefix_used {
return Err(format!("'env' cannot call builtin '{name}'"));
}
let builtin_command = Command {
args: parsed.args.iter().cloned().map(Arg::Text).collect(),
redirects: Vec::new(),
};
let input_text = String::from_utf8_lossy(&input);
let result =
run_inproc_builtin(&builtin_command, &mut parsed.env, Some(input_text.as_ref()))?;
if !in_pipeline {
*shenv = parsed.env.clone();
}
crate::shell::environment::ShellCommandOutput {
stdout: result.stdout.into_bytes(),
stderr: result.stderr.into_bytes(),
exit_code: result.exit_code,
}
};
redirects.write(redirects.stdout, &output.stdout)?;
redirects.write(redirects.stderr, &output.stderr)?;
let (stdout, stderr) = redirects.captured()?;
let mut exit_code = output.exit_code;
apply_negation(&mut exit_code, parsed.negation);
let timed_out = deadline.is_some_and(|deadline| Instant::now() >= deadline);
Ok(StageRecord {
result: CommandResult {
stdout: String::from_utf8_lossy(&stdout).into_owned(),
stderr: String::from_utf8_lossy(&stderr).into_owned(),
exit_code,
timed_out,
},
stdout_bytes: stdout,
stdout_visible: is_pipeline_last,
})
}
fn execute_external_segment(
commands: &[ParsedCommand],
input: Option<Vec<u8>>,
segment_ends_pipeline: bool,
deadline: Option<Instant>,
) -> Result<(Vec<StageRecord>, Vec<u8>, bool), String> {
let mut input_sink = input
.map(|data| {
let sink = create_capture_sink("stdin", CaptureStream::Stdout)?;
sink.write_all(&data)?;
Ok::<OpenSink, String>(sink)
})
.transpose()?;
let mut previous_pipe = None;
let mut running = RunningPipeline {
commands: Vec::new(),
complete: false,
};
for (index, parsed) in commands.iter().enumerate() {
let is_last = index + 1 == commands.len();
let redirects = resolve_redirects(&parsed.command.redirects, &parsed.env.cwd, is_last)?;
let executable = resolve_executable(
parsed
.args
.first()
.ok_or_else(|| "empty command".to_string())?,
&parsed.env,
)?;
let mut command = SysCommand::new(executable);
command
.args(&parsed.args[1..])
.current_dir(&parsed.env.cwd)
.env_clear()
.envs(&parsed.env.env);
if let Some(path) = &redirects.stdin_path {
command.stdin(
File::open(path)
.map(Stdio::from)
.map_err(|error| format!("failed to open '{}': {error}", path.display()))?,
);
previous_pipe.take();
} else if index == 0 {
if let Some(sink) = &input_sink {
command.stdin(
File::open(&sink.path)
.map(Stdio::from)
.map_err(|error| format!("failed to reopen pipeline input: {error}"))?,
);
} else {
command.stdin(Stdio::null());
}
} else {
command.stdin(previous_pipe.take().unwrap_or_else(Stdio::null));
}
let stdout_is_pipe = redirects.stdout == OutputTarget::Pipe;
let stderr_is_pipe = redirects.stderr == OutputTarget::Pipe;
match redirects.stdout {
OutputTarget::Pipe => {
command.stdout(Stdio::piped());
}
OutputTarget::Sink(index) => {
command.stdout(redirects.sinks[index].stdio()?);
}
}
match redirects.stderr {
OutputTarget::Pipe if stdout_is_pipe => {
#[cfg(not(unix))]
return Err(
"merging stderr into a live pipeline is unsupported on this platform"
.to_string(),
);
#[cfg(unix)]
command.stderr(Stdio::null());
}
OutputTarget::Pipe => {
command.stderr(Stdio::piped());
}
OutputTarget::Sink(index) => {
command.stderr(redirects.sinks[index].stdio()?);
}
}
#[cfg(unix)]
{
let merge_pipe = stdout_is_pipe && stderr_is_pipe;
let umask = parsed.env.umask;
unsafe {
command.pre_exec(move || {
if let Err(error) = nix::unistd::setpgid(Pid::from_raw(0), Pid::from_raw(0)) {
return Err(io::Error::from_raw_os_error(error as i32));
}
if merge_pipe && dup2(1, 2) == -1 {
return Err(io::Error::last_os_error());
}
if let Some(mask) = umask {
set_umask(mask);
}
Ok(())
});
}
}
let mut child = command.spawn().map_err(|error| {
format!(
"failed to spawn command '{}': {error}",
parsed.args.first().map(String::as_str).unwrap_or("")
)
})?;
previous_pipe = if stdout_is_pipe {
child.stdout.take().map(Stdio::from)
} else if stderr_is_pipe {
child.stderr.take().map(Stdio::from)
} else {
None
};
running.commands.push(RunningCommand {
child,
parsed: parsed.clone(),
redirects,
});
}
previous_pipe.take();
input_sink.take();
let (statuses, timed_out) = wait_for_pipeline(&mut running.commands, deadline)?;
let mut records = Vec::with_capacity(running.commands.len());
let record_count = running.commands.len();
for (index, (command, status)) in running.commands.iter().zip(statuses).enumerate() {
let (stdout, stderr) = command.redirects.captured()?;
let mut exit_code = process_exit_code(status);
apply_negation(&mut exit_code, command.parsed.negation);
records.push(StageRecord {
result: CommandResult {
stdout: String::from_utf8_lossy(&stdout).into_owned(),
stderr: String::from_utf8_lossy(&stderr).into_owned(),
exit_code,
timed_out,
},
stdout_bytes: stdout,
stdout_visible: segment_ends_pipeline && index + 1 == record_count,
});
}
running.complete = true;
let output = records
.last()
.map(|record| record.stdout_bytes.clone())
.unwrap_or_default();
Ok((records, output, timed_out))
}
fn resolve_redirects(
redirects: &[Redirect],
cwd: &Path,
capture_stdout: bool,
) -> Result<RedirectLayout, String> {
let mut sinks = Vec::new();
let stdout = if capture_stdout {
sinks.push(create_capture_sink("stdout", CaptureStream::Stdout)?);
OutputTarget::Sink(0)
} else {
OutputTarget::Pipe
};
let stderr_index = sinks.len();
sinks.push(create_capture_sink("stderr", CaptureStream::Stderr)?);
let mut layout = RedirectLayout {
stdin_path: None,
stdout,
stderr: OutputTarget::Sink(stderr_index),
sinks,
};
for redirect in redirects {
let target = expand_glob_argument(&redirect.target, cwd)?;
match (redirect.source_fd, redirect.op.as_str(), target.as_str()) {
(0, "<", _) => layout.stdin_path = Some(resolve_path(cwd, &target)),
(0, "<&", "0") => {}
(1, ">", _) | (1, ">>", _) => {
let append = redirect.op == ">>";
let index = layout.sinks.len();
layout.sinks.push(open_redirect_sink(cwd, &target, append)?);
layout.stdout = OutputTarget::Sink(index);
}
(2, ">", _) | (2, ">>", _) => {
let append = redirect.op == ">>";
let index = layout.sinks.len();
layout.sinks.push(open_redirect_sink(cwd, &target, append)?);
layout.stderr = OutputTarget::Sink(index);
}
(2, ">&", "1") => layout.stderr = layout.stdout,
(1, ">&", "2") => layout.stdout = layout.stderr,
(1, "&>", _) | (1, ">&", _) => {
let index = layout.sinks.len();
layout.sinks.push(open_redirect_sink(cwd, &target, false)?);
layout.stdout = OutputTarget::Sink(index);
layout.stderr = OutputTarget::Sink(index);
}
_ => {
return Err(format!(
"unsupported redirection '{}{} {}'",
redirect.source_fd, redirect.op, redirect.target
));
}
}
}
Ok(layout)
}
fn create_capture_sink(tag: &str, stream: CaptureStream) -> Result<OpenSink, String> {
let pid = std::process::id();
for _ in 0..128 {
let serial = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
let path = std::env::temp_dir().join(format!("litnik-{tag}-{pid}-{serial}"));
match fs::OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.open(&path)
{
Ok(file) => {
return Ok(OpenSink {
file: Some(file),
path,
capture: Some(stream),
});
}
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
Err(error) => return Err(format!("failed to create capture file: {error}")),
}
}
Err("failed to allocate a unique capture file".to_string())
}
fn open_redirect_sink(cwd: &Path, target: &str, append: bool) -> Result<OpenSink, String> {
let path = resolve_path(cwd, target);
let mut options = fs::OpenOptions::new();
options.write(true).create(true);
if append {
options.append(true);
} else {
options.truncate(true);
}
let file = options
.open(&path)
.map_err(|error| format!("failed to open '{}': {error}", path.display()))?;
Ok(OpenSink {
file: Some(file),
path,
capture: None,
})
}
fn wait_for_pipeline(
commands: &mut [RunningCommand],
deadline: Option<Instant>,
) -> Result<(Vec<ExitStatus>, bool), String> {
let mut statuses = vec![None; commands.len()];
let mut remaining = commands.len();
let mut timed_out = false;
while remaining > 0 {
for (command, status) in commands.iter_mut().zip(statuses.iter_mut()) {
if status.is_some() {
continue;
}
if let Some(completed) = command
.child
.try_wait()
.map_err(|error| format!("failed checking process status: {error}"))?
{
*status = Some(completed);
remaining -= 1;
}
}
if remaining == 0 {
break;
}
if deadline.is_some_and(|deadline| Instant::now() >= deadline) {
timed_out = true;
for (command, status) in commands.iter_mut().zip(statuses.iter()) {
if status.is_none() {
terminate_process(&mut command.child);
}
}
break;
}
std::thread::sleep(Duration::from_millis(10));
}
for (command, status) in commands.iter_mut().zip(statuses.iter_mut()) {
if status.is_none() {
*status = Some(
command
.child
.wait()
.map_err(|error| format!("failed waiting for child: {error}"))?,
);
}
}
Ok((
statuses
.into_iter()
.map(|status| status.expect("every child was waited"))
.collect(),
timed_out,
))
}
fn resolve_executable(name: &str, env: &ShellEnv) -> Result<PathBuf, String> {
if let Some(path) = env.tools.get(name) {
let path = if path.is_absolute() {
path.clone()
} else {
env.cwd.join(path)
};
return executable_file(path).ok_or_else(|| format!("{name}: command not found"));
}
if name.contains('/') || name.contains('\\') {
let candidate = resolve_path(&env.cwd, name);
return executable_file(candidate).ok_or_else(|| format!("{name}: command not found"));
}
let path = env.env.get("PATH").cloned().unwrap_or_default();
let path_ext = env
.env
.get("PATHEXT")
.cloned()
.unwrap_or_else(|| ".COM;.EXE;.BAT;.CMD".to_string());
for directory in std::env::split_paths(&path) {
let directory = if directory.is_absolute() {
directory
} else {
env.cwd.join(directory)
};
if let Some(path) = executable_file(directory.join(name)) {
return Ok(path);
}
if cfg!(windows) {
for extension in path_ext
.split(';')
.filter(|extension| !extension.is_empty())
{
if let Some(path) = executable_file(directory.join(format!("{name}{extension}"))) {
return Ok(path);
}
}
}
}
Err(format!("{name}: command not found"))
}
fn executable_file(path: PathBuf) -> Option<PathBuf> {
let metadata = path.metadata().ok()?;
if !metadata.is_file() {
return None;
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if metadata.permissions().mode() & 0o111 == 0 {
return None;
}
}
Some(path)
}
fn expand_late_substitutions(args: &mut [String], cwd: &Path) -> Result<(), String> {
for arg in args {
let (expanded, _) = expand_late_text(arg, &[], cwd, false)?;
*arg = expanded;
}
Ok(())
}
fn expand_late_argument(arg: Arg, cwd: &Path, normalize_slashes: bool) -> Result<Arg, String> {
if matches!(arg, Arg::Text(_)) && !normalize_slashes {
let mut text = [arg.into_text()];
expand_late_substitutions(&mut text, cwd)?;
return Ok(Arg::Text(text.into_iter().next().unwrap_or_default()));
}
let active_metacharacters = arg.active_metacharacters();
let (text, active_metacharacters) =
expand_late_text(arg.as_str(), &active_metacharacters, cwd, normalize_slashes)?;
Ok(Arg::from_glob_parts(text, active_metacharacters))
}
fn expand_late_text(
value: &str,
active_metacharacters: &[usize],
cwd: &Path,
normalize_slashes: bool,
) -> Result<(String, Vec<usize>), String> {
const MARKER: &[char] = &['%', '{', 'r', 'e', 'a', 'd', 'f', 'i', 'l', 'e', ':'];
let chars = value.chars().collect::<Vec<_>>();
let mut output = String::new();
let mut output_chars = 0usize;
let mut mapped_wildcards = Vec::new();
let mut index = 0usize;
while index < chars.len() {
if chars[index..].starts_with(MARKER) {
let path_start = index + MARKER.len();
let Some(path_end) = chars[path_start..]
.iter()
.position(|ch| *ch == '}')
.map(|offset| path_start + offset)
else {
return Err("readfile substitution missing '}'".to_string());
};
let mut path = chars[path_start..path_end].iter().collect::<String>();
if normalize_slashes {
path = path.replace('\\', "/");
}
let absolute = resolve_path(cwd, &path);
let contents = fs::read_to_string(&absolute).map_err(|error| {
format!(
"readfile substitution failed for '{}': {error}",
absolute.display()
)
})?;
output_chars += contents.chars().count();
output.push_str(&contents);
index = path_end + 1;
continue;
}
if active_metacharacters.binary_search(&index).is_ok() {
mapped_wildcards.push(output_chars);
}
output.push(chars[index]);
output_chars += 1;
index += 1;
}
Ok((output, mapped_wildcards))
}
fn expand_glob_argument(arg: &Arg, cwd: &Path) -> Result<String, String> {
let expanded = expand_arg_glob(arg, cwd);
if expanded.len() != 1 {
return Err(format!(
"globbing redirect '{arg}' produced multiple results: {}",
expanded.join(", ")
));
}
expanded
.into_iter()
.next()
.ok_or_else(|| "glob expansion returned no result".to_string())
}
fn resolve_path(cwd: &Path, target: &str) -> PathBuf {
let candidate = PathBuf::from(target);
if candidate.is_absolute() {
candidate
} else {
cwd.join(candidate)
}
}
fn apply_negation(code: &mut i32, negation: Negation) {
*code = match negation {
Negation::None => *code,
Negation::Even => i32::from(*code != 0),
Negation::Shell => i32::from(*code == 0),
Negation::Lit => i32::from(*code <= 0),
Negation::Crash => i32::from(!(*code < 0 || (cfg!(windows) && *code > 255))),
};
}
fn terminate_process(process: &mut Child) {
#[cfg(unix)]
{
let pid = Pid::from_raw(process.id() as i32);
match signal::killpg(pid, Signal::SIGKILL) {
Ok(()) | Err(Errno::ESRCH) => {}
Err(_) => {
let _ = process.kill();
}
}
}
#[cfg(not(unix))]
{
let _ = process.kill();
}
}
fn process_exit_code(status: ExitStatus) -> i32 {
#[cfg(unix)]
{
use std::os::unix::process::ExitStatusExt;
if let Some(signal) = status.signal() {
return -signal;
}
}
status.code().unwrap_or(128)
}
#[cfg(unix)]
unsafe extern "C" {
fn dup2(old_fd: i32, new_fd: i32) -> i32;
#[link_name = "umask"]
fn set_umask(mask: u32) -> u32;
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::path::PathBuf;
use tempfile::tempdir;
use crate::shell::environment::ShellEnv;
use crate::shell::ShParser;
#[test]
fn executes_single_command_with_not() {
let path = std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| String::from("."));
let command = "not false";
let mut parser = ShParser::new(command, false, false);
let ast = parser.parse().unwrap();
let mut env = ShellEnv::new(PathBuf::from(path), HashMap::new(), false);
let result = super::execute_script_node(&ast, &mut env, None).unwrap();
assert_eq!(result.exit_code, 0);
}
#[test]
fn executes_double_not_as_double_negation() {
let path = std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| String::from("."));
let command = "not not false";
let mut parser = ShParser::new(command, false, false);
let ast = parser.parse().unwrap();
let mut env = ShellEnv::new(PathBuf::from(path.clone()), HashMap::new(), false);
let result = super::execute_script_node(&ast, &mut env, None).unwrap();
assert_eq!(result.exit_code, 1);
let command = "not ! false";
let mut parser = ShParser::new(command, false, false);
let ast = parser.parse().unwrap();
let result = super::execute_script_node(&ast, &mut env, None).unwrap();
assert_eq!(result.exit_code, 1);
}
#[test]
fn not_crash_rejects_normal_exit() {
let path = std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| String::from("."));
let mut parser = ShParser::new("not --crash false", false, false);
let ast = parser.parse().unwrap();
let mut env = ShellEnv::new(PathBuf::from(path), HashMap::new(), false);
let normal_failure = super::execute_script_node(&ast, &mut env, None).unwrap();
assert_eq!(normal_failure.exit_code, 1);
let mut parser = ShParser::new("not --crash true", false, false);
let ast = parser.parse().unwrap();
let normal_success = super::execute_script_node(&ast, &mut env, None).unwrap();
assert_eq!(normal_success.exit_code, 1);
}
#[test]
fn executes_redirect_stderr_to_stdout() {
let root = tempdir().unwrap();
let out_path = root.path().join("out.txt");
let command = format!(
"/bin/sh -c 'printf \"stdout\\\\n\"; printf \"stderr\\\\n\" 1>&2' > {} 2>&1",
out_path.display()
);
let mut parser = ShParser::new(&command, false, false);
let ast = parser.parse().unwrap();
let mut env = ShellEnv::new(
std::env::var("CARGO_TARGET_DIR")
.unwrap_or_else(|_| String::from("."))
.into(),
HashMap::new(),
false,
);
let result = super::execute_script_node(&ast, &mut env, None).unwrap();
assert_eq!(result.exit_code, 0);
let output = std::fs::read_to_string(&out_path).unwrap_or_default();
assert!(output.contains("stdout"));
assert!(output.contains("stderr"));
}
#[test]
fn pipeline_respects_pipefail_setting() {
let root = tempdir().unwrap();
let path = root.path().to_path_buf();
let command = "false | true";
let mut parser = ShParser::new(command, false, true);
let ast = parser.parse().unwrap();
let mut env = ShellEnv::new(path.clone(), HashMap::new(), false);
let result = super::execute_script_node(&ast, &mut env, None).unwrap();
assert_eq!(result.exit_code, 1);
let mut parser = ShParser::new(command, false, false);
let ast = parser.parse().unwrap();
let mut env = ShellEnv::new(path, HashMap::new(), false);
let result = super::execute_script_node(&ast, &mut env, None).unwrap();
assert_eq!(result.exit_code, 0);
}
#[test]
fn substitution_readfile_is_applied() {
let tmp = tempdir().unwrap();
let file = tmp.path().join("x.txt");
std::fs::write(&file, "abc").unwrap();
let mut args = vec!["cat".to_string(), "%{readfile:./x.txt}".to_string()];
let cwd = tmp.path();
super::expand_late_substitutions(&mut args, cwd).unwrap();
assert_eq!(args[1], "abc");
}
}