1 /* input.c -- functions to perform buffered input with synchronization. */
3 /* Copyright (C) 1992-2020 Free Software Foundation, Inc.
5 This file is part of GNU Bush, the Bourne Again SHell.
7 Bush 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.
12 Bush 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.
17 You should have received a copy of the GNU General Public License
18 along with Bush. If not, see <http://www.gnu.org/licenses/>.
23 #include "bushtypes.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)
40 #include "input/input.h"
49 # define X_EAGAIN EAGAIN
54 #if defined (EWOULDBLOCK)
55 # define X_EWOULDBLOCK EWOULDBLOCK
57 # define X_EWOULDBLOCK -99
60 extern void termsig_handler
PARAMS((int));
62 /* Functions to handle reading input on systems that don't restart read(2)
63 if a signal is received. */
65 static char localbuf
[1024];
66 static int local_index
= 0, local_bufused
= 0;
68 /* Posix and USG systems do not guarantee to restart read () if it is
69 interrupted by a signal. We do the read ourselves, and restart it
70 if it returns EINTR. */
72 getc_with_restart (stream
)
79 /* Try local buffering to reduce the number of read(2) calls. */
80 if (local_index
== local_bufused
|| local_bufused
== 0)
87 local_bufused
= read (fileno (stream
), localbuf
, sizeof(localbuf
));
88 if (local_bufused
> 0)
90 else if (local_bufused
== 0)
95 else if (errno
== X_EAGAIN
|| errno
== X_EWOULDBLOCK
)
97 if (sh_unset_nodelay_mode (fileno (stream
)) < 0)
99 sys_error (_("cannot reset nodelay mode for fd %d"), fileno (stream
));
100 local_index
= local_bufused
= 0;
105 else if (errno
!= EINTR
)
107 local_index
= local_bufused
= 0;
110 else if (interrupt_state
|| terminating_signal
) /* QUIT; */
111 local_index
= local_bufused
= 0;
115 uc
= localbuf
[local_index
++];
120 ungetc_with_restart (c
, stream
)
124 if (local_index
== 0 || c
== EOF
)
126 localbuf
[--local_index
] = c
;
130 #if defined (BUFFERED_INPUT)
132 /* A facility similar to stdio, but input-only. */
134 #if defined (USING_BUSH_MALLOC)
135 # define MAX_INPUT_BUFFER_SIZE 8172
137 # define MAX_INPUT_BUFFER_SIZE 8192
140 #if !defined (SEEK_CUR)
142 #endif /* !SEEK_CUR */
147 #define max(a, b) (((a) > (b)) ? (a) : (b))
151 #define min(a, b) ((a) > (b) ? (b) : (a))
153 int bush_input_fd_changed
;
155 /* This provides a way to map from a file descriptor to the buffer
156 associated with that file descriptor, rather than just the other
157 way around. This is needed so that buffers are managed properly
158 in constructs like 3<&4. buffers[x]->b_fd == x -- that is how the
159 correspondence is maintained. */
160 static BUFFERED_STREAM
**buffers
= (BUFFERED_STREAM
**)NULL
;
163 #define ALLOCATE_BUFFERS(n) \
164 do { if ((n) >= nbuffers) allocate_buffers (n); } while (0)
166 /* Make sure `buffers' has at least N elements. */
171 register int i
, orig_nbuffers
;
173 orig_nbuffers
= nbuffers
;
175 buffers
= (BUFFERED_STREAM
**)xrealloc
176 (buffers
, nbuffers
* sizeof (BUFFERED_STREAM
*));
178 /* Zero out the new buffers. */
179 for (i
= orig_nbuffers
; i
< nbuffers
; i
++)
180 buffers
[i
] = (BUFFERED_STREAM
*)NULL
;
183 /* Construct and return a BUFFERED_STREAM corresponding to file descriptor
185 static BUFFERED_STREAM
*
186 make_buffered_stream (fd
, buffer
, bufsize
)
193 bp
= (BUFFERED_STREAM
*)xmalloc (sizeof (BUFFERED_STREAM
));
194 ALLOCATE_BUFFERS (fd
);
197 bp
->b_buffer
= buffer
;
198 bp
->b_size
= bufsize
;
199 bp
->b_used
= bp
->b_inputp
= bp
->b_flag
= 0;
201 bp
->b_flag
|= B_UNBUFF
;
202 if (O_TEXT
&& (fcntl (fd
, F_GETFL
) & O_TEXT
) != 0)
203 bp
->b_flag
|= B_TEXT
;
207 /* Allocate a new BUFFERED_STREAM, copy BP to it, and return the new copy. */
208 static BUFFERED_STREAM
*
209 copy_buffered_stream (bp
)
212 BUFFERED_STREAM
*nbp
;
215 return ((BUFFERED_STREAM
*)NULL
);
217 nbp
= (BUFFERED_STREAM
*)xmalloc (sizeof (BUFFERED_STREAM
));
218 xbcopy ((char *)bp
, (char *)nbp
, sizeof (BUFFERED_STREAM
));
223 set_bush_input_fd (fd
)
226 if (bush_input
.type
== st_bstream
)
227 bush_input
.location
.buffered_fd
= fd
;
228 else if (interactive_shell
== 0)
229 default_buffered_input
= fd
;
234 fd_is_bush_input (fd
)
237 if (bush_input
.type
== st_bstream
&& bush_input
.location
.buffered_fd
== fd
)
239 else if (interactive_shell
== 0 && default_buffered_input
== fd
)
244 /* Save the buffered stream corresponding to file descriptor FD (which bush
245 is using to read input) to a buffered stream associated with NEW_FD. If
246 NEW_FD is -1, a new file descriptor is allocated with fcntl. The new
247 file descriptor is returned on success, -1 on error. */
249 save_bush_input (fd
, new_fd
)
254 /* Sync the stream so we can re-read from the new file descriptor. We
255 might be able to avoid this by copying the buffered stream verbatim
256 to the new file descriptor. */
258 sync_buffered_stream (fd
);
260 /* Now take care of duplicating the file descriptor that bush is
261 using for input, so we can reinitialize it later. */
262 nfd
= (new_fd
== -1) ? fcntl (fd
, F_DUPFD
, 10) : new_fd
;
265 if (fcntl (fd
, F_GETFD
, 0) == 0)
266 sys_error (_("cannot allocate new file descriptor for bush input from fd %d"), fd
);
270 if (nfd
< nbuffers
&& buffers
[nfd
])
272 /* What's this? A stray buffer without an associated open file
273 descriptor? Free up the buffer and report the error. */
274 internal_error (_("save_bush_input: buffer already exists for new fd %d"), nfd
);
275 if (buffers
[nfd
]->b_flag
& B_SHAREDBUF
)
276 buffers
[nfd
]->b_buffer
= (char *)NULL
;
277 free_buffered_stream (buffers
[nfd
]);
280 /* Reinitialize bush_input.location. */
281 if (bush_input
.type
== st_bstream
)
283 bush_input
.location
.buffered_fd
= nfd
;
284 fd_to_buffered_stream (nfd
);
285 close_buffered_fd (fd
); /* XXX */
288 /* If the current input type is not a buffered stream, but the shell
289 is not interactive and therefore using a buffered stream to read
290 input (e.g. with an `eval exec 3>output' inside a script), note
291 that the input fd has been changed. pop_stream() looks at this
292 value and adjusts the input fd to the new value of
293 default_buffered_input accordingly. */
294 bush_input_fd_changed
++;
296 if (default_buffered_input
== fd
)
297 default_buffered_input
= nfd
;
299 SET_CLOSE_ON_EXEC (nfd
);
303 /* Check that file descriptor FD is not the one that bush is currently
304 using to read input from a script. FD is about to be duplicated onto,
305 which means that the kernel will close it for us. If FD is the bush
306 input file descriptor, we need to seek backwards in the script (if
307 possible and necessary -- scripts read from stdin are still unbuffered),
308 allocate a new file descriptor to use for bush input, and re-initialize
309 the buffered stream. Make sure the file descriptor used to save bush
310 input is set close-on-exec. Returns 0 on success, -1 on failure. This
311 works only if fd is > 0 -- if fd == 0 and bush is reading input from
312 fd 0, sync_buffered_stream is used instead, to cooperate with input
313 redirection (look at redir.c:add_undo_redirect()). */
315 check_bush_input (fd
)
318 if (fd_is_bush_input (fd
))
321 return ((save_bush_input (fd
, -1) == -1) ? -1 : 0);
323 return ((sync_buffered_stream (fd
) == -1) ? -1 : 0);
328 /* This is the buffered stream analogue of dup2(fd1, fd2). The
329 BUFFERED_STREAM corresponding to fd2 is deallocated, if one exists.
330 BUFFERS[fd1] is copied to BUFFERS[fd2]. This is called by the
331 redirect code for constructs like 4<&0 and 3</etc/rc.local. */
333 duplicate_buffered_stream (fd1
, fd2
)
336 int is_bush_input
, m
;
342 ALLOCATE_BUFFERS (m
);
344 /* If FD2 is the file descriptor bush is currently using for shell input,
345 we need to do some extra work to make sure that the buffered stream
346 actually exists (it might not if fd1 was not active, and the copy
347 didn't actually do anything). */
348 is_bush_input
= (bush_input
.type
== st_bstream
) &&
349 (bush_input
.location
.buffered_fd
== fd2
);
353 /* If the two objects share the same b_buffer, don't free it. */
354 if (buffers
[fd1
] && buffers
[fd1
]->b_buffer
&& buffers
[fd1
]->b_buffer
== buffers
[fd2
]->b_buffer
)
355 buffers
[fd2
] = (BUFFERED_STREAM
*)NULL
;
356 /* If this buffer is shared with another fd, don't free the buffer */
357 else if (buffers
[fd2
]->b_flag
& B_SHAREDBUF
)
359 buffers
[fd2
]->b_buffer
= (char *)NULL
;
360 free_buffered_stream (buffers
[fd2
]);
363 free_buffered_stream (buffers
[fd2
]);
365 buffers
[fd2
] = copy_buffered_stream (buffers
[fd1
]);
367 buffers
[fd2
]->b_fd
= fd2
;
372 fd_to_buffered_stream (fd2
);
373 buffers
[fd2
]->b_flag
|= B_WASBUSHINPUT
;
376 if (fd_is_bush_input (fd1
) || (buffers
[fd1
] && (buffers
[fd1
]->b_flag
& B_SHAREDBUF
)))
377 buffers
[fd2
]->b_flag
|= B_SHAREDBUF
;
382 /* Return 1 if a seek on FD will succeed. */
383 #define fd_is_seekable(fd) (lseek ((fd), 0L, SEEK_CUR) >= 0)
385 /* Take FD, a file descriptor, and create and return a buffered stream
386 corresponding to it. If something is wrong and the file descriptor
387 is invalid, return a NULL stream. */
389 fd_to_buffered_stream (fd
)
396 if (fstat (fd
, &sb
) < 0)
399 return ((BUFFERED_STREAM
*)NULL
);
402 size
= (fd_is_seekable (fd
)) ? min (sb
.st_size
, MAX_INPUT_BUFFER_SIZE
) : 1;
405 buffer
= (char *)xmalloc (size
);
407 return (make_buffered_stream (fd
, buffer
, size
));
410 /* Return a buffered stream corresponding to FILE, a file name. */
412 open_buffered_stream (file
)
417 fd
= open (file
, O_RDONLY
);
418 return ((fd
>= 0) ? fd_to_buffered_stream (fd
) : (BUFFERED_STREAM
*)NULL
);
421 /* Deallocate a buffered stream and free up its resources. Make sure we
422 zero out the slot in BUFFERS that points to BP. */
424 free_buffered_stream (bp
)
436 buffers
[n
] = (BUFFERED_STREAM
*)NULL
;
439 /* Close the file descriptor associated with BP, a buffered stream, and free
440 up the stream. Return the status of closing BP's file descriptor. */
442 close_buffered_stream (bp
)
450 if (bp
->b_flag
& B_SHAREDBUF
)
451 bp
->b_buffer
= (char *)NULL
;
452 free_buffered_stream (bp
);
456 /* Deallocate the buffered stream associated with file descriptor FD, and
457 close FD. Return the status of the close on FD. */
459 close_buffered_fd (fd
)
467 if (fd
>= nbuffers
|| !buffers
|| !buffers
[fd
])
469 return (close_buffered_stream (buffers
[fd
]));
472 /* Make the BUFFERED_STREAM associated with buffers[FD] be BP, and return
473 the old BUFFERED_STREAM. */
475 set_buffered_stream (fd
, bp
)
479 BUFFERED_STREAM
*ret
;
486 /* Read a buffer full of characters from BP, a buffered stream. */
495 /* In an environment where text and binary files are treated differently,
496 compensate for lseek() on text files returning an offset different from
497 the count of characters read() returns. Text-mode streams have to be
498 treated as unbuffered. */
499 if ((bp
->b_flag
& (B_TEXT
| B_UNBUFF
)) == B_TEXT
)
501 o
= lseek (bp
->b_fd
, 0, SEEK_CUR
);
502 nr
= zread (bp
->b_fd
, bp
->b_buffer
, bp
->b_size
);
503 if (nr
> 0 && nr
< lseek (bp
->b_fd
, 0, SEEK_CUR
) - o
)
505 lseek (bp
->b_fd
, o
, SEEK_SET
);
506 bp
->b_flag
|= B_UNBUFF
;
508 nr
= zread (bp
->b_fd
, bp
->b_buffer
, bp
->b_size
);
512 nr
= zread (bp
->b_fd
, bp
->b_buffer
, bp
->b_size
);
515 bp
->b_used
= bp
->b_inputp
= 0;
520 bp
->b_flag
|= B_ERROR
;
526 return (bp
->b_buffer
[bp
->b_inputp
++] & 0xFF);
529 /* Get a character from buffered stream BP. */
530 #define bufstream_getc(bp) \
531 (bp->b_inputp == bp->b_used || !bp->b_used) \
532 ? b_fill_buffer (bp) \
533 : bp->b_buffer[bp->b_inputp++] & 0xFF
535 /* Push C back onto buffered stream BP. */
537 bufstream_ungetc(c
, bp
)
541 if (c
== EOF
|| bp
== 0 || bp
->b_inputp
== 0)
544 bp
->b_buffer
[--bp
->b_inputp
] = c
;
548 /* Seek backwards on file BFD to synchronize what we've read so far
549 with the underlying file pointer. */
551 sync_buffered_stream (bfd
)
557 if (buffers
== 0 || (bp
= buffers
[bfd
]) == 0)
560 chars_left
= bp
->b_used
- bp
->b_inputp
;
562 lseek (bp
->b_fd
, -chars_left
, SEEK_CUR
);
563 bp
->b_used
= bp
->b_inputp
= 0;
572 if (bush_input
.location
.buffered_fd
< 0 || buffers
[bush_input
.location
.buffered_fd
] == 0)
576 return (bufstream_getc (buffers
[bush_input
.location
.buffered_fd
]));
578 /* On DJGPP, ignore \r. */
580 while ((ch
= bufstream_getc (buffers
[bush_input
.location
.buffered_fd
])) == '\r')
587 buffered_ungetchar (c
)
590 return (bufstream_ungetc (c
, buffers
[bush_input
.location
.buffered_fd
]));
593 /* Make input come from file descriptor BFD through a buffered stream. */
595 with_input_from_buffered_stream (bfd
, name
)
599 INPUT_STREAM location
;
602 location
.buffered_fd
= bfd
;
603 /* Make sure the buffered stream exists. */
604 bp
= fd_to_buffered_stream (bfd
);
605 init_yy_io (bp
== 0 ? return_EOF
: buffered_getchar
,
606 buffered_ungetchar
, st_bstream
, name
, location
);
623 return(malloc (size
));
625 return(realloc (s
, size
));
638 while ((c
= bufstream_getc(bp
)) != EOF
)
642 BUSH_INPUT bush_input
;
644 struct stat dsb
; /* can be used from gdb */
646 /* imitate /bin/cat */
655 bp
= fd_to_buffered_stream (0);
659 for (i
= 1; i
< argc
; i
++) {
660 if (argv
[i
][0] == '-' && argv
[i
][1] == '\0') {
661 bp
= fd_to_buffered_stream (0);
665 free_buffered_stream (bp
);
667 bp
= open_buffered_stream (argv
[i
]);
671 close_buffered_stream (bp
);
677 #endif /* BUFFERED_INPUT */