diff --git a/src/lib.rs b/src/lib.rs index 60e5589..91e569d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ pub struct LineDiscipline { termios: Termios, output_paused: bool, column: usize, + icanon_eof: bool, } impl LineDiscipline { @@ -18,6 +19,7 @@ impl LineDiscipline { termios: Termios::default(), output_paused: false, column: 0, + icanon_eof: false, } } @@ -60,6 +62,8 @@ impl LineDiscipline { || (self.termios.iflags.ixany && self.output_paused) { self.output_paused = false; + } else if ch as char == self.termios.veof && self.termios.lflags.icanon { + self.icanon_eof = true; } else { self.input_buffer.push(ch); if self.termios.lflags.echo || (ch == b'\n' && self.termios.lflags.echonl) { @@ -114,16 +118,19 @@ impl LineDiscipline { } } - pub fn get_program_input(&mut self) -> Vec { + pub fn get_program_input(&mut self) -> Option> { if self.termios.lflags.icanon { if let Some(last_full_line) = self.input_buffer.iter().positions(|&x| x == b'\n').last() { let rem_buf = self.input_buffer.split_off(last_full_line + 1); - std::mem::replace(&mut self.input_buffer, rem_buf) + Some(std::mem::replace(&mut self.input_buffer, rem_buf)) + } else if self.icanon_eof { + self.icanon_eof = false; + Some(std::mem::take(&mut self.input_buffer)) } else { - Vec::new() + None } } else { - std::mem::take(&mut self.input_buffer) + Some(std::mem::take(&mut self.input_buffer)) } }