      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

    use std::fs::{File, OpenOptions};
    use std::io::{self, Read, Write};
    use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
    use std::os::unix::process::CommandExt;
    use std::path::PathBuf;
    use std::process::{self, Stdio};
    use std::thread::{self, JoinHandle};
    use sys;

    pub type ChannelId = u32;

    #[derive(Debug)]
    pub struct Channel {
        id: ChannelId,
        peer_id: ChannelId,
        process: Option<process::Child>,
        pty: Option<(RawFd, PathBuf)>,
        master: Option<File>,
        window_size: u32,
        peer_window_size: u32,
        max_packet_size: u32,
        read_thread: Option<JoinHandle<()>>,
    }

    #[derive(Debug)]
    pub enum ChannelRequest {
        Pty {
            term: String,
            chars: u16,
            rows: u16,
            pixel_width: u16,
            pixel_height: u16,
            modes: Vec<u8>,
        },
        Shell,
    }

    impl Channel {
        pub fn new(
            id: ChannelId, peer_id: ChannelId, peer_window_size: u32,
            max_packet_size: u32
        ) -> Channel {
            Channel {
                id: id,
                peer_id: peer_id,
                process: None,
                master: None,
                pty: None,
                window_size: peer_window_size,
                peer_window_size: peer_window_size,
                max_packet_size: max_packet_size,
                read_thread: None,
            }
        }

        pub fn id(&self) -> ChannelId {
            self.id
        }

        pub fn window_size(&self) -> u32 {
            self.window_size
        }

        pub fn max_packet_size(&self) -> u32 {
            self.max_packet_size
        }

        pub fn request(&mut self, request: ChannelRequest) {
            match request
            {
                ChannelRequest::Pty {
                    chars,
                    rows,
                    pixel_width,
                    pixel_height,
                    ..
                } => {
                    let (master_fd, tty_path) = sys::getpty();

                    sys::set_winsize(
                        master_fd,
                        chars,
                        rows,
                        pixel_width,
                        pixel_height,
                    );

                    self.read_thread = Some(thread::spawn(move || {
                        use libc::dup;
                        let master2 = unsafe { dup(master_fd) };

                        println!("dup result: {}", dup as u32);
                        let mut master = unsafe { File::from_raw_fd(master2) };
                        loop {
                            use std::str::from_utf8_unchecked;
                            let mut buf = [0; 4096];
                            let count = master.read(&mut buf).unwrap();
                            if count == 0 {
                                break;
                            }
                            println!("Read: {}", unsafe {
                                from_utf8_unchecked(&buf[0..count])
                            });
                        }

                        println!("Quitting read thread.");
                    }));

                    self.pty = Some((master_fd, tty_path));
                    self.master = Some(unsafe { File::from_raw_fd(master_fd) });
                }
                ChannelRequest::Shell => {
                    if let Some(&(_, ref tty_path)) = self.pty.as_ref() {
                        let stdin = OpenOptions::new()
                            .read(true)
                            .write(true)
                            .open(&tty_path)
                            .unwrap()
                            .into_raw_fd();

                        let stdout = OpenOptions::new()
                            .read(true)
                            .write(true)
                            .open(&tty_path)
                            .unwrap()
                            .into_raw_fd();

                        let stderr = OpenOptions::new()
                            .read(true)
                            .write(true)
                            .open(&tty_path)
                            .unwrap()
                            .into_raw_fd();

                        process::Command::new("login")
                            .stdin(unsafe { Stdio::from_raw_fd(stdin) })
                            .stdout(unsafe { Stdio::from_raw_fd(stdout) })
                            .stderr(unsafe { Stdio::from_raw_fd(stderr) })
                            .before_exec(|| sys::before_exec())
                            .spawn()
                            .unwrap();
                    }
                }
            }
            debug!("Channel Request: {:?}", request);
        }

        pub fn data(&mut self, data: &[u8]) -> io::Result<()> {
            if let Some(ref mut master) = self.master {
                master.write_all(data)?;
                master.flush()
            }
            else {
                Ok(())
            }
        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)