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
33 #include <stdio.h> /* defines BUFSIZ */
41 * This file implements the input routines used by the parser.
55 #include "myhistedit.h"
59 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
62 struct strpush *prev; /* preceding string on stack */
63 const char *prevstring;
66 struct alias *ap; /* if push was associated with an alias */
70 * The parsefile structure pointed to by the global variable parsefile
71 * contains information about the current file being read.
75 struct parsefile *prev; /* preceding file on stack */
76 int linno; /* current line */
77 int fd; /* file descriptor (or -1 if string) */
78 int nleft; /* number of chars left in this line */
79 int lleft; /* number of lines left in this buffer */
80 const char *nextc; /* next char in buffer */
81 char *buf; /* input buffer */
82 struct strpush *strpush; /* for pushing strings at this level */
83 struct strpush basestrpush; /* so pushing one is fast */
87 int plinno = 1; /* input line number */
88 int parsenleft; /* copy of parsefile->nleft */
89 static int parselleft; /* copy of parsefile->lleft */
90 const char *parsenextc; /* copy of parsefile->nextc */
91 static char basebuf[BUFSIZ + 1];/* buffer for top level input file */
92 static struct parsefile basepf = { /* top level input file */
96 static struct parsefile *parsefile = &basepf; /* current input file */
97 int whichprompt; /* 1 == PS1, 2 == PS2 */
99 static void pushfile(void);
100 static int preadfd(void);
101 static void popstring(void);
107 parselleft = parsenleft = 0; /* clear input buffer */
113 * Read a character from the script, returning PEOF on end of file.
114 * Nul characters in the input are silently discarded.
120 return pgetc_macro();
128 parsenextc = parsefile->buf;
132 if (parsefile->fd == 0 && el) {
133 static const char *rl_cp;
138 rl_cp = el_gets(el, &el_len);
141 nr = el_len == 0 ? 0 : -1;
146 memcpy(parsefile->buf, rl_cp, nr);
155 nr = read(parsefile->fd, parsefile->buf, BUFSIZ);
161 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
162 int flags = fcntl(0, F_GETFL, 0);
163 if (flags >= 0 && flags & O_NONBLOCK) {
164 flags &=~ O_NONBLOCK;
165 if (fcntl(0, F_SETFL, flags) >= 0) {
166 out2fmt_flush("sh: turning off NDELAY mode\n");
178 * Refill the input buffer and return the next input character:
180 * 1) If a string was pushed back on the input, pop it;
181 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
182 * from a string so we can't refill the buffer, return EOF.
183 * 3) If there is more in this buffer, use it else call read to fill it.
184 * 4) Process input up to the next newline, deleting nul characters.
190 char *p, *q, *r, *end;
193 while (parsefile->strpush) {
195 * Add a space to the end of an alias to ensure that the
196 * alias remains in use while parsing its last word.
197 * This avoids alias recursions.
199 if (parsenleft == -1 && parsefile->strpush->ap != NULL)
202 if (--parsenleft >= 0)
203 return (*parsenextc++);
205 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
209 if (parselleft <= 0) {
210 if ((parselleft = preadfd()) == -1) {
211 parselleft = parsenleft = EOF_NLEFT;
216 p = parsefile->buf + (parsenextc - parsefile->buf);
217 end = p + parselleft;
219 q = strchrnul(p, '\n');
220 if (q != end && *q == '\0') {
221 /* delete nul characters */
222 for (r = q; q != end; q++) {
226 parselleft -= end - r;
229 end = p + parselleft;
231 q = strchrnul(p, '\n');
234 parsenleft = parselleft;
236 } else /* *q == '\n' */ {
238 parsenleft = q - parsenextc;
239 parselleft -= parsenleft;
247 if (parsefile->fd == 0 && hist &&
248 parsenextc[strspn(parsenextc, " \t\n")] != '\0') {
251 history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD,
264 return *parsenextc++;
268 * Returns if we are certain we are at EOF. Does not cause any more input
269 * to be read from the outside world.
277 if (parsefile->strpush)
279 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
285 * Undo the last call to pgetc. Only one character may be pushed back.
286 * PEOF may be pushed back.
297 * Push a string back onto the input at this current parsefile level.
298 * We handle aliases this way.
301 pushstring(const char *s, int len, struct alias *ap)
306 /*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/
307 if (parsefile->strpush) {
308 sp = ckmalloc(sizeof (struct strpush));
309 sp->prev = parsefile->strpush;
310 parsefile->strpush = sp;
312 sp = parsefile->strpush = &(parsefile->basestrpush);
313 sp->prevstring = parsenextc;
314 sp->prevnleft = parsenleft;
315 sp->prevlleft = parselleft;
318 ap->flag |= ALIASINUSE;
327 struct strpush *sp = parsefile->strpush;
331 if (parsenextc != sp->ap->val &&
332 (parsenextc[-1] == ' ' || parsenextc[-1] == '\t'))
334 sp->ap->flag &= ~ALIASINUSE;
336 parsenextc = sp->prevstring;
337 parsenleft = sp->prevnleft;
338 parselleft = sp->prevlleft;
339 /*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/
340 parsefile->strpush = sp->prev;
341 if (sp != &(parsefile->basestrpush))
347 * Set the input to take input from a file. If push is set, push the
348 * old input onto the stack first.
350 * -1: Obey verifyflag
356 setinputfile(const char *fname, int push, int verify)
361 int oflags = O_RDONLY | O_CLOEXEC;
363 if (verify == 1 || (verify == -1 && verifyflag))
367 if ((fd = open(fname, oflags)) < 0) {
369 errorwithstatus(e == ENOENT || e == ENOTDIR ? 127 : 126,
370 "cannot open %s: %s", fname, strerror(e));
373 fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 10);
376 error("Out of file descriptors");
379 setinputfd(fd, push);
385 * Like setinputfile, but takes an open file descriptor (which should have
386 * its FD_CLOEXEC flag already set). Call this with interrupts off.
390 setinputfd(int fd, int push)
394 parsefile->buf = ckmalloc(BUFSIZ + 1);
396 if (parsefile->fd > 0)
397 close(parsefile->fd);
399 if (parsefile->buf == NULL)
400 parsefile->buf = ckmalloc(BUFSIZ + 1);
401 parselleft = parsenleft = 0;
407 * Like setinputfile, but takes input from a string.
411 setinputstring(const char *string, int push)
417 parselleft = parsenleft = strlen(string);
418 parsefile->buf = NULL;
426 * To handle the "." command, a stack of input files is used. Pushfile
427 * adds a new entry to the stack and popfile restores the previous level.
433 struct parsefile *pf;
435 parsefile->nleft = parsenleft;
436 parsefile->lleft = parselleft;
437 parsefile->nextc = parsenextc;
438 parsefile->linno = plinno;
439 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
440 pf->prev = parsefile;
443 pf->basestrpush.prev = NULL;
451 struct parsefile *pf = parsefile;
460 parsefile = pf->prev;
462 parsenleft = parsefile->nleft;
463 parselleft = parsefile->lleft;
464 parsenextc = parsefile->nextc;
465 plinno = parsefile->linno;
471 * Return current file (to go back to it later using popfilesupto()).
482 * Pop files until the given file is on top again. Useful for regular
483 * builtins that read shell commands from files or strings.
484 * If the given file is not an active file, an error is raised.
488 popfilesupto(struct parsefile *file)
490 while (parsefile != file && parsefile != &basepf)
492 if (parsefile != file)
493 error("popfilesupto() misused");
497 * Return to top level.
503 while (parsefile != &basepf)
510 * Close the file(s) that the shell is reading commands from. Called
511 * after a fork is done.
518 if (parsefile->fd > 0) {
519 close(parsefile->fd);