kernel - Fix serious bug in MAP_STACK, deprecate auto-grow semantics
[dragonfly.git] / bin / sh / input.c
blob51071a855f9aa8bfe03d326c777f43863db57083
1 /*-
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
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)input.c 8.3 (Berkeley) 6/9/95";
36 #endif
37 #endif /* not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <stdio.h> /* defines BUFSIZ */
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <string.h>
49 * This file implements the input routines used by the parser.
52 #include "shell.h"
53 #include "redir.h"
54 #include "syntax.h"
55 #include "input.h"
56 #include "output.h"
57 #include "options.h"
58 #include "memalloc.h"
59 #include "error.h"
60 #include "alias.h"
61 #include "parser.h"
62 #ifndef NO_HISTORY
63 #include "myhistedit.h"
64 #endif
65 #include "trap.h"
67 #define EOF_NLEFT -99 /* value of parsenleft when EOF pushed back */
69 struct strpush {
70 struct strpush *prev; /* preceding string on stack */
71 const char *prevstring;
72 int prevnleft;
73 int prevlleft;
74 struct alias *ap; /* if push was associated with an alias */
78 * The parsefile structure pointed to by the global variable parsefile
79 * contains information about the current file being read.
82 struct parsefile {
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 const 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 int parsenleft; /* copy of parsefile->nleft */
97 static int parselleft; /* copy of parsefile->lleft */
98 const char *parsenextc; /* copy of parsefile->nextc */
99 static char basebuf[BUFSIZ + 1];/* buffer for top level input file */
100 static struct parsefile basepf = { /* top level input file */
101 .nextc = basebuf,
102 .buf = basebuf
104 static struct parsefile *parsefile = &basepf; /* current input file */
105 int whichprompt; /* 1 == PS1, 2 == PS2 */
107 #ifndef NO_HISTORY
108 EditLine *el; /* cookie for editline package */
109 #endif
111 static void pushfile(void);
112 static int preadfd(void);
113 static void popstring(void);
115 void
116 resetinput(void)
118 popallfiles();
119 parselleft = parsenleft = 0; /* clear input buffer */
125 * Read a character from the script, returning PEOF on end of file.
126 * Nul characters in the input are silently discarded.
130 pgetc(void)
132 return pgetc_macro();
136 static int
137 preadfd(void)
139 int nr;
140 parsenextc = parsefile->buf;
142 retry:
143 #ifndef NO_HISTORY
144 if (parsefile->fd == 0 && el) {
145 static const char *rl_cp;
146 static int el_len;
148 if (rl_cp == NULL) {
149 el_resize(el);
150 rl_cp = el_gets(el, &el_len);
152 if (rl_cp == NULL)
153 nr = el_len == 0 ? 0 : -1;
154 else {
155 nr = el_len;
156 if (nr > BUFSIZ)
157 nr = BUFSIZ;
158 memcpy(parsefile->buf, rl_cp, nr);
159 if (nr != el_len) {
160 el_len -= nr;
161 rl_cp += nr;
162 } else
163 rl_cp = NULL;
165 } else
166 #endif
167 nr = read(parsefile->fd, parsefile->buf, BUFSIZ);
169 if (nr <= 0) {
170 if (nr < 0) {
171 if (errno == EINTR)
172 goto retry;
173 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
174 int flags = fcntl(0, F_GETFL, 0);
175 if (flags >= 0 && flags & O_NONBLOCK) {
176 flags &=~ O_NONBLOCK;
177 if (fcntl(0, F_SETFL, flags) >= 0) {
178 out2fmt_flush("sh: turning off NDELAY mode\n");
179 goto retry;
184 nr = -1;
186 return nr;
190 * Refill the input buffer and return the next input character:
192 * 1) If a string was pushed back on the input, pop it;
193 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
194 * from a string so we can't refill the buffer, return EOF.
195 * 3) If there is more in this buffer, use it else call read to fill it.
196 * 4) Process input up to the next newline, deleting nul characters.
200 preadbuffer(void)
202 char *p, *q;
203 int more;
204 char savec;
206 while (parsefile->strpush) {
208 * Add a space to the end of an alias to ensure that the
209 * alias remains in use while parsing its last word.
210 * This avoids alias recursions.
212 if (parsenleft == -1 && parsefile->strpush->ap != NULL)
213 return ' ';
214 popstring();
215 if (--parsenleft >= 0)
216 return (*parsenextc++);
218 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
219 return PEOF;
220 flushout(&output);
221 flushout(&errout);
223 again:
224 if (parselleft <= 0) {
225 if ((parselleft = preadfd()) == -1) {
226 parselleft = parsenleft = EOF_NLEFT;
227 return PEOF;
231 q = p = parsefile->buf + (parsenextc - parsefile->buf);
233 /* delete nul characters */
234 for (more = 1; more;) {
235 switch (*p) {
236 case '\0':
237 p++; /* Skip nul */
238 goto check;
240 case '\n':
241 parsenleft = q - parsenextc;
242 more = 0; /* Stop processing here */
243 break;
245 default:
246 break;
249 *q++ = *p++;
250 check:
251 if (--parselleft <= 0) {
252 parsenleft = q - parsenextc - 1;
253 if (parsenleft < 0)
254 goto again;
255 *q = '\0';
256 more = 0;
260 savec = *q;
261 *q = '\0';
263 #ifndef NO_HISTORY
264 if (parsefile->fd == 0 && hist &&
265 parsenextc[strspn(parsenextc, " \t\n")] != '\0') {
266 HistEvent he;
267 INTOFF;
268 history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD,
269 parsenextc);
270 INTON;
272 #endif
274 if (vflag) {
275 out2str(parsenextc);
276 flushout(out2);
279 *q = savec;
281 return *parsenextc++;
285 * Returns if we are certain we are at EOF. Does not cause any more input
286 * to be read from the outside world.
290 preadateof(void)
292 if (parsenleft > 0)
293 return 0;
294 if (parsefile->strpush)
295 return 0;
296 if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
297 return 1;
298 return 0;
302 * Undo the last call to pgetc. Only one character may be pushed back.
303 * PEOF may be pushed back.
306 void
307 pungetc(void)
309 parsenleft++;
310 parsenextc--;
314 * Push a string back onto the input at this current parsefile level.
315 * We handle aliases this way.
317 void
318 pushstring(const char *s, int len, struct alias *ap)
320 struct strpush *sp;
322 INTOFF;
323 /*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/
324 if (parsefile->strpush) {
325 sp = ckmalloc(sizeof (struct strpush));
326 sp->prev = parsefile->strpush;
327 parsefile->strpush = sp;
328 } else
329 sp = parsefile->strpush = &(parsefile->basestrpush);
330 sp->prevstring = parsenextc;
331 sp->prevnleft = parsenleft;
332 sp->prevlleft = parselleft;
333 sp->ap = ap;
334 if (ap)
335 ap->flag |= ALIASINUSE;
336 parsenextc = s;
337 parsenleft = len;
338 INTON;
341 static void
342 popstring(void)
344 struct strpush *sp = parsefile->strpush;
346 INTOFF;
347 if (sp->ap) {
348 if (parsenextc != sp->ap->val &&
349 (parsenextc[-1] == ' ' || parsenextc[-1] == '\t'))
350 forcealias();
351 sp->ap->flag &= ~ALIASINUSE;
353 parsenextc = sp->prevstring;
354 parsenleft = sp->prevnleft;
355 parselleft = sp->prevlleft;
356 /*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/
357 parsefile->strpush = sp->prev;
358 if (sp != &(parsefile->basestrpush))
359 ckfree(sp);
360 INTON;
364 * Set the input to take input from a file. If push is set, push the
365 * old input onto the stack first.
368 void
369 setinputfile(const char *fname, int push)
371 int fd;
372 int fd2;
374 INTOFF;
375 if ((fd = open(fname, O_RDONLY | O_CLOEXEC_MAYBE)) < 0)
376 error("cannot open %s: %s", fname, strerror(errno));
377 if (fd < 10) {
378 fd2 = fcntl(fd, F_DUPFD_CLOEXEC_MAYBE, 10);
379 close(fd);
380 if (fd2 < 0)
381 error("Out of file descriptors");
382 fd = fd2;
384 setinputfd(fd, push);
385 INTON;
390 * Like setinputfile, but takes an open file descriptor (which should have
391 * its FD_CLOEXEC flag already set). Call this with interrupts off.
394 void
395 setinputfd(int fd, int push)
397 #if !defined(O_CLOEXEC) || !defined(F_DUPFD_CLOEXEC)
398 fcntl(fd, F_SETFD, FD_CLOEXEC);
399 #endif
400 if (push) {
401 pushfile();
402 parsefile->buf = ckmalloc(BUFSIZ + 1);
404 if (parsefile->fd > 0)
405 close(parsefile->fd);
406 parsefile->fd = fd;
407 if (parsefile->buf == NULL)
408 parsefile->buf = ckmalloc(BUFSIZ + 1);
409 parselleft = parsenleft = 0;
410 plinno = 1;
415 * Like setinputfile, but takes input from a string.
418 void
419 setinputstring(const char *string, int push)
421 INTOFF;
422 if (push)
423 pushfile();
424 parsenextc = string;
425 parselleft = parsenleft = strlen(string);
426 parsefile->buf = NULL;
427 plinno = 1;
428 INTON;
434 * To handle the "." command, a stack of input files is used. Pushfile
435 * adds a new entry to the stack and popfile restores the previous level.
438 static void
439 pushfile(void)
441 struct parsefile *pf;
443 parsefile->nleft = parsenleft;
444 parsefile->lleft = parselleft;
445 parsefile->nextc = parsenextc;
446 parsefile->linno = plinno;
447 pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
448 pf->prev = parsefile;
449 pf->fd = -1;
450 pf->strpush = NULL;
451 pf->basestrpush.prev = NULL;
452 parsefile = pf;
456 void
457 popfile(void)
459 struct parsefile *pf = parsefile;
461 INTOFF;
462 if (pf->fd >= 0)
463 close(pf->fd);
464 if (pf->buf)
465 ckfree(pf->buf);
466 while (pf->strpush)
467 popstring();
468 parsefile = pf->prev;
469 ckfree(pf);
470 parsenleft = parsefile->nleft;
471 parselleft = parsefile->lleft;
472 parsenextc = parsefile->nextc;
473 plinno = parsefile->linno;
474 INTON;
479 * Return current file (to go back to it later using popfilesupto()).
482 struct parsefile *
483 getcurrentfile(void)
485 return parsefile;
490 * Pop files until the given file is on top again. Useful for regular
491 * builtins that read shell commands from files or strings.
492 * If the given file is not an active file, an error is raised.
495 void
496 popfilesupto(struct parsefile *file)
498 while (parsefile != file && parsefile != &basepf)
499 popfile();
500 if (parsefile != file)
501 error("popfilesupto() misused");
505 * Return to top level.
508 void
509 popallfiles(void)
511 while (parsefile != &basepf)
512 popfile();
518 * Close the file(s) that the shell is reading commands from. Called
519 * after a fork is done.
522 void
523 closescript(void)
525 popallfiles();
526 if (parsefile->fd > 0) {
527 close(parsefile->fd);
528 parsefile->fd = 0;