input.c revision 1.63
1/*	$NetBSD: input.c,v 1.63 2018/08/19 23:50:27 kre Exp $	*/
2
3/*-
4 * Copyright (c) 1991, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)input.c	8.3 (Berkeley) 6/9/95";
39#else
40__RCSID("$NetBSD: input.c,v 1.63 2018/08/19 23:50:27 kre Exp $");
41#endif
42#endif /* not lint */
43
44#include <stdio.h>	/* defines BUFSIZ */
45#include <fcntl.h>
46#include <errno.h>
47#include <unistd.h>
48#include <limits.h>
49#include <stdlib.h>
50#include <string.h>
51#include <sys/stat.h>
52
53/*
54 * This file implements the input routines used by the parser.
55 */
56
57#include "shell.h"
58#include "redir.h"
59#include "syntax.h"
60#include "input.h"
61#include "output.h"
62#include "options.h"
63#include "memalloc.h"
64#include "error.h"
65#include "alias.h"
66#include "parser.h"
67#include "myhistedit.h"
68#include "show.h"
69
70#define EOF_NLEFT -99		/* value of parsenleft when EOF pushed back */
71
72MKINIT
73struct strpush {
74	struct strpush *prev;	/* preceding string on stack */
75	const char *prevstring;
76	int prevnleft;
77	int prevlleft;
78	struct alias *ap;	/* if push was associated with an alias */
79};
80
81/*
82 * The parsefile structure pointed to by the global variable parsefile
83 * contains information about the current file being read.
84 */
85
86MKINIT
87struct parsefile {
88	struct parsefile *prev;	/* preceding file on stack */
89	int linno;		/* current line */
90	int fd;			/* file descriptor (or -1 if string) */
91	int nleft;		/* number of chars left in this line */
92	int lleft;		/* number of chars left in this buffer */
93	const char *nextc;	/* next char in buffer */
94	char *buf;		/* input buffer */
95	struct strpush *strpush; /* for pushing strings at this level */
96	struct strpush basestrpush; /* so pushing one is fast */
97};
98
99
100int plinno = 1;			/* input line number */
101int parsenleft;			/* copy of parsefile->nleft */
102MKINIT int parselleft;		/* copy of parsefile->lleft */
103const char *parsenextc;		/* copy of parsefile->nextc */
104MKINIT struct parsefile basepf;	/* top level input file */
105MKINIT char basebuf[BUFSIZ];	/* buffer for top level input file */
106struct parsefile *parsefile = &basepf;	/* current input file */
107int init_editline = 0;		/* editline library initialized? */
108int whichprompt;		/* 1 == PS1, 2 == PS2 */
109
110STATIC void pushfile(void);
111static int preadfd(void);
112
113#ifdef mkinit
114INCLUDE <stdio.h>
115INCLUDE "input.h"
116INCLUDE "error.h"
117
118INIT {
119	basepf.nextc = basepf.buf = basebuf;
120}
121
122RESET {
123	if (exception != EXSHELLPROC)
124		parselleft = parsenleft = 0;	/* clear input buffer */
125	popallfiles();
126}
127
128SHELLPROC {
129	popallfiles();
130}
131#endif
132
133
134#if 0		/* this is unused */
135/*
136 * Read a line from the script.
137 */
138
139char *
140pfgets(char *line, int len)
141{
142	char *p = line;
143	int nleft = len;
144	int c;
145
146	while (--nleft > 0) {
147		c = pgetc_macro();
148		if (c == PEOF) {
149			if (p == line)
150				return NULL;
151			break;
152		}
153		*p++ = c;
154		if (c == '\n')
155			break;
156	}
157	*p = '\0';
158	return line;
159}
160#endif
161
162
163/*
164 * Read a character from the script, returning PEOF on end of file.
165 * Nul characters in the input are silently discarded.
166 */
167
168int
169pgetc(void)
170{
171	return pgetc_macro();
172}
173
174
175static int
176preadfd(void)
177{
178	int nr;
179	char *buf =  parsefile->buf;
180	parsenextc = buf;
181
182 retry:
183#ifndef SMALL
184	if (parsefile->fd == 0 && el) {
185		static const char *rl_cp;
186		static int el_len;
187
188		if (rl_cp == NULL)
189			rl_cp = el_gets(el, &el_len);
190		if (rl_cp == NULL)
191			nr = el_len == 0 ? 0 : -1;
192		else {
193			nr = el_len;
194			if (nr > BUFSIZ - 8)
195				nr = BUFSIZ - 8;
196			memcpy(buf, rl_cp, nr);
197			if (nr != el_len) {
198				el_len -= nr;
199				rl_cp += nr;
200			} else
201				rl_cp = 0;
202		}
203
204	} else
205#endif
206		nr = read(parsefile->fd, buf, BUFSIZ - 8);
207
208
209	if (nr <= 0) {
210                if (nr < 0) {
211                        if (errno == EINTR)
212                                goto retry;
213                        if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
214                                int flags = fcntl(0, F_GETFL, 0);
215
216                                if (flags >= 0 && flags & O_NONBLOCK) {
217                                        flags &=~ O_NONBLOCK;
218                                        if (fcntl(0, F_SETFL, flags) >= 0) {
219						out2str("sh: turning off NDELAY mode\n");
220                                                goto retry;
221                                        }
222                                }
223                        }
224                }
225                nr = -1;
226	}
227	return nr;
228}
229
230/*
231 * Refill the input buffer and return the next input character:
232 *
233 * 1) If a string was pushed back on the input, pop it;
234 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
235 *    from a string so we can't refill the buffer, return EOF.
236 * 3) If the is more stuff in this buffer, use it else call read to fill it.
237 * 4) Process input up to the next newline, deleting nul characters.
238 */
239
240int
241preadbuffer(void)
242{
243	char *p, *q;
244	int more;
245#ifndef SMALL
246	int something;
247#endif
248	char savec;
249
250	while (parsefile->strpush) {
251		popstring();
252		if (--parsenleft >= 0)
253			return (*parsenextc++);
254	}
255	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
256		return PEOF;
257	flushout(&output);
258	flushout(&errout);
259
260 again:
261	if (parselleft <= 0) {
262		if ((parselleft = preadfd()) == -1) {
263			parselleft = parsenleft = EOF_NLEFT;
264			return PEOF;
265		}
266	}
267
268		/* p = (not const char *)parsenextc; */
269	p = parsefile->buf + (parsenextc - parsefile->buf);
270	q = p;
271
272	/* delete nul characters */
273#ifndef SMALL
274	something = 0;
275#endif
276	for (more = 1; more;) {
277		switch (*p) {
278		case '\0':
279			p++;	/* Skip nul */
280			goto check;
281
282		case '\t':
283		case ' ':
284			break;
285
286		case '\n':
287			parsenleft = q - parsenextc;
288			more = 0; /* Stop processing here */
289			break;
290
291		default:
292#ifndef SMALL
293			something = 1;
294#endif
295			break;
296		}
297
298		*q++ = *p++;
299 check:
300		if (--parselleft <= 0) {
301			parsenleft = q - parsenextc - 1;
302			if (parsenleft < 0)
303				goto again;
304			*q = '\0';
305			more = 0;
306		}
307	}
308
309	savec = *q;
310	*q = '\0';
311
312#ifndef SMALL
313	if (parsefile->fd == 0 && hist && (something || whichprompt == 2)) {
314		HistEvent he;
315
316		INTOFF;
317		history(hist, &he, whichprompt != 2 ? H_ENTER : H_APPEND,
318		    parsenextc);
319		INTON;
320	}
321#endif
322
323	if (vflag) {
324		out2str(parsenextc);
325		flushout(out2);
326	}
327
328	*q = savec;
329
330	return *parsenextc++;
331}
332
333/*
334 * Test whether we have reached EOF on input stream.
335 * Return true only if certain (without attempting a read).
336 *
337 * Note the similarity to the opening section of preadbuffer()
338 */
339int
340at_eof(void)
341{
342	struct strpush *sp = parsefile->strpush;
343
344	if (parsenleft > 0)	/* more chars are in the buffer */
345		return 0;
346
347	while (sp != NULL) {
348		/*
349		 * If any pushed string has any remaining data,
350		 * then we are not at EOF  (simulating popstring())
351		 */
352		if (sp->prevnleft > 0)
353			return 0;
354		sp = sp->prev;
355	}
356
357	/*
358	 * If we reached real EOF and pushed it back,
359	 * or if we are just processing a string (not reading a file)
360	 * then there is no more.   Note that if a file pushes a
361	 * string, the file's ->buf remains present.
362	 */
363	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
364		return 1;
365
366	/*
367	 * In other cases, there might be more
368	 */
369	return 0;
370}
371
372/*
373 * Undo the last call to pgetc.  Only one character may be pushed back.
374 * PEOF may be pushed back.
375 */
376
377void
378pungetc(void)
379{
380	parsenleft++;
381	parsenextc--;
382}
383
384/*
385 * Push a string back onto the input at this current parsefile level.
386 * We handle aliases this way.
387 */
388void
389pushstring(const char *s, int len, struct alias *ap)
390{
391	struct strpush *sp;
392
393	VTRACE(DBG_INPUT,
394	    ("pushstring(\"%.*s\", %d)%s%s%s had: nl=%d ll=%d \"%.*s\"\n",
395	    len, s, len, ap ? " for alias:'" : "",
396	    ap ? ap->name : "", ap ? "'" : "",
397	    parsenleft, parselleft, parsenleft, parsenextc));
398
399	INTOFF;
400	if (parsefile->strpush) {
401		sp = ckmalloc(sizeof (struct strpush));
402		sp->prev = parsefile->strpush;
403		parsefile->strpush = sp;
404	} else
405		sp = parsefile->strpush = &(parsefile->basestrpush);
406
407	sp->prevstring = parsenextc;
408	sp->prevnleft = parsenleft;
409	sp->prevlleft = parselleft;
410	sp->ap = ap;
411	if (ap)
412		ap->flag |= ALIASINUSE;
413	parsenextc = s;
414	parsenleft = len;
415	INTON;
416}
417
418void
419popstring(void)
420{
421	struct strpush *sp = parsefile->strpush;
422
423	INTOFF;
424	parsenextc = sp->prevstring;
425	parsenleft = sp->prevnleft;
426	parselleft = sp->prevlleft;
427
428	VTRACE(DBG_INPUT, ("popstring()%s%s%s nl=%d ll=%d \"%.*s\"\n",
429	    sp->ap ? " from alias:'" : "", sp->ap ? sp->ap->name : "",
430	    sp->ap ? "'" : "", parsenleft, parselleft, parsenleft, parsenextc));
431
432	if (sp->ap)
433		sp->ap->flag &= ~ALIASINUSE;
434	parsefile->strpush = sp->prev;
435	if (sp != &(parsefile->basestrpush))
436		ckfree(sp);
437	INTON;
438}
439
440/*
441 * Set the input to take input from a file.  If push is set, push the
442 * old input onto the stack first.
443 */
444
445void
446setinputfile(const char *fname, int push)
447{
448	unsigned char magic[4];
449	int fd;
450	int fd2;
451	struct stat sb;
452
453	CTRACE(DBG_INPUT,("setinputfile(\"%s\", %spush)\n",fname,push?"":"no"));
454
455	INTOFF;
456	if ((fd = open(fname, O_RDONLY)) < 0)
457		error("Can't open %s", fname);
458
459	/* Since the message "Syntax error: "(" unexpected" is not very
460	 * helpful, we check if the file starts with the ELF magic to
461	 * avoid that message. The first lseek tries to make sure that
462	 * we can later rewind the file.
463	 */
464	if (fstat(fd, &sb) == 0 && S_ISREG(sb.st_mode) &&
465	    lseek(fd, 0, SEEK_SET) == 0) {
466		if (read(fd, magic, 4) == 4) {
467			if (memcmp(magic, "\177ELF", 4) == 0) {
468				(void)close(fd);
469				error("Cannot execute ELF binary %s", fname);
470			}
471		}
472		if (lseek(fd, 0, SEEK_SET) != 0) {
473			(void)close(fd);
474			error("Cannot rewind the file %s", fname);
475		}
476	}
477
478	fd2 = to_upper_fd(fd);	/* closes fd, returns higher equiv */
479	if (fd2 == fd) {
480		(void) close(fd);
481		error("Out of file descriptors");
482	}
483
484	setinputfd(fd2, push);
485	INTON;
486}
487
488/*
489 * When a shell fd needs to be altered (when the user wants to use
490 * the same fd - rare, but happens - we need to locate the ref to
491 * the fd, and update it.  This happens via a callback.
492 * This is the callback func for fd's used for shell input
493 */
494static void
495input_fd_swap(int from, int to)
496{
497	struct parsefile *pf;
498
499	pf = parsefile;
500	while (pf != NULL) {		/* don't need to stop at basepf */
501		if (pf->fd == from)
502			pf->fd = to;
503		pf = pf->prev;
504	}
505}
506
507/*
508 * Like setinputfile, but takes an open file descriptor.  Call this with
509 * interrupts off.
510 */
511
512void
513setinputfd(int fd, int push)
514{
515	VTRACE(DBG_INPUT, ("setinputfd(%d, %spush)\n", fd, push?"":"no"));
516
517	register_sh_fd(fd, input_fd_swap);
518	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
519	if (push)
520		pushfile();
521	if (parsefile->fd > 0)
522		sh_close(parsefile->fd);
523	parsefile->fd = fd;
524	if (parsefile->buf == NULL)
525		parsefile->buf = ckmalloc(BUFSIZ);
526	parselleft = parsenleft = 0;
527	plinno = 1;
528
529	CTRACE(DBG_INPUT, ("setinputfd(%d, %spush) done; plinno=1\n", fd,
530	    push ? "" : "no"));
531}
532
533
534/*
535 * Like setinputfile, but takes input from a string.
536 */
537
538void
539setinputstring(char *string, int push, int line1)
540{
541
542	INTOFF;
543	if (push)		/* XXX: always, as it happens */
544		pushfile();
545	parsenextc = string;
546	parselleft = parsenleft = strlen(string);
547	plinno = line1;
548
549	CTRACE(DBG_INPUT,
550	    ("setinputstring(\"%.20s%s\" (%d), %spush, @ %d)\n", string,
551	    (parsenleft > 20 ? "..." : ""), parsenleft, push?"":"no", line1));
552	INTON;
553}
554
555
556
557/*
558 * To handle the "." command, a stack of input files is used.  Pushfile
559 * adds a new entry to the stack and popfile restores the previous level.
560 */
561
562STATIC void
563pushfile(void)
564{
565	struct parsefile *pf;
566
567	VTRACE(DBG_INPUT,
568	    ("pushfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno=%d\n",
569	    parsefile->fd, parsefile->buf, parsenleft, parselleft,
570	    parsenleft, parsenextc, plinno));
571
572	parsefile->nleft = parsenleft;
573	parsefile->lleft = parselleft;
574	parsefile->nextc = parsenextc;
575	parsefile->linno = plinno;
576	pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
577	pf->prev = parsefile;
578	pf->fd = -1;
579	pf->strpush = NULL;
580	pf->basestrpush.prev = NULL;
581	pf->buf = NULL;
582	parsefile = pf;
583}
584
585
586void
587popfile(void)
588{
589	struct parsefile *pf = parsefile;
590
591	INTOFF;
592	if (pf->fd >= 0)
593		sh_close(pf->fd);
594	if (pf->buf)
595		ckfree(pf->buf);
596	while (pf->strpush)
597		popstring();
598	parsefile = pf->prev;
599	ckfree(pf);
600	parsenleft = parsefile->nleft;
601	parselleft = parsefile->lleft;
602	parsenextc = parsefile->nextc;
603
604	VTRACE(DBG_INPUT,
605	    ("popfile(): fd=%d buf=%p nl=%d ll=%d \"%.*s\" plinno:%d->%d\n",
606	    parsefile->fd, parsefile->buf, parsenleft, parselleft,
607	    parsenleft, parsenextc, plinno, parsefile->linno));
608
609	plinno = parsefile->linno;
610	INTON;
611}
612
613/*
614 * Return current file (to go back to it later using popfilesupto()).
615 */
616
617struct parsefile *
618getcurrentfile(void)
619{
620	return parsefile;
621}
622
623
624/*
625 * Pop files until the given file is on top again. Useful for regular
626 * builtins that read shell commands from files or strings.
627 * If the given file is not an active file, an error is raised.
628 */
629
630void
631popfilesupto(struct parsefile *file)
632{
633	while (parsefile != file && parsefile != &basepf)
634		popfile();
635	if (parsefile != file)
636		error("popfilesupto() misused");
637}
638
639
640/*
641 * Return to top level.
642 */
643
644void
645popallfiles(void)
646{
647	while (parsefile != &basepf)
648		popfile();
649}
650
651
652
653/*
654 * Close the file(s) that the shell is reading commands from.  Called
655 * after a fork is done.
656 *
657 * Takes one arg, vfork, which tells it to not modify its global vars
658 * as it is still running in the parent.
659 *
660 * This code is (probably) unnecessary as the 'close on exec' flag is
661 * set and should be enough.  In the vfork case it is definitely wrong
662 * to close the fds as another fork() may be done later to feed data
663 * from a 'here' document into a pipe and we don't want to close the
664 * pipe!
665 */
666
667void
668closescript(int vforked)
669{
670	if (vforked)
671		return;
672	popallfiles();
673	if (parsefile->fd > 0) {
674		sh_close(parsefile->fd);
675		parsefile->fd = 0;
676	}
677}
678