shell/
jobs.rs

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
use crate::command_data::Run;
use crate::parse::parse_line;
use crate::platform::{OsSignal, Pid, Platform, Sys, TermSettings, STDIN_FILENO};
use std::collections::HashMap;
use std::ffi::{OsStr, OsString};
use std::fmt::{Display, Formatter};
use std::{env, fmt, io};

/// Status of a Job.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum JobStatus {
    /// New job, not initialized.
    New,
    /// Running job.
    Running,
    /// Job is paused in background.
    Stopped,
    /// Job is done.
    Done,
}

impl fmt::Display for JobStatus {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            JobStatus::New => write!(f, "New"),
            JobStatus::Running => write!(f, "Running"),
            JobStatus::Stopped => write!(f, "Stopped"),
            JobStatus::Done => write!(f, "Done"),
        }
    }
}

/// Status of a process that is part of a job.
#[derive(Copy, Clone, Debug)]
pub enum PidStatus {
    /// Process is running, contains the pid.
    Running(Pid),
    /// Process is done, contains the pid and status code.
    Done(Pid, i32), // pid, status
    /// Process had an error, no status code available.  Contains the pid.
    Error(Pid),
    /// Process was stopped due to a signal.  Contains the pid and signal that stopped it.
    Signaled(Pid, OsSignal),
}

impl PidStatus {
    pub fn pid(&self) -> Pid {
        match self {
            PidStatus::Running(pid) => *pid,
            PidStatus::Done(pid, _) => *pid,
            PidStatus::Error(pid) => *pid,
            PidStatus::Signaled(pid, _) => *pid,
        }
    }
}

#[derive(Clone, Debug)]
pub struct Job {
    id: u32,
    shell_pid: Pid,
    pgid: Pid,
    pids: Vec<PidStatus>,
    names: Vec<String>,
    status: JobStatus,
    interactive: bool,
    stealth: bool, // If true don't report when background job ends.
}

impl Job {
    /// Create a new empty job with id.
    fn new(id: u32, shell_pid: Pid, interactive: bool) -> Self {
        Self {
            id,
            shell_pid,
            pgid: shell_pid,
            pids: vec![],
            names: vec![],
            status: JobStatus::New,
            interactive,
            stealth: false,
        }
    }

    /// True if this job is empty (contains no processes).
    pub fn is_empty(&self) -> bool {
        self.pids.is_empty()
    }

    /// The process group id for the job.  Should be equal to the first pid.
    pub fn pgid(&self) -> Pid {
        self.pgid
    }

    /// Pids with status that make up a job.
    pub fn pids(&self) -> &[PidStatus] {
        &self.pids[..]
    }

    /// Status of this job.
    pub fn status(&self) -> JobStatus {
        self.status
    }

    /// Mark the job as stopped.
    pub fn mark_stopped(&mut self) {
        self.status = JobStatus::Stopped;
    }

    /// Mark the job as running.
    pub fn mark_running(&mut self) {
        self.status = JobStatus::Running;
    }

    /// Mark the job status to done.
    pub fn mark_done(&mut self) {
        self.status = JobStatus::Done;
    }

    /// Add pid to list of pids as Running with name.
    /// If the pid list is currently empty record this pid as the pgid (process group) for the job.
    pub fn add_process(&mut self, pid: Pid, name: impl Into<String>) {
        if self.pids.is_empty() {
            self.pgid = pid;
        }
        self.pids.push(PidStatus::Running(pid));
        self.names.push(name.into());
    }

    /// Move the process pid to the done state with status.
    /// Will panic if pid is not part of job.
    pub fn process_done(&mut self, pid: Pid, status: i32) {
        for proc in self.pids.iter_mut() {
            if proc.pid() == pid {
                *proc = PidStatus::Done(pid, status);
                return;
            }
        }
        panic!("process {pid} not part of job");
    }

    /// Move the process pid to the error state.
    /// Will panic if pid is not part of job.
    pub fn process_error(&mut self, pid: Pid) {
        for proc in self.pids.iter_mut() {
            if proc.pid() == pid {
                *proc = PidStatus::Error(pid);
                return;
            }
        }
        panic!("process {pid} not part of job");
    }

    /// Move the process pid to the signaled state.
    /// Will panic if pid is not part of job.
    pub fn process_signaled(&mut self, pid: Pid, signal: OsSignal) {
        for proc in self.pids.iter_mut() {
            if proc.pid() == pid {
                *proc = PidStatus::Signaled(pid, signal);
                return;
            }
        }
        panic!("process {pid} not part of job");
    }

    /// Reverse the list of pids in the job.
    pub fn reverse(&mut self) {
        self.pids.reverse();
        self.names.reverse();
    }

    /// Set the stealth flag, if true then do NOT print out when a background job ends (be stealthy...).
    pub fn set_stealth(&mut self, stealth: bool) {
        self.stealth = stealth;
    }

    /// Is this a stealth job (do not report when complete if in the background)?
    pub fn stealth(&self) -> bool {
        self.stealth
    }

    /// Is this job running in an interactive shell?
    pub fn interactive(&self) -> bool {
        self.interactive
    }

    /// The PID of the shell running this job.
    pub fn shell_pid(&self) -> Pid {
        self.shell_pid
    }
}

impl Display for Job {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        writeln!(
            f,
            "[{}]\t{}\t{:?}\t{:?}",
            self.id, self.status, self.pids, self.names
        )
    }
}

pub struct Jobs {
    next_job: u32,
    jobs: Vec<Job>,
    shell_pid: Pid,
    interactive: bool,
    term_settings: Option<TermSettings>,
    alias: HashMap<String, Run>,
    local_vars: HashMap<OsString, OsString>,
}

impl Jobs {
    pub fn new(interactive: bool) -> Self {
        let shell_pid = Sys::getpid();
        let term_settings = if interactive {
            if let Ok(term_setting) = Sys::get_term_settings(STDIN_FILENO) {
                Some(term_setting)
            } else {
                None
            }
        } else {
            None
        };
        Self {
            next_job: 0,
            jobs: vec![],
            shell_pid,
            interactive,
            term_settings,
            alias: HashMap::new(),
            local_vars: HashMap::new(),
        }
    }

    pub fn cap_term(&mut self) {
        self.term_settings = if let Ok(term_setting) = Sys::get_term_settings(STDIN_FILENO) {
            Some(term_setting)
        } else {
            None
        };
    }

    /// Create a new job with the next job number.
    /// Note, this new job is NOT in the jobs list.
    pub fn new_job(&mut self) -> Job {
        if self.jobs.is_empty() {
            self.next_job = 0;
        }
        // Job numbers/ids always start at 1.
        self.next_job += 1;
        Job::new(self.next_job, self.shell_pid, self.interactive)
    }

    /// Push job onto the list of jobs.
    pub fn push_job(&mut self, job: Job) {
        self.jobs.push(job);
    }

    /// Set not on a tty.
    pub fn set_no_tty(&mut self) {
        self.term_settings = None;
    }

    /// Sets whether we are interactive or not.
    pub fn set_interactive(&mut self, interactive: bool) {
        self.interactive = interactive;
    }

    /// Get the mutable job for job_id if it exists.
    pub fn get_job_mut(&mut self, job_id: u32) -> Option<&mut Job> {
        self.jobs.iter_mut().find(|job| job.id == job_id)
    }

    /// Check any pids in a job by calling wait and updating the books.
    pub fn reap_procs(&mut self) {
        for job in self.jobs.iter_mut() {
            if let JobStatus::Running = job.status() {
                let pids: Vec<Pid> = job.pids().iter().map(|s| s.pid()).collect();
                for pid in &pids {
                    Sys::try_wait_pid(*pid, job);
                }
                let mut done = true;
                for pid_status in job.pids() {
                    if let PidStatus::Running(_) = pid_status {
                        done = false;
                    }
                }
                if done {
                    job.mark_done();
                }
            }
        }
        // Remove any Done jobs.
        let jobs_len = self.jobs.len();
        for i in (0..jobs_len).rev() {
            if let JobStatus::Done = self.jobs[i].status() {
                if !self.jobs[i].stealth() {
                    eprintln!("{}", self.jobs[i]);
                }
                self.jobs.remove(i);
            }
        }
    }

    /// Move the job for job_num to te foreground.
    pub fn foreground_job(&mut self, job_num: u32) {
        let term_settings = self.term_settings.clone();
        if let Some(job) = self.get_job_mut(job_num) {
            if let Err(err) = Sys::foreground_job(job, &term_settings) {
                eprintln!("Error making job {job_num} foreground in parent: {err}");
            }
        } else {
            eprintln!("job number {job_num} is invalid");
        }
    }

    /// Move the job for job_num to te background and running (start a stopped job in the background).
    pub fn background_job(&mut self, job_num: u32) {
        if let Some(job) = self.get_job_mut(job_num) {
            if let Err(err) = Sys::background_job(job) {
                eprintln!("Error making job {job_num} background in parent: {err}");
            }
        } else {
            eprintln!("job number {job_num} is invalid");
        }
    }

    /// Restore terminal settings and put the shell back into the foreground.
    pub fn restore_terminal(&self) {
        // This assumes file descriptor 0 (STDIN) is the terminal.
        // Move the shell back into the foreground.
        if let Some(term_settings) = &self.term_settings {
            // Restore terminal settings.
            if let Err(err) = Sys::restore_terminal(term_settings, self.shell_pid) {
                eprintln!("Error resetting shell terminal settings: {}", err);
            }
        }
    }

    /// Gets an alias.
    pub fn get_alias<S: AsRef<str>>(&self, name: S) -> Option<Run> {
        self.alias.get(name.as_ref()).cloned()
    }

    /// Clears all the existing aliases.
    pub fn clear_aliases(&mut self) {
        self.alias.clear();
    }

    /// Removes the alias name, return the value if removed..
    pub fn remove_alias<S: AsRef<str>>(&mut self, name: S) -> Option<Run> {
        self.alias.remove(name.as_ref())
    }

    /// Add an alias.
    pub fn add_alias(&mut self, name: String, value: String) -> Result<(), io::Error> {
        let runj = parse_line(self, &value)?;
        self.alias.insert(name, runj.into_run());
        Ok(())
    }

    /// Add an already parsed alias.
    pub fn add_alias_run(&mut self, name: String, value: Run) {
        self.alias.insert(name, value);
    }

    /// Print a the alias for name if set.
    pub fn print_alias(&self, name: String) {
        if let Some(value) = self.alias.get(&name) {
            println!("alias {name}='{value}'");
        } else {
            eprintln!("no alias set for: {name}");
        }
    }

    /// Print all the defined aliases.
    pub fn print_all_alias(&self) {
        for (name, value) in &self.alias {
            println!("alias {name}='{value}'");
        }
    }

    /// Set a local var into key.
    /// Will overwrite an existing value.
    pub fn set_local_var(&mut self, key: OsString, val: OsString) {
        self.local_vars.insert(key, val);
    }

    /// Get the local var key or None if does not exist.
    pub fn get_local_var(&self, key: &OsStr) -> Option<&OsStr> {
        self.local_vars.get(key).map(|s| s as &OsStr)
    }

    /// Remove the local var key.
    /// If key exists returns the removed value.
    pub fn remove_local_var(&mut self, key: &OsStr) -> Option<OsString> {
        self.local_vars.remove(key)
    }

    /// First tries to fin key in the environment then checks local variable and returns None if
    /// not found.
    pub fn get_env_or_local_var(&self, key: &OsStr) -> Option<OsString> {
        if let Some(val) = env::var_os(key) {
            Some(val)
        } else {
            self.get_local_var(key).map(|val| val.to_os_string())
        }
    }
}

impl Display for Jobs {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        for job in &self.jobs {
            writeln!(f, "{job}")?;
        }
        Ok(())
    }
}