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