litnik 0.2.1

An embedded Lit and FileCheck runner for compiler test suites.
Documentation
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
use std::fs::{self, File};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command as SysCommand, ExitStatus, Stdio};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::shell::ast::{Arg, Command, Pipeline, Redirect, ScriptNode, SeqOp};
use crate::shell::builtins::{is_inproc_builtin, run_inproc_builtin};
use crate::shell::environment::{expand_arg_glob, ShellCommandHandler, ShellEnv};

#[cfg(unix)]
use nix::errno::Errno;
#[cfg(unix)]
use nix::sys::signal::{self, Signal};
#[cfg(unix)]
use nix::unistd::Pid;
#[cfg(unix)]
use std::os::unix::process::CommandExt;

/// The observable result of one pipeline stage.
#[derive(Debug, Clone)]
pub struct CommandResult {
    /// Captured standard output decoded with replacement characters.
    pub stdout: String,
    /// Captured standard error decoded with replacement characters.
    pub stderr: String,
    /// Process-style status after applying `not` or `!`.
    pub exit_code: i32,
    /// Whether the script deadline expired while this stage was running.
    pub timed_out: bool,
}

/// Result of executing one parsed shell expression.
#[derive(Debug, Clone)]
pub struct ScriptResult {
    /// Status of the final expression, after pipefail and negation handling.
    pub exit_code: i32,
    /// Output that reached the script's standard output.
    pub stdout: String,
    /// Standard error captured from executed stages.
    pub stderr: String,
    /// Whether the expression exceeded its deadline.
    pub timed_out: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CaptureStream {
    Stdout,
    Stderr,
}

struct OpenSink {
    file: Option<File>,
    path: PathBuf,
    capture: Option<CaptureStream>,
}

impl OpenSink {
    fn stdio(&self) -> Result<Stdio, String> {
        self.file
            .as_ref()
            .ok_or_else(|| "output sink is closed".to_string())?
            .try_clone()
            .map(Stdio::from)
            .map_err(|error| format!("failed to clone output sink: {error}"))
    }

    fn write_all(&self, data: &[u8]) -> Result<(), String> {
        let mut file = self
            .file
            .as_ref()
            .ok_or_else(|| "output sink is closed".to_string())?;
        file.write_all(data)
            .map_err(|error| format!("failed to write '{}': {error}", self.path.display()))
    }
}

impl Drop for OpenSink {
    fn drop(&mut self) {
        self.file.take();
        if self.capture.is_some() {
            let _ = fs::remove_file(&self.path);
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum OutputTarget {
    Pipe,
    Sink(usize),
}

struct RedirectLayout {
    stdin_path: Option<PathBuf>,
    stdout: OutputTarget,
    stderr: OutputTarget,
    sinks: Vec<OpenSink>,
}

impl RedirectLayout {
    fn captured(&self) -> Result<(Vec<u8>, Vec<u8>), String> {
        let mut stdout = Vec::new();
        let mut stderr = Vec::new();
        for sink in &self.sinks {
            let Some(stream) = sink.capture else {
                continue;
            };
            let data = fs::read(&sink.path)
                .map_err(|error| format!("failed to read '{}': {error}", sink.path.display()))?;
            match stream {
                CaptureStream::Stdout => stdout.extend(data),
                CaptureStream::Stderr => stderr.extend(data),
            }
        }
        Ok((stdout, stderr))
    }

    fn write(&self, target: OutputTarget, data: &[u8]) -> Result<(), String> {
        match target {
            OutputTarget::Pipe => Err("in-process pipeline output was not buffered".to_string()),
            OutputTarget::Sink(index) => self
                .sinks
                .get(index)
                .ok_or_else(|| "invalid output sink".to_string())?
                .write_all(data),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Negation {
    None,
    Shell,
    Lit,
    Crash,
    Even,
}

#[derive(Clone)]
struct ParsedCommand {
    command: Command,
    env: ShellEnv,
    args: Vec<String>,
    negation: Negation,
    env_prefix_used: bool,
    env_print: bool,
}

struct StageRecord {
    result: CommandResult,
    stdout_bytes: Vec<u8>,
    stdout_visible: bool,
}

struct RunningCommand {
    child: Child,
    parsed: ParsedCommand,
    redirects: RedirectLayout,
}

struct RunningPipeline {
    commands: Vec<RunningCommand>,
    complete: bool,
}

impl Drop for RunningPipeline {
    fn drop(&mut self) {
        if self.complete {
            return;
        }
        for command in &mut self.commands {
            if command.child.try_wait().ok().flatten().is_none() {
                terminate_process(&mut command.child);
                let _ = command.child.wait();
            }
        }
    }
}

static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);

/// Executes a parsed expression while preserving mutations to `shenv` made by
/// standalone stateful builtins.
///
/// External pipeline stages run concurrently. Embedded handlers receive
/// buffered input at their process-graph boundary.
///
/// # Errors
///
/// Returns an error when the expression requests background execution with
/// `&`. Command lookup, redirection, process creation, and embedded-command
/// errors produce status 127 so that `||` and `;` retain shell control-flow
/// behavior.
///
/// # Blocking
///
/// This function blocks the current thread until the expression finishes or
/// the timeout expires. Embedded handlers execute synchronously and cannot be
/// preempted while inside [`ShellCommandHandler::execute`].
pub fn execute_script_node(
    script: &ScriptNode,
    shenv: &mut ShellEnv,
    timeout: Option<Duration>,
) -> Result<ScriptResult, String> {
    let deadline = timeout
        .filter(|duration| !duration.is_zero())
        .and_then(|duration| Instant::now().checked_add(duration));
    execute_script_node_before(script, shenv, deadline)
}

fn execute_script_node_before(
    script: &ScriptNode,
    shenv: &mut ShellEnv,
    deadline: Option<Instant>,
) -> Result<ScriptResult, String> {
    match script {
        ScriptNode::Pipeline(pipeline) => match execute_pipeline(pipeline, shenv, deadline) {
            Ok(result) => Ok(result),
            Err(error) => Ok(ScriptResult {
                exit_code: if pipeline.negate { 0 } else { 127 },
                stdout: String::new(),
                stderr: format!("{error}\n"),
                timed_out: false,
            }),
        },
        ScriptNode::Seq(lhs, op, rhs) => {
            let left = execute_script_node_before(lhs, shenv, deadline)?;
            if left.timed_out {
                return Ok(left);
            }

            let execute_right = match op {
                SeqOp::Semicolon => true,
                SeqOp::Ampersand => {
                    return Err("unsupported shell operator: '&'".to_string());
                }
                SeqOp::Or => left.exit_code != 0,
                SeqOp::And => left.exit_code == 0,
            };
            if !execute_right {
                return Ok(left);
            }

            let right = execute_script_node_before(rhs, shenv, deadline)?;
            let mut stdout = left.stdout;
            stdout.push_str(&right.stdout);
            let mut stderr = left.stderr;
            stderr.push_str(&right.stderr);
            Ok(ScriptResult {
                exit_code: right.exit_code,
                stdout,
                stderr,
                timed_out: right.timed_out,
            })
        }
    }
}

fn execute_pipeline(
    pipeline: &Pipeline,
    shenv: &mut ShellEnv,
    deadline: Option<Instant>,
) -> Result<ScriptResult, String> {
    let mut parsed = pipeline
        .commands
        .iter()
        .map(|command| parse_command(command, shenv))
        .collect::<Result<Vec<_>, _>>()?;
    let command_count = parsed.len();
    let mut records = Vec::with_capacity(command_count);
    let mut input = None;
    let mut external = Vec::new();
    let mut timed_out = false;

    for (index, command) in parsed.drain(..).enumerate() {
        let command_name = command.args.first().map(String::as_str).unwrap_or("env");
        let embedded = command.env.commands.get(command_name).cloned();
        let builtin = command.env_print || is_inproc_builtin(command_name);

        if embedded.is_none() && !builtin {
            external.push(command);
            continue;
        }

        if !external.is_empty() {
            let execution = execute_external_segment(&external, input.take(), false, deadline);
            let (next_input, segment_timed_out) = match execution {
                Ok((mut completed, next_input, segment_timed_out)) => {
                    records.append(&mut completed);
                    (next_input, segment_timed_out)
                }
                Err(error) => {
                    let negation = external
                        .last()
                        .map_or(Negation::None, |command| command.negation);
                    records.push(failed_stage(error, negation, false));
                    (Vec::new(), false)
                }
            };
            external.clear();
            input = Some(next_input);
            if segment_timed_out {
                timed_out = true;
                break;
            }
        }

        let is_pipeline_last = index + 1 == command_count;
        let negation = command.negation;
        let record = match execute_in_process_stage(
            command,
            embedded,
            input.take(),
            command_count > 1,
            is_pipeline_last,
            deadline,
            shenv,
        ) {
            Ok(record) => record,
            Err(error) => failed_stage(error, negation, is_pipeline_last),
        };
        timed_out |= record.result.timed_out;
        input = Some(record.stdout_bytes.clone());
        records.push(record);
        if timed_out {
            break;
        }
    }

    if !timed_out && !external.is_empty() {
        match execute_external_segment(&external, input.take(), true, deadline) {
            Ok((mut completed, _, segment_timed_out)) => {
                timed_out = segment_timed_out;
                records.append(&mut completed);
            }
            Err(error) => {
                let negation = external
                    .last()
                    .map_or(Negation::None, |command| command.negation);
                records.push(failed_stage(error, negation, true));
            }
        }
    }

    let mut stdout = String::new();
    let mut stderr = String::new();
    for record in &records {
        if record.stdout_visible {
            stdout.push_str(&record.result.stdout);
        }
        stderr.push_str(&record.result.stderr);
    }

    let mut exit_code = if pipeline.pipe_fail {
        records
            .iter()
            .rev()
            .find(|record| record.result.exit_code != 0)
            .map(|record| record.result.exit_code)
            .unwrap_or(0)
    } else {
        records
            .last()
            .map(|record| record.result.exit_code)
            .unwrap_or(0)
    };
    if pipeline.negate {
        exit_code = i32::from(exit_code == 0);
    }

    Ok(ScriptResult {
        exit_code,
        stdout,
        stderr,
        timed_out,
    })
}

fn failed_stage(error: String, negation: Negation, stdout_visible: bool) -> StageRecord {
    let mut exit_code = 127;
    apply_negation(&mut exit_code, negation);
    StageRecord {
        result: CommandResult {
            stdout: String::new(),
            stderr: format!("{error}\n"),
            exit_code,
            timed_out: false,
        },
        stdout_bytes: Vec::new(),
        stdout_visible,
    }
}

fn parse_command(command: &Command, shenv: &ShellEnv) -> Result<ParsedCommand, String> {
    let mut args = command.args.clone();
    let mut env = shenv.clone();
    let mut not_count = 0usize;
    let mut saw_lit_not = false;
    let mut crash = false;
    let mut env_prefix_used = false;
    let mut pos = 0usize;

    while pos < args.len() {
        match args[pos].as_str() {
            "not" => {
                saw_lit_not = true;
                not_count += 1;
                pos += 1;
                if pos < args.len() && args[pos].as_str() == "--crash" {
                    crash = true;
                    pos += 1;
                }
            }
            "!" => {
                not_count += 1;
                pos += 1;
            }
            "env" => {
                env_prefix_used = true;
                pos += 1;
                while pos < args.len() {
                    let value = args[pos].as_str();
                    if value == "-i" {
                        env.env.clear();
                        pos += 1;
                    } else if value == "-u" {
                        let name = args
                            .get(pos + 1)
                            .ok_or_else(|| "'env -u' requires a variable name".to_string())?;
                        env.env.remove(name.as_str());
                        pos += 2;
                    } else if let Some((key, value)) = value.split_once('=') {
                        env.env.insert(key.to_string(), value.to_string());
                        pos += 1;
                    } else {
                        break;
                    }
                }
            }
            _ => break,
        }
    }

    let env_print = env_prefix_used && pos == args.len();
    if pos == args.len() && !env_print {
        return Err("empty command".to_string());
    }

    let mut expanded = Vec::new();
    for (index, arg) in args.split_off(pos).into_iter().enumerate() {
        let arg = expand_late_argument(arg, &env.cwd, env.normalize_slashes)?;
        let matches = expand_arg_glob(&arg, &env.cwd);
        if index == 0 {
            expanded.extend(matches.into_iter().take(1));
        } else {
            expanded.extend(matches);
        }
    }

    let negation = if not_count == 0 {
        Negation::None
    } else if not_count & 1 == 0 {
        Negation::Even
    } else if crash {
        Negation::Crash
    } else if saw_lit_not {
        Negation::Lit
    } else {
        Negation::Shell
    };

    Ok(ParsedCommand {
        command: command.clone(),
        env,
        args: expanded,
        negation,
        env_prefix_used,
        env_print,
    })
}

fn execute_in_process_stage(
    mut parsed: ParsedCommand,
    embedded: Option<Arc<dyn ShellCommandHandler>>,
    piped_input: Option<Vec<u8>>,
    in_pipeline: bool,
    is_pipeline_last: bool,
    deadline: Option<Instant>,
    shenv: &mut ShellEnv,
) -> Result<StageRecord, String> {
    let redirects = resolve_redirects(&parsed.command.redirects, &parsed.env.cwd, true)?;
    let input = if let Some(path) = &redirects.stdin_path {
        fs::read(path).map_err(|error| format!("failed to read '{}': {error}", path.display()))?
    } else {
        piped_input.unwrap_or_default()
    };

    if deadline.is_some_and(|deadline| Instant::now() >= deadline) {
        return Ok(StageRecord {
            result: CommandResult {
                stdout: String::new(),
                stderr: String::new(),
                exit_code: -1,
                timed_out: true,
            },
            stdout_bytes: Vec::new(),
            stdout_visible: is_pipeline_last,
        });
    }

    let output = if parsed.env_print {
        let mut entries = parsed
            .env
            .env
            .iter()
            .map(|(key, value)| format!("{key}={value}"))
            .collect::<Vec<_>>();
        entries.sort_unstable();
        let stdout = if entries.is_empty() {
            Vec::new()
        } else {
            (entries.join("\n") + "\n").into_bytes()
        };
        crate::shell::environment::ShellCommandOutput {
            stdout,
            stderr: Vec::new(),
            exit_code: 0,
        }
    } else if let Some(handler) = embedded {
        handler.execute(&parsed.args, &input, &parsed.env.cwd, &parsed.env.env)?
    } else {
        let name = parsed.args.first().map(String::as_str).unwrap_or("");
        if in_pipeline && !matches!(name, "echo" | "@echo" | ":") {
            return Err(format!("builtin '{name}' cannot be part of a pipeline"));
        }
        if parsed.env_prefix_used {
            return Err(format!("'env' cannot call builtin '{name}'"));
        }
        let builtin_command = Command {
            args: parsed.args.iter().cloned().map(Arg::Text).collect(),
            redirects: Vec::new(),
        };
        let input_text = String::from_utf8_lossy(&input);
        let result =
            run_inproc_builtin(&builtin_command, &mut parsed.env, Some(input_text.as_ref()))?;
        if !in_pipeline {
            *shenv = parsed.env.clone();
        }
        crate::shell::environment::ShellCommandOutput {
            stdout: result.stdout.into_bytes(),
            stderr: result.stderr.into_bytes(),
            exit_code: result.exit_code,
        }
    };

    redirects.write(redirects.stdout, &output.stdout)?;
    redirects.write(redirects.stderr, &output.stderr)?;
    let (stdout, stderr) = redirects.captured()?;
    let mut exit_code = output.exit_code;
    apply_negation(&mut exit_code, parsed.negation);
    let timed_out = deadline.is_some_and(|deadline| Instant::now() >= deadline);

    Ok(StageRecord {
        result: CommandResult {
            stdout: String::from_utf8_lossy(&stdout).into_owned(),
            stderr: String::from_utf8_lossy(&stderr).into_owned(),
            exit_code,
            timed_out,
        },
        stdout_bytes: stdout,
        stdout_visible: is_pipeline_last,
    })
}

fn execute_external_segment(
    commands: &[ParsedCommand],
    input: Option<Vec<u8>>,
    segment_ends_pipeline: bool,
    deadline: Option<Instant>,
) -> Result<(Vec<StageRecord>, Vec<u8>, bool), String> {
    let mut input_sink = input
        .map(|data| {
            let sink = create_capture_sink("stdin", CaptureStream::Stdout)?;
            sink.write_all(&data)?;
            Ok::<OpenSink, String>(sink)
        })
        .transpose()?;
    let mut previous_pipe = None;
    let mut running = RunningPipeline {
        commands: Vec::new(),
        complete: false,
    };

    for (index, parsed) in commands.iter().enumerate() {
        let is_last = index + 1 == commands.len();
        let redirects = resolve_redirects(&parsed.command.redirects, &parsed.env.cwd, is_last)?;
        let executable = resolve_executable(
            parsed
                .args
                .first()
                .ok_or_else(|| "empty command".to_string())?,
            &parsed.env,
        )?;
        let mut command = SysCommand::new(executable);
        command
            .args(&parsed.args[1..])
            .current_dir(&parsed.env.cwd)
            .env_clear()
            .envs(&parsed.env.env);

        if let Some(path) = &redirects.stdin_path {
            command.stdin(
                File::open(path)
                    .map(Stdio::from)
                    .map_err(|error| format!("failed to open '{}': {error}", path.display()))?,
            );
            previous_pipe.take();
        } else if index == 0 {
            if let Some(sink) = &input_sink {
                command.stdin(
                    File::open(&sink.path)
                        .map(Stdio::from)
                        .map_err(|error| format!("failed to reopen pipeline input: {error}"))?,
                );
            } else {
                command.stdin(Stdio::null());
            }
        } else {
            command.stdin(previous_pipe.take().unwrap_or_else(Stdio::null));
        }

        let stdout_is_pipe = redirects.stdout == OutputTarget::Pipe;
        let stderr_is_pipe = redirects.stderr == OutputTarget::Pipe;
        match redirects.stdout {
            OutputTarget::Pipe => {
                command.stdout(Stdio::piped());
            }
            OutputTarget::Sink(index) => {
                command.stdout(redirects.sinks[index].stdio()?);
            }
        }
        match redirects.stderr {
            OutputTarget::Pipe if stdout_is_pipe => {
                #[cfg(not(unix))]
                return Err(
                    "merging stderr into a live pipeline is unsupported on this platform"
                        .to_string(),
                );
                #[cfg(unix)]
                command.stderr(Stdio::null());
            }
            OutputTarget::Pipe => {
                command.stderr(Stdio::piped());
            }
            OutputTarget::Sink(index) => {
                command.stderr(redirects.sinks[index].stdio()?);
            }
        }

        #[cfg(unix)]
        {
            let merge_pipe = stdout_is_pipe && stderr_is_pipe;
            let umask = parsed.env.umask;
            // SAFETY: The post-fork closure captures only copyable values. It
            // calls setpgid, dup2, and umask. The error paths construct raw OS
            // errors. The closure does not allocate or acquire a lock.
            unsafe {
                command.pre_exec(move || {
                    if let Err(error) = nix::unistd::setpgid(Pid::from_raw(0), Pid::from_raw(0)) {
                        return Err(io::Error::from_raw_os_error(error as i32));
                    }
                    if merge_pipe && dup2(1, 2) == -1 {
                        return Err(io::Error::last_os_error());
                    }
                    if let Some(mask) = umask {
                        set_umask(mask);
                    }
                    Ok(())
                });
            }
        }

        let mut child = command.spawn().map_err(|error| {
            format!(
                "failed to spawn command '{}': {error}",
                parsed.args.first().map(String::as_str).unwrap_or("")
            )
        })?;
        previous_pipe = if stdout_is_pipe {
            child.stdout.take().map(Stdio::from)
        } else if stderr_is_pipe {
            child.stderr.take().map(Stdio::from)
        } else {
            None
        };
        running.commands.push(RunningCommand {
            child,
            parsed: parsed.clone(),
            redirects,
        });
    }

    previous_pipe.take();
    input_sink.take();
    let (statuses, timed_out) = wait_for_pipeline(&mut running.commands, deadline)?;
    let mut records = Vec::with_capacity(running.commands.len());
    let record_count = running.commands.len();
    for (index, (command, status)) in running.commands.iter().zip(statuses).enumerate() {
        let (stdout, stderr) = command.redirects.captured()?;
        let mut exit_code = process_exit_code(status);
        apply_negation(&mut exit_code, command.parsed.negation);
        records.push(StageRecord {
            result: CommandResult {
                stdout: String::from_utf8_lossy(&stdout).into_owned(),
                stderr: String::from_utf8_lossy(&stderr).into_owned(),
                exit_code,
                timed_out,
            },
            stdout_bytes: stdout,
            stdout_visible: segment_ends_pipeline && index + 1 == record_count,
        });
    }
    running.complete = true;
    let output = records
        .last()
        .map(|record| record.stdout_bytes.clone())
        .unwrap_or_default();
    Ok((records, output, timed_out))
}

fn resolve_redirects(
    redirects: &[Redirect],
    cwd: &Path,
    capture_stdout: bool,
) -> Result<RedirectLayout, String> {
    let mut sinks = Vec::new();
    let stdout = if capture_stdout {
        sinks.push(create_capture_sink("stdout", CaptureStream::Stdout)?);
        OutputTarget::Sink(0)
    } else {
        OutputTarget::Pipe
    };
    let stderr_index = sinks.len();
    sinks.push(create_capture_sink("stderr", CaptureStream::Stderr)?);
    let mut layout = RedirectLayout {
        stdin_path: None,
        stdout,
        stderr: OutputTarget::Sink(stderr_index),
        sinks,
    };

    for redirect in redirects {
        let target = expand_glob_argument(&redirect.target, cwd)?;
        match (redirect.source_fd, redirect.op.as_str(), target.as_str()) {
            (0, "<", _) => layout.stdin_path = Some(resolve_path(cwd, &target)),
            (0, "<&", "0") => {}
            (1, ">", _) | (1, ">>", _) => {
                let append = redirect.op == ">>";
                let index = layout.sinks.len();
                layout.sinks.push(open_redirect_sink(cwd, &target, append)?);
                layout.stdout = OutputTarget::Sink(index);
            }
            (2, ">", _) | (2, ">>", _) => {
                let append = redirect.op == ">>";
                let index = layout.sinks.len();
                layout.sinks.push(open_redirect_sink(cwd, &target, append)?);
                layout.stderr = OutputTarget::Sink(index);
            }
            (2, ">&", "1") => layout.stderr = layout.stdout,
            (1, ">&", "2") => layout.stdout = layout.stderr,
            (1, "&>", _) | (1, ">&", _) => {
                let index = layout.sinks.len();
                layout.sinks.push(open_redirect_sink(cwd, &target, false)?);
                layout.stdout = OutputTarget::Sink(index);
                layout.stderr = OutputTarget::Sink(index);
            }
            _ => {
                return Err(format!(
                    "unsupported redirection '{}{} {}'",
                    redirect.source_fd, redirect.op, redirect.target
                ));
            }
        }
    }
    Ok(layout)
}

fn create_capture_sink(tag: &str, stream: CaptureStream) -> Result<OpenSink, String> {
    let pid = std::process::id();
    for _ in 0..128 {
        let serial = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
        let path = std::env::temp_dir().join(format!("litnik-{tag}-{pid}-{serial}"));
        match fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create_new(true)
            .open(&path)
        {
            Ok(file) => {
                return Ok(OpenSink {
                    file: Some(file),
                    path,
                    capture: Some(stream),
                });
            }
            Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
            Err(error) => return Err(format!("failed to create capture file: {error}")),
        }
    }
    Err("failed to allocate a unique capture file".to_string())
}

fn open_redirect_sink(cwd: &Path, target: &str, append: bool) -> Result<OpenSink, String> {
    let path = resolve_path(cwd, target);
    let mut options = fs::OpenOptions::new();
    options.write(true).create(true);
    if append {
        options.append(true);
    } else {
        options.truncate(true);
    }
    let file = options
        .open(&path)
        .map_err(|error| format!("failed to open '{}': {error}", path.display()))?;
    Ok(OpenSink {
        file: Some(file),
        path,
        capture: None,
    })
}

fn wait_for_pipeline(
    commands: &mut [RunningCommand],
    deadline: Option<Instant>,
) -> Result<(Vec<ExitStatus>, bool), String> {
    let mut statuses = vec![None; commands.len()];
    let mut remaining = commands.len();
    let mut timed_out = false;
    while remaining > 0 {
        for (command, status) in commands.iter_mut().zip(statuses.iter_mut()) {
            if status.is_some() {
                continue;
            }
            if let Some(completed) = command
                .child
                .try_wait()
                .map_err(|error| format!("failed checking process status: {error}"))?
            {
                *status = Some(completed);
                remaining -= 1;
            }
        }
        if remaining == 0 {
            break;
        }
        if deadline.is_some_and(|deadline| Instant::now() >= deadline) {
            timed_out = true;
            for (command, status) in commands.iter_mut().zip(statuses.iter()) {
                if status.is_none() {
                    terminate_process(&mut command.child);
                }
            }
            break;
        }
        std::thread::sleep(Duration::from_millis(10));
    }

    for (command, status) in commands.iter_mut().zip(statuses.iter_mut()) {
        if status.is_none() {
            *status = Some(
                command
                    .child
                    .wait()
                    .map_err(|error| format!("failed waiting for child: {error}"))?,
            );
        }
    }
    Ok((
        statuses
            .into_iter()
            .map(|status| status.expect("every child was waited"))
            .collect(),
        timed_out,
    ))
}

fn resolve_executable(name: &str, env: &ShellEnv) -> Result<PathBuf, String> {
    if let Some(path) = env.tools.get(name) {
        let path = if path.is_absolute() {
            path.clone()
        } else {
            env.cwd.join(path)
        };
        return executable_file(path).ok_or_else(|| format!("{name}: command not found"));
    }

    if name.contains('/') || name.contains('\\') {
        let candidate = resolve_path(&env.cwd, name);
        return executable_file(candidate).ok_or_else(|| format!("{name}: command not found"));
    }

    let path = env.env.get("PATH").cloned().unwrap_or_default();
    let path_ext = env
        .env
        .get("PATHEXT")
        .cloned()
        .unwrap_or_else(|| ".COM;.EXE;.BAT;.CMD".to_string());
    for directory in std::env::split_paths(&path) {
        let directory = if directory.is_absolute() {
            directory
        } else {
            env.cwd.join(directory)
        };
        if let Some(path) = executable_file(directory.join(name)) {
            return Ok(path);
        }
        if cfg!(windows) {
            for extension in path_ext
                .split(';')
                .filter(|extension| !extension.is_empty())
            {
                if let Some(path) = executable_file(directory.join(format!("{name}{extension}"))) {
                    return Ok(path);
                }
            }
        }
    }
    Err(format!("{name}: command not found"))
}

fn executable_file(path: PathBuf) -> Option<PathBuf> {
    let metadata = path.metadata().ok()?;
    if !metadata.is_file() {
        return None;
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if metadata.permissions().mode() & 0o111 == 0 {
            return None;
        }
    }
    Some(path)
}

fn expand_late_substitutions(args: &mut [String], cwd: &Path) -> Result<(), String> {
    for arg in args {
        let (expanded, _) = expand_late_text(arg, &[], cwd, false)?;
        *arg = expanded;
    }
    Ok(())
}

fn expand_late_argument(arg: Arg, cwd: &Path, normalize_slashes: bool) -> Result<Arg, String> {
    if matches!(arg, Arg::Text(_)) && !normalize_slashes {
        let mut text = [arg.into_text()];
        expand_late_substitutions(&mut text, cwd)?;
        return Ok(Arg::Text(text.into_iter().next().unwrap_or_default()));
    }
    let active_metacharacters = arg.active_metacharacters();
    let (text, active_metacharacters) =
        expand_late_text(arg.as_str(), &active_metacharacters, cwd, normalize_slashes)?;
    Ok(Arg::from_glob_parts(text, active_metacharacters))
}

fn expand_late_text(
    value: &str,
    active_metacharacters: &[usize],
    cwd: &Path,
    normalize_slashes: bool,
) -> Result<(String, Vec<usize>), String> {
    const MARKER: &[char] = &['%', '{', 'r', 'e', 'a', 'd', 'f', 'i', 'l', 'e', ':'];
    let chars = value.chars().collect::<Vec<_>>();
    let mut output = String::new();
    let mut output_chars = 0usize;
    let mut mapped_wildcards = Vec::new();
    let mut index = 0usize;
    while index < chars.len() {
        if chars[index..].starts_with(MARKER) {
            let path_start = index + MARKER.len();
            let Some(path_end) = chars[path_start..]
                .iter()
                .position(|ch| *ch == '}')
                .map(|offset| path_start + offset)
            else {
                return Err("readfile substitution missing '}'".to_string());
            };
            let mut path = chars[path_start..path_end].iter().collect::<String>();
            if normalize_slashes {
                path = path.replace('\\', "/");
            }
            let absolute = resolve_path(cwd, &path);
            let contents = fs::read_to_string(&absolute).map_err(|error| {
                format!(
                    "readfile substitution failed for '{}': {error}",
                    absolute.display()
                )
            })?;
            output_chars += contents.chars().count();
            output.push_str(&contents);
            index = path_end + 1;
            continue;
        }
        if active_metacharacters.binary_search(&index).is_ok() {
            mapped_wildcards.push(output_chars);
        }
        output.push(chars[index]);
        output_chars += 1;
        index += 1;
    }
    Ok((output, mapped_wildcards))
}

fn expand_glob_argument(arg: &Arg, cwd: &Path) -> Result<String, String> {
    let expanded = expand_arg_glob(arg, cwd);
    if expanded.len() != 1 {
        return Err(format!(
            "globbing redirect '{arg}' produced multiple results: {}",
            expanded.join(", ")
        ));
    }
    expanded
        .into_iter()
        .next()
        .ok_or_else(|| "glob expansion returned no result".to_string())
}

fn resolve_path(cwd: &Path, target: &str) -> PathBuf {
    let candidate = PathBuf::from(target);
    if candidate.is_absolute() {
        candidate
    } else {
        cwd.join(candidate)
    }
}

fn apply_negation(code: &mut i32, negation: Negation) {
    *code = match negation {
        Negation::None => *code,
        Negation::Even => i32::from(*code != 0),
        Negation::Shell => i32::from(*code == 0),
        Negation::Lit => i32::from(*code <= 0),
        Negation::Crash => i32::from(!(*code < 0 || (cfg!(windows) && *code > 255))),
    };
}

fn terminate_process(process: &mut Child) {
    #[cfg(unix)]
    {
        let pid = Pid::from_raw(process.id() as i32);
        match signal::killpg(pid, Signal::SIGKILL) {
            Ok(()) | Err(Errno::ESRCH) => {}
            Err(_) => {
                let _ = process.kill();
            }
        }
    }
    #[cfg(not(unix))]
    {
        let _ = process.kill();
    }
}

fn process_exit_code(status: ExitStatus) -> i32 {
    #[cfg(unix)]
    {
        use std::os::unix::process::ExitStatusExt;
        if let Some(signal) = status.signal() {
            return -signal;
        }
    }
    status.code().unwrap_or(128)
}

#[cfg(unix)]
unsafe extern "C" {
    fn dup2(old_fd: i32, new_fd: i32) -> i32;
    #[link_name = "umask"]
    fn set_umask(mask: u32) -> u32;
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::path::PathBuf;

    use tempfile::tempdir;

    use crate::shell::environment::ShellEnv;
    use crate::shell::ShParser;

    #[test]
    fn executes_single_command_with_not() {
        let path = std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| String::from("."));
        let command = "not false";
        let mut parser = ShParser::new(command, false, false);
        let ast = parser.parse().unwrap();
        let mut env = ShellEnv::new(PathBuf::from(path), HashMap::new(), false);
        let result = super::execute_script_node(&ast, &mut env, None).unwrap();
        assert_eq!(result.exit_code, 0);
    }

    #[test]
    fn executes_double_not_as_double_negation() {
        let path = std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| String::from("."));
        let command = "not not false";
        let mut parser = ShParser::new(command, false, false);
        let ast = parser.parse().unwrap();
        let mut env = ShellEnv::new(PathBuf::from(path.clone()), HashMap::new(), false);
        let result = super::execute_script_node(&ast, &mut env, None).unwrap();
        assert_eq!(result.exit_code, 1);

        let command = "not ! false";
        let mut parser = ShParser::new(command, false, false);
        let ast = parser.parse().unwrap();
        let result = super::execute_script_node(&ast, &mut env, None).unwrap();
        assert_eq!(result.exit_code, 1);
    }

    #[test]
    fn not_crash_rejects_normal_exit() {
        let path = std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| String::from("."));
        let mut parser = ShParser::new("not --crash false", false, false);
        let ast = parser.parse().unwrap();
        let mut env = ShellEnv::new(PathBuf::from(path), HashMap::new(), false);
        let normal_failure = super::execute_script_node(&ast, &mut env, None).unwrap();
        assert_eq!(normal_failure.exit_code, 1);

        let mut parser = ShParser::new("not --crash true", false, false);
        let ast = parser.parse().unwrap();
        let normal_success = super::execute_script_node(&ast, &mut env, None).unwrap();
        assert_eq!(normal_success.exit_code, 1);
    }

    #[test]
    fn executes_redirect_stderr_to_stdout() {
        let root = tempdir().unwrap();
        let out_path = root.path().join("out.txt");
        let command = format!(
            "/bin/sh -c 'printf \"stdout\\\\n\"; printf \"stderr\\\\n\" 1>&2' > {} 2>&1",
            out_path.display()
        );
        let mut parser = ShParser::new(&command, false, false);
        let ast = parser.parse().unwrap();
        let mut env = ShellEnv::new(
            std::env::var("CARGO_TARGET_DIR")
                .unwrap_or_else(|_| String::from("."))
                .into(),
            HashMap::new(),
            false,
        );

        let result = super::execute_script_node(&ast, &mut env, None).unwrap();
        assert_eq!(result.exit_code, 0);

        let output = std::fs::read_to_string(&out_path).unwrap_or_default();
        assert!(output.contains("stdout"));
        assert!(output.contains("stderr"));
    }

    #[test]
    fn pipeline_respects_pipefail_setting() {
        let root = tempdir().unwrap();
        let path = root.path().to_path_buf();

        let command = "false | true";
        let mut parser = ShParser::new(command, false, true);
        let ast = parser.parse().unwrap();
        let mut env = ShellEnv::new(path.clone(), HashMap::new(), false);
        let result = super::execute_script_node(&ast, &mut env, None).unwrap();
        assert_eq!(result.exit_code, 1);

        let mut parser = ShParser::new(command, false, false);
        let ast = parser.parse().unwrap();
        let mut env = ShellEnv::new(path, HashMap::new(), false);
        let result = super::execute_script_node(&ast, &mut env, None).unwrap();
        assert_eq!(result.exit_code, 0);
    }

    #[test]
    fn substitution_readfile_is_applied() {
        let tmp = tempdir().unwrap();
        let file = tmp.path().join("x.txt");
        std::fs::write(&file, "abc").unwrap();

        let mut args = vec!["cat".to_string(), "%{readfile:./x.txt}".to_string()];
        let cwd = tmp.path();
        super::expand_late_substitutions(&mut args, cwd).unwrap();
        assert_eq!(args[1], "abc");
    }
}