Skip to main content

shrub_core/
commands.rs

1//! Command execution
2
3use std::borrow::Cow;
4use std::ffi::OsStr;
5use std::fmt::{Display, Write as _};
6use std::path::{Path, PathBuf};
7use std::process::Stdio;
8
9use itertools::Itertools;
10use shrub_parser::ast;
11use sys::commands::{CommandExt, CommandFdInjectionExt, CommandFgControlExt};
12
13use crate::extensions::{self, ShellExtensions};
14use crate::interp::{self, Execute, ProcessGroupPolicy};
15use crate::openfiles::{self, OpenFile, OpenFiles};
16use crate::results::ExecutionSpawnResult;
17use crate::{
18    ErrorKind, ExecutionControlFlow, ExecutionExitCode, ExecutionParameters, ExecutionResult,
19    Shell, ShellFd, builtins, commands, env, error, escape, functions, pathsearch, processes, sys,
20    trace_categories, traps, variables,
21};
22
23/// Encapsulates the result of waiting for a command to complete.
24pub enum CommandWaitResult {
25    /// The command completed.
26    CommandCompleted(ExecutionResult),
27    /// The command was stopped before it completed.
28    CommandStopped(ExecutionResult, processes::ChildProcess),
29}
30
31/// Represents the context for executing a command.
32pub struct ExecutionContext<'a, SE: ShellExtensions = extensions::DefaultShellExtensions> {
33    /// The shell in which the command is being executed.
34    pub shell: &'a mut Shell<SE>,
35    /// The name of the command being executed.
36    pub command_name: String,
37    /// The parameters for the execution.
38    pub params: ExecutionParameters,
39}
40
41impl<SE: ShellExtensions> ExecutionContext<'_, SE> {
42    /// Returns the standard input file; usable with `write!` et al.
43    pub fn stdin(&self) -> impl std::io::Read + 'static {
44        self.params.stdin(self.shell)
45    }
46
47    /// Returns the standard output file; usable with `write!` et al.
48    pub fn stdout(&self) -> impl std::io::Write + 'static {
49        self.params.stdout(self.shell)
50    }
51
52    /// Returns the standard error file; usable with `write!` et al.
53    pub fn stderr(&self) -> impl std::io::Write + 'static {
54        self.params.stderr(self.shell)
55    }
56
57    /// Returns the file descriptor with the given number. Returns `None`
58    /// if the file descriptor is not open.
59    ///
60    /// # Arguments
61    ///
62    /// * `fd` - The file descriptor number to retrieve.
63    pub fn try_fd(&self, fd: ShellFd) -> Option<openfiles::OpenFile> {
64        self.params.try_fd(self.shell, fd)
65    }
66
67    /// Iterates over all open file descriptors.
68    pub fn iter_fds(&self) -> impl Iterator<Item = (ShellFd, openfiles::OpenFile)> {
69        self.params.iter_fds(self.shell)
70    }
71}
72
73/// An argument to a command.
74#[derive(Clone, Debug)]
75pub enum CommandArg {
76    /// A simple string argument.
77    String(String),
78    /// An assignment/declaration; typically treated as a string, but will
79    /// be specially handled by a limited set of built-in commands.
80    Assignment(ast::Assignment),
81}
82
83impl Display for CommandArg {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        match self {
86            Self::String(s) => f.write_str(s),
87            Self::Assignment(a) => write!(f, "{a}"),
88        }
89    }
90}
91
92impl From<String> for CommandArg {
93    fn from(s: String) -> Self {
94        Self::String(s)
95    }
96}
97
98impl From<&String> for CommandArg {
99    fn from(value: &String) -> Self {
100        Self::String(value.clone())
101    }
102}
103
104impl CommandArg {
105    const fn to_string_len_hint(&self) -> usize {
106        match self {
107            Self::String(s) => s.len(),
108            Self::Assignment(_) => 0,
109        }
110    }
111
112    pub(crate) fn quote_for_tracing(&self) -> Cow<'_, str> {
113        match self {
114            Self::String(s) => escape::quote_if_needed(s, escape::QuoteMode::SingleQuote),
115            Self::Assignment(a) => {
116                let mut s = a.name.to_string();
117                let op = if a.append { "+=" } else { "=" };
118                s.push_str(op);
119                s.push_str(&escape::quote_if_needed(
120                    a.value.to_string().as_str(),
121                    escape::QuoteMode::SingleQuote,
122                ));
123                s.into()
124            }
125        }
126    }
127}
128
129/// Encapsulates a possibly-owned reference to a `Shell` for command execution.
130pub enum ShellForCommand<'a, SE: extensions::ShellExtensions> {
131    /// The command is run in the same shell as its parent; the provided
132    /// mutable reference allows modifying the parent shell.
133    ParentShell(&'a mut Shell<SE>),
134    /// The command is run in its own owned shell (which is also provided).
135    OwnedShell {
136        /// The owned shell.
137        target: Box<Shell<SE>>,
138        /// The parent shell.
139        parent: &'a mut Shell<SE>,
140    },
141}
142
143impl<SE: extensions::ShellExtensions> std::ops::Deref for ShellForCommand<'_, SE> {
144    type Target = Shell<SE>;
145
146    fn deref(&self) -> &Self::Target {
147        match self {
148            ShellForCommand::ParentShell(shell) => shell,
149            ShellForCommand::OwnedShell { target, .. } => target,
150        }
151    }
152}
153
154impl<SE: extensions::ShellExtensions> std::ops::DerefMut for ShellForCommand<'_, SE> {
155    fn deref_mut(&mut self) -> &mut Self::Target {
156        match self {
157            ShellForCommand::ParentShell(shell) => shell,
158            ShellForCommand::OwnedShell { target, .. } => target,
159        }
160    }
161}
162
163/// Composes a `std::process::Command` to execute the given command.
164/// Appropriately configures the command name and arguments, redirections,
165/// injected file descriptors, environment variables, etc.
166///
167/// # Arguments
168///
169/// * `context` - The execution context in which the command is being composed.
170/// * `command_name` - The name of the command to execute.
171/// * `argv0` - The value to use for `argv[0]` (may be different from the
172///   command).
173/// * `args` - The arguments to pass to the command.
174/// * `empty_env` - If true, the command will be executed with an empty
175///   environment; if false, the command will inherit environment variables
176///   marked as exported in the provided `Shell`.
177#[allow(unused_variables, reason = "argv0 is only used on unix platforms")]
178pub fn compose_std_command<S: AsRef<OsStr>, SE: extensions::ShellExtensions>(
179    context: &ExecutionContext<'_, SE>,
180    command_name: &str,
181    argv0: &str,
182    args: &[S],
183    empty_env: bool,
184) -> Result<std::process::Command, error::Error> {
185    let mut cmd = std::process::Command::new(command_name);
186
187    // Override argv[0].
188    // NOTE: Not supported on all platforms.
189    cmd.arg0(argv0);
190
191    // Pass through args.
192    cmd.args(args);
193
194    // Use the shell's current working dir.
195    cmd.current_dir(context.shell.working_dir());
196
197    // Start with a clear environment.
198    cmd.env_clear();
199
200    // Add in exported variables.
201    if !empty_env {
202        for (k, v) in context.shell.env().iter_exported() {
203            // NOTE: To match bash behavior, we only include exported variables
204            // that are set (i.e., have a value). This means a variable that
205            // shows up in `declare -p` but has no *set* value will be omitted.
206            if v.value().is_set() {
207                cmd.env(k.as_str(), v.value().to_cow_str(context.shell).as_ref());
208            }
209        }
210        // Set _ to the resolved command path for external commands.
211        cmd.env("_", command_name);
212    }
213
214    // Add in exported functions.
215    if !empty_env {
216        for (func_name, registration) in context.shell.funcs().iter() {
217            if registration.is_exported() {
218                let var_name = std::format!("BASH_FUNC_{func_name}%%");
219                let value = std::format!("() {}", registration.definition().body);
220                cmd.env(var_name, value);
221            }
222        }
223    }
224
225    // Redirect stdin, if applicable.
226    match context.try_fd(OpenFiles::STDIN_FD) {
227        Some(OpenFile::Stdin(_)) | None => (),
228        Some(stdin_file) => {
229            let as_stdio: Stdio = stdin_file.try_into()?;
230            cmd.stdin(as_stdio);
231        }
232    }
233
234    // Redirect stdout, if applicable.
235    match context.try_fd(OpenFiles::STDOUT_FD) {
236        Some(OpenFile::Stdout(_)) | None => (),
237        Some(stdout_file) => {
238            let as_stdio: Stdio = stdout_file.try_into()?;
239            cmd.stdout(as_stdio);
240        }
241    }
242
243    // Redirect stderr, if applicable.
244    match context.try_fd(OpenFiles::STDERR_FD) {
245        Some(OpenFile::Stderr(_)) | None => {}
246        Some(stderr_file) => {
247            let as_stdio: Stdio = stderr_file.try_into()?;
248            cmd.stderr(as_stdio);
249        }
250    }
251
252    // Inject any other fds.
253    let other_files = context.iter_fds().filter(|(fd, _)| {
254        *fd != OpenFiles::STDIN_FD && *fd != OpenFiles::STDOUT_FD && *fd != OpenFiles::STDERR_FD
255    });
256    cmd.inject_fds(other_files)?;
257
258    Ok(cmd)
259}
260
261pub(crate) async fn on_preexecute(
262    cmd: &mut commands::SimpleCommand<'_, impl extensions::ShellExtensions>,
263) -> Result<(), error::Error> {
264    // Set BASH_COMMAND before invoking the DEBUG trap (and generally before
265    // executing commands).
266    let full_cmd = format_command_args(&cmd.args);
267    cmd.shell.env_mut().update_or_add(
268        "BASH_COMMAND",
269        variables::ShellValueLiteral::Scalar(full_cmd),
270        |_| Ok(()),
271        env::EnvironmentLookup::Anywhere,
272        env::EnvironmentScope::Global,
273    )?;
274
275    // Fire the DEBUG trap if one is registered.
276    if cmd.shell.traps().handles(traps::TrapSignal::Debug) {
277        let _ = cmd
278            .shell
279            .invoke_trap_handler(traps::TrapSignal::Debug, &cmd.params)
280            .await?;
281    }
282
283    Ok(())
284}
285
286fn format_command_args(args: &[CommandArg]) -> String {
287    let capacity = args
288        .iter()
289        .map(CommandArg::to_string_len_hint)
290        .sum::<usize>()
291        + args.len().saturating_sub(1);
292    let mut result = String::with_capacity(capacity);
293
294    for (i, arg) in args.iter().enumerate() {
295        if i > 0 {
296            result.push(' ');
297        }
298        let _ = write!(result, "{arg}");
299    }
300
301    result
302}
303
304/// Represents a simple command to be executed.
305pub struct SimpleCommand<'a, SE: extensions::ShellExtensions> {
306    /// The shell to run the command in.
307    shell: ShellForCommand<'a, SE>,
308
309    /// The execution parameters for the command.
310    pub params: ExecutionParameters,
311
312    /// The name of the command to execute.
313    pub command_name: String,
314
315    /// The arguments to the command, including the command itself.
316    pub args: Vec<CommandArg>,
317
318    /// Whether to consider shell functions when looking up the command name.
319    /// If true, shell functions will be checked; if false, they will be
320    /// ignored.
321    pub use_functions: bool,
322
323    /// Optional list of directories to search for external commands. If left
324    /// `None`, the default search logic will be used.
325    pub path_dirs: Option<Vec<PathBuf>>,
326
327    /// The process group ID to use for externally executed commands. This may
328    /// be `None`, in which case the default behavior will be used.
329    pub process_group_id: Option<i32>,
330
331    /// Optional override for the `argv[0]` value presented to an externally
332    /// spawned process. When `None`, `command_name` is used.
333    pub argv0: Option<String>,
334}
335
336impl<'a, SE: extensions::ShellExtensions> SimpleCommand<'a, SE> {
337    /// Creates a new `SimpleCommand` instance.
338    ///
339    /// # Arguments
340    ///
341    /// * `shell` - The shell in which to execute the command.
342    /// * `params` - The execution parameters for the command.
343    /// * `command_name` - The name of the command to execute.
344    /// * `args` - The arguments to the command, including the command itself.
345    pub fn new<I>(
346        shell: ShellForCommand<'a, SE>,
347        params: ExecutionParameters,
348        command_name: String,
349        args: I,
350    ) -> Self
351    where
352        I: IntoIterator<Item = CommandArg>,
353    {
354        Self {
355            shell,
356            params,
357            command_name,
358            args: args.into_iter().collect(),
359            use_functions: true,
360            path_dirs: None,
361            process_group_id: None,
362            argv0: None,
363        }
364    }
365
366    /// Executes the simple command.
367    ///
368    /// The command may be a builtin, a shell function, or an externally
369    /// executed command. This function's implementation is responsible for
370    /// dispatching it appropriately according to the context provided.
371    #[allow(
372        clippy::missing_panics_doc,
373        reason = "these unwrap calls should not panic"
374    )]
375    pub async fn execute(mut self) -> Result<ExecutionSpawnResult, error::Error> {
376        // First see if it's the name of a builtin.
377        let builtin = self.shell.builtins().get(&self.command_name).cloned();
378
379        // If we're in POSIX mode and found a special builtin (that's not
380        // disabled), then invoke it without considering functions.
381        if self.shell.options().posix_mode
382            && builtin
383                .as_ref()
384                .is_some_and(|r| !r.disabled && r.special_builtin)
385        {
386            #[allow(clippy::unwrap_used, reason = "we just checked that builtin is Some")]
387            let builtin = builtin.unwrap();
388            return self.execute_via_builtin(builtin).await;
389        }
390
391        // Assuming we weren't requested not to do so, check if it's the name of
392        // a shell function.
393        if self.use_functions {
394            if let Some(func_registration) =
395                self.shell.funcs().get(self.command_name.as_str()).cloned()
396            {
397                return self.execute_via_function(func_registration).await;
398            }
399        }
400
401        // If we haven't yet resolved the command name and found a builtin
402        // that's not disabled, then invoke it.
403        if let Some(builtin) = builtin {
404            if !builtin.disabled {
405                return self.execute_via_builtin(builtin).await;
406            }
407        }
408
409        // We still haven't found a command to invoke. We'll need to look for an
410        // external command.
411        if !sys::fs::contains_path_separator(&self.command_name) {
412            // All else failed; if we were given path directories to search,
413            // look through them for a match. Otherwise, use our
414            // default search logic.
415            let path = if let Some(path_dirs) = &self.path_dirs {
416                pathsearch::resolve_command(path_dirs, self.command_name.as_str())
417            } else {
418                self.shell
419                    .resolve_command_in_path_using_cache(&self.command_name)
420            };
421
422            if let Some(path) = path {
423                self.execute_via_external(&path)
424            } else {
425                // Bash updates $_ even when the command is not found, so mirror
426                // that here before reporting the error.
427                let last_arg = Self::take_last_arg(&self.args);
428                self.shell.update_last_arg_variable(last_arg);
429
430                Err(ErrorKind::CommandNotFound(self.command_name).into())
431            }
432        } else {
433            let command_name = sys::fs::resolve_executable_path(
434                PathBuf::from(&self.command_name),
435                self.shell.working_dir(),
436            );
437            self.execute_via_external(command_name.as_path())
438        }
439    }
440
441    /// Extracts the owned string representation of the last argument of a
442    /// command, suitable for recording into `$_`.
443    fn take_last_arg(args: &[CommandArg]) -> Option<String> {
444        args.last().map(ToString::to_string)
445    }
446
447    async fn execute_via_builtin(
448        self,
449        builtin: builtins::Registration<SE>,
450    ) -> Result<ExecutionSpawnResult, error::Error> {
451        match self.shell {
452            ShellForCommand::OwnedShell { target, .. } => {
453                Ok(Self::execute_via_builtin_in_owned_shell(
454                    *target,
455                    self.params,
456                    builtin,
457                    self.command_name,
458                    self.args,
459                )
460                .await)
461            }
462            ShellForCommand::ParentShell(..) => {
463                self.execute_via_builtin_in_parent_shell(builtin).await
464            }
465        }
466    }
467
468    // Not an `async fn`: its future would hold `shell`, and that would grow the
469    // future of every simple command, not just this rare case.
470    fn execute_via_builtin_in_owned_shell(
471        mut shell: Shell<SE>,
472        params: ExecutionParameters,
473        builtin: builtins::Registration<SE>,
474        command_name: String,
475        args: Vec<CommandArg>,
476    ) -> impl Future<Output = ExecutionSpawnResult> + use<SE> {
477        let last_arg = Self::take_last_arg(&args);
478        // Builtins may block (e.g., reading input), so keep them from holding
479        // up the rest of the pipeline.
480        let task = sys::tasks::spawn_blocking(async move {
481            let cmd_context = ExecutionContext {
482                shell: &mut shell,
483                command_name,
484                params,
485            };
486
487            let result = execute_builtin_command(&builtin, cmd_context, args).await;
488
489            // Update $_ after command execution.
490            shell.update_last_arg_variable(last_arg);
491
492            result
493        });
494
495        async move { ExecutionSpawnResult::StartedTask(task.await) }
496    }
497
498    async fn execute_via_builtin_in_parent_shell(
499        self,
500        builtin: builtins::Registration<SE>,
501    ) -> Result<ExecutionSpawnResult, error::Error> {
502        let mut shell = self.shell;
503        let last_arg = Self::take_last_arg(&self.args);
504
505        let cmd_context = ExecutionContext {
506            shell: &mut shell,
507            command_name: self.command_name,
508            params: self.params,
509        };
510
511        let result = execute_builtin_command(&builtin, cmd_context, self.args).await;
512
513        // Update $_ after command execution.
514        shell.update_last_arg_variable(last_arg);
515
516        let result = result?;
517
518        Ok(result.into())
519    }
520
521    async fn execute_via_function(
522        self,
523        func_registration: functions::Registration,
524    ) -> Result<ExecutionSpawnResult, error::Error> {
525        match self.shell {
526            ShellForCommand::OwnedShell { target, .. } => {
527                Ok(Self::execute_via_function_in_owned_shell(
528                    *target,
529                    self.params,
530                    func_registration,
531                    self.command_name,
532                    self.args,
533                )
534                .await)
535            }
536            ShellForCommand::ParentShell(..) => {
537                self.execute_via_function_in_parent_shell(func_registration)
538                    .await
539            }
540        }
541    }
542
543    // As a non-last pipeline stage, a function must run as a background task
544    // (mirroring `execute_via_builtin_in_owned_shell` above) rather than
545    // being awaited inline in `spawn_pipeline_processes`'s own per-stage
546    // loop: awaiting it inline there blocks that loop until the function's
547    // body fully returns, so the *next* pipeline stage (the one that would
548    // actually drain this function's stdout pipe) is never even spawned
549    // until this one is done. A function that writes more to its stdout
550    // pipe than the OS pipe buffer holds (~64KiB on Linux) before returning
551    // then blocks on that `write()` forever, since nothing is reading the
552    // other end yet -- a plain deadlock, not a slow completion. Confirmed
553    // empirically with a two-line repro having nothing to do with any
554    // external command or builtin: `f() { for i in $(seq 1 5000); do echo
555    // line $i; done; }; f | cat` hangs indefinitely. Real bash always runs
556    // every pipeline stage as its own concurrently-running process, so this
557    // case never arises there.
558    fn execute_via_function_in_owned_shell(
559        mut shell: Shell<SE>,
560        params: ExecutionParameters,
561        func_registration: functions::Registration,
562        command_name: String,
563        args: Vec<CommandArg>,
564    ) -> impl Future<Output = ExecutionSpawnResult> + use<SE> {
565        let last_arg = Self::take_last_arg(&args);
566        let task = sys::tasks::spawn_blocking(async move {
567            let cmd_context = ExecutionContext {
568                shell: &mut shell,
569                command_name,
570                params,
571            };
572
573            let spawn_result =
574                invoke_shell_function(func_registration, cmd_context, &args[1..]).await;
575
576            // Update $_ after command execution.
577            shell.update_last_arg_variable(last_arg);
578
579            match spawn_result? {
580                ExecutionSpawnResult::Completed(result) => Ok(result),
581                ExecutionSpawnResult::StartedProcess(_) | ExecutionSpawnResult::StartedTask(_) => {
582                    // invoke_shell_function() only ever returns Completed
583                    // today; this guards against a silent behavior change
584                    // rather than dropping already-started work.
585                    error::unimp("invoke_shell_function returned an unexpected spawn result")
586                }
587            }
588        });
589
590        async move { ExecutionSpawnResult::StartedTask(task.await) }
591    }
592
593    async fn execute_via_function_in_parent_shell(
594        self,
595        func_registration: functions::Registration,
596    ) -> Result<ExecutionSpawnResult, error::Error> {
597        let mut shell = self.shell;
598        let last_arg = Self::take_last_arg(&self.args);
599
600        let cmd_context = ExecutionContext {
601            shell: &mut shell,
602            command_name: self.command_name,
603            params: self.params,
604        };
605
606        // Strip the function name off args.
607        let result = invoke_shell_function(func_registration, cmd_context, &self.args[1..]).await;
608
609        // $_ is reset *after* the function body runs, to the last argument of
610        // the invocation (or the function name itself if zero args). Any
611        // mutations made inside the body are overwritten — this matches bash,
612        // where the caller observes only the invocation's last argument.
613        shell.update_last_arg_variable(last_arg);
614
615        result
616    }
617
618    fn execute_via_external(self, path: &Path) -> Result<ExecutionSpawnResult, error::Error> {
619        let mut shell = self.shell;
620        let last_arg = Self::take_last_arg(&self.args);
621
622        let cmd_context = ExecutionContext {
623            shell: &mut shell,
624            command_name: self.command_name,
625            params: self.params,
626        };
627
628        let resolved_path = path.to_string_lossy();
629        let result = execute_external_command(
630            cmd_context,
631            resolved_path.as_ref(),
632            self.process_group_id,
633            self.argv0.as_deref(),
634            &self.args[1..],
635        );
636
637        // Update $_ after command execution.
638        shell.update_last_arg_variable(last_arg);
639
640        result
641    }
642}
643
644pub(crate) fn execute_external_command(
645    context: ExecutionContext<'_, impl extensions::ShellExtensions>,
646    executable_path: &str,
647    process_group_id: Option<i32>,
648    argv0_override: Option<&str>,
649    args: &[CommandArg],
650) -> Result<ExecutionSpawnResult, error::Error> {
651    // Filter out the args; we only want strings.
652    let cmd_args = args
653        .iter()
654        .filter_map(|e| {
655            if let CommandArg::String(s) = e {
656                Some(s)
657            } else {
658                None
659            }
660        })
661        .collect::<Vec<_>>();
662
663    // Before we lose ownership of the open files, figure out if stdin will be a
664    // terminal.
665    let child_stdin_is_terminal = context
666        .try_fd(openfiles::OpenFiles::STDIN_FD)
667        .is_some_and(|f| f.is_terminal());
668
669    // Figure out if we should be setting up a new process group.
670    let new_pg = matches!(
671        context.params.process_group_policy,
672        ProcessGroupPolicy::NewProcessGroup
673    );
674
675    // Compose the std::process::Command that encapsulates what we want to
676    // launch. argv[0] defaults to context.command_name (the user-facing
677    // name of the command) unless the caller specified an explicit
678    // override.
679    let argv0 = argv0_override.unwrap_or(context.command_name.as_str());
680    #[allow(unused_mut, reason = "only mutated on unix platforms")]
681    let mut cmd = compose_std_command(
682        &context,
683        executable_path,
684        argv0,
685        cmd_args.as_slice(),
686        false, /* empty environment? */
687    )?;
688
689    // Set up process group state.
690    if new_pg {
691        // Check if we'll be doing terminal control setup (which includes
692        // setsid)
693        if child_stdin_is_terminal && context.shell.options().external_cmd_leads_session {
694            // Don't set process_group(0) - setsid() in pre_exec will handle it
695            cmd.lead_session();
696        } else {
697            // Normal case: create new process group in current session
698            cmd.process_group(0);
699            if child_stdin_is_terminal {
700                cmd.take_foreground();
701            }
702        }
703    } else {
704        // We need to join an established process group.
705        if let Some(pgid) = process_group_id {
706            cmd.process_group(pgid);
707        }
708    }
709
710    // When tracing is enabled, report.
711    tracing::debug!(
712        target: trace_categories::COMMANDS,
713        "Spawning: cmd='{} {}'",
714        cmd.get_program().to_string_lossy().to_string(),
715        cmd.get_args()
716            .map(|a| a.to_string_lossy().to_string())
717            .join(" ")
718    );
719
720    match sys::process::spawn(cmd, context.shell.options().kill_external_commands_on_drop) {
721        Ok(child) => {
722            // Retrieve the pid.
723            #[expect(clippy::cast_possible_wrap)]
724            let pid = child.id().map(|id| id as i32);
725            let mut actual_pgid = process_group_id;
726            if let Some(pid) = &pid {
727                if new_pg {
728                    actual_pgid = Some(*pid);
729                }
730            } else {
731                tracing::warn!("could not retrieve pid for child process");
732            }
733
734            Ok(ExecutionSpawnResult::StartedProcess(
735                processes::ChildProcess::new(child, pid, actual_pgid),
736            ))
737        }
738        Err(spawn_err) => {
739            if context.shell.options().interactive {
740                sys::terminal::move_self_to_foreground()?;
741            }
742
743            if spawn_err.kind() == std::io::ErrorKind::NotFound {
744                if !context.shell.working_dir().exists() {
745                    Err(
746                        error::ErrorKind::WorkingDirMissing(context.shell.working_dir().to_owned())
747                            .into(),
748                    )
749                } else {
750                    Err(error::ErrorKind::CommandNotFound(context.command_name).into())
751                }
752            } else {
753                Err(
754                    error::ErrorKind::FailedToExecuteCommand(context.command_name, spawn_err)
755                        .into(),
756                )
757            }
758        }
759    }
760}
761
762async fn execute_builtin_command<SE: extensions::ShellExtensions>(
763    builtin: &builtins::Registration<SE>,
764    context: ExecutionContext<'_, SE>,
765    args: Vec<CommandArg>,
766) -> Result<ExecutionResult, error::Error> {
767    // In POSIX mode, special builtins that return errors are to be treated as
768    // fatal.
769    let mark_errors_fatal = builtin.special_builtin && context.shell.options().posix_mode;
770
771    match (builtin.execute_func)(context, args).await {
772        Ok(result) => Ok(result),
773        Err(e) => {
774            // Broken pipe errors should silently return the appropriate exit
775            // code
776            if let Some(io_err) = e.as_io_error() {
777                if io_err.kind() == std::io::ErrorKind::BrokenPipe {
778                    return Ok(ExecutionExitCode::from(io_err).into());
779                }
780            }
781
782            Err(if mark_errors_fatal { e.into_fatal() } else { e })
783        }
784    }
785}
786
787pub(crate) async fn invoke_shell_function(
788    function: functions::Registration,
789    mut context: ExecutionContext<'_, impl extensions::ShellExtensions>,
790    args: &[CommandArg],
791) -> Result<ExecutionSpawnResult, error::Error> {
792    let ast::FunctionBody(body, redirects) = &function.definition().body;
793
794    // Apply any redirects specified at function definition-time.
795    if let Some(redirects) = redirects {
796        for redirect in &redirects.0 {
797            interp::setup_redirect(context.shell, &mut context.params, redirect).await?;
798        }
799    }
800
801    let positional_args = args.iter().map(|a| a.to_string());
802
803    // Enter the function; it's left however its body ends, even if it's
804    // cancelled.
805    let mut shell =
806        context
807            .shell
808            .enter_function(context.command_name.as_str(), &function, positional_args)?;
809
810    // A function executes within the current shell process and shares its
811    // caller's open files, so the parameters are passed through by shared
812    // reference rather than cloned. This prevents direct mutation of the
813    // caller's `ExecutionParameters` open-file table, though the function
814    // may still change the shell's persistent open files via builtins (e.g.
815    // `exec`).
816    let result = body.execute(&mut shell, &context.params).await;
817
818    // Leave the function: pop its frame and its local variables.
819    drop(shell);
820
821    // Get the actual execution result from the body of the function.
822    let mut result = result?;
823
824    // Handle control-flow.
825    match result.next_control_flow {
826        ExecutionControlFlow::BreakLoop { .. } | ExecutionControlFlow::ContinueLoop { .. } => {
827            return error::unimp("break or continue returned from function invocation");
828        }
829        ExecutionControlFlow::ReturnFromFunctionOrScript => {
830            // It's now been handled.
831            result.next_control_flow = ExecutionControlFlow::Normal;
832        }
833        _ => {}
834    }
835
836    Ok(result.into())
837}
838
839pub(crate) async fn invoke_command_in_subshell_and_get_output(
840    shell: &mut Shell<impl extensions::ShellExtensions>,
841    params: &ExecutionParameters,
842    s: String,
843) -> Result<String, error::Error> {
844    // Instantiate a subshell to run the command in.
845    let mut subshell = shell.clone();
846
847    // Command substitutions don't inherit errexit by default. Only inherit it
848    // when command_subst_inherits_errexit is enabled, otherwise disable
849    // errexit in the subshell.
850    if !shell.options().command_subst_inherits_errexit {
851        subshell.options_mut().exit_on_nonzero_command_exit = false;
852    }
853
854    // Get our own set of parameters we can customize and use.
855    let mut params = params.clone();
856    params.process_group_policy = ProcessGroupPolicy::SameProcessGroup;
857
858    // Set up pipe so we can read the output.
859    let (reader, writer) = sys::pipes::pipe()?;
860    params.set_fd(OpenFiles::STDOUT_FD, writer.into());
861
862    let mut async_reader = sys::async_pipe::AsyncPipeReader::new(reader)?;
863
864    let cmd_join_handle = sys::tasks::spawn(run_substitution_command(subshell, params, s));
865
866    let output_str = async_reader.read_to_string().await?;
867
868    // Now observe the command's completion.
869    let run_result = cmd_join_handle.await?;
870    let cmd_result = run_result?;
871
872    // Store the status.
873    shell.set_last_exit_status(cmd_result.exit_code.into());
874
875    // Note: $_ is naturally isolated from the parent because we cloned the
876    // shell to run the substitution.
877
878    Ok(output_str)
879}
880
881async fn run_substitution_command(
882    mut shell: Shell<impl extensions::ShellExtensions>,
883    mut params: ExecutionParameters,
884    command: String,
885) -> Result<ExecutionResult, error::Error> {
886    // Parse the string into a whole shell program.
887    let parse_result = shell.parse_string(command);
888
889    // Check for a command that is only an input redirection ("< file").
890    // If detected, emulate `cat file` to stdout and return immediately.
891    // If we failed to parse, then we'll fall below and handle it there.
892    if let Ok(program) = &parse_result {
893        if let Some(redir) = try_unwrap_bare_input_redir_program(program) {
894            interp::setup_redirect(&mut shell, &mut params, redir).await?;
895            std::io::copy(&mut params.stdin(&shell), &mut params.stdout(&shell))?;
896            return Ok(ExecutionResult::new(0));
897        }
898    }
899
900    // TODO(source-info): review this
901    let source_info = crate::SourceInfo::from("main");
902
903    // Handle the parse result using default shell behavior.
904    shell
905        .run_parsed_result(parse_result, &source_info, &params)
906        .await
907}
908
909// Detects a subshell command that consists solely of a single input redirection
910// (e.g., "< file"), returning the IoRedirect when present.
911fn try_unwrap_bare_input_redir_program(program: &ast::Program) -> Option<&ast::IoRedirect> {
912    // We're looking for exactly one complete command...
913    let [complete] = program.complete_commands.as_slice() else {
914        return None;
915    };
916
917    // ...a single list item...
918    let ast::CompoundList(items) = complete;
919    let [item] = items.as_slice() else {
920        return None;
921    };
922
923    // ...with a single pipeline (no && or || chaining)...
924    let and_or = &item.0;
925    if !and_or.additional.is_empty() {
926        return None;
927    }
928
929    // ...not negated...
930    let pipeline = &and_or.first;
931    if pipeline.bang {
932        return None;
933    }
934
935    // ...with a single command in the pipeline...
936    let [ast::Command::Simple(simple_cmd)] = pipeline.seq.as_slice() else {
937        return None;
938    };
939
940    // ...with no program word/name and no suffix...
941    if simple_cmd.word_or_name.is_some() || simple_cmd.suffix.is_some() {
942        return None;
943    }
944
945    // ...and exactly one prefix containing an I/O redirect...
946    let prefix = simple_cmd.prefix.as_ref()?;
947    let [ast::CommandPrefixOrSuffixItem::IoRedirect(redir)] = prefix.0.as_slice() else {
948        return None;
949    };
950
951    // ...that is a file input redirection to a filename, targeting stdin.
952    match redir {
953        ast::IoRedirect::File(
954            fd,
955            ast::IoFileRedirectKind::Read,
956            ast::IoFileRedirectTarget::Filename(..),
957        ) if fd.is_none_or(|fd| fd == openfiles::OpenFiles::STDIN_FD) => Some(redir),
958        _ => None,
959    }
960}