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
39 static char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: src/bin/sh/input.c,v 1.14.2.2 2002/08/27 01:36:28 tjr Exp $");
45 #include <stdio.h> /* defines BUFSIZ */
53 * This file implements the input routines used by the parser.
66 #include "myhistedit.h"
69 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
73 struct strpush *prev; /* preceding string on stack */
77 struct alias *ap; /* if push was associated with an alias */
81 * The parsefile structure pointed to by the global variable parsefile
82 * contains information about the current file being read.
87 struct parsefile *prev; /* preceding file on stack */
88 int linno; /* current line */
89 int fd; /* file descriptor (or -1 if string) */
90 int nleft; /* number of chars left in this line */
91 int lleft; /* number of lines left in this buffer */
92 char *nextc; /* next char in buffer */
93 char *buf; /* input buffer */
94 struct strpush *strpush; /* for pushing strings at this level */
95 struct strpush basestrpush; /* so pushing one is fast */
99 int plinno = 1; /* input line number */
100 MKINIT int parsenleft; /* copy of parsefile->nleft */
101 MKINIT int parselleft; /* copy of parsefile->lleft */
102 char *parsenextc; /* copy of parsefile->nextc */
103 MKINIT struct parsefile basepf; /* top level input file */
104 char basebuf[BUFSIZ]; /* buffer for top level input file */
105 struct parsefile *parsefile = &basepf; /* current input file */
106 int init_editline = 0; /* editline library initialized? */
107 int whichprompt; /* 1 == PS1, 2 == PS2 */
109 EditLine *el; /* cookie for editline package */
111 STATIC void pushfile(void);
112 static int preadfd(void);
119 extern char basebuf[];
121 basepf.nextc = basepf.buf = basebuf;
125 if (exception != EXSHELLPROC)
126 parselleft = parsenleft = 0; /* clear input buffer */
137 * Read a line from the script.
141 pfgets(char *line, int len)
147 while (--nleft > 0) {
165 * Read a character from the script, returning PEOF on end of file.
166 * Nul characters in the input are silently discarded.
172 return pgetc_macro();
180 parsenextc = parsefile->buf;
183 if (el != NULL && gotwinch) {
190 if (parsefile->fd == 0 && el) {
193 rl_cp = el_gets(el, &nr);
197 /* XXX - BUFSIZE should redesign so not necessary */
198 (void) strcpy(parsenextc, rl_cp);
202 nr = read(parsefile->fd, parsenextc, BUFSIZ - 1);
208 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
209 int flags = fcntl(0, F_GETFL, 0);
210 if (flags >= 0 && flags & O_NONBLOCK) {
211 flags &=~ O_NONBLOCK;
212 if (fcntl(0, F_SETFL, flags) >= 0) {
213 out2str("sh: turning off NDELAY mode\n");
225 * Refill the input buffer and return the next input character:
227 * 1) If a string was pushed back on the input, pop it;
228 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
229 * from a string so we can't refill the buffer, return EOF.
230 * 3) If there is more in this buffer, use it else call read to fill it.
231 * 4) Process input up to the next newline, deleting nul characters.
242 if (parsefile->strpush) {
244 if (--parsenleft >= 0)
245 return (*parsenextc++);
247 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
253 if (parselleft <= 0) {
254 if ((parselleft = preadfd()) == -1) {
255 parselleft = parsenleft = EOF_NLEFT;
262 /* delete nul characters */
264 for (more = 1; more;) {
275 parsenleft = q - parsenextc;
276 more = 0; /* Stop processing here */
286 if (--parselleft <= 0) {
287 parsenleft = q - parsenextc - 1;
299 if (parsefile->fd == 0 && hist && something) {
301 history(hist, whichprompt == 1 ? H_ENTER : H_ADD, parsenextc);
313 return *parsenextc++;
317 * Undo the last call to pgetc. Only one character may be pushed back.
318 * PEOF may be pushed back.
329 * Push a string back onto the input at this current parsefile level.
330 * We handle aliases this way.
333 pushstring(char *s, int len, void *ap)
338 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
339 if (parsefile->strpush) {
340 sp = ckmalloc(sizeof (struct strpush));
341 sp->prev = parsefile->strpush;
342 parsefile->strpush = sp;
344 sp = parsefile->strpush = &(parsefile->basestrpush);
345 sp->prevstring = parsenextc;
346 sp->prevnleft = parsenleft;
347 sp->prevlleft = parselleft;
348 sp->ap = (struct alias *)ap;
350 ((struct alias *)ap)->flag |= ALIASINUSE;
359 struct strpush *sp = parsefile->strpush;
362 parsenextc = sp->prevstring;
363 parsenleft = sp->prevnleft;
364 parselleft = sp->prevlleft;
365 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
367 sp->ap->flag &= ~ALIASINUSE;
368 parsefile->strpush = sp->prev;
369 if (sp != &(parsefile->basestrpush))
375 * Set the input to take input from a file. If push is set, push the
376 * old input onto the stack first.
380 setinputfile(char *fname, int push)
386 if ((fd = open(fname, O_RDONLY)) < 0)
387 error("Can't open %s: %s", fname, strerror(errno));
389 fd2 = copyfd(fd, 10);
392 error("Out of file descriptors");
395 setinputfd(fd, push);
401 * Like setinputfile, but takes an open file descriptor. Call this with
406 setinputfd(int fd, int push)
408 (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
411 parsefile->buf = ckmalloc(BUFSIZ);
413 if (parsefile->fd > 0)
414 close(parsefile->fd);
416 if (parsefile->buf == NULL)
417 parsefile->buf = ckmalloc(BUFSIZ);
418 parselleft = parsenleft = 0;
424 * Like setinputfile, but takes input from a string.
428 setinputstring(char *string, int push)
434 parselleft = parsenleft = strlen(string);
435 parsefile->buf = NULL;
443 * To handle the "." command, a stack of input files is used. Pushfile
444 * adds a new entry to the stack and popfile restores the previous level.
450 struct parsefile *pf;
452 parsefile->nleft = parsenleft;
453 parsefile->lleft = parselleft;
454 parsefile->nextc = parsenextc;
455 parsefile->linno = plinno;
456 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
457 pf->prev = parsefile;
460 pf->basestrpush.prev = NULL;
468 struct parsefile *pf = parsefile;
477 parsefile = pf->prev;
479 parsenleft = parsefile->nleft;
480 parselleft = parsefile->lleft;
481 parsenextc = parsefile->nextc;
482 plinno = parsefile->linno;
488 * Return to top level.
494 while (parsefile != &basepf)
501 * Close the file(s) that the shell is reading commands from. Called
502 * after a fork is done.
509 if (parsefile->fd > 0) {
510 close(parsefile->fd);