forked from drewwalton19216801/rush-sh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.rs
More file actions
778 lines (694 loc) · 30.6 KB
/
command.rs
File metadata and controls
778 lines (694 loc) · 30.6 KB
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
//! Command execution module for the Rush shell.
//!
//! This module handles the execution of individual commands and pipelines.
use std::cell::RefCell;
use std::fs::File;
use std::io::pipe;
use std::os::fd::{FromRawFd, IntoRawFd, RawFd};
use std::os::unix::process::CommandExt;
use std::process::{Command, Stdio};
use std::rc::Rc;
use crate::parser::{Ast, ShellCommand};
use crate::state::ShellState;
use super::expansion::{expand_variables_in_args, expand_variables_in_string, expand_wildcards};
use super::redirection::apply_redirections;
use super::{execute, execute_compound_in_pipeline, execute_compound_with_redirections};
/// Execute the given AST and return its standard output (as produced to stdout) with trailing newlines removed.
///
/// The function runs the AST in the provided shell state and captures whatever would be written to stdout
/// (including results from pipelines, builtins, functions, subshells, and external commands). If the executed
/// AST exits with a non-zero status or fails to spawn/execute, an `Err(String)` describing the failure is returned.
///
/// # Examples
///
/// ```
/// // Note: execute_and_capture_output is a crate-internal function
/// // This example is for documentation only
/// ```
pub(crate) fn execute_and_capture_output(
ast: Ast,
shell_state: &mut ShellState,
) -> Result<String, String> {
// Create a pipe to capture stdout
let (reader, writer) = pipe().map_err(|e| format!("Failed to create pipe: {}", e))?;
// We need to capture the output, so we'll redirect stdout to our pipe
// For builtins, we can pass the writer directly
// For external commands, we need to handle them specially
match &ast {
Ast::Pipeline(commands) => {
// Handle both single commands and multi-command pipelines
if commands.is_empty() {
return Ok(String::new());
}
if commands.len() == 1 {
// Single command - use the existing optimized path
let cmd = &commands[0];
if cmd.args.is_empty() {
return Ok(String::new());
}
// Expand variables and wildcards
let var_expanded_args = expand_variables_in_args(&cmd.args, shell_state);
let expanded_args = expand_wildcards(&var_expanded_args, shell_state)
.map_err(|e| format!("Wildcard expansion failed: {}", e))?;
if expanded_args.is_empty() {
return Ok(String::new());
}
// Check if it's a function call
if shell_state.get_function(&expanded_args[0]).is_some() {
// Save previous capture state (for nested command substitutions)
let previous_capture = shell_state.capture_output.clone();
// Enable output capture mode
let capture_buffer = Rc::new(RefCell::new(Vec::new()));
shell_state.capture_output = Some(capture_buffer.clone());
// Create a FunctionCall AST and execute it
let function_call_ast = Ast::FunctionCall {
name: expanded_args[0].clone(),
args: expanded_args[1..].to_vec(),
};
let exit_code = execute(function_call_ast, shell_state);
// Retrieve captured output
let captured = capture_buffer.borrow().clone();
let output = String::from_utf8_lossy(&captured).trim_end().to_string();
// Restore previous capture state
shell_state.capture_output = previous_capture;
if exit_code == 0 {
Ok(output)
} else {
Err(format!("Function failed with exit code {}", exit_code))
}
} else if crate::builtins::is_builtin(&expanded_args[0]) {
let temp_cmd = ShellCommand {
args: expanded_args,
redirections: cmd.redirections.clone(),
compound: None,
};
// Execute builtin with our writer
let exit_code = crate::builtins::execute_builtin(
&temp_cmd,
shell_state,
Some(Box::new(writer)),
);
// Read the captured output
drop(temp_cmd); // Ensure writer is dropped
let mut output = String::new();
use std::io::Read;
let mut reader = reader;
reader
.read_to_string(&mut output)
.map_err(|e| format!("Failed to read output: {}", e))?;
if exit_code == 0 {
Ok(output.trim_end().to_string())
} else {
Err(format!("Command failed with exit code {}", exit_code))
}
} else {
// External command - execute with output capture
drop(writer); // Close writer end before spawning
let mut command = Command::new(&expanded_args[0]);
command.args(&expanded_args[1..]);
command.stdout(Stdio::piped());
command.stderr(Stdio::null()); // Suppress stderr for command substitution
// Set environment
let child_env = shell_state.get_env_for_child();
command.env_clear();
for (key, value) in child_env {
command.env(key, value);
}
let output = command
.output()
.map_err(|e| format!("Failed to execute command: {}", e))?;
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout)
.trim_end()
.to_string())
} else {
Err(format!(
"Command failed with exit code {}",
output.status.code().unwrap_or(1)
))
}
}
} else {
// Multi-command pipeline - execute the entire pipeline and capture output
drop(writer); // Close writer end before executing pipeline
// Save previous capture state (for nested command substitutions)
let previous_capture = shell_state.capture_output.clone();
// Enable output capture mode
let capture_buffer = Rc::new(RefCell::new(Vec::new()));
shell_state.capture_output = Some(capture_buffer.clone());
// Execute the pipeline
let exit_code = execute_pipeline(commands, shell_state);
// Retrieve captured output
let captured = capture_buffer.borrow().clone();
let output = String::from_utf8_lossy(&captured).trim_end().to_string();
// Restore previous capture state
shell_state.capture_output = previous_capture;
if exit_code == 0 {
Ok(output)
} else {
Err(format!("Pipeline failed with exit code {}", exit_code))
}
}
}
_ => {
// For other AST nodes (sequences, etc.), we need special handling
drop(writer);
// Save previous capture state
let previous_capture = shell_state.capture_output.clone();
// Enable output capture mode
let capture_buffer = Rc::new(RefCell::new(Vec::new()));
shell_state.capture_output = Some(capture_buffer.clone());
// Execute the AST
let exit_code = execute(ast, shell_state);
// Retrieve captured output
let captured = capture_buffer.borrow().clone();
let output = String::from_utf8_lossy(&captured).trim_end().to_string();
// Restore previous capture state
shell_state.capture_output = previous_capture;
if exit_code == 0 {
Ok(output)
} else {
Err(format!("Command failed with exit code {}", exit_code))
}
}
}
}
/// ```
/// // Note: execute_single_command is a private function
/// // This example is for documentation only
/// ```
pub(crate) fn execute_single_command(cmd: &ShellCommand, shell_state: &mut ShellState) -> i32 {
// Check if this is a compound command (subshell)
if let Some(ref compound_ast) = cmd.compound {
// Check noexec option (-n) for compound commands
// Exception: The 'set' builtin must always execute to allow disabling noexec
if shell_state.options.noexec {
return 0; // Return success without executing
}
// Execute compound command with redirections
return execute_compound_with_redirections(compound_ast, shell_state, &cmd.redirections);
}
// Check noexec option (-n): Read commands but don't execute them
// Exception: The 'set' builtin must always execute to allow disabling noexec
// IMPORTANT: Check this BEFORE processing redirections to prevent side effects
let is_set_builtin = !cmd.args.is_empty() && cmd.args[0] == "set";
if shell_state.options.noexec && !is_set_builtin {
return 0; // Return success without executing (no side effects)
}
if cmd.args.is_empty() {
// No command, but may have redirections - process them for side effects
if !cmd.redirections.is_empty()
&& let Err(e) = apply_redirections(&cmd.redirections, shell_state, None)
{
if shell_state.colors_enabled {
eprintln!(
"{}Redirection error: {}\x1b[0m",
shell_state.color_scheme.error, e
);
} else {
eprintln!("Redirection error: {}", e);
}
return 1;
}
return 0;
}
// First expand variables, then wildcards
let var_expanded_args = expand_variables_in_args(&cmd.args, shell_state);
let expanded_args = match expand_wildcards(&var_expanded_args, shell_state) {
Ok(args) => args,
Err(_) => return 1,
};
if expanded_args.is_empty() {
return 0;
}
// Print command if xtrace is enabled (-x)
if shell_state.options.xtrace {
// Get PS4 prompt (default: "+ ")
let ps4_raw = shell_state
.get_var("PS4")
.unwrap_or_else(|| "+ ".to_string());
// Expand variables in PS4 (e.g., $LINENO)
let ps4 = expand_variables_in_string(&ps4_raw, shell_state);
// Print the command with expanded arguments to stderr
let command_str = expanded_args.join(" ");
if shell_state.colors_enabled {
eprintln!(
"{}{}{}\x1b[0m",
shell_state.color_scheme.builtin, ps4, command_str
);
} else {
eprintln!("{}{}", ps4, command_str);
}
}
// Check if this is a function call
if shell_state.get_function(&expanded_args[0]).is_some() {
// This is a function call - create a FunctionCall AST node and execute it
let function_call = Ast::FunctionCall {
name: expanded_args[0].clone(),
args: expanded_args[1..].to_vec(),
};
return execute(function_call, shell_state);
}
if crate::builtins::is_builtin(&expanded_args[0]) {
// Create a temporary ShellCommand with expanded args
let temp_cmd = ShellCommand {
args: expanded_args,
redirections: cmd.redirections.clone(),
compound: None,
};
// If we're capturing output, create a writer for it
let exit_code = if let Some(ref capture_buffer) = shell_state.capture_output.clone() {
// Create a writer that writes to our capture buffer
struct CaptureWriter {
buffer: Rc<RefCell<Vec<u8>>>,
}
impl std::io::Write for CaptureWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.buffer.borrow_mut().extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let writer = CaptureWriter {
buffer: capture_buffer.clone(),
};
crate::builtins::execute_builtin(&temp_cmd, shell_state, Some(Box::new(writer)))
} else {
crate::builtins::execute_builtin(&temp_cmd, shell_state, None)
};
// Check errexit option (-e): Exit immediately if command fails
// POSIX: Don't exit in these contexts:
// 1. Inside if/while/until condition (tracked by in_condition flag)
// 2. Part of && or || chain (tracked by in_logical_chain flag)
// 3. Pipeline (except last command) - handled by pipeline executor
// 4. Negated command (tracked by in_negation flag)
if shell_state.options.errexit
&& exit_code != 0
&& !shell_state.in_condition
&& !shell_state.in_logical_chain
&& !shell_state.in_negation
{
// Set exit_requested flag to trigger shell exit
shell_state.exit_requested = true;
shell_state.exit_code = exit_code;
}
exit_code
} else {
// Separate environment variable assignments from the actual command
// Environment vars must come before the command and have the form VAR=value
let mut env_assignments = Vec::new();
let mut command_start_idx = 0;
for (idx, arg) in expanded_args.iter().enumerate() {
// Check if this looks like an environment variable assignment
if let Some(eq_pos) = arg.find('=')
&& eq_pos > 0
{
let var_part = &arg[..eq_pos];
// Check if var_part is a valid variable name
if var_part
.chars()
.next()
.map(|c| c.is_alphabetic() || c == '_')
.unwrap_or(false)
&& var_part.chars().all(|c| c.is_alphanumeric() || c == '_')
{
env_assignments.push(arg.clone());
command_start_idx = idx + 1;
continue;
}
}
// If we reach here, this is not an env assignment, so we've found the command
break;
}
// Check if we have a command to execute (vs just env assignments)
let has_command = command_start_idx < expanded_args.len();
// If all args were env assignments, set them in the shell
// but continue to process redirections per POSIX
if !has_command {
for assignment in &env_assignments {
if let Some(eq_pos) = assignment.find('=') {
let var_name = &assignment[..eq_pos];
let var_value = &assignment[eq_pos + 1..];
shell_state.set_var(var_name, var_value.to_string());
// Auto-export if allexport option (-a) is enabled
if shell_state.options.allexport {
shell_state.export_var(var_name);
}
}
}
// Process redirections even without a command
if !cmd.redirections.is_empty()
&& let Err(e) = apply_redirections(&cmd.redirections, shell_state, None)
{
if shell_state.colors_enabled {
eprintln!(
"{}Redirection error: {}\x1b[0m",
shell_state.color_scheme.error, e
);
} else {
eprintln!("Redirection error: {}", e);
}
return 1;
}
return 0;
}
// Prepare command
let mut command = Command::new(&expanded_args[command_start_idx]);
command.args(&expanded_args[command_start_idx + 1..]);
// Check for stdin override (for pipeline subshells)
if let Some(fd) = shell_state.stdin_override {
unsafe {
let dup_fd = libc::dup(fd);
if dup_fd >= 0 {
command.stdin(Stdio::from_raw_fd(dup_fd));
}
}
}
// Set environment for child process
let mut child_env = shell_state.get_env_for_child();
// Add the per-command environment variable assignments
for assignment in env_assignments {
if let Some(eq_pos) = assignment.find('=') {
let var_name = assignment[..eq_pos].to_string();
let var_value = assignment[eq_pos + 1..].to_string();
child_env.insert(var_name, var_value);
}
}
command.env_clear();
for (key, value) in child_env {
command.env(key, value);
}
// If we're capturing output, redirect stdout to capture buffer
let capturing = shell_state.capture_output.is_some();
if capturing {
command.stdout(Stdio::piped());
}
// Apply all redirections
if let Err(e) = apply_redirections(&cmd.redirections, shell_state, Some(&mut command)) {
if shell_state.colors_enabled {
eprintln!(
"{}Redirection error: {}\x1b[0m",
shell_state.color_scheme.error, e
);
} else {
eprintln!("Redirection error: {}", e);
}
return 1;
}
// Apply custom file descriptors (3-9) from fd table to external command
// We need to keep the FD table borrowed until after the child is spawned
// to prevent File handles from being dropped and FDs from being closed
let custom_fds: Vec<(i32, RawFd)> = {
let fd_table = shell_state.fd_table.borrow();
let mut fds = Vec::new();
for fd_num in 3..=9 {
if fd_table.is_open(fd_num)
&& let Some(raw_fd) = fd_table.get_raw_fd(fd_num)
{
fds.push((fd_num, raw_fd));
}
}
fds
};
// If we have custom fds to apply, use pre_exec to set them in the child
if !custom_fds.is_empty() {
unsafe {
command.pre_exec(move || {
for (target_fd, source_fd) in &custom_fds {
let result = libc::dup2(*source_fd, *target_fd);
if result < 0 {
return Err(std::io::Error::last_os_error());
}
}
Ok(())
});
}
}
// Spawn and execute the command
// Note: The FD table borrow above has been released, but the custom_fds
// closure capture keeps the file handles alive
match command.spawn() {
Ok(mut child) => {
if capturing && let Some(mut stdout) = child.stdout.take() {
use std::io::Read;
let mut output = Vec::new();
if stdout.read_to_end(&mut output).is_ok()
&& let Some(ref capture_buffer) = shell_state.capture_output
{
capture_buffer.borrow_mut().extend_from_slice(&output);
}
}
let exit_code = match child.wait() {
Ok(status) => status.code().unwrap_or(0),
Err(e) => {
if shell_state.colors_enabled {
eprintln!(
"{}Error waiting for command: {}\x1b[0m",
shell_state.color_scheme.error, e
);
} else {
eprintln!("Error waiting for command: {}", e);
}
1
}
};
// Check errexit option (-e): Exit immediately if command fails
// POSIX: Don't exit in these contexts:
// 1. Inside if/while/until condition (tracked by in_condition flag)
// 2. Part of && or || chain (tracked by in_logical_chain flag)
// 3. Pipeline (except last command) - handled by pipeline executor
// 4. Negated command (tracked by in_negation flag)
if shell_state.options.errexit
&& exit_code != 0
&& !shell_state.in_condition
&& !shell_state.in_logical_chain
&& !shell_state.in_negation
{
// Set exit_requested flag to trigger shell exit
shell_state.exit_requested = true;
shell_state.exit_code = exit_code;
}
exit_code
}
Err(e) => {
if shell_state.colors_enabled {
eprintln!(
"{}Command spawn error: {}\x1b[0m",
shell_state.color_scheme.error, e
);
} else {
eprintln!("Command spawn error: {}", e);
}
1
}
}
}
}
/// ```
/// // Note: execute_pipeline is a private function
/// // This example is for documentation only
/// ```
pub(crate) fn execute_pipeline(commands: &[ShellCommand], shell_state: &mut ShellState) -> i32 {
// Check noexec option (-n): Read commands but don't execute them
// Exception: The 'set' builtin must always execute to allow disabling noexec
// For pipelines, check if any command is 'set', otherwise skip execution
let has_set_builtin = commands
.iter()
.any(|cmd| !cmd.args.is_empty() && cmd.args[0] == "set");
if shell_state.options.noexec && !has_set_builtin {
return 0; // Return success without executing (no side effects)
}
let mut exit_code = 0;
let mut previous_stdout: Option<File> = None;
for (i, cmd) in commands.iter().enumerate() {
let is_last = i == commands.len() - 1;
if let Some(ref compound_ast) = cmd.compound {
// Execute compound command (subshell) in pipeline
let (com_exit_code, com_stdout) = execute_compound_in_pipeline(
compound_ast,
shell_state,
previous_stdout.take(),
i == 0,
is_last,
&cmd.redirections,
);
exit_code = com_exit_code;
previous_stdout = com_stdout;
continue;
}
if cmd.args.is_empty() {
continue;
}
// First expand variables, then wildcards
let var_expanded_args = expand_variables_in_args(&cmd.args, shell_state);
let expanded_args = match expand_wildcards(&var_expanded_args, shell_state) {
Ok(args) => args,
Err(_) => return 1,
};
if expanded_args.is_empty() {
continue;
}
if crate::builtins::is_builtin(&expanded_args[0]) {
// Built-ins in pipelines are tricky - for now, execute them separately
// This is not perfect but better than nothing
let temp_cmd = ShellCommand {
args: expanded_args,
redirections: cmd.redirections.clone(),
compound: None,
};
if !is_last {
// Create a safe pipe
let (reader, writer) = match pipe() {
Ok((r, w)) => (unsafe { File::from_raw_fd(r.into_raw_fd()) }, w),
Err(e) => {
if shell_state.colors_enabled {
eprintln!(
"{}Error creating pipe for builtin: {}\x1b[0m",
shell_state.color_scheme.error, e
);
} else {
eprintln!("Error creating pipe for builtin: {}", e);
}
return 1;
}
};
// Execute builtin with writer for output capture
exit_code = crate::builtins::execute_builtin(
&temp_cmd,
shell_state,
Some(Box::new(writer)),
);
// Use reader for next command's stdin
previous_stdout = Some(reader);
} else {
// Last command: check if we're capturing output
if let Some(ref capture_buffer) = shell_state.capture_output.clone() {
// Create a writer that writes to our capture buffer
struct CaptureWriter {
buffer: Rc<RefCell<Vec<u8>>>,
}
impl std::io::Write for CaptureWriter {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.buffer.borrow_mut().extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let writer = CaptureWriter {
buffer: capture_buffer.clone(),
};
exit_code = crate::builtins::execute_builtin(
&temp_cmd,
shell_state,
Some(Box::new(writer)),
);
} else {
// Not capturing, execute normally
exit_code = crate::builtins::execute_builtin(&temp_cmd, shell_state, None);
}
previous_stdout = None;
}
} else {
let mut command = Command::new(&expanded_args[0]);
command.args(&expanded_args[1..]);
// Set environment for child process
let child_env = shell_state.get_env_for_child();
command.env_clear();
for (key, value) in child_env {
command.env(key, value);
}
// Set stdin from previous command's stdout
if let Some(prev) = previous_stdout.take() {
command.stdin(Stdio::from(prev));
} else if i > 0 {
// We are in a pipeline (not first command) but have no input pipe.
// This means the previous command didn't produce a pipe.
// We should treat this as empty input (EOF), not inherit stdin!
command.stdin(Stdio::null());
} else if let Some(fd) = shell_state.stdin_override {
// We have a stdin override (e.g. from parent subshell)
// We must duplicate it because Stdio takes ownership
unsafe {
let dup_fd = libc::dup(fd);
if dup_fd >= 0 {
command.stdin(Stdio::from_raw_fd(dup_fd));
}
}
}
// Set stdout for next command, or for capturing if this is the last
if !is_last {
command.stdout(Stdio::piped());
} else if shell_state.capture_output.is_some() {
// Last command in pipeline but we're capturing output
command.stdout(Stdio::piped());
}
// Apply redirections for this command
if let Err(e) = apply_redirections(&cmd.redirections, shell_state, Some(&mut command)) {
if shell_state.colors_enabled {
eprintln!(
"{}Redirection error: {}\x1b[0m",
shell_state.color_scheme.error, e
);
} else {
eprintln!("Redirection error: {}", e);
}
return 1;
}
match command.spawn() {
Ok(mut child) => {
if !is_last {
previous_stdout = child
.stdout
.take()
.map(|s| unsafe { File::from_raw_fd(s.into_raw_fd()) });
} else if shell_state.capture_output.is_some() {
// Last command and we're capturing - read its output
if let Some(mut stdout) = child.stdout.take() {
use std::io::Read;
let mut output = Vec::new();
if stdout.read_to_end(&mut output).is_ok()
&& let Some(ref capture_buffer) = shell_state.capture_output
{
capture_buffer.borrow_mut().extend_from_slice(&output);
}
}
}
match child.wait() {
Ok(status) => {
exit_code = status.code().unwrap_or(0);
}
Err(e) => {
if shell_state.colors_enabled {
eprintln!(
"{}Error waiting for command: {}\x1b[0m",
shell_state.color_scheme.error, e
);
} else {
eprintln!("Error waiting for command: {}", e);
}
exit_code = 1;
}
}
}
Err(e) => {
if shell_state.colors_enabled {
eprintln!(
"{}Error spawning command '{}{}",
shell_state.color_scheme.error,
expanded_args[0],
&format!("': {}\x1b[0m", e)
);
} else {
eprintln!("Error spawning command '{}': {}", expanded_args[0], e);
}
exit_code = 1;
}
}
}
}
exit_code
}