Differentiate between no program input data and a 0-byte read

This commit is contained in:
pjht 2024-11-20 13:58:38 -06:00
parent ee1b595b81
commit e432c77cb8
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A

View File

@ -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<u8> {
pub fn get_program_input(&mut self) -> Option<Vec<u8>> {
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))
}
}