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>
39 __FBSDID("$FreeBSD$");
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 */
68 struct strpush *prev; /* preceding string on stack */
69 const char *prevstring;
72 struct alias *ap; /* if push was associated with an alias */
76 * The parsefile structure pointed to by the global variable parsefile
77 * contains information about the current file being read.
81 struct parsefile *prev; /* preceding file on stack */
82 int linno; /* current line */
83 int fd; /* file descriptor (or -1 if string) */
84 int nleft; /* number of chars left in this line */
85 int lleft; /* number of lines left in this buffer */
86 const char *nextc; /* next char in buffer */
87 char *buf; /* input buffer */
88 struct strpush *strpush; /* for pushing strings at this level */
89 struct strpush basestrpush; /* so pushing one is fast */
93 int plinno = 1; /* input line number */
94 int parsenleft; /* copy of parsefile->nleft */
95 static int parselleft; /* copy of parsefile->lleft */
96 const char *parsenextc; /* copy of parsefile->nextc */
97 static char basebuf[BUFSIZ + 1];/* buffer for top level input file */
98 static struct parsefile basepf = { /* top level input file */
102 static struct parsefile *parsefile = &basepf; /* current input file */
103 int whichprompt; /* 1 == PS1, 2 == PS2 */
105 EditLine *el; /* cookie for editline package */
107 static void pushfile(void);
108 static int preadfd(void);
109 static void popstring(void);
115 parselleft = parsenleft = 0; /* clear input buffer */
120 * Read a line from the script.
124 pfgets(char *line, int len)
130 while (--nleft > 0) {
148 * Read a character from the script, returning PEOF on end of file.
149 * Nul characters in the input are silently discarded.
155 return pgetc_macro();
163 parsenextc = parsefile->buf;
167 if (parsefile->fd == 0 && el) {
168 static const char *rl_cp;
173 rl_cp = el_gets(el, &el_len);
176 nr = el_len == 0 ? 0 : -1;
181 memcpy(parsefile->buf, rl_cp, nr);
190 nr = read(parsefile->fd, parsefile->buf, BUFSIZ);
196 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
197 int flags = fcntl(0, F_GETFL, 0);
198 if (flags >= 0 && flags & O_NONBLOCK) {
199 flags &=~ O_NONBLOCK;
200 if (fcntl(0, F_SETFL, flags) >= 0) {
201 out2fmt_flush("sh: turning off NDELAY mode\n");
213 * Refill the input buffer and return the next input character:
215 * 1) If a string was pushed back on the input, pop it;
216 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
217 * from a string so we can't refill the buffer, return EOF.
218 * 3) If there is more in this buffer, use it else call read to fill it.
219 * 4) Process input up to the next newline, deleting nul characters.
229 while (parsefile->strpush) {
231 * Add a space to the end of an alias to ensure that the
232 * alias remains in use while parsing its last word.
233 * This avoids alias recursions.
235 if (parsenleft == -1 && parsefile->strpush->ap != NULL)
238 if (--parsenleft >= 0)
239 return (*parsenextc++);
241 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
247 if (parselleft <= 0) {
248 if ((parselleft = preadfd()) == -1) {
249 parselleft = parsenleft = EOF_NLEFT;
254 q = p = parsefile->buf + (parsenextc - parsefile->buf);
256 /* delete nul characters */
257 for (more = 1; more;) {
264 parsenleft = q - parsenextc;
265 more = 0; /* Stop processing here */
274 if (--parselleft <= 0) {
275 parsenleft = q - parsenextc - 1;
287 if (parsefile->fd == 0 && hist &&
288 parsenextc[strspn(parsenextc, " \t\n")] != '\0') {
291 history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD,
304 return *parsenextc++;
308 * Returns if we are certain we are at EOF. Does not cause any more input
309 * to be read from the outside world.
317 if (parsefile->strpush)
319 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
325 * Undo the last call to pgetc. Only one character may be pushed back.
326 * PEOF may be pushed back.
337 * Push a string back onto the input at this current parsefile level.
338 * We handle aliases this way.
341 pushstring(char *s, int len, struct alias *ap)
346 /*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/
347 if (parsefile->strpush) {
348 sp = ckmalloc(sizeof (struct strpush));
349 sp->prev = parsefile->strpush;
350 parsefile->strpush = sp;
352 sp = parsefile->strpush = &(parsefile->basestrpush);
353 sp->prevstring = parsenextc;
354 sp->prevnleft = parsenleft;
355 sp->prevlleft = parselleft;
358 ap->flag |= ALIASINUSE;
367 struct strpush *sp = parsefile->strpush;
371 if (parsenextc != sp->ap->val &&
372 (parsenextc[-1] == ' ' || parsenextc[-1] == '\t'))
374 sp->ap->flag &= ~ALIASINUSE;
376 parsenextc = sp->prevstring;
377 parsenleft = sp->prevnleft;
378 parselleft = sp->prevlleft;
379 /*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/
380 parsefile->strpush = sp->prev;
381 if (sp != &(parsefile->basestrpush))
387 * Set the input to take input from a file. If push is set, push the
388 * old input onto the stack first.
392 setinputfile(const char *fname, int push)
398 if ((fd = open(fname, O_RDONLY | O_CLOEXEC)) < 0)
399 error("cannot open %s: %s", fname, strerror(errno));
401 fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 10);
404 error("Out of file descriptors");
407 setinputfd(fd, push);
413 * Like setinputfile, but takes an open file descriptor (which should have
414 * its FD_CLOEXEC flag already set). Call this with interrupts off.
418 setinputfd(int fd, int push)
422 parsefile->buf = ckmalloc(BUFSIZ + 1);
424 if (parsefile->fd > 0)
425 close(parsefile->fd);
427 if (parsefile->buf == NULL)
428 parsefile->buf = ckmalloc(BUFSIZ + 1);
429 parselleft = parsenleft = 0;
435 * Like setinputfile, but takes input from a string.
439 setinputstring(const char *string, int push)
445 parselleft = parsenleft = strlen(string);
446 parsefile->buf = NULL;
454 * To handle the "." command, a stack of input files is used. Pushfile
455 * adds a new entry to the stack and popfile restores the previous level.
461 struct parsefile *pf;
463 parsefile->nleft = parsenleft;
464 parsefile->lleft = parselleft;
465 parsefile->nextc = parsenextc;
466 parsefile->linno = plinno;
467 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
468 pf->prev = parsefile;
471 pf->basestrpush.prev = NULL;
479 struct parsefile *pf = parsefile;
488 parsefile = pf->prev;
490 parsenleft = parsefile->nleft;
491 parselleft = parsefile->lleft;
492 parsenextc = parsefile->nextc;
493 plinno = parsefile->linno;
499 * Return current file (to go back to it later using popfilesupto()).
510 * Pop files until the given file is on top again. Useful for regular
511 * builtins that read shell commands from files or strings.
512 * If the given file is not an active file, an error is raised.
516 popfilesupto(struct parsefile *file)
518 while (parsefile != file && parsefile != &basepf)
520 if (parsefile != file)
521 error("popfilesupto() misused");
525 * Return to top level.
531 while (parsefile != &basepf)
538 * Close the file(s) that the shell is reading commands from. Called
539 * after a fork is done.
546 if (parsefile->fd > 0) {
547 close(parsefile->fd);