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