]> git.ipfire.org Git - thirdparty/bash.git/blame - input.c
Imported from ../bash-2.05b.tar.gz.
[thirdparty/bash.git] / input.c
CommitLineData
726f6388
JA
1/* input.c -- functions to perform buffered input with synchronization. */
2
3/* Copyright (C) 1992 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
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
10 version.
11
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
15 for more details.
16
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
bb70624e 19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
726f6388 20
ccc6cda3 21#include "config.h"
726f6388
JA
22
23#include "bashtypes.h"
cce855bc
JA
24#ifndef _MINIX
25# include <sys/file.h>
26#endif
726f6388
JA
27#include "filecntl.h"
28#include "posixstat.h"
29#include <stdio.h>
30#include <errno.h>
31
ccc6cda3
JA
32#if defined (HAVE_UNISTD_H)
33# include <unistd.h>
34#endif
35
726f6388 36#include "bashansi.h"
726f6388
JA
37#include "command.h"
38#include "general.h"
39#include "input.h"
ccc6cda3
JA
40#include "error.h"
41#include "externs.h"
726f6388
JA
42
43#if !defined (errno)
44extern int errno;
45#endif /* !errno */
46
ccc6cda3
JA
47/* Functions to handle reading input on systems that don't restart read(2)
48 if a signal is received. */
49
f73dda09 50static char localbuf[128];
ccc6cda3
JA
51static int local_index, local_bufused;
52
53/* Posix and USG systems do not guarantee to restart read () if it is
54 interrupted by a signal. We do the read ourselves, and restart it
55 if it returns EINTR. */
56int
57getc_with_restart (stream)
58 FILE *stream;
59{
f73dda09
JA
60 unsigned char uc;
61
ccc6cda3
JA
62 /* Try local buffering to reduce the number of read(2) calls. */
63 if (local_index == local_bufused || local_bufused == 0)
64 {
65 while (1)
66 {
67 local_bufused = read (fileno (stream), localbuf, sizeof(localbuf));
68 if (local_bufused > 0)
69 break;
70 else if (local_bufused == 0 || errno != EINTR)
71 {
72 local_index = 0;
73 return EOF;
74 }
75 }
76 local_index = 0;
77 }
f73dda09
JA
78 uc = localbuf[local_index++];
79 return uc;
ccc6cda3
JA
80}
81
82int
83ungetc_with_restart (c, stream)
84 int c;
85 FILE *stream;
86{
87 if (local_index == 0 || c == EOF)
88 return EOF;
f73dda09
JA
89 localbuf[--local_index] = c;
90 return c;
ccc6cda3 91}
ccc6cda3
JA
92
93#if defined (BUFFERED_INPUT)
94
95/* A facility similar to stdio, but input-only. */
96
bb70624e
JA
97#if defined (USING_BASH_MALLOC)
98# define MAX_INPUT_BUFFER_SIZE 8176
99#else
100# define MAX_INPUT_BUFFER_SIZE 8192
101#endif
726f6388
JA
102
103#if !defined (SEEK_CUR)
104# define SEEK_CUR 1
105#endif /* !SEEK_CUR */
106
28ef6c31
JA
107#ifdef max
108# undef max
109#endif
110#define max(a, b) (((a) > (b)) ? (a) : (b))
111#ifdef min
112# undef min
113#endif
114#define min(a, b) ((a) > (b) ? (b) : (a))
115
726f6388
JA
116extern int interactive_shell;
117
118int bash_input_fd_changed;
ccc6cda3 119
726f6388
JA
120/* This provides a way to map from a file descriptor to the buffer
121 associated with that file descriptor, rather than just the other
122 way around. This is needed so that buffers are managed properly
123 in constructs like 3<&4. buffers[x]->b_fd == x -- that is how the
124 correspondence is maintained. */
cce855bc 125static BUFFERED_STREAM **buffers = (BUFFERED_STREAM **)NULL;
ccc6cda3 126static int nbuffers;
726f6388 127
726f6388
JA
128#define ALLOCATE_BUFFERS(n) \
129 do { if ((n) >= nbuffers) allocate_buffers (n); } while (0)
130
131/* Make sure `buffers' has at least N elements. */
132static void
133allocate_buffers (n)
134 int n;
135{
136 register int i, orig_nbuffers;
137
138 orig_nbuffers = nbuffers;
139 nbuffers = n + 20;
140 buffers = (BUFFERED_STREAM **)xrealloc
141 (buffers, nbuffers * sizeof (BUFFERED_STREAM *));
142
143 /* Zero out the new buffers. */
144 for (i = orig_nbuffers; i < nbuffers; i++)
145 buffers[i] = (BUFFERED_STREAM *)NULL;
146}
147
148/* Construct and return a BUFFERED_STREAM corresponding to file descriptor
149 FD, using BUFFER. */
150static BUFFERED_STREAM *
151make_buffered_stream (fd, buffer, bufsize)
152 int fd;
153 char *buffer;
cce855bc 154 size_t bufsize;
726f6388
JA
155{
156 BUFFERED_STREAM *bp;
157
158 bp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
159 ALLOCATE_BUFFERS (fd);
160 buffers[fd] = bp;
161 bp->b_fd = fd;
162 bp->b_buffer = buffer;
163 bp->b_size = bufsize;
ccc6cda3 164 bp->b_used = bp->b_inputp = bp->b_flag = 0;
726f6388
JA
165 if (bufsize == 1)
166 bp->b_flag |= B_UNBUFF;
167 return (bp);
168}
169
170/* Allocate a new BUFFERED_STREAM, copy BP to it, and return the new copy. */
171static BUFFERED_STREAM *
172copy_buffered_stream (bp)
173 BUFFERED_STREAM *bp;
174{
175 BUFFERED_STREAM *nbp;
176
177 if (!bp)
178 return ((BUFFERED_STREAM *)NULL);
179
180 nbp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
181 xbcopy ((char *)bp, (char *)nbp, sizeof (BUFFERED_STREAM));
182 return (nbp);
183}
184
28ef6c31
JA
185int
186set_bash_input_fd (fd)
187 int fd;
188{
189 if (bash_input.type == st_bstream)
190 bash_input.location.buffered_fd = fd;
191 else if (interactive_shell == 0)
192 default_buffered_input = fd;
193 return 0;
194}
195
196int
197fd_is_bash_input (fd)
198 int fd;
199{
200 if (bash_input.type == st_bstream && bash_input.location.buffered_fd == fd)
201 return 1;
202 else if (interactive_shell == 0 && default_buffered_input == fd)
203 return 1;
204 return 0;
205}
206
207/* Save the buffered stream corresponding to file descriptor FD (which bash
208 is using to read input) to a buffered stream associated with NEW_FD. If
209 NEW_FD is -1, a new file descriptor is allocated with fcntl. The new
210 file descriptor is returned on success, -1 on error. */
211int
212save_bash_input (fd, new_fd)
213 int fd, new_fd;
214{
215 int nfd;
216
217 /* Sync the stream so we can re-read from the new file descriptor. We
218 might be able to avoid this by copying the buffered stream verbatim
219 to the new file descriptor. */
220 if (buffers[fd])
221 sync_buffered_stream (fd);
222
223 /* Now take care of duplicating the file descriptor that bash is
224 using for input, so we can reinitialize it later. */
225 nfd = (new_fd == -1) ? fcntl (fd, F_DUPFD, 10) : new_fd;
226 if (nfd == -1)
227 {
228 if (fcntl (fd, F_GETFD, 0) == 0)
229 sys_error ("cannot allocate new file descriptor for bash input from fd %d", fd);
230 return -1;
231 }
232
233 if (buffers[nfd])
234 {
235 /* What's this? A stray buffer without an associated open file
236 descriptor? Free up the buffer and report the error. */
237 internal_error ("check_bash_input: buffer already exists for new fd %d", nfd);
238 free_buffered_stream (buffers[nfd]);
239 }
240
241 /* Reinitialize bash_input.location. */
242 if (bash_input.type == st_bstream)
243 {
244 bash_input.location.buffered_fd = nfd;
245 fd_to_buffered_stream (nfd);
246 close_buffered_fd (fd); /* XXX */
247 }
248 else
249 /* If the current input type is not a buffered stream, but the shell
250 is not interactive and therefore using a buffered stream to read
251 input (e.g. with an `eval exec 3>output' inside a script), note
252 that the input fd has been changed. pop_stream() looks at this
253 value and adjusts the input fd to the new value of
254 default_buffered_input accordingly. */
255 bash_input_fd_changed++;
256
257 if (default_buffered_input == fd)
258 default_buffered_input = nfd;
259
260 SET_CLOSE_ON_EXEC (nfd);
261 return nfd;
262}
263
726f6388
JA
264/* Check that file descriptor FD is not the one that bash is currently
265 using to read input from a script. FD is about to be duplicated onto,
266 which means that the kernel will close it for us. If FD is the bash
267 input file descriptor, we need to seek backwards in the script (if
268 possible and necessary -- scripts read from stdin are still unbuffered),
269 allocate a new file descriptor to use for bash input, and re-initialize
28ef6c31
JA
270 the buffered stream. Make sure the file descriptor used to save bash
271 input is set close-on-exec. Returns 0 on success, -1 on failure. This
272 works only if fd is > 0 -- if fd == 0 and bash is reading input from
273 fd 0, save_bash_input is used instead, to cooperate with input
274 redirection (look at redir.c:add_undo_redirect()). */
726f6388
JA
275int
276check_bash_input (fd)
277 int fd;
278{
28ef6c31
JA
279 if (fd > 0 && fd_is_bash_input (fd))
280 return ((save_bash_input (fd, -1) == -1) ? -1 : 0);
726f6388
JA
281 return 0;
282}
283
284/* This is the buffered stream analogue of dup2(fd1, fd2). The
285 BUFFERED_STREAM corresponding to fd2 is deallocated, if one exists.
286 BUFFERS[fd1] is copied to BUFFERS[fd2]. This is called by the
287 redirect code for constructs like 4<&0 and 3</etc/rc.local. */
ccc6cda3 288int
726f6388
JA
289duplicate_buffered_stream (fd1, fd2)
290 int fd1, fd2;
291{
292 int is_bash_input, m;
293
294 if (fd1 == fd2)
295 return 0;
296
297 m = max (fd1, fd2);
298 ALLOCATE_BUFFERS (m);
299
300 /* If FD2 is the file descriptor bash is currently using for shell input,
301 we need to do some extra work to make sure that the buffered stream
302 actually exists (it might not if fd1 was not active, and the copy
303 didn't actually do anything). */
304 is_bash_input = (bash_input.type == st_bstream) &&
305 (bash_input.location.buffered_fd == fd2);
306
307 if (buffers[fd2])
308 free_buffered_stream (buffers[fd2]);
309 buffers[fd2] = copy_buffered_stream (buffers[fd1]);
310 if (buffers[fd2])
311 buffers[fd2]->b_fd = fd2;
312
28ef6c31
JA
313 if (is_bash_input)
314 {
315 if (!buffers[fd2])
316 fd_to_buffered_stream (fd2);
317 buffers[fd2]->b_flag |= B_WASBASHINPUT;
318 }
ccc6cda3 319
726f6388
JA
320 return (fd2);
321}
322
323/* Return 1 if a seek on FD will succeed. */
28ef6c31 324#ifndef __CYGWIN__
cce855bc
JA
325# define fd_is_seekable(fd) (lseek ((fd), 0L, SEEK_CUR) >= 0)
326#else
327# define fd_is_seekable(fd) 0
28ef6c31 328#endif /* __CYGWIN__ */
726f6388
JA
329
330/* Take FD, a file descriptor, and create and return a buffered stream
331 corresponding to it. If something is wrong and the file descriptor
332 is invalid, return a NULL stream. */
333BUFFERED_STREAM *
334fd_to_buffered_stream (fd)
335 int fd;
336{
337 char *buffer;
cce855bc 338 size_t size;
726f6388
JA
339 struct stat sb;
340
341 if (fstat (fd, &sb) < 0)
342 {
343 close (fd);
344 return ((BUFFERED_STREAM *)NULL);
345 }
346
28ef6c31
JA
347 size = (fd_is_seekable (fd)) ? min (sb.st_size, MAX_INPUT_BUFFER_SIZE) : 1;
348 if (size == 0)
726f6388 349 size = 1;
726f6388
JA
350 buffer = (char *)xmalloc (size);
351
352 return (make_buffered_stream (fd, buffer, size));
353}
354
355/* Return a buffered stream corresponding to FILE, a file name. */
356BUFFERED_STREAM *
357open_buffered_stream (file)
358 char *file;
359{
360 int fd;
361
362 fd = open (file, O_RDONLY);
ccc6cda3 363 return ((fd >= 0) ? fd_to_buffered_stream (fd) : (BUFFERED_STREAM *)NULL);
726f6388
JA
364}
365
366/* Deallocate a buffered stream and free up its resources. Make sure we
367 zero out the slot in BUFFERS that points to BP. */
368void
369free_buffered_stream (bp)
370 BUFFERED_STREAM *bp;
371{
372 int n;
373
374 if (!bp)
375 return;
376
377 n = bp->b_fd;
378 if (bp->b_buffer)
379 free (bp->b_buffer);
380 free (bp);
381 buffers[n] = (BUFFERED_STREAM *)NULL;
382}
383
384/* Close the file descriptor associated with BP, a buffered stream, and free
385 up the stream. Return the status of closing BP's file descriptor. */
386int
387close_buffered_stream (bp)
388 BUFFERED_STREAM *bp;
389{
390 int fd;
391
392 if (!bp)
393 return (0);
394 fd = bp->b_fd;
395 free_buffered_stream (bp);
396 return (close (fd));
397}
398
399/* Deallocate the buffered stream associated with file descriptor FD, and
400 close FD. Return the status of the close on FD. */
401int
402close_buffered_fd (fd)
403 int fd;
404{
f73dda09
JA
405 if (fd < 0)
406 {
407 errno = EBADF;
408 return -1;
409 }
726f6388
JA
410 if (fd >= nbuffers || !buffers || !buffers[fd])
411 return (close (fd));
412 return (close_buffered_stream (buffers[fd]));
413}
414
cce855bc
JA
415/* Make the BUFFERED_STREAM associcated with buffers[FD] be BP, and return
416 the old BUFFERED_STREAM. */
417BUFFERED_STREAM *
418set_buffered_stream (fd, bp)
419 int fd;
420 BUFFERED_STREAM *bp;
421{
422 BUFFERED_STREAM *ret;
423
424 ret = buffers[fd];
425 buffers[fd] = bp;
426 return ret;
427}
428
726f6388
JA
429/* Read a buffer full of characters from BP, a buffered stream. */
430static int
431b_fill_buffer (bp)
432 BUFFERED_STREAM *bp;
433{
f73dda09
JA
434 ssize_t nr;
435
436 nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
437 if (nr <= 0)
726f6388 438 {
f73dda09 439 bp->b_used = 0;
726f6388 440 bp->b_buffer[0] = 0;
f73dda09 441 if (nr == 0)
726f6388
JA
442 bp->b_flag |= B_EOF;
443 else
444 bp->b_flag |= B_ERROR;
445 return (EOF);
446 }
f73dda09
JA
447
448#if defined (__CYGWIN__)
449 /* If on cygwin, translate \r\n to \n. */
450 if (nr >= 2 && bp->b_buffer[nr - 2] == '\r' && bp->b_buffer[nr - 1] == '\n')
451 {
452 bp->b_buffer[nr - 2] = '\n';
453 nr--;
454 }
455#endif
456
457 bp->b_used = nr;
726f6388
JA
458 bp->b_inputp = 0;
459 return (bp->b_buffer[bp->b_inputp++] & 0xFF);
460}
461
462/* Get a character from buffered stream BP. */
463#define bufstream_getc(bp) \
464 (bp->b_inputp == bp->b_used || !bp->b_used) \
465 ? b_fill_buffer (bp) \
466 : bp->b_buffer[bp->b_inputp++] & 0xFF
467
468/* Push C back onto buffered stream BP. */
469static int
470bufstream_ungetc(c, bp)
471 int c;
472 BUFFERED_STREAM *bp;
473{
474 if (c == EOF || bp->b_inputp == 0)
475 return (EOF);
476
477 bp->b_buffer[--bp->b_inputp] = c;
478 return (c);
479}
480
481/* Seek backwards on file BFD to synchronize what we've read so far
482 with the underlying file pointer. */
483int
484sync_buffered_stream (bfd)
485 int bfd;
486{
487 BUFFERED_STREAM *bp;
ccc6cda3 488 off_t chars_left;
726f6388 489
28ef6c31 490 if (buffers == 0 || (bp = buffers[bfd]) == 0)
726f6388 491 return (-1);
28ef6c31 492
726f6388
JA
493 chars_left = bp->b_used - bp->b_inputp;
494 if (chars_left)
495 lseek (bp->b_fd, -chars_left, SEEK_CUR);
496 bp->b_used = bp->b_inputp = 0;
497 return (0);
498}
499
500int
501buffered_getchar ()
502{
28ef6c31 503#if !defined (DJGPP)
726f6388 504 return (bufstream_getc (buffers[bash_input.location.buffered_fd]));
28ef6c31
JA
505#else
506 /* On DJGPP, ignore \r. */
507 int ch;
508 while ((ch = bufstream_getc (buffers[bash_input.location.buffered_fd])) == '\r')
509 ;
510 return ch;
511#endif
726f6388
JA
512}
513
514int
515buffered_ungetchar (c)
516 int c;
517{
518 return (bufstream_ungetc (c, buffers[bash_input.location.buffered_fd]));
519}
520
521/* Make input come from file descriptor BFD through a buffered stream. */
522void
523with_input_from_buffered_stream (bfd, name)
524 int bfd;
525 char *name;
526{
527 INPUT_STREAM location;
ccc6cda3 528 BUFFERED_STREAM *bp;
726f6388
JA
529
530 location.buffered_fd = bfd;
531 /* Make sure the buffered stream exists. */
ccc6cda3
JA
532 bp = fd_to_buffered_stream (bfd);
533 init_yy_io (bp == 0 ? return_EOF : buffered_getchar,
534 buffered_ungetchar, st_bstream, name, location);
726f6388
JA
535}
536
537#if defined (TEST)
f73dda09 538void *
726f6388
JA
539xmalloc(s)
540int s;
541{
f73dda09 542 return (malloc (s));
726f6388
JA
543}
544
f73dda09 545void *
726f6388
JA
546xrealloc(s, size)
547char *s;
548int size;
549{
550 if (!s)
f73dda09 551 return(malloc (size));
726f6388 552 else
f73dda09 553 return(realloc (s, size));
726f6388
JA
554}
555
556void
557init_yy_io ()
558{
559}
560
561process(bp)
562BUFFERED_STREAM *bp;
563{
564 int c;
565
566 while ((c = bufstream_getc(bp)) != EOF)
567 putchar(c);
568}
569
570BASH_INPUT bash_input;
571
572struct stat dsb; /* can be used from gdb */
573
574/* imitate /bin/cat */
575main(argc, argv)
576int argc;
577char **argv;
578{
579 register int i;
580 BUFFERED_STREAM *bp;
581
582 if (argc == 1) {
583 bp = fd_to_buffered_stream (0);
584 process(bp);
585 exit(0);
586 }
587 for (i = 1; i < argc; i++) {
588 if (argv[i][0] == '-' && argv[i][1] == '\0') {
589 bp = fd_to_buffered_stream (0);
590 if (!bp)
591 continue;
592 process(bp);
593 free_buffered_stream (bp);
594 } else {
595 bp = open_buffered_stream (argv[i]);
596 if (!bp)
597 continue;
598 process(bp);
599 close_buffered_stream (bp);
600 }
601 }
602 exit(0);
603}
ccc6cda3
JA
604#endif /* TEST */
605#endif /* BUFFERED_INPUT */