]> git.ipfire.org Git - thirdparty/bash.git/blame - input.c
Initial devel branch import from bash-3.0-alpha
[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"
d3a24ed2 24#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
cce855bc
JA
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. */
d3a24ed2 237 internal_error ("save_bash_input: buffer already exists for new fd %d", nfd);
28ef6c31
JA
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{
d3a24ed2
CR
279 if (fd_is_bash_input (fd))
280 {
281 if (fd > 0)
282 return ((save_bash_input (fd, -1) == -1) ? -1 : 0);
283 else if (fd == 0)
284 return ((sync_buffered_stream (fd) == -1) ? -1 : 0);
285 }
726f6388
JA
286 return 0;
287}
288
289/* This is the buffered stream analogue of dup2(fd1, fd2). The
290 BUFFERED_STREAM corresponding to fd2 is deallocated, if one exists.
291 BUFFERS[fd1] is copied to BUFFERS[fd2]. This is called by the
292 redirect code for constructs like 4<&0 and 3</etc/rc.local. */
ccc6cda3 293int
726f6388
JA
294duplicate_buffered_stream (fd1, fd2)
295 int fd1, fd2;
296{
297 int is_bash_input, m;
298
299 if (fd1 == fd2)
300 return 0;
301
302 m = max (fd1, fd2);
303 ALLOCATE_BUFFERS (m);
304
305 /* If FD2 is the file descriptor bash is currently using for shell input,
306 we need to do some extra work to make sure that the buffered stream
307 actually exists (it might not if fd1 was not active, and the copy
308 didn't actually do anything). */
309 is_bash_input = (bash_input.type == st_bstream) &&
310 (bash_input.location.buffered_fd == fd2);
311
312 if (buffers[fd2])
313 free_buffered_stream (buffers[fd2]);
314 buffers[fd2] = copy_buffered_stream (buffers[fd1]);
315 if (buffers[fd2])
316 buffers[fd2]->b_fd = fd2;
317
28ef6c31
JA
318 if (is_bash_input)
319 {
320 if (!buffers[fd2])
321 fd_to_buffered_stream (fd2);
322 buffers[fd2]->b_flag |= B_WASBASHINPUT;
323 }
ccc6cda3 324
726f6388
JA
325 return (fd2);
326}
327
328/* Return 1 if a seek on FD will succeed. */
28ef6c31 329#ifndef __CYGWIN__
cce855bc
JA
330# define fd_is_seekable(fd) (lseek ((fd), 0L, SEEK_CUR) >= 0)
331#else
332# define fd_is_seekable(fd) 0
28ef6c31 333#endif /* __CYGWIN__ */
726f6388
JA
334
335/* Take FD, a file descriptor, and create and return a buffered stream
336 corresponding to it. If something is wrong and the file descriptor
337 is invalid, return a NULL stream. */
338BUFFERED_STREAM *
339fd_to_buffered_stream (fd)
340 int fd;
341{
342 char *buffer;
cce855bc 343 size_t size;
726f6388
JA
344 struct stat sb;
345
346 if (fstat (fd, &sb) < 0)
347 {
348 close (fd);
349 return ((BUFFERED_STREAM *)NULL);
350 }
351
28ef6c31
JA
352 size = (fd_is_seekable (fd)) ? min (sb.st_size, MAX_INPUT_BUFFER_SIZE) : 1;
353 if (size == 0)
726f6388 354 size = 1;
726f6388
JA
355 buffer = (char *)xmalloc (size);
356
357 return (make_buffered_stream (fd, buffer, size));
358}
359
360/* Return a buffered stream corresponding to FILE, a file name. */
361BUFFERED_STREAM *
362open_buffered_stream (file)
363 char *file;
364{
365 int fd;
366
367 fd = open (file, O_RDONLY);
ccc6cda3 368 return ((fd >= 0) ? fd_to_buffered_stream (fd) : (BUFFERED_STREAM *)NULL);
726f6388
JA
369}
370
371/* Deallocate a buffered stream and free up its resources. Make sure we
372 zero out the slot in BUFFERS that points to BP. */
373void
374free_buffered_stream (bp)
375 BUFFERED_STREAM *bp;
376{
377 int n;
378
379 if (!bp)
380 return;
381
382 n = bp->b_fd;
383 if (bp->b_buffer)
384 free (bp->b_buffer);
385 free (bp);
386 buffers[n] = (BUFFERED_STREAM *)NULL;
387}
388
389/* Close the file descriptor associated with BP, a buffered stream, and free
390 up the stream. Return the status of closing BP's file descriptor. */
391int
392close_buffered_stream (bp)
393 BUFFERED_STREAM *bp;
394{
395 int fd;
396
397 if (!bp)
398 return (0);
399 fd = bp->b_fd;
400 free_buffered_stream (bp);
401 return (close (fd));
402}
403
404/* Deallocate the buffered stream associated with file descriptor FD, and
405 close FD. Return the status of the close on FD. */
406int
407close_buffered_fd (fd)
408 int fd;
409{
f73dda09
JA
410 if (fd < 0)
411 {
412 errno = EBADF;
413 return -1;
414 }
726f6388
JA
415 if (fd >= nbuffers || !buffers || !buffers[fd])
416 return (close (fd));
417 return (close_buffered_stream (buffers[fd]));
418}
419
cce855bc
JA
420/* Make the BUFFERED_STREAM associcated with buffers[FD] be BP, and return
421 the old BUFFERED_STREAM. */
422BUFFERED_STREAM *
423set_buffered_stream (fd, bp)
424 int fd;
425 BUFFERED_STREAM *bp;
426{
427 BUFFERED_STREAM *ret;
428
429 ret = buffers[fd];
430 buffers[fd] = bp;
431 return ret;
432}
433
726f6388
JA
434/* Read a buffer full of characters from BP, a buffered stream. */
435static int
436b_fill_buffer (bp)
437 BUFFERED_STREAM *bp;
438{
f73dda09
JA
439 ssize_t nr;
440
441 nr = zread (bp->b_fd, bp->b_buffer, bp->b_size);
442 if (nr <= 0)
726f6388 443 {
f73dda09 444 bp->b_used = 0;
726f6388 445 bp->b_buffer[0] = 0;
f73dda09 446 if (nr == 0)
726f6388
JA
447 bp->b_flag |= B_EOF;
448 else
449 bp->b_flag |= B_ERROR;
450 return (EOF);
451 }
f73dda09
JA
452
453#if defined (__CYGWIN__)
454 /* If on cygwin, translate \r\n to \n. */
455 if (nr >= 2 && bp->b_buffer[nr - 2] == '\r' && bp->b_buffer[nr - 1] == '\n')
456 {
457 bp->b_buffer[nr - 2] = '\n';
458 nr--;
459 }
460#endif
461
462 bp->b_used = nr;
726f6388
JA
463 bp->b_inputp = 0;
464 return (bp->b_buffer[bp->b_inputp++] & 0xFF);
465}
466
467/* Get a character from buffered stream BP. */
468#define bufstream_getc(bp) \
469 (bp->b_inputp == bp->b_used || !bp->b_used) \
470 ? b_fill_buffer (bp) \
471 : bp->b_buffer[bp->b_inputp++] & 0xFF
472
473/* Push C back onto buffered stream BP. */
474static int
475bufstream_ungetc(c, bp)
476 int c;
477 BUFFERED_STREAM *bp;
478{
479 if (c == EOF || bp->b_inputp == 0)
480 return (EOF);
481
482 bp->b_buffer[--bp->b_inputp] = c;
483 return (c);
484}
485
486/* Seek backwards on file BFD to synchronize what we've read so far
487 with the underlying file pointer. */
488int
489sync_buffered_stream (bfd)
490 int bfd;
491{
492 BUFFERED_STREAM *bp;
ccc6cda3 493 off_t chars_left;
726f6388 494
28ef6c31 495 if (buffers == 0 || (bp = buffers[bfd]) == 0)
726f6388 496 return (-1);
28ef6c31 497
726f6388
JA
498 chars_left = bp->b_used - bp->b_inputp;
499 if (chars_left)
500 lseek (bp->b_fd, -chars_left, SEEK_CUR);
501 bp->b_used = bp->b_inputp = 0;
502 return (0);
503}
504
505int
506buffered_getchar ()
507{
28ef6c31 508#if !defined (DJGPP)
726f6388 509 return (bufstream_getc (buffers[bash_input.location.buffered_fd]));
28ef6c31
JA
510#else
511 /* On DJGPP, ignore \r. */
512 int ch;
513 while ((ch = bufstream_getc (buffers[bash_input.location.buffered_fd])) == '\r')
514 ;
515 return ch;
516#endif
726f6388
JA
517}
518
519int
520buffered_ungetchar (c)
521 int c;
522{
523 return (bufstream_ungetc (c, buffers[bash_input.location.buffered_fd]));
524}
525
526/* Make input come from file descriptor BFD through a buffered stream. */
527void
528with_input_from_buffered_stream (bfd, name)
529 int bfd;
530 char *name;
531{
532 INPUT_STREAM location;
ccc6cda3 533 BUFFERED_STREAM *bp;
726f6388
JA
534
535 location.buffered_fd = bfd;
536 /* Make sure the buffered stream exists. */
ccc6cda3
JA
537 bp = fd_to_buffered_stream (bfd);
538 init_yy_io (bp == 0 ? return_EOF : buffered_getchar,
539 buffered_ungetchar, st_bstream, name, location);
726f6388
JA
540}
541
542#if defined (TEST)
f73dda09 543void *
726f6388
JA
544xmalloc(s)
545int s;
546{
f73dda09 547 return (malloc (s));
726f6388
JA
548}
549
f73dda09 550void *
726f6388
JA
551xrealloc(s, size)
552char *s;
553int size;
554{
555 if (!s)
f73dda09 556 return(malloc (size));
726f6388 557 else
f73dda09 558 return(realloc (s, size));
726f6388
JA
559}
560
561void
562init_yy_io ()
563{
564}
565
566process(bp)
567BUFFERED_STREAM *bp;
568{
569 int c;
570
571 while ((c = bufstream_getc(bp)) != EOF)
572 putchar(c);
573}
574
575BASH_INPUT bash_input;
576
577struct stat dsb; /* can be used from gdb */
578
579/* imitate /bin/cat */
580main(argc, argv)
581int argc;
582char **argv;
583{
584 register int i;
585 BUFFERED_STREAM *bp;
586
587 if (argc == 1) {
588 bp = fd_to_buffered_stream (0);
589 process(bp);
590 exit(0);
591 }
592 for (i = 1; i < argc; i++) {
593 if (argv[i][0] == '-' && argv[i][1] == '\0') {
594 bp = fd_to_buffered_stream (0);
595 if (!bp)
596 continue;
597 process(bp);
598 free_buffered_stream (bp);
599 } else {
600 bp = open_buffered_stream (argv[i]);
601 if (!bp)
602 continue;
603 process(bp);
604 close_buffered_stream (bp);
605 }
606 }
607 exit(0);
608}
ccc6cda3
JA
609#endif /* TEST */
610#endif /* BUFFERED_INPUT */