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";
38 #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 */
53 * This file implements the input routines used by the parser.
68 #include <readline/readline.h>
70 /* What about other systems? */
71 char *readline(char *prompt
);
74 #include "myhistedit.h"
79 static void popstring(void);
81 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
85 struct strpush
*prev
; /* preceding string on stack */
89 struct alias
*ap
; /* if push was associated with an alias */
93 * The parsefile structure pointed to by the global variable parsefile
94 * contains information about the current file being read.
99 struct parsefile
*prev
; /* preceding file on stack */
100 int linno
; /* current line */
101 int fd
; /* file descriptor (or -1 if string) */
102 int nleft
; /* number of chars left in this line */
103 int lleft
; /* number of lines left in this buffer */
104 char *nextc
; /* next char in buffer */
105 char *buf
; /* input buffer */
106 struct strpush
*strpush
; /* for pushing strings at this level */
107 struct strpush basestrpush
; /* so pushing one is fast */
111 int plinno
= 1; /* input line number */
112 MKINIT
int parsenleft
; /* copy of parsefile->nleft */
113 MKINIT
int parselleft
; /* copy of parsefile->lleft */
114 char *parsenextc
; /* copy of parsefile->nextc */
115 MKINIT
struct parsefile basepf
; /* top level input file */
116 char basebuf
[BUFSIZ
]; /* buffer for top level input file */
117 STATIC
struct parsefile
*parsefile
= &basepf
; /* current input file */
118 int init_editline
= 0; /* editline library initialized? */
119 int whichprompt
; /* -1 == PSE, 1 == PS1, 2 == PS2 */
122 EditLine
*el
; /* cookie for editline package */
125 STATIC
void pushfile(void);
126 static int preadfd(void);
133 extern char basebuf
[];
135 basepf
.nextc
= basepf
.buf
= basebuf
;
139 if (exception
!= EXSHELLPROC
)
140 parselleft
= parsenleft
= 0; /* clear input buffer */
151 * Read a line from the script.
155 pfgets(char *line
, int len
)
161 while (--nleft
> 0) {
179 * Read a character from the script, returning PEOF on end of file.
180 * Nul characters in the input are silently discarded.
186 return pgetc_macro();
194 parsenextc
= parsefile
->buf
;
196 #if !defined(NO_HISTORY) && !defined(EDITLINE)
197 if (el
!= NULL
&& gotwinch
) {
205 if (parsefile
->fd
== 0 && editable
) {
206 static const char *rl_cp
= NULL
;
207 static size_t rl_off
= 0;
211 rl_cp
= readline(getprompt(NULL
));
217 nr
= strlen(rl_cp
+rl_off
);
221 (void) memcpy(parsenextc
, rl_cp
+rl_off
, nr
);
226 (void) memcpy(parsenextc
, rl_cp
+rl_off
, nr
);
227 parsenextc
[nr
++]= '\n';
234 #else /* !EDITLINE */
235 if (parsefile
->fd
== 0 && el
) {
238 rl_cp
= el_gets(el
, &nr
);
242 /* XXX - BUFSIZE should redesign so not necessary */
243 (void) strcpy(parsenextc
, rl_cp
);
246 #endif /* !EDITLINE */
248 nr
= read(parsefile
->fd
, parsenextc
, BUFSIZ
- 1);
255 if (parsefile
->fd
== 0 && errno
== EWOULDBLOCK
) {
256 int flags
= fcntl(0, F_GETFL
, 0);
257 if (flags
>= 0 && flags
& O_NONBLOCK
) {
258 flags
&=~ O_NONBLOCK
;
259 if (fcntl(0, F_SETFL
, flags
) >= 0) {
260 out2str("sh: turning off NDELAY mode\n");
265 #endif /* EWOULDBLOCK */
273 * Refill the input buffer and return the next input character:
275 * 1) If a string was pushed back on the input, pop it;
276 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
277 * from a string so we can't refill the buffer, return EOF.
278 * 3) If there is more in this buffer, use it else call read to fill it.
279 * 4) Process input up to the next newline, deleting nul characters.
290 if (parsefile
->strpush
) {
292 if (--parsenleft
>= 0)
293 return (*parsenextc
++);
295 if (parsenleft
== EOF_NLEFT
|| parsefile
->buf
== NULL
)
301 if (parselleft
<= 0) {
302 if ((parselleft
= preadfd()) == -1) {
303 parselleft
= parsenleft
= EOF_NLEFT
;
310 /* delete nul characters */
312 for (more
= 1; more
;) {
323 parsenleft
= q
- parsenextc
;
324 more
= 0; /* Stop processing here */
334 if (--parselleft
<= 0) {
335 parsenleft
= q
- parsenextc
- 1;
346 #if !defined(NO_HISTORY) && !defined(EDITLINE)
347 if (parsefile
->fd
== 0 && hist
&& something
) {
350 history(hist
, &he
, whichprompt
== 1 ? H_ENTER
: H_ADD
,
363 return *parsenextc
++;
367 * Undo the last call to pgetc. Only one character may be pushed back.
368 * PEOF may be pushed back.
379 * Push a string back onto the input at this current parsefile level.
380 * We handle aliases this way.
383 pushstring(char *s
, int len
, void *ap
)
388 /*dbgprintf("*** calling pushstring: %s, %d\n", s, len);*/
389 if (parsefile
->strpush
) {
390 sp
= ckmalloc(sizeof (struct strpush
));
391 sp
->prev
= parsefile
->strpush
;
392 parsefile
->strpush
= sp
;
394 sp
= parsefile
->strpush
= &(parsefile
->basestrpush
);
395 sp
->prevstring
= parsenextc
;
396 sp
->prevnleft
= parsenleft
;
397 sp
->prevlleft
= parselleft
;
398 sp
->ap
= (struct alias
*)ap
;
400 ((struct alias
*)ap
)->flag
|= ALIASINUSE
;
409 struct strpush
*sp
= parsefile
->strpush
;
412 parsenextc
= sp
->prevstring
;
413 parsenleft
= sp
->prevnleft
;
414 parselleft
= sp
->prevlleft
;
415 /*dbgprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
417 sp
->ap
->flag
&= ~ALIASINUSE
;
418 parsefile
->strpush
= sp
->prev
;
419 if (sp
!= &(parsefile
->basestrpush
))
425 * Set the input to take input from a file. If push is set, push the
426 * old input onto the stack first.
430 setinputfile(char *fname
, int push
)
438 if ((fd
= open(fname
, O_RDONLY
)) < 0)
439 error("Can't open %s: %s", fname
, strerror(errno
));
440 if (fstat(fd
, &statbuf
) < 0) {
443 error("Can't stat %s: %s", fname
, strerror(saved_errno
));
445 if (!S_ISREG(statbuf
.st_mode
)) {
447 error("Can't open %s: %s", fname
, strerror(ENOEXEC
));
450 fd2
= fcntl(fd
, F_DUPFD
, 10);
453 error("Out of file descriptors");
456 setinputfd(fd
, push
);
462 * Like setinputfile, but takes an open file descriptor. Call this with
467 setinputfd(int fd
, int push
)
469 (void)fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
472 parsefile
->buf
= ckmalloc(BUFSIZ
);
474 if (parsefile
->fd
> 0)
475 close(parsefile
->fd
);
477 if (parsefile
->buf
== NULL
)
478 parsefile
->buf
= ckmalloc(BUFSIZ
);
479 parselleft
= parsenleft
= 0;
485 * Like setinputfile, but takes input from a string.
489 setinputstring(char *string
, int push
)
495 parselleft
= parsenleft
= strlen(string
);
496 parsefile
->buf
= NULL
;
504 * To handle the "." command, a stack of input files is used. Pushfile
505 * adds a new entry to the stack and popfile restores the previous level.
511 struct parsefile
*pf
;
513 parsefile
->nleft
= parsenleft
;
514 parsefile
->lleft
= parselleft
;
515 parsefile
->nextc
= parsenextc
;
516 parsefile
->linno
= plinno
;
517 pf
= (struct parsefile
*)ckmalloc(sizeof (struct parsefile
));
518 pf
->prev
= parsefile
;
521 pf
->basestrpush
.prev
= NULL
;
529 struct parsefile
*pf
= parsefile
;
538 parsefile
= pf
->prev
;
540 parsenleft
= parsefile
->nleft
;
541 parselleft
= parsefile
->lleft
;
542 parsenextc
= parsefile
->nextc
;
543 plinno
= parsefile
->linno
;
549 * Return to top level.
555 while (parsefile
!= &basepf
)
562 * Close the file(s) that the shell is reading commands from. Called
563 * after a fork is done.
570 if (parsefile
->fd
> 0) {
571 close(parsefile
->fd
);
577 * $PchId: input.c,v 1.7 2006/05/29 13:09:38 philip Exp $