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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid
[] = "@(#)input.c 8.3 (Berkeley) 6/9/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/input.c,v 1.22 2004/04/06 20:06:51 markm Exp $");
43 #include <sys/types.h>
44 #include <stdio.h> /* defines BUFSIZ */
52 * This file implements the input routines used by the parser.
67 #include <readline/readline.h>
69 /* What about other systems? */
70 char *readline(char *prompt
);
73 #include "myhistedit.h"
78 static void popstring(void);
80 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
84 struct strpush
*prev
; /* preceding string on stack */
88 struct alias
*ap
; /* if push was associated with an alias */
92 * The parsefile structure pointed to by the global variable parsefile
93 * contains information about the current file being read.
98 struct parsefile
*prev
; /* preceding file on stack */
99 int linno
; /* current line */
100 int fd
; /* file descriptor (or -1 if string) */
101 int nleft
; /* number of chars left in this line */
102 int lleft
; /* number of lines left in this buffer */
103 char *nextc
; /* next char in buffer */
104 char *buf
; /* input buffer */
105 struct strpush
*strpush
; /* for pushing strings at this level */
106 struct strpush basestrpush
; /* so pushing one is fast */
110 int plinno
= 1; /* input line number */
111 MKINIT
int parsenleft
; /* copy of parsefile->nleft */
112 MKINIT
int parselleft
; /* copy of parsefile->lleft */
113 char *parsenextc
; /* copy of parsefile->nextc */
114 MKINIT
struct parsefile basepf
; /* top level input file */
115 char basebuf
[BUFSIZ
]; /* buffer for top level input file */
116 STATIC
struct parsefile
*parsefile
= &basepf
; /* current input file */
117 int init_editline
= 0; /* editline library initialized? */
118 int whichprompt
; /* -1 == PSE, 1 == PS1, 2 == PS2 */
121 EditLine
*el
; /* cookie for editline package */
124 STATIC
void pushfile(void);
125 static int preadfd(void);
132 extern char basebuf
[];
134 basepf
.nextc
= basepf
.buf
= basebuf
;
138 if (exception
!= EXSHELLPROC
)
139 parselleft
= parsenleft
= 0; /* clear input buffer */
150 * Read a line from the script.
154 pfgets(char *line
, int len
)
160 while (--nleft
> 0) {
178 * Read a character from the script, returning PEOF on end of file.
179 * Nul characters in the input are silently discarded.
185 return pgetc_macro();
193 parsenextc
= parsefile
->buf
;
195 #if !defined(NO_HISTORY) && !defined(EDITLINE)
196 if (el
!= NULL
&& gotwinch
) {
204 if (parsefile
->fd
== 0 && editable
) {
205 static const char *rl_cp
= NULL
;
206 static size_t rl_off
= 0;
210 rl_cp
= readline(getprompt(NULL
));
216 nr
= strlen(rl_cp
+rl_off
);
220 (void) memcpy(parsenextc
, rl_cp
+rl_off
, nr
);
225 (void) memcpy(parsenextc
, rl_cp
+rl_off
, nr
);
226 parsenextc
[nr
++]= '\n';
233 #else /* !EDITLINE */
234 if (parsefile
->fd
== 0 && el
) {
237 rl_cp
= el_gets(el
, &nr
);
241 /* XXX - BUFSIZE should redesign so not necessary */
242 (void) strcpy(parsenextc
, rl_cp
);
245 #endif /* !EDITLINE */
247 nr
= read(parsefile
->fd
, parsenextc
, BUFSIZ
- 1);
254 if (parsefile
->fd
== 0 && errno
== EWOULDBLOCK
) {
255 int flags
= fcntl(0, F_GETFL
, 0);
256 if (flags
>= 0 && flags
& O_NONBLOCK
) {
257 flags
&=~ O_NONBLOCK
;
258 if (fcntl(0, F_SETFL
, flags
) >= 0) {
259 out2str("sh: turning off NDELAY mode\n");
264 #endif /* EWOULDBLOCK */
272 * Refill the input buffer and return the next input character:
274 * 1) If a string was pushed back on the input, pop it;
275 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
276 * from a string so we can't refill the buffer, return EOF.
277 * 3) If there is more in this buffer, use it else call read to fill it.
278 * 4) Process input up to the next newline, deleting nul characters.
289 if (parsefile
->strpush
) {
291 if (--parsenleft
>= 0)
292 return (*parsenextc
++);
294 if (parsenleft
== EOF_NLEFT
|| parsefile
->buf
== NULL
)
300 if (parselleft
<= 0) {
301 if ((parselleft
= preadfd()) == -1) {
302 parselleft
= parsenleft
= EOF_NLEFT
;
309 /* delete nul characters */
311 for (more
= 1; more
;) {
322 parsenleft
= q
- parsenextc
;
323 more
= 0; /* Stop processing here */
333 if (--parselleft
<= 0) {
334 parsenleft
= q
- parsenextc
- 1;
345 #if !defined(NO_HISTORY) && !defined(EDITLINE)
346 if (parsefile
->fd
== 0 && hist
&& something
) {
349 history(hist
, &he
, whichprompt
== 1 ? H_ENTER
: H_ADD
,
362 return *parsenextc
++;
366 * Undo the last call to pgetc. Only one character may be pushed back.
367 * PEOF may be pushed back.
378 * Push a string back onto the input at this current parsefile level.
379 * We handle aliases this way.
382 pushstring(char *s
, int len
, void *ap
)
387 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
388 if (parsefile
->strpush
) {
389 sp
= ckmalloc(sizeof (struct strpush
));
390 sp
->prev
= parsefile
->strpush
;
391 parsefile
->strpush
= sp
;
393 sp
= parsefile
->strpush
= &(parsefile
->basestrpush
);
394 sp
->prevstring
= parsenextc
;
395 sp
->prevnleft
= parsenleft
;
396 sp
->prevlleft
= parselleft
;
397 sp
->ap
= (struct alias
*)ap
;
399 ((struct alias
*)ap
)->flag
|= ALIASINUSE
;
408 struct strpush
*sp
= parsefile
->strpush
;
411 parsenextc
= sp
->prevstring
;
412 parsenleft
= sp
->prevnleft
;
413 parselleft
= sp
->prevlleft
;
414 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
416 sp
->ap
->flag
&= ~ALIASINUSE
;
417 parsefile
->strpush
= sp
->prev
;
418 if (sp
!= &(parsefile
->basestrpush
))
424 * Set the input to take input from a file. If push is set, push the
425 * old input onto the stack first.
429 setinputfile(char *fname
, int push
)
435 if ((fd
= open(fname
, O_RDONLY
)) < 0)
436 error("Can't open %s: %s", fname
, strerror(errno
));
438 fd2
= fcntl(fd
, F_DUPFD
, 10);
441 error("Out of file descriptors");
444 setinputfd(fd
, push
);
450 * Like setinputfile, but takes an open file descriptor. Call this with
455 setinputfd(int fd
, int push
)
457 (void)fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
460 parsefile
->buf
= ckmalloc(BUFSIZ
);
462 if (parsefile
->fd
> 0)
463 close(parsefile
->fd
);
465 if (parsefile
->buf
== NULL
)
466 parsefile
->buf
= ckmalloc(BUFSIZ
);
467 parselleft
= parsenleft
= 0;
473 * Like setinputfile, but takes input from a string.
477 setinputstring(char *string
, int push
)
483 parselleft
= parsenleft
= strlen(string
);
484 parsefile
->buf
= NULL
;
492 * To handle the "." command, a stack of input files is used. Pushfile
493 * adds a new entry to the stack and popfile restores the previous level.
499 struct parsefile
*pf
;
501 parsefile
->nleft
= parsenleft
;
502 parsefile
->lleft
= parselleft
;
503 parsefile
->nextc
= parsenextc
;
504 parsefile
->linno
= plinno
;
505 pf
= (struct parsefile
*)ckmalloc(sizeof (struct parsefile
));
506 pf
->prev
= parsefile
;
509 pf
->basestrpush
.prev
= NULL
;
517 struct parsefile
*pf
= parsefile
;
526 parsefile
= pf
->prev
;
528 parsenleft
= parsefile
->nleft
;
529 parselleft
= parsefile
->lleft
;
530 parsenextc
= parsefile
->nextc
;
531 plinno
= parsefile
->linno
;
537 * Return to top level.
543 while (parsefile
!= &basepf
)
550 * Close the file(s) that the shell is reading commands from. Called
551 * after a fork is done.
558 if (parsefile
->fd
> 0) {
559 close(parsefile
->fd
);
565 * $PchId: input.c,v 1.7 2006/05/29 13:09:38 philip Exp $