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, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 #include "bashtypes.h"
26 #include "posixstat.h"
30 #if defined (HAVE_UNISTD_H)
45 /* Functions to handle reading input on systems that don't restart read(2)
46 if a signal is received. */
48 #if !defined (HAVE_RESTARTABLE_SYSCALLS)
49 static unsigned char localbuf[128];
50 static int local_index, local_bufused;
52 /* Posix and USG systems do not guarantee to restart read () if it is
53 interrupted by a signal. We do the read ourselves, and restart it
54 if it returns EINTR. */
56 getc_with_restart (stream)
59 /* Try local buffering to reduce the number of read(2) calls. */
60 if (local_index == local_bufused || local_bufused == 0)
64 local_bufused = read (fileno (stream), localbuf, sizeof(localbuf));
65 if (local_bufused > 0)
67 else if (local_bufused == 0 || errno != EINTR)
75 return (localbuf[local_index++]);
79 ungetc_with_restart (c, stream)
83 if (local_index == 0 || c == EOF)
85 return (localbuf[--local_index] = c);
87 #endif /* !HAVE_RESTARTABLE_SYSCALLS */
89 #if defined (BUFFERED_INPUT)
91 /* A facility similar to stdio, but input-only. */
93 #define MAX_INPUT_BUFFER_SIZE 8192
95 #if !defined (SEEK_CUR)
97 #endif /* !SEEK_CUR */
99 void free_buffered_stream ();
101 extern int return_EOF ();
103 extern int interactive_shell;
105 int bash_input_fd_changed;
107 /* This provides a way to map from a file descriptor to the buffer
108 associated with that file descriptor, rather than just the other
109 way around. This is needed so that buffers are managed properly
110 in constructs like 3<&4. buffers[x]->b_fd == x -- that is how the
111 correspondence is maintained. */
112 BUFFERED_STREAM **buffers = (BUFFERED_STREAM **)NULL;
115 #define max(a, b) (((a) > (b)) ? (a) : (b))
117 #define ALLOCATE_BUFFERS(n) \
118 do { if ((n) >= nbuffers) allocate_buffers (n); } while (0)
120 /* Make sure `buffers' has at least N elements. */
125 register int i, orig_nbuffers;
127 orig_nbuffers = nbuffers;
129 buffers = (BUFFERED_STREAM **)xrealloc
130 (buffers, nbuffers * sizeof (BUFFERED_STREAM *));
132 /* Zero out the new buffers. */
133 for (i = orig_nbuffers; i < nbuffers; i++)
134 buffers[i] = (BUFFERED_STREAM *)NULL;
137 /* Construct and return a BUFFERED_STREAM corresponding to file descriptor
139 static BUFFERED_STREAM *
140 make_buffered_stream (fd, buffer, bufsize)
147 bp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
148 ALLOCATE_BUFFERS (fd);
151 bp->b_buffer = buffer;
152 bp->b_size = bufsize;
153 bp->b_used = bp->b_inputp = bp->b_flag = 0;
155 bp->b_flag |= B_UNBUFF;
159 /* Allocate a new BUFFERED_STREAM, copy BP to it, and return the new copy. */
160 static BUFFERED_STREAM *
161 copy_buffered_stream (bp)
164 BUFFERED_STREAM *nbp;
167 return ((BUFFERED_STREAM *)NULL);
169 nbp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
170 xbcopy ((char *)bp, (char *)nbp, sizeof (BUFFERED_STREAM));
174 /* Check that file descriptor FD is not the one that bash is currently
175 using to read input from a script. FD is about to be duplicated onto,
176 which means that the kernel will close it for us. If FD is the bash
177 input file descriptor, we need to seek backwards in the script (if
178 possible and necessary -- scripts read from stdin are still unbuffered),
179 allocate a new file descriptor to use for bash input, and re-initialize
180 the buffered stream. */
182 check_bash_input (fd)
187 if (fd > 0 && ((bash_input.type == st_bstream && bash_input.location.buffered_fd == fd) ||
188 (interactive_shell == 0 && default_buffered_input == fd)))
190 /* Sync the stream so we can re-read from the new file descriptor. We
191 might be able to avoid this by copying the buffered stream verbatim
192 to the new file descriptor. */
194 sync_buffered_stream (fd);
196 /* Now take care of duplicating the file descriptor that bash is
197 using for input, so we can reinitialize it later. */
198 nfd = fcntl (fd, F_DUPFD, 10);
201 if (fcntl (fd, F_GETFD, 0) == 0)
202 sys_error ("cannot allocate new file descriptor for bash input from fd %d", fd);
208 /* What's this? A stray buffer without an associated open file
209 descriptor? Free up the buffer and report the error. */
210 internal_error ("check_bash_input: buffer already exists for new fd %d", nfd);
211 free_buffered_stream (buffers[nfd]);
214 /* Reinitialize bash_input.location. */
215 if (bash_input.type == st_bstream)
217 bash_input.location.buffered_fd = nfd;
218 fd_to_buffered_stream (nfd);
219 close_buffered_fd (fd); /* XXX */
222 /* If the current input type is not a buffered stream, but the shell
223 is not interactive and therefore using a buffered stream to read
224 input (e.g. with an `eval exec 3>output' inside a script), note
225 that the input fd has been changed. pop_stream() looks at this
226 value and adjusts the input fd to the new value of
227 default_buffered_input accordingly. */
228 bash_input_fd_changed++;
230 if (default_buffered_input == fd)
231 default_buffered_input = nfd;
236 /* This is the buffered stream analogue of dup2(fd1, fd2). The
237 BUFFERED_STREAM corresponding to fd2 is deallocated, if one exists.
238 BUFFERS[fd1] is copied to BUFFERS[fd2]. This is called by the
239 redirect code for constructs like 4<&0 and 3</etc/rc.local. */
241 duplicate_buffered_stream (fd1, fd2)
244 int is_bash_input, m;
250 ALLOCATE_BUFFERS (m);
252 /* If FD2 is the file descriptor bash is currently using for shell input,
253 we need to do some extra work to make sure that the buffered stream
254 actually exists (it might not if fd1 was not active, and the copy
255 didn't actually do anything). */
256 is_bash_input = (bash_input.type == st_bstream) &&
257 (bash_input.location.buffered_fd == fd2);
260 free_buffered_stream (buffers[fd2]);
261 buffers[fd2] = copy_buffered_stream (buffers[fd1]);
263 buffers[fd2]->b_fd = fd2;
265 if (is_bash_input && !buffers[fd2])
266 fd_to_buffered_stream (fd2);
271 /* Return 1 if a seek on FD will succeed. */
272 #define fd_is_seekable(fd) (lseek ((fd), 0L, SEEK_CUR) >= 0)
274 /* Take FD, a file descriptor, and create and return a buffered stream
275 corresponding to it. If something is wrong and the file descriptor
276 is invalid, return a NULL stream. */
278 fd_to_buffered_stream (fd)
285 if (fstat (fd, &sb) < 0)
288 return ((BUFFERED_STREAM *)NULL);
291 if (fd_is_seekable (fd) == 0)
294 size = (sb.st_size > MAX_INPUT_BUFFER_SIZE) ? MAX_INPUT_BUFFER_SIZE
297 buffer = (char *)xmalloc (size);
299 return (make_buffered_stream (fd, buffer, size));
302 /* Return a buffered stream corresponding to FILE, a file name. */
304 open_buffered_stream (file)
309 fd = open (file, O_RDONLY);
310 return ((fd >= 0) ? fd_to_buffered_stream (fd) : (BUFFERED_STREAM *)NULL);
313 /* Deallocate a buffered stream and free up its resources. Make sure we
314 zero out the slot in BUFFERS that points to BP. */
316 free_buffered_stream (bp)
328 buffers[n] = (BUFFERED_STREAM *)NULL;
331 /* Close the file descriptor associated with BP, a buffered stream, and free
332 up the stream. Return the status of closing BP's file descriptor. */
334 close_buffered_stream (bp)
342 free_buffered_stream (bp);
346 /* Deallocate the buffered stream associated with file descriptor FD, and
347 close FD. Return the status of the close on FD. */
349 close_buffered_fd (fd)
352 if (fd >= nbuffers || !buffers || !buffers[fd])
354 return (close_buffered_stream (buffers[fd]));
357 /* Read a buffer full of characters from BP, a buffered stream. */
364 bp->b_used = read (bp->b_fd, bp->b_buffer, bp->b_size);
366 while (bp->b_used < 0 && errno == EINTR);
373 bp->b_flag |= B_ERROR;
377 return (bp->b_buffer[bp->b_inputp++] & 0xFF);
380 /* Get a character from buffered stream BP. */
381 #define bufstream_getc(bp) \
382 (bp->b_inputp == bp->b_used || !bp->b_used) \
383 ? b_fill_buffer (bp) \
384 : bp->b_buffer[bp->b_inputp++] & 0xFF
386 /* Push C back onto buffered stream BP. */
388 bufstream_ungetc(c, bp)
392 if (c == EOF || bp->b_inputp == 0)
395 bp->b_buffer[--bp->b_inputp] = c;
399 /* Seek backwards on file BFD to synchronize what we've read so far
400 with the underlying file pointer. */
402 sync_buffered_stream (bfd)
411 chars_left = bp->b_used - bp->b_inputp;
413 lseek (bp->b_fd, -chars_left, SEEK_CUR);
414 bp->b_used = bp->b_inputp = 0;
421 return (bufstream_getc (buffers[bash_input.location.buffered_fd]));
425 buffered_ungetchar (c)
428 return (bufstream_ungetc (c, buffers[bash_input.location.buffered_fd]));
431 /* Make input come from file descriptor BFD through a buffered stream. */
433 with_input_from_buffered_stream (bfd, name)
437 INPUT_STREAM location;
440 location.buffered_fd = bfd;
441 /* Make sure the buffered stream exists. */
442 bp = fd_to_buffered_stream (bfd);
443 init_yy_io (bp == 0 ? return_EOF : buffered_getchar,
444 buffered_ungetchar, st_bstream, name, location);
452 return ((char *)malloc (s));
461 return((char *)malloc (size));
463 return((char *)realloc (s, size));
476 while ((c = bufstream_getc(bp)) != EOF)
480 BASH_INPUT bash_input;
482 struct stat dsb; /* can be used from gdb */
484 /* imitate /bin/cat */
493 bp = fd_to_buffered_stream (0);
497 for (i = 1; i < argc; i++) {
498 if (argv[i][0] == '-' && argv[i][1] == '\0') {
499 bp = fd_to_buffered_stream (0);
503 free_buffered_stream (bp);
505 bp = open_buffered_stream (argv[i]);
509 close_buffered_stream (bp);
515 #endif /* BUFFERED_INPUT */