blob: ea2e407bbb0bb2f8ea6a7401fa482f65ea8e2a4b [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001/* input.c -- functions to perform buffered input with synchronization. */
2
Jari Aalto31859422009-01-12 13:36:28 +00003/* Copyright (C) 1992-2009 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00004
5 This file is part of GNU Bash, the Bourne Again SHell.
6
Jari Aalto31859422009-01-12 13:36:28 +00007 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.
Jari Aalto726f6381996-08-26 18:22:31 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 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.
Jari Aalto726f6381996-08-26 18:22:31 +000016
Jari Aalto31859422009-01-12 13:36:28 +000017 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
Jari Aalto726f6381996-08-26 18:22:31 +000020
Jari Aaltoccc6cda1996-12-23 17:02:34 +000021#include "config.h"
Jari Aalto726f6381996-08-26 18:22:31 +000022
23#include "bashtypes.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000024#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
Jari Aaltocce855b1998-04-17 19:52:44 +000025# include <sys/file.h>
26#endif
Jari Aalto726f6381996-08-26 18:22:31 +000027#include "filecntl.h"
28#include "posixstat.h"
29#include <stdio.h>
30#include <errno.h>
31
Jari Aaltoccc6cda1996-12-23 17:02:34 +000032#if defined (HAVE_UNISTD_H)
33# include <unistd.h>
34#endif
35
Jari Aalto726f6381996-08-26 18:22:31 +000036#include "bashansi.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000037#include "bashintl.h"
38
Jari Aalto726f6381996-08-26 18:22:31 +000039#include "command.h"
40#include "general.h"
41#include "input.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000042#include "error.h"
43#include "externs.h"
Jari Aalto06285672006-10-10 14:15:34 +000044#include "quit.h"
Chet Rameyac50fba2014-02-26 09:36:43 -050045#include "trap.h"
Jari Aalto726f6381996-08-26 18:22:31 +000046
47#if !defined (errno)
Ricardo Cerqueiraa02fbff2013-07-25 22:35:34 +010048#include <errno.h>
Jari Aalto726f6381996-08-26 18:22:31 +000049#endif /* !errno */
50
Jari Aalto31859422009-01-12 13:36:28 +000051#if defined (EAGAIN)
52# define X_EAGAIN EAGAIN
53#else
54# define X_EAGAIN -99
55#endif
56
57#if defined (EWOULDBLOCK)
58# define X_EWOULDBLOCK EWOULDBLOCK
59#else
60# define X_EWOULDBLOCK -99
61#endif
62
Jari Aalto06285672006-10-10 14:15:34 +000063extern void termsig_handler __P((int));
64
Jari Aaltoccc6cda1996-12-23 17:02:34 +000065/* Functions to handle reading input on systems that don't restart read(2)
66 if a signal is received. */
67
Jari Aaltof73dda02001-11-13 17:56:06 +000068static char localbuf[128];
Jari Aalto95732b42005-12-07 14:08:12 +000069static int local_index = 0, local_bufused = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000070
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. */
74int
75getc_with_restart (stream)
76 FILE *stream;
77{
Jari Aaltof73dda02001-11-13 17:56:06 +000078 unsigned char uc;
79
Jari Aalto06285672006-10-10 14:15:34 +000080 CHECK_TERMSIG;
81
Jari Aaltoccc6cda1996-12-23 17:02:34 +000082 /* Try local buffering to reduce the number of read(2) calls. */
83 if (local_index == local_bufused || local_bufused == 0)
84 {
85 while (1)
86 {
Chet Rameyac50fba2014-02-26 09:36:43 -050087 QUIT;
88 run_pending_traps ();
89
Jari Aaltoccc6cda1996-12-23 17:02:34 +000090 local_bufused = read (fileno (stream), localbuf, sizeof(localbuf));
91 if (local_bufused > 0)
92 break;
Chet Rameyac50fba2014-02-26 09:36:43 -050093 else if (local_bufused == 0)
94 {
95 local_index = 0;
96 return EOF;
97 }
Jari Aalto31859422009-01-12 13:36:28 +000098 else if (errno == X_EAGAIN || errno == X_EWOULDBLOCK)
99 {
100 if (sh_unset_nodelay_mode (fileno (stream)) < 0)
101 {
102 sys_error (_("cannot reset nodelay mode for fd %d"), fileno (stream));
Chet Rameyac50fba2014-02-26 09:36:43 -0500103 local_index = local_bufused = 0;
Jari Aalto31859422009-01-12 13:36:28 +0000104 return EOF;
105 }
106 continue;
107 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500108 else if (errno != EINTR)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000109 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500110 local_index = local_bufused = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000111 return EOF;
112 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500113 else if (interrupt_state || terminating_signal) /* QUIT; */
114 local_index = local_bufused = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000115 }
116 local_index = 0;
117 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000118 uc = localbuf[local_index++];
119 return uc;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000120}
121
122int
123ungetc_with_restart (c, stream)
124 int c;
125 FILE *stream;
126{
127 if (local_index == 0 || c == EOF)
128 return EOF;
Jari Aaltof73dda02001-11-13 17:56:06 +0000129 localbuf[--local_index] = c;
130 return c;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000131}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000132
133#if defined (BUFFERED_INPUT)
134
135/* A facility similar to stdio, but input-only. */
136
Jari Aaltobb706242000-03-17 21:46:59 +0000137#if defined (USING_BASH_MALLOC)
138# define MAX_INPUT_BUFFER_SIZE 8176
139#else
140# define MAX_INPUT_BUFFER_SIZE 8192
141#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000142
143#if !defined (SEEK_CUR)
144# define SEEK_CUR 1
145#endif /* !SEEK_CUR */
146
Jari Aalto28ef6c32001-04-06 19:14:31 +0000147#ifdef max
148# undef max
149#endif
150#define max(a, b) (((a) > (b)) ? (a) : (b))
151#ifdef min
152# undef min
153#endif
154#define min(a, b) ((a) > (b) ? (b) : (a))
155
Jari Aalto726f6381996-08-26 18:22:31 +0000156extern int interactive_shell;
157
158int bash_input_fd_changed;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000159
Jari Aalto726f6381996-08-26 18:22:31 +0000160/* 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. */
Jari Aaltocce855b1998-04-17 19:52:44 +0000165static BUFFERED_STREAM **buffers = (BUFFERED_STREAM **)NULL;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000166static int nbuffers;
Jari Aalto726f6381996-08-26 18:22:31 +0000167
Jari Aalto726f6381996-08-26 18:22:31 +0000168#define ALLOCATE_BUFFERS(n) \
169 do { if ((n) >= nbuffers) allocate_buffers (n); } while (0)
170
171/* Make sure `buffers' has at least N elements. */
172static void
173allocate_buffers (n)
174 int n;
175{
176 register int i, orig_nbuffers;
177
178 orig_nbuffers = nbuffers;
179 nbuffers = n + 20;
180 buffers = (BUFFERED_STREAM **)xrealloc
181 (buffers, nbuffers * sizeof (BUFFERED_STREAM *));
182
183 /* Zero out the new buffers. */
184 for (i = orig_nbuffers; i < nbuffers; i++)
185 buffers[i] = (BUFFERED_STREAM *)NULL;
186}
187
188/* Construct and return a BUFFERED_STREAM corresponding to file descriptor
189 FD, using BUFFER. */
190static BUFFERED_STREAM *
191make_buffered_stream (fd, buffer, bufsize)
192 int fd;
193 char *buffer;
Jari Aaltocce855b1998-04-17 19:52:44 +0000194 size_t bufsize;
Jari Aalto726f6381996-08-26 18:22:31 +0000195{
196 BUFFERED_STREAM *bp;
197
198 bp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
199 ALLOCATE_BUFFERS (fd);
200 buffers[fd] = bp;
201 bp->b_fd = fd;
202 bp->b_buffer = buffer;
203 bp->b_size = bufsize;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000204 bp->b_used = bp->b_inputp = bp->b_flag = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000205 if (bufsize == 1)
206 bp->b_flag |= B_UNBUFF;
Chet Ramey495aee42011-11-22 19:11:26 -0500207 if (O_TEXT && (fcntl (fd, F_GETFL) & O_TEXT) != 0)
208 bp->b_flag |= O_TEXT;
Jari Aalto726f6381996-08-26 18:22:31 +0000209 return (bp);
210}
211
212/* Allocate a new BUFFERED_STREAM, copy BP to it, and return the new copy. */
213static BUFFERED_STREAM *
214copy_buffered_stream (bp)
215 BUFFERED_STREAM *bp;
216{
217 BUFFERED_STREAM *nbp;
218
219 if (!bp)
220 return ((BUFFERED_STREAM *)NULL);
221
222 nbp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
223 xbcopy ((char *)bp, (char *)nbp, sizeof (BUFFERED_STREAM));
224 return (nbp);
225}
226
Jari Aalto28ef6c32001-04-06 19:14:31 +0000227int
228set_bash_input_fd (fd)
229 int fd;
230{
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;
235 return 0;
236}
237
238int
239fd_is_bash_input (fd)
240 int fd;
241{
242 if (bash_input.type == st_bstream && bash_input.location.buffered_fd == fd)
243 return 1;
244 else if (interactive_shell == 0 && default_buffered_input == fd)
245 return 1;
246 return 0;
247}
248
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. */
253int
254save_bash_input (fd, new_fd)
255 int fd, new_fd;
256{
257 int nfd;
258
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. */
262 if (buffers[fd])
263 sync_buffered_stream (fd);
264
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;
268 if (nfd == -1)
269 {
270 if (fcntl (fd, F_GETFD, 0) == 0)
Jari Aaltob80f6442004-07-27 13:29:18 +0000271 sys_error (_("cannot allocate new file descriptor for bash input from fd %d"), fd);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000272 return -1;
273 }
274
275 if (buffers[nfd])
276 {
277 /* What's this? A stray buffer without an associated open file
278 descriptor? Free up the buffer and report the error. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000279 internal_error (_("save_bash_input: buffer already exists for new fd %d"), nfd);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000280 free_buffered_stream (buffers[nfd]);
281 }
282
283 /* Reinitialize bash_input.location. */
284 if (bash_input.type == st_bstream)
285 {
286 bash_input.location.buffered_fd = nfd;
287 fd_to_buffered_stream (nfd);
288 close_buffered_fd (fd); /* XXX */
289 }
290 else
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++;
298
299 if (default_buffered_input == fd)
300 default_buffered_input = nfd;
301
302 SET_CLOSE_ON_EXEC (nfd);
303 return nfd;
304}
305
Jari Aalto726f6381996-08-26 18:22:31 +0000306/* 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
Jari Aalto28ef6c32001-04-06 19:14:31 +0000312 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
Chet Rameyac50fba2014-02-26 09:36:43 -0500315 fd 0, sync_buffered_stream is used instead, to cooperate with input
Jari Aalto28ef6c32001-04-06 19:14:31 +0000316 redirection (look at redir.c:add_undo_redirect()). */
Jari Aalto726f6381996-08-26 18:22:31 +0000317int
318check_bash_input (fd)
319 int fd;
320{
Jari Aaltob80f6442004-07-27 13:29:18 +0000321 if (fd_is_bash_input (fd))
322 {
323 if (fd > 0)
324 return ((save_bash_input (fd, -1) == -1) ? -1 : 0);
325 else if (fd == 0)
326 return ((sync_buffered_stream (fd) == -1) ? -1 : 0);
327 }
Jari Aalto726f6381996-08-26 18:22:31 +0000328 return 0;
329}
330
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. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000335int
Jari Aalto726f6381996-08-26 18:22:31 +0000336duplicate_buffered_stream (fd1, fd2)
337 int fd1, fd2;
338{
339 int is_bash_input, m;
340
341 if (fd1 == fd2)
342 return 0;
343
344 m = max (fd1, fd2);
345 ALLOCATE_BUFFERS (m);
346
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);
353
354 if (buffers[fd2])
Jari Aalto95732b42005-12-07 14:08:12 +0000355 {
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;
359 else
360 free_buffered_stream (buffers[fd2]);
361 }
Jari Aalto726f6381996-08-26 18:22:31 +0000362 buffers[fd2] = copy_buffered_stream (buffers[fd1]);
363 if (buffers[fd2])
364 buffers[fd2]->b_fd = fd2;
365
Jari Aalto28ef6c32001-04-06 19:14:31 +0000366 if (is_bash_input)
367 {
368 if (!buffers[fd2])
369 fd_to_buffered_stream (fd2);
370 buffers[fd2]->b_flag |= B_WASBASHINPUT;
371 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000372
Jari Aalto726f6381996-08-26 18:22:31 +0000373 return (fd2);
374}
375
376/* Return 1 if a seek on FD will succeed. */
Chet Ramey495aee42011-11-22 19:11:26 -0500377#define fd_is_seekable(fd) (lseek ((fd), 0L, SEEK_CUR) >= 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000378
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. */
382BUFFERED_STREAM *
383fd_to_buffered_stream (fd)
384 int fd;
385{
386 char *buffer;
Jari Aaltocce855b1998-04-17 19:52:44 +0000387 size_t size;
Jari Aalto726f6381996-08-26 18:22:31 +0000388 struct stat sb;
389
390 if (fstat (fd, &sb) < 0)
391 {
392 close (fd);
393 return ((BUFFERED_STREAM *)NULL);
394 }
395
Jari Aalto28ef6c32001-04-06 19:14:31 +0000396 size = (fd_is_seekable (fd)) ? min (sb.st_size, MAX_INPUT_BUFFER_SIZE) : 1;
397 if (size == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000398 size = 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000399 buffer = (char *)xmalloc (size);
400
401 return (make_buffered_stream (fd, buffer, size));
402}
403
404/* Return a buffered stream corresponding to FILE, a file name. */
405BUFFERED_STREAM *
406open_buffered_stream (file)
407 char *file;
408{
409 int fd;
410
411 fd = open (file, O_RDONLY);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000412 return ((fd >= 0) ? fd_to_buffered_stream (fd) : (BUFFERED_STREAM *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000413}
414
415/* Deallocate a buffered stream and free up its resources. Make sure we
416 zero out the slot in BUFFERS that points to BP. */
417void
418free_buffered_stream (bp)
419 BUFFERED_STREAM *bp;
420{
421 int n;
422
423 if (!bp)
424 return;
425
426 n = bp->b_fd;
427 if (bp->b_buffer)
428 free (bp->b_buffer);
429 free (bp);
430 buffers[n] = (BUFFERED_STREAM *)NULL;
431}
432
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. */
435int
436close_buffered_stream (bp)
437 BUFFERED_STREAM *bp;
438{
439 int fd;
440
441 if (!bp)
442 return (0);
443 fd = bp->b_fd;
444 free_buffered_stream (bp);
445 return (close (fd));
446}
447
448/* Deallocate the buffered stream associated with file descriptor FD, and
449 close FD. Return the status of the close on FD. */
450int
451close_buffered_fd (fd)
452 int fd;
453{
Jari Aaltof73dda02001-11-13 17:56:06 +0000454 if (fd < 0)
455 {
456 errno = EBADF;
457 return -1;
458 }
Jari Aalto726f6381996-08-26 18:22:31 +0000459 if (fd >= nbuffers || !buffers || !buffers[fd])
460 return (close (fd));
461 return (close_buffered_stream (buffers[fd]));
462}
463
Chet Rameyac50fba2014-02-26 09:36:43 -0500464/* Make the BUFFERED_STREAM associated with buffers[FD] be BP, and return
Jari Aaltocce855b1998-04-17 19:52:44 +0000465 the old BUFFERED_STREAM. */
466BUFFERED_STREAM *
467set_buffered_stream (fd, bp)
468 int fd;
469 BUFFERED_STREAM *bp;
470{
471 BUFFERED_STREAM *ret;
472
473 ret = buffers[fd];
474 buffers[fd] = bp;
475 return ret;
476}
477
Jari Aalto726f6381996-08-26 18:22:31 +0000478/* Read a buffer full of characters from BP, a buffered stream. */
479static int
480b_fill_buffer (bp)
481 BUFFERED_STREAM *bp;
482{
Jari Aaltof73dda02001-11-13 17:56:06 +0000483 ssize_t nr;
Chet Ramey495aee42011-11-22 19:11:26 -0500484 off_t o;
Jari Aaltof73dda02001-11-13 17:56:06 +0000485
Jari Aalto06285672006-10-10 14:15:34 +0000486 CHECK_TERMSIG;
Chet Ramey495aee42011-11-22 19:11:26 -0500487 /* 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)
492 {
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)
496 {
497 lseek (bp->b_fd, o, SEEK_SET);
498 bp->b_flag |= B_UNBUFF;
499 bp->b_size = 1;
500 nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
501 }
502 }
503 else
504 nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
Jari Aaltof73dda02001-11-13 17:56:06 +0000505 if (nr <= 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000506 {
Jari Aaltof73dda02001-11-13 17:56:06 +0000507 bp->b_used = 0;
Jari Aalto726f6381996-08-26 18:22:31 +0000508 bp->b_buffer[0] = 0;
Jari Aaltof73dda02001-11-13 17:56:06 +0000509 if (nr == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000510 bp->b_flag |= B_EOF;
511 else
512 bp->b_flag |= B_ERROR;
513 return (EOF);
514 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000515
Jari Aaltof73dda02001-11-13 17:56:06 +0000516 bp->b_used = nr;
Jari Aalto726f6381996-08-26 18:22:31 +0000517 bp->b_inputp = 0;
518 return (bp->b_buffer[bp->b_inputp++] & 0xFF);
519}
520
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
526
527/* Push C back onto buffered stream BP. */
528static int
529bufstream_ungetc(c, bp)
530 int c;
531 BUFFERED_STREAM *bp;
532{
533 if (c == EOF || bp->b_inputp == 0)
534 return (EOF);
535
536 bp->b_buffer[--bp->b_inputp] = c;
537 return (c);
538}
539
540/* Seek backwards on file BFD to synchronize what we've read so far
541 with the underlying file pointer. */
542int
543sync_buffered_stream (bfd)
544 int bfd;
545{
546 BUFFERED_STREAM *bp;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000547 off_t chars_left;
Jari Aalto726f6381996-08-26 18:22:31 +0000548
Jari Aalto28ef6c32001-04-06 19:14:31 +0000549 if (buffers == 0 || (bp = buffers[bfd]) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000550 return (-1);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000551
Jari Aalto726f6381996-08-26 18:22:31 +0000552 chars_left = bp->b_used - bp->b_inputp;
553 if (chars_left)
554 lseek (bp->b_fd, -chars_left, SEEK_CUR);
555 bp->b_used = bp->b_inputp = 0;
556 return (0);
557}
558
559int
560buffered_getchar ()
561{
Jari Aalto06285672006-10-10 14:15:34 +0000562 CHECK_TERMSIG;
563
Jari Aalto28ef6c32001-04-06 19:14:31 +0000564#if !defined (DJGPP)
Jari Aalto726f6381996-08-26 18:22:31 +0000565 return (bufstream_getc (buffers[bash_input.location.buffered_fd]));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000566#else
567 /* On DJGPP, ignore \r. */
568 int ch;
569 while ((ch = bufstream_getc (buffers[bash_input.location.buffered_fd])) == '\r')
570 ;
571 return ch;
572#endif
Jari Aalto726f6381996-08-26 18:22:31 +0000573}
574
575int
576buffered_ungetchar (c)
577 int c;
578{
579 return (bufstream_ungetc (c, buffers[bash_input.location.buffered_fd]));
580}
581
582/* Make input come from file descriptor BFD through a buffered stream. */
583void
584with_input_from_buffered_stream (bfd, name)
585 int bfd;
586 char *name;
587{
588 INPUT_STREAM location;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000589 BUFFERED_STREAM *bp;
Jari Aalto726f6381996-08-26 18:22:31 +0000590
591 location.buffered_fd = bfd;
592 /* Make sure the buffered stream exists. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000593 bp = fd_to_buffered_stream (bfd);
594 init_yy_io (bp == 0 ? return_EOF : buffered_getchar,
595 buffered_ungetchar, st_bstream, name, location);
Jari Aalto726f6381996-08-26 18:22:31 +0000596}
597
598#if defined (TEST)
Jari Aaltof73dda02001-11-13 17:56:06 +0000599void *
Jari Aalto726f6381996-08-26 18:22:31 +0000600xmalloc(s)
601int s;
602{
Jari Aaltof73dda02001-11-13 17:56:06 +0000603 return (malloc (s));
Jari Aalto726f6381996-08-26 18:22:31 +0000604}
605
Jari Aaltof73dda02001-11-13 17:56:06 +0000606void *
Jari Aalto726f6381996-08-26 18:22:31 +0000607xrealloc(s, size)
608char *s;
609int size;
610{
611 if (!s)
Jari Aaltof73dda02001-11-13 17:56:06 +0000612 return(malloc (size));
Jari Aalto726f6381996-08-26 18:22:31 +0000613 else
Jari Aaltof73dda02001-11-13 17:56:06 +0000614 return(realloc (s, size));
Jari Aalto726f6381996-08-26 18:22:31 +0000615}
616
617void
618init_yy_io ()
619{
620}
621
622process(bp)
623BUFFERED_STREAM *bp;
624{
625 int c;
626
627 while ((c = bufstream_getc(bp)) != EOF)
628 putchar(c);
629}
630
631BASH_INPUT bash_input;
632
633struct stat dsb; /* can be used from gdb */
634
635/* imitate /bin/cat */
636main(argc, argv)
637int argc;
638char **argv;
639{
640 register int i;
641 BUFFERED_STREAM *bp;
642
643 if (argc == 1) {
644 bp = fd_to_buffered_stream (0);
645 process(bp);
646 exit(0);
647 }
648 for (i = 1; i < argc; i++) {
649 if (argv[i][0] == '-' && argv[i][1] == '\0') {
650 bp = fd_to_buffered_stream (0);
651 if (!bp)
652 continue;
653 process(bp);
654 free_buffered_stream (bp);
655 } else {
656 bp = open_buffered_stream (argv[i]);
657 if (!bp)
658 continue;
659 process(bp);
660 close_buffered_stream (bp);
661 }
662 }
663 exit(0);
664}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000665#endif /* TEST */
666#endif /* BUFFERED_INPUT */