]>
git.ipfire.org Git - thirdparty/bash.git/blob - input.c
6ca49a80b6cee7e6bc008e658ecbe452e0e8be43
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"
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 #if !defined (HAVE_RESTARTABLE_SYSCALLS)
51 static unsigned char localbuf
[128];
52 static int local_index
, local_bufused
;
54 /* Posix and USG systems do not guarantee to restart read () if it is
55 interrupted by a signal. We do the read ourselves, and restart it
56 if it returns EINTR. */
58 getc_with_restart (stream
)
61 /* Try local buffering to reduce the number of read(2) calls. */
62 if (local_index
== local_bufused
|| local_bufused
== 0)
66 local_bufused
= read (fileno (stream
), localbuf
, sizeof(localbuf
));
67 if (local_bufused
> 0)
69 else if (local_bufused
== 0 || errno
!= EINTR
)
77 return (localbuf
[local_index
++]);
81 ungetc_with_restart (c
, stream
)
85 if (local_index
== 0 || c
== EOF
)
87 return (localbuf
[--local_index
] = c
);
89 #endif /* !HAVE_RESTARTABLE_SYSCALLS */
91 #if defined (BUFFERED_INPUT)
93 /* A facility similar to stdio, but input-only. */
95 #define MAX_INPUT_BUFFER_SIZE 8192
97 #if !defined (SEEK_CUR)
99 #endif /* !SEEK_CUR */
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 static 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. */
273 # define fd_is_seekable(fd) (lseek ((fd), 0L, SEEK_CUR) >= 0)
275 # define fd_is_seekable(fd) 0
276 #endif /* __CYGWIN32__ */
278 /* Take FD, a file descriptor, and create and return a buffered stream
279 corresponding to it. If something is wrong and the file descriptor
280 is invalid, return a NULL stream. */
282 fd_to_buffered_stream (fd
)
289 if (fstat (fd
, &sb
) < 0)
292 return ((BUFFERED_STREAM
*)NULL
);
295 if (fd_is_seekable (fd
) == 0)
298 size
= (size_t)((sb
.st_size
> MAX_INPUT_BUFFER_SIZE
)
299 ? MAX_INPUT_BUFFER_SIZE
302 buffer
= (char *)xmalloc (size
);
304 return (make_buffered_stream (fd
, buffer
, size
));
307 /* Return a buffered stream corresponding to FILE, a file name. */
309 open_buffered_stream (file
)
314 fd
= open (file
, O_RDONLY
);
315 return ((fd
>= 0) ? fd_to_buffered_stream (fd
) : (BUFFERED_STREAM
*)NULL
);
318 /* Deallocate a buffered stream and free up its resources. Make sure we
319 zero out the slot in BUFFERS that points to BP. */
321 free_buffered_stream (bp
)
333 buffers
[n
] = (BUFFERED_STREAM
*)NULL
;
336 /* Close the file descriptor associated with BP, a buffered stream, and free
337 up the stream. Return the status of closing BP's file descriptor. */
339 close_buffered_stream (bp
)
347 free_buffered_stream (bp
);
351 /* Deallocate the buffered stream associated with file descriptor FD, and
352 close FD. Return the status of the close on FD. */
354 close_buffered_fd (fd
)
357 if (fd
>= nbuffers
|| !buffers
|| !buffers
[fd
])
359 return (close_buffered_stream (buffers
[fd
]));
362 /* Make the BUFFERED_STREAM associcated with buffers[FD] be BP, and return
363 the old BUFFERED_STREAM. */
365 set_buffered_stream (fd
, bp
)
369 BUFFERED_STREAM
*ret
;
376 /* Read a buffer full of characters from BP, a buffered stream. */
383 bp
->b_used
= read (bp
->b_fd
, bp
->b_buffer
, bp
->b_size
);
385 while (bp
->b_used
< 0 && errno
== EINTR
);
392 bp
->b_flag
|= B_ERROR
;
396 return (bp
->b_buffer
[bp
->b_inputp
++] & 0xFF);
399 /* Get a character from buffered stream BP. */
400 #define bufstream_getc(bp) \
401 (bp->b_inputp == bp->b_used || !bp->b_used) \
402 ? b_fill_buffer (bp) \
403 : bp->b_buffer[bp->b_inputp++] & 0xFF
405 /* Push C back onto buffered stream BP. */
407 bufstream_ungetc(c
, bp
)
411 if (c
== EOF
|| bp
->b_inputp
== 0)
414 bp
->b_buffer
[--bp
->b_inputp
] = c
;
418 /* Seek backwards on file BFD to synchronize what we've read so far
419 with the underlying file pointer. */
421 sync_buffered_stream (bfd
)
430 chars_left
= bp
->b_used
- bp
->b_inputp
;
432 lseek (bp
->b_fd
, -chars_left
, SEEK_CUR
);
433 bp
->b_used
= bp
->b_inputp
= 0;
440 return (bufstream_getc (buffers
[bash_input
.location
.buffered_fd
]));
444 buffered_ungetchar (c
)
447 return (bufstream_ungetc (c
, buffers
[bash_input
.location
.buffered_fd
]));
450 /* Make input come from file descriptor BFD through a buffered stream. */
452 with_input_from_buffered_stream (bfd
, name
)
456 INPUT_STREAM location
;
459 location
.buffered_fd
= bfd
;
460 /* Make sure the buffered stream exists. */
461 bp
= fd_to_buffered_stream (bfd
);
462 init_yy_io (bp
== 0 ? return_EOF
: buffered_getchar
,
463 buffered_ungetchar
, st_bstream
, name
, location
);
471 return ((char *)malloc (s
));
480 return((char *)malloc (size
));
482 return((char *)realloc (s
, size
));
495 while ((c
= bufstream_getc(bp
)) != EOF
)
499 BASH_INPUT bash_input
;
501 struct stat dsb
; /* can be used from gdb */
503 /* imitate /bin/cat */
512 bp
= fd_to_buffered_stream (0);
516 for (i
= 1; i
< argc
; i
++) {
517 if (argv
[i
][0] == '-' && argv
[i
][1] == '\0') {
518 bp
= fd_to_buffered_stream (0);
522 free_buffered_stream (bp
);
524 bp
= open_buffered_stream (argv
[i
]);
528 close_buffered_stream (bp
);
534 #endif /* BUFFERED_INPUT */