1 /* input.c -- functions to perform buffered input with synchronization. */
3 /* Copyright (C) 1992 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 it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #include "bashtypes.h"
25 # include <sys/file.h>
28 #include "posixstat.h"
32 #if defined (HAVE_UNISTD_H)
47 /* Functions to handle reading input on systems that don't restart read(2)
48 if a signal is received. */
50 static char localbuf[128];
51 static int local_index, local_bufused;
53 /* Posix and USG systems do not guarantee to restart read () if it is
54 interrupted by a signal. We do the read ourselves, and restart it
55 if it returns EINTR. */
57 getc_with_restart (stream)
62 /* Try local buffering to reduce the number of read(2) calls. */
63 if (local_index == local_bufused || local_bufused == 0)
67 local_bufused = read (fileno (stream), localbuf, sizeof(localbuf));
68 if (local_bufused > 0)
70 else if (local_bufused == 0 || errno != EINTR)
78 uc = localbuf[local_index++];
83 ungetc_with_restart (c, stream)
87 if (local_index == 0 || c == EOF)
89 localbuf[--local_index] = c;
93 #if defined (BUFFERED_INPUT)
95 /* A facility similar to stdio, but input-only. */
97 #if defined (USING_BASH_MALLOC)
98 # define MAX_INPUT_BUFFER_SIZE 8176
100 # define MAX_INPUT_BUFFER_SIZE 8192
103 #if !defined (SEEK_CUR)
105 #endif /* !SEEK_CUR */
110 #define max(a, b) (((a) > (b)) ? (a) : (b))
114 #define min(a, b) ((a) > (b) ? (b) : (a))
116 extern int interactive_shell;
118 int bash_input_fd_changed;
120 /* This provides a way to map from a file descriptor to the buffer
121 associated with that file descriptor, rather than just the other
122 way around. This is needed so that buffers are managed properly
123 in constructs like 3<&4. buffers[x]->b_fd == x -- that is how the
124 correspondence is maintained. */
125 static BUFFERED_STREAM **buffers = (BUFFERED_STREAM **)NULL;
128 #define ALLOCATE_BUFFERS(n) \
129 do { if ((n) >= nbuffers) allocate_buffers (n); } while (0)
131 /* Make sure `buffers' has at least N elements. */
136 register int i, orig_nbuffers;
138 orig_nbuffers = nbuffers;
140 buffers = (BUFFERED_STREAM **)xrealloc
141 (buffers, nbuffers * sizeof (BUFFERED_STREAM *));
143 /* Zero out the new buffers. */
144 for (i = orig_nbuffers; i < nbuffers; i++)
145 buffers[i] = (BUFFERED_STREAM *)NULL;
148 /* Construct and return a BUFFERED_STREAM corresponding to file descriptor
150 static BUFFERED_STREAM *
151 make_buffered_stream (fd, buffer, bufsize)
158 bp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
159 ALLOCATE_BUFFERS (fd);
162 bp->b_buffer = buffer;
163 bp->b_size = bufsize;
164 bp->b_used = bp->b_inputp = bp->b_flag = 0;
166 bp->b_flag |= B_UNBUFF;
170 /* Allocate a new BUFFERED_STREAM, copy BP to it, and return the new copy. */
171 static BUFFERED_STREAM *
172 copy_buffered_stream (bp)
175 BUFFERED_STREAM *nbp;
178 return ((BUFFERED_STREAM *)NULL);
180 nbp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
181 xbcopy ((char *)bp, (char *)nbp, sizeof (BUFFERED_STREAM));
186 set_bash_input_fd (fd)
189 if (bash_input.type == st_bstream)
190 bash_input.location.buffered_fd = fd;
191 else if (interactive_shell == 0)
192 default_buffered_input = fd;
197 fd_is_bash_input (fd)
200 if (bash_input.type == st_bstream && bash_input.location.buffered_fd == fd)
202 else if (interactive_shell == 0 && default_buffered_input == fd)
207 /* Save the buffered stream corresponding to file descriptor FD (which bash
208 is using to read input) to a buffered stream associated with NEW_FD. If
209 NEW_FD is -1, a new file descriptor is allocated with fcntl. The new
210 file descriptor is returned on success, -1 on error. */
212 save_bash_input (fd, new_fd)
217 /* Sync the stream so we can re-read from the new file descriptor. We
218 might be able to avoid this by copying the buffered stream verbatim
219 to the new file descriptor. */
221 sync_buffered_stream (fd);
223 /* Now take care of duplicating the file descriptor that bash is
224 using for input, so we can reinitialize it later. */
225 nfd = (new_fd == -1) ? fcntl (fd, F_DUPFD, 10) : new_fd;
228 if (fcntl (fd, F_GETFD, 0) == 0)
229 sys_error ("cannot allocate new file descriptor for bash input from fd %d", fd);
235 /* What's this? A stray buffer without an associated open file
236 descriptor? Free up the buffer and report the error. */
237 internal_error ("check_bash_input: buffer already exists for new fd %d", nfd);
238 free_buffered_stream (buffers[nfd]);
241 /* Reinitialize bash_input.location. */
242 if (bash_input.type == st_bstream)
244 bash_input.location.buffered_fd = nfd;
245 fd_to_buffered_stream (nfd);
246 close_buffered_fd (fd); /* XXX */
249 /* If the current input type is not a buffered stream, but the shell
250 is not interactive and therefore using a buffered stream to read
251 input (e.g. with an `eval exec 3>output' inside a script), note
252 that the input fd has been changed. pop_stream() looks at this
253 value and adjusts the input fd to the new value of
254 default_buffered_input accordingly. */
255 bash_input_fd_changed++;
257 if (default_buffered_input == fd)
258 default_buffered_input = nfd;
260 SET_CLOSE_ON_EXEC (nfd);
264 /* Check that file descriptor FD is not the one that bash is currently
265 using to read input from a script. FD is about to be duplicated onto,
266 which means that the kernel will close it for us. If FD is the bash
267 input file descriptor, we need to seek backwards in the script (if
268 possible and necessary -- scripts read from stdin are still unbuffered),
269 allocate a new file descriptor to use for bash input, and re-initialize
270 the buffered stream. Make sure the file descriptor used to save bash
271 input is set close-on-exec. Returns 0 on success, -1 on failure. This
272 works only if fd is > 0 -- if fd == 0 and bash is reading input from
273 fd 0, save_bash_input is used instead, to cooperate with input
274 redirection (look at redir.c:add_undo_redirect()). */
276 check_bash_input (fd)
279 if (fd > 0 && fd_is_bash_input (fd))
280 return ((save_bash_input (fd, -1) == -1) ? -1 : 0);
284 /* This is the buffered stream analogue of dup2(fd1, fd2). The
285 BUFFERED_STREAM corresponding to fd2 is deallocated, if one exists.
286 BUFFERS[fd1] is copied to BUFFERS[fd2]. This is called by the
287 redirect code for constructs like 4<&0 and 3</etc/rc.local. */
289 duplicate_buffered_stream (fd1, fd2)
292 int is_bash_input, m;
298 ALLOCATE_BUFFERS (m);
300 /* If FD2 is the file descriptor bash is currently using for shell input,
301 we need to do some extra work to make sure that the buffered stream
302 actually exists (it might not if fd1 was not active, and the copy
303 didn't actually do anything). */
304 is_bash_input = (bash_input.type == st_bstream) &&
305 (bash_input.location.buffered_fd == fd2);
308 free_buffered_stream (buffers[fd2]);
309 buffers[fd2] = copy_buffered_stream (buffers[fd1]);
311 buffers[fd2]->b_fd = fd2;
316 fd_to_buffered_stream (fd2);
317 buffers[fd2]->b_flag |= B_WASBASHINPUT;
323 /* Return 1 if a seek on FD will succeed. */
325 # define fd_is_seekable(fd) (lseek ((fd), 0L, SEEK_CUR) >= 0)
327 # define fd_is_seekable(fd) 0
328 #endif /* __CYGWIN__ */
330 /* Take FD, a file descriptor, and create and return a buffered stream
331 corresponding to it. If something is wrong and the file descriptor
332 is invalid, return a NULL stream. */
334 fd_to_buffered_stream (fd)
341 if (fstat (fd, &sb) < 0)
344 return ((BUFFERED_STREAM *)NULL);
347 size = (fd_is_seekable (fd)) ? min (sb.st_size, MAX_INPUT_BUFFER_SIZE) : 1;
350 buffer = (char *)xmalloc (size);
352 return (make_buffered_stream (fd, buffer, size));
355 /* Return a buffered stream corresponding to FILE, a file name. */
357 open_buffered_stream (file)
362 fd = open (file, O_RDONLY);
363 return ((fd >= 0) ? fd_to_buffered_stream (fd) : (BUFFERED_STREAM *)NULL);
366 /* Deallocate a buffered stream and free up its resources. Make sure we
367 zero out the slot in BUFFERS that points to BP. */
369 free_buffered_stream (bp)
381 buffers[n] = (BUFFERED_STREAM *)NULL;
384 /* Close the file descriptor associated with BP, a buffered stream, and free
385 up the stream. Return the status of closing BP's file descriptor. */
387 close_buffered_stream (bp)
395 free_buffered_stream (bp);
399 /* Deallocate the buffered stream associated with file descriptor FD, and
400 close FD. Return the status of the close on FD. */
402 close_buffered_fd (fd)
410 if (fd >= nbuffers || !buffers || !buffers[fd])
412 return (close_buffered_stream (buffers[fd]));
415 /* Make the BUFFERED_STREAM associcated with buffers[FD] be BP, and return
416 the old BUFFERED_STREAM. */
418 set_buffered_stream (fd, bp)
422 BUFFERED_STREAM *ret;
429 /* Read a buffer full of characters from BP, a buffered stream. */
436 nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
444 bp->b_flag |= B_ERROR;
448 #if defined (__CYGWIN__)
449 /* If on cygwin, translate \r\n to \n. */
450 if (nr >= 2 && bp->b_buffer[nr - 2] == '\r' && bp->b_buffer[nr - 1] == '\n')
452 bp->b_buffer[nr - 2] = '\n';
459 return (bp->b_buffer[bp->b_inputp++] & 0xFF);
462 /* Get a character from buffered stream BP. */
463 #define bufstream_getc(bp) \
464 (bp->b_inputp == bp->b_used || !bp->b_used) \
465 ? b_fill_buffer (bp) \
466 : bp->b_buffer[bp->b_inputp++] & 0xFF
468 /* Push C back onto buffered stream BP. */
470 bufstream_ungetc(c, bp)
474 if (c == EOF || bp->b_inputp == 0)
477 bp->b_buffer[--bp->b_inputp] = c;
481 /* Seek backwards on file BFD to synchronize what we've read so far
482 with the underlying file pointer. */
484 sync_buffered_stream (bfd)
490 if (buffers == 0 || (bp = buffers[bfd]) == 0)
493 chars_left = bp->b_used - bp->b_inputp;
495 lseek (bp->b_fd, -chars_left, SEEK_CUR);
496 bp->b_used = bp->b_inputp = 0;
504 return (bufstream_getc (buffers[bash_input.location.buffered_fd]));
506 /* On DJGPP, ignore \r. */
508 while ((ch = bufstream_getc (buffers[bash_input.location.buffered_fd])) == '\r')
515 buffered_ungetchar (c)
518 return (bufstream_ungetc (c, buffers[bash_input.location.buffered_fd]));
521 /* Make input come from file descriptor BFD through a buffered stream. */
523 with_input_from_buffered_stream (bfd, name)
527 INPUT_STREAM location;
530 location.buffered_fd = bfd;
531 /* Make sure the buffered stream exists. */
532 bp = fd_to_buffered_stream (bfd);
533 init_yy_io (bp == 0 ? return_EOF : buffered_getchar,
534 buffered_ungetchar, st_bstream, name, location);
551 return(malloc (size));
553 return(realloc (s, size));
566 while ((c = bufstream_getc(bp)) != EOF)
570 BASH_INPUT bash_input;
572 struct stat dsb; /* can be used from gdb */
574 /* imitate /bin/cat */
583 bp = fd_to_buffered_stream (0);
587 for (i = 1; i < argc; i++) {
588 if (argv[i][0] == '-' && argv[i][1] == '\0') {
589 bp = fd_to_buffered_stream (0);
593 free_buffered_stream (bp);
595 bp = open_buffered_stream (argv[i]);
599 close_buffered_stream (bp);
605 #endif /* BUFFERED_INPUT */