2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)input.c 8.3 (Berkeley) 6/9/95
37 * $FreeBSD: src/bin/sh/input.c,v 1.36 2012/07/11 22:17:58 pfg Exp $
40 #include <stdio.h> /* defines BUFSIZ */
48 * This file implements the input routines used by the parser.
61 #include "myhistedit.h"
64 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
68 struct strpush
*prev
; /* preceding string on stack */
72 struct alias
*ap
; /* if push was associated with an alias */
76 * The parsefile structure pointed to by the global variable parsefile
77 * contains information about the current file being read.
82 struct parsefile
*prev
; /* preceding file on stack */
83 int linno
; /* current line */
84 int fd
; /* file descriptor (or -1 if string) */
85 int nleft
; /* number of chars left in this line */
86 int lleft
; /* number of lines left in this buffer */
87 char *nextc
; /* next char in buffer */
88 char *buf
; /* input buffer */
89 struct strpush
*strpush
; /* for pushing strings at this level */
90 struct strpush basestrpush
; /* so pushing one is fast */
94 int plinno
= 1; /* input line number */
95 int parsenleft
; /* copy of parsefile->nleft */
96 MKINIT
int parselleft
; /* copy of parsefile->lleft */
97 char *parsenextc
; /* copy of parsefile->nextc */
98 MKINIT
struct parsefile basepf
; /* top level input file */
99 MKINIT
char basebuf
[BUFSIZ
+ 1]; /* buffer for top level input file */
100 static struct parsefile
*parsefile
= &basepf
; /* current input file */
101 int init_editline
= 0; /* editline library initialized? */
102 int whichprompt
; /* 1 == PS1, 2 == PS2 */
104 EditLine
*el
; /* cookie for editline package */
106 static void pushfile(void);
107 static int preadfd(void);
108 static void popstring(void);
116 basepf
.nextc
= basepf
.buf
= basebuf
;
121 parselleft
= parsenleft
= 0; /* clear input buffer */
127 * Read a line from the script.
131 pfgets(char *line
, int len
)
137 while (--nleft
> 0) {
155 * Read a character from the script, returning PEOF on end of file.
156 * Nul characters in the input are silently discarded.
162 return pgetc_macro();
170 parsenextc
= parsefile
->buf
;
173 if (el
!= NULL
&& gotwinch
) {
180 if (parsefile
->fd
== 0 && el
) {
181 static const char *rl_cp
;
185 rl_cp
= el_gets(el
, &el_len
);
187 nr
= el_len
== 0 ? 0 : -1;
192 memcpy(parsenextc
, rl_cp
, nr
);
201 nr
= read(parsefile
->fd
, parsenextc
, BUFSIZ
);
207 if (parsefile
->fd
== 0 && errno
== EWOULDBLOCK
) {
208 int flags
= fcntl(0, F_GETFL
, 0);
209 if (flags
>= 0 && flags
& O_NONBLOCK
) {
210 flags
&=~ O_NONBLOCK
;
211 if (fcntl(0, F_SETFL
, flags
) >= 0) {
212 out2fmt_flush("sh: turning off NDELAY mode\n");
224 * Refill the input buffer and return the next input character:
226 * 1) If a string was pushed back on the input, pop it;
227 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
228 * from a string so we can't refill the buffer, return EOF.
229 * 3) If there is more in this buffer, use it else call read to fill it.
230 * 4) Process input up to the next newline, deleting nul characters.
241 if (parsefile
->strpush
) {
243 if (--parsenleft
>= 0)
244 return (*parsenextc
++);
246 if (parsenleft
== EOF_NLEFT
|| parsefile
->buf
== NULL
)
252 if (parselleft
<= 0) {
253 if ((parselleft
= preadfd()) == -1) {
254 parselleft
= parsenleft
= EOF_NLEFT
;
261 /* delete nul characters */
263 for (more
= 1; more
;) {
274 parsenleft
= q
- parsenextc
;
275 more
= 0; /* Stop processing here */
285 if (--parselleft
<= 0) {
286 parsenleft
= q
- parsenextc
- 1;
298 if (parsefile
->fd
== 0 && hist
&& something
) {
301 history(hist
, &he
, whichprompt
== 1 ? H_ENTER
: H_ADD
,
314 return *parsenextc
++;
318 * Returns if we are certain we are at EOF. Does not cause any more input
319 * to be read from the outside world.
327 if (parsefile
->strpush
)
329 if (parsenleft
== EOF_NLEFT
|| parsefile
->buf
== NULL
)
335 * Undo the last call to pgetc. Only one character may be pushed back.
336 * PEOF may be pushed back.
347 * Push a string back onto the input at this current parsefile level.
348 * We handle aliases this way.
351 pushstring(char *s
, int len
, void *ap
)
356 /*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/
357 if (parsefile
->strpush
) {
358 sp
= ckmalloc(sizeof (struct strpush
));
359 sp
->prev
= parsefile
->strpush
;
360 parsefile
->strpush
= sp
;
362 sp
= parsefile
->strpush
= &(parsefile
->basestrpush
);
363 sp
->prevstring
= parsenextc
;
364 sp
->prevnleft
= parsenleft
;
365 sp
->prevlleft
= parselleft
;
366 sp
->ap
= (struct alias
*)ap
;
368 ((struct alias
*)ap
)->flag
|= ALIASINUSE
;
377 struct strpush
*sp
= parsefile
->strpush
;
380 parsenextc
= sp
->prevstring
;
381 parsenleft
= sp
->prevnleft
;
382 parselleft
= sp
->prevlleft
;
383 /*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/
385 sp
->ap
->flag
&= ~ALIASINUSE
;
386 parsefile
->strpush
= sp
->prev
;
387 if (sp
!= &(parsefile
->basestrpush
))
393 * Set the input to take input from a file. If push is set, push the
394 * old input onto the stack first.
398 setinputfile(const char *fname
, int push
)
404 if ((fd
= open(fname
, O_RDONLY
)) < 0)
405 error("cannot open %s: %s", fname
, strerror(errno
));
407 fd2
= fcntl(fd
, F_DUPFD
, 10);
410 error("Out of file descriptors");
413 setinputfd(fd
, push
);
419 * Like setinputfile, but takes an open file descriptor. Call this with
424 setinputfd(int fd
, int push
)
426 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
429 parsefile
->buf
= ckmalloc(BUFSIZ
+ 1);
431 if (parsefile
->fd
> 0)
432 close(parsefile
->fd
);
434 if (parsefile
->buf
== NULL
)
435 parsefile
->buf
= ckmalloc(BUFSIZ
+ 1);
436 parselleft
= parsenleft
= 0;
442 * Like setinputfile, but takes input from a string.
446 setinputstring(char *string
, int push
)
452 parselleft
= parsenleft
= strlen(string
);
453 parsefile
->buf
= NULL
;
461 * To handle the "." command, a stack of input files is used. Pushfile
462 * adds a new entry to the stack and popfile restores the previous level.
468 struct parsefile
*pf
;
470 parsefile
->nleft
= parsenleft
;
471 parsefile
->lleft
= parselleft
;
472 parsefile
->nextc
= parsenextc
;
473 parsefile
->linno
= plinno
;
474 pf
= (struct parsefile
*)ckmalloc(sizeof (struct parsefile
));
475 pf
->prev
= parsefile
;
478 pf
->basestrpush
.prev
= NULL
;
486 struct parsefile
*pf
= parsefile
;
495 parsefile
= pf
->prev
;
497 parsenleft
= parsefile
->nleft
;
498 parselleft
= parsefile
->lleft
;
499 parsenextc
= parsefile
->nextc
;
500 plinno
= parsefile
->linno
;
506 * Return current file (to go back to it later using popfilesupto()).
517 * Pop files until the given file is on top again. Useful for regular
518 * builtins that read shell commands from files or strings.
519 * If the given file is not an active file, an error is raised.
523 popfilesupto(struct parsefile
*file
)
525 while (parsefile
!= file
&& parsefile
!= &basepf
)
527 if (parsefile
!= file
)
528 error("popfilesupto() misused");
532 * Return to top level.
538 while (parsefile
!= &basepf
)
545 * Close the file(s) that the shell is reading commands from. Called
546 * after a fork is done.
553 if (parsefile
->fd
> 0) {
554 close(parsefile
->fd
);