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. 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
32 * @(#)input.c 8.3 (Berkeley) 6/9/95
33 * $FreeBSD: head/bin/sh/input.c 253658 2013-07-25 19:48:15Z jilles $
36 #include <stdio.h> /* defines BUFSIZ */
44 * This file implements the input routines used by the parser.
57 #include "myhistedit.h"
60 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
63 struct strpush *prev; /* preceding string on stack */
64 const char *prevstring;
67 struct alias *ap; /* if push was associated with an alias */
71 * The parsefile structure pointed to by the global variable parsefile
72 * contains information about the current file being read.
76 struct parsefile *prev; /* preceding file on stack */
77 int linno; /* current line */
78 int fd; /* file descriptor (or -1 if string) */
79 int nleft; /* number of chars left in this line */
80 int lleft; /* number of lines left in this buffer */
81 const char *nextc; /* next char in buffer */
82 char *buf; /* input buffer */
83 struct strpush *strpush; /* for pushing strings at this level */
84 struct strpush basestrpush; /* so pushing one is fast */
88 int plinno = 1; /* input line number */
89 int parsenleft; /* copy of parsefile->nleft */
90 static int parselleft; /* copy of parsefile->lleft */
91 const char *parsenextc; /* copy of parsefile->nextc */
92 static char basebuf[BUFSIZ + 1];/* buffer for top level input file */
93 static struct parsefile basepf = { /* top level input file */
97 static struct parsefile *parsefile = &basepf; /* current input file */
98 int whichprompt; /* 1 == PS1, 2 == PS2 */
100 EditLine *el; /* cookie for editline package */
102 static void pushfile(void);
103 static int preadfd(void);
104 static void popstring(void);
110 parselleft = parsenleft = 0; /* clear input buffer */
115 * Read a line from the script.
119 pfgets(char *line, int len)
125 while (--nleft > 0) {
143 * Read a character from the script, returning PEOF on end of file.
144 * Nul characters in the input are silently discarded.
150 return pgetc_macro();
158 parsenextc = parsefile->buf;
161 if (el != NULL && gotwinch) {
168 if (parsefile->fd == 0 && el) {
169 static const char *rl_cp;
173 rl_cp = el_gets(el, &el_len);
175 nr = el_len == 0 ? 0 : -1;
180 memcpy(parsefile->buf, rl_cp, nr);
189 nr = read(parsefile->fd, parsefile->buf, BUFSIZ);
195 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
196 int flags = fcntl(0, F_GETFL, 0);
197 if (flags >= 0 && flags & O_NONBLOCK) {
198 flags &=~ O_NONBLOCK;
199 if (fcntl(0, F_SETFL, flags) >= 0) {
200 out2fmt_flush("sh: turning off NDELAY mode\n");
212 * Refill the input buffer and return the next input character:
214 * 1) If a string was pushed back on the input, pop it;
215 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
216 * from a string so we can't refill the buffer, return EOF.
217 * 3) If there is more in this buffer, use it else call read to fill it.
218 * 4) Process input up to the next newline, deleting nul characters.
229 if (parsefile->strpush) {
231 if (--parsenleft >= 0)
232 return (*parsenextc++);
234 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
240 if (parselleft <= 0) {
241 if ((parselleft = preadfd()) == -1) {
242 parselleft = parsenleft = EOF_NLEFT;
247 q = p = parsefile->buf + (parsenextc - parsefile->buf);
249 /* delete nul characters */
251 for (more = 1; more;) {
262 parsenleft = q - parsenextc;
263 more = 0; /* Stop processing here */
273 if (--parselleft <= 0) {
274 parsenleft = q - parsenextc - 1;
286 if (parsefile->fd == 0 && hist && something) {
289 history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD,
302 return *parsenextc++;
306 * Returns if we are certain we are at EOF. Does not cause any more input
307 * to be read from the outside world.
315 if (parsefile->strpush)
317 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
323 * Undo the last call to pgetc. Only one character may be pushed back.
324 * PEOF may be pushed back.
335 * Push a string back onto the input at this current parsefile level.
336 * We handle aliases this way.
339 pushstring(char *s, int len, struct alias *ap)
344 /*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/
345 if (parsefile->strpush) {
346 sp = ckmalloc(sizeof (struct strpush));
347 sp->prev = parsefile->strpush;
348 parsefile->strpush = sp;
350 sp = parsefile->strpush = &(parsefile->basestrpush);
351 sp->prevstring = parsenextc;
352 sp->prevnleft = parsenleft;
353 sp->prevlleft = parselleft;
356 ap->flag |= ALIASINUSE;
365 struct strpush *sp = parsefile->strpush;
368 parsenextc = sp->prevstring;
369 parsenleft = sp->prevnleft;
370 parselleft = sp->prevlleft;
371 /*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/
373 sp->ap->flag &= ~ALIASINUSE;
374 parsefile->strpush = sp->prev;
375 if (sp != &(parsefile->basestrpush))
381 * Set the input to take input from a file. If push is set, push the
382 * old input onto the stack first.
386 setinputfile(const char *fname, int push)
393 if ((fd = open(fname, O_RDONLY)) < 0)
395 if ((fd = open(fname, O_RDONLY | O_CLOEXEC)) < 0)
397 error("cannot open %s: %s", fname, strerror(errno));
399 #ifndef F_DUPFD_CLOEXEC
400 fd2 = fcntl(fd, F_DUPFD, 10);
402 fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 10);
406 error("Out of file descriptors");
409 setinputfd(fd, push);
415 * Like setinputfile, but takes an open file descriptor (which should have
416 * its FD_CLOEXEC flag already set). Call this with interrupts off.
420 setinputfd(int fd, int push)
422 #if !defined(O_CLOEXEC) || !defined(F_DUPFD_CLOEXEC)
423 fcntl(fd, F_SETFD, FD_CLOEXEC);
427 parsefile->buf = ckmalloc(BUFSIZ + 1);
429 if (parsefile->fd > 0)
430 close(parsefile->fd);
432 if (parsefile->buf == NULL)
433 parsefile->buf = ckmalloc(BUFSIZ + 1);
434 parselleft = parsenleft = 0;
440 * Like setinputfile, but takes input from a string.
444 setinputstring(const char *string, int push)
450 parselleft = parsenleft = strlen(string);
451 parsefile->buf = NULL;
459 * To handle the "." command, a stack of input files is used. Pushfile
460 * adds a new entry to the stack and popfile restores the previous level.
466 struct parsefile *pf;
468 parsefile->nleft = parsenleft;
469 parsefile->lleft = parselleft;
470 parsefile->nextc = parsenextc;
471 parsefile->linno = plinno;
472 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
473 pf->prev = parsefile;
476 pf->basestrpush.prev = NULL;
484 struct parsefile *pf = parsefile;
493 parsefile = pf->prev;
495 parsenleft = parsefile->nleft;
496 parselleft = parsefile->lleft;
497 parsenextc = parsefile->nextc;
498 plinno = parsefile->linno;
504 * Return current file (to go back to it later using popfilesupto()).
515 * Pop files until the given file is on top again. Useful for regular
516 * builtins that read shell commands from files or strings.
517 * If the given file is not an active file, an error is raised.
521 popfilesupto(struct parsefile *file)
523 while (parsefile != file && parsefile != &basepf)
525 if (parsefile != file)
526 error("popfilesupto() misused");
530 * Return to top level.
536 while (parsefile != &basepf)
543 * Close the file(s) that the shell is reading commands from. Called
544 * after a fork is done.
551 if (parsefile->fd > 0) {
552 close(parsefile->fd);