input.c revision 1.18
1/*	$NetBSD: input.c,v 1.18 1995/10/06 21:38:18 christos 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. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#ifndef lint
40#if 0
41static char sccsid[] = "@(#)input.c	8.3 (Berkeley) 6/9/95";
42#else
43static char rcsid[] = "$NetBSD: input.c,v 1.18 1995/10/06 21:38:18 christos Exp $";
44#endif
45#endif /* not lint */
46
47#include <stdio.h>	/* defines BUFSIZ */
48#include <fcntl.h>
49#include <errno.h>
50#include <unistd.h>
51#include <stdlib.h>
52#include <string.h>
53
54/*
55 * This file implements the input routines used by the parser.
56 */
57
58#include "shell.h"
59#include "redir.h"
60#include "syntax.h"
61#include "input.h"
62#include "output.h"
63#include "options.h"
64#include "memalloc.h"
65#include "error.h"
66#include "alias.h"
67#include "parser.h"
68#include "myhistedit.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	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	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 */
101MKINIT int parsenleft;		/* copy of parsefile->nleft */
102MKINIT int parselleft;		/* copy of parsefile->lleft */
103char *parsenextc;		/* copy of parsefile->nextc */
104MKINIT struct parsefile basepf;	/* top level input file */
105char 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
110EditLine *el;			/* cookie for editline package */
111
112STATIC void pushfile __P((void));
113static int pread __P((void));
114
115#ifdef mkinit
116INCLUDE "input.h"
117INCLUDE "error.h"
118
119INIT {
120	extern char basebuf[];
121
122	basepf.nextc = basepf.buf = basebuf;
123}
124
125RESET {
126	if (exception != EXSHELLPROC)
127		parselleft = parsenleft = 0;	/* clear input buffer */
128	popallfiles();
129}
130
131SHELLPROC {
132	popallfiles();
133}
134#endif
135
136
137/*
138 * Read a line from the script.
139 */
140
141char *
142pfgets(line, len)
143	char *line;
144	int len;
145{
146	register 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()
174{
175	return pgetc_macro();
176}
177
178
179static int
180pread()
181{
182	int nr;
183	parsenextc = parsefile->buf;
184
185retry:
186	if (parsefile->fd == 0 && el) {
187		const char *rl_cp;
188		int len;
189
190		rl_cp = el_gets(el, &nr);
191		if (rl_cp == NULL)
192			nr = 0;
193		else {
194			/* XXX - BUFSIZE should redesign so not necessary */
195			(void) strcpy(parsenextc, rl_cp);
196		}
197	} else {
198		nr = read(parsefile->fd, parsenextc, BUFSIZ - 1);
199	}
200
201	if (nr <= 0) {
202                if (nr < 0) {
203                        if (errno == EINTR)
204                                goto retry;
205                        if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
206                                int flags = fcntl(0, F_GETFL, 0);
207                                if (flags >= 0 && flags & O_NONBLOCK) {
208                                        flags &=~ O_NONBLOCK;
209                                        if (fcntl(0, F_SETFL, flags) >= 0) {
210						out2str("sh: turning off NDELAY mode\n");
211                                                goto retry;
212                                        }
213                                }
214                        }
215                }
216                nr = -1;
217	}
218	return nr;
219}
220
221/*
222 * Refill the input buffer and return the next input character:
223 *
224 * 1) If a string was pushed back on the input, pop it;
225 * 2) If an EOF was pushed back (parsenleft == EOF_NLEFT) or we are reading
226 *    from a string so we can't refill the buffer, return EOF.
227 * 3) If the is more stuff in this buffer, use it else call read to fill it.
228 * 4) Process input up to the next newline, deleting nul characters.
229 */
230
231int
232preadbuffer()
233{
234	char *p, *q;
235	int more;
236	int something;
237	extern EditLine *el;
238	char savec;
239
240	if (parsefile->strpush) {
241		popstring();
242		if (--parsenleft >= 0)
243			return (*parsenextc++);
244	}
245	if (parsenleft == EOF_NLEFT || parsefile->buf == NULL)
246		return PEOF;
247	flushout(&output);
248	flushout(&errout);
249
250again:
251	if (parselleft <= 0) {
252		if ((parselleft = pread()) == -1) {
253			parselleft = parsenleft = EOF_NLEFT;
254			return PEOF;
255		}
256	}
257
258	q = p = parsenextc;
259
260	/* delete nul characters */
261	something = 0;
262	for (more = 1; more;) {
263		switch (*p) {
264		case '\0':
265			p++;	/* Skip nul */
266			goto check;
267
268		case '\t':
269		case ' ':
270			break;
271
272		case '\n':
273			parsenleft = q - parsenextc;
274			more = 0; /* Stop processing here */
275			break;
276
277		default:
278			something = 1;
279			break;
280		}
281
282		*q++ = *p++;
283check:
284		if (--parselleft <= 0) {
285			parsenleft = q - parsenextc - 1;
286			if (parsenleft < 0)
287				goto again;
288			*q = '\0';
289			more = 0;
290		}
291	}
292
293	savec = *q;
294	*q = '\0';
295
296	if (parsefile->fd == 0 && hist && something) {
297		INTOFF;
298		history(hist, whichprompt == 1 ? H_ENTER : H_ADD, parsenextc);
299		INTON;
300	}
301
302	if (vflag) {
303		out2str(parsenextc);
304		flushout(out2);
305	}
306
307	*q = savec;
308
309	return *parsenextc++;
310}
311
312/*
313 * Undo the last call to pgetc.  Only one character may be pushed back.
314 * PEOF may be pushed back.
315 */
316
317void
318pungetc() {
319	parsenleft++;
320	parsenextc--;
321}
322
323/*
324 * Push a string back onto the input at this current parsefile level.
325 * We handle aliases this way.
326 */
327void
328pushstring(s, len, ap)
329	char *s;
330	int len;
331	void *ap;
332	{
333	struct strpush *sp;
334
335	INTOFF;
336/*dprintf("*** calling pushstring: %s, %d\n", s, len);*/
337	if (parsefile->strpush) {
338		sp = ckmalloc(sizeof (struct strpush));
339		sp->prev = parsefile->strpush;
340		parsefile->strpush = sp;
341	} else
342		sp = parsefile->strpush = &(parsefile->basestrpush);
343	sp->prevstring = parsenextc;
344	sp->prevnleft = parsenleft;
345	sp->prevlleft = parselleft;
346	sp->ap = (struct alias *)ap;
347	if (ap)
348		((struct alias *)ap)->flag |= ALIASINUSE;
349	parsenextc = s;
350	parsenleft = len;
351	INTON;
352}
353
354void
355popstring()
356{
357	struct strpush *sp = parsefile->strpush;
358
359	INTOFF;
360	parsenextc = sp->prevstring;
361	parsenleft = sp->prevnleft;
362	parselleft = sp->prevlleft;
363/*dprintf("*** calling popstring: restoring to '%s'\n", parsenextc);*/
364	if (sp->ap)
365		sp->ap->flag &= ~ALIASINUSE;
366	parsefile->strpush = sp->prev;
367	if (sp != &(parsefile->basestrpush))
368		ckfree(sp);
369	INTON;
370}
371
372/*
373 * Set the input to take input from a file.  If push is set, push the
374 * old input onto the stack first.
375 */
376
377void
378setinputfile(fname, push)
379	char *fname;
380	int push;
381{
382	int fd;
383	int fd2;
384
385	INTOFF;
386	if ((fd = open(fname, O_RDONLY)) < 0)
387		error("Can't open %s", fname);
388	if (fd < 10) {
389		fd2 = copyfd(fd, 10);
390		close(fd);
391		if (fd2 < 0)
392			error("Out of file descriptors");
393		fd = fd2;
394	}
395	setinputfd(fd, push);
396	INTON;
397}
398
399
400/*
401 * Like setinputfile, but takes an open file descriptor.  Call this with
402 * interrupts off.
403 */
404
405void
406setinputfd(fd, push)
407	int fd, push;
408{
409	if (push) {
410		pushfile();
411		parsefile->buf = ckmalloc(BUFSIZ);
412	}
413	if (parsefile->fd > 0)
414		close(parsefile->fd);
415	parsefile->fd = fd;
416	if (parsefile->buf == NULL)
417		parsefile->buf = ckmalloc(BUFSIZ);
418	parselleft = parsenleft = 0;
419	plinno = 1;
420}
421
422
423/*
424 * Like setinputfile, but takes input from a string.
425 */
426
427void
428setinputstring(string, push)
429	char *string;
430	int push;
431	{
432	INTOFF;
433	if (push)
434		pushfile();
435	parsenextc = string;
436	parselleft = parsenleft = strlen(string);
437	parsefile->buf = NULL;
438	plinno = 1;
439	INTON;
440}
441
442
443
444/*
445 * To handle the "." command, a stack of input files is used.  Pushfile
446 * adds a new entry to the stack and popfile restores the previous level.
447 */
448
449STATIC void
450pushfile() {
451	struct parsefile *pf;
452
453	parsefile->nleft = parsenleft;
454	parsefile->lleft = parselleft;
455	parsefile->nextc = parsenextc;
456	parsefile->linno = plinno;
457	pf = (struct parsefile *)ckmalloc(sizeof (struct parsefile));
458	pf->prev = parsefile;
459	pf->fd = -1;
460	pf->strpush = NULL;
461	pf->basestrpush.prev = NULL;
462	parsefile = pf;
463}
464
465
466void
467popfile() {
468	struct parsefile *pf = parsefile;
469
470	INTOFF;
471	if (pf->fd >= 0)
472		close(pf->fd);
473	if (pf->buf)
474		ckfree(pf->buf);
475	while (pf->strpush)
476		popstring();
477	parsefile = pf->prev;
478	ckfree(pf);
479	parsenleft = parsefile->nleft;
480	parselleft = parsefile->lleft;
481	parsenextc = parsefile->nextc;
482	plinno = parsefile->linno;
483	INTON;
484}
485
486
487/*
488 * Return to top level.
489 */
490
491void
492popallfiles() {
493	while (parsefile != &basepf)
494		popfile();
495}
496
497
498
499/*
500 * Close the file(s) that the shell is reading commands from.  Called
501 * after a fork is done.
502 */
503
504void
505closescript() {
506	popallfiles();
507	if (parsefile->fd > 0) {
508		close(parsefile->fd);
509		parsefile->fd = 0;
510	}
511}
512