]> git.ipfire.org Git - thirdparty/bash.git/blame - input.c
Imported from ../bash-2.05.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
ccc6cda3
JA
50static unsigned char localbuf[128];
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{
60 /* Try local buffering to reduce the number of read(2) calls. */
61 if (local_index == local_bufused || local_bufused == 0)
62 {
63 while (1)
64 {
65 local_bufused = read (fileno (stream), localbuf, sizeof(localbuf));
66 if (local_bufused > 0)
67 break;
68 else if (local_bufused == 0 || errno != EINTR)
69 {
70 local_index = 0;
71 return EOF;
72 }
73 }
74 local_index = 0;
75 }
76 return (localbuf[local_index++]);
77}
78
79int
80ungetc_with_restart (c, stream)
81 int c;
82 FILE *stream;
83{
84 if (local_index == 0 || c == EOF)
85 return EOF;
86 return (localbuf[--local_index] = c);
87}
ccc6cda3
JA
88
89#if defined (BUFFERED_INPUT)
90
91/* A facility similar to stdio, but input-only. */
92
bb70624e
JA
93#if defined (USING_BASH_MALLOC)
94# define MAX_INPUT_BUFFER_SIZE 8176
95#else
96# define MAX_INPUT_BUFFER_SIZE 8192
97#endif
726f6388
JA
98
99#if !defined (SEEK_CUR)
100# define SEEK_CUR 1
101#endif /* !SEEK_CUR */
102
28ef6c31
JA
103#ifdef max
104# undef max
105#endif
106#define max(a, b) (((a) > (b)) ? (a) : (b))
107#ifdef min
108# undef min
109#endif
110#define min(a, b) ((a) > (b) ? (b) : (a))
111
ccc6cda3
JA
112extern int return_EOF ();
113
726f6388
JA
114extern int interactive_shell;
115
116int bash_input_fd_changed;
ccc6cda3 117
726f6388
JA
118/* This provides a way to map from a file descriptor to the buffer
119 associated with that file descriptor, rather than just the other
120 way around. This is needed so that buffers are managed properly
121 in constructs like 3<&4. buffers[x]->b_fd == x -- that is how the
122 correspondence is maintained. */
cce855bc 123static BUFFERED_STREAM **buffers = (BUFFERED_STREAM **)NULL;
ccc6cda3 124static int nbuffers;
726f6388 125
726f6388
JA
126#define ALLOCATE_BUFFERS(n) \
127 do { if ((n) >= nbuffers) allocate_buffers (n); } while (0)
128
129/* Make sure `buffers' has at least N elements. */
130static void
131allocate_buffers (n)
132 int n;
133{
134 register int i, orig_nbuffers;
135
136 orig_nbuffers = nbuffers;
137 nbuffers = n + 20;
138 buffers = (BUFFERED_STREAM **)xrealloc
139 (buffers, nbuffers * sizeof (BUFFERED_STREAM *));
140
141 /* Zero out the new buffers. */
142 for (i = orig_nbuffers; i < nbuffers; i++)
143 buffers[i] = (BUFFERED_STREAM *)NULL;
144}
145
146/* Construct and return a BUFFERED_STREAM corresponding to file descriptor
147 FD, using BUFFER. */
148static BUFFERED_STREAM *
149make_buffered_stream (fd, buffer, bufsize)
150 int fd;
151 char *buffer;
cce855bc 152 size_t bufsize;
726f6388
JA
153{
154 BUFFERED_STREAM *bp;
155
156 bp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
157 ALLOCATE_BUFFERS (fd);
158 buffers[fd] = bp;
159 bp->b_fd = fd;
160 bp->b_buffer = buffer;
161 bp->b_size = bufsize;
ccc6cda3 162 bp->b_used = bp->b_inputp = bp->b_flag = 0;
726f6388
JA
163 if (bufsize == 1)
164 bp->b_flag |= B_UNBUFF;
165 return (bp);
166}
167
168/* Allocate a new BUFFERED_STREAM, copy BP to it, and return the new copy. */
169static BUFFERED_STREAM *
170copy_buffered_stream (bp)
171 BUFFERED_STREAM *bp;
172{
173 BUFFERED_STREAM *nbp;
174
175 if (!bp)
176 return ((BUFFERED_STREAM *)NULL);
177
178 nbp = (BUFFERED_STREAM *)xmalloc (sizeof (BUFFERED_STREAM));
179 xbcopy ((char *)bp, (char *)nbp, sizeof (BUFFERED_STREAM));
180 return (nbp);
181}
182
28ef6c31
JA
183int
184set_bash_input_fd (fd)
185 int fd;
186{
187 if (bash_input.type == st_bstream)
188 bash_input.location.buffered_fd = fd;
189 else if (interactive_shell == 0)
190 default_buffered_input = fd;
191 return 0;
192}
193
194int
195fd_is_bash_input (fd)
196 int fd;
197{
198 if (bash_input.type == st_bstream && bash_input.location.buffered_fd == fd)
199 return 1;
200 else if (interactive_shell == 0 && default_buffered_input == fd)
201 return 1;
202 return 0;
203}
204
205/* Save the buffered stream corresponding to file descriptor FD (which bash
206 is using to read input) to a buffered stream associated with NEW_FD. If
207 NEW_FD is -1, a new file descriptor is allocated with fcntl. The new
208 file descriptor is returned on success, -1 on error. */
209int
210save_bash_input (fd, new_fd)
211 int fd, new_fd;
212{
213 int nfd;
214
215 /* Sync the stream so we can re-read from the new file descriptor. We
216 might be able to avoid this by copying the buffered stream verbatim
217 to the new file descriptor. */
218 if (buffers[fd])
219 sync_buffered_stream (fd);
220
221 /* Now take care of duplicating the file descriptor that bash is
222 using for input, so we can reinitialize it later. */
223 nfd = (new_fd == -1) ? fcntl (fd, F_DUPFD, 10) : new_fd;
224 if (nfd == -1)
225 {
226 if (fcntl (fd, F_GETFD, 0) == 0)
227 sys_error ("cannot allocate new file descriptor for bash input from fd %d", fd);
228 return -1;
229 }
230
231 if (buffers[nfd])
232 {
233 /* What's this? A stray buffer without an associated open file
234 descriptor? Free up the buffer and report the error. */
235 internal_error ("check_bash_input: buffer already exists for new fd %d", nfd);
236 free_buffered_stream (buffers[nfd]);
237 }
238
239 /* Reinitialize bash_input.location. */
240 if (bash_input.type == st_bstream)
241 {
242 bash_input.location.buffered_fd = nfd;
243 fd_to_buffered_stream (nfd);
244 close_buffered_fd (fd); /* XXX */
245 }
246 else
247 /* If the current input type is not a buffered stream, but the shell
248 is not interactive and therefore using a buffered stream to read
249 input (e.g. with an `eval exec 3>output' inside a script), note
250 that the input fd has been changed. pop_stream() looks at this
251 value and adjusts the input fd to the new value of
252 default_buffered_input accordingly. */
253 bash_input_fd_changed++;
254
255 if (default_buffered_input == fd)
256 default_buffered_input = nfd;
257
258 SET_CLOSE_ON_EXEC (nfd);
259 return nfd;
260}
261
726f6388
JA
262/* Check that file descriptor FD is not the one that bash is currently
263 using to read input from a script. FD is about to be duplicated onto,
264 which means that the kernel will close it for us. If FD is the bash
265 input file descriptor, we need to seek backwards in the script (if
266 possible and necessary -- scripts read from stdin are still unbuffered),
267 allocate a new file descriptor to use for bash input, and re-initialize
28ef6c31
JA
268 the buffered stream. Make sure the file descriptor used to save bash
269 input is set close-on-exec. Returns 0 on success, -1 on failure. This
270 works only if fd is > 0 -- if fd == 0 and bash is reading input from
271 fd 0, save_bash_input is used instead, to cooperate with input
272 redirection (look at redir.c:add_undo_redirect()). */
726f6388
JA
273int
274check_bash_input (fd)
275 int fd;
276{
277 int nfd;
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{
405 if (fd >= nbuffers || !buffers || !buffers[fd])
406 return (close (fd));
407 return (close_buffered_stream (buffers[fd]));
408}
409
cce855bc
JA
410/* Make the BUFFERED_STREAM associcated with buffers[FD] be BP, and return
411 the old BUFFERED_STREAM. */
412BUFFERED_STREAM *
413set_buffered_stream (fd, bp)
414 int fd;
415 BUFFERED_STREAM *bp;
416{
417 BUFFERED_STREAM *ret;
418
419 ret = buffers[fd];
420 buffers[fd] = bp;
421 return ret;
422}
423
726f6388
JA
424/* Read a buffer full of characters from BP, a buffered stream. */
425static int
426b_fill_buffer (bp)
427 BUFFERED_STREAM *bp;
428{
bb70624e 429 bp->b_used = zread (bp->b_fd, bp->b_buffer, bp->b_size);
28ef6c31
JA
430#if defined (__CYGWIN__)
431 /* If on cygwin, translate \r\n to \n. */
432 if (bp->b_buffer[bp->b_used - 1] == '\r' && bp->b_buffer[bp->b_used] == '\n')
433 bp->b_buffer[--bp->b_used] = '\n';
434#endif
726f6388
JA
435 if (bp->b_used <= 0)
436 {
437 bp->b_buffer[0] = 0;
438 if (bp->b_used == 0)
439 bp->b_flag |= B_EOF;
440 else
441 bp->b_flag |= B_ERROR;
442 return (EOF);
443 }
444 bp->b_inputp = 0;
445 return (bp->b_buffer[bp->b_inputp++] & 0xFF);
446}
447
448/* Get a character from buffered stream BP. */
449#define bufstream_getc(bp) \
450 (bp->b_inputp == bp->b_used || !bp->b_used) \
451 ? b_fill_buffer (bp) \
452 : bp->b_buffer[bp->b_inputp++] & 0xFF
453
454/* Push C back onto buffered stream BP. */
455static int
456bufstream_ungetc(c, bp)
457 int c;
458 BUFFERED_STREAM *bp;
459{
460 if (c == EOF || bp->b_inputp == 0)
461 return (EOF);
462
463 bp->b_buffer[--bp->b_inputp] = c;
464 return (c);
465}
466
467/* Seek backwards on file BFD to synchronize what we've read so far
468 with the underlying file pointer. */
469int
470sync_buffered_stream (bfd)
471 int bfd;
472{
473 BUFFERED_STREAM *bp;
ccc6cda3 474 off_t chars_left;
726f6388 475
28ef6c31 476 if (buffers == 0 || (bp = buffers[bfd]) == 0)
726f6388 477 return (-1);
28ef6c31 478
726f6388
JA
479 chars_left = bp->b_used - bp->b_inputp;
480 if (chars_left)
481 lseek (bp->b_fd, -chars_left, SEEK_CUR);
482 bp->b_used = bp->b_inputp = 0;
483 return (0);
484}
485
486int
487buffered_getchar ()
488{
28ef6c31 489#if !defined (DJGPP)
726f6388 490 return (bufstream_getc (buffers[bash_input.location.buffered_fd]));
28ef6c31
JA
491#else
492 /* On DJGPP, ignore \r. */
493 int ch;
494 while ((ch = bufstream_getc (buffers[bash_input.location.buffered_fd])) == '\r')
495 ;
496 return ch;
497#endif
726f6388
JA
498}
499
500int
501buffered_ungetchar (c)
502 int c;
503{
504 return (bufstream_ungetc (c, buffers[bash_input.location.buffered_fd]));
505}
506
507/* Make input come from file descriptor BFD through a buffered stream. */
508void
509with_input_from_buffered_stream (bfd, name)
510 int bfd;
511 char *name;
512{
513 INPUT_STREAM location;
ccc6cda3 514 BUFFERED_STREAM *bp;
726f6388
JA
515
516 location.buffered_fd = bfd;
517 /* Make sure the buffered stream exists. */
ccc6cda3
JA
518 bp = fd_to_buffered_stream (bfd);
519 init_yy_io (bp == 0 ? return_EOF : buffered_getchar,
520 buffered_ungetchar, st_bstream, name, location);
726f6388
JA
521}
522
523#if defined (TEST)
726f6388
JA
524char *
525xmalloc(s)
526int s;
527{
528 return ((char *)malloc (s));
529}
530
531char *
532xrealloc(s, size)
533char *s;
534int size;
535{
536 if (!s)
537 return((char *)malloc (size));
538 else
539 return((char *)realloc (s, size));
540}
541
542void
543init_yy_io ()
544{
545}
546
547process(bp)
548BUFFERED_STREAM *bp;
549{
550 int c;
551
552 while ((c = bufstream_getc(bp)) != EOF)
553 putchar(c);
554}
555
556BASH_INPUT bash_input;
557
558struct stat dsb; /* can be used from gdb */
559
560/* imitate /bin/cat */
561main(argc, argv)
562int argc;
563char **argv;
564{
565 register int i;
566 BUFFERED_STREAM *bp;
567
568 if (argc == 1) {
569 bp = fd_to_buffered_stream (0);
570 process(bp);
571 exit(0);
572 }
573 for (i = 1; i < argc; i++) {
574 if (argv[i][0] == '-' && argv[i][1] == '\0') {
575 bp = fd_to_buffered_stream (0);
576 if (!bp)
577 continue;
578 process(bp);
579 free_buffered_stream (bp);
580 } else {
581 bp = open_buffered_stream (argv[i]);
582 if (!bp)
583 continue;
584 process(bp);
585 close_buffered_stream (bp);
586 }
587 }
588 exit(0);
589}
ccc6cda3
JA
590#endif /* TEST */
591#endif /* BUFFERED_INPUT */