xref: /freebsd/bin/sh/input.c (revision 50dad48bb740a8e56d185d9e8c165e0758f46e25)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
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  * 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.
19  *
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.
31  */
32 
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$");
40 
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>
47 
48 /*
49  * This file implements the input routines used by the parser.
50  */
51 
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 #include "myhistedit.h"
63 #include "trap.h"
64 
65 #define EOF_NLEFT -99		/* value of parsenleft when EOF pushed back */
66 
67 MKINIT
68 struct strpush {
69 	struct strpush *prev;	/* preceding string on stack */
70 	char *prevstring;
71 	int prevnleft;
72 	int prevlleft;
73 	struct alias *ap;	/* if push was associated with an alias */
74 };
75 
76 /*
77  * The parsefile structure pointed to by the global variable parsefile
78  * contains information about the current file being read.
79  */
80 
81 MKINIT
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 	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 */
92 };
93 
94 
95 int plinno = 1;			/* input line number */
96 int parsenleft;			/* copy of parsefile->nleft */
97 MKINIT int parselleft;		/* copy of parsefile->lleft */
98 char *parsenextc;		/* copy of parsefile->nextc */
99 MKINIT struct parsefile basepf;	/* top level input file */
100 char basebuf[BUFSIZ];		/* buffer for top level input file */
101 static struct parsefile *parsefile = &basepf;	/* current input file */
102 int init_editline = 0;		/* editline library initialized? */
103 int whichprompt;		/* 1 == PS1, 2 == PS2 */
104 
105 EditLine *el;			/* cookie for editline package */
106 
107 static void pushfile(void);
108 static int preadfd(void);
109 
110 #ifdef mkinit
111 INCLUDE "input.h"
112 INCLUDE "error.h"
113 
114 MKINIT char basebuf[];
115 
116 INIT {
117 	basepf.nextc = basepf.buf = basebuf;
118 }
119 
120 RESET {
121 	popallfiles();
122 	parselleft = parsenleft = 0;	/* clear input buffer */
123 }
124 #endif
125 
126 
127 /*
128  * Read a line from the script.
129  */
130 
131 char *
132 pfgets(char *line, int len)
133 {
134 	char *p = line;
135 	int nleft = len;
136 	int c;
137 
138 	while (--nleft > 0) {
139 		c = pgetc_macro();
140 		if (c == PEOF) {
141 			if (p == line)
142 				return NULL;
143 			break;
144 		}
145 		*p++ = c;
146 		if (c == '\n')
147 			break;
148 	}
149 	*p = '\0';
150 	return line;
151 }
152 
153 
154 
155 /*
156  * Read a character from the script, returning PEOF on end of file.
157  * Nul characters in the input are silently discarded.
158  */
159 
160 int
161 pgetc(void)
162 {
163 	return pgetc_macro();
164 }
165 
166 
167 static int
168 preadfd(void)
169 {
170 	int nr;
171 	parsenextc = parsefile->buf;
172 
173 #ifndef NO_HISTORY
174 	if (el != NULL && gotwinch) {
175 		gotwinch = 0;
176 		el_resize(el);
177 	}
178 #endif
179 retry:
180 #ifndef NO_HISTORY
181 	if (parsefile->fd == 0 && el) {
182 		static const char *rl_cp;
183 		static int el_len;
184 
185 		if (rl_cp == NULL)
186 			rl_cp = el_gets(el, &el_len);
187 		if (rl_cp == NULL)
188 			nr = 0;
189 		else {
190 			nr = el_len;
191 			if (nr > BUFSIZ - 1)
192 				nr = BUFSIZ - 1;
193 			memcpy(parsenextc, rl_cp, nr);
194 			if (nr != el_len) {
195 				el_len -= nr;
196 				rl_cp += nr;
197 			} else
198 				rl_cp = NULL;
199 		}
200 	} else
201 #endif
202 		nr = read(parsefile->fd, parsenextc, BUFSIZ - 1);
203 
204 	if (nr <= 0) {
205                 if (nr < 0) {
206                         if (errno == EINTR)
207                                 goto retry;
208                         if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
209                                 int flags = fcntl(0, F_GETFL, 0);
210                                 if (flags >= 0 && flags & O_NONBLOCK) {
211                                         flags &=~ O_NONBLOCK;
212                                         if (fcntl(0, F_SETFL, flags) >= 0) {
213 						out2fmt_flush("sh: turning off NDELAY mode\n");
214                                                 goto retry;
215                                         }
216                                 }
217                         }
218                 }
219                 nr = -1;
220 	}
221 	return nr;
222 }
223 
224 /*
225  * Refill the input buffer and return the next input character:
226  *
227  * 1) If a string was pushed back on the input, pop it;
228  * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
229  *    from a string so we can't refill the buffer, return EOF.
230  * 3) If there is more in this buffer, use it else call read to fill it.
231  * 4) Process input up to the next newline, deleting nul characters.
232  */
233 
234 int
235 preadbuffer(void)
236 {
237 	char *p, *q;
238 	int more;
239 	int something;
240 	char savec;
241 
242 	if (parsefile->strpush) {
243 		popstring();
244 		if (--parsenleft >= 0)
245 			return (*parsenextc++);
246 	}
247 	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
248 		return PEOF;
249 	flushout(&output);
250 	flushout(&errout);
251 
252 again:
253 	if (parselleft <= 0) {
254 		if ((parselleft = preadfd()) == -1) {
255 			parselleft = parsenleft = EOF_NLEFT;
256 			return PEOF;
257 		}
258 	}
259 
260 	q = p = parsenextc;
261 
262 	/* delete nul characters */
263 	something = 0;
264 	for (more = 1; more;) {
265 		switch (*p) {
266 		case '\0':
267 			p++;	/* Skip nul */
268 			goto check;
269 
270 		case '\t':
271 		case ' ':
272 			break;
273 
274 		case '\n':
275 			parsenleft = q - parsenextc;
276 			more = 0; /* Stop processing here */
277 			break;
278 
279 		default:
280 			something = 1;
281 			break;
282 		}
283 
284 		*q++ = *p++;
285 check:
286 		if (--parselleft <= 0) {
287 			parsenleft = q - parsenextc - 1;
288 			if (parsenleft < 0)
289 				goto again;
290 			*q = '\0';
291 			more = 0;
292 		}
293 	}
294 
295 	savec = *q;
296 	*q = '\0';
297 
298 #ifndef NO_HISTORY
299 	if (parsefile->fd == 0 && hist && something) {
300 		HistEvent he;
301 		INTOFF;
302 		history(hist, &he, whichprompt == 1 ? H_ENTER : H_ADD,
303 		    parsenextc);
304 		INTON;
305 	}
306 #endif
307 
308 	if (vflag) {
309 		out2str(parsenextc);
310 		flushout(out2);
311 	}
312 
313 	*q = savec;
314 
315 	return *parsenextc++;
316 }
317 
318 /*
319  * Returns if we are certain we are at EOF. Does not cause any more input
320  * to be read from the outside world.
321  */
322 
323 int
324 preadateof(void)
325 {
326 	if (parsenleft > 0)
327 		return 0;
328 	if (parsefile->strpush)
329 		return 0;
330 	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
331 		return 1;
332 	return 0;
333 }
334 
335 /*
336  * Undo the last call to pgetc.  Only one character may be pushed back.
337  * PEOF may be pushed back.
338  */
339 
340 void
341 pungetc(void)
342 {
343 	parsenleft++;
344 	parsenextc--;
345 }
346 
347 /*
348  * Push a string back onto the input at this current parsefile level.
349  * We handle aliases this way.
350  */
351 void
352 pushstring(char *s, int len, void *ap)
353 {
354 	struct strpush *sp;
355 
356 	INTOFF;
357 /*out2fmt_flush("*** calling pushstring: %s, %d\n", s, len);*/
358 	if (parsefile->strpush) {
359 		sp = ckmalloc(sizeof (struct strpush));
360 		sp->prev = parsefile->strpush;
361 		parsefile->strpush = sp;
362 	} else
363 		sp = parsefile->strpush = &(parsefile->basestrpush);
364 	sp->prevstring = parsenextc;
365 	sp->prevnleft = parsenleft;
366 	sp->prevlleft = parselleft;
367 	sp->ap = (struct alias *)ap;
368 	if (ap)
369 		((struct alias *)ap)->flag |= ALIASINUSE;
370 	parsenextc = s;
371 	parsenleft = len;
372 	INTON;
373 }
374 
375 void
376 popstring(void)
377 {
378 	struct strpush *sp = parsefile->strpush;
379 
380 	INTOFF;
381 	parsenextc = sp->prevstring;
382 	parsenleft = sp->prevnleft;
383 	parselleft = sp->prevlleft;
384 /*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/
385 	if (sp->ap)
386 		sp->ap->flag &= ~ALIASINUSE;
387 	parsefile->strpush = sp->prev;
388 	if (sp != &(parsefile->basestrpush))
389 		ckfree(sp);
390 	INTON;
391 }
392 
393 /*
394  * Set the input to take input from a file.  If push is set, push the
395  * old input onto the stack first.
396  */
397 
398 void
399 setinputfile(const char *fname, int push)
400 {
401 	int fd;
402 	int fd2;
403 
404 	INTOFF;
405 	if ((fd = open(fname, O_RDONLY)) < 0)
406 		error("Can't open %s: %s", fname, strerror(errno));
407 	if (fd < 10) {
408 		fd2 = fcntl(fd, F_DUPFD, 10);
409 		close(fd);
410 		if (fd2 < 0)
411 			error("Out of file descriptors");
412 		fd = fd2;
413 	}
414 	setinputfd(fd, push);
415 	INTON;
416 }
417 
418 
419 /*
420  * Like setinputfile, but takes an open file descriptor.  Call this with
421  * interrupts off.
422  */
423 
424 void
425 setinputfd(int fd, int push)
426 {
427 	(void)fcntl(fd, F_SETFD, FD_CLOEXEC);
428 	if (push) {
429 		pushfile();
430 		parsefile->buf = ckmalloc(BUFSIZ);
431 	}
432 	if (parsefile->fd > 0)
433 		close(parsefile->fd);
434 	parsefile->fd = fd;
435 	if (parsefile->buf == NULL)
436 		parsefile->buf = ckmalloc(BUFSIZ);
437 	parselleft = parsenleft = 0;
438 	plinno = 1;
439 }
440 
441 
442 /*
443  * Like setinputfile, but takes input from a string.
444  */
445 
446 void
447 setinputstring(char *string, int push)
448 {
449 	INTOFF;
450 	if (push)
451 		pushfile();
452 	parsenextc = string;
453 	parselleft = parsenleft = strlen(string);
454 	parsefile->buf = NULL;
455 	plinno = 1;
456 	INTON;
457 }
458 
459 
460 
461 /*
462  * To handle the "." command, a stack of input files is used.  Pushfile
463  * adds a new entry to the stack and popfile restores the previous level.
464  */
465 
466 static void
467 pushfile(void)
468 {
469 	struct parsefile *pf;
470 
471 	parsefile->nleft = parsenleft;
472 	parsefile->lleft = parselleft;
473 	parsefile->nextc = parsenextc;
474 	parsefile->linno = plinno;
475 	pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
476 	pf->prev = parsefile;
477 	pf->fd = -1;
478 	pf->strpush = NULL;
479 	pf->basestrpush.prev = NULL;
480 	parsefile = pf;
481 }
482 
483 
484 void
485 popfile(void)
486 {
487 	struct parsefile *pf = parsefile;
488 
489 	INTOFF;
490 	if (pf->fd >= 0)
491 		close(pf->fd);
492 	if (pf->buf)
493 		ckfree(pf->buf);
494 	while (pf->strpush)
495 		popstring();
496 	parsefile = pf->prev;
497 	ckfree(pf);
498 	parsenleft = parsefile->nleft;
499 	parselleft = parsefile->lleft;
500 	parsenextc = parsefile->nextc;
501 	plinno = parsefile->linno;
502 	INTON;
503 }
504 
505 
506 /*
507  * Return current file (to go back to it later using popfilesupto()).
508  */
509 
510 struct parsefile *
511 getcurrentfile(void)
512 {
513 	return parsefile;
514 }
515 
516 
517 /*
518  * Pop files until the given file is on top again. Useful for regular
519  * builtins that read shell commands from files or strings.
520  * If the given file is not an active file, an error is raised.
521  */
522 
523 void
524 popfilesupto(struct parsefile *file)
525 {
526 	while (parsefile != file && parsefile != &basepf)
527 		popfile();
528 	if (parsefile != file)
529 		error("popfilesupto() misused");
530 }
531 
532 /*
533  * Return to top level.
534  */
535 
536 void
537 popallfiles(void)
538 {
539 	while (parsefile != &basepf)
540 		popfile();
541 }
542 
543 
544 
545 /*
546  * Close the file(s) that the shell is reading commands from.  Called
547  * after a fork is done.
548  */
549 
550 void
551 closescript(void)
552 {
553 	popallfiles();
554 	if (parsefile->fd > 0) {
555 		close(parsefile->fd);
556 		parsefile->fd = 0;
557 	}
558 }
559