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: head/bin/sh/input.c 245676 2013-01-19 22:12:08Z jilles $
40 #include <stdio.h> /* defines BUFSIZ */
48 * This file implements the input routines used by the parser.
61 #include "myhistedit.h"
64 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
67 struct strpush *prev; /* preceding string on stack */
71 struct alias *ap; /* if push was associated with an alias */
75 * The parsefile structure pointed to by the global variable parsefile
76 * contains information about the current file being read.
80 struct parsefile *prev; /* preceding file on stack */
81 int linno; /* current line */
82 int fd; /* file descriptor (or -1 if string) */
83 int nleft; /* number of chars left in this line */
84 int lleft; /* number of lines left in this buffer */
85 char *nextc; /* next char in buffer */
86 char *buf; /* input buffer */
87 struct strpush *strpush; /* for pushing strings at this level */
88 struct strpush basestrpush; /* so pushing one is fast */
92 int plinno = 1; /* input line number */
93 int parsenleft; /* copy of parsefile->nleft */
94 MKINIT int parselleft; /* copy of parsefile->lleft */
95 char *parsenextc; /* copy of parsefile->nextc */
96 static char basebuf[BUFSIZ + 1];/* buffer for top level input file */
97 static struct parsefile basepf = { /* top level input file */
101 static struct parsefile *parsefile = &basepf; /* current input file */
102 int whichprompt; /* 1 == PS1, 2 == PS2 */
104 EditLine *el; /* cookie for editline package */
106 static void pushfile(void);
107 static int preadfd(void);
108 static void popstring(void);
117 parselleft = parsenleft = 0; /* clear input buffer */
123 * Read a line from the script.
127 pfgets(char *line, int len)
133 while (--nleft > 0) {
151 * Read a character from the script, returning PEOF on end of file.
152 * Nul characters in the input are silently discarded.
158 return pgetc_macro();
166 parsenextc = parsefile->buf;
169 if (el != NULL && gotwinch) {
176 if (parsefile->fd == 0 && el) {
177 static const char *rl_cp;
181 rl_cp = el_gets(el, &el_len);
183 nr = el_len == 0 ? 0 : -1;
188 memcpy(parsenextc, rl_cp, nr);
197 nr = read(parsefile->fd, parsenextc, BUFSIZ);
203 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
204 int flags = fcntl(0, F_GETFL, 0);
205 if (flags >= 0 && flags & O_NONBLOCK) {
206 flags &=~ O_NONBLOCK;
207 if (fcntl(0, F_SETFL, flags) >= 0) {
208 out2fmt_flush("sh: turning off NDELAY mode\n");
220 * Refill the input buffer and return the next input character:
222 * 1) If a string was pushed back on the input, pop it;
223 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
224 * from a string so we can't refill the buffer, return EOF.
225 * 3) If there is more in this buffer, use it else call read to fill it.
226 * 4) Process input up to the next newline, deleting nul characters.
237 if (parsefile->strpush) {
239 if (--parsenleft >= 0)
240 return (*parsenextc++);
242 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
248 if (parselleft <= 0) {
249 if ((parselleft = preadfd()) == -1) {
250 parselleft = parsenleft = EOF_NLEFT;
257 /* delete nul characters */
259 for (more = 1; more;) {
270 parsenleft = q - parsenextc;
271 more = 0; /* Stop processing here */
281 if (--parselleft <= 0) {
282 parsenleft = q - parsenextc - 1;
294 if (parsefile->fd == 0 && hist && something) {
297 history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD,
310 return *parsenextc++;
314 * Returns if we are certain we are at EOF. Does not cause any more input
315 * to be read from the outside world.
323 if (parsefile->strpush)
325 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
331 * Undo the last call to pgetc. Only one character may be pushed back.
332 * PEOF may be pushed back.
343 * Push a string back onto the input at this current parsefile level.
344 * We handle aliases this way.
347 pushstring(char *s, int len, struct alias *ap)
352 /*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/
353 if (parsefile->strpush) {
354 sp = ckmalloc(sizeof (struct strpush));
355 sp->prev = parsefile->strpush;
356 parsefile->strpush = sp;
358 sp = parsefile->strpush = &(parsefile->basestrpush);
359 sp->prevstring = parsenextc;
360 sp->prevnleft = parsenleft;
361 sp->prevlleft = parselleft;
364 ap->flag |= ALIASINUSE;
373 struct strpush *sp = parsefile->strpush;
376 parsenextc = sp->prevstring;
377 parsenleft = sp->prevnleft;
378 parselleft = sp->prevlleft;
379 /*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/
381 sp->ap->flag &= ~ALIASINUSE;
382 parsefile->strpush = sp->prev;
383 if (sp != &(parsefile->basestrpush))
389 * Set the input to take input from a file. If push is set, push the
390 * old input onto the stack first.
394 setinputfile(const char *fname, int push)
400 if ((fd = open(fname, O_RDONLY)) < 0)
401 error("cannot open %s: %s", fname, strerror(errno));
403 fd2 = fcntl(fd, F_DUPFD, 10);
406 error("Out of file descriptors");
409 setinputfd(fd, push);
415 * Like setinputfile, but takes an open file descriptor. Call this with
420 setinputfd(int fd, int push)
422 fcntl(fd, F_SETFD, FD_CLOEXEC);
425 parsefile->buf = ckmalloc(BUFSIZ + 1);
427 if (parsefile->fd > 0)
428 close(parsefile->fd);
430 if (parsefile->buf == NULL)
431 parsefile->buf = ckmalloc(BUFSIZ + 1);
432 parselleft = parsenleft = 0;
438 * Like setinputfile, but takes input from a string.
442 setinputstring(char *string, int push)
448 parselleft = parsenleft = strlen(string);
449 parsefile->buf = NULL;
457 * To handle the "." command, a stack of input files is used. Pushfile
458 * adds a new entry to the stack and popfile restores the previous level.
464 struct parsefile *pf;
466 parsefile->nleft = parsenleft;
467 parsefile->lleft = parselleft;
468 parsefile->nextc = parsenextc;
469 parsefile->linno = plinno;
470 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
471 pf->prev = parsefile;
474 pf->basestrpush.prev = NULL;
482 struct parsefile *pf = parsefile;
491 parsefile = pf->prev;
493 parsenleft = parsefile->nleft;
494 parselleft = parsefile->lleft;
495 parsenextc = parsefile->nextc;
496 plinno = parsefile->linno;
502 * Return current file (to go back to it later using popfilesupto()).
513 * Pop files until the given file is on top again. Useful for regular
514 * builtins that read shell commands from files or strings.
515 * If the given file is not an active file, an error is raised.
519 popfilesupto(struct parsefile *file)
521 while (parsefile != file && parsefile != &basepf)
523 if (parsefile != file)
524 error("popfilesupto() misused");
528 * Return to top level.
534 while (parsefile != &basepf)
541 * Close the file(s) that the shell is reading commands from. Called
542 * after a fork is done.
549 if (parsefile->fd > 0) {
550 close(parsefile->fd);