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"
24 #if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
25 # include <sys/file.h>
28 #include "posixstat.h"
32 #if defined (HAVE_UNISTD_H)
49 /* Functions to handle reading input on systems that don't restart read(2)
50 if a signal is received. */
52 static char localbuf[128];
53 static int local_index = 0, local_bufused = 0;
55 /* Posix and USG systems do not guarantee to restart read () if it is
56 interrupted by a signal. We do the read ourselves, and restart it
57 if it returns EINTR. */
59 getc_with_restart (stream)
64 /* Try local buffering to reduce the number of read(2) calls. */
65 if (local_index == local_bufused || local_bufused == 0)
69 local_bufused = read (fileno (stream), localbuf, sizeof(localbuf));
70 if (local_bufused > 0)
72 else if (local_bufused == 0 || errno != EINTR)
80 uc = localbuf[local_index++];
85 ungetc_with_restart (c, stream)
89 if (local_index == 0 || c == EOF)
91 localbuf[--local_index] = c;
95 #if defined (BUFFERED_INPUT)
97 /* A facility similar to stdio, but input-only. */
99 #if defined (USING_BASH_MALLOC)
100 # define MAX_INPUT_BUFFER_SIZE 8176
102 # define MAX_INPUT_BUFFER_SIZE 8192
105 #if !defined (SEEK_CUR)
107 #endif /* !SEEK_CUR */
112 #define max(a, b) (((a) > (b)) ? (a) : (b))
116 #define min(a, b) ((a) > (b) ? (b) : (a))
118 extern int interactive_shell;
120 int bash_input_fd_changed;
122 /* This provides a way to map from a file descriptor to the buffer
123 associated with that file descriptor, rather than just the other
124 way around. This is needed so that buffers are managed properly
125 in constructs like 3<&4. buffers[x]->b_fd == x -- that is how the
126 correspondence is maintained. */
127 static BUFFERED_STREAM **buffers = (BUFFERED_STREAM **)NULL;
130 #define ALLOCATE_BUFFERS(n) \
131 do { if ((n) >= nbuffers) allocate_buffers (n); } while (0)
133 /* Make sure `buffers' has at least N elements. */
138 register int i, orig_nbuffers;
140 orig_nbuffers = nbuffers;
142 buffers = (BUFFERED_STREAM **)xrealloc
143 (buffers, nbuffers * sizeof (BUFFERED_STREAM *));
145 /* Zero out the new buffers. */
146 for (i = orig_nbuffers; i < nbuffers; i++)
147 buffers[i] = (BUFFERED_STREAM *)NULL;
150 /* Construct and return a BUFFERED_STREAM corresponding to file descriptor
152 static BUFFERED_STREAM *
153 make_buffered_stream (fd, buffer, bufsize)
160 bp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
161 ALLOCATE_BUFFERS (fd);
164 bp->b_buffer = buffer;
165 bp->b_size = bufsize;
166 bp->b_used = bp->b_inputp = bp->b_flag = 0;
168 bp->b_flag |= B_UNBUFF;
172 /* Allocate a new BUFFERED_STREAM, copy BP to it, and return the new copy. */
173 static BUFFERED_STREAM *
174 copy_buffered_stream (bp)
177 BUFFERED_STREAM *nbp;
180 return ((BUFFERED_STREAM *)NULL);
182 nbp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
183 xbcopy ((char *)bp, (char *)nbp, sizeof (BUFFERED_STREAM));
188 set_bash_input_fd (fd)
191 if (bash_input.type == st_bstream)
192 bash_input.location.buffered_fd = fd;
193 else if (interactive_shell == 0)
194 default_buffered_input = fd;
199 fd_is_bash_input (fd)
202 if (bash_input.type == st_bstream && bash_input.location.buffered_fd == fd)
204 else if (interactive_shell == 0 && default_buffered_input == fd)
209 /* Save the buffered stream corresponding to file descriptor FD (which bash
210 is using to read input) to a buffered stream associated with NEW_FD. If
211 NEW_FD is -1, a new file descriptor is allocated with fcntl. The new
212 file descriptor is returned on success, -1 on error. */
214 save_bash_input (fd, new_fd)
219 /* Sync the stream so we can re-read from the new file descriptor. We
220 might be able to avoid this by copying the buffered stream verbatim
221 to the new file descriptor. */
223 sync_buffered_stream (fd);
225 /* Now take care of duplicating the file descriptor that bash is
226 using for input, so we can reinitialize it later. */
227 nfd = (new_fd == -1) ? fcntl (fd, F_DUPFD, 10) : new_fd;
230 if (fcntl (fd, F_GETFD, 0) == 0)
231 sys_error (_("cannot allocate new file descriptor for bash input from fd %d"), fd);
237 /* What's this? A stray buffer without an associated open file
238 descriptor? Free up the buffer and report the error. */
239 internal_error (_("save_bash_input: buffer already exists for new fd %d"), nfd);
240 free_buffered_stream (buffers[nfd]);
243 /* Reinitialize bash_input.location. */
244 if (bash_input.type == st_bstream)
246 bash_input.location.buffered_fd = nfd;
247 fd_to_buffered_stream (nfd);
248 close_buffered_fd (fd); /* XXX */
251 /* If the current input type is not a buffered stream, but the shell
252 is not interactive and therefore using a buffered stream to read
253 input (e.g. with an `eval exec 3>output' inside a script), note
254 that the input fd has been changed. pop_stream() looks at this
255 value and adjusts the input fd to the new value of
256 default_buffered_input accordingly. */
257 bash_input_fd_changed++;
259 if (default_buffered_input == fd)
260 default_buffered_input = nfd;
262 SET_CLOSE_ON_EXEC (nfd);
266 /* Check that file descriptor FD is not the one that bash is currently
267 using to read input from a script. FD is about to be duplicated onto,
268 which means that the kernel will close it for us. If FD is the bash
269 input file descriptor, we need to seek backwards in the script (if
270 possible and necessary -- scripts read from stdin are still unbuffered),
271 allocate a new file descriptor to use for bash input, and re-initialize
272 the buffered stream. Make sure the file descriptor used to save bash
273 input is set close-on-exec. Returns 0 on success, -1 on failure. This
274 works only if fd is > 0 -- if fd == 0 and bash is reading input from
275 fd 0, save_bash_input is used instead, to cooperate with input
276 redirection (look at redir.c:add_undo_redirect()). */
278 check_bash_input (fd)
281 if (fd_is_bash_input (fd))
284 return ((save_bash_input (fd, -1) == -1) ? -1 : 0);
286 return ((sync_buffered_stream (fd) == -1) ? -1 : 0);
291 /* This is the buffered stream analogue of dup2(fd1, fd2). The
292 BUFFERED_STREAM corresponding to fd2 is deallocated, if one exists.
293 BUFFERS[fd1] is copied to BUFFERS[fd2]. This is called by the
294 redirect code for constructs like 4<&0 and 3</etc/rc.local. */
296 duplicate_buffered_stream (fd1, fd2)
299 int is_bash_input, m;
305 ALLOCATE_BUFFERS (m);
307 /* If FD2 is the file descriptor bash is currently using for shell input,
308 we need to do some extra work to make sure that the buffered stream
309 actually exists (it might not if fd1 was not active, and the copy
310 didn't actually do anything). */
311 is_bash_input = (bash_input.type == st_bstream) &&
312 (bash_input.location.buffered_fd == fd2);
316 /* If the two objects share the same b_buffer, don't free it. */
317 if (buffers[fd1] && buffers[fd1]->b_buffer && buffers[fd1]->b_buffer == buffers[fd2]->b_buffer)
318 buffers[fd2] = (BUFFERED_STREAM *)NULL;
320 free_buffered_stream (buffers[fd2]);
322 buffers[fd2] = copy_buffered_stream (buffers[fd1]);
324 buffers[fd2]->b_fd = fd2;
329 fd_to_buffered_stream (fd2);
330 buffers[fd2]->b_flag |= B_WASBASHINPUT;
336 /* Return 1 if a seek on FD will succeed. */
338 # define fd_is_seekable(fd) (lseek ((fd), 0L, SEEK_CUR) >= 0)
340 # define fd_is_seekable(fd) 0
341 #endif /* __CYGWIN__ */
343 /* Take FD, a file descriptor, and create and return a buffered stream
344 corresponding to it. If something is wrong and the file descriptor
345 is invalid, return a NULL stream. */
347 fd_to_buffered_stream (fd)
354 if (fstat (fd, &sb) < 0)
357 return ((BUFFERED_STREAM *)NULL);
360 size = (fd_is_seekable (fd)) ? min (sb.st_size, MAX_INPUT_BUFFER_SIZE) : 1;
363 buffer = (char *)xmalloc (size);
365 return (make_buffered_stream (fd, buffer, size));
368 /* Return a buffered stream corresponding to FILE, a file name. */
370 open_buffered_stream (file)
375 fd = open (file, O_RDONLY);
376 return ((fd >= 0) ? fd_to_buffered_stream (fd) : (BUFFERED_STREAM *)NULL);
379 /* Deallocate a buffered stream and free up its resources. Make sure we
380 zero out the slot in BUFFERS that points to BP. */
382 free_buffered_stream (bp)
394 buffers[n] = (BUFFERED_STREAM *)NULL;
397 /* Close the file descriptor associated with BP, a buffered stream, and free
398 up the stream. Return the status of closing BP's file descriptor. */
400 close_buffered_stream (bp)
408 free_buffered_stream (bp);
412 /* Deallocate the buffered stream associated with file descriptor FD, and
413 close FD. Return the status of the close on FD. */
415 close_buffered_fd (fd)
423 if (fd >= nbuffers || !buffers || !buffers[fd])
425 return (close_buffered_stream (buffers[fd]));
428 /* Make the BUFFERED_STREAM associcated with buffers[FD] be BP, and return
429 the old BUFFERED_STREAM. */
431 set_buffered_stream (fd, bp)
435 BUFFERED_STREAM *ret;
442 /* Read a buffer full of characters from BP, a buffered stream. */
449 nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
457 bp->b_flag |= B_ERROR;
461 #if defined (__CYGWIN__)
462 /* If on cygwin, translate \r\n to \n. */
463 if (nr >= 2 && bp->b_buffer[nr - 2] == '\r' && bp->b_buffer[nr - 1] == '\n')
465 bp->b_buffer[nr - 2] = '\n';
472 return (bp->b_buffer[bp->b_inputp++] & 0xFF);
475 /* Get a character from buffered stream BP. */
476 #define bufstream_getc(bp) \
477 (bp->b_inputp == bp->b_used || !bp->b_used) \
478 ? b_fill_buffer (bp) \
479 : bp->b_buffer[bp->b_inputp++] & 0xFF
481 /* Push C back onto buffered stream BP. */
483 bufstream_ungetc(c, bp)
487 if (c == EOF || bp->b_inputp == 0)
490 bp->b_buffer[--bp->b_inputp] = c;
494 /* Seek backwards on file BFD to synchronize what we've read so far
495 with the underlying file pointer. */
497 sync_buffered_stream (bfd)
503 if (buffers == 0 || (bp = buffers[bfd]) == 0)
506 chars_left = bp->b_used - bp->b_inputp;
508 lseek (bp->b_fd, -chars_left, SEEK_CUR);
509 bp->b_used = bp->b_inputp = 0;
517 return (bufstream_getc (buffers[bash_input.location.buffered_fd]));
519 /* On DJGPP, ignore \r. */
521 while ((ch = bufstream_getc (buffers[bash_input.location.buffered_fd])) == '\r')
528 buffered_ungetchar (c)
531 return (bufstream_ungetc (c, buffers[bash_input.location.buffered_fd]));
534 /* Make input come from file descriptor BFD through a buffered stream. */
536 with_input_from_buffered_stream (bfd, name)
540 INPUT_STREAM location;
543 location.buffered_fd = bfd;
544 /* Make sure the buffered stream exists. */
545 bp = fd_to_buffered_stream (bfd);
546 init_yy_io (bp == 0 ? return_EOF : buffered_getchar,
547 buffered_ungetchar, st_bstream, name, location);
564 return(malloc (size));
566 return(realloc (s, size));
579 while ((c = bufstream_getc(bp)) != EOF)
583 BASH_INPUT bash_input;
585 struct stat dsb; /* can be used from gdb */
587 /* imitate /bin/cat */
596 bp = fd_to_buffered_stream (0);
600 for (i = 1; i < argc; i++) {
601 if (argv[i][0] == '-' && argv[i][1] == '\0') {
602 bp = fd_to_buffered_stream (0);
606 free_buffered_stream (bp);
608 bp = open_buffered_stream (argv[i]);
612 close_buffered_stream (bp);
618 #endif /* BUFFERED_INPUT */