shell/
run.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
use crate::builtins::run_builtin;
use crate::command_data::{CommandWithArgs, Run};
use crate::jobs::{Job, Jobs};
use crate::parse::parse_line;
use crate::platform::{FileDesc, Platform, Sys};
use crate::signals::{install_sigint_handler, mask_signals};
use std::{env, io};

pub fn setup_shell_tty(shell_terminal: FileDesc) {
    Sys::terminal_foreground(shell_terminal);

    mask_signals();

    if let Err(err) = Sys::set_self_pgroup() {
        eprintln!("Couldn't put the shell in its own process group: {}\n", err)
    }
    /* Grab control of the terminal.  */
    if let Err(err) = Sys::grab_terminal(shell_terminal) {
        eprintln!("Couldn't grab control of terminal: {err}");
        return;
    }

    if !install_sigint_handler() {
        std::process::exit(1)
    }
}

/// Finish a job run.
///
/// If not a background job will wait for proc to end and return the exit status
/// or -66 if the wait fails.  If it is a background job then return the pid of
/// the last proc in the job.
/// Also sets the LAST_STATUS env var to the exit status (if not background) or
/// 0 if a background job.
fn finish_run(background: bool, mut job: Job, jobs: &mut Jobs) -> i32 {
    job.mark_running();
    let status = if !background {
        if let Some(status) = Sys::wait_job(&mut job) {
            env::set_var("LAST_STATUS", format!("{}", status));
            status
        } else {
            env::set_var("LAST_STATUS", format!("{}", -66));
            jobs.push_job(job);
            -66
        }
    } else {
        env::set_var("LAST_STATUS", format!("{}", 0));
        let pid = if let Some(p) = job.pids().last() {
            p.pid().try_into().unwrap_or(0)
        } else {
            0
        };
        jobs.push_job(job);
        pid
    };
    jobs.restore_terminal();
    status
}

fn run_command(
    command: &CommandWithArgs,
    jobs: &mut Jobs,
    background: bool,
    stealth: bool,
) -> Result<i32, io::Error> {
    Ok(if let Some(command_name) = command.command(jobs) {
        let command_name = command_name?;
        if let Some(init_alias_run) = jobs.remove_alias(command_name.to_string_lossy()) {
            // need to remove the alias so we don't recurse for alias with a command the same name
            let mut alias_run = init_alias_run.clone();
            for arg in command.args_iter() {
                alias_run.push_arg_end(arg.clone());
            }
            if let Some(stdios) = command.stdios() {
                alias_run.extend_redirs_end(stdios)
            }
            let r = run_job(&alias_run, jobs, background);
            jobs.add_alias_run(command_name.to_string_lossy().to_string(), init_alias_run);
            r?
        } else {
            let mut args = command.args_iter();
            match run_builtin(&command_name, &mut args, jobs) {
                Some(status) => status,
                None => {
                    let mut job = jobs.new_job();
                    job.set_stealth(stealth);
                    match Sys::fork_exec(command, &mut job, jobs) {
                        Ok(()) => finish_run(background, job, jobs),
                        Err(err) => {
                            // Make sure we restore the terminal...
                            jobs.restore_terminal();
                            return Err(err);
                        }
                    }
                }
            }
        }
    } else {
        0
    })
}

pub fn run_job(run: &Run, jobs: &mut Jobs, force_background: bool) -> Result<i32, io::Error> {
    let status = match run {
        Run::Command(command) => run_command(command, jobs, force_background, force_background)?,
        Run::BackgroundCommand(command) => run_command(command, jobs, true, false)?,
        Run::Pipe(pipe) => {
            let mut job = jobs.new_job();
            job.set_stealth(force_background);
            match run_pipe(&pipe[..], &mut job, jobs) {
                Ok(background) => finish_run(force_background || background, job, jobs),
                Err(err) => {
                    // Make sure we restore the terminal...
                    jobs.restore_terminal();
                    return Err(err);
                }
            }
        }
        Run::Sequence(seq) => {
            let mut status = 0;
            for r in seq {
                status = run_job(r, jobs, force_background)?;
            }
            status
        }
        Run::And(seq) => {
            // XXXX should background == true be an error?
            let mut status = 0;
            for r in seq {
                status = run_job(r, jobs, force_background)?;
                if status != 0 {
                    break;
                }
            }
            status
        }
        Run::Or(seq) => {
            // XXXX should background == true be an error?
            let mut status = 0;
            for r in seq {
                status = run_job(r, jobs, force_background)?;
                if status == 0 {
                    break;
                }
            }
            status
        }
        Run::Subshell(sub_run) => {
            let mut job = jobs.new_job();
            job.set_stealth(force_background);
            match Sys::fork_run(sub_run, &mut job, jobs) {
                Ok(()) => finish_run(false, job, jobs),
                Err(err) => {
                    // Make sure we restore the terminal...
                    jobs.restore_terminal();
                    return Err(err);
                }
            }
        }
        Run::Empty => 0,
    };
    Ok(status)
}

pub fn run_one_command(command: &str, jobs: &mut Jobs) -> Result<i32, io::Error> {
    // Try to make sense out of whatever crap we get (looking at you fzf-tmux)
    // and make it work.
    let commands = parse_line(jobs, command)?;

    run_job(commands.commands(), jobs, false)
}

fn pipe_command(
    command: &CommandWithArgs,
    next_in: Option<FileDesc>,
    next_out: Option<FileDesc>,
    job: &mut Job,
    jobs: &mut Jobs,
) -> Result<(), io::Error> {
    let mut command = command.clone();
    if let Some(command_name) = command.command(jobs) {
        let command_name = command_name?;
        if let Some(mut alias_run) = jobs.get_alias(command_name.to_string_lossy()) {
            alias_run.push_stdin_front(next_in);
            alias_run.push_stdout_front(next_out);
            for arg in command.args_iter() {
                alias_run.push_arg_end(arg.clone());
            }
            if let Some(stdios) = command.stdios() {
                alias_run.extend_redirs_end(stdios)
            }
            match alias_run {
                Run::Command(command) | Run::BackgroundCommand(command) => {
                    Sys::fork_exec(&command, job, jobs)?;
                }
                _ => {
                    Sys::fork_run(&alias_run, job, jobs)?;
                }
            }
        } else {
            command.push_stdin_front(next_in);
            command.push_stdout_front(next_out);
            Sys::fork_exec(&command, job, jobs)?;
        }
    }
    Ok(())
}

fn run_pipe(new_job: &[Run], job: &mut Job, jobs: &mut Jobs) -> Result<bool, io::Error> {
    let progs = new_job.len();
    let (p_in, p_out) = Sys::anon_pipe()?;
    let mut next_in = Some(p_in);
    let mut next_out = None;
    let mut upcoming_out = p_out;
    let mut background = false;
    for (i, program) in new_job.iter().rev().enumerate() {
        match program {
            Run::Command(command) => {
                pipe_command(command, next_in, next_out, job, jobs)?;
            }
            Run::BackgroundCommand(command) => {
                pipe_command(command, next_in, next_out, job, jobs)?;
                if i == 0 {
                    background = true;
                }
            }
            Run::Subshell(_) => {
                let mut program = program.clone();
                program.push_stdin_front(next_in);
                program.push_stdout_front(next_out);
                if let Run::Subshell(sub_run) = &mut program {
                    match Sys::fork_run(&*sub_run, job, jobs) {
                        Ok(()) => {
                            jobs.restore_terminal();
                        }
                        Err(err) => {
                            // Make sure we restore the terminal...
                            jobs.restore_terminal();
                            return Err(err);
                        }
                    }
                }
            }
            Run::Pipe(_) | Run::Sequence(_) | Run::And(_) | Run::Or(_) => {
                // Don't think this is expressible with the parser and maybe should be an error?
                let mut program = program.clone();
                program.push_stdin_front(next_in);
                program.push_stdout_front(next_out);
                run_job(&program, jobs, true)?;
            }
            Run::Empty => {}
        }
        next_out = Some(upcoming_out);
        if i < (progs - 1) {
            let (p_in, p_out) = Sys::anon_pipe()?;
            upcoming_out = p_out;
            next_in = Some(p_in);
        } else {
            next_in = None;
        }
    }
    if job.is_empty() {
        Err(io::Error::new(io::ErrorKind::Other, "no processes started"))
    } else {
        job.reverse();
        Ok(background)
    }
}