1 /* $NetBSD: input.c,v 1.41 2008/10/16 14:36:40 dholland Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)input.c 8.3 (Berkeley) 6/9/95";
40 __RCSID("$NetBSD: input.c,v 1.41 2008/10/16 14:36:40 dholland Exp $");
44 #include <stdio.h> /* defines BUFSIZ */
52 * This file implements the input routines used by the parser.
65 #include "myhistedit.h"
67 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
71 struct strpush
*prev
; /* preceding string on stack */
75 struct alias
*ap
; /* if push was associated with an alias */
79 * The parsefile structure pointed to by the global variable parsefile
80 * contains information about the current file being read.
85 struct parsefile
*prev
; /* preceding file on stack */
86 int linno
; /* current line */
87 int fd
; /* file descriptor (or -1 if string) */
88 int nleft
; /* number of chars left in this line */
89 int lleft
; /* number of chars left in this buffer */
90 char *nextc
; /* next char in buffer */
91 char *buf
; /* input buffer */
92 struct strpush
*strpush
; /* for pushing strings at this level */
93 struct strpush basestrpush
; /* so pushing one is fast */
97 int plinno
= 1; /* input line number */
98 int parsenleft
; /* copy of parsefile->nleft */
99 MKINIT
int parselleft
; /* copy of parsefile->lleft */
100 char *parsenextc
; /* copy of parsefile->nextc */
101 MKINIT
struct parsefile basepf
; /* top level input file */
102 MKINIT
char basebuf
[BUFSIZ
]; /* buffer for top level input file */
103 struct parsefile
*parsefile
= &basepf
; /* current input file */
104 int init_editline
= 0; /* editline library initialized? */
105 int whichprompt
; /* 1 == PS1, 2 == PS2 */
107 STATIC
void pushfile(void);
108 static int preadfd(void);
116 basepf
.nextc
= basepf
.buf
= basebuf
;
120 if (exception
!= EXSHELLPROC
)
121 parselleft
= parsenleft
= 0; /* clear input buffer */
132 * Read a line from the script.
136 pfgets(char *line
, int len
)
142 while (--nleft
> 0) {
160 * Read a character from the script, returning PEOF on end of file.
161 * Nul characters in the input are silently discarded.
167 return pgetc_macro();
175 char *buf
= parsefile
->buf
;
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(buf
, rl_cp
, nr
);
202 nr
= read(parsefile
->fd
, buf
, BUFSIZ
- 8);
209 if (parsefile
->fd
== 0 && errno
== EWOULDBLOCK
) {
210 int flags
= fcntl(0, F_GETFL
, 0);
211 if (flags
>= 0 && flags
& O_NONBLOCK
) {
212 flags
&=~ O_NONBLOCK
;
213 if (fcntl(0, F_SETFL
, flags
) >= 0) {
214 out2str("sh: turning off NDELAY mode\n");
226 * Refill the input buffer and return the next input character:
228 * 1) If a string was pushed back on the input, pop it;
229 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
230 * from a string so we can't refill the buffer, return EOF.
231 * 3) If the is more stuff in this buffer, use it else call read to fill it.
232 * 4) Process input up to the next newline, deleting nul characters.
243 if (parsefile
->strpush
) {
245 if (--parsenleft
>= 0)
246 return (*parsenextc
++);
248 if (parsenleft
== EOF_NLEFT
|| parsefile
->buf
== NULL
)
254 if (parselleft
<= 0) {
255 if ((parselleft
= preadfd()) == -1) {
256 parselleft
= parsenleft
= EOF_NLEFT
;
263 /* delete nul characters */
265 for (more
= 1; more
;) {
276 parsenleft
= q
- parsenextc
;
277 more
= 0; /* Stop processing here */
287 if (--parselleft
<= 0) {
288 parsenleft
= q
- parsenextc
- 1;
300 if (parsefile
->fd
== 0 && hist
&& something
) {
303 history(hist
, &he
, whichprompt
== 1? H_ENTER
: H_APPEND
,
316 return *parsenextc
++;
320 * Undo the last call to pgetc. Only one character may be pushed back.
321 * PEOF may be pushed back.
332 * Push a string back onto the input at this current parsefile level.
333 * We handle aliases this way.
336 pushstring(char *s
, int len
, void *ap
)
341 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
342 if (parsefile
->strpush
) {
343 sp
= ckmalloc(sizeof (struct strpush
));
344 sp
->prev
= parsefile
->strpush
;
345 parsefile
->strpush
= sp
;
347 sp
= parsefile
->strpush
= &(parsefile
->basestrpush
);
348 sp
->prevstring
= parsenextc
;
349 sp
->prevnleft
= parsenleft
;
350 sp
->prevlleft
= parselleft
;
351 sp
->ap
= (struct alias
*)ap
;
353 ((struct alias
*)ap
)->flag
|= ALIASINUSE
;
362 struct strpush
*sp
= parsefile
->strpush
;
365 parsenextc
= sp
->prevstring
;
366 parsenleft
= sp
->prevnleft
;
367 parselleft
= sp
->prevlleft
;
368 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
370 sp
->ap
->flag
&= ~ALIASINUSE
;
371 parsefile
->strpush
= sp
->prev
;
372 if (sp
!= &(parsefile
->basestrpush
))
378 * Set the input to take input from a file. If push is set, push the
379 * old input onto the stack first.
383 setinputfile(const char *fname
, int push
)
385 unsigned char magic
[4];
390 if ((fd
= open(fname
, O_RDONLY
)) < 0)
391 error("Can't open %s", fname
);
393 /* Since the message "Syntax error: "(" unexpected" is not very
394 * helpful, we check if the file starts with the ELF magic to
395 * avoid that message. The first lseek tries to make sure that
396 * we can later rewind the file.
398 if (lseek(fd
, 0, SEEK_SET
) == 0) {
399 if (read(fd
, magic
, 4) == 4) {
400 if (memcmp(magic
, "\177ELF", 4) == 0)
401 error("Cannot execute ELF binary %s", fname
);
403 if (lseek(fd
, 0, SEEK_SET
) != 0)
404 error("Cannot rewind the file %s", fname
);
408 fd2
= copyfd(fd
, 10);
411 error("Out of file descriptors");
414 setinputfd(fd
, push
);
420 * Like setinputfile, but takes an open file descriptor. Call this with
425 setinputfd(int fd
, int push
)
427 (void) fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
430 parsefile
->buf
= ckmalloc(BUFSIZ
);
432 if (parsefile
->fd
> 0)
433 close(parsefile
->fd
);
435 if (parsefile
->buf
== NULL
)
436 parsefile
->buf
= ckmalloc(BUFSIZ
);
437 parselleft
= parsenleft
= 0;
443 * Like setinputfile, but takes input from a string.
447 setinputstring(char *string
, int push
)
453 parselleft
= parsenleft
= strlen(string
);
454 parsefile
->buf
= NULL
;
462 * To handle the "." command, a stack of input files is used. Pushfile
463 * adds a new entry to the stack and popfile restores the previous level.
469 struct parsefile
*pf
;
471 parsefile
->nleft
= parsenleft
;
472 parsefile
->lleft
= parselleft
;
473 parsefile
->nextc
= parsenextc
;
474 parsefile
->linno
= plinno
;
475 pf
= (struct parsefile
*)ckmalloc(sizeof (struct parsefile
));
476 pf
->prev
= parsefile
;
479 pf
->basestrpush
.prev
= NULL
;
487 struct parsefile
*pf
= parsefile
;
496 parsefile
= pf
->prev
;
498 parsenleft
= parsefile
->nleft
;
499 parselleft
= parsefile
->lleft
;
500 parsenextc
= parsefile
->nextc
;
501 plinno
= parsefile
->linno
;
507 * Return to top level.
513 while (parsefile
!= &basepf
)
520 * Close the file(s) that the shell is reading commands from. Called
521 * after a fork is done.
523 * Takes one arg, vfork, which tells it to not modify its global vars
524 * as it is still running in the parent.
526 * This code is (probably) unnecessary as the 'close on exec' flag is
527 * set and should be enough. In the vfork case it is definitely wrong
528 * to close the fds as another fork() may be done later to feed data
529 * from a 'here' document into a pipe and we don't want to close the
534 closescript(int vforked
)
539 if (parsefile
->fd
> 0) {
540 close(parsefile
->fd
);