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
use crate::builtins::Builtin;
use crate::parser::ShellCommand;
use crate::state::{JobStatus, ShellState};
use std::io::Write;

pub struct FgBuiltin;

impl FgBuiltin {
    /// Bring a job to the foreground
    fn foreground_job(job_id: usize, shell_state: &mut ShellState, output_writer: &mut dyn Write) -> i32 {
        // Get the job
        let job = {
            let job_table = shell_state.job_table.borrow();
            match job_table.get_job(job_id) {
                Some(j) => j.clone(),
                None => {
                    let _ = writeln!(output_writer, "fg: %{}: no such job", job_id);
                    return 1;
                }
            }
        };

        // Check if job is already done
        if let JobStatus::Done(code) = job.status {
            let _ = writeln!(output_writer, "fg: job has terminated");
            // Remove the completed job from the table
            shell_state.job_table.borrow_mut().remove_job(job_id);
            return code;
        }

        // Print the command being foregrounded
        let _ = writeln!(output_writer, "{}", job.command);

        // Only perform terminal control in interactive mode
        if shell_state.interactive {
            // Get the process group ID
            let pgid = match job.pgid {
                Some(pgid) => pgid as libc::pid_t,
                None => {
                    // For builtin jobs without PGID, we can't foreground them
                    let _ = writeln!(output_writer, "fg: job does not have a process group");
                    return 1;
                }
            };

            // Get terminal file descriptor
            let terminal_fd = match shell_state.terminal_fd {
                Some(fd) => fd,
                None => {
                    let _ = writeln!(output_writer, "fg: no controlling terminal");
                    return 1;
                }
            };

            // Give the job's process group control of the terminal
            // SAFETY: tcsetpgrp is a standard POSIX function that requires a valid fd and pgid
            let result = unsafe { libc::tcsetpgrp(terminal_fd, pgid) };
            if result == -1 {
                let err = std::io::Error::last_os_error();
                let _ = writeln!(output_writer, "fg: failed to set terminal process group: {}", err);
                return 1;
            }

            // If the job was stopped, send SIGCONT to resume it
            if job.status == JobStatus::Stopped {
                // Prefer signaling the process group if available
                if let Some(pgid) = job.pgid {
                    // SAFETY: kill is a standard POSIX function; we're sending SIGCONT to the process group
                    let _ = unsafe { libc::kill(-(pgid as libc::pid_t), libc::SIGCONT) };
                } else {
                    // Fall back to signaling each PID individually
                    for pid in &job.pids {
                        // SAFETY: kill is a standard POSIX function; we're sending SIGCONT to resume the process
                        let _ = unsafe { libc::kill(*pid as libc::pid_t, libc::SIGCONT) };
                    }
                }
                // Update job status to running
                shell_state.job_table.borrow_mut().get_job_mut(job_id).unwrap().update_status(JobStatus::Running);
            }

            // Wait for the job to complete
            let exit_code = Self::wait_for_job(&job, shell_state);

            // Restore terminal control to the shell
            // SAFETY: getpgrp and tcsetpgrp are standard POSIX functions
            let shell_pgid = unsafe { libc::getpgrp() };
            let _ = unsafe { libc::tcsetpgrp(terminal_fd, shell_pgid) };

            // Remove completed job from table or update status
            match shell_state.job_table.borrow().get_job(job_id) {
                Some(j) if matches!(j.status, JobStatus::Done(_)) => {
                    shell_state.job_table.borrow_mut().remove_job(job_id);
                }
                _ => {}
            }

            exit_code
        } else {
            // Non-interactive mode: just wait for the job
            // Send SIGCONT if stopped
            if job.status == JobStatus::Stopped {
                // Prefer signaling the process group if available
                if let Some(pgid) = job.pgid {
                    // SAFETY: kill is a standard POSIX function; we're sending SIGCONT to the process group
                    let _ = unsafe { libc::kill(-(pgid as libc::pid_t), libc::SIGCONT) };
                } else {
                    // Fall back to signaling each PID individually
                    for pid in &job.pids {
                        // SAFETY: kill is a standard POSIX function; we're sending SIGCONT to resume the process
                        let _ = unsafe { libc::kill(*pid as libc::pid_t, libc::SIGCONT) };
                    }
                }
                shell_state.job_table.borrow_mut().get_job_mut(job_id).unwrap().update_status(JobStatus::Running);
            }

            let exit_code = Self::wait_for_job(&job, shell_state);

            // Remove completed job from table
            match shell_state.job_table.borrow().get_job(job_id) {
                Some(j) if matches!(j.status, JobStatus::Done(_)) => {
                    shell_state.job_table.borrow_mut().remove_job(job_id);
                }
                _ => {}
            }

            exit_code
        }
    }

    /// Wait for a job to complete
    fn wait_for_job(job: &crate::state::Job, shell_state: &mut ShellState) -> i32 {
        let mut last_exit_code = 0;

        // Wait for all PIDs in the job
        for pid in &job.pids {
            let mut status: libc::c_int = 0;
            
            // Use WUNTRACED to detect if the job is stopped again
            // SAFETY: waitpid is a standard POSIX function for waiting on child processes
            let result = loop {
                let res = unsafe {
                    libc::waitpid(*pid as libc::pid_t, &mut status, libc::WUNTRACED)
                };
                if res == -1 {
                    let err = std::io::Error::last_os_error();
                    if err.raw_os_error() == Some(libc::EINTR) {
                        continue;
                    }
                    // ECHILD means the process doesn't exist or isn't a child
                    if err.raw_os_error() != Some(libc::ECHILD) {
                        eprintln!("fg: waitpid failed: {}", err);
                    }
                }
                break res;
            };
            if result == -1 {
                continue;
            }

            // Extract exit code or signal from status
            // SAFETY: These macros are safe to use on a valid status from waitpid
            let exit_code = if libc::WIFEXITED(status) {
                libc::WEXITSTATUS(status)
            } else if libc::WIFSIGNALED(status) {
                128 + libc::WTERMSIG(status)
            } else if libc::WIFSTOPPED(status) {
                // Job was stopped (e.g., by Ctrl-Z)
                let signal = libc::WSTOPSIG(status);
                
                // Update job status to stopped
                {
                    let mut job_table = shell_state.job_table.borrow_mut();
                    if let Some(job) = job_table.get_job_mut(job.job_id) {
                        job.update_status(JobStatus::Stopped);
                    }
                }
                
                // Return 128 + signal number for stopped jobs
                128 + signal
            } else {
                0
            };

            last_exit_code = exit_code;
        }

        // Update job status if all processes completed
        {
            let mut job_table = shell_state.job_table.borrow_mut();
            if let Some(job) = job_table.get_job_mut(job.job_id)
                && job.status != JobStatus::Stopped {
                    job.update_status(JobStatus::Done(last_exit_code));
                }
        }

        last_exit_code
    }
}

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

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

    fn description(&self) -> &'static str {
        "Move job to the foreground"
    }

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

        // Parse jobspec (default to current job if no argument)
        let job_id = if args.len() == 1 {
            // No argument - use current job
            match shell_state.job_table.borrow().get_current_job() {
                Some(id) => id,
                None => {
                    let _ = writeln!(output_writer, "fg: no current job");
                    return 1;
                }
            }
        } else if args.len() == 2 {
            // Parse jobspec argument
            match shell_state.job_table.borrow().parse_jobspec(&args[1], "fg") {
                Ok(id) => id,
                Err(e) => {
                    let _ = writeln!(output_writer, "{}", e);
                    return 1;
                }
            }
        } else {
            // Too many arguments
            let _ = writeln!(output_writer, "fg: usage: fg [job_spec]");
            return 1;
        };

        // Verify the job exists
        {
            let job_table = shell_state.job_table.borrow();
            if job_table.get_job(job_id).is_none() {
                let _ = writeln!(output_writer, "fg: %{}: no such job", job_id);
                return 1;
            }
        }

        // Bring the job to the foreground
        Self::foreground_job(job_id, shell_state, output_writer)
    }
}

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

    #[test]
    fn test_fg_no_jobs() {
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

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

        let builtin = FgBuiltin;
        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 current job"));
    }

    #[test]
    fn test_fg_invalid_jobspec() {
        let mut shell_state = ShellState::new();
        
        // Add a job
        let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
        shell_state.job_table.borrow_mut().add_job(job);

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

        let builtin = FgBuiltin;
        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_fg_completed_job() {
        let mut shell_state = ShellState::new();
        
        // Add a completed job
        let mut job = Job::new(1, Some(1234), "sleep 1 &".to_string(), vec![1234], false);
        job.update_status(JobStatus::Done(0));
        shell_state.job_table.borrow_mut().add_job(job);

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

        let builtin = FgBuiltin;
        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("job has terminated"));
    }

    #[test]
    fn test_fg_current_jobspec() {
        let mut shell_state = ShellState::new();
        
        // Add a completed job as current
        let mut job = Job::new(1, Some(1234), "sleep 1 &".to_string(), vec![1234], false);
        job.update_status(JobStatus::Done(0));
        shell_state.job_table.borrow_mut().add_job(job);

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

        let builtin = FgBuiltin;
        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("job has terminated"));
    }

    #[test]
    fn test_fg_previous_jobspec() {
        let mut shell_state = ShellState::new();
        
        // Add two jobs
        let mut job1 = Job::new(1, Some(1234), "sleep 1 &".to_string(), vec![1234], false);
        job1.update_status(JobStatus::Done(0));
        let job2 = Job::new(2, Some(1235), "sleep 2 &".to_string(), vec![1235], false);
        shell_state.job_table.borrow_mut().add_job(job1);
        shell_state.job_table.borrow_mut().add_job(job2);

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

        let builtin = FgBuiltin;
        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("job has terminated"));
    }

    #[test]
    fn test_fg_no_current_job() {
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

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

        let builtin = FgBuiltin;
        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 current job"));
    }

    #[test]
    fn test_fg_no_previous_job() {
        let mut shell_state = ShellState::new();
        
        // Add only one job (no previous)
        let job = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
        shell_state.job_table.borrow_mut().add_job(job);

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

        let builtin = FgBuiltin;
        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 previous job"));
    }

    #[test]
    fn test_fg_direct_number_jobspec() {
        let mut shell_state = ShellState::new();
        
        // Add a completed job
        let mut job = Job::new(5, Some(1234), "sleep 1 &".to_string(), vec![1234], false);
        job.update_status(JobStatus::Done(0));
        shell_state.job_table.borrow_mut().add_job(job);

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

        let builtin = FgBuiltin;
        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("job has terminated"));
    }

    #[test]
    fn test_fg_too_many_arguments() {
        let mut shell_state = ShellState::new();
        let mut output = Vec::new();

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

        let builtin = FgBuiltin;
        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_fg_command_prefix_match() {
        let shell_state = ShellState::new();
        
        // Add jobs with different commands - both running so prefix match works
        let job1 = Job::new(1, Some(1234), "sleep 10 &".to_string(), vec![1234], false);
        let job2 = Job::new(2, Some(1235), "grep pattern file &".to_string(), vec![1235], false);
        shell_state.job_table.borrow_mut().add_job(job1);
        shell_state.job_table.borrow_mut().add_job(job2);

        // Should match job 1 (sleep)
        let result = shell_state.job_table.borrow().parse_jobspec("%sleep", "fg");
        assert_eq!(result.unwrap(), 1);
    }

    #[test]
    fn test_fg_command_contains_match() {
        let shell_state = ShellState::new();
        
        // Add jobs with different commands - both running so contains match works
        let job1 = Job::new(1, Some(1234), "cat file.txt &".to_string(), vec![1234], false);
        let job2 = Job::new(2, Some(1235), "grep pattern file &".to_string(), vec![1235], false);
        shell_state.job_table.borrow_mut().add_job(job1);
        shell_state.job_table.borrow_mut().add_job(job2);

        // Should match job 2 (contains "pattern")
        let result = shell_state.job_table.borrow().parse_jobspec("%?pattern", "fg");
        assert_eq!(result.unwrap(), 2);
    }

    #[test]
    fn test_fg_builtin_job_without_pgid() {
        let mut shell_state = ShellState::new();
        shell_state.interactive = true;
        shell_state.terminal_fd = Some(libc::STDIN_FILENO);
        
        // Add a builtin job without PGID
        let job = Job::new(1, None, "sleep 10 &".to_string(), vec![], true);
        shell_state.job_table.borrow_mut().add_job(job);

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

        let builtin = FgBuiltin;
        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("does not have a process group"));
    }

    #[test]
    fn test_fg_exit_code_propagation() {
        let mut shell_state = ShellState::new();
        
        // Add a completed job with non-zero exit code
        let mut job = Job::new(1, Some(1234), "false &".to_string(), vec![1234], false);
        job.update_status(JobStatus::Done(1));
        shell_state.job_table.borrow_mut().add_job(job);

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

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

        assert_eq!(exit_code, 1);
    }

    #[test]
    fn test_fg_removes_completed_job() {
        let mut shell_state = ShellState::new();
        
        // Add a completed job
        let mut job = Job::new(1, Some(1234), "sleep 1 &".to_string(), vec![1234], false);
        job.update_status(JobStatus::Done(0));
        shell_state.job_table.borrow_mut().add_job(job);

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

        let builtin = FgBuiltin;
        let _ = builtin.run(&cmd, &mut shell_state, &mut output);

        // Job should be removed from table
        let job_table = shell_state.job_table.borrow();
        assert!(job_table.get_job(1).is_none());
    }

    #[test]
    fn test_fg_command_prefix_skips_completed_jobs() {
        let shell_state = ShellState::new();
        
        // Add a completed job with "sleep" command
        let mut job1 = Job::new(1, Some(1234), "sleep 5 &".to_string(), vec![1234], false);
        job1.update_status(JobStatus::Done(0));
        
        // Add a running job with "sleep" command
        let job2 = Job::new(2, Some(1235), "sleep 10 &".to_string(), vec![1235], false);
        
        shell_state.job_table.borrow_mut().add_job(job1);
        shell_state.job_table.borrow_mut().add_job(job2);

        // Should match job 2 (running), not job 1 (completed)
        let result = shell_state.job_table.borrow().parse_jobspec("%sleep", "fg");
        assert_eq!(result.unwrap(), 2);
    }

    #[test]
    fn test_fg_command_contains_skips_completed_jobs() {
        let shell_state = ShellState::new();
        
        // Add a completed job containing "pattern"
        let mut job1 = Job::new(1, Some(1234), "grep pattern file1 &".to_string(), vec![1234], false);
        job1.update_status(JobStatus::Done(0));
        
        // Add a running job containing "pattern"
        let job2 = Job::new(2, Some(1235), "grep pattern file2 &".to_string(), vec![1235], false);
        
        shell_state.job_table.borrow_mut().add_job(job1);
        shell_state.job_table.borrow_mut().add_job(job2);

        // Should match job 2 (running), not job 1 (completed)
        let result = shell_state.job_table.borrow().parse_jobspec("%?pattern", "fg");
        assert_eq!(result.unwrap(), 2);
    }
}