+int
+set_bash_input_fd (fd)
+ int fd;
+{
+ if (bash_input.type == st_bstream)
+ bash_input.location.buffered_fd = fd;
+ else if (interactive_shell == 0)
+ default_buffered_input = fd;
+ return 0;
+}
+
+int
+fd_is_bash_input (fd)
+ int fd;
+{
+ if (bash_input.type == st_bstream && bash_input.location.buffered_fd == fd)
+ return 1;
+ else if (interactive_shell == 0 && default_buffered_input == fd)
+ return 1;
+ return 0;
+}
+
+/* Save the buffered stream corresponding to file descriptor FD (which bash
+ is using to read input) to a buffered stream associated with NEW_FD. If
+ NEW_FD is -1, a new file descriptor is allocated with fcntl. The new
+ file descriptor is returned on success, -1 on error. */
+int
+save_bash_input (fd, new_fd)
+ int fd, new_fd;
+{
+ int nfd;
+
+ /* Sync the stream so we can re-read from the new file descriptor. We
+ might be able to avoid this by copying the buffered stream verbatim
+ to the new file descriptor. */
+ if (buffers[fd])
+ sync_buffered_stream (fd);
+
+ /* Now take care of duplicating the file descriptor that bash is
+ using for input, so we can reinitialize it later. */
+ nfd = (new_fd == -1) ? fcntl (fd, F_DUPFD, 10) : new_fd;
+ if (nfd == -1)
+ {
+ if (fcntl (fd, F_GETFD, 0) == 0)
+ sys_error (_("cannot allocate new file descriptor for bash input from fd %d"), fd);
+ return -1;
+ }
+
+ if (nfd < nbuffers && buffers[nfd])
+ {
+ /* What's this? A stray buffer without an associated open file
+ descriptor? Free up the buffer and report the error. */
+ internal_error (_("save_bash_input: buffer already exists for new fd %d"), nfd);
+ free_buffered_stream (buffers[nfd]);
+ }
+
+ /* Reinitialize bash_input.location. */
+ if (bash_input.type == st_bstream)
+ {
+ bash_input.location.buffered_fd = nfd;
+ fd_to_buffered_stream (nfd);
+ close_buffered_fd (fd); /* XXX */
+ }
+ else
+ /* If the current input type is not a buffered stream, but the shell
+ is not interactive and therefore using a buffered stream to read
+ input (e.g. with an `eval exec 3>output' inside a script), note
+ that the input fd has been changed. pop_stream() looks at this
+ value and adjusts the input fd to the new value of
+ default_buffered_input accordingly. */
+ bash_input_fd_changed++;
+
+ if (default_buffered_input == fd)
+ default_buffered_input = nfd;
+
+ SET_CLOSE_ON_EXEC (nfd);
+ return nfd;
+}
+