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.14.2.2 2002/08/27 01:36:28 tjr Exp $
38 * $DragonFly: src/bin/sh/input.c,v 1.6 2005/11/13 11:58:30 corecode Exp $
41 #include <stdio.h> /* defines BUFSIZ */
49 * This file implements the input routines used by the parser.
62 #include "myhistedit.h"
65 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
69 struct strpush *prev; /* preceding string on stack */
73 struct alias *ap; /* if push was associated with an alias */
77 * The parsefile structure pointed to by the global variable parsefile
78 * contains information about the current file being read.
83 struct parsefile *prev; /* preceding file on stack */
84 int linno; /* current line */
85 int fd; /* file descriptor (or -1 if string) */
86 int nleft; /* number of chars left in this line */
87 int lleft; /* number of lines left in this buffer */
88 char *nextc; /* next char in buffer */
89 char *buf; /* input buffer */
90 struct strpush *strpush; /* for pushing strings at this level */
91 struct strpush basestrpush; /* so pushing one is fast */
95 int plinno = 1; /* input line number */
96 MKINIT int parsenleft; /* copy of parsefile->nleft */
97 MKINIT int parselleft; /* copy of parsefile->lleft */
98 char *parsenextc; /* copy of parsefile->nextc */
99 MKINIT struct parsefile basepf; /* top level input file */
100 char basebuf[BUFSIZ]; /* buffer for top level input file */
101 STATIC struct parsefile *parsefile = &basepf; /* current input file */
102 int init_editline = 0; /* editline library initialized? */
103 int whichprompt; /* 1 == PS1, 2 == PS2 */
105 EditLine *el; /* cookie for editline package */
107 STATIC void pushfile(void);
108 static int preadfd(void);
115 extern char basebuf[];
117 basepf.nextc = basepf.buf = basebuf;
121 if (exception != EXSHELLPROC)
122 parselleft = parsenleft = 0; /* clear input buffer */
133 * Read a line from the script.
137 pfgets(char *line, int len)
143 while (--nleft > 0) {
161 * Read a character from the script, returning PEOF on end of file.
162 * Nul characters in the input are silently discarded.
168 return pgetc_macro();
176 parsenextc = parsefile->buf;
179 if (el != NULL && gotwinch) {
186 if (parsefile->fd == 0 && el) {
189 rl_cp = el_gets(el, &nr);
193 /* XXX - BUFSIZE should redesign so not necessary */
194 strcpy(parsenextc, rl_cp);
198 nr = read(parsefile->fd, parsenextc, BUFSIZ - 1);
204 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
205 int flags = fcntl(0, F_GETFL, 0);
206 if (flags >= 0 && flags & O_NONBLOCK) {
207 flags &=~ O_NONBLOCK;
208 if (fcntl(0, F_SETFL, flags) >= 0) {
209 out2str("sh: turning off NDELAY mode\n");
221 * Refill the input buffer and return the next input character:
223 * 1) If a string was pushed back on the input, pop it;
224 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
225 * from a string so we can't refill the buffer, return EOF.
226 * 3) If there is more in this buffer, use it else call read to fill it.
227 * 4) Process input up to the next newline, deleting nul characters.
238 if (parsefile->strpush) {
240 if (--parsenleft >= 0)
241 return (*parsenextc++);
243 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
249 if (parselleft <= 0) {
250 if ((parselleft = preadfd()) == -1) {
251 parselleft = parsenleft = EOF_NLEFT;
258 /* delete nul characters */
260 for (more = 1; more;) {
271 parsenleft = q - parsenextc;
272 more = 0; /* Stop processing here */
282 if (--parselleft <= 0) {
283 parsenleft = q - parsenextc - 1;
295 if (parsefile->fd == 0 && hist && something) {
298 history(hist, &he, whichprompt == 1 ? H_ENTER : H_APPEND, parsenextc);
310 return *parsenextc++;
314 * Undo the last call to pgetc. Only one character may be pushed back.
315 * PEOF may be pushed back.
326 * Push a string back onto the input at this current parsefile level.
327 * We handle aliases this way.
330 pushstring(char *s, int len, void *ap)
335 /*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
336 if (parsefile->strpush) {
337 sp = ckmalloc(sizeof (struct strpush));
338 sp->prev = parsefile->strpush;
339 parsefile->strpush = sp;
341 sp = parsefile->strpush = &(parsefile->basestrpush);
342 sp->prevstring = parsenextc;
343 sp->prevnleft = parsenleft;
344 sp->prevlleft = parselleft;
345 sp->ap = (struct alias *)ap;
347 ((struct alias *)ap)->flag |= ALIASINUSE;
356 struct strpush *sp = parsefile->strpush;
359 parsenextc = sp->prevstring;
360 parsenleft = sp->prevnleft;
361 parselleft = sp->prevlleft;
362 /*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
364 sp->ap->flag &= ~ALIASINUSE;
365 parsefile->strpush = sp->prev;
366 if (sp != &(parsefile->basestrpush))
372 * Set the input to take input from a file. If push is set, push the
373 * old input onto the stack first.
377 setinputfile(const char *fname, int push)
383 if ((fd = open(fname, O_RDONLY)) < 0)
384 error("Can't open %s: %s", fname, strerror(errno));
386 fd2 = copyfd(fd, 10);
389 error("Out of file descriptors");
392 setinputfd(fd, push);
398 * Like setinputfile, but takes an open file descriptor. Call this with
403 setinputfd(int fd, int push)
405 fcntl(fd, F_SETFD, FD_CLOEXEC);
408 parsefile->buf = ckmalloc(BUFSIZ);
410 if (parsefile->fd > 0)
411 close(parsefile->fd);
413 if (parsefile->buf == NULL)
414 parsefile->buf = ckmalloc(BUFSIZ);
415 parselleft = parsenleft = 0;
421 * Like setinputfile, but takes input from a string.
425 setinputstring(char *string, int push)
431 parselleft = parsenleft = strlen(string);
432 parsefile->buf = NULL;
440 * To handle the "." command, a stack of input files is used. Pushfile
441 * adds a new entry to the stack and popfile restores the previous level.
447 struct parsefile *pf;
449 parsefile->nleft = parsenleft;
450 parsefile->lleft = parselleft;
451 parsefile->nextc = parsenextc;
452 parsefile->linno = plinno;
453 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
454 pf->prev = parsefile;
457 pf->basestrpush.prev = NULL;
465 struct parsefile *pf = parsefile;
474 parsefile = pf->prev;
476 parsenleft = parsefile->nleft;
477 parselleft = parsefile->lleft;
478 parsenextc = parsefile->nextc;
479 plinno = parsefile->linno;
485 * Return to top level.
491 while (parsefile != &basepf)
498 * Close the file(s) that the shell is reading commands from. Called
499 * after a fork is done.
506 if (parsefile->fd > 0) {
507 close(parsefile->fd);