rush-sh 0.8.0

A POSIX sh-compatible shell written in Rust
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
use crate::builtins::Builtin;
use crate::parser::ShellCommand;
use crate::state::ShellState;
use std::io::Write;

pub struct KillBuiltin;

impl KillBuiltin {
    /// Parse signal specification from command line argument
    ///
    /// Supports:
    /// - Signal names: TERM, KILL, INT, HUP, STOP, CONT, etc.
    /// - Signal names with SIG prefix: SIGTERM, SIGKILL, etc.
    /// - Non-negative signal numbers: 0 and higher
    /// - Returns signal number on success, error message on failure
    fn parse_signal(signal_str: &str) -> Result<i32, String> {
        // Try to parse as number first
        if let Ok(num) = signal_str.parse::<i32>() {
            if num >= 0 {
                return Ok(num);
            } else {
                return Err(format!("kill: {}: invalid signal specification", signal_str));
            }
        }

        // Parse as signal name (with or without SIG prefix)
        let name = if signal_str.starts_with("SIG") {
            &signal_str[3..]
        } else {
            signal_str
        };

        // Map common signal names to numbers
        match name {
            "HUP" => Ok(libc::SIGHUP),
            "INT" => Ok(libc::SIGINT),
            "QUIT" => Ok(libc::SIGQUIT),
            "ILL" => Ok(libc::SIGILL),
            "TRAP" => Ok(libc::SIGTRAP),
            "ABRT" => Ok(libc::SIGABRT),
            "BUS" => Ok(libc::SIGBUS),
            "FPE" => Ok(libc::SIGFPE),
            "KILL" => Ok(libc::SIGKILL),
            "USR1" => Ok(libc::SIGUSR1),
            "SEGV" => Ok(libc::SIGSEGV),
            "USR2" => Ok(libc::SIGUSR2),
            "PIPE" => Ok(libc::SIGPIPE),
            "ALRM" => Ok(libc::SIGALRM),
            "TERM" => Ok(libc::SIGTERM),
            "CHLD" => Ok(libc::SIGCHLD),
            "CONT" => Ok(libc::SIGCONT),
            "STOP" => Ok(libc::SIGSTOP),
            "TSTP" => Ok(libc::SIGTSTP),
            "TTIN" => Ok(libc::SIGTTIN),
            "TTOU" => Ok(libc::SIGTTOU),
            "URG" => Ok(libc::SIGURG),
            "XCPU" => Ok(libc::SIGXCPU),
            "XFSZ" => Ok(libc::SIGXFSZ),
            "VTALRM" => Ok(libc::SIGVTALRM),
            "PROF" => Ok(libc::SIGPROF),
            "WINCH" => Ok(libc::SIGWINCH),
            "IO" | "POLL" => Ok(libc::SIGIO),
            "SYS" => Ok(libc::SIGSYS),
            _ => Err(format!("kill: {}: invalid signal specification", signal_str)),
        }
    }

    /// List all available signals
    fn list_signals(output_writer: &mut dyn Write) -> i32 {
        let signals = vec![
            (libc::SIGHUP, "HUP"),
            (libc::SIGINT, "INT"),
            (libc::SIGQUIT, "QUIT"),
            (libc::SIGILL, "ILL"),
            (libc::SIGTRAP, "TRAP"),
            (libc::SIGABRT, "ABRT"),
            (libc::SIGBUS, "BUS"),
            (libc::SIGFPE, "FPE"),
            (libc::SIGKILL, "KILL"),
            (libc::SIGUSR1, "USR1"),
            (libc::SIGSEGV, "SEGV"),
            (libc::SIGUSR2, "USR2"),
            (libc::SIGPIPE, "PIPE"),
            (libc::SIGALRM, "ALRM"),
            (libc::SIGTERM, "TERM"),
            (libc::SIGCHLD, "CHLD"),
            (libc::SIGCONT, "CONT"),
            (libc::SIGSTOP, "STOP"),
            (libc::SIGTSTP, "TSTP"),
            (libc::SIGTTIN, "TTIN"),
            (libc::SIGTTOU, "TTOU"),
            (libc::SIGURG, "URG"),
            (libc::SIGXCPU, "XCPU"),
            (libc::SIGXFSZ, "XFSZ"),
            (libc::SIGVTALRM, "VTALRM"),
            (libc::SIGPROF, "PROF"),
            (libc::SIGWINCH, "WINCH"),
            (libc::SIGIO, "IO"),
            (libc::SIGSYS, "SYS"),
        ];

        for (num, name) in signals {
            let _ = writeln!(output_writer, "{}) SIG{}", num, name);
        }

        0
    }

    /// Get PIDs for a target (either PID or jobspec)
    ///
    /// Returns signed PIDs to support POSIX process group signaling:
    /// - Positive PID: signal a single process
    /// - Zero: signal all processes in current process group
    /// - -1: signal all processes (with permissions)
    /// - Negative PID < -1: signal all processes in process group |PID|
    fn get_target_pids(target: &str, shell_state: &ShellState) -> Result<Vec<i32>, String> {
        if target.starts_with('%') {
            // It's a jobspec - convert u32 PIDs to i32
            let job_id = shell_state.job_table.borrow().parse_jobspec(target, "kill")?;
            let job_table = shell_state.job_table.borrow();
            match job_table.get_job(job_id) {
                Some(job) => {
                    if job.pids.is_empty() {
                        Err(format!("kill: %{}: no such job", job_id))
                    } else {
                        // Convert u32 PIDs to i32 (job PIDs are always positive)
                        Ok(job.pids.iter().map(|&pid| pid as i32).collect())
                    }
                }
                None => Err(format!("kill: %{}: no such job", job_id)),
            }
        } else {
            // It's a PID (or process group if negative)
            // Preserve the sign to support POSIX process group signaling
            match target.parse::<i32>() {
                Ok(pid) => Ok(vec![pid]),
                Err(_) => Err(format!("kill: {}: arguments must be process or job IDs", target)),
            }
        }
    }

    /// Send signal to a PID or process group
    ///
    /// Supports POSIX process group signaling:
    /// - pid > 0: Signal the process with ID pid
    /// - pid == 0: Signal all processes in current process group
    /// - pid == -1: Signal all processes (with permissions)
    /// - pid < -1: Signal all processes in process group |pid|
    fn send_signal(pid: i32, signal: i32) -> Result<(), String> {
        // SAFETY: kill is a standard POSIX function for sending signals to processes
        // Pass pid directly to libc::kill to preserve sign for process group signaling
        let result = unsafe { libc::kill(pid as libc::pid_t, signal) };
        
        if result == -1 {
            let err = std::io::Error::last_os_error();
            match err.raw_os_error() {
                Some(libc::ESRCH) => {
                    if pid < -1 {
                        Err(format!("kill: ({}): No such process group", pid.abs()))
                    } else {
                        Err(format!("kill: ({}): No such process", pid))
                    }
                }
                Some(libc::EPERM) => Err(format!("kill: ({}): Operation not permitted", pid)),
                _ => Err(format!("kill: ({}): {}", pid, err)),
            }
        } else {
            Ok(())
        }
    }
}

impl Builtin for KillBuiltin {
    fn name(&self) -> &'static str {
        "kill"
    }

    fn names(&self) -> Vec<&'static str> {
        vec!["kill"]
    }

    fn description(&self) -> &'static str {
        "Send a signal to a job or process"
    }

    fn run(
        &self,
        cmd: &ShellCommand,
        shell_state: &mut ShellState,
        output_writer: &mut dyn Write,
    ) -> i32 {
        let args = &cmd.args;

        // Need at least one argument
        if args.len() < 2 {
            let _ = writeln!(output_writer, "kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ...");
            let _ = writeln!(output_writer, "       kill -l [sigspec]");
            return 1;
        }

        let mut signal = libc::SIGTERM; // Default signal
        let mut arg_index = 1;
        let mut targets = Vec::new();
        let mut signal_specified_with_option = false; // Track if signal was specified with -s or -n

        // Parse options and signal specification
        while arg_index < args.len() {
            let arg = &args[arg_index];

            if arg == "--" {
                // End of options marker - remaining arguments are targets
                arg_index += 1;
                break;
            } else if arg == "-l" {
                // List signals
                return Self::list_signals(output_writer);
            } else if arg == "-s" {
                // Signal specified with -s option
                arg_index += 1;
                if arg_index >= args.len() {
                    let _ = writeln!(output_writer, "kill: -s: option requires an argument");
                    return 1;
                }
                match Self::parse_signal(&args[arg_index]) {
                    Ok(sig) => {
                        signal = sig;
                        signal_specified_with_option = true;
                    }
                    Err(e) => {
                        let _ = writeln!(output_writer, "{}", e);
                        return 1;
                    }
                }
                arg_index += 1;
            } else if arg == "-n" {
                // Signal specified with -n option (number)
                arg_index += 1;
                if arg_index >= args.len() {
                    let _ = writeln!(output_writer, "kill: -n: option requires an argument");
                    return 1;
                }
                match args[arg_index].parse::<i32>() {
                    Ok(num) if num >= 0 => {
                        signal = num;
                        signal_specified_with_option = true;
                    }
                    _ => {
                        let _ = writeln!(output_writer, "kill: {}: invalid signal specification", args[arg_index]);
                        return 1;
                    }
                }
                arg_index += 1;
            } else if !signal_specified_with_option && arg.starts_with('-') && arg.len() > 1 {
                // Signal specified with -SIGNAME or -NUM format (only if not already specified with -s/-n)
                let sig_spec = &arg[1..];
                match Self::parse_signal(sig_spec) {
                    Ok(sig) => {
                        signal = sig;
                        signal_specified_with_option = true;
                    }
                    Err(e) => {
                        let _ = writeln!(output_writer, "{}", e);
                        return 1;
                    }
                }
                arg_index += 1;
            } else {
                // This and remaining arguments are targets
                break;
            }
        }

        // Collect all targets
        while arg_index < args.len() {
            targets.push(args[arg_index].clone());
            arg_index += 1;
        }

        // Need at least one target
        if targets.is_empty() {
            let _ = writeln!(output_writer, "kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ...");
            return 1;
        }

        // Send signal to all targets
        let mut exit_code = 0;
        for target in targets {
            match Self::get_target_pids(&target, shell_state) {
                Ok(pids) => {
                    for pid in pids {
                        if let Err(e) = Self::send_signal(pid, signal) {
                            let _ = writeln!(output_writer, "{}", e);
                            exit_code = 1;
                        }
                    }
                }
                Err(e) => {
                    let _ = writeln!(output_writer, "{}", e);
                    exit_code = 1;
                }
            }
        }

        exit_code
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::Job;
    
    #[cfg(test)]
    use rush_sh::test_sync::JOB_CONTROL_LOCK;

    #[test]
    fn test_kill_no_arguments() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        let cmd = ShellCommand {
            args: vec!["kill".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("usage"));
    }

    #[test]
    fn test_kill_list_signals() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-l".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        assert_eq!(exit_code, 0);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("SIGTERM"));
        assert!(output_str.contains("SIGKILL"));
        assert!(output_str.contains("SIGINT"));
    }

    #[test]
    fn test_parse_signal_by_name() {
        assert_eq!(KillBuiltin::parse_signal("TERM").unwrap(), libc::SIGTERM);
        assert_eq!(KillBuiltin::parse_signal("KILL").unwrap(), libc::SIGKILL);
        assert_eq!(KillBuiltin::parse_signal("INT").unwrap(), libc::SIGINT);
        assert_eq!(KillBuiltin::parse_signal("HUP").unwrap(), libc::SIGHUP);
    }

    #[test]
    fn test_parse_signal_with_sig_prefix() {
        assert_eq!(KillBuiltin::parse_signal("SIGTERM").unwrap(), libc::SIGTERM);
        assert_eq!(KillBuiltin::parse_signal("SIGKILL").unwrap(), libc::SIGKILL);
        assert_eq!(KillBuiltin::parse_signal("SIGINT").unwrap(), libc::SIGINT);
    }

    #[test]
    fn test_parse_signal_by_number() {
        assert_eq!(KillBuiltin::parse_signal("9").unwrap(), 9);
        assert_eq!(KillBuiltin::parse_signal("15").unwrap(), 15);
        assert_eq!(KillBuiltin::parse_signal("1").unwrap(), 1);
    }

    #[test]
    fn test_parse_signal_invalid() {
        assert!(KillBuiltin::parse_signal("INVALID").is_err());
        assert!(KillBuiltin::parse_signal("-1").is_err());
    }

    #[test]
    fn test_kill_with_signal_name() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Try to kill the current process with signal 0 (test if process exists)
        let pid = std::process::id();
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-s".to_string(), "0".to_string(), pid.to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Signal 0 is now valid (null signal to test if process exists), should succeed
        assert_eq!(exit_code, 0);
    }

    #[test]
    fn test_kill_invalid_pid() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("No such process") || output_str.contains("Operation not permitted"));
    }

    #[test]
    fn test_kill_jobspec_no_jobs() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "%1".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("no such job"));
    }

    #[test]
    fn test_kill_jobspec_current() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        
        // Add a job with the current process PID (so we can test without actually killing)
        let pid = std::process::id();
        let job = Job::new(1, Some(pid), "sleep 10 &".to_string(), vec![pid], false);
        shell_state.job_table.borrow_mut().add_job(job);

        let mut output = Vec::new();
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-s".to_string(), "0".to_string(), "%".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Signal 0 is now valid (null signal to test if process exists), should succeed
        assert_eq!(exit_code, 0);
    }

    #[test]
    fn test_kill_multiple_targets() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Use invalid PIDs that don't exist
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "999998".to_string(), "999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        // Should have error messages for both PIDs
        assert!(output_str.contains("999998") || output_str.contains("999999"));
    }

    #[test]
    fn test_kill_with_dash_signal() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Use an invalid PID instead of our own process
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-TERM".to_string(), "999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Should fail because PID doesn't exist
        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("No such process") || output_str.contains("Operation not permitted"));
    }

    #[test]
    fn test_kill_with_dash_number() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-9".to_string(), "999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("No such process") || output_str.contains("Operation not permitted"));
    }

    #[test]
    fn test_kill_with_n_option() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-n".to_string(), "15".to_string(), "999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("No such process") || output_str.contains("Operation not permitted"));
    }

    #[test]
    fn test_kill_missing_signal_argument() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-s".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("option requires an argument"));
    }

    #[test]
    fn test_kill_no_targets_after_signal() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-TERM".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("usage"));
    }

    #[test]
    fn test_parse_jobspec_current() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();
        let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
        shell_state.job_table.borrow_mut().add_job(job);

        let result = shell_state.job_table.borrow().parse_jobspec("%", "kill");
        assert_eq!(result.unwrap(), 1);

        let result = shell_state.job_table.borrow().parse_jobspec("%+", "kill");
        assert_eq!(result.unwrap(), 1);
    }

    #[test]
    fn test_parse_jobspec_previous() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();
        let job1 = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
        let job2 = Job::new(2, Some(1235), "sleep 20 &".to_string(), vec![1235], false);
        shell_state.job_table.borrow_mut().add_job(job1);
        shell_state.job_table.borrow_mut().add_job(job2);

        let result = shell_state.job_table.borrow().parse_jobspec("%-", "kill");
        assert_eq!(result.unwrap(), 1);
    }

    #[test]
    fn test_parse_jobspec_by_number() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();
        let job = Job::new(5, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
        shell_state.job_table.borrow_mut().add_job(job);

        let result = shell_state.job_table.borrow().parse_jobspec("%5", "kill");
        assert_eq!(result.unwrap(), 5);
    }

    #[test]
    fn test_parse_jobspec_by_prefix() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();
        let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
        shell_state.job_table.borrow_mut().add_job(job);

        let result = shell_state.job_table.borrow().parse_jobspec("%sleep", "kill");
        assert_eq!(result.unwrap(), 1);
    }

    #[test]
    fn test_parse_jobspec_by_contains() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();
        let job = Job::new(1, Some(1234), "grep pattern file &".to_string(), vec![1234], false);
        shell_state.job_table.borrow_mut().add_job(job);

        let result = shell_state.job_table.borrow().parse_jobspec("%?pattern", "kill");
        assert_eq!(result.unwrap(), 1);
    }

    #[test]
    fn test_get_target_pids_for_job() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();
        let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234, 1235], false);
        shell_state.job_table.borrow_mut().add_job(job);

        let result = KillBuiltin::get_target_pids("%1", &shell_state);
        assert!(result.is_ok());
        let pids = result.unwrap();
        assert_eq!(pids.len(), 2);
        assert!(pids.contains(&1234));
        assert!(pids.contains(&1235));
    }

    #[test]
    fn test_get_target_pids_for_pid() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();

        let result = KillBuiltin::get_target_pids("1234", &shell_state);
        assert!(result.is_ok());
        let pids = result.unwrap();
        assert_eq!(pids.len(), 1);
        assert_eq!(pids[0], 1234);
    }

    #[test]
    fn test_kill_job_with_multiple_pids() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        
        // Add a job with multiple PIDs (use invalid PIDs so we don't actually kill anything)
        let job = Job::new(1, Some(999998), "sleep 10 | cat &".to_string(), vec![999998, 999999], false);
        shell_state.job_table.borrow_mut().add_job(job);

        let mut output = Vec::new();
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "%1".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        // Should have error messages for both PIDs
        assert!(output_str.contains("999998") || output_str.contains("999999"));
    }

    #[test]
    fn test_get_target_pids_negative_pid() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();

        // Test negative PID (process group)
        let result = KillBuiltin::get_target_pids("-123", &shell_state);
        assert!(result.is_ok());
        let pids = result.unwrap();
        assert_eq!(pids.len(), 1);
        assert_eq!(pids[0], -123);
    }

    #[test]
    fn test_get_target_pids_zero() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();

        // Test PID 0 (current process group)
        let result = KillBuiltin::get_target_pids("0", &shell_state);
        assert!(result.is_ok());
        let pids = result.unwrap();
        assert_eq!(pids.len(), 1);
        assert_eq!(pids[0], 0);
    }

    #[test]
    fn test_get_target_pids_minus_one() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();

        // Test PID -1 (all processes)
        let result = KillBuiltin::get_target_pids("-1", &shell_state);
        assert!(result.is_ok());
        let pids = result.unwrap();
        assert_eq!(pids.len(), 1);
        assert_eq!(pids[0], -1);
    }

    #[test]
    fn test_get_target_pids_positive_pid() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();

        // Test positive PID (single process)
        let result = KillBuiltin::get_target_pids("1234", &shell_state);
        assert!(result.is_ok());
        let pids = result.unwrap();
        assert_eq!(pids.len(), 1);
        assert_eq!(pids[0], 1234);
    }

    #[test]
    fn test_send_signal_preserves_negative_pid() {
        // Test that send_signal correctly passes negative PIDs to libc::kill
        // We use a non-existent process group to avoid actually signaling anything
        let result = KillBuiltin::send_signal(-999999, libc::SIGTERM);
        
        // Should fail with ESRCH (no such process group)
        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        assert!(err_msg.contains("No such process group") || err_msg.contains("999999"));
    }

    #[test]
    fn test_send_signal_zero_pid() {
        // Test that send_signal handles PID 0 (current process group)
        // This should succeed if we have permission to signal our own process group
        let result = KillBuiltin::send_signal(0, 0);
        
        // Signal 0 is a null signal used to check if we can signal the process
        // This should succeed for our own process group
        assert!(result.is_ok());
    }

    #[test]
    fn test_kill_negative_pid_process_group() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Try to kill a non-existent process group
        // Use -s to specify signal explicitly, then negative PID
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-s".to_string(), "TERM".to_string(), "-999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Should fail because process group doesn't exist
        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("No such process group") || output_str.contains("999999"));
    }

    #[test]
    fn test_kill_zero_current_process_group() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Signal 0 to current process group (should succeed)
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-s".to_string(), "0".to_string(), "0".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Signal 0 is now valid (null signal), should succeed
        assert_eq!(exit_code, 0);
    }

    #[test]
    fn test_kill_minus_one_all_processes() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Try to signal all processes (will likely fail due to permissions)
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-s".to_string(), "0".to_string(), "-1".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Signal 0 is now valid, but -1 may fail due to permissions
        // Accept either success (0) or permission error (1)
        assert!(exit_code == 0 || exit_code == 1);
    }

    #[test]
    fn test_parse_signal_accepts_zero() {
        // Signal 0 should now be accepted (null signal to test process existence)
        let result = KillBuiltin::parse_signal("0");
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 0);
    }

    #[test]
    fn test_kill_multiple_negative_pids() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Try to kill multiple non-existent process groups
        // Use default signal (TERM) and specify negative PIDs explicitly with -s
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-s".to_string(), "TERM".to_string(), "-999998".to_string(), "-999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Should fail for both process groups
        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        // Should have error messages for both process groups (check for "process group" or the PIDs)
        assert!(output_str.contains("process group") || output_str.contains("999998") || output_str.contains("999999"));
    }

    #[test]
    fn test_kill_mixed_positive_and_negative_pids() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Mix of positive PID and negative PID (process group) - use -- for negative PID
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "--".to_string(), "999998".to_string(), "-999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Should fail for both
        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("999998") || output_str.contains("999999"));
    }

    #[test]
    fn test_get_target_pids_preserves_sign_for_large_negative() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let shell_state = ShellState::new();

        // Test large negative PID
        let result = KillBuiltin::get_target_pids("-32768", &shell_state);
        assert!(result.is_ok());
        let pids = result.unwrap();
        assert_eq!(pids.len(), 1);
        assert_eq!(pids[0], -32768);
    }

    #[test]
    fn test_kill_double_dash_separator() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Use -- to separate options from arguments
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-s".to_string(), "TERM".to_string(), "--".to_string(), "-999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Should fail because process group doesn't exist
        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("No such process group") || output_str.contains("999999"));
    }

    #[test]
    fn test_kill_double_dash_with_default_signal() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Use -- with default signal (TERM)
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "--".to_string(), "-999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Should fail because process group doesn't exist
        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("No such process group") || output_str.contains("999999"));
    }

    #[test]
    fn test_kill_negative_pid_without_double_dash() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Without --, negative numbers are treated as signal specifications
        // -999999 is not a valid signal, so it should fail with invalid signal error
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Should fail with invalid signal specification
        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("invalid signal specification") || output_str.contains("usage"));
    }

    #[test]
    fn test_kill_ambiguous_negative_number_as_signal() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // -9 should be interpreted as signal 9 (SIGKILL), not PID -9
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-9".to_string(), "999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Should fail because PID doesn't exist
        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("No such process") || output_str.contains("999999"));
    }

    #[test]
    fn test_kill_negative_pid_after_explicit_signal() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // After explicit signal with -s, negative numbers should be PIDs
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-s".to_string(), "TERM".to_string(), "-999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Should fail because process group doesn't exist
        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("No such process group") || output_str.contains("999999"));
    }

    #[test]
    fn test_kill_double_dash_multiple_negative_pids() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // Multiple negative PIDs after --
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "--".to_string(), "-999998".to_string(), "-999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        // Should fail for both process groups
        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("process group") || output_str.contains("999998") || output_str.contains("999999"));
    }

    #[test]
    fn test_kill_invalid_signal_with_negative_number() {
        let _lock = JOB_CONTROL_LOCK.lock().unwrap();
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

        // -INVALID should fail as invalid signal, not be treated as negative PID
        let cmd = ShellCommand {
            args: vec!["kill".to_string(), "-INVALID".to_string(), "999999".to_string()],
            redirections: Vec::new(),
            compound: None,
        };

        let builtin = KillBuiltin;
        let exit_code = builtin.run(&cmd, &mut shell_state, &mut output);

        assert_eq!(exit_code, 1);
        let output_str = String::from_utf8(output).unwrap();
        assert!(output_str.contains("invalid signal specification"));
    }
}