1 /* input.c -- functions to perform buffered input with synchronization. */
3 /* Copyright (C) 1992-2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #include "bashtypes.h"
24 #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
25 # include <sys/file.h>
28 #include "posixstat.h"
32 #if defined (HAVE_UNISTD_H)
52 # define X_EAGAIN EAGAIN
57 #if defined (EWOULDBLOCK)
58 # define X_EWOULDBLOCK EWOULDBLOCK
60 # define X_EWOULDBLOCK -99
63 extern void termsig_handler __P((int));
65 /* Functions to handle reading input on systems that don't restart read(2)
66 if a signal is received. */
68 static char localbuf[128];
69 static int local_index = 0, local_bufused = 0;
71 /* Posix and USG systems do not guarantee to restart read () if it is
72 interrupted by a signal. We do the read ourselves, and restart it
73 if it returns EINTR. */
75 getc_with_restart (stream)
82 /* Try local buffering to reduce the number of read(2) calls. */
83 if (local_index == local_bufused || local_bufused == 0)
90 local_bufused = read (fileno (stream), localbuf, sizeof(localbuf));
91 if (local_bufused > 0)
93 else if (local_bufused == 0)
98 else if (errno == X_EAGAIN || errno == X_EWOULDBLOCK)
100 if (sh_unset_nodelay_mode (fileno (stream)) < 0)
102 sys_error (_("cannot reset nodelay mode for fd %d"), fileno (stream));
103 local_index = local_bufused = 0;
108 else if (errno != EINTR)
110 local_index = local_bufused = 0;
113 else if (interrupt_state || terminating_signal) /* QUIT; */
114 local_index = local_bufused = 0;
118 uc = localbuf[local_index++];
123 ungetc_with_restart (c, stream)
127 if (local_index == 0 || c == EOF)
129 localbuf[--local_index] = c;
133 #if defined (BUFFERED_INPUT)
135 /* A facility similar to stdio, but input-only. */
137 #if defined (USING_BASH_MALLOC)
138 # define MAX_INPUT_BUFFER_SIZE 8176
140 # define MAX_INPUT_BUFFER_SIZE 8192
143 #if !defined (SEEK_CUR)
145 #endif /* !SEEK_CUR */
150 #define max(a, b) (((a) > (b)) ? (a) : (b))
154 #define min(a, b) ((a) > (b) ? (b) : (a))
156 extern int interactive_shell;
158 int bash_input_fd_changed;
160 /* This provides a way to map from a file descriptor to the buffer
161 associated with that file descriptor, rather than just the other
162 way around. This is needed so that buffers are managed properly
163 in constructs like 3<&4. buffers[x]->b_fd == x -- that is how the
164 correspondence is maintained. */
165 static BUFFERED_STREAM **buffers = (BUFFERED_STREAM **)NULL;
168 #define ALLOCATE_BUFFERS(n) \
169 do { if ((n) >= nbuffers) allocate_buffers (n); } while (0)
171 /* Make sure `buffers' has at least N elements. */
176 register int i, orig_nbuffers;
178 orig_nbuffers = nbuffers;
180 buffers = (BUFFERED_STREAM **)xrealloc
181 (buffers, nbuffers * sizeof (BUFFERED_STREAM *));
183 /* Zero out the new buffers. */
184 for (i = orig_nbuffers; i < nbuffers; i++)
185 buffers[i] = (BUFFERED_STREAM *)NULL;
188 /* Construct and return a BUFFERED_STREAM corresponding to file descriptor
190 static BUFFERED_STREAM *
191 make_buffered_stream (fd, buffer, bufsize)
198 bp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
199 ALLOCATE_BUFFERS (fd);
202 bp->b_buffer = buffer;
203 bp->b_size = bufsize;
204 bp->b_used = bp->b_inputp = bp->b_flag = 0;
206 bp->b_flag |= B_UNBUFF;
207 if (O_TEXT && (fcntl (fd, F_GETFL) & O_TEXT) != 0)
208 bp->b_flag |= O_TEXT;
212 /* Allocate a new BUFFERED_STREAM, copy BP to it, and return the new copy. */
213 static BUFFERED_STREAM *
214 copy_buffered_stream (bp)
217 BUFFERED_STREAM *nbp;
220 return ((BUFFERED_STREAM *)NULL);
222 nbp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
223 xbcopy ((char *)bp, (char *)nbp, sizeof (BUFFERED_STREAM));
228 set_bash_input_fd (fd)
231 if (bash_input.type == st_bstream)
232 bash_input.location.buffered_fd = fd;
233 else if (interactive_shell == 0)
234 default_buffered_input = fd;
239 fd_is_bash_input (fd)
242 if (bash_input.type == st_bstream && bash_input.location.buffered_fd == fd)
244 else if (interactive_shell == 0 && default_buffered_input == fd)
249 /* Save the buffered stream corresponding to file descriptor FD (which bash
250 is using to read input) to a buffered stream associated with NEW_FD. If
251 NEW_FD is -1, a new file descriptor is allocated with fcntl. The new
252 file descriptor is returned on success, -1 on error. */
254 save_bash_input (fd, new_fd)
259 /* Sync the stream so we can re-read from the new file descriptor. We
260 might be able to avoid this by copying the buffered stream verbatim
261 to the new file descriptor. */
263 sync_buffered_stream (fd);
265 /* Now take care of duplicating the file descriptor that bash is
266 using for input, so we can reinitialize it later. */
267 nfd = (new_fd == -1) ? fcntl (fd, F_DUPFD, 10) : new_fd;
270 if (fcntl (fd, F_GETFD, 0) == 0)
271 sys_error (_("cannot allocate new file descriptor for bash input from fd %d"), fd);
277 /* What's this? A stray buffer without an associated open file
278 descriptor? Free up the buffer and report the error. */
279 internal_error (_("save_bash_input: buffer already exists for new fd %d"), nfd);
280 free_buffered_stream (buffers[nfd]);
283 /* Reinitialize bash_input.location. */
284 if (bash_input.type == st_bstream)
286 bash_input.location.buffered_fd = nfd;
287 fd_to_buffered_stream (nfd);
288 close_buffered_fd (fd); /* XXX */
291 /* If the current input type is not a buffered stream, but the shell
292 is not interactive and therefore using a buffered stream to read
293 input (e.g. with an `eval exec 3>output' inside a script), note
294 that the input fd has been changed. pop_stream() looks at this
295 value and adjusts the input fd to the new value of
296 default_buffered_input accordingly. */
297 bash_input_fd_changed++;
299 if (default_buffered_input == fd)
300 default_buffered_input = nfd;
302 SET_CLOSE_ON_EXEC (nfd);
306 /* Check that file descriptor FD is not the one that bash is currently
307 using to read input from a script. FD is about to be duplicated onto,
308 which means that the kernel will close it for us. If FD is the bash
309 input file descriptor, we need to seek backwards in the script (if
310 possible and necessary -- scripts read from stdin are still unbuffered),
311 allocate a new file descriptor to use for bash input, and re-initialize
312 the buffered stream. Make sure the file descriptor used to save bash
313 input is set close-on-exec. Returns 0 on success, -1 on failure. This
314 works only if fd is > 0 -- if fd == 0 and bash is reading input from
315 fd 0, sync_buffered_stream is used instead, to cooperate with input
316 redirection (look at redir.c:add_undo_redirect()). */
318 check_bash_input (fd)
321 if (fd_is_bash_input (fd))
324 return ((save_bash_input (fd, -1) == -1) ? -1 : 0);
326 return ((sync_buffered_stream (fd) == -1) ? -1 : 0);
331 /* This is the buffered stream analogue of dup2(fd1, fd2). The
332 BUFFERED_STREAM corresponding to fd2 is deallocated, if one exists.
333 BUFFERS[fd1] is copied to BUFFERS[fd2]. This is called by the
334 redirect code for constructs like 4<&0 and 3</etc/rc.local. */
336 duplicate_buffered_stream (fd1, fd2)
339 int is_bash_input, m;
345 ALLOCATE_BUFFERS (m);
347 /* If FD2 is the file descriptor bash is currently using for shell input,
348 we need to do some extra work to make sure that the buffered stream
349 actually exists (it might not if fd1 was not active, and the copy
350 didn't actually do anything). */
351 is_bash_input = (bash_input.type == st_bstream) &&
352 (bash_input.location.buffered_fd == fd2);
356 /* If the two objects share the same b_buffer, don't free it. */
357 if (buffers[fd1] && buffers[fd1]->b_buffer && buffers[fd1]->b_buffer == buffers[fd2]->b_buffer)
358 buffers[fd2] = (BUFFERED_STREAM *)NULL;
360 free_buffered_stream (buffers[fd2]);
362 buffers[fd2] = copy_buffered_stream (buffers[fd1]);
364 buffers[fd2]->b_fd = fd2;
369 fd_to_buffered_stream (fd2);
370 buffers[fd2]->b_flag |= B_WASBASHINPUT;
376 /* Return 1 if a seek on FD will succeed. */
377 #define fd_is_seekable(fd) (lseek ((fd), 0L, SEEK_CUR) >= 0)
379 /* Take FD, a file descriptor, and create and return a buffered stream
380 corresponding to it. If something is wrong and the file descriptor
381 is invalid, return a NULL stream. */
383 fd_to_buffered_stream (fd)
390 if (fstat (fd, &sb) < 0)
393 return ((BUFFERED_STREAM *)NULL);
396 size = (fd_is_seekable (fd)) ? min (sb.st_size, MAX_INPUT_BUFFER_SIZE) : 1;
399 buffer = (char *)xmalloc (size);
401 return (make_buffered_stream (fd, buffer, size));
404 /* Return a buffered stream corresponding to FILE, a file name. */
406 open_buffered_stream (file)
411 fd = open (file, O_RDONLY);
412 return ((fd >= 0) ? fd_to_buffered_stream (fd) : (BUFFERED_STREAM *)NULL);
415 /* Deallocate a buffered stream and free up its resources. Make sure we
416 zero out the slot in BUFFERS that points to BP. */
418 free_buffered_stream (bp)
430 buffers[n] = (BUFFERED_STREAM *)NULL;
433 /* Close the file descriptor associated with BP, a buffered stream, and free
434 up the stream. Return the status of closing BP's file descriptor. */
436 close_buffered_stream (bp)
444 free_buffered_stream (bp);
448 /* Deallocate the buffered stream associated with file descriptor FD, and
449 close FD. Return the status of the close on FD. */
451 close_buffered_fd (fd)
459 if (fd >= nbuffers || !buffers || !buffers[fd])
461 return (close_buffered_stream (buffers[fd]));
464 /* Make the BUFFERED_STREAM associated with buffers[FD] be BP, and return
465 the old BUFFERED_STREAM. */
467 set_buffered_stream (fd, bp)
471 BUFFERED_STREAM *ret;
478 /* Read a buffer full of characters from BP, a buffered stream. */
487 /* In an environment where text and binary files are treated differently,
488 compensate for lseek() on text files returning an offset different from
489 the count of characters read() returns. Text-mode streams have to be
490 treated as unbuffered. */
491 if ((bp->b_flag & (B_TEXT | B_UNBUFF)) == B_TEXT)
493 o = lseek (bp->b_fd, 0, SEEK_CUR);
494 nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
495 if (nr > 0 && nr < lseek (bp->b_fd, 0, SEEK_CUR) - o)
497 lseek (bp->b_fd, o, SEEK_SET);
498 bp->b_flag |= B_UNBUFF;
500 nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
504 nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
512 bp->b_flag |= B_ERROR;
518 return (bp->b_buffer[bp->b_inputp++] & 0xFF);
521 /* Get a character from buffered stream BP. */
522 #define bufstream_getc(bp) \
523 (bp->b_inputp == bp->b_used || !bp->b_used) \
524 ? b_fill_buffer (bp) \
525 : bp->b_buffer[bp->b_inputp++] & 0xFF
527 /* Push C back onto buffered stream BP. */
529 bufstream_ungetc(c, bp)
533 if (c == EOF || bp->b_inputp == 0)
536 bp->b_buffer[--bp->b_inputp] = c;
540 /* Seek backwards on file BFD to synchronize what we've read so far
541 with the underlying file pointer. */
543 sync_buffered_stream (bfd)
549 if (buffers == 0 || (bp = buffers[bfd]) == 0)
552 chars_left = bp->b_used - bp->b_inputp;
554 lseek (bp->b_fd, -chars_left, SEEK_CUR);
555 bp->b_used = bp->b_inputp = 0;
565 return (bufstream_getc (buffers[bash_input.location.buffered_fd]));
567 /* On DJGPP, ignore \r. */
569 while ((ch = bufstream_getc (buffers[bash_input.location.buffered_fd])) == '\r')
576 buffered_ungetchar (c)
579 return (bufstream_ungetc (c, buffers[bash_input.location.buffered_fd]));
582 /* Make input come from file descriptor BFD through a buffered stream. */
584 with_input_from_buffered_stream (bfd, name)
588 INPUT_STREAM location;
591 location.buffered_fd = bfd;
592 /* Make sure the buffered stream exists. */
593 bp = fd_to_buffered_stream (bfd);
594 init_yy_io (bp == 0 ? return_EOF : buffered_getchar,
595 buffered_ungetchar, st_bstream, name, location);
612 return(malloc (size));
614 return(realloc (s, size));
627 while ((c = bufstream_getc(bp)) != EOF)
631 BASH_INPUT bash_input;
633 struct stat dsb; /* can be used from gdb */
635 /* imitate /bin/cat */
644 bp = fd_to_buffered_stream (0);
648 for (i = 1; i < argc; i++) {
649 if (argv[i][0] == '-' && argv[i][1] == '\0') {
650 bp = fd_to_buffered_stream (0);
654 free_buffered_stream (bp);
656 bp = open_buffered_stream (argv[i]);
660 close_buffered_stream (bp);
666 #endif /* BUFFERED_INPUT */