aboutsummaryrefslogtreecommitdiffstats
path: root/src/os_input_output.rs
blob: cd50555bf59e3db9bebd30b26c52d9a7764a7344 (plain) (blame)
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
use crate::terminal_pane::PositionAndSize;
use nix::fcntl::{fcntl, FcntlArg, OFlag};
use nix::pty::{forkpty, Winsize};
use nix::sys::signal::{kill, Signal};
use nix::sys::termios::{cfmakeraw, tcdrain, tcgetattr, tcsetattr, SetArg, Termios};
use nix::sys::wait::waitpid;
use nix::unistd::{read, write, ForkResult, Pid};
use std::io::{Read, Write};
use std::os::unix::io::RawFd;
use std::path::PathBuf;
use std::process::{Child, Command};
use std::sync::{Arc, Mutex};

use std::env;

fn into_raw_mode(pid: RawFd) {
    let mut tio = tcgetattr(pid).expect("could not get terminal attribute");
    cfmakeraw(&mut tio);
    match tcsetattr(pid, SetArg::TCSANOW, &mut tio) {
        Ok(_) => {}
        Err(e) => panic!("error {:?}", e),
    };
}

fn unset_raw_mode(pid: RawFd, mut orig_termios: Termios) {
    match tcsetattr(pid, SetArg::TCSANOW, &mut orig_termios) {
        Ok(_) => {}
        Err(e) => panic!("error {:?}", e),
    };
}

pub fn get_terminal_size_using_fd(fd: RawFd) -> PositionAndSize {
    // TODO: do this with the nix ioctl
    use libc::ioctl;
    use libc::TIOCGWINSZ;

    let mut winsize = Winsize {
        ws_row: 0,
        ws_col: 0,
        ws_xpixel: 0,
        ws_ypixel: 0,
    };

    unsafe { ioctl(fd, TIOCGWINSZ.into(), &mut winsize) };
    PositionAndSize::from(winsize)
}

pub fn set_terminal_size_using_fd(fd: RawFd, columns: u16, rows: u16) {
    // TODO: do this with the nix ioctl
    use libc::ioctl;
    use libc::TIOCSWINSZ;

    let winsize = Winsize {
        ws_col: columns,
        ws_row: rows,
        ws_xpixel: 0,
        ws_ypixel: 0,
    };
    unsafe { ioctl(fd, TIOCSWINSZ.into(), &winsize) };
}

fn handle_command_exit(mut child: Child) {
    let signals = ::signal_hook::iterator::Signals::new(&[::signal_hook::SIGINT]).unwrap();
    'handle_exit: loop {
        match child.try_wait() {
            Ok(Some(_status)) => {
                // TODO: handle errors?
                break;
            }
            Ok(None) => {
                ::std::thread::sleep(::std::time::Duration::from_millis(100));
            }
            Err(e) => panic!("error attempting to wait: {}", e),
        }

        for signal in signals.pending() {
            match signal {
                signal_hook::SIGINT => {
                    child.kill().unwrap();
                    child.wait().unwrap();
                    break 'handle_exit;
                }
                _ => {}
            }
        }
    }
}

fn spawn_terminal(file_to_open: Option<PathBuf>, orig_termios: Termios) -> (RawFd, RawFd) {
    let (pid_primary, pid_secondary): (RawFd, RawFd) = {
        match forkpty(None, Some(&orig_termios)) {
            Ok(fork_pty_res) => {
                let pid_primary = fork_pty_res.master;
                let pid_secondary = match fork_pty_res.fork_result {
                    ForkResult::Parent { child } => {
                        // fcntl(pid_primary, FcntlArg::F_SETFL(OFlag::empty())).expect("could not fcntl");
                        fcntl(pid_primary, FcntlArg::F_SETFL(OFlag::O_NONBLOCK))
                            .expect("could not fcntl");
                        child
                    }
                    ForkResult::Child => match file_to_open {
                        Some(file_to_open) => {
                            if env::var("EDITOR").is_err() && env::var("VISUAL").is_err() {
                                panic!("Can't edit files if an editor is not defined. To fix: define the EDITOR or VISUAL environment variables with the path to your editor (eg. /usr/bin/vim)");
                            }
                            let editor =
                                env::var("EDITOR").unwrap_or_else(|_| env::var("VISUAL").unwrap());

                            let child = Command::new(editor)
                                .args(&[file_to_open])
                                .spawn()
                                .expect("failed to spawn");
                            handle_command_exit(child);
                            ::std::process::exit(0);
                        }
                        None => {
                            let child = Command::new(env::var("SHELL").unwrap())
                                .spawn()
                                .expect("failed to spawn");
                            handle_command_exit(child);
                            ::std::process::exit(0);
                        }
                    },
                };
                (pid_primary, pid_secondary.as_raw())
            }
            Err(e) => {
                panic!("failed to fork {:?}", e);
            }
        }
    };
    (pid_primary, pid_secondary)
}

#[derive(Clone)]
pub struct OsInputOutput {
    orig_termios: Arc<Mutex<Termios>>,
}

pub trait OsApi: Send + Sync {
    fn get_terminal_size_using_fd(&self, pid: RawFd) -> PositionAndSize;
    fn set_terminal_size_using_fd(&mut self, pid: RawFd, cols: u16, rows: u16);
    fn into_raw_mode(&mut self, pid: RawFd);
    fn unset_raw_mode(&mut self, pid: RawFd);
    fn spawn_terminal(&mut self, file_to_open: Option<PathBuf>) -> (RawFd, RawFd);
    fn read_from_tty_stdout(&mut self, pid: RawFd, buf: &mut [u8]) -> Result<usize, nix::Error>;
    fn write_to_tty_stdin(&mut self, pid: RawFd, buf: &mut [u8]) -> Result<usize, nix::Error>;
    fn tcdrain(&mut self, pid: RawFd) -> Result<(), nix::Error>;
    fn kill(&mut self, pid: RawFd) -> Result<(), nix::Error>;
    fn get_stdin_reader(&self) -> Box<dyn Read>;
    fn get_stdout_writer(&self) -> Box<dyn Write>;
    fn box_clone(&self) -> Box<dyn OsApi>;
}

impl OsApi for OsInputOutput {
    fn get_terminal_size_using_fd(&self, pid: RawFd) -> PositionAndSize {
        get_terminal_size_using_fd(pid)
    }
    fn set_terminal_size_using_fd(&mut self, pid: RawFd, cols: u16, rows: u16) {
        set_terminal_size_using_fd(pid, cols, rows);
    }
    fn into_raw_mode(&mut self, pid: RawFd) {
        into_raw_mode(pid);
    }
    fn unset_raw_mode(&mut self, pid: RawFd) {
        let orig_termios = self.orig_termios.lock().unwrap();
        unset_raw_mode(pid, orig_termios.clone());
    }
    fn spawn_terminal(&mut self, file_to_open: Option<PathBuf>) -> (RawFd, RawFd) {
        let orig_termios = self.orig_termios.lock().unwrap();
        spawn_terminal(file_to_open, orig_termios.clone())
    }
    fn read_from_tty_stdout(&mut self, pid: RawFd, buf: &mut [u8]) -> Result<usize, nix::Error> {
        read(pid, buf)
    }
    fn write_to_tty_stdin(&mut self, pid: RawFd, buf: &mut [u8]) -> Result<usize, nix::Error> {
        write(pid, buf)
    }
    fn tcdrain(&mut self, pid: RawFd) -> Result<(), nix::Error> {
        tcdrain(pid)
    }
    fn box_clone(&self) -> Box<dyn OsApi> {
        Box::new((*self).clone())
    }
    fn get_stdin_reader(&self) -> Box<dyn Read> {
        // TODO: stdin lock, right now it's not done because we don't have where to put it
        // if we put it on the struct, we won't be able to clone the struct
        // if we leave it here, we're referencing a temporary value
        let stdin = ::std::io::stdin();
        Box::new(stdin)
    }
    fn get_stdout_writer(&self) -> Box<dyn Write> {
        let stdout = ::std::io::stdout();
        Box::new(stdout)
    }
    fn kill(&mut self, fd: RawFd) -> Result<(), nix::Error> {
        kill(Pid::from_raw(fd), Some(Signal::SIGINT)).unwrap();
        waitpid(Pid::from_raw(fd), None).unwrap();
        Ok(())
    }
}

impl Clone for Box<dyn OsApi> {
    fn clone(&self) -> Box<dyn OsApi> {
        self.box_clone()
    }
}

pub fn get_os_input() -> OsInputOutput {
    let current_termios = tcgetattr(0).unwrap();
    let orig_termios = Arc::new(Mutex::new(current_termios));
    OsInputOutput { orig_termios }
}