diff --git a/src/machine/machine_state.rs b/src/machine/machine_state.rs index 148a9d8f..ca88a6ea 100644 --- a/src/machine/machine_state.rs +++ b/src/machine/machine_state.rs @@ -745,25 +745,50 @@ impl MachineState { stream: Stream, indices: &mut IndexStore, ) -> CallResult { - if let Stream::Readline(ptr) = stream { - let readline = unsafe { ptr.as_ptr().as_mut() }.unwrap(); - readline.set_atoms_for_completion(&self.atom_tbl); - return self.read_term( + match stream { + #[cfg(feature = "http")] + Stream::HttpRead(_) => self.read_term( stream, indices, MachineState::read_term_from_user_input_eof_handler, - ); - } - - if let Stream::Byte(_) = stream { - return self.read_term( + ), + #[cfg(feature = "tls")] + Stream::NamedTls(_) => self.read_term( stream, indices, MachineState::read_term_from_user_input_eof_handler, - ); - } + ), + Stream::Readline(ptr) => { + let readline = unsafe { ptr.as_ptr().as_mut() }.unwrap(); + readline.set_atoms_for_completion(&self.atom_tbl); + self.read_term( + stream, + indices, + MachineState::read_term_from_user_input_eof_handler, + ) + } + Stream::Byte(_) + | Stream::InputChannel(_) + | Stream::InputFile(_) + | Stream::NamedTcp(_) + | Stream::Null(_) + | Stream::PipeReader(_) + | Stream::StaticString(_) => self.read_term( + stream, + indices, + MachineState::read_term_from_user_input_eof_handler, + ), + _ => { + let stub = functor_stub(atom!("read_term_from_user_input"), 3); + let err = self.permission_error( + Permission::InputStream, + atom!("stream"), + atom_as_cell!(atom!("user_input")), + ); - unreachable!("Stream must be a Stream::Readline(_)") + Err(self.error_form(err, stub)) + } + } } pub fn read_term_eof_handler(&mut self, mut stream: Stream) -> Result { diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index a2a8576b..b0c0044d 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -6735,7 +6735,10 @@ impl Machine { #[inline(always)] pub(crate) fn read_query_term(&mut self) -> CallResult { - self.user_input.reset(); + match self.user_input { + Stream::Byte(_) | Stream::Readline(_) => self.user_input.reset(), + _ => true, + }; set_prompt(true); // let result = self.machine_st.read_term(self.user_input, &mut self.indices); @@ -6747,7 +6750,10 @@ impl Machine { match result { Ok(()) => Ok(()), Err(e) => { - self.user_input.reset(); + match self.user_input { + Stream::Byte(_) | Stream::Readline(_) => self.user_input.reset(), + _ => true, + }; Err(e) } } diff --git a/tests/scryer/cli/issues/set_input_from_input_file_stream.in/input b/tests/scryer/cli/issues/set_input_from_input_file_stream.in/input new file mode 100644 index 00000000..60c0d887 --- /dev/null +++ b/tests/scryer/cli/issues/set_input_from_input_file_stream.in/input @@ -0,0 +1 @@ +true. \ No newline at end of file diff --git a/tests/scryer/cli/issues/set_input_from_input_file_stream.in/set_input_from_top-level.pl b/tests/scryer/cli/issues/set_input_from_input_file_stream.in/set_input_from_top-level.pl new file mode 100644 index 00000000..b38f2223 --- /dev/null +++ b/tests/scryer/cli/issues/set_input_from_input_file_stream.in/set_input_from_top-level.pl @@ -0,0 +1,10 @@ +:- use_module(library(files)). + +main :- + current_input(UserStream), + open('./input', read, InputStream), + set_input(InputStream), + read_term(T, []), write(T), + set_input(UserStream). + +:- initialization(main). diff --git a/tests/scryer/cli/issues/set_input_from_input_file_stream.stderr b/tests/scryer/cli/issues/set_input_from_input_file_stream.stderr new file mode 100644 index 00000000..e69de29b diff --git a/tests/scryer/cli/issues/set_input_from_input_file_stream.stdin b/tests/scryer/cli/issues/set_input_from_input_file_stream.stdin new file mode 100644 index 00000000..e69de29b diff --git a/tests/scryer/cli/issues/set_input_from_input_file_stream.stdout b/tests/scryer/cli/issues/set_input_from_input_file_stream.stdout new file mode 100644 index 00000000..f32a5804 --- /dev/null +++ b/tests/scryer/cli/issues/set_input_from_input_file_stream.stdout @@ -0,0 +1 @@ +true \ No newline at end of file diff --git a/tests/scryer/cli/issues/set_input_from_input_file_stream.toml b/tests/scryer/cli/issues/set_input_from_input_file_stream.toml new file mode 100644 index 00000000..e488fd1f --- /dev/null +++ b/tests/scryer/cli/issues/set_input_from_input_file_stream.toml @@ -0,0 +1 @@ +args = ["-f", "--no-add-history", "-g", "consult('set_input_from_top-level.pl').", "-g", "halt."]